mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 16:33:37 +01:00
fix(cd): on android/termux fails to cd into /sdcard (#10329)
fix on android/termux fails to cd into /sdcard or any directory that user has access via group fixes #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
This commit is contained in:
parent
f59a6990dc
commit
729373aba0
@ -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 {
|
fn any_group(current_user_gid: gid_t, owner_group: u32) -> bool {
|
||||||
use crate::filesystem::util::users;
|
use crate::filesystem::util::users;
|
||||||
|
|
||||||
|
@ -165,9 +165,8 @@ pub fn is_older(src: &Path, dst: &Path) -> Option<bool> {
|
|||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
pub mod users {
|
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 nix::unistd::{Gid, Group, Uid, User};
|
||||||
use std::ffi::CString;
|
|
||||||
|
|
||||||
pub fn get_user_by_uid(uid: uid_t) -> Option<User> {
|
pub fn get_user_by_uid(uid: uid_t) -> Option<User> {
|
||||||
User::from_uid(Uid::from_raw(uid)).ok().flatten()
|
User::from_uid(Uid::from_raw(uid)).ok().flatten()
|
||||||
@ -185,6 +184,7 @@ pub mod users {
|
|||||||
Gid::current().as_raw()
|
Gid::current().as_raw()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(any(target_os = "linux", target_os = "android")))]
|
||||||
pub fn get_current_username() -> Option<String> {
|
pub fn get_current_username() -> Option<String> {
|
||||||
User::from_uid(Uid::current())
|
User::from_uid(Uid::current())
|
||||||
.ok()
|
.ok()
|
||||||
@ -192,6 +192,30 @@ pub mod users {
|
|||||||
.map(|user| user.name)
|
.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.
|
/// Returns groups for a provided user name and primary group id.
|
||||||
///
|
///
|
||||||
/// # libc functions used
|
/// # libc functions used
|
||||||
@ -207,7 +231,9 @@ pub mod users {
|
|||||||
/// println!("User is a member of group #{group}");
|
/// 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>> {
|
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
|
// MacOS uses i32 instead of gid_t in getgrouplist for unknown reasons
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
let mut buff: Vec<i32> = vec![0; 1024];
|
let mut buff: Vec<i32> = vec![0; 1024];
|
||||||
@ -218,7 +244,7 @@ pub mod users {
|
|||||||
return None;
|
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
|
// MacOS uses i32 instead of gid_t in getgrouplist for unknown reasons
|
||||||
// SAFETY:
|
// SAFETY:
|
||||||
|
Loading…
Reference in New Issue
Block a user