refactor(hostname): rely on the hostname crate (#1517)

This commit is contained in:
Sylvestre Ledru 2020-05-21 10:58:38 +02:00 committed by GitHub
parent 186c173559
commit 0a02aeda71
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 84 deletions

18
Cargo.lock generated
View file

@ -661,11 +661,22 @@ name = "hostname"
version = "0.0.1"
dependencies = [
"getopts 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
"hostname 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
"uucore 0.0.2 (git+https://github.com/uutils/uucore/?tag=0.0.2)",
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "hostname"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
"match_cfg 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "id"
version = "0.0.1"
@ -819,6 +830,11 @@ dependencies = [
"uucore 0.0.2 (git+https://github.com/uutils/uucore/?tag=0.0.2)",
]
[[package]]
name = "match_cfg"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "matches"
version = "0.1.8"
@ -2226,6 +2242,7 @@ dependencies = [
"checksum glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
"checksum half 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9353c2a89d550b58fa0061d8ed8d002a7d8cdf2494eb0e432859bd3a9e543836"
"checksum hex 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d6a22814455d41612f41161581c2883c0c6a1c41852729b17d5ed88f01e153aa"
"checksum hostname 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3c731c3e10504cc8ed35cfe2f1db4c9274c3d35fa486e3b31df46f068ef3e867"
"checksum idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e"
"checksum if_chain 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4bac95d9aa0624e7b78187d6fb8ab012b41d9f6f54b1bcb61e61c4845f8357ec"
"checksum ioctl-sys 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5e2c4b26352496eaaa8ca7cfa9bd99e93419d3f7983dc6e99c2a35fe9e33504a"
@ -2236,6 +2253,7 @@ dependencies = [
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14"
"checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba"
"checksum match_cfg 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4"
"checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
"checksum md5 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "79c56d6a0b07f9e19282511c83fc5b086364cbae4ba8c7d5f190c3d9b0425a48"
"checksum memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "148fab2e51b4f1cfc66da2a7c32981d1d3c083a803978268bb11fe4b86925e7a"

View file

@ -14,6 +14,7 @@ getopts = "0.2"
libc = "0.2.42"
uucore = "0.0.2"
winapi = { version = "0.3", features = ["sysinfoapi", "winsock2"] }
hostname = { version = "^0.3", features = ["set"] }
[[bin]]
name = "hostname"

View file

@ -10,6 +10,7 @@
*/
extern crate getopts;
extern crate hostname;
extern crate libc;
#[cfg(windows)]
extern crate winapi;
@ -19,24 +20,14 @@ extern crate uucore;
use getopts::Matches;
use std::collections::hash_set::HashSet;
use std::io;
use std::iter::repeat;
use std::ffi::OsStr;
use std::net::ToSocketAddrs;
use std::str;
#[cfg(windows)]
use uucore::wide::*;
#[cfg(windows)]
use winapi::shared::minwindef::MAKEWORD;
#[cfg(windows)]
use winapi::um::sysinfoapi::{ComputerNamePhysicalDnsHostname, SetComputerNameExW};
#[cfg(windows)]
use winapi::um::winsock2::{GetHostNameW, WSACleanup, WSAStartup};
#[cfg(not(windows))]
use libc::gethostname;
#[cfg(not(windows))]
use libc::sethostname;
use winapi::um::winsock2::{WSACleanup, WSAStartup};
const SYNTAX: &str = "[OPTION]... [HOSTNAME]";
const SUMMARY: &str = "Print or set the system's host name.";
@ -90,7 +81,7 @@ fn execute(args: Vec<String>) -> i32 {
match matches.free.len() {
0 => display_hostname(matches),
1 => {
if let Err(err) = xsethostname(matches.free.last().unwrap()) {
if let Err(err) = hostname::set(OsStr::new(matches.free.last().unwrap())) {
show_error!("{}", err);
1
} else {
@ -105,7 +96,7 @@ fn execute(args: Vec<String>) -> i32 {
}
fn display_hostname(matches: Matches) -> i32 {
let hostname = return_if_err!(1, xgethostname());
let hostname = hostname::get().unwrap().into_string().unwrap();
if matches.opt_present("i") {
// XXX: to_socket_addrs needs hostname:port so append a dummy port and remove it later.
@ -160,73 +151,3 @@ fn display_hostname(matches: Matches) -> i32 {
0
}
}
#[cfg(not(windows))]
fn xgethostname() -> io::Result<String> {
use std::ffi::CStr;
let namelen = 256;
let mut name: Vec<u8> = repeat(0).take(namelen).collect();
let err = unsafe {
gethostname(
name.as_mut_ptr() as *mut libc::c_char,
namelen as libc::size_t,
)
};
if err == 0 {
let null_pos = name.iter().position(|byte| *byte == 0).unwrap_or(namelen);
if null_pos == namelen {
name.push(0);
}
Ok(CStr::from_bytes_with_nul(&name[..=null_pos])
.unwrap()
.to_string_lossy()
.into_owned())
} else {
Err(io::Error::last_os_error())
}
}
#[cfg(windows)]
fn xgethostname() -> io::Result<String> {
let namelen = 256;
let mut name: Vec<u16> = repeat(0).take(namelen).collect();
let err = unsafe { GetHostNameW(name.as_mut_ptr(), namelen as libc::c_int) };
if err == 0 {
Ok(String::from_wide_null(&name))
} else {
Err(io::Error::last_os_error())
}
}
#[cfg(not(windows))]
fn xsethostname(name: &str) -> io::Result<()> {
let vec_name: Vec<libc::c_char> = name.bytes().map(|c| c as libc::c_char).collect();
let err = unsafe { sethostname(vec_name.as_ptr(), vec_name.len() as _) };
if err != 0 {
Err(io::Error::last_os_error())
} else {
Ok(())
}
}
#[cfg(windows)]
fn xsethostname(name: &str) -> io::Result<()> {
use std::ffi::OsStr;
let wide_name = OsStr::new(name).to_wide_null();
let err = unsafe { SetComputerNameExW(ComputerNamePhysicalDnsHostname, wide_name.as_ptr()) };
if err == 0 {
// NOTE: the above is correct, failure is when the function returns 0 apparently
Err(io::Error::last_os_error())
} else {
Ok(())
}
}