Kernel: Support O_APPEND

As per the manpage, this acts as a transparent lseek() before write.
This commit is contained in:
Robin Burchell 2019-05-26 01:18:04 +02:00 committed by Andreas Kling
parent 90dbf689c0
commit c6e79bd53a
Notes: sideshowbarker 2024-07-19 13:57:05 +09:00
3 changed files with 13 additions and 2 deletions

View File

@ -75,6 +75,7 @@ Retained<FileDescriptor> FileDescriptor::clone()
ASSERT(descriptor);
descriptor->m_current_offset = m_current_offset;
descriptor->m_is_blocking = m_is_blocking;
descriptor->m_should_append = m_should_append;
descriptor->m_file_flags = m_file_flags;
return *descriptor;
}

View File

@ -70,6 +70,8 @@ public:
bool is_blocking() const { return m_is_blocking; }
void set_blocking(bool b) { m_is_blocking = b; }
bool should_append() const { return m_should_append; }
void set_should_append(bool s) { m_should_append = s; }
dword file_flags() const { return m_file_flags; }
void set_file_flags(dword flags) { m_file_flags = flags; }
@ -113,6 +115,7 @@ private:
dword m_file_flags { 0 };
bool m_is_blocking { true };
bool m_should_append { false };
SocketRole m_socket_role { SocketRole::None };
FIFO::Direction m_fifo_direction { FIFO::Direction::Neither };
};

View File

@ -861,6 +861,13 @@ ssize_t Process::do_write(FileDescriptor& descriptor, const byte* data, int data
return -EAGAIN;
}
if (descriptor.should_append()) {
#ifdef IO_DEBUG
dbgprintf("seeking to end (O_APPEND)\n");
#endif
descriptor.seek(0, SEEK_END);
}
while (nwritten < data_size) {
#ifdef IO_DEBUG
dbgprintf("while %u < %u\n", nwritten, size);
@ -1118,8 +1125,8 @@ int Process::sys$open(const char* path, int options, mode_t mode)
auto descriptor = result.value();
if (options & O_DIRECTORY && !descriptor->is_directory())
return -ENOTDIR; // FIXME: This should be handled by VFS::open.
if (options & O_NONBLOCK)
descriptor->set_blocking(false);
descriptor->set_blocking(!(options & O_NONBLOCK));
descriptor->set_should_append(options & O_APPEND);
dword flags = (options & O_CLOEXEC) ? FD_CLOEXEC : 0;
m_fds[fd].set(move(descriptor), flags);
return fd;