• 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 /**
33  * NOTE: these directories are protected by SELinux, any changes here must also update
34  * the SELinux policies.
35  */
36 #define TRAIN_INFO_DIR "/data/misc/train-info"
37 #define TRAIN_INFO_PATH "/data/misc/train-info/train-info.bin"
38 
39 // Magic word at the start of the train info file, change this if changing the file format
40 const uint32_t TRAIN_INFO_FILE_MAGIC = 0xfb7447bf;
41 
42 class StorageManager : public virtual RefBase {
43 public:
44     struct FileInfo {
FileInfoFileInfo45         FileInfo(std::string name, bool isHistory, int fileSize, long fileAge)
46             : mFileName(name),
47               mIsHistory(isHistory),
48               mFileSizeBytes(fileSize),
49               mFileAgeSec(fileAge) {
50         }
51         std::string mFileName;
52         bool mIsHistory;
53         int mFileSizeBytes;
54         long mFileAgeSec;
55     };
56 
57     /**
58      * Writes a given byte array as a file to the specified file path.
59      */
60     static void writeFile(const char* file, const void* buffer, int numBytes);
61 
62     /**
63      * Writes train info.
64      */
65     static bool writeTrainInfo(const InstallTrainInfo& trainInfo);
66 
67     /**
68      * Reads train info.
69      */
70     static bool readTrainInfo(const std::string& trainName, InstallTrainInfo& trainInfo);
71 
72     /**
73      * Reads train info assuming lock is obtained.
74      */
75     static bool readTrainInfoLocked(const std::string& trainName, InstallTrainInfo& trainInfo);
76 
77     /**
78      * Reads all train info and returns a vector of train info.
79      */
80     static vector<InstallTrainInfo> readAllTrainInfo();
81 
82     /**
83      * Reads the file content to the buffer.
84      */
85     static bool readFileToString(const char* file, string* content);
86 
87     /**
88      * Deletes a single file given a file name.
89      */
90     static void deleteFile(const char* file);
91 
92     /**
93      * Deletes all files in a given directory.
94      */
95     static void deleteAllFiles(const char* path);
96 
97     /**
98      * Deletes all files whose name matches with a provided suffix.
99      */
100     static void deleteSuffixedFiles(const char* path, const char* suffix);
101 
102     /**
103      * Send broadcasts to relevant receiver for each data stored on disk.
104      */
105     static void sendBroadcast(const char* path,
106                               const std::function<void(const ConfigKey&)>& sendBroadcast);
107 
108     /**
109      * Returns true if there's at least one report on disk.
110      */
111     static bool hasConfigMetricsReport(const ConfigKey& key);
112 
113     /**
114      * Appends the ConfigMetricsReport found on disk to the specifid proto
115      * and, if erase_data, deletes it from disk.
116      *
117      * [isAdb]: if the caller is adb dump. This includes local adb dump or dumpsys by
118      * bugreport or incidentd. When true, we will append any local history data too.
119      *
120      * When
121      * erase_data=true, isAdb=true:   append history data to output, remove all data after read
122      * erase_data=false, isAdb=true:  append history data to output, keep data after read
123      * erase_data=true, isAdb=false:  do not append history data, and remove data after read
124      * erase_data=false, isAdb=false: do not append history data and *rename* all data files to
125      *                                history files.
126      */
127     static void appendConfigMetricsReport(const ConfigKey& key, ProtoOutputStream* proto,
128                                           bool erase_data, bool isAdb);
129 
130     /**
131      * Call to load the saved configs from disk.
132      */
133     static void readConfigFromDisk(std::map<ConfigKey, StatsdConfig>& configsMap);
134 
135     /**
136      * Call to load the specified config from disk. Returns false if the config file does not
137      * exist or error occurs when reading the file.
138      */
139     static bool readConfigFromDisk(const ConfigKey& key, StatsdConfig* config);
140     static bool readConfigFromDisk(const ConfigKey& key, string* config);
141 
142     /**
143      * Trims files in the provided directory to limit the total size, number of
144      * files, accumulation of outdated files.
145      */
146     static void trimToFit(const char* dir, bool parseTimestampOnly = false);
147 
148     /**
149      * Returns true if there already exists identical configuration on device.
150      */
151     static bool hasIdenticalConfig(const ConfigKey& key,
152                                    const vector<uint8_t>& config);
153 
154     /**
155      * Prints disk usage statistics related to statsd.
156      */
157     static void printStats(int out);
158 
159     static string getDataFileName(long wallClockSec, int uid, int64_t id);
160 
161     static string getDataHistoryFileName(long wallClockSec, int uid, int64_t id);
162 
163     static void sortFiles(vector<FileInfo>* fileNames);
164 
165     static void enforceDbGuardrails(const char* path, const int64_t wallClockSec,
166                                     const int64_t maxBytes);
167 
168     static bool hasFile(const char* file);
169 
170 private:
171     /**
172      * Prints disk usage statistics about a directory related to statsd.
173      */
174     static void printDirStats(int out, const char* path);
175 
176     static std::mutex sTrainInfoMutex;
177 };
178 
179 }  // namespace statsd
180 }  // namespace os
181 }  // namespace android
182 
183 #endif  // STORAGE_MANAGER_H
184