bazaar 3.5.4

Rust implementation of the Bazaar formats and protocols
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
//! Disk staging for a tree transform (breezy's `DiskTreeTransform`).
//!
//! New file/directory/symlink contents are written into a temporary *limbo*
//! directory as they are scheduled, then renamed into their final places when
//! the transform is applied. Staging on disk (rather than in memory) keeps the
//! transform's memory use bounded and lets apply be a sequence of renames.
//!
//! This layer uses the simple limbo-naming scheme: every staged trans-id maps
//! to `<limbo>/<trans_id>`, and apply renames each into place. breezy also has
//! a direct-path optimisation that stages children inside their parent's limbo
//! directory to avoid some renames; that is a performance refinement, not a
//! correctness requirement, and is not implemented here.

use super::{ContentKind, Error, TransformTree, TreeTransformBase};
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

/// How to handle unversioned children of a directory being removed.
///
/// Mirrors breezy's `transform.orphan_policy` config option, whose default is
/// `Conflict` (refuse, leaving a conflict on the directory).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum OrphanPolicy {
    /// Refuse to create orphans; the caller keeps the directory and conflicts.
    #[default]
    Conflict,
    /// Move orphans into a `brz-orphans` directory at the tree root.
    Move,
}

/// A tree transform that stages new contents in a limbo directory on disk.
pub struct DiskTreeTransform<T: TransformTree> {
    base: TreeTransformBase<T>,
    /// The limbo directory (absolute path) where new contents are staged.
    limbo_dir: PathBuf,
    /// trans-id -> its limbo file path.
    limbo_files: HashMap<String, PathBuf>,
    /// trans-id -> the observed `(sha1, size)` of freshly written file content.
    observed_sha1s: HashMap<String, (Vec<u8>, u64)>,
    /// Whether the limbo filesystem supports symlinks.
    create_symlinks: bool,
    /// Whether apply must still consult the mode/mtime (set once created).
    creation_done: HashSet<String>,
    /// The modification time every file staged by this transform is given,
    /// taken when the first one is created.
    creation_mtime: Option<std::time::SystemTime>,
}

impl<T: TransformTree> DiskTreeTransform<T> {
    /// Create a disk transform over `base`, staging into `limbo_dir` (which
    /// must already exist and be empty). `create_symlinks` says whether the
    /// limbo filesystem can hold symlinks.
    pub fn new(base: TreeTransformBase<T>, limbo_dir: PathBuf, create_symlinks: bool) -> Self {
        DiskTreeTransform {
            base,
            limbo_dir,
            limbo_files: HashMap::new(),
            observed_sha1s: HashMap::new(),
            create_symlinks,
            creation_done: HashSet::new(),
            creation_mtime: None,
        }
    }

    /// The underlying bookkeeping.
    pub fn base(&self) -> &TreeTransformBase<T> {
        &self.base
    }

    /// The underlying bookkeeping, mutably.
    pub fn base_mut(&mut self) -> &mut TreeTransformBase<T> {
        &mut self.base
    }

    /// The limbo path of `trans_id`, assigning `<limbo>/<trans_id>` on first
    /// use.
    pub fn limbo_name(&mut self, trans_id: &str) -> PathBuf {
        if let Some(path) = self.limbo_files.get(trans_id) {
            return path.clone();
        }
        let path = self.limbo_dir.join(trans_id);
        self.limbo_files.insert(trans_id.to_string(), path.clone());
        path
    }

    /// The already-assigned limbo path of `trans_id`, if any.
    pub fn limbo_path_of(&self, trans_id: &str) -> Option<&Path> {
        self.limbo_files.get(trans_id).map(PathBuf::as_path)
    }

    /// Check that no contents are staged for `trans_id` yet.
    fn check_no_contents(&self, trans_id: &str) -> Result<(), Error> {
        if self.base.new_contents_map().contains_key(trans_id) {
            return Err(Error::DuplicateKey(format!("{trans_id:?}")));
        }
        Ok(())
    }

    /// Give the file staged at `path` the modification time shared by all
    /// files this transform creates (breezy's `_set_mtime`).
    fn set_mtime(&mut self, path: &Path) -> Result<(), Error> {
        let mtime = *self
            .creation_mtime
            .get_or_insert_with(std::time::SystemTime::now);
        let times = std::fs::FileTimes::new()
            .set_accessed(mtime)
            .set_modified(mtime);
        std::fs::File::options()
            .write(true)
            .open(path)
            .and_then(|file| file.set_times(times))
            .map_err(io_err)
    }

    /// Give the file staged for `trans_id` the mode of the regular file the
    /// tree has at `mode_id`, if it has one (breezy's `_set_mode`).
    fn copy_mode(&mut self, trans_id: &str, mode_id: &str) -> Result<(), Error> {
        use std::io::ErrorKind;

        let Some(old_path) = self.base.tree_path(mode_id) else {
            return Ok(());
        };
        let metadata = match std::fs::metadata(self.base.tree().abspath(old_path)) {
            Ok(metadata) => metadata,
            Err(e) if matches!(e.kind(), ErrorKind::NotFound | ErrorKind::NotADirectory) => {
                return Ok(())
            }
            Err(e) => return Err(io_err(e)),
        };
        if !metadata.is_file() {
            return Ok(());
        }
        let name = self.limbo_name(trans_id);
        chmod_if_possible(&name, metadata.permissions()).map_err(io_err)
    }

    /// Stage a new file with `contents` for `trans_id`. `sha1`, when known,
    /// is recorded so apply can seed the tree's stat cache.
    ///
    /// If `trans_id` is a regular file in the tree, the new file keeps its
    /// mode.
    pub fn create_file(
        &mut self,
        contents: &[u8],
        trans_id: &str,
        sha1: Option<Vec<u8>>,
    ) -> Result<(), Error> {
        self.create_file_with_mode_of(contents, trans_id, trans_id, sha1)
    }

    /// Stage a new file as [`create_file`](Self::create_file) does, taking
    /// its mode from the tree file at `mode_id` rather than at `trans_id`.
    pub fn create_file_with_mode_of(
        &mut self,
        contents: &[u8],
        trans_id: &str,
        mode_id: &str,
        sha1: Option<Vec<u8>>,
    ) -> Result<(), Error> {
        self.check_no_contents(trans_id)?;
        let name = self.limbo_name(trans_id);
        std::fs::write(&name, contents).map_err(io_err)?;
        self.base.set_new_contents(trans_id, ContentKind::File);
        self.creation_done.insert(trans_id.to_string());
        self.set_mtime(&name)?;
        self.copy_mode(trans_id, mode_id)?;
        if let Some(sha1) = sha1 {
            self.observed_sha1s
                .insert(trans_id.to_string(), (sha1, contents.len() as u64));
        }
        Ok(())
    }

    /// Stage a new directory for `trans_id`.
    pub fn create_directory(&mut self, trans_id: &str) -> Result<(), Error> {
        self.check_no_contents(trans_id)?;
        let name = self.limbo_name(trans_id);
        std::fs::create_dir(&name).map_err(io_err)?;
        self.base.set_new_contents(trans_id, ContentKind::Directory);
        self.creation_done.insert(trans_id.to_string());
        Ok(())
    }

    /// Schedule `trans_id` to be orphaned, under `policy`.
    ///
    /// When a directory is removed, its unversioned children are orphaned
    /// rather than deleted. With [`OrphanPolicy::Conflict`] (breezy's default)
    /// this refuses, returning [`Error::OrphaningForbidden`] so the caller
    /// cancels the directory deletion. With [`OrphanPolicy::Move`] the child is
    /// moved into a `brz-orphans` directory at the tree root, with a warning.
    pub fn new_orphan(
        &mut self,
        trans_id: &str,
        parent_id: &str,
        policy: OrphanPolicy,
    ) -> Result<(), Error> {
        match policy {
            OrphanPolicy::Conflict => Err(Error::OrphaningForbidden("conflict".to_string())),
            OrphanPolicy::Move => {
                let orphan_dir_basename = "brz-orphans";
                let od_id = self.base.trans_id_tree_path(orphan_dir_basename);
                if self.base.final_kind(&od_id).is_none() {
                    self.create_directory(&od_id)?;
                }
                let parent_path = self
                    .base
                    .tree_id_paths_map()
                    .get(parent_id)
                    .cloned()
                    .ok_or_else(|| {
                        Error::Tree(format!("orphan parent {parent_id} is not a tree path"))
                    })?;
                let actual_name = self.base.final_name(trans_id)?;
                let new_name = self.base.available_backup_name(&actual_name, &od_id)?;
                self.base.adjust_path(&new_name, &od_id, trans_id)?;
                log::warn!(
                    "{} has been orphaned in {}",
                    super::joinpath(&parent_path, &actual_name),
                    orphan_dir_basename
                );
                Ok(())
            }
        }
    }

    /// Stage a new symlink to `target` for `trans_id`. On a filesystem without
    /// symlink support the link is not created, but the content is still
    /// recorded (matching breezy) so conflict detection is consistent.
    pub fn create_symlink(&mut self, target: &str, trans_id: &str) -> Result<(), Error> {
        self.check_no_contents(trans_id)?;
        let name = self.limbo_name(trans_id);
        if self.create_symlinks {
            symlink(target, &name).map_err(io_err)?;
        } else {
            match self.base.final_path(trans_id) {
                Ok(path) => {
                    log::warn!("Unable to create symlink \"{path}\" on this filesystem.")
                }
                Err(_) => log::warn!("Unable to create symlink \"None\" on this filesystem."),
            }
        }
        self.base.set_new_contents(trans_id, ContentKind::Symlink);
        self.creation_done.insert(trans_id.to_string());
        Ok(())
    }

    /// Create a new path under `parent_id`, versioning it with `file_id` if
    /// given. Breezy's `_new_entry`.
    fn new_entry(
        &mut self,
        name: &str,
        parent_id: &str,
        file_id: Option<crate::FileId>,
    ) -> Result<String, Error> {
        let trans_id = self.base.create_path(name, parent_id)?;
        if let Some(file_id) = file_id {
            self.base.version_file(&trans_id, file_id)?;
        }
        Ok(trans_id)
    }

    /// Convenience: create (and optionally version) a new file, staging its
    /// content and setting executability. Breezy's `new_file`.
    pub fn new_file(
        &mut self,
        name: &str,
        parent_id: &str,
        contents: &[u8],
        file_id: Option<crate::FileId>,
        executable: Option<bool>,
        sha1: Option<Vec<u8>>,
    ) -> Result<String, Error> {
        let trans_id = self.new_entry(name, parent_id, file_id)?;
        self.create_file(contents, &trans_id, sha1)?;
        if let Some(executable) = executable {
            self.base.set_executability(Some(executable), &trans_id)?;
        }
        Ok(trans_id)
    }

    /// Convenience: create (and optionally version) a new directory. Breezy's
    /// `new_directory`.
    pub fn new_directory(
        &mut self,
        name: &str,
        parent_id: &str,
        file_id: Option<crate::FileId>,
    ) -> Result<String, Error> {
        let trans_id = self.new_entry(name, parent_id, file_id)?;
        self.create_directory(&trans_id)?;
        Ok(trans_id)
    }

    /// Convenience: create (and optionally version) a new symlink. Breezy's
    /// `new_symlink`.
    pub fn new_symlink(
        &mut self,
        name: &str,
        parent_id: &str,
        target: &str,
        file_id: Option<crate::FileId>,
    ) -> Result<String, Error> {
        let trans_id = self.new_entry(name, parent_id, file_id)?;
        self.create_symlink(target, &trans_id)?;
        Ok(trans_id)
    }

    /// Fold a newly created root into the existing one, as
    /// [`TreeTransformBase::fixup_new_roots`] does, and discard whatever was
    /// staged in limbo for the root that goes away.
    pub fn fixup_new_roots(&mut self) -> Result<(), Error> {
        self.base.fixup_new_roots()?;
        let cancelled: Vec<String> = self
            .creation_done
            .iter()
            .filter(|trans_id| !self.base.new_contents_map().contains_key(*trans_id))
            .cloned()
            .collect();
        for trans_id in cancelled {
            self.discard_staged(&trans_id)?;
        }
        Ok(())
    }

    /// Cancel staged content creation for `trans_id`, removing its limbo file.
    pub fn cancel_creation(&mut self, trans_id: &str) -> Result<(), Error> {
        if !self.base.has_new_contents(trans_id) {
            return Err(Error::Malformed(format!(
                "{trans_id} has no contents scheduled"
            )));
        }
        self.base.cancel_contents(trans_id);
        self.discard_staged(trans_id)
    }

    /// Remove what is staged in limbo for `trans_id`.
    fn discard_staged(&mut self, trans_id: &str) -> Result<(), Error> {
        self.observed_sha1s.remove(trans_id);
        if let Some(path) = self.limbo_files.remove(trans_id) {
            delete_any(&path).map_err(io_err)?;
        }
        self.creation_done.remove(trans_id);
        Ok(())
    }

    /// The recorded `(sha1, size)` observations for freshly written files.
    pub fn observed_sha1s(&self) -> &HashMap<String, (Vec<u8>, u64)> {
        &self.observed_sha1s
    }

    /// Remove the limbo directory and everything under it. Call after apply,
    /// or to abandon an unapplied transform.
    pub fn finalize(&mut self) -> Result<(), Error> {
        if self.limbo_dir.exists() {
            std::fs::remove_dir_all(&self.limbo_dir).map_err(io_err)?;
        }
        self.limbo_files.clear();
        Ok(())
    }
}

/// Map an I/O error into a transform error.
fn io_err(e: std::io::Error) -> Error {
    Error::Tree(e.to_string())
}

/// Set the mode of `path` where the filesystem allows it; some refuse even
/// on Unix, which is not worth failing over.
pub(super) fn chmod_if_possible(
    path: &Path,
    permissions: std::fs::Permissions,
) -> std::io::Result<()> {
    match std::fs::set_permissions(path, permissions) {
        Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => {
            log::debug!("ignore error on chmod of {path:?}: {e:?}");
            Ok(())
        }
        other => other,
    }
}

/// Delete a file, directory or symlink at `path`, ignoring absence.
fn delete_any(path: &Path) -> std::io::Result<()> {
    match std::fs::symlink_metadata(path) {
        Ok(m) if m.is_dir() => std::fs::remove_dir_all(path),
        Ok(_) => std::fs::remove_file(path),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(e) => Err(e),
    }
}

#[cfg(unix)]
fn symlink(target: &str, path: &Path) -> std::io::Result<()> {
    std::os::unix::fs::symlink(target, path)
}

#[cfg(not(unix))]
fn symlink(_target: &str, _path: &Path) -> std::io::Result<()> {
    Err(std::io::Error::new(
        std::io::ErrorKind::Unsupported,
        "symlinks unsupported",
    ))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::osutils::Kind;
    use crate::transform::tests_support::FakeTree;
    use crate::FileId;

    fn disk_tt() -> (tempfile::TempDir, DiskTreeTransform<FakeTree>) {
        let dir = tempfile::tempdir().unwrap();
        let limbo = dir.path().join("limbo");
        std::fs::create_dir(&limbo).unwrap();
        let base = TreeTransformBase::new(FakeTree::new(), true);
        (dir, DiskTreeTransform::new(base, limbo, cfg!(unix)))
    }

    #[test]
    fn create_file_stages_content_in_limbo() {
        let (_d, mut tt) = disk_tt();
        let root = tt.base().root().unwrap().to_string();
        let tid = tt.base_mut().create_path("f.txt", &root).unwrap();
        tt.create_file(b"hello\n", &tid, None).unwrap();
        // The content is on disk in limbo, and the base records a file.
        let path = tt.limbo_name(&tid);
        assert_eq!(std::fs::read(&path).unwrap(), b"hello\n");
        assert_eq!(tt.base().final_kind(&tid), Some(Kind::File));
    }

    #[test]
    fn create_directory_and_cancel() {
        let (_d, mut tt) = disk_tt();
        let root = tt.base().root().unwrap().to_string();
        let tid = tt.base_mut().create_path("sub", &root).unwrap();
        tt.create_directory(&tid).unwrap();
        assert!(tt.limbo_name(&tid).is_dir());
        tt.cancel_creation(&tid).unwrap();
        // The limbo entry is gone and the base no longer records contents.
        assert_eq!(tt.base().final_kind(&tid), None);
    }

    #[test]
    fn create_file_records_observed_sha1() {
        let (_d, mut tt) = disk_tt();
        let root = tt.base().root().unwrap().to_string();
        let tid = tt.base_mut().create_path("f.txt", &root).unwrap();
        tt.base_mut()
            .version_file(&tid, FileId::from(b"f-id".to_vec()))
            .unwrap();
        tt.create_file(b"data", &tid, Some(b"deadbeef".to_vec()))
            .unwrap();
        let (sha1, size) = &tt.observed_sha1s()[&tid];
        assert_eq!(sha1, b"deadbeef");
        assert_eq!(*size, 4);
    }

    #[cfg(unix)]
    #[test]
    fn create_symlink_stages_a_link() {
        let (_d, mut tt) = disk_tt();
        let root = tt.base().root().unwrap().to_string();
        let tid = tt.base_mut().create_path("link", &root).unwrap();
        tt.create_symlink("target", &tid).unwrap();
        let path = tt.limbo_name(&tid);
        assert_eq!(
            std::fs::read_link(&path).unwrap().to_str().unwrap(),
            "target"
        );
        assert_eq!(tt.base().final_kind(&tid), Some(Kind::Symlink));
    }

    /// Contents can be staged for a trans-id only once.
    #[test]
    fn staging_contents_twice_is_refused() {
        let (_d, mut tt) = disk_tt();
        let root = tt.base().root().unwrap().to_string();
        let tid = tt.base_mut().create_path("f.txt", &root).unwrap();
        tt.create_file(b"first\n", &tid, None).unwrap();
        for result in [
            tt.create_file(b"second\n", &tid, None),
            tt.create_directory(&tid),
            tt.create_symlink("target", &tid),
        ] {
            match result {
                Err(Error::DuplicateKey(key)) => assert_eq!(key, format!("{tid:?}")),
                other => panic!("expected a duplicate key, got {:?}", other),
            }
        }
        assert_eq!(std::fs::read(tt.limbo_name(&tid)).unwrap(), b"first\n");
    }

    /// Every file a transform creates gets the same modification time.
    #[test]
    fn created_files_share_an_mtime() {
        let (_d, mut tt) = disk_tt();
        let root = tt.base().root().unwrap().to_string();
        let first = tt.base_mut().create_path("first", &root).unwrap();
        tt.create_file(b"1", &first, None).unwrap();
        std::thread::sleep(std::time::Duration::from_millis(20));
        let second = tt.base_mut().create_path("second", &root).unwrap();
        tt.create_file(b"2", &second, None).unwrap();
        let mtime = |tt: &mut DiskTreeTransform<FakeTree>, tid: &str| {
            std::fs::metadata(tt.limbo_name(tid))
                .unwrap()
                .modified()
                .unwrap()
        };
        assert_eq!(mtime(&mut tt, &first), mtime(&mut tt, &second));
    }

    /// Where symlinks cannot be made the link is left out, but still counts
    /// as the contents.
    #[test]
    fn create_symlink_without_symlink_support() {
        let dir = tempfile::tempdir().unwrap();
        let limbo = dir.path().join("limbo");
        std::fs::create_dir(&limbo).unwrap();
        let base = TreeTransformBase::new(FakeTree::new(), true);
        let mut tt = DiskTreeTransform::new(base, limbo, false);
        let root = tt.base().root().unwrap().to_string();
        let tid = tt.base_mut().create_path("link", &root).unwrap();
        tt.create_symlink("target", &tid).unwrap();
        assert!(tt.limbo_name(&tid).symlink_metadata().is_err());
        assert_eq!(tt.base().final_kind(&tid), Some(Kind::Symlink));
    }

    #[test]
    fn fixup_new_roots_discards_the_staged_root() {
        let (_d, mut tt) = disk_tt();
        let new_root = tt
            .base_mut()
            .create_path("", crate::transform::ROOT_PARENT)
            .unwrap();
        tt.create_directory(&new_root).unwrap();
        let staged = tt.limbo_name(&new_root);
        assert!(staged.is_dir());

        tt.fixup_new_roots().unwrap();

        assert!(!staged.exists());
        assert_eq!(tt.base().final_kind(&new_root), None);
    }

    #[test]
    fn cancel_creation_needs_staged_contents() {
        let (_d, mut tt) = disk_tt();
        let root = tt.base().root().unwrap().to_string();
        let tid = tt.base_mut().create_path("f", &root).unwrap();
        match tt.cancel_creation(&tid) {
            Err(Error::Malformed(message)) => {
                assert_eq!(message, format!("{tid} has no contents scheduled"))
            }
            other => panic!("expected a malformed transform, got {:?}", other),
        }
    }

    #[test]
    fn finalize_removes_limbo() {
        let (_d, mut tt) = disk_tt();
        let root = tt.base().root().unwrap().to_string();
        let tid = tt.base_mut().create_path("f", &root).unwrap();
        tt.create_file(b"x", &tid, None).unwrap();
        let limbo = tt.limbo_dir.clone();
        tt.finalize().unwrap();
        assert!(!limbo.exists());
    }

    /// An unversioned file in a directory that is being removed.
    fn orphan_in_removed_dir(tt: &mut DiskTreeTransform<FakeTree>) -> (String, String) {
        tt.base_mut().tree.add("dir", b"dir-id", Kind::Directory);
        let dir = tt.base_mut().trans_id_tree_path("dir");
        let orphan = tt.base_mut().trans_id_tree_path("dir/foo");
        tt.base_mut().delete_contents(&dir);
        tt.base_mut().unversion_file(&dir);
        (dir, orphan)
    }

    #[test]
    fn new_orphan_refuses_by_default() {
        let (_d, mut tt) = disk_tt();
        let (dir, orphan) = orphan_in_removed_dir(&mut tt);
        match tt.new_orphan(&orphan, &dir, OrphanPolicy::default()) {
            Err(Error::OrphaningForbidden(policy)) => assert_eq!(policy, "conflict"),
            other => panic!("expected orphaning to be forbidden, got {:?}", other),
        }
        // The orphan stays where it was.
        assert_eq!(tt.base_mut().final_parent(&orphan).unwrap(), dir);
        assert_eq!(tt.base().final_name(&orphan).unwrap(), "foo");
    }

    #[test]
    fn new_orphan_moves_into_the_orphans_directory() {
        let (_d, mut tt) = disk_tt();
        let (dir, orphan) = orphan_in_removed_dir(&mut tt);
        tt.new_orphan(&orphan, &dir, OrphanPolicy::Move).unwrap();

        let orphans_dir = tt.base_mut().final_parent(&orphan).unwrap();
        assert_eq!(tt.base().final_name(&orphans_dir).unwrap(), "brz-orphans");
        assert_eq!(tt.base().final_kind(&orphans_dir), Some(Kind::Directory));
        assert!(tt.limbo_name(&orphans_dir).is_dir());
        assert_eq!(tt.base().final_name(&orphan).unwrap(), "foo.~1~");
    }

    /// The parent of an orphan is a directory of the tree.
    #[test]
    fn new_orphan_needs_a_tree_parent() {
        let (_d, mut tt) = disk_tt();
        let root = tt.base().root().unwrap().to_string();
        let new_dir = tt.base_mut().create_path("dir", &root).unwrap();
        let orphan = tt.base_mut().create_path("foo", &new_dir).unwrap();
        match tt.new_orphan(&orphan, &new_dir, OrphanPolicy::Move) {
            Err(Error::Tree(message)) => assert_eq!(
                message,
                format!("orphan parent {new_dir} is not a tree path")
            ),
            other => panic!("expected a tree error, got {:?}", other),
        }
    }

    /// A second orphan of the same name goes into the same directory under
    /// the next free backup name.
    #[test]
    fn new_orphan_avoids_earlier_orphans() {
        let (_d, mut tt) = disk_tt();
        let (dir, first) = orphan_in_removed_dir(&mut tt);
        tt.base_mut()
            .tree
            .add("other", b"other-id", Kind::Directory);
        let other = tt.base_mut().trans_id_tree_path("other");
        let second = tt.base_mut().trans_id_tree_path("other/foo");
        tt.new_orphan(&first, &dir, OrphanPolicy::Move).unwrap();
        tt.new_orphan(&second, &other, OrphanPolicy::Move).unwrap();

        assert_eq!(
            tt.base_mut().final_parent(&second).unwrap(),
            tt.base_mut().final_parent(&first).unwrap()
        );
        assert_eq!(tt.base().final_name(&first).unwrap(), "foo.~1~");
        assert_eq!(tt.base().final_name(&second).unwrap(), "foo.~2~");
    }
}