Skip to content

Commit

Permalink
Overwrite file/directory instead of extending it with timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
sireliah committed Jul 19, 2023
1 parent 36a8a68 commit 8e697f3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 66 deletions.
8 changes: 6 additions & 2 deletions src/p2p/transfer/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ pub async fn unzip_stream(
let mut compat_reader = buf_reader.compat();

let task = spawn(async move {
let base_path = Path::new(&target_path);
let base_path = Path::new(&target_path)
.parent()
.unwrap_or(Path::new(&target_path));
let mut zip = ZipFileReader::new(&mut compat_reader);
let mut counter: usize = 0;
while !zip.finished() {
Expand All @@ -201,7 +203,9 @@ pub async fn unzip_stream(

if is_zip_dir(&path) {
debug!("Creating dir {:?}", path);
create_dir(path).await?;
if let Err(e) = create_dir(path).await {
warn!("Could not create directory: {:?}", e);
};
} else {
debug!("Creating file {:?}", path);
let mut file = File::create(&path).await?;
Expand Down
10 changes: 8 additions & 2 deletions src/p2p/transfer/protocol.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use async_std::fs::File;
use async_std::fs::OpenOptions;
use std::fmt;
use std::fs::remove_file;
use std::io::ErrorKind;
Expand Down Expand Up @@ -114,7 +114,13 @@ impl TransferPayload {
size: usize,
direction: &Direction,
) -> Result<usize, io::Error> {
let mut file = File::create(path).await?;
info!("Path: {}", path);
let mut file = OpenOptions::new()
.write(true)
.create(true)
.open(path)
.await
.expect("Opening failed!");
let mut counter: usize = 0;
let mut current_size: usize = 0;
loop {
Expand Down
72 changes: 10 additions & 62 deletions src/user_data/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::fs;
use std::io::{Error, ErrorKind, Read, Write};
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

use directories_next::{BaseDirs, UserDirs};
use serde::{Deserialize, Serialize};
Expand All @@ -11,52 +10,9 @@ use toml;
const DEFAULT_LISTEN_PORT: u16 = 36571;
const DEFAULT_FIREWALL_CHECKED: bool = false;

fn get_timestamp() -> u64 {
let now = SystemTime::now();
now.duration_since(UNIX_EPOCH)
.expect("Time failed")
.as_secs()
}

fn extend_dir(path: &Path, dir_name: &str, time: u64) -> PathBuf {
path.join(format!("{}_{}", dir_name, time))
}

fn extend_file(path: &Path, name: &str, time: u64) -> PathBuf {
let file_name_path = Path::new(name);
let extension: String = match file_name_path.extension() {
Some(v) => v.to_string_lossy().to_string(),
None => "".to_string(),
};
let basename = match file_name_path.file_stem() {
Some(v) => v.to_string_lossy().to_string(),
None => "file".to_string(),
};
let new_name = format!("{}_{}", basename, time);
let mut path = path.join(&new_name);
path.set_extension(extension);
path
}

fn generate_full_path<F>(path: &Path, name: &str, timestamp: F) -> Result<String, Error>
where
F: Fn() -> u64,
{
fn generate_full_path(path: &Path, name: &str) -> Result<String, Error> {
// If file or dir already exists in the target directory, create a path extended with a timestamp
let joined = path.join(&name);
let time = timestamp();

let joined = if joined.exists() {
info!("File already exists: {:?}", joined);
if joined.is_file() {
extend_file(path, name, time)
} else {
extend_dir(path, name, time)
}
} else {
joined
};

joined.into_os_string().into_string().or_else(|_| {
Err(Error::new(
ErrorKind::InvalidData,
Expand All @@ -69,12 +25,12 @@ pub fn get_target_path(name: &str, target_path: Option<&String>) -> Result<Strin
match target_path {
Some(path) => {
let path = Path::new(path);
generate_full_path(path, name, get_timestamp)
generate_full_path(path, name)
}
None => {
let config = UserConfig::new()?;
let dir = config.get_downloads_dir();
generate_full_path(dir.as_path(), name, get_timestamp)
generate_full_path(dir.as_path(), name)
}
}
}
Expand Down Expand Up @@ -200,21 +156,14 @@ impl UserConfig {

#[cfg(test)]
mod tests {
use crate::user_data::{extend_dir, generate_full_path};
use crate::user_data::generate_full_path;
use std::fs::{create_dir_all, File};
use std::path::Path;
use tempfile::tempdir;

#[test]
fn test_extend_dir_should_extend_name_with_timestamp() {
let result = extend_dir(Path::new("/home/user/"), "directory", 1111);

assert_eq!(result, Path::new("/home/user/directory_1111"))
}

#[test]
fn test_generate_full_file_path() {
let result = generate_full_path(Path::new("/home/user/"), "a-file.txt", || 1111).unwrap();
let result = generate_full_path(Path::new("/home/user/"), "a-file.txt").unwrap();

assert_eq!(result, "/home/user/a-file.txt");
}
Expand All @@ -226,15 +175,14 @@ mod tests {
let received_file_name = "a-file.txt";
File::create(path.join(received_file_name)).unwrap();

let result = generate_full_path(path, received_file_name, || 1111).unwrap();
let result = generate_full_path(path, received_file_name).unwrap();

assert_eq!(result, path.join("a-file_1111.txt").to_string_lossy());
assert_eq!(result, path.join("a-file.txt").to_string_lossy());
}

#[test]
fn test_generate_full_dir_path() {
let result =
generate_full_path(Path::new("/home/user/"), "some_directory", || 1111).unwrap();
let result = generate_full_path(Path::new("/home/user/"), "some_directory").unwrap();

assert_eq!(result, "/home/user/some_directory");
}
Expand All @@ -244,8 +192,8 @@ mod tests {
let path = dir.path();
let received_dir_name = "some_directory";
create_dir_all(path.join(received_dir_name)).unwrap();
let result = generate_full_path(path, received_dir_name, || 1111).unwrap();
let result = generate_full_path(path, received_dir_name).unwrap();

assert_eq!(result, path.join("some_directory_1111").to_string_lossy());
assert_eq!(result, path.join("some_directory").to_string_lossy());
}
}

0 comments on commit 8e697f3

Please sign in to comment.