| 1 |
// status.rs |
| 2 |
// |
| 3 |
// Copyright 2019 Raphaël Gomès <[email protected]> |
| 4 |
// |
| 5 |
// This software may be used and distributed according to the terms of the |
| 6 |
// GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
//! Rust implementation of dirstate.status (dirstate.py). |
| 9 |
//! It is currently missing a lot of functionality compared to the Python one |
| 10 |
//! and will only be triggered in narrow cases. |
| 11 |
|
| 12 |
use std::borrow::Cow; |
| 13 |
use std::fmt; |
| 14 |
use std::io; |
| 15 |
use std::os::unix::prelude::FileTypeExt; |
| 16 |
use std::path::Path; |
| 17 |
use std::path::PathBuf; |
| 18 |
use std::sync::Mutex; |
| 19 |
|
| 20 |
use once_cell::sync::OnceCell; |
| 21 |
use rayon::prelude::*; |
| 22 |
use sha1::Digest; |
| 23 |
use sha1::Sha1; |
| 24 |
|
| 25 |
use crate::dirstate::dirstate_map::BorrowedPath; |
| 26 |
use crate::dirstate::dirstate_map::ChildNodesRef; |
| 27 |
use crate::dirstate::dirstate_map::DirstateMap; |
| 28 |
use crate::dirstate::dirstate_map::DirstateVersion; |
| 29 |
use crate::dirstate::dirstate_map::NodeRef; |
| 30 |
use crate::dirstate::entry::TruncatedTimestamp; |
| 31 |
use crate::dirstate::on_disk::DirstateV2ParseError; |
| 32 |
use crate::file_patterns::PatternError; |
| 33 |
use crate::matchers::get_ignore_function; |
| 34 |
use crate::matchers::Matcher; |
| 35 |
use crate::matchers::VisitChildrenSet; |
| 36 |
use crate::utils::files::filesystem_now; |
| 37 |
use crate::utils::files::get_bytes_from_os_string; |
| 38 |
use crate::utils::files::get_bytes_from_path; |
| 39 |
use crate::utils::files::get_path_from_bytes; |
| 40 |
use crate::utils::hg_path::hg_path_to_path_buf; |
| 41 |
use crate::utils::hg_path::HgPath; |
| 42 |
use crate::utils::hg_path::HgPathError; |
| 43 |
use crate::warnings::HgWarningSender; |
| 44 |
|
| 45 |
/// Wrong type of file from a `BadMatch` |
| 46 |
/// Note: a lot of those don't exist on all platforms. |
| 47 |
#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 48 |
pub enum BadType { |
| 49 |
CharacterDevice, |
| 50 |
BlockDevice, |
| 51 |
FIFO, |
| 52 |
Socket, |
| 53 |
Directory, |
| 54 |
Unknown, |
| 55 |
} |
| 56 |
|
| 57 |
impl fmt::Display for BadType { |
| 58 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 59 |
f.write_str(match self { |
| 60 |
BadType::CharacterDevice => "character device", |
| 61 |
BadType::BlockDevice => "block device", |
| 62 |
BadType::FIFO => "fifo", |
| 63 |
BadType::Socket => "socket", |
| 64 |
BadType::Directory => "directory", |
| 65 |
BadType::Unknown => "unknown", |
| 66 |
}) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/// Was explicitly matched but cannot be found/accessed |
| 71 |
#[derive(Debug, Copy, Clone)] |
| 72 |
pub enum BadMatch { |
| 73 |
OsError(i32), |
| 74 |
BadType(BadType), |
| 75 |
} |
| 76 |
|
| 77 |
/// `Box<dyn Trait>` is syntactic sugar for `Box<dyn Trait + 'static>`, so add |
| 78 |
/// an explicit lifetime here to not fight `'static` bounds "out of nowhere". |
| 79 |
pub type IgnoreFnType<'a> = |
| 80 |
Box<dyn for<'r> Fn(&'r HgPath) -> bool + Sync + Send + 'a>; |
| 81 |
|
| 82 |
/// We have a good mix of owned (from directory traversal) and borrowed (from |
| 83 |
/// the dirstate/explicit) paths, this comes up a lot. |
| 84 |
pub type HgPathCow<'a> = Cow<'a, HgPath>; |
| 85 |
|
| 86 |
#[derive(Debug, Copy, Clone)] |
| 87 |
pub struct StatusOptions { |
| 88 |
/// Whether we are on a filesystem with UNIX-like exec flags |
| 89 |
pub check_exec: bool, |
| 90 |
pub list_clean: bool, |
| 91 |
pub list_unknown: bool, |
| 92 |
pub list_ignored: bool, |
| 93 |
/// Whether to populate `StatusPath::copy_source` |
| 94 |
pub list_copies: bool, |
| 95 |
/// Whether to collect traversed dirs for applying a callback later. |
| 96 |
/// Used by `hg purge` for example. |
| 97 |
pub collect_traversed_dirs: bool, |
| 98 |
// Compute empty_dirs as if the files were not deleted |
| 99 |
pub empty_dirs_keep_files: bool, |
| 100 |
} |
| 101 |
|
| 102 |
#[derive(Default)] |
| 103 |
pub struct DirstateStatus<'a> { |
| 104 |
/// The current time at the start of the `status()` algorithm, as measured |
| 105 |
/// and possibly truncated by the filesystem. |
| 106 |
pub filesystem_time_at_status_start: Option<TruncatedTimestamp>, |
| 107 |
|
| 108 |
/// Tracked files whose contents have changed since the parent revision |
| 109 |
pub modified: Vec<StatusPath<'a>>, |
| 110 |
|
| 111 |
/// Newly-tracked files that were not present in the parent |
| 112 |
pub added: Vec<StatusPath<'a>>, |
| 113 |
|
| 114 |
/// Previously-tracked files that have been (re)moved with an hg command |
| 115 |
pub removed: Vec<StatusPath<'a>>, |
| 116 |
|
| 117 |
/// (Still) tracked files that are missing, (re)moved with an non-hg |
| 118 |
/// command |
| 119 |
pub deleted: Vec<StatusPath<'a>>, |
| 120 |
|
| 121 |
/// Tracked files that are up to date with the parent. |
| 122 |
/// Only pupulated if `StatusOptions::list_clean` is true. |
| 123 |
pub clean: Vec<StatusPath<'a>>, |
| 124 |
|
| 125 |
/// Files in the working directory that are ignored with `.hgignore`. |
| 126 |
/// Only pupulated if `StatusOptions::list_ignored` is true. |
| 127 |
pub ignored: Vec<StatusPath<'a>>, |
| 128 |
|
| 129 |
/// Files in the working directory that are neither tracked nor ignored. |
| 130 |
/// Only pupulated if `StatusOptions::list_unknown` is true. |
| 131 |
pub unknown: Vec<StatusPath<'a>>, |
| 132 |
|
| 133 |
/// Was explicitly matched but cannot be found/accessed |
| 134 |
pub bad: Vec<(HgPathCow<'a>, BadMatch)>, |
| 135 |
|
| 136 |
/// Either clean or modified, but we can’t tell from filesystem metadata |
| 137 |
/// alone. The file contents need to be read and compared with that in |
| 138 |
/// the parent. |
| 139 |
pub unsure: Vec<StatusPath<'a>>, |
| 140 |
|
| 141 |
/// Only filled if `collect_traversed_dirs` is `true` |
| 142 |
pub traversed: Vec<HgPathCow<'a>>, |
| 143 |
|
| 144 |
// Contains all directories which are empty or only have empty directories |
| 145 |
// as children |
| 146 |
pub empty_dirs: Vec<HgPathCow<'a>>, |
| 147 |
|
| 148 |
/// Whether `status()` made changed to the `DirstateMap` that should be |
| 149 |
/// written back to disk |
| 150 |
pub dirty: bool, |
| 151 |
} |
| 152 |
|
| 153 |
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 154 |
pub struct StatusPath<'a> { |
| 155 |
pub path: HgPathCow<'a>, |
| 156 |
pub copy_source: Option<HgPathCow<'a>>, |
| 157 |
} |
| 158 |
|
| 159 |
#[derive(Debug, derive_more::From)] |
| 160 |
pub enum StatusError { |
| 161 |
/// An invalid path that cannot be represented in Mercurial was found |
| 162 |
Path(HgPathError), |
| 163 |
/// An invalid "ignore" pattern was found |
| 164 |
Pattern(PatternError), |
| 165 |
/// Corrupted dirstate |
| 166 |
DirstateV2ParseError(DirstateV2ParseError), |
| 167 |
} |
| 168 |
|
| 169 |
impl fmt::Display for StatusError { |
| 170 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 171 |
match self { |
| 172 |
StatusError::Path(error) => error.fmt(f), |
| 173 |
StatusError::Pattern(error) => error.fmt(f), |
| 174 |
StatusError::DirstateV2ParseError(error) => error.fmt(f), |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
/// Returns the status of the working directory compared to its parent |
| 180 |
/// changeset. |
| 181 |
/// |
| 182 |
/// This algorithm is based on traversing the filesystem tree (`fs` in function |
| 183 |
/// and variable names) and dirstate tree at the same time. The core of this |
| 184 |
/// traversal is the recursive `traverse_fs_directory_and_dirstate` function |
| 185 |
/// and its use of `itertools::merge_join_by`. When reaching a path that only |
| 186 |
/// exists in one of the two trees, depending on information requested by |
| 187 |
/// `options` we may need to traverse the remaining subtree. |
| 188 |
#[tracing::instrument(level = "debug", skip_all)] |
| 189 |
pub fn status<'dirstate>( |
| 190 |
dmap: &'dirstate mut DirstateMap, |
| 191 |
matcher: &impl Matcher, |
| 192 |
root_dir: PathBuf, |
| 193 |
ignore_files: Vec<PathBuf>, |
| 194 |
options: StatusOptions, |
| 195 |
warnings: &HgWarningSender, |
| 196 |
) -> Result<DirstateStatus<'dirstate>, StatusError> { |
| 197 |
// Also cap for a Python caller of this function, but don't complain if |
| 198 |
// the global threadpool has already been set since this code path is also |
| 199 |
// being used by `rhg`, which calls this early. |
| 200 |
let _ = crate::utils::cap_default_rayon_threads(); |
| 201 |
|
| 202 |
let (ignore_fn, patterns_changed): (IgnoreFnType, _) = |
| 203 |
if options.list_ignored || options.list_unknown { |
| 204 |
let (ignore_fn, changed) = match dmap.dirstate_version { |
| 205 |
DirstateVersion::V1 => { |
| 206 |
let ignore_fn = get_ignore_function( |
| 207 |
ignore_files, |
| 208 |
&root_dir, |
| 209 |
&mut |_source, _pattern_bytes| {}, |
| 210 |
warnings, |
| 211 |
)?; |
| 212 |
(ignore_fn, None) |
| 213 |
} |
| 214 |
DirstateVersion::V2 => { |
| 215 |
let mut hasher = Sha1::new(); |
| 216 |
let ignore_fn = get_ignore_function( |
| 217 |
ignore_files, |
| 218 |
&root_dir, |
| 219 |
&mut |source, pattern_bytes| { |
| 220 |
// If inside the repo, use the relative version to |
| 221 |
// make it deterministic inside tests. |
| 222 |
// The performance hit should be negligible. |
| 223 |
let source = source |
| 224 |
.strip_prefix(&root_dir) |
| 225 |
.unwrap_or(source); |
| 226 |
let source = get_bytes_from_path(source); |
| 227 |
|
| 228 |
let mut subhasher = Sha1::new(); |
| 229 |
subhasher.update(pattern_bytes); |
| 230 |
let patterns_hash = subhasher.finalize(); |
| 231 |
|
| 232 |
hasher.update(source); |
| 233 |
hasher.update(b" "); |
| 234 |
hasher.update(patterns_hash); |
| 235 |
hasher.update(b"\n"); |
| 236 |
}, |
| 237 |
warnings, |
| 238 |
)?; |
| 239 |
let new_hash = *hasher.finalize().as_ref(); |
| 240 |
let changed = new_hash != dmap.ignore_patterns_hash; |
| 241 |
dmap.ignore_patterns_hash = new_hash; |
| 242 |
(ignore_fn, Some(changed)) |
| 243 |
} |
| 244 |
}; |
| 245 |
(ignore_fn, changed) |
| 246 |
} else { |
| 247 |
(Box::new(|&_| true), None) |
| 248 |
}; |
| 249 |
|
| 250 |
let filesystem_time_at_status_start = |
| 251 |
filesystem_now(&root_dir).ok().map(TruncatedTimestamp::from); |
| 252 |
|
| 253 |
// If the repository is under the current directory, prefer using a |
| 254 |
// relative path, so the kernel needs to traverse fewer directory in every |
| 255 |
// call to `read_dir` or `symlink_metadata`. |
| 256 |
// This is effective in the common case where the current directory is the |
| 257 |
// repository root. |
| 258 |
|
| 259 |
// TODO: Better yet would be to use libc functions like `openat` and |
| 260 |
// `fstatat` to remove such repeated traversals entirely, but the standard |
| 261 |
// library does not provide APIs based on those. |
| 262 |
// Maybe with a crate like https://crates.io/crates/openat instead? |
| 263 |
let root_dir = if let Some(relative) = std::env::current_dir() |
| 264 |
.ok() |
| 265 |
.and_then(|cwd| root_dir.strip_prefix(cwd).ok()) |
| 266 |
{ |
| 267 |
relative |
| 268 |
} else { |
| 269 |
&root_dir |
| 270 |
}; |
| 271 |
|
| 272 |
let outcome = DirstateStatus { |
| 273 |
filesystem_time_at_status_start, |
| 274 |
..Default::default() |
| 275 |
}; |
| 276 |
let common = StatusCommon { |
| 277 |
dmap, |
| 278 |
options, |
| 279 |
matcher, |
| 280 |
ignore_fn, |
| 281 |
outcome: Mutex::new(outcome), |
| 282 |
ignore_patterns_have_changed: patterns_changed, |
| 283 |
new_cacheable_directories: Default::default(), |
| 284 |
outdated_cached_directories: Default::default(), |
| 285 |
filesystem_time_at_status_start, |
| 286 |
}; |
| 287 |
let is_at_repo_root = true; |
| 288 |
let hg_path = &BorrowedPath::OnDisk(HgPath::new("")); |
| 289 |
let has_ignored_ancestor = HasIgnoredAncestor::create(None, hg_path); |
| 290 |
let root_cached_mtime = None; |
| 291 |
// If the path we have for the repository root is a symlink, do follow it. |
| 292 |
// (As opposed to symlinks within the working directory which are not |
| 293 |
// followed, using `std::fs::symlink_metadata`.) |
| 294 |
common.traverse_fs_directory_and_dirstate( |
| 295 |
&has_ignored_ancestor, |
| 296 |
dmap.root.as_ref(), |
| 297 |
hg_path, |
| 298 |
&DirEntry { |
| 299 |
hg_path: Cow::Borrowed(HgPath::new(b"")), |
| 300 |
fs_path: Cow::Borrowed(root_dir), |
| 301 |
symlink_metadata: None, |
| 302 |
file_type: FakeFileType::Directory, |
| 303 |
}, |
| 304 |
root_cached_mtime, |
| 305 |
is_at_repo_root, |
| 306 |
)?; |
| 307 |
if let Some(file_set) = common.matcher.file_set() { |
| 308 |
for file in file_set { |
| 309 |
if !file.is_empty() && !dmap.has_node(file)? { |
| 310 |
let path = hg_path_to_path_buf(file)?; |
| 311 |
if let io::Result::Err(error) = |
| 312 |
root_dir.join(path).symlink_metadata() |
| 313 |
{ |
| 314 |
common.io_error(error, file) |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
let mut outcome = common.outcome.into_inner().unwrap(); |
| 320 |
let new_cacheable = common.new_cacheable_directories.into_inner().unwrap(); |
| 321 |
let outdated = common.outdated_cached_directories.into_inner().unwrap(); |
| 322 |
|
| 323 |
outcome.dirty = common.ignore_patterns_have_changed == Some(true) |
| 324 |
|| !outdated.is_empty() |
| 325 |
|| (!new_cacheable.is_empty() |
| 326 |
&& dmap.dirstate_version == DirstateVersion::V2); |
| 327 |
|
| 328 |
// Remove outdated mtimes before adding new mtimes, in case a given |
| 329 |
// directory is both |
| 330 |
for path in &outdated { |
| 331 |
dmap.clear_cached_mtime(path)?; |
| 332 |
} |
| 333 |
for (path, mtime) in &new_cacheable { |
| 334 |
dmap.set_cached_mtime(path, *mtime)?; |
| 335 |
} |
| 336 |
|
| 337 |
Ok(outcome) |
| 338 |
} |
| 339 |
|
| 340 |
/// Bag of random things needed by various parts of the algorithm. Reduces the |
| 341 |
/// number of parameters passed to functions. |
| 342 |
struct StatusCommon<'a, 'tree, 'on_disk: 'tree> { |
| 343 |
dmap: &'tree DirstateMap<'on_disk>, |
| 344 |
options: StatusOptions, |
| 345 |
matcher: &'a dyn Matcher, |
| 346 |
ignore_fn: IgnoreFnType<'a>, |
| 347 |
outcome: Mutex<DirstateStatus<'on_disk>>, |
| 348 |
/// New timestamps of directories to be used for caching their readdirs |
| 349 |
new_cacheable_directories: |
| 350 |
Mutex<Vec<(Cow<'on_disk, HgPath>, TruncatedTimestamp)>>, |
| 351 |
/// Used to invalidate the readdir cache of directories |
| 352 |
outdated_cached_directories: Mutex<Vec<Cow<'on_disk, HgPath>>>, |
| 353 |
|
| 354 |
/// Whether ignore files like `.hgignore` have changed since the previous |
| 355 |
/// time a `status()` call wrote their hash to the dirstate. `None` means |
| 356 |
/// we don’t know as this run doesn’t list either ignored or uknown files |
| 357 |
/// and therefore isn’t reading `.hgignore`. |
| 358 |
ignore_patterns_have_changed: Option<bool>, |
| 359 |
|
| 360 |
/// The current time at the start of the `status()` algorithm, as measured |
| 361 |
/// and possibly truncated by the filesystem. |
| 362 |
filesystem_time_at_status_start: Option<TruncatedTimestamp>, |
| 363 |
} |
| 364 |
|
| 365 |
enum Outcome { |
| 366 |
Modified, |
| 367 |
Added, |
| 368 |
Removed, |
| 369 |
Deleted, |
| 370 |
Clean, |
| 371 |
Ignored, |
| 372 |
Unknown, |
| 373 |
Unsure, |
| 374 |
} |
| 375 |
|
| 376 |
/// Lazy computation of whether a given path has a hgignored |
| 377 |
/// ancestor. |
| 378 |
struct HasIgnoredAncestor<'a> { |
| 379 |
/// `path` and `parent` constitute the inputs to the computation, |
| 380 |
/// `cache` stores the outcome. |
| 381 |
path: &'a HgPath, |
| 382 |
parent: Option<&'a HasIgnoredAncestor<'a>>, |
| 383 |
cache: OnceCell<bool>, |
| 384 |
} |
| 385 |
|
| 386 |
impl<'a> HasIgnoredAncestor<'a> { |
| 387 |
fn create( |
| 388 |
parent: Option<&'a HasIgnoredAncestor<'a>>, |
| 389 |
path: &'a HgPath, |
| 390 |
) -> HasIgnoredAncestor<'a> { |
| 391 |
Self { path, parent, cache: OnceCell::new() } |
| 392 |
} |
| 393 |
|
| 394 |
fn force(&self, ignore_fn: &IgnoreFnType<'_>) -> bool { |
| 395 |
match self.parent { |
| 396 |
None => false, |
| 397 |
Some(parent) => { |
| 398 |
*(self.cache.get_or_init(|| { |
| 399 |
parent.force(ignore_fn) || ignore_fn(self.path) |
| 400 |
})) |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
/// Groups the information we need to bubble up the status recursion so that |
| 407 |
/// a given parent context can make decisions base on its descendants. |
| 408 |
#[derive(Debug)] |
| 409 |
struct RecursiveResponse { |
| 410 |
/// `true` if all children are either ignored or have a dirstate node |
| 411 |
ignored_or_dirstate: bool, |
| 412 |
/// `true` if the subtree will be removed in context of `purge` |
| 413 |
subtree_will_be_purged: bool, |
| 414 |
} |
| 415 |
|
| 416 |
impl RecursiveResponse { |
| 417 |
fn identity() -> Self { |
| 418 |
Self { ignored_or_dirstate: true, subtree_will_be_purged: true } |
| 419 |
} |
| 420 |
|
| 421 |
fn combine(a: Self, b: Self) -> Self { |
| 422 |
Self { |
| 423 |
ignored_or_dirstate: a.ignored_or_dirstate && b.ignored_or_dirstate, |
| 424 |
subtree_will_be_purged: a.subtree_will_be_purged |
| 425 |
&& b.subtree_will_be_purged, |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
impl<'tree, 'on_disk> StatusCommon<'_, 'tree, 'on_disk> { |
| 431 |
fn push_outcome( |
| 432 |
&self, |
| 433 |
which: Outcome, |
| 434 |
dirstate_node: &NodeRef<'tree, 'on_disk>, |
| 435 |
) -> Result<(), DirstateV2ParseError> { |
| 436 |
let path = dirstate_node |
| 437 |
.full_path_borrowed(self.dmap.on_disk)? |
| 438 |
.detach_from_tree(); |
| 439 |
let copy_source = if self.options.list_copies { |
| 440 |
dirstate_node |
| 441 |
.copy_source_borrowed(self.dmap.on_disk)? |
| 442 |
.map(|source| source.detach_from_tree()) |
| 443 |
} else { |
| 444 |
None |
| 445 |
}; |
| 446 |
self.push_outcome_common(which, path, copy_source); |
| 447 |
Ok(()) |
| 448 |
} |
| 449 |
|
| 450 |
fn push_outcome_without_copy_source( |
| 451 |
&self, |
| 452 |
which: Outcome, |
| 453 |
path: &BorrowedPath<'_, 'on_disk>, |
| 454 |
) { |
| 455 |
self.push_outcome_common(which, path.detach_from_tree(), None) |
| 456 |
} |
| 457 |
|
| 458 |
fn push_outcome_common( |
| 459 |
&self, |
| 460 |
which: Outcome, |
| 461 |
path: HgPathCow<'on_disk>, |
| 462 |
copy_source: Option<HgPathCow<'on_disk>>, |
| 463 |
) { |
| 464 |
let mut outcome = self.outcome.lock().unwrap(); |
| 465 |
let vec = match which { |
| 466 |
Outcome::Modified => &mut outcome.modified, |
| 467 |
Outcome::Added => &mut outcome.added, |
| 468 |
Outcome::Removed => &mut outcome.removed, |
| 469 |
Outcome::Deleted => &mut outcome.deleted, |
| 470 |
Outcome::Clean => &mut outcome.clean, |
| 471 |
Outcome::Ignored => &mut outcome.ignored, |
| 472 |
Outcome::Unknown => &mut outcome.unknown, |
| 473 |
Outcome::Unsure => &mut outcome.unsure, |
| 474 |
}; |
| 475 |
vec.push(StatusPath { path, copy_source }); |
| 476 |
} |
| 477 |
|
| 478 |
fn read_dir<'a>( |
| 479 |
&self, |
| 480 |
hg_path: &HgPath, |
| 481 |
fs_path: &'a Path, |
| 482 |
is_at_repo_root: bool, |
| 483 |
) -> Result<ReadDirResponse<'a>, ()> { |
| 484 |
DirEntry::read_dir(fs_path, is_at_repo_root) |
| 485 |
.map_err(|error| self.io_error(error, hg_path)) |
| 486 |
} |
| 487 |
|
| 488 |
fn io_error(&self, error: std::io::Error, hg_path: &HgPath) { |
| 489 |
let errno = error.raw_os_error().expect("expected real OS error"); |
| 490 |
self.outcome |
| 491 |
.lock() |
| 492 |
.unwrap() |
| 493 |
.bad |
| 494 |
.push((hg_path.to_owned().into(), BadMatch::OsError(errno))) |
| 495 |
} |
| 496 |
|
| 497 |
fn check_for_outdated_directory_cache( |
| 498 |
&self, |
| 499 |
dirstate_node: &NodeRef<'tree, 'on_disk>, |
| 500 |
) -> Result<bool, DirstateV2ParseError> { |
| 501 |
if self.ignore_patterns_have_changed == Some(true) |
| 502 |
&& dirstate_node.cached_directory_mtime()?.is_some() |
| 503 |
{ |
| 504 |
self.outdated_cached_directories.lock().unwrap().push( |
| 505 |
dirstate_node |
| 506 |
.full_path_borrowed(self.dmap.on_disk)? |
| 507 |
.detach_from_tree(), |
| 508 |
); |
| 509 |
return Ok(true); |
| 510 |
} |
| 511 |
Ok(false) |
| 512 |
} |
| 513 |
|
| 514 |
/// If this returns true, we can get accurate results by only using |
| 515 |
/// `symlink_metadata` for child nodes that exist in the dirstate and don’t |
| 516 |
/// need to call `read_dir`. |
| 517 |
fn can_skip_fs_readdir( |
| 518 |
&self, |
| 519 |
directory_entry: &DirEntry, |
| 520 |
cached_directory_mtime: Option<TruncatedTimestamp>, |
| 521 |
) -> bool { |
| 522 |
if !self.options.list_unknown && !self.options.list_ignored { |
| 523 |
// All states that we care about listing have corresponding |
| 524 |
// dirstate entries. |
| 525 |
// This happens for example with `hg status -mard`. |
| 526 |
return true; |
| 527 |
} |
| 528 |
if !self.options.list_ignored |
| 529 |
&& self.ignore_patterns_have_changed == Some(false) |
| 530 |
{ |
| 531 |
if let Some(cached_mtime) = cached_directory_mtime { |
| 532 |
// The dirstate contains a cached mtime for this directory, set |
| 533 |
// by a previous run of the `status` algorithm which found this |
| 534 |
// directory eligible for `read_dir` caching. |
| 535 |
if let Ok(meta) = directory_entry.symlink_metadata() { |
| 536 |
if cached_mtime |
| 537 |
.likely_equal_to_mtime_of(&meta) |
| 538 |
.unwrap_or(false) |
| 539 |
{ |
| 540 |
// The mtime of that directory has not changed |
| 541 |
// since then, which means that the results of |
| 542 |
// `read_dir` should also be unchanged. |
| 543 |
return true; |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
} |
| 548 |
false |
| 549 |
} |
| 550 |
|
| 551 |
fn should_visit(set: &VisitChildrenSet, basename: &HgPath) -> bool { |
| 552 |
match set { |
| 553 |
VisitChildrenSet::This | VisitChildrenSet::Recursive => true, |
| 554 |
VisitChildrenSet::Empty => false, |
| 555 |
VisitChildrenSet::Set(children_to_visit) => { |
| 556 |
children_to_visit.contains(basename) |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
/// Returns whether all child entries of the filesystem directory have a |
| 562 |
/// corresponding dirstate node or are ignored. |
| 563 |
#[cfg_attr( |
| 564 |
feature = "full-tracing", |
| 565 |
tracing::instrument(level = "trace", skip_all) |
| 566 |
)] |
| 567 |
fn traverse_fs_directory_and_dirstate<'ancestor>( |
| 568 |
&self, |
| 569 |
has_ignored_ancestor: &'ancestor HasIgnoredAncestor<'ancestor>, |
| 570 |
dirstate_nodes: ChildNodesRef<'tree, 'on_disk>, |
| 571 |
directory_hg_path: &BorrowedPath<'tree, 'on_disk>, |
| 572 |
directory_entry: &DirEntry, |
| 573 |
cached_directory_mtime: Option<TruncatedTimestamp>, |
| 574 |
is_at_repo_root: bool, |
| 575 |
) -> Result<RecursiveResponse, DirstateV2ParseError> { |
| 576 |
let children_set = self.matcher.visit_children_set(directory_hg_path); |
| 577 |
if let VisitChildrenSet::Empty = children_set { |
| 578 |
return Ok(RecursiveResponse { |
| 579 |
ignored_or_dirstate: false, |
| 580 |
subtree_will_be_purged: true, |
| 581 |
}); |
| 582 |
} |
| 583 |
if self.can_skip_fs_readdir(directory_entry, cached_directory_mtime) { |
| 584 |
dirstate_nodes |
| 585 |
.par_iter() |
| 586 |
.map(|dirstate_node| { |
| 587 |
let fs_path = &directory_entry.fs_path; |
| 588 |
let basename = |
| 589 |
dirstate_node.base_name(self.dmap.on_disk)?.as_bytes(); |
| 590 |
let fs_path = fs_path.join(get_path_from_bytes(basename)); |
| 591 |
if !Self::should_visit(&children_set, HgPath::new(basename)) |
| 592 |
{ |
| 593 |
return Ok(()); |
| 594 |
} |
| 595 |
match std::fs::symlink_metadata(&fs_path) { |
| 596 |
Ok(fs_metadata) => { |
| 597 |
let file_type = fs_metadata.file_type().into(); |
| 598 |
let entry = DirEntry { |
| 599 |
hg_path: Cow::Borrowed( |
| 600 |
dirstate_node |
| 601 |
.full_path(self.dmap.on_disk)?, |
| 602 |
), |
| 603 |
fs_path: Cow::Borrowed(&fs_path), |
| 604 |
symlink_metadata: Some(fs_metadata), |
| 605 |
file_type, |
| 606 |
}; |
| 607 |
|
| 608 |
let _: RecursiveResponse = self |
| 609 |
.traverse_fs_and_dirstate( |
| 610 |
&entry, |
| 611 |
dirstate_node, |
| 612 |
has_ignored_ancestor, |
| 613 |
)?; |
| 614 |
Ok(()) |
| 615 |
} |
| 616 |
Err(e) if e.kind() == std::io::ErrorKind::NotFound => { |
| 617 |
self.traverse_dirstate_only(dirstate_node) |
| 618 |
} |
| 619 |
Err(error) => { |
| 620 |
let hg_path = |
| 621 |
dirstate_node.full_path(self.dmap.on_disk)?; |
| 622 |
self.io_error(error, hg_path); |
| 623 |
Ok(()) |
| 624 |
} |
| 625 |
} |
| 626 |
}) |
| 627 |
.collect::<Result<(), _>>()?; |
| 628 |
|
| 629 |
// We don’t know, so conservatively say this isn’t the case |
| 630 |
let ignored_or_dirstate = false; |
| 631 |
|
| 632 |
return Ok(RecursiveResponse { |
| 633 |
ignored_or_dirstate, |
| 634 |
subtree_will_be_purged: false, |
| 635 |
}); |
| 636 |
} |
| 637 |
|
| 638 |
let (mut fs_entries, readdir_succeeded, is_nested_repo) = match self |
| 639 |
.read_dir( |
| 640 |
directory_hg_path, |
| 641 |
&directory_entry.fs_path, |
| 642 |
is_at_repo_root, |
| 643 |
) { |
| 644 |
Ok(ReadDirResponse::NestedRepo) => (Vec::new(), true, true), |
| 645 |
Ok(ReadDirResponse::Dir(entries)) => (entries, true, false), |
| 646 |
_ => { |
| 647 |
// Treat an unreadable directory (typically because of |
| 648 |
// insufficient permissions) like an empty |
| 649 |
// directory. `self.read_dir` has already called |
| 650 |
// `self.io_error` so a warning will be emitted. |
| 651 |
// We still need to remember that there was an error so that we |
| 652 |
// know not to cache this result. |
| 653 |
|
| 654 |
(Vec::new(), false, false) |
| 655 |
} |
| 656 |
}; |
| 657 |
|
| 658 |
// `merge_join_by` requires both its input iterators to be sorted: |
| 659 |
|
| 660 |
let dirstate_nodes = dirstate_nodes.sorted(); |
| 661 |
// `sort_unstable_by_key` doesn’t allow keys borrowing from the value: |
| 662 |
// https://github.com/rust-lang/rust/issues/34162 |
| 663 |
fs_entries.sort_unstable_by(|e1, e2| e1.hg_path.cmp(&e2.hg_path)); |
| 664 |
|
| 665 |
// Propagate here any error that would happen inside the comparison |
| 666 |
// callback below |
| 667 |
for dirstate_node in &dirstate_nodes { |
| 668 |
dirstate_node.base_name(self.dmap.on_disk)?; |
| 669 |
} |
| 670 |
|
| 671 |
itertools::merge_join_by( |
| 672 |
dirstate_nodes, |
| 673 |
&fs_entries, |
| 674 |
|dirstate_node, fs_entry| { |
| 675 |
// This `unwrap` never panics because we already propagated |
| 676 |
// those errors above |
| 677 |
dirstate_node |
| 678 |
.base_name(self.dmap.on_disk) |
| 679 |
.unwrap() |
| 680 |
.cmp(&fs_entry.hg_path) |
| 681 |
}, |
| 682 |
) |
| 683 |
.par_bridge() |
| 684 |
.map(|pair| { |
| 685 |
use itertools::EitherOrBoth::*; |
| 686 |
let basename = match &pair { |
| 687 |
Left(dirstate_node) | Both(dirstate_node, _) => HgPath::new( |
| 688 |
dirstate_node.base_name(self.dmap.on_disk)?.as_bytes(), |
| 689 |
), |
| 690 |
Right(fs_entry) => &fs_entry.hg_path, |
| 691 |
}; |
| 692 |
if !Self::should_visit(&children_set, basename) { |
| 693 |
return Ok(RecursiveResponse { |
| 694 |
ignored_or_dirstate: false, |
| 695 |
subtree_will_be_purged: false, |
| 696 |
}); |
| 697 |
} |
| 698 |
let has_dirstate_node_or_is_ignored = match pair { |
| 699 |
Both(dirstate_node, fs_entry) => RecursiveResponse { |
| 700 |
ignored_or_dirstate: true, |
| 701 |
..self.traverse_fs_and_dirstate( |
| 702 |
fs_entry, |
| 703 |
dirstate_node, |
| 704 |
has_ignored_ancestor, |
| 705 |
)? |
| 706 |
}, |
| 707 |
Left(dirstate_node) => { |
| 708 |
self.traverse_dirstate_only(dirstate_node)?; |
| 709 |
RecursiveResponse { |
| 710 |
ignored_or_dirstate: true, |
| 711 |
subtree_will_be_purged: true, |
| 712 |
} |
| 713 |
} |
| 714 |
Right(fs_entry) => self.traverse_fs_only( |
| 715 |
has_ignored_ancestor.force(&self.ignore_fn), |
| 716 |
directory_hg_path, |
| 717 |
fs_entry, |
| 718 |
), |
| 719 |
}; |
| 720 |
|
| 721 |
Ok(has_dirstate_node_or_is_ignored) |
| 722 |
}) |
| 723 |
.try_reduce(RecursiveResponse::identity, |a, b| { |
| 724 |
Ok(RecursiveResponse::combine(a, b)) |
| 725 |
}) |
| 726 |
.map(|response| { |
| 727 |
RecursiveResponse::combine( |
| 728 |
response, |
| 729 |
RecursiveResponse { |
| 730 |
ignored_or_dirstate: readdir_succeeded, |
| 731 |
subtree_will_be_purged: !is_nested_repo, |
| 732 |
}, |
| 733 |
) |
| 734 |
}) |
| 735 |
} |
| 736 |
|
| 737 |
#[cfg_attr( |
| 738 |
feature = "full-tracing", |
| 739 |
tracing::instrument(level = "trace", skip_all) |
| 740 |
)] |
| 741 |
fn traverse_fs_and_dirstate<'ancestor>( |
| 742 |
&self, |
| 743 |
fs_entry: &DirEntry, |
| 744 |
dirstate_node: NodeRef<'tree, 'on_disk>, |
| 745 |
has_ignored_ancestor: &'ancestor HasIgnoredAncestor<'ancestor>, |
| 746 |
) -> Result<RecursiveResponse, DirstateV2ParseError> { |
| 747 |
let outdated_dircache = |
| 748 |
self.check_for_outdated_directory_cache(&dirstate_node)?; |
| 749 |
let hg_path = &dirstate_node.full_path_borrowed(self.dmap.on_disk)?; |
| 750 |
let file_or_symlink = fs_entry.is_file() || fs_entry.is_symlink(); |
| 751 |
if !file_or_symlink { |
| 752 |
// If we previously had a file here, it was removed (with |
| 753 |
// `hg rm` or similar) or deleted before it could be |
| 754 |
// replaced by a directory or something else. |
| 755 |
self.mark_removed_or_deleted_if_file(&dirstate_node)?; |
| 756 |
} |
| 757 |
if let Some(bad_type) = fs_entry.is_bad() { |
| 758 |
if self.matcher.exact_match(hg_path) { |
| 759 |
let path = dirstate_node.full_path(self.dmap.on_disk)?; |
| 760 |
self.outcome |
| 761 |
.lock() |
| 762 |
.unwrap() |
| 763 |
.bad |
| 764 |
.push((path.to_owned().into(), BadMatch::BadType(bad_type))) |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
let response = if fs_entry.is_dir() { |
| 769 |
if self.options.collect_traversed_dirs { |
| 770 |
self.outcome |
| 771 |
.lock() |
| 772 |
.unwrap() |
| 773 |
.traversed |
| 774 |
.push(hg_path.detach_from_tree()) |
| 775 |
} |
| 776 |
let is_ignored = |
| 777 |
HasIgnoredAncestor::create(Some(has_ignored_ancestor), hg_path); |
| 778 |
let is_at_repo_root = false; |
| 779 |
let recursive_response = self.traverse_fs_directory_and_dirstate( |
| 780 |
&is_ignored, |
| 781 |
dirstate_node.children(self.dmap.on_disk)?, |
| 782 |
hg_path, |
| 783 |
fs_entry, |
| 784 |
dirstate_node.cached_directory_mtime()?, |
| 785 |
is_at_repo_root, |
| 786 |
)?; |
| 787 |
|
| 788 |
if recursive_response.subtree_will_be_purged |
| 789 |
&& self.matcher.matches(hg_path) |
| 790 |
{ |
| 791 |
self.outcome |
| 792 |
.lock() |
| 793 |
.unwrap() |
| 794 |
.empty_dirs |
| 795 |
.push(hg_path.detach_from_tree()) |
| 796 |
} |
| 797 |
|
| 798 |
let children_all_have_dirstate_node_or_are_ignored = |
| 799 |
recursive_response.ignored_or_dirstate; |
| 800 |
self.maybe_save_directory_mtime( |
| 801 |
children_all_have_dirstate_node_or_are_ignored, |
| 802 |
fs_entry, |
| 803 |
dirstate_node, |
| 804 |
outdated_dircache, |
| 805 |
)?; |
| 806 |
|
| 807 |
recursive_response |
| 808 |
} else { |
| 809 |
let recursive_response = if file_or_symlink |
| 810 |
&& self.matcher.matches(hg_path) |
| 811 |
{ |
| 812 |
if let Some(entry) = dirstate_node.entry()? { |
| 813 |
if !entry.any_tracked() { |
| 814 |
// Forward-compat if we start tracking unknown/ignored |
| 815 |
// files for caching reasons |
| 816 |
|
| 817 |
self.mark_unknown_or_ignored( |
| 818 |
has_ignored_ancestor.force(&self.ignore_fn), |
| 819 |
hg_path, |
| 820 |
); |
| 821 |
} |
| 822 |
if entry.added() { |
| 823 |
self.push_outcome(Outcome::Added, &dirstate_node)?; |
| 824 |
} else if entry.removed() { |
| 825 |
self.push_outcome(Outcome::Removed, &dirstate_node)?; |
| 826 |
} else if entry.modified() { |
| 827 |
self.push_outcome(Outcome::Modified, &dirstate_node)?; |
| 828 |
} else { |
| 829 |
self.handle_normal_file(&dirstate_node, fs_entry)?; |
| 830 |
} |
| 831 |
|
| 832 |
RecursiveResponse { |
| 833 |
ignored_or_dirstate: false, |
| 834 |
subtree_will_be_purged: entry.removed(), |
| 835 |
} |
| 836 |
} else { |
| 837 |
// `node.entry.is_none()` indicates a "directory" |
| 838 |
// node, but the filesystem has a file |
| 839 |
self.mark_unknown_or_ignored( |
| 840 |
has_ignored_ancestor.force(&self.ignore_fn), |
| 841 |
hg_path, |
| 842 |
); |
| 843 |
|
| 844 |
RecursiveResponse { |
| 845 |
ignored_or_dirstate: true, |
| 846 |
subtree_will_be_purged: false, |
| 847 |
} |
| 848 |
} |
| 849 |
} else { |
| 850 |
RecursiveResponse { |
| 851 |
ignored_or_dirstate: false, |
| 852 |
subtree_will_be_purged: false, |
| 853 |
} |
| 854 |
}; |
| 855 |
|
| 856 |
for child_node in dirstate_node.children(self.dmap.on_disk)?.iter() |
| 857 |
{ |
| 858 |
self.traverse_dirstate_only(child_node)?; |
| 859 |
} |
| 860 |
|
| 861 |
recursive_response |
| 862 |
}; |
| 863 |
|
| 864 |
Ok(response) |
| 865 |
} |
| 866 |
|
| 867 |
/// Save directory mtime if applicable. |
| 868 |
/// |
| 869 |
/// `outdated_directory_cache` is `true` if we've just invalidated the |
| 870 |
/// cache for this directory in `check_for_outdated_directory_cache`, |
| 871 |
/// which forces the update. |
| 872 |
fn maybe_save_directory_mtime( |
| 873 |
&self, |
| 874 |
children_all_have_dirstate_node_or_are_ignored: bool, |
| 875 |
directory_entry: &DirEntry, |
| 876 |
dirstate_node: NodeRef<'tree, 'on_disk>, |
| 877 |
outdated_directory_cache: bool, |
| 878 |
) -> Result<(), DirstateV2ParseError> { |
| 879 |
if !children_all_have_dirstate_node_or_are_ignored { |
| 880 |
return Ok(()); |
| 881 |
} |
| 882 |
// All filesystem directory entries from `read_dir` have a |
| 883 |
// corresponding node in the dirstate, so we can reconstitute the |
| 884 |
// names of those entries without calling `read_dir` again. |
| 885 |
|
| 886 |
// TODO: use let-else here and below when available: |
| 887 |
// https://github.com/rust-lang/rust/issues/87335 |
| 888 |
let status_start = |
| 889 |
if let Some(status_start) = &self.filesystem_time_at_status_start { |
| 890 |
status_start |
| 891 |
} else { |
| 892 |
return Ok(()); |
| 893 |
}; |
| 894 |
|
| 895 |
// Although the Rust standard library’s `SystemTime` type |
| 896 |
// has nanosecond precision, the times reported for a |
| 897 |
// directory’s (or file’s) modified time may have lower |
| 898 |
// resolution based on the filesystem (for example ext3 |
| 899 |
// only stores integer seconds), kernel (see |
| 900 |
// https://stackoverflow.com/a/14393315/1162888), etc. |
| 901 |
let metadata = match directory_entry.symlink_metadata() { |
| 902 |
Ok(meta) => meta, |
| 903 |
Err(_) => return Ok(()), |
| 904 |
}; |
| 905 |
|
| 906 |
let directory_mtime = match TruncatedTimestamp::for_reliable_mtime_of( |
| 907 |
&metadata, |
| 908 |
status_start, |
| 909 |
) { |
| 910 |
Ok(Some(directory_mtime)) => directory_mtime, |
| 911 |
Ok(None) => { |
| 912 |
// The directory was modified too recently, |
| 913 |
// don’t cache its `read_dir` results. |
| 914 |
// |
| 915 |
// 1. A change to this directory (direct child was added or |
| 916 |
// removed) cause its mtime to be set (possibly truncated) to |
| 917 |
// `directory_mtime` |
| 918 |
// 2. This `status` algorithm calls `read_dir` |
| 919 |
// 3. An other change is made to the same directory is made so |
| 920 |
// that calling `read_dir` agin would give different results, |
| 921 |
// but soon enough after 1. that the mtime stays the same |
| 922 |
// |
| 923 |
// On a system where the time resolution poor, this |
| 924 |
// scenario is not unlikely if all three steps are caused |
| 925 |
// by the same script. |
| 926 |
return Ok(()); |
| 927 |
} |
| 928 |
Err(_) => { |
| 929 |
// OS/libc does not support mtime? |
| 930 |
return Ok(()); |
| 931 |
} |
| 932 |
}; |
| 933 |
// We’ve observed (through `status_start`) that time has |
| 934 |
// “progressed” since `directory_mtime`, so any further |
| 935 |
// change to this directory is extremely likely to cause a |
| 936 |
// different mtime. |
| 937 |
// |
| 938 |
// Having the same mtime again is not entirely impossible |
| 939 |
// since the system clock is not monotonous. It could jump |
| 940 |
// backward to some point before `directory_mtime`, then a |
| 941 |
// directory change could potentially happen during exactly |
| 942 |
// the wrong tick. |
| 943 |
// |
| 944 |
// We deem this scenario (unlike the previous one) to be |
| 945 |
// unlikely enough in practice. |
| 946 |
|
| 947 |
let is_up_to_date = if let Some(cached) = |
| 948 |
dirstate_node.cached_directory_mtime()? |
| 949 |
{ |
| 950 |
!outdated_directory_cache && cached.likely_equal(directory_mtime) |
| 951 |
} else { |
| 952 |
false |
| 953 |
}; |
| 954 |
if !is_up_to_date { |
| 955 |
let hg_path = dirstate_node |
| 956 |
.full_path_borrowed(self.dmap.on_disk)? |
| 957 |
.detach_from_tree(); |
| 958 |
self.new_cacheable_directories |
| 959 |
.lock() |
| 960 |
.unwrap() |
| 961 |
.push((hg_path, directory_mtime)) |
| 962 |
} |
| 963 |
Ok(()) |
| 964 |
} |
| 965 |
|
| 966 |
/// A file that is clean in the dirstate was found in the filesystem |
| 967 |
fn handle_normal_file( |
| 968 |
&self, |
| 969 |
dirstate_node: &NodeRef<'tree, 'on_disk>, |
| 970 |
fs_entry: &DirEntry, |
| 971 |
) -> Result<(), DirstateV2ParseError> { |
| 972 |
// Keep the low 31 bits |
| 973 |
fn truncate_u64(value: u64) -> i32 { |
| 974 |
(value & 0x7FFF_FFFF) as i32 |
| 975 |
} |
| 976 |
|
| 977 |
let fs_metadata = match fs_entry.symlink_metadata() { |
| 978 |
Ok(meta) => meta, |
| 979 |
Err(_) => return Ok(()), |
| 980 |
}; |
| 981 |
|
| 982 |
let entry = dirstate_node |
| 983 |
.entry()? |
| 984 |
.expect("handle_normal_file called with entry-less node"); |
| 985 |
let mode_changed = |
| 986 |
|| self.options.check_exec && entry.mode_changed(&fs_metadata); |
| 987 |
let size = entry.size(); |
| 988 |
let size_changed = size != truncate_u64(fs_metadata.len()); |
| 989 |
if size >= 0 && size_changed && fs_metadata.file_type().is_symlink() { |
| 990 |
// issue6456: Size returned may be longer due to encryption |
| 991 |
// on EXT-4 fscrypt. TODO maybe only do it on EXT4? |
| 992 |
self.push_outcome(Outcome::Unsure, dirstate_node)? |
| 993 |
} else if dirstate_node.has_copy_source() |
| 994 |
|| entry.is_from_other_parent() |
| 995 |
|| (size >= 0 && (size_changed || mode_changed())) |
| 996 |
{ |
| 997 |
self.push_outcome(Outcome::Modified, dirstate_node)? |
| 998 |
} else { |
| 999 |
let mtime_looks_clean = if let Some(dirstate_mtime) = |
| 1000 |
entry.truncated_mtime() |
| 1001 |
{ |
| 1002 |
let fs_mtime = TruncatedTimestamp::for_mtime_of(&fs_metadata) |
| 1003 |
.expect("OS/libc does not support mtime?"); |
| 1004 |
// There might be a change in the future if for example the |
| 1005 |
// internal clock become off while process run, but this is a |
| 1006 |
// case where the issues the user would face |
| 1007 |
// would be a lot worse and there is nothing we |
| 1008 |
// can really do. |
| 1009 |
fs_mtime.likely_equal(dirstate_mtime) |
| 1010 |
} else { |
| 1011 |
// No mtime in the dirstate entry |
| 1012 |
false |
| 1013 |
}; |
| 1014 |
if !mtime_looks_clean { |
| 1015 |
self.push_outcome(Outcome::Unsure, dirstate_node)? |
| 1016 |
} else if self.options.list_clean { |
| 1017 |
self.push_outcome(Outcome::Clean, dirstate_node)? |
| 1018 |
} |
| 1019 |
} |
| 1020 |
Ok(()) |
| 1021 |
} |
| 1022 |
|
| 1023 |
#[cfg_attr( |
| 1024 |
feature = "full-tracing", |
| 1025 |
tracing::instrument(level = "trace", skip_all) |
| 1026 |
)] |
| 1027 |
/// A node in the dirstate tree has no corresponding filesystem entry |
| 1028 |
fn traverse_dirstate_only( |
| 1029 |
&self, |
| 1030 |
dirstate_node: NodeRef<'tree, 'on_disk>, |
| 1031 |
) -> Result<(), DirstateV2ParseError> { |
| 1032 |
self.check_for_outdated_directory_cache(&dirstate_node)?; |
| 1033 |
self.mark_removed_or_deleted_if_file(&dirstate_node)?; |
| 1034 |
|
| 1035 |
dirstate_node |
| 1036 |
.children(self.dmap.on_disk)? |
| 1037 |
.par_iter() |
| 1038 |
.map(|child_node| self.traverse_dirstate_only(child_node)) |
| 1039 |
.collect() |
| 1040 |
} |
| 1041 |
|
| 1042 |
/// A node in the dirstate tree has no corresponding *file* on the |
| 1043 |
/// filesystem |
| 1044 |
/// |
| 1045 |
/// Does nothing on a "directory" node |
| 1046 |
fn mark_removed_or_deleted_if_file( |
| 1047 |
&self, |
| 1048 |
dirstate_node: &NodeRef<'tree, 'on_disk>, |
| 1049 |
) -> Result<(), DirstateV2ParseError> { |
| 1050 |
if let Some(entry) = dirstate_node.entry()? { |
| 1051 |
if !entry.any_tracked() { |
| 1052 |
// Future-compat for when we start storing ignored and unknown |
| 1053 |
// files for caching reasons |
| 1054 |
return Ok(()); |
| 1055 |
} |
| 1056 |
let path = dirstate_node.full_path(self.dmap.on_disk)?; |
| 1057 |
if self.matcher.matches(path) { |
| 1058 |
if entry.removed() { |
| 1059 |
self.push_outcome(Outcome::Removed, dirstate_node)? |
| 1060 |
} else { |
| 1061 |
self.push_outcome(Outcome::Deleted, dirstate_node)? |
| 1062 |
} |
| 1063 |
} |
| 1064 |
} |
| 1065 |
Ok(()) |
| 1066 |
} |
| 1067 |
|
| 1068 |
#[cfg_attr( |
| 1069 |
feature = "full-tracing", |
| 1070 |
tracing::instrument(level = "trace", skip_all) |
| 1071 |
)] |
| 1072 |
/// Something in the filesystem has no corresponding dirstate node |
| 1073 |
/// |
| 1074 |
/// Returns whether that path is ignored |
| 1075 |
fn traverse_fs_only( |
| 1076 |
&self, |
| 1077 |
has_ignored_ancestor: bool, |
| 1078 |
directory_hg_path: &HgPath, |
| 1079 |
fs_entry: &DirEntry, |
| 1080 |
) -> RecursiveResponse { |
| 1081 |
let hg_path = directory_hg_path.join(&fs_entry.hg_path); |
| 1082 |
let file_or_symlink = fs_entry.is_file() || fs_entry.is_symlink(); |
| 1083 |
if fs_entry.is_dir() { |
| 1084 |
let is_ignored = has_ignored_ancestor || (self.ignore_fn)(&hg_path); |
| 1085 |
let traverse_children = if is_ignored { |
| 1086 |
// Descendants of an ignored directory are all ignored |
| 1087 |
self.options.list_ignored |
| 1088 |
} else { |
| 1089 |
// Descendants of an unknown directory may be either unknown or |
| 1090 |
// ignored |
| 1091 |
self.options.list_unknown || self.options.list_ignored |
| 1092 |
}; |
| 1093 |
let subtree_will_be_purged = |
| 1094 |
match self.read_dir(&hg_path, &fs_entry.fs_path, false) { |
| 1095 |
Ok(ReadDirResponse::NestedRepo) => false, |
| 1096 |
Ok(ReadDirResponse::Dir(children_fs_entries)) |
| 1097 |
if traverse_children => |
| 1098 |
{ |
| 1099 |
children_fs_entries |
| 1100 |
.par_iter() |
| 1101 |
.map(|child_fs_entry| { |
| 1102 |
self.traverse_fs_only( |
| 1103 |
is_ignored, |
| 1104 |
&hg_path, |
| 1105 |
child_fs_entry, |
| 1106 |
) |
| 1107 |
}) |
| 1108 |
.reduce( |
| 1109 |
RecursiveResponse::identity, |
| 1110 |
RecursiveResponse::combine, |
| 1111 |
) |
| 1112 |
.subtree_will_be_purged |
| 1113 |
} |
| 1114 |
_ => true, |
| 1115 |
}; |
| 1116 |
|
| 1117 |
let subtree_will_be_purged = subtree_will_be_purged |
| 1118 |
&& (!is_ignored || self.options.list_ignored); |
| 1119 |
|
| 1120 |
if subtree_will_be_purged && self.matcher.matches(&hg_path) { |
| 1121 |
self.outcome.lock().unwrap().empty_dirs.push(hg_path.into()) |
| 1122 |
} |
| 1123 |
|
| 1124 |
RecursiveResponse { |
| 1125 |
ignored_or_dirstate: is_ignored, |
| 1126 |
subtree_will_be_purged, |
| 1127 |
} |
| 1128 |
} else if file_or_symlink { |
| 1129 |
if self.matcher.matches(&hg_path) { |
| 1130 |
let ignored = self.mark_unknown_or_ignored( |
| 1131 |
has_ignored_ancestor, |
| 1132 |
&BorrowedPath::InMemory(&hg_path), |
| 1133 |
); |
| 1134 |
|
| 1135 |
let remove_file = if ignored { |
| 1136 |
self.options.list_ignored |
| 1137 |
} else { |
| 1138 |
self.options.list_unknown |
| 1139 |
}; |
| 1140 |
|
| 1141 |
RecursiveResponse { |
| 1142 |
ignored_or_dirstate: ignored, |
| 1143 |
subtree_will_be_purged: remove_file |
| 1144 |
&& !self.options.empty_dirs_keep_files, |
| 1145 |
} |
| 1146 |
} else { |
| 1147 |
// We haven’t computed whether this path is ignored. It |
| 1148 |
// might not be, and a future run of status might have a |
| 1149 |
// different matcher that matches it. So treat it as not |
| 1150 |
// ignored. That is, inhibit readdir caching of the parent |
| 1151 |
// directory. |
| 1152 |
RecursiveResponse { |
| 1153 |
ignored_or_dirstate: false, |
| 1154 |
subtree_will_be_purged: false, |
| 1155 |
} |
| 1156 |
} |
| 1157 |
} else { |
| 1158 |
// This is neither a directory, a plain file, or a symlink. |
| 1159 |
// Treat it like an ignored file. |
| 1160 |
RecursiveResponse { |
| 1161 |
ignored_or_dirstate: true, |
| 1162 |
subtree_will_be_purged: self.options.list_ignored, |
| 1163 |
} |
| 1164 |
} |
| 1165 |
} |
| 1166 |
|
| 1167 |
/// Returns whether that path is ignored |
| 1168 |
fn mark_unknown_or_ignored( |
| 1169 |
&self, |
| 1170 |
has_ignored_ancestor: bool, |
| 1171 |
hg_path: &BorrowedPath<'_, 'on_disk>, |
| 1172 |
) -> bool { |
| 1173 |
let is_ignored = has_ignored_ancestor || (self.ignore_fn)(hg_path); |
| 1174 |
if is_ignored { |
| 1175 |
if self.options.list_ignored { |
| 1176 |
self.push_outcome_without_copy_source(Outcome::Ignored, hg_path) |
| 1177 |
} |
| 1178 |
} else if self.options.list_unknown { |
| 1179 |
self.push_outcome_without_copy_source(Outcome::Unknown, hg_path) |
| 1180 |
} |
| 1181 |
is_ignored |
| 1182 |
} |
| 1183 |
} |
| 1184 |
|
| 1185 |
/// Since [`std::fs::FileType`] cannot be built directly, we emulate what we |
| 1186 |
/// care about. |
| 1187 |
#[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 1188 |
enum FakeFileType { |
| 1189 |
File, |
| 1190 |
Directory, |
| 1191 |
Symlink, |
| 1192 |
BadType(BadType), |
| 1193 |
} |
| 1194 |
|
| 1195 |
impl From<std::fs::FileType> for FakeFileType { |
| 1196 |
fn from(f: std::fs::FileType) -> Self { |
| 1197 |
if f.is_dir() { |
| 1198 |
Self::Directory |
| 1199 |
} else if f.is_file() { |
| 1200 |
Self::File |
| 1201 |
} else if f.is_symlink() { |
| 1202 |
Self::Symlink |
| 1203 |
} else if f.is_fifo() { |
| 1204 |
Self::BadType(BadType::FIFO) |
| 1205 |
} else if f.is_block_device() { |
| 1206 |
Self::BadType(BadType::BlockDevice) |
| 1207 |
} else if f.is_char_device() { |
| 1208 |
Self::BadType(BadType::CharacterDevice) |
| 1209 |
} else if f.is_socket() { |
| 1210 |
Self::BadType(BadType::Socket) |
| 1211 |
} else { |
| 1212 |
Self::BadType(BadType::Unknown) |
| 1213 |
} |
| 1214 |
} |
| 1215 |
} |
| 1216 |
|
| 1217 |
struct DirEntry<'a> { |
| 1218 |
/// Path as stored in the dirstate, or just the filename for optimization. |
| 1219 |
hg_path: HgPathCow<'a>, |
| 1220 |
/// Filesystem path |
| 1221 |
fs_path: Cow<'a, Path>, |
| 1222 |
/// Lazily computed |
| 1223 |
symlink_metadata: Option<std::fs::Metadata>, |
| 1224 |
/// Already computed for ergonomics. |
| 1225 |
file_type: FakeFileType, |
| 1226 |
} |
| 1227 |
|
| 1228 |
enum ReadDirResponse<'a> { |
| 1229 |
NestedRepo, |
| 1230 |
Dir(Vec<DirEntry<'a>>), |
| 1231 |
} |
| 1232 |
|
| 1233 |
impl DirEntry<'_> { |
| 1234 |
/// Returns **unsorted** entries in the given directory, with name, |
| 1235 |
/// metadata and file type. |
| 1236 |
/// |
| 1237 |
/// If a `.hg` sub-directory is encountered: |
| 1238 |
/// |
| 1239 |
/// * At the repository root, ignore that sub-directory |
| 1240 |
/// * Elsewhere, we’re listing the content of a sub-repo. Returns |
| 1241 |
/// NestedRepo. |
| 1242 |
fn read_dir( |
| 1243 |
path: &Path, |
| 1244 |
is_at_repo_root: bool, |
| 1245 |
) -> io::Result<ReadDirResponse> { |
| 1246 |
// `read_dir` returns a "not found" error for the empty path |
| 1247 |
let at_cwd = path == Path::new(""); |
| 1248 |
let read_dir_path = if at_cwd { Path::new(".") } else { path }; |
| 1249 |
let mut results = Vec::new(); |
| 1250 |
for entry in read_dir_path.read_dir()? { |
| 1251 |
let entry = entry?; |
| 1252 |
let file_type = match entry.file_type() { |
| 1253 |
Ok(v) => v, |
| 1254 |
Err(e) => { |
| 1255 |
// race with file deletion? |
| 1256 |
if e.kind() == std::io::ErrorKind::NotFound { |
| 1257 |
continue; |
| 1258 |
} else { |
| 1259 |
return Err(e); |
| 1260 |
} |
| 1261 |
} |
| 1262 |
}; |
| 1263 |
let file_name = entry.file_name(); |
| 1264 |
// FIXME don't do this when cached |
| 1265 |
if file_name == ".hg" { |
| 1266 |
if is_at_repo_root { |
| 1267 |
// Skip the repo’s own .hg (might be a symlink) |
| 1268 |
continue; |
| 1269 |
} else if file_type.is_dir() { |
| 1270 |
// A .hg sub-directory at another location means a subrepo, |
| 1271 |
// skip it entirely. |
| 1272 |
return Ok(ReadDirResponse::NestedRepo); |
| 1273 |
} |
| 1274 |
} |
| 1275 |
let full_path = if at_cwd { |
| 1276 |
file_name.clone().into() |
| 1277 |
} else { |
| 1278 |
entry.path() |
| 1279 |
}; |
| 1280 |
let filename = |
| 1281 |
Cow::Owned(get_bytes_from_os_string(file_name).into()); |
| 1282 |
let file_type = FakeFileType::from(file_type); |
| 1283 |
results.push(DirEntry { |
| 1284 |
hg_path: filename, |
| 1285 |
fs_path: Cow::Owned(full_path.to_path_buf()), |
| 1286 |
symlink_metadata: None, |
| 1287 |
file_type, |
| 1288 |
}) |
| 1289 |
} |
| 1290 |
Ok(ReadDirResponse::Dir(results)) |
| 1291 |
} |
| 1292 |
|
| 1293 |
fn symlink_metadata(&self) -> Result<std::fs::Metadata, std::io::Error> { |
| 1294 |
match &self.symlink_metadata { |
| 1295 |
Some(meta) => Ok(meta.clone()), |
| 1296 |
None => std::fs::symlink_metadata(&self.fs_path), |
| 1297 |
} |
| 1298 |
} |
| 1299 |
|
| 1300 |
fn is_dir(&self) -> bool { |
| 1301 |
self.file_type == FakeFileType::Directory |
| 1302 |
} |
| 1303 |
|
| 1304 |
fn is_file(&self) -> bool { |
| 1305 |
self.file_type == FakeFileType::File |
| 1306 |
} |
| 1307 |
|
| 1308 |
fn is_symlink(&self) -> bool { |
| 1309 |
self.file_type == FakeFileType::Symlink |
| 1310 |
} |
| 1311 |
|
| 1312 |
fn is_bad(&self) -> Option<BadType> { |
| 1313 |
match self.file_type { |
| 1314 |
FakeFileType::BadType(ty) => Some(ty), |
| 1315 |
_ => None, |
| 1316 |
} |
| 1317 |
} |
| 1318 |
} |