Skip to content

Commit

Permalink
Merge pull request #25 from ids1024/env
Browse files Browse the repository at this point in the history
Make env: return ENOENT on non-existent; support unlink()
  • Loading branch information
jackpot51 committed Jun 20, 2017
2 parents cd67aab + b9f659d commit 0478e5f
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/scheme/env.rs
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))
}
}
}

0 comments on commit 0478e5f

Please sign in to comment.