Skip to content

Commit 6f18c80

Browse files
committed
native: return both the user and the group from current_user
1 parent 4b82a3a commit 6f18c80

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

src/cmd/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,8 @@ impl<'w, 'pl> Command<'w, 'pl> {
388388
.workdir(container_dirs::WORK_DIR.to_str().unwrap())
389389
.cmd(cmd);
390390

391-
if let Some(user_id) = native::current_user() {
392-
builder = builder.env("MAP_USER_ID", user_id.to_string());
391+
if let Some(user) = native::current_user() {
392+
builder = builder.env("MAP_USER_ID", user.user_id.to_string());
393393
}
394394

395395
for (key, value) in self.env {

src/native/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ pub(crate) use self::unix::*;
77
mod windows;
88
#[cfg(windows)]
99
pub(crate) use self::windows::*;
10+
11+
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
12+
pub(crate) struct CurrentUser {
13+
pub(crate) user_id: u32,
14+
pub(crate) group_id: u32,
15+
}

src/native/unix.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::CurrentUser;
12
use crate::cmd::KillFailedError;
23
use failure::Error;
34
use nix::{
@@ -24,20 +25,21 @@ pub(crate) fn kill_process(id: u32) -> Result<(), KillFailedError> {
2425
}
2526
}
2627

27-
pub(crate) fn current_user() -> Option<u32> {
28-
Some(Uid::effective().into())
29-
}
30-
31-
fn current_group() -> u32 {
32-
Gid::effective().into()
28+
pub(crate) fn current_user() -> Option<CurrentUser> {
29+
Some(CurrentUser {
30+
user_id: Uid::effective().into(),
31+
group_id: Gid::effective().into(),
32+
})
3333
}
3434

3535
fn executable_mode_for(path: &Path) -> Result<u32, Error> {
3636
let metadata = path.metadata()?;
3737

38-
if metadata.uid() == current_user().unwrap() {
38+
let user = current_user().unwrap();
39+
40+
if metadata.uid() == user.user_id {
3941
Ok(EXECUTABLE_BITS << 6)
40-
} else if metadata.gid() == current_group() {
42+
} else if metadata.gid() == user.group_id {
4143
Ok(EXECUTABLE_BITS << 3)
4244
} else {
4345
Ok(EXECUTABLE_BITS)
@@ -65,6 +67,7 @@ pub(crate) fn make_executable<P: AsRef<Path>>(path: P) -> Result<(), Error> {
6567

6668
#[cfg(test)]
6769
mod tests {
70+
use super::CurrentUser;
6871
use nix::unistd::{Gid, Uid};
6972
use std::fs::File;
7073
use std::os::unix::process::ExitStatusExt;
@@ -82,12 +85,13 @@ mod tests {
8285

8386
#[test]
8487
fn test_current_user() {
85-
assert_eq!(super::current_user(), Some(u32::from(Uid::effective())));
86-
}
87-
88-
#[test]
89-
fn test_current_group() {
90-
assert_eq!(super::current_group(), u32::from(Gid::effective()));
88+
assert_eq!(
89+
super::current_user(),
90+
Some(CurrentUser {
91+
user_id: u32::from(Uid::effective()),
92+
group_id: u32::from(Gid::effective()),
93+
})
94+
);
9195
}
9296

9397
#[test]

src/native/windows.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
use super::CurrentUser;
12
use crate::cmd::KillFailedError;
2-
use failure::Error;
3+
use failure::{bail, Error};
34
use std::fs::File;
45
use std::path::Path;
56
use winapi::um::handleapi::CloseHandle;
@@ -25,7 +26,7 @@ pub(crate) fn kill_process(id: u32) -> Result<(), KillFailedError> {
2526
Ok(())
2627
}
2728

28-
pub(crate) fn current_user() -> Option<u32> {
29+
pub(crate) fn current_user() -> Option<CurrentUser> {
2930
None
3031
}
3132

0 commit comments

Comments
 (0)