diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-03-27 16:42:30 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-03-27 16:42:30 +0100 |
commit | 0058da734e32bdd2e5111bfd3eee55d60acaa269 (patch) | |
tree | 694575dc535ea8a9a979ffcc04bf9f3dae46b6f4 | |
parent | e9f2cc359521b7be6a6a7655c525bbd796135362 (diff) | |
download | serenity-0058da734e32bdd2e5111bfd3eee55d60acaa269.zip |
Kernel: Add Inode::truncate(size).
- Use this to implement the O_TRUNC open flag.
- Fix creat() to pass O_CREAT | O_TRUNC | O_WRONLY.
- Make sure we truncate wherever appropriate.
-rw-r--r-- | Kernel/Ext2FileSystem.cpp | 10 | ||||
-rw-r--r-- | Kernel/Ext2FileSystem.h | 1 | ||||
-rw-r--r-- | Kernel/FileSystem.h | 1 | ||||
-rw-r--r-- | Kernel/VirtualFileSystem.cpp | 12 | ||||
-rw-r--r-- | LibC/unistd.cpp | 2 | ||||
-rw-r--r-- | LibGUI/GTextEditor.cpp | 2 | ||||
-rw-r--r-- | SharedGraphics/Font.cpp | 2 | ||||
-rw-r--r-- | Userland/cp.cpp | 4 |
8 files changed, 26 insertions, 8 deletions
diff --git a/Kernel/Ext2FileSystem.cpp b/Kernel/Ext2FileSystem.cpp index 114176ce65..c1a91da68f 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 48af843b5d..80f779b278 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 fd58c15720..4c16ba05c0 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 e03efde33c..fa26e8c1a0 100644 --- a/Kernel/VirtualFileSystem.cpp +++ b/Kernel/VirtualFileSystem.cpp @@ -170,7 +170,10 @@ KResultOr<Retained<FileDescriptor>> 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<Retained<FileDescriptor>> 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<Retained<FileDescriptor>> 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<VFS>(), *inode_or_error.value()); + descriptor_or_error.value()->set_original_inode(Badge<VFS>(), *inode); return descriptor_or_error; } - return FileDescriptor::create(*inode_or_error.value()); + if (should_truncate_file) + inode->truncate(0); + return FileDescriptor::create(*inode); } KResultOr<Retained<FileDescriptor>> VFS::create(const String& path, int options, mode_t mode, Inode& base) diff --git a/LibC/unistd.cpp b/LibC/unistd.cpp index 1c140ab88d..92ad156e59 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 3dd3b57774..015b8b6727 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 a6bbb4cc27..a2aea930b5 100644 --- a/SharedGraphics/Font.cpp +++ b/SharedGraphics/Font.cpp @@ -143,7 +143,7 @@ RetainPtr<Font> 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 3ecfae505b..bfc263e0aa 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; |