blob: b0527e7b3f5e153fa69418bcc2f28fb1adc7c3c8 [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#ifndef ANDROID_INSTALLD_CACHE_TRACKER_H
18#define ANDROID_INSTALLD_CACHE_TRACKER_H
19
20#include <memory>
21#include <string>
22#include <queue>
23
24#include <sys/types.h>
25#include <sys/stat.h>
26
27#include <android-base/macros.h>
28#include <cutils/multiuser.h>
29
30#include "CacheItem.h"
31
32namespace android {
33namespace installd {
34
35/**
36 * Cache tracker for a single UID. Each tracker is used in two modes: first
37 * for loading lightweight "stats", and then by loading detailed "items"
38 * which can then be purged to free up space.
39 */
40class CacheTracker {
41public:
Risan5f308262018-10-26 12:06:58 -060042 CacheTracker(userid_t userId, appid_t appId, const std::string& uuid);
Jeff Sharkey88ddd942017-01-17 18:05:54 -070043 ~CacheTracker();
44
45 std::string toString();
46
47 void addDataPath(const std::string& dataPath);
48
49 void loadStats();
50 void loadItems();
51
52 void ensureItems();
53
54 int getCacheRatio();
55
56 int64_t cacheUsed;
57 int64_t cacheQuota;
58
59 std::vector<std::shared_ptr<CacheItem>> items;
60
61private:
62 userid_t mUserId;
63 appid_t mAppId;
Jeff Sharkey88ddd942017-01-17 18:05:54 -070064 bool mItemsLoaded;
Risan5f308262018-10-26 12:06:58 -060065 const std::string& mUuid;
Jeff Sharkey88ddd942017-01-17 18:05:54 -070066
67 std::vector<std::string> mDataPaths;
68
Jeff Sharkeyd712f0c2017-05-03 11:36:42 -060069 bool loadQuotaStats();
Jeff Sharkey88ddd942017-01-17 18:05:54 -070070 void loadItemsFrom(const std::string& path);
71
72 DISALLOW_COPY_AND_ASSIGN(CacheTracker);
73};
74
75} // namespace installd
76} // namespace android
77
78#endif // ANDROID_INSTALLD_CACHE_TRACKER_H