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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
//! The raw cache module manages directories of read-only files
//! subject to a (batched) Second Chance eviction policy.  Calling
//! [`prune`] deletes files to make sure a cache directory does not
//! exceed its capacity, in file count.  The deletions will obey a
//! Second Chance policy as long as insertions and updates go through
//! [`insert_or_update`] or [`insert_or_touch`], in order to update the
//! cached files' modification times correctly.  Opening the cached
//! file will automatically update its metadata to take that access
//! into account, but a path can also be [`touch`]ed explicitly.
//!
//! This module implements mechanisms, but does not hardcode any
//! policy... except the use of a second chance strategy.
use filetime::FileTime;
use std::fs::DirEntry;
use std::io::ErrorKind;
use std::io::Result;
use std::path::Path;
use std::path::PathBuf;

use crate::second_chance;

/// A `CachedFile` represents what we know about a given file in a raw
/// cache directory: its direntry, mtime, and atime.
struct CachedFile {
    entry: DirEntry,
    mtime: FileTime,
    accessed: bool,
}

/// Removes a file if it exists.
fn ensure_file_removed(path: &Path) -> Result<()> {
    match std::fs::remove_file(path) {
        Ok(()) => Ok(()),
        Err(e) if e.kind() == ErrorKind::NotFound => Ok(()),
        err => err,
    }
}

/// Moves the file at `path` to the back of the second chance list.
fn move_to_back_of_list(path: &Path) -> Result<()> {
    filetime::set_file_mtime(path, FileTime::now())
}

/// Marks the file at `path` as read-only.
fn set_read_only(path: &Path) -> Result<()> {
    let mut permissions = std::fs::symlink_metadata(path)?.permissions();

    permissions.set_readonly(true);
    std::fs::set_permissions(path, permissions)
}

/// Sets the access bit to true for the file at `path`: the next time
/// that file is up for eviction, it will get a second chance.  Returns
/// true if the file was found, false otherwise.
///
/// In most cases, there is no need to explicitly call this function:
/// the operating system will automatically perform the required
/// update while opening the file at `path`.
pub fn touch(path: impl AsRef<Path>) -> Result<bool> {
    fn run(path: &Path) -> Result<bool> {
        match filetime::set_file_atime(path, FileTime::now()) {
            Ok(()) => Ok(true),
            // It's OK if the file we're trying to touch was removed:
            // things do disappear from caches.
            Err(e) if e.kind() == ErrorKind::NotFound => Ok(false),
            Err(e) => Err(e),
        }
    }

    run(path.as_ref())
}

/// Consumes the file `from` and publishes it to the raw cache file
/// `to`, a file directly under the raw cache directory.
///
/// On success, `from` is deleted.  On failure, the cache directory is
/// always in a valid state.
///
/// The rename will be atomic, but the source file must be private to
/// the caller: this function accesses the source file multiple time.
///
/// If durability is necessary, the caller is responsible for
/// `sync_data`ing the contents of `from`.  This function does not
/// fsync the cache directory itself: it's a cache, so stale contents
/// are assumed safe.
pub fn insert_or_update(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<()> {
    fn run(from: &Path, to: &Path) -> Result<()> {
        // Move to the back of the list before publishing: if a reader
        // comes in right away, we want it to set the access bit.
        move_to_back_of_list(from)?;
        set_read_only(from)?;
        std::fs::rename(from, to)?;
        ensure_file_removed(from)
    }

    run(from.as_ref(), to.as_ref())
}

/// Consumes the file `from` and publishes it to the raw cache file
/// `to`, a file directly under the raw cache directly, only if that
/// file does not already exist.
///
/// If the file exists, `from` is still consumed, but `to` is not
/// updated; instead, its access bit is set to true.
///
/// On success, `from` is deleted.  On failure, the cache directory is
/// always in a valid state.
///
/// The link will be atomic, but the source file must be private to
/// the caller: this function accesses the source file multiple times.
///
/// If durability is necessary, the caller is responsible for
/// `sync_data`ing the contents of `from`.  This function does not
/// fsync the cache directory itself: it's a cache, so stale contents
/// are assumed safe.
pub fn insert_or_touch(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<()> {
    fn run(from: &Path, to: &Path) -> Result<()> {
        // Optimise for the successful publication case: we expect callers
        // to only `insert_or_touch` after a failed lookup, so the `link`
        // call will only fail with EEXIST if another writer raced with us.
        move_to_back_of_list(from)?;
        set_read_only(from)?;
        match std::fs::hard_link(from, to) {
            Ok(()) => {}
            // The destination file already exists; we just have to mark
            // it as accessed.
            Err(e) if e.kind() == ErrorKind::AlreadyExists => {
                touch(to)?;
            }
            err => err?,
        }

        ensure_file_removed(from)
    }

    run(from.as_ref(), to.as_ref())
}

impl second_chance::Entry for CachedFile {
    type Rank = FileTime;

    #[inline]
    fn rank(&self) -> FileTime {
        self.mtime
    }

    #[inline]
    fn accessed(&self) -> bool {
        self.accessed
    }
}

impl CachedFile {
    fn new(entry: DirEntry, meta: &std::fs::Metadata) -> CachedFile {
        let atime = FileTime::from_last_access_time(meta);
        let mtime = FileTime::from_last_modification_time(meta);

        CachedFile {
            entry,
            mtime,
            accessed: atime > mtime,
        }
    }
}

/// Attempts to list all the cached files directly under `cache_dir`.
///
/// On success, returns the list of cached files and an estimate of
/// the number of files under `cache_dir`.
///
/// That estimate is always greater than or equal to the number of
/// `CachedFile`s returned in the vector.
fn collect_cached_files(cache_dir: &Path) -> Result<(Vec<CachedFile>, u64)> {
    let mut cache = Vec::new();
    let mut count = 0;
    for maybe_entry in std::fs::read_dir(cache_dir)? {
        count += 1;
        if let Ok(entry) = maybe_entry {
            let meta = match entry.metadata() {
                Ok(meta) => meta,
                Err(e) if e.kind() == ErrorKind::NotFound => continue,
                err => err?,
            };

            if meta.is_dir() {
                // Don't count subdirectories, we never delete them.
                count -= 1;
            } else {
                cache.push(CachedFile::new(entry, &meta));
            }
        }
    }

    // We increment before pushing to `cache`.
    assert!(count >= cache.len() as u64);
    Ok((cache, count))
}

/// Applies the `update` to files in `parent`.
fn apply_update(parent: PathBuf, update: second_chance::Update<CachedFile>) -> Result<()> {
    // When the `parent` path is long, we can spend most of our time
    // constructing paths with `DirEntry::path`.  Directly push/pop
    // after the parent directory instead.
    let mut cached = parent;

    for entry in update.to_evict {
        cached.push(entry.entry.file_name());
        ensure_file_removed(&cached)?;
        cached.pop();
    }

    for entry in update.to_move_back {
        cached.push(entry.entry.file_name());
        match move_to_back_of_list(&cached) {
            Ok(()) => {}
            // Silently ignore ENOENT: things do disappear from caches.
            Err(e) if e.kind() == ErrorKind::NotFound => {}
            err => err?,
        }
        cached.pop();
    }

    Ok(())
}

/// Attempts to prune the contents of `cache_dir` down to at most
/// `capacity` files, with a second chance policy.
///
/// On success, returns an estimate of the number of files remaining
/// in `cache_dir` and the number of files deleted.
///
/// Ignores any subdirectory of `cache_dir`.
pub fn prune(cache_dir: PathBuf, capacity: usize) -> Result<(u64, usize)> {
    let (cached_files, count) = collect_cached_files(&cache_dir)?;
    let update = second_chance::Update::new(cached_files, capacity);
    let num_evicted = update.to_evict.len();

    // `CachedFile` doesn't implement `Clone` (nor can `Update::new`
    // assume the trait is available), so the values in `to_evict`
    // must all come from `cached_files`; since `cached_files.len() <=
    // count`, we must find `num_evicted <= count`.
    assert!(num_evicted as u64 <= count);

    apply_update(cache_dir, update)?;
    Ok((count - (num_evicted as u64), num_evicted))
}

/// Removing a file should remove that file.
#[test]
fn test_remove_file() {
    use test_dir::{DirBuilder, FileType, TestDir};
    let temp = TestDir::temp().create("cache_file", FileType::ZeroFile(10));

    let path = temp.path("cache_file");
    assert!(std::fs::metadata(&path).is_ok());
    assert!(ensure_file_removed(&path).is_ok());

    assert!(matches!(std::fs::metadata(&path),
                     Err(e) if e.kind() == ErrorKind::NotFound));

    // Removing a file that does not exist is ok.
    assert!(ensure_file_removed(&path).is_ok());
    assert!(matches!(std::fs::metadata(&path),
                     Err(e) if e.kind() == ErrorKind::NotFound));
}

/// ensure_file_removed should succeed if the file is already gone.
#[test]
fn test_remove_inexistent_file() {
    use test_dir::{DirBuilder, TestDir};
    let temp = TestDir::temp();

    let path = temp.path("cache_file");
    assert!(matches!(std::fs::metadata(&path),
                     Err(e) if e.kind() == ErrorKind::NotFound));

    // Removing a file that does not exist is ok.
    assert!(ensure_file_removed(&path).is_ok());
    assert!(matches!(std::fs::metadata(&path),
                     Err(e) if e.kind() == ErrorKind::NotFound));
}

/// Ensures enough time has elapsed for atime / mtime to change: some
/// file systems only work at second granularity.
#[cfg(test)]
fn advance_time() {
    std::thread::sleep(std::time::Duration::from_secs_f64(1.5));
}

/// Moving to the back of the list should increase the file's rank and
/// not set the accessed bit.
#[test]
fn test_back_of_list() {
    use crate::second_chance::Entry;
    use test_dir::{DirBuilder, FileType, TestDir};

    let temp = TestDir::temp().create("old_cache_file", FileType::ZeroFile(10));

    let path = temp.path("old_cache_file");

    let get_entry = || {
        let (mut files, count) =
            collect_cached_files(path.parent().unwrap()).expect("directory listing must succeed");
        assert_eq!(count, 1);
        assert_eq!(files.len(), 1);

        files.pop().expect("vec is non-empty")
    };

    let old_entry = get_entry();
    advance_time();
    move_to_back_of_list(&path).expect("call should succeed");
    let new_entry = get_entry();

    assert!(new_entry.rank() > old_entry.rank());
    assert!(!new_entry.accessed());
}

/// Setting a file read only should... make it read only.
#[test]
fn test_set_read_only() {
    use test_dir::{DirBuilder, FileType, TestDir};

    let temp = TestDir::temp().create("old_cache_file", FileType::ZeroFile(10));

    let path = temp.path("old_cache_file");

    // The file should be initially read-write
    {
        let permissions = std::fs::metadata(&path)
            .expect("metadata should succeed")
            .permissions();

        assert!(!permissions.readonly());
    }

    set_read_only(&path).expect("set_read_only should succeed");

    // The file should now be read-only.
    {
        let permissions = std::fs::metadata(&path)
            .expect("metadata should succeed")
            .permissions();

        assert!(permissions.readonly());
    }
}

/// Touching should set the accessed bit, but not change the rank.
#[test]
fn test_touch() {
    use crate::second_chance::Entry;
    use test_dir::{DirBuilder, FileType, TestDir};

    let temp = TestDir::temp().create("old_cache_file", FileType::ZeroFile(10));

    let path = temp.path("old_cache_file");

    let get_entry = || {
        let (mut files, count) =
            collect_cached_files(path.parent().unwrap()).expect("directory listing must succeed");
        assert_eq!(count, 1);
        assert_eq!(files.len(), 1);

        files.pop().expect("vec is non-empty")
    };

    move_to_back_of_list(&path).expect("call should succeed");
    let old_entry = get_entry();
    assert!(!old_entry.accessed());

    advance_time();
    // Should return true: the file exists.
    assert!(touch(&path).expect("call should succeed"));
    let new_entry = get_entry();

    assert_eq!(new_entry.rank(), old_entry.rank());
    assert!(new_entry.accessed());
}

/// Touching should set the accessed bit, but not change the rank.
#[test]
fn test_touch_missing() {
    use test_dir::{DirBuilder, TestDir};

    let temp = TestDir::temp();
    // Should return file: the file does not exist.
    assert!(!touch(&temp.path("absent")).expect("should succeed on missing files"));
}

/// Reading a file should set the accessed bit, but not change the rank.
#[test]
fn test_touch_by_open() {
    use crate::second_chance::Entry;
    use test_dir::{DirBuilder, FileType, TestDir};

    let temp = TestDir::temp().create("old_cache_file", FileType::ZeroFile(10));

    let path = temp.path("old_cache_file");

    let get_entry = || {
        let (mut files, count) =
            collect_cached_files(path.parent().unwrap()).expect("directory listing must succeed");
        assert_eq!(count, 1);
        assert_eq!(files.len(), 1);

        files.pop().expect("vec is non-empty")
    };

    move_to_back_of_list(&path).expect("call should succeed");
    let old_entry = get_entry();
    assert!(!old_entry.accessed());

    advance_time();
    let _ = std::fs::read(&path).expect("read should succeed");
    let new_entry = get_entry();

    assert_eq!(new_entry.rank(), old_entry.rank());
    assert!(new_entry.accessed());
}

/// Moving to the back of the list should clear the access bit.
#[test]
fn test_back_of_list_after_touch() {
    use crate::second_chance::Entry;
    use test_dir::{DirBuilder, FileType, TestDir};

    let temp = TestDir::temp().create("old_cache_file", FileType::ZeroFile(10));

    let path = temp.path("old_cache_file");

    let get_entry = || {
        let (mut files, count) =
            collect_cached_files(path.parent().unwrap()).expect("directory listing must succeed");
        assert_eq!(count, 1);
        assert_eq!(files.len(), 1);

        files.pop().expect("vec is non-empty")
    };

    advance_time();
    touch(&path).expect("call should succeed");
    let old_entry = get_entry();
    assert!(old_entry.accessed());

    advance_time();
    move_to_back_of_list(&path).expect("call should succeed");
    let new_entry = get_entry();

    assert!(new_entry.rank() > old_entry.rank());
    assert!(!new_entry.accessed());
}

/// Moving to the back of the list should clear the access bit, even when
/// set by opening the file for read.
#[test]
fn test_back_of_list_after_open() {
    use crate::second_chance::Entry;
    use test_dir::{DirBuilder, FileType, TestDir};

    let temp = TestDir::temp().create("old_cache_file", FileType::ZeroFile(10));

    let path = temp.path("old_cache_file");

    let get_entry = || {
        let (mut files, count) =
            collect_cached_files(path.parent().unwrap()).expect("directory listing must succeed");
        assert_eq!(count, 1);
        assert_eq!(files.len(), 1);

        files.pop().expect("vec is non-empty")
    };

    advance_time();
    let _ = std::fs::read(&path).expect("read should succeed");
    let old_entry = get_entry();
    assert!(old_entry.accessed());

    advance_time();
    move_to_back_of_list(&path).expect("call should succeed");
    let new_entry = get_entry();

    assert!(new_entry.rank() > old_entry.rank());
    assert!(!new_entry.accessed());
}

/// Inserting a new file should move to the destination, and mark the
/// file as not yet accessed.
#[test]
fn test_insert_empty() {
    use crate::second_chance::Entry;
    use test_dir::{DirBuilder, FileType, TestDir};

    let temp = TestDir::temp().create("temp_file", FileType::RandomFile(10));

    let path = temp.path("temp_file");
    let dst = temp.path("cache");

    let get_entry = || {
        let (mut files, count) =
            collect_cached_files(path.parent().unwrap()).expect("directory listing must succeed");
        assert_eq!(count, 1);
        assert_eq!(files.len(), 1);

        files.pop().expect("vec is non-empty")
    };

    // Read *after* the file is created, to make sure it would have
    // the accessed bit set.
    advance_time();
    let payload = std::fs::read(&path).expect("read should succeed");

    insert_or_update(&path, &dst).expect("insert_or_touch should succeed");
    // The old file should be gone.
    assert!(matches!(std::fs::metadata(&path),
                     Err(e) if e.kind() == ErrorKind::NotFound));

    // The destination file should not be marked as accessed.
    assert!(!get_entry().accessed());
    // The destination file should have the original payload.
    assert_eq!(&payload, &std::fs::read(&dst).expect("read should succeed"));
    // The destination file should now be read-only.
    assert!(std::fs::metadata(&dst)
        .expect("metadata should succeed")
        .permissions()
        .readonly());
}

/// Inserting a new file over an old one should overwrite, and mark
/// the file as not yet accessed.
#[test]
fn test_insert_overwrite() {
    use crate::second_chance::Entry;
    use test_dir::{DirBuilder, FileType, TestDir};

    let temp = TestDir::temp()
        .create("temp_file", FileType::RandomFile(10))
        .create("cache", FileType::ZeroFile(100));

    let path = temp.path("temp_file");
    let dst = temp.path("cache");

    let get_entry = || {
        let (mut files, count) =
            collect_cached_files(path.parent().unwrap()).expect("directory listing must succeed");
        assert_eq!(count, 1);
        assert_eq!(files.len(), 1);

        files.pop().expect("vec is non-empty")
    };

    // Read *after* the file is created, to make sure it would have
    // the accessed bit set.
    advance_time();
    let payload = std::fs::read(&path).expect("read should succeed");

    insert_or_update(&path, &dst).expect("insert_or_touch should succeed");
    // The old file should be gone.
    assert!(matches!(std::fs::metadata(&path),
                     Err(e) if e.kind() == ErrorKind::NotFound));

    // The destination file should not be marked as accessed.
    assert!(!get_entry().accessed());
    // The destination file should have the original payload.
    assert_eq!(&payload, &std::fs::read(&dst).expect("read should succeed"));
}

/// Inserting a new file should move to the destination, and mark the
/// file as not yet accessed.
#[test]
fn test_insert_or_touch_empty() {
    use crate::second_chance::Entry;
    use test_dir::{DirBuilder, FileType, TestDir};

    let temp = TestDir::temp().create("temp_file", FileType::RandomFile(10));

    let path = temp.path("temp_file");
    let dst = temp.path("cache");

    let get_entry = || {
        let (mut files, count) =
            collect_cached_files(path.parent().unwrap()).expect("directory listing must succeed");
        assert_eq!(count, 1);
        assert_eq!(files.len(), 1);

        files.pop().expect("vec is non-empty")
    };

    // Read *after* the file is created, to make sure it would have
    // the accessed bit set.
    advance_time();
    let payload = std::fs::read(&path).expect("read should succeed");

    insert_or_touch(&path, &dst).expect("insert_or_touch should succeed");
    // The old file should be gone.
    assert!(matches!(std::fs::metadata(&path),
                     Err(e) if e.kind() == ErrorKind::NotFound));

    // The destination file should not be marked as accessed.
    assert!(!get_entry().accessed());
    // The destination file should have the original payload.
    assert_eq!(&payload, &std::fs::read(&dst).expect("read should succeed"));
    // The destination file should now be read-only.
    assert!(std::fs::metadata(&dst)
        .expect("metadata should succeed")
        .permissions()
        .readonly());
}

/// insert_or_touch'ing over an old file should not overwrite, but
/// mark the file as newly accessed.
#[test]
fn test_insert_touch_overwrite() {
    use crate::second_chance::Entry;
    use test_dir::{DirBuilder, FileType, TestDir};

    let temp = TestDir::temp()
        .create("temp_file", FileType::RandomFile(10))
        .create("cache", FileType::RandomFile(10));

    let path = temp.path("temp_file");
    let dst = temp.path("cache");

    let get_entry = || {
        let (mut files, count) =
            collect_cached_files(path.parent().unwrap()).expect("directory listing must succeed");
        assert_eq!(count, 1);
        assert_eq!(files.len(), 1);

        files.pop().expect("vec is non-empty")
    };

    let payload = std::fs::read(&dst).expect("read should succeed");
    // Clear the access bit.
    move_to_back_of_list(&dst).expect("move to back should succeed");

    advance_time();

    insert_or_touch(&path, &dst).expect("insert_or_touch should succeed");
    // The old file should be gone.
    assert!(matches!(std::fs::metadata(&path),
                     Err(e) if e.kind() == ErrorKind::NotFound));

    // The destination file should be marked as accessed.
    assert!(get_entry().accessed());
    // The destination file should have the original payload.
    assert_eq!(&payload, &std::fs::read(&dst).expect("read should succeed"));
}

/// Smoke test that we find the order we expect.
#[test]
fn test_collect_cached_files() {
    use crate::second_chance::Entry;
    use std::ffi::OsString;
    use test_dir::{DirBuilder, FileType, TestDir};

    let temp = TestDir::temp()
        .create("a", FileType::RandomFile(10))
        .create("b", FileType::RandomFile(10))
        .create("c", FileType::RandomFile(10))
        .create("d", FileType::RandomFile(10))
        // The directory should be ignored.
        .create("a_directory", FileType::Dir);

    // Set up the order: d, a, c, b
    advance_time();
    move_to_back_of_list(&temp.path("d")).expect("should succeed");
    advance_time();
    move_to_back_of_list(&temp.path("a")).expect("should succeed");
    advance_time();
    move_to_back_of_list(&temp.path("c")).expect("should succeed");
    advance_time();
    move_to_back_of_list(&temp.path("b")).expect("should succeed");

    // Touch d and c.
    advance_time();
    touch(&temp.path("d")).expect("should succeed");
    touch(&temp.path("c")).expect("should succeed");

    let (mut cached, count) = collect_cached_files(&temp.path(".")).expect("should succeed");

    assert_eq!(count, 4);
    cached.sort_by_key(|e| e.rank());

    assert_eq!(
        cached
            .iter()
            .map(|e| e.entry.file_name())
            .collect::<Vec<OsString>>(),
        vec!["d", "a", "c", "b"]
    );

    assert_eq!(
        cached.iter().map(|e| e.accessed()).collect::<Vec<bool>>(),
        vec![true, false, true, false]
    );
}

/// Similar setup, but now directly delete / move files.
#[test]
fn test_apply_update() {
    use crate::second_chance::Entry;
    use std::ffi::OsString;
    use test_dir::{DirBuilder, FileType, TestDir};

    let temp = TestDir::temp()
        .create("a", FileType::RandomFile(10))
        .create("b", FileType::RandomFile(10))
        .create("c", FileType::RandomFile(10))
        .create("d", FileType::RandomFile(10))
        // We will add these files to the update before deleting them;
        // `apply_update` shouldn't blow up.
        .create("deleted_touch", FileType::RandomFile(10))
        .create("already_deleted", FileType::RandomFile(10));

    // Set up the order: d, a, c, b
    advance_time();
    move_to_back_of_list(&temp.path("d")).expect("should succeed");
    advance_time();
    move_to_back_of_list(&temp.path("a")).expect("should succeed");
    advance_time();
    move_to_back_of_list(&temp.path("c")).expect("should succeed");
    advance_time();
    move_to_back_of_list(&temp.path("b")).expect("should succeed");

    // Move deleted_touch and already_deleted to the back.
    advance_time();
    move_to_back_of_list(&temp.path("deleted_touch")).expect("should succeed");
    advance_time();
    move_to_back_of_list(&temp.path("already_deleted")).expect("should succeed");

    // Touch d and c.
    advance_time();
    touch(&temp.path("d")).expect("should succeed");
    touch(&temp.path("c")).expect("should succeed");

    // move d to the back, delete a.
    {
        let (mut cached, count) = collect_cached_files(&temp.path(".")).expect("should succeed");

        assert_eq!(count, 6);
        cached.sort_by_key(|e| e.rank());

        assert_eq!(
            cached
                .iter()
                .map(|e| e.entry.file_name())
                .collect::<Vec<OsString>>(),
            vec!["d", "a", "c", "b", "deleted_touch", "already_deleted"]
        );

        let mut to_evict = Vec::new();
        let mut to_move_back = Vec::new();

        to_move_back.extend(cached.drain(0..1));
        to_evict.extend(cached.drain(0..1));
        to_evict.push(cached.pop().expect("is non-empty"));
        to_move_back.push(cached.pop().expect("is non-empty"));

        // Delete these files before applying the update; it should
        // just ignore their entries.
        std::fs::remove_file(&temp.path("deleted_touch")).expect("deletion should succeed");
        std::fs::remove_file(&temp.path("already_deleted")).expect("deletion should succeed");

        apply_update(
            temp.path("."),
            second_chance::Update {
                to_evict,
                to_move_back,
            },
        )
        .expect("update should succeed");
    }

    let (mut cached, count) = collect_cached_files(&temp.path(".")).expect("should succeed");

    assert_eq!(count, 3);
    cached.sort_by_key(|e| e.rank());

    // We should find 3 files, in this order
    assert_eq!(
        cached
            .iter()
            .map(|e| e.entry.file_name())
            .collect::<Vec<OsString>>(),
        vec!["c", "b", "d"]
    );

    // And "d"'s accessed bit should be reset.
    assert_eq!(
        cached.iter().map(|e| e.accessed()).collect::<Vec<bool>>(),
        vec![true, false, false]
    );
}

/// Same setup, but now directly delete / move files.
#[test]
fn test_prune() {
    use crate::second_chance::Entry;
    use std::ffi::OsString;
    use test_dir::{DirBuilder, FileType, TestDir};

    let temp = TestDir::temp()
        .create("a", FileType::RandomFile(10))
        .create("b", FileType::RandomFile(10))
        .create("c", FileType::RandomFile(10))
        .create("d", FileType::RandomFile(10));

    // Set up the order: d, a, c, b
    advance_time();
    move_to_back_of_list(&temp.path("d")).expect("should succeed");
    advance_time();
    move_to_back_of_list(&temp.path("a")).expect("should succeed");
    advance_time();
    move_to_back_of_list(&temp.path("c")).expect("should succeed");
    advance_time();
    move_to_back_of_list(&temp.path("b")).expect("should succeed");

    // Touch d and c.
    advance_time();
    touch(&temp.path("d")).expect("should succeed");
    touch(&temp.path("c")).expect("should succeed");

    // With capacity for 3 files, we should move "d" to the back,
    // and delete "a".
    //
    // That leaves us with 3 files (1 deletion).
    assert_eq!(
        prune(temp.path("."), 3).expect("prune should succeed"),
        (3, 1)
    );

    let (mut cached, count) = collect_cached_files(&temp.path(".")).expect("should succeed");

    assert_eq!(count, 3);
    cached.sort_by_key(|e| e.rank());

    // We should find 3 files, in this order
    assert_eq!(
        cached
            .iter()
            .map(|e| e.entry.file_name())
            .collect::<Vec<OsString>>(),
        vec!["c", "b", "d"]
    );

    // And "d"'s accessed bit should be reset.
    assert_eq!(
        cached.iter().map(|e| e.accessed()).collect::<Vec<bool>>(),
        vec![true, false, false]
    );
}