check existance w/o traversing symlinks (#10872)

# Description

Currently `path exists` checks the file/folder's existence by traversing
symlinks. I've added a `-n` switch/flag that disables symlink
traversing, similar to what `path expand -n` does.

## The Long Story (for those interested)

Hello! 👋 While working on one of my scripts, I discovered that the `path
exists` command was traversing symlinks. This meant that even if the
file existed, it would fail if the pointed location didn't exist. To
address this, I've introduced a new `-n` flag, which I borrowed from the
`path expand` command. This addition should make the behavior more
consistent within the *path commands universe*.

## But, is it any useful?
 
```nushell
let compat = /run/media/userX/DriveX/steam/steamapps/compatdata
if "symlink" == ($compat | path expand -n | path type) {}
# to this
if ($compat | path exists -n) {}
```

# User-Facing Changes

Users, will not efect. Unless they use the mentioned `-n` flag/switch.
This commit is contained in:
A. Taha Baki 2024-01-13 23:33:33 +00:00 committed by GitHub
parent 6eb6086823
commit d25be66929
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 2 deletions

View File

@ -1,6 +1,6 @@
use std::path::{Path, PathBuf};
use nu_engine::{current_dir, current_dir_const};
use nu_engine::{current_dir, current_dir_const, CallExt};
use nu_path::expand_path_with;
use nu_protocol::ast::Call;
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
@ -12,6 +12,7 @@ use super::PathSubcommandArguments;
struct Arguments {
pwd: PathBuf,
not_follow_symlink: bool,
}
impl PathSubcommandArguments for Arguments {}
@ -33,6 +34,7 @@ impl Command for SubCommand {
Type::List(Box::new(Type::Bool)),
),
])
.switch("no-symlink", "Do not resolve symbolic links", Some('n'))
.category(Category::Path)
}
@ -59,6 +61,7 @@ If you need to distinguish dirs and files, please use `path type`."#
let head = call.head;
let args = Arguments {
pwd: current_dir(engine_state, stack)?,
not_follow_symlink: call.has_flag(engine_state, stack, "no-symlink")?,
};
// This doesn't match explicit nulls
if matches!(input, PipelineData::Empty) {
@ -79,6 +82,7 @@ If you need to distinguish dirs and files, please use `path type`."#
let head = call.head;
let args = Arguments {
pwd: current_dir_const(working_set)?,
not_follow_symlink: call.has_flag_const(working_set, "no-symlink")?,
};
// This doesn't match explicit nulls
if matches!(input, PipelineData::Empty) {
@ -134,8 +138,22 @@ fn exists(path: &Path, span: Span, args: &Arguments) -> Value {
return Value::bool(false, span);
}
let path = expand_path_with(path, &args.pwd);
let exists = if args.not_follow_symlink {
// symlink_metadata returns true if the file/folder exists
// whether it is a symbolic link or not. Sorry, but returns Err
// in every other scenario including the NotFound
std::fs::symlink_metadata(path).map_or_else(
|e| match e.kind() {
std::io::ErrorKind::NotFound => Ok(false),
_ => Err(e),
},
|_| Ok(true),
)
} else {
path.try_exists()
};
Value::bool(
match path.try_exists() {
match exists {
Ok(exists) => exists,
Err(err) => {
return Value::error(

View File

@ -63,3 +63,30 @@ fn const_path_exists() {
let actual = nu!("const exists = ('~' | path exists); $exists");
assert_eq!(actual.out, "true");
}
#[test]
fn test_check_symlink_exists() {
use nu_test_support::{nu, playground::Playground};
let symlink_target = "symlink_target";
let symlink = "symlink";
Playground::setup("path_exists_5", |dirs, sandbox| {
#[cfg(not(windows))]
std::os::unix::fs::symlink(dirs.test().join(symlink_target), dirs.test().join(symlink))
.unwrap();
#[cfg(windows)]
std::os::windows::fs::symlink_file(
dirs.test().join(symlink_target),
dirs.test().join(symlink),
)
.unwrap();
let false_out = "false".to_string();
let shell_res = nu!(cwd: sandbox.cwd(), "'symlink_target' | path exists");
assert_eq!(false_out, shell_res.out);
let shell_res = nu!(cwd: sandbox.cwd(), "'symlink' | path exists");
assert_eq!(false_out, shell_res.out);
let shell_res = nu!(cwd: sandbox.cwd(), "'symlink' | path exists -n");
assert_eq!("true".to_string(), shell_res.out);
});
}