• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     struct FileInfo {
FileInfoFileInfo35         FileInfo(std::string name, bool isHistory, int fileSize, long fileAge)
36             : mFileName(name),
37               mIsHistory(isHistory),
38               mFileSizeBytes(fileSize),
39               mFileAgeSec(fileAge) {
40         }
41         std::string mFileName;
42         bool mIsHistory;
43         int mFileSizeBytes;
44         long mFileAgeSec;
45     };
46 
47     /**
48      * Writes a given byte array as a file to the specified file path.
49      */
50     static void writeFile(const char* file, const void* buffer, int numBytes);
51 
52     /**
53      * Writes train info.
54      */
55     static bool writeTrainInfo(int64_t trainVersionCode, const std::string& trainName,
56                                int32_t status, const std::vector<int64_t>& experimentIds);
57 
58     /**
59      * Reads train info.
60      */
61     static bool readTrainInfo(InstallTrainInfo& trainInfo);
62 
63     /**
64      * Reads the file content to the buffer.
65      */
66     static bool readFileToString(const char* file, string* content);
67 
68     /**
69      * Deletes a single file given a file name.
70      */
71     static void deleteFile(const char* file);
72 
73     /**
74      * Deletes all files in a given directory.
75      */
76     static void deleteAllFiles(const char* path);
77 
78     /**
79      * Deletes all files whose name matches with a provided suffix.
80      */
81     static void deleteSuffixedFiles(const char* path, const char* suffix);
82 
83     /**
84      * Send broadcasts to relevant receiver for each data stored on disk.
85      */
86     static void sendBroadcast(const char* path,
87                               const std::function<void(const ConfigKey&)>& sendBroadcast);
88 
89     /**
90      * Returns true if there's at least one report on disk.
91      */
92     static bool hasConfigMetricsReport(const ConfigKey& key);
93 
94     /**
95      * Appends the ConfigMetricsReport found on disk to the specifid proto
96      * and, if erase_data, deletes it from disk.
97      *
98      * [isAdb]: if the caller is adb dump. This includes local adb dump or dumpsys by
99      * bugreport or incidentd. When true, we will append any local history data too.
100      *
101      * When
102      * erase_data=true, isAdb=true:   append history data to output, remove all data after read
103      * erase_data=false, isAdb=true:  append history data to output, keep data after read
104      * erase_data=true, isAdb=false:  do not append history data, and remove data after read
105      * erase_data=false, isAdb=false: do not append history data and *rename* all data files to
106      *                                history files.
107      */
108     static void appendConfigMetricsReport(const ConfigKey& key, ProtoOutputStream* proto,
109                                           bool erase_data, bool isAdb);
110 
111     /**
112      * Call to load the saved configs from disk.
113      */
114     static void readConfigFromDisk(std::map<ConfigKey, StatsdConfig>& configsMap);
115 
116     /**
117      * Call to load the specified config from disk. Returns false if the config file does not
118      * exist or error occurs when reading the file.
119      */
120     static bool readConfigFromDisk(const ConfigKey& key, StatsdConfig* config);
121     static bool readConfigFromDisk(const ConfigKey& key, string* config);
122 
123     /**
124      * Trims files in the provided directory to limit the total size, number of
125      * files, accumulation of outdated files.
126      */
127     static void trimToFit(const char* dir);
128 
129     /**
130      * Returns true if there already exists identical configuration on device.
131      */
132     static bool hasIdenticalConfig(const ConfigKey& key,
133                                    const vector<uint8_t>& config);
134 
135     /**
136      * Prints disk usage statistics related to statsd.
137      */
138     static void printStats(int out);
139 
140     static string getDataFileName(long wallClockSec, int uid, int64_t id);
141 
142     static string getDataHistoryFileName(long wallClockSec, int uid, int64_t id);
143 
144     static void sortFiles(vector<FileInfo>* fileNames);
145 
146 private:
147     /**
148      * Prints disk usage statistics about a directory related to statsd.
149      */
150     static void printDirStats(int out, const char* path);
151 
152     static std::mutex sTrainInfoMutex;
153 };
154 
155 }  // namespace statsd
156 }  // namespace os
157 }  // namespace android
158 
159 #endif  // STORAGE_MANAGER_H
160