From 203ee463c74fd0c7f134c2f1009d9a5accf8fbf9 Mon Sep 17 00:00:00 2001 From: Jan Scheer Date: Sun, 9 May 2021 14:21:15 +0200 Subject: [PATCH] stat/uucore: refactor - move fsext.rs to uucore --- src/uu/stat/Cargo.toml | 4 +- src/uu/stat/src/stat.rs | 9 ++-- src/uucore/Cargo.toml | 1 + src/uucore/src/lib/features.rs | 2 + .../src => uucore/src/lib/features}/fsext.rs | 43 ++++++++++++++++--- src/uucore/src/lib/lib.rs | 2 + tests/by-util/test_stat.rs | 27 ------------ 7 files changed, 47 insertions(+), 41 deletions(-) rename src/{uu/stat/src => uucore/src/lib/features}/fsext.rs (93%) diff --git a/src/uu/stat/Cargo.toml b/src/uu/stat/Cargo.toml index c51f972a9..86b7da139 100644 --- a/src/uu/stat/Cargo.toml +++ b/src/uu/stat/Cargo.toml @@ -16,9 +16,7 @@ path = "src/stat.rs" [dependencies] clap = "2.33" -time = "0.1.40" -libc = "0.2" -uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["entries", "libc", "fs"] } +uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["entries", "libc", "fs", "fsext"] } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } [[bin]] diff --git a/src/uu/stat/src/stat.rs b/src/uu/stat/src/stat.rs index 905058766..8b148d39d 100644 --- a/src/uu/stat/src/stat.rs +++ b/src/uu/stat/src/stat.rs @@ -5,15 +5,16 @@ // For the full copyright and license information, please view the LICENSE file // that was distributed with this source code. -// spell-checker:ignore (ToDO) mtab fsext showfs otype fmtstr prec ftype blocksize nlink rdev fnodes fsid namelen blksize inodes fstype iosize statfs gnulib NBLOCKSIZE - -mod fsext; -pub use crate::fsext::*; +// spell-checker:ignore (ToDO) showfs otype fmtstr prec ftype blocksize nlink rdev fnodes fsid namelen blksize inodes fstype iosize statfs gnulib NBLOCKSIZE #[macro_use] extern crate uucore; use uucore::entries; use uucore::fs::display_permissions; +use uucore::fsext::{ + pretty_filetype, pretty_fstype, pretty_time, read_fs_list, statfs, BirthTime, FsMeta, +}; +use uucore::libc::mode_t; use clap::{App, Arg, ArgMatches}; use std::borrow::Cow; diff --git a/src/uucore/Cargo.toml b/src/uucore/Cargo.toml index 291456760..51bb4c66e 100644 --- a/src/uucore/Cargo.toml +++ b/src/uucore/Cargo.toml @@ -38,6 +38,7 @@ default = [] encoding = ["data-encoding", "thiserror"] entries = ["libc"] fs = ["libc"] +fsext = ["libc", "time"] mode = ["libc"] parse_time = [] perms = ["libc"] diff --git a/src/uucore/src/lib/features.rs b/src/uucore/src/lib/features.rs index c26225cb7..0287b9675 100644 --- a/src/uucore/src/lib/features.rs +++ b/src/uucore/src/lib/features.rs @@ -4,6 +4,8 @@ pub mod encoding; #[cfg(feature = "fs")] pub mod fs; +#[cfg(feature = "fsext")] +pub mod fsext; #[cfg(feature = "parse_time")] pub mod parse_time; #[cfg(feature = "zero-copy")] diff --git a/src/uu/stat/src/fsext.rs b/src/uucore/src/lib/features/fsext.rs similarity index 93% rename from src/uu/stat/src/fsext.rs rename to src/uucore/src/lib/features/fsext.rs index e831a159e..3c95af73e 100644 --- a/src/uu/stat/src/fsext.rs +++ b/src/uucore/src/lib/features/fsext.rs @@ -9,6 +9,8 @@ extern crate time; +pub use crate::*; // import macros from `../../macros.rs` + #[cfg(target_os = "linux")] static LINUX_MTAB: &str = "/etc/mtab"; #[cfg(target_os = "linux")] @@ -16,12 +18,12 @@ static LINUX_MOUNTINFO: &str = "/proc/self/mountinfo"; static MOUNT_OPT_BIND: &str = "bind"; use self::time::Timespec; -use std::time::UNIX_EPOCH; -pub use uucore::libc::{ +pub use libc::{ c_int, mode_t, strerror, S_IFBLK, S_IFCHR, S_IFDIR, S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, S_IFSOCK, S_IRGRP, S_IROTH, S_IRUSR, S_ISGID, S_ISUID, S_ISVTX, S_IWGRP, S_IWOTH, S_IWUSR, S_IXGRP, S_IXOTH, S_IXUSR, }; +use std::time::UNIX_EPOCH; pub trait BirthTime { fn pretty_birth(&self) -> String; @@ -93,7 +95,7 @@ use std::path::Path; target_os = "android", target_os = "freebsd" ))] -use uucore::libc::statfs as Sstatfs; +use libc::statfs as Sstatfs; #[cfg(any( target_os = "openbsd", target_os = "netbsd", @@ -101,7 +103,7 @@ use uucore::libc::statfs as Sstatfs; target_os = "bitrig", target_os = "dragonfly" ))] -use uucore::libc::statvfs as Sstatfs; +use libc::statvfs as Sstatfs; #[cfg(any( target_os = "linux", @@ -109,7 +111,7 @@ use uucore::libc::statvfs as Sstatfs; target_os = "android", target_os = "freebsd" ))] -use uucore::libc::statfs as statfs_fn; +use libc::statfs as statfs_fn; #[cfg(any( target_os = "openbsd", target_os = "netbsd", @@ -117,7 +119,7 @@ use uucore::libc::statfs as statfs_fn; target_os = "bitrig", target_os = "dragonfly" ))] -use uucore::libc::statvfs as statfs_fn; +use libc::statvfs as statfs_fn; pub trait FsMeta { fn fs_type(&self) -> i64; @@ -184,7 +186,7 @@ impl FsMeta for Sstatfs { #[cfg(any(target_vendor = "apple", target_os = "freebsd", target_os = "linux"))] fn fsid(&self) -> u64 { let f_fsid: &[u32; 2] = - unsafe { &*(&self.f_fsid as *const uucore::libc::fsid_t as *const [u32; 2]) }; + unsafe { &*(&self.f_fsid as *const libc::fsid_t as *const [u32; 2]) }; (u64::from(f_fsid[0])) << 32 | u64::from(f_fsid[1]) } #[cfg(not(any(target_vendor = "apple", target_os = "freebsd", target_os = "linux")))] @@ -534,3 +536,30 @@ pub fn read_fs_list() -> Vec { .collect::>() } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_file_type() { + assert_eq!("block special file", pretty_filetype(S_IFBLK, 0)); + assert_eq!("character special file", pretty_filetype(S_IFCHR, 0)); + assert_eq!("regular file", pretty_filetype(S_IFREG, 1)); + assert_eq!("regular empty file", pretty_filetype(S_IFREG, 0)); + assert_eq!("weird file", pretty_filetype(0, 0)); + } + + #[test] + fn test_fs_type() { + assert_eq!("ext2/ext3", pretty_fstype(0xEF53)); + assert_eq!("tmpfs", pretty_fstype(0x01021994)); + assert_eq!("nfs", pretty_fstype(0x6969)); + assert_eq!("btrfs", pretty_fstype(0x9123683e)); + assert_eq!("xfs", pretty_fstype(0x58465342)); + assert_eq!("zfs", pretty_fstype(0x2FC12FC1)); + assert_eq!("ntfs", pretty_fstype(0x5346544e)); + assert_eq!("fat", pretty_fstype(0x4006)); + assert_eq!("UNKNOWN (0x1234)", pretty_fstype(0x1234)); + } +} diff --git a/src/uucore/src/lib/lib.rs b/src/uucore/src/lib/lib.rs index 6dddf8696..f2a4292fb 100644 --- a/src/uucore/src/lib/lib.rs +++ b/src/uucore/src/lib/lib.rs @@ -35,6 +35,8 @@ pub use crate::mods::ranges; pub use crate::features::encoding; #[cfg(feature = "fs")] pub use crate::features::fs; +#[cfg(feature = "fsext")] +pub use crate::features::fsext; #[cfg(feature = "parse_time")] pub use crate::features::parse_time; #[cfg(feature = "zero-copy")] diff --git a/tests/by-util/test_stat.rs b/tests/by-util/test_stat.rs index 569d6873e..308dcb9f5 100644 --- a/tests/by-util/test_stat.rs +++ b/tests/by-util/test_stat.rs @@ -5,33 +5,6 @@ use crate::common::util::*; extern crate stat; pub use self::stat::*; -#[cfg(test)] -mod test_fsext { - use super::*; - - #[test] - fn test_file_type() { - assert_eq!("block special file", pretty_filetype(S_IFBLK, 0)); - assert_eq!("character special file", pretty_filetype(S_IFCHR, 0)); - assert_eq!("regular file", pretty_filetype(S_IFREG, 1)); - assert_eq!("regular empty file", pretty_filetype(S_IFREG, 0)); - assert_eq!("weird file", pretty_filetype(0, 0)); - } - - #[test] - fn test_fs_type() { - assert_eq!("ext2/ext3", pretty_fstype(0xEF53)); - assert_eq!("tmpfs", pretty_fstype(0x01021994)); - assert_eq!("nfs", pretty_fstype(0x6969)); - assert_eq!("btrfs", pretty_fstype(0x9123683e)); - assert_eq!("xfs", pretty_fstype(0x58465342)); - assert_eq!("zfs", pretty_fstype(0x2FC12FC1)); - assert_eq!("ntfs", pretty_fstype(0x5346544e)); - assert_eq!("fat", pretty_fstype(0x4006)); - assert_eq!("UNKNOWN (0x1234)", pretty_fstype(0x1234)); - } -} - #[test] fn test_scanutil() { assert_eq!(Some((-5, 2)), "-5zxc".scan_num::());