From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] applied: [PATCH backup 2/4] log rotate: factor out compression in private function
Date: Tue, 20 Oct 2020 11:10:42 +0200 [thread overview]
Message-ID: <20201020091044.6971-2-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20201020091044.6971-1-t.lamprecht@proxmox.com>
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
---
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
next prev parent reply other threads:[~2020-10-20 9:10 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-20 9:10 [pbs-devel] applied: [PATCH backup 1/4] log rotate: do NOT overwrite file with possible writers Thomas Lamprecht
2020-10-20 9:10 ` Thomas Lamprecht [this message]
2020-10-20 9:10 ` [pbs-devel] applied: [PATCH backup 3/4] log rotate: do NOT compress first rotation Thomas Lamprecht
2020-10-20 9:10 ` [pbs-devel] applied: [PATCH backup 4/4] log rotate: move basic rotation logic into module for reuse Thomas Lamprecht
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201020091044.6971-2-t.lamprecht@proxmox.com \
--to=t.lamprecht@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox