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 B632A61257 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 B4270CF5F 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 BAF8FCF48 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 8969945E3B 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:44 +0200 Message-Id: <20201020091044.6971-4-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 4/4] log rotate: move basic rotation logic into module for reuse 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/server/worker_task.rs | 24 ++++-------------------- src/tools/logrotate.rs | 31 ++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/server/worker_task.rs b/src/server/worker_task.rs index ab8e2bf6..8ef0fde7 100644 --- a/src/server/worker_task.rs +++ b/src/server/worker_task.rs @@ -1,6 +1,5 @@ use std::collections::{HashMap, VecDeque}; use std::fs::File; -use std::path::Path; use std::io::{Read, Write, BufRead, BufReader}; use std::panic::UnwindSafe; use std::sync::atomic::{AtomicBool, Ordering}; @@ -349,26 +348,11 @@ fn lock_task_list_files(exclusive: bool) -> Result { /// rotates it if it is pub fn rotate_task_log_archive(size_threshold: u64, compress: bool, max_files: Option) -> Result { let _lock = lock_task_list_files(true)?; - let path = Path::new(PROXMOX_BACKUP_ARCHIVE_TASK_FN); - let metadata = match path.metadata() { - Ok(metadata) => metadata, - Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(false), - Err(err) => bail!("unable to open task archive - {}", err), - }; - if metadata.len() > size_threshold { - let mut logrotate = LogRotate::new(PROXMOX_BACKUP_ARCHIVE_TASK_FN, compress).ok_or_else(|| format_err!("could not get archive file names"))?; - let backup_user = crate::backup::backup_user()?; - logrotate.rotate( - CreateOptions::new() - .owner(backup_user.uid) - .group(backup_user.gid), - max_files, - )?; - Ok(true) - } else { - Ok(false) - } + let mut logrotate = LogRotate::new(PROXMOX_BACKUP_ARCHIVE_TASK_FN, compress) + .ok_or(format_err!("could not get archive file names"))?; + + logrotate.rotate(size_threshold, None, max_files) } // atomically read/update the task list, update status of finished tasks diff --git a/src/tools/logrotate.rs b/src/tools/logrotate.rs index 54fe4bfe..dc703922 100644 --- a/src/tools/logrotate.rs +++ b/src/tools/logrotate.rs @@ -83,7 +83,7 @@ impl LogRotate { /// foo.1.zst => foo.2.zst /// foo => foo.1.zst /// => foo - pub fn rotate(&mut self, options: CreateOptions, max_files: Option) -> Result<(), Error> { + pub fn do_rotate(&mut self, options: CreateOptions, max_files: Option) -> Result<(), Error> { let mut filenames: Vec = self.file_names().collect(); if filenames.is_empty() { return Ok(()); // no file means nothing to rotate @@ -123,6 +123,35 @@ impl LogRotate { Ok(()) } + + pub fn rotate( + &mut self, + max_size: u64, + options: Option, + max_files: Option + ) -> Result { + + let options = match options { + Some(options) => options, + None => { + let backup_user = crate::backup::backup_user()?; + CreateOptions::new().owner(backup_user.uid).group(backup_user.gid) + }, + }; + + let metadata = match self.base_path.metadata() { + Ok(metadata) => metadata, + Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(false), + Err(err) => bail!("unable to open task archive - {}", err), + }; + + if metadata.len() > max_size { + self.do_rotate(options, max_files)?; + Ok(true) + } else { + Ok(false) + } + } } /// Iterator over logrotated file names -- 2.27.0