file.rs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. //! Files, and methods and fields to access their metadata.
  2. #[cfg(unix)]
  3. use std::collections::HashMap;
  4. use std::io;
  5. #[cfg(unix)]
  6. use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt};
  7. #[cfg(windows)]
  8. use std::os::windows::fs::MetadataExt;
  9. use std::path::{Path, PathBuf};
  10. #[cfg(unix)]
  11. use std::sync::Mutex;
  12. use std::sync::OnceLock;
  13. use chrono::prelude::*;
  14. use log::*;
  15. #[cfg(unix)]
  16. use once_cell::sync::Lazy;
  17. use crate::fs::dir::Dir;
  18. use crate::fs::feature::xattr;
  19. use crate::fs::feature::xattr::{Attribute, FileAttributes};
  20. use crate::fs::fields as f;
  21. use crate::fs::recursive_size::RecursiveSize;
  22. use super::mounts::all_mounts;
  23. use super::mounts::MountedFs;
  24. // Maps (device_id, inode) => (size_in_bytes, size_in_blocks)
  25. // Mutex::new is const but HashMap::new is not const requiring us to use lazy
  26. // initialization.
  27. // TODO: Replace with std::sync::LazyLock when it is stable.
  28. #[allow(clippy::type_complexity)]
  29. #[cfg(unix)]
  30. static DIRECTORY_SIZE_CACHE: Lazy<Mutex<HashMap<(u64, u64), (u64, u64)>>> =
  31. Lazy::new(|| Mutex::new(HashMap::new()));
  32. /// A **File** is a wrapper around one of Rust’s `PathBuf` values, along with
  33. /// associated data about the file.
  34. ///
  35. /// Each file is definitely going to have its filename displayed at least
  36. /// once, have its file extension extracted at least once, and have its metadata
  37. /// information queried at least once, so it makes sense to do all this at the
  38. /// start and hold on to all the information.
  39. pub struct File<'dir> {
  40. /// The filename portion of this file’s path, including the extension.
  41. ///
  42. /// This is used to compare against certain filenames (such as checking if
  43. /// it’s “Makefile” or something) and to highlight only the filename in
  44. /// colour when displaying the path.
  45. pub name: String,
  46. /// The file’s name’s extension, if present, extracted from the name.
  47. ///
  48. /// This is queried many times over, so it’s worth caching it.
  49. pub ext: Option<String>,
  50. /// The path that begat this file.
  51. ///
  52. /// Even though the file’s name is extracted, the path needs to be kept
  53. /// around, as certain operations involve looking up the file’s absolute
  54. /// location (such as searching for compiled files) or using its original
  55. /// path (following a symlink).
  56. pub path: PathBuf,
  57. /// A cached `metadata` (`stat`) call for this file.
  58. ///
  59. /// This too is queried multiple times, and is *not* cached by the OS, as
  60. /// it could easily change between invocations — but exa is so short-lived
  61. /// it’s better to just cache it.
  62. pub metadata: std::fs::Metadata,
  63. /// A reference to the directory that contains this file, if any.
  64. ///
  65. /// Filenames that get passed in on the command-line directly will have no
  66. /// parent directory reference — although they technically have one on the
  67. /// filesystem, we’ll never need to look at it, so it’ll be `None`.
  68. /// However, *directories* that get passed in will produce files that
  69. /// contain a reference to it, which is used in certain operations (such
  70. /// as looking up compiled files).
  71. pub parent_dir: Option<&'dir Dir>,
  72. /// Whether this is one of the two `--all all` directories, `.` and `..`.
  73. ///
  74. /// Unlike all other entries, these are not returned as part of the
  75. /// directory’s children, and are in fact added specifically by exa; this
  76. /// means that they should be skipped when recursing.
  77. pub is_all_all: bool,
  78. /// Whether to dereference symbolic links when querying for information.
  79. ///
  80. /// For instance, when querying the size of a symbolic link, if
  81. /// dereferencing is enabled, the size of the target will be displayed
  82. /// instead.
  83. pub deref_links: bool,
  84. /// The recursive directory size when total_size is used.
  85. recursive_size: RecursiveSize,
  86. /// The extended attributes of this file.
  87. extended_attributes: OnceLock<Vec<Attribute>>,
  88. /// The absolute value of this path, used to look up mount points.
  89. absolute_path: OnceLock<Option<PathBuf>>,
  90. }
  91. impl<'dir> File<'dir> {
  92. pub fn from_args<PD, FN>(
  93. path: PathBuf,
  94. parent_dir: PD,
  95. filename: FN,
  96. deref_links: bool,
  97. total_size: bool,
  98. ) -> io::Result<File<'dir>>
  99. where
  100. PD: Into<Option<&'dir Dir>>,
  101. FN: Into<Option<String>>,
  102. {
  103. let parent_dir = parent_dir.into();
  104. let name = filename.into().unwrap_or_else(|| File::filename(&path));
  105. let ext = File::ext(&path);
  106. debug!("Statting file {:?}", &path);
  107. let metadata = std::fs::symlink_metadata(&path)?;
  108. let is_all_all = false;
  109. let extended_attributes = OnceLock::new();
  110. let absolute_path = OnceLock::new();
  111. let recursive_size = if total_size {
  112. RecursiveSize::Unknown
  113. } else {
  114. RecursiveSize::None
  115. };
  116. let mut file = File {
  117. name,
  118. ext,
  119. path,
  120. metadata,
  121. parent_dir,
  122. is_all_all,
  123. deref_links,
  124. recursive_size,
  125. extended_attributes,
  126. absolute_path,
  127. };
  128. if total_size {
  129. file.recursive_size = file.recursive_directory_size();
  130. }
  131. Ok(file)
  132. }
  133. fn new_aa(
  134. path: PathBuf,
  135. parent_dir: &'dir Dir,
  136. name: &'static str,
  137. total_size: bool,
  138. ) -> io::Result<File<'dir>> {
  139. let ext = File::ext(&path);
  140. debug!("Statting file {:?}", &path);
  141. let metadata = std::fs::symlink_metadata(&path)?;
  142. let is_all_all = true;
  143. let parent_dir = Some(parent_dir);
  144. let extended_attributes = OnceLock::new();
  145. let absolute_path = OnceLock::new();
  146. let recursive_size = if total_size {
  147. RecursiveSize::Unknown
  148. } else {
  149. RecursiveSize::None
  150. };
  151. let mut file = File {
  152. name: name.into(),
  153. ext,
  154. path,
  155. metadata,
  156. parent_dir,
  157. is_all_all,
  158. deref_links: false,
  159. extended_attributes,
  160. absolute_path,
  161. recursive_size,
  162. };
  163. if total_size {
  164. file.recursive_size = file.recursive_directory_size();
  165. }
  166. Ok(file)
  167. }
  168. pub fn new_aa_current(parent_dir: &'dir Dir, total_size: bool) -> io::Result<File<'dir>> {
  169. File::new_aa(parent_dir.path.clone(), parent_dir, ".", total_size)
  170. }
  171. pub fn new_aa_parent(
  172. path: PathBuf,
  173. parent_dir: &'dir Dir,
  174. total_size: bool,
  175. ) -> io::Result<File<'dir>> {
  176. File::new_aa(path, parent_dir, "..", total_size)
  177. }
  178. /// A file’s name is derived from its string. This needs to handle directories
  179. /// such as `/` or `..`, which have no `file_name` component. So instead, just
  180. /// use the last component as the name.
  181. pub fn filename(path: &Path) -> String {
  182. if let Some(back) = path.components().next_back() {
  183. back.as_os_str().to_string_lossy().to_string()
  184. } else {
  185. // use the path as fallback
  186. error!("Path {:?} has no last component", path);
  187. path.display().to_string()
  188. }
  189. }
  190. /// Extract an extension from a file path, if one is present, in lowercase.
  191. ///
  192. /// The extension is the series of characters after the last dot. This
  193. /// deliberately counts dotfiles, so the “.git” folder has the extension “git”.
  194. ///
  195. /// ASCII lowercasing is used because these extensions are only compared
  196. /// against a pre-compiled list of extensions which are known to only exist
  197. /// within ASCII, so it’s alright.
  198. fn ext(path: &Path) -> Option<String> {
  199. let name = path.file_name().map(|f| f.to_string_lossy().to_string())?;
  200. name.rfind('.').map(|p| name[p + 1..].to_ascii_lowercase())
  201. }
  202. /// Read the extended attributes of a file path.
  203. fn gather_extended_attributes(path: &Path) -> Vec<Attribute> {
  204. if xattr::ENABLED {
  205. match path.symlink_attributes() {
  206. Ok(xattrs) => xattrs,
  207. Err(e) => {
  208. error!(
  209. "Error looking up extended attributes for {}: {}",
  210. path.display(),
  211. e
  212. );
  213. Vec::new()
  214. }
  215. }
  216. } else {
  217. Vec::new()
  218. }
  219. }
  220. /// Get the extended attributes of a file path on demand.
  221. pub fn extended_attributes(&self) -> &Vec<Attribute> {
  222. self.extended_attributes
  223. .get_or_init(|| File::gather_extended_attributes(&self.path))
  224. }
  225. /// Whether this file is a directory on the filesystem.
  226. pub fn is_directory(&self) -> bool {
  227. self.metadata.is_dir()
  228. }
  229. /// Whether this file is a directory, or a symlink pointing to a directory.
  230. pub fn points_to_directory(&self) -> bool {
  231. if self.is_directory() {
  232. return true;
  233. }
  234. if self.is_link() {
  235. let target = self.link_target();
  236. if let FileTarget::Ok(target) = target {
  237. return target.points_to_directory();
  238. }
  239. }
  240. false
  241. }
  242. /// If this file is a directory on the filesystem, then clone its
  243. /// `PathBuf` for use in one of our own `Dir` values, and read a list of
  244. /// its contents.
  245. ///
  246. /// Returns an IO error upon failure, but this shouldn’t be used to check
  247. /// if a `File` is a directory or not! For that, just use `is_directory()`.
  248. pub fn to_dir(&self) -> io::Result<Dir> {
  249. trace!("to_dir: reading dir");
  250. Dir::read_dir(self.path.clone())
  251. }
  252. /// Whether this file is a regular file on the filesystem — that is, not a
  253. /// directory, a link, or anything else treated specially.
  254. pub fn is_file(&self) -> bool {
  255. self.metadata.is_file()
  256. }
  257. /// Whether this file is both a regular file *and* executable for the
  258. /// current user. An executable file has a different purpose from an
  259. /// executable directory, so they should be highlighted differently.
  260. #[cfg(unix)]
  261. pub fn is_executable_file(&self) -> bool {
  262. let bit = modes::USER_EXECUTE;
  263. self.is_file() && (self.metadata.permissions().mode() & bit) == bit
  264. }
  265. /// Whether this file is a symlink on the filesystem.
  266. pub fn is_link(&self) -> bool {
  267. self.metadata.file_type().is_symlink()
  268. }
  269. /// Whether this file is a named pipe on the filesystem.
  270. #[cfg(unix)]
  271. pub fn is_pipe(&self) -> bool {
  272. self.metadata.file_type().is_fifo()
  273. }
  274. /// Whether this file is a char device on the filesystem.
  275. #[cfg(unix)]
  276. pub fn is_char_device(&self) -> bool {
  277. self.metadata.file_type().is_char_device()
  278. }
  279. /// Whether this file is a block device on the filesystem.
  280. #[cfg(unix)]
  281. pub fn is_block_device(&self) -> bool {
  282. self.metadata.file_type().is_block_device()
  283. }
  284. /// Whether this file is a socket on the filesystem.
  285. #[cfg(unix)]
  286. pub fn is_socket(&self) -> bool {
  287. self.metadata.file_type().is_socket()
  288. }
  289. /// Determine the full path resolving all symbolic links on demand.
  290. pub fn absolute_path(&self) -> Option<&PathBuf> {
  291. self.absolute_path
  292. .get_or_init(|| std::fs::canonicalize(&self.path).ok())
  293. .as_ref()
  294. }
  295. /// Whether this file is a mount point
  296. pub fn is_mount_point(&self) -> bool {
  297. cfg!(any(target_os = "linux", target_os = "macos"))
  298. && self.is_directory()
  299. && self
  300. .absolute_path()
  301. .is_some_and(|p| all_mounts().contains_key(p))
  302. }
  303. /// The filesystem device and type for a mount point
  304. pub fn mount_point_info(&self) -> Option<&MountedFs> {
  305. if cfg!(any(target_os = "linux", target_os = "macos")) {
  306. return self.absolute_path().and_then(|p| all_mounts().get(p));
  307. }
  308. None
  309. }
  310. /// Re-prefixes the path pointed to by this file, if it’s a symlink, to
  311. /// make it an absolute path that can be accessed from whichever
  312. /// directory exa is being run from.
  313. fn reorient_target_path(&self, path: &Path) -> PathBuf {
  314. if path.is_absolute() {
  315. path.to_path_buf()
  316. } else if let Some(dir) = self.parent_dir {
  317. dir.join(path)
  318. } else if let Some(parent) = self.path.parent() {
  319. parent.join(path)
  320. } else {
  321. self.path.join(path)
  322. }
  323. }
  324. /// Again assuming this file is a symlink, follows that link and returns
  325. /// the result of following it.
  326. ///
  327. /// For a working symlink that the user is allowed to follow,
  328. /// this will be the `File` object at the other end, which can then have
  329. /// its name, colour, and other details read.
  330. ///
  331. /// For a broken symlink, returns where the file *would* be, if it
  332. /// existed. If this file cannot be read at all, returns the error that
  333. /// we got when we tried to read it.
  334. pub fn link_target(&self) -> FileTarget<'dir> {
  335. // We need to be careful to treat the path actually pointed to by
  336. // this file — which could be absolute or relative — to the path
  337. // we actually look up and turn into a `File` — which needs to be
  338. // absolute to be accessible from any directory.
  339. debug!("Reading link {:?}", &self.path);
  340. let path = match std::fs::read_link(&self.path) {
  341. Ok(p) => p,
  342. Err(e) => return FileTarget::Err(e),
  343. };
  344. let absolute_path = self.reorient_target_path(&path);
  345. // Use plain `metadata` instead of `symlink_metadata` - we *want* to
  346. // follow links.
  347. match std::fs::metadata(&absolute_path) {
  348. Ok(metadata) => {
  349. let ext = File::ext(&path);
  350. let name = File::filename(&path);
  351. let extended_attributes = OnceLock::new();
  352. let absolute_path_cell = OnceLock::from(Some(absolute_path));
  353. let file = File {
  354. parent_dir: None,
  355. path,
  356. ext,
  357. metadata,
  358. name,
  359. is_all_all: false,
  360. deref_links: self.deref_links,
  361. extended_attributes,
  362. absolute_path: absolute_path_cell,
  363. recursive_size: RecursiveSize::None,
  364. };
  365. FileTarget::Ok(Box::new(file))
  366. }
  367. Err(e) => {
  368. error!("Error following link {:?}: {:#?}", &path, e);
  369. FileTarget::Broken(path)
  370. }
  371. }
  372. }
  373. /// Assuming this file is a symlink, follows that link and any further
  374. /// links recursively, returning the result from following the trail.
  375. ///
  376. /// For a working symlink that the user is allowed to follow,
  377. /// this will be the `File` object at the other end, which can then have
  378. /// its name, colour, and other details read.
  379. ///
  380. /// For a broken symlink, returns where the file *would* be, if it
  381. /// existed. If this file cannot be read at all, returns the error that
  382. /// we got when we tried to read it.
  383. pub fn link_target_recurse(&self) -> FileTarget<'dir> {
  384. let target = self.link_target();
  385. if let FileTarget::Ok(f) = target {
  386. if f.is_link() {
  387. return f.link_target_recurse();
  388. }
  389. return FileTarget::Ok(f);
  390. }
  391. target
  392. }
  393. /// This file’s number of hard links.
  394. ///
  395. /// It also reports whether this is both a regular file, and a file with
  396. /// multiple links. This is important, because a file with multiple links
  397. /// is uncommon, while you come across directories and other types
  398. /// with multiple links much more often. Thus, it should get highlighted
  399. /// more attentively.
  400. #[cfg(unix)]
  401. pub fn links(&self) -> f::Links {
  402. let count = self.metadata.nlink();
  403. f::Links {
  404. count,
  405. multiple: self.is_file() && count > 1,
  406. }
  407. }
  408. /// This file’s inode.
  409. #[cfg(unix)]
  410. pub fn inode(&self) -> f::Inode {
  411. f::Inode(self.metadata.ino())
  412. }
  413. /// This actual size the file takes up on disk, in bytes.
  414. #[cfg(unix)]
  415. pub fn blocksize(&self) -> f::Blocksize {
  416. if self.deref_links && self.is_link() {
  417. match self.link_target() {
  418. FileTarget::Ok(f) => f.blocksize(),
  419. _ => f::Blocksize::None,
  420. }
  421. } else if self.is_directory() {
  422. self.recursive_size.map_or(f::Blocksize::None, |_, blocks| {
  423. f::Blocksize::Some(blocks * 512)
  424. })
  425. } else if self.is_file() {
  426. // Note that metadata.blocks returns the number of blocks
  427. // for 512 byte blocks according to the POSIX standard
  428. // even though the physical block size may be different.
  429. f::Blocksize::Some(self.metadata.blocks() * 512)
  430. } else {
  431. // directory or symlinks
  432. f::Blocksize::None
  433. }
  434. }
  435. /// The ID of the user that own this file. If dereferencing links, the links
  436. /// may be broken, in which case `None` will be returned.
  437. #[cfg(unix)]
  438. pub fn user(&self) -> Option<f::User> {
  439. if self.is_link() && self.deref_links {
  440. return match self.link_target_recurse() {
  441. FileTarget::Ok(f) => f.user(),
  442. _ => None,
  443. };
  444. }
  445. Some(f::User(self.metadata.uid()))
  446. }
  447. /// The ID of the group that owns this file.
  448. #[cfg(unix)]
  449. pub fn group(&self) -> Option<f::Group> {
  450. if self.is_link() && self.deref_links {
  451. return match self.link_target_recurse() {
  452. FileTarget::Ok(f) => f.group(),
  453. _ => None,
  454. };
  455. }
  456. Some(f::Group(self.metadata.gid()))
  457. }
  458. /// This file’s size, if it’s a regular file.
  459. ///
  460. /// For directories, the recursive size or no size is given depending on
  461. /// flags. Although they do have a size on some filesystems, I’ve never
  462. /// looked at one of those numbers and gained any information from it.
  463. ///
  464. /// Block and character devices return their device IDs, because they
  465. /// usually just have a file size of zero.
  466. ///
  467. /// Links will return the size of their target (recursively through other
  468. /// links) if dereferencing is enabled, otherwise None.
  469. #[cfg(unix)]
  470. pub fn size(&self) -> f::Size {
  471. if self.deref_links && self.is_link() {
  472. match self.link_target() {
  473. FileTarget::Ok(f) => f.size(),
  474. _ => f::Size::None,
  475. }
  476. } else if self.is_directory() {
  477. self.recursive_size
  478. .map_or(f::Size::None, |bytes, _| f::Size::Some(bytes))
  479. } else if self.is_char_device() || self.is_block_device() {
  480. let device_id = self.metadata.rdev();
  481. // MacOS and Linux have different arguments and return types for the
  482. // functions major and minor. On Linux the try_into().unwrap() and
  483. // the "as u32" cast are not needed. We turn off the warning to
  484. // allow it to compile cleanly on Linux.
  485. #[allow(trivial_numeric_casts)]
  486. #[allow(clippy::unnecessary_cast, clippy::useless_conversion)]
  487. f::Size::DeviceIDs(f::DeviceIDs {
  488. // SAFETY: Calling libc function to decompose the device_id
  489. major: unsafe { libc::major(device_id.try_into().unwrap()) } as u32,
  490. minor: unsafe { libc::minor(device_id.try_into().unwrap()) } as u32,
  491. })
  492. } else if self.is_file() {
  493. f::Size::Some(self.metadata.len())
  494. } else {
  495. // symlink
  496. f::Size::None
  497. }
  498. }
  499. /// Returns the size of the file or indicates no size if it's a directory.
  500. ///
  501. /// For Windows platforms, the size of directories is not computed and will
  502. /// return `Size::None`.
  503. #[cfg(windows)]
  504. pub fn size(&self) -> f::Size {
  505. if self.is_directory() {
  506. f::Size::None
  507. } else {
  508. f::Size::Some(self.metadata.len())
  509. }
  510. }
  511. /// Calculate the total directory size recursively. If not a directory `None`
  512. /// will be returned. The directory size is cached for recursive directory
  513. /// listing.
  514. #[cfg(unix)]
  515. fn recursive_directory_size(&self) -> RecursiveSize {
  516. if self.is_directory() {
  517. let key = (self.metadata.dev(), self.metadata.ino());
  518. if let Some(size) = DIRECTORY_SIZE_CACHE.lock().unwrap().get(&key) {
  519. return RecursiveSize::Some(size.0, size.1);
  520. }
  521. Dir::read_dir(self.path.clone()).map_or(RecursiveSize::Unknown, |dir| {
  522. let mut size = 0;
  523. let mut blocks = 0;
  524. for file in dir
  525. .files(super::DotFilter::Dotfiles, None, false, false, true)
  526. .flatten()
  527. {
  528. match file.recursive_directory_size() {
  529. RecursiveSize::Some(bytes, blks) => {
  530. size += bytes;
  531. blocks += blks;
  532. }
  533. RecursiveSize::Unknown => {}
  534. RecursiveSize::None => {
  535. size += file.metadata.size();
  536. blocks += file.metadata.blocks();
  537. }
  538. }
  539. }
  540. DIRECTORY_SIZE_CACHE
  541. .lock()
  542. .unwrap()
  543. .insert(key, (size, blocks));
  544. RecursiveSize::Some(size, blocks)
  545. })
  546. } else {
  547. RecursiveSize::None
  548. }
  549. }
  550. /// Windows version always returns None. The metadata for
  551. /// `volume_serial_number` and `file_index` are marked unstable so we can
  552. /// not cache the sizes. Without caching we could end up walking the
  553. /// directory structure several times.
  554. #[cfg(windows)]
  555. fn recursive_directory_size(&self) -> RecursiveSize {
  556. RecursiveSize::None
  557. }
  558. /// Returns the same value as `self.metadata.len()` or the recursive size
  559. /// of a directory when `total_size` is used.
  560. #[inline]
  561. pub fn length(&self) -> u64 {
  562. self.recursive_size.unwrap_bytes_or(self.metadata.len())
  563. }
  564. /// Is the file is using recursive size calculation
  565. #[inline]
  566. pub fn is_recursive_size(&self) -> bool {
  567. !self.recursive_size.is_none()
  568. }
  569. /// Determines if the directory is empty or not.
  570. ///
  571. /// For Unix platforms, this function first checks the link count to quickly
  572. /// determine non-empty directories. On most UNIX filesystems the link count
  573. /// is two plus the number of subdirectories. If the link count is less than
  574. /// or equal to 2, it then checks the directory contents to determine if
  575. /// it's truly empty. The naive approach used here checks the contents
  576. /// directly, as certain filesystems make it difficult to infer emptiness
  577. /// based on directory size alone.
  578. #[cfg(unix)]
  579. pub fn is_empty_dir(&self) -> bool {
  580. if self.is_directory() {
  581. if self.metadata.nlink() > 2 {
  582. // Directories will have a link count of two if they do not have any subdirectories.
  583. // The '.' entry is a link to itself and the '..' is a link to the parent directory.
  584. // A subdirectory will have a link to its parent directory increasing the link count
  585. // above two. This will avoid the expensive read_dir call below when a directory
  586. // has subdirectories.
  587. false
  588. } else {
  589. self.is_empty_directory()
  590. }
  591. } else {
  592. false
  593. }
  594. }
  595. /// Determines if the directory is empty or not.
  596. ///
  597. /// For Windows platforms, this function checks the directory contents directly
  598. /// to determine if it's empty. Since certain filesystems on Windows make it
  599. /// challenging to infer emptiness based on directory size, this approach is used.
  600. #[cfg(windows)]
  601. pub fn is_empty_dir(&self) -> bool {
  602. if self.is_directory() {
  603. self.is_empty_directory()
  604. } else {
  605. false
  606. }
  607. }
  608. /// Checks the contents of the directory to determine if it's empty.
  609. ///
  610. /// This function avoids counting '.' and '..' when determining if the directory is
  611. /// empty. If any other entries are found, it returns `false`.
  612. ///
  613. /// The naive approach, as one would think that this info may have been cached.
  614. /// but as mentioned in the size function comment above, different filesystems
  615. /// make it difficult to get any info about a dir by it's size, so this may be it.
  616. fn is_empty_directory(&self) -> bool {
  617. trace!("is_empty_directory: reading dir");
  618. match Dir::read_dir(self.path.clone()) {
  619. // . & .. are skipped, if the returned iterator has .next(), it's not empty
  620. Ok(has_files) => has_files
  621. .files(super::DotFilter::Dotfiles, None, false, false, false)
  622. .next()
  623. .is_none(),
  624. Err(_) => false,
  625. }
  626. }
  627. /// This file’s last modified timestamp, if available on this platform.
  628. pub fn modified_time(&self) -> Option<NaiveDateTime> {
  629. if self.is_link() && self.deref_links {
  630. return match self.link_target_recurse() {
  631. FileTarget::Ok(f) => f.modified_time(),
  632. _ => None,
  633. };
  634. }
  635. self.metadata
  636. .modified()
  637. .map(|st| DateTime::<Utc>::from(st).naive_utc())
  638. .ok()
  639. }
  640. /// This file’s last changed timestamp, if available on this platform.
  641. #[cfg(unix)]
  642. pub fn changed_time(&self) -> Option<NaiveDateTime> {
  643. if self.is_link() && self.deref_links {
  644. return match self.link_target_recurse() {
  645. FileTarget::Ok(f) => f.changed_time(),
  646. _ => None,
  647. };
  648. }
  649. NaiveDateTime::from_timestamp_opt(self.metadata.ctime(), self.metadata.ctime_nsec() as u32)
  650. }
  651. #[cfg(windows)]
  652. pub fn changed_time(&self) -> Option<NaiveDateTime> {
  653. self.modified_time()
  654. }
  655. /// This file’s last accessed timestamp, if available on this platform.
  656. pub fn accessed_time(&self) -> Option<NaiveDateTime> {
  657. if self.is_link() && self.deref_links {
  658. return match self.link_target_recurse() {
  659. FileTarget::Ok(f) => f.accessed_time(),
  660. _ => None,
  661. };
  662. }
  663. self.metadata
  664. .accessed()
  665. .map(|st| DateTime::<Utc>::from(st).naive_utc())
  666. .ok()
  667. }
  668. /// This file’s created timestamp, if available on this platform.
  669. pub fn created_time(&self) -> Option<NaiveDateTime> {
  670. if self.is_link() && self.deref_links {
  671. return match self.link_target_recurse() {
  672. FileTarget::Ok(f) => f.created_time(),
  673. _ => None,
  674. };
  675. }
  676. self.metadata
  677. .created()
  678. .map(|st| DateTime::<Utc>::from(st).naive_utc())
  679. .ok()
  680. }
  681. /// This file’s ‘type’.
  682. ///
  683. /// This is used a the leftmost character of the permissions column.
  684. /// The file type can usually be guessed from the colour of the file, but
  685. /// ls puts this character there.
  686. #[cfg(unix)]
  687. pub fn type_char(&self) -> f::Type {
  688. if self.is_file() {
  689. f::Type::File
  690. } else if self.is_directory() {
  691. f::Type::Directory
  692. } else if self.is_pipe() {
  693. f::Type::Pipe
  694. } else if self.is_link() {
  695. f::Type::Link
  696. } else if self.is_char_device() {
  697. f::Type::CharDevice
  698. } else if self.is_block_device() {
  699. f::Type::BlockDevice
  700. } else if self.is_socket() {
  701. f::Type::Socket
  702. } else {
  703. f::Type::Special
  704. }
  705. }
  706. #[cfg(windows)]
  707. pub fn type_char(&self) -> f::Type {
  708. if self.is_file() {
  709. f::Type::File
  710. } else if self.is_directory() {
  711. f::Type::Directory
  712. } else {
  713. f::Type::Special
  714. }
  715. }
  716. /// This file’s permissions, with flags for each bit.
  717. #[cfg(unix)]
  718. pub fn permissions(&self) -> Option<f::Permissions> {
  719. if self.is_link() && self.deref_links {
  720. // If the chain of links is broken, we instead fall through and
  721. // return the permissions of the original link, as would have been
  722. // done if we were not dereferencing.
  723. return match self.link_target_recurse() {
  724. FileTarget::Ok(f) => f.permissions(),
  725. _ => None,
  726. };
  727. }
  728. let bits = self.metadata.mode();
  729. let has_bit = |bit| bits & bit == bit;
  730. Some(f::Permissions {
  731. user_read: has_bit(modes::USER_READ),
  732. user_write: has_bit(modes::USER_WRITE),
  733. user_execute: has_bit(modes::USER_EXECUTE),
  734. group_read: has_bit(modes::GROUP_READ),
  735. group_write: has_bit(modes::GROUP_WRITE),
  736. group_execute: has_bit(modes::GROUP_EXECUTE),
  737. other_read: has_bit(modes::OTHER_READ),
  738. other_write: has_bit(modes::OTHER_WRITE),
  739. other_execute: has_bit(modes::OTHER_EXECUTE),
  740. sticky: has_bit(modes::STICKY),
  741. setgid: has_bit(modes::SETGID),
  742. setuid: has_bit(modes::SETUID),
  743. })
  744. }
  745. #[cfg(windows)]
  746. pub fn attributes(&self) -> f::Attributes {
  747. let bits = self.metadata.file_attributes();
  748. let has_bit = |bit| bits & bit == bit;
  749. // https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
  750. f::Attributes {
  751. directory: has_bit(0x10),
  752. archive: has_bit(0x20),
  753. readonly: has_bit(0x1),
  754. hidden: has_bit(0x2),
  755. system: has_bit(0x4),
  756. reparse_point: has_bit(0x400),
  757. }
  758. }
  759. /// This file’s security context field.
  760. pub fn security_context(&self) -> f::SecurityContext<'_> {
  761. let context = match self
  762. .extended_attributes()
  763. .iter()
  764. .find(|a| a.name == "security.selinux")
  765. {
  766. Some(attr) => f::SecurityContextType::SELinux(&attr.value),
  767. None => f::SecurityContextType::None,
  768. };
  769. f::SecurityContext { context }
  770. }
  771. }
  772. impl<'a> AsRef<File<'a>> for File<'a> {
  773. fn as_ref(&self) -> &File<'a> {
  774. self
  775. }
  776. }
  777. /// The result of following a symlink.
  778. pub enum FileTarget<'dir> {
  779. /// The symlink pointed at a file that exists.
  780. Ok(Box<File<'dir>>),
  781. /// The symlink pointed at a file that does not exist. Holds the path
  782. /// where the file would be, if it existed.
  783. Broken(PathBuf),
  784. /// There was an IO error when following the link. This can happen if the
  785. /// file isn’t a link to begin with, but also if, say, we don’t have
  786. /// permission to follow it.
  787. Err(io::Error),
  788. // Err is its own variant, instead of having the whole thing be inside an
  789. // `io::Result`, because being unable to follow a symlink is not a serious
  790. // error — we just display the error message and move on.
  791. }
  792. impl<'dir> FileTarget<'dir> {
  793. /// Whether this link doesn’t lead to a file, for whatever reason. This
  794. /// gets used to determine how to highlight the link in grid views.
  795. pub fn is_broken(&self) -> bool {
  796. matches!(self, Self::Broken(_) | Self::Err(_))
  797. }
  798. }
  799. /// More readable aliases for the permission bits exposed by libc.
  800. #[allow(trivial_numeric_casts)]
  801. #[cfg(unix)]
  802. mod modes {
  803. // The `libc::mode_t` type’s actual type varies, but the value returned
  804. // from `metadata.permissions().mode()` is always `u32`.
  805. pub type Mode = u32;
  806. pub const USER_READ: Mode = libc::S_IRUSR as Mode;
  807. pub const USER_WRITE: Mode = libc::S_IWUSR as Mode;
  808. pub const USER_EXECUTE: Mode = libc::S_IXUSR as Mode;
  809. pub const GROUP_READ: Mode = libc::S_IRGRP as Mode;
  810. pub const GROUP_WRITE: Mode = libc::S_IWGRP as Mode;
  811. pub const GROUP_EXECUTE: Mode = libc::S_IXGRP as Mode;
  812. pub const OTHER_READ: Mode = libc::S_IROTH as Mode;
  813. pub const OTHER_WRITE: Mode = libc::S_IWOTH as Mode;
  814. pub const OTHER_EXECUTE: Mode = libc::S_IXOTH as Mode;
  815. pub const STICKY: Mode = libc::S_ISVTX as Mode;
  816. pub const SETGID: Mode = libc::S_ISGID as Mode;
  817. pub const SETUID: Mode = libc::S_ISUID as Mode;
  818. }
  819. #[cfg(test)]
  820. mod ext_test {
  821. use super::File;
  822. use std::path::Path;
  823. #[test]
  824. fn extension() {
  825. assert_eq!(Some("dat".to_string()), File::ext(Path::new("fester.dat")));
  826. }
  827. #[test]
  828. fn dotfile() {
  829. assert_eq!(Some("vimrc".to_string()), File::ext(Path::new(".vimrc")));
  830. }
  831. #[test]
  832. fn no_extension() {
  833. assert_eq!(None, File::ext(Path::new("jarlsberg")));
  834. }
  835. }
  836. #[cfg(test)]
  837. mod filename_test {
  838. use super::File;
  839. use std::path::Path;
  840. #[test]
  841. fn file() {
  842. assert_eq!("fester.dat", File::filename(Path::new("fester.dat")));
  843. }
  844. #[test]
  845. fn no_path() {
  846. assert_eq!("foo.wha", File::filename(Path::new("/var/cache/foo.wha")));
  847. }
  848. #[test]
  849. fn here() {
  850. assert_eq!(".", File::filename(Path::new(".")));
  851. }
  852. #[test]
  853. fn there() {
  854. assert_eq!("..", File::filename(Path::new("..")));
  855. }
  856. #[test]
  857. fn everywhere() {
  858. assert_eq!("..", File::filename(Path::new("./..")));
  859. }
  860. #[test]
  861. #[cfg(unix)]
  862. fn topmost() {
  863. assert_eq!("/", File::filename(Path::new("/")));
  864. }
  865. }