Fix canonicalization of remote files

This commit is contained in:
Leonard Steppy 2025-02-03 13:05:47 +01:00
parent ffd8f71f8b
commit 2eb37aa07a

View File

@ -15,7 +15,7 @@ use clap::{Parser, Subcommand, ValueEnum};
use lazy_regex::{lazy_regex, Lazy, Regex}; use lazy_regex::{lazy_regex, Lazy, Regex};
use server::{Server, ServerReference}; use server::{Server, ServerReference};
use std::cell::LazyCell; use std::cell::LazyCell;
use std::ffi::{OsStr, OsString}; use std::ffi::OsStr;
use std::hash::Hash; use std::hash::Hash;
use std::io::Write; use std::io::Write;
use std::iter::once; use std::iter::once;
@ -207,32 +207,35 @@ fn main() -> Result<(), String> {
match &file_server { match &file_server {
Some(file_server) => match &file_server.address { Some(file_server) => match &file_server.address {
ServerAddress::Ssh { ssh_address } => { ServerAddress::Ssh { ssh_address } => {
//canonicalize remote files //canonicalize remote files -> also makes sure they exist
//TODO only join when there are multiple paths files = files
let joined_files = osf!("{{")
+ files
.iter() .iter()
.map(|file| file.as_os_str()) .map(|file| {
.collect::<Vec<_>>() let output = ShellCmd::new("ssh")
.join(&OsString::from(","))
+ "}";
//TODO handle bad exit status
let realpath_output = ShellCmd::new("ssh")
.arg(ssh_address) .arg(ssh_address)
.arg(osf!("realpath ") + file_server.server_directory_path.join(joined_files)) .arg(osf!("realpath -e ") + file_server.server_directory_path.join(file))
.collect_output() .collect_full_output()
.map_err(|e| format!("failed to canonicalize files to upload: {e}"))?; .map_err(|e| format!("Failed to canonicalize files: {e}"))?;
files = realpath_output
if !output.status.success() {
Err(format!(
"Path doesn't match any files on file-server: {}",
file.to_string_lossy()
))?;
}
let denoted_files = output
.stdout .stdout
.split(|&b| b == b'\n') //split at line breaks .split(|&b| b == b'\n') //split at line breaks
.map(|bytes| PathBuf::from(OsStr::from_bytes(bytes))) .map(|bytes| PathBuf::from(OsStr::from_bytes(bytes)))
.collect(); .collect::<Vec<_>>();
log!(logger, debug, "canonical files: {files:?}");
for file in &files { Ok(denoted_files)
check_file_exists_on_server(file, ssh_address, &file_server.server_directory_path)?; })
} .collect::<Result<Vec<_>, String>>()?
.into_iter()
.flatten()
.collect();
} }
ServerAddress::Localhost => files ServerAddress::Localhost => files
.iter() .iter()
@ -565,6 +568,7 @@ where
Ok(()) Ok(())
} }
#[allow(dead_code)]
fn check_file_exists_on_server<P, S, D>( fn check_file_exists_on_server<P, S, D>(
path: P, path: P,
ssh_address: S, ssh_address: S,