[util] Remove umask manipulation from makedirs

This causes a race condition: umask is process-wide setting, so any file
created by another thread while the umask is set to 0 will have wrong
permissions. This also removes the possible problem of not resetting the
umask if exception is raised.

Fixes: #239
Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2016-03-31 09:27:22 +02:00
parent 4ec8ad5de8
commit 69316d827a
2 changed files with 13 additions and 4 deletions

View File

@ -138,13 +138,11 @@ def _doCheckSum(path, hash, logger):
def makedirs(path, mode=0o775):
mask = os.umask(0)
try:
os.makedirs(path, mode=mode)
except OSError as ex:
if ex.errno != errno.EEXIST:
raise
os.umask(mask)
def rmtree(path, ignore_errors=False, onerror=None):

View File

@ -13,7 +13,7 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from pungi import compose
from pungi import util
from tests.helpers import touch
from tests.helpers import touch, PungiTestCase
class TestGitRefResolver(unittest.TestCase):
@ -193,7 +193,7 @@ class TestFindOldCompose(unittest.TestCase):
self.assertEqual(old, self.tmp_dir + '/Fedora-Rawhide-Base-1-20160229.0')
class TestHelpers(unittest.TestCase):
class TestHelpers(PungiTestCase):
def test_process_args(self):
self.assertEqual(util.process_args('--opt={}', None), [])
self.assertEqual(util.process_args('--opt={}', []), [])
@ -201,6 +201,17 @@ class TestHelpers(unittest.TestCase):
['--opt=foo', '--opt=bar'])
self.assertEqual(util.process_args('--opt={}', 'foo'), ['--opt=foo'])
def test_makedirs(self):
util.makedirs(self.topdir + '/foo/bar/baz')
self.assertTrue(os.path.isdir(self.topdir + '/foo/bar/baz'))
def test_makedirs_on_existing(self):
os.makedirs(self.topdir + '/foo/bar/baz')
try:
util.makedirs(self.topdir + '/foo/bar/baz')
except OSError:
self.fail('makedirs raised exception on existing directory')
if __name__ == "__main__":
unittest.main()