Skip to content

Commit 6e5e974

Browse files
committed
worked on the functionality to copied the files from source to the destination
1 parent 749f304 commit 6e5e974

4 files changed

Lines changed: 54 additions & 34 deletions

File tree

Cargo.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ walkdir = "2.5.0"
1010
[workspace.package]
1111
edition = "2024"
1212
repository = "https://github.com/ibilalkayy/cover"
13-
version = "0.1.1"
13+
version = "0.1.2"
1414
license = "Apache-2.0"
1515
readme = "README.md"
1616
categories = ["command-line-utilities"]

cli/src/flags/sync.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
use std::path::PathBuf;
2+
13
use clap::Parser;
24

35
#[derive(Debug, Parser)]
46
pub struct SyncData {
57
/// Source folder to take the file from
68
#[clap(short, long)]
7-
pub source: String,
9+
pub source: PathBuf,
810

911
/// Destination folder to move the file to
1012
#[clap(short, long)]
11-
pub destination: String,
13+
pub destination: PathBuf,
1214

1315
/// Copy only the changed files
1416
#[clap(long)]

crates/sync/src/lib.rs

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
use std::time::SystemTime;
1+
use std::{path::PathBuf, time::SystemTime};
22
use walkdir::{DirEntry, WalkDir};
33

44
pub struct SyncData {
5-
pub source: String,
6-
pub destination: String,
5+
pub source: PathBuf,
6+
pub destination: PathBuf,
77
pub incremental: Option<bool>,
88
pub delete: Option<bool>,
99
pub dry_run: Option<bool>,
1010
pub verbose: Option<bool>,
1111
pub hash: Option<bool>,
1212
}
1313

14-
fn file_name(source_entry: DirEntry) {
15-
if let Some(name) = source_entry.path().file_name() {
16-
println!("File: {}", name.to_string_lossy());
17-
}
18-
}
19-
2014
fn file_types(source_entry: DirEntry) {
2115
if let Ok(metadata) = source_entry.metadata() {
2216
let size = metadata.len();
@@ -58,27 +52,51 @@ fn file_last_modified(source_entry: DirEntry) {
5852

5953
impl SyncData {
6054
pub fn sync_output(&self) {
61-
for source_entry in WalkDir::new(&self.source) {
62-
let source_entry = source_entry.expect("Err: failed to get the source entry");
63-
let file_type = source_entry.file_type();
55+
let mut source_files: Vec<PathBuf> = Vec::new();
56+
for entry in WalkDir::new(&self.source) {
57+
let entry = entry.expect("Err: failed to get the source entry");
58+
if entry.file_type().is_file() {
59+
source_files.push(entry.path().to_path_buf());
60+
// file_types(source_entry.clone());
61+
// file_last_modified(source_entry);
62+
}
63+
}
6464

65-
if file_type.is_file() {
66-
file_name(source_entry.clone());
67-
file_types(source_entry.clone());
68-
file_last_modified(source_entry);
65+
let mut destination_files: Vec<PathBuf> = Vec::new();
66+
for entry in WalkDir::new(&self.destination) {
67+
let entry = entry.expect("Err: failed to get the destination entry");
68+
if entry.file_type().is_file() {
69+
destination_files.push(entry.path().to_path_buf());
70+
// file_types(entry.clone());
71+
// file_last_modified(entry);
6972
}
7073
}
7174

72-
for destination_entry in WalkDir::new(&self.destination) {
73-
let destination_entry =
74-
destination_entry.expect("Err: failed to get the destination entry");
75-
let file_type = destination_entry.file_type();
75+
let mut copied_files = Vec::new();
76+
for src in &source_files {
77+
let src_file_name = src.file_name().expect("Err: failed to get the file name");
78+
79+
let mut dest_path = self.destination.clone();
80+
dest_path.push(src_file_name);
7681

77-
if file_type.is_file() {
78-
file_name(destination_entry.clone());
79-
file_types(destination_entry.clone());
80-
file_last_modified(destination_entry);
82+
let mut found = false;
83+
for dest in &destination_files {
84+
let dest_file_name = dest.file_name().expect("Err: failed to get the file name");
85+
if src_file_name == dest_file_name {
86+
found = true;
87+
}
8188
}
89+
90+
if !found {
91+
std::fs::copy(src, dest_path).unwrap();
92+
copied_files.push(src_file_name.to_owned());
93+
}
94+
}
95+
96+
if !copied_files.is_empty() {
97+
println!("Missing files are copied successfully");
98+
} else {
99+
eprintln!("No missing files to copy. Destination is up-to-date.");
82100
}
83101

84102
// println!("{}", self.incremental.unwrap());

0 commit comments

Comments
 (0)