From 71dd85792637d19c4bff3aefb49589327bd92652 Mon Sep 17 00:00:00 2001 From: Scott Boggs Date: Sat, 26 Mar 2022 14:21:19 -0400 Subject: [PATCH] Termux/Android target support for v0.60.0 (#4956) * Add android as target os for procfs-based ps * Turn off code for dealing with trash on platforms which are known to not support a standard trash protocol * Update lib.rs * Update lib.rs Co-authored-by: JT <547158+jntrnr@users.noreply.github.com> --- crates/nu-command/Cargo.toml | 5 +- crates/nu-command/src/filesystem/rm.rs | 71 +++++++++++++++++++------- crates/nu-glob/src/lib.rs | 7 ++- crates/nu-system/Cargo.toml | 4 +- crates/nu-system/src/lib.rs | 4 +- 5 files changed, 67 insertions(+), 24 deletions(-) diff --git a/crates/nu-command/Cargo.toml b/crates/nu-command/Cargo.toml index b1fe334135..29536a663e 100644 --- a/crates/nu-command/Cargo.toml +++ b/crates/nu-command/Cargo.toml @@ -73,7 +73,6 @@ terminal_size = "0.1.17" thiserror = "1.0.29" titlecase = "1.1.0" toml = "0.5.8" -trash = { version = "2.0.2", optional = true } unicode-segmentation = "1.8.0" url = "2.2.1" uuid = { version = "0.8.2", features = ["v4"] } @@ -85,6 +84,10 @@ zip = { version="0.5.9", optional = true } umask = "1.0.0" users = "0.11.0" +[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies.trash] +version = "2.0.2" +optional = true + [dependencies.polars] version = "0.20.0" optional = true diff --git a/crates/nu-command/src/filesystem/rm.rs b/crates/nu-command/src/filesystem/rm.rs index 8d7dbaca0d..4c98cad334 100644 --- a/crates/nu-command/src/filesystem/rm.rs +++ b/crates/nu-command/src/filesystem/rm.rs @@ -1,5 +1,9 @@ use std::collections::HashMap; -#[cfg(feature = "trash-support")] +#[cfg(all( + feature = "trash-support", + not(target_os = "android"), + not(target_os = "ios") +))] use std::io::ErrorKind; #[cfg(unix)] use std::os::unix::prelude::FileTypeExt; @@ -35,7 +39,13 @@ impl Command for Rm { } fn signature(&self) -> Signature { - Signature::build("rm") + let sig = Signature::build("rm"); + #[cfg(all( + feature = "trash-support", + not(target_os = "android"), + not(target_os = "ios") + ))] + let sig = sig .switch( "trash", "use the platform's recycle bin instead of permanently deleting", @@ -45,8 +55,8 @@ impl Command for Rm { "permanent", "don't use recycle bin, delete permanently", Some('p'), - ) - .switch("recursive", "delete subdirectories recursively", Some('r')) + ); + sig.switch("recursive", "delete subdirectories recursively", Some('r')) .switch("force", "suppress error when no file", Some('f')) .switch("quiet", "suppress output showing files deleted", Some('q')) // .switch("interactive", "ask user to confirm action", Some('i')) @@ -69,12 +79,18 @@ impl Command for Rm { } fn examples(&self) -> Vec { - vec![ + let mut examples = vec![ Example { description: "Delete or move a file to the system trash (depending on 'rm_always_trash' config option)", example: "rm file.txt", result: None, - }, + }]; + #[cfg(all( + feature = "trash-support", + not(target_os = "android"), + not(target_os = "ios") + ))] + examples.append(&mut vec![ Example { description: "Move a file to the system trash", example: "rm --trash file.txt", @@ -85,12 +101,13 @@ impl Command for Rm { example: "rm --permanent file.txt", result: None, }, - Example { - description: "Delete a file, and suppress errors if no file is found", - example: "rm --force file.txt", - result: None, - } - ] + ]); + examples.push(Example { + description: "Delete a file, and suppress errors if no file is found", + example: "rm --force file.txt", + result: None, + }); + examples } } @@ -100,7 +117,11 @@ fn rm( call: &Call, ) -> Result { let trash = call.has_flag("trash"); - #[cfg(feature = "trash-support")] + #[cfg(all( + feature = "trash-support", + not(target_os = "android"), + not(target_os = "ios") + ))] let permanent = call.has_flag("permanent"); let recursive = call.has_flag("recursive"); let force = call.has_flag("force"); @@ -116,20 +137,26 @@ fn rm( let rm_always_trash = config.rm_always_trash; - #[cfg(not(feature = "trash-support"))] + #[cfg(any( + not(feature = "trash-support"), + target_os = "android", + target_os = "ios" + ))] { if rm_always_trash { return Err(ShellError::SpannedLabeledError( "Cannot execute `rm`; the current configuration specifies \ `rm_always_trash = true`, but the current nu executable was not \ - built with feature `trash_support`." + built with feature `trash_support` or trash is not supported on \ + your platform." .into(), "trash required to be true but not supported".into(), span, )); } else if trash { return Err(ShellError::SpannedLabeledError( - "Cannot execute `rm` with option `--trash`; feature `trash-support` not enabled" + "Cannot execute `rm` with option `--trash`; feature `trash-support` not \ + enabled or trash is not supported on your platform" .into(), "this option is only available if nu is built with the `trash-support` feature" .into(), @@ -241,7 +268,11 @@ fn rm( || is_empty() { let result; - #[cfg(feature = "trash-support")] + #[cfg(all( + feature = "trash-support", + not(target_os = "android"), + not(target_os = "ios") + ))] { use std::io::Error; result = if trash || (rm_always_trash && !permanent) { @@ -254,7 +285,11 @@ fn rm( std::fs::remove_dir_all(&f) }; } - #[cfg(not(feature = "trash-support"))] + #[cfg(any( + not(feature = "trash-support"), + target_os = "android", + target_os = "ios" + ))] { result = if metadata.is_file() || is_socket || is_fifo { std::fs::remove_file(&f) diff --git a/crates/nu-glob/src/lib.rs b/crates/nu-glob/src/lib.rs index 24608f1c44..e2412d8969 100644 --- a/crates/nu-glob/src/lib.rs +++ b/crates/nu-glob/src/lib.rs @@ -921,7 +921,12 @@ mod test { // this test assumes that there is a /root directory and that // the user running this test is not root or otherwise doesn't // have permission to read its contents - #[cfg(all(unix, not(target_os = "macos")))] + #[cfg(all( + unix, + not(target_os = "macos"), + not(target_os = "android"), + not(target_os = "ios") + ))] #[test] fn test_iteration_errors() { use std::io; diff --git a/crates/nu-system/Cargo.toml b/crates/nu-system/Cargo.toml index 53cebea905..fa0397a73c 100644 --- a/crates/nu-system/Cargo.toml +++ b/crates/nu-system/Cargo.toml @@ -15,7 +15,7 @@ path = "src/main.rs" [dependencies] -[target.'cfg(target_os = "linux")'.dependencies] +[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies] procfs = "0.12.0" users = "0.11" which = "4" @@ -32,4 +32,4 @@ winapi = { version = "0.3.9", features = ["tlhelp32", "fileapi", "handleapi", "i chrono = "0.4" libc = "0.2" ntapi = "0.3" -once_cell = "1.0" \ No newline at end of file +once_cell = "1.0" diff --git a/crates/nu-system/src/lib.rs b/crates/nu-system/src/lib.rs index a8e9728d1f..1ad89d73d5 100644 --- a/crates/nu-system/src/lib.rs +++ b/crates/nu-system/src/lib.rs @@ -1,11 +1,11 @@ -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "android", target_os = "linux"))] mod linux; #[cfg(target_os = "macos")] mod macos; #[cfg(target_os = "windows")] mod windows; -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "android", target_os = "linux"))] pub use self::linux::*; #[cfg(target_os = "macos")] pub use self::macos::*;