From 15eaac46e73abf7082b38a7b3822e15bdf5857be Mon Sep 17 00:00:00 2001 From: Gaius Date: Fri, 12 Jan 2024 13:38:26 +0800 Subject: [PATCH] feat: if hardlink failed, remove the file and copy to output path (#214) Signed-off-by: Gaius --- Cargo.lock | 20 +++++++++---------- Cargo.toml | 4 ++-- src/bin/dfget/main.rs | 2 +- src/grpc/dfdaemon_download.rs | 36 +++++++++++++++++++---------------- src/storage/content.rs | 6 +++--- src/storage/metadata.rs | 3 +-- 6 files changed, 37 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ad039aae..a62e10df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -61,9 +61,9 @@ dependencies = [ [[package]] name = "anstream" -version = "0.6.5" +version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d664a92ecae85fd0a7392615844904654d1d5f5514837f471ddef4a057aba1b6" +checksum = "4cd2405b3ac1faab2990b74d728624cd9fd115651fcecc7c2d8daf01376275ba" dependencies = [ "anstyle", "anstyle-parse", @@ -220,9 +220,9 @@ checksum = "4c7f02d4ea65f2c1853089ffd8d2787bdbc63de2f0d29dedbcf8ccdfa0ccd4cf" [[package]] name = "base64" -version = "0.21.6" +version = "0.21.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c79fed4cdb43e993fcdadc7e58a09fd0e3e649c4436fa11da71c9f1f3ee7feb9" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" [[package]] name = "bindgen" @@ -348,9 +348,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.4.14" +version = "4.4.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33e92c5c1a78c62968ec57dbc2440366a2d6e5a23faf829970ff1585dc6b18e2" +checksum = "58e54881c004cec7895b0068a0a954cd5d62da01aef83fa35b1e594497bf5445" dependencies = [ "clap_builder", "clap_derive", @@ -358,9 +358,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.4.14" +version = "4.4.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4323769dc8a61e2c39ad7dc26f6f2800524691a44d74fe3d1071a5c24db6370" +checksum = "59cb82d7f531603d2fd1f507441cdd35184fa81beff7bd489570de7f773460bb" dependencies = [ "anstream", "anstyle", @@ -523,9 +523,9 @@ dependencies = [ [[package]] name = "dragonfly-api" -version = "2.0.78" +version = "2.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "358cffc9a9b0b2f0e08496b9c6e3fd044a916d1a3139760a27fcf60120deba3b" +checksum = "64e10ea493515e43f502f96ead4fb3e0a649de94b9863c84f4375a1e4fefffdb" dependencies = [ "prost 0.11.9", "prost-types 0.12.3", diff --git a/Cargo.toml b/Cargo.toml index aa26c8cf..d0b09fbf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dragonfly-client" -version = "0.1.12" +version = "0.1.13" authors = ["The Dragonfly Developers"] homepage = "https://d7y.io/" repository = "https://github.com/dragonflyoss/client.git" @@ -55,7 +55,7 @@ local-ip-address = "0.5.3" rocksdb = "0.21.0" num_cpus = "1.0" chrono = { version = "0.4.26", features = ["serde"] } -dragonfly-api = "2.0.78" +dragonfly-api = "2.0.79" sysinfo = "0.29.6" sha2 = "0.10" hex = "0.4" diff --git a/src/bin/dfget/main.rs b/src/bin/dfget/main.rs index 4810f4f8..d910bf15 100644 --- a/src/bin/dfget/main.rs +++ b/src/bin/dfget/main.rs @@ -245,7 +245,7 @@ async fn main() -> Result<(), anyhow::Error> { filters: args.filters.unwrap_or_default(), header: parse_header(args.header.unwrap_or_default())?, piece_length: args.piece_length, - output_path: args.output.into_os_string().into_string().unwrap(), + output_path: Some(args.output.into_os_string().into_string().unwrap()), timeout: Some(prost_wkt_types::Duration::try_from(args.timeout)?), need_back_to_source: false, }), diff --git a/src/grpc/dfdaemon_download.rs b/src/grpc/dfdaemon_download.rs index 38fa23c8..8341b210 100644 --- a/src/grpc/dfdaemon_download.rs +++ b/src/grpc/dfdaemon_download.rs @@ -247,23 +247,27 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler { }); } - // Hard link or copy the task content to the destination. - if let Err(err) = task - .hard_link_or_copy( - task_id.as_str(), - Path::new(download.output_path.clone().as_str()), - download.range.clone(), - ) - .await - { - error!("hard link or copy task: {}", err); - out_stream_tx - .send(Err(Status::internal(err.to_string()))) + // Check whether the output path is empty. If output path is empty, + // should not hard link or copy the task content to the destination. + if let Some(output_path) = download.output_path.clone() { + // Hard link or copy the task content to the destination. + if let Err(err) = task + .hard_link_or_copy( + task_id.as_str(), + Path::new(output_path.as_str()), + download.range.clone(), + ) .await - .unwrap_or_else(|err| { - error!("send download progress error: {:?}", err); - }); - }; + { + error!("hard link or copy task: {}", err); + out_stream_tx + .send(Err(Status::internal(err.to_string()))) + .await + .unwrap_or_else(|err| { + error!("send download progress error: {:?}", err); + }); + }; + } } Err(e) => { // Download task failed. diff --git a/src/storage/content.rs b/src/storage/content.rs index d5b76be8..e9efdb2f 100644 --- a/src/storage/content.rs +++ b/src/storage/content.rs @@ -14,7 +14,6 @@ * limitations under the License. */ -use crate::config; use crate::Result; use dragonfly_api::common::v2::Range; use sha2::{Digest, Sha256}; @@ -46,7 +45,7 @@ pub struct WritePieceResponse { impl Content { // new returns a new content. pub async fn new(dir: &Path) -> Result { - let dir = dir.join(config::NAME).join(DEFAULT_DIR_NAME); + let dir = dir.join(DEFAULT_DIR_NAME); fs::create_dir_all(&dir).await?; info!("content initialized directory: {:?}", dir); @@ -68,11 +67,12 @@ impl Content { return Ok(()); } - // Copy the task content to the destination. If the hard link fails, + // If the hard link fails, // copy the task content to the destination. if let Err(err) = self.hard_link_task(task_id, to).await { info!("hard link task failed: {}", err); + fs::remove_file(to).await?; self.copy_task(task_id, to).await?; info!("copy task success"); return Ok(()); diff --git a/src/storage/metadata.rs b/src/storage/metadata.rs index 06f586d6..c985dc6b 100644 --- a/src/storage/metadata.rs +++ b/src/storage/metadata.rs @@ -14,7 +14,6 @@ * limitations under the License. */ -use crate::config; use crate::{Error, Result}; use chrono::{NaiveDateTime, Utc}; use rocksdb::{ @@ -209,7 +208,7 @@ impl Metadata { options.set_block_based_table_factory(&block_options); // Open rocksdb. - let dir = dir.join(config::NAME).join(DEFAULT_DIR_NAME); + let dir = dir.join(DEFAULT_DIR_NAME); let cf_names = [TASK_CF_NAME, PIECE_CF_NAME]; let db = TransactionDB::open_cf(&options, &TransactionDBOptions::default(), &dir, cf_names)?;