From d99c4cacdf3292f92c2ae9902cadb07bf359dc43 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Fri, 16 Feb 2024 06:51:34 -0600 Subject: [PATCH] Read from `/bin/sh` if `/bin/ls` cannot be found when determing libc path (#1433) I'm not sure if we should just switch to _always_ reading from sh instead? I don't love that all these errors are strings and I if `/bin/ls` exists but can't be parsed we still won't try `/bin/sh`. We may want to address these things in the future. Closes https://github.com/astral-sh/uv/issues/1395 --- crates/platform-host/src/linux.rs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/crates/platform-host/src/linux.rs b/crates/platform-host/src/linux.rs index 46d438a7a..cd698b232 100644 --- a/crates/platform-host/src/linux.rs +++ b/crates/platform-host/src/linux.rs @@ -127,17 +127,27 @@ fn get_musl_version(ld_path: impl AsRef) -> std::io::Result Result { - let buffer = fs::read("/bin/ls")?; - let error_str = "Couldn't parse /bin/ls for detecting the ld version"; - let elf = Elf::parse(&buffer) - .map_err(|err| PlatformError::OsVersionDetectionError(format!("{error_str}: {err}")))?; - if let Some(elf_interpreter) = elf.interpreter { - Ok(PathBuf::from(elf_interpreter)) - } else { - Err(PlatformError::OsVersionDetectionError( - error_str.to_string(), - )) + // We'll try to parse the first file we read successfully + for path in ["/bin/ls", "/bin/sh"] { + let Ok(buffer) = fs::read(path) else { + continue; + }; + let elf = Elf::parse(&buffer).map_err(|err| { + PlatformError::OsVersionDetectionError(format!( + "Couldn't parse {path} to detect the ld version: {err}" + )) + })?; + if let Some(elf_interpreter) = elf.interpreter { + return Ok(PathBuf::from(elf_interpreter)); + } + + return Err(PlatformError::OsVersionDetectionError(format!( + "Couldn't parse {path} to detect the ld version" + ))); } + Err(PlatformError::OsVersionDetectionError( + "Failed to find binary at `/bin/ls` or `/bin/sh` to read ld version from".to_string(), + )) } #[cfg(test)]