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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//! Job specifications for the client
// (c) 2024 Ross Younger
use std::{ffi::OsStr, path::Path, str::FromStr};
use crate::os::{self, AbstractPlatform as _};
use crate::protocol::control::Direction;
/// Strips the optional user@ part off a hostname
fn hostname_of(user_at_host: &str) -> &str {
user_at_host.split_once('@').unwrap_or(("", user_at_host)).1
}
/// Returns the username from a user@host, if one was specified
fn username_of(user_at_host: &str) -> Option<&str> {
user_at_host.split_once('@').map(|tup| tup.0)
}
/// A file source or destination specified by the user
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct FileSpec {
/// The remote `[user@]host` for the file. This may be a hostname or an IP address.
/// It may also be a _hostname alias_ that matches a Host section in the user's ssh config file.
/// (In that case, the ssh config file must specify a HostName.)
///
/// If not present, this is a local file.
pub user_at_host: Option<String>,
/// Filename
///
/// If this is a destination, it might be a directory.
pub filename: String,
}
impl FileSpec {
/// Returns only the hostname part of the file, if any; the username is stripped.
pub(crate) fn hostname(&self) -> Option<&str> {
self.user_at_host.as_ref().map(|s| hostname_of(s))
}
/// Returns the username part of the filespec, if this is a remote filespec and a username was given.
/// Otherwise, returns None.
pub(crate) fn remote_user(&self) -> Option<&str> {
self.user_at_host.as_ref().and_then(|s| username_of(s))
}
}
impl FromStr for FileSpec {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.starts_with('[') {
// Assume raw IPv6 address [1:2:3::4]:File
match s.split_once("]:") {
Some((hostish, filename)) => Ok(Self {
// lose the leading bracket as well so it can be looked up as if a hostname
user_at_host: Some(hostish[1..].to_owned()),
filename: filename.into(),
}),
None => Ok(Self {
user_at_host: None,
filename: s.to_owned(),
}),
}
} else {
// If all we had to worry about was unix paths, life would be easier!
// A path could be Host:File, or a raw IPv4 address 1.2.3.4:File; or even just a filename.
// However, absolute paths on Windows hosts (remote or local)
// may be expressed as 'C:\dir\file' or as the UNC-style '\\?\C:\dir\file'.
// If there is more than one : in the path, that's easy: the first one delimits the remote user@host,
// so we only need to split once.
//
// The tricky situation is if there is precisely one colon in the string, which is a common case!
//
// On a Windows host, using OpenSSH, these all work:
// scp host:file c:\users\me\file
// scp host:file c:/users/me/file
// scp c:\users\me\file host:file
// scp c:/users/me/file host:file
//
// So we cannot rely on the presence of backslashes to identify a Windows path.
// Instead, we will add special treatment on Windows builds to detect local paths.
//
// Interestingly, UNC paths do not work as local files:
// scp host:file \\?\c:\users\me\file
// scp host:file \\.\c:\users\me\file
// Both fail with `ssh: Could not resolve hostname \\\\?\\c: No such host is known`
// so we won't worry about them.
if os::Platform::override_path_is_local(s) {
// Special case
Ok(Self {
user_at_host: None,
filename: s.to_owned(),
})
} else {
match s.split_once(':') {
Some((host, filename)) => Ok(Self {
user_at_host: Some(host.to_string()),
filename: filename.to_string(),
}),
None => Ok(Self {
user_at_host: None,
filename: s.to_owned(),
}),
}
}
}
}
}
impl std::fmt::Display for FileSpec {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(host) = &self.user_at_host {
write!(f, "{}:{}", host, self.filename)
} else {
write!(f, "{}", self.filename)
}
}
}
/// Details of a file copy job.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct CopyJobSpec {
pub(crate) source: FileSpec,
pub(crate) destination: FileSpec,
/// The `[user@]host` part of whichever of the source or destination contained one.
/// (There can be only one.)
pub(crate) user_at_host: String,
/// If set, we want to preserve times and permissions as far as possible.
pub(crate) preserve: bool,
/// Is this entry for a directory?
///
/// **This flag does not provide any information regarding recursion.** That is up to the caller to determine from context.
pub(crate) directory: bool,
/// If present, Unix-style mode bits to apply to the target.
/// (This currently only applies to directories.)
pub(crate) mode: Option<u32>,
}
impl CopyJobSpec {
/// standard constructor
pub(crate) fn try_new(
source: FileSpec,
destination: FileSpec,
preserve: bool,
directory: bool,
) -> anyhow::Result<Self> {
if !(source.user_at_host.is_none() ^ destination.user_at_host.is_none()) {
anyhow::bail!("One file argument must be remote");
}
let user_at_host = source
.user_at_host
.clone()
.unwrap_or_else(|| destination.user_at_host.clone().unwrap_or_default());
Ok(Self {
source,
destination,
user_at_host,
preserve,
directory,
mode: None,
})
}
#[allow(dead_code)] // used by tests and qcp-unsafe-tests
pub(crate) fn from_parts(
source: &str,
destination: &str,
preserve: bool,
directory: bool,
) -> anyhow::Result<Self> {
let source = FileSpec::from_str(source)?;
let destination = FileSpec::from_str(destination)?;
Self::try_new(source, destination, preserve, directory)
}
/// The hostname portion of whichever of the arguments contained one.
pub(crate) fn remote_host(&self) -> &str {
hostname_of(&self.user_at_host)
}
/// The username portion of whichever of the arguments was remote, if one contained a username.
#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
pub(crate) fn remote_user(&self) -> Option<&str> {
username_of(&self.user_at_host)
}
pub(crate) fn direction(&self) -> Direction {
if self.source.user_at_host.is_some() {
Direction::ServerToClient
} else {
Direction::ClientToServer
}
}
/// The display filename for a job spec. This is the source filename.
pub(crate) fn display_filename<'a>(&'a self) -> &'a OsStr {
let s: &'a str = &self.source.filename;
let p = Path::new(s);
p.file_name().unwrap_or_default()
}
}
#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod test {
type Res = anyhow::Result<()>;
use engineering_repr::EngineeringQuantity;
use pretty_assertions::assert_eq;
use crate::{CopyJobSpec, protocol::control::Direction, transport::ThroughputMode};
use super::FileSpec;
use std::str::FromStr;
#[test]
fn filename_no_host() -> Res {
let fs = FileSpec::from_str("/dir/file")?;
assert!(fs.user_at_host.is_none());
assert_eq!(fs.filename, "/dir/file");
Ok(())
}
#[test]
fn host_no_file() -> Res {
let fs = FileSpec::from_str("host:")?;
assert_eq!(fs.user_at_host.unwrap(), "host");
assert_eq!(fs.filename, "");
Ok(())
}
#[test]
fn host_and_file() -> Res {
let fs = FileSpec::from_str("host:file")?;
assert_eq!(fs.user_at_host.unwrap(), "host");
assert_eq!(fs.filename, "file");
Ok(())
}
#[test]
fn bare_ipv4() -> Res {
let fs = FileSpec::from_str("1.2.3.4:file")?;
assert_eq!(fs.user_at_host.unwrap(), "1.2.3.4");
assert_eq!(fs.filename, "file");
Ok(())
}
#[test]
fn bare_ipv6() -> Res {
let fs = FileSpec::from_str("[1:2:3:4::5]:file")?;
assert_eq!(fs.user_at_host.unwrap(), "1:2:3:4::5");
assert_eq!(fs.filename, "file");
Ok(())
}
#[test]
fn bare_ipv6_localhost() -> Res {
let fs = FileSpec::from_str("[::1]:file")?;
assert_eq!(fs.user_at_host.unwrap(), "::1");
assert_eq!(fs.filename, "file");
Ok(())
}
#[test]
fn not_really_ipv6() {
let spec = FileSpec::from_str("[1:2:3:4::5").unwrap();
assert_eq!(spec.user_at_host, None);
assert_eq!(spec.filename, "[1:2:3:4::5");
}
#[test]
fn size_is_kb_not_kib() {
// same mechanism that clap uses
let q = "1k".parse::<EngineeringQuantity<u64>>().unwrap();
assert_eq!(u64::from(q), 1000);
}
#[test]
fn direction() {
let js = CopyJobSpec::from_parts("server:file", "file", false, false).unwrap();
assert_eq!(js.direction(), Direction::ServerToClient);
assert_eq!(js.direction().server_mode(), ThroughputMode::Tx);
assert_eq!(js.direction().client_mode(), ThroughputMode::Rx);
let js = CopyJobSpec::from_parts("file", "server:file", false, false).unwrap();
assert_eq!(js.direction(), Direction::ClientToServer);
assert_eq!(js.direction().server_mode(), ThroughputMode::Rx);
assert_eq!(js.direction().client_mode(), ThroughputMode::Tx);
let dir = Direction::Both;
assert_eq!(dir.server_mode(), ThroughputMode::Both);
assert_eq!(dir.client_mode(), ThroughputMode::Both);
}
#[test]
fn display_filename() {
let js = CopyJobSpec::from_parts("server:somedir/file1", "otherdir/file2", false, false)
.unwrap();
assert_eq!(js.display_filename(), "file1");
}
#[test]
#[cfg(windows)]
fn windows_local_paths() {
// This test requires littertray@1.1.0
let remote_cases = &[
// Syntax: ("qcp job arg","expected path extracted from it")
("server:C:\\dir\\file", "C:\\dir\\file"),
("server:C:/dir/file", "C:/dir/file"),
("server:\\\\?\\C:\\dir\\file", "\\\\?\\C:\\dir\\file"),
];
for (arg, expected_path) in remote_cases {
let js = CopyJobSpec::from_parts(arg, "otherdir/file2", false, false)
.unwrap_or_else(|_| panic!("failed to create jobspec; input case is {arg}"));
assert_eq!(js.source.filename, *expected_path, "remove case is {arg}");
}
let local_cases = &[
// Local cases are tougher as the drive letter can be mistaken for a filename
("C:\\dir\\file", "C:\\dir\\file"),
// Windows scp supports forward slashes for local paths, so we'd better support them too.
("C:/dir/file", "C:/dir/file"),
// but we do not support UNC paths as local files, because the Windows build of scp does not support them.
];
for (arg, expected_path) in local_cases {
let js = CopyJobSpec::from_parts(arg, "server:otherdir/file2", false, false)
.unwrap_or_else(|_| panic!("failed to create jobspec; local case is {arg}"));
assert_eq!(js.source.filename, *expected_path, "input case is {arg}");
}
}
}