worktree.rs

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