This file is indexed.

/usr/bin/backup-manager-purge is in backup-manager 0.7.10.1-2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/usr/bin/perl
# Copyright © 2005-2010 Alexis Sukrieh
# See the AUTHORS file for details.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

use strict;
use warnings;

=pod

=head1 NAME

backup-manager-purge - backup-manager's wrapper for outdating files

=head1 SYNOPSIS

backup-manager-purge [TTL] <options> 

=head1 DESCRIPTION

B<backup-manager-purge> is the only authorized entity that can say if an archive
should be purged or not. Any tasks used by backup-manager may have to know if
an archive is deprecated (eg: the purging phase of an upload method). This tool
is here to fulfill that need.

Given a I<time to live (TTL)> and a list of archives, B<backup-manager-purge>
will return another list of archives, corresponding to the ones that are
outdated by the TTL. 

=head1 REQUIRED ARGS

=over 4

=item B<--ttl=>I<time-to-live>

Specify the time to live (in days) for the archives. Any archive that is older
than I<ttl> days will be outdated.

=back

=head1 OPTIONAL ARGS

=over 4

=item B<--files-from=>file

A file containing a list of archives to parse, one archive per line.
If this option is not used, STDIN will be used for catching the files to parse.

=back

=head1 RETURN

B<backup-manager-purge> will return the list of outdated files on STDOUT, one
file per line.

=head1 ERROR CODES

If an error occurs, it will print the error message on stderr and will exit with
an error code greater than 0.

Here are the possible error codes:

=over 4

=item bad command line (wrong arguments) : 10

=item internal error (should be reported as a bug) : 20

=back

=head1 SEE ALSO

backup-manager(8) backup-manager-upload(8)

=head1 AUTHORS

Concept and design by Alexis Sukrieh and Jan Metzger.

=cut

##############################################################
# Uses
##############################################################
use BackupManager::Config;
use BackupManager::Logger;
use BackupManager::Dialog;
use POSIX qw(strftime);
use File::Basename;
use Data::Dumper;

##############################################################
# Constants
##############################################################
use constant E_SUCCESS => 0;
use constant E_INVALID => 10;
use constant E_INTERNAL => 20;
use constant TRUE => 1;
use constant FALSE => 0;
use constant DIALOG_VERBOSE => 0;
use constant MSG_INTERNAL => "Internal system error, please report the bug.";

##############################################################
# Global variables
##############################################################
my $g_ttl = undef;
my $g_filelist = undef;
my @g_archives = ();
my @g_outdated = ();
my $g_fh = *STDIN;
my $g_rh_archives = {};

##############################################################
# Command line parsing
##############################################################
BackupManager::Config::getopt("$0 -ttl=<TTL> --files-from=<FILE>\n
--ttl|-t:        the time to live for outdating files
--files-from|-f: a file that contains the list of archives to process",
'ttl|t=s'         => \$g_ttl,
'files-from|f=s'  => \$g_filelist,
);

##############################################################
# Subs
##############################################################

# Takes an archive an returns all meta-data contained in its name
sub parse_archive ($)
{
    my ($archive) = @_;
    unless (defined $archive) {
        print_error MSG_INTERNAL;
        exit E_INTERNAL;
    }
    my ($prefix, $name, $date, $master, $filetype);
    $archive = basename ($archive);

    if ($archive =~ m/^\s*($ENV{BM_ARCHIVE_PREFIX})-?(\S+)?\.?(\d{8})\.(master\.)?(\S+)\s*$/) {
	($prefix, $name, $date, $master, $filetype) = ($1, $2, $3, $4, $5);
	$master = $master ? 1 : 0;
	$name = "$prefix-md5" if $filetype eq 'md5' and not $name;
    }

    # The archive pattern
    elsif ($archive =~ /^\s*([^-]+)-(\S+)\.(\d{8})\.(\S+)\s*$/) {
        $prefix = $1;
        $name = $2;
        $date = $3;
        my $suffix = $4;
        if ($suffix =~ /master\.(\S+)/) {
            $master = 1;
            $filetype = $1;
        }
        elsif ($suffix =~ /\.?(.+)/) {
            $master = 0;
            $filetype = $1;
        }
    }

    # The md5 file pattern
    elsif ($archive =~ /^\s*([^-]+)-(\d{8})\.md5\s*$/) {
        $prefix = $1;
        $name = "$prefix-md5";
        $date = $2;
        $filetype = "md5";
        $master = 0;
    }

    # Unknown pattern
    else {
        return undef;
    }
    
    return { prefix => $prefix, 
             name => $name, 
             date => $date, 
             master => $master, 
             filetype => $filetype};
}

# Takes a file handle and an array ref, parse the file's content
# and store in the array exiting filenames.
sub read_archives($$)
{
    my ($ra_archives, $fh) = @_;
    
    my $archive = "";
    while (<$fh>) {
        chomp();

        if (/^\s*(\S+)\s*$/) {
            $archive = $1;
        }
        
        my $rh_data = parse_archive ($archive);
        next unless defined $rh_data;
        next unless defined $rh_data->{date};
        
        if ($rh_data->{master}) {
            $g_rh_archives->{$rh_data->{name}}{pathByDateMasters}{$rh_data->{date}} = $archive;
        }
        $g_rh_archives->{$rh_data->{name}}{pathByDate}{$rh_data->{date}} = $archive;
        $g_rh_archives->{dataByPath}{$archive} = $rh_data;
        
        push @{$ra_archives}, $archive;
    }
}


# Takes an archive and a meta-data hash ref
# and return 1 if the archive is outded according to $g_ttl,
# 0 else.
sub outdate_master_archive($$$$$)
{
    my ($archive, $rh_meta, $purge_date,
        $ra_archives, $ra_outdated) = @_;
}


# Takes two array refs. Reads from the first one the list of archives 
# to process, and push in the second one the outdated archives.
sub outdate_archives($$)
{
    my ($ra_archives, $ra_outdated) = @_;
    unless (defined $ra_archives and
            defined $ra_outdated) {
        exit E_INTERNAL;
    }

    my $purge_date = strftime ('%Y%m%d', 
                               localtime(time() - $g_ttl * 24 * 3600)); 
    print_info "Outdating archives made before $purge_date";
    
    my %seen = ();
    foreach my $archive (sort @{$ra_archives}) {
        my $data = $g_rh_archives->{dataByPath}{$archive};
        next unless defined $data;
        next unless defined $data->{date};
        next if $seen{$archive};
        $seen{$archive} = 1;
       
        # if the date of the archive is older than $purge_date, we may have to outdate it
        # unless, nothing to do for that archive.
        next if ($data->{date} > $purge_date);

        # we can outdate a master only if a younger master exists
        if ($data->{master}) {
            foreach my $master_date (
                keys %{$g_rh_archives->{$data->{name}}{pathByDateMasters}}) {
                if ($master_date > $data->{date}) {
                    push @{$ra_outdated}, $archive;
                    last;
                }
            }
        }

        # here the archive is deprecated, its date is < to $purge_date
        else {
            # if BM_ARCHIVE_STRICTPURGE is true, we can only purge 
            # an archive prefixed with BM_ARCHIVE_PREFIX
            next if (($ENV{BM_ARCHIVE_STRICTPURGE} eq "true") and 
                     ($data->{prefix} ne $ENV{BM_ARCHIVE_PREFIX}));
            
            # now, we're sure we can outdate the archive
            push @{$ra_outdated}, $archive;
        }
    }
}

##############################################################
# Main
##############################################################

# Init
init_dialog (DIALOG_VERBOSE);

# Args check
unless (defined $g_ttl) {
    print_error "No TTL given";
    exit E_INVALID;
}

# In    
if (defined $g_filelist and -f $g_filelist) {
    print_info "Reading archives from $g_filelist";
    open $g_fh, $g_filelist or die "Unable to open $g_filelist";
}
else {
    print_info "Reading archives from STDIN";
}
read_archives (\@g_archives, $g_fh);

# Process
outdate_archives (\@g_archives, \@g_outdated);

# Out
foreach my $archive (@g_outdated) {
    print "$archive\n";
}

exit E_SUCCESS;