diff --git a/Cargo.toml b/Cargo.toml index a3e23a7..ec4fb12 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "file-rotate" -version = "0.7.5" +version = "0.7.6" authors = ["Kevin Robert Stravers ", "Erlend Langseth <3rlendhl@gmail.com>"] edition = "2018" description = "Log rotation for files" diff --git a/src/lib.rs b/src/lib.rs index 257d2ef..898ee34 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -595,7 +595,7 @@ impl FileRotate { let mut result = Ok(()); for (i, suffix) in self.suffixes.iter().enumerate().rev() { if self.suffix_scheme.too_old(&suffix.suffix, i) { - result = result.and(std::fs::remove_file(suffix.to_path(&self.basepath))); + result = result.and(fs::remove_file(suffix.to_path(&self.basepath))); youngest_old = Some((*suffix).clone()); } else { break; diff --git a/src/suffix.rs b/src/suffix.rs index ae867c4..b7279f3 100644 --- a/src/suffix.rs +++ b/src/suffix.rs @@ -57,7 +57,7 @@ pub trait SuffixScheme { /// Find all files in the basepath.parent() directory that has path equal to basepath + a valid /// suffix. Return sorted collection - sorted from most recent to oldest based on the - /// [Ord](std::cmp::Ord) implementation of `Self::Repr`. + /// [Ord] implementation of `Self::Repr`. fn scan_suffixes(&self, basepath: &Path) -> BTreeSet> { let mut suffixes = BTreeSet::new(); let filename_prefix = basepath @@ -294,6 +294,7 @@ impl SuffixScheme for AppendTimestamp { let old_timestamp = (Local::now() - age).format(self.format).to_string(); suffix.timestamp < old_timestamp } + FileLimit::Unlimited => false, } } } @@ -304,6 +305,8 @@ pub enum FileLimit { MaxFiles(usize), /// Delete files whose age exceeds the `Duration` - age is determined by the suffix of the file Age(Duration), + /// Never delete files + Unlimited, } #[cfg(test)] @@ -410,7 +413,7 @@ mod test { let log_path = dir.join("file"); for suffix in case.suffixes.iter().chain(case.incorrect_suffixes) { - std::fs::File::create(dir.join(format!("file.{}", suffix))).unwrap(); + File::create(dir.join(format!("file.{}", suffix))).unwrap(); } let scheme = AppendTimestamp::with_format( diff --git a/src/tests.rs b/src/tests.rs index 6a6a139..4e545d7 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -92,7 +92,7 @@ fn timestamp_max_age_deletion() { ); writeln!(log, "trigger\nat\nleast\none\nrotation").unwrap(); - let mut filenames = std::fs::read_dir(dir) + let mut filenames = fs::read_dir(dir) .unwrap() .filter_map(|entry| entry.ok()) .filter(|entry| entry.path().is_file()) @@ -278,7 +278,7 @@ fn byte_count_recalculation() { let parent = tmp_dir.path(); let log_path = parent.join("log"); - std::fs::write(&log_path, b"a").unwrap(); + fs::write(&log_path, b"a").unwrap(); let mut file_rotate = FileRotate::new( &*log_path.to_string_lossy(), @@ -292,10 +292,10 @@ fn byte_count_recalculation() { write!(file_rotate, "bc").unwrap(); assert_eq!(file_rotate.log_paths().len(), 1); // The size of the rotated file should be 2 ('ab) - let rotated_content = std::fs::read(&file_rotate.log_paths()[0]).unwrap(); + let rotated_content = fs::read(&file_rotate.log_paths()[0]).unwrap(); assert_eq!(rotated_content, b"ab"); // The size of the main file should be 1 ('c') - let main_content = std::fs::read(log_path).unwrap(); + let main_content = fs::read(log_path).unwrap(); assert_eq!(main_content, b"c"); } @@ -307,7 +307,7 @@ fn line_count_recalculation() { let parent = tmp_dir.path(); let log_path = parent.join("log"); - std::fs::write(&log_path, b"a\n").unwrap(); + fs::write(&log_path, b"a\n").unwrap(); let mut file_rotate = FileRotate::new( &*log_path.to_string_lossy(), @@ -510,14 +510,14 @@ fn test_file_limit() { let log_path = dir.join("file"); let old_file = dir.join("file.2022-02-01"); - std::fs::File::create(&old_file).unwrap(); + File::create(&old_file).unwrap(); let first = get_fake_date_time("2022-02-02T01:00:00"); let second = get_fake_date_time("2022-02-03T01:00:00"); let third = get_fake_date_time("2022-02-04T01:00:00"); let mut log = FileRotate::new( - &log_path, + log_path, AppendTimestamp::with_format("%Y-%m-%d", FileLimit::MaxFiles(1), DateFrom::DateYesterday), ContentLimit::Time(TimeFrequency::Daily), Compression::None,