path: use std::io::Error instead of raw ints

The functions we use already give us errors from `std`, so we might as
well use them, instead of converting them to raw libc errors.

Part of #12365
This commit is contained in:
Daniel Rainer
2026-01-21 17:17:15 +01:00
committed by Johannes Altmanninger
parent 98eaef2778
commit 77471c500e

View File

@@ -88,14 +88,14 @@ pub fn path_get_config_remoteness() -> DirRemoteness {
/// Use the given environment stack to ensure this only occurs once.
pub fn path_emit_config_directory_messages(vars: &EnvStack) {
let data = get_data_directory();
if !data.success() {
if let Some(error) = &data.err {
maybe_issue_path_warning(
L!("data"),
wgettext!("can not save history"),
data.used_xdg,
L!("XDG_DATA_HOME"),
&data.path,
data.err,
error,
vars,
);
}
@@ -104,14 +104,14 @@ pub fn path_emit_config_directory_messages(vars: &EnvStack) {
}
let config = get_config_directory();
if !config.success() {
if let Some(error) = &data.err {
maybe_issue_path_warning(
L!("config"),
wgettext!("can not save universal variables or functions"),
config.used_xdg,
L!("XDG_CONFIG_HOME"),
&config.path,
config.err,
error,
vars,
);
}
@@ -130,7 +130,7 @@ fn maybe_issue_path_warning(
using_xdg: bool,
xdg_var: &wstr,
path: &wstr,
saved_errno: libc::c_int,
error: &std::io::Error,
vars: &EnvStack,
) {
let warning_var_name = L!("_FISH_WARNED_").to_owned() + which_dir;
@@ -170,7 +170,7 @@ fn maybe_issue_path_warning(
);
flog!(
warning_path,
wgettext_fmt!("The error was '%s'.", Errno(saved_errno).to_string())
wgettext_fmt!("The error was '%s'.", error.to_string())
);
flog!(
warning_path,
@@ -562,14 +562,14 @@ struct BaseDirectory {
/// whether the dir is remote
remoteness: DirRemoteness,
/// the error code if creating the directory failed, or 0 on success.
err: libc::c_int,
err: Option<std::io::Error>,
/// whether an XDG variable was used in resolving the directory.
used_xdg: bool,
}
impl BaseDirectory {
fn success(&self) -> bool {
self.err == 0
self.err.is_none()
}
}
@@ -589,12 +589,7 @@ fn make_base_directory(xdg_var: &wstr, non_xdg_homepath: &wstr) -> BaseDirectory
let mut build_dir = PathBuf::from(BUILD_DIR);
build_dir.push("fish-test-home");
let err = match std::fs::create_dir_all(&build_dir) {
Ok(_) => 0,
Err(e) => e
.raw_os_error()
.expect("Failed to create fish base directory, but it wasn't an OS error!"),
};
let err = std::fs::create_dir_all(&build_dir).err();
return BaseDirectory {
path: bytes2wcstring(build_dir.as_os_str().as_bytes()),
@@ -621,20 +616,21 @@ fn make_base_directory(xdg_var: &wstr, non_xdg_homepath: &wstr) -> BaseDirectory
used_xdg = false;
}
set_errno(Errno(0));
let err;
let mut remoteness = DirRemoteness::Unknown;
if path.is_empty() {
err = ENOENT;
let err = if path.is_empty() {
Some(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Path is empty",
))
} else if let Err(io_error) = create_dir_all_with_mode(wcs2osstring(&path), 0o700) {
err = io_error.raw_os_error().unwrap_or_default();
Some(io_error)
} else {
err = 0;
// Need to append a trailing slash to check the contents of the directory, not its parent.
let mut tmp = path.clone();
tmp.push('/');
remoteness = path_remoteness(&tmp);
}
None
};
BaseDirectory {
path,