Commit 56c96a11 authored by Kirill Smelkov's avatar Kirill Smelkov Committed by Julien Muchembled

FileStorage: Report problem on read-only open of non-existent file

... 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 30bbabf19e837b4ca9e35fab5d5c1278b078fe45)
parent 60ce5795
...@@ -189,6 +189,10 @@ class FileStorage( ...@@ -189,6 +189,10 @@ class FileStorage(
if exc.errno == errno.EFBIG: if exc.errno == errno.EFBIG:
# The file is too big to open. Fail visibly. # The file is too big to open. Fail visibly.
raise raise
if read_only:
# When open request is read-only we do not want to create
# the file
raise
if exc.errno == errno.ENOENT: if exc.errno == errno.ENOENT:
# The file doesn't exist. Create it. # The file doesn't exist. Create it.
create = 1 create = 1
......
...@@ -73,6 +73,16 @@ class ZODBConfigTest(ConfigTestBase): ...@@ -73,6 +73,16 @@ class ZODBConfigTest(ConfigTestBase):
def test_file_config2(self): def test_file_config2(self):
path = tempfile.mktemp() 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 = """ cfg = """
<zodb> <zodb>
<filestorage> <filestorage>
......
...@@ -711,6 +711,19 @@ def pack_with_open_blob_files(): ...@@ -711,6 +711,19 @@ def pack_with_open_blob_files():
>>> db.close() >>> 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(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
for klass in [ for klass in [
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment