common/
link.rs

1use anyhow::{anyhow, Context};
2use async_recursion::async_recursion;
3use std::os::linux::fs::MetadataExt as LinuxMetadataExt;
4use tracing::instrument;
5
6use crate::copy;
7use crate::copy::{Settings as CopySettings, Summary as CopySummary};
8use crate::filecmp;
9use crate::preserve;
10use crate::progress;
11use crate::rm;
12
13lazy_static! {
14    static ref RLINK_PRESERVE_SETTINGS: preserve::Settings = preserve::preserve_all();
15}
16
17/// Error type for link operations that preserves operation summary even on failure.
18///
19/// # Logging Convention
20/// When logging this error, use `{:#}` or `{:?}` format to preserve the error chain:
21/// ```ignore
22/// tracing::error!("operation failed: {:#}", &error); // ✅ Shows full chain
23/// tracing::error!("operation failed: {:?}", &error); // ✅ Shows full chain
24/// ```
25/// The Display implementation also shows the full chain, but workspace linting enforces `{:#}`
26/// for consistency.
27#[derive(Debug, thiserror::Error)]
28#[error("{source:#}")]
29pub struct Error {
30    #[source]
31    pub source: anyhow::Error,
32    pub summary: Summary,
33}
34
35impl Error {
36    #[must_use]
37    pub fn new(source: anyhow::Error, summary: Summary) -> Self {
38        Error { source, summary }
39    }
40}
41
42#[derive(Debug, Copy, Clone)]
43pub struct Settings {
44    pub copy_settings: CopySettings,
45    pub update_compare: filecmp::MetadataCmpSettings,
46    pub update_exclusive: bool,
47}
48
49#[derive(Copy, Clone, Debug, Default)]
50pub struct Summary {
51    pub hard_links_created: usize,
52    pub hard_links_unchanged: usize,
53    pub copy_summary: CopySummary,
54}
55
56impl std::ops::Add for Summary {
57    type Output = Self;
58    fn add(self, other: Self) -> Self {
59        Self {
60            hard_links_created: self.hard_links_created + other.hard_links_created,
61            hard_links_unchanged: self.hard_links_unchanged + other.hard_links_unchanged,
62            copy_summary: self.copy_summary + other.copy_summary,
63        }
64    }
65}
66
67impl std::fmt::Display for Summary {
68    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
69        write!(
70            f,
71            "{}hard-links created: {}\nhard links unchanged: {}\n",
72            &self.copy_summary, self.hard_links_created, self.hard_links_unchanged
73        )
74    }
75}
76
77fn is_hard_link(md1: &std::fs::Metadata, md2: &std::fs::Metadata) -> bool {
78    copy::is_file_type_same(md1, md2)
79        && md2.st_dev() == md1.st_dev()
80        && md2.st_ino() == md1.st_ino()
81}
82
83#[instrument(skip(prog_track))]
84async fn hard_link_helper(
85    prog_track: &'static progress::Progress,
86    src: &std::path::Path,
87    src_metadata: &std::fs::Metadata,
88    dst: &std::path::Path,
89    settings: &Settings,
90) -> Result<Summary, Error> {
91    let mut link_summary = Summary::default();
92    if let Err(error) = tokio::fs::hard_link(src, dst).await {
93        if settings.copy_settings.overwrite && error.kind() == std::io::ErrorKind::AlreadyExists {
94            tracing::debug!("'dst' already exists, check if we need to update");
95            let dst_metadata = tokio::fs::symlink_metadata(dst)
96                .await
97                .with_context(|| format!("cannot read {dst:?} metadata"))
98                .map_err(|err| Error::new(err, Default::default()))?;
99            if is_hard_link(src_metadata, &dst_metadata) {
100                tracing::debug!("no change, leaving file as is");
101                prog_track.hard_links_unchanged.inc();
102                return Ok(Summary {
103                    hard_links_unchanged: 1,
104                    ..Default::default()
105                });
106            }
107            tracing::info!("'dst' file type changed, removing and hard-linking");
108            let rm_summary = rm::rm(
109                prog_track,
110                dst,
111                &rm::Settings {
112                    fail_early: settings.copy_settings.fail_early,
113                },
114            )
115            .await
116            .map_err(|err| {
117                let rm_summary = err.summary;
118                link_summary.copy_summary.rm_summary = rm_summary;
119                Error::new(err.source, link_summary)
120            })?;
121            link_summary.copy_summary.rm_summary = rm_summary;
122            tokio::fs::hard_link(src, dst)
123                .await
124                .with_context(|| format!("failed to hard link {:?} to {:?}", src, dst))
125                .map_err(|err| Error::new(err, link_summary))?;
126        }
127    }
128    prog_track.hard_links_created.inc();
129    link_summary.hard_links_created = 1;
130    Ok(link_summary)
131}
132
133#[instrument(skip(prog_track))]
134#[async_recursion]
135pub async fn link(
136    prog_track: &'static progress::Progress,
137    cwd: &std::path::Path,
138    src: &std::path::Path,
139    dst: &std::path::Path,
140    update: &Option<std::path::PathBuf>,
141    settings: &Settings,
142    mut is_fresh: bool,
143) -> Result<Summary, Error> {
144    let _prog_guard = prog_track.ops.guard();
145    tracing::debug!("reading source metadata");
146    let src_metadata = tokio::fs::symlink_metadata(src)
147        .await
148        .with_context(|| format!("failed reading metadata from {:?}", &src))
149        .map_err(|err| Error::new(err, Default::default()))?;
150    let update_metadata_opt = match update {
151        Some(update) => {
152            tracing::debug!("reading 'update' metadata");
153            let update_metadata_res = tokio::fs::symlink_metadata(update).await;
154            match update_metadata_res {
155                Ok(update_metadata) => Some(update_metadata),
156                Err(error) => {
157                    if error.kind() == std::io::ErrorKind::NotFound {
158                        if settings.update_exclusive {
159                            // the path is missing from update, we're done
160                            return Ok(Default::default());
161                        }
162                        None
163                    } else {
164                        return Err(Error::new(
165                            anyhow!("failed reading metadata from {:?}", &update),
166                            Default::default(),
167                        ));
168                    }
169                }
170            }
171        }
172        None => None,
173    };
174    if let Some(update_metadata) = update_metadata_opt.as_ref() {
175        let update = update.as_ref().unwrap();
176        if !copy::is_file_type_same(&src_metadata, update_metadata) {
177            // file type changed, just copy the updated one
178            tracing::debug!(
179                "link: file type of {:?} ({:?}) and {:?} ({:?}) differs - copying from update",
180                src,
181                src_metadata.file_type(),
182                update,
183                update_metadata.file_type()
184            );
185            let copy_summary = copy::copy(
186                prog_track,
187                update,
188                dst,
189                &settings.copy_settings,
190                &RLINK_PRESERVE_SETTINGS,
191                is_fresh,
192            )
193            .await
194            .map_err(|err| {
195                let copy_summary = err.summary;
196                let link_summary = Summary {
197                    copy_summary,
198                    ..Default::default()
199                };
200                Error::new(err.source, link_summary)
201            })?;
202            return Ok(Summary {
203                copy_summary,
204                ..Default::default()
205            });
206        }
207        if update_metadata.is_file() {
208            // check if the file is unchanged and if so hard-link, otherwise copy from the updated one
209            if filecmp::metadata_equal(&settings.update_compare, &src_metadata, update_metadata) {
210                tracing::debug!("no change, hard link 'src'");
211                return hard_link_helper(prog_track, src, &src_metadata, dst, settings).await;
212            }
213            tracing::debug!(
214                "link: {:?} metadata has changed, copying from {:?}",
215                src,
216                update
217            );
218            return Ok(Summary {
219                copy_summary: copy::copy_file(
220                    prog_track,
221                    update,
222                    dst,
223                    &settings.copy_settings,
224                    &RLINK_PRESERVE_SETTINGS,
225                    is_fresh,
226                )
227                .await
228                .map_err(|err| {
229                    let copy_summary = err.summary;
230                    let link_summary = Summary {
231                        copy_summary,
232                        ..Default::default()
233                    };
234                    Error::new(err.source, link_summary)
235                })?,
236                ..Default::default()
237            });
238        }
239        if update_metadata.is_symlink() {
240            tracing::debug!("'update' is a symlink so just symlink that");
241            // use "copy" function to handle the overwrite logic
242            let copy_summary = copy::copy(
243                prog_track,
244                update,
245                dst,
246                &settings.copy_settings,
247                &RLINK_PRESERVE_SETTINGS,
248                is_fresh,
249            )
250            .await
251            .map_err(|err| {
252                let copy_summary = err.summary;
253                let link_summary = Summary {
254                    copy_summary,
255                    ..Default::default()
256                };
257                Error::new(err.source, link_summary)
258            })?;
259            return Ok(Summary {
260                copy_summary,
261                ..Default::default()
262            });
263        }
264    } else {
265        // update hasn't been specified, if this is a file just hard-link the source or symlink if it's a symlink
266        tracing::debug!("no 'update' specified");
267        if src_metadata.is_file() {
268            return hard_link_helper(prog_track, src, &src_metadata, dst, settings).await;
269        }
270        if src_metadata.is_symlink() {
271            tracing::debug!("'src' is a symlink so just symlink that");
272            // use "copy" function to handle the overwrite logic
273            let copy_summary = copy::copy(
274                prog_track,
275                src,
276                dst,
277                &settings.copy_settings,
278                &RLINK_PRESERVE_SETTINGS,
279                is_fresh,
280            )
281            .await
282            .map_err(|err| {
283                let copy_summary = err.summary;
284                let link_summary = Summary {
285                    copy_summary,
286                    ..Default::default()
287                };
288                Error::new(err.source, link_summary)
289            })?;
290            return Ok(Summary {
291                copy_summary,
292                ..Default::default()
293            });
294        }
295    }
296    if !src_metadata.is_dir() {
297        return Err(Error::new(
298            anyhow!(
299                "copy: {:?} -> {:?} failed, unsupported src file type: {:?}",
300                src,
301                dst,
302                src_metadata.file_type()
303            ),
304            Default::default(),
305        ));
306    }
307    assert!(update_metadata_opt.is_none() || update_metadata_opt.as_ref().unwrap().is_dir());
308    tracing::debug!("process contents of 'src' directory");
309    let mut src_entries = tokio::fs::read_dir(src)
310        .await
311        .with_context(|| format!("cannot open directory {src:?} for reading"))
312        .map_err(|err| Error::new(err, Default::default()))?;
313    let copy_summary = {
314        if let Err(error) = tokio::fs::create_dir(dst).await {
315            assert!(!is_fresh, "unexpected error creating directory: {:?}", &dst);
316            if settings.copy_settings.overwrite && error.kind() == std::io::ErrorKind::AlreadyExists
317            {
318                // check if the destination is a directory - if so, leave it
319                //
320                // N.B. the permissions may prevent us from writing to it but the alternative is to open up the directory
321                // while we're writing to it which isn't safe
322                let dst_metadata = tokio::fs::metadata(dst)
323                    .await
324                    .with_context(|| format!("failed reading metadata from {:?}", &dst))
325                    .map_err(|err| Error::new(err, Default::default()))?;
326                if dst_metadata.is_dir() {
327                    tracing::debug!("'dst' is a directory, leaving it as is");
328                    CopySummary {
329                        directories_unchanged: 1,
330                        ..Default::default()
331                    }
332                } else {
333                    tracing::info!("'dst' is not a directory, removing and creating a new one");
334                    let mut copy_summary = CopySummary::default();
335                    let rm_summary = rm::rm(
336                        prog_track,
337                        dst,
338                        &rm::Settings {
339                            fail_early: settings.copy_settings.fail_early,
340                        },
341                    )
342                    .await
343                    .map_err(|err| {
344                        let rm_summary = err.summary;
345                        copy_summary.rm_summary = rm_summary;
346                        Error::new(
347                            err.source,
348                            Summary {
349                                copy_summary,
350                                ..Default::default()
351                            },
352                        )
353                    })?;
354                    tokio::fs::create_dir(dst)
355                        .await
356                        .with_context(|| format!("cannot create directory {dst:?}"))
357                        .map_err(|err| {
358                            copy_summary.rm_summary = rm_summary;
359                            Error::new(
360                                err,
361                                Summary {
362                                    copy_summary,
363                                    ..Default::default()
364                                },
365                            )
366                        })?;
367                    // anything copied into dst may assume they don't need to check for conflicts
368                    is_fresh = true;
369                    CopySummary {
370                        rm_summary,
371                        directories_created: 1,
372                        ..Default::default()
373                    }
374                }
375            } else {
376                return Err(error)
377                    .with_context(|| format!("cannot create directory {dst:?}"))
378                    .map_err(|err| Error::new(err, Default::default()))?;
379            }
380        } else {
381            // new directory created, anything copied into dst may assume they don't need to check for conflicts
382            is_fresh = true;
383            CopySummary {
384                directories_created: 1,
385                ..Default::default()
386            }
387        }
388    };
389    let mut link_summary = Summary {
390        copy_summary,
391        ..Default::default()
392    };
393    let mut join_set = tokio::task::JoinSet::new();
394    let mut success = true;
395    // create a set of all the files we already processed
396    let mut processed_files = std::collections::HashSet::new();
397    // iterate through src entries and recursively call "link" on each one
398    while let Some(src_entry) = src_entries
399        .next_entry()
400        .await
401        .with_context(|| format!("failed traversing directory {:?}", &src))
402        .map_err(|err| Error::new(err, link_summary))?
403    {
404        // it's better to await the token here so that we throttle the syscalls generated by the
405        // DirEntry call. the ops-throttle will never cause a deadlock (unlike max-open-files limit)
406        // so it's safe to do here.
407        throttle::get_ops_token().await;
408        let cwd_path = cwd.to_owned();
409        let entry_path = src_entry.path();
410        let entry_name = entry_path.file_name().unwrap();
411        processed_files.insert(entry_name.to_owned());
412        let dst_path = dst.join(entry_name);
413        let update_path = update.as_ref().map(|s| s.join(entry_name));
414        let settings = *settings;
415        let do_link = || async move {
416            link(
417                prog_track,
418                &cwd_path,
419                &entry_path,
420                &dst_path,
421                &update_path,
422                &settings,
423                is_fresh,
424            )
425            .await
426        };
427        join_set.spawn(do_link());
428    }
429    // unfortunately ReadDir is opening file-descriptors and there's not a good way to limit this,
430    // one thing we CAN do however is to drop it as soon as we're done with it
431    drop(src_entries);
432    // only process update if the path was provided and the directory is present
433    if update_metadata_opt.is_some() {
434        let update = update.as_ref().unwrap();
435        tracing::debug!("process contents of 'update' directory");
436        let mut update_entries = tokio::fs::read_dir(update)
437            .await
438            .with_context(|| format!("cannot open directory {:?} for reading", &update))
439            .map_err(|err| Error::new(err, link_summary))?;
440        // iterate through update entries and for each one that's not present in src call "copy"
441        while let Some(update_entry) = update_entries
442            .next_entry()
443            .await
444            .with_context(|| format!("failed traversing directory {:?}", &update))
445            .map_err(|err| Error::new(err, link_summary))?
446        {
447            let entry_path = update_entry.path();
448            let entry_name = entry_path.file_name().unwrap();
449            if processed_files.contains(entry_name) {
450                // we already must have considered this file, skip it
451                continue;
452            }
453            tracing::debug!("found a new entry in the 'update' directory");
454            let dst_path = dst.join(entry_name);
455            let update_path = update.join(entry_name);
456            let settings = *settings;
457            let do_copy = || async move {
458                let copy_summary = copy::copy(
459                    prog_track,
460                    &update_path,
461                    &dst_path,
462                    &settings.copy_settings,
463                    &RLINK_PRESERVE_SETTINGS,
464                    is_fresh,
465                )
466                .await
467                .map_err(|err| {
468                    link_summary.copy_summary = link_summary.copy_summary + err.summary;
469                    Error::new(err.source, link_summary)
470                })?;
471                Ok(Summary {
472                    copy_summary,
473                    ..Default::default()
474                })
475            };
476            join_set.spawn(do_copy());
477        }
478        // unfortunately ReadDir is opening file-descriptors and there's not a good way to limit this,
479        // one thing we CAN do however is to drop it as soon as we're done with it
480        drop(update_entries);
481    }
482    while let Some(res) = join_set.join_next().await {
483        match res {
484            Ok(result) => match result {
485                Ok(summary) => link_summary = link_summary + summary,
486                Err(error) => {
487                    tracing::error!(
488                        "link: {:?} {:?} -> {:?} failed with: {:#}",
489                        src,
490                        update,
491                        dst,
492                        &error
493                    );
494                    if settings.copy_settings.fail_early {
495                        return Err(error);
496                    }
497                    success = false;
498                }
499            },
500            Err(error) => {
501                if settings.copy_settings.fail_early {
502                    return Err(Error::new(error.into(), link_summary));
503                }
504            }
505        }
506    }
507    if !success {
508        return Err(Error::new(
509            anyhow!("link: {:?} {:?} -> {:?} failed!", src, update, dst),
510            link_summary,
511        ))?;
512    }
513    tracing::debug!("set 'dst' directory metadata");
514    let preserve_metadata = if let Some(update_metadata) = update_metadata_opt.as_ref() {
515        update_metadata
516    } else {
517        &src_metadata
518    };
519    preserve::set_dir_metadata(&RLINK_PRESERVE_SETTINGS, preserve_metadata, dst)
520        .await
521        .map_err(|err| Error::new(err, link_summary))?;
522    Ok(link_summary)
523}
524
525#[cfg(test)]
526mod link_tests {
527    use crate::testutils;
528    use std::os::unix::fs::PermissionsExt;
529    use tracing_test::traced_test;
530
531    use super::*;
532
533    lazy_static! {
534        static ref PROGRESS: progress::Progress = progress::Progress::new();
535    }
536
537    fn common_settings(dereference: bool, overwrite: bool) -> Settings {
538        Settings {
539            copy_settings: CopySettings {
540                dereference,
541                fail_early: false,
542                overwrite,
543                overwrite_compare: filecmp::MetadataCmpSettings {
544                    size: true,
545                    mtime: true,
546                    ..Default::default()
547                },
548                chunk_size: 0,
549            },
550            update_compare: filecmp::MetadataCmpSettings {
551                size: true,
552                mtime: true,
553                ..Default::default()
554            },
555            update_exclusive: false,
556        }
557    }
558
559    #[tokio::test]
560    #[traced_test]
561    async fn test_basic_link() -> Result<(), anyhow::Error> {
562        let tmp_dir = testutils::setup_test_dir().await?;
563        let test_path = tmp_dir.as_path();
564        let summary = link(
565            &PROGRESS,
566            test_path,
567            &test_path.join("foo"),
568            &test_path.join("bar"),
569            &None,
570            &common_settings(false, false),
571            false,
572        )
573        .await?;
574        assert_eq!(summary.hard_links_created, 5);
575        assert_eq!(summary.copy_summary.files_copied, 0);
576        assert_eq!(summary.copy_summary.symlinks_created, 2);
577        assert_eq!(summary.copy_summary.directories_created, 3);
578        testutils::check_dirs_identical(
579            &test_path.join("foo"),
580            &test_path.join("bar"),
581            testutils::FileEqualityCheck::Timestamp,
582        )
583        .await?;
584        Ok(())
585    }
586
587    #[tokio::test]
588    #[traced_test]
589    async fn test_basic_link_update() -> Result<(), anyhow::Error> {
590        let tmp_dir = testutils::setup_test_dir().await?;
591        let test_path = tmp_dir.as_path();
592        let summary = link(
593            &PROGRESS,
594            test_path,
595            &test_path.join("foo"),
596            &test_path.join("bar"),
597            &Some(test_path.join("foo")),
598            &common_settings(false, false),
599            false,
600        )
601        .await?;
602        assert_eq!(summary.hard_links_created, 5);
603        assert_eq!(summary.copy_summary.files_copied, 0);
604        assert_eq!(summary.copy_summary.symlinks_created, 2);
605        assert_eq!(summary.copy_summary.directories_created, 3);
606        testutils::check_dirs_identical(
607            &test_path.join("foo"),
608            &test_path.join("bar"),
609            testutils::FileEqualityCheck::Timestamp,
610        )
611        .await?;
612        Ok(())
613    }
614
615    #[tokio::test]
616    #[traced_test]
617    async fn test_basic_link_empty_src() -> Result<(), anyhow::Error> {
618        let tmp_dir = testutils::setup_test_dir().await?;
619        tokio::fs::create_dir(tmp_dir.join("baz")).await?;
620        let test_path = tmp_dir.as_path();
621        let summary = link(
622            &PROGRESS,
623            test_path,
624            &test_path.join("baz"), // empty source
625            &test_path.join("bar"),
626            &Some(test_path.join("foo")),
627            &common_settings(false, false),
628            false,
629        )
630        .await?;
631        assert_eq!(summary.hard_links_created, 0);
632        assert_eq!(summary.copy_summary.files_copied, 5);
633        assert_eq!(summary.copy_summary.symlinks_created, 2);
634        assert_eq!(summary.copy_summary.directories_created, 3);
635        testutils::check_dirs_identical(
636            &test_path.join("foo"),
637            &test_path.join("bar"),
638            testutils::FileEqualityCheck::Timestamp,
639        )
640        .await?;
641        Ok(())
642    }
643
644    #[tokio::test]
645    #[traced_test]
646    async fn test_link_destination_permission_error_includes_root_cause(
647    ) -> Result<(), anyhow::Error> {
648        let tmp_dir = testutils::setup_test_dir().await?;
649        let test_path = tmp_dir.as_path();
650        let readonly_parent = test_path.join("readonly_dest");
651        tokio::fs::create_dir(&readonly_parent).await?;
652        tokio::fs::set_permissions(&readonly_parent, std::fs::Permissions::from_mode(0o555))
653            .await?;
654
655        let mut settings = common_settings(false, false);
656        settings.copy_settings.fail_early = true;
657
658        let result = link(
659            &PROGRESS,
660            test_path,
661            &test_path.join("foo"),
662            &readonly_parent.join("bar"),
663            &None,
664            &settings,
665            false,
666        )
667        .await;
668
669        // restore permissions to allow temporary directory cleanup
670        tokio::fs::set_permissions(&readonly_parent, std::fs::Permissions::from_mode(0o755))
671            .await?;
672
673        assert!(result.is_err(), "link into read-only parent should fail");
674        let err = result.unwrap_err();
675        let err_msg = format!("{:#}", err.source);
676        assert!(
677            err_msg.to_lowercase().contains("permission denied") || err_msg.contains("EACCES"),
678            "Error message must include permission denied text. Got: {}",
679            err_msg
680        );
681        Ok(())
682    }
683
684    pub async fn setup_update_dir(tmp_dir: &std::path::Path) -> Result<(), anyhow::Error> {
685        // update
686        // |- 0.txt
687        // |- bar
688        //    |- 1.txt
689        //    |- 2.txt -> ../0.txt
690        let foo_path = tmp_dir.join("update");
691        tokio::fs::create_dir(&foo_path).await.unwrap();
692        tokio::fs::write(foo_path.join("0.txt"), "0-new")
693            .await
694            .unwrap();
695        let bar_path = foo_path.join("bar");
696        tokio::fs::create_dir(&bar_path).await.unwrap();
697        tokio::fs::write(bar_path.join("1.txt"), "1-new")
698            .await
699            .unwrap();
700        tokio::fs::symlink("../1.txt", bar_path.join("2.txt"))
701            .await
702            .unwrap();
703        tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
704        Ok(())
705    }
706
707    #[tokio::test]
708    #[traced_test]
709    async fn test_link_update() -> Result<(), anyhow::Error> {
710        let tmp_dir = testutils::setup_test_dir().await?;
711        setup_update_dir(&tmp_dir).await?;
712        let test_path = tmp_dir.as_path();
713        let summary = link(
714            &PROGRESS,
715            test_path,
716            &test_path.join("foo"),
717            &test_path.join("bar"),
718            &Some(test_path.join("update")),
719            &common_settings(false, false),
720            false,
721        )
722        .await?;
723        assert_eq!(summary.hard_links_created, 2);
724        assert_eq!(summary.copy_summary.files_copied, 2);
725        assert_eq!(summary.copy_summary.symlinks_created, 3);
726        assert_eq!(summary.copy_summary.directories_created, 3);
727        // compare subset of src and dst
728        testutils::check_dirs_identical(
729            &test_path.join("foo").join("baz"),
730            &test_path.join("bar").join("baz"),
731            testutils::FileEqualityCheck::HardLink,
732        )
733        .await?;
734        // compare update and dst
735        testutils::check_dirs_identical(
736            &test_path.join("update"),
737            &test_path.join("bar"),
738            testutils::FileEqualityCheck::Timestamp,
739        )
740        .await?;
741        Ok(())
742    }
743
744    #[tokio::test]
745    #[traced_test]
746    async fn test_link_update_exclusive() -> Result<(), anyhow::Error> {
747        let tmp_dir = testutils::setup_test_dir().await?;
748        setup_update_dir(&tmp_dir).await?;
749        let test_path = tmp_dir.as_path();
750        let mut settings = common_settings(false, false);
751        settings.update_exclusive = true;
752        let summary = link(
753            &PROGRESS,
754            test_path,
755            &test_path.join("foo"),
756            &test_path.join("bar"),
757            &Some(test_path.join("update")),
758            &settings,
759            false,
760        )
761        .await?;
762        // we should end up with same directory as the update
763        // |- 0.txt
764        // |- bar
765        //    |- 1.txt
766        //    |- 2.txt -> ../0.txt
767        assert_eq!(summary.hard_links_created, 0);
768        assert_eq!(summary.copy_summary.files_copied, 2);
769        assert_eq!(summary.copy_summary.symlinks_created, 1);
770        assert_eq!(summary.copy_summary.directories_created, 2);
771        // compare update and dst
772        testutils::check_dirs_identical(
773            &test_path.join("update"),
774            &test_path.join("bar"),
775            testutils::FileEqualityCheck::Timestamp,
776        )
777        .await?;
778        Ok(())
779    }
780
781    async fn setup_test_dir_and_link() -> Result<std::path::PathBuf, anyhow::Error> {
782        let tmp_dir = testutils::setup_test_dir().await?;
783        let test_path = tmp_dir.as_path();
784        let summary = link(
785            &PROGRESS,
786            test_path,
787            &test_path.join("foo"),
788            &test_path.join("bar"),
789            &None,
790            &common_settings(false, false),
791            false,
792        )
793        .await?;
794        assert_eq!(summary.hard_links_created, 5);
795        assert_eq!(summary.copy_summary.symlinks_created, 2);
796        assert_eq!(summary.copy_summary.directories_created, 3);
797        Ok(tmp_dir)
798    }
799
800    #[tokio::test]
801    #[traced_test]
802    async fn test_link_overwrite_basic() -> Result<(), anyhow::Error> {
803        let tmp_dir = setup_test_dir_and_link().await?;
804        let output_path = &tmp_dir.join("bar");
805        {
806            // bar
807            // |- 0.txt
808            // |- bar  <---------------------------------------- REMOVE
809            //    |- 1.txt  <----------------------------------- REMOVE
810            //    |- 2.txt  <----------------------------------- REMOVE
811            //    |- 3.txt  <----------------------------------- REMOVE
812            // |- baz
813            //    |- 4.txt
814            //    |- 5.txt -> ../bar/2.txt <-------------------- REMOVE
815            //    |- 6.txt -> (absolute path) .../foo/bar/3.txt
816            let summary = rm::rm(
817                &PROGRESS,
818                &output_path.join("bar"),
819                &rm::Settings { fail_early: false },
820            )
821            .await?
822                + rm::rm(
823                    &PROGRESS,
824                    &output_path.join("baz").join("5.txt"),
825                    &rm::Settings { fail_early: false },
826                )
827                .await?;
828            assert_eq!(summary.files_removed, 3);
829            assert_eq!(summary.symlinks_removed, 1);
830            assert_eq!(summary.directories_removed, 1);
831        }
832        let summary = link(
833            &PROGRESS,
834            &tmp_dir,
835            &tmp_dir.join("foo"),
836            output_path,
837            &None,
838            &common_settings(false, true), // overwrite!
839            false,
840        )
841        .await?;
842        assert_eq!(summary.hard_links_created, 3);
843        assert_eq!(summary.copy_summary.symlinks_created, 1);
844        assert_eq!(summary.copy_summary.directories_created, 1);
845        testutils::check_dirs_identical(
846            &tmp_dir.join("foo"),
847            output_path,
848            testutils::FileEqualityCheck::Timestamp,
849        )
850        .await?;
851        Ok(())
852    }
853
854    #[tokio::test]
855    #[traced_test]
856    async fn test_link_update_overwrite_basic() -> Result<(), anyhow::Error> {
857        let tmp_dir = setup_test_dir_and_link().await?;
858        let output_path = &tmp_dir.join("bar");
859        {
860            // bar
861            // |- 0.txt
862            // |- bar  <---------------------------------------- REMOVE
863            //    |- 1.txt  <----------------------------------- REMOVE
864            //    |- 2.txt  <----------------------------------- REMOVE
865            //    |- 3.txt  <----------------------------------- REMOVE
866            // |- baz
867            //    |- 4.txt
868            //    |- 5.txt -> ../bar/2.txt <-------------------- REMOVE
869            //    |- 6.txt -> (absolute path) .../foo/bar/3.txt
870            let summary = rm::rm(
871                &PROGRESS,
872                &output_path.join("bar"),
873                &rm::Settings { fail_early: false },
874            )
875            .await?
876                + rm::rm(
877                    &PROGRESS,
878                    &output_path.join("baz").join("5.txt"),
879                    &rm::Settings { fail_early: false },
880                )
881                .await?;
882            assert_eq!(summary.files_removed, 3);
883            assert_eq!(summary.symlinks_removed, 1);
884            assert_eq!(summary.directories_removed, 1);
885        }
886        setup_update_dir(&tmp_dir).await?;
887        // update
888        // |- 0.txt
889        // |- bar
890        //    |- 1.txt
891        //    |- 2.txt -> ../0.txt
892        let summary = link(
893            &PROGRESS,
894            &tmp_dir,
895            &tmp_dir.join("foo"),
896            output_path,
897            &Some(tmp_dir.join("update")),
898            &common_settings(false, true), // overwrite!
899            false,
900        )
901        .await?;
902        assert_eq!(summary.hard_links_created, 1); // 3.txt
903        assert_eq!(summary.copy_summary.files_copied, 2); // 0.txt, 1.txt
904        assert_eq!(summary.copy_summary.symlinks_created, 2); // 2.txt, 5.txt
905        assert_eq!(summary.copy_summary.directories_created, 1);
906        // compare subset of src and dst
907        testutils::check_dirs_identical(
908            &tmp_dir.join("foo").join("baz"),
909            &tmp_dir.join("bar").join("baz"),
910            testutils::FileEqualityCheck::HardLink,
911        )
912        .await?;
913        // compare update and dst
914        testutils::check_dirs_identical(
915            &tmp_dir.join("update"),
916            &tmp_dir.join("bar"),
917            testutils::FileEqualityCheck::Timestamp,
918        )
919        .await?;
920        Ok(())
921    }
922
923    #[tokio::test]
924    #[traced_test]
925    async fn test_link_overwrite_hardlink_file() -> Result<(), anyhow::Error> {
926        let tmp_dir = setup_test_dir_and_link().await?;
927        let output_path = &tmp_dir.join("bar");
928        {
929            // bar
930            // |- 0.txt
931            // |- bar
932            //    |- 1.txt  <----------------------------------- REPLACE W/ FILE
933            //    |- 2.txt  <----------------------------------- REPLACE W/ SYMLINK
934            //    |- 3.txt  <----------------------------------- REPLACE W/ DIRECTORY
935            // |- baz    <-------------------------------------- REPLACE W/ FILE
936            //    |- ...
937            let bar_path = output_path.join("bar");
938            let summary = rm::rm(
939                &PROGRESS,
940                &bar_path.join("1.txt"),
941                &rm::Settings { fail_early: false },
942            )
943            .await?
944                + rm::rm(
945                    &PROGRESS,
946                    &bar_path.join("2.txt"),
947                    &rm::Settings { fail_early: false },
948                )
949                .await?
950                + rm::rm(
951                    &PROGRESS,
952                    &bar_path.join("3.txt"),
953                    &rm::Settings { fail_early: false },
954                )
955                .await?
956                + rm::rm(
957                    &PROGRESS,
958                    &output_path.join("baz"),
959                    &rm::Settings { fail_early: false },
960                )
961                .await?;
962            assert_eq!(summary.files_removed, 4);
963            assert_eq!(summary.symlinks_removed, 2);
964            assert_eq!(summary.directories_removed, 1);
965            // REPLACE with a file, a symlink, a directory and a file
966            tokio::fs::write(bar_path.join("1.txt"), "1-new")
967                .await
968                .unwrap();
969            tokio::fs::symlink("../0.txt", bar_path.join("2.txt"))
970                .await
971                .unwrap();
972            tokio::fs::create_dir(&bar_path.join("3.txt"))
973                .await
974                .unwrap();
975            tokio::fs::write(&output_path.join("baz"), "baz")
976                .await
977                .unwrap();
978        }
979        let summary = link(
980            &PROGRESS,
981            &tmp_dir,
982            &tmp_dir.join("foo"),
983            output_path,
984            &None,
985            &common_settings(false, true), // overwrite!
986            false,
987        )
988        .await?;
989        assert_eq!(summary.hard_links_created, 4);
990        assert_eq!(summary.copy_summary.files_copied, 0);
991        assert_eq!(summary.copy_summary.symlinks_created, 2);
992        assert_eq!(summary.copy_summary.directories_created, 1);
993        testutils::check_dirs_identical(
994            &tmp_dir.join("foo"),
995            &tmp_dir.join("bar"),
996            testutils::FileEqualityCheck::HardLink,
997        )
998        .await?;
999        Ok(())
1000    }
1001
1002    #[tokio::test]
1003    #[traced_test]
1004    async fn test_link_overwrite_error() -> Result<(), anyhow::Error> {
1005        let tmp_dir = setup_test_dir_and_link().await?;
1006        let output_path = &tmp_dir.join("bar");
1007        {
1008            // bar
1009            // |- 0.txt
1010            // |- bar
1011            //    |- 1.txt  <----------------------------------- REPLACE W/ FILE
1012            //    |- 2.txt  <----------------------------------- REPLACE W/ SYMLINK
1013            //    |- 3.txt  <----------------------------------- REPLACE W/ DIRECTORY
1014            // |- baz    <-------------------------------------- REPLACE W/ FILE
1015            //    |- ...
1016            let bar_path = output_path.join("bar");
1017            let summary = rm::rm(
1018                &PROGRESS,
1019                &bar_path.join("1.txt"),
1020                &rm::Settings { fail_early: false },
1021            )
1022            .await?
1023                + rm::rm(
1024                    &PROGRESS,
1025                    &bar_path.join("2.txt"),
1026                    &rm::Settings { fail_early: false },
1027                )
1028                .await?
1029                + rm::rm(
1030                    &PROGRESS,
1031                    &bar_path.join("3.txt"),
1032                    &rm::Settings { fail_early: false },
1033                )
1034                .await?
1035                + rm::rm(
1036                    &PROGRESS,
1037                    &output_path.join("baz"),
1038                    &rm::Settings { fail_early: false },
1039                )
1040                .await?;
1041            assert_eq!(summary.files_removed, 4);
1042            assert_eq!(summary.symlinks_removed, 2);
1043            assert_eq!(summary.directories_removed, 1);
1044            // REPLACE with a file, a symlink, a directory and a file
1045            tokio::fs::write(bar_path.join("1.txt"), "1-new")
1046                .await
1047                .unwrap();
1048            tokio::fs::symlink("../0.txt", bar_path.join("2.txt"))
1049                .await
1050                .unwrap();
1051            tokio::fs::create_dir(&bar_path.join("3.txt"))
1052                .await
1053                .unwrap();
1054            tokio::fs::write(&output_path.join("baz"), "baz")
1055                .await
1056                .unwrap();
1057        }
1058        let source_path = &tmp_dir.join("foo");
1059        // unreadable
1060        tokio::fs::set_permissions(
1061            &source_path.join("baz"),
1062            std::fs::Permissions::from_mode(0o000),
1063        )
1064        .await?;
1065        // bar
1066        // |- ...
1067        // |- baz <- NON READABLE
1068        match link(
1069            &PROGRESS,
1070            &tmp_dir,
1071            &tmp_dir.join("foo"),
1072            output_path,
1073            &None,
1074            &common_settings(false, true), // overwrite!
1075            false,
1076        )
1077        .await
1078        {
1079            Ok(_) => panic!("Expected the link to error!"),
1080            Err(error) => {
1081                tracing::info!("{}", &error);
1082                assert_eq!(error.summary.hard_links_created, 3);
1083                assert_eq!(error.summary.copy_summary.files_copied, 0);
1084                assert_eq!(error.summary.copy_summary.symlinks_created, 0);
1085                assert_eq!(error.summary.copy_summary.directories_created, 0);
1086                assert_eq!(error.summary.copy_summary.rm_summary.files_removed, 1);
1087                assert_eq!(error.summary.copy_summary.rm_summary.directories_removed, 1);
1088                assert_eq!(error.summary.copy_summary.rm_summary.symlinks_removed, 1);
1089            }
1090        }
1091        Ok(())
1092    }
1093}