Skip to content

Commit

Permalink
Merge pull request #50 from sireliah/overwrite-file
Browse files Browse the repository at this point in the history
Overwrite file/directory instead of extending it with timestamp
  • Loading branch information
sireliah authored Jul 20, 2023
2 parents 36a8a68 + dedb910 commit ede2c28
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 77 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());
}
}
7 changes: 6 additions & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use async_std::channel::{bounded, Receiver, Sender};
use async_std::sync::Mutex;
use hex;
use md5::{Digest, Md5};
use tempfile::{tempdir, TempDir};

use libp2p::{
core::transport::Transport, core::upgrade, identity, mplex, noise, tcp, PeerId, Swarm,
Expand Down Expand Up @@ -39,6 +40,7 @@ pub fn build_swarm() -> (
Sender<TransferCommand>,
Receiver<PeerEvent>,
Swarm<TransferBehaviour>,
TempDir,
) {
let (_, _) = bounded::<FileToSend>(1024 * 24);
let (command_sender, command_receiver) = bounded::<TransferCommand>(1024 * 24);
Expand All @@ -49,10 +51,12 @@ pub fn build_swarm() -> (

let command_receiver = Arc::new(Mutex::new(command_receiver));

let dir = tempdir().unwrap();

let transfer_behaviour = TransferBehaviour::new(
peer_sender.clone(),
command_receiver,
Some("/tmp/".to_string()),
Some(dir.path().to_string_lossy().to_string()),
);

let timeout = Duration::from_secs(60);
Expand Down Expand Up @@ -81,6 +85,7 @@ pub fn build_swarm() -> (
command_sender,
peer_receiver,
Swarm::new(transport, transfer_behaviour, local_peer_id),
dir, // return tmp dir to test function; after value is dropped, directory will be cleaned up
)
}

Expand Down
12 changes: 6 additions & 6 deletions tests/test_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use common::{build_swarm, setup_logger};
fn test_directory_transfer() {
setup_logger();
let (tx, mut rx) = bounded::<Multiaddr>(10);
let (peer1, sender, _, mut swarm1) = build_swarm();
let (_, _, _, mut swarm2) = build_swarm();
let (peer1, sender, _, mut swarm1, _tempdir1) = build_swarm();
let (_, _, _, mut swarm2, _tempdir2) = build_swarm();

sender
.try_send(TransferCommand::Accept("directory".to_string()))
Expand Down Expand Up @@ -133,24 +133,24 @@ fn test_directory_transfer() {
let meta = fs::metadata(&path).expect("No file found");
assert!(meta.is_dir());
assert_eq!(
fs::metadata(Path::new(&path).join("test_dir/test.odt"))
fs::metadata(Path::new(&path).join("test.odt"))
.unwrap()
.len(),
8988
);
assert_eq!(
fs::metadata(Path::new(&path).join("test_dir/Der_Zauberberg.epub"))
fs::metadata(Path::new(&path).join("Der_Zauberberg.epub"))
.unwrap()
.len(),
659903
);
assert_eq!(
fs::metadata(Path::new(&path).join("test_dir/empty_file"))
fs::metadata(Path::new(&path).join("empty_file"))
.unwrap()
.len(),
0
);
assert!(fs::metadata(Path::new(&path).join("test_dir/empty_dir/"))
assert!(fs::metadata(Path::new(&path).join("empty_dir/"))
.unwrap()
.is_dir());
}
Expand Down
4 changes: 2 additions & 2 deletions tests/test_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ fn test_file_transfer() {

let file_path = "tests/data/file.txt".to_string();
let (tx, mut rx) = bounded::<Multiaddr>(10);
let (peer1, sender, _, mut swarm1) = build_swarm();
let (_, _, _, mut swarm2) = build_swarm();
let (peer1, sender, _, mut swarm1, _tempdir1) = build_swarm();
let (_, _, _, mut swarm2, _tempdir2) = build_swarm();

// File hash should be accepted from the beginning
let file = fs::File::open(&file_path).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions tests/test_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use common::{build_swarm, setup_logger};
fn test_text_transfer() {
setup_logger();
let (tx, mut rx) = bounded::<Multiaddr>(10);
let (peer1, sender, _, mut swarm1) = build_swarm();
let (_, _, _, mut swarm2) = build_swarm();
let (peer1, sender, _, mut swarm1, _tempdir1) = build_swarm();
let (_, _, _, mut swarm2, _tempdir2) = build_swarm();

// Text hash should be accepted from the beginning
sender
Expand Down

0 comments on commit ede2c28

Please sign in to comment.