-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Throw exception on empty filepath in create_folder #252
Throw exception on empty filepath in create_folder #252
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this submission Martin! I think we can make create_folder()
more Pythonic by removing the check for an empty string and instead refining our exception handler. What do you think?
securesystemslib/storage.py
Outdated
if not filepath: | ||
return | ||
raise securesystemslib.exceptions.StorageError( | ||
"Can't create a folder with an empty filepath!") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather we deleted this conditional entirely and instead improve the exception handler in try
/except
block around the os.makedirs()
call below.
On Python2 os.makedirs()
with an empty string throws an OSError
, whereas on Python3 we'll get a FileNotFoundError
. In either case the exception's errno field will equal errno.ENOENT
.
We should be able to adapt our existing exception handling to check for errno.ENOENT and pass a different message when raising securesystemslib.exceptions.StorageError
in that case.
Now, when securesystemslib.storage.FilesystemBackend.create_folder() is called with an empty string it returns immediately, without exception, but if we call os.mkdir() with an empty string we receive this exception: "[Errno 2] No such file or directory: '' If we want to mimic the os.mkdir() function when using the securesystemslib.storage.FilesystemBackend.create_folder() the function we need to throw our respective exception securesystemslib.exceptions.StorageError with an appropriate message when an empty path is given. Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
The check if an exception is thrown when calling the securesystemslib.storage.FilesystemBackend.create_folder() function with empty string is better suited to be in the test_exceptions function in the tests/test_storage.py where the rest exception checks are. Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
98cf2ee
to
adf5d39
Compare
Yep, it does make sense given that we are already doing exception handling. |
Statements like this make me nervous. We shouldn't be adding code "just in case", so I decided to reason about under what other conditions we might get an
We get an |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix @MVrachev
Because there is another case where we can receive |
Fixes issue #: #241
Description of the changes being introduced by the pull request:
Now, when securesystemslib.storage.FilesystemBackend.create_folder()
is called with an empty string it returns immediately,
without exception, but if we call os.mkdir() with an empty string
we receive this exception:
"[Errno 2] No such file or directory: ''
If we want to mimic the os.mkdir() function when using the
securesystemslib.storage.FilesystemBackend.create_folder()
the function we need to throw our respective exception
securesystemslib.exceptions.StorageError with an appropriate message
when an empty path is given.
Please verify and check that the pull request fulfils the following
requirements: