From f95478f1f146eef1615504ea425412580eb1eff5 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Sun, 21 May 2023 12:09:20 +0800 Subject: [PATCH] Truncate file only if needed --- native/src/sepolicy/policydb.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/native/src/sepolicy/policydb.cpp b/native/src/sepolicy/policydb.cpp index 540990616..b6525cbcb 100644 --- a/native/src/sepolicy/policydb.cpp +++ b/native/src/sepolicy/policydb.cpp @@ -248,9 +248,12 @@ bool sepolicy::to_file(const char *file) { return false; } - int fd = xopen(file, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); + int fd = xopen(file, O_WRONLY | O_CREAT | O_CLOEXEC, 0644); if (fd < 0) return false; + if (struct stat st{}; xfstat(fd, &st) == 0 && st.st_size > 0) { + ftruncate(fd, 0); + } xwrite(fd, data.buf, data.sz); close(fd);