From 0ccead664d916aed81e301790fe6d7a0bb0e90ea Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Thu, 24 Feb 2022 15:56:25 +0400 Subject: [PATCH 1/2] Version bumped to 0.4.4. Signed-off-by: Pavel Kirilin --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1bbab12..5c17e3b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2373,7 +2373,7 @@ dependencies = [ [[package]] name = "rustus" -version = "0.4.3" +version = "0.4.4" dependencies = [ "actix-files", "actix-rt", diff --git a/Cargo.toml b/Cargo.toml index 5255413..f6de28b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rustus" -version = "0.4.3" +version = "0.4.4" edition = "2021" description = "TUS protocol implementation written in Rust." From 6029124cb567565813970c871639cbebc3bd20a1 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Thu, 24 Feb 2022 16:07:59 +0400 Subject: [PATCH 2/2] Writing changed to copying. (#44) Signed-off-by: Pavel Kirilin --- Cargo.toml | 2 +- src/info_storages/file_info_storage.rs | 30 ++++++-------------------- src/main.rs | 1 + src/storages/file_storage.rs | 15 ++++--------- 4 files changed, 13 insertions(+), 35 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f6de28b..da7cc4a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -113,5 +113,5 @@ httptest = "0.15.4" [profile.release] lto = true panic = "abort" -opt-level = "z" +opt-level = 2 codegen-units = 1 diff --git a/src/info_storages/file_info_storage.rs b/src/info_storages/file_info_storage.rs index 08f977b..993f312 100644 --- a/src/info_storages/file_info_storage.rs +++ b/src/info_storages/file_info_storage.rs @@ -3,7 +3,7 @@ use std::path::PathBuf; use async_trait::async_trait; use log::error; use tokio::fs::{read_to_string, remove_file, DirBuilder, OpenOptions}; -use tokio::io::{AsyncWriteExt, BufWriter}; +use tokio::io::copy; use crate::errors::{RustusError, RustusResult}; use crate::info_storages::{FileInfo, InfoStorage}; @@ -35,7 +35,7 @@ impl InfoStorage for FileInfoStorage { } async fn set_info(&self, file_info: &FileInfo, create: bool) -> RustusResult<()> { - let file = OpenOptions::new() + let mut file = OpenOptions::new() .write(true) .create(create) .truncate(true) @@ -45,27 +45,11 @@ impl InfoStorage for FileInfoStorage { error!("{:?}", err); RustusError::UnableToWrite(err.to_string()) })?; - let mut writer = BufWriter::new(file); - writer - .write( - serde_json::to_string(&file_info) - .map_err(|err| { - error!("{:#?}", err); - err - })? - .as_bytes(), - ) - .await - .map_err(|err| { - error!("{:?}", err); - RustusError::UnableToWrite( - self.info_file_path(file_info.id.as_str()) - .as_path() - .display() - .to_string(), - ) - })?; - writer.flush().await?; + let data = serde_json::to_string(&file_info).map_err(|err| { + error!("{:#?}", err); + err + })?; + copy(&mut data.as_bytes(), &mut file).await?; Ok(()) } diff --git a/src/main.rs b/src/main.rs index 93aacc1..5b5330c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ #![cfg_attr(coverage, feature(no_coverage))] + use std::str::FromStr; use std::sync::Arc; diff --git a/src/storages/file_storage.rs b/src/storages/file_storage.rs index 607dbc1..a89985a 100644 --- a/src/storages/file_storage.rs +++ b/src/storages/file_storage.rs @@ -4,7 +4,7 @@ use actix_files::NamedFile; use async_trait::async_trait; use log::error; use tokio::fs::{remove_file, DirBuilder, OpenOptions}; -use tokio::io::{copy, AsyncWriteExt, BufWriter}; +use tokio::io::{copy, BufReader}; use crate::errors::{RustusError, RustusResult}; use crate::info_storages::FileInfo; @@ -85,7 +85,7 @@ impl Storage for FileStorage { // Opening file in w+a mode. // It means that we're going to append some // bytes to the end of a file. - let file = OpenOptions::new() + let mut file = OpenOptions::new() .write(true) .append(true) .create(false) @@ -95,15 +95,8 @@ impl Storage for FileStorage { error!("{:?}", err); RustusError::UnableToWrite(err.to_string()) })?; - let mut writer = BufWriter::new(file); - writer.write(bytes).await.map_err(|err| { - error!("{:?}", err); - RustusError::UnableToWrite(info.path.clone().unwrap()) - })?; - writer.flush().await.map_err(|err| { - error!("{:?}", err); - RustusError::UnableToWrite(info.path.clone().unwrap()) - })?; + let mut reader = BufReader::new(bytes); + copy(&mut reader, &mut file).await?; Ok(()) }