1use crate::{ProjectEntryId, RemoveOptions};
2
3use super::{
4 fs::{self, Fs},
5 ignore::IgnoreStack,
6 DiagnosticSummary,
7};
8use ::ignore::gitignore::{Gitignore, GitignoreBuilder};
9use anyhow::{anyhow, Context, Result};
10use client::{proto, Client, TypedEnvelope};
11use clock::ReplicaId;
12use collections::HashMap;
13use futures::{
14 channel::{
15 mpsc::{self, UnboundedSender},
16 oneshot,
17 },
18 Stream, StreamExt,
19};
20use fuzzy::CharBag;
21use gpui::{
22 executor, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext,
23 Task,
24};
25use language::{
26 proto::{deserialize_version, serialize_version},
27 Buffer, DiagnosticEntry, PointUtf16, Rope,
28};
29use lazy_static::lazy_static;
30use parking_lot::Mutex;
31use postage::{
32 prelude::{Sink as _, Stream as _},
33 watch,
34};
35use smol::channel::{self, Sender};
36use std::{
37 any::Any,
38 cmp::{self, Ordering},
39 convert::TryFrom,
40 ffi::{OsStr, OsString},
41 fmt,
42 future::Future,
43 ops::{Deref, DerefMut},
44 os::unix::prelude::{OsStrExt, OsStringExt},
45 path::{Path, PathBuf},
46 sync::{atomic::AtomicUsize, Arc},
47 time::{Duration, SystemTime},
48};
49use sum_tree::{Bias, Edit, SeekTarget, SumTree, TreeMap};
50use util::{ResultExt, TryFutureExt};
51
52lazy_static! {
53 static ref GITIGNORE: &'static OsStr = OsStr::new(".gitignore");
54}
55
56#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)]
57pub struct WorktreeId(usize);
58
59pub enum Worktree {
60 Local(LocalWorktree),
61 Remote(RemoteWorktree),
62}
63
64pub struct LocalWorktree {
65 snapshot: LocalSnapshot,
66 background_snapshot: Arc<Mutex<LocalSnapshot>>,
67 last_scan_state_rx: watch::Receiver<ScanState>,
68 _background_scanner_task: Option<Task<()>>,
69 poll_task: Option<Task<()>>,
70 registration: Registration,
71 share: Option<ShareState>,
72 diagnostics: HashMap<Arc<Path>, Vec<DiagnosticEntry<PointUtf16>>>,
73 diagnostic_summaries: TreeMap<PathKey, DiagnosticSummary>,
74 client: Arc<Client>,
75 fs: Arc<dyn Fs>,
76 visible: bool,
77}
78
79pub struct RemoteWorktree {
80 pub snapshot: Snapshot,
81 pub(crate) background_snapshot: Arc<Mutex<Snapshot>>,
82 project_id: u64,
83 client: Arc<Client>,
84 updates_tx: UnboundedSender<proto::UpdateWorktree>,
85 last_scan_id_rx: watch::Receiver<usize>,
86 replica_id: ReplicaId,
87 diagnostic_summaries: TreeMap<PathKey, DiagnosticSummary>,
88 visible: bool,
89}
90
91#[derive(Clone)]
92pub struct Snapshot {
93 id: WorktreeId,
94 root_name: String,
95 root_char_bag: CharBag,
96 entries_by_path: SumTree<Entry>,
97 entries_by_id: SumTree<PathEntry>,
98 scan_id: usize,
99}
100
101#[derive(Clone)]
102pub struct LocalSnapshot {
103 abs_path: Arc<Path>,
104 ignores: HashMap<Arc<Path>, (Arc<Gitignore>, usize)>,
105 removed_entry_ids: HashMap<u64, ProjectEntryId>,
106 next_entry_id: Arc<AtomicUsize>,
107 snapshot: Snapshot,
108}
109
110impl Deref for LocalSnapshot {
111 type Target = Snapshot;
112
113 fn deref(&self) -> &Self::Target {
114 &self.snapshot
115 }
116}
117
118impl DerefMut for LocalSnapshot {
119 fn deref_mut(&mut self) -> &mut Self::Target {
120 &mut self.snapshot
121 }
122}
123
124#[derive(Clone, Debug)]
125enum ScanState {
126 Idle,
127 Scanning,
128 Err(Arc<anyhow::Error>),
129}
130
131#[derive(Debug, Eq, PartialEq)]
132enum Registration {
133 None,
134 Pending,
135 Done { project_id: u64 },
136}
137
138struct ShareState {
139 project_id: u64,
140 snapshots_tx: Sender<LocalSnapshot>,
141 _maintain_remote_snapshot: Option<Task<Option<()>>>,
142}
143
144pub enum Event {
145 UpdatedEntries,
146}
147
148impl Entity for Worktree {
149 type Event = Event;
150
151 fn release(&mut self, _: &mut MutableAppContext) {
152 if let Some(worktree) = self.as_local_mut() {
153 if let Registration::Done { project_id } = worktree.registration {
154 let client = worktree.client.clone();
155 let unregister_message = proto::UnregisterWorktree {
156 project_id,
157 worktree_id: worktree.id().to_proto(),
158 };
159 client.send(unregister_message).log_err();
160 }
161 }
162 }
163}
164
165impl Worktree {
166 pub async fn local(
167 client: Arc<Client>,
168 path: impl Into<Arc<Path>>,
169 visible: bool,
170 fs: Arc<dyn Fs>,
171 next_entry_id: Arc<AtomicUsize>,
172 cx: &mut AsyncAppContext,
173 ) -> Result<ModelHandle<Self>> {
174 let (tree, scan_states_tx) =
175 LocalWorktree::new(client, path, visible, fs.clone(), next_entry_id, cx).await?;
176 tree.update(cx, |tree, cx| {
177 let tree = tree.as_local_mut().unwrap();
178 let abs_path = tree.abs_path().clone();
179 let background_snapshot = tree.background_snapshot.clone();
180 let background = cx.background().clone();
181 tree._background_scanner_task = Some(cx.background().spawn(async move {
182 let events = fs.watch(&abs_path, Duration::from_millis(100)).await;
183 let scanner =
184 BackgroundScanner::new(background_snapshot, scan_states_tx, fs, background);
185 scanner.run(events).await;
186 }));
187 });
188 Ok(tree)
189 }
190
191 pub fn remote(
192 project_remote_id: u64,
193 replica_id: ReplicaId,
194 worktree: proto::Worktree,
195 client: Arc<Client>,
196 cx: &mut MutableAppContext,
197 ) -> (ModelHandle<Self>, Task<()>) {
198 let remote_id = worktree.id;
199 let root_char_bag: CharBag = worktree
200 .root_name
201 .chars()
202 .map(|c| c.to_ascii_lowercase())
203 .collect();
204 let root_name = worktree.root_name.clone();
205 let visible = worktree.visible;
206 let snapshot = Snapshot {
207 id: WorktreeId(remote_id as usize),
208 root_name,
209 root_char_bag,
210 entries_by_path: Default::default(),
211 entries_by_id: Default::default(),
212 scan_id: worktree.scan_id as usize,
213 };
214
215 let (updates_tx, mut updates_rx) = mpsc::unbounded();
216 let background_snapshot = Arc::new(Mutex::new(snapshot.clone()));
217 let (mut snapshot_updated_tx, mut snapshot_updated_rx) = watch::channel();
218 let (mut last_scan_id_tx, last_scan_id_rx) = watch::channel_with(worktree.scan_id as usize);
219 let worktree_handle = cx.add_model(|_: &mut ModelContext<Worktree>| {
220 Worktree::Remote(RemoteWorktree {
221 project_id: project_remote_id,
222 replica_id,
223 snapshot: snapshot.clone(),
224 background_snapshot: background_snapshot.clone(),
225 updates_tx,
226 last_scan_id_rx,
227 client: client.clone(),
228 diagnostic_summaries: TreeMap::from_ordered_entries(
229 worktree.diagnostic_summaries.into_iter().map(|summary| {
230 (
231 PathKey(PathBuf::from(summary.path).into()),
232 DiagnosticSummary {
233 error_count: summary.error_count as usize,
234 warning_count: summary.warning_count as usize,
235 },
236 )
237 }),
238 ),
239 visible,
240 })
241 });
242
243 let deserialize_task = cx.spawn({
244 let worktree_handle = worktree_handle.clone();
245 |cx| async move {
246 let (entries_by_path, entries_by_id) = cx
247 .background()
248 .spawn(async move {
249 let mut entries_by_path_edits = Vec::new();
250 let mut entries_by_id_edits = Vec::new();
251 for entry in worktree.entries {
252 match Entry::try_from((&root_char_bag, entry)) {
253 Ok(entry) => {
254 entries_by_id_edits.push(Edit::Insert(PathEntry {
255 id: entry.id,
256 path: entry.path.clone(),
257 is_ignored: entry.is_ignored,
258 scan_id: 0,
259 }));
260 entries_by_path_edits.push(Edit::Insert(entry));
261 }
262 Err(err) => log::warn!("error for remote worktree entry {:?}", err),
263 }
264 }
265
266 let mut entries_by_path = SumTree::new();
267 let mut entries_by_id = SumTree::new();
268 entries_by_path.edit(entries_by_path_edits, &());
269 entries_by_id.edit(entries_by_id_edits, &());
270
271 (entries_by_path, entries_by_id)
272 })
273 .await;
274
275 {
276 let mut snapshot = background_snapshot.lock();
277 snapshot.entries_by_path = entries_by_path;
278 snapshot.entries_by_id = entries_by_id;
279 snapshot_updated_tx.send(()).await.ok();
280 }
281
282 cx.background()
283 .spawn(async move {
284 while let Some(update) = updates_rx.next().await {
285 if let Err(error) =
286 background_snapshot.lock().apply_remote_update(update)
287 {
288 log::error!("error applying worktree update: {}", error);
289 }
290 snapshot_updated_tx.send(()).await.ok();
291 }
292 })
293 .detach();
294
295 cx.spawn(|mut cx| {
296 let this = worktree_handle.downgrade();
297 async move {
298 while let Some(_) = snapshot_updated_rx.recv().await {
299 if let Some(this) = this.upgrade(&cx) {
300 this.update(&mut cx, |this, cx| {
301 this.poll_snapshot(cx);
302 let this = this.as_remote_mut().unwrap();
303 *last_scan_id_tx.borrow_mut() = this.snapshot.scan_id;
304 });
305 } else {
306 break;
307 }
308 }
309 }
310 })
311 .detach();
312 }
313 });
314 (worktree_handle, deserialize_task)
315 }
316
317 pub fn as_local(&self) -> Option<&LocalWorktree> {
318 if let Worktree::Local(worktree) = self {
319 Some(worktree)
320 } else {
321 None
322 }
323 }
324
325 pub fn as_remote(&self) -> Option<&RemoteWorktree> {
326 if let Worktree::Remote(worktree) = self {
327 Some(worktree)
328 } else {
329 None
330 }
331 }
332
333 pub fn as_local_mut(&mut self) -> Option<&mut LocalWorktree> {
334 if let Worktree::Local(worktree) = self {
335 Some(worktree)
336 } else {
337 None
338 }
339 }
340
341 pub fn as_remote_mut(&mut self) -> Option<&mut RemoteWorktree> {
342 if let Worktree::Remote(worktree) = self {
343 Some(worktree)
344 } else {
345 None
346 }
347 }
348
349 pub fn is_local(&self) -> bool {
350 matches!(self, Worktree::Local(_))
351 }
352
353 pub fn is_remote(&self) -> bool {
354 !self.is_local()
355 }
356
357 pub fn snapshot(&self) -> Snapshot {
358 match self {
359 Worktree::Local(worktree) => worktree.snapshot().snapshot,
360 Worktree::Remote(worktree) => worktree.snapshot(),
361 }
362 }
363
364 pub fn scan_id(&self) -> usize {
365 match self {
366 Worktree::Local(worktree) => worktree.snapshot.scan_id,
367 Worktree::Remote(worktree) => worktree.snapshot.scan_id,
368 }
369 }
370
371 pub fn is_visible(&self) -> bool {
372 match self {
373 Worktree::Local(worktree) => worktree.visible,
374 Worktree::Remote(worktree) => worktree.visible,
375 }
376 }
377
378 pub fn replica_id(&self) -> ReplicaId {
379 match self {
380 Worktree::Local(_) => 0,
381 Worktree::Remote(worktree) => worktree.replica_id,
382 }
383 }
384
385 pub fn diagnostic_summaries<'a>(
386 &'a self,
387 ) -> impl Iterator<Item = (Arc<Path>, DiagnosticSummary)> + 'a {
388 match self {
389 Worktree::Local(worktree) => &worktree.diagnostic_summaries,
390 Worktree::Remote(worktree) => &worktree.diagnostic_summaries,
391 }
392 .iter()
393 .map(|(path, summary)| (path.0.clone(), summary.clone()))
394 }
395
396 fn poll_snapshot(&mut self, cx: &mut ModelContext<Self>) {
397 match self {
398 Self::Local(worktree) => {
399 let is_fake_fs = worktree.fs.is_fake();
400 worktree.snapshot = worktree.background_snapshot.lock().clone();
401 if worktree.is_scanning() {
402 if worktree.poll_task.is_none() {
403 worktree.poll_task = Some(cx.spawn_weak(|this, mut cx| async move {
404 if is_fake_fs {
405 #[cfg(any(test, feature = "test-support"))]
406 cx.background().simulate_random_delay().await;
407 } else {
408 smol::Timer::after(Duration::from_millis(100)).await;
409 }
410 if let Some(this) = this.upgrade(&cx) {
411 this.update(&mut cx, |this, cx| {
412 this.as_local_mut().unwrap().poll_task = None;
413 this.poll_snapshot(cx);
414 });
415 }
416 }));
417 }
418 } else {
419 worktree.poll_task.take();
420 cx.emit(Event::UpdatedEntries);
421 }
422 }
423 Self::Remote(worktree) => {
424 worktree.snapshot = worktree.background_snapshot.lock().clone();
425 cx.emit(Event::UpdatedEntries);
426 }
427 };
428
429 cx.notify();
430 }
431}
432
433impl LocalWorktree {
434 async fn new(
435 client: Arc<Client>,
436 path: impl Into<Arc<Path>>,
437 visible: bool,
438 fs: Arc<dyn Fs>,
439 next_entry_id: Arc<AtomicUsize>,
440 cx: &mut AsyncAppContext,
441 ) -> Result<(ModelHandle<Worktree>, UnboundedSender<ScanState>)> {
442 let abs_path = path.into();
443 let path: Arc<Path> = Arc::from(Path::new(""));
444
445 // After determining whether the root entry is a file or a directory, populate the
446 // snapshot's "root name", which will be used for the purpose of fuzzy matching.
447 let root_name = abs_path
448 .file_name()
449 .map_or(String::new(), |f| f.to_string_lossy().to_string());
450 let root_char_bag = root_name.chars().map(|c| c.to_ascii_lowercase()).collect();
451 let metadata = fs
452 .metadata(&abs_path)
453 .await
454 .context("failed to stat worktree path")?;
455
456 let (scan_states_tx, mut scan_states_rx) = mpsc::unbounded();
457 let (mut last_scan_state_tx, last_scan_state_rx) = watch::channel_with(ScanState::Scanning);
458 let tree = cx.add_model(move |cx: &mut ModelContext<Worktree>| {
459 let mut snapshot = LocalSnapshot {
460 abs_path,
461 ignores: Default::default(),
462 removed_entry_ids: Default::default(),
463 next_entry_id,
464 snapshot: Snapshot {
465 id: WorktreeId::from_usize(cx.model_id()),
466 root_name: root_name.clone(),
467 root_char_bag,
468 entries_by_path: Default::default(),
469 entries_by_id: Default::default(),
470 scan_id: 0,
471 },
472 };
473 if let Some(metadata) = metadata {
474 let entry = Entry::new(
475 path.into(),
476 &metadata,
477 &snapshot.next_entry_id,
478 snapshot.root_char_bag,
479 );
480 snapshot.insert_entry(entry, fs.as_ref());
481 }
482
483 let tree = Self {
484 snapshot: snapshot.clone(),
485 background_snapshot: Arc::new(Mutex::new(snapshot)),
486 last_scan_state_rx,
487 _background_scanner_task: None,
488 registration: Registration::None,
489 share: None,
490 poll_task: None,
491 diagnostics: Default::default(),
492 diagnostic_summaries: Default::default(),
493 client,
494 fs,
495 visible,
496 };
497
498 cx.spawn_weak(|this, mut cx| async move {
499 while let Some(scan_state) = scan_states_rx.next().await {
500 if let Some(this) = this.upgrade(&cx) {
501 last_scan_state_tx.blocking_send(scan_state).ok();
502 this.update(&mut cx, |this, cx| {
503 this.poll_snapshot(cx);
504 this.as_local().unwrap().broadcast_snapshot()
505 })
506 .await;
507 } else {
508 break;
509 }
510 }
511 })
512 .detach();
513
514 Worktree::Local(tree)
515 });
516
517 Ok((tree, scan_states_tx))
518 }
519
520 pub fn contains_abs_path(&self, path: &Path) -> bool {
521 path.starts_with(&self.abs_path)
522 }
523
524 fn absolutize(&self, path: &Path) -> PathBuf {
525 if path.file_name().is_some() {
526 self.abs_path.join(path)
527 } else {
528 self.abs_path.to_path_buf()
529 }
530 }
531
532 pub(crate) fn load_buffer(
533 &mut self,
534 path: &Path,
535 cx: &mut ModelContext<Worktree>,
536 ) -> Task<Result<ModelHandle<Buffer>>> {
537 let path = Arc::from(path);
538 cx.spawn(move |this, mut cx| async move {
539 let (file, contents) = this
540 .update(&mut cx, |t, cx| t.as_local().unwrap().load(&path, cx))
541 .await?;
542 Ok(cx.add_model(|cx| Buffer::from_file(0, contents, Box::new(file), cx)))
543 })
544 }
545
546 pub fn diagnostics_for_path(&self, path: &Path) -> Option<Vec<DiagnosticEntry<PointUtf16>>> {
547 self.diagnostics.get(path).cloned()
548 }
549
550 pub fn update_diagnostics(
551 &mut self,
552 worktree_path: Arc<Path>,
553 diagnostics: Vec<DiagnosticEntry<PointUtf16>>,
554 _: &mut ModelContext<Worktree>,
555 ) -> Result<bool> {
556 self.diagnostics.remove(&worktree_path);
557 let old_summary = self
558 .diagnostic_summaries
559 .remove(&PathKey(worktree_path.clone()))
560 .unwrap_or_default();
561 let new_summary = DiagnosticSummary::new(&diagnostics);
562 if !new_summary.is_empty() {
563 self.diagnostic_summaries
564 .insert(PathKey(worktree_path.clone()), new_summary);
565 self.diagnostics.insert(worktree_path.clone(), diagnostics);
566 }
567
568 let updated = !old_summary.is_empty() || !new_summary.is_empty();
569 if updated {
570 if let Some(share) = self.share.as_ref() {
571 self.client
572 .send(proto::UpdateDiagnosticSummary {
573 project_id: share.project_id,
574 worktree_id: self.id().to_proto(),
575 summary: Some(proto::DiagnosticSummary {
576 path: worktree_path.to_string_lossy().to_string(),
577 error_count: new_summary.error_count as u32,
578 warning_count: new_summary.warning_count as u32,
579 }),
580 })
581 .log_err();
582 }
583 }
584
585 Ok(updated)
586 }
587
588 pub fn scan_complete(&self) -> impl Future<Output = ()> {
589 let mut scan_state_rx = self.last_scan_state_rx.clone();
590 async move {
591 let mut scan_state = Some(scan_state_rx.borrow().clone());
592 while let Some(ScanState::Scanning) = scan_state {
593 scan_state = scan_state_rx.recv().await;
594 }
595 }
596 }
597
598 fn is_scanning(&self) -> bool {
599 if let ScanState::Scanning = *self.last_scan_state_rx.borrow() {
600 true
601 } else {
602 false
603 }
604 }
605
606 pub fn snapshot(&self) -> LocalSnapshot {
607 self.snapshot.clone()
608 }
609
610 fn load(&self, path: &Path, cx: &mut ModelContext<Worktree>) -> Task<Result<(File, String)>> {
611 let handle = cx.handle();
612 let path = Arc::from(path);
613 let abs_path = self.absolutize(&path);
614 let fs = self.fs.clone();
615 cx.spawn(|this, mut cx| async move {
616 let text = fs.load(&abs_path).await?;
617 // Eagerly populate the snapshot with an updated entry for the loaded file
618 let entry = this
619 .update(&mut cx, |this, cx| {
620 this.as_local()
621 .unwrap()
622 .refresh_entry(path, abs_path, None, cx)
623 })
624 .await?;
625 this.update(&mut cx, |this, cx| this.poll_snapshot(cx));
626 Ok((
627 File {
628 entry_id: Some(entry.id),
629 worktree: handle,
630 path: entry.path,
631 mtime: entry.mtime,
632 is_local: true,
633 },
634 text,
635 ))
636 })
637 }
638
639 pub fn save_buffer_as(
640 &self,
641 buffer_handle: ModelHandle<Buffer>,
642 path: impl Into<Arc<Path>>,
643 cx: &mut ModelContext<Worktree>,
644 ) -> Task<Result<()>> {
645 let buffer = buffer_handle.read(cx);
646 let text = buffer.as_rope().clone();
647 let version = buffer.version();
648 let save = self.write_file(path, text, cx);
649 let handle = cx.handle();
650 cx.as_mut().spawn(|mut cx| async move {
651 let entry = save.await?;
652 let file = File {
653 entry_id: Some(entry.id),
654 worktree: handle,
655 path: entry.path,
656 mtime: entry.mtime,
657 is_local: true,
658 };
659
660 buffer_handle.update(&mut cx, |buffer, cx| {
661 buffer.did_save(version, file.mtime, Some(Box::new(file)), cx);
662 });
663
664 Ok(())
665 })
666 }
667
668 pub fn create_entry(
669 &self,
670 path: impl Into<Arc<Path>>,
671 is_dir: bool,
672 cx: &mut ModelContext<Worktree>,
673 ) -> Task<Result<Entry>> {
674 self.write_entry_internal(
675 path,
676 if is_dir {
677 None
678 } else {
679 Some(Default::default())
680 },
681 cx,
682 )
683 }
684
685 pub fn write_file(
686 &self,
687 path: impl Into<Arc<Path>>,
688 text: Rope,
689 cx: &mut ModelContext<Worktree>,
690 ) -> Task<Result<Entry>> {
691 self.write_entry_internal(path, Some(text), cx)
692 }
693
694 pub fn delete_entry(
695 &self,
696 entry_id: ProjectEntryId,
697 cx: &mut ModelContext<Worktree>,
698 ) -> Option<Task<Result<()>>> {
699 let entry = self.entry_for_id(entry_id)?.clone();
700 let abs_path = self.absolutize(&entry.path);
701 let delete = cx.background().spawn({
702 let fs = self.fs.clone();
703 let abs_path = abs_path.clone();
704 async move {
705 if entry.is_file() {
706 fs.remove_file(&abs_path, Default::default()).await
707 } else {
708 fs.remove_dir(
709 &abs_path,
710 RemoveOptions {
711 recursive: true,
712 ignore_if_not_exists: false,
713 },
714 )
715 .await
716 }
717 }
718 });
719
720 Some(cx.spawn(|this, mut cx| async move {
721 delete.await?;
722 this.update(&mut cx, |this, _| {
723 let this = this.as_local_mut().unwrap();
724 let mut snapshot = this.background_snapshot.lock();
725 snapshot.delete_entry(entry_id);
726 });
727 this.update(&mut cx, |this, cx| {
728 this.poll_snapshot(cx);
729 this.as_local().unwrap().broadcast_snapshot()
730 })
731 .await;
732 Ok(())
733 }))
734 }
735
736 pub fn rename_entry(
737 &self,
738 entry_id: ProjectEntryId,
739 new_path: impl Into<Arc<Path>>,
740 cx: &mut ModelContext<Worktree>,
741 ) -> Option<Task<Result<Entry>>> {
742 let old_path = self.entry_for_id(entry_id)?.path.clone();
743 let new_path = new_path.into();
744 let abs_old_path = self.absolutize(&old_path);
745 let abs_new_path = self.absolutize(&new_path);
746 let rename = cx.background().spawn({
747 let fs = self.fs.clone();
748 let abs_new_path = abs_new_path.clone();
749 async move {
750 fs.rename(&abs_old_path, &abs_new_path, Default::default())
751 .await
752 }
753 });
754
755 Some(cx.spawn(|this, mut cx| async move {
756 rename.await?;
757 let entry = this
758 .update(&mut cx, |this, cx| {
759 this.as_local_mut().unwrap().refresh_entry(
760 new_path.clone(),
761 abs_new_path,
762 Some(old_path),
763 cx,
764 )
765 })
766 .await?;
767 this.update(&mut cx, |this, cx| {
768 this.poll_snapshot(cx);
769 this.as_local().unwrap().broadcast_snapshot()
770 })
771 .await;
772 Ok(entry)
773 }))
774 }
775
776 fn write_entry_internal(
777 &self,
778 path: impl Into<Arc<Path>>,
779 text_if_file: Option<Rope>,
780 cx: &mut ModelContext<Worktree>,
781 ) -> Task<Result<Entry>> {
782 let path = path.into();
783 let abs_path = self.absolutize(&path);
784 let write = cx.background().spawn({
785 let fs = self.fs.clone();
786 let abs_path = abs_path.clone();
787 async move {
788 if let Some(text) = text_if_file {
789 fs.save(&abs_path, &text).await
790 } else {
791 fs.create_dir(&abs_path).await
792 }
793 }
794 });
795
796 cx.spawn(|this, mut cx| async move {
797 write.await?;
798 let entry = this
799 .update(&mut cx, |this, cx| {
800 this.as_local_mut()
801 .unwrap()
802 .refresh_entry(path, abs_path, None, cx)
803 })
804 .await?;
805 this.update(&mut cx, |this, cx| {
806 this.poll_snapshot(cx);
807 this.as_local().unwrap().broadcast_snapshot()
808 })
809 .await;
810 Ok(entry)
811 })
812 }
813
814 fn refresh_entry(
815 &self,
816 path: Arc<Path>,
817 abs_path: PathBuf,
818 old_path: Option<Arc<Path>>,
819 cx: &mut ModelContext<Worktree>,
820 ) -> Task<Result<Entry>> {
821 let fs = self.fs.clone();
822 let root_char_bag;
823 let next_entry_id;
824 {
825 let snapshot = self.background_snapshot.lock();
826 root_char_bag = snapshot.root_char_bag;
827 next_entry_id = snapshot.next_entry_id.clone();
828 }
829 cx.spawn_weak(|this, mut cx| async move {
830 let entry = Entry::new(
831 path,
832 &fs.metadata(&abs_path)
833 .await?
834 .ok_or_else(|| anyhow!("could not read saved file metadata"))?,
835 &next_entry_id,
836 root_char_bag,
837 );
838
839 let this = this
840 .upgrade(&cx)
841 .ok_or_else(|| anyhow!("worktree was dropped"))?;
842 let (entry, snapshot, snapshots_tx) = this.read_with(&cx, |this, _| {
843 let this = this.as_local().unwrap();
844 let mut snapshot = this.background_snapshot.lock();
845 if let Some(old_path) = old_path {
846 snapshot.remove_path(&old_path);
847 }
848 let entry = snapshot.insert_entry(entry, fs.as_ref());
849 snapshot.scan_id += 1;
850 let snapshots_tx = this.share.as_ref().map(|s| s.snapshots_tx.clone());
851 (entry, snapshot.clone(), snapshots_tx)
852 });
853 this.update(&mut cx, |this, cx| this.poll_snapshot(cx));
854
855 if let Some(snapshots_tx) = snapshots_tx {
856 snapshots_tx.send(snapshot).await.ok();
857 }
858
859 Ok(entry)
860 })
861 }
862
863 pub fn register(
864 &mut self,
865 project_id: u64,
866 cx: &mut ModelContext<Worktree>,
867 ) -> Task<anyhow::Result<()>> {
868 if self.registration != Registration::None {
869 return Task::ready(Ok(()));
870 }
871
872 self.registration = Registration::Pending;
873 let client = self.client.clone();
874 let register_message = proto::RegisterWorktree {
875 project_id,
876 worktree_id: self.id().to_proto(),
877 root_name: self.root_name().to_string(),
878 visible: self.visible,
879 };
880 let request = client.request(register_message);
881 cx.spawn(|this, mut cx| async move {
882 let response = request.await;
883 this.update(&mut cx, |this, _| {
884 let worktree = this.as_local_mut().unwrap();
885 match response {
886 Ok(_) => {
887 if worktree.registration == Registration::Pending {
888 worktree.registration = Registration::Done { project_id };
889 }
890 Ok(())
891 }
892 Err(error) => {
893 worktree.registration = Registration::None;
894 Err(error)
895 }
896 }
897 })
898 })
899 }
900
901 pub fn share(&mut self, project_id: u64, cx: &mut ModelContext<Worktree>) -> Task<Result<()>> {
902 let register = self.register(project_id, cx);
903 let (share_tx, share_rx) = oneshot::channel();
904 let (snapshots_to_send_tx, snapshots_to_send_rx) =
905 smol::channel::unbounded::<LocalSnapshot>();
906 if self.share.is_some() {
907 let _ = share_tx.send(Ok(()));
908 } else {
909 let rpc = self.client.clone();
910 let worktree_id = cx.model_id() as u64;
911 let maintain_remote_snapshot = cx.background().spawn({
912 let rpc = rpc.clone();
913 let diagnostic_summaries = self.diagnostic_summaries.clone();
914 async move {
915 let mut prev_snapshot = match snapshots_to_send_rx.recv().await {
916 Ok(snapshot) => {
917 if let Err(error) = rpc
918 .request(proto::UpdateWorktree {
919 project_id,
920 worktree_id,
921 root_name: snapshot.root_name().to_string(),
922 updated_entries: snapshot
923 .entries_by_path
924 .iter()
925 .filter(|e| !e.is_ignored)
926 .map(Into::into)
927 .collect(),
928 removed_entries: Default::default(),
929 scan_id: snapshot.scan_id as u64,
930 })
931 .await
932 {
933 let _ = share_tx.send(Err(error));
934 return Err(anyhow!("failed to send initial update worktree"));
935 } else {
936 let _ = share_tx.send(Ok(()));
937 snapshot
938 }
939 }
940 Err(error) => {
941 let _ = share_tx.send(Err(error.into()));
942 return Err(anyhow!("failed to send initial update worktree"));
943 }
944 };
945
946 for (path, summary) in diagnostic_summaries.iter() {
947 rpc.send(proto::UpdateDiagnosticSummary {
948 project_id,
949 worktree_id,
950 summary: Some(summary.to_proto(&path.0)),
951 })?;
952 }
953
954 while let Ok(snapshot) = snapshots_to_send_rx.recv().await {
955 let message =
956 snapshot.build_update(&prev_snapshot, project_id, worktree_id, false);
957 rpc.request(message).await?;
958 prev_snapshot = snapshot;
959 }
960
961 Ok::<_, anyhow::Error>(())
962 }
963 .log_err()
964 });
965 self.share = Some(ShareState {
966 project_id,
967 snapshots_tx: snapshots_to_send_tx.clone(),
968 _maintain_remote_snapshot: Some(maintain_remote_snapshot),
969 });
970 }
971
972 cx.spawn_weak(|this, cx| async move {
973 register.await?;
974 if let Some(this) = this.upgrade(&cx) {
975 this.read_with(&cx, |this, _| {
976 let this = this.as_local().unwrap();
977 let _ = snapshots_to_send_tx.try_send(this.snapshot());
978 });
979 }
980 share_rx
981 .await
982 .unwrap_or_else(|_| Err(anyhow!("share ended")))
983 })
984 }
985
986 pub fn unregister(&mut self) {
987 self.unshare();
988 self.registration = Registration::None;
989 }
990
991 pub fn unshare(&mut self) {
992 self.share.take();
993 }
994
995 pub fn is_shared(&self) -> bool {
996 self.share.is_some()
997 }
998
999 fn broadcast_snapshot(&self) -> impl Future<Output = ()> {
1000 let mut to_send = None;
1001 if !self.is_scanning() {
1002 if let Some(share) = self.share.as_ref() {
1003 to_send = Some((self.snapshot(), share.snapshots_tx.clone()));
1004 }
1005 }
1006
1007 async move {
1008 if let Some((snapshot, snapshots_to_send_tx)) = to_send {
1009 if let Err(err) = snapshots_to_send_tx.send(snapshot).await {
1010 log::error!("error submitting snapshot to send {}", err);
1011 }
1012 }
1013 }
1014 }
1015}
1016
1017impl RemoteWorktree {
1018 fn snapshot(&self) -> Snapshot {
1019 self.snapshot.clone()
1020 }
1021
1022 pub fn update_from_remote(
1023 &mut self,
1024 envelope: TypedEnvelope<proto::UpdateWorktree>,
1025 ) -> Result<()> {
1026 self.updates_tx
1027 .unbounded_send(envelope.payload)
1028 .expect("consumer runs to completion");
1029 Ok(())
1030 }
1031
1032 fn wait_for_snapshot(&self, scan_id: usize) -> impl Future<Output = ()> {
1033 let mut rx = self.last_scan_id_rx.clone();
1034 async move {
1035 while let Some(applied_scan_id) = rx.next().await {
1036 if applied_scan_id >= scan_id {
1037 return;
1038 }
1039 }
1040 }
1041 }
1042
1043 pub fn update_diagnostic_summary(
1044 &mut self,
1045 path: Arc<Path>,
1046 summary: &proto::DiagnosticSummary,
1047 ) {
1048 let summary = DiagnosticSummary {
1049 error_count: summary.error_count as usize,
1050 warning_count: summary.warning_count as usize,
1051 };
1052 if summary.is_empty() {
1053 self.diagnostic_summaries.remove(&PathKey(path.clone()));
1054 } else {
1055 self.diagnostic_summaries
1056 .insert(PathKey(path.clone()), summary);
1057 }
1058 }
1059
1060 pub fn insert_entry(
1061 &self,
1062 entry: proto::Entry,
1063 scan_id: usize,
1064 cx: &mut ModelContext<Worktree>,
1065 ) -> Task<Result<Entry>> {
1066 let wait_for_snapshot = self.wait_for_snapshot(scan_id);
1067 cx.spawn(|this, mut cx| async move {
1068 wait_for_snapshot.await;
1069 this.update(&mut cx, |worktree, _| {
1070 let worktree = worktree.as_remote_mut().unwrap();
1071 let mut snapshot = worktree.background_snapshot.lock();
1072 let entry = snapshot.insert_entry(entry);
1073 worktree.snapshot = snapshot.clone();
1074 entry
1075 })
1076 })
1077 }
1078
1079 pub(crate) fn delete_entry(
1080 &self,
1081 id: ProjectEntryId,
1082 scan_id: usize,
1083 cx: &mut ModelContext<Worktree>,
1084 ) -> Task<Result<()>> {
1085 let wait_for_snapshot = self.wait_for_snapshot(scan_id);
1086 cx.spawn(|this, mut cx| async move {
1087 wait_for_snapshot.await;
1088 this.update(&mut cx, |worktree, _| {
1089 let worktree = worktree.as_remote_mut().unwrap();
1090 let mut snapshot = worktree.background_snapshot.lock();
1091 snapshot.delete_entry(id);
1092 worktree.snapshot = snapshot.clone();
1093 });
1094 Ok(())
1095 })
1096 }
1097}
1098
1099impl Snapshot {
1100 pub fn id(&self) -> WorktreeId {
1101 self.id
1102 }
1103
1104 pub fn contains_entry(&self, entry_id: ProjectEntryId) -> bool {
1105 self.entries_by_id.get(&entry_id, &()).is_some()
1106 }
1107
1108 pub(crate) fn insert_entry(&mut self, entry: proto::Entry) -> Result<Entry> {
1109 let entry = Entry::try_from((&self.root_char_bag, entry))?;
1110 let old_entry = self.entries_by_id.insert_or_replace(
1111 PathEntry {
1112 id: entry.id,
1113 path: entry.path.clone(),
1114 is_ignored: entry.is_ignored,
1115 scan_id: 0,
1116 },
1117 &(),
1118 );
1119 if let Some(old_entry) = old_entry {
1120 self.entries_by_path.remove(&PathKey(old_entry.path), &());
1121 }
1122 self.entries_by_path.insert_or_replace(entry.clone(), &());
1123 Ok(entry)
1124 }
1125
1126 fn delete_entry(&mut self, entry_id: ProjectEntryId) -> bool {
1127 if let Some(entry) = self.entries_by_id.remove(&entry_id, &()) {
1128 self.entries_by_path.remove(&PathKey(entry.path), &());
1129 true
1130 } else {
1131 false
1132 }
1133 }
1134
1135 pub(crate) fn apply_remote_update(&mut self, update: proto::UpdateWorktree) -> Result<()> {
1136 let mut entries_by_path_edits = Vec::new();
1137 let mut entries_by_id_edits = Vec::new();
1138 for entry_id in update.removed_entries {
1139 let entry = self
1140 .entry_for_id(ProjectEntryId::from_proto(entry_id))
1141 .ok_or_else(|| anyhow!("unknown entry"))?;
1142 entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone())));
1143 entries_by_id_edits.push(Edit::Remove(entry.id));
1144 }
1145
1146 for entry in update.updated_entries {
1147 let entry = Entry::try_from((&self.root_char_bag, entry))?;
1148 if let Some(PathEntry { path, .. }) = self.entries_by_id.get(&entry.id, &()) {
1149 entries_by_path_edits.push(Edit::Remove(PathKey(path.clone())));
1150 }
1151 entries_by_id_edits.push(Edit::Insert(PathEntry {
1152 id: entry.id,
1153 path: entry.path.clone(),
1154 is_ignored: entry.is_ignored,
1155 scan_id: 0,
1156 }));
1157 entries_by_path_edits.push(Edit::Insert(entry));
1158 }
1159
1160 self.entries_by_path.edit(entries_by_path_edits, &());
1161 self.entries_by_id.edit(entries_by_id_edits, &());
1162 self.scan_id = update.scan_id as usize;
1163
1164 Ok(())
1165 }
1166
1167 pub fn file_count(&self) -> usize {
1168 self.entries_by_path.summary().file_count
1169 }
1170
1171 pub fn visible_file_count(&self) -> usize {
1172 self.entries_by_path.summary().visible_file_count
1173 }
1174
1175 fn traverse_from_offset(
1176 &self,
1177 include_dirs: bool,
1178 include_ignored: bool,
1179 start_offset: usize,
1180 ) -> Traversal {
1181 let mut cursor = self.entries_by_path.cursor();
1182 cursor.seek(
1183 &TraversalTarget::Count {
1184 count: start_offset,
1185 include_dirs,
1186 include_ignored,
1187 },
1188 Bias::Right,
1189 &(),
1190 );
1191 Traversal {
1192 cursor,
1193 include_dirs,
1194 include_ignored,
1195 }
1196 }
1197
1198 fn traverse_from_path(
1199 &self,
1200 include_dirs: bool,
1201 include_ignored: bool,
1202 path: &Path,
1203 ) -> Traversal {
1204 let mut cursor = self.entries_by_path.cursor();
1205 cursor.seek(&TraversalTarget::Path(path), Bias::Left, &());
1206 Traversal {
1207 cursor,
1208 include_dirs,
1209 include_ignored,
1210 }
1211 }
1212
1213 pub fn files(&self, include_ignored: bool, start: usize) -> Traversal {
1214 self.traverse_from_offset(false, include_ignored, start)
1215 }
1216
1217 pub fn entries(&self, include_ignored: bool) -> Traversal {
1218 self.traverse_from_offset(true, include_ignored, 0)
1219 }
1220
1221 pub fn paths(&self) -> impl Iterator<Item = &Arc<Path>> {
1222 let empty_path = Path::new("");
1223 self.entries_by_path
1224 .cursor::<()>()
1225 .filter(move |entry| entry.path.as_ref() != empty_path)
1226 .map(|entry| &entry.path)
1227 }
1228
1229 fn child_entries<'a>(&'a self, parent_path: &'a Path) -> ChildEntriesIter<'a> {
1230 let mut cursor = self.entries_by_path.cursor();
1231 cursor.seek(&TraversalTarget::Path(parent_path), Bias::Right, &());
1232 let traversal = Traversal {
1233 cursor,
1234 include_dirs: true,
1235 include_ignored: true,
1236 };
1237 ChildEntriesIter {
1238 traversal,
1239 parent_path,
1240 }
1241 }
1242
1243 pub fn root_entry(&self) -> Option<&Entry> {
1244 self.entry_for_path("")
1245 }
1246
1247 pub fn root_name(&self) -> &str {
1248 &self.root_name
1249 }
1250
1251 pub fn scan_id(&self) -> usize {
1252 self.scan_id
1253 }
1254
1255 pub fn entry_for_path(&self, path: impl AsRef<Path>) -> Option<&Entry> {
1256 let path = path.as_ref();
1257 self.traverse_from_path(true, true, path)
1258 .entry()
1259 .and_then(|entry| {
1260 if entry.path.as_ref() == path {
1261 Some(entry)
1262 } else {
1263 None
1264 }
1265 })
1266 }
1267
1268 pub fn entry_for_id(&self, id: ProjectEntryId) -> Option<&Entry> {
1269 let entry = self.entries_by_id.get(&id, &())?;
1270 self.entry_for_path(&entry.path)
1271 }
1272
1273 pub fn inode_for_path(&self, path: impl AsRef<Path>) -> Option<u64> {
1274 self.entry_for_path(path.as_ref()).map(|e| e.inode)
1275 }
1276}
1277
1278impl LocalSnapshot {
1279 pub fn abs_path(&self) -> &Arc<Path> {
1280 &self.abs_path
1281 }
1282
1283 #[cfg(test)]
1284 pub(crate) fn to_proto(
1285 &self,
1286 diagnostic_summaries: &TreeMap<PathKey, DiagnosticSummary>,
1287 visible: bool,
1288 ) -> proto::Worktree {
1289 let root_name = self.root_name.clone();
1290 proto::Worktree {
1291 id: self.id.0 as u64,
1292 root_name,
1293 entries: self
1294 .entries_by_path
1295 .iter()
1296 .filter(|e| !e.is_ignored)
1297 .map(Into::into)
1298 .collect(),
1299 diagnostic_summaries: diagnostic_summaries
1300 .iter()
1301 .map(|(path, summary)| summary.to_proto(&path.0))
1302 .collect(),
1303 visible,
1304 scan_id: self.scan_id as u64,
1305 }
1306 }
1307
1308 pub(crate) fn build_update(
1309 &self,
1310 other: &Self,
1311 project_id: u64,
1312 worktree_id: u64,
1313 include_ignored: bool,
1314 ) -> proto::UpdateWorktree {
1315 let mut updated_entries = Vec::new();
1316 let mut removed_entries = Vec::new();
1317 let mut self_entries = self
1318 .entries_by_id
1319 .cursor::<()>()
1320 .filter(|e| include_ignored || !e.is_ignored)
1321 .peekable();
1322 let mut other_entries = other
1323 .entries_by_id
1324 .cursor::<()>()
1325 .filter(|e| include_ignored || !e.is_ignored)
1326 .peekable();
1327 loop {
1328 match (self_entries.peek(), other_entries.peek()) {
1329 (Some(self_entry), Some(other_entry)) => {
1330 match Ord::cmp(&self_entry.id, &other_entry.id) {
1331 Ordering::Less => {
1332 let entry = self.entry_for_id(self_entry.id).unwrap().into();
1333 updated_entries.push(entry);
1334 self_entries.next();
1335 }
1336 Ordering::Equal => {
1337 if self_entry.scan_id != other_entry.scan_id {
1338 let entry = self.entry_for_id(self_entry.id).unwrap().into();
1339 updated_entries.push(entry);
1340 }
1341
1342 self_entries.next();
1343 other_entries.next();
1344 }
1345 Ordering::Greater => {
1346 removed_entries.push(other_entry.id.to_proto());
1347 other_entries.next();
1348 }
1349 }
1350 }
1351 (Some(self_entry), None) => {
1352 let entry = self.entry_for_id(self_entry.id).unwrap().into();
1353 updated_entries.push(entry);
1354 self_entries.next();
1355 }
1356 (None, Some(other_entry)) => {
1357 removed_entries.push(other_entry.id.to_proto());
1358 other_entries.next();
1359 }
1360 (None, None) => break,
1361 }
1362 }
1363
1364 proto::UpdateWorktree {
1365 project_id,
1366 worktree_id,
1367 root_name: self.root_name().to_string(),
1368 updated_entries,
1369 removed_entries,
1370 scan_id: self.scan_id as u64,
1371 }
1372 }
1373
1374 fn insert_entry(&mut self, mut entry: Entry, fs: &dyn Fs) -> Entry {
1375 if !entry.is_dir() && entry.path.file_name() == Some(&GITIGNORE) {
1376 let abs_path = self.abs_path.join(&entry.path);
1377 match build_gitignore(&abs_path, fs) {
1378 Ok(ignore) => {
1379 let ignore_dir_path = entry.path.parent().unwrap();
1380 self.ignores
1381 .insert(ignore_dir_path.into(), (Arc::new(ignore), self.scan_id));
1382 }
1383 Err(error) => {
1384 log::error!(
1385 "error loading .gitignore file {:?} - {:?}",
1386 &entry.path,
1387 error
1388 );
1389 }
1390 }
1391 }
1392
1393 self.reuse_entry_id(&mut entry);
1394 self.entries_by_path.insert_or_replace(entry.clone(), &());
1395 let scan_id = self.scan_id;
1396 self.entries_by_id.insert_or_replace(
1397 PathEntry {
1398 id: entry.id,
1399 path: entry.path.clone(),
1400 is_ignored: entry.is_ignored,
1401 scan_id,
1402 },
1403 &(),
1404 );
1405 entry
1406 }
1407
1408 fn populate_dir(
1409 &mut self,
1410 parent_path: Arc<Path>,
1411 entries: impl IntoIterator<Item = Entry>,
1412 ignore: Option<Arc<Gitignore>>,
1413 ) {
1414 let mut parent_entry = if let Some(parent_entry) =
1415 self.entries_by_path.get(&PathKey(parent_path.clone()), &())
1416 {
1417 parent_entry.clone()
1418 } else {
1419 log::warn!(
1420 "populating a directory {:?} that has been removed",
1421 parent_path
1422 );
1423 return;
1424 };
1425
1426 if let Some(ignore) = ignore {
1427 self.ignores.insert(parent_path, (ignore, self.scan_id));
1428 }
1429 if matches!(parent_entry.kind, EntryKind::PendingDir) {
1430 parent_entry.kind = EntryKind::Dir;
1431 } else {
1432 unreachable!();
1433 }
1434
1435 let mut entries_by_path_edits = vec![Edit::Insert(parent_entry)];
1436 let mut entries_by_id_edits = Vec::new();
1437
1438 for mut entry in entries {
1439 self.reuse_entry_id(&mut entry);
1440 entries_by_id_edits.push(Edit::Insert(PathEntry {
1441 id: entry.id,
1442 path: entry.path.clone(),
1443 is_ignored: entry.is_ignored,
1444 scan_id: self.scan_id,
1445 }));
1446 entries_by_path_edits.push(Edit::Insert(entry));
1447 }
1448
1449 self.entries_by_path.edit(entries_by_path_edits, &());
1450 self.entries_by_id.edit(entries_by_id_edits, &());
1451 }
1452
1453 fn reuse_entry_id(&mut self, entry: &mut Entry) {
1454 if let Some(removed_entry_id) = self.removed_entry_ids.remove(&entry.inode) {
1455 entry.id = removed_entry_id;
1456 } else if let Some(existing_entry) = self.entry_for_path(&entry.path) {
1457 entry.id = existing_entry.id;
1458 }
1459 }
1460
1461 fn remove_path(&mut self, path: &Path) {
1462 let mut new_entries;
1463 let removed_entries;
1464 {
1465 let mut cursor = self.entries_by_path.cursor::<TraversalProgress>();
1466 new_entries = cursor.slice(&TraversalTarget::Path(path), Bias::Left, &());
1467 removed_entries = cursor.slice(&TraversalTarget::PathSuccessor(path), Bias::Left, &());
1468 new_entries.push_tree(cursor.suffix(&()), &());
1469 }
1470 self.entries_by_path = new_entries;
1471
1472 let mut entries_by_id_edits = Vec::new();
1473 for entry in removed_entries.cursor::<()>() {
1474 let removed_entry_id = self
1475 .removed_entry_ids
1476 .entry(entry.inode)
1477 .or_insert(entry.id);
1478 *removed_entry_id = cmp::max(*removed_entry_id, entry.id);
1479 entries_by_id_edits.push(Edit::Remove(entry.id));
1480 }
1481 self.entries_by_id.edit(entries_by_id_edits, &());
1482
1483 if path.file_name() == Some(&GITIGNORE) {
1484 if let Some((_, scan_id)) = self.ignores.get_mut(path.parent().unwrap()) {
1485 *scan_id = self.snapshot.scan_id;
1486 }
1487 }
1488 }
1489
1490 fn ignore_stack_for_path(&self, path: &Path, is_dir: bool) -> Arc<IgnoreStack> {
1491 let mut new_ignores = Vec::new();
1492 for ancestor in path.ancestors().skip(1) {
1493 if let Some((ignore, _)) = self.ignores.get(ancestor) {
1494 new_ignores.push((ancestor, Some(ignore.clone())));
1495 } else {
1496 new_ignores.push((ancestor, None));
1497 }
1498 }
1499
1500 let mut ignore_stack = IgnoreStack::none();
1501 for (parent_path, ignore) in new_ignores.into_iter().rev() {
1502 if ignore_stack.is_path_ignored(&parent_path, true) {
1503 ignore_stack = IgnoreStack::all();
1504 break;
1505 } else if let Some(ignore) = ignore {
1506 ignore_stack = ignore_stack.append(Arc::from(parent_path), ignore);
1507 }
1508 }
1509
1510 if ignore_stack.is_path_ignored(path, is_dir) {
1511 ignore_stack = IgnoreStack::all();
1512 }
1513
1514 ignore_stack
1515 }
1516}
1517
1518fn build_gitignore(abs_path: &Path, fs: &dyn Fs) -> Result<Gitignore> {
1519 let contents = smol::block_on(fs.load(&abs_path))?;
1520 let parent = abs_path.parent().unwrap_or(Path::new("/"));
1521 let mut builder = GitignoreBuilder::new(parent);
1522 for line in contents.lines() {
1523 builder.add_line(Some(abs_path.into()), line)?;
1524 }
1525 Ok(builder.build()?)
1526}
1527
1528impl WorktreeId {
1529 pub fn from_usize(handle_id: usize) -> Self {
1530 Self(handle_id)
1531 }
1532
1533 pub(crate) fn from_proto(id: u64) -> Self {
1534 Self(id as usize)
1535 }
1536
1537 pub fn to_proto(&self) -> u64 {
1538 self.0 as u64
1539 }
1540
1541 pub fn to_usize(&self) -> usize {
1542 self.0
1543 }
1544}
1545
1546impl fmt::Display for WorktreeId {
1547 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1548 self.0.fmt(f)
1549 }
1550}
1551
1552impl Deref for Worktree {
1553 type Target = Snapshot;
1554
1555 fn deref(&self) -> &Self::Target {
1556 match self {
1557 Worktree::Local(worktree) => &worktree.snapshot,
1558 Worktree::Remote(worktree) => &worktree.snapshot,
1559 }
1560 }
1561}
1562
1563impl Deref for LocalWorktree {
1564 type Target = LocalSnapshot;
1565
1566 fn deref(&self) -> &Self::Target {
1567 &self.snapshot
1568 }
1569}
1570
1571impl Deref for RemoteWorktree {
1572 type Target = Snapshot;
1573
1574 fn deref(&self) -> &Self::Target {
1575 &self.snapshot
1576 }
1577}
1578
1579impl fmt::Debug for LocalWorktree {
1580 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1581 self.snapshot.fmt(f)
1582 }
1583}
1584
1585impl fmt::Debug for Snapshot {
1586 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1587 struct EntriesById<'a>(&'a SumTree<PathEntry>);
1588 struct EntriesByPath<'a>(&'a SumTree<Entry>);
1589
1590 impl<'a> fmt::Debug for EntriesByPath<'a> {
1591 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1592 f.debug_map()
1593 .entries(self.0.iter().map(|entry| (&entry.path, entry.id)))
1594 .finish()
1595 }
1596 }
1597
1598 impl<'a> fmt::Debug for EntriesById<'a> {
1599 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1600 f.debug_list().entries(self.0.iter()).finish()
1601 }
1602 }
1603
1604 f.debug_struct("Snapshot")
1605 .field("id", &self.id)
1606 .field("root_name", &self.root_name)
1607 .field("entries_by_path", &EntriesByPath(&self.entries_by_path))
1608 .field("entries_by_id", &EntriesById(&self.entries_by_id))
1609 .finish()
1610 }
1611}
1612
1613#[derive(Clone, PartialEq)]
1614pub struct File {
1615 pub worktree: ModelHandle<Worktree>,
1616 pub path: Arc<Path>,
1617 pub mtime: SystemTime,
1618 pub(crate) entry_id: Option<ProjectEntryId>,
1619 pub(crate) is_local: bool,
1620}
1621
1622impl language::File for File {
1623 fn as_local(&self) -> Option<&dyn language::LocalFile> {
1624 if self.is_local {
1625 Some(self)
1626 } else {
1627 None
1628 }
1629 }
1630
1631 fn mtime(&self) -> SystemTime {
1632 self.mtime
1633 }
1634
1635 fn path(&self) -> &Arc<Path> {
1636 &self.path
1637 }
1638
1639 fn full_path(&self, cx: &AppContext) -> PathBuf {
1640 let mut full_path = PathBuf::new();
1641 full_path.push(self.worktree.read(cx).root_name());
1642 if self.path.components().next().is_some() {
1643 full_path.push(&self.path);
1644 }
1645 full_path
1646 }
1647
1648 /// Returns the last component of this handle's absolute path. If this handle refers to the root
1649 /// of its worktree, then this method will return the name of the worktree itself.
1650 fn file_name(&self, cx: &AppContext) -> OsString {
1651 self.path
1652 .file_name()
1653 .map(|name| name.into())
1654 .unwrap_or_else(|| OsString::from(&self.worktree.read(cx).root_name))
1655 }
1656
1657 fn is_deleted(&self) -> bool {
1658 self.entry_id.is_none()
1659 }
1660
1661 fn save(
1662 &self,
1663 buffer_id: u64,
1664 text: Rope,
1665 version: clock::Global,
1666 cx: &mut MutableAppContext,
1667 ) -> Task<Result<(clock::Global, SystemTime)>> {
1668 self.worktree.update(cx, |worktree, cx| match worktree {
1669 Worktree::Local(worktree) => {
1670 let rpc = worktree.client.clone();
1671 let project_id = worktree.share.as_ref().map(|share| share.project_id);
1672 let save = worktree.write_file(self.path.clone(), text, cx);
1673 cx.background().spawn(async move {
1674 let entry = save.await?;
1675 if let Some(project_id) = project_id {
1676 rpc.send(proto::BufferSaved {
1677 project_id,
1678 buffer_id,
1679 version: serialize_version(&version),
1680 mtime: Some(entry.mtime.into()),
1681 })?;
1682 }
1683 Ok((version, entry.mtime))
1684 })
1685 }
1686 Worktree::Remote(worktree) => {
1687 let rpc = worktree.client.clone();
1688 let project_id = worktree.project_id;
1689 cx.foreground().spawn(async move {
1690 let response = rpc
1691 .request(proto::SaveBuffer {
1692 project_id,
1693 buffer_id,
1694 version: serialize_version(&version),
1695 })
1696 .await?;
1697 let version = deserialize_version(response.version);
1698 let mtime = response
1699 .mtime
1700 .ok_or_else(|| anyhow!("missing mtime"))?
1701 .into();
1702 Ok((version, mtime))
1703 })
1704 }
1705 })
1706 }
1707
1708 fn as_any(&self) -> &dyn Any {
1709 self
1710 }
1711
1712 fn to_proto(&self) -> rpc::proto::File {
1713 rpc::proto::File {
1714 worktree_id: self.worktree.id() as u64,
1715 entry_id: self.entry_id.map(|entry_id| entry_id.to_proto()),
1716 path: self.path.to_string_lossy().into(),
1717 mtime: Some(self.mtime.into()),
1718 }
1719 }
1720}
1721
1722impl language::LocalFile for File {
1723 fn abs_path(&self, cx: &AppContext) -> PathBuf {
1724 self.worktree
1725 .read(cx)
1726 .as_local()
1727 .unwrap()
1728 .abs_path
1729 .join(&self.path)
1730 }
1731
1732 fn load(&self, cx: &AppContext) -> Task<Result<String>> {
1733 let worktree = self.worktree.read(cx).as_local().unwrap();
1734 let abs_path = worktree.absolutize(&self.path);
1735 let fs = worktree.fs.clone();
1736 cx.background()
1737 .spawn(async move { fs.load(&abs_path).await })
1738 }
1739
1740 fn buffer_reloaded(
1741 &self,
1742 buffer_id: u64,
1743 version: &clock::Global,
1744 mtime: SystemTime,
1745 cx: &mut MutableAppContext,
1746 ) {
1747 let worktree = self.worktree.read(cx).as_local().unwrap();
1748 if let Some(project_id) = worktree.share.as_ref().map(|share| share.project_id) {
1749 worktree
1750 .client
1751 .send(proto::BufferReloaded {
1752 project_id,
1753 buffer_id,
1754 version: serialize_version(&version),
1755 mtime: Some(mtime.into()),
1756 })
1757 .log_err();
1758 }
1759 }
1760}
1761
1762impl File {
1763 pub fn from_proto(
1764 proto: rpc::proto::File,
1765 worktree: ModelHandle<Worktree>,
1766 cx: &AppContext,
1767 ) -> Result<Self> {
1768 let worktree_id = worktree
1769 .read(cx)
1770 .as_remote()
1771 .ok_or_else(|| anyhow!("not remote"))?
1772 .id();
1773
1774 if worktree_id.to_proto() != proto.worktree_id {
1775 return Err(anyhow!("worktree id does not match file"));
1776 }
1777
1778 Ok(Self {
1779 worktree,
1780 path: Path::new(&proto.path).into(),
1781 mtime: proto.mtime.ok_or_else(|| anyhow!("no timestamp"))?.into(),
1782 entry_id: proto.entry_id.map(ProjectEntryId::from_proto),
1783 is_local: false,
1784 })
1785 }
1786
1787 pub fn from_dyn(file: Option<&dyn language::File>) -> Option<&Self> {
1788 file.and_then(|f| f.as_any().downcast_ref())
1789 }
1790
1791 pub fn worktree_id(&self, cx: &AppContext) -> WorktreeId {
1792 self.worktree.read(cx).id()
1793 }
1794
1795 pub fn project_entry_id(&self, _: &AppContext) -> Option<ProjectEntryId> {
1796 self.entry_id
1797 }
1798}
1799
1800#[derive(Clone, Debug, PartialEq, Eq)]
1801pub struct Entry {
1802 pub id: ProjectEntryId,
1803 pub kind: EntryKind,
1804 pub path: Arc<Path>,
1805 pub inode: u64,
1806 pub mtime: SystemTime,
1807 pub is_symlink: bool,
1808 pub is_ignored: bool,
1809}
1810
1811#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1812pub enum EntryKind {
1813 PendingDir,
1814 Dir,
1815 File(CharBag),
1816}
1817
1818impl Entry {
1819 fn new(
1820 path: Arc<Path>,
1821 metadata: &fs::Metadata,
1822 next_entry_id: &AtomicUsize,
1823 root_char_bag: CharBag,
1824 ) -> Self {
1825 Self {
1826 id: ProjectEntryId::new(next_entry_id),
1827 kind: if metadata.is_dir {
1828 EntryKind::PendingDir
1829 } else {
1830 EntryKind::File(char_bag_for_path(root_char_bag, &path))
1831 },
1832 path,
1833 inode: metadata.inode,
1834 mtime: metadata.mtime,
1835 is_symlink: metadata.is_symlink,
1836 is_ignored: false,
1837 }
1838 }
1839
1840 pub fn is_dir(&self) -> bool {
1841 matches!(self.kind, EntryKind::Dir | EntryKind::PendingDir)
1842 }
1843
1844 pub fn is_file(&self) -> bool {
1845 matches!(self.kind, EntryKind::File(_))
1846 }
1847}
1848
1849impl sum_tree::Item for Entry {
1850 type Summary = EntrySummary;
1851
1852 fn summary(&self) -> Self::Summary {
1853 let visible_count = if self.is_ignored { 0 } else { 1 };
1854 let file_count;
1855 let visible_file_count;
1856 if self.is_file() {
1857 file_count = 1;
1858 visible_file_count = visible_count;
1859 } else {
1860 file_count = 0;
1861 visible_file_count = 0;
1862 }
1863
1864 EntrySummary {
1865 max_path: self.path.clone(),
1866 count: 1,
1867 visible_count,
1868 file_count,
1869 visible_file_count,
1870 }
1871 }
1872}
1873
1874impl sum_tree::KeyedItem for Entry {
1875 type Key = PathKey;
1876
1877 fn key(&self) -> Self::Key {
1878 PathKey(self.path.clone())
1879 }
1880}
1881
1882#[derive(Clone, Debug)]
1883pub struct EntrySummary {
1884 max_path: Arc<Path>,
1885 count: usize,
1886 visible_count: usize,
1887 file_count: usize,
1888 visible_file_count: usize,
1889}
1890
1891impl Default for EntrySummary {
1892 fn default() -> Self {
1893 Self {
1894 max_path: Arc::from(Path::new("")),
1895 count: 0,
1896 visible_count: 0,
1897 file_count: 0,
1898 visible_file_count: 0,
1899 }
1900 }
1901}
1902
1903impl sum_tree::Summary for EntrySummary {
1904 type Context = ();
1905
1906 fn add_summary(&mut self, rhs: &Self, _: &()) {
1907 self.max_path = rhs.max_path.clone();
1908 self.visible_count += rhs.visible_count;
1909 self.file_count += rhs.file_count;
1910 self.visible_file_count += rhs.visible_file_count;
1911 }
1912}
1913
1914#[derive(Clone, Debug)]
1915struct PathEntry {
1916 id: ProjectEntryId,
1917 path: Arc<Path>,
1918 is_ignored: bool,
1919 scan_id: usize,
1920}
1921
1922impl sum_tree::Item for PathEntry {
1923 type Summary = PathEntrySummary;
1924
1925 fn summary(&self) -> Self::Summary {
1926 PathEntrySummary { max_id: self.id }
1927 }
1928}
1929
1930impl sum_tree::KeyedItem for PathEntry {
1931 type Key = ProjectEntryId;
1932
1933 fn key(&self) -> Self::Key {
1934 self.id
1935 }
1936}
1937
1938#[derive(Clone, Debug, Default)]
1939struct PathEntrySummary {
1940 max_id: ProjectEntryId,
1941}
1942
1943impl sum_tree::Summary for PathEntrySummary {
1944 type Context = ();
1945
1946 fn add_summary(&mut self, summary: &Self, _: &Self::Context) {
1947 self.max_id = summary.max_id;
1948 }
1949}
1950
1951impl<'a> sum_tree::Dimension<'a, PathEntrySummary> for ProjectEntryId {
1952 fn add_summary(&mut self, summary: &'a PathEntrySummary, _: &()) {
1953 *self = summary.max_id;
1954 }
1955}
1956
1957#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
1958pub struct PathKey(Arc<Path>);
1959
1960impl Default for PathKey {
1961 fn default() -> Self {
1962 Self(Path::new("").into())
1963 }
1964}
1965
1966impl<'a> sum_tree::Dimension<'a, EntrySummary> for PathKey {
1967 fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
1968 self.0 = summary.max_path.clone();
1969 }
1970}
1971
1972struct BackgroundScanner {
1973 fs: Arc<dyn Fs>,
1974 snapshot: Arc<Mutex<LocalSnapshot>>,
1975 notify: UnboundedSender<ScanState>,
1976 executor: Arc<executor::Background>,
1977}
1978
1979impl BackgroundScanner {
1980 fn new(
1981 snapshot: Arc<Mutex<LocalSnapshot>>,
1982 notify: UnboundedSender<ScanState>,
1983 fs: Arc<dyn Fs>,
1984 executor: Arc<executor::Background>,
1985 ) -> Self {
1986 Self {
1987 fs,
1988 snapshot,
1989 notify,
1990 executor,
1991 }
1992 }
1993
1994 fn abs_path(&self) -> Arc<Path> {
1995 self.snapshot.lock().abs_path.clone()
1996 }
1997
1998 fn snapshot(&self) -> LocalSnapshot {
1999 self.snapshot.lock().clone()
2000 }
2001
2002 async fn run(mut self, events_rx: impl Stream<Item = Vec<fsevent::Event>>) {
2003 if self.notify.unbounded_send(ScanState::Scanning).is_err() {
2004 return;
2005 }
2006
2007 if let Err(err) = self.scan_dirs().await {
2008 if self
2009 .notify
2010 .unbounded_send(ScanState::Err(Arc::new(err)))
2011 .is_err()
2012 {
2013 return;
2014 }
2015 }
2016
2017 if self.notify.unbounded_send(ScanState::Idle).is_err() {
2018 return;
2019 }
2020
2021 futures::pin_mut!(events_rx);
2022 while let Some(events) = events_rx.next().await {
2023 if self.notify.unbounded_send(ScanState::Scanning).is_err() {
2024 break;
2025 }
2026
2027 if !self.process_events(events).await {
2028 break;
2029 }
2030
2031 if self.notify.unbounded_send(ScanState::Idle).is_err() {
2032 break;
2033 }
2034 }
2035 }
2036
2037 async fn scan_dirs(&mut self) -> Result<()> {
2038 let root_char_bag;
2039 let next_entry_id;
2040 let is_dir;
2041 {
2042 let snapshot = self.snapshot.lock();
2043 root_char_bag = snapshot.root_char_bag;
2044 next_entry_id = snapshot.next_entry_id.clone();
2045 is_dir = snapshot.root_entry().map_or(false, |e| e.is_dir())
2046 };
2047
2048 if is_dir {
2049 let path: Arc<Path> = Arc::from(Path::new(""));
2050 let abs_path = self.abs_path();
2051 let (tx, rx) = channel::unbounded();
2052 self.executor
2053 .block(tx.send(ScanJob {
2054 abs_path: abs_path.to_path_buf(),
2055 path,
2056 ignore_stack: IgnoreStack::none(),
2057 scan_queue: tx.clone(),
2058 }))
2059 .unwrap();
2060 drop(tx);
2061
2062 self.executor
2063 .scoped(|scope| {
2064 for _ in 0..self.executor.num_cpus() {
2065 scope.spawn(async {
2066 while let Ok(job) = rx.recv().await {
2067 if let Err(err) = self
2068 .scan_dir(root_char_bag, next_entry_id.clone(), &job)
2069 .await
2070 {
2071 log::error!("error scanning {:?}: {}", job.abs_path, err);
2072 }
2073 }
2074 });
2075 }
2076 })
2077 .await;
2078 }
2079
2080 Ok(())
2081 }
2082
2083 async fn scan_dir(
2084 &self,
2085 root_char_bag: CharBag,
2086 next_entry_id: Arc<AtomicUsize>,
2087 job: &ScanJob,
2088 ) -> Result<()> {
2089 let mut new_entries: Vec<Entry> = Vec::new();
2090 let mut new_jobs: Vec<ScanJob> = Vec::new();
2091 let mut ignore_stack = job.ignore_stack.clone();
2092 let mut new_ignore = None;
2093
2094 let mut child_paths = self.fs.read_dir(&job.abs_path).await?;
2095 while let Some(child_abs_path) = child_paths.next().await {
2096 let child_abs_path = match child_abs_path {
2097 Ok(child_abs_path) => child_abs_path,
2098 Err(error) => {
2099 log::error!("error processing entry {:?}", error);
2100 continue;
2101 }
2102 };
2103 let child_name = child_abs_path.file_name().unwrap();
2104 let child_path: Arc<Path> = job.path.join(child_name).into();
2105 let child_metadata = match self.fs.metadata(&child_abs_path).await? {
2106 Some(metadata) => metadata,
2107 None => continue,
2108 };
2109
2110 // If we find a .gitignore, add it to the stack of ignores used to determine which paths are ignored
2111 if child_name == *GITIGNORE {
2112 match build_gitignore(&child_abs_path, self.fs.as_ref()) {
2113 Ok(ignore) => {
2114 let ignore = Arc::new(ignore);
2115 ignore_stack = ignore_stack.append(job.path.clone(), ignore.clone());
2116 new_ignore = Some(ignore);
2117 }
2118 Err(error) => {
2119 log::error!(
2120 "error loading .gitignore file {:?} - {:?}",
2121 child_name,
2122 error
2123 );
2124 }
2125 }
2126
2127 // Update ignore status of any child entries we've already processed to reflect the
2128 // ignore file in the current directory. Because `.gitignore` starts with a `.`,
2129 // there should rarely be too numerous. Update the ignore stack associated with any
2130 // new jobs as well.
2131 let mut new_jobs = new_jobs.iter_mut();
2132 for entry in &mut new_entries {
2133 entry.is_ignored = ignore_stack.is_path_ignored(&entry.path, entry.is_dir());
2134 if entry.is_dir() {
2135 new_jobs.next().unwrap().ignore_stack = if entry.is_ignored {
2136 IgnoreStack::all()
2137 } else {
2138 ignore_stack.clone()
2139 };
2140 }
2141 }
2142 }
2143
2144 let mut child_entry = Entry::new(
2145 child_path.clone(),
2146 &child_metadata,
2147 &next_entry_id,
2148 root_char_bag,
2149 );
2150
2151 if child_metadata.is_dir {
2152 let is_ignored = ignore_stack.is_path_ignored(&child_path, true);
2153 child_entry.is_ignored = is_ignored;
2154 new_entries.push(child_entry);
2155 new_jobs.push(ScanJob {
2156 abs_path: child_abs_path,
2157 path: child_path,
2158 ignore_stack: if is_ignored {
2159 IgnoreStack::all()
2160 } else {
2161 ignore_stack.clone()
2162 },
2163 scan_queue: job.scan_queue.clone(),
2164 });
2165 } else {
2166 child_entry.is_ignored = ignore_stack.is_path_ignored(&child_path, false);
2167 new_entries.push(child_entry);
2168 };
2169 }
2170
2171 self.snapshot
2172 .lock()
2173 .populate_dir(job.path.clone(), new_entries, new_ignore);
2174 for new_job in new_jobs {
2175 job.scan_queue.send(new_job).await.unwrap();
2176 }
2177
2178 Ok(())
2179 }
2180
2181 async fn process_events(&mut self, mut events: Vec<fsevent::Event>) -> bool {
2182 events.sort_unstable_by(|a, b| a.path.cmp(&b.path));
2183 events.dedup_by(|a, b| a.path.starts_with(&b.path));
2184
2185 let root_char_bag;
2186 let root_abs_path;
2187 let next_entry_id;
2188 {
2189 let snapshot = self.snapshot.lock();
2190 root_char_bag = snapshot.root_char_bag;
2191 root_abs_path = snapshot.abs_path.clone();
2192 next_entry_id = snapshot.next_entry_id.clone();
2193 }
2194
2195 let root_abs_path = if let Ok(abs_path) = self.fs.canonicalize(&root_abs_path).await {
2196 abs_path
2197 } else {
2198 return false;
2199 };
2200 let metadata = futures::future::join_all(
2201 events
2202 .iter()
2203 .map(|event| self.fs.metadata(&event.path))
2204 .collect::<Vec<_>>(),
2205 )
2206 .await;
2207
2208 // Hold the snapshot lock while clearing and re-inserting the root entries
2209 // for each event. This way, the snapshot is not observable to the foreground
2210 // thread while this operation is in-progress.
2211 let (scan_queue_tx, scan_queue_rx) = channel::unbounded();
2212 {
2213 let mut snapshot = self.snapshot.lock();
2214 snapshot.scan_id += 1;
2215 for event in &events {
2216 if let Ok(path) = event.path.strip_prefix(&root_abs_path) {
2217 snapshot.remove_path(&path);
2218 }
2219 }
2220
2221 for (event, metadata) in events.into_iter().zip(metadata.into_iter()) {
2222 let path: Arc<Path> = match event.path.strip_prefix(&root_abs_path) {
2223 Ok(path) => Arc::from(path.to_path_buf()),
2224 Err(_) => {
2225 log::error!(
2226 "unexpected event {:?} for root path {:?}",
2227 event.path,
2228 root_abs_path
2229 );
2230 continue;
2231 }
2232 };
2233
2234 match metadata {
2235 Ok(Some(metadata)) => {
2236 let ignore_stack = snapshot.ignore_stack_for_path(&path, metadata.is_dir);
2237 let mut fs_entry = Entry::new(
2238 path.clone(),
2239 &metadata,
2240 snapshot.next_entry_id.as_ref(),
2241 snapshot.root_char_bag,
2242 );
2243 fs_entry.is_ignored = ignore_stack.is_all();
2244 snapshot.insert_entry(fs_entry, self.fs.as_ref());
2245 if metadata.is_dir {
2246 self.executor
2247 .block(scan_queue_tx.send(ScanJob {
2248 abs_path: event.path,
2249 path,
2250 ignore_stack,
2251 scan_queue: scan_queue_tx.clone(),
2252 }))
2253 .unwrap();
2254 }
2255 }
2256 Ok(None) => {}
2257 Err(err) => {
2258 // TODO - create a special 'error' entry in the entries tree to mark this
2259 log::error!("error reading file on event {:?}", err);
2260 }
2261 }
2262 }
2263 drop(scan_queue_tx);
2264 }
2265
2266 // Scan any directories that were created as part of this event batch.
2267 self.executor
2268 .scoped(|scope| {
2269 for _ in 0..self.executor.num_cpus() {
2270 scope.spawn(async {
2271 while let Ok(job) = scan_queue_rx.recv().await {
2272 if let Err(err) = self
2273 .scan_dir(root_char_bag, next_entry_id.clone(), &job)
2274 .await
2275 {
2276 log::error!("error scanning {:?}: {}", job.abs_path, err);
2277 }
2278 }
2279 });
2280 }
2281 })
2282 .await;
2283
2284 // Attempt to detect renames only over a single batch of file-system events.
2285 self.snapshot.lock().removed_entry_ids.clear();
2286
2287 self.update_ignore_statuses().await;
2288 true
2289 }
2290
2291 async fn update_ignore_statuses(&self) {
2292 let mut snapshot = self.snapshot();
2293
2294 let mut ignores_to_update = Vec::new();
2295 let mut ignores_to_delete = Vec::new();
2296 for (parent_path, (_, scan_id)) in &snapshot.ignores {
2297 if *scan_id == snapshot.scan_id && snapshot.entry_for_path(parent_path).is_some() {
2298 ignores_to_update.push(parent_path.clone());
2299 }
2300
2301 let ignore_path = parent_path.join(&*GITIGNORE);
2302 if snapshot.entry_for_path(ignore_path).is_none() {
2303 ignores_to_delete.push(parent_path.clone());
2304 }
2305 }
2306
2307 for parent_path in ignores_to_delete {
2308 snapshot.ignores.remove(&parent_path);
2309 self.snapshot.lock().ignores.remove(&parent_path);
2310 }
2311
2312 let (ignore_queue_tx, ignore_queue_rx) = channel::unbounded();
2313 ignores_to_update.sort_unstable();
2314 let mut ignores_to_update = ignores_to_update.into_iter().peekable();
2315 while let Some(parent_path) = ignores_to_update.next() {
2316 while ignores_to_update
2317 .peek()
2318 .map_or(false, |p| p.starts_with(&parent_path))
2319 {
2320 ignores_to_update.next().unwrap();
2321 }
2322
2323 let ignore_stack = snapshot.ignore_stack_for_path(&parent_path, true);
2324 ignore_queue_tx
2325 .send(UpdateIgnoreStatusJob {
2326 path: parent_path,
2327 ignore_stack,
2328 ignore_queue: ignore_queue_tx.clone(),
2329 })
2330 .await
2331 .unwrap();
2332 }
2333 drop(ignore_queue_tx);
2334
2335 self.executor
2336 .scoped(|scope| {
2337 for _ in 0..self.executor.num_cpus() {
2338 scope.spawn(async {
2339 while let Ok(job) = ignore_queue_rx.recv().await {
2340 self.update_ignore_status(job, &snapshot).await;
2341 }
2342 });
2343 }
2344 })
2345 .await;
2346 }
2347
2348 async fn update_ignore_status(&self, job: UpdateIgnoreStatusJob, snapshot: &LocalSnapshot) {
2349 let mut ignore_stack = job.ignore_stack;
2350 if let Some((ignore, _)) = snapshot.ignores.get(&job.path) {
2351 ignore_stack = ignore_stack.append(job.path.clone(), ignore.clone());
2352 }
2353
2354 let mut entries_by_id_edits = Vec::new();
2355 let mut entries_by_path_edits = Vec::new();
2356 for mut entry in snapshot.child_entries(&job.path).cloned() {
2357 let was_ignored = entry.is_ignored;
2358 entry.is_ignored = ignore_stack.is_path_ignored(&entry.path, entry.is_dir());
2359 if entry.is_dir() {
2360 let child_ignore_stack = if entry.is_ignored {
2361 IgnoreStack::all()
2362 } else {
2363 ignore_stack.clone()
2364 };
2365 job.ignore_queue
2366 .send(UpdateIgnoreStatusJob {
2367 path: entry.path.clone(),
2368 ignore_stack: child_ignore_stack,
2369 ignore_queue: job.ignore_queue.clone(),
2370 })
2371 .await
2372 .unwrap();
2373 }
2374
2375 if entry.is_ignored != was_ignored {
2376 let mut path_entry = snapshot.entries_by_id.get(&entry.id, &()).unwrap().clone();
2377 path_entry.scan_id = snapshot.scan_id;
2378 path_entry.is_ignored = entry.is_ignored;
2379 entries_by_id_edits.push(Edit::Insert(path_entry));
2380 entries_by_path_edits.push(Edit::Insert(entry));
2381 }
2382 }
2383
2384 let mut snapshot = self.snapshot.lock();
2385 snapshot.entries_by_path.edit(entries_by_path_edits, &());
2386 snapshot.entries_by_id.edit(entries_by_id_edits, &());
2387 }
2388}
2389
2390fn char_bag_for_path(root_char_bag: CharBag, path: &Path) -> CharBag {
2391 let mut result = root_char_bag;
2392 result.extend(
2393 path.to_string_lossy()
2394 .chars()
2395 .map(|c| c.to_ascii_lowercase()),
2396 );
2397 result
2398}
2399
2400struct ScanJob {
2401 abs_path: PathBuf,
2402 path: Arc<Path>,
2403 ignore_stack: Arc<IgnoreStack>,
2404 scan_queue: Sender<ScanJob>,
2405}
2406
2407struct UpdateIgnoreStatusJob {
2408 path: Arc<Path>,
2409 ignore_stack: Arc<IgnoreStack>,
2410 ignore_queue: Sender<UpdateIgnoreStatusJob>,
2411}
2412
2413pub trait WorktreeHandle {
2414 #[cfg(any(test, feature = "test-support"))]
2415 fn flush_fs_events<'a>(
2416 &self,
2417 cx: &'a gpui::TestAppContext,
2418 ) -> futures::future::LocalBoxFuture<'a, ()>;
2419}
2420
2421impl WorktreeHandle for ModelHandle<Worktree> {
2422 // When the worktree's FS event stream sometimes delivers "redundant" events for FS changes that
2423 // occurred before the worktree was constructed. These events can cause the worktree to perfrom
2424 // extra directory scans, and emit extra scan-state notifications.
2425 //
2426 // This function mutates the worktree's directory and waits for those mutations to be picked up,
2427 // to ensure that all redundant FS events have already been processed.
2428 #[cfg(any(test, feature = "test-support"))]
2429 fn flush_fs_events<'a>(
2430 &self,
2431 cx: &'a gpui::TestAppContext,
2432 ) -> futures::future::LocalBoxFuture<'a, ()> {
2433 use smol::future::FutureExt;
2434
2435 let filename = "fs-event-sentinel";
2436 let tree = self.clone();
2437 let (fs, root_path) = self.read_with(cx, |tree, _| {
2438 let tree = tree.as_local().unwrap();
2439 (tree.fs.clone(), tree.abs_path().clone())
2440 });
2441
2442 async move {
2443 fs.create_file(&root_path.join(filename), Default::default())
2444 .await
2445 .unwrap();
2446 tree.condition(&cx, |tree, _| tree.entry_for_path(filename).is_some())
2447 .await;
2448
2449 fs.remove_file(&root_path.join(filename), Default::default())
2450 .await
2451 .unwrap();
2452 tree.condition(&cx, |tree, _| tree.entry_for_path(filename).is_none())
2453 .await;
2454
2455 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2456 .await;
2457 }
2458 .boxed_local()
2459 }
2460}
2461
2462#[derive(Clone, Debug)]
2463struct TraversalProgress<'a> {
2464 max_path: &'a Path,
2465 count: usize,
2466 visible_count: usize,
2467 file_count: usize,
2468 visible_file_count: usize,
2469}
2470
2471impl<'a> TraversalProgress<'a> {
2472 fn count(&self, include_dirs: bool, include_ignored: bool) -> usize {
2473 match (include_ignored, include_dirs) {
2474 (true, true) => self.count,
2475 (true, false) => self.file_count,
2476 (false, true) => self.visible_count,
2477 (false, false) => self.visible_file_count,
2478 }
2479 }
2480}
2481
2482impl<'a> sum_tree::Dimension<'a, EntrySummary> for TraversalProgress<'a> {
2483 fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
2484 self.max_path = summary.max_path.as_ref();
2485 self.count += summary.count;
2486 self.visible_count += summary.visible_count;
2487 self.file_count += summary.file_count;
2488 self.visible_file_count += summary.visible_file_count;
2489 }
2490}
2491
2492impl<'a> Default for TraversalProgress<'a> {
2493 fn default() -> Self {
2494 Self {
2495 max_path: Path::new(""),
2496 count: 0,
2497 visible_count: 0,
2498 file_count: 0,
2499 visible_file_count: 0,
2500 }
2501 }
2502}
2503
2504pub struct Traversal<'a> {
2505 cursor: sum_tree::Cursor<'a, Entry, TraversalProgress<'a>>,
2506 include_ignored: bool,
2507 include_dirs: bool,
2508}
2509
2510impl<'a> Traversal<'a> {
2511 pub fn advance(&mut self) -> bool {
2512 self.advance_to_offset(self.offset() + 1)
2513 }
2514
2515 pub fn advance_to_offset(&mut self, offset: usize) -> bool {
2516 self.cursor.seek_forward(
2517 &TraversalTarget::Count {
2518 count: offset,
2519 include_dirs: self.include_dirs,
2520 include_ignored: self.include_ignored,
2521 },
2522 Bias::Right,
2523 &(),
2524 )
2525 }
2526
2527 pub fn advance_to_sibling(&mut self) -> bool {
2528 while let Some(entry) = self.cursor.item() {
2529 self.cursor.seek_forward(
2530 &TraversalTarget::PathSuccessor(&entry.path),
2531 Bias::Left,
2532 &(),
2533 );
2534 if let Some(entry) = self.cursor.item() {
2535 if (self.include_dirs || !entry.is_dir())
2536 && (self.include_ignored || !entry.is_ignored)
2537 {
2538 return true;
2539 }
2540 }
2541 }
2542 false
2543 }
2544
2545 pub fn entry(&self) -> Option<&'a Entry> {
2546 self.cursor.item()
2547 }
2548
2549 pub fn offset(&self) -> usize {
2550 self.cursor
2551 .start()
2552 .count(self.include_dirs, self.include_ignored)
2553 }
2554}
2555
2556impl<'a> Iterator for Traversal<'a> {
2557 type Item = &'a Entry;
2558
2559 fn next(&mut self) -> Option<Self::Item> {
2560 if let Some(item) = self.entry() {
2561 self.advance();
2562 Some(item)
2563 } else {
2564 None
2565 }
2566 }
2567}
2568
2569#[derive(Debug)]
2570enum TraversalTarget<'a> {
2571 Path(&'a Path),
2572 PathSuccessor(&'a Path),
2573 Count {
2574 count: usize,
2575 include_ignored: bool,
2576 include_dirs: bool,
2577 },
2578}
2579
2580impl<'a, 'b> SeekTarget<'a, EntrySummary, TraversalProgress<'a>> for TraversalTarget<'b> {
2581 fn cmp(&self, cursor_location: &TraversalProgress<'a>, _: &()) -> Ordering {
2582 match self {
2583 TraversalTarget::Path(path) => path.cmp(&cursor_location.max_path),
2584 TraversalTarget::PathSuccessor(path) => {
2585 if !cursor_location.max_path.starts_with(path) {
2586 Ordering::Equal
2587 } else {
2588 Ordering::Greater
2589 }
2590 }
2591 TraversalTarget::Count {
2592 count,
2593 include_dirs,
2594 include_ignored,
2595 } => Ord::cmp(
2596 count,
2597 &cursor_location.count(*include_dirs, *include_ignored),
2598 ),
2599 }
2600 }
2601}
2602
2603struct ChildEntriesIter<'a> {
2604 parent_path: &'a Path,
2605 traversal: Traversal<'a>,
2606}
2607
2608impl<'a> Iterator for ChildEntriesIter<'a> {
2609 type Item = &'a Entry;
2610
2611 fn next(&mut self) -> Option<Self::Item> {
2612 if let Some(item) = self.traversal.entry() {
2613 if item.path.starts_with(&self.parent_path) {
2614 self.traversal.advance_to_sibling();
2615 return Some(item);
2616 }
2617 }
2618 None
2619 }
2620}
2621
2622impl<'a> From<&'a Entry> for proto::Entry {
2623 fn from(entry: &'a Entry) -> Self {
2624 Self {
2625 id: entry.id.to_proto(),
2626 is_dir: entry.is_dir(),
2627 path: entry.path.as_os_str().as_bytes().to_vec(),
2628 inode: entry.inode,
2629 mtime: Some(entry.mtime.into()),
2630 is_symlink: entry.is_symlink,
2631 is_ignored: entry.is_ignored,
2632 }
2633 }
2634}
2635
2636impl<'a> TryFrom<(&'a CharBag, proto::Entry)> for Entry {
2637 type Error = anyhow::Error;
2638
2639 fn try_from((root_char_bag, entry): (&'a CharBag, proto::Entry)) -> Result<Self> {
2640 if let Some(mtime) = entry.mtime {
2641 let kind = if entry.is_dir {
2642 EntryKind::Dir
2643 } else {
2644 let mut char_bag = root_char_bag.clone();
2645 char_bag.extend(
2646 String::from_utf8_lossy(&entry.path)
2647 .chars()
2648 .map(|c| c.to_ascii_lowercase()),
2649 );
2650 EntryKind::File(char_bag)
2651 };
2652 let path: Arc<Path> = PathBuf::from(OsString::from_vec(entry.path)).into();
2653 Ok(Entry {
2654 id: ProjectEntryId::from_proto(entry.id),
2655 kind,
2656 path: path.clone(),
2657 inode: entry.inode,
2658 mtime: mtime.into(),
2659 is_symlink: entry.is_symlink,
2660 is_ignored: entry.is_ignored,
2661 })
2662 } else {
2663 Err(anyhow!(
2664 "missing mtime in remote worktree entry {:?}",
2665 entry.path
2666 ))
2667 }
2668 }
2669}
2670
2671#[cfg(test)]
2672mod tests {
2673 use super::*;
2674 use crate::fs::FakeFs;
2675 use anyhow::Result;
2676 use client::test::FakeHttpClient;
2677 use fs::RealFs;
2678 use rand::prelude::*;
2679 use serde_json::json;
2680 use std::{
2681 env,
2682 fmt::Write,
2683 time::{SystemTime, UNIX_EPOCH},
2684 };
2685 use util::test::temp_tree;
2686
2687 #[gpui::test]
2688 async fn test_traversal(cx: &mut gpui::TestAppContext) {
2689 let fs = FakeFs::new(cx.background());
2690 fs.insert_tree(
2691 "/root",
2692 json!({
2693 ".gitignore": "a/b\n",
2694 "a": {
2695 "b": "",
2696 "c": "",
2697 }
2698 }),
2699 )
2700 .await;
2701
2702 let http_client = FakeHttpClient::with_404_response();
2703 let client = Client::new(http_client);
2704
2705 let tree = Worktree::local(
2706 client,
2707 Arc::from(Path::new("/root")),
2708 true,
2709 fs,
2710 Default::default(),
2711 &mut cx.to_async(),
2712 )
2713 .await
2714 .unwrap();
2715 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2716 .await;
2717
2718 tree.read_with(cx, |tree, _| {
2719 assert_eq!(
2720 tree.entries(false)
2721 .map(|entry| entry.path.as_ref())
2722 .collect::<Vec<_>>(),
2723 vec![
2724 Path::new(""),
2725 Path::new(".gitignore"),
2726 Path::new("a"),
2727 Path::new("a/c"),
2728 ]
2729 );
2730 })
2731 }
2732
2733 #[gpui::test]
2734 async fn test_rescan_with_gitignore(cx: &mut gpui::TestAppContext) {
2735 let dir = temp_tree(json!({
2736 ".git": {},
2737 ".gitignore": "ignored-dir\n",
2738 "tracked-dir": {
2739 "tracked-file1": "tracked contents",
2740 },
2741 "ignored-dir": {
2742 "ignored-file1": "ignored contents",
2743 }
2744 }));
2745
2746 let http_client = FakeHttpClient::with_404_response();
2747 let client = Client::new(http_client.clone());
2748
2749 let tree = Worktree::local(
2750 client,
2751 dir.path(),
2752 true,
2753 Arc::new(RealFs),
2754 Default::default(),
2755 &mut cx.to_async(),
2756 )
2757 .await
2758 .unwrap();
2759 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2760 .await;
2761 tree.flush_fs_events(&cx).await;
2762 cx.read(|cx| {
2763 let tree = tree.read(cx);
2764 let tracked = tree.entry_for_path("tracked-dir/tracked-file1").unwrap();
2765 let ignored = tree.entry_for_path("ignored-dir/ignored-file1").unwrap();
2766 assert_eq!(tracked.is_ignored, false);
2767 assert_eq!(ignored.is_ignored, true);
2768 });
2769
2770 std::fs::write(dir.path().join("tracked-dir/tracked-file2"), "").unwrap();
2771 std::fs::write(dir.path().join("ignored-dir/ignored-file2"), "").unwrap();
2772 tree.flush_fs_events(&cx).await;
2773 cx.read(|cx| {
2774 let tree = tree.read(cx);
2775 let dot_git = tree.entry_for_path(".git").unwrap();
2776 let tracked = tree.entry_for_path("tracked-dir/tracked-file2").unwrap();
2777 let ignored = tree.entry_for_path("ignored-dir/ignored-file2").unwrap();
2778 assert_eq!(tracked.is_ignored, false);
2779 assert_eq!(ignored.is_ignored, true);
2780 assert_eq!(dot_git.is_ignored, true);
2781 });
2782 }
2783
2784 #[gpui::test(iterations = 100)]
2785 fn test_random(mut rng: StdRng) {
2786 let operations = env::var("OPERATIONS")
2787 .map(|o| o.parse().unwrap())
2788 .unwrap_or(40);
2789 let initial_entries = env::var("INITIAL_ENTRIES")
2790 .map(|o| o.parse().unwrap())
2791 .unwrap_or(20);
2792
2793 let root_dir = tempdir::TempDir::new("worktree-test").unwrap();
2794 for _ in 0..initial_entries {
2795 randomly_mutate_tree(root_dir.path(), 1.0, &mut rng).unwrap();
2796 }
2797 log::info!("Generated initial tree");
2798
2799 let (notify_tx, _notify_rx) = mpsc::unbounded();
2800 let fs = Arc::new(RealFs);
2801 let next_entry_id = Arc::new(AtomicUsize::new(0));
2802 let mut initial_snapshot = LocalSnapshot {
2803 abs_path: root_dir.path().into(),
2804 removed_entry_ids: Default::default(),
2805 ignores: Default::default(),
2806 next_entry_id: next_entry_id.clone(),
2807 snapshot: Snapshot {
2808 id: WorktreeId::from_usize(0),
2809 entries_by_path: Default::default(),
2810 entries_by_id: Default::default(),
2811 root_name: Default::default(),
2812 root_char_bag: Default::default(),
2813 scan_id: 0,
2814 },
2815 };
2816 initial_snapshot.insert_entry(
2817 Entry::new(
2818 Path::new("").into(),
2819 &smol::block_on(fs.metadata(root_dir.path()))
2820 .unwrap()
2821 .unwrap(),
2822 &next_entry_id,
2823 Default::default(),
2824 ),
2825 fs.as_ref(),
2826 );
2827 let mut scanner = BackgroundScanner::new(
2828 Arc::new(Mutex::new(initial_snapshot.clone())),
2829 notify_tx,
2830 fs.clone(),
2831 Arc::new(gpui::executor::Background::new()),
2832 );
2833 smol::block_on(scanner.scan_dirs()).unwrap();
2834 scanner.snapshot().check_invariants();
2835
2836 let mut events = Vec::new();
2837 let mut snapshots = Vec::new();
2838 let mut mutations_len = operations;
2839 while mutations_len > 1 {
2840 if !events.is_empty() && rng.gen_bool(0.4) {
2841 let len = rng.gen_range(0..=events.len());
2842 let to_deliver = events.drain(0..len).collect::<Vec<_>>();
2843 log::info!("Delivering events: {:#?}", to_deliver);
2844 smol::block_on(scanner.process_events(to_deliver));
2845 scanner.snapshot().check_invariants();
2846 } else {
2847 events.extend(randomly_mutate_tree(root_dir.path(), 0.6, &mut rng).unwrap());
2848 mutations_len -= 1;
2849 }
2850
2851 if rng.gen_bool(0.2) {
2852 snapshots.push(scanner.snapshot());
2853 }
2854 }
2855 log::info!("Quiescing: {:#?}", events);
2856 smol::block_on(scanner.process_events(events));
2857 scanner.snapshot().check_invariants();
2858
2859 let (notify_tx, _notify_rx) = mpsc::unbounded();
2860 let mut new_scanner = BackgroundScanner::new(
2861 Arc::new(Mutex::new(initial_snapshot)),
2862 notify_tx,
2863 scanner.fs.clone(),
2864 scanner.executor.clone(),
2865 );
2866 smol::block_on(new_scanner.scan_dirs()).unwrap();
2867 assert_eq!(
2868 scanner.snapshot().to_vec(true),
2869 new_scanner.snapshot().to_vec(true)
2870 );
2871
2872 for mut prev_snapshot in snapshots {
2873 let include_ignored = rng.gen::<bool>();
2874 if !include_ignored {
2875 let mut entries_by_path_edits = Vec::new();
2876 let mut entries_by_id_edits = Vec::new();
2877 for entry in prev_snapshot
2878 .entries_by_id
2879 .cursor::<()>()
2880 .filter(|e| e.is_ignored)
2881 {
2882 entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone())));
2883 entries_by_id_edits.push(Edit::Remove(entry.id));
2884 }
2885
2886 prev_snapshot
2887 .entries_by_path
2888 .edit(entries_by_path_edits, &());
2889 prev_snapshot.entries_by_id.edit(entries_by_id_edits, &());
2890 }
2891
2892 let update = scanner
2893 .snapshot()
2894 .build_update(&prev_snapshot, 0, 0, include_ignored);
2895 prev_snapshot.apply_remote_update(update).unwrap();
2896 assert_eq!(
2897 prev_snapshot.to_vec(true),
2898 scanner.snapshot().to_vec(include_ignored)
2899 );
2900 }
2901 }
2902
2903 fn randomly_mutate_tree(
2904 root_path: &Path,
2905 insertion_probability: f64,
2906 rng: &mut impl Rng,
2907 ) -> Result<Vec<fsevent::Event>> {
2908 let root_path = root_path.canonicalize().unwrap();
2909 let (dirs, files) = read_dir_recursive(root_path.clone());
2910
2911 let mut events = Vec::new();
2912 let mut record_event = |path: PathBuf| {
2913 events.push(fsevent::Event {
2914 event_id: SystemTime::now()
2915 .duration_since(UNIX_EPOCH)
2916 .unwrap()
2917 .as_secs(),
2918 flags: fsevent::StreamFlags::empty(),
2919 path,
2920 });
2921 };
2922
2923 if (files.is_empty() && dirs.len() == 1) || rng.gen_bool(insertion_probability) {
2924 let path = dirs.choose(rng).unwrap();
2925 let new_path = path.join(gen_name(rng));
2926
2927 if rng.gen() {
2928 log::info!("Creating dir {:?}", new_path.strip_prefix(root_path)?);
2929 std::fs::create_dir(&new_path)?;
2930 } else {
2931 log::info!("Creating file {:?}", new_path.strip_prefix(root_path)?);
2932 std::fs::write(&new_path, "")?;
2933 }
2934 record_event(new_path);
2935 } else if rng.gen_bool(0.05) {
2936 let ignore_dir_path = dirs.choose(rng).unwrap();
2937 let ignore_path = ignore_dir_path.join(&*GITIGNORE);
2938
2939 let (subdirs, subfiles) = read_dir_recursive(ignore_dir_path.clone());
2940 let files_to_ignore = {
2941 let len = rng.gen_range(0..=subfiles.len());
2942 subfiles.choose_multiple(rng, len)
2943 };
2944 let dirs_to_ignore = {
2945 let len = rng.gen_range(0..subdirs.len());
2946 subdirs.choose_multiple(rng, len)
2947 };
2948
2949 let mut ignore_contents = String::new();
2950 for path_to_ignore in files_to_ignore.chain(dirs_to_ignore) {
2951 write!(
2952 ignore_contents,
2953 "{}\n",
2954 path_to_ignore
2955 .strip_prefix(&ignore_dir_path)?
2956 .to_str()
2957 .unwrap()
2958 )
2959 .unwrap();
2960 }
2961 log::info!(
2962 "Creating {:?} with contents:\n{}",
2963 ignore_path.strip_prefix(&root_path)?,
2964 ignore_contents
2965 );
2966 std::fs::write(&ignore_path, ignore_contents).unwrap();
2967 record_event(ignore_path);
2968 } else {
2969 let old_path = {
2970 let file_path = files.choose(rng);
2971 let dir_path = dirs[1..].choose(rng);
2972 file_path.into_iter().chain(dir_path).choose(rng).unwrap()
2973 };
2974
2975 let is_rename = rng.gen();
2976 if is_rename {
2977 let new_path_parent = dirs
2978 .iter()
2979 .filter(|d| !d.starts_with(old_path))
2980 .choose(rng)
2981 .unwrap();
2982
2983 let overwrite_existing_dir =
2984 !old_path.starts_with(&new_path_parent) && rng.gen_bool(0.3);
2985 let new_path = if overwrite_existing_dir {
2986 std::fs::remove_dir_all(&new_path_parent).ok();
2987 new_path_parent.to_path_buf()
2988 } else {
2989 new_path_parent.join(gen_name(rng))
2990 };
2991
2992 log::info!(
2993 "Renaming {:?} to {}{:?}",
2994 old_path.strip_prefix(&root_path)?,
2995 if overwrite_existing_dir {
2996 "overwrite "
2997 } else {
2998 ""
2999 },
3000 new_path.strip_prefix(&root_path)?
3001 );
3002 std::fs::rename(&old_path, &new_path)?;
3003 record_event(old_path.clone());
3004 record_event(new_path);
3005 } else if old_path.is_dir() {
3006 let (dirs, files) = read_dir_recursive(old_path.clone());
3007
3008 log::info!("Deleting dir {:?}", old_path.strip_prefix(&root_path)?);
3009 std::fs::remove_dir_all(&old_path).unwrap();
3010 for file in files {
3011 record_event(file);
3012 }
3013 for dir in dirs {
3014 record_event(dir);
3015 }
3016 } else {
3017 log::info!("Deleting file {:?}", old_path.strip_prefix(&root_path)?);
3018 std::fs::remove_file(old_path).unwrap();
3019 record_event(old_path.clone());
3020 }
3021 }
3022
3023 Ok(events)
3024 }
3025
3026 fn read_dir_recursive(path: PathBuf) -> (Vec<PathBuf>, Vec<PathBuf>) {
3027 let child_entries = std::fs::read_dir(&path).unwrap();
3028 let mut dirs = vec![path];
3029 let mut files = Vec::new();
3030 for child_entry in child_entries {
3031 let child_path = child_entry.unwrap().path();
3032 if child_path.is_dir() {
3033 let (child_dirs, child_files) = read_dir_recursive(child_path);
3034 dirs.extend(child_dirs);
3035 files.extend(child_files);
3036 } else {
3037 files.push(child_path);
3038 }
3039 }
3040 (dirs, files)
3041 }
3042
3043 fn gen_name(rng: &mut impl Rng) -> String {
3044 (0..6)
3045 .map(|_| rng.sample(rand::distributions::Alphanumeric))
3046 .map(char::from)
3047 .collect()
3048 }
3049
3050 impl LocalSnapshot {
3051 fn check_invariants(&self) {
3052 let mut files = self.files(true, 0);
3053 let mut visible_files = self.files(false, 0);
3054 for entry in self.entries_by_path.cursor::<()>() {
3055 if entry.is_file() {
3056 assert_eq!(files.next().unwrap().inode, entry.inode);
3057 if !entry.is_ignored {
3058 assert_eq!(visible_files.next().unwrap().inode, entry.inode);
3059 }
3060 }
3061 }
3062 assert!(files.next().is_none());
3063 assert!(visible_files.next().is_none());
3064
3065 let mut bfs_paths = Vec::new();
3066 let mut stack = vec![Path::new("")];
3067 while let Some(path) = stack.pop() {
3068 bfs_paths.push(path);
3069 let ix = stack.len();
3070 for child_entry in self.child_entries(path) {
3071 stack.insert(ix, &child_entry.path);
3072 }
3073 }
3074
3075 let dfs_paths = self
3076 .entries_by_path
3077 .cursor::<()>()
3078 .map(|e| e.path.as_ref())
3079 .collect::<Vec<_>>();
3080 assert_eq!(bfs_paths, dfs_paths);
3081
3082 for (ignore_parent_path, _) in &self.ignores {
3083 assert!(self.entry_for_path(ignore_parent_path).is_some());
3084 assert!(self
3085 .entry_for_path(ignore_parent_path.join(&*GITIGNORE))
3086 .is_some());
3087 }
3088 }
3089
3090 fn to_vec(&self, include_ignored: bool) -> Vec<(&Path, u64, bool)> {
3091 let mut paths = Vec::new();
3092 for entry in self.entries_by_path.cursor::<()>() {
3093 if include_ignored || !entry.is_ignored {
3094 paths.push((entry.path.as_ref(), entry.inode, entry.is_ignored));
3095 }
3096 }
3097 paths.sort_by(|a, b| a.0.cmp(&b.0));
3098 paths
3099 }
3100 }
3101}