1mod char_bag;
2mod fuzzy;
3mod ignore;
4
5use crate::{
6 editor::{History, Snapshot as BufferSnapshot},
7 sum_tree::{self, Cursor, Edit, SeekBias, SumTree},
8};
9use ::ignore::gitignore::Gitignore;
10use anyhow::{anyhow, Context, Result};
11pub use fuzzy::{match_paths, PathMatch};
12use gpui::{scoped_pool, AppContext, Entity, ModelContext, ModelHandle, Task};
13use lazy_static::lazy_static;
14use parking_lot::Mutex;
15use postage::{
16 prelude::{Sink, Stream},
17 watch,
18};
19use smol::{channel::Sender, Timer};
20use std::{
21 cmp,
22 collections::{HashMap, HashSet},
23 ffi::{CStr, OsStr},
24 fmt, fs,
25 future::Future,
26 io::{self, Read, Write},
27 ops::{AddAssign, Deref},
28 os::unix::{ffi::OsStrExt, fs::MetadataExt},
29 path::{Path, PathBuf},
30 sync::{Arc, Weak},
31 time::Duration,
32};
33
34use self::{char_bag::CharBag, ignore::IgnoreStack};
35
36lazy_static! {
37 static ref GITIGNORE: &'static OsStr = OsStr::new(".gitignore");
38}
39
40#[derive(Clone, Debug)]
41enum ScanState {
42 Idle,
43 Scanning,
44 Err(Arc<io::Error>),
45}
46
47pub struct Worktree {
48 snapshot: Snapshot,
49 background_snapshot: Arc<Mutex<Snapshot>>,
50 handles: Arc<Mutex<HashMap<Arc<Path>, Weak<Mutex<FileHandleState>>>>>,
51 scan_state: (watch::Sender<ScanState>, watch::Receiver<ScanState>),
52 _event_stream_handle: fsevent::Handle,
53 poll_scheduled: bool,
54}
55
56#[derive(Clone)]
57pub struct FileHandle {
58 worktree: ModelHandle<Worktree>,
59 state: Arc<Mutex<FileHandleState>>,
60}
61
62struct FileHandleState {
63 path: Arc<Path>,
64 is_deleted: bool,
65}
66
67impl Worktree {
68 pub fn new(path: impl Into<Arc<Path>>, ctx: &mut ModelContext<Self>) -> Self {
69 let abs_path = path.into();
70 let root_name_chars = abs_path.file_name().map_or(Vec::new(), |n| {
71 n.to_string_lossy().chars().chain(Some('/')).collect()
72 });
73 let (scan_state_tx, scan_state_rx) = smol::channel::unbounded();
74 let id = ctx.model_id();
75 let snapshot = Snapshot {
76 id,
77 scan_id: 0,
78 abs_path,
79 root_name_chars,
80 ignores: Default::default(),
81 entries: Default::default(),
82 };
83 let (event_stream, event_stream_handle) =
84 fsevent::EventStream::new(&[snapshot.abs_path.as_ref()], Duration::from_millis(100));
85
86 let background_snapshot = Arc::new(Mutex::new(snapshot.clone()));
87 let handles = Arc::new(Mutex::new(Default::default()));
88
89 let tree = Self {
90 snapshot,
91 background_snapshot: background_snapshot.clone(),
92 handles: handles.clone(),
93 scan_state: watch::channel_with(ScanState::Scanning),
94 _event_stream_handle: event_stream_handle,
95 poll_scheduled: false,
96 };
97
98 std::thread::spawn(move || {
99 let scanner = BackgroundScanner::new(background_snapshot, handles, scan_state_tx, id);
100 scanner.run(event_stream)
101 });
102
103 ctx.spawn_stream(scan_state_rx, Self::observe_scan_state, |_, _| {})
104 .detach();
105
106 tree
107 }
108
109 pub fn scan_complete(&self) -> impl Future<Output = ()> {
110 let mut scan_state_rx = self.scan_state.1.clone();
111 async move {
112 let mut scan_state = Some(scan_state_rx.borrow().clone());
113 while let Some(ScanState::Scanning) = scan_state {
114 scan_state = scan_state_rx.recv().await;
115 }
116 }
117 }
118
119 pub fn next_scan_complete(&self) -> impl Future<Output = ()> {
120 let mut scan_state_rx = self.scan_state.1.clone();
121 let mut did_scan = matches!(*scan_state_rx.borrow(), ScanState::Scanning);
122 async move {
123 loop {
124 if let ScanState::Scanning = *scan_state_rx.borrow() {
125 did_scan = true;
126 } else if did_scan {
127 break;
128 }
129 scan_state_rx.recv().await;
130 }
131 }
132 }
133
134 fn observe_scan_state(&mut self, scan_state: ScanState, ctx: &mut ModelContext<Self>) {
135 let _ = self.scan_state.0.blocking_send(scan_state);
136 self.poll_entries(ctx);
137 }
138
139 fn poll_entries(&mut self, ctx: &mut ModelContext<Self>) {
140 self.snapshot = self.background_snapshot.lock().clone();
141 ctx.notify();
142
143 if self.is_scanning() && !self.poll_scheduled {
144 ctx.spawn(Timer::after(Duration::from_millis(100)), |this, _, ctx| {
145 this.poll_scheduled = false;
146 this.poll_entries(ctx);
147 })
148 .detach();
149 self.poll_scheduled = true;
150 }
151 }
152
153 fn is_scanning(&self) -> bool {
154 if let ScanState::Scanning = *self.scan_state.1.borrow() {
155 true
156 } else {
157 false
158 }
159 }
160
161 pub fn snapshot(&self) -> Snapshot {
162 self.snapshot.clone()
163 }
164
165 pub fn contains_abs_path(&self, path: &Path) -> bool {
166 path.starts_with(&self.snapshot.abs_path)
167 }
168
169 pub fn load_history(
170 &self,
171 path: &Path,
172 ctx: &AppContext,
173 ) -> impl Future<Output = Result<History>> {
174 let abs_path = self.snapshot.abs_path.join(path);
175 ctx.background_executor().spawn(async move {
176 let mut file = std::fs::File::open(&abs_path)?;
177 let mut base_text = String::new();
178 file.read_to_string(&mut base_text)?;
179 Ok(History::new(Arc::from(base_text)))
180 })
181 }
182
183 pub fn save<'a>(
184 &self,
185 path: &Path,
186 content: BufferSnapshot,
187 ctx: &AppContext,
188 ) -> Task<Result<()>> {
189 let abs_path = self.snapshot.abs_path.join(path);
190 ctx.background_executor().spawn(async move {
191 let buffer_size = content.text_summary().bytes.min(10 * 1024);
192 let file = std::fs::File::create(&abs_path)?;
193 let mut writer = std::io::BufWriter::with_capacity(buffer_size, file);
194 for chunk in content.fragments() {
195 writer.write(chunk.as_bytes())?;
196 }
197 writer.flush()?;
198 Ok(())
199 })
200 }
201}
202
203impl Entity for Worktree {
204 type Event = ();
205}
206
207impl Deref for Worktree {
208 type Target = Snapshot;
209
210 fn deref(&self) -> &Self::Target {
211 &self.snapshot
212 }
213}
214
215impl fmt::Debug for Worktree {
216 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
217 self.snapshot.fmt(f)
218 }
219}
220
221#[derive(Clone)]
222pub struct Snapshot {
223 id: usize,
224 scan_id: usize,
225 abs_path: Arc<Path>,
226 root_name_chars: Vec<char>,
227 ignores: HashMap<Arc<Path>, (Arc<Gitignore>, usize)>,
228 entries: SumTree<Entry>,
229}
230
231impl Snapshot {
232 pub fn file_count(&self) -> usize {
233 self.entries.summary().file_count
234 }
235
236 pub fn visible_file_count(&self) -> usize {
237 self.entries.summary().visible_file_count
238 }
239
240 pub fn files(&self, start: usize) -> FileIter {
241 FileIter::all(self, start)
242 }
243
244 #[cfg(test)]
245 pub fn paths(&self) -> impl Iterator<Item = &Arc<Path>> {
246 let mut cursor = self.entries.cursor::<(), ()>();
247 cursor.next();
248 cursor.map(|entry| entry.path())
249 }
250
251 pub fn visible_files(&self, start: usize) -> FileIter {
252 FileIter::visible(self, start)
253 }
254
255 fn child_entries<'a>(&'a self, path: &'a Path) -> ChildEntriesIter<'a> {
256 ChildEntriesIter::new(path, self)
257 }
258
259 pub fn root_entry(&self) -> &Entry {
260 self.entry_for_path("").unwrap()
261 }
262
263 pub fn root_name(&self) -> Option<&OsStr> {
264 self.abs_path.file_name()
265 }
266
267 pub fn root_name_chars(&self) -> &[char] {
268 &self.root_name_chars
269 }
270
271 fn entry_for_path(&self, path: impl AsRef<Path>) -> Option<&Entry> {
272 let mut cursor = self.entries.cursor::<_, ()>();
273 if cursor.seek(&PathSearch::Exact(path.as_ref()), SeekBias::Left) {
274 cursor.item()
275 } else {
276 None
277 }
278 }
279
280 pub fn inode_for_path(&self, path: impl AsRef<Path>) -> Option<u64> {
281 self.entry_for_path(path.as_ref()).map(|e| e.inode())
282 }
283
284 fn insert_entry(&mut self, entry: Entry) {
285 if !entry.is_dir() && entry.path().file_name() == Some(&GITIGNORE) {
286 let (ignore, err) = Gitignore::new(self.abs_path.join(entry.path()));
287 if let Some(err) = err {
288 log::error!("error in ignore file {:?} - {:?}", entry.path(), err);
289 }
290
291 let ignore_dir_path = entry.path().parent().unwrap();
292 self.ignores
293 .insert(ignore_dir_path.into(), (Arc::new(ignore), self.scan_id));
294 }
295 self.entries.insert(entry);
296 }
297
298 fn populate_dir(
299 &mut self,
300 parent_path: Arc<Path>,
301 entries: impl IntoIterator<Item = Entry>,
302 ignore: Option<Arc<Gitignore>>,
303 ) {
304 let mut edits = Vec::new();
305
306 let mut parent_entry = self
307 .entries
308 .get(&PathKey(parent_path.clone()))
309 .unwrap()
310 .clone();
311 if let Some(ignore) = ignore {
312 self.ignores.insert(parent_path, (ignore, self.scan_id));
313 }
314 if matches!(parent_entry.kind, EntryKind::PendingDir) {
315 parent_entry.kind = EntryKind::Dir;
316 } else {
317 unreachable!();
318 }
319 edits.push(Edit::Insert(parent_entry));
320
321 for entry in entries {
322 edits.push(Edit::Insert(entry));
323 }
324 self.entries.edit(edits);
325 }
326
327 fn remove_path(&mut self, path: &Path) {
328 let new_entries = {
329 let mut cursor = self.entries.cursor::<_, ()>();
330 let mut new_entries = cursor.slice(&PathSearch::Exact(path), SeekBias::Left);
331 cursor.seek_forward(&PathSearch::Successor(path), SeekBias::Left);
332 new_entries.push_tree(cursor.suffix());
333 new_entries
334 };
335 self.entries = new_entries;
336
337 if path.file_name() == Some(&GITIGNORE) {
338 if let Some((_, scan_id)) = self.ignores.get_mut(path.parent().unwrap()) {
339 *scan_id = self.scan_id;
340 }
341 }
342 }
343
344 fn ignore_stack_for_path(&self, path: &Path, is_dir: bool) -> Arc<IgnoreStack> {
345 let mut new_ignores = Vec::new();
346 for ancestor in path.ancestors().skip(1) {
347 if let Some((ignore, _)) = self.ignores.get(ancestor) {
348 new_ignores.push((ancestor, Some(ignore.clone())));
349 } else {
350 new_ignores.push((ancestor, None));
351 }
352 }
353
354 let mut ignore_stack = IgnoreStack::none();
355 for (parent_path, ignore) in new_ignores.into_iter().rev() {
356 if ignore_stack.is_path_ignored(&parent_path, true) {
357 ignore_stack = IgnoreStack::all();
358 break;
359 } else if let Some(ignore) = ignore {
360 ignore_stack = ignore_stack.append(Arc::from(parent_path), ignore);
361 }
362 }
363
364 if ignore_stack.is_path_ignored(path, is_dir) {
365 ignore_stack = IgnoreStack::all();
366 }
367
368 ignore_stack
369 }
370}
371
372impl fmt::Debug for Snapshot {
373 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
374 for entry in self.entries.cursor::<(), ()>() {
375 for _ in entry.path().ancestors().skip(1) {
376 write!(f, " ")?;
377 }
378 writeln!(f, "{:?} (inode: {})", entry.path(), entry.inode())?;
379 }
380 Ok(())
381 }
382}
383
384impl FileHandle {
385 pub fn path(&self) -> Arc<Path> {
386 self.state.lock().path.clone()
387 }
388
389 pub fn load_history(&self, ctx: &AppContext) -> impl Future<Output = Result<History>> {
390 self.worktree.read(ctx).load_history(&self.path(), ctx)
391 }
392
393 pub fn save<'a>(&self, content: BufferSnapshot, ctx: &AppContext) -> Task<Result<()>> {
394 let worktree = self.worktree.read(ctx);
395 worktree.save(&self.path(), content, ctx)
396 }
397
398 pub fn entry_id(&self) -> (usize, Arc<Path>) {
399 (self.worktree.id(), self.path())
400 }
401}
402
403#[derive(Clone, Debug)]
404pub struct Entry {
405 kind: EntryKind,
406 path: Arc<Path>,
407 inode: u64,
408 is_symlink: bool,
409 is_ignored: bool,
410}
411
412#[derive(Clone, Debug)]
413pub enum EntryKind {
414 PendingDir,
415 Dir,
416 File(CharBag),
417}
418
419impl Entry {
420 pub fn path(&self) -> &Arc<Path> {
421 &self.path
422 }
423
424 pub fn inode(&self) -> u64 {
425 self.inode
426 }
427
428 pub fn is_ignored(&self) -> bool {
429 self.is_ignored
430 }
431
432 fn is_dir(&self) -> bool {
433 matches!(self.kind, EntryKind::Dir | EntryKind::PendingDir)
434 }
435}
436
437impl sum_tree::Item for Entry {
438 type Summary = EntrySummary;
439
440 fn summary(&self) -> Self::Summary {
441 let file_count;
442 let visible_file_count;
443 if matches!(self.kind, EntryKind::File(_)) {
444 file_count = 1;
445 if self.is_ignored {
446 visible_file_count = 0;
447 } else {
448 visible_file_count = 1;
449 }
450 } else {
451 file_count = 0;
452 visible_file_count = 0;
453 }
454
455 EntrySummary {
456 max_path: self.path().clone(),
457 file_count,
458 visible_file_count,
459 }
460 }
461}
462
463impl sum_tree::KeyedItem for Entry {
464 type Key = PathKey;
465
466 fn key(&self) -> Self::Key {
467 PathKey(self.path().clone())
468 }
469}
470
471#[derive(Clone, Debug)]
472pub struct EntrySummary {
473 max_path: Arc<Path>,
474 file_count: usize,
475 visible_file_count: usize,
476}
477
478impl Default for EntrySummary {
479 fn default() -> Self {
480 Self {
481 max_path: Arc::from(Path::new("")),
482 file_count: 0,
483 visible_file_count: 0,
484 }
485 }
486}
487
488impl<'a> AddAssign<&'a EntrySummary> for EntrySummary {
489 fn add_assign(&mut self, rhs: &'a EntrySummary) {
490 self.max_path = rhs.max_path.clone();
491 self.file_count += rhs.file_count;
492 self.visible_file_count += rhs.visible_file_count;
493 }
494}
495
496#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
497pub struct PathKey(Arc<Path>);
498
499impl Default for PathKey {
500 fn default() -> Self {
501 Self(Path::new("").into())
502 }
503}
504
505impl<'a> sum_tree::Dimension<'a, EntrySummary> for PathKey {
506 fn add_summary(&mut self, summary: &'a EntrySummary) {
507 self.0 = summary.max_path.clone();
508 }
509}
510
511#[derive(Copy, Clone, Debug, PartialEq, Eq)]
512enum PathSearch<'a> {
513 Exact(&'a Path),
514 Successor(&'a Path),
515}
516
517impl<'a> Ord for PathSearch<'a> {
518 fn cmp(&self, other: &Self) -> cmp::Ordering {
519 match (self, other) {
520 (Self::Exact(a), Self::Exact(b)) => a.cmp(b),
521 (Self::Successor(a), Self::Exact(b)) => {
522 if b.starts_with(a) {
523 cmp::Ordering::Greater
524 } else {
525 a.cmp(b)
526 }
527 }
528 _ => todo!("not sure we need the other two cases"),
529 }
530 }
531}
532
533impl<'a> PartialOrd for PathSearch<'a> {
534 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
535 Some(self.cmp(other))
536 }
537}
538
539impl<'a> Default for PathSearch<'a> {
540 fn default() -> Self {
541 Self::Exact(Path::new("").into())
542 }
543}
544
545impl<'a: 'b, 'b> sum_tree::Dimension<'a, EntrySummary> for PathSearch<'b> {
546 fn add_summary(&mut self, summary: &'a EntrySummary) {
547 *self = Self::Exact(summary.max_path.as_ref());
548 }
549}
550
551#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, Ord, PartialOrd)]
552pub struct FileCount(usize);
553
554impl<'a> sum_tree::Dimension<'a, EntrySummary> for FileCount {
555 fn add_summary(&mut self, summary: &'a EntrySummary) {
556 self.0 += summary.file_count;
557 }
558}
559
560#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, Ord, PartialOrd)]
561pub struct VisibleFileCount(usize);
562
563impl<'a> sum_tree::Dimension<'a, EntrySummary> for VisibleFileCount {
564 fn add_summary(&mut self, summary: &'a EntrySummary) {
565 self.0 += summary.visible_file_count;
566 }
567}
568
569struct BackgroundScanner {
570 snapshot: Arc<Mutex<Snapshot>>,
571 notify: Sender<ScanState>,
572 handles: Arc<Mutex<HashMap<Arc<Path>, Weak<Mutex<FileHandleState>>>>>,
573 other_mount_paths: HashSet<PathBuf>,
574 thread_pool: scoped_pool::Pool,
575 root_char_bag: CharBag,
576}
577
578impl BackgroundScanner {
579 fn new(
580 snapshot: Arc<Mutex<Snapshot>>,
581 handles: Arc<Mutex<HashMap<Arc<Path>, Weak<Mutex<FileHandleState>>>>>,
582 notify: Sender<ScanState>,
583 worktree_id: usize,
584 ) -> Self {
585 let root_char_bag = CharBag::from(snapshot.lock().root_name_chars.as_slice());
586 let mut scanner = Self {
587 root_char_bag,
588 snapshot,
589 notify,
590 handles,
591 other_mount_paths: Default::default(),
592 thread_pool: scoped_pool::Pool::new(16, format!("worktree-{}-scanner", worktree_id)),
593 };
594 scanner.update_other_mount_paths();
595 scanner
596 }
597
598 fn update_other_mount_paths(&mut self) {
599 let path = self.snapshot.lock().abs_path.clone();
600 self.other_mount_paths.clear();
601 self.other_mount_paths.extend(
602 mounted_volume_paths()
603 .into_iter()
604 .filter(|mount_path| !path.starts_with(mount_path)),
605 );
606 }
607
608 fn abs_path(&self) -> Arc<Path> {
609 self.snapshot.lock().abs_path.clone()
610 }
611
612 fn snapshot(&self) -> Snapshot {
613 self.snapshot.lock().clone()
614 }
615
616 fn run(mut self, event_stream: fsevent::EventStream) {
617 if smol::block_on(self.notify.send(ScanState::Scanning)).is_err() {
618 return;
619 }
620
621 if let Err(err) = self.scan_dirs() {
622 if smol::block_on(self.notify.send(ScanState::Err(Arc::new(err)))).is_err() {
623 return;
624 }
625 }
626
627 if smol::block_on(self.notify.send(ScanState::Idle)).is_err() {
628 return;
629 }
630
631 event_stream.run(move |events| {
632 if smol::block_on(self.notify.send(ScanState::Scanning)).is_err() {
633 return false;
634 }
635
636 if !self.process_events(events) {
637 return false;
638 }
639
640 if smol::block_on(self.notify.send(ScanState::Idle)).is_err() {
641 return false;
642 }
643
644 true
645 });
646 }
647
648 fn scan_dirs(&self) -> io::Result<()> {
649 self.snapshot.lock().scan_id += 1;
650
651 let path: Arc<Path> = Arc::from(Path::new(""));
652 let abs_path = self.abs_path();
653 let metadata = fs::metadata(&abs_path)?;
654 let inode = metadata.ino();
655 let is_symlink = fs::symlink_metadata(&abs_path)?.file_type().is_symlink();
656
657 if metadata.file_type().is_dir() {
658 let dir_entry = Entry {
659 kind: EntryKind::PendingDir,
660 path: path.clone(),
661 inode,
662 is_symlink,
663 is_ignored: false,
664 };
665 self.snapshot.lock().insert_entry(dir_entry);
666
667 let (tx, rx) = crossbeam_channel::unbounded();
668
669 tx.send(ScanJob {
670 abs_path: abs_path.to_path_buf(),
671 path,
672 ignore_stack: IgnoreStack::none(),
673 scan_queue: tx.clone(),
674 })
675 .unwrap();
676 drop(tx);
677
678 self.thread_pool.scoped(|pool| {
679 for _ in 0..self.thread_pool.thread_count() {
680 pool.execute(|| {
681 while let Ok(job) = rx.recv() {
682 if let Err(err) = self.scan_dir(&job) {
683 log::error!("error scanning {:?}: {}", job.abs_path, err);
684 }
685 }
686 });
687 }
688 });
689 } else {
690 self.snapshot.lock().insert_entry(Entry {
691 kind: EntryKind::File(self.char_bag(&path)),
692 path,
693 inode,
694 is_symlink,
695 is_ignored: false,
696 });
697 }
698
699 Ok(())
700 }
701
702 fn scan_dir(&self, job: &ScanJob) -> io::Result<()> {
703 let mut new_entries: Vec<Entry> = Vec::new();
704 let mut new_jobs: Vec<ScanJob> = Vec::new();
705 let mut ignore_stack = job.ignore_stack.clone();
706 let mut new_ignore = None;
707
708 for child_entry in fs::read_dir(&job.abs_path)? {
709 let child_entry = child_entry?;
710 let child_name = child_entry.file_name();
711 let child_abs_path = job.abs_path.join(&child_name);
712 let child_path: Arc<Path> = job.path.join(&child_name).into();
713 let child_metadata = child_entry.metadata()?;
714 let child_inode = child_metadata.ino();
715 let child_is_symlink = child_metadata.file_type().is_symlink();
716
717 // Disallow mount points outside the file system containing the root of this worktree
718 if self.other_mount_paths.contains(&child_abs_path) {
719 continue;
720 }
721
722 // If we find a .gitignore, add it to the stack of ignores used to determine which paths are ignored
723 if child_name == *GITIGNORE {
724 let (ignore, err) = Gitignore::new(&child_abs_path);
725 if let Some(err) = err {
726 log::error!("error in ignore file {:?} - {:?}", child_path, err);
727 }
728 let ignore = Arc::new(ignore);
729 ignore_stack = ignore_stack.append(job.path.clone(), ignore.clone());
730 new_ignore = Some(ignore);
731
732 // Update ignore status of any child entries we've already processed to reflect the
733 // ignore file in the current directory. Because `.gitignore` starts with a `.`,
734 // there should rarely be too numerous. Update the ignore stack associated with any
735 // new jobs as well.
736 let mut new_jobs = new_jobs.iter_mut();
737 for entry in &mut new_entries {
738 entry.is_ignored = ignore_stack.is_path_ignored(&entry.path, entry.is_dir());
739 if entry.is_dir() {
740 new_jobs.next().unwrap().ignore_stack = if entry.is_ignored {
741 IgnoreStack::all()
742 } else {
743 ignore_stack.clone()
744 };
745 }
746 }
747 }
748
749 if child_metadata.is_dir() {
750 let is_ignored = ignore_stack.is_path_ignored(&child_path, true);
751 new_entries.push(Entry {
752 kind: EntryKind::PendingDir,
753 path: child_path.clone(),
754 inode: child_inode,
755 is_symlink: child_is_symlink,
756 is_ignored,
757 });
758 new_jobs.push(ScanJob {
759 abs_path: child_abs_path,
760 path: child_path,
761 ignore_stack: if is_ignored {
762 IgnoreStack::all()
763 } else {
764 ignore_stack.clone()
765 },
766 scan_queue: job.scan_queue.clone(),
767 });
768 } else {
769 let is_ignored = ignore_stack.is_path_ignored(&child_path, false);
770 new_entries.push(Entry {
771 kind: EntryKind::File(self.char_bag(&child_path)),
772 path: child_path,
773 inode: child_inode,
774 is_symlink: child_is_symlink,
775 is_ignored,
776 });
777 };
778 }
779
780 self.snapshot
781 .lock()
782 .populate_dir(job.path.clone(), new_entries, new_ignore);
783 for new_job in new_jobs {
784 job.scan_queue.send(new_job).unwrap();
785 }
786
787 Ok(())
788 }
789
790 fn process_events(&mut self, mut events: Vec<fsevent::Event>) -> bool {
791 self.update_other_mount_paths();
792
793 let mut snapshot = self.snapshot();
794 snapshot.scan_id += 1;
795
796 let root_abs_path = if let Ok(abs_path) = snapshot.abs_path.canonicalize() {
797 abs_path
798 } else {
799 return false;
800 };
801
802 events.sort_unstable_by(|a, b| a.path.cmp(&b.path));
803 let mut abs_paths = events.into_iter().map(|e| e.path).peekable();
804 let (scan_queue_tx, scan_queue_rx) = crossbeam_channel::unbounded();
805
806 while let Some(abs_path) = abs_paths.next() {
807 let path = match abs_path.strip_prefix(&root_abs_path) {
808 Ok(path) => Arc::from(path.to_path_buf()),
809 Err(_) => {
810 log::error!(
811 "unexpected event {:?} for root path {:?}",
812 abs_path,
813 root_abs_path
814 );
815 continue;
816 }
817 };
818
819 while abs_paths.peek().map_or(false, |p| p.starts_with(&abs_path)) {
820 abs_paths.next();
821 }
822
823 snapshot.remove_path(&path);
824
825 match self.fs_entry_for_path(path.clone(), &abs_path) {
826 Ok(Some(mut fs_entry)) => {
827 let is_dir = fs_entry.is_dir();
828 let ignore_stack = snapshot.ignore_stack_for_path(&path, is_dir);
829 fs_entry.is_ignored = ignore_stack.is_all();
830 snapshot.insert_entry(fs_entry);
831 if is_dir {
832 scan_queue_tx
833 .send(ScanJob {
834 abs_path,
835 path,
836 ignore_stack,
837 scan_queue: scan_queue_tx.clone(),
838 })
839 .unwrap();
840 }
841 }
842 Ok(None) => {}
843 Err(err) => {
844 // TODO - create a special 'error' entry in the entries tree to mark this
845 log::error!("error reading file on event {:?}", err);
846 }
847 }
848 }
849
850 *self.snapshot.lock() = snapshot;
851
852 // Scan any directories that were created as part of this event batch.
853 drop(scan_queue_tx);
854 self.thread_pool.scoped(|pool| {
855 for _ in 0..self.thread_pool.thread_count() {
856 pool.execute(|| {
857 while let Ok(job) = scan_queue_rx.recv() {
858 if let Err(err) = self.scan_dir(&job) {
859 log::error!("error scanning {:?}: {}", job.abs_path, err);
860 }
861 }
862 });
863 }
864 });
865
866 self.update_ignore_statuses();
867
868 true
869 }
870
871 fn update_ignore_statuses(&self) {
872 let mut snapshot = self.snapshot();
873
874 let mut ignores_to_update = Vec::new();
875 let mut ignores_to_delete = Vec::new();
876 for (parent_path, (_, scan_id)) in &snapshot.ignores {
877 if *scan_id == snapshot.scan_id && snapshot.entry_for_path(parent_path).is_some() {
878 ignores_to_update.push(parent_path.clone());
879 }
880
881 let ignore_path = parent_path.join(&*GITIGNORE);
882 if snapshot.entry_for_path(ignore_path).is_none() {
883 ignores_to_delete.push(parent_path.clone());
884 }
885 }
886
887 for parent_path in ignores_to_delete {
888 snapshot.ignores.remove(&parent_path);
889 self.snapshot.lock().ignores.remove(&parent_path);
890 }
891
892 let (ignore_queue_tx, ignore_queue_rx) = crossbeam_channel::unbounded();
893 ignores_to_update.sort_unstable();
894 let mut ignores_to_update = ignores_to_update.into_iter().peekable();
895 while let Some(parent_path) = ignores_to_update.next() {
896 while ignores_to_update
897 .peek()
898 .map_or(false, |p| p.starts_with(&parent_path))
899 {
900 ignores_to_update.next().unwrap();
901 }
902
903 let ignore_stack = snapshot.ignore_stack_for_path(&parent_path, true);
904 ignore_queue_tx
905 .send(UpdateIgnoreStatusJob {
906 path: parent_path,
907 ignore_stack,
908 ignore_queue: ignore_queue_tx.clone(),
909 })
910 .unwrap();
911 }
912 drop(ignore_queue_tx);
913
914 self.thread_pool.scoped(|scope| {
915 for _ in 0..self.thread_pool.thread_count() {
916 scope.execute(|| {
917 while let Ok(job) = ignore_queue_rx.recv() {
918 self.update_ignore_status(job, &snapshot);
919 }
920 });
921 }
922 });
923 }
924
925 fn update_ignore_status(&self, job: UpdateIgnoreStatusJob, snapshot: &Snapshot) {
926 let mut ignore_stack = job.ignore_stack;
927 if let Some((ignore, _)) = snapshot.ignores.get(&job.path) {
928 ignore_stack = ignore_stack.append(job.path.clone(), ignore.clone());
929 }
930
931 let mut edits = Vec::new();
932 for mut entry in snapshot.child_entries(&job.path).cloned() {
933 let was_ignored = entry.is_ignored;
934 entry.is_ignored = ignore_stack.is_path_ignored(entry.path(), entry.is_dir());
935 if entry.is_dir() {
936 let child_ignore_stack = if entry.is_ignored {
937 IgnoreStack::all()
938 } else {
939 ignore_stack.clone()
940 };
941 job.ignore_queue
942 .send(UpdateIgnoreStatusJob {
943 path: entry.path().clone(),
944 ignore_stack: child_ignore_stack,
945 ignore_queue: job.ignore_queue.clone(),
946 })
947 .unwrap();
948 }
949
950 if entry.is_ignored != was_ignored {
951 edits.push(Edit::Insert(entry));
952 }
953 }
954 self.snapshot.lock().entries.edit(edits);
955 }
956
957 fn fs_entry_for_path(&self, path: Arc<Path>, abs_path: &Path) -> Result<Option<Entry>> {
958 let metadata = match fs::metadata(&abs_path) {
959 Err(err) => {
960 return match (err.kind(), err.raw_os_error()) {
961 (io::ErrorKind::NotFound, _) => Ok(None),
962 (io::ErrorKind::Other, Some(libc::ENOTDIR)) => Ok(None),
963 _ => Err(anyhow::Error::new(err)),
964 }
965 }
966 Ok(metadata) => metadata,
967 };
968 let inode = metadata.ino();
969 let is_symlink = fs::symlink_metadata(&abs_path)
970 .context("failed to read symlink metadata")?
971 .file_type()
972 .is_symlink();
973
974 let entry = Entry {
975 kind: if metadata.file_type().is_dir() {
976 EntryKind::PendingDir
977 } else {
978 EntryKind::File(self.char_bag(&path))
979 },
980 path,
981 inode,
982 is_symlink,
983 is_ignored: false,
984 };
985
986 Ok(Some(entry))
987 }
988
989 fn char_bag(&self, path: &Path) -> CharBag {
990 let mut result = self.root_char_bag;
991 result.extend(path.to_string_lossy().chars());
992 result
993 }
994}
995
996struct ScanJob {
997 abs_path: PathBuf,
998 path: Arc<Path>,
999 ignore_stack: Arc<IgnoreStack>,
1000 scan_queue: crossbeam_channel::Sender<ScanJob>,
1001}
1002
1003struct UpdateIgnoreStatusJob {
1004 path: Arc<Path>,
1005 ignore_stack: Arc<IgnoreStack>,
1006 ignore_queue: crossbeam_channel::Sender<UpdateIgnoreStatusJob>,
1007}
1008
1009pub trait WorktreeHandle {
1010 fn file(&self, path: impl AsRef<Path>, app: &AppContext) -> Result<FileHandle>;
1011}
1012
1013impl WorktreeHandle for ModelHandle<Worktree> {
1014 fn file(&self, path: impl AsRef<Path>, app: &AppContext) -> Result<FileHandle> {
1015 let tree = self.read(app);
1016 let entry = tree
1017 .entry_for_path(&path)
1018 .ok_or_else(|| anyhow!("path does not exist in tree"))?;
1019 let path = entry.path().clone();
1020 let mut handles = tree.handles.lock();
1021 let state = if let Some(state) = handles.get(&path).and_then(Weak::upgrade) {
1022 state
1023 } else {
1024 let state = Arc::new(Mutex::new(FileHandleState {
1025 path: path.clone(),
1026 is_deleted: false,
1027 }));
1028 handles.insert(path, Arc::downgrade(&state));
1029 state
1030 };
1031
1032 Ok(FileHandle {
1033 worktree: self.clone(),
1034 state,
1035 })
1036 }
1037}
1038
1039pub enum FileIter<'a> {
1040 All(Cursor<'a, Entry, FileCount, FileCount>),
1041 Visible(Cursor<'a, Entry, VisibleFileCount, VisibleFileCount>),
1042}
1043
1044impl<'a> FileIter<'a> {
1045 fn all(snapshot: &'a Snapshot, start: usize) -> Self {
1046 let mut cursor = snapshot.entries.cursor();
1047 cursor.seek(&FileCount(start), SeekBias::Right);
1048 Self::All(cursor)
1049 }
1050
1051 fn visible(snapshot: &'a Snapshot, start: usize) -> Self {
1052 let mut cursor = snapshot.entries.cursor();
1053 cursor.seek(&VisibleFileCount(start), SeekBias::Right);
1054 Self::Visible(cursor)
1055 }
1056
1057 fn next_internal(&mut self) {
1058 match self {
1059 Self::All(cursor) => {
1060 let ix = *cursor.start();
1061 cursor.seek_forward(&FileCount(ix.0 + 1), SeekBias::Right);
1062 }
1063 Self::Visible(cursor) => {
1064 let ix = *cursor.start();
1065 cursor.seek_forward(&VisibleFileCount(ix.0 + 1), SeekBias::Right);
1066 }
1067 }
1068 }
1069
1070 fn item(&self) -> Option<&'a Entry> {
1071 match self {
1072 Self::All(cursor) => cursor.item(),
1073 Self::Visible(cursor) => cursor.item(),
1074 }
1075 }
1076}
1077
1078impl<'a> Iterator for FileIter<'a> {
1079 type Item = &'a Entry;
1080
1081 fn next(&mut self) -> Option<Self::Item> {
1082 if let Some(entry) = self.item() {
1083 self.next_internal();
1084 Some(entry)
1085 } else {
1086 None
1087 }
1088 }
1089}
1090
1091struct ChildEntriesIter<'a> {
1092 parent_path: &'a Path,
1093 cursor: Cursor<'a, Entry, PathSearch<'a>, ()>,
1094}
1095
1096impl<'a> ChildEntriesIter<'a> {
1097 fn new(parent_path: &'a Path, snapshot: &'a Snapshot) -> Self {
1098 let mut cursor = snapshot.entries.cursor();
1099 cursor.seek(&PathSearch::Exact(parent_path), SeekBias::Right);
1100 Self {
1101 parent_path,
1102 cursor,
1103 }
1104 }
1105}
1106
1107impl<'a> Iterator for ChildEntriesIter<'a> {
1108 type Item = &'a Entry;
1109
1110 fn next(&mut self) -> Option<Self::Item> {
1111 if let Some(item) = self.cursor.item() {
1112 if item.path().starts_with(self.parent_path) {
1113 self.cursor
1114 .seek_forward(&PathSearch::Successor(item.path()), SeekBias::Left);
1115 Some(item)
1116 } else {
1117 None
1118 }
1119 } else {
1120 None
1121 }
1122 }
1123}
1124
1125fn mounted_volume_paths() -> Vec<PathBuf> {
1126 unsafe {
1127 let mut stat_ptr: *mut libc::statfs = std::ptr::null_mut();
1128 let count = libc::getmntinfo(&mut stat_ptr as *mut _, libc::MNT_WAIT);
1129 if count >= 0 {
1130 std::slice::from_raw_parts(stat_ptr, count as usize)
1131 .iter()
1132 .map(|stat| {
1133 PathBuf::from(OsStr::from_bytes(
1134 CStr::from_ptr(&stat.f_mntonname[0]).to_bytes(),
1135 ))
1136 })
1137 .collect()
1138 } else {
1139 panic!("failed to run getmntinfo");
1140 }
1141 }
1142}
1143
1144#[cfg(test)]
1145mod tests {
1146 use super::*;
1147 use crate::editor::Buffer;
1148 use crate::test::*;
1149 use anyhow::Result;
1150 use gpui::App;
1151 use rand::prelude::*;
1152 use serde_json::json;
1153 use std::env;
1154 use std::fmt::Write;
1155 use std::os::unix;
1156 use std::time::{SystemTime, UNIX_EPOCH};
1157
1158 #[test]
1159 fn test_populate_and_search() {
1160 App::test_async((), |mut app| async move {
1161 let dir = temp_tree(json!({
1162 "root": {
1163 "apple": "",
1164 "banana": {
1165 "carrot": {
1166 "date": "",
1167 "endive": "",
1168 }
1169 },
1170 "fennel": {
1171 "grape": "",
1172 }
1173 }
1174 }));
1175
1176 let root_link_path = dir.path().join("root_link");
1177 unix::fs::symlink(&dir.path().join("root"), &root_link_path).unwrap();
1178
1179 let tree = app.add_model(|ctx| Worktree::new(root_link_path, ctx));
1180
1181 app.read(|ctx| tree.read(ctx).scan_complete()).await;
1182 app.read(|ctx| {
1183 let tree = tree.read(ctx);
1184 assert_eq!(tree.file_count(), 4);
1185 let results = match_paths(
1186 Some(tree.snapshot()).iter(),
1187 "bna",
1188 false,
1189 false,
1190 false,
1191 10,
1192 ctx.thread_pool().clone(),
1193 )
1194 .into_iter()
1195 .map(|result| result.path)
1196 .collect::<Vec<Arc<Path>>>();
1197 assert_eq!(
1198 results,
1199 vec![
1200 PathBuf::from("banana/carrot/date").into(),
1201 PathBuf::from("banana/carrot/endive").into(),
1202 ]
1203 );
1204 })
1205 });
1206 }
1207
1208 #[test]
1209 fn test_save_file() {
1210 App::test_async((), |mut app| async move {
1211 let dir = temp_tree(json!({
1212 "file1": "the old contents",
1213 }));
1214
1215 let tree = app.add_model(|ctx| Worktree::new(dir.path(), ctx));
1216 app.read(|ctx| tree.read(ctx).scan_complete()).await;
1217 app.read(|ctx| assert_eq!(tree.read(ctx).file_count(), 1));
1218
1219 let buffer = Buffer::new(1, "a line of text.\n".repeat(10 * 1024));
1220
1221 let path = tree.update(&mut app, |tree, ctx| {
1222 let path = tree.files(0).next().unwrap().path().clone();
1223 assert_eq!(path.file_name().unwrap(), "file1");
1224 smol::block_on(tree.save(&path, buffer.snapshot(), ctx.as_ref())).unwrap();
1225 path
1226 });
1227
1228 let loaded_history = app
1229 .read(|ctx| tree.read(ctx).load_history(&path, ctx))
1230 .await
1231 .unwrap();
1232 assert_eq!(loaded_history.base_text.as_ref(), buffer.text());
1233 });
1234 }
1235
1236 #[test]
1237 fn test_rescan_simple() {
1238 App::test_async((), |mut app| async move {
1239 let dir = temp_tree(json!({
1240 "a": {
1241 "file1": "",
1242 },
1243 "b": {
1244 "c": {
1245 "file2": "",
1246 }
1247 }
1248 }));
1249
1250 let tree = app.add_model(|ctx| Worktree::new(dir.path(), ctx));
1251 app.read(|ctx| tree.read(ctx).scan_complete()).await;
1252 app.read(|ctx| assert_eq!(tree.read(ctx).file_count(), 2));
1253
1254 let file2 = app.read(|ctx| {
1255 let file2 = tree.file("b/c/file2", ctx).unwrap();
1256 assert_eq!(file2.path().as_ref(), Path::new("b/c/file2"));
1257 file2
1258 });
1259
1260 std::fs::rename(dir.path().join("b/c"), dir.path().join("d")).unwrap();
1261
1262 app.read(|ctx| tree.read(ctx).next_scan_complete()).await;
1263
1264 app.read(|ctx| {
1265 assert_eq!(
1266 tree.read(ctx)
1267 .paths()
1268 .map(|p| p.to_str().unwrap())
1269 .collect::<Vec<_>>(),
1270 vec!["a", "a/file1", "b", "d", "d/file2"]
1271 )
1272 });
1273
1274 // tree.condition(&app, move |_, _| {
1275 // file2.path().as_ref() == Path::new("d/file2")
1276 // })
1277 // .await;
1278 });
1279 }
1280
1281 #[test]
1282 fn test_rescan_with_gitignore() {
1283 App::test_async((), |mut app| async move {
1284 let dir = temp_tree(json!({
1285 ".git": {},
1286 ".gitignore": "ignored-dir\n",
1287 "tracked-dir": {
1288 "tracked-file1": "tracked contents",
1289 },
1290 "ignored-dir": {
1291 "ignored-file1": "ignored contents",
1292 }
1293 }));
1294
1295 let tree = app.add_model(|ctx| Worktree::new(dir.path(), ctx));
1296 app.read(|ctx| tree.read(ctx).scan_complete()).await;
1297 app.read(|ctx| {
1298 let tree = tree.read(ctx);
1299 let tracked = tree.entry_for_path("tracked-dir/tracked-file1").unwrap();
1300 let ignored = tree.entry_for_path("ignored-dir/ignored-file1").unwrap();
1301 assert_eq!(tracked.is_ignored(), false);
1302 assert_eq!(ignored.is_ignored(), true);
1303 });
1304
1305 fs::write(dir.path().join("tracked-dir/tracked-file2"), "").unwrap();
1306 fs::write(dir.path().join("ignored-dir/ignored-file2"), "").unwrap();
1307 app.read(|ctx| tree.read(ctx).next_scan_complete()).await;
1308 app.read(|ctx| {
1309 let tree = tree.read(ctx);
1310 let tracked = tree.entry_for_path("tracked-dir/tracked-file2").unwrap();
1311 let ignored = tree.entry_for_path("ignored-dir/ignored-file2").unwrap();
1312 assert_eq!(tracked.is_ignored(), false);
1313 assert_eq!(ignored.is_ignored(), true);
1314 });
1315 });
1316 }
1317
1318 #[test]
1319 fn test_mounted_volume_paths() {
1320 let paths = mounted_volume_paths();
1321 assert!(paths.contains(&"/".into()));
1322 }
1323
1324 #[test]
1325 fn test_random() {
1326 let iterations = env::var("ITERATIONS")
1327 .map(|i| i.parse().unwrap())
1328 .unwrap_or(100);
1329 let operations = env::var("OPERATIONS")
1330 .map(|o| o.parse().unwrap())
1331 .unwrap_or(40);
1332 let initial_entries = env::var("INITIAL_ENTRIES")
1333 .map(|o| o.parse().unwrap())
1334 .unwrap_or(20);
1335 let seeds = if let Ok(seed) = env::var("SEED").map(|s| s.parse().unwrap()) {
1336 seed..seed + 1
1337 } else {
1338 0..iterations
1339 };
1340
1341 for seed in seeds {
1342 dbg!(seed);
1343 let mut rng = StdRng::seed_from_u64(seed);
1344
1345 let root_dir = tempdir::TempDir::new(&format!("test-{}", seed)).unwrap();
1346 for _ in 0..initial_entries {
1347 randomly_mutate_tree(root_dir.path(), 1.0, &mut rng).unwrap();
1348 }
1349 log::info!("Generated initial tree");
1350
1351 let (notify_tx, _notify_rx) = smol::channel::unbounded();
1352 let mut scanner = BackgroundScanner::new(
1353 Arc::new(Mutex::new(Snapshot {
1354 id: 0,
1355 scan_id: 0,
1356 abs_path: root_dir.path().into(),
1357 entries: Default::default(),
1358 ignores: Default::default(),
1359 root_name_chars: Default::default(),
1360 })),
1361 Arc::new(Mutex::new(Default::default())),
1362 notify_tx,
1363 0,
1364 );
1365 scanner.scan_dirs().unwrap();
1366 scanner.snapshot().check_invariants();
1367
1368 let mut events = Vec::new();
1369 let mut mutations_len = operations;
1370 while mutations_len > 1 {
1371 if !events.is_empty() && rng.gen_bool(0.4) {
1372 let len = rng.gen_range(0..=events.len());
1373 let to_deliver = events.drain(0..len).collect::<Vec<_>>();
1374 log::info!("Delivering events: {:#?}", to_deliver);
1375 scanner.process_events(to_deliver);
1376 scanner.snapshot().check_invariants();
1377 } else {
1378 events.extend(randomly_mutate_tree(root_dir.path(), 0.6, &mut rng).unwrap());
1379 mutations_len -= 1;
1380 }
1381 }
1382 log::info!("Quiescing: {:#?}", events);
1383 scanner.process_events(events);
1384 scanner.snapshot().check_invariants();
1385
1386 let (notify_tx, _notify_rx) = smol::channel::unbounded();
1387 let new_scanner = BackgroundScanner::new(
1388 Arc::new(Mutex::new(Snapshot {
1389 id: 0,
1390 scan_id: 0,
1391 abs_path: root_dir.path().into(),
1392 entries: Default::default(),
1393 ignores: Default::default(),
1394 root_name_chars: Default::default(),
1395 })),
1396 Arc::new(Mutex::new(Default::default())),
1397 notify_tx,
1398 1,
1399 );
1400 new_scanner.scan_dirs().unwrap();
1401 assert_eq!(scanner.snapshot().to_vec(), new_scanner.snapshot().to_vec());
1402 }
1403 }
1404
1405 fn randomly_mutate_tree(
1406 root_path: &Path,
1407 insertion_probability: f64,
1408 rng: &mut impl Rng,
1409 ) -> Result<Vec<fsevent::Event>> {
1410 let root_path = root_path.canonicalize().unwrap();
1411 let (dirs, files) = read_dir_recursive(root_path.clone());
1412
1413 let mut events = Vec::new();
1414 let mut record_event = |path: PathBuf| {
1415 events.push(fsevent::Event {
1416 event_id: SystemTime::now()
1417 .duration_since(UNIX_EPOCH)
1418 .unwrap()
1419 .as_secs(),
1420 flags: fsevent::StreamFlags::empty(),
1421 path,
1422 });
1423 };
1424
1425 if (files.is_empty() && dirs.len() == 1) || rng.gen_bool(insertion_probability) {
1426 let path = dirs.choose(rng).unwrap();
1427 let new_path = path.join(gen_name(rng));
1428
1429 if rng.gen() {
1430 log::info!("Creating dir {:?}", new_path.strip_prefix(root_path)?);
1431 fs::create_dir(&new_path)?;
1432 } else {
1433 log::info!("Creating file {:?}", new_path.strip_prefix(root_path)?);
1434 fs::write(&new_path, "")?;
1435 }
1436 record_event(new_path);
1437 } else if rng.gen_bool(0.05) {
1438 let ignore_dir_path = dirs.choose(rng).unwrap();
1439 let ignore_path = ignore_dir_path.join(&*GITIGNORE);
1440
1441 let (subdirs, subfiles) = read_dir_recursive(ignore_dir_path.clone());
1442 let files_to_ignore = {
1443 let len = rng.gen_range(0..=subfiles.len());
1444 subfiles.choose_multiple(rng, len)
1445 };
1446 let dirs_to_ignore = {
1447 let len = rng.gen_range(0..subdirs.len());
1448 subdirs.choose_multiple(rng, len)
1449 };
1450
1451 let mut ignore_contents = String::new();
1452 for path_to_ignore in files_to_ignore.chain(dirs_to_ignore) {
1453 write!(
1454 ignore_contents,
1455 "{}\n",
1456 path_to_ignore
1457 .strip_prefix(&ignore_dir_path)?
1458 .to_str()
1459 .unwrap()
1460 )
1461 .unwrap();
1462 }
1463 log::info!(
1464 "Creating {:?} with contents:\n{}",
1465 ignore_path.strip_prefix(&root_path)?,
1466 ignore_contents
1467 );
1468 fs::write(&ignore_path, ignore_contents).unwrap();
1469 record_event(ignore_path);
1470 } else {
1471 let old_path = {
1472 let file_path = files.choose(rng);
1473 let dir_path = dirs[1..].choose(rng);
1474 file_path.into_iter().chain(dir_path).choose(rng).unwrap()
1475 };
1476
1477 let is_rename = rng.gen();
1478 if is_rename {
1479 let new_path_parent = dirs
1480 .iter()
1481 .filter(|d| !d.starts_with(old_path))
1482 .choose(rng)
1483 .unwrap();
1484
1485 let overwrite_existing_dir =
1486 !old_path.starts_with(&new_path_parent) && rng.gen_bool(0.3);
1487 let new_path = if overwrite_existing_dir {
1488 fs::remove_dir_all(&new_path_parent).ok();
1489 new_path_parent.to_path_buf()
1490 } else {
1491 new_path_parent.join(gen_name(rng))
1492 };
1493
1494 log::info!(
1495 "Renaming {:?} to {}{:?}",
1496 old_path.strip_prefix(&root_path)?,
1497 if overwrite_existing_dir {
1498 "overwrite "
1499 } else {
1500 ""
1501 },
1502 new_path.strip_prefix(&root_path)?
1503 );
1504 fs::rename(&old_path, &new_path)?;
1505 record_event(old_path.clone());
1506 record_event(new_path);
1507 } else if old_path.is_dir() {
1508 let (dirs, files) = read_dir_recursive(old_path.clone());
1509
1510 log::info!("Deleting dir {:?}", old_path.strip_prefix(&root_path)?);
1511 fs::remove_dir_all(&old_path).unwrap();
1512 for file in files {
1513 record_event(file);
1514 }
1515 for dir in dirs {
1516 record_event(dir);
1517 }
1518 } else {
1519 log::info!("Deleting file {:?}", old_path.strip_prefix(&root_path)?);
1520 fs::remove_file(old_path).unwrap();
1521 record_event(old_path.clone());
1522 }
1523 }
1524
1525 Ok(events)
1526 }
1527
1528 fn read_dir_recursive(path: PathBuf) -> (Vec<PathBuf>, Vec<PathBuf>) {
1529 let child_entries = fs::read_dir(&path).unwrap();
1530 let mut dirs = vec![path];
1531 let mut files = Vec::new();
1532 for child_entry in child_entries {
1533 let child_path = child_entry.unwrap().path();
1534 if child_path.is_dir() {
1535 let (child_dirs, child_files) = read_dir_recursive(child_path);
1536 dirs.extend(child_dirs);
1537 files.extend(child_files);
1538 } else {
1539 files.push(child_path);
1540 }
1541 }
1542 (dirs, files)
1543 }
1544
1545 fn gen_name(rng: &mut impl Rng) -> String {
1546 (0..6)
1547 .map(|_| rng.sample(rand::distributions::Alphanumeric))
1548 .map(char::from)
1549 .collect()
1550 }
1551
1552 impl Snapshot {
1553 fn check_invariants(&self) {
1554 let mut files = self.files(0);
1555 let mut visible_files = self.visible_files(0);
1556 for entry in self.entries.cursor::<(), ()>() {
1557 if matches!(entry.kind, EntryKind::File(_)) {
1558 assert_eq!(files.next().unwrap().inode(), entry.inode);
1559 if !entry.is_ignored {
1560 assert_eq!(visible_files.next().unwrap().inode(), entry.inode);
1561 }
1562 }
1563 }
1564 assert!(files.next().is_none());
1565 assert!(visible_files.next().is_none());
1566
1567 let mut bfs_paths = Vec::new();
1568 let mut stack = vec![Path::new("")];
1569 while let Some(path) = stack.pop() {
1570 bfs_paths.push(path);
1571 let ix = stack.len();
1572 for child_entry in self.child_entries(path) {
1573 stack.insert(ix, child_entry.path());
1574 }
1575 }
1576
1577 let dfs_paths = self
1578 .entries
1579 .cursor::<(), ()>()
1580 .map(|e| e.path().as_ref())
1581 .collect::<Vec<_>>();
1582 assert_eq!(bfs_paths, dfs_paths);
1583
1584 for (ignore_parent_path, _) in &self.ignores {
1585 assert!(self.entry_for_path(ignore_parent_path).is_some());
1586 assert!(self
1587 .entry_for_path(ignore_parent_path.join(&*GITIGNORE))
1588 .is_some());
1589 }
1590 }
1591
1592 fn to_vec(&self) -> Vec<(&Path, u64, bool)> {
1593 let mut paths = Vec::new();
1594 for entry in self.entries.cursor::<(), ()>() {
1595 paths.push((entry.path().as_ref(), entry.inode(), entry.is_ignored()));
1596 }
1597 paths.sort_by(|a, b| a.0.cmp(&b.0));
1598 paths
1599 }
1600 }
1601}