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

Only absolutize exe if path is a file and executable #671

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions app/buck2_core/src/fs/fs_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,17 @@ pub fn remove_dir_all<P: AsRef<AbsPath>>(path: P) -> Result<(), IoError> {
)
}

/// `None` if file does not exist.
pub fn metadata_if_exists<P: AsRef<AbsPath>>(
path: P,
) -> Result<Option<fs::Metadata>, IoError> {
let _guard = IoCounterKey::Stat.guard();
make_error!(
if_exists(fs::metadata(path.as_ref().as_maybe_relativized())),
format!("metadata({})", path.as_ref().display())
)
}

/// `None` if file does not exist.
pub fn symlink_metadata_if_exists<P: AsRef<AbsPath>>(
path: P,
Expand Down
19 changes: 16 additions & 3 deletions app/buck2_forkserver/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ impl KillProcess for DefaultKillProcess {

/// Unify the the behavior of using a relative path for the executable between Unix and Windows. On
/// UNIX, the path is understood to be relative to the cwd of the *spawned process*, whereas on
/// Windows, it's relative ot the cwd of the *spawning* process.
/// Windows, it's relative to the cwd of the *spawning* process.
///
/// Here, we unify the two behaviors since we always run our subprocesses with a known cwd: we
/// check if the executable actually exists relative to said cwd, and if it does, we use that.
Expand All @@ -352,8 +352,21 @@ pub fn maybe_absolutize_exe<'a>(
let exe = exe.as_ref();

let abs = spawned_process_cwd.join(exe);
if fs_util::try_exists(&abs).context("Error absolute-izing executable")? {
return Ok(abs.into_path_buf().into());

if let Some(metadata) = fs_util::metadata_if_exists(&abs).context("Error getting metadata for path")? {
if metadata.is_file() {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if metadata.permissions().mode() & 0o111 != 0 {
return Ok(abs.into_path_buf().into());
}
}
#[cfg(not(unix))]
{
return Ok(abs.into_path_buf().into());
}
}
}

Ok(exe.into())
Expand Down
Loading