redox/kernel/syscall/fs.rs

237 lines
7.3 KiB
Rust
Raw Normal View History

2016-08-14 23:58:35 +02:00
//! Filesystem syscalls
2016-08-18 16:30:45 +02:00
use context;
use scheme;
2016-10-06 02:01:05 +02:00
use syscall::data::Packet;
use syscall::error::*;
2016-08-14 23:58:35 +02:00
2016-09-25 19:20:59 +02:00
pub fn file_op(a: usize, fd: usize, c: usize, d: usize) -> Result<usize> {
2016-10-05 23:43:35 +02:00
let (file, pid, uid, gid) = {
2016-09-25 19:20:59 +02:00
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
let context = context_lock.read();
let file = context.get_file(fd).ok_or(Error::new(EBADF))?;
2016-10-05 23:43:35 +02:00
(file, context.id, context.uid, context.gid)
2016-09-25 19:20:59 +02:00
};
let scheme = {
let schemes = scheme::schemes();
let scheme = schemes.get(file.scheme).ok_or(Error::new(EBADF))?;
scheme.clone()
};
let mut packet = Packet {
id: 0,
2016-10-05 23:43:35 +02:00
pid: pid,
uid: uid,
gid: gid,
2016-09-25 19:20:59 +02:00
a: a,
b: file.number,
c: c,
d: d
};
scheme.handle(&mut packet);
Error::demux(packet.a)
}
pub fn file_op_slice(a: usize, fd: usize, slice: &[u8]) -> Result<usize> {
file_op(a, fd, slice.as_ptr() as usize, slice.len())
}
pub fn file_op_mut_slice(a: usize, fd: usize, slice: &mut [u8]) -> Result<usize> {
file_op(a, fd, slice.as_mut_ptr() as usize, slice.len())
}
/// Change the current working directory
pub fn chdir(path: &[u8]) -> Result<usize> {
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
let context = context_lock.read();
let canonical = context.canonicalize(path);
*context.cwd.lock() = canonical;
Ok(0)
}
/// Get the current working directory
pub fn getcwd(buf: &mut [u8]) -> Result<usize> {
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
let context = context_lock.read();
let cwd = context.cwd.lock();
let mut i = 0;
while i < buf.len() && i < cwd.len() {
buf[i] = cwd[i];
i += 1;
}
Ok(i)
}
2016-08-14 23:58:35 +02:00
/// Open syscall
pub fn open(path: &[u8], flags: usize) -> Result<usize> {
2016-10-06 02:01:05 +02:00
let (path_canon, uid, gid) = {
2016-09-18 20:54:10 +02:00
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
2016-09-18 20:54:10 +02:00
let context = context_lock.read();
2016-10-06 02:01:05 +02:00
(context.canonicalize(path), context.uid, context.gid)
2016-09-18 20:54:10 +02:00
};
let mut parts = path_canon.splitn(2, |&b| b == b':');
2016-08-16 19:04:14 +02:00
let namespace_opt = parts.next();
let reference_opt = parts.next();
let (scheme_id, file_id) = {
let namespace = namespace_opt.ok_or(Error::new(ENOENT))?;
let (scheme_id, scheme) = {
let schemes = scheme::schemes();
let (scheme_id, scheme) = schemes.get_name(namespace).ok_or(Error::new(ENOENT))?;
(scheme_id, scheme.clone())
};
2016-10-06 02:01:05 +02:00
let file_id = scheme.open(reference_opt.unwrap_or(b""), flags, uid, gid)?;
(scheme_id, file_id)
2016-09-09 03:31:26 +02:00
};
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
2016-09-18 20:54:10 +02:00
let context = context_lock.read();
2016-09-09 03:31:26 +02:00
context.add_file(::context::file::File {
scheme: scheme_id,
number: file_id
}).ok_or(Error::new(EMFILE))
2016-08-14 23:58:35 +02:00
}
2016-09-28 19:26:49 +02:00
/// mkdir syscall
2016-10-06 02:01:05 +02:00
pub fn mkdir(path: &[u8], mode: u16) -> Result<usize> {
let (path_canon, uid, gid) = {
2016-09-28 19:26:49 +02:00
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
let context = context_lock.read();
2016-10-06 02:01:05 +02:00
(context.canonicalize(path), context.uid, context.gid)
2016-09-28 19:26:49 +02:00
};
let mut parts = path_canon.splitn(2, |&b| b == b':');
let namespace_opt = parts.next();
let reference_opt = parts.next();
let namespace = namespace_opt.ok_or(Error::new(ENOENT))?;
let scheme = {
let schemes = scheme::schemes();
let (_scheme_id, scheme) = schemes.get_name(namespace).ok_or(Error::new(ENOENT))?;
scheme.clone()
};
2016-10-06 02:01:05 +02:00
scheme.mkdir(reference_opt.unwrap_or(b""), mode, uid, gid)
2016-09-28 19:26:49 +02:00
}
/// rmdir syscall
pub fn rmdir(path: &[u8]) -> Result<usize> {
2016-10-06 02:01:05 +02:00
let (path_canon, uid, gid) = {
2016-09-28 19:26:49 +02:00
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
let context = context_lock.read();
2016-10-06 02:01:05 +02:00
(context.canonicalize(path), context.uid, context.gid)
2016-09-28 19:26:49 +02:00
};
let mut parts = path_canon.splitn(2, |&b| b == b':');
let namespace_opt = parts.next();
let reference_opt = parts.next();
let namespace = namespace_opt.ok_or(Error::new(ENOENT))?;
let scheme = {
let schemes = scheme::schemes();
let (_scheme_id, scheme) = schemes.get_name(namespace).ok_or(Error::new(ENOENT))?;
scheme.clone()
};
2016-10-06 02:01:05 +02:00
scheme.rmdir(reference_opt.unwrap_or(b""), uid, gid)
2016-09-28 19:26:49 +02:00
}
2016-09-28 19:18:28 +02:00
/// Unlink syscall
pub fn unlink(path: &[u8]) -> Result<usize> {
2016-10-06 02:01:05 +02:00
let (path_canon, uid, gid) = {
2016-09-28 19:18:28 +02:00
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
let context = context_lock.read();
2016-10-06 02:01:05 +02:00
(context.canonicalize(path), context.uid, context.gid)
2016-09-28 19:18:28 +02:00
};
let mut parts = path_canon.splitn(2, |&b| b == b':');
let namespace_opt = parts.next();
let reference_opt = parts.next();
let namespace = namespace_opt.ok_or(Error::new(ENOENT))?;
let scheme = {
let schemes = scheme::schemes();
let (_scheme_id, scheme) = schemes.get_name(namespace).ok_or(Error::new(ENOENT))?;
scheme.clone()
};
2016-10-06 02:01:05 +02:00
scheme.unlink(reference_opt.unwrap_or(b""), uid, gid)
2016-09-28 19:18:28 +02:00
}
2016-08-14 23:58:35 +02:00
/// Close syscall
pub fn close(fd: usize) -> Result<usize> {
let file = {
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
2016-09-18 20:54:10 +02:00
let context = context_lock.read();
let file = context.remove_file(fd).ok_or(Error::new(EBADF))?;
file
};
context::event::unregister(fd, file.scheme, file.number);
let scheme = {
let schemes = scheme::schemes();
let scheme = schemes.get(file.scheme).ok_or(Error::new(EBADF))?;
scheme.clone()
};
scheme.close(file.number)
2016-08-14 23:58:35 +02:00
}
2016-09-12 01:31:21 +02:00
/// Duplicate file descriptor
pub fn dup(fd: usize) -> Result<usize> {
let file = {
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
2016-09-12 01:31:21 +02:00
let context = context_lock.read();
let file = context.get_file(fd).ok_or(Error::new(EBADF))?;
2016-09-12 01:31:21 +02:00
file
};
2016-09-28 19:22:01 +02:00
let new_id = {
let scheme = {
let schemes = scheme::schemes();
let scheme = schemes.get(file.scheme).ok_or(Error::new(EBADF))?;
scheme.clone()
};
scheme.dup(file.number)?
};
2016-09-28 19:22:01 +02:00
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
let context = context_lock.read();
context.add_file(::context::file::File {
scheme: file.scheme,
number: new_id
}).ok_or(Error::new(EMFILE))
2016-09-12 01:31:21 +02:00
}
2016-09-23 23:47:53 +02:00
/// Register events for file
pub fn fevent(fd: usize, flags: usize) -> Result<usize> {
let file = {
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
let context = context_lock.read();
let file = context.get_file(fd).ok_or(Error::new(EBADF))?;
file
};
let scheme = {
let schemes = scheme::schemes();
let scheme = schemes.get(file.scheme).ok_or(Error::new(EBADF))?;
scheme.clone()
};
scheme.fevent(file.number, flags)?;
context::event::register(fd, file.scheme, file.number);
Ok(0)
2016-09-23 23:47:53 +02:00
}