diff --git a/diskcache/core.py b/diskcache/core.py index 7a3d23b..2f14f1b 100644 --- a/diskcache/core.py +++ b/diskcache/core.py @@ -444,7 +444,11 @@ def __init__(self, directory=None, timeout=60, disk=Disk, **settings): if not op.isdir(directory): try: - os.makedirs(directory, 0o755) + # Let the OS apply the umask to the default mode rather than + # hard-coding 0o755, so the cache directory's permissions + # honor the caller's umask like any other newly created + # directory would. + os.makedirs(directory) except OSError as error: if error.errno != errno.EEXIST: raise EnvironmentError( diff --git a/tests/test_core.py b/tests/test_core.py index 788afef..6bdc95f 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -144,6 +144,23 @@ def test_init_makedirs(): raise +def test_init_makedirs_respects_umask(): + cache_dir = tempfile.mkdtemp() + shutil.rmtree(cache_dir) + makedirs = mock.Mock(wraps=os.makedirs) + + try: + with mock.patch('os.makedirs', makedirs): + cache = dc.Cache(cache_dir) + cache.close() + finally: + shutil.rmtree(cache_dir, ignore_errors=True) + + # No explicit mode should be passed, so the OS applies the umask to the + # default mode like it does for any other new directory. + makedirs.assert_called_once_with(cache_dir) + + def test_pragma_error(cache): local = mock.Mock() con = mock.Mock()