file.rs 38 KB

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