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