-
Notifications
You must be signed in to change notification settings - Fork 27
/
channel_scp.rs
141 lines (129 loc) · 4.02 KB
/
channel_scp.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use crate::constant::{scp, ssh_msg_code, ssh_str};
use crate::data::Data;
use crate::error::{SshError, SshResult};
use crate::Channel;
use std::borrow::BorrowMut;
use std::path::{Path, PathBuf};
pub struct ChannelScp {
pub(crate) channel: Channel,
pub(crate) local_path: PathBuf,
}
impl ChannelScp {
pub(crate) fn open(channel: Channel) -> Self {
ChannelScp {
channel,
local_path: Default::default(),
}
}
pub(crate) fn send_str(&mut self, cmd: &str) -> SshResult<()> {
self.send_bytes(cmd.as_bytes())
}
pub(crate) fn send_end(&mut self) -> SshResult<()> {
self.send_bytes(&[scp::END])
}
pub(crate) fn send_bytes(&mut self, bytes: &[u8]) -> SshResult<()> {
let mut data = Data::new();
data.put_u8(ssh_msg_code::SSH_MSG_CHANNEL_DATA)
.put_u32(self.channel.server_channel_no)
.put_u8s(bytes);
let session = unsafe { &mut *self.channel.session };
session
.client
.as_mut()
.unwrap()
.write_data(data, Some(self.channel.window_size.borrow_mut()))
}
pub(crate) fn read_data(&mut self) -> SshResult<Vec<u8>> {
let mut vec = vec![];
loop {
if !vec.is_empty() {
break;
}
let session = unsafe { &mut *self.channel.session };
let results = session
.client
.as_mut()
.unwrap()
.read_data(Some(self.channel.window_size.borrow_mut()))?;
for mut result in results {
let message_code = result.get_u8();
match message_code {
ssh_msg_code::SSH_MSG_CHANNEL_DATA => {
let cc = result.get_u32();
if cc == self.channel.client_channel_no {
vec.extend(result.get_u8s())
}
}
ssh_msg_code::SSH_MSG_CHANNEL_CLOSE => {
let cc = result.get_u32();
if cc == self.channel.client_channel_no {
self.channel.remote_close = true;
self.channel.close()?;
return Ok(vec);
}
}
_ => self.channel.other(message_code, result)?,
}
}
}
Ok(vec)
}
pub(crate) fn exec_scp(&mut self, command: &str) -> SshResult<()> {
let mut data = Data::new();
data.put_u8(ssh_msg_code::SSH_MSG_CHANNEL_REQUEST)
.put_u32(self.channel.server_channel_no)
.put_str(ssh_str::EXEC)
.put_u8(true as u8)
.put_str(command);
self.channel
.get_session_mut()
.client
.as_mut()
.unwrap()
.write(data)
}
pub(crate) fn command_init(&self, remote_path: &str, arg: &str) -> String {
format!(
"{} {} {} {} {} {}",
ssh_str::SCP,
arg,
scp::QUIET,
scp::RECURSIVE,
scp::PRESERVE_TIMES,
remote_path
)
}
}
pub(crate) fn check_path(path: &Path) -> SshResult<()> {
if path.to_str().is_none() {
return Err(SshError::from("path is null."));
}
Ok(())
}
pub struct ScpFile {
pub(crate) modify_time: i64,
pub(crate) access_time: i64,
pub(crate) size: u64,
pub(crate) name: String,
pub(crate) is_dir: bool,
pub(crate) local_path: PathBuf,
}
impl ScpFile {
pub(crate) fn new() -> Self {
ScpFile {
modify_time: 0,
access_time: 0,
size: 0,
name: String::new(),
is_dir: false,
local_path: Default::default(),
}
}
pub(crate) fn join(&self, filename: &str) -> PathBuf {
if self.local_path.is_dir() {
self.local_path.join(filename)
} else {
self.local_path.clone()
}
}
}