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

Make env: return ENOENT on non-existent; support unlink() #25

Merged
merged 1 commit into from
Jun 20, 2017
Merged
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
25 changes: 22 additions & 3 deletions src/scheme/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use spin::{Mutex, RwLock};
use context;
use syscall::data::Stat;
use syscall::error::*;
use syscall::flag::{MODE_FILE, SEEK_SET, SEEK_CUR, SEEK_END};
use syscall::flag::{MODE_FILE, SEEK_SET, SEEK_CUR, SEEK_END, O_CREAT};
use syscall::scheme::Scheme;

#[derive(Clone)]
Expand All @@ -32,7 +32,7 @@ impl EnvScheme {
}

impl Scheme for EnvScheme {
fn open(&self, path: &[u8], _flags: usize, _uid: u32, _gid: u32) -> Result<usize> {
fn open(&self, path: &[u8], flags: usize, _uid: u32, _gid: u32) -> Result<usize> {
let path = str::from_utf8(path).or(Err(Error::new(ENOENT)))?.trim_matches('/');

let env_lock = {
Expand Down Expand Up @@ -69,11 +69,13 @@ impl Scheme for EnvScheme {
let mut env = env_lock.lock();
if env.contains_key(path.as_bytes()) {
env[path.as_bytes()].clone()
} else /*if flags & O_CREAT == O_CREAT*/ {
} else if flags & O_CREAT == O_CREAT {
let name = path.as_bytes().to_vec().into_boxed_slice();
let data = Arc::new(Mutex::new(Vec::new()));
env.insert(name, data.clone());
data
} else {
return Err(Error::new(ENOENT));
}
};

Expand Down Expand Up @@ -211,4 +213,21 @@ impl Scheme for EnvScheme {
fn close(&self, id: usize) -> Result<usize> {
self.handles.write().remove(&id).ok_or(Error::new(EBADF)).and(Ok(0))
}

fn unlink(&self, path: &[u8], uid: u32, _gid: u32) -> Result<usize> {
let env_lock = {
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
let context = context_lock.read();
context.env.clone()
};

let mut env = env_lock.lock();

if let Some(_) = env.remove(path) {
Ok(0)
} else {
Err(Error::new(ENOENT))
}
}
}