Skip to content

Commit

Permalink
fix: generic remoteFs
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Oct 26, 2024
1 parent 8024879 commit 09d7c92
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 190 deletions.
2 changes: 1 addition & 1 deletion remotefs-fuse-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ remotefs-fuse = { path = "../remotefs-fuse", version = "0.1" }
remotefs-kube = { version = "0.4", optional = true }
remotefs-memory = "0.1"
remotefs-smb = { version = "0.3", optional = true }
remotefs-ssh = { version = "0.4", optional = true }
remotefs-ssh = { version = "0.5", optional = true }
remotefs-webdav = { version = "0.2", optional = true }
thiserror = "^1"
tokio = { version = "1", features = ["rt"] }
Expand Down
153 changes: 1 addition & 152 deletions remotefs-fuse-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ mod webdav;
use std::path::PathBuf;

use argh::FromArgs;
use remotefs::{fs::UnixPex, RemoteFs, RemoteResult};

#[cfg(feature = "aws-s3")]
use self::aws_s3::AwsS3Args;
Expand All @@ -30,6 +29,7 @@ use self::smb::SmbArgs;
use self::ssh::{ScpArgs, SftpArgs};
#[cfg(feature = "webdav")]
use self::webdav::WebdavArgs;
use crate::remotefs_wrapper::RemoteFsWrapper;

/// RemoteFS FUSE CLI
///
Expand Down Expand Up @@ -139,154 +139,3 @@ impl CliArgs {
}
}
}

enum RemoteFsWrapper {
#[cfg(feature = "aws-s3")]
Aws(remotefs_aws_s3::AwsS3Fs),
#[cfg(feature = "ftp")]
Ftp(remotefs_ftp::FtpFs),
#[cfg(feature = "kube")]
Kube(remotefs_kube::KubeMultiPodFs),
Memory(remotefs_memory::MemoryFs),
#[cfg(feature = "ssh")]
Scp(remotefs_ssh::ScpFs),
#[cfg(feature = "ssh")]
Sftp(remotefs_ssh::SftpFs),
#[cfg(feature = "smb")]
Smb(remotefs_smb::SmbFs),
#[cfg(feature = "webdav")]
Webdav(remotefs_webdav::WebDAVFs),
}

impl RemoteFsWrapper {
fn on_remote<F, T>(&mut self, f: F) -> T
where
F: FnOnce(&mut dyn RemoteFs) -> T,
{
match self {
#[cfg(feature = "aws-s3")]
RemoteFsWrapper::Aws(fs) => f(fs),
#[cfg(feature = "ftp")]
RemoteFsWrapper::Ftp(fs) => f(fs),
#[cfg(feature = "kube")]
RemoteFsWrapper::Kube(fs) => f(fs),
RemoteFsWrapper::Memory(fs) => f(fs),
#[cfg(feature = "ssh")]
RemoteFsWrapper::Scp(fs) => f(fs),
#[cfg(feature = "ssh")]
RemoteFsWrapper::Sftp(fs) => f(fs),
#[cfg(feature = "smb")]
RemoteFsWrapper::Smb(fs) => f(fs),
#[cfg(feature = "webdav")]
RemoteFsWrapper::Webdav(fs) => f(fs),
}
}
}

impl RemoteFs for RemoteFsWrapper {
fn append(
&mut self,
path: &std::path::Path,
metadata: &remotefs::fs::Metadata,
) -> RemoteResult<remotefs::fs::WriteStream> {
self.on_remote(|fs| fs.append(path, metadata))
}
fn append_file(
&mut self,
path: &std::path::Path,
metadata: &remotefs::fs::Metadata,
reader: Box<dyn std::io::Read + Send>,
) -> RemoteResult<u64> {
self.on_remote(|fs| fs.append_file(path, metadata, reader))
}
fn create_dir(&mut self, path: &std::path::Path, mode: UnixPex) -> RemoteResult<()> {
self.on_remote(|fs| fs.create_dir(path, mode))
}
fn change_dir(&mut self, dir: &std::path::Path) -> RemoteResult<PathBuf> {
self.on_remote(|fs| fs.change_dir(dir))
}
fn connect(&mut self) -> RemoteResult<remotefs::fs::Welcome> {
self.on_remote(|fs| fs.connect())
}
fn copy(&mut self, src: &std::path::Path, dest: &std::path::Path) -> RemoteResult<()> {
self.on_remote(|fs| fs.copy(src, dest))
}
fn create(
&mut self,
path: &std::path::Path,
metadata: &remotefs::fs::Metadata,
) -> RemoteResult<remotefs::fs::WriteStream> {
self.on_remote(|fs| fs.create(path, metadata))
}
fn create_file(
&mut self,
path: &std::path::Path,
metadata: &remotefs::fs::Metadata,
reader: Box<dyn std::io::Read + Send>,
) -> RemoteResult<u64> {
self.on_remote(|fs| fs.create_file(path, metadata, reader))
}
fn disconnect(&mut self) -> RemoteResult<()> {
self.on_remote(|fs| fs.disconnect())
}
fn exec(&mut self, cmd: &str) -> RemoteResult<(u32, String)> {
self.on_remote(|fs| fs.exec(cmd))
}
fn exists(&mut self, path: &std::path::Path) -> RemoteResult<bool> {
self.on_remote(|fs| fs.exists(path))
}
fn find(&mut self, search: &str) -> RemoteResult<Vec<remotefs::File>> {
self.on_remote(|fs| fs.find(search))
}
fn is_connected(&mut self) -> bool {
self.on_remote(|fs| fs.is_connected())
}

fn list_dir(&mut self, path: &std::path::Path) -> RemoteResult<Vec<remotefs::File>> {
self.on_remote(|fs| fs.list_dir(path))
}
fn mov(&mut self, src: &std::path::Path, dest: &std::path::Path) -> RemoteResult<()> {
self.on_remote(|fs| fs.mov(src, dest))
}
fn on_read(&mut self, readable: remotefs::fs::ReadStream) -> RemoteResult<()> {
self.on_remote(|fs| fs.on_read(readable))
}
fn on_written(&mut self, writable: remotefs::fs::WriteStream) -> RemoteResult<()> {
self.on_remote(|fs| fs.on_written(writable))
}
fn open(&mut self, path: &std::path::Path) -> RemoteResult<remotefs::fs::ReadStream> {
self.on_remote(|fs| fs.open(path))
}
fn open_file(
&mut self,
src: &std::path::Path,
dest: Box<dyn std::io::Write + Send>,
) -> RemoteResult<u64> {
self.on_remote(|fs| fs.open_file(src, dest))
}
fn pwd(&mut self) -> RemoteResult<PathBuf> {
self.on_remote(|fs| fs.pwd())
}
fn remove_dir(&mut self, path: &std::path::Path) -> RemoteResult<()> {
self.on_remote(|fs| fs.remove_dir(path))
}
fn remove_dir_all(&mut self, path: &std::path::Path) -> RemoteResult<()> {
self.on_remote(|fs| fs.remove_dir_all(path))
}
fn remove_file(&mut self, path: &std::path::Path) -> RemoteResult<()> {
self.on_remote(|fs| fs.remove_file(path))
}
fn setstat(
&mut self,
path: &std::path::Path,
metadata: remotefs::fs::Metadata,
) -> RemoteResult<()> {
self.on_remote(|fs| fs.setstat(path, metadata))
}
fn stat(&mut self, path: &std::path::Path) -> RemoteResult<remotefs::File> {
self.on_remote(|fs| fs.stat(path))
}
fn symlink(&mut self, path: &std::path::Path, target: &std::path::Path) -> RemoteResult<()> {
self.on_remote(|fs| fs.symlink(path, target))
}
}
1 change: 1 addition & 0 deletions remotefs-fuse-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod cli;
mod remotefs_wrapper;

use remotefs_fuse::{Mount, MountOption};

Expand Down
182 changes: 182 additions & 0 deletions remotefs-fuse-cli/src/remotefs_wrapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
use std::path::PathBuf;

use remotefs::fs::UnixPex;
use remotefs::{RemoteFs, RemoteResult};

/// Wrapper around the different [`RemoteFs`] implementations
#[allow(clippy::large_enum_variant)]
pub enum RemoteFsWrapper {
#[cfg(feature = "aws-s3")]
Aws(remotefs_aws_s3::AwsS3Fs),
#[cfg(feature = "ftp")]
Ftp(remotefs_ftp::FtpFs),
#[cfg(feature = "kube")]
Kube(remotefs_kube::KubeMultiPodFs),
Memory(remotefs_memory::MemoryFs),
#[cfg(feature = "ssh")]
Scp(remotefs_ssh::ScpFs),
#[cfg(feature = "ssh")]
Sftp(remotefs_ssh::SftpFs),
#[cfg(feature = "smb")]
Smb(remotefs_smb::SmbFs),
#[cfg(feature = "webdav")]
Webdav(remotefs_webdav::WebDAVFs),
}

impl RemoteFsWrapper {
/// Call the given closure with the appropriate [`RemoteFs`] implementation
fn on_remote<F, T>(&mut self, f: F) -> T
where
F: FnOnce(&mut dyn RemoteFs) -> T,
{
match self {
#[cfg(feature = "aws-s3")]
RemoteFsWrapper::Aws(fs) => f(fs),
#[cfg(feature = "ftp")]
RemoteFsWrapper::Ftp(fs) => f(fs),
#[cfg(feature = "kube")]
RemoteFsWrapper::Kube(fs) => f(fs),
RemoteFsWrapper::Memory(fs) => f(fs),
#[cfg(feature = "ssh")]
RemoteFsWrapper::Scp(fs) => f(fs),
#[cfg(feature = "ssh")]
RemoteFsWrapper::Sftp(fs) => f(fs),
#[cfg(feature = "smb")]
RemoteFsWrapper::Smb(fs) => f(fs),
#[cfg(feature = "webdav")]
RemoteFsWrapper::Webdav(fs) => f(fs),
}
}
}

impl RemoteFs for RemoteFsWrapper {
fn append(
&mut self,
path: &std::path::Path,
metadata: &remotefs::fs::Metadata,
) -> RemoteResult<remotefs::fs::WriteStream> {
self.on_remote(|fs| fs.append(path, metadata))
}

fn append_file(
&mut self,
path: &std::path::Path,
metadata: &remotefs::fs::Metadata,
reader: Box<dyn std::io::Read + Send>,
) -> RemoteResult<u64> {
self.on_remote(|fs| fs.append_file(path, metadata, reader))
}

fn create_dir(&mut self, path: &std::path::Path, mode: UnixPex) -> RemoteResult<()> {
self.on_remote(|fs| fs.create_dir(path, mode))
}

fn change_dir(&mut self, dir: &std::path::Path) -> RemoteResult<PathBuf> {
self.on_remote(|fs| fs.change_dir(dir))
}

fn connect(&mut self) -> RemoteResult<remotefs::fs::Welcome> {
self.on_remote(|fs| fs.connect())
}

fn copy(&mut self, src: &std::path::Path, dest: &std::path::Path) -> RemoteResult<()> {
self.on_remote(|fs| fs.copy(src, dest))
}

fn create(
&mut self,
path: &std::path::Path,
metadata: &remotefs::fs::Metadata,
) -> RemoteResult<remotefs::fs::WriteStream> {
self.on_remote(|fs| fs.create(path, metadata))
}

fn create_file(
&mut self,
path: &std::path::Path,
metadata: &remotefs::fs::Metadata,
reader: Box<dyn std::io::Read + Send>,
) -> RemoteResult<u64> {
self.on_remote(|fs| fs.create_file(path, metadata, reader))
}

fn disconnect(&mut self) -> RemoteResult<()> {
self.on_remote(|fs| fs.disconnect())
}

fn exec(&mut self, cmd: &str) -> RemoteResult<(u32, String)> {
self.on_remote(|fs| fs.exec(cmd))
}

fn exists(&mut self, path: &std::path::Path) -> RemoteResult<bool> {
self.on_remote(|fs| fs.exists(path))
}

fn find(&mut self, search: &str) -> RemoteResult<Vec<remotefs::File>> {
self.on_remote(|fs| fs.find(search))
}

fn is_connected(&mut self) -> bool {
self.on_remote(|fs| fs.is_connected())
}

fn list_dir(&mut self, path: &std::path::Path) -> RemoteResult<Vec<remotefs::File>> {
self.on_remote(|fs| fs.list_dir(path))
}

fn mov(&mut self, src: &std::path::Path, dest: &std::path::Path) -> RemoteResult<()> {
self.on_remote(|fs| fs.mov(src, dest))
}

fn on_read(&mut self, readable: remotefs::fs::ReadStream) -> RemoteResult<()> {
self.on_remote(|fs| fs.on_read(readable))
}

fn on_written(&mut self, writable: remotefs::fs::WriteStream) -> RemoteResult<()> {
self.on_remote(|fs| fs.on_written(writable))
}

fn open(&mut self, path: &std::path::Path) -> RemoteResult<remotefs::fs::ReadStream> {
self.on_remote(|fs| fs.open(path))
}

fn open_file(
&mut self,
src: &std::path::Path,
dest: Box<dyn std::io::Write + Send>,
) -> RemoteResult<u64> {
self.on_remote(|fs| fs.open_file(src, dest))
}

fn pwd(&mut self) -> RemoteResult<PathBuf> {
self.on_remote(|fs| fs.pwd())
}

fn remove_dir(&mut self, path: &std::path::Path) -> RemoteResult<()> {
self.on_remote(|fs| fs.remove_dir(path))
}

fn remove_dir_all(&mut self, path: &std::path::Path) -> RemoteResult<()> {
self.on_remote(|fs| fs.remove_dir_all(path))
}

fn remove_file(&mut self, path: &std::path::Path) -> RemoteResult<()> {
self.on_remote(|fs| fs.remove_file(path))
}

fn setstat(
&mut self,
path: &std::path::Path,
metadata: remotefs::fs::Metadata,
) -> RemoteResult<()> {
self.on_remote(|fs| fs.setstat(path, metadata))
}

fn stat(&mut self, path: &std::path::Path) -> RemoteResult<remotefs::File> {
self.on_remote(|fs| fs.stat(path))
}

fn symlink(&mut self, path: &std::path::Path, target: &std::path::Path) -> RemoteResult<()> {
self.on_remote(|fs| fs.symlink(path, target))
}
}
2 changes: 0 additions & 2 deletions remotefs-fuse/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ mod unix;
#[cfg_attr(docsrs, doc(cfg(windows)))]
mod windows;

use std::sync::{Arc, Mutex};

use remotefs::RemoteFs;

use crate::MountOption;
Expand Down
Loading

0 comments on commit 09d7c92

Please sign in to comment.