1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//! Simple crate that allows easy usage of rotating logfiles by faking being a
//! single [`std::io::Write`] implementor
//!
//! # Alright, sure, but what's a rotating logfile?
//!
//! Well, imagine we are logging *a lot*, and after a while we use up all our disk space with logs.
//! We don't want this, nobody wants this, so how do we solve it?
//!
//! We'll introduce the concept of changing what file we log to periodically, or in other words, we'll
//! *rotate* our log files so that we don't generate too much stored logging.
//!
//! One of the concepts that is involved in rotation is a limit to how many log files can exist at once.
//!
//! # Examples
//!
//! To demostrate what was said above, here's to create a file which rotates every day, storing up to a week of logs
//! in `/logs`
//!
//! ```rust
//! # use std::{time::Duration, num::NonZeroUsize};
//! # use file_rotator::{RotationPeriod, RotatingFile, Compression};
//! RotatingFile::new(
//!     "loggylog",
//!     "/logs",
//!     RotationPeriod::Interval(Duration::from_secs(60 * 60 * 24)),
//!     NonZeroUsize::new(7).unwrap(),
//!     Compression::None,
//! );
//! ```

#![warn(
    missing_docs,
    missing_debug_implementations,
    missing_copy_implementations
)]

use std::borrow::Cow;
use std::fs;
use std::io::{self, prelude::*};
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};
use std::time::Duration;

/// A specifier for how often we should rotate files
#[derive(Clone, Copy, Debug, Ord, PartialOrd, Eq, PartialEq)]
#[non_exhaustive]
pub enum RotationPeriod {
    /// Rotate every N line terminator bytes (0x0a, b'\n')
    Lines(usize),

    /// Rotate every N bytes successfully written
    ///
    /// This does not count bytes that are not written to the underlying file
    /// (when the given buffer's len does not match with the return value of
    /// [`io::Write::write`])
    ///
    /// [`io::Write::write`]: https://doc.rust-lang.org/std/io/trait.Write.html#tymethod.write
    Bytes(usize),

    /// Rotate every time N amount of time passes
    ///
    /// This is calculated on every write and is based on comparing two [`Instant::now`] return values
    ///
    /// [`Instant::now`]: https://doc.rust-lang.org/std/time/struct.Instant.html#method.now
    Interval(Duration),

    /// Rotate only via [`RotatingFile::rotate`]
    ///
    /// [`RotatingFile::rotate']: struct.RotatingFile.html#method.rotate
    Manual,
}

mod rotation_tracker;
use rotation_tracker::RotationTracker;

/// As per the name, a rotating file
///
/// Handles being a fake file which will automagicaly rotate as bytes are written into it
#[derive(Debug)]
pub struct RotatingFile {
    name: Cow<'static, str>,
    directory: PathBuf,
    rotation_tracker: RotationTracker,
    max_index: usize,

    compression: Compression,
    current_file: Option<fs::File>,
}

/// What compression algorithm should be used?
///
/// The current log file (`NAME.0.log`) is always written uncompressed; once its time to rotate
/// out, compression will be applied. Depending on compression type, an extra extension might be
/// added.
#[derive(Clone, Copy, Debug)]
pub enum Compression {
    /// No compression, just bytes to disk.
    None,
    /// Zstd compression.
    Zstd {
        /// What level of compression should be used? As per the zstd crate's docs, zero means default.
        level: i32,
    },
}

impl RotatingFile {
    /// Create a new rotating file with the given base name, in the given directory, rotating every
    /// given period and with a max of a given number of files
    pub fn new<Name, Directory>(
        name: Name,
        directory: Directory,
        rotate_every: RotationPeriod,
        max_files: NonZeroUsize,
        compression: Compression,
    ) -> Self
    where
        Name: Into<Cow<'static, str>>,
        Directory: Into<PathBuf>,
    {
        Self {
            name: name.into(),
            directory: directory.into(),
            rotation_tracker: RotationTracker::from(rotate_every),
            max_index: max_files.get() - 1,
            compression,
            current_file: None,
        }
    }

    fn should_rotate(&self) -> bool {
        // If we have no current file, it's probably best if we make one :p
        self.current_file.is_none() || self.rotation_tracker.should_rotate()
    }

    // Paths are split into three parts: NAME.INDEX.EXTENSION
    // NAME is user-defined and unimportant to us, while EXTENSION must be either log or log.zstd
    fn logfile_index<P: AsRef<Path>>(&self, path: P) -> Option<usize> {
        let mut parts = path.as_ref().file_name()?.to_str()?.split('.');
        match parts.next_back() {
            Some("zstd") => {
                if parts.next_back() != Some("log") {
                    return None;
                }
            }
            Some("log") => {}
            Some(..) | None => return None,
        }
        parts.next_back()?.parse().ok()
    }

    // Increment a log file's index component by one by moving it, compressing if necessary
    fn increment_index(&self, index: usize) -> io::Result<()> {
        let path = self.make_filepath(index);
        let dst = self.make_filepath(index + 1);
        debug_assert!(!dst.exists());
        // If we're rotating out the current log file, we must compress it. Otherwise, everything's
        // already compressed and we can just rotate
        match self.compression {
            Compression::Zstd { level } if index == 0 => {
                zstd::stream::copy_encode(fs::File::open(&path)?, fs::File::create(dst)?, level)?;
                fs::remove_file(&path)?;
                Ok(())
            }

            Compression::None | Compression::Zstd { .. } => fs::rename(path, dst),
        }
    }

    fn make_filepath(&self, index: usize) -> PathBuf {
        self.directory.join(format!(
            "{}.{}.{}",
            self.name,
            index,
            match self.compression {
                Compression::Zstd { .. } if index != 0 => "log.zstd",
                Compression::None | Compression::Zstd { .. } => "log",
            }
        ))
    }

    fn create_file(&self) -> io::Result<fs::File> {
        // Let's survey the directory and find out what's the biggest index in there
        let max_found_index = itertools::process_results(fs::read_dir(&self.directory)?, |dir| {
            dir.into_iter()
                .filter_map(|entry| self.logfile_index(entry.path()))
                .max()
        })?;

        // If we've found any logs, let's make sure we stay under `self.max_index` files.
        if let Some(mut max_found_index) = max_found_index {
            // First, let's check if we have the maximum amount of logs available (or maybe even more!)
            if max_found_index >= self.max_index {
                // If so, let's remove all of the ones >=self.max_index so that we can make room for one more
                (self.max_index..=max_found_index)
                    .try_for_each(|index| fs::remove_file(self.make_filepath(index)))?;

                // We'll need to update our `max_found_index` to avoid trying to
                // move stuff that isn't there, but we'll use a saturating
                // subtraction to handle the case where self.max_index == 0
                // (only one logfile ever)
                max_found_index = self.max_index.saturating_sub(1);
            }

            // If we've got a non-zero max index, we've got files to shuffle around!
            if self.max_index != 0 {
                // Increment all the remaining log files' indices so that we have
                // room for a new one with index 0. Make sure that we do this in reverse order so
                // we don't trample anything!
                (0..=max_found_index)
                    .rev()
                    .try_for_each(|index| self.increment_index(index))?;
            }
        }

        // Make sure we pass `create_new` so that nobody tries to be sneaky and
        // place a file under us
        fs::OpenOptions::new()
            .create_new(true)
            .write(true)
            .open(self.make_filepath(0))
    }

    fn current_file(&mut self) -> io::Result<&mut fs::File> {
        if self.should_rotate() {
            self.rotate()?;
        }

        Ok(self
            .current_file
            .as_mut()
            .expect("should've been created before"))
    }

    /// Manually rotate the file out
    ///
    /// This is the only way that a file whose `rotation_period` is [`RotationPeriod::Manual`] can rotate.
    ///
    /// # Errors
    ///
    /// Returns an error if one is encountered during creation of the new logfile.
    ///
    /// [`RotationPeriod::Manual`]: enum.RotationPeriod.html#variant.Manual
    pub fn rotate(&mut self) -> io::Result<()> {
        self.current_file = Some(self.create_file()?);
        self.rotation_tracker.reset();
        Ok(())
    }
}

impl Write for RotatingFile {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let written = self.current_file()?.write(buf)?;
        self.rotation_tracker.wrote(&buf[..written]);
        Ok(written)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.current_file()?.flush()
    }
}

#[cfg(test)]
mod tests {
    use std::fs;
    use std::num::NonZeroUsize;
    use std::path::Path;

    use proptest::prelude::*;

    use super::{RotatingFile, RotationPeriod};

    #[track_caller]
    fn assert_contains_files<P: AsRef<Path>>(
        directory: P,
        num: usize,
    ) -> Result<(), TestCaseError> {
        let p = directory.as_ref();
        prop_assert_eq!(
            fs::read_dir(p).unwrap().count(),
            num,
            "Directory {:?} did not contain {} file(s) (at {})",
            p,
            num,
            std::panic::Location::caller()
        );
        Ok(())
    }

    proptest! {
        #![proptest_config(ProptestConfig {
            cases: 15,
            ..ProptestConfig::default()
        })]

        #[test]
        fn test_max_files(name in "[a-zA-Z_-]+", n in 1..25usize) {
            let directory = tempfile::tempdir().unwrap();

            let mut file = RotatingFile::new(
                name,
                directory.path().to_owned(),
                RotationPeriod::Manual,
                NonZeroUsize::new(n).unwrap(),
                crate::Compression::None
            );

            assert_contains_files(&directory, 0)?;
            for i in 0..n {
                file.rotate().unwrap();
                assert_contains_files(&directory, i+1)?;
            }

            for _ in 0..n {
                assert_contains_files(&directory, n)?;
                file.rotate().unwrap();
            }
        }

        #[test]
        fn test_roundtrip_uncompressed(name in "[a-zA-Z_-]+", data: Vec<u8>) {
            use std::io::prelude::*;

            let directory = tempfile::tempdir().unwrap();
            let mut file = RotatingFile::new(
                name,
                directory.path().to_owned(),
                RotationPeriod::Manual,
                NonZeroUsize::new(10).unwrap(),
                crate::Compression::None
            );
            file.write_all(&data).unwrap();
            file.rotate().unwrap();
            file.write_all(&data).unwrap();
            drop(file);

            for entry in fs::read_dir(&directory).unwrap().map(Result::unwrap) {
                let path = entry.path();
                let read = fs::read(path).unwrap();
                prop_assert_eq!(&read, &data);
            }
        }

        #[test]
        fn test_roundtrip_zstd(name in "[a-zA-Z_-]+", n in 1..25usize, level in 0..21, data: Vec<u8>) {
            use std::io::prelude::*;

            let directory = tempfile::tempdir().unwrap();
            let mut file = RotatingFile::new(
                name,
                directory.path().to_owned(),
                RotationPeriod::Manual,
                NonZeroUsize::new(n * 10).unwrap(),
                crate::Compression::Zstd { level }
            );
            file.write_all(&data).unwrap();
            for i in 0..n {
                assert_contains_files(&directory, i + 1)?;
                file.rotate().unwrap();
                file.write_all(&data).unwrap();
            }
            assert_contains_files(&directory, n + 1)?;
            drop(file);

            for entry in fs::read_dir(&directory).unwrap().map(Result::unwrap) {
                let path = entry.path();
                let read = fs::read(&path).unwrap();
                if path.file_stem().unwrap().to_string_lossy().ends_with(".0") {
                    prop_assert_eq!(path.extension().unwrap().to_string_lossy(), "log");
                    prop_assert_eq!(&read, &data);
                } else {
                    prop_assert_eq!(path.extension().unwrap().to_string_lossy(), "zstd");
                    let read = zstd::decode_all(std::io::Cursor::new(read)).unwrap();
                    prop_assert_eq!(&read, &data);
                }
            }
        }
    }
}