Skip to content

Commit

Permalink
fix #4: "file not found" error string when the file does not exist
Browse files Browse the repository at this point in the history
Signed-off-by: Quentin JEROME <[email protected]>
  • Loading branch information
qjerome committed Sep 18, 2023
1 parent 9d51b87 commit 0cb6c14
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions kunai/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub enum Error {
FileModSinceKernelEvent(&'static str),
#[error("metadata is needed for ebpf path")]
MetadataRequired,
#[error("hash not found")]
HashNotFound,
#[error("file not found")]
FileNotFound,
}

#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -127,6 +127,12 @@ impl Key {
path: &kunai_common::path::Path,
) -> Result<Self, Error> {
let pb = path.to_path_buf();

// checking if the file still exists
if !pb.exists() {
return Err(Error::FileNotFound);
}

let meta = pb.metadata()?;

let mut k = Key {
Expand Down Expand Up @@ -214,25 +220,23 @@ impl Cache {
return Err(Error::UnknownNs(ns_inum));
};

let mut ret = Err(Error::HashNotFound);

// we switch to namespace
entry.ns.enter()?;
let pb = path.to_path_buf();

if pb.exists() {
let key = Key::from_ebpf_path_ref_with_ns(&entry.ns, path)?;
let key = Key::from_ebpf_path_ref_with_ns(&entry.ns, path)?;

if !self.hcache.contains_key(&key) {
let h = Hashes::from_path_ref(pb);
self.hcache.insert(key.clone(), h);
}

// we cannot panic here as we are sure the cache contains value
ret = Ok(self.hcache.get(&key).unwrap().clone());
if !self.hcache.contains_key(&key) {
let h = Hashes::from_path_ref(pb);
self.hcache.insert(key.clone(), h);
}

// we cannot panic here as we are sure the cache contains value
let res = Ok(self.hcache.get(&key).unwrap().clone());

// we must be sure that we restore our namespace
entry.ns.exit().expect("failed to restore namespace");
ret

res
}
}

0 comments on commit 0cb6c14

Please sign in to comment.