Skip to content

Commit

Permalink
Use the nix crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Aug 27, 2019
1 parent f9a728e commit c3ffe11
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 14 deletions.
36 changes: 31 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ rustc_version = "0.2.1"
semver = "0.9.0"
toml = "0.2.1"

[target.'cfg(not(windows))'.dependencies]
nix = "0.15"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.7", features = ["winbase"] }
2 changes: 1 addition & 1 deletion src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn run(target: &Target,
.args(&["-e", "XARGO_HOME=/xargo"])
.args(&["-e", "CARGO_HOME=/cargo"])
.args(&["-e", "CARGO_TARGET_DIR=/target"])
.args(&["-e", &format!("USER={}", id::username())]);
.args(&["-e", &format!("USER={}", id::username().unwrap().unwrap())]);

if let Ok(value) = env::var("QEMU_STRACE") {
docker.args(&["-e", &format!("QEMU_STRACE={}", value)]);
Expand Down
30 changes: 22 additions & 8 deletions src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use libc;
#[cfg(not(target_os = "windows"))]
use std::ffi::CStr;
#[cfg(not(target_os = "windows"))]
use nix::{errno::{errno, Errno}, unistd::{Gid, Uid}, Error};

#[cfg(target_os = "windows")]
pub fn group() -> u32 {
Expand All @@ -10,7 +12,7 @@ pub fn group() -> u32 {

#[cfg(not(target_os = "windows"))]
pub fn group() -> u32 {
unsafe { libc::getgid() }
Gid::current().as_raw()
}

#[cfg(target_os = "windows")]
Expand All @@ -20,7 +22,7 @@ pub fn user() -> u32 {

#[cfg(not(target_os = "windows"))]
pub fn user() -> u32 {
unsafe { libc::getuid() }
Uid::current().as_raw()
}

#[cfg(target_os = "windows")]
Expand Down Expand Up @@ -51,10 +53,22 @@ pub fn username() -> String {
}

#[cfg(not(target_os = "windows"))]
pub fn username() -> String {
unsafe {
CStr::from_ptr((*libc::getpwuid(user())).pw_name)
.to_string_lossy()
.into_owned()
}
pub fn username() -> Result<Option<String>, Error> {
let name = unsafe {
Errno::clear();

let passwd = libc::getpwuid(Uid::current().as_raw());

if passwd.is_null() {
let errno = errno();

if errno == 0 { return Ok(None) }

return Err(Error::Sys(Errno::from_i32(errno)))
}

CStr::from_ptr((*passwd).pw_name)
};

Ok(Some(name.to_string_lossy().into_owned()))
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern crate libc;
extern crate rustc_version;
extern crate semver;
extern crate toml;
extern crate nix;

#[cfg(target_os = "windows")]
extern crate winapi;
Expand Down

0 comments on commit c3ffe11

Please sign in to comment.