blob: d1bdded8b1b0fc7503e141c7a32a20c8d4c01a8e [file] [log] [blame]
Jeff Sharkey88ddd942017-01-17 18:05:54 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "CacheItem.h"
18
19#include <stdint.h>
20#include <inttypes.h>
21
22#include <android-base/logging.h>
23#include <android-base/stringprintf.h>
24
25#include "utils.h"
26
27using android::base::StringPrintf;
28
29namespace android {
30namespace installd {
31
32CacheItem::CacheItem(const std::shared_ptr<CacheItem>& parent, FTSENT* p) : mParent(parent) {
33 level = p->fts_level;
34 directory = S_ISDIR(p->fts_statp->st_mode);
35 size = p->fts_statp->st_blocks * 512;
36 modified = p->fts_statp->st_mtime;
37 mName = p->fts_path;
38}
39
40CacheItem::~CacheItem() {
41}
42
43std::string CacheItem::toString() {
44 return StringPrintf("%s size=%" PRId64 " mod=%ld", buildPath().c_str(), size, modified);
45}
46
47std::string CacheItem::buildPath() {
48 std::string res = mName;
49 std::shared_ptr<CacheItem> parent = mParent;
50 while (parent) {
51 res.insert(0, parent->mName);
52 parent = parent->mParent;
53 }
54 return res;
55}
56
57int CacheItem::purge() {
58 auto path = buildPath();
59 if (directory) {
60 return delete_dir_contents_and_dir(path, true);
61 } else {
62 int res = unlink(path.c_str());
63 if (res != 0) {
64 PLOG(WARNING) << "Failed to unlink " << path;
65 }
66 return res;
67 }
68}
69
70} // namespace installd
71} // namespace android