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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#[macro_use]
extern crate lazy_static;

use std::{fmt, io};
use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::fmt::{Display, Formatter};
use std::fs::{read_dir, remove_file};
use std::hash::Hash;
use std::path::{Path, PathBuf};

use log::{error, info};
use regex::Regex;
use termion::{color, style};
use termion::color::Fg;
use thiserror::Error;
use wildmatch::WildMatch;

#[derive(Debug, Error)]
pub struct IoError(io::Error);

impl Display for IoError {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        self.0.fmt(f)
    }
}

impl PartialEq for IoError {
    fn eq(&self, other: &Self) -> bool {
        self == other
    }
}

#[derive(Error, Debug, PartialEq)]
pub enum BackedUpError {
    #[error("Couldn't open directory {path}")]
    ReadDirError { source: IoError, path: PathBuf },
    #[error("At least one slot must be configured")]
    NoSlot,
    #[error("Invalid regex")]
    InvalidRegex(#[from] regex::Error),
    #[error("Regex missing capture group for \"{name}\". -- example: (?P<{name}>\\d{{2}})")]
    MissingCaptureGroup { name: String },
}

#[derive(Copy, Clone)]
pub struct SlotConfig {
    yearly: usize,
    monthly: usize,
    daily: usize,
    hourly: usize,
    minutely: usize,
}

impl SlotConfig {
    pub fn new(
        years: usize,
        months: usize,
        days: usize,
        hours: usize,
        minutes: usize,
    ) -> Result<Self, BackedUpError> {
        if years + months + days + hours + minutes == 0 {
            return Err(BackedUpError::NoSlot);
        }
        Ok(Self {
            yearly: years,
            monthly: months,
            daily: days,
            hourly: hours,
            minutely: minutes,
        })
    }
}

pub struct Config {
    slots: SlotConfig,
    pattern: Vec<WildMatch>,
    re: Regex,
}

impl Config {
    /// [String] pattern(s) will filter entries with wildcard expressions - see [WildMatch] for details
    /// An empty [Vec] implies no filter
    ///
    /// An optional regex [String] can be provided for parsing into timeslots.
    /// At least `year`, `month` and `day` must be provided as named groups
    pub fn new(
        slot_config: SlotConfig,
        pattern: &[String],
        re_str: Option<&str>,
    ) -> Result<Self, BackedUpError> {
        let pattern = pattern.into_iter().map(|s| WildMatch::new(s)).collect();
        let re = match re_str {
            None => (*RE).clone(),
            Some(s) => Regex::new(s).map_err(|e| BackedUpError::InvalidRegex(e))?,
        };
        let capture_names: Vec<_> = re.capture_names().flatten().collect();
        for i in ["year", "month", "day"].iter() {
            if !capture_names.contains(i) {
                return Err(BackedUpError::MissingCaptureGroup {
                    name: i.to_string(),
                });
            }
        }
        Ok(Self {
            slots: slot_config,
            pattern,
            re,
        })
    }
}

#[derive(Debug, Clone, PartialOrd, PartialEq, Eq, Hash)]
struct BackupEntry<'a> {
    year: u16,
    month: u8,
    day: u8,
    hour: u8,
    minute: u8,
    path: &'a Path,
}

impl<'a> BackupEntry<'a> {
    fn new(path: &'a Path, pattern: &[WildMatch], re: &Regex) -> Option<Self> {
        let filename = path.file_name()?.to_str()?;
        if !pattern.is_empty() && !pattern.iter().any(|w| w.matches(filename)) {
            return None;
        }
        let m = re.captures(filename)?;
        let year = m.name("year")?.as_str().parse().ok()?;
        let month = m.name("month")?.as_str().parse().ok()?;
        let day = m.name("day")?.as_str().parse().ok()?;
        let hour = m
            .name("hour")
            .and_then(|s| s.as_str().parse().ok())
            .unwrap_or(0);
        let minute = m
            .name("minute")
            .and_then(|s| s.as_str().parse().ok())
            .unwrap_or(0);
        Some(Self {
            year,
            month,
            day,
            hour,
            minute,
            path,
        })
    }

    fn get_ordering_tuple(&self) -> (u16, u8, u8, u8, u8) {
        (self.year, self.month, self.day, self.hour, self.minute)
    }
}

impl<'a> Ord for BackupEntry<'a> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.get_ordering_tuple().cmp(&other.get_ordering_tuple())
    }
}

#[derive(Copy, Clone, Debug)]
pub enum Period {
    Years,
    Months,
    Days,
    Hours,
    Minutes,
}

impl Period {
    fn to_string(&self) -> &'static str {
        match self {
            Period::Years => "Years",
            Period::Months => "Months",
            Period::Days => "Days",
            Period::Hours => "Hours",
            Period::Minutes => "Minutes",
        }
    }
}

lazy_static! {
    static ref RE: Regex = Regex::new(
        r"(?x)(?P<year>\d{4}) \D?
(?P<month>\d{2}) \D?
(?P<day>\d{2}) \D?
(
   # Optional components.
   (?P<hour>\d{2}) \D?
   (?P<minute>\d{2}) \D?
   (?P<second>\d{2})?
)?"
    )
    .unwrap();
}

/// Plan for keeping/removing [`PathBuf`] with configured slots.
///
/// [`PathBuf`] that are invalid strings aren't considered for either retention or deletion.
pub struct Plan {
    pub to_keep: Vec<PathBuf>,
    pub to_remove: Vec<PathBuf>,
    period_map: HashMap<PathBuf, Vec<Period>>,
}

impl Display for Plan {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        writeln!(f, "Plan to:\n")?;
        if self.to_keep.is_empty() && self.to_remove.is_empty() {
            writeln!(f, "\tDo nothing: no valid timestamps")?;
            return Ok(());
        }
        writeln!(
            f,
            "\t{}Keep {} file(s) matching {}period(s)",
            Fg(color::Green),
            &self.to_keep.len(),
            style::Reset
        )?;
        for i in &self.to_keep {
            write!(
                f,
                "\t\t{}{} {}",
                Fg(color::Green),
                i.to_str().unwrap(),
                style::Reset
            )?;
            let periods = self.period_map.get(i).unwrap();
            let periods: Vec<_> = periods.iter().map(|x| x.to_string()).collect();
            writeln!(f, "-> ({})", periods.join(","))?;
        }
        writeln!(f, "")?;
        writeln!(
            f,
            "\t{}Remove {} file(s) not matching periods",
            Fg(color::Red),
            &self.to_remove.len()
        )?;
        for i in &self.to_remove {
            writeln!(f, "\t\t{}", i.to_str().unwrap())?;
        }
        Ok(())
    }
}

impl Plan {
    pub fn new<P: AsRef<Path>>(config: &Config, path: P) -> Result<Self, BackedUpError> {
        let dir = read_dir(&path).map_err(|e| BackedUpError::ReadDirError {
            source: IoError(e),
            path: path.as_ref().to_path_buf(),
        })?;
        let entries: Vec<_> = dir.flatten().map(|x| x.path()).collect();
        Ok(Self::from(&config, &entries))
    }

    fn from(config: &Config, entries: &[PathBuf]) -> Self {
        let entries: BTreeSet<_> = entries
            .into_iter()
            .filter_map(|x| BackupEntry::new(x, &config.pattern, &config.re))
            .collect();
        let mut year_slots = BTreeMap::new();
        let mut month_slots = BTreeMap::new();
        let mut day_slots = BTreeMap::new();
        let mut hour_slots = BTreeMap::new();
        let mut minute_slots = BTreeMap::new();
        for entry in entries.iter().rev() {
            year_slots.insert(entry.year, entry);
            month_slots.insert((entry.year, entry.month), entry);
            day_slots.insert((entry.year, entry.month, entry.day), entry);
            hour_slots.insert((entry.year, entry.month, entry.day, entry.hour), entry);
            minute_slots.insert(
                (entry.year, entry.month, entry.day, entry.hour, entry.minute),
                entry,
            );
        }

        let mut to_keep = BTreeSet::new();
        let mut period_map: HashMap<PathBuf, Vec<Period>> = HashMap::new();
        let SlotConfig {
            yearly,
            monthly,
            daily,
            hourly,
            minutely,
        } = config.slots;
        for (_, entry) in year_slots.into_iter().rev().take(yearly) {
            to_keep.insert(entry.clone());
            period_map
                .entry(entry.path.to_path_buf())
                .or_default()
                .push(Period::Years);
        }
        for (_, entry) in month_slots.into_iter().rev().take(monthly) {
            to_keep.insert(entry.clone());
            period_map
                .entry(entry.path.to_path_buf())
                .or_default()
                .push(Period::Months);
        }
        for (_, entry) in day_slots.into_iter().rev().take(daily) {
            to_keep.insert(entry.clone());
            period_map
                .entry(entry.path.to_path_buf())
                .or_default()
                .push(Period::Days);
        }
        for (_, entry) in hour_slots.into_iter().rev().take(hourly) {
            to_keep.insert(entry.clone());
            period_map
                .entry(entry.path.to_path_buf())
                .or_default()
                .push(Period::Hours);
        }
        for (_, entry) in minute_slots.into_iter().rev().take(minutely) {
            to_keep.insert(entry.clone());
            period_map
                .entry(entry.path.to_path_buf())
                .or_default()
                .push(Period::Minutes);
        }

        let to_remove: Vec<_> = entries
            .difference(&to_keep)
            .map(|x| x.path.to_path_buf())
            .collect();
        let to_keep: Vec<_> = to_keep.into_iter().map(|x| x.path.to_path_buf()).collect();
        assert_eq!(entries.len(), &to_keep.len() + &to_remove.len());
        Self {
            to_keep,
            to_remove,
            period_map,
        }
    }

    /// Execute plan and remove timestamped files not matching any slots
    pub fn execute(&self) {
        if self.to_remove.is_empty() {
            info!("No file to remove")
        }
        for p in self.to_remove.iter() {
            let filename = p.to_str().unwrap();
            match remove_file(p) {
                Ok(_) => {
                    info!("removed file {}", filename)
                }
                Err(e) => {
                    error!("failed to remove file \"{}\": {}", filename, e)
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::ffi::OsString;

    use chrono::{DateTime, Duration, TimeZone, Utc};

    use super::*;

    fn create_test_data(
        fmt: &str,
        mut start_dt: DateTime<Utc>,
        days: usize,
        extension: &str,
    ) -> Vec<PathBuf> {
        let mut result = Vec::new();
        let fmt = format!("{}{}", fmt, extension);
        for _ in 0..days {
            let path = PathBuf::from(start_dt.format(&fmt).to_string());
            result.push(path);
            start_dt = start_dt - Duration::days(1);
        }
        result
    }

    #[test]
    fn test_make_plan() {
        let fmt = "%Y-%m-%d";
        let mut parsed_backups =
            create_test_data(fmt, Utc.ymd(2015, 1, 1).and_hms(0, 0, 0), 400, "");

        // no effect for number of matches until changing pattern
        parsed_backups.append(&mut create_test_data(
            fmt,
            Utc.ymd(2015, 1, 1).and_hms(0, 0, 0),
            30,
            ".log",
        ));
        let slot_config = SlotConfig::new(3, 0, 0, 0, 0).unwrap();
        let mut config = Config::new(slot_config, &vec![], None).unwrap();

        let plan = Plan::from(&config, &parsed_backups);
        assert_eq!(plan.to_keep.len(), 3);

        config.slots.monthly = 13;
        let plan = Plan::from(&config, &parsed_backups);
        assert_eq!(plan.to_keep.len(), 14);

        config.slots.daily = 30;
        let plan = Plan::from(&config, &parsed_backups);
        assert_eq!(plan.to_keep.len(), 43);

        config.pattern = vec![WildMatch::new("*.log")];
        let plan = Plan::from(&config, &parsed_backups);
        assert_eq!(plan.to_keep.len(), 30);
    }

    #[test]
    fn test_custom_regex() {
        let fmt = "%y%m%d";
        let parsed_backups = create_test_data(fmt, Utc.ymd(2015, 1, 1).and_hms(0, 0, 0), 400, "");
        let slot_config = SlotConfig::new(3, 13, 30, 0, 0).unwrap();
        let re_str = r"(?P<year>\d{2})(?P<month>\d{2})(?P<day>\d{2})";
        let config = Config::new(slot_config, &vec![], Some(re_str)).unwrap();
        let plan = Plan::from(&config, &parsed_backups);
        assert_eq!(plan.to_keep.len(), 43);
    }

    #[test]
    fn test_no_slot() {
        let slot_config = SlotConfig::new(0, 0, 0, 0, 0);
        assert_eq!(BackedUpError::NoSlot, slot_config.err().unwrap());
    }

    #[test]
    fn test_missing_named_group() {
        let slot_config = SlotConfig::new(1, 0, 0, 0, 0).unwrap();
        let re_str = r"(?P<month>\d{2})(?P<day>\d{2})";

        let config = Config::new(slot_config, &vec![], Some(re_str));
        assert_eq!(
            BackedUpError::MissingCaptureGroup {
                name: "year".to_string()
            },
            config.err().unwrap()
        );

        let re_str = r"(?P<year>\d{2})(?P<day>\d{2})";
        let config = Config::new(slot_config, &vec![], Some(re_str));
        assert_eq!(
            BackedUpError::MissingCaptureGroup {
                name: "month".to_string()
            },
            config.err().unwrap()
        );

        let re_str = r"(?P<year>\d{2})(?P<month>\d{2})";
        let config = Config::new(slot_config, &vec![], Some(re_str));
        assert_eq!(
            BackedUpError::MissingCaptureGroup {
                name: "day".to_string()
            },
            config.err().unwrap()
        );
    }

    #[test]
    fn test_invalid_regex() {
        let re_str = r"/(notaregex";
        let slot_config = SlotConfig::new(1, 0, 0, 0, 0).unwrap();
        let config = Config::new(slot_config, &vec![], Some(re_str));
        assert!(matches!(
            config.err().unwrap(),
            BackedUpError::InvalidRegex(_)
        ))
    }

    #[cfg(target_family = "unix")]
    #[test]
    fn test_invalid_utf_entry() {
        use std::os::unix::ffi::OsStringExt;
        let invalid_utf = b"2021-04-11\xe7";
        let path = PathBuf::from(OsString::from_vec(invalid_utf.to_vec()));
        let entry = BackupEntry::new(&path, &vec![], &RE);
        assert_eq!(entry, None);
    }
}