Skip to content

Commit

Permalink
FileStorage: Report problem on read-only open of non-existent file
Browse files Browse the repository at this point in the history
... instead of silently creating empty database on such opens.

Use-case for this are utilities like e.g. zodbdump and zodbcmp which
expect such storage opens to fail so that the tool can know there is no
such storage and report it to user.

In contrast current state is: read-only opens get created-on-the-fly
empty storage with no content, but which can be iterated over without
getting any error.

This way e.g. `zodbdump non-existent.fs` produces empty output _and_
exit code 0 which is not what caller expects.

(cherry picked from commit 30bbabf)
  • Loading branch information
navytux authored and jmuchemb committed Apr 3, 2017
1 parent 60ce579 commit 56c96a1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ZODB/FileStorage/FileStorage.py
Expand Up @@ -189,6 +189,10 @@ def __init__(self, file_name, create=False, read_only=False, stop=None,
if exc.errno == errno.EFBIG:
# The file is too big to open. Fail visibly.
raise
if read_only:
# When open request is read-only we do not want to create
# the file
raise
if exc.errno == errno.ENOENT:
# The file doesn't exist. Create it.
create = 1
Expand Down
10 changes: 10 additions & 0 deletions src/ZODB/tests/testConfig.py
Expand Up @@ -73,6 +73,16 @@ def test_file_config1(self):

def test_file_config2(self):
path = tempfile.mktemp()
# first pass to actually create database file
self._test(
"""
<zodb>
<filestorage>
path %s
</filestorage>
</zodb>
""" % path)
# write operations must be disallowed on read-only access
cfg = """
<zodb>
<filestorage>
Expand Down
13 changes: 13 additions & 0 deletions src/ZODB/tests/testFileStorage.py
Expand Up @@ -711,6 +711,19 @@ def pack_with_open_blob_files():
>>> db.close()
"""

def readonly_open_nonexistent_file():
"""
Make sure error is reported when non-existent file is tried to be opened
read-only.
>>> try:
... fs = ZODB.FileStorage.FileStorage('nonexistent.fs', read_only=True)
... except Exception as e:
... # Python2 raises IOError; Python3 - FileNotFoundError
... print("error: %s" % str(e)) # doctest: +ELLIPSIS
error: ... No such file or directory: 'nonexistent.fs'
"""

def test_suite():
suite = unittest.TestSuite()
for klass in [
Expand Down

0 comments on commit 56c96a1

Please sign in to comment.