summaryrefslogtreecommitdiff
path: root/statsd
diff options
context:
space:
mode:
authorTreehugger Robot <android-test-infra-autosubmit@system.gserviceaccount.com>2024-05-07 23:59:03 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2024-05-07 23:59:03 +0000
commitaa1d086b39b387e4ef874fd2b857253e2dbee6a7 (patch)
tree78b900d2321a7b71db661342b68ba069fa342c9f /statsd
parent19cd60cd533a3abd13bcbf4e67bca7109861d616 (diff)
parent01031cba066c41b00004762de3d39d1dcb98ab82 (diff)
downloadStatsD-aa1d086b39b387e4ef874fd2b857253e2dbee6a7.tar.gz
Merge "Truncate existing files before overwriting." into main
Diffstat (limited to 'statsd')
-rw-r--r--statsd/src/storage/StorageManager.cpp2
-rw-r--r--statsd/src/storage/StorageManager.h3
-rw-r--r--statsd/tests/utils/DbUtils_test.cpp2
3 files changed, 5 insertions, 2 deletions
diff --git a/statsd/src/storage/StorageManager.cpp b/statsd/src/storage/StorageManager.cpp
index 7ca88fe1..26418e1c 100644
--- a/statsd/src/storage/StorageManager.cpp
+++ b/statsd/src/storage/StorageManager.cpp
@@ -133,7 +133,7 @@ static ConfigKey parseDbName(char* name) {
}
void StorageManager::writeFile(const char* file, const void* buffer, int numBytes) {
- int fd = open(file, O_WRONLY | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR);
+ int fd = open(file, O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd == -1) {
VLOG("Attempt to access %s but failed", file);
return;
diff --git a/statsd/src/storage/StorageManager.h b/statsd/src/storage/StorageManager.h
index 0a459a86..ac09a30a 100644
--- a/statsd/src/storage/StorageManager.h
+++ b/statsd/src/storage/StorageManager.h
@@ -56,6 +56,9 @@ public:
/**
* Writes a given byte array as a file to the specified file path.
+ *
+ * A new file is created if the file does not already exist.
+ * If the file already exists, it is cleared and then overwritten.
*/
static void writeFile(const char* file, const void* buffer, int numBytes);
diff --git a/statsd/tests/utils/DbUtils_test.cpp b/statsd/tests/utils/DbUtils_test.cpp
index 9bc1d61a..bbac4b10 100644
--- a/statsd/tests/utils/DbUtils_test.cpp
+++ b/statsd/tests/utils/DbUtils_test.cpp
@@ -327,7 +327,7 @@ TEST_F(DbUtilsTest, TestInsertStringIntegrityCheckFails) {
string err;
EXPECT_TRUE(insert(key, metricId, events, err));
- vector<string> randomData{"1232hasha14125ashfas21512sh31321"};
+ string randomData("1232hasha14125ashfas21512sh31321");
string fileName = StringPrintf("%s/%d_%lld.db", STATS_RESTRICTED_DATA_DIR, key.GetUid(),
(long long)key.GetId());
StorageManager::writeFile(fileName.c_str(), randomData.data(), randomData.size());