remotefs_ssh/ssh/
backend.rs1#[cfg(feature = "libssh")]
5#[cfg_attr(docsrs, doc(cfg(feature = "libssh")))]
6mod libssh;
7
8#[cfg(feature = "libssh2")]
9#[cfg_attr(docsrs, doc(cfg(feature = "libssh2")))]
10mod libssh2;
11
12use std::io::{Read, Write};
13use std::path::{Path, PathBuf};
14
15use remotefs::fs::{Metadata, ReadStream, WriteStream};
16use remotefs::{File, RemoteResult};
17
18#[cfg(feature = "libssh")]
19#[cfg_attr(docsrs, doc(cfg(feature = "libssh")))]
20pub use self::libssh::LibSshSession;
21#[cfg(feature = "libssh2")]
22#[cfg_attr(docsrs, doc(cfg(feature = "libssh2")))]
23pub use self::libssh2::LibSsh2Session;
24use crate::SshOpts;
25
26pub trait SshSession: Sized {
30 type Sftp: Sftp;
31
32 fn connect(opts: &SshOpts) -> RemoteResult<Self>;
34
35 fn disconnect(&self) -> RemoteResult<()>;
37
38 fn banner(&self) -> RemoteResult<Option<String>>;
40
41 fn authenticated(&self) -> RemoteResult<bool>;
43
44 fn cmd<S>(&mut self, cmd: S) -> RemoteResult<(u32, String)>
46 where
47 S: AsRef<str>;
48
49 fn cmd_at<S>(&mut self, cmd: S, path: &Path) -> RemoteResult<(u32, String)>
51 where
52 S: AsRef<str>,
53 {
54 self.cmd(format!("cd \"{}\"; {}", path.display(), cmd.as_ref()))
55 }
56
57 fn scp_recv(&self, path: &Path) -> RemoteResult<Box<dyn Read + Send>>;
61
62 fn scp_send(
66 &self,
67 remote_path: &Path,
68 mode: i32,
69 size: u64,
70 times: Option<(u64, u64)>,
71 ) -> RemoteResult<Box<dyn Write + Send>>;
72
73 fn sftp(&self) -> RemoteResult<Self::Sftp>;
75}
76
77pub trait Sftp {
79 fn mkdir(&self, path: &Path, mode: i32) -> RemoteResult<()>;
81
82 fn open_read(&self, path: &Path) -> RemoteResult<ReadStream>;
84
85 fn open_write(&self, path: &Path, flags: WriteMode, mode: i32) -> RemoteResult<WriteStream>;
87
88 fn readdir<T>(&self, dirname: T) -> RemoteResult<Vec<File>>
90 where
91 T: AsRef<Path>;
92
93 #[allow(dead_code)]
95 fn realpath(&self, path: &Path) -> RemoteResult<PathBuf>;
96
97 fn rename(&self, src: &Path, dest: &Path) -> RemoteResult<()>;
99
100 fn rmdir(&self, path: &Path) -> RemoteResult<()>;
102
103 fn setstat(&self, path: &Path, metadata: Metadata) -> RemoteResult<()>;
105
106 fn stat(&self, filename: &Path) -> RemoteResult<File>;
108
109 fn symlink(&self, path: &Path, target: &Path) -> RemoteResult<()>;
111
112 fn unlink(&self, path: &Path) -> RemoteResult<()>;
114}
115
116#[derive(Debug, Clone, Copy, PartialEq, Eq)]
117pub enum WriteMode {
119 Append,
120 Truncate,
121}