Skip to content

Commit

Permalink
Handle running out of inodes
Browse files Browse the repository at this point in the history
The "except Exception" is too general for the makedirs
call.  Instead we only catch the specific case where a directory
already exists (because a thread has already come along and created it).
Any other exception propogates.

Fixes aws#739.
  • Loading branch information
jamesls committed Apr 4, 2014
1 parent ebe5ff9 commit 4910a5e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
10 changes: 8 additions & 2 deletions awscli/customizations/s3/fileinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
guess_content_type, MD5Error


class CreateDirectoryError(Exception):
pass


def read_file(filename):
"""
This reads the file into a form that can be sent to S3
Expand All @@ -34,8 +38,10 @@ def save_file(filename, response_data, last_update):
try:
if not os.path.exists(d):
os.makedirs(d)
except Exception:
pass
except OSError as e:
if not e.errno == errno.EEXIST:
raise CreateDirectoryError(
"Could not create directory %s: %s" % (d, e))
md5 = hashlib.md5()
file_chunks = iter(partial(body.read, 1024 * 1024), b'')
with open(filename, 'wb') as out_file:
Expand Down
60 changes: 60 additions & 0 deletions tests/unit/customizations/s3/test_fileinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import os
import tempfile
import shutil
from datetime import datetime
from hashlib import md5

import six
import mock

from tests import unittest
from awscli.customizations.s3 import fileinfo


class TestSaveFile(unittest.TestCase):
def setUp(self):
self.tempdir = tempfile.mkdtemp()
self.filename = os.path.join(self.tempdir, 'dir1', 'dir2', 'foo.txt')
etag = md5()
etag.update(b'foobar')
etag = etag.hexdigest()
self.response_data = {
'Body': six.BytesIO(b'foobar'),
'ETag': '"%s"' % etag,
}
self.last_update = datetime.now()

def tearDown(self):
shutil.rmtree(self.tempdir)

def test_save_file(self):
fileinfo.save_file(self.filename, self.response_data, self.last_update)
self.assertTrue(os.path.isfile(self.filename))

def test_save_file_dir_exists(self):
os.makedirs(os.path.dirname(self.filename))
# We should still be able to save the file.
fileinfo.save_file(self.filename, self.response_data, self.last_update)
self.assertTrue(os.path.isfile(self.filename))

@mock.patch('os.makedirs')
def test_makedir_other_exception(self, makedirs):
# If makedirs() raises any other kind of exception, we should
# propogate the exception.
makedirs.side_effect = RuntimeError()
with self.assertRaises(RuntimeError):
fileinfo.save_file(self.filename, self.response_data,
self.last_update)
self.assertFalse(os.path.isfile(self.filename))

0 comments on commit 4910a5e

Please sign in to comment.