Skip to main content

cqlite_core/platform/
fs.rs

1//! File system abstraction
2
3use crate::{error::Error, Result};
4use std::path::Path;
5use tokio::fs;
6
7/// File system operations
8#[derive(Debug)]
9pub struct FileSystem;
10
11impl FileSystem {
12    /// Create a new file system
13    pub async fn new() -> Result<Self> {
14        Ok(Self)
15    }
16
17    /// Check if path exists
18    pub async fn exists(&self, path: &Path) -> Result<bool> {
19        Ok(fs::metadata(path).await.is_ok())
20    }
21
22    /// Create directory (single level)
23    pub async fn create_dir(&self, path: &Path) -> Result<()> {
24        fs::create_dir(path).await.map_err(Error::from)
25    }
26
27    /// Create directory (with all parent directories)
28    pub async fn create_dir_all(&self, path: &Path) -> Result<()> {
29        fs::create_dir_all(path).await.map_err(Error::from)
30    }
31
32    /// Read directory
33    pub async fn read_dir(&self, path: &Path) -> Result<fs::ReadDir> {
34        fs::read_dir(path).await.map_err(Error::from)
35    }
36
37    /// Remove file
38    pub async fn remove_file(&self, path: &Path) -> Result<()> {
39        fs::remove_file(path).await.map_err(Error::from)
40    }
41
42    /// Copy file
43    pub async fn copy(&self, from: &Path, to: &Path) -> Result<()> {
44        fs::copy(from, to).await.map_err(Error::from)?;
45        Ok(())
46    }
47
48    /// Create file
49    pub async fn create_file(&self, path: &Path) -> Result<tokio::fs::File> {
50        tokio::fs::File::create(path).await.map_err(Error::from)
51    }
52
53    /// Open file for reading
54    pub async fn open_file(&self, path: &Path) -> Result<tokio::fs::File> {
55        tokio::fs::File::open(path).await.map_err(Error::from)
56    }
57
58    /// Read file contents
59    pub async fn read_file(&self, path: &Path) -> Result<Vec<u8>> {
60        fs::read(path).await.map_err(Error::from)
61    }
62
63    /// Write file contents
64    pub async fn write_file(&self, path: &Path, contents: &[u8]) -> Result<()> {
65        fs::write(path, contents).await.map_err(Error::from)
66    }
67
68    /// Get file size
69    pub async fn file_size(&self, path: &Path) -> Result<u64> {
70        let metadata = fs::metadata(path).await.map_err(Error::from)?;
71        Ok(metadata.len())
72    }
73
74    /// Get file metadata
75    pub async fn metadata(&self, path: &Path) -> Result<std::fs::Metadata> {
76        fs::metadata(path).await.map_err(Error::from)
77    }
78}