Skip to content

Commit

Permalink
Kernel: Support O_APPEND
Browse files Browse the repository at this point in the history
As per the manpage, this acts as a transparent lseek() before write.
  • Loading branch information
rburchell authored and awesomekling committed May 25, 2019
1 parent 90dbf68 commit c6e79bd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions Kernel/FileSystem/FileDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions Kernel/FileSystem/FileDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class FileDescriptor : public Retainable<FileDescriptor> {

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; }
Expand Down Expand Up @@ -113,6 +115,7 @@ class FileDescriptor : public Retainable<FileDescriptor> {
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 };
};
Expand Down
11 changes: 9 additions & 2 deletions Kernel/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c6e79bd

Please sign in to comment.