3 # /etc/ports/drivers/rsync: rsync(1) driver script for ports(8)
19 print "Error: $message ($!)\nUpdating failed\n";
26 print "Warning: $message ($!)\n";
31 print "Usage: $0 <file>\n";
35 open(FILE
, $ARGV[0]) or error
("Couldn't open $ARGV[0]");
39 if (/^host=(.*)/) { $host = $1; }
40 elsif (/^collection=(.*)/) { $collection = $1; }
41 elsif (/^destination=(.*)/) { $destination = $1; }
45 if ($host eq '') { error
("Host field not set in $ARGV[0]"); }
46 if ($collection eq '') { error
("Collection field not set in $ARGV[0]"); }
47 if ($destination eq '') { error
("Destination field not set in $ARGV[0]"); }
49 if (-e
"$destination/.checkouts")
51 # read the old .checkouts file into memory
52 open(FILE
, "$destination/.checkouts") or error
("Couldn't read checkouts from $destination/.checkouts");
56 $old_checkouts{$_} = 1;
61 print "Updating file list from " . $host . "::$collection\n";
63 # get the remote file list (new .checkouts)
64 open(PIPE
, 'rsync -crz ' . $host . '::' . $collection . '|') or error
("Couldn't open pipe to rsync");
69 next if /^MOTD:/; # ignore MOTD lines
70 s/^(.{43})//; # ignore the first 43 characters (mode, date etc...)
71 next if /^.$/; # ignore the . directory
73 $new_checkouts{$_} = 1;
76 error
("Running rsync failed") unless $? == 0;
78 print "Updating collection " . basename
($destination) . "\n";
80 # now really run rsync
81 open(PIPE
, 'rsync -crz --log-format "%o %n" ' . $host . "::$collection $destination|") or error
("Couldn't open pipe to rsync");
88 if ($old_checkouts{$1})
101 error
("Running rsync failed") unless $? == 0;
103 # save new checkouts into .checkouts
104 open(FILE
, ">$destination/.checkouts") or error
("Couldn't save checkouts to $destination/.checkouts");
105 foreach my $checkout (sort keys %new_checkouts)
107 print FILE
"$checkout\n";
111 # use chroot as an additional safety measure when removing files
112 chroot($destination) or error
("Couldn't chroot into $destination");
115 # iterate through old checkouts, remove obsolete files
116 foreach my $checkout (sort keys %old_checkouts)
118 if (!$new_checkouts{$checkout})
122 print " Delete $checkout\n";
123 unlink($checkout) or warning
("Couldn't delete $checkout");
128 # iterate through old checkouts, remove obsolete directories
129 foreach my $checkout (sort keys %old_checkouts)
131 if (!$new_checkouts{$checkout})
135 print " Delete $checkout\n";
136 rmdir($checkout) or warning
("Couldn't delete $checkout");
141 print "Finished successfully\n";