use getgroups(2) for apple macos

Although nix-rust/nix mentioned that this feature should be implemented
based on opendirectoryd, CPython implemented it based on getgroups(2)
for apple macOS.
So I ported the code from nix-rust/nix and it works well.

ref0: 500036a206/src/unistd.rs (L1320)
ref1: c4cacc8c5e/Modules/posixmodule.c (L6852)
This commit is contained in:
Dong-hee Na
2019-08-18 00:57:48 +09:00
parent e3344df00e
commit ea5efdbcde
2 changed files with 42 additions and 2 deletions

View File

@@ -12,8 +12,22 @@ before_cache:
matrix:
fast_finish: true
include:
- name: Run Rust tests
- name: Run Rust tests(linux)
language: rust
os: linux
rust: stable
cache: cargo
script:
- cargo build --verbose --all
- cargo test --verbose --all
env:
# Prevention of cache corruption.
# See: https://docs.travis-ci.com/user/caching/#caches-and-build-matrices
- JOBCACHE=1
- name: Run Rust tests(osx)
language: rust
os: osx
rust: stable
cache: cargo
script:

View File

@@ -262,7 +262,7 @@ fn get_right_permission(
let others_permissions = get_permissions(others_mode);
let user_id = nix::unistd::getuid();
let groups_ids = nix::unistd::getgroups()?;
let groups_ids = getgroups()?;
if file_owner == user_id {
Ok(owner_permissions)
@@ -273,6 +273,32 @@ fn get_right_permission(
}
}
#[cfg(target_os = "macos")]
fn getgroups() -> nix::Result<Vec<Gid>> {
use libc::{c_int, gid_t};
use std::ptr;
let ret = unsafe { libc::getgroups(0, ptr::null_mut()) };
let mut groups = Vec::<Gid>::with_capacity(Errno::result(ret)? as usize);
loop {
let ret = unsafe {
libc::getgroups(
groups.capacity() as c_int,
groups.as_mut_ptr() as *mut gid_t,
)
};
return Errno::result(ret).map(|s| {
unsafe { groups.set_len(s as usize) };
groups
});
}
}
#[cfg(target_os = "linux")]
fn getgroups() -> nix::Result<Vec<Gid>> {
nix::unistd::getgroups()
}
#[cfg(unix)]
fn os_access(path: PyStringRef, mode: u8, vm: &VirtualMachine) -> PyResult<bool> {
use std::os::unix::fs::MetadataExt;