Don't use scp on localhost

This commit is contained in:
Leonard Steppy 2026-08-31 21:27:06 +02:00
parent a4d7fd7506
commit ffc3c478d9
2 changed files with 77 additions and 38 deletions

View File

@ -18,7 +18,7 @@ use crate::os_string_builder::ReplaceWithOsStr;
use crate::server::{RelativeLocalPathAnker, ServerAddress};
use crate::shell_interface::{ScpParam, ServerCommand, ShellCommand, ShellInterface};
use clap::{Parser, Subcommand, ValueEnum};
use lazy_regex::{lazy_regex, Lazy, Regex};
use lazy_regex::{Lazy, Regex, lazy_regex};
use server::{Server, ServerReference};
use std::cell::LazyCell;
use std::ffi::OsString;
@ -486,12 +486,24 @@ where
for file_action in server_actions.actions {
match file_action.kind {
Action::Add | Action::Replace => {
//don't use scp on localhost
if file_server.is_none() && matches!(server.address, ServerAddress::Localhost) {
let file = file_action.file;
let dest = server_actions.working_directory.join(file_action.file_name);
fs::copy(&file, &dest).map_err(|e| {
format!(
"Failed to copy from {} to {}: {e}",
file.to_string_lossy(),
dest.to_string_lossy()
)
})?;
} else {
let source = match &file_server {
Some(file_server) => ScpParam::from((
file_server,
file_server.server_directory_path.join(&file_action.file),
)),
None => ScpParam::from(file_action.file.as_path()),
None => ScpParam::from(&file_action.file),
};
let destination = ScpParam::from((server, &server_actions.working_directory));
scp! {
@ -504,6 +516,7 @@ where
.into_result_with_error_logging(&logger)
.map_err(|e| format!("upload failure: {e}"))?;
}
}
Action::Delete => match &server.address {
ServerAddress::Ssh { ssh_address } => {
ssh! {
@ -649,24 +662,34 @@ where
for server in servers {
log!(logger, "Getting file from {}...", server.get_name());
let source = ScpParam::from((&server, server.server_directory_path.join(&file)));
let file_path = server.server_directory_path.join(&file);
let downloaded_file_path = download_directory.join(file_name);
if matches!(server.address, ServerAddress::Localhost) {
//no need to use scp on localhost
fs::copy(&file_path, &downloaded_file_path).map_err(|e| {
format!(
"failed to copy {} to {}: {e}",
file_path.to_string_lossy(),
downloaded_file_path.to_string_lossy()
)
})?;
} else {
scp! {
source: source.clone(),
destination: ScpParam::from(download_directory.as_path()),
source: ScpParam::from((&server, &file_path)),
destination: ScpParam::from(&download_directory),
}
.in_env(env!())
.run_logged(&logger)
.and_expect_success()
.into_result_with_error_logging(&logger)
.map_err(|e| format!("download failure: {e}"))?;
}
//open file in editor
let editor_command = shell_words::split(&editor)
.map_err(|e| format!("failed to parse editor command: {e}"))?
.into_iter()
.map(|part| {
part.replace_with_os_str(FILE_PLACEHOLDER, download_directory.join(file_name))
})
.map(|part| part.replace_with_os_str(FILE_PLACEHOLDER, &file_path))
.collect::<Vec<_>>();
ShellCommand::Editor(editor_command)
@ -676,10 +699,19 @@ where
.into_result_with_error_logging(&logger)
.map_err(|e| format!("failed to open file in editor: {e}"))?;
//upload file again
//upload file again; don't use scp on localhost
if matches!(server.address, ServerAddress::Localhost) {
fs::copy(&downloaded_file_path, &file_path).map_err(|e| {
format!(
"failed to copy {} to {}: {e}",
file_path.to_string_lossy(),
download_directory.to_string_lossy()
)
})?;
} else {
scp! {
source: ScpParam::from(download_directory.join(file_name).as_path()),
destination: source,
source: ScpParam::from(&downloaded_file_path),
destination: ScpParam::from((&server, &file_path)),
}
.in_env(env!())
.run_logged(&logger)
@ -687,6 +719,7 @@ where
.into_result_with_error_logging(&logger)
.map_err(|e| format!("failed to re-upload file: {e}"))?;
}
}
log!(logger, "Done!");
}

View File

@ -129,6 +129,12 @@ impl From<&Path> for ScpParam {
}
}
impl From<&PathBuf> for ScpParam {
fn from(value: &PathBuf) -> Self {
Self::from(value.as_path())
}
}
impl From<&ScpParam> for OsString {
fn from(value: &ScpParam) -> Self {
let mut builder = osf!();