Add function to clear empty directories at end of compact process.

Compact moves data to new segments, and then removes the old segments.
When enough segments are moved, directories holding the now cleared segments
may thus become empty.

With this commit any empty directories are cleared after segments compacting.
Fixes #6823
This commit is contained in:
nain 2023-05-21 11:47:23 -04:00
parent 00e19d047d
commit 4a7a5b2253
1 changed files with 16 additions and 0 deletions

View File

@ -907,6 +907,7 @@ class Repository:
pi.show()
pi.finish()
complete_xfer(intermediate=False)
self.io.clear_empty_dirs()
quota_use_after = self.storage_quota_use
logger.info("Compaction freed about %s repository space.", format_file_size(quota_use_before - quota_use_after))
logger.debug("Compaction completed.")
@ -1554,6 +1555,21 @@ class LoggedIO:
except FileNotFoundError:
pass
def clear_empty_dirs(self):
"""Delete empty segment dirs, i.e those with no segment files."""
data_dir = os.path.join(self.path, "data")
segment_dirs = self.get_segment_dirs(data_dir)
for segment_dir in segment_dirs:
try:
# os.rmdir will only delete the directory if it is empty
# so we don't need to explicitly check for emptiness first.
os.rmdir(segment_dir)
except OSError:
# OSError is raised by os.rmdir if directory is not empty. This is expected.
# It's subclass FileNotFoundError may be raised if the directory already does not exist. Ignorable.
pass
sync_dir(data_dir)
def segment_exists(self, segment):
filename = self.segment_filename(segment)
# When deleting segments, they are first truncated. If truncate(2) and unlink(2) are split