From 49a02d977b46b08b95615c149404baf321d688ac Mon Sep 17 00:00:00 2001 From: Sreenath Krishnan Date: Mon, 30 Oct 2023 15:22:06 -0700 Subject: [PATCH] feat: Add hard_link_or_copy function to MartianRover --- martian/src/stage.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/martian/src/stage.rs b/martian/src/stage.rs index 0487a4d768..d91207aadb 100644 --- a/martian/src/stage.rs +++ b/martian/src/stage.rs @@ -494,6 +494,23 @@ impl MartianRover { Ok(()) } } + + /// Creates a hard link (falls back to copy) to the given MartianFileType file + /// within the rover's file directory + /// + /// # Arguments + /// + /// * `src` - The path to the MartianFileType file + /// + /// # Returns + /// + /// * The path of the newly created link + pub fn hard_link_or_copy(&self, src: &M) -> Result { + let stem = src.as_ref().file_name().expect("Could not get file name."); + let dst: M = self.make_path(stem); + std::fs::hard_link(src, &dst).or_else(|_| std::fs::copy(src, &dst).map(|_| ()))?; + Ok(dst) + } } /// Two different kinds of marian stages. `MainOnly` stages only have a