Skip to content

Commit 634a7bf

Browse files
committed
Issue 29: Consistent number of arguments across OSes
- take `Option<OpenOptions>` in `FileRotate::new`
1 parent fc60342 commit 634a7bf

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "file-rotate"
3-
version = "0.7.6"
3+
version = "0.8.0"
44
authors = ["Kevin Robert Stravers <kevin@stravers.net>", "Erlend Langseth <3rlendhl@gmail.com>"]
55
edition = "2018"
66
description = "Log rotation for files"

src/lib.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
//! AppendCount::new(2),
3030
//! ContentLimit::Lines(3),
3131
//! Compression::None,
32-
//! #[cfg(unix)]
3332
//! None,
3433
//! );
3534
//!
@@ -62,7 +61,6 @@
6261
//! AppendCount::new(2),
6362
//! ContentLimit::Bytes(5),
6463
//! Compression::None,
65-
//! #[cfg(unix)]
6664
//! None,
6765
//! );
6866
//!
@@ -99,7 +97,6 @@
9997
//! AppendCount::new(3),
10098
//! ContentLimit::Bytes(1),
10199
//! Compression::None,
102-
//! #[cfg(unix)]
103100
//! None,
104101
//! );
105102
//!
@@ -158,7 +155,6 @@
158155
//! AppendTimestamp::default(FileLimit::MaxFiles(2)),
159156
//! ContentLimit::Bytes(1),
160157
//! Compression::None,
161-
//! #[cfg(unix)]
162158
//! None,
163159
//! );
164160
//!
@@ -204,7 +200,6 @@
204200
//! AppendTimestamp::default(FileLimit::MaxFiles(4)),
205201
//! ContentLimit::Bytes(1),
206202
//! Compression::OnRotate(2),
207-
//! #[cfg(unix)]
208203
//! None,
209204
//! );
210205
//!
@@ -303,9 +298,6 @@ use std::{
303298
};
304299
use suffix::*;
305300

306-
#[cfg(unix)]
307-
use std::os::unix::fs::OpenOptionsExt;
308-
309301
pub mod compression;
310302
pub mod suffix;
311303
#[cfg(test)]
@@ -392,8 +384,7 @@ pub struct FileRotate<S: SuffixScheme> {
392384
suffix_scheme: S,
393385
/// The bool is whether or not there's a .gz suffix to the filename
394386
suffixes: BTreeSet<SuffixInfo<S::Repr>>,
395-
#[cfg(unix)]
396-
mode: Option<u32>,
387+
open_options: Option<OpenOptions>,
397388
}
398389

399390
impl<S: SuffixScheme> FileRotate<S> {
@@ -404,6 +395,8 @@ impl<S: SuffixScheme> FileRotate<S> {
404395
///
405396
/// `content_limit` specifies the limits for rotating a file.
406397
///
398+
/// `open_options`: If provided, you must set `.read(true).create(true).append(true)`!
399+
///
407400
/// # Panics
408401
///
409402
/// Panics if `bytes == 0` or `lines == 0`.
@@ -412,7 +405,7 @@ impl<S: SuffixScheme> FileRotate<S> {
412405
suffix_scheme: S,
413406
content_limit: ContentLimit,
414407
compression: Compression,
415-
#[cfg(unix)] mode: Option<u32>,
408+
open_options: Option<OpenOptions>,
416409
) -> Self {
417410
match content_limit {
418411
ContentLimit::Bytes(bytes) => {
@@ -440,8 +433,7 @@ impl<S: SuffixScheme> FileRotate<S> {
440433
compression,
441434
suffixes: BTreeSet::new(),
442435
suffix_scheme,
443-
#[cfg(unix)]
444-
mode,
436+
open_options,
445437
};
446438
s.ensure_log_directory_exists();
447439
s.scan_suffixes();
@@ -484,14 +476,11 @@ impl<S: SuffixScheme> FileRotate<S> {
484476
}
485477

486478
fn open_file(&mut self) {
487-
let mut open_options = OpenOptions::new();
488-
489-
open_options.read(true).create(true).append(true);
490-
491-
#[cfg(unix)]
492-
if let Some(mode) = self.mode {
493-
open_options.mode(mode);
494-
}
479+
let open_options = self.open_options.clone().unwrap_or_else(|| {
480+
let mut o = OpenOptions::new();
481+
o.read(true).create(true).append(true);
482+
o
483+
});
495484

496485
self.file = open_options.open(&self.basepath).ok();
497486
}
@@ -772,10 +761,12 @@ pub mod mock_time {
772761
static MOCK_TIME: RefCell<Option<DateTime<Local>>> = RefCell::new(None);
773762
}
774763

764+
/// Get current _mocked_ time
775765
pub fn now() -> DateTime<Local> {
776766
MOCK_TIME.with(|cell| cell.borrow().as_ref().cloned().unwrap_or_else(Local::now))
777767
}
778768

769+
/// Set mocked time
779770
pub fn set_mock_time(time: DateTime<Local>) {
780771
MOCK_TIME.with(|cell| *cell.borrow_mut() = Some(time));
781772
}

src/tests.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ fn timestamp_max_files_rotation() {
6565
assert_eq!("m\n", fs::read_to_string(&log_path).unwrap());
6666
}
6767
#[test]
68-
#[cfg(feature = "chrono04")]
6968
fn timestamp_max_age_deletion() {
7069
// In order not to have to sleep, and keep it deterministic, let's already create the log files and see how FileRotate
7170
// cleans up the old ones.
@@ -74,9 +73,7 @@ fn timestamp_max_age_deletion() {
7473
let log_path = dir.join("log");
7574

7675
// One recent file:
77-
let recent_file = chrono::offset::Local::now()
78-
.format("log.%Y%m%dT%H%M%S")
79-
.to_string();
76+
let recent_file = Local::now().format("log.%Y%m%dT%H%M%S").to_string();
8077
File::create(dir.join(&recent_file)).unwrap();
8178
// Two very old files:
8279
File::create(dir.join("log.20200825T151133")).unwrap();
@@ -339,19 +336,27 @@ fn line_count_recalculation() {
339336
#[cfg(unix)]
340337
#[test]
341338
fn unix_file_permissions() {
339+
use std::os::unix::fs::OpenOptionsExt;
342340
let permissions = &[0o600, 0o644];
343341

344342
for permission in permissions {
345343
let tmp_dir = TempDir::new().unwrap();
346344
let parent = tmp_dir.path();
347345
let log_path = parent.join("log");
348346

347+
let mut options = OpenOptions::new();
348+
options
349+
.read(true)
350+
.create(true)
351+
.append(true)
352+
.mode(*permission);
353+
349354
let mut file_rotate = FileRotate::new(
350355
&*log_path.to_string_lossy(),
351356
AppendCount::new(3),
352357
ContentLimit::Lines(2),
353358
Compression::None,
354-
Some(*permission),
359+
Some(options),
355360
);
356361

357362
// Trigger a rotation by writing three lines

0 commit comments

Comments
 (0)