From 956adfc90ac3e22048d0e17e73986c1f9a3fd5a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?KARASZI=20Istv=C3=A1n?= Date: Thu, 7 Jan 2021 16:50:13 +0100 Subject: [PATCH] Replace in-place compression Compress gzip files to a temporary file first and then do an atomic rename. --- Cargo.lock | 1 + meilisearch-http/Cargo.toml | 1 + meilisearch-http/src/helpers/compression.rs | 12 ++++++++++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index da4f99f6c..c306c86dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1734,6 +1734,7 @@ dependencies = [ "tempfile", "tokio", "ureq", + "uuid", "vergen", "walkdir", "whoami", diff --git a/meilisearch-http/Cargo.toml b/meilisearch-http/Cargo.toml index 7212a584a..0ad18e340 100644 --- a/meilisearch-http/Cargo.toml +++ b/meilisearch-http/Cargo.toml @@ -51,6 +51,7 @@ tar = "0.4.30" tempfile = "3.1.0" tokio = { version = "0.2", features = ["macros"] } ureq = { version = "2.0.0", features = ["tls"], default-features = false } +uuid = "0.8" walkdir = "2.3.1" whoami = "1.0.3" diff --git a/meilisearch-http/src/helpers/compression.rs b/meilisearch-http/src/helpers/compression.rs index ff3e1258f..93f5c6a08 100644 --- a/meilisearch-http/src/helpers/compression.rs +++ b/meilisearch-http/src/helpers/compression.rs @@ -1,19 +1,27 @@ use flate2::Compression; use flate2::read::GzDecoder; use flate2::write::GzEncoder; -use std::fs::{create_dir_all, File}; +use std::fs::{create_dir_all, rename, File}; use std::path::Path; use tar::{Builder, Archive}; +use uuid::Uuid; use crate::error::Error; pub fn to_tar_gz(src: &Path, dest: &Path) -> Result<(), Error> { - let f = File::create(dest)?; + let file_name = format!(".{}", Uuid::new_v4().to_urn()); + let p = dest.with_file_name(file_name); + let tmp_dest = p.as_path(); + + let f = File::create(tmp_dest)?; let gz_encoder = GzEncoder::new(f, Compression::default()); let mut tar_encoder = Builder::new(gz_encoder); tar_encoder.append_dir_all(".", src)?; let gz_encoder = tar_encoder.into_inner()?; gz_encoder.finish()?; + + rename(tmp_dest, dest)?; + Ok(()) }