From 2386630e10780b5ef94713e8ee24083b50b24b77 Mon Sep 17 00:00:00 2001 From: Massimiliano Culpo Date: Sat, 9 Oct 2021 00:35:23 +0200 Subject: Remove DB reindex during a read operation (#26601) The DB should be what is trusted for certain operations. If it is not present when read we should assume the corresponding store is empty, rather than trying a write operation during a read. * Add a unit test * Document what needs to be there in tests --- lib/spack/spack/database.py | 13 +------------ lib/spack/spack/test/database.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/database.py b/lib/spack/spack/database.py index 2dbcd51950..b7294f3ecb 100644 --- a/lib/spack/spack/database.py +++ b/lib/spack/spack/database.py @@ -1024,12 +1024,7 @@ class Database(object): raise def _read(self): - """Re-read Database from the data in the set location. - - This does no locking, with one exception: it will automatically - try to regenerate a missing DB if local. This requires taking a - write lock. - """ + """Re-read Database from the data in the set location. This does no locking.""" if os.path.isfile(self._index_path): current_verifier = '' if _use_uuid: @@ -1049,12 +1044,6 @@ class Database(object): "No database index file is present, and upstream" " databases cannot generate an index file") - # The file doesn't exist, try to traverse the directory. - # reindex() takes its own write lock, so no lock here. - with lk.WriteTransaction(self.lock): - self._write(None, None, None) - self.reindex(spack.store.layout) - def _add( self, spec, diff --git a/lib/spack/spack/test/database.py b/lib/spack/spack/test/database.py index 37d19d6794..57a03a5db9 100644 --- a/lib/spack/spack/test/database.py +++ b/lib/spack/spack/test/database.py @@ -894,3 +894,18 @@ def test_prefix_write_lock_error(mutable_database, monkeypatch): with pytest.raises(Exception): with spack.store.db.prefix_write_lock(s): assert False + + +@pytest.mark.regression('26600') +def test_database_works_with_empty_dir(tmpdir): + # Create the lockfile and failures directory otherwise + # we'll get a permission error on Database creation + db_dir = tmpdir.ensure_dir('.spack-db') + db_dir.ensure('lock') + db_dir.ensure_dir('failures') + tmpdir.chmod(mode=0o555, rec=1) + db = spack.database.Database(str(tmpdir)) + with db.read_transaction(): + db.query() + # Check that reading an empty directory didn't create a new index.json + assert not os.path.exists(db._index_path) -- cgit v1.2.3-70-g09d2