File Coverage

blib/lib/App/Yabsm/Snap.pm
Criterion Covered Total %
statement 17 40 42.5
branch 0 6 0.0
condition n/a
subroutine 6 7 85.7
pod 0 1 0.0
total 23 54 42.5


line stmt bran cond sub pod time code
1             # Author: Nicholas Hubbard
2             # WWW: https://github.com/NicholasBHubbard/yabsm
3             # License: MIT
4              
5             # Provides the &do_snap subroutine, which performs a single snap. This is a
6             # top-level subroutine that is scheduled to be run by the daemon.
7              
8 1     1   370 use strict;
  1         5  
  1         21  
9 1     1   3 use warnings;
  1         1  
  1         15  
10 1     1   6 use v5.16.3;
  1         2  
11              
12             package App::Yabsm::Snap;
13              
14 1     1   367 use App::Yabsm::Config::Query qw ( :ALL );
  1         2  
  1         329  
15 1         69 use App::Yabsm::Snapshot qw(take_snapshot
16             delete_snapshot
17             sort_snapshots
18             is_snapshot_name
19 1     1   335 );
  1         2  
20              
21 1     1   6 use Exporter qw(import);
  1         1  
  1         181  
22             our @EXPORT_OK = qw(do_snap);
23              
24             ####################################
25             # SUBROUTINES #
26             ####################################
27              
28             sub do_snap {
29              
30             # Perform a single $tframe snap of $snap.
31              
32 0     0 0   my $snap = shift;
33 0           my $tframe = shift;
34 0           my $config_ref = shift;
35              
36 0           my $mountpoint = snap_mountpoint($snap, $config_ref);
37 0           my $snap_dest = snap_dest($snap, $tframe, $config_ref);
38              
39 0           my $snapshot = take_snapshot($mountpoint, $snap_dest);
40              
41             # @snapshots is sorted from newest to oldest
42 0           my @snapshots = sort_snapshots(do {
43 0 0         opendir my $dh, $snap_dest or confess("yabsm: internal error: cannot opendir '$snap_dest'");
44 0           my @snapshots = grep { is_snapshot_name($_, ALLOW_BOOTSTRAP => 0) } readdir($dh);
  0            
45 0           map { $_ = "$snap_dest/$_" } @snapshots;
  0            
46 0           closedir $dh;
47 0           \@snapshots;
48             });
49              
50 0           my $num_snaps = scalar @snapshots;
51 0           my $to_keep = snap_timeframe_keep($snap, $tframe, $config_ref);
52              
53             # There is 1 more snap than should be kept because we just performed a snap.
54 0 0         if ($num_snaps == $to_keep + 1) {
    0          
55 0           my $oldest = pop @snapshots;
56 0           delete_snapshot($oldest);
57             }
58             # We havent reached the quota yet so we don't delete anything
59             elsif ($num_snaps <= $to_keep) {
60             ;
61             }
62             # User changed their settings to keep less snaps than they were keeping
63             # prior.
64             else {
65 0           for (; $num_snaps > $to_keep; $num_snaps--) {
66 0           my $oldest = pop @snapshots;
67 0           delete_snapshot($oldest);
68             }
69             }
70              
71 0           return $snapshot;
72             }
73              
74             1;