Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl Log for ConsoleLogger {
let _ = write_fmt(format_args!(
"[{:5}.{:06}] {}: {}\r\n",
uptime.as_secs(),
uptime.as_micros(),
uptime.subsec_micros(),
record
.module_path()
.map(|x| x.strip_prefix("moss::").unwrap_or(x))
Expand Down
13 changes: 12 additions & 1 deletion src/fs/reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use async_trait::async_trait;
use core::{cmp::min, pin::Pin};
use libkernel::{
error::Result,
fs::{Inode, SeekFrom},
fs::{Inode, OpenFlags, SeekFrom},
memory::{PAGE_SIZE, address::UA},
};

Expand Down Expand Up @@ -63,6 +63,17 @@ impl FileOps for RegFile {
Ok(total_bytes_read)
}

/// Writes at the cursor, or at the end of the file if it was opened with
/// `O_APPEND`, and advances the cursor past the data written.
async fn write(&mut self, ctx: &mut FileCtx, buf: UA, count: usize) -> Result<usize> {
if ctx.flags.contains(OpenFlags::O_APPEND) {
ctx.pos = self.inode.getattr().await?.size;
}
let bytes_written = self.writeat(buf, count, ctx.pos).await?;
ctx.pos += bytes_written as u64;
Ok(bytes_written)
}

/// Writes data from `buf` to the current file position.
/// The file's cursor is advanced by the number of bytes written.
async fn writeat(&mut self, mut buf: UA, mut count: usize, mut offset: u64) -> Result<usize> {
Expand Down
2 changes: 1 addition & 1 deletion src/fs/syscalls/chdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub async fn sys_getcwd(ctx: &ProcessCtx, buf: UA, len: usize) -> Result<usize>
let slice = cstr.as_bytes_with_nul();

if slice.len() > len {
return Err(KernelError::TooLarge);
return Err(KernelError::RangeError);
}

copy_to_user_slice(slice, buf).await?;
Expand Down
27 changes: 27 additions & 0 deletions usertest/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,3 +612,30 @@ fn test_rust_dir() {
}

register_test!(test_rust_dir);

fn test_o_append() {
use std::io::{Seek, SeekFrom, Write};
let path = "/tmp/o_append_test";
fs::write(path, "first\n").unwrap();
let mut file = fs::OpenOptions::new().append(true).open(path).unwrap();
file.write_all(b"second\n").unwrap();
// O_APPEND writes always go to the end, even after seeking elsewhere.
file.seek(SeekFrom::Start(0)).unwrap();
file.write_all(b"third\n").unwrap();
assert_eq!(fs::read_to_string(path).unwrap(), "first\nsecond\nthird\n");
fs::remove_file(path).unwrap();
}

register_test!(test_o_append);

fn test_getcwd_buffer_too_small() {
let mut buf = [0 as libc::c_char; 1];
let ret = unsafe { libc::getcwd(buf.as_mut_ptr(), buf.len()) };
assert!(ret.is_null());
assert_eq!(
std::io::Error::last_os_error().raw_os_error(),
Some(libc::ERANGE)
);
}

register_test!(test_getcwd_buffer_too_small);
Loading