File: apt-cacher-cleanup.pl

package info (click to toggle)
apt-cacher 0.9.4sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 268 kB
  • ctags: 224
  • sloc: perl: 1,862; makefile: 495; sh: 37
file content (145 lines) | stat: -rwxr-xr-x 4,083 bytes parent folder | download
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
#!/usr/bin/perl -w

# apt-cacher-cleanup.pl
# Script to clean the cache for the Apt-cacher package caching system.
# For more information visit www.apt-cacher.org
#
# Copyright (C) 2005, Eduard Bloch <blade@debian.org>
# Copyright (C) 2002-03, Jonathan Oxer <jon@debian.org>
# Portions  (C) 2002, Jacob Lundberg <jacob@chaos2.org>
# Distributed under the terms of the GNU Public Licence (GPL).


# add one argument like 1 to make it verbose

# do locking, not loosing files because someone redownloaded the index files
# right then
# use IO::Handle;
use Fcntl ':flock';
use IO::Handle;
use POSIX;

for(@ARGV) {
   if($_ eq "-n") { $nice_mode=1; }
   elsif ($_ eq "-v") { $verbose=1;}
   else {
      die <<EOM
Usage: $0 [ -n ] [ -v ]
      -n : nice mode, refresh index files first, then renice to 20 and continue
      -v : verbose mode
EOM
      ;
   }
}

sub printmsg {
   print @_ if $verbose;
}

#use strict;
#############################################################################
### configuration ###########################################################
# Include the library for the config file parser
require '/usr/share/apt-cacher/apt-cacher-lib.pl';

# Read in the config file and set the necessary variables
my $configfile = '/etc/apt-cacher/apt-cacher.conf';

my $configref;
eval {
        $configref = read_config($configfile);
};
my %config = %$configref;

# not sure what to do if we can't read the config file...
die "Could not read config file: $@" if $@;

# check whether we're actually meant to clean the cache
if ( $config{clean_cache} ne 1 ) {
	exit 0;
}

#############################################################################

my $refresh=1;

#my %valid;

@info = stat $config{cache_dir};

#if ($< != $info[4]) {
#   $refresh=0;
#   print "Not running with apt-cacher's user ID, won't refresh the index files.
#Try sth. like su -c /usr/share/apt-cacherapt-cacher-cleanup.pl www-data
#";
#   # that's not critical, will just hurt the apt-qupdate/apt-dupdate users
#}
#
#
### Preparation of the package lists ########################################

chdir "$config{cache_dir}/packages" && -w "." || die "Could not enter the cache dir";

@ifiles=(<*es.gz>, <*es.bz2>, <*es>);
for (@ifiles) {
   printmsg "Processing index: $_\n";
   die "Someone is cheating, bad filename found: $_" if /[^.\-\w]/;

   # preserve the index files
   $valid{$_}=1;

   # now refresh them, unless disabled by the setting above
   if($refresh) {
      $ENV{"REMOTE_ADDR"}="local";
      # if the path is stored there, better use that
      if(-s "../private/$_.complete") {
         open(my $tmp, "../private/$_.complete");
         $ENV{"PATH_INFO"}=<$tmp>;
         close $tmp;
      }
      if(!$ENV{"PATH_INFO"}) {
         my $tmp=$_;
         $tmp=~s/^/\//;
         $tmp=~s/_/\//;
      }
      if($ENV{"PATH_INFO"}) {
         system "/usr/share/apt-cacher/apt-cacher.pl >/dev/null";
         # fix the permissions, apt-cacher can have been executed as root
         chown $info[4], $info[5], $_, "../private/$_.complete", "../headers/$_";
      }
   }
}

setpriority 0, 0, 20 if $nice_mode;

# use the list of config files we already know
for(@ifiles) { extract_sums($_, \%valid);}

printmsg "Found ".scalar (keys %valid)." valid file entries\n";
#print join("\n",keys %valid);

for(<*.deb>, <*.bz2>, <*.gz>, <*.dsc>) {
   if(! defined($valid{$_})) {
      unlink $_, "../headers/$_", "../private/$_.complete";
      printmsg "Removing: $_ and company...\n";
   }
}

# similar thing for possibly remaining cruft
chdir "$config{cache_dir}/headers" && -w "." || die "Could not enter the cache dir";

for(<*.deb>, <*.bz2>, <*.gz>, <*.dsc>) {
   if(! defined($valid{$_})) {
      unlink $_, "../private/$_.complete";
      printmsg "Removing: $_ and company...\n";
   }
}

chdir "$config{cache_dir}/private" && -w "." || die "Could not enter the cache dir";
for(<*.deb.complete>, <*.bz2.complete>, <*.gz.complete>, <*.dsc.complete>) {
   s/.complete$//;
   if(! defined($valid{$_})) {
      printmsg "Removing: $_.complete\n";
      unlink "$_.complete";
   }
}