3 # Copyright (c) 2006 Anthony Towns <ajt@debian.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
18 use POSIX
qw(strftime
);
22 my $local_dir = "/srv/ftp.debian.org/mirror";
23 my $rsync_host = undef; #"merkel.debian.org";
24 my $rsync_dir = undef; #"debian";
26 my $dest = "/srv/ftp.debian.org/rsync/typical";
29 $ENV{"PATH"} = "/bin:/usr/bin";
33 my $hostname = `/bin/hostname -f`;
34 die "bad hostname" unless $hostname =~ m/^([a-zA-Z0-9._-]+)/;
37 my $lockfile = "./Archive-Update-in-Progress-$hostname";
39 unless (open LKFILE
, "> $dest/$lockfile" and flock(LKFILE
, LOCK_EX
)) {
40 print "$hostname is unable to start sync, lock file exists\n";
44 if (defined $rsync_host && defined $rsync_dir) {
45 system("rsync --links --hard-links --times --verbose --recursive"
46 ." --delay-updates --files-from :indices/files/typical.files"
47 ." rsync://$rsync_host/$rsync_dir/ $dest/");
49 open FILELIST
, "< $local_dir/indices/files/typical.files"
50 or die "typical.files index not found";
54 my @l = lstat("$local_dir/$_");
58 my $lpath = readlink("$local_dir/$_");
59 $lpath =~ m/^(.*)$/; $lpath = $1;
61 next if ($lpath eq readlink("$dest/$_"));
64 unless (mk_dirname_as_dirs
($dest, $_)) {
65 print "E: couldn't create path for $_\n";
70 rename "$dest/$_", "$dest/$_.remove" or print "E: couldn't rename old dir $_ out of the way\n";
71 } elsif (-e
"$dest/$_") {
72 unlink("$dest/$_") or print "E: couldn't unlink $_\n";
74 symlink($lpath, "$dest/$_") or print "E: couldn't create $_ as symlink to $lpath\n";
80 unless (mk_dirname_as_dirs
($dest, $_)) {
81 print "E: couldn't create path for $_\n";
85 my @d = lstat("$dest/$_");
88 rename("$dest/$_", "$dest/$_.remove") or print "E: couldn't rename old dir $_ out of the way\n";
90 next if (@l and @d and $l[0] == $d[0] and $l[1] == $d[1]);
91 #next if (@l and @d and $l[7] == $d[7]);
92 print "I: updating $_\n";
97 link("$local_dir/$_", "$dest/$_") or print "E: couldn't link $_\n";
102 print "Files synced, now deleting any unnecessary files\n";
104 my %expected_files = ();
105 open FILES
, "< $dest/indices/files/typical.files"
106 or die "typical.files index not found";
109 $expected_files{$_} = 1;
117 finddepth
({wanted
=> \
&wanted
, no_chdir
=> 1}, ".");
119 open TRACE
, "> $dest/project/trace/$hostname" or die "couldn't open trace";
120 print TRACE strftime
("%a %b %e %H:%M:%S UTC %Y", gmtime) . "\n";
124 unlink("$dest/$lockfile");
128 my ($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_);
130 if (substr($last, 0, length($_) + 1) ne "$_/") {
131 print "Deleting empty directory: $_\n";
138 } elsif ($_ =~ m
|^\
./project/trace
/| or $_ eq $lockfile) {
140 } elsif (defined $expected_files{$_}) {
142 } elsif ($del_count < $max_del) {
144 print "Deleting file: $_\n";
151 sub mk_dirname_as_dirs
{
152 my ($base, $file) = @_;
153 while ($file =~ m
,^/*([^/]+)/+([^/].*)$,) {
156 my @blah = lstat($base);
159 } elsif (-l _
or ! -d _
) {
160 print "SHOULD BE A DIRECTORY: $base\n";