file.rs 19 KB

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