subx_cli/core/
fs_util.rs

1//! Utility functions for filesystem operations with CIFS compatibility.
2//!
3//! Provides helpers to perform file copy operations that avoid POSIX metadata
4//! copy which may not be supported on CIFS (SMB) filesystems.
5
6use std::fs::File;
7use std::io::{self, copy};
8use std::path::Path;
9
10/// Copies file contents from `source` to `destination` without copying metadata.
11///
12/// This function opens the source file and creates/truncates the destination file,
13/// then copies the data stream. It avoids POSIX permission copy to maintain
14/// compatibility with CIFS filesystems where metadata operations may fail.
15///
16/// # Errors
17///
18/// Returns an `io::Error` if reading from source or writing to destination fails.
19pub fn copy_file_cifs_safe(source: &Path, destination: &Path) -> io::Result<u64> {
20    let mut src = File::open(source)?;
21    let mut dst = File::create(destination)?;
22    copy(&mut src, &mut dst)
23}
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28    use std::fs;
29    use tempfile::TempDir;
30
31    #[test]
32    fn test_copy_file_cifs_safe() -> io::Result<()> {
33        let temp = TempDir::new()?;
34        let src_path = temp.path().join("src.txt");
35        let dst_path = temp.path().join("dst.txt");
36        let content = b"hello cifs safe copy";
37        fs::write(&src_path, content)?;
38        let bytes = copy_file_cifs_safe(&src_path, &dst_path)?;
39        assert_eq!(bytes as usize, content.len());
40        let copied = fs::read(&dst_path)?;
41        assert_eq!(copied, content);
42        Ok(())
43    }
44}