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                remote_copy_buffer_size: 0,
550            },
551            update_compare: filecmp::MetadataCmpSettings {
552                size: true,
553                mtime: true,
554                ..Default::default()
555            },
556            update_exclusive: false,
557        }
558    }
559
560    #[tokio::test]
561    #[traced_test]
562    async fn test_basic_link() -> Result<(), anyhow::Error> {
563        let tmp_dir = testutils::setup_test_dir().await?;
564        let test_path = tmp_dir.as_path();
565        let summary = link(
566            &PROGRESS,
567            test_path,
568            &test_path.join("foo"),
569            &test_path.join("bar"),
570            &None,
571            &common_settings(false, false),
572            false,
573        )
574        .await?;
575        assert_eq!(summary.hard_links_created, 5);
576        assert_eq!(summary.copy_summary.files_copied, 0);
577        assert_eq!(summary.copy_summary.symlinks_created, 2);
578        assert_eq!(summary.copy_summary.directories_created, 3);
579        testutils::check_dirs_identical(
580            &test_path.join("foo"),
581            &test_path.join("bar"),
582            testutils::FileEqualityCheck::Timestamp,
583        )
584        .await?;
585        Ok(())
586    }
587
588    #[tokio::test]
589    #[traced_test]
590    async fn test_basic_link_update() -> Result<(), anyhow::Error> {
591        let tmp_dir = testutils::setup_test_dir().await?;
592        let test_path = tmp_dir.as_path();
593        let summary = link(
594            &PROGRESS,
595            test_path,
596            &test_path.join("foo"),
597            &test_path.join("bar"),
598            &Some(test_path.join("foo")),
599            &common_settings(false, false),
600            false,
601        )
602        .await?;
603        assert_eq!(summary.hard_links_created, 5);
604        assert_eq!(summary.copy_summary.files_copied, 0);
605        assert_eq!(summary.copy_summary.symlinks_created, 2);
606        assert_eq!(summary.copy_summary.directories_created, 3);
607        testutils::check_dirs_identical(
608            &test_path.join("foo"),
609            &test_path.join("bar"),
610            testutils::FileEqualityCheck::Timestamp,
611        )
612        .await?;
613        Ok(())
614    }
615
616    #[tokio::test]
617    #[traced_test]
618    async fn test_basic_link_empty_src() -> Result<(), anyhow::Error> {
619        let tmp_dir = testutils::setup_test_dir().await?;
620        tokio::fs::create_dir(tmp_dir.join("baz")).await?;
621        let test_path = tmp_dir.as_path();
622        let summary = link(
623            &PROGRESS,
624            test_path,
625            &test_path.join("baz"), // empty source
626            &test_path.join("bar"),
627            &Some(test_path.join("foo")),
628            &common_settings(false, false),
629            false,
630        )
631        .await?;
632        assert_eq!(summary.hard_links_created, 0);
633        assert_eq!(summary.copy_summary.files_copied, 5);
634        assert_eq!(summary.copy_summary.symlinks_created, 2);
635        assert_eq!(summary.copy_summary.directories_created, 3);
636        testutils::check_dirs_identical(
637            &test_path.join("foo"),
638            &test_path.join("bar"),
639            testutils::FileEqualityCheck::Timestamp,
640        )
641        .await?;
642        Ok(())
643    }
644
645    #[tokio::test]
646    #[traced_test]
647    async fn test_link_destination_permission_error_includes_root_cause(
648    ) -> Result<(), anyhow::Error> {
649        let tmp_dir = testutils::setup_test_dir().await?;
650        let test_path = tmp_dir.as_path();
651        let readonly_parent = test_path.join("readonly_dest");
652        tokio::fs::create_dir(&readonly_parent).await?;
653        tokio::fs::set_permissions(&readonly_parent, std::fs::Permissions::from_mode(0o555))
654            .await?;
655
656        let mut settings = common_settings(false, false);
657        settings.copy_settings.fail_early = true;
658
659        let result = link(
660            &PROGRESS,
661            test_path,
662            &test_path.join("foo"),
663            &readonly_parent.join("bar"),
664            &None,
665            &settings,
666            false,
667        )
668        .await;
669
670        // restore permissions to allow temporary directory cleanup
671        tokio::fs::set_permissions(&readonly_parent, std::fs::Permissions::from_mode(0o755))
672            .await?;
673
674        assert!(result.is_err(), "link into read-only parent should fail");
675        let err = result.unwrap_err();
676        let err_msg = format!("{:#}", err.source);
677        assert!(
678            err_msg.to_lowercase().contains("permission denied") || err_msg.contains("EACCES"),
679            "Error message must include permission denied text. Got: {}",
680            err_msg
681        );
682        Ok(())
683    }
684
685    pub async fn setup_update_dir(tmp_dir: &std::path::Path) -> Result<(), anyhow::Error> {
686        // update
687        // |- 0.txt
688        // |- bar
689        //    |- 1.txt
690        //    |- 2.txt -> ../0.txt
691        let foo_path = tmp_dir.join("update");
692        tokio::fs::create_dir(&foo_path).await.unwrap();
693        tokio::fs::write(foo_path.join("0.txt"), "0-new")
694            .await
695            .unwrap();
696        let bar_path = foo_path.join("bar");
697        tokio::fs::create_dir(&bar_path).await.unwrap();
698        tokio::fs::write(bar_path.join("1.txt"), "1-new")
699            .await
700            .unwrap();
701        tokio::fs::symlink("../1.txt", bar_path.join("2.txt"))
702            .await
703            .unwrap();
704        tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
705        Ok(())
706    }
707
708    #[tokio::test]
709    #[traced_test]
710    async fn test_link_update() -> Result<(), anyhow::Error> {
711        let tmp_dir = testutils::setup_test_dir().await?;
712        setup_update_dir(&tmp_dir).await?;
713        let test_path = tmp_dir.as_path();
714        let summary = link(
715            &PROGRESS,
716            test_path,
717            &test_path.join("foo"),
718            &test_path.join("bar"),
719            &Some(test_path.join("update")),
720            &common_settings(false, false),
721            false,
722        )
723        .await?;
724        assert_eq!(summary.hard_links_created, 2);
725        assert_eq!(summary.copy_summary.files_copied, 2);
726        assert_eq!(summary.copy_summary.symlinks_created, 3);
727        assert_eq!(summary.copy_summary.directories_created, 3);
728        // compare subset of src and dst
729        testutils::check_dirs_identical(
730            &test_path.join("foo").join("baz"),
731            &test_path.join("bar").join("baz"),
732            testutils::FileEqualityCheck::HardLink,
733        )
734        .await?;
735        // compare update and dst
736        testutils::check_dirs_identical(
737            &test_path.join("update"),
738            &test_path.join("bar"),
739            testutils::FileEqualityCheck::Timestamp,
740        )
741        .await?;
742        Ok(())
743    }
744
745    #[tokio::test]
746    #[traced_test]
747    async fn test_link_update_exclusive() -> Result<(), anyhow::Error> {
748        let tmp_dir = testutils::setup_test_dir().await?;
749        setup_update_dir(&tmp_dir).await?;
750        let test_path = tmp_dir.as_path();
751        let mut settings = common_settings(false, false);
752        settings.update_exclusive = true;
753        let summary = link(
754            &PROGRESS,
755            test_path,
756            &test_path.join("foo"),
757            &test_path.join("bar"),
758            &Some(test_path.join("update")),
759            &settings,
760            false,
761        )
762        .await?;
763        // we should end up with same directory as the update
764        // |- 0.txt
765        // |- bar
766        //    |- 1.txt
767        //    |- 2.txt -> ../0.txt
768        assert_eq!(summary.hard_links_created, 0);
769        assert_eq!(summary.copy_summary.files_copied, 2);
770        assert_eq!(summary.copy_summary.symlinks_created, 1);
771        assert_eq!(summary.copy_summary.directories_created, 2);
772        // compare update and dst
773        testutils::check_dirs_identical(
774            &test_path.join("update"),
775            &test_path.join("bar"),
776            testutils::FileEqualityCheck::Timestamp,
777        )
778        .await?;
779        Ok(())
780    }
781
782    async fn setup_test_dir_and_link() -> Result<std::path::PathBuf, anyhow::Error> {
783        let tmp_dir = testutils::setup_test_dir().await?;
784        let test_path = tmp_dir.as_path();
785        let summary = link(
786            &PROGRESS,
787            test_path,
788            &test_path.join("foo"),
789            &test_path.join("bar"),
790            &None,
791            &common_settings(false, false),
792            false,
793        )
794        .await?;
795        assert_eq!(summary.hard_links_created, 5);
796        assert_eq!(summary.copy_summary.symlinks_created, 2);
797        assert_eq!(summary.copy_summary.directories_created, 3);
798        Ok(tmp_dir)
799    }
800
801    #[tokio::test]
802    #[traced_test]
803    async fn test_link_overwrite_basic() -> Result<(), anyhow::Error> {
804        let tmp_dir = setup_test_dir_and_link().await?;
805        let output_path = &tmp_dir.join("bar");
806        {
807            // bar
808            // |- 0.txt
809            // |- bar  <---------------------------------------- REMOVE
810            //    |- 1.txt  <----------------------------------- REMOVE
811            //    |- 2.txt  <----------------------------------- REMOVE
812            //    |- 3.txt  <----------------------------------- REMOVE
813            // |- baz
814            //    |- 4.txt
815            //    |- 5.txt -> ../bar/2.txt <-------------------- REMOVE
816            //    |- 6.txt -> (absolute path) .../foo/bar/3.txt
817            let summary = rm::rm(
818                &PROGRESS,
819                &output_path.join("bar"),
820                &rm::Settings { fail_early: false },
821            )
822            .await?
823                + rm::rm(
824                    &PROGRESS,
825                    &output_path.join("baz").join("5.txt"),
826                    &rm::Settings { fail_early: false },
827                )
828                .await?;
829            assert_eq!(summary.files_removed, 3);
830            assert_eq!(summary.symlinks_removed, 1);
831            assert_eq!(summary.directories_removed, 1);
832        }
833        let summary = link(
834            &PROGRESS,
835            &tmp_dir,
836            &tmp_dir.join("foo"),
837            output_path,
838            &None,
839            &common_settings(false, true), // overwrite!
840            false,
841        )
842        .await?;
843        assert_eq!(summary.hard_links_created, 3);
844        assert_eq!(summary.copy_summary.symlinks_created, 1);
845        assert_eq!(summary.copy_summary.directories_created, 1);
846        testutils::check_dirs_identical(
847            &tmp_dir.join("foo"),
848            output_path,
849            testutils::FileEqualityCheck::Timestamp,
850        )
851        .await?;
852        Ok(())
853    }
854
855    #[tokio::test]
856    #[traced_test]
857    async fn test_link_update_overwrite_basic() -> Result<(), anyhow::Error> {
858        let tmp_dir = setup_test_dir_and_link().await?;
859        let output_path = &tmp_dir.join("bar");
860        {
861            // bar
862            // |- 0.txt
863            // |- bar  <---------------------------------------- REMOVE
864            //    |- 1.txt  <----------------------------------- REMOVE
865            //    |- 2.txt  <----------------------------------- REMOVE
866            //    |- 3.txt  <----------------------------------- REMOVE
867            // |- baz
868            //    |- 4.txt
869            //    |- 5.txt -> ../bar/2.txt <-------------------- REMOVE
870            //    |- 6.txt -> (absolute path) .../foo/bar/3.txt
871            let summary = rm::rm(
872                &PROGRESS,
873                &output_path.join("bar"),
874                &rm::Settings { fail_early: false },
875            )
876            .await?
877                + rm::rm(
878                    &PROGRESS,
879                    &output_path.join("baz").join("5.txt"),
880                    &rm::Settings { fail_early: false },
881                )
882                .await?;
883            assert_eq!(summary.files_removed, 3);
884            assert_eq!(summary.symlinks_removed, 1);
885            assert_eq!(summary.directories_removed, 1);
886        }
887        setup_update_dir(&tmp_dir).await?;
888        // update
889        // |- 0.txt
890        // |- bar
891        //    |- 1.txt
892        //    |- 2.txt -> ../0.txt
893        let summary = link(
894            &PROGRESS,
895            &tmp_dir,
896            &tmp_dir.join("foo"),
897            output_path,
898            &Some(tmp_dir.join("update")),
899            &common_settings(false, true), // overwrite!
900            false,
901        )
902        .await?;
903        assert_eq!(summary.hard_links_created, 1); // 3.txt
904        assert_eq!(summary.copy_summary.files_copied, 2); // 0.txt, 1.txt
905        assert_eq!(summary.copy_summary.symlinks_created, 2); // 2.txt, 5.txt
906        assert_eq!(summary.copy_summary.directories_created, 1);
907        // compare subset of src and dst
908        testutils::check_dirs_identical(
909            &tmp_dir.join("foo").join("baz"),
910            &tmp_dir.join("bar").join("baz"),
911            testutils::FileEqualityCheck::HardLink,
912        )
913        .await?;
914        // compare update and dst
915        testutils::check_dirs_identical(
916            &tmp_dir.join("update"),
917            &tmp_dir.join("bar"),
918            testutils::FileEqualityCheck::Timestamp,
919        )
920        .await?;
921        Ok(())
922    }
923
924    #[tokio::test]
925    #[traced_test]
926    async fn test_link_overwrite_hardlink_file() -> Result<(), anyhow::Error> {
927        let tmp_dir = setup_test_dir_and_link().await?;
928        let output_path = &tmp_dir.join("bar");
929        {
930            // bar
931            // |- 0.txt
932            // |- bar
933            //    |- 1.txt  <----------------------------------- REPLACE W/ FILE
934            //    |- 2.txt  <----------------------------------- REPLACE W/ SYMLINK
935            //    |- 3.txt  <----------------------------------- REPLACE W/ DIRECTORY
936            // |- baz    <-------------------------------------- REPLACE W/ FILE
937            //    |- ...
938            let bar_path = output_path.join("bar");
939            let summary = rm::rm(
940                &PROGRESS,
941                &bar_path.join("1.txt"),
942                &rm::Settings { fail_early: false },
943            )
944            .await?
945                + rm::rm(
946                    &PROGRESS,
947                    &bar_path.join("2.txt"),
948                    &rm::Settings { fail_early: false },
949                )
950                .await?
951                + rm::rm(
952                    &PROGRESS,
953                    &bar_path.join("3.txt"),
954                    &rm::Settings { fail_early: false },
955                )
956                .await?
957                + rm::rm(
958                    &PROGRESS,
959                    &output_path.join("baz"),
960                    &rm::Settings { fail_early: false },
961                )
962                .await?;
963            assert_eq!(summary.files_removed, 4);
964            assert_eq!(summary.symlinks_removed, 2);
965            assert_eq!(summary.directories_removed, 1);
966            // REPLACE with a file, a symlink, a directory and a file
967            tokio::fs::write(bar_path.join("1.txt"), "1-new")
968                .await
969                .unwrap();
970            tokio::fs::symlink("../0.txt", bar_path.join("2.txt"))
971                .await
972                .unwrap();
973            tokio::fs::create_dir(&bar_path.join("3.txt"))
974                .await
975                .unwrap();
976            tokio::fs::write(&output_path.join("baz"), "baz")
977                .await
978                .unwrap();
979        }
980        let summary = link(
981            &PROGRESS,
982            &tmp_dir,
983            &tmp_dir.join("foo"),
984            output_path,
985            &None,
986            &common_settings(false, true), // overwrite!
987            false,
988        )
989        .await?;
990        assert_eq!(summary.hard_links_created, 4);
991        assert_eq!(summary.copy_summary.files_copied, 0);
992        assert_eq!(summary.copy_summary.symlinks_created, 2);
993        assert_eq!(summary.copy_summary.directories_created, 1);
994        testutils::check_dirs_identical(
995            &tmp_dir.join("foo"),
996            &tmp_dir.join("bar"),
997            testutils::FileEqualityCheck::HardLink,
998        )
999        .await?;
1000        Ok(())
1001    }
1002
1003    #[tokio::test]
1004    #[traced_test]
1005    async fn test_link_overwrite_error() -> Result<(), anyhow::Error> {
1006        let tmp_dir = setup_test_dir_and_link().await?;
1007        let output_path = &tmp_dir.join("bar");
1008        {
1009            // bar
1010            // |- 0.txt
1011            // |- bar
1012            //    |- 1.txt  <----------------------------------- REPLACE W/ FILE
1013            //    |- 2.txt  <----------------------------------- REPLACE W/ SYMLINK
1014            //    |- 3.txt  <----------------------------------- REPLACE W/ DIRECTORY
1015            // |- baz    <-------------------------------------- REPLACE W/ FILE
1016            //    |- ...
1017            let bar_path = output_path.join("bar");
1018            let summary = rm::rm(
1019                &PROGRESS,
1020                &bar_path.join("1.txt"),
1021                &rm::Settings { fail_early: false },
1022            )
1023            .await?
1024                + rm::rm(
1025                    &PROGRESS,
1026                    &bar_path.join("2.txt"),
1027                    &rm::Settings { fail_early: false },
1028                )
1029                .await?
1030                + rm::rm(
1031                    &PROGRESS,
1032                    &bar_path.join("3.txt"),
1033                    &rm::Settings { fail_early: false },
1034                )
1035                .await?
1036                + rm::rm(
1037                    &PROGRESS,
1038                    &output_path.join("baz"),
1039                    &rm::Settings { fail_early: false },
1040                )
1041                .await?;
1042            assert_eq!(summary.files_removed, 4);
1043            assert_eq!(summary.symlinks_removed, 2);
1044            assert_eq!(summary.directories_removed, 1);
1045            // REPLACE with a file, a symlink, a directory and a file
1046            tokio::fs::write(bar_path.join("1.txt"), "1-new")
1047                .await
1048                .unwrap();
1049            tokio::fs::symlink("../0.txt", bar_path.join("2.txt"))
1050                .await
1051                .unwrap();
1052            tokio::fs::create_dir(&bar_path.join("3.txt"))
1053                .await
1054                .unwrap();
1055            tokio::fs::write(&output_path.join("baz"), "baz")
1056                .await
1057                .unwrap();
1058        }
1059        let source_path = &tmp_dir.join("foo");
1060        // unreadable
1061        tokio::fs::set_permissions(
1062            &source_path.join("baz"),
1063            std::fs::Permissions::from_mode(0o000),
1064        )
1065        .await?;
1066        // bar
1067        // |- ...
1068        // |- baz <- NON READABLE
1069        match link(
1070            &PROGRESS,
1071            &tmp_dir,
1072            &tmp_dir.join("foo"),
1073            output_path,
1074            &None,
1075            &common_settings(false, true), // overwrite!
1076            false,
1077        )
1078        .await
1079        {
1080            Ok(_) => panic!("Expected the link to error!"),
1081            Err(error) => {
1082                tracing::info!("{}", &error);
1083                assert_eq!(error.summary.hard_links_created, 3);
1084                assert_eq!(error.summary.copy_summary.files_copied, 0);
1085                assert_eq!(error.summary.copy_summary.symlinks_created, 0);
1086                assert_eq!(error.summary.copy_summary.directories_created, 0);
1087                assert_eq!(error.summary.copy_summary.rm_summary.files_removed, 1);
1088                assert_eq!(error.summary.copy_summary.rm_summary.directories_removed, 1);
1089                assert_eq!(error.summary.copy_summary.rm_summary.symlinks_removed, 1);
1090            }
1091        }
1092        Ok(())
1093    }
1094}