yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 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 | |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 32 | struct TrainInfo { |
| 33 | int64_t trainVersionCode; |
| 34 | std::vector<uint8_t> experimentIds; |
| 35 | }; |
| 36 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 37 | class StorageManager : public virtual RefBase { |
| 38 | public: |
| 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 Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 45 | * Writes train info. |
| 46 | */ |
| 47 | static bool writeTrainInfo(int64_t trainVersionCode, const std::vector<uint8_t>& experimentIds); |
| 48 | |
| 49 | /** |
| 50 | * Reads train info. |
| 51 | */ |
Chenjie Yu | 97dbb20 | 2019-02-13 16:42:04 -0800 | [diff] [blame] | 52 | static bool readTrainInfo(InstallTrainInfo& trainInfo); |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 55 | * Reads the file content to the buffer. |
| 56 | */ |
| 57 | static bool readFileToString(const char* file, string* content); |
| 58 | |
| 59 | /** |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 60 | * Deletes a single file given a file name. |
| 61 | */ |
| 62 | static void deleteFile(const char* file); |
| 63 | |
| 64 | /** |
| 65 | * Deletes all files in a given directory. |
| 66 | */ |
| 67 | static void deleteAllFiles(const char* path); |
| 68 | |
| 69 | /** |
yro | e5f8292 | 2018-01-22 18:37:27 -0800 | [diff] [blame] | 70 | * Deletes all files whose name matches with a provided suffix. |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 71 | */ |
yro | e5f8292 | 2018-01-22 18:37:27 -0800 | [diff] [blame] | 72 | static void deleteSuffixedFiles(const char* path, const char* suffix); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Send broadcasts to relevant receiver for each data stored on disk. |
| 76 | */ |
| 77 | static void sendBroadcast(const char* path, |
| 78 | const std::function<void(const ConfigKey&)>& sendBroadcast); |
| 79 | |
| 80 | /** |
David Chen | 4894490 | 2018-05-03 10:29:11 -0700 | [diff] [blame] | 81 | * Returns true if there's at least one report on disk. |
| 82 | */ |
| 83 | static bool hasConfigMetricsReport(const ConfigKey& key); |
| 84 | |
| 85 | /** |
Bookatz | c71d901 | 2018-12-19 12:28:38 -0800 | [diff] [blame] | 86 | * Appends the ConfigMetricsReport found on disk to the specifid proto |
| 87 | * and, if erase_data, deletes it from disk. |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 88 | */ |
Bookatz | c71d901 | 2018-12-19 12:28:38 -0800 | [diff] [blame] | 89 | static void appendConfigMetricsReport(const ConfigKey& key, |
| 90 | ProtoOutputStream* proto, |
| 91 | bool erase_data); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Call to load the saved configs from disk. |
| 95 | */ |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 96 | static void readConfigFromDisk(std::map<ConfigKey, StatsdConfig>& configsMap); |
yro | 98a2850 | 2018-01-18 17:00:14 -0800 | [diff] [blame] | 97 | |
| 98 | /** |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 99 | * Call to load the specified config from disk. Returns false if the config file does not |
| 100 | * exist or error occurs when reading the file. |
| 101 | */ |
| 102 | static bool readConfigFromDisk(const ConfigKey& key, StatsdConfig* config); |
| 103 | static bool readConfigFromDisk(const ConfigKey& key, string* config); |
| 104 | |
| 105 | /** |
yro | 98a2850 | 2018-01-18 17:00:14 -0800 | [diff] [blame] | 106 | * Trims files in the provided directory to limit the total size, number of |
| 107 | * files, accumulation of outdated files. |
| 108 | */ |
| 109 | static void trimToFit(const char* dir); |
yro | 4490765 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Returns true if there already exists identical configuration on device. |
| 113 | */ |
| 114 | static bool hasIdenticalConfig(const ConfigKey& key, |
| 115 | const vector<uint8_t>& config); |
yro | 665208d | 2018-03-13 18:08:09 -0700 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * Prints disk usage statistics related to statsd. |
| 119 | */ |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 120 | static void printStats(int out); |
yro | 665208d | 2018-03-13 18:08:09 -0700 | [diff] [blame] | 121 | |
| 122 | private: |
| 123 | /** |
| 124 | * Prints disk usage statistics about a directory related to statsd. |
| 125 | */ |
Yao Chen | a80e5c0 | 2018-09-04 13:55:29 -0700 | [diff] [blame] | 126 | static void printDirStats(int out, const char* path); |
Chenjie Yu | 6b1667c | 2019-01-18 10:09:33 -0800 | [diff] [blame] | 127 | |
| 128 | static std::mutex sTrainInfoMutex; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | } // namespace statsd |
| 132 | } // namespace os |
| 133 | } // namespace android |
| 134 | |
| 135 | #endif // STORAGE_MANAGER_H |