-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
281 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,42 @@ | ||
mod error; | ||
#[cfg(target_family = "unix")] | ||
#[cfg_attr(docsrs, doc(cfg(target_family = "unix")))] | ||
mod unix; | ||
|
||
use std::path::{Path, PathBuf}; | ||
|
||
use remotefs::RemoteFs; | ||
|
||
pub use self::error::{DriverError, DriverResult}; | ||
|
||
/// Remote Filesystem Driver | ||
/// | ||
/// This driver takes a instance which implements the [`RemoteFs`] trait and mounts it to a local directory. | ||
/// | ||
/// The driver will use the [`fuser`](https://crates.io/crates/fuser) crate to mount the filesystem, on Unix systems, while | ||
/// it will use [dokan](https://crates.io/crates/dokan) on Windows. | ||
pub struct Driver { | ||
data_dir: PathBuf, | ||
#[cfg(target_family = "unix")] | ||
database: unix::InodeDb, | ||
remote: Box<dyn RemoteFs>, | ||
} | ||
|
||
impl From<Box<dyn RemoteFs>> for Driver { | ||
fn from(remote: Box<dyn RemoteFs>) -> Self { | ||
Self::new(remote) | ||
} | ||
} | ||
|
||
impl Driver { | ||
/// Create a new instance of the [`Driver`] providing a instance which implements the [`RemoteFs`] trait. | ||
/// | ||
/// The [`RemoteFs`] instance must be boxed. | ||
pub fn new(remote: Box<dyn RemoteFs>) -> Self { | ||
Self { remote } | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `data_dir` - A directory where inodes will be mapped. | ||
/// * `remote` - The instance which implements the [`RemoteFs`] trait. | ||
pub fn new(data_dir: &Path, remote: Box<dyn RemoteFs>) -> DriverResult<Self> { | ||
Ok(Self { | ||
data_dir: data_dir.to_path_buf(), | ||
#[cfg(target_family = "unix")] | ||
database: unix::InodeDb::load(&data_dir.join("inodes.json"))?, | ||
remote, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use thiserror::Error; | ||
|
||
pub type DriverResult<T> = Result<T, DriverError>; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum DriverError { | ||
#[cfg(target_family = "unix")] | ||
#[error("Inode DB error: {0}")] | ||
Inode(#[from] super::unix::InodeDbError), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use std::collections::HashMap; | ||
use std::path::{Path, PathBuf}; | ||
|
||
use thiserror::Error; | ||
|
||
/// Error type for InodeDb | ||
#[derive(Error, Debug)] | ||
pub enum InodeDbError { | ||
#[error("IO error: {0}")] | ||
IoError(#[from] std::io::Error), | ||
#[error("Serde error: {0}")] | ||
SerdeError(#[from] serde_json::Error), | ||
} | ||
|
||
pub type InodeDbResult<T> = Result<T, InodeDbError>; | ||
pub type Inode = u64; | ||
|
||
type Database = HashMap<Inode, PathBuf>; | ||
|
||
/// A database to map inodes to files | ||
/// | ||
/// The database is saved to a file when the instance is dropped | ||
#[derive(Debug, Default, Clone)] | ||
pub struct InodeDb { | ||
database: Database, | ||
path: PathBuf, | ||
} | ||
|
||
impl InodeDb { | ||
/// Load [`InodeDb`] from a file | ||
pub fn load(path: &Path) -> Result<Self, InodeDbError> { | ||
let data = std::fs::read_to_string(path)?; | ||
let database: Database = serde_json::from_str(&data)?; | ||
|
||
Ok(Self { | ||
database, | ||
path: path.to_path_buf(), | ||
}) | ||
} | ||
|
||
/// Check if the database contains an inode | ||
pub fn has(&self, inode: Inode) -> bool { | ||
self.database.contains_key(&inode) | ||
} | ||
|
||
/// Put a new inode into the database | ||
pub fn put(&mut self, inode: Inode, path: PathBuf) { | ||
self.database.insert(inode, path); | ||
} | ||
|
||
/// Get a path from an inode | ||
pub fn get(&self, inode: Inode) -> Option<&Path> { | ||
self.database.get(&inode).map(|x| x.as_path()) | ||
} | ||
|
||
/// Save [`InodeDb`] to a file | ||
fn save(&self) -> InodeDbResult<()> { | ||
let data = serde_json::to_string(&self.database)?; | ||
std::fs::write(&self.path, data)?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl Drop for InodeDb { | ||
fn drop(&mut self) { | ||
if let Err(err) = self.save() { | ||
error!("Failed to save InodeDb: {err}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.