user4815162342
user4815162342

Reputation: 154936

How does one get the error message as provided by the system without the "os error n" suffix?

Error messages displayed by std::io::Error come with an "(os error n)" suffix, easily reproduced by running a program such as:

use std::fs;
use std::io::Write;

fn main() {
    let fl = "no such file";
    if let Err(e) = fs::metadata(fl) {
        writeln!(std::io::stderr(), "{}: {}", fl, e).unwrap();
    }
}

Output:

no such file: No such file or directory (os error 2)

How does one get the system error message as provided by the system, i.e. without the "os error 2" part?

I tried:

Please note that I'm aiming for a portable and not a Linux-only solution.

Upvotes: 9

Views: 1724

Answers (1)

wimh
wimh

Reputation: 15232

This is the code adding "os error 2":

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self.repr {
            Repr::Os(code) => {
                let detail = sys::os::error_string(code);
                write!(fmt, "{} (os error {})", detail, code)
            }
            Repr::Custom(ref c) => c.error.fmt(fmt),
            Repr::Simple(kind) => write!(fmt, "{}", kind.as_str()),
        }
    }
}

Unfortunately, sys::os::error_string does not seem accessible, so you would have to copy the code into your program.

extern crate libc;

use std::ffi::CStr;
use std::fs;
use std::os::raw::{c_char, c_int};
use std::str;

const TMPBUF_SZ: usize = 128;

// from https://github.com/rust-lang/rust/blob/1.26.2/src/libstd/sys/unix/os.rs#L87-L107
pub fn error_string(errno: i32) -> String {
    extern "C" {
        #[cfg_attr(any(target_os = "linux", target_env = "newlib"), link_name = "__xpg_strerror_r")]
        fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: libc::size_t) -> c_int;
    }

    let mut buf = [0 as c_char; TMPBUF_SZ];

    let p = buf.as_mut_ptr();
    unsafe {
        if strerror_r(errno as c_int, p, buf.len()) < 0 {
            panic!("strerror_r failure");
        }

        let p = p as *const _;
        str::from_utf8(CStr::from_ptr(p).to_bytes())
            .unwrap()
            .to_owned()
    }
}

fn main() {
    let fl = "no such file";
    if let Err(e) = fs::metadata(fl) {
        eprintln!("{}: {}", fl, e);
        eprintln!("{}", error_string(e.raw_os_error().unwrap()));
    }
}

Output:

no such file: No such file or directory (os error 2)
No such file or directory

Upvotes: 1

Related Questions