1#!@PERL@
2# Copyright (c) 2008-2013 Zmanda, Inc.  All Rights Reserved.
3#
4# This program is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License
6# as published by the Free Software Foundation; either version 2
7# of the License, or (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12# for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc.,
16# 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17#
18# Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19# Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20
21use lib '@amperldir@';
22use strict;
23use warnings;
24
25use Getopt::Long;
26use Amanda::Config qw( :init :getconf config_dir_relative );
27use Amanda::Util qw( :constants );
28use Amanda::Paths;
29use Amanda::Constants;
30use Amanda::Process;
31use Amanda::Logfile;
32use Amanda::Holding;
33use Amanda::Debug qw( debug );
34my $kill_enable=0;
35my $process_alive=0;
36my $verbose=0;
37my $clean_holding=0;
38
39sub usage() {
40    print "Usage: amcleanupdisk [-v] [-r] conf\n";
41    exit 1;
42}
43
44Amanda::Util::setup_application("amcleanupdisk", "server", $CONTEXT_CMDLINE);
45
46my $config_overrides = new_config_overrides($#ARGV+1);
47
48debug("Arguments: " . join(' ', @ARGV));
49Getopt::Long::Configure(qw(bundling));
50GetOptions(
51    'version' => \&Amanda::Util::version_opt,
52    'v' => \$verbose,
53    'r' => \$clean_holding,
54    'help|usage' => \&usage,
55    'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
56) or usage();
57
58my $config_name = shift @ARGV or usage;
59
60set_config_overrides($config_overrides);
61config_init($CONFIG_INIT_EXPLICIT_NAME, $config_name);
62my ($cfgerr_level, @cfgerr_errors) = config_errors();
63if ($cfgerr_level >= $CFGERR_WARNINGS) {
64    config_print_errors();
65    if ($cfgerr_level >= $CFGERR_ERRORS) {
66	die("errors processing config file");
67    }
68}
69
70Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
71
72my $amcleanupdisk="$amlibexecdir/amcleanupdisk";
73
74if ( ! -e "$CONFIG_DIR/$config_name" ) {
75    die "Configuration directory '$CONFIG_DIR/$config_name' doesn't exist\n";
76}
77if ( ! -d "$CONFIG_DIR/$config_name" ) {
78    die "Configuration directory '$CONFIG_DIR/$config_name' is not a directory\n";
79}
80
81my $stdout;
82open($stdout, ">&STDOUT") if $verbose;;
83my @hfiles = Amanda::Holding::all_files($stdout);
84close $stdout if $verbose;
85@hfiles = Amanda::Holding::merge_all_files(@hfiles);
86while (@hfiles) {
87    my $hfile = pop @hfiles;
88    if ($hfile->{'header'}->{'type'} == $Amanda::Header::F_DUMPFILE) {
89	if ($hfile->{'filename'} =~ /\.tmp$/) {
90	    print "Rename tmp holding file: $hfile->{'filename'}\n" if $verbose;
91	    Amanda::Holding::rename_tmp($hfile->{'filename'}, 0);
92	} else {
93	    # normal holding file
94	}
95    } elsif ($hfile->{'header'}->{'type'} == $Amanda::Header::F_CONT_DUMPFILE) {
96	# orphan cont_dumpfile
97	if ($clean_holding) {
98	    print "Remove orphan chunk file: $hfile->{'filename'}\n" if $verbose;
99	    unlink $hfile->{'filename'};
100	} else {
101	    print "orphan chunk file: $hfile->{'filename'}\n" if $verbose;
102	}
103    } elsif ($hfile->{'header'}->{'type'} == $Amanda::Header::F_EMPTY) {
104	# empty file
105	if ($clean_holding) {
106	    print "Remove empty file: $hfile->{'filename'}\n" if $verbose;
107	    unlink $hfile->{'filename'};
108	} else {
109	    print "empty holding file: $hfile->{'filename'}\n" if $verbose;
110	}
111    } elsif ($hfile->{'header'}->{'type'} == $Amanda::Header::F_WEIRD) {
112	# weird file
113	if ($clean_holding) {
114	    print "Remove non amanda file: $hfile->{'filename'}\n" if $verbose;
115	    unlink $hfile->{'filename'};
116	} else {
117	    print "non amanda holding file: $hfile->{'filename'}\n" if $verbose;
118	}
119    } else {
120	# any other file
121	if ($clean_holding) {
122	    print "Remove file: $hfile->{'filename'}\n" if $verbose;
123	    unlink $hfile->{'filename'};
124	} else {
125	    print "unknown holding file: $hfile->{'filename'}\n" if $verbose;
126	}
127    }
128}
129
130Amanda::Holding::dir_unlink();
131
132Amanda::Util::finish_application();
133