+ # Upload to a new file, so if it fails we don't end up with
+ # a partially written file. Give the new file a completely
+ # random name so we have only a tiny chance of overwriting
+ # some existing file.
+ my $dirname = $filename;
+ $dirname =~ s{/[^/]+$}{/};
+
+ my @chars = ('a'..'z', 'A'..'Z', '0'..'9');
+ my $newname = $dirname;
+ foreach (0..7) {
+ $newname .= $chars[rand @chars];
+ }
+
+ $g->upload ($do_upload, $newname);
+
+ # Backup or overwrite?
+ $g->mv ($filename, "$filename$backup") if defined $backup;
+ $g->mv ($newname, $filename);
+