From aa49419173e860db5a4f4d4c9cbb8182fea394e9 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 26 Nov 2019 14:32:59 +0100 Subject: [PATCH] LibCore: Make CFile::open() truncate when opening something "WriteOnly" Unless we're also opening to append (and/or if the file is required to be a new file), CFile::open(WriteOnly) will now truncate the file. --- Libraries/LibCore/CFile.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Libraries/LibCore/CFile.cpp b/Libraries/LibCore/CFile.cpp index dfa5b2c073..91c0a09258 100644 --- a/Libraries/LibCore/CFile.cpp +++ b/Libraries/LibCore/CFile.cpp @@ -34,6 +34,9 @@ bool CFile::open(CIODevice::OpenMode mode) flags |= O_RDONLY; } else if (mode & CIODevice::WriteOnly) { flags |= O_WRONLY | O_CREAT; + bool should_truncate = !((mode & CIODevice::Append) || (mode & CIODevice::MustBeNew)); + if (should_truncate) + flags |= O_TRUNC; } if (mode & CIODevice::Append) flags |= O_APPEND;