1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

Move path_ends_with_terminator from mv into uucore

This commit is contained in:
Sylvestre Ledru 2024-01-04 00:24:08 +01:00
parent 4c698d58e0
commit 108dc4a0cd
2 changed files with 23 additions and 20 deletions

View file

@ -23,7 +23,10 @@ use std::path::{Path, PathBuf};
use uucore::backup_control::{self, source_is_target_backup}; use uucore::backup_control::{self, source_is_target_backup};
use uucore::display::Quotable; use uucore::display::Quotable;
use uucore::error::{set_exit_code, FromIo, UResult, USimpleError, UUsageError}; use uucore::error::{set_exit_code, FromIo, UResult, USimpleError, UUsageError};
use uucore::fs::{are_hardlinks_or_one_way_symlink_to_same_file, are_hardlinks_to_same_file}; use uucore::fs::{
are_hardlinks_or_one_way_symlink_to_same_file, are_hardlinks_to_same_file,
path_ends_with_terminator,
};
use uucore::update_control; use uucore::update_control;
// These are exposed for projects (e.g. nushell) that want to create an `Options` value, which // These are exposed for projects (e.g. nushell) that want to create an `Options` value, which
// requires these enums // requires these enums
@ -104,25 +107,6 @@ static OPT_VERBOSE: &str = "verbose";
static OPT_PROGRESS: &str = "progress"; static OPT_PROGRESS: &str = "progress";
static ARG_FILES: &str = "files"; static ARG_FILES: &str = "files";
/// Returns true if the passed `path` ends with a path terminator.
#[cfg(unix)]
fn path_ends_with_terminator(path: &Path) -> bool {
use std::os::unix::prelude::OsStrExt;
path.as_os_str()
.as_bytes()
.last()
.map_or(false, |&byte| byte == b'/' || byte == b'\\')
}
#[cfg(windows)]
fn path_ends_with_terminator(path: &Path) -> bool {
use std::os::windows::prelude::OsStrExt;
path.as_os_str()
.encode_wide()
.last()
.map_or(false, |wide| wide == b'/'.into() || wide == b'\\'.into())
}
#[uucore::main] #[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> { pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let mut app = uu_app(); let mut app = uu_app();

View file

@ -714,6 +714,25 @@ pub fn are_hardlinks_or_one_way_symlink_to_same_file(source: &Path, target: &Pat
source_metadata.ino() == target_metadata.ino() && source_metadata.dev() == target_metadata.dev() source_metadata.ino() == target_metadata.ino() && source_metadata.dev() == target_metadata.dev()
} }
/// Returns true if the passed `path` ends with a path terminator.
#[cfg(unix)]
pub fn path_ends_with_terminator(path: &Path) -> bool {
use std::os::unix::prelude::OsStrExt;
path.as_os_str()
.as_bytes()
.last()
.map_or(false, |&byte| byte == b'/' || byte == b'\\')
}
#[cfg(windows)]
pub fn path_ends_with_terminator(path: &Path) -> bool {
use std::os::windows::prelude::OsStrExt;
path.as_os_str()
.encode_wide()
.last()
.map_or(false, |wide| wide == b'/'.into() || wide == b'\\'.into())
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope. // Note this useful idiom: importing names from outer (for mod tests) scope.