2 updates to storing configs on disk
1. When an existing config is updated, remove the outdated config from
disk to stop accumulating configs.
2. Check disk to remove configs to make sure we delete lingering files
that does not live on memory.
Test: statsd, statsd_test, manual
Change-Id: Iedce4b6eb99a3d36bb5e1d1ccc0d88c84859e8f5
diff --git a/bin/src/config/ConfigManager.cpp b/bin/src/config/ConfigManager.cpp
index 9680e4a..3a0448d 100644
--- a/bin/src/config/ConfigManager.cpp
+++ b/bin/src/config/ConfigManager.cpp
@@ -80,15 +80,15 @@
// Remove from map
mConfigs.erase(it);
- // Remove from disk
- remove_saved_configs(key);
-
// Tell everyone
for (auto& listener : mListeners) {
listener->OnConfigRemoved(key);
}
}
- // If we didn't find it, just quietly ignore it.
+
+ // Remove from disk. There can still be a lingering file on disk so we check
+ // whether or not the config was on memory.
+ remove_saved_configs(key);
}
void ConfigManager::remove_saved_configs(const ConfigKey& key) {
@@ -102,9 +102,7 @@
while ((de = readdir(dir.get()))) {
char* name = de->d_name;
if (name[0] != '.' && strncmp(name, prefix.c_str(), prefix.size()) == 0) {
- if (remove(StringPrintf("%s/%d-%s", STATS_SERVICE_DIR, key.GetUid(),
- key.GetName().c_str())
- .c_str()) != 0) {
+ if (remove(StringPrintf("%s/%s", STATS_SERVICE_DIR, name).c_str()) != 0) {
ALOGD("no file found");
}
}
@@ -212,6 +210,11 @@
void ConfigManager::update_saved_configs(const ConfigKey& key, const StatsdConfig& config) {
mkdir(STATS_SERVICE_DIR, S_IRWXU);
+
+ // If there is a pre-existing config with same key we should first delete it.
+ remove_saved_configs(key);
+
+ // Then we save the latest config.
string file_name = StringPrintf("%s/%d-%s-%ld", STATS_SERVICE_DIR, key.GetUid(),
key.GetName().c_str(), time(nullptr));
int fd = open(file_name.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR);