cqlite_core/platform/
fs.rs1use crate::{error::Error, Result};
4use std::path::Path;
5use tokio::fs;
6
7#[derive(Debug)]
9pub struct FileSystem;
10
11impl FileSystem {
12 pub async fn new() -> Result<Self> {
14 Ok(Self)
15 }
16
17 pub async fn exists(&self, path: &Path) -> Result<bool> {
19 Ok(fs::metadata(path).await.is_ok())
20 }
21
22 pub async fn create_dir(&self, path: &Path) -> Result<()> {
24 fs::create_dir(path).await.map_err(Error::from)
25 }
26
27 pub async fn create_dir_all(&self, path: &Path) -> Result<()> {
29 fs::create_dir_all(path).await.map_err(Error::from)
30 }
31
32 pub async fn read_dir(&self, path: &Path) -> Result<fs::ReadDir> {
34 fs::read_dir(path).await.map_err(Error::from)
35 }
36
37 pub async fn remove_file(&self, path: &Path) -> Result<()> {
39 fs::remove_file(path).await.map_err(Error::from)
40 }
41
42 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 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 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 pub async fn read_file(&self, path: &Path) -> Result<Vec<u8>> {
60 fs::read(path).await.map_err(Error::from)
61 }
62
63 pub async fn write_file(&self, path: &Path, contents: &[u8]) -> Result<()> {
65 fs::write(path, contents).await.map_err(Error::from)
66 }
67
68 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 pub async fn metadata(&self, path: &Path) -> Result<std::fs::Metadata> {
76 fs::metadata(path).await.map_err(Error::from)
77 }
78}