Skip to content

Commit

Permalink
FreeBSD compatibility patches (nushell#11869)
Browse files Browse the repository at this point in the history
# Description

nushell is verified to work on FreeBSD 14 with these patches.

What isn't supported on FreeBSD:
* the crate 'procfs' doesn't support FreeBSD yet, all functionality
depending on procfs is disabled
* several RLIMIT_* values aren't supported on FreeBSD - functions
related to these are disabled




# User-Facing Changes
n/a

# Tests + Formatting
n/a

# After Submitting
n/a

---------

Co-authored-by: sholderbach <sholderbach@users.noreply.github.com>
  • Loading branch information
2 people authored and kik4444 committed Feb 28, 2024
1 parent bfd1f22 commit 3131e2c
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 15 deletions.
7 changes: 5 additions & 2 deletions crates/nu-command/src/filesystem/cd.rs
Expand Up @@ -229,7 +229,7 @@ fn have_permission(dir: impl AsRef<Path>) -> PermissionResult<'static> {
}
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(any(target_os = "linux", target_os = "freebsd", 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 {
Expand All @@ -238,7 +238,10 @@ fn any_group(_current_user_gid: gid_t, owner_group: u32) -> bool {
user_groups.iter().any(|gid| gid.as_raw() == owner_group)
}

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

Expand Down
17 changes: 15 additions & 2 deletions crates/nu-command/src/filesystem/ucp.rs
Expand Up @@ -141,9 +141,19 @@ impl Command for UCp {
} else {
uu_cp::OverwriteMode::Clobber(uu_cp::ClobberMode::Standard)
};
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos"))]
#[cfg(any(
target_os = "linux",
target_os = "freebsd",
target_os = "android",
target_os = "macos"
))]
let reflink_mode = uu_cp::ReflinkMode::Auto;
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "macos")))]
#[cfg(not(any(
target_os = "linux",
target_os = "freebsd",
target_os = "android",
target_os = "macos"
)))]
let reflink_mode = uu_cp::ReflinkMode::Never;
let mut paths: Vec<Spanned<NuPath>> = call.rest(engine_state, stack, 0)?;
if paths.is_empty() {
Expand Down Expand Up @@ -283,6 +293,7 @@ fn make_attributes(preserve: Option<Value>) -> Result<uu_cp::Attributes, ShellEr
let mut attributes = uu_cp::Attributes {
#[cfg(any(
target_os = "linux",
target_os = "freebsd",
target_os = "android",
target_os = "macos",
target_os = "netbsd"
Expand All @@ -303,6 +314,7 @@ fn make_attributes(preserve: Option<Value>) -> Result<uu_cp::Attributes, ShellEr
mode: ATTR_SET,
#[cfg(any(
target_os = "linux",
target_os = "freebsd",
target_os = "android",
target_os = "macos",
target_os = "netbsd"
Expand Down Expand Up @@ -344,6 +356,7 @@ fn parse_and_set_attribute(
"mode" => &mut attribute.mode,
#[cfg(any(
target_os = "linux",
target_os = "freebsd",
target_os = "android",
target_os = "macos",
target_os = "netbsd"
Expand Down
6 changes: 3 additions & 3 deletions crates/nu-command/src/filesystem/util.rs
Expand Up @@ -107,15 +107,15 @@ pub mod users {
Gid::current().as_raw()
}

#[cfg(not(any(target_os = "linux", target_os = "android")))]
#[cfg(not(any(target_os = "linux", target_os = "freebsd", 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"))]
#[cfg(any(target_os = "linux", target_os = "freebsd", 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.
Expand Down Expand Up @@ -154,7 +154,7 @@ pub mod users {
/// println!("User is a member of group #{group}");
/// }
/// ```
#[cfg(not(any(target_os = "linux", target_os = "android")))]
#[cfg(not(any(target_os = "linux", target_os = "freebsd", 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
Expand Down
3 changes: 3 additions & 0 deletions crates/nu-command/src/platform/ulimit.rs
Expand Up @@ -111,6 +111,7 @@ static RESOURCE_ARRAY: Lazy<Vec<ResourceInfo>> = Lazy::new(|| {
target_os = "freebsd",
target_os = "openbsd",
target_os = "linux",
target_os = "freebsd",
target_os = "netbsd"
))]
(
Expand All @@ -126,6 +127,7 @@ static RESOURCE_ARRAY: Lazy<Vec<ResourceInfo>> = Lazy::new(|| {
target_os = "netbsd",
target_os = "openbsd",
target_os = "linux",
target_os = "freebsd",
target_os = "aix",
))]
(
Expand Down Expand Up @@ -178,6 +180,7 @@ static RESOURCE_ARRAY: Lazy<Vec<ResourceInfo>> = Lazy::new(|| {
target_os = "netbsd",
target_os = "openbsd",
target_os = "linux",
target_os = "freebsd",
target_os = "aix",
))]
(
Expand Down
2 changes: 2 additions & 0 deletions crates/nu-command/src/system/mod.rs
Expand Up @@ -4,6 +4,7 @@ mod nu_check;
#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "freebsd",
target_os = "macos",
target_os = "windows"
))]
Expand All @@ -20,6 +21,7 @@ pub use nu_check::NuCheck;
#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "freebsd",
target_os = "macos",
target_os = "windows"
))]
Expand Down
3 changes: 3 additions & 0 deletions crates/nu-command/src/system/ps.rs
Expand Up @@ -16,6 +16,7 @@ use nu_protocol::{
};
#[cfg(all(
unix,
not(target_os = "freebsd"),
not(target_os = "macos"),
not(target_os = "windows"),
not(target_os = "android"),
Expand All @@ -27,6 +28,7 @@ use std::time::Duration;
#[derive(Clone)]
pub struct Ps;

#[cfg(not(target_os = "freebsd"))]
impl Command for Ps {
fn name(&self) -> &str {
"ps"
Expand Down Expand Up @@ -93,6 +95,7 @@ impl Command for Ps {
}
}

#[cfg(not(target_os = "freebsd"))]
fn run_ps(
engine_state: &EngineState,
stack: &mut Stack,
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/tests/commands/ls.rs
Expand Up @@ -603,7 +603,7 @@ fn list_a_directory_not_exists() {
})
}

#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
#[test]
fn list_directory_contains_invalid_utf8() {
use std::ffi::OsStr;
Expand Down
10 changes: 5 additions & 5 deletions crates/nu-command/tests/commands/move_/umv.rs
Expand Up @@ -436,19 +436,19 @@ fn mv_change_case_of_directory() {
.map(|de| de.unwrap().file_name().to_string_lossy().into_owned())
.collect();

#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
assert!(
!_files_in_test_directory.contains(&original_dir)
&& _files_in_test_directory.contains(&new_dir)
);

#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
assert!(files_exist_at(
vec!["somefile.txt",],
dirs.test().join(new_dir)
));

#[cfg(not(target_os = "linux"))]
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
_actual.err.contains("to a subdirectory of itself");
})
}
Expand All @@ -474,12 +474,12 @@ fn mv_change_case_of_file() {
.unwrap()
.map(|de| de.unwrap().file_name().to_string_lossy().into_owned())
.collect();
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
assert!(
!_files_in_test_directory.contains(&original_file_name)
&& _files_in_test_directory.contains(&new_file_name)
);
#[cfg(not(target_os = "linux"))]
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
_actual.err.contains("are the same file");
})
}
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/tests/commands/ucp.rs
Expand Up @@ -903,7 +903,7 @@ fn test_cp_debug_default() {
{
panic!("{}", format!("Failure: stdout was \n{}", actual.out));
}
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
if !actual
.out
.contains("copy offload: unknown, reflink: unsupported, sparse detection: no")
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/tests/commands/ulimit.rs
Expand Up @@ -93,7 +93,7 @@ fn limit_set_invalid1() {
});
}

#[cfg(any(target_os = "linux", target_os = "macos"))]
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "macos"))]
#[test]
fn limit_set_invalid2() {
Playground::setup("limit_set_invalid2", |dirs, _sandbox| {
Expand Down

0 comments on commit 3131e2c

Please sign in to comment.