Skip to content

Commit

Permalink
S3: cleanup if key.get_contents_to_filename fails
Browse files Browse the repository at this point in the history
if key.get_contents_to_filename fails, it leaves an empty file and open file descriptor behind.  Instead, remove the file (since we created it, or at least truncated it to 0 length) and close the file descriptor.
  • Loading branch information
Jim Wilcoxson committed Dec 24, 2012
1 parent c3971e3 commit af97f2a
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions boto/s3/key.py
Expand Up @@ -1468,11 +1468,16 @@ def get_contents_to_filename(self, filename, headers=None,
http://goo.gl/EWOPb for details.
"""
fp = open(filename, 'wb')
self.get_contents_to_file(fp, headers, cb, num_cb, torrent=torrent,
version_id=version_id,
res_download_handler=res_download_handler,
response_headers=response_headers)
fp.close()
try:
self.get_contents_to_file(fp, headers, cb, num_cb, torrent=torrent,
version_id=version_id,
res_download_handler=res_download_handler,
response_headers=response_headers)
except Exception:
os.remove(filename)
raise
finally:
fp.close()
# if last_modified date was sent from s3, try to set file's timestamp
if self.last_modified != None:
try:
Expand Down

0 comments on commit af97f2a

Please sign in to comment.