worktree.rs

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