xvc_file/common/
mod.rs

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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
//! Common operations for xvc file
pub mod compare;
pub mod gitignore;

use std::collections::{HashMap, HashSet};
use std::fs::{self};

use std::{
    fs::Metadata,
    path::{Path, PathBuf},
};

#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;

use crate::common::gitignore::IgnoreOperation;
use crate::error::{Error, Result};
use crossbeam_channel::{Receiver, Sender};
use derive_more::{AsRef, Deref, Display, From, FromStr};
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use serde::{Deserialize, Serialize};
use xvc_config::{conf, FromConfigKey};
use xvc_core::types::xvcpath::XvcCachePath;
use xvc_core::util::file::make_symlink;
use xvc_core::util::xvcignore::COMMON_IGNORE_PATTERNS;
use xvc_core::{
    all_paths_and_metadata, apply_diff, ContentDigest, DiffStore, RecheckMethod, TextOrBinary,
    XvcFileType, XvcMetadata, XvcPath, XvcPathMetadataMap, XvcRoot,
};
use xvc_core::{get_absolute_git_command, get_git_tracked_files, HashAlgorithm};
use xvc_ecs::ecs::event::EventLog;
use xvc_logging::{error, info, uwr, warn, watch, XvcOutputSender};

use xvc_ecs::{persist, HStore, Storable, XvcStore};

use xvc_walker::walk_serial::path_metadata_map_from_file_targets;
use xvc_walker::{AbsolutePath, Glob, PathSync};

use self::gitignore::IgnoreOp;

/// Represents whether a file is a text file or not. We wrap [TextOrBinary] to specify [persist!] and [conf!].
#[derive(
    Debug,
    Clone,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Serialize,
    Deserialize,
    Hash,
    Display,
    FromStr,
    From,
    AsRef,
    Deref,
    Copy,
    Default,
)]
pub struct FileTextOrBinary(TextOrBinary);
conf!(FileTextOrBinary, "file.track.text_or_binary");
persist!(FileTextOrBinary, "file-text-or-binary");

impl FileTextOrBinary {
    /// Returns the inner TextOrBinary
    pub fn as_inner(&self) -> TextOrBinary {
        self.0
    }
}

/// Receives path and metadata and sends content digests of the sent paths.
pub fn pipe_path_digest(
    receiver: Receiver<(PathBuf, Metadata)>,
    sender: Sender<(PathBuf, ContentDigest)>,
    algorithm: HashAlgorithm,
    text_or_binary: TextOrBinary,
) -> Result<()> {
    while let Ok((p, _)) = receiver.try_recv() {
        let digest = ContentDigest::new(&p, algorithm, text_or_binary);
        match digest {
            Ok(digest) => {
                let _ = sender.send((p, digest));
            }
            Err(err) => {
                log::warn!("{:?}", err);
            }
        }
    }
    Ok(())
}

/// This is to convert targets given in the CLI to XvcPaths. It doesn't walk the
/// file system. It's to be used in `xvc file carry-in` or `xvc file recheck`,
/// where we already track the files in the store.
///
/// Just loads the stores, compiles targets as globs and checks
/// which paths in the store matches. If the matches contain directories, all their
/// children are also selected.
///
/// If `targets` is `None`, all paths in the store are returned.
pub fn load_targets_from_store(
    output_snd: &XvcOutputSender,
    xvc_root: &XvcRoot,
    current_dir: &AbsolutePath,
    targets: &Option<Vec<String>>,
) -> Result<HStore<XvcPath>> {
    let xvc_path_store: XvcStore<XvcPath> = xvc_root.load_store()?;
    filter_targets_from_store(output_snd, xvc_root, &xvc_path_store, current_dir, targets)
}

/// Filters the paths in the store by given globs.
///
/// If `targets` is None, returns all paths in the store.
///
/// If `current_dir` is not the root, all targets are prefixed with it.
pub fn filter_targets_from_store(
    output_snd: &XvcOutputSender,
    xvc_root: &XvcRoot,
    xvc_path_store: &XvcStore<XvcPath>,
    current_dir: &AbsolutePath,
    targets: &Option<Vec<String>>,
) -> Result<HStore<XvcPath>> {
    // If we are not in the root, we add current dir to all targets and recur.
    if *current_dir != *xvc_root.absolute_path() {
        let cwd = current_dir
            .strip_prefix(xvc_root.absolute_path())?
            .to_str()
            .unwrap();
        let targets = match targets {
            Some(targets) => targets.iter().map(|t| format!("{cwd}{t}")).collect(),
            None => vec![cwd.to_string()],
        };

        return filter_targets_from_store(
            output_snd,
            xvc_root,
            xvc_path_store,
            xvc_root.absolute_path(),
            &Some(targets),
        );
    }

    watch!(targets);

    if let Some(targets) = targets {
        let paths =
            filter_paths_by_globs(output_snd, xvc_root, xvc_path_store, targets.as_slice())?;
        watch!(paths);
        Ok(paths)
    } else {
        Ok(xvc_path_store.into())
    }
}

/// Filter a set of paths by a set of globs. The globs are compiled into a
/// GlobSet and paths are checked against the set.
///
/// If a target ends with /, it's considered a directory and all its children are also selected.
pub fn filter_paths_by_globs(
    output_snd: &XvcOutputSender,
    xvc_root: &XvcRoot,
    paths: &XvcStore<XvcPath>,
    globs: &[String],
) -> Result<HStore<XvcPath>> {
    watch!(globs);
    if globs.is_empty() {
        return Ok(paths.into());
    }

    // Ensure directories end with /
    let globs = globs
        .iter()
        .map(|g| {
            watch!(g);
            if !g.ends_with('/') && !g.contains('*') {
                let slashed = format!("{g}/");
                watch!(slashed);
                // We don't track directories. Instead we look for files that start with the directory.
                if paths.any(|_, p| p.as_str().starts_with(&slashed)) {
                    slashed
                } else {
                    g.clone()
                }
            } else {
                g.clone()
            }
        })
        .collect::<Vec<String>>();

    watch!(globs);
    let mut glob_matcher = build_glob_matcher(output_snd, xvc_root, &globs)?;
    watch!(glob_matcher);
    let paths = paths
        .iter()
        .filter_map(|(e, p)| {
            if glob_matcher.is_match(p.as_str()) {
                Some((*e, p.clone()))
            } else {
                None
            }
        })
        .collect();

    watch!(paths);
    Ok(paths)
}

/// Builds a glob matcher based on the provided directory and glob patterns.
///
/// # Arguments
///
/// * `output_snd`: A sender for output messages.
/// * `dir`: The directory to which the glob patterns will be applied.
/// * `globs`: A slice of glob patterns as strings.
///
/// # Returns
///
/// * `Result<Glob>`: A `Result` that contains the `Glob` matcher if successful, or an error if not.
///
/// # Errors
///
/// This function will return an error if any of the glob patterns are invalid.
///

pub fn build_glob_matcher(
    output_snd: &XvcOutputSender,
    dir: &Path,
    globs: &[String],
) -> Result<Glob> {
    let mut glob_matcher = Glob::default();
    globs.iter().for_each(|t| {
        watch!(t);
        if t.ends_with('/') {
            if !glob_matcher.add(&format!("{t}**")) {
                error!(output_snd, "Error in glob: {t}");
            }
        } else if !t.contains('*') {
            let abs_target = dir.join(Path::new(t));
            watch!(abs_target);
            if abs_target.is_dir() {
                if !glob_matcher.add(&format!("{t}/**")) {
                    error!(output_snd, "Error in glob: {t}")
                }
            } else if !glob_matcher.add(t) {
                error!(output_snd, "Error in glob: {t}")
            }
        } else if !glob_matcher.add(t) {
            error!(output_snd, "Error in glob: {t}")
        }
    });
    Ok(glob_matcher)
}

/// Converts targets to a map of XvcPaths and their metadata. It walks the file
/// system with [`all_paths_and_metadata`]. This is aimed towards `xvc file
/// track`, `xvc file hash` and similar commands where we work with the existing
/// files.
///
/// This walks all the repository. It doesn't try to optimize the walk by
/// selecting targets first, because,
/// - This is a premature optimization.
/// - We need to consider ignore files and this requires to start a walk from
///   the root.
///
/// If some day we need to optimize first walking the ignores, then walking the
/// directories in the targets, I'd be glad that this is used in very large
/// repositories.

pub fn targets_from_disk(
    output_snd: &XvcOutputSender,
    xvc_root: &XvcRoot,
    current_dir: &AbsolutePath,
    targets: &Option<Vec<String>>,
    filter_git_paths: bool,
) -> Result<XvcPathMetadataMap> {
    watch!(current_dir);
    watch!(xvc_root.absolute_path());
    // If we are not in the root, we add current dir to all targets and recur.
    if *current_dir != *xvc_root.absolute_path() {
        let cwd = current_dir
            .strip_prefix(xvc_root.absolute_path())?
            .to_str()
            .unwrap();

        let cwd = if cwd.ends_with('/') {
            cwd.to_owned()
        } else {
            format!("{cwd}/")
        };

        let targets = match targets {
            Some(targets) => targets.iter().map(|t| format!("{cwd}{t}")).collect(),
            None => vec![cwd.to_string()],
        };
        watch!(targets);
        return targets_from_disk(
            output_snd,
            xvc_root,
            xvc_root.absolute_path(),
            &Some(targets),
            filter_git_paths,
        );
    }

    let has_globs_or_dirs = targets
        .as_ref()
        .map(|targets| {
            targets.iter().any(|t| {
                t.contains('*') || t.ends_with('/') || t.contains('/') || PathBuf::from(t).is_dir()
            })
        })
        // None means all paths
        .unwrap_or(true);
    // If there are no globs/directories in the targets, no need to retrieve all the paths
    // here.

    let all_paths = if has_globs_or_dirs {
        all_paths_and_metadata(xvc_root).0
    } else {
        // FIXME: Move this to a function
        let (pmm, _) = path_metadata_map_from_file_targets(
            output_snd,
            COMMON_IGNORE_PATTERNS,
            xvc_root,
            // This should be ok as we checked empty condition on has_globs_or_dirs
            targets.clone().unwrap(),
            &xvc_walker::WalkOptions::xvcignore(),
        )?;
        let mut xpmm = HashMap::new();

        pmm.into_iter().for_each(|pm| {
            let md: XvcMetadata = XvcMetadata::from(pm.metadata);
            let rxp = XvcPath::new(xvc_root, xvc_root.absolute_path(), &pm.path);
            match rxp {
                Ok(xvc_path) => {
                    xpmm.insert(xvc_path, md);
                }
                Err(e) => {
                    e.warn();
                }
            }
        });
        xpmm
    };

    watch!(all_paths);
    // Return false when the path is a git path

    let git_files: HashSet<String> = if filter_git_paths {
        let git_command_str = xvc_root.config().get_str("git.command")?.option;
        let git_command = get_absolute_git_command(&git_command_str)?;
        get_git_tracked_files(
            &git_command,
            xvc_root
                .absolute_path()
                .to_str()
                .expect("xvc_root must have a path"),
        )?
        .into_iter()
        .collect()
    } else {
        HashSet::new()
    };

    let mut git_path_filter: Box<dyn FnMut(&XvcPath) -> bool> = if filter_git_paths {
        Box::new(|p: &XvcPath| {
            let path_str = p.as_str();
            let path_str = path_str
                .strip_prefix(
                    xvc_root
                        .absolute_path()
                        .to_str()
                        .expect("xvc_root must have a path"),
                )
                .unwrap_or(path_str);
            !git_files.contains(path_str)
        })
    } else {
        Box::new(|_p: &XvcPath| true)
    };

    if let Some(targets) = targets {
        // FIXME: Is this a bug? When targets is empty, we can return all files.
        // Targets should be None to return all paths but what about we pass Some([])?

        if targets.is_empty() {
            return Ok(XvcPathMetadataMap::new());
        }

        let mut glob_matcher = build_glob_matcher(output_snd, xvc_root, targets)?;
        watch!(glob_matcher);
        Ok(all_paths
            .into_iter()
            .filter(|(p, _)| git_path_filter(p))
            .filter(|(p, _)| glob_matcher.is_match(p.as_str()))
            .collect())
    } else {
        Ok(all_paths
            .into_iter()
            .filter(|(p, _)| git_path_filter(p))
            .collect())
    }
}

/// Selects only the files in `targets` by matching them with the metadata in `xvc_metadata_store`.
pub fn only_file_targets(
    xvc_metadata_store: &XvcStore<XvcMetadata>,
    targets: &HStore<XvcPath>,
) -> Result<HStore<XvcPath>> {
    let target_metadata = xvc_metadata_store.subset(targets.keys().copied())?;

    assert! {
        target_metadata.len() == targets.len(),
        "The number of targets and the number of target metadata should be the same."
    }

    let target_files = targets.subset(
        target_metadata
            .filter(|_, xmd| xmd.file_type == XvcFileType::File)
            .keys()
            .copied(),
    )?;

    Ok(target_files)
}

/// Return the metadata of targets. This is used in various functions to get the
/// changed files in repository. When you want to get all files and their
/// metadata, it may be better to use [all_paths_and_metadata].
pub fn xvc_path_metadata_map_from_disk(
    xvc_root: &XvcRoot,
    targets: &HStore<XvcPath>,
) -> XvcPathMetadataMap {
    targets
        .par_iter()
        .map(|(_, xp)| {
            let p = xp.to_absolute_path(xvc_root);
            let xmd = XvcMetadata::from(p.metadata());
            (xp.clone(), xmd)
        })
        .collect()
}

/// Copies / links `cache_path` to `xvc_path` with `recheck_method`.
/// WARNING: If `xvc_path` is already present, it will be deleted first.
/// It also sends an ignore operation to `ignore_writer`.
pub fn recheck_from_cache(
    output_snd: &XvcOutputSender,
    xvc_root: &XvcRoot,
    xvc_path: &XvcPath,
    cache_path: &XvcCachePath,
    recheck_method: RecheckMethod,
    ignore_writer: &Sender<IgnoreOp>,
) -> Result<()> {
    if let Some(parent) = xvc_path.parents().first() {
        watch!(parent);
        let parent_dir = parent.to_absolute_path(xvc_root);
        watch!(parent_dir);
        if !parent_dir.exists() {
            watch!(&parent_dir);
            fs::create_dir_all(parent_dir)?;
            uwr!(
                ignore_writer.send(Some(IgnoreOperation::IgnoreDir {
                    dir: parent.clone(),
                })),
                output_snd
            );
        }
    }
    let cache_path = cache_path.to_absolute_path(xvc_root);
    watch!(cache_path);
    let path = xvc_path.to_absolute_path(xvc_root);
    watch!(path);
    // If the file already exists, we delete it.
    if path.exists() {
        watch!("exists!");
        fs::remove_file(&path)?;
    }

    watch!(path);
    watch!(recheck_method);

    match recheck_method {
        RecheckMethod::Copy => {
            copy_file(output_snd, cache_path, path)?;
        }
        RecheckMethod::Hardlink => {
            fs::hard_link(&cache_path, &path)?;
            info!(output_snd, "[HARDLINK] {} -> {}", cache_path, path);
        }
        RecheckMethod::Symlink => {
            make_symlink(&cache_path, &path)?;
            info!(output_snd, "[SYMLINK] {} -> {}", cache_path, path);
        }
        RecheckMethod::Reflink => {
            reflink(output_snd, cache_path, path)?;
        }
    }
    uwr!(
        ignore_writer.send(Some(IgnoreOperation::IgnoreFile {
            file: xvc_path.clone(),
        })),
        output_snd
    );
    watch!("Return recheck_from_cache");
    Ok(())
}

#[cfg(feature = "reflink")]
fn reflink(
    output_snd: &XvcOutputSender,
    cache_path: AbsolutePath,
    path: AbsolutePath,
) -> Result<()> {
    match reflink::reflink(&cache_path, &path) {
        Ok(_) => {
            info!(output_snd, "[REFLINK] {} -> {}", cache_path, path);
            Ok(())
        }
        Err(e) => {
            warn!(
                output_snd,
                "File system doesn't support reflink. {e}. Copying instead."
            );
            copy_file(output_snd, cache_path, path)
        }
    }
}

fn copy_file(
    output_snd: &XvcOutputSender,
    cache_path: AbsolutePath,
    path: AbsolutePath,
) -> Result<()> {
    fs::copy(&cache_path, &path)?;
    set_writable(&path)?;
    info!(output_snd, "[COPY] {} -> {}", cache_path, path);
    Ok(())
}

#[cfg(not(unix))]
pub fn set_writable(path: &Path) -> Result<()> {
    let mut perm = path.metadata()?.permissions();
    watch!(&perm);
    perm.set_readonly(false);
    watch!(&perm);
    fs::set_permissions(path, perm)?;
    Ok(())
}

#[cfg(not(unix))]
pub fn set_readonly(path: &Path) -> Result<()> {
    let mut perm = path.metadata()?.permissions();
    watch!(&perm);
    perm.set_readonly(true);
    watch!(&perm);
    fs::set_permissions(path, perm)?;
    Ok(())
}

/// Set a path to user writable on unix systems.
#[cfg(unix)]
pub fn set_writable(path: &Path) -> Result<()> {
    let mut permissions = path.metadata()?.permissions();
    let mode = permissions.mode();
    let new_mode = mode | 0o200;
    permissions.set_mode(new_mode);
    fs::set_permissions(path, permissions)?;
    Ok(())
}

/// Set a path to readonly on unix systems.
#[cfg(unix)]
pub fn set_readonly(path: &Path) -> Result<()> {
    let mut permissions = path.metadata()?.permissions();
    let mode = permissions.mode();
    let new_mode = mode & !0o200;
    permissions.set_mode(new_mode);
    fs::set_permissions(path, permissions)?;
    Ok(())
}

#[cfg(not(feature = "reflink"))]
fn reflink(
    output_snd: &XvcOutputSender,
    cache_path: AbsolutePath,
    path: AbsolutePath,
) -> Result<()> {
    warn!(
        output_snd,
        "Xvc isn't compiled with reflink support. Copying the file."
    );
    copy_file(output_snd, cache_path, path)
}

/// All cache paths for all xvc paths.
/// There are extracted from the event logs.
pub fn cache_paths_for_xvc_paths(
    output_snd: &XvcOutputSender,
    all_paths: &XvcStore<XvcPath>,
    all_content_digests: &XvcStore<ContentDigest>,
) -> Result<HStore<Vec<XvcCachePath>>> {
    // Get cache paths for each

    let mut all_cache_paths: HStore<Vec<XvcCachePath>> = HStore::new();

    // Find all cache paths
    // We have 1-1 relationship between content digests and paths.
    // So, in order to get earlier versions, we check the event log.
    for (xe, xp) in all_paths.iter() {
        let path_digest_events: EventLog<ContentDigest> =
            all_content_digests.all_event_log_for_entity(*xe)?;
        let cache_paths = path_digest_events
            .iter()
            .filter_map(|cd_event| match cd_event {
                xvc_ecs::ecs::event::Event::Add { entity: _, value } => {
                    let xcp = uwr!(XvcCachePath::new(xp, value), output_snd
                 );

                    Some(xcp)
                }
                xvc_ecs::ecs::event::Event::Remove { entity } => {
                    // We don't delete ContentDigests of available XvcPaths.
                    // This is an error.
                    error!(
                    output_snd,
                    "There shouldn't be a remove event for content digest of {xp}. Please report this. {}",
                    entity
                );
                    None
                }
            })
            .collect();
        all_cache_paths.insert(*xe, cache_paths);
    }

    Ok(all_cache_paths)
}

/// Moves the `path` to `cache_path`.
///
/// It creates the cache directory and sets the cache file read only.
///
/// It overwrites the cache file if it already exists.
///
/// The [PathSync] struct is used to lock the paths during the operation, so that no two threads
/// try to accessl to the same path at the same time.
// TODO: Remove this when we set unix permissions in platform dependent fashion
#[allow(clippy::permissions_set_readonly_false)]
pub fn move_to_cache(
    path: &AbsolutePath,
    cache_path: &AbsolutePath,
    path_sync: &PathSync,
) -> Result<()> {
    let cache_dir = cache_path.parent().ok_or(Error::InternalError {
        message: "Cache path has no parent.".to_string(),
    })?;
    watch!(cache_dir);
    // We don't lock the path_sync here because we don't want to block other threads.
    path_sync
        .with_sync_abs_path(path, |path| {
            path_sync.with_sync_abs_path(cache_path, |cache_path| {
                if !cache_dir.exists() {
                    fs::create_dir_all(cache_dir)?;
                }
                // Set to writable
                let mut dir_perm = cache_dir.metadata()?.permissions();
                dir_perm.set_readonly(false);
                fs::set_permissions(cache_dir, dir_perm)?;

                fs::rename(path, cache_path)
                    .map_err(|source| xvc_walker::Error::IoError { source })?;
                let mut file_perm = cache_path.metadata()?.permissions();
                watch!(&file_perm.clone());
                file_perm.set_readonly(true);
                fs::set_permissions(cache_path, file_perm.clone())?;
                watch!(&file_perm.clone());
                let mut dir_perm = cache_dir.metadata()?.permissions();
                dir_perm.set_readonly(true);
                fs::set_permissions(cache_dir, dir_perm)?;
                Ok(())
            })
        })
        .map_err(|e| e.into())
}

/// Move an xvc_path to the cache path.
/// Uses [move_to_cache]
pub fn move_xvc_path_to_cache(
    xvc_root: &XvcRoot,
    xvc_path: &XvcPath,
    cache_path: &XvcCachePath,
    path_sync: &PathSync,
) -> Result<()> {
    let path = xvc_path.to_absolute_path(xvc_root);
    watch!(path);
    let cache_path = cache_path.to_absolute_path(xvc_root);
    watch!(cache_path);
    move_to_cache(&path, &cache_path, path_sync)
}

/// Record store records checking their Diff.
/// It loads the store and creates a new store by [apply_diff], then saves it.
/// TODO: This may be optimized for in place update when stores get larger.
pub fn update_store_records<T>(
    xvc_root: &XvcRoot,
    diffs: &DiffStore<T>,
    add_new: bool,
    remove_missing: bool,
) -> Result<()>
where
    T: Storable,
{
    let records = xvc_root.load_store::<T>()?;
    watch!(records.len());
    let new_store = apply_diff(&records, diffs, add_new, remove_missing)?;
    watch!(new_store.len());
    xvc_root.save_store(&new_store)?;
    Ok(())
}