|
| 1 | +//! This module contains a `Metadata` implementation that must be used instead of `std::fs::Metadata` to assure |
| 2 | +//! that the `ctime` information is populated exactly like the one in `git`, which wouldn't be the case on unix. |
| 3 | +#![allow(clippy::useless_conversion)] // on some MacOOS conversions are required, but on linux usually not. |
| 4 | +#![allow(clippy::unnecessary_cast)] |
| 5 | + |
| 6 | +// it's allowed for good measure, in case there are systems that use different types for that. |
| 7 | +use std::path::Path; |
| 8 | +use std::time::{Duration, SystemTime}; |
| 9 | + |
| 10 | +/// A structure to partially mirror [`std::fs::Metadata`]. |
| 11 | +#[cfg(not(windows))] |
| 12 | +pub struct Metadata(rustix::fs::Stat); |
| 13 | + |
| 14 | +#[cfg(windows)] |
| 15 | +/// A structure to partially mirror [`std::fs::Metadata`]. |
| 16 | +pub struct Metadata(std::fs::Metadata); |
| 17 | + |
| 18 | +/// Lifecycle |
| 19 | +impl Metadata { |
| 20 | + /// Obtain the metadata at `path` without following symlinks. |
| 21 | + pub fn from_path_no_follow(path: &Path) -> Result<Self, std::io::Error> { |
| 22 | + #[cfg(not(windows))] |
| 23 | + { |
| 24 | + rustix::fs::lstat(path).map(Metadata).map_err(Into::into) |
| 25 | + } |
| 26 | + #[cfg(windows)] |
| 27 | + path.symlink_metadata().map(Metadata) |
| 28 | + } |
| 29 | + |
| 30 | + /// Obtain the metadata at `path` without following symlinks. |
| 31 | + pub fn from_file(file: &std::fs::File) -> Result<Self, std::io::Error> { |
| 32 | + #[cfg(not(windows))] |
| 33 | + { |
| 34 | + rustix::fs::fstat(file).map(Metadata).map_err(Into::into) |
| 35 | + } |
| 36 | + #[cfg(windows)] |
| 37 | + file.metadata().map(Metadata) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/// Access |
| 42 | +#[allow(clippy::len_without_is_empty)] |
| 43 | +impl Metadata { |
| 44 | + /// Return true if the metadata belongs to a directory |
| 45 | + pub fn is_dir(&self) -> bool { |
| 46 | + #[cfg(not(windows))] |
| 47 | + { |
| 48 | + (self.0.st_mode & libc::S_IFMT) == libc::S_IFDIR |
| 49 | + } |
| 50 | + #[cfg(windows)] |
| 51 | + self.0.is_dir() |
| 52 | + } |
| 53 | + |
| 54 | + /// Return the time at which the underlying file was modified. |
| 55 | + pub fn modified(&self) -> Option<SystemTime> { |
| 56 | + #[cfg(not(windows))] |
| 57 | + { |
| 58 | + Some(system_time_from_secs_nanos( |
| 59 | + self.0.st_mtime.try_into().ok()?, |
| 60 | + self.0.st_mtime_nsec.try_into().ok()?, |
| 61 | + )) |
| 62 | + } |
| 63 | + #[cfg(windows)] |
| 64 | + self.0.modified().ok() |
| 65 | + } |
| 66 | + |
| 67 | + /// Return the time at which the underlying file was created. |
| 68 | + /// |
| 69 | + /// Note that this differes from [`std::fs::Metadata::created()`] which would return |
| 70 | + /// the inode birth time, which is notably different to what `git` does. |
| 71 | + pub fn created(&self) -> Option<SystemTime> { |
| 72 | + #[cfg(not(windows))] |
| 73 | + { |
| 74 | + Some(system_time_from_secs_nanos( |
| 75 | + self.0.st_ctime.try_into().ok()?, |
| 76 | + self.0.st_ctime_nsec.try_into().ok()?, |
| 77 | + )) |
| 78 | + } |
| 79 | + #[cfg(windows)] |
| 80 | + self.0.created().ok() |
| 81 | + } |
| 82 | + |
| 83 | + /// Return the size of the file in bytes. |
| 84 | + pub fn len(&self) -> u64 { |
| 85 | + #[cfg(not(windows))] |
| 86 | + { |
| 87 | + self.0.st_size as u64 |
| 88 | + } |
| 89 | + #[cfg(windows)] |
| 90 | + self.0.len() |
| 91 | + } |
| 92 | + |
| 93 | + /// Return the device id on which the file is located, or 0 on windows. |
| 94 | + pub fn dev(&self) -> u64 { |
| 95 | + #[cfg(not(windows))] |
| 96 | + { |
| 97 | + self.0.st_dev as u64 |
| 98 | + } |
| 99 | + #[cfg(windows)] |
| 100 | + 0 |
| 101 | + } |
| 102 | + |
| 103 | + /// Return the inode id tracking the file, or 0 on windows. |
| 104 | + pub fn ino(&self) -> u64 { |
| 105 | + #[cfg(not(windows))] |
| 106 | + { |
| 107 | + self.0.st_ino as u64 |
| 108 | + } |
| 109 | + #[cfg(windows)] |
| 110 | + 0 |
| 111 | + } |
| 112 | + |
| 113 | + /// Return the user-id of the file or 0 on windows. |
| 114 | + pub fn uid(&self) -> u32 { |
| 115 | + #[cfg(not(windows))] |
| 116 | + { |
| 117 | + self.0.st_uid as u32 |
| 118 | + } |
| 119 | + #[cfg(windows)] |
| 120 | + 0 |
| 121 | + } |
| 122 | + |
| 123 | + /// Return the group-id of the file or 0 on windows. |
| 124 | + pub fn gid(&self) -> u32 { |
| 125 | + #[cfg(not(windows))] |
| 126 | + { |
| 127 | + self.0.st_gid as u32 |
| 128 | + } |
| 129 | + #[cfg(windows)] |
| 130 | + 0 |
| 131 | + } |
| 132 | + |
| 133 | + /// Return `true` if the file's executable bit is set, or `false` on windows. |
| 134 | + pub fn is_executable(&self) -> bool { |
| 135 | + #[cfg(not(windows))] |
| 136 | + { |
| 137 | + (self.0.st_mode & libc::S_IFMT) == libc::S_IFREG && self.0.st_mode & libc::S_IXUSR == libc::S_IXUSR |
| 138 | + } |
| 139 | + #[cfg(windows)] |
| 140 | + gix_fs::is_executable(&self.0) |
| 141 | + } |
| 142 | + |
| 143 | + /// Return `true` if the file's is a symbolic link. |
| 144 | + pub fn is_symlink(&self) -> bool { |
| 145 | + #[cfg(not(windows))] |
| 146 | + { |
| 147 | + (self.0.st_mode & libc::S_IFMT) == libc::S_IFLNK |
| 148 | + } |
| 149 | + #[cfg(windows)] |
| 150 | + self.0.is_symlink() |
| 151 | + } |
| 152 | + |
| 153 | + /// Return `true` if this is a regular file, executable or not. |
| 154 | + pub fn is_file(&self) -> bool { |
| 155 | + #[cfg(not(windows))] |
| 156 | + { |
| 157 | + (self.0.st_mode & libc::S_IFMT) == libc::S_IFREG |
| 158 | + } |
| 159 | + #[cfg(windows)] |
| 160 | + self.0.is_file() |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +fn system_time_from_secs_nanos(secs: u64, nanos: u32) -> SystemTime { |
| 165 | + std::time::UNIX_EPOCH + Duration::new(secs, nanos) |
| 166 | +} |
0 commit comments