gzip compress audit file

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>
This commit is contained in:
ゆめ 2024-10-16 18:34:45 -05:00
parent c67e647b11
commit 1af251420e
No known key found for this signature in database

View file

@ -1,7 +1,10 @@
use axum::response::IntoResponse;
use flate2::write::GzEncoder;
use flate2::{Compression, GzBuilder};
use serde::Serialize;
use std::collections::HashSet;
use std::fs::{File, OpenOptions};
use std::io::{BufReader, BufWriter};
use std::sync::atomic::AtomicU32;
use std::{collections::HashMap, fmt::Debug, ops::DerefMut, path::PathBuf, sync::Arc};
use tokio::sync::{Mutex, RwLock};
@ -57,7 +60,7 @@ pub enum AuditError {
pub struct AuditState {
options: AuditOptions,
cur_file: Option<RwLock<HashMap<String, Mutex<File>>>>,
cur_file: Option<RwLock<HashMap<String, Mutex<GzEncoder<File>>>>>,
vacuum_counter: AtomicU32,
}
@ -81,7 +84,7 @@ impl AuditState {
.filter_map(|f| f.ok())
.filter(|f| {
f.file_type().map(|t| t.is_file()).unwrap_or(false)
&& f.file_name().to_string_lossy().ends_with(".json")
&& f.file_name().to_string_lossy().ends_with(".json.gz")
})
.filter(|f| !in_use_files.contains(&f.file_name().to_string_lossy().to_string()));
@ -108,7 +111,7 @@ impl AuditState {
let mut write = self.cur_file.as_ref().unwrap().write().await;
let full_name = format!("{}_{}.json", name, time_str);
let full_name = format!("{}_{}.json.gz", name, time_str);
let file = OpenOptions::new()
.create(true)
@ -116,7 +119,9 @@ impl AuditState {
.append(true)
.open(&self.options.output.join(&full_name))?;
write.insert(name.to_string(), Mutex::new(file));
let gz = GzEncoder::new(file, Compression::default());
write.insert(name.to_string(), Mutex::new(gz));
Ok(())
}
@ -143,7 +148,7 @@ impl AuditState {
serde_json::to_writer(f.deref_mut(), &item)?;
let meta = f.metadata()?;
let meta = f.get_ref().metadata()?;
if let Some(size) = self.options.rotate_size {
if meta.len() >= size {
@ -161,6 +166,10 @@ impl AuditState {
let file = File::create(&self.options.output.join(name))?;
let file = GzBuilder::new()
.filename(name)
.write(file, Compression::default());
write.insert(name.to_string(), Mutex::new(file));
// this is deliberately out of order to make sure we don't create endless files if serialization fails