Skip to main content

cc_sync_session/filesystem/
real.rs

1use std::fs;
2use std::path::Path;
3use std::time::SystemTime;
4use filetime::{set_file_mtime, FileTime};
5
6use super::{EntryMetadata, FileSystem, Result};
7
8#[derive(Debug, Clone)]
9pub struct RealFileSystem;
10
11impl RealFileSystem {
12    pub fn new() -> Self {
13        Self
14    }
15}
16
17impl FileSystem for RealFileSystem {
18    fn list_directory(&self, path: &Path) -> Result<Vec<EntryMetadata>> {
19        let mut results = Vec::new();
20        
21        let entries = match fs::read_dir(path) {
22            Ok(entries) => entries,
23            Err(e) => return Err(e.into()),
24        };
25        
26        for entry in entries {
27            let entry = match entry {
28                Ok(e) => e,
29                Err(_) => continue, // Skip entries we can't read
30            };
31            
32            let path = entry.path();
33            let metadata = match entry.metadata() {
34                Ok(m) => m,
35                Err(_) => continue, // Skip entries we can't get metadata for
36            };
37            
38            let modified = match metadata.modified() {
39                Ok(m) => m,
40                Err(_) => SystemTime::now(), // Use current time as fallback
41            };
42            
43            results.push(EntryMetadata {
44                path,
45                modified,
46                is_directory: metadata.is_dir(),
47            });
48        }
49        
50        Ok(results)
51    }
52    
53    fn get_metadata(&self, path: &Path) -> Result<EntryMetadata> {
54        let metadata = fs::metadata(path)?;
55        
56        Ok(EntryMetadata {
57            path: path.to_path_buf(),
58            modified: metadata.modified()?,
59            is_directory: metadata.is_dir(),
60        })
61    }
62    
63    fn copy_file(&self, from: &Path, to: &Path) -> Result<()> {
64        // Ensure parent directory exists
65        if let Some(parent) = to.parent() {
66            fs::create_dir_all(parent)?;
67        }
68        
69        fs::copy(from, to)?;
70        Ok(())
71    }
72    
73    fn create_directory(&self, path: &Path) -> Result<()> {
74        fs::create_dir_all(path)?;
75        Ok(())
76    }
77    
78    fn exists(&self, path: &Path) -> Result<bool> {
79        Ok(path.exists())
80    }
81    
82    fn set_modified_time(&self, path: &Path, time: SystemTime) -> Result<()> {
83        let file_time = FileTime::from(time);
84        set_file_mtime(path, file_time)?;
85        Ok(())
86    }
87}