diff --git a/Kernel/Ext2FileSystem.cpp b/Kernel/Ext2FileSystem.cpp index 114176ce655..c1a91da68fd 100644 --- a/Kernel/Ext2FileSystem.cpp +++ b/Kernel/Ext2FileSystem.cpp @@ -1391,6 +1391,16 @@ KResult Ext2FSInode::chown(uid_t uid, gid_t gid) return KSuccess; } +KResult Ext2FSInode::truncate(int size) +{ + LOCKER(m_lock); + if (m_raw_inode.i_size == size) + return KSuccess; + m_raw_inode.i_size = size; + set_metadata_dirty(true); + return KSuccess; +} + unsigned Ext2FS::total_block_count() const { LOCKER(m_lock); diff --git a/Kernel/Ext2FileSystem.h b/Kernel/Ext2FileSystem.h index 48af843b5d3..80f779b278a 100644 --- a/Kernel/Ext2FileSystem.h +++ b/Kernel/Ext2FileSystem.h @@ -42,6 +42,7 @@ private: virtual size_t directory_entry_count() const override; virtual KResult chmod(mode_t) override; virtual KResult chown(uid_t, gid_t) override; + virtual KResult truncate(int) override; void populate_lookup_cache() const; diff --git a/Kernel/FileSystem.h b/Kernel/FileSystem.h index fd58c15720c..4c16ba05c0f 100644 --- a/Kernel/FileSystem.h +++ b/Kernel/FileSystem.h @@ -103,6 +103,7 @@ public: virtual size_t directory_entry_count() const = 0; virtual KResult chmod(mode_t) = 0; virtual KResult chown(uid_t, gid_t) = 0; + virtual KResult truncate(int) { return KSuccess; } LocalSocket* socket() { return m_socket.ptr(); } const LocalSocket* socket() const { return m_socket.ptr(); } diff --git a/Kernel/VirtualFileSystem.cpp b/Kernel/VirtualFileSystem.cpp index e03efde33c4..fa26e8c1a03 100644 --- a/Kernel/VirtualFileSystem.cpp +++ b/Kernel/VirtualFileSystem.cpp @@ -170,7 +170,10 @@ KResultOr> VFS::open(const String& path, int options, m if (inode_or_error.is_error()) return inode_or_error.error(); - auto metadata = inode_or_error.value()->metadata(); + auto inode = inode_or_error.value(); + auto metadata = inode->metadata(); + + bool should_truncate_file = false; // NOTE: Read permission is a bit weird, since O_RDONLY == 0, // so we check if (NOT write_only OR read_and_write) @@ -183,6 +186,7 @@ KResultOr> VFS::open(const String& path, int options, m return KResult(-EACCES); if (metadata.is_directory()) return KResult(-EISDIR); + should_truncate_file = options & O_TRUNC; } if (metadata.is_device()) { @@ -193,10 +197,12 @@ KResultOr> VFS::open(const String& path, int options, m auto descriptor_or_error = (*it).value->open(options); if (descriptor_or_error.is_error()) return descriptor_or_error.error(); - descriptor_or_error.value()->set_original_inode(Badge(), *inode_or_error.value()); + descriptor_or_error.value()->set_original_inode(Badge(), *inode); return descriptor_or_error; } - return FileDescriptor::create(*inode_or_error.value()); + if (should_truncate_file) + inode->truncate(0); + return FileDescriptor::create(*inode); } KResultOr> VFS::create(const String& path, int options, mode_t mode, Inode& base) diff --git a/LibC/unistd.cpp b/LibC/unistd.cpp index 1c140ab88d1..92ad156e59a 100644 --- a/LibC/unistd.cpp +++ b/LibC/unistd.cpp @@ -146,7 +146,7 @@ pid_t getpgrp() int creat(const char* path, mode_t mode) { - return open(path, O_CREAT, mode); + return open(path, O_CREAT | O_WRONLY | O_TRUNC, mode); } int open(const char* path, int options, ...) diff --git a/LibGUI/GTextEditor.cpp b/LibGUI/GTextEditor.cpp index 3dd3b577743..015b8b6727c 100644 --- a/LibGUI/GTextEditor.cpp +++ b/LibGUI/GTextEditor.cpp @@ -645,7 +645,7 @@ void GTextEditor::Line::truncate(int length) bool GTextEditor::write_to_file(const String& path) { - int fd = open(path.characters(), O_WRONLY | O_CREAT, 0666); + int fd = open(path.characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666); if (fd < 0) { perror("open"); return false; diff --git a/SharedGraphics/Font.cpp b/SharedGraphics/Font.cpp index a6bbb4cc278..a2aea930b51 100644 --- a/SharedGraphics/Font.cpp +++ b/SharedGraphics/Font.cpp @@ -143,7 +143,7 @@ RetainPtr Font::load_from_file(const String& path) bool Font::write_to_file(const String& path) { - int fd = open(path.characters(), O_WRONLY | O_CREAT, 0644); + int fd = creat(path.characters(), 0644); if (fd < 0) { perror("open"); return false; diff --git a/Userland/cp.cpp b/Userland/cp.cpp index 3ecfae505bc..bfc263e0aac 100644 --- a/Userland/cp.cpp +++ b/Userland/cp.cpp @@ -34,7 +34,7 @@ int main(int argc, char** argv) return 1; } - int dst_fd = open(dst_path.characters(), O_WRONLY | O_CREAT, 0666); + int dst_fd = creat(dst_path.characters(), 0666); if (dst_fd < 0) { if (errno != EISDIR) { perror("open dst"); @@ -45,7 +45,7 @@ int main(int argc, char** argv) builder.append('/'); builder.append(FileSystemPath(src_path).basename()); dst_path = builder.to_string(); - dst_fd = open(dst_path.characters(), O_WRONLY | O_CREAT, 0666); + dst_fd = creat(dst_path.characters(), 0666); if (dst_fd < 0) { perror("open dst"); return 1;