absolutize path common functions

This commit is contained in:
veeso 2021-08-24 09:28:49 +02:00
parent 0cb9254e63
commit 214ec0c5a5
5 changed files with 26 additions and 58 deletions

View file

@ -28,6 +28,7 @@
use super::{FileTransfer, FileTransferError, FileTransferErrorType};
use crate::fs::{FsDirectory, FsEntry, FsFile, UnixPex};
use crate::utils::fmt::shadow_password;
use crate::utils::path;
// Includes
use std::convert::TryFrom;
@ -125,14 +126,7 @@ impl FtpFileTransfer {
None => None,
Some(p) => {
// Make abs path
let abs_path: PathBuf = match p.is_absolute() {
true => p.to_path_buf(),
false => {
let mut abs = wrkdir.to_path_buf();
abs.push(p);
abs
}
};
let abs_path: PathBuf = path::absolutize(wrkdir, p);
Some(Box::new(FsEntry::File(FsFile {
name: p
.file_name()

View file

@ -76,6 +76,21 @@ impl ScpFileTransfer {
p.to_path_buf()
}
/// ### absolutize
///
/// Absolutize target path if relative.
/// This also converts backslashes to slashes if relative
fn absolutize(wrkdir: &Path, target: &Path) -> PathBuf {
match target.is_absolute() {
true => target.to_path_buf(),
false => {
let mut p: PathBuf = wrkdir.to_path_buf();
p.push(target);
Self::resolve(p.as_path())
}
}
}
/// ### parse_ls_output
///
/// Parse a line of `ls -l` output and tokenize the output into a `FsEntry`
@ -506,14 +521,7 @@ impl FileTransfer for ScpFileTransfer {
match self.is_connected() {
true => {
let p: PathBuf = self.wrkdir.clone();
let remote_path: PathBuf = match dir.is_absolute() {
true => PathBuf::from(dir),
false => {
let mut p: PathBuf = PathBuf::from(".");
p.push(dir);
Self::resolve(p.as_path())
}
};
let remote_path: PathBuf = Self::absolutize(&Path::new("."), dir);
info!("Changing working directory to {}", remote_path.display());
// Change directory
match self.perform_shell_cmd_with_path(
@ -774,14 +782,7 @@ impl FileTransfer for ScpFileTransfer {
///
/// Stat file and return FsEntry
fn stat(&mut self, path: &Path) -> Result<FsEntry, FileTransferError> {
let path: PathBuf = match path.is_absolute() {
true => PathBuf::from(path),
false => {
let mut p: PathBuf = self.wrkdir.clone();
p.push(path);
Self::resolve(p.as_path())
}
};
let path: PathBuf = Self::absolutize(self.wrkdir.as_path(), path);
match self.is_connected() {
true => {
let p: PathBuf = self.wrkdir.clone();
@ -857,15 +858,7 @@ impl FileTransfer for ScpFileTransfer {
) -> Result<Box<dyn Write>, FileTransferError> {
match self.session.as_ref() {
Some(session) => {
let file_name: PathBuf = match file_name.is_absolute() {
true => PathBuf::from(file_name),
false => {
let mut p: PathBuf = self.wrkdir.clone();
p.push(file_name);
Self::resolve(p.as_path())
}
};
let file_name: PathBuf = Self::resolve(file_name.as_path());
let file_name: PathBuf = Self::absolutize(self.wrkdir.as_path(), file_name);
info!(
"Sending file {} to {}",
local.abs_path.display(),

View file

@ -39,6 +39,7 @@ use std::os::unix::fs::{MetadataExt, PermissionsExt};
// Locals
use crate::fs::{FsDirectory, FsEntry, FsFile, UnixPex};
use crate::utils::path;
/// ## HostErrorType
///
@ -803,15 +804,7 @@ impl Localhost {
///
/// Convert path to absolute path
fn to_abs_path(&self, p: &Path) -> PathBuf {
// Convert to abs path
match p.is_relative() {
true => {
let mut path: PathBuf = self.wrkdir.clone();
path.push(p);
path
}
false => PathBuf::from(p),
}
path::absolutize(self.wrkdir.as_path(), p)
}
}

View file

@ -25,6 +25,7 @@
use super::{ConfigClient, FileTransferActivity, LogLevel, LogRecord};
use crate::system::environment;
use crate::system::sshkey_storage::SshKeyStorage;
use crate::utils::path;
// Ext
use std::env;
use std::path::{Path, PathBuf};
@ -124,27 +125,13 @@ impl FileTransferActivity {
///
/// Convert a path to absolute according to local explorer
pub(super) fn local_to_abs_path(&self, path: &Path) -> PathBuf {
match path.is_relative() {
true => {
let mut d: PathBuf = self.local().wrkdir.clone();
d.push(path);
d
}
false => path.to_path_buf(),
}
path::absolutize(self.local().wrkdir.as_path(), path)
}
/// ### remote_to_abs_path
///
/// Convert a path to absolute according to remote explorer
pub(super) fn remote_to_abs_path(&self, path: &Path) -> PathBuf {
match path.is_relative() {
true => {
let mut wrkdir: PathBuf = self.remote().wrkdir.clone();
wrkdir.push(path);
wrkdir
}
false => path.to_path_buf(),
}
path::absolutize(self.remote().wrkdir.as_path(), path)
}
}

View file

@ -31,6 +31,7 @@ pub mod file;
pub mod fmt;
pub mod git;
pub mod parser;
pub mod path;
pub mod random;
pub mod ui;