Truncate file only if needed

This commit is contained in:
LoveSy 2023-05-21 12:09:20 +08:00 committed by John Wu
parent 9fe8741a02
commit f95478f1f1

View File

@ -248,9 +248,12 @@ bool sepolicy::to_file(const char *file) {
return false; 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) if (fd < 0)
return false; return false;
if (struct stat st{}; xfstat(fd, &st) == 0 && st.st_size > 0) {
ftruncate(fd, 0);
}
xwrite(fd, data.buf, data.sz); xwrite(fd, data.buf, data.sz);
close(fd); close(fd);