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
use std::path::Path;
use anyhow::{bail, Context};
use crate::{local, Session};
impl Session {
/// Upload local files `local_paths` to the remote location `remote_parent_path`.
///
/// Requires `rsync` to be available locally and remotely.
///
/// If `remote_user` is specified, it will be used for the upload
/// (requires `sudo` available on the remote system).
///
/// Existing remote files will be replaced by new files. When uploading directories,
/// extraneous files will be deleted from destination directories.
pub async fn upload(
&mut self,
local_paths: impl IntoIterator<Item = impl AsRef<Path>>,
remote_parent_path: impl AsRef<Path>,
remote_user: Option<&str>,
) -> anyhow::Result<()> {
if !self
.fs
.metadata(remote_parent_path.as_ref())
.await?
.file_type()
.context("missing file type for remote_parent_path")?
.is_dir()
{
bail!(
"upload destination {:?} is not a directory",
remote_parent_path.as_ref()
);
}
let mut command = local::LocalCommand::new([
"rsync",
"--itemize-changes",
"--recursive",
"--links",
"--perms",
"--times",
"--compress",
"--delete",
])
.hide_command();
if let Some(remote_user) = remote_user {
if remote_user
.chars()
.any(|c| !(c.is_ascii_alphanumeric() || c == '.' || c == '_' || c == '-'))
{
bail!("unsafe user: {remote_user:?}");
}
command = command
.arg("--rsync-path")
.arg(format!("sudo --user {remote_user} rsync"));
}
for arg in local_paths {
command = command.arg(arg.as_ref().to_str().context("non-utf8 path")?);
}
if let Some(port) = &self.port {
command = command.args(["--rsh", &format!("ssh -p {port}")]);
}
let destination = if let Some(user) = &self.user {
format!("{}@{}", user, self.destination)
} else {
self.destination.clone()
};
command
.arg(format!(
"{}:{}",
destination,
remote_parent_path
.as_ref()
.to_str()
.context("non-utf8 path")?
))
.run()
.await?;
Ok(())
}
}