aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2011-09-27 17:27:34 -0700
committerH. Peter Anvin <hpa@zytor.com>2011-09-27 17:27:34 -0700
commit8ee6b7f1484166914d4e3ee5eb9d1d0198338589 (patch)
tree79e0a211723a19bd14da3e2e66fb1ced91889cc7
parent0fad268a96c4e3f14e951ff795d1e571c06a6d27 (diff)
downloadkup-8ee6b7f1484166914d4e3ee5eb9d1d0198338589.tar.gz
Add DELETE command
-rwxr-xr-xkorgupload69
-rwxr-xr-xkup28
2 files changed, 94 insertions, 3 deletions
diff --git a/korgupload b/korgupload
index 389ea0b..717a7ab 100755
--- a/korgupload
+++ b/korgupload
@@ -22,8 +22,10 @@
# - creates a new directory
# MOVE old-path new-path
# - moves <old-path> to <new-path>
-# LINK old-path:new-path
+# LINK old-path new-path
# - hard links <old-path> to <new-path>
+# DELETE old-path
+# - removes <old-path>
# DONE
# - optional command, terminates transaction
#
@@ -31,8 +33,6 @@
#
# SYMLINK old-path:new-path
# - symlinks <old-path> to <new-path>
-# DELETE old-path
-# - removes <old-path>
#
use strict;
@@ -697,6 +697,67 @@ sub move_or_link_file($@)
unlock_tree();
}
+sub delete_path(@)
+{
+ my(@args) = @_;
+
+ if (scalar(@args) != 1) {
+ fatal("Bad DELETE command");
+ }
+
+ my($file) = @args;
+
+ if (!is_valid_filename($file))
+ fatal("Invalid pathname in DELETE command");
+ }
+
+ if ($file =~ /\.(bz2|xz|sign)$/) {
+ fatal("DELETE of individual .bz2, .xz, or .sign files not supported");
+ }
+
+ lock_tree();
+
+ if (!-e $data_path.$file) {
+ fatal("DELETE of nonexistent object");
+ } elsif (-d $data_path.$file) {
+ if (!rmdir($data_path.$file)) {
+ fatal("DELETE of directory failed");
+ }
+ } elsif (-f $data_path.$file) {
+ if ($file =~ /^(.*)\.gz$/) {
+ my $stem = $1;
+
+ if ((-e $data_path.$stem.'.gz' && ! -f _) ||
+ (-e $data_path.$stem.'.bz2' && ! -f _) ||
+ (-e $data_path.$stem.'.xz' && ! -f _) ||
+ (-e $data_path.$stem.'.sign' && ! -f _)) {
+ fatal("DELETE encountered files and non-files");
+ }
+
+ if (!unlink($data_path.$stem.'.gz') ||
+ !unlink($data_path.$stem.'.bz2') ||
+ !unlink($data_path.$stem.'.xz') ||
+ !unlink($data_path.$from_stem.'.sign')) {
+ fatal("DELETE of compressed file failed");
+ }
+ } else {
+ if ((-e $data_path.$file && ! -f _) ||
+ (-e $data_path.$file.'.sign' && ! -f _)) {
+ fatal("DELETE encountered files and non-files");
+ }
+
+ if (!unlink($data_path.$file) ||
+ !unlink($data_path.$file.'.sign')) {
+ fatal("DELETE of plain file failed");
+ }
+ }
+ } else {
+ fatal("DELETE of non-directory/non-file not currently supported");
+ }
+
+ unlock_tree();
+}
+
my $line;
while (defined($line = <STDIN>)) {
# Ignore lines with only whitespace or starting with #
@@ -731,6 +792,8 @@ while (defined($line = <STDIN>)) {
do_mkdir(@args);
} elsif ($cmd eq 'MOVE' || $cmd eq 'LINK') {
move_or_link_file($cmd, @args);
+ } elsif ($cmd eq 'DELETE') {
+ delete_path(@args);
} elsif ($cmd eq 'DONE') {
last;
} else {
diff --git a/kup b/kup
index 3efd57e..a46d423 100755
--- a/kup
+++ b/kup
@@ -54,6 +54,7 @@ sub usage($) {
print STDERR " mkdir remote_path\n";
print STDERR " mv|move old_path new_path\n";
print STDERR " ln|link old_path new_path\n";
+ print STDERR " rm|delete old_path\n";
exit $err;
}
@@ -305,6 +306,31 @@ sub cmd_mkdir()
}
}
+# DELETE command
+sub cmd_delete()
+{
+ my $remote = shift @args;
+
+ if (!defined($remote)) {
+ usage(1);
+ }
+
+ if (!is_valid_filename($remote)) {
+ die "$0: invalid pathname: $remote\n";
+ }
+
+ if ($remote =~ /\.sign$/) {
+ die "$0: cannot delete .sign files directly\n";
+ }
+
+ # DWIM: .bz2, .xz -> .gz
+ $remote =~ s/\.(bz2|xz)$/\.gz/;
+
+ if ($real) {
+ print 'DELETE ', url_encode($remote), "\n";
+ }
+}
+
# MOVE or LINK command
sub cmd_move_link($)
{
@@ -371,6 +397,8 @@ sub process_commands()
cmd_move_link('MOVE');
} elsif ($cmd eq 'link' || $cmd eq 'ln') {
cmd_move_link('LINK');
+ } elsif ($cmd eq 'delete' || $cmd eq 'del' || $cmd eq 'rm') {
+ cmd_delete();
} else {
die "$0: unknown command: $cmd\n";
}