From 39e903b1d389878a6bc356f6693e3579498902b8 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Sun, 7 Jul 2024 18:24:45 +0300 Subject: [PATCH] melib/utils: fix issues with ShellExpandTrait ShellExpandTrait was not expanding paths properly: tilde was expanded even if it wasn't the first byte in the sequence. Since $HOME tends to be an absolute path, the entire value of the path accumulator up till that point was overwritten, meaning a /path/to/~/some/file would get expanded to ${HOME}/some/file. ShellExpandTrait was also not completing paths properly, especially between the generic impl and the linux specialized one. This commit adds test to make sure their behavior is synced and changes the result type to an enum to make the result more descriptive. Concerns #431. Signed-off-by: Manos Pitsidianakis --- Cargo.lock | 1 + meli/src/command.rs | 2 +- meli/src/utilities.rs | 11 +- melib/Cargo.toml | 1 + melib/src/utils/mod.rs | 2 + melib/src/utils/shellexpand.rs | 285 +++++++++++++++++++++++---------- melib/src/utils/tests.rs | 254 +++++++++++++++++++++++++++++ 7 files changed, 463 insertions(+), 93 deletions(-) create mode 100644 melib/src/utils/tests.rs diff --git a/Cargo.lock b/Cargo.lock index a88b0b4e..78721478 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1371,6 +1371,7 @@ dependencies = [ "smol", "socket2 0.5.5", "stderrlog", + "tempfile", "unicode-segmentation", "url", "uuid", diff --git a/meli/src/command.rs b/meli/src/command.rs index 63d0b32f..d07eb322 100644 --- a/meli/src/command.rs +++ b/meli/src/command.rs @@ -548,7 +548,7 @@ pub fn command_completion_suggestions(input: &str) -> Vec { } if let Some((s, Filepath)) = _m.last() { let p = std::path::Path::new(s); - sugg.extend(p.complete(true).into_iter()); + sugg.extend(p.complete(true, s.ends_with('/')).into_iter()); } } sugg.into_iter() diff --git a/meli/src/utilities.rs b/meli/src/utilities.rs index d8b6042e..960c3565 100644 --- a/meli/src/utilities.rs +++ b/meli/src/utilities.rs @@ -317,15 +317,10 @@ impl Component for StatusBar { } })); */ - if let Some(p) = self - .ex_buffer - .as_str() - .split_whitespace() - .last() - .map(std::path::Path::new) - { + if let Some(p) = self.ex_buffer.as_str().split_whitespace().last() { + let path = std::path::Path::new(p); suggestions.extend( - p.complete(true) + path.complete(true, p.ends_with('/')) .into_iter() .map(|m| format!("{}{}", self.ex_buffer.as_str(), m).into()), ); diff --git a/melib/Cargo.toml b/melib/Cargo.toml index 626b6da1..c8935625 100644 --- a/melib/Cargo.toml +++ b/melib/Cargo.toml @@ -85,3 +85,4 @@ flate2 = { version = "1.0.16" } [dev-dependencies] mailin-embedded = { version = "0.8", features = ["rtls"] } stderrlog = "^0.5" +tempfile = "3.3" diff --git a/melib/src/utils/mod.rs b/melib/src/utils/mod.rs index d92c7e2c..766dc8b6 100644 --- a/melib/src/utils/mod.rs +++ b/melib/src/utils/mod.rs @@ -32,6 +32,8 @@ pub mod percent_encoding; pub mod shellexpand; #[cfg(feature = "sqlite3")] pub mod sqlite3; +#[cfg(test)] +mod tests; pub mod xdg; pub mod html_escape { diff --git a/melib/src/utils/shellexpand.rs b/melib/src/utils/shellexpand.rs index 4a378f65..55193599 100644 --- a/melib/src/utils/shellexpand.rs +++ b/melib/src/utils/shellexpand.rs @@ -19,7 +19,7 @@ * along with meli. If not, see . */ -//! A `ShellExpandTrait` to expand paths like a shell. +//! A [`ShellExpandTrait`] to expand paths like a shell. use std::{ ffi::OsStr, @@ -27,15 +27,48 @@ use std::{ path::{Path, PathBuf}, }; -use smallvec::SmallVec; +/// The return type of [`ShellExpandTrait`]. +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum Completions { + /// Completion entries, e.g. strings that if appended to the path being + /// completed, would result in all valid paths that exist with the path + /// as a prefix. + Entries(Vec), + /// Path is a file and there are no completions. + IsFile, + /// Path is a directory and there are no completions. + IsDirectory, + /// Path is invalid and there are no completions. + IsInvalid, +} + +impl IntoIterator for Completions { + type Item = String; + type IntoIter = as IntoIterator>::IntoIter; -// [ref:needs_dev_doc]: ShellExpandTrait -// [ref:needs_unit_test]: ShellExpandTrait + fn into_iter(self) -> Self::IntoIter { + match self { + Self::Entries(e) => e.into_iter(), + _ => Vec::default().into_iter(), + } + } +} + +/// Utility trait to expand paths like an interactive shell does. pub trait ShellExpandTrait { - // [ref:needs_dev_doc] + /// Expands `~` to the content of `${HOME}` and environment variables to + /// their content. fn expand(&self) -> PathBuf; - // [ref:needs_dev_doc] - fn complete(&self, force: bool) -> SmallVec<[String; 128]>; + /// Returns what completions are available for this path depending on the + /// input options given. + /// + /// `force`: whether to force completion if `self` is a directory. If + /// `false` and `self` is a directory, it returns + /// `Completions::IsDirectory`. Otherwise the return value depends on + /// the value of `treat_as_dir`. + /// `treat_as_dir`: whether to treat the last path component in `self` as a + /// pattern or a directory name. + fn complete(&self, force: bool, treat_as_dir: bool) -> Completions; } impl ShellExpandTrait for Path { @@ -46,9 +79,9 @@ impl ShellExpandTrait for Path { for c in self.components() { let c_to_str = c.as_os_str(); match c_to_str { - tilde if tilde == "~" => { + tilde if tilde == "~" && ret.as_os_str().is_empty() => { if let Ok(home_dir) = std::env::var("HOME") { - ret.push(home_dir) + ret.push(home_dir); } else { // POSIX says that if HOME is unset, the results of tilde expansion is // unspecified. @@ -74,29 +107,131 @@ impl ShellExpandTrait for Path { ret } + fn complete(&self, force: bool, treat_as_dir: bool) -> Completions { + #[cfg(not(target_os = "linux"))] + { + impls::inner_complete_generic(self, force, treat_as_dir) + } + #[cfg(target_os = "linux")] + { + impls::inner_complete_linux(self, force, treat_as_dir) + } + } +} + +pub mod impls { + //! Inner implementations for [`ShellExpandTrait`]'s complete function. + use super::*; + + /// Completing implementation for all platforms. + pub fn inner_complete_generic(_self: &Path, force: bool, treat_as_dir: bool) -> Completions { + let mut entries = Vec::new(); + let (prefix, _match) = { + if _self.exists() + && !treat_as_dir + && (!force || _self.as_os_str().as_bytes().ends_with(b"/")) + { + return Completions::IsDirectory; + } else if treat_as_dir { + (_self, OsStr::from_bytes(b"")) + } else { + let last_component = _self + .components() + .last() + .map(|c| c.as_os_str()) + .unwrap_or_else(|| OsStr::from_bytes(b"")); + let prefix = if let Some(p) = _self.parent() { + p + } else { + return Completions::IsInvalid; + }; + (prefix, last_component) + } + }; + + if let Ok(iter) = std::fs::read_dir(prefix) { + for entry in iter.flatten() { + if entry.path().as_os_str().as_bytes() != b"." + && entry.path().as_os_str().as_bytes() != b".." + && entry + .path() + .as_os_str() + .ext_trim_prefix(prefix.as_os_str()) + .ext_starts_with(_match) + { + if entry.path().is_dir() && !entry.path().as_os_str().as_bytes().ends_with(b"/") + { + let mut s = unsafe { + String::from_utf8_unchecked( + entry + .path() + .as_os_str() + .ext_trim_prefix(prefix.as_os_str()) + .as_bytes()[_match.as_bytes().len()..] + .to_vec(), + ) + }; + s.push('/'); + entries.push(s); + } else { + entries.push(unsafe { + String::from_utf8_unchecked( + entry + .path() + .as_os_str() + .ext_trim_prefix(prefix.as_os_str()) + .as_bytes()[_match.as_bytes().len()..] + .to_vec(), + ) + }); + } + } + } + } + + if force && treat_as_dir { + for entry in &mut entries { + let entry: &mut String = entry; + if entry.starts_with(std::path::MAIN_SEPARATOR) { + entry.remove(0); + } + } + } + + entries.sort(); + Completions::Entries(entries) + } + #[cfg(target_os = "linux")] - fn complete(&self, force: bool) -> SmallVec<[String; 128]> { + /// Completing implementation for Linux only, because it uses the + /// [`getdents64`](::libc::SYS_getdents64) to get results faster, since we + /// only care for raw name byte matching. + pub fn inner_complete_linux(_self: &Path, force: bool, treat_as_dir: bool) -> Completions { use std::os::unix::io::AsRawFd; - use libc::dirent64; use nix::fcntl::OFlag; const BUF_SIZE: ::libc::size_t = 8 << 10; - let (prefix, _match) = if self.as_os_str().as_bytes().ends_with(b"/.") { - (self.components().as_path(), OsStr::from_bytes(b".")) - } else if self.exists() && (!force || self.as_os_str().as_bytes().ends_with(b"/")) { - return SmallVec::new(); + let (prefix, _match): (&Path, &OsStr) = if _self.as_os_str().as_bytes().ends_with(b"/.") { + (_self.components().as_path(), OsStr::from_bytes(b".")) + } else if _self.exists() + && !treat_as_dir + && (!force || _self.as_os_str().as_bytes().ends_with(b"/")) + { + return Completions::IsDirectory; + } else if treat_as_dir { + (_self, OsStr::from_bytes(b"")) } else { - let last_component = self + let last_component = _self .components() .last() .map(|c| c.as_os_str()) .unwrap_or_else(|| OsStr::from_bytes(b"")); - let prefix = if let Some(p) = self.parent() { + let prefix = if let Some(p) = _self.parent() { p } else { - return SmallVec::new(); + return Completions::IsInvalid; }; (prefix, last_component) }; @@ -119,24 +254,24 @@ impl ShellExpandTrait for Path { Err(err) => { debug!(prefix); debug!(err); - return SmallVec::new(); + return Completions::IsInvalid; } }; - let mut buf: Vec = Vec::with_capacity(BUF_SIZE); - let mut entries = SmallVec::new(); + let mut buf: Vec = vec![0; BUF_SIZE * std::mem::size_of::<::libc::dirent64>()]; + let mut entries = Vec::new(); loop { let n: i64 = unsafe { ::libc::syscall( ::libc::SYS_getdents64, dir.as_raw_fd(), buf.as_ptr(), - BUF_SIZE - 256, + BUF_SIZE, ) }; let Ok(n) = usize::try_from(n) else { - return SmallVec::new(); + return Completions::IsInvalid; }; if n == 0 { break; @@ -146,7 +281,7 @@ impl ShellExpandTrait for Path { } let mut pos = 0; while pos < n { - let dir = unsafe { std::mem::transmute::<&[u8], &[dirent64]>(&buf[pos..]) }; + let dir = unsafe { std::mem::transmute::<&[u8], &[::libc::dirent64]>(&buf[pos..]) }; let entry = unsafe { std::ffi::CStr::from_ptr(dir[0].d_name.as_ptr()) }; if entry.to_bytes() != b"." && entry.to_bytes() != b".." @@ -179,71 +314,53 @@ impl ShellExpandTrait for Path { // It sometimes writes a partial list of entries even if the // full list would fit." } - entries + + entries.sort(); + Completions::Entries(entries) } - #[cfg(not(target_os = "linux"))] - fn complete(&self, force: bool) -> SmallVec<[String; 128]> { - let mut entries = SmallVec::new(); - let (prefix, _match) = { - if self.exists() && (!force || self.as_os_str().as_bytes().ends_with(b"/")) { - // println!("{} {:?}", self.display(), self.components().last()); - return entries; - } else { - let last_component = self - .components() - .last() - .map(|c| c.as_os_str()) - .unwrap_or_else(|| OsStr::from_bytes(b"")); - let prefix = if let Some(p) = self.parent() { - p - } else { - return entries; - }; - (prefix, last_component) - } - }; - if force && self.is_dir() && !self.as_os_str().as_bytes().ends_with(b"/") { - entries.push("/".to_string()); + trait AsBytesExt { + fn as_bytes_ext(&self) -> &[u8]; + } + + impl AsBytesExt for [u8] { + #[inline] + fn as_bytes_ext(&self) -> &[u8] { + self } + } - if let Ok(iter) = std::fs::read_dir(prefix) { - for entry in iter.flatten() { - if entry.path().as_os_str().as_bytes() != b"." - && entry.path().as_os_str().as_bytes() != b".." - && entry - .path() - .as_os_str() - .as_bytes() - .starts_with(_match.as_bytes()) - { - if entry.path().is_dir() && !entry.path().as_os_str().as_bytes().ends_with(b"/") - { - let mut s = unsafe { - String::from_utf8_unchecked( - entry.path().as_os_str().as_bytes()[_match.as_bytes().len()..] - .to_vec(), - ) - }; - s.push('/'); - entries.push(s); - } else { - entries.push(unsafe { - String::from_utf8_unchecked( - entry.path().as_os_str().as_bytes()[_match.as_bytes().len()..] - .to_vec(), - ) - }); - } - } - } + impl AsBytesExt for OsStr { + #[inline] + fn as_bytes_ext(&self) -> &[u8] { + self.as_bytes() } - entries } -} -#[test] -fn test_shellexpandtrait() { - assert!(Path::new("~").expand().complete(false).is_empty()); - assert!(!Path::new("~").expand().complete(true).is_empty()); + trait TrimExt { + fn ext_trim_prefix<'s>(&'s self, prefix: &Self) -> &'s Self; + fn ext_starts_with(&self, prefix: &B) -> bool; + } + + impl TrimExt for OsStr { + #[inline] + fn ext_trim_prefix<'s>(&'s self, prefix: &Self) -> &'s Self { + Self::from_bytes( + self.as_bytes() + .strip_prefix(prefix.as_bytes()) + .and_then(|s| s.strip_prefix(b"/")) + .unwrap_or_else(|| { + self.as_bytes() + .strip_prefix(b"/") + .unwrap_or_else(|| self.as_bytes()) + }), + ) + } + + #[inline] + fn ext_starts_with(&self, prefix: &B) -> bool { + let prefix = prefix.as_bytes_ext(); + self.as_bytes().starts_with(prefix) + } + } } diff --git a/melib/src/utils/tests.rs b/melib/src/utils/tests.rs new file mode 100644 index 00000000..4c3170fa --- /dev/null +++ b/melib/src/utils/tests.rs @@ -0,0 +1,254 @@ +// +// meli +// +// Copyright 2024 Emmanouil Pitsidianakis +// +// This file is part of meli. +// +// meli is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// meli is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with meli. If not, see . +// +// SPDX-License-Identifier: EUPL-1.2 OR GPL-3.0-or-later + +#[test] +fn test_shellexpandtrait() { + use std::{fs::File, io::Write, os::unix::fs::PermissionsExt, path::Path}; + + use tempfile::TempDir; + + use super::shellexpand::*; + + fn create_file_util(path: &str) { + let path = Path::new(path).expand(); + let mut f = File::create(&path).unwrap(); + let mut permissions = f.metadata().unwrap().permissions(); + permissions.set_mode(0o600); // Read/write for owner only. + f.set_permissions(permissions).unwrap(); + f.write_all(b"\n").unwrap(); + f.flush().unwrap(); + assert!(path.exists()); + } + + fn create_dir_util(path: &str) { + let path = Path::new(path).expand(); + std::fs::create_dir(&path).unwrap(); + assert!(path.exists()); + assert!(path.is_dir()); + } + + std::env::remove_var("HOME"); + + let tmp_dir = TempDir::new().unwrap(); + + std::env::set_var("HOME", tmp_dir.path()); + + assert_eq!(&Path::new("~").expand(), tmp_dir.path()); + + assert_eq!( + &tmp_dir.path().join("~/doc.pdf").expand(), + &tmp_dir.path().join("~/doc.pdf") + ); + + assert_eq!( + Path::new("~").expand().complete(false, false), + Completions::IsDirectory + ); + assert_eq!( + Path::new("~").expand().complete(true, false), + Completions::Entries(vec!["/".to_string()]), + r#" ~ should append a "/": "~/""#, + ); + assert_eq!( + Path::new("~").expand().complete(true, true), + Completions::Entries(vec![]), + r#"~/ should return directory entries."# + ); + + create_file_util("~/doc.pdf"); + + assert_eq!( + Path::new("~").expand().complete(false, false), + Completions::IsDirectory + ); + assert_eq!( + Path::new("~").expand().complete(true, false), + Completions::Entries(vec!["/".to_string()]), + r#"~ should again append a "/": "~/""# + ); + assert_eq!( + Path::new("~").expand().complete(true, true), + Completions::Entries(vec!["doc.pdf".to_string()]), + r#"~/ should return directory entries."# + ); + assert_eq!( + Path::new("~/doc").expand().complete(true, false), + Completions::Entries(vec![".pdf".to_string()]), + r#"~/pattern should return directory entries matching ~/pattern* glob."# + ); + create_file_util("~/doc2.pdf"); + assert_eq!( + Path::new("~/doc").expand().complete(true, false), + Completions::Entries(vec![".pdf".to_string(), "2.pdf".to_string()]), + r#"~/pattern should again return directory entries matching ~/pattern* glob."# + ); + create_dir_util("~/doc"); + create_file_util("~/doc/2.pdf"); + assert_eq!( + Path::new("~/doc").expand().complete(true, false), + Completions::Entries(vec![ + ".pdf".to_string(), + "/".to_string(), + "2.pdf".to_string(), + ]), + r#"~/pattern should again return directory entries matching ~/pattern* glob."# + ); + assert_eq!( + Path::new("~/doc").expand().complete(true, false), + Completions::Entries(vec![ + ".pdf".to_string(), + "/".to_string(), + "2.pdf".to_string(), + ]), + r#"~/pattern should again return directory entries matching ~/pattern* glob."# + ); + assert_eq!( + Path::new("~/doc").expand().complete(false, false), + Completions::IsDirectory, + r#"~/doc/ should not return ~/ entries matching ~/doc* glob."# + ); + assert_eq!( + Path::new("~/doc").expand().complete(true, true), + Completions::Entries(vec!["2.pdf".to_string()]) + ); + assert_eq!( + Path::new("/").expand().complete(false, false), + Completions::IsDirectory + ); + assert_eq!( + Path::new("/").expand().complete(true, false), + Completions::IsDirectory + ); + assert!(matches!(Path::new("/").expand().complete(true, true), + Completions::Entries(entries) if !entries.is_empty())); +} + +#[cfg(target_os = "linux")] +#[test] +fn test_shellexpandtrait_impls() { + use std::{fs::File, io::Write, os::unix::fs::PermissionsExt, path::Path}; + + use tempfile::TempDir; + + use super::shellexpand::*; + + fn create_file_util(path: &str) { + let path = Path::new(path).expand(); + let mut f = File::create(&path).unwrap(); + let mut permissions = f.metadata().unwrap().permissions(); + permissions.set_mode(0o600); // Read/write for owner only. + f.set_permissions(permissions).unwrap(); + f.write_all(b"\n").unwrap(); + f.flush().unwrap(); + assert!(path.exists()); + } + + fn create_dir_util(path: &str) { + let path = Path::new(path).expand(); + std::fs::create_dir(&path).unwrap(); + assert!(path.exists()); + assert!(path.is_dir()); + } + + std::env::remove_var("HOME"); + + let tmp_dir = TempDir::new().unwrap(); + + std::env::set_var("HOME", tmp_dir.path()); + + macro_rules! assert_complete { + (($path:expr, $force:literal, $treat_as_dir:literal), $($expected:tt)*) => {{ + assert_eq!( + impls::inner_complete_linux($path, $force, $treat_as_dir), + impls::inner_complete_generic($path, $force, $treat_as_dir), + concat!("Expected ", stringify!($($expected)*)), + ); + assert_eq!( + impls::inner_complete_generic($path, $force, $treat_as_dir), + $($expected)*, + ); + }}; + } + + assert_complete!( + (&Path::new("~").expand(), false, false), + Completions::IsDirectory + ); + assert_complete!( + (&Path::new("~").expand(), true, false), + Completions::Entries(vec!["/".to_string()]) + ); + assert_complete!( + (&Path::new("~").expand(), true, true), + Completions::Entries(vec![]) + ); + + create_file_util("~/doc.pdf"); + + assert_complete!( + (&Path::new("~").expand(), false, false), + Completions::IsDirectory + ); + assert_complete!( + (&Path::new("~").expand(), true, false), + Completions::Entries(vec!["/".to_string()]) + ); + assert_complete!( + (&Path::new("~").expand(), true, true), + Completions::Entries(vec!["doc.pdf".to_string()]) + ); + assert_complete!( + (&Path::new("~/doc").expand(), true, false), + Completions::Entries(vec![".pdf".to_string()]) + ); + create_file_util("~/doc2.pdf"); + assert_complete!( + (&Path::new("~/doc").expand(), true, false), + Completions::Entries(vec![".pdf".to_string(), "2.pdf".to_string()]) + ); + create_dir_util("~/doc"); + create_file_util("~/doc/2.pdf"); + assert_complete!( + (&Path::new("~/doc").expand(), true, false), + Completions::Entries(vec![ + ".pdf".to_string(), + "/".to_string(), + "2.pdf".to_string(), + ]) + ); + assert_complete!( + (&Path::new("~/doc").expand(), false, false), + Completions::IsDirectory + ); + assert_complete!( + (&Path::new("~/doc").expand(), true, true), + Completions::Entries(vec!["2.pdf".to_string()]) + ); + assert_complete!( + (&Path::new("/").expand(), false, false), + Completions::IsDirectory + ); + assert_complete!( + (&Path::new("/").expand(), true, false), + Completions::IsDirectory + ); +}