feat: add file digest

This commit is contained in:
kingtous
2022-04-26 14:56:15 +08:00
committed by Kingtous
parent dab0fce18d
commit f4c6c4f6c4
5 changed files with 205 additions and 5 deletions

View File

@@ -245,6 +245,7 @@ message FileAction {
oneof union {
ReadDir read_dir = 1;
FileTransferSendRequest send = 2;
FileTransferSendConfirmRequest send_confirm = 9;
FileTransferReceiveRequest receive = 3;
FileDirCreate create = 4;
FileRemoveDir remove_dir = 5;
@@ -262,14 +263,23 @@ message FileResponse {
FileTransferBlock block = 2;
FileTransferError error = 3;
FileTransferDone done = 4;
FileTransferDigest digest = 5;
}
}
message FileTransferDigest {
int32 id = 1;
sint32 file_num = 2;
uint64 last_edit_timestamp = 3;
uint64 file_size = 4;
}
message FileTransferBlock {
int32 id = 1;
sint32 file_num = 2;
bytes data = 3;
bool compressed = 4;
uint32 blk_id = 5;
}
message FileTransferError {
@@ -284,6 +294,15 @@ message FileTransferSendRequest {
bool include_hidden = 3;
}
message FileTransferSendConfirmRequest {
int32 id = 1;
sint32 file_num = 2;
oneof union {
bool skip = 3;
uint32 offset_blk = 4;
}
}
message FileTransferDone {
int32 id = 1;
sint32 file_num = 2;

View File

@@ -5,8 +5,10 @@ use crate::{
compress::{compress, decompress},
config::{Config, COMPRESS_LEVEL},
};
use log::log;
#[cfg(windows)]
use std::os::windows::prelude::*;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tokio::{fs::File, io::*};
pub fn read_dir(path: &PathBuf, include_hidden: bool) -> ResultType<FileDirectory> {
@@ -184,6 +186,11 @@ pub fn get_recursive_files(path: &str, include_hidden: bool) -> ResultType<Vec<F
read_dir_recursive(&get_path(path), &get_path(""), include_hidden)
}
#[inline]
pub fn is_file_exists(file_path: &str) -> bool {
return Path::new(file_path).exists();
}
#[derive(Default)]
pub struct TransferJob {
id: i32,
@@ -194,6 +201,7 @@ pub struct TransferJob {
total_size: u64,
finished_size: u64,
transferred: u64,
default_overwrite_strategy: Option<bool>,
}
#[inline]
@@ -220,6 +228,7 @@ fn is_compressed_file(name: &str) -> bool {
impl TransferJob {
pub fn new_write(id: i32, path: String, files: Vec<FileEntry>) -> Self {
println!("new write {}", path);
let total_size = files.iter().map(|x| x.size as u64).sum();
Self {
id,
@@ -231,6 +240,7 @@ impl TransferJob {
}
pub fn new_read(id: i32, path: String, include_hidden: bool) -> ResultType<Self> {
println!("new read {}", path);
let files = get_recursive_files(&path, include_hidden)?;
let total_size = files.iter().map(|x| x.size as u64).sum();
Ok(Self {
@@ -342,7 +352,7 @@ impl TransferJob {
}
#[inline]
fn join(&self, name: &str) -> PathBuf {
pub fn join(&self, name: &str) -> PathBuf {
if name.is_empty() {
self.path.clone()
} else {
@@ -413,6 +423,13 @@ impl TransferJob {
..Default::default()
}))
}
pub fn set_overwrite_strategy(&mut self, overwrite_strategy: Option<bool>) {
self.default_overwrite_strategy = overwrite_strategy;
}
pub fn default_overwrite_strategy(&self) -> Option<bool> {
self.default_overwrite_strategy
}
}
#[inline]
@@ -468,6 +485,7 @@ pub fn new_receive(id: i32, path: String, files: Vec<FileEntry>) -> Message {
#[inline]
pub fn new_send(id: i32, path: String, include_hidden: bool) -> Message {
println!("new send: {},id : {}", path, id);
let mut action = FileAction::new();
action.set_send(FileTransferSendRequest {
id,
@@ -558,3 +576,27 @@ pub fn create_dir(dir: &str) -> ResultType<()> {
std::fs::create_dir_all(get_path(dir))?;
Ok(())
}
#[inline]
pub fn is_write_need_confirmation(
file_path: &str,
digest: &FileTransferDigest,
) -> ResultType<bool> {
let path = Path::new(file_path);
if path.exists() && path.is_file() {
let metadata = std::fs::metadata(path)?;
let modified_time = metadata.modified()?;
let remote_mt = Duration::from_millis(digest.last_edit_timestamp);
let local_mt = modified_time.duration_since(UNIX_EPOCH)?;
// if
// is_recv && remote_mt >= local_mt) || (!is_recv && remote_mt <= local_mt) ||
if remote_mt == local_mt && digest.file_size == metadata.len() {
// I'm recving or sending an newer modified file!
// or a
return Ok(false);
}
Ok(true)
} else {
Ok(false)
}
}