From f6edea2d0565d476d6725f57f20bb4b4abf16f20 Mon Sep 17 00:00:00 2001 From: Benjamin Bara Date: Sat, 18 Feb 2023 15:44:49 +0100 Subject: [PATCH] tail: enable non-utf8 paths --- src/uu/tail/src/args.rs | 12 ++++++++---- src/uu/tail/src/paths.rs | 13 +++++++------ tests/by-util/test_tail.rs | 16 ---------------- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/src/uu/tail/src/args.rs b/src/uu/tail/src/args.rs index f18f99d1b..7bf06f853 100644 --- a/src/uu/tail/src/args.rs +++ b/src/uu/tail/src/args.rs @@ -13,6 +13,7 @@ use fundu::DurationParser; use is_terminal::IsTerminal; use same_file::Handle; use std::collections::VecDeque; +use std::ffi::OsString; use std::time::Duration; use uucore::error::{UResult, USimpleError, UUsageError}; use uucore::parse_size::{parse_size, ParseSizeError}; @@ -144,7 +145,7 @@ pub struct Settings { } impl Settings { - pub fn from_obsolete_args(args: &parse::ObsoleteArgs, name: Option<&str>) -> Self { + pub fn from_obsolete_args(args: &parse::ObsoleteArgs, name: Option) -> Self { let mut settings: Self = Self { sleep_sec: Duration::from_secs_f32(1.0), max_unchanged_stats: 5, @@ -159,7 +160,7 @@ impl Settings { } settings.mode = FilterMode::from_obsolete_args(args); let input = if let Some(name) = name { - Input::from(name.to_string()) + Input::from(&name) } else { Input::default() }; @@ -249,7 +250,7 @@ impl Settings { let mut inputs: VecDeque = matches .get_many::(options::ARG_FILES) - .map(|v| v.map(|string| Input::from(string.clone())).collect()) + .map(|v| v.map(|string| Input::from(&string)).collect()) .unwrap_or_default(); // apply default and add '-' to inputs if none is present @@ -575,7 +576,10 @@ mod tests { #[test] fn test_parse_obsolete_settings_f() { - let args = ObsoleteArgs { follow: true, ..Default::default() }; + let args = ObsoleteArgs { + follow: true, + ..Default::default() + }; let result = Settings::from_obsolete_args(&args, None); assert_eq!(result.follow, Some(FollowMode::Descriptor)); diff --git a/src/uu/tail/src/paths.rs b/src/uu/tail/src/paths.rs index d8e6ece9a..b1cbc9bb8 100644 --- a/src/uu/tail/src/paths.rs +++ b/src/uu/tail/src/paths.rs @@ -6,6 +6,7 @@ // spell-checker:ignore tailable seekable stdlib (stdlib) use crate::text; +use std::ffi::OsStr; use std::fs::{File, Metadata}; use std::io::{Seek, SeekFrom}; #[cfg(unix)] @@ -26,21 +27,21 @@ pub struct Input { } impl Input { - // TODO: from &str may be the better choice - pub fn from(string: String) -> Self { - let kind = if string == text::DASH { + pub fn from>(string: &T) -> Self { + let valid_string = string.as_ref().to_str(); + let kind = if valid_string.is_some() && valid_string.unwrap() == text::DASH { InputKind::Stdin } else { - InputKind::File(PathBuf::from(&string)) + InputKind::File(PathBuf::from(string.as_ref())) }; let display_name = match kind { - InputKind::File(_) => string, + InputKind::File(_) => string.as_ref().to_string_lossy().to_string(), InputKind::Stdin => { if cfg!(unix) { text::STDIN_HEADER.to_string() } else { - string + string.as_ref().to_string_lossy().to_string() } } }; diff --git a/tests/by-util/test_tail.rs b/tests/by-util/test_tail.rs index d1e5ba632..b450d303b 100644 --- a/tests/by-util/test_tail.rs +++ b/tests/by-util/test_tail.rs @@ -4768,7 +4768,6 @@ fn test_obsolete_encoding_unix() { let scene = TestScenario::new(util_name!()); let invalid_utf8_arg = OsStr::from_bytes(&[b'-', INVALID_UTF8, b'b']); - let valid_utf8_arg = OsStr::from_bytes(&[b'-', b'b']); scene .ucmd() @@ -4777,13 +4776,6 @@ fn test_obsolete_encoding_unix() { .no_stdout() .stderr_is("tail: bad argument encoding: '-�b'\n") .code_is(1); - scene - .ucmd() - .args(&[valid_utf8_arg, invalid_utf8_arg]) - .fails() - .no_stdout() - .stderr_is("tail: bad argument encoding\n") - .code_is(1); } #[test] @@ -4794,7 +4786,6 @@ fn test_obsolete_encoding_windows() { let scene = TestScenario::new(util_name!()); let invalid_utf16_arg = OsString::from_wide(&['-' as u16, INVALID_UTF16, 'b' as u16]); - let valid_utf16_arg = OsString::from("-b"); scene .ucmd() @@ -4803,11 +4794,4 @@ fn test_obsolete_encoding_windows() { .no_stdout() .stderr_is("tail: bad argument encoding: '-�b'\n") .code_is(1); - scene - .ucmd() - .args(&[&valid_utf16_arg, &invalid_utf16_arg]) - .fails() - .no_stdout() - .stderr_is("tail: bad argument encoding\n") - .code_is(1); }