Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

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:

  • calling e.description(), which returns a different error message ("entity not found"), which is useful, but not what I'm looking for;

  • inspecting the structure of the Error object, e.g. using the {:?} debug display, which reveals that the object does contain the undecorated error string, but it seems to be hidden in an internal field.

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

like image 868
user4815162342 Avatar asked Nov 20 '16 22:11

user4815162342


1 Answers

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
like image 132
wimh Avatar answered Oct 24 '22 19:10

wimh