From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id A4889611B7 for ; Tue, 20 Oct 2020 11:10:51 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id A22BECF5D for ; Tue, 20 Oct 2020 11:10:51 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 9F190CF2A for ; Tue, 20 Oct 2020 11:10:50 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 71EB445E41 for ; Tue, 20 Oct 2020 11:10:50 +0200 (CEST) From: Thomas Lamprecht To: pbs-devel@lists.proxmox.com Date: Tue, 20 Oct 2020 11:10:42 +0200 Message-Id: <20201020091044.6971-2-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201020091044.6971-1-t.lamprecht@proxmox.com> References: <20201020091044.6971-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.129 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [logrotate.rs] Subject: [pbs-devel] applied: [PATCH backup 2/4] log rotate: factor out compression in private function X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 20 Oct 2020 09:10:51 -0000 Signed-off-by: Thomas Lamprecht --- src/tools/logrotate.rs | 58 ++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/src/tools/logrotate.rs b/src/tools/logrotate.rs index f7d1a693..28e3b7bb 100644 --- a/src/tools/logrotate.rs +++ b/src/tools/logrotate.rs @@ -46,6 +46,35 @@ impl LogRotate { } } + fn compress(file: &PathBuf, options: &CreateOptions) -> Result<(), Error> { + let mut source = File::open(file)?; + let (fd, tmp_path) = make_tmp_file(file, options.clone())?; + let target = unsafe { File::from_raw_fd(fd) }; + let mut encoder = match zstd::stream::write::Encoder::new(target, 0) { + Ok(encoder) => encoder, + Err(err) => { + let _ = unistd::unlink(&tmp_path); + bail!("creating zstd encoder failed - {}", err); + } + }; + + if let Err(err) = std::io::copy(&mut source, &mut encoder) { + let _ = unistd::unlink(&tmp_path); + bail!("zstd encoding failed for file {:?} - {}", file, err); + } + + if let Err(err) = encoder.finish() { + let _ = unistd::unlink(&tmp_path); + bail!("zstd finish failed for file {:?} - {}", file, err); + } + + if let Err(err) = rename(&tmp_path, file) { + let _ = unistd::unlink(&tmp_path); + bail!("rename failed for file {:?} - {}", file, err); + } + Ok(()) + } + /// Rotates the files up to 'max_files' /// if the 'compress' option was given it will compress the newest file /// @@ -77,38 +106,13 @@ impl LogRotate { } if self.compress { - let mut source = File::open(&filenames[0])?; - let (fd, tmp_path) = make_tmp_file(&filenames[1], options.clone())?; - let target = unsafe { File::from_raw_fd(fd) }; - let mut encoder = match zstd::stream::write::Encoder::new(target, 0) { - Ok(encoder) => encoder, - Err(err) => { - let _ = unistd::unlink(&tmp_path); - bail!("creating zstd encoder failed - {}", err); - } - }; - - if let Err(err) = std::io::copy(&mut source, &mut encoder) { - let _ = unistd::unlink(&tmp_path); - bail!("zstd encoding failed for file {:?} - {}", &filenames[1], err); + if filenames[0].extension().unwrap_or(std::ffi::OsStr::new("")) != "zst" { + Self::compress(&filenames[0], &options)?; } - - if let Err(err) = encoder.finish() { - let _ = unistd::unlink(&tmp_path); - bail!("zstd finish failed for file {:?} - {}", &filenames[1], err); - } - - if let Err(err) = rename(&tmp_path, &filenames[1]) { - let _ = unistd::unlink(&tmp_path); - bail!("rename failed for file {:?} - {}", &filenames[1], err); - } - - unistd::unlink(&filenames[0])?; } else { rename(&filenames[0], &filenames[1])?; } - if let Some(max_files) = max_files { // delete all files > max_files for file in filenames.iter().skip(max_files) { -- 2.27.0