file.rs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. //! Files, and methods and fields to access their metadata.
  2. use std::ascii::AsciiExt;
  3. use std::env::current_dir;
  4. use std::fs;
  5. use std::io;
  6. use std::os::unix::fs::{MetadataExt, PermissionsExt};
  7. use std::path::{Component, Path, PathBuf};
  8. use unicode_width::UnicodeWidthStr;
  9. use dir::Dir;
  10. use self::fields as f;
  11. /// Constant table copied from https://doc.rust-lang.org/src/std/sys/unix/ext/fs.rs.html#11-259
  12. /// which is currently unstable and lacks vision for stabilization,
  13. /// see https://github.com/rust-lang/rust/issues/27712
  14. #[allow(dead_code)]
  15. mod modes {
  16. use std::os::unix::raw;
  17. pub const USER_READ: raw::mode_t = 0o400;
  18. pub const USER_WRITE: raw::mode_t = 0o200;
  19. pub const USER_EXECUTE: raw::mode_t = 0o100;
  20. pub const USER_RWX: raw::mode_t = 0o700;
  21. pub const GROUP_READ: raw::mode_t = 0o040;
  22. pub const GROUP_WRITE: raw::mode_t = 0o020;
  23. pub const GROUP_EXECUTE: raw::mode_t = 0o010;
  24. pub const GROUP_RWX: raw::mode_t = 0o070;
  25. pub const OTHER_READ: raw::mode_t = 0o004;
  26. pub const OTHER_WRITE: raw::mode_t = 0o002;
  27. pub const OTHER_EXECUTE: raw::mode_t = 0o001;
  28. pub const OTHER_RWX: raw::mode_t = 0o007;
  29. pub const ALL_READ: raw::mode_t = 0o444;
  30. pub const ALL_WRITE: raw::mode_t = 0o222;
  31. pub const ALL_EXECUTE: raw::mode_t = 0o111;
  32. pub const ALL_RWX: raw::mode_t = 0o777;
  33. pub const SETUID: raw::mode_t = 0o4000;
  34. pub const SETGID: raw::mode_t = 0o2000;
  35. pub const STICKY_BIT: raw::mode_t = 0o1000;
  36. }
  37. /// A **File** is a wrapper around one of Rust's Path objects, along with
  38. /// associated data about the file.
  39. ///
  40. /// Each file is definitely going to have its filename displayed at least
  41. /// once, have its file extension extracted at least once, and have its metadata
  42. /// information queried at least once, so it makes sense to do all this at the
  43. /// start and hold on to all the information.
  44. pub struct File<'dir> {
  45. /// This file's name, as a UTF-8 encoded String.
  46. pub name: String,
  47. /// The file's name's extension, if present, extracted from the name. This
  48. /// is queried a lot, so it's worth being cached.
  49. pub ext: Option<String>,
  50. /// The path that begat this file. Even though the file's name is
  51. /// extracted, the path needs to be kept around, as certain operations
  52. /// involve looking up the file's absolute location (such as the Git
  53. /// status, or searching for compiled files).
  54. pub path: PathBuf,
  55. /// A cached `metadata` call for this file. This is queried multiple
  56. /// times, and is *not* cached by the OS, as it could easily change
  57. /// between invocations - but exa is so short-lived it's better to just
  58. /// cache it.
  59. pub metadata: fs::Metadata,
  60. /// A reference to the directory that contains this file, if present.
  61. ///
  62. /// Filenames that get passed in on the command-line directly will have no
  63. /// parent directory reference - although they technically have one on the
  64. /// filesystem, we'll never need to look at it, so it'll be `None`.
  65. /// However, *directories* that get passed in will produce files that
  66. /// contain a reference to it, which is used in certain operations (such
  67. /// as looking up a file's Git status).
  68. pub dir: Option<&'dir Dir>,
  69. }
  70. impl<'dir> File<'dir> {
  71. /// Create a new `File` object from the given `Path`, inside the given
  72. /// `Dir`, if appropriate.
  73. ///
  74. /// This uses `symlink_metadata` instead of `metadata`, which doesn't
  75. /// follow symbolic links.
  76. pub fn from_path(path: &Path, parent: Option<&'dir Dir>) -> io::Result<File<'dir>> {
  77. fs::symlink_metadata(path).map(|metadata| File::with_metadata(metadata, path, parent))
  78. }
  79. /// Create a new File object from the given metadata result, and other data.
  80. pub fn with_metadata(metadata: fs::Metadata, path: &Path, parent: Option<&'dir Dir>) -> File<'dir> {
  81. let filename = path_filename(path);
  82. File {
  83. path: path.to_path_buf(),
  84. dir: parent,
  85. metadata: metadata,
  86. ext: ext(&filename),
  87. name: filename.to_string(),
  88. }
  89. }
  90. /// Whether this file is a directory on the filesystem.
  91. pub fn is_directory(&self) -> bool {
  92. self.metadata.is_dir()
  93. }
  94. /// If this file is a directory on the filesystem, then clone its
  95. /// `PathBuf` for use in one of our own `Dir` objects, and read a list of
  96. /// its contents.
  97. ///
  98. /// Returns an IO error upon failure, but this shouldn't be used to check
  99. /// if a `File` is a directory or not! For that, just use `is_directory()`.
  100. pub fn to_dir(&self, scan_for_git: bool) -> io::Result<Dir> {
  101. Dir::read_dir(&*self.path, scan_for_git)
  102. }
  103. /// Whether this file is a regular file on the filesystem - that is, not a
  104. /// directory, a link, or anything else treated specially.
  105. pub fn is_file(&self) -> bool {
  106. self.metadata.is_file()
  107. }
  108. /// Whether this file is both a regular file *and* executable for the
  109. /// current user. Executable files have different semantics than
  110. /// executable directories, and so should be highlighted differently.
  111. pub fn is_executable_file(&self) -> bool {
  112. let bit = modes::USER_EXECUTE;
  113. self.is_file() && (self.metadata.permissions().mode() & bit) == bit
  114. }
  115. /// Whether this file is a symlink on the filesystem.
  116. pub fn is_link(&self) -> bool {
  117. self.metadata.file_type().is_symlink()
  118. }
  119. /// Whether this file is a named pipe on the filesystem.
  120. pub fn is_pipe(&self) -> bool {
  121. false // TODO: Still waiting on this one...
  122. }
  123. /// Whether this file is a dotfile, based on its name. In Unix, file names
  124. /// beginning with a dot represent system or configuration files, and
  125. /// should be hidden by default.
  126. pub fn is_dotfile(&self) -> bool {
  127. self.name.starts_with(".")
  128. }
  129. /// Constructs the 'path prefix' of this file, which is the portion of the
  130. /// path up to, but not including, the file name.
  131. ///
  132. /// This gets used when displaying the path a symlink points to. In
  133. /// certain cases, it may return an empty-length string. Examples:
  134. ///
  135. /// - `code/exa/file.rs` has `code/exa/` as its prefix, including the
  136. /// trailing slash.
  137. /// - `code/exa` has just `code/` as its prefix.
  138. /// - `code` has the empty string as its prefix.
  139. /// - `/` also has the empty string as its prefix. It does not have a
  140. /// trailing slash, as the slash constitutes the 'name' of this file.
  141. pub fn path_prefix(&self) -> String {
  142. let components: Vec<Component> = self.path.components().collect();
  143. let mut path_prefix = String::new();
  144. // This slicing is safe as components always has the RootComponent
  145. // as the first element.
  146. for component in components[..(components.len() - 1)].iter() {
  147. path_prefix.push_str(&*component.as_os_str().to_string_lossy());
  148. if component != &Component::RootDir {
  149. path_prefix.push_str("/");
  150. }
  151. }
  152. path_prefix
  153. }
  154. /// The Unicode 'display width' of the filename.
  155. ///
  156. /// This is related to the number of graphemes in the string: most
  157. /// characters are 1 columns wide, but in some contexts, certain
  158. /// characters are actually 2 columns wide.
  159. pub fn file_name_width(&self) -> usize {
  160. UnicodeWidthStr::width(&self.name[..])
  161. }
  162. /// Assuming the current file is a symlink, follows the link and
  163. /// returns a File object from the path the link points to.
  164. ///
  165. /// If statting the file fails (usually because the file on the
  166. /// other end doesn't exist), returns the *filename* of the file
  167. /// that should be there.
  168. pub fn link_target(&self) -> Result<File, String> {
  169. let path = match fs::read_link(&self.path) {
  170. Ok(path) => path,
  171. Err(_) => return Err(self.name.clone()),
  172. };
  173. let target_path = match self.dir {
  174. Some(dir) => dir.join(&*path),
  175. None => path
  176. };
  177. let filename = path_filename(&target_path);
  178. // Use plain `metadata` instead of `symlink_metadata` - we *want* to follow links.
  179. if let Ok(metadata) = fs::metadata(&target_path) {
  180. Ok(File {
  181. path: target_path.to_path_buf(),
  182. dir: self.dir,
  183. metadata: metadata,
  184. ext: ext(&filename),
  185. name: filename.to_string(),
  186. })
  187. }
  188. else {
  189. Err(filename.to_string())
  190. }
  191. }
  192. /// This file's number of hard links.
  193. ///
  194. /// It also reports whether this is both a regular file, and a file with
  195. /// multiple links. This is important, because a file with multiple links
  196. /// is uncommon, while you can come across directories and other types
  197. /// with multiple links much more often. Thus, it should get highlighted
  198. /// more attentively.
  199. pub fn links(&self) -> f::Links {
  200. let count = self.metadata.nlink();
  201. f::Links {
  202. count: count,
  203. multiple: self.is_file() && count > 1,
  204. }
  205. }
  206. /// This file's inode.
  207. pub fn inode(&self) -> f::Inode {
  208. f::Inode(self.metadata.ino())
  209. }
  210. /// This file's number of filesystem blocks.
  211. ///
  212. /// (Not the size of each block, which we don't actually report on)
  213. pub fn blocks(&self) -> f::Blocks {
  214. if self.is_file() || self.is_link() {
  215. f::Blocks::Some(self.metadata.blocks())
  216. }
  217. else {
  218. f::Blocks::None
  219. }
  220. }
  221. /// The ID of the user that own this file.
  222. pub fn user(&self) -> f::User {
  223. f::User(self.metadata.uid())
  224. }
  225. /// The ID of the group that owns this file.
  226. pub fn group(&self) -> f::Group {
  227. f::Group(self.metadata.gid())
  228. }
  229. /// This file's size, if it's a regular file.
  230. ///
  231. /// For directories, no size is given. Although they do have a size on
  232. /// some filesystems, I've never looked at one of those numbers and gained
  233. /// any information from it. So it's going to be hidden instead.
  234. pub fn size(&self) -> f::Size {
  235. if self.is_directory() {
  236. f::Size::None
  237. }
  238. else {
  239. f::Size::Some(self.metadata.len())
  240. }
  241. }
  242. pub fn modified_time(&self) -> f::Time {
  243. f::Time(self.metadata.mtime())
  244. }
  245. pub fn created_time(&self) -> f::Time {
  246. f::Time(self.metadata.ctime())
  247. }
  248. pub fn accessed_time(&self) -> f::Time {
  249. f::Time(self.metadata.mtime())
  250. }
  251. /// This file's 'type'.
  252. ///
  253. /// This is used in the leftmost column of the permissions column.
  254. /// Although the file type can usually be guessed from the colour of the
  255. /// file, `ls` puts this character there, so people will expect it.
  256. fn type_char(&self) -> f::Type {
  257. if self.is_file() {
  258. f::Type::File
  259. }
  260. else if self.is_directory() {
  261. f::Type::Directory
  262. }
  263. else if self.is_pipe() {
  264. f::Type::Pipe
  265. }
  266. else if self.is_link() {
  267. f::Type::Link
  268. }
  269. else {
  270. f::Type::Special
  271. }
  272. }
  273. /// This file's permissions, with flags for each bit.
  274. ///
  275. /// The extended-attribute '@' character that you see in here is in fact
  276. /// added in later, to avoid querying the extended attributes more than
  277. /// once. (Yes, it's a little hacky.)
  278. pub fn permissions(&self) -> f::Permissions {
  279. let bits = self.metadata.permissions().mode();
  280. let has_bit = |bit| { bits & bit == bit };
  281. f::Permissions {
  282. file_type: self.type_char(),
  283. user_read: has_bit(modes::USER_READ),
  284. user_write: has_bit(modes::USER_WRITE),
  285. user_execute: has_bit(modes::USER_EXECUTE),
  286. group_read: has_bit(modes::GROUP_READ),
  287. group_write: has_bit(modes::GROUP_WRITE),
  288. group_execute: has_bit(modes::GROUP_EXECUTE),
  289. other_read: has_bit(modes::OTHER_READ),
  290. other_write: has_bit(modes::OTHER_WRITE),
  291. other_execute: has_bit(modes::OTHER_EXECUTE),
  292. }
  293. }
  294. /// For this file, return a vector of alternate file paths that, if any of
  295. /// them exist, mean that *this* file should be coloured as `Compiled`.
  296. ///
  297. /// The point of this is to highlight compiled files such as `foo.o` when
  298. /// their source file `foo.c` exists in the same directory. It's too
  299. /// dangerous to highlight *all* compiled, so the paths in this vector
  300. /// are checked for existence first: for example, `foo.js` is perfectly
  301. /// valid without `foo.coffee`.
  302. pub fn get_source_files(&self) -> Vec<PathBuf> {
  303. if let Some(ref ext) = self.ext {
  304. match &ext[..] {
  305. "class" => vec![self.path.with_extension("java")], // Java
  306. "css" => vec![self.path.with_extension("sass"), self.path.with_extension("less")], // SASS, Less
  307. "elc" => vec![self.path.with_extension("el")], // Emacs Lisp
  308. "hi" => vec![self.path.with_extension("hs")], // Haskell
  309. "js" => vec![self.path.with_extension("coffee"), self.path.with_extension("ts")], // CoffeeScript, TypeScript
  310. "o" => vec![self.path.with_extension("c"), self.path.with_extension("cpp")], // C, C++
  311. "pyc" => vec![self.path.with_extension("py")], // Python
  312. "aux" => vec![self.path.with_extension("tex")], // TeX: auxiliary file
  313. "bbl" => vec![self.path.with_extension("tex")], // BibTeX bibliography file
  314. "blg" => vec![self.path.with_extension("tex")], // BibTeX log file
  315. "lof" => vec![self.path.with_extension("tex")], // TeX list of figures
  316. "log" => vec![self.path.with_extension("tex")], // TeX log file
  317. "lot" => vec![self.path.with_extension("tex")], // TeX list of tables
  318. "toc" => vec![self.path.with_extension("tex")], // TeX table of contents
  319. _ => vec![], // No source files if none of the above
  320. }
  321. }
  322. else {
  323. vec![] // No source files if there's no extension, either!
  324. }
  325. }
  326. /// Whether this file's extension is any of the strings that get passed in.
  327. ///
  328. /// This will always return `false` if the file has no extension.
  329. pub fn extension_is_one_of(&self, choices: &[&str]) -> bool {
  330. match self.ext {
  331. Some(ref ext) => choices.contains(&&ext[..]),
  332. None => false,
  333. }
  334. }
  335. /// Whether this file's name, including extension, is any of the strings
  336. /// that get passed in.
  337. pub fn name_is_one_of(&self, choices: &[&str]) -> bool {
  338. choices.contains(&&self.name[..])
  339. }
  340. /// This file's Git status as two flags: one for staged changes, and the
  341. /// other for unstaged changes.
  342. ///
  343. /// This requires looking at the `git` field of this file's parent
  344. /// directory, so will not work if this file has just been passed in on
  345. /// the command line.
  346. pub fn git_status(&self) -> f::Git {
  347. match self.dir {
  348. None => f::Git { staged: f::GitStatus::NotModified, unstaged: f::GitStatus::NotModified },
  349. Some(d) => {
  350. let cwd = match current_dir() {
  351. Err(_) => Path::new(".").join(&self.path),
  352. Ok(dir) => dir.join(&self.path),
  353. };
  354. d.git_status(&cwd, self.is_directory())
  355. },
  356. }
  357. }
  358. }
  359. /// Extract the filename to display from a path, converting it from UTF-8
  360. /// lossily, into a String.
  361. ///
  362. /// The filename to display is the last component of the path. However,
  363. /// the path has no components for `.`, `..`, and `/`, so in these
  364. /// cases, the entire path is used.
  365. fn path_filename(path: &Path) -> String {
  366. match path.iter().last() {
  367. Some(os_str) => os_str.to_string_lossy().to_string(),
  368. None => ".".to_string(), // can this even be reached?
  369. }
  370. }
  371. /// Extract an extension from a string, if one is present, in lowercase.
  372. ///
  373. /// The extension is the series of characters after the last dot. This
  374. /// deliberately counts dotfiles, so the ".git" folder has the extension "git".
  375. ///
  376. /// ASCII lowercasing is used because these extensions are only compared
  377. /// against a pre-compiled list of extensions which are known to only exist
  378. /// within ASCII, so it's alright.
  379. fn ext(name: &str) -> Option<String> {
  380. name.rfind('.').map(|p| name[p+1..].to_ascii_lowercase())
  381. }
  382. /// Wrapper types for the values returned from `File` objects.
  383. ///
  384. /// The methods of `File` don't return formatted strings; neither do they
  385. /// return raw numbers representing timestamps or user IDs. Instead, they will
  386. /// return an object in this `fields` module. These objects are later rendered
  387. /// into formatted strings in the `output/details` module.
  388. pub mod fields {
  389. use std::os::unix::raw::{blkcnt_t, gid_t, ino_t, nlink_t, time_t, uid_t};
  390. pub enum Type {
  391. File, Directory, Pipe, Link, Special,
  392. }
  393. pub struct Permissions {
  394. pub file_type: Type,
  395. pub user_read: bool,
  396. pub user_write: bool,
  397. pub user_execute: bool,
  398. pub group_read: bool,
  399. pub group_write: bool,
  400. pub group_execute: bool,
  401. pub other_read: bool,
  402. pub other_write: bool,
  403. pub other_execute: bool,
  404. }
  405. pub struct Links {
  406. pub count: nlink_t,
  407. pub multiple: bool,
  408. }
  409. pub struct Inode(pub ino_t);
  410. pub enum Blocks {
  411. Some(blkcnt_t),
  412. None,
  413. }
  414. pub struct User(pub uid_t);
  415. pub struct Group(pub gid_t);
  416. pub enum Size {
  417. Some(u64),
  418. None,
  419. }
  420. pub struct Time(pub time_t);
  421. pub enum GitStatus {
  422. NotModified,
  423. New,
  424. Modified,
  425. Deleted,
  426. Renamed,
  427. TypeChange,
  428. }
  429. pub struct Git {
  430. pub staged: GitStatus,
  431. pub unstaged: GitStatus,
  432. }
  433. impl Git {
  434. pub fn empty() -> Git {
  435. Git { staged: GitStatus::NotModified, unstaged: GitStatus::NotModified }
  436. }
  437. }
  438. }
  439. #[cfg(test)]
  440. mod test {
  441. use super::ext;
  442. use super::File;
  443. use std::path::Path;
  444. #[test]
  445. fn extension() {
  446. assert_eq!(Some("dat".to_string()), ext("fester.dat"))
  447. }
  448. #[test]
  449. fn dotfile() {
  450. assert_eq!(Some("vimrc".to_string()), ext(".vimrc"))
  451. }
  452. #[test]
  453. fn no_extension() {
  454. assert_eq!(None, ext("jarlsberg"))
  455. }
  456. #[test]
  457. fn test_prefix_empty() {
  458. let f = File::from_path(Path::new("Cargo.toml"), None).unwrap();
  459. assert_eq!("", f.path_prefix());
  460. }
  461. #[test]
  462. fn test_prefix_file() {
  463. let f = File::from_path(Path::new("src/main.rs"), None).unwrap();
  464. assert_eq!("src/", f.path_prefix());
  465. }
  466. #[test]
  467. fn test_prefix_path() {
  468. let f = File::from_path(Path::new("src"), None).unwrap();
  469. assert_eq!("", f.path_prefix());
  470. }
  471. #[test]
  472. fn test_prefix_root() {
  473. let f = File::from_path(Path::new("/"), None).unwrap();
  474. assert_eq!("", f.path_prefix());
  475. }
  476. }