gitbutler/gitbutler-app/src/writer.rs

89 lines
2.7 KiB
Rust
Raw Normal View History

2023-04-14 13:25:07 +03:00
use anyhow::{Context, Result};
2023-12-22 14:27:32 +03:00
use crate::lock;
pub struct DirWriter(lock::Dir);
2023-04-12 15:14:39 +03:00
2023-05-31 10:29:26 +03:00
impl DirWriter {
2023-12-22 14:27:32 +03:00
pub fn open<P: AsRef<std::path::Path>>(root: P) -> Result<Self, std::io::Error> {
lock::Dir::new(root).map(Self)
2023-04-13 14:37:46 +03:00
}
2023-04-12 15:14:39 +03:00
}
impl DirWriter {
2023-04-17 12:13:57 +03:00
fn write(&self, path: &str, contents: &[u8]) -> Result<()> {
2023-12-22 14:27:32 +03:00
self.0.batch(|root| {
let file_path = root.join(path);
let dir_path = file_path.parent().context("failed to get parent")?;
std::fs::create_dir_all(dir_path).context("failed to create directory")?;
std::fs::write(file_path, contents)?;
Ok(())
})?
2023-04-12 15:14:39 +03:00
}
pub fn remove(&self, path: &str) -> Result<()> {
2023-12-22 14:27:32 +03:00
self.0.batch(|root| {
let file_path = root.join(path);
if file_path.is_dir() {
match std::fs::remove_dir_all(file_path) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e.into()),
}
} else {
match std::fs::remove_file(file_path) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e.into()),
}
2023-07-01 11:30:00 +03:00
}
2023-12-22 14:27:32 +03:00
})?
2023-06-07 13:43:07 +03:00
}
pub fn write_usize(&self, path: &str, contents: &usize) -> Result<()> {
self.write_string(path, &contents.to_string())
}
pub fn write_u128(&self, path: &str, contents: &u128) -> Result<()> {
self.write_string(path, &contents.to_string())
}
pub fn write_bool(&self, path: &str, contents: &bool) -> Result<()> {
self.write_string(path, &contents.to_string())
}
pub fn write_string(&self, path: &str, contents: &str) -> Result<()> {
self.write(path, contents.as_bytes())
}
2023-04-12 15:14:39 +03:00
}
2023-06-05 17:12:25 +03:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_write() {
let root = tempfile::tempdir().unwrap();
2023-12-22 14:27:32 +03:00
let writer = DirWriter::open(root.path()).unwrap();
2023-06-05 17:12:25 +03:00
writer.write("foo/bar", b"baz").unwrap();
assert_eq!(
std::fs::read_to_string(root.path().join("foo/bar")).unwrap(),
"baz"
);
}
2023-06-07 13:43:07 +03:00
#[test]
fn test_remove() {
let root = tempfile::tempdir().unwrap();
2023-12-22 14:27:32 +03:00
let writer = DirWriter::open(root.path()).unwrap();
2023-06-07 13:43:07 +03:00
writer.remove("foo/bar").unwrap();
assert!(!root.path().join("foo/bar").exists());
writer.write("foo/bar", b"baz").unwrap();
writer.remove("foo/bar").unwrap();
assert!(!root.path().join("foo/bar").exists());
2023-07-01 11:30:00 +03:00
writer.write("parent/child", b"baz").unwrap();
writer.remove("parent").unwrap();
assert!(!root.path().join("parent").exists());
2023-06-07 13:43:07 +03:00
}
2023-06-05 17:12:25 +03:00
}