Add futimens

This commit is contained in:
Jeremy Soller
2017-07-09 15:54:03 -06:00
parent 1f97d8220c
commit 8d66d9ce54
2 changed files with 37 additions and 1 deletions
+26
View File
@@ -1,8 +1,10 @@
use redoxfs::FileSystem;
use std::cmp::{min, max};
use std::mem;
use std::time::{SystemTime, UNIX_EPOCH};
use syscall::data::TimeSpec;
use syscall::error::{Error, Result, EBADF, EINVAL};
use syscall::flag::{O_ACCMODE, O_RDONLY, O_WRONLY, O_RDWR, F_GETFL, F_SETFL};
use syscall::{Stat, SEEK_SET, SEEK_CUR, SEEK_END};
@@ -17,6 +19,7 @@ pub trait Resource {
fn stat(&self, _stat: &mut Stat, fs: &mut FileSystem) -> Result<usize>;
fn sync(&mut self) -> Result<usize>;
fn truncate(&mut self, len: usize, fs: &mut FileSystem) -> Result<usize>;
fn utimens(&mut self, times: &[TimeSpec], fs: &mut FileSystem) -> Result<usize>;
}
pub struct DirResource {
@@ -116,6 +119,10 @@ impl Resource for DirResource {
fn truncate(&mut self, _len: usize, _fs: &mut FileSystem) -> Result<usize> {
Err(Error::new(EBADF))
}
fn utimens(&mut self, _times: &[TimeSpec], _fs: &mut FileSystem) -> Result<usize> {
Err(Error::new(EBADF))
}
}
pub struct FileResource {
@@ -236,4 +243,23 @@ impl Resource for FileResource {
Err(Error::new(EBADF))
}
}
fn utimens(&mut self, times: &[TimeSpec], fs: &mut FileSystem) -> Result<usize> {
if self.flags & O_ACCMODE == O_RDWR || self.flags & O_ACCMODE == O_WRONLY {
if let Some(mtime) = times.get(0) {
let mut node = fs.node(self.block)?;
node.1.mtime = mtime.tv_sec as u64;
node.1.mtime_nsec = mtime.tv_nsec as u32;
fs.write_at(node.0, &node.1)?;
Ok(mem::size_of::<TimeSpec>())
} else {
Ok(0)
}
} else {
Err(Error::new(EBADF))
}
}
}
+11 -1
View File
@@ -8,7 +8,7 @@ use std::str;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
use syscall::data::{Stat, StatVfs};
use syscall::data::{Stat, StatVfs, TimeSpec};
use syscall::error::{Error, Result, EACCES, EEXIST, EISDIR, ENOTDIR, EPERM, ENOENT, EBADF, ELOOP, EINVAL};
use syscall::flag::{O_APPEND, O_CREAT, O_DIRECTORY, O_STAT, O_EXCL, O_TRUNC, O_ACCMODE, O_RDONLY, O_WRONLY, O_RDWR, MODE_PERM, O_SYMLINK, O_NOFOLLOW};
use syscall::scheme::Scheme;
@@ -539,6 +539,16 @@ impl Scheme for FileScheme {
}
}
fn futimens(&self, id: usize, times: &[TimeSpec]) -> Result<usize> {
// println!("Futimens {}, {}", id, times.len());
let mut files = self.files.lock();
if let Some(mut file) = files.get_mut(&id) {
file.utimens(times, &mut self.fs.borrow_mut())
} else {
Err(Error::new(EBADF))
}
}
fn close(&self, id: usize) -> Result<usize> {
// println!("Close {}", id);
let mut files = self.files.lock();