Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): store relative file paths in archive instead of absolute wh… #2523

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions autonomi/src/client/files/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use super::archive::{PrivateArchive, PrivateArchiveAccess};
use crate::client::data::{CostError, DataMapChunk, GetError, PutError};
use crate::client::files::get_relative_file_path_from_abs_file_and_folder_path;
use crate::client::utils::process_tasks_with_max_concurrency;
use crate::client::Client;
use ant_evm::EvmWallet;
use bytes::Bytes;
use std::{path::PathBuf, sync::LazyLock};

use super::archive::{PrivateArchive, PrivateArchiveAccess};

/// Number of files to upload in parallel.
///
/// Can be overridden by the `FILE_UPLOAD_BATCH_SIZE` environment variable.
Expand Down Expand Up @@ -124,7 +124,7 @@ impl Client {

// start upload of file in parallel
let mut upload_tasks = Vec::new();
for entry in walkdir::WalkDir::new(dir_path) {
for entry in walkdir::WalkDir::new(dir_path.clone()) {
let entry = entry?;
if !entry.file_type().is_file() {
continue;
Expand All @@ -148,8 +148,10 @@ impl Client {
);
let mut archive = PrivateArchive::new();
for (path, metadata, maybe_file) in uploads.into_iter() {
let rel_path = get_relative_file_path_from_abs_file_and_folder_path(&path, &dir_path);

match maybe_file {
Ok(file) => archive.add_file(path, file, metadata),
Ok(file) => archive.add_file(rel_path, file, metadata),
Err(err) => {
error!("Failed to upload file: {path:?}: {err:?}");
return Err(err);
Expand Down
12 changes: 7 additions & 5 deletions autonomi/src/client/files/fs_public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use super::archive_public::{ArchiveAddr, PublicArchive};
use super::fs::*;
use crate::client::data::DataAddr;
use crate::client::files::archive::Metadata;
use crate::client::files::get_relative_file_path_from_abs_file_and_folder_path;
use crate::client::utils::process_tasks_with_max_concurrency;
use crate::client::Client;
use ant_evm::EvmWallet;
use ant_networking::target_arch::{Duration, SystemTime};
use bytes::Bytes;
use std::path::PathBuf;

use super::archive_public::{ArchiveAddr, PublicArchive};
use super::fs::*;

impl Client {
/// Download file from network to local file system
pub async fn file_download_public(
Expand Down Expand Up @@ -69,7 +69,7 @@ impl Client {

// start upload of files in parallel
let mut upload_tasks = Vec::new();
for entry in walkdir::WalkDir::new(dir_path) {
for entry in walkdir::WalkDir::new(dir_path.clone()) {
let entry = entry?;
if !entry.file_type().is_file() {
continue;
Expand All @@ -93,8 +93,10 @@ impl Client {
);
let mut archive = PublicArchive::new();
for (path, metadata, maybe_file) in uploads.into_iter() {
let rel_path = get_relative_file_path_from_abs_file_and_folder_path(&path, &dir_path);

match maybe_file {
Ok(file) => archive.add_file(path, file, metadata),
Ok(file) => archive.add_file(rel_path, file, metadata),
Err(err) => {
error!("Failed to upload file: {path:?}: {err:?}");
return Err(err);
Expand Down
32 changes: 32 additions & 0 deletions autonomi/src/client/files/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "fs")]
use std::path::{Path, PathBuf};

pub mod archive;
pub mod archive_public;
#[cfg(feature = "fs")]
Expand All @@ -6,3 +9,32 @@ pub mod fs;
#[cfg(feature = "fs")]
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
pub mod fs_public;

#[cfg(feature = "fs")]
pub(crate) fn get_relative_file_path_from_abs_file_and_folder_path(
abs_file_pah: &Path,
abs_folder_path: &Path,
) -> PathBuf {
// check if the dir is a file
let is_file = abs_folder_path.is_file();

// could also be the file name
let dir_name = PathBuf::from(
abs_folder_path
.file_name()
.expect("Failed to get file/dir name"),
);

if is_file {
dir_name
} else {
let folder_prefix = abs_folder_path
.parent()
.unwrap_or(Path::new(""))
.to_path_buf();
abs_file_pah
.strip_prefix(folder_prefix)
.expect("Could not strip prefix path")
.to_path_buf()
}
}
Loading