From 6f6d62f401009db6b68870060f01ff677d069181 Mon Sep 17 00:00:00 2001 From: Kai Luo Date: Thu, 17 Nov 2022 02:50:34 -0500 Subject: [PATCH 1/5] Implement current_exe for AIX --- library/std/src/sys/unix/os.rs | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/library/std/src/sys/unix/os.rs b/library/std/src/sys/unix/os.rs index 2f2663db60769..5cf0fe32f3fe9 100644 --- a/library/std/src/sys/unix/os.rs +++ b/library/std/src/sys/unix/os.rs @@ -471,6 +471,46 @@ pub fn current_exe() -> io::Result { if !path.is_absolute() { getcwd().map(|cwd| cwd.join(path)) } else { Ok(path) } } +#[cfg(target_os) = "aix")] +pub fn current_exe() -> io::Result { + use crate::io::ErrorKind; + + #[cfg(test)] + use realstd::env; + + #[cfg(not(test))] + use crate::env; + + let exe_path = env::args().next().ok_or(io::const_io_error!( + ErrorKind::Uncategorized, + "an executable path was not found because no arguments were provided through argv" + ))?; + let path = PathBuf::from(exe_path); + if path.is_absolute() { + return path.canonicalize(); + } + // Search PWD to infer current_exe. + if let Some(pstr) = path.to_str() && pstr.contains("/") { + return getcwd().map(|cwd| cwd.join(path))?.canonicalize(); + } + // Search PATH to infer current_exe. + if let Some(p) = getenv(OsStr::from_bytes("PATH".as_bytes())) { + for search_path in split_paths(&p) { + let pb = search_path.join(&path); + if pb.is_file() && let Ok(metadata) = crate::fs::metadata(&pb) { + if metadata.permissions().mode() & 0o111 != 0 { + return pb.canonicalize(); + } + } else { + continue; + } + } + } + return Err(io::const_io_error!( + ErrorKind::Uncategorized, + "an executable path was not found")); +} + pub struct Env { iter: vec::IntoIter<(OsString, OsString)>, } From 8e047f1dd0dad5ccdb1db0f90a532b03dada274f Mon Sep 17 00:00:00 2001 From: Kai Luo Date: Thu, 17 Nov 2022 03:02:00 -0500 Subject: [PATCH 2/5] Refine code --- library/std/src/sys/unix/os.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/library/std/src/sys/unix/os.rs b/library/std/src/sys/unix/os.rs index 5cf0fe32f3fe9..d7d920cbdad2c 100644 --- a/library/std/src/sys/unix/os.rs +++ b/library/std/src/sys/unix/os.rs @@ -497,12 +497,9 @@ pub fn current_exe() -> io::Result { if let Some(p) = getenv(OsStr::from_bytes("PATH".as_bytes())) { for search_path in split_paths(&p) { let pb = search_path.join(&path); - if pb.is_file() && let Ok(metadata) = crate::fs::metadata(&pb) { - if metadata.permissions().mode() & 0o111 != 0 { - return pb.canonicalize(); - } - } else { - continue; + if pb.is_file() && let Ok(metadata) = crate::fs::metadata(&pb) && + metadata.permissions().mode() & 0o111 != 0 { + return pb.canonicalize(); } } } From 6ebe551393a333b25ae52f5b39091a4e23c77335 Mon Sep 17 00:00:00 2001 From: Kai Luo Date: Thu, 17 Nov 2022 03:05:50 -0500 Subject: [PATCH 3/5] Fix typo --- library/std/src/sys/unix/os.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/sys/unix/os.rs b/library/std/src/sys/unix/os.rs index d7d920cbdad2c..4268bc4c9dc63 100644 --- a/library/std/src/sys/unix/os.rs +++ b/library/std/src/sys/unix/os.rs @@ -471,7 +471,7 @@ pub fn current_exe() -> io::Result { if !path.is_absolute() { getcwd().map(|cwd| cwd.join(path)) } else { Ok(path) } } -#[cfg(target_os) = "aix")] +#[cfg(target_os = "aix")] pub fn current_exe() -> io::Result { use crate::io::ErrorKind; From d9ea4210f540efe3b81b2b34d3515786740d9a70 Mon Sep 17 00:00:00 2001 From: Kai Luo Date: Tue, 6 Dec 2022 16:48:53 +0800 Subject: [PATCH 4/5] Minor tune --- library/std/src/sys/unix/os.rs | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/library/std/src/sys/unix/os.rs b/library/std/src/sys/unix/os.rs index 4268bc4c9dc63..2c012de8acf44 100644 --- a/library/std/src/sys/unix/os.rs +++ b/library/std/src/sys/unix/os.rs @@ -451,7 +451,7 @@ pub fn current_exe() -> io::Result { super::unsupported::unsupported() } -#[cfg(target_os = "fuchsia")] +#[cfg(target_os = "fuchsia", target_os = "aix")] pub fn current_exe() -> io::Result { use crate::io::ErrorKind; @@ -468,31 +468,19 @@ pub fn current_exe() -> io::Result { let path = PathBuf::from(exe_path); // Prepend the current working directory to the path if it's not absolute. - if !path.is_absolute() { getcwd().map(|cwd| cwd.join(path)) } else { Ok(path) } -} - -#[cfg(target_os = "aix")] -pub fn current_exe() -> io::Result { - use crate::io::ErrorKind; - - #[cfg(test)] - use realstd::env; - - #[cfg(not(test))] - use crate::env; + if cfg!(target_os = "fuchsia") { + if !path.is_absolute() { getcwd().map(|cwd| cwd.join(path)) } else { Ok(path) } + } - let exe_path = env::args().next().ok_or(io::const_io_error!( - ErrorKind::Uncategorized, - "an executable path was not found because no arguments were provided through argv" - ))?; - let path = PathBuf::from(exe_path); if path.is_absolute() { return path.canonicalize(); } + // Search PWD to infer current_exe. if let Some(pstr) = path.to_str() && pstr.contains("/") { return getcwd().map(|cwd| cwd.join(path))?.canonicalize(); } + // Search PATH to infer current_exe. if let Some(p) = getenv(OsStr::from_bytes("PATH".as_bytes())) { for search_path in split_paths(&p) { @@ -503,9 +491,8 @@ pub fn current_exe() -> io::Result { } } } - return Err(io::const_io_error!( - ErrorKind::Uncategorized, - "an executable path was not found")); + + return Err(io::const_io_error!(ErrorKind::Uncategorized, "an executable path was not found")); } pub struct Env { From 33fd260f59504f6a61f35986ceb0a2a89a8646f1 Mon Sep 17 00:00:00 2001 From: Kai Luo Date: Tue, 6 Dec 2022 16:54:49 +0800 Subject: [PATCH 5/5] Fix build --- library/std/src/sys/unix/os.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/library/std/src/sys/unix/os.rs b/library/std/src/sys/unix/os.rs index 2c012de8acf44..fe428c85d3854 100644 --- a/library/std/src/sys/unix/os.rs +++ b/library/std/src/sys/unix/os.rs @@ -451,7 +451,7 @@ pub fn current_exe() -> io::Result { super::unsupported::unsupported() } -#[cfg(target_os = "fuchsia", target_os = "aix")] +#[cfg(any(target_os = "fuchsia", target_os = "aix"))] pub fn current_exe() -> io::Result { use crate::io::ErrorKind; @@ -469,7 +469,11 @@ pub fn current_exe() -> io::Result { // Prepend the current working directory to the path if it's not absolute. if cfg!(target_os = "fuchsia") { - if !path.is_absolute() { getcwd().map(|cwd| cwd.join(path)) } else { Ok(path) } + if !path.is_absolute() { + return getcwd().map(|cwd| cwd.join(path)); + } else { + return Ok(path); + } } if path.is_absolute() {