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#[cfg(feature = "russh")]
12#[cfg_attr(docsrs, doc(cfg(feature = "russh")))]
13mod russh;
14
15use std::io::{Read, Write};
16use std::path::{Path, PathBuf};
17
18use remotefs::fs::{Metadata, ReadStream, WriteStream};
19use remotefs::{File, RemoteResult};
20
21#[cfg(feature = "libssh")]
22#[cfg_attr(docsrs, doc(cfg(feature = "libssh")))]
23pub use self::libssh::LibSshSession;
24#[cfg(feature = "libssh2")]
25#[cfg_attr(docsrs, doc(cfg(feature = "libssh2")))]
26pub use self::libssh2::LibSsh2Session;
27#[cfg(feature = "russh")]
28#[cfg_attr(docsrs, doc(cfg(feature = "russh")))]
29pub use self::russh::{NoCheckServerKey, RusshSession};
30use crate::SshOpts;
31
32pub trait SshSession: Sized {
36 type Sftp: Sftp;
37
38 fn connect(opts: &SshOpts) -> RemoteResult<Self>;
40
41 fn disconnect(&self) -> RemoteResult<()>;
43
44 fn banner(&self) -> RemoteResult<Option<String>>;
46
47 fn authenticated(&self) -> RemoteResult<bool>;
49
50 fn cmd<S>(&mut self, cmd: S) -> RemoteResult<(u32, String)>
52 where
53 S: AsRef<str>;
54
55 fn cmd_at<S>(&mut self, cmd: S, path: &Path) -> RemoteResult<(u32, String)>
57 where
58 S: AsRef<str>,
59 {
60 self.cmd(format!("cd \"{}\"; {}", path.display(), cmd.as_ref()))
61 }
62
63 fn scp_recv(&self, path: &Path) -> RemoteResult<Box<dyn Read + Send>>;
67
68 fn scp_send(
72 &self,
73 remote_path: &Path,
74 mode: i32,
75 size: u64,
76 times: Option<(u64, u64)>,
77 ) -> RemoteResult<Box<dyn Write + Send>>;
78
79 fn sftp(&self) -> RemoteResult<Self::Sftp>;
81}
82
83pub trait Sftp {
85 fn mkdir(&self, path: &Path, mode: i32) -> RemoteResult<()>;
87
88 fn open_read(&self, path: &Path) -> RemoteResult<ReadStream>;
90
91 fn open_write(&self, path: &Path, flags: WriteMode, mode: i32) -> RemoteResult<WriteStream>;
93
94 fn readdir<T>(&self, dirname: T) -> RemoteResult<Vec<File>>
96 where
97 T: AsRef<Path>;
98
99 #[allow(dead_code)]
101 fn realpath(&self, path: &Path) -> RemoteResult<PathBuf>;
102
103 fn rename(&self, src: &Path, dest: &Path) -> RemoteResult<()>;
105
106 fn rmdir(&self, path: &Path) -> RemoteResult<()>;
108
109 fn setstat(&self, path: &Path, metadata: Metadata) -> RemoteResult<()>;
111
112 fn stat(&self, filename: &Path) -> RemoteResult<File>;
114
115 fn symlink(&self, path: &Path, target: &Path) -> RemoteResult<()>;
117
118 fn unlink(&self, path: &Path) -> RemoteResult<()>;
120}
121
122#[derive(Debug, Clone, Copy, PartialEq, Eq)]
123pub enum WriteMode {
125 Append,
126 Truncate,
127}