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 STORAGE_MANAGER_H
18 #define STORAGE_MANAGER_H
19 
20 #include <android/util/ProtoOutputStream.h>
21 #include <utils/Log.h>
22 #include <utils/RefBase.h>
23 
24 #include "packages/UidMap.h"
25 
26 namespace android {
27 namespace os {
28 namespace statsd {
29 
30 using android::util::ProtoOutputStream;
31 
32 class StorageManager : public virtual RefBase {
33 public:
34     /**
35      * Writes a given byte array as a file to the specified file path.
36      */
37     static void writeFile(const char* file, const void* buffer, int numBytes);
38 
39     /**
40      * Reads the file content to the buffer.
41      */
42     static bool readFileToString(const char* file, string* content);
43 
44     /**
45      * Deletes a single file given a file name.
46      */
47     static void deleteFile(const char* file);
48 
49     /**
50      * Deletes all files in a given directory.
51      */
52     static void deleteAllFiles(const char* path);
53 
54     /**
55      * Deletes all files whose name matches with a provided suffix.
56      */
57     static void deleteSuffixedFiles(const char* path, const char* suffix);
58 
59     /**
60      * Send broadcasts to relevant receiver for each data stored on disk.
61      */
62     static void sendBroadcast(const char* path,
63                               const std::function<void(const ConfigKey&)>& sendBroadcast);
64 
65     /**
66      * Returns true if there's at least one report on disk.
67      */
68     static bool hasConfigMetricsReport(const ConfigKey& key);
69 
70     /**
71      * Appends ConfigMetricsReport found on disk to the specific proto and
72      * delete it.
73      */
74     static void appendConfigMetricsReport(const ConfigKey& key, ProtoOutputStream* proto);
75 
76     /**
77      * Call to load the saved configs from disk.
78      */
79     static void readConfigFromDisk(std::map<ConfigKey, StatsdConfig>& configsMap);
80 
81     /**
82      * Call to load the specified config from disk. Returns false if the config file does not
83      * exist or error occurs when reading the file.
84      */
85     static bool readConfigFromDisk(const ConfigKey& key, StatsdConfig* config);
86     static bool readConfigFromDisk(const ConfigKey& key, string* config);
87 
88     /**
89      * Trims files in the provided directory to limit the total size, number of
90      * files, accumulation of outdated files.
91      */
92     static void trimToFit(const char* dir);
93 
94     /**
95      * Returns true if there already exists identical configuration on device.
96      */
97     static bool hasIdenticalConfig(const ConfigKey& key,
98                                    const vector<uint8_t>& config);
99 
100     /**
101      * Prints disk usage statistics related to statsd.
102      */
103     static void printStats(FILE* out);
104 
105 private:
106     /**
107      * Prints disk usage statistics about a directory related to statsd.
108      */
109     static void printDirStats(FILE* out, const char* path);
110 };
111 
112 }  // namespace statsd
113 }  // namespace os
114 }  // namespace android
115 
116 #endif  // STORAGE_MANAGER_H
117