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

Fix trailing slash in link #46

Merged
merged 2 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
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
22 changes: 21 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,26 @@ where
C: Display + Send + Sync + 'static,
F: FnOnce() -> C,
{
self.map_err(|error| Into::<Error>::into(error).with_msg(f()))
match self {
Ok(t) => Ok(t),
Err(e) => Err(e).context(f()),
}
}
}

#[allow(dead_code)]
impl<T> WithContext<T> for result::Result<T, Error> {
fn context<C>(self, context: C) -> Result<T>
where
C: Display + Send + Sync + 'static,
{
self.map_err(|error| {
if let Some(ref msg) = error.msg {
let new_msg = format!("{}: {}", context, msg);
error.with_msg(new_msg)
} else {
error.with_msg(context)
}
})
}
}
17 changes: 15 additions & 2 deletions src/filesystem/canonicalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::filesystem::binding::Side;
use crate::filesystem::substitution::Substitutor;
use crate::filesystem::FileSystem;

use super::ext::{PathBufExt, PathExt};

pub trait Canonicalizer {
fn canonicalize<P: AsRef<Path>>(&self, path: P, deref_final: bool) -> Result<PathBuf>;
}
Expand Down Expand Up @@ -59,6 +61,8 @@ impl Canonicalizer for FileSystem {
));
}

let trailing_slash = guest_path.with_trailing_slash();

// build guest_path_new from user_path
let mut guest_path_new = PathBuf::new();

Expand Down Expand Up @@ -133,13 +137,22 @@ impl Canonicalizer for FileSystem {
if let Some(comp) = next_comp {
new_user_path.push(comp);
}
new_user_path.push(it);
it.for_each(|comp| new_user_path.push(comp));
if trailing_slash {
// recover the trailing slash
new_user_path.try_add_trailing_slash();
}
// use new_user_path to call this function again and return
// TODO: Can be optimized by replacing `it`
return self.canonicalize(&new_user_path, deref_final);
}
// we cannot go through a path which is neither a directory nor a symlink
if !is_last_component {
if !is_last_component
|| (is_last_component
&& !file_type.is_dir()
&& !file_type.is_symlink()
&& trailing_slash)
{
return Err(Error::errno_with_msg(
Errno::ENOTDIR,
"when canonicalizing an intermediate path",
Expand Down
27 changes: 27 additions & 0 deletions tests/applets.bats
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,30 @@ function script_test_should_not_follow {
[ "$status" -eq 0 ]

}



function script_test_trailing_slash_in_symlink {
PATH=/bin

cd /tmp/test_trailing_slash_in_symlink

echo "hello world" > file
ln -s file link1
ln -s file/ link2
ln -s file/. link3

[[ "$(stat -L -c "%F" link1 2>&1)" == *"regular file"* ]]
[[ "$(stat -L -c "%F" link2 2>&1)" == *"Not a directory"* ]]
[[ "$(stat -L -c "%F" link3 2>&1)" == *"Not a directory"* ]]

}

@test "test trailing slash in symlink" {
local test_dir="$ROOTFS/tmp/test_trailing_slash_in_symlink"
mkdir "$test_dir"
runp proot-rs --rootfs "$ROOTFS" -- /bin/sh -e -x -c "$(declare -f script_test_trailing_slash_in_symlink); script_test_trailing_slash_in_symlink"
rm -rf "$test_dir"
[ "$status" -eq 0 ]

}