Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File redirections #87

Merged
merged 4 commits into from
Sep 25, 2021
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
68 changes: 68 additions & 0 deletions yash-env/src/virtual_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ impl VirtualSystem {
offset: 0,
is_readable: true,
is_writable: true,
is_appending: true,
})),
cloexec: false,
};
Expand Down Expand Up @@ -245,6 +246,9 @@ impl System for VirtualSystem {
if option.contains(OFlag::O_EXCL) {
return Err(Errno::EEXIST);
}
if option.contains(OFlag::O_TRUNC) {
inode.borrow_mut().content.clear();
}
Rc::clone(inode)
} else {
if !option.contains(OFlag::O_CREAT) {
Expand Down Expand Up @@ -272,6 +276,7 @@ impl System for VirtualSystem {
offset: 0,
is_readable,
is_writable,
is_appending: option.contains(OFlag::O_APPEND),
}));
let body = FdBody {
open_file_description,
Expand Down Expand Up @@ -745,6 +750,69 @@ mod tests {
assert_eq!(second, Err(Errno::EEXIST));
}

#[test]
fn open_truncating() {
let mut system = VirtualSystem::new();
let fd = system
.open(
&CString::new("file").unwrap(),
OFlag::O_WRONLY | OFlag::O_CREAT,
nix::sys::stat::Mode::all(),
)
.unwrap();
system.write(fd, &[1, 2, 3]).unwrap();

let result = system.open(
&CString::new("file").unwrap(),
OFlag::O_WRONLY | OFlag::O_TRUNC,
nix::sys::stat::Mode::empty(),
);
assert_eq!(result, Ok(Fd(4)));

let reader = system
.open(
&CString::new("file").unwrap(),
OFlag::O_RDONLY,
nix::sys::stat::Mode::empty(),
)
.unwrap();
let count = system.read(reader, &mut [0; 1]).unwrap();
assert_eq!(count, 0);
}

#[test]
fn open_appending() {
let mut system = VirtualSystem::new();
let fd = system
.open(
&CString::new("file").unwrap(),
OFlag::O_WRONLY | OFlag::O_CREAT,
nix::sys::stat::Mode::all(),
)
.unwrap();
system.write(fd, &[1, 2, 3]).unwrap();

let result = system.open(
&CString::new("file").unwrap(),
OFlag::O_WRONLY | OFlag::O_APPEND,
nix::sys::stat::Mode::empty(),
);
assert_eq!(result, Ok(Fd(4)));
system.write(Fd(4), &[4, 5, 6]).unwrap();

let reader = system
.open(
&CString::new("file").unwrap(),
OFlag::O_RDONLY,
nix::sys::stat::Mode::empty(),
)
.unwrap();
let mut buffer = [0; 7];
let count = system.read(reader, &mut buffer).unwrap();
assert_eq!(count, 6);
assert_eq!(buffer, [1, 2, 3, 4, 5, 6, 0]);
}

#[test]
fn close() {
let mut system = VirtualSystem::new();
Expand Down
34 changes: 34 additions & 0 deletions yash-env/src/virtual_system/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ pub struct OpenFile {
pub is_readable: bool,
/// Whether this file is opened for writing.
pub is_writable: bool,
/// Whether this file is opened for appending.
pub is_appending: bool,
}

impl PartialEq for OpenFile {
Expand Down Expand Up @@ -125,6 +127,9 @@ impl OpenFileDescription for OpenFile {
let mut file = self.file.borrow_mut();
let len = file.content.len();
let count = buffer.len();
if self.is_appending {
self.offset = len;
}
if self.offset > len {
let zeroes = self.offset - len;
file.content.reserve(zeroes + count);
Expand Down Expand Up @@ -327,6 +332,7 @@ mod tests {
offset: 0,
is_readable: false,
is_writable: false,
is_appending: false,
};

let mut buffer = [0];
Expand All @@ -343,6 +349,7 @@ mod tests {
offset: 1,
is_readable: true,
is_writable: false,
is_appending: false,
};

let mut buffer = [0];
Expand All @@ -365,6 +372,7 @@ mod tests {
offset: 1,
is_readable: true,
is_writable: false,
is_appending: false,
};

let mut buffer = [0; 3];
Expand All @@ -383,6 +391,7 @@ mod tests {
offset: 1,
is_readable: true,
is_writable: false,
is_appending: false,
};

let mut buffer = [0; 3];
Expand All @@ -399,6 +408,7 @@ mod tests {
offset: 0,
is_readable: false,
is_writable: false,
is_appending: false,
};

let result = open_file.write(&[0]);
Expand All @@ -414,6 +424,7 @@ mod tests {
offset: 1,
is_readable: false,
is_writable: true,
is_appending: false,
};

let result = open_file.write(&[9, 8, 7]);
Expand All @@ -431,6 +442,7 @@ mod tests {
offset: 1,
is_readable: false,
is_writable: true,
is_appending: false,
};

let result = open_file.write(&[9, 8, 7, 6]);
Expand All @@ -448,6 +460,7 @@ mod tests {
offset: 3,
is_readable: false,
is_writable: true,
is_appending: false,
};

let result = open_file.write(&[2, 3]);
Expand All @@ -456,13 +469,32 @@ mod tests {
assert_eq!(open_file.file.borrow().content, [1, 0, 0, 2, 3]);
}

#[test]
fn open_file_write_appending() {
let mut inode = INode::new();
inode.content = vec![1, 2, 3];
let mut open_file = OpenFile {
file: Rc::new(RefCell::new(inode)),
offset: 1,
is_readable: false,
is_writable: true,
is_appending: true,
};

let result = open_file.write(&[4, 5]);
assert_eq!(result, Ok(2));
assert_eq!(open_file.offset, 5);
assert_eq!(open_file.file.borrow().content, [1, 2, 3, 4, 5]);
}

#[test]
fn open_file_seek_set() {
let mut open_file = OpenFile {
file: Rc::new(RefCell::new(INode::new())),
offset: 3,
is_readable: true,
is_writable: true,
is_appending: false,
};

let result = open_file.seek(10, Whence::SeekSet);
Expand All @@ -489,6 +521,7 @@ mod tests {
offset: 5,
is_readable: true,
is_writable: true,
is_appending: false,
};

let result = open_file.seek(10, Whence::SeekCur);
Expand Down Expand Up @@ -517,6 +550,7 @@ mod tests {
offset: 2,
is_readable: true,
is_writable: true,
is_appending: false,
};

let result = open_file.seek(7, Whence::SeekEnd);
Expand Down
Loading