Skip to content

Commit

Permalink
fix macos
Browse files Browse the repository at this point in the history
  • Loading branch information
nibon7 committed Jul 4, 2023
1 parent 84c8918 commit 29f741d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
6 changes: 1 addition & 5 deletions crates/nu-command/src/filesystem/cd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,9 @@ fn have_permission(dir: impl AsRef<Path>) -> PermissionResult<'static> {
#[cfg(unix)]
fn any_group(current_user_gid: gid_t, owner_group: u32) -> bool {
use crate::filesystem::util::users;
use std::ffi::CString;

users::get_current_username()
.and_then(|name| CString::new(name.as_bytes()).ok())
.and_then(|name| {
users::getgrouplist(name.as_c_str(), users::Gid::from_raw(current_user_gid)).ok()
})
.and_then(|name| users::get_user_groups(&name, current_user_gid))
.unwrap_or_default()
.into_iter()
.any(|gid| gid.as_raw() == owner_group)
Expand Down
58 changes: 56 additions & 2 deletions crates/nu-command/src/filesystem/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,62 @@ pub fn is_older(src: &Path, dst: &Path) -> bool {
#[cfg(unix)]
pub mod users {
use libc::{gid_t, uid_t};
pub use nix::unistd::{getgrouplist, Gid};
use nix::unistd::{Group, Uid, User};
use nix::unistd::{Gid, Group, Uid, User};
use std::ffi::CString;

#[cfg(target_os = "macos")]
pub fn get_user_groups(user: &str, gid: git_t) -> Option<Vec<Gid>> {

Check failure on line 171 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / std-lib-and-python-virtualenv (macos-latest, stable, py)

cannot find type `git_t` in this scope

Check failure on line 171 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / nu-tests (macos-latest, default, stable)

cannot find type `git_t` in this scope

Check failure on line 171 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / nu-fmt-clippy (macos-latest, default, stable)

cannot find type `git_t` in this scope
use libc::{c_int, sysconf};
use std::cmp::min;

let Ok(user) = CString::new(user.as_bytes()) else {
return None;
};

let ngroups_max = match sysconf(SysconfVar::NGROUPS_MAX) {

Check failure on line 179 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / std-lib-and-python-virtualenv (macos-latest, stable, py)

failed to resolve: use of undeclared type `SysconfVar`

Check failure on line 179 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / nu-tests (macos-latest, default, stable)

failed to resolve: use of undeclared type `SysconfVar`

Check failure on line 179 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / nu-fmt-clippy (macos-latest, default, stable)

failed to resolve: use of undeclared type `SysconfVar`
Ok(Some(n)) => n as c_int,

Check failure on line 180 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / std-lib-and-python-virtualenv (macos-latest, stable, py)

mismatched types

Check failure on line 180 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / nu-tests (macos-latest, default, stable)

mismatched types

Check failure on line 180 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / nu-fmt-clippy (macos-latest, default, stable)

mismatched types
Ok(None) | Err(_) => <c_int>::max_value(),

Check failure on line 181 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / std-lib-and-python-virtualenv (macos-latest, stable, py)

mismatched types

Check failure on line 181 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / std-lib-and-python-virtualenv (macos-latest, stable, py)

mismatched types

Check failure on line 181 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / nu-tests (macos-latest, default, stable)

mismatched types

Check failure on line 181 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / nu-tests (macos-latest, default, stable)

mismatched types

Check failure on line 181 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / nu-fmt-clippy (macos-latest, default, stable)

mismatched types

Check failure on line 181 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / nu-fmt-clippy (macos-latest, default, stable)

mismatched types
};
let mut groups = Vec::<Gid>::with_capacity(min(ngroups_max, 8) as usize);
loop {
let mut ngroups = groups.capacity() as i32;
let ret = unsafe {
libc::getgrouplist(
user.c_str(),

Check failure on line 188 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / std-lib-and-python-virtualenv (macos-latest, stable, py)

no method named `c_str` found for struct `CString` in the current scope

Check failure on line 188 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / nu-tests (macos-latest, default, stable)

no method named `c_str` found for struct `CString` in the current scope

Check failure on line 188 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / nu-fmt-clippy (macos-latest, default, stable)

no method named `c_str` found for struct `CString` in the current scope
gid as c_int,
groups.as_mut_ptr() as *mut c_int,
&mut ngroups,
)
};

// BSD systems only return 0 or -1, Linux returns ngroups on success.
if ret >= 0 {
unsafe { groups.set_len(ngroups as usize) };
return Ok(groups);

Check failure on line 198 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / std-lib-and-python-virtualenv (macos-latest, stable, py)

mismatched types

Check failure on line 198 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / nu-tests (macos-latest, default, stable)

mismatched types

Check failure on line 198 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / nu-fmt-clippy (macos-latest, default, stable)

mismatched types
} else if ret == -1 {
// Returns -1 if ngroups is too small, but does not set errno.
// BSD systems will still fill the groups buffer with as many
// groups as possible, but Linux manpages do not mention this
// behavior.
if groups.capacity() >= ngroups_max {

Check failure on line 204 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / std-lib-and-python-virtualenv (macos-latest, stable, py)

mismatched types

Check failure on line 204 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / nu-tests (macos-latest, default, stable)

mismatched types

Check failure on line 204 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / nu-fmt-clippy (macos-latest, default, stable)

mismatched types
return None;
}

let capacity = min(groups.capacity() * 2, ngroup_max);

Check failure on line 208 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / std-lib-and-python-virtualenv (macos-latest, stable, py)

cannot find value `ngroup_max` in this scope

Check failure on line 208 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / nu-tests (macos-latest, default, stable)

cannot find value `ngroup_max` in this scope

Check failure on line 208 in crates/nu-command/src/filesystem/util.rs

View workflow job for this annotation

GitHub Actions / nu-fmt-clippy (macos-latest, default, stable)

cannot find value `ngroup_max` in this scope

groups.reserve(capacity);
}
}
}

#[cfg(not(target_os = "macos"))]
pub fn get_user_groups(user: &str, gid: gid_t) -> Option<Vec<Gid>> {
let Ok(user) = CString::new(user.as_bytes()) else {
return None;
};

nix::unistd::getgrouplist(user.as_c_str(), Gid::from_raw(gid)).ok()
}

pub fn get_user_by_uid(uid: uid_t) -> Option<User> {
User::from_uid(Uid::from_raw(uid)).ok().flatten()
Expand Down

0 comments on commit 29f741d

Please sign in to comment.