Skip to main content

vector_core/
files.rs

1//! Files an account keeps beside its database, on either target: the
2//! filesystem natively, OPFS on the web. Async because OPFS only answers
3//! through promises; natively the calls are the same std::fs ones as before.
4
5use std::path::{Path, PathBuf};
6
7pub async fn read(path: &Path) -> Result<Vec<u8>, String> {
8    #[cfg(target_arch = "wasm32")]
9    return crate::webfiles::read(path).await;
10    #[cfg(not(target_arch = "wasm32"))]
11    std::fs::read(path).map_err(|e| format!("Failed to read {}: {e}", path.display()))
12}
13
14/// Replace `path` whole: a reader never sees a partial file.
15pub async fn write(path: &Path, bytes: &[u8]) -> Result<(), String> {
16    #[cfg(target_arch = "wasm32")]
17    return crate::webfiles::write(path, bytes).await;
18    #[cfg(not(target_arch = "wasm32"))]
19    {
20        if let Some(dir) = path.parent() {
21            std::fs::create_dir_all(dir).map_err(|e| format!("Failed to create {}: {e}", dir.display()))?;
22        }
23        let name = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
24        let tmp = path.with_file_name(format!("{name}.tmp"));
25        std::fs::write(&tmp, bytes).map_err(|e| format!("Failed to write {}: {e}", path.display()))?;
26        std::fs::rename(&tmp, path).map_err(|e| format!("Failed to commit {}: {e}", path.display()))
27    }
28}
29
30/// Write a Vector Cache file: sealed natively, so it is never a plain picture on disk.
31pub async fn write_cache(path: &Path, bytes: &[u8]) -> Result<(), String> {
32    #[cfg(target_arch = "wasm32")]
33    return crate::webfiles::write(path, bytes).await;
34    #[cfg(not(target_arch = "wasm32"))]
35    {
36        if let Some(dir) = path.parent() {
37            std::fs::create_dir_all(dir).map_err(|e| format!("Failed to create {}: {e}", dir.display()))?;
38        }
39        crate::vcache::write(path, bytes).map_err(|e| format!("Failed to write {}: {e}", path.display()))
40    }
41}
42
43/// Read a Vector Cache file, sealed or from before sealing.
44pub async fn read_cache(path: &Path) -> Result<Vec<u8>, String> {
45    #[cfg(target_arch = "wasm32")]
46    return crate::webfiles::read(path).await;
47    #[cfg(not(target_arch = "wasm32"))]
48    crate::vcache::read(path).map(|b| b.to_vec()).map_err(|e| format!("Failed to read {}: {e}", path.display()))
49}
50
51pub async fn rename(from: &Path, to: &Path) -> Result<(), String> {
52    #[cfg(target_arch = "wasm32")]
53    {
54        let bytes = crate::webfiles::read(from).await?;
55        crate::webfiles::write(to, &bytes).await?;
56        crate::webfiles::remove(from).await;
57        Ok(())
58    }
59    #[cfg(not(target_arch = "wasm32"))]
60    std::fs::rename(from, to).map_err(|e| format!("Failed to move {}: {e}", from.display()))
61}
62
63pub async fn remove(path: &Path) {
64    #[cfg(target_arch = "wasm32")]
65    crate::webfiles::remove(path).await;
66    #[cfg(not(target_arch = "wasm32"))]
67    let _ = std::fs::remove_file(path);
68}
69
70/// The files directly inside `dir`; empty when it doesn't exist.
71pub async fn list(dir: &Path) -> Vec<PathBuf> {
72    #[cfg(target_arch = "wasm32")]
73    return crate::webfiles::list(dir).await.into_iter().map(|n| dir.join(n)).collect();
74    #[cfg(not(target_arch = "wasm32"))]
75    std::fs::read_dir(dir)
76        .map(|entries| entries.flatten().map(|e| e.path()).filter(|p| p.is_file()).collect())
77        .unwrap_or_default()
78}