File Coverage

blib/lib/Filename/Backup.pm
Criterion Covered Total %
statement 31 31 100.0
branch 9 10 90.0
condition 2 2 100.0
subroutine 5 5 100.0
pod 1 1 100.0
total 48 49 97.9


line stmt bran cond sub pod time code
1             package Filename::Backup;
2              
3 2     2   389596 use 5.010001;
  2         11  
4 2     2   12 use strict;
  2         11  
  2         56  
5 2     2   14 use warnings;
  2         19  
  2         147  
6              
7 2     2   11 use Exporter qw(import);
  2         5  
  2         1722  
8              
9             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10             our $DATE = '2023-12-15'; # DATE
11             our $DIST = 'Filename-Backup'; # DIST
12             our $VERSION = '0.04'; # VERSION
13              
14             our @EXPORT_OK = qw(check_backup_filename);
15              
16             our %SPEC;
17              
18             our %SUFFIXES = (
19             '~' => 1,
20             '.bak' => 1,
21             '.old' => 1,
22             '.orig' => 1, # patch
23             '.rej' => 1, # patch
24             '.swp' => 1,
25             # XXX % (from /etc/mime.types)
26             # XXX sik? (from /etc/mime.types)
27             # XXX .dpkg*
28             # XXX .rpm*
29             );
30              
31             $SPEC{check_backup_filename} = {
32             v => 1.1,
33             summary => 'Check whether filename indicates being a backup file',
34             description => <<'_',
35              
36              
37             _
38             args => {
39             filename => {
40             schema => 'str*',
41             req => 1,
42             pos => 0,
43             },
44             # XXX recurse?
45             ignore_case => {
46             summary => 'Whether to match case-insensitively',
47             schema => 'bool',
48             default => 1,
49             },
50             },
51             result_naked => 1,
52             result => {
53             schema => ['any*', of=>['bool*', 'hash*']],
54             description => <<'_',
55              
56             Return false if not detected as backup name. Otherwise return a hash, which may
57             contain these keys: `original_filename`. In the future there will be extra
58             information returned, e.g. editor name (if filename indicates backup from
59             certain backup program), date (if filename contains date information), and so
60             on.
61              
62             _
63             },
64             examples => [
65             {
66             args=>{filename => 'foo.bar'},
67             },
68             {
69             args=>{filename => 'foo.bak'},
70             },
71             {
72             args=>{filename => 'foo.Bak', ignore_case => 0},
73             },
74             ],
75             };
76             sub check_backup_filename {
77 10     10 1 635085 my %args = @_;
78              
79 10         28 my $filename = $args{filename};
80 10         18 my $orig_filename;
81              
82 10 100       53 if ($filename =~ /\A#(.+)#\z/) {
83 1         4 $orig_filename = $1;
84 1         7 goto RETURN;
85             }
86              
87 9 50       81 $filename =~ /(~|\.\w+)\z/ or return 0;
88 9   100     44 my $ci = $args{ignore_case} // 1;
89              
90 9         28 my $suffix = $1;
91              
92 9         16 my $spec;
93 9 100       22 if ($ci) {
94 8         18 my $suffix_lc = lc($suffix);
95 8         33 for (keys %SUFFIXES) {
96 32 100       80 if (lc($_) eq $suffix_lc) {
97 7         14 $spec = $SUFFIXES{$_};
98 7         17 last;
99             }
100             }
101             } else {
102 1         4 $spec = $SUFFIXES{$suffix};
103             }
104 9 100       42 return 0 unless $spec;
105              
106 7         201 ($orig_filename = $filename) =~ s/\Q$suffix\E\z//;
107              
108 8         88 RETURN:
109             return {
110             original_filename => $orig_filename,
111             };
112             }
113              
114             1;
115             # ABSTRACT: Check whether filename indicates being a backup file
116              
117             __END__