log rotate: move basic rotation logic into module for reuse
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
parent
9e870b5f39
commit
95ade8fdb5
@ -1,6 +1,5 @@
|
|||||||
use std::collections::{HashMap, VecDeque};
|
use std::collections::{HashMap, VecDeque};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::path::Path;
|
|
||||||
use std::io::{Read, Write, BufRead, BufReader};
|
use std::io::{Read, Write, BufRead, BufReader};
|
||||||
use std::panic::UnwindSafe;
|
use std::panic::UnwindSafe;
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
@ -349,26 +348,11 @@ fn lock_task_list_files(exclusive: bool) -> Result<std::fs::File, Error> {
|
|||||||
/// rotates it if it is
|
/// rotates it if it is
|
||||||
pub fn rotate_task_log_archive(size_threshold: u64, compress: bool, max_files: Option<usize>) -> Result<bool, Error> {
|
pub fn rotate_task_log_archive(size_threshold: u64, compress: bool, max_files: Option<usize>) -> Result<bool, Error> {
|
||||||
let _lock = lock_task_list_files(true)?;
|
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)
|
||||||
let mut logrotate = LogRotate::new(PROXMOX_BACKUP_ARCHIVE_TASK_FN, compress).ok_or_else(|| format_err!("could not get archive file names"))?;
|
.ok_or(format_err!("could not get archive file names"))?;
|
||||||
let backup_user = crate::backup::backup_user()?;
|
|
||||||
logrotate.rotate(
|
logrotate.rotate(size_threshold, None, max_files)
|
||||||
CreateOptions::new()
|
|
||||||
.owner(backup_user.uid)
|
|
||||||
.group(backup_user.gid),
|
|
||||||
max_files,
|
|
||||||
)?;
|
|
||||||
Ok(true)
|
|
||||||
} else {
|
|
||||||
Ok(false)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// atomically read/update the task list, update status of finished tasks
|
// atomically read/update the task list, update status of finished tasks
|
||||||
|
@ -83,7 +83,7 @@ impl LogRotate {
|
|||||||
/// foo.1.zst => foo.2.zst
|
/// foo.1.zst => foo.2.zst
|
||||||
/// foo => foo.1.zst
|
/// foo => foo.1.zst
|
||||||
/// => foo
|
/// => foo
|
||||||
pub fn rotate(&mut self, options: CreateOptions, max_files: Option<usize>) -> Result<(), Error> {
|
pub fn do_rotate(&mut self, options: CreateOptions, max_files: Option<usize>) -> Result<(), Error> {
|
||||||
let mut filenames: Vec<PathBuf> = self.file_names().collect();
|
let mut filenames: Vec<PathBuf> = self.file_names().collect();
|
||||||
if filenames.is_empty() {
|
if filenames.is_empty() {
|
||||||
return Ok(()); // no file means nothing to rotate
|
return Ok(()); // no file means nothing to rotate
|
||||||
@ -123,6 +123,35 @@ impl LogRotate {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn rotate(
|
||||||
|
&mut self,
|
||||||
|
max_size: u64,
|
||||||
|
options: Option<CreateOptions>,
|
||||||
|
max_files: Option<usize>
|
||||||
|
) -> Result<bool, Error> {
|
||||||
|
|
||||||
|
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
|
/// Iterator over logrotated file names
|
||||||
|
Loading…
Reference in New Issue
Block a user