Skip to content

Commit

Permalink
fix(cd): on android/termux fails to cd into /sdcard (nushell#10329)
Browse files Browse the repository at this point in the history
fix on android/termux fails to cd into /sdcard or any directory that
user has access via group

fixes nushell#8095

I am not aware how this works on other platform so feel free to modify
this pr or even close it if it is not correct

# Description
on android or on linux to check if the user belongs to given directory
group, use `libc::getgroups` function

# User-Facing Changes
NA
  • Loading branch information
hardfau1t authored and dmatos2012 committed Feb 20, 2024
1 parent f51af50 commit 828356f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
11 changes: 10 additions & 1 deletion crates/nu-command/src/filesystem/cd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,16 @@ fn have_permission(dir: impl AsRef<Path>) -> PermissionResult<'static> {
}
}

#[cfg(unix)]
#[cfg(any(target_os = "linux", target_os = "android"))]
fn any_group(_current_user_gid: gid_t, owner_group: u32) -> bool {
use crate::filesystem::util::users;
let Some(user_groups) = users::current_user_groups() else {
return false;
};
user_groups.iter().any(|gid| gid.as_raw() == owner_group)
}

#[cfg(all(unix, not(any(target_os = "linux", target_os = "android"))))]
fn any_group(current_user_gid: gid_t, owner_group: u32) -> bool {
use crate::filesystem::util::users;

Expand Down
32 changes: 29 additions & 3 deletions crates/nu-command/src/filesystem/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,8 @@ pub fn is_older(src: &Path, dst: &Path) -> Option<bool> {

#[cfg(unix)]
pub mod users {
use libc::{c_int, gid_t, uid_t};
use libc::{gid_t, uid_t};
use nix::unistd::{Gid, Group, Uid, User};
use std::ffi::CString;

pub fn get_user_by_uid(uid: uid_t) -> Option<User> {
User::from_uid(Uid::from_raw(uid)).ok().flatten()
Expand All @@ -185,13 +184,38 @@ pub mod users {
Gid::current().as_raw()
}

#[cfg(not(any(target_os = "linux", target_os = "android")))]
pub fn get_current_username() -> Option<String> {
User::from_uid(Uid::current())
.ok()
.flatten()
.map(|user| user.name)
}

#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn current_user_groups() -> Option<Vec<Gid>> {
// SAFETY:
// if first arg is 0 then it ignores second argument and returns number of groups present for given user.
let ngroups = unsafe { libc::getgroups(0, core::ptr::null::<gid_t> as *mut _) };
let mut buff: Vec<gid_t> = vec![0; ngroups as usize];

// SAFETY:
// buff is the size of ngroups and getgroups reads max ngroups elements into buff
let found = unsafe { libc::getgroups(ngroups, buff.as_mut_ptr()) };

if found < 0 {
None
} else {
buff.truncate(found as usize);
buff.sort_unstable();
buff.dedup();
buff.into_iter()
.filter_map(|i| get_group_by_gid(i as gid_t))
.map(|group| group.gid)
.collect::<Vec<_>>()
.into()
}
}
/// Returns groups for a provided user name and primary group id.
///
/// # libc functions used
Expand All @@ -207,7 +231,9 @@ pub mod users {
/// println!("User is a member of group #{group}");
/// }
/// ```
#[cfg(not(any(target_os = "linux", target_os = "android")))]
pub fn get_user_groups(username: &str, gid: gid_t) -> Option<Vec<Gid>> {
use std::ffi::CString;
// MacOS uses i32 instead of gid_t in getgrouplist for unknown reasons
#[cfg(target_os = "macos")]
let mut buff: Vec<i32> = vec![0; 1024];
Expand All @@ -218,7 +244,7 @@ pub mod users {
return None;
};

let mut count = buff.len() as c_int;
let mut count = buff.len() as libc::c_int;

// MacOS uses i32 instead of gid_t in getgrouplist for unknown reasons
// SAFETY:
Expand Down

0 comments on commit 828356f

Please sign in to comment.