1 /*
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 
32 namespace android {
33 namespace 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  */
40 class CacheTracker {
41 public:
42     CacheTracker(userid_t userId, appid_t appId, const std::string& uuid);
43     ~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 
61 private:
62     userid_t mUserId;
63     appid_t mAppId;
64     bool mItemsLoaded;
65     const std::string& mUuid;
66 
67     std::vector<std::string> mDataPaths;
68 
69     bool loadQuotaStats();
70     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
79