Browse code

s3cmd get: delete a files if object_get had failed

's3cmd get' command opens destination files as 'ab' (append+write) before even trying to download. In effect, if the
file doesn't exist, s3cmd creates it.

This patch resolves an unwanted side-effect empty files being left by s3cmd after an error.
(e.g. file not found, no permission.)

Should only delete files that s3cmd created.

Signed-off-by: Oren Held <oren@held.org.il>

Oren Held authored on 2013/01/01 05:54:14
Showing 1 changed files
... ...
@@ -440,7 +440,14 @@ def cmd_object_get(args):
440 440
             except IOError, e:
441 441
                 error(u"Skipping %s: %s" % (destination, e.strerror))
442 442
                 continue
443
-        response = s3.object_get(uri, dst_stream, start_position = start_position, extra_label = seq_label)
443
+        try:
444
+            response = s3.object_get(uri, dst_stream, start_position = start_position, extra_label = seq_label)
445
+        except S3Error, e:
446
+            if not file_exists: # Delete, only if file didn't exist before!
447
+                debug(u"object_get failed for '%s', deleting..." % (destination,))
448
+                os.unlink(destination)
449
+                raise
450
+
444 451
         if response["headers"].has_key("x-amz-meta-s3tools-gpgenc"):
445 452
             gpg_decrypt(destination, response["headers"]["x-amz-meta-s3tools-gpgenc"])
446 453
             response["size"] = os.stat(destination)[6]