blob: 88280cf218b31723b727756a355123a7cf867394 [file] [log] [blame]
yro947fbce2017-11-15 22:50:23 -08001/*
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
26namespace android {
27namespace os {
28namespace statsd {
29
30using android::util::ProtoOutputStream;
31
Chenjie Yu6b1667c2019-01-18 10:09:33 -080032struct TrainInfo {
33 int64_t trainVersionCode;
34 std::vector<uint8_t> experimentIds;
35};
36
yro947fbce2017-11-15 22:50:23 -080037class StorageManager : public virtual RefBase {
38public:
39 /**
40 * Writes a given byte array as a file to the specified file path.
41 */
42 static void writeFile(const char* file, const void* buffer, int numBytes);
43
44 /**
Chenjie Yu6b1667c2019-01-18 10:09:33 -080045 * Writes train info.
46 */
Muhammad Qureshif4ca8242019-03-01 09:20:15 -080047 static bool writeTrainInfo(int64_t trainVersionCode, const std::string& trainName,
48 int32_t status, const std::vector<uint8_t>& experimentIds);
Chenjie Yu6b1667c2019-01-18 10:09:33 -080049
50 /**
51 * Reads train info.
52 */
Chenjie Yu97dbb202019-02-13 16:42:04 -080053 static bool readTrainInfo(InstallTrainInfo& trainInfo);
Chenjie Yu6b1667c2019-01-18 10:09:33 -080054
55 /**
Yangster-mac932ecec2018-02-01 10:23:52 -080056 * Reads the file content to the buffer.
57 */
58 static bool readFileToString(const char* file, string* content);
59
60 /**
yro947fbce2017-11-15 22:50:23 -080061 * Deletes a single file given a file name.
62 */
63 static void deleteFile(const char* file);
64
65 /**
66 * Deletes all files in a given directory.
67 */
68 static void deleteAllFiles(const char* path);
69
70 /**
yroe5f82922018-01-22 18:37:27 -080071 * Deletes all files whose name matches with a provided suffix.
yro947fbce2017-11-15 22:50:23 -080072 */
yroe5f82922018-01-22 18:37:27 -080073 static void deleteSuffixedFiles(const char* path, const char* suffix);
yro947fbce2017-11-15 22:50:23 -080074
75 /**
76 * Send broadcasts to relevant receiver for each data stored on disk.
77 */
78 static void sendBroadcast(const char* path,
79 const std::function<void(const ConfigKey&)>& sendBroadcast);
80
81 /**
David Chen48944902018-05-03 10:29:11 -070082 * Returns true if there's at least one report on disk.
83 */
84 static bool hasConfigMetricsReport(const ConfigKey& key);
85
86 /**
Bookatzc71d9012018-12-19 12:28:38 -080087 * Appends the ConfigMetricsReport found on disk to the specifid proto
88 * and, if erase_data, deletes it from disk.
yro947fbce2017-11-15 22:50:23 -080089 */
Bookatzc71d9012018-12-19 12:28:38 -080090 static void appendConfigMetricsReport(const ConfigKey& key,
91 ProtoOutputStream* proto,
92 bool erase_data);
yro947fbce2017-11-15 22:50:23 -080093
94 /**
95 * Call to load the saved configs from disk.
96 */
Yao Chenf09569f2017-12-13 17:00:51 -080097 static void readConfigFromDisk(std::map<ConfigKey, StatsdConfig>& configsMap);
yro98a28502018-01-18 17:00:14 -080098
99 /**
Yangster-macb142cc82018-03-30 15:22:08 -0700100 * Call to load the specified config from disk. Returns false if the config file does not
101 * exist or error occurs when reading the file.
102 */
103 static bool readConfigFromDisk(const ConfigKey& key, StatsdConfig* config);
104 static bool readConfigFromDisk(const ConfigKey& key, string* config);
105
106 /**
yro98a28502018-01-18 17:00:14 -0800107 * Trims files in the provided directory to limit the total size, number of
108 * files, accumulation of outdated files.
109 */
110 static void trimToFit(const char* dir);
yro44907652018-03-12 20:44:05 -0700111
112 /**
113 * Returns true if there already exists identical configuration on device.
114 */
115 static bool hasIdenticalConfig(const ConfigKey& key,
116 const vector<uint8_t>& config);
yro665208d2018-03-13 18:08:09 -0700117
118 /**
119 * Prints disk usage statistics related to statsd.
120 */
Yao Chena80e5c02018-09-04 13:55:29 -0700121 static void printStats(int out);
yro665208d2018-03-13 18:08:09 -0700122
123private:
124 /**
125 * Prints disk usage statistics about a directory related to statsd.
126 */
Yao Chena80e5c02018-09-04 13:55:29 -0700127 static void printDirStats(int out, const char* path);
Chenjie Yu6b1667c2019-01-18 10:09:33 -0800128
129 static std::mutex sTrainInfoMutex;
yro947fbce2017-11-15 22:50:23 -0800130};
131
132} // namespace statsd
133} // namespace os
134} // namespace android
135
136#endif // STORAGE_MANAGER_H