drop os.exists() before makedirs()
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Thu, 5 Jul 2018 06:53:06 +0000 (08:53 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Sat, 1 Feb 2020 14:14:06 +0000 (15:14 +0100)
os.makedirs() has proper EEXIST handling builtin. Checking with
``os.exists()`` is both redundant and inherently racy.

deltatar/deltatar.py

index 057c98f..161008c 100644 (file)
@@ -655,8 +655,7 @@ class DeltaTar(object):
             raise Exception('Source path "%s" is not readable' % source_path)
 
         # try to create backup path if needed
-        if not os.path.exists(backup_path):
-            os.makedirs(backup_path)
+        os.makedirs(backup_path, exist_ok=True)
 
         if not os.access(backup_path, os.W_OK):
             raise Exception('Backup path "%s" is not writeable' % backup_path)
@@ -825,8 +824,7 @@ class DeltaTar(object):
             raise Exception('Index path "%s" is not readable' % previous_index_path)
 
         # try to create backup path if needed
-        if not os.path.exists(backup_path):
-            os.makedirs(backup_path)
+        os.makedirs(backup_path, exist_ok=True)
 
         if not os.access(backup_path, os.W_OK):
             raise Exception('Backup path "%s" is not writeable' % backup_path)
@@ -1424,8 +1422,7 @@ class DeltaTar(object):
                     raise Exception('Index path "%s" is not readable' % index)
 
         # try to create backup path if needed
-        if not os.path.exists(target_path):
-            os.makedirs(target_path)
+        os.makedirs(target_path, exist_ok=True)
 
         # make backup_tar_path absolute so that iterate_tar_path works fine
         if backup_tar_path and not os.path.isabs(backup_tar_path):
@@ -1787,8 +1784,7 @@ class RestoreHelper(object):
 
         # to preserve parent directory mtime, we save it
         parent_dir = os.path.dirname(upath) or os.getcwd()
-        if not os.path.exists(parent_dir):
-            os.makedirs(parent_dir)
+        os.makedirs(parent_dir, exist_ok=True)
         parent_dir_mtime = int(os.stat(parent_dir).st_mtime)
 
         # if path is found in the newest index as to be snapshotted, deal with it