Skip to content

Commit ba7ee10

Browse files
bors[bot]Mark McCaskey
and
Mark McCaskey
authored
Merge #2325
2325: Clean up some internal WASI code r=Hywan a=MarkMcCaskey Shout out to @Earthmark on GitHub for the suggestions Comments left on #2247, I completely forgot about the PR and didn't see any of the comments until this morning when I saw that the PR had been shipped. ----- This PR addresses some community feedback by rewording a doc comment and updating the internals of a function using 3 mutable variables to find the best fit to only use 1 mutable variable. This change increases readability of the code by making it obvious that all 3 variables are conveying the same bit of information: that we found a match. Co-authored-by: Mark McCaskey <mark@wasmer.io>
2 parents 7e22191 + 2232338 commit ba7ee10

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

lib/wasi/src/state/mod.rs

+41-17
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ impl WasiFs {
816816
symlink_count += 1;
817817
Kind::Symlink {
818818
base_po_dir: pre_open_dir_fd,
819-
path_to_symlink: relative_path,
819+
path_to_symlink: relative_path.to_owned(),
820820
relative_path: link_value,
821821
}
822822
} else {
@@ -958,15 +958,37 @@ impl WasiFs {
958958
Ok(cur_inode)
959959
}
960960

961-
/// Splits a path into the preopened directory that has the largest prefix of
962-
/// the given path, if such a preopened directory exists, and the rest of the path.
963-
fn path_into_pre_open_and_relative_path(
961+
/// Finds the preopened directory that is the "best match" for the given path and
962+
/// returns a path relative to this preopened directory.
963+
///
964+
/// The "best match" is the preopened directory that has the longest prefix of the
965+
/// given path. For example, given preopened directories [`a`, `a/b`, `a/c`] and
966+
/// the path `a/b/c/file`, we will return the fd corresponding to the preopened
967+
/// directory, `a/b` and the relative path `c/file`.
968+
///
969+
/// In the case of a tie, the later preopened fd is preferred.
970+
fn path_into_pre_open_and_relative_path<'path>(
964971
&self,
965-
path: &Path,
966-
) -> Result<(__wasi_fd_t, PathBuf), __wasi_errno_t> {
967-
let mut max_prefix_len = 0;
968-
let mut max_stripped_path = None;
969-
let mut max_po_fd = None;
972+
path: &'path Path,
973+
) -> Result<(__wasi_fd_t, &'path Path), __wasi_errno_t> {
974+
enum BaseFdAndRelPath<'a> {
975+
None,
976+
BestMatch {
977+
fd: __wasi_fd_t,
978+
rel_path: &'a Path,
979+
max_seen: usize,
980+
},
981+
}
982+
983+
impl<'a> BaseFdAndRelPath<'a> {
984+
const fn max_seen(&self) -> usize {
985+
match self {
986+
Self::None => 0,
987+
Self::BestMatch { max_seen, .. } => *max_seen,
988+
}
989+
}
990+
}
991+
let mut res = BaseFdAndRelPath::None;
970992
// for each preopened directory
971993
for po_fd in &self.preopen_fds {
972994
let po_inode = self.fd_map[po_fd].inode;
@@ -981,17 +1003,19 @@ impl WasiFs {
9811003
let new_prefix_len = po_path.as_os_str().len();
9821004
// we use >= to favor later preopens because we iterate in order
9831005
// whereas WASI libc iterates in reverse to get this behavior.
984-
if new_prefix_len >= max_prefix_len {
985-
max_prefix_len = new_prefix_len;
986-
max_stripped_path = Some(stripped_path);
987-
max_po_fd = Some(*po_fd);
1006+
if new_prefix_len >= res.max_seen() {
1007+
res = BaseFdAndRelPath::BestMatch {
1008+
fd: *po_fd,
1009+
rel_path: stripped_path,
1010+
max_seen: new_prefix_len,
1011+
};
9881012
}
9891013
}
9901014
}
991-
if max_prefix_len == 0 {
992-
Err(__WASI_EINVAL) // this may not make sense depending on where it's called
993-
} else {
994-
Ok((max_po_fd.unwrap(), max_stripped_path.unwrap().to_owned()))
1015+
match res {
1016+
// this error may not make sense depending on where it's called
1017+
BaseFdAndRelPath::None => Err(__WASI_EINVAL),
1018+
BaseFdAndRelPath::BestMatch { fd, rel_path, .. } => Ok((fd, rel_path)),
9951019
}
9961020
}
9971021

0 commit comments

Comments
 (0)