Skip to content

Commit a7308f0

Browse files
authored
Merge pull request #260 from MVrachev/fix-empty-directory
Fix directory being empty in ensure_parent_dir
2 parents 5451477 + 247cf9b commit a7308f0

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

securesystemslib/util.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,10 @@ def ensure_parent_dir(filename, storage_backend=None):
254254
# Split 'filename' into head and tail, check if head exists.
255255
directory = os.path.split(filename)[0]
256256

257-
storage_backend.create_folder(directory)
257+
# Check for cases where filename is without directory like 'file.txt'
258+
# and as a result directory is an empty string
259+
if directory:
260+
storage_backend.create_folder(directory)
258261

259262

260263
def file_in_confined_directories(filepath, confined_directories):

tests/test_util.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ def test_B4_ensure_parent_dir(self):
153153
self.assertRaises(securesystemslib.exceptions.FormatError,
154154
securesystemslib.util.ensure_parent_dir, parent_dir)
155155

156+
# When we call ensure_parent_dir with filepath arg like "a.txt",
157+
# then the directory of that filepath will be an empty string.
158+
# We want to make sure that securesyslib.storage.create_folder()
159+
# won't be called with an empty string and thus raise an exception.
160+
# If an exception is thrown the test will fail.
161+
securesystemslib.util.ensure_parent_dir('a.txt')
162+
156163

157164

158165
def test_B5_file_in_confined_directories(self):

0 commit comments

Comments
 (0)