Remove parents in remove_empty_directories (#21726)

The original implementation did a full directory tree walk to find and remove
empty directories, so this implementation should remove the parents as well,
like the original did.
This commit is contained in:
John Shaw 2026-01-19 22:24:27 -06:00 committed by GitHub
parent bcccae7f9c
commit 16d94c3cfa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 12 deletions

View File

@ -11,7 +11,7 @@ from pathlib import Path
from playhouse.sqlite_ext import SqliteExtDatabase from playhouse.sqlite_ext import SqliteExtDatabase
from frigate.config import CameraConfig, FrigateConfig, RetainModeEnum from frigate.config import CameraConfig, FrigateConfig, RetainModeEnum
from frigate.const import CACHE_DIR, CLIPS_DIR, MAX_WAL_SIZE from frigate.const import CACHE_DIR, CLIPS_DIR, MAX_WAL_SIZE, RECORD_DIR
from frigate.models import Previews, Recordings, ReviewSegment, UserReviewStatus from frigate.models import Previews, Recordings, ReviewSegment, UserReviewStatus
from frigate.util.builtin import clear_and_unlink from frigate.util.builtin import clear_and_unlink
from frigate.util.media import remove_empty_directories from frigate.util.media import remove_empty_directories
@ -376,5 +376,5 @@ class RecordingCleanup(threading.Thread):
if counter == 0: if counter == 0:
self.clean_tmp_clips() self.clean_tmp_clips()
maybe_empty_dirs = self.expire_recordings() maybe_empty_dirs = self.expire_recordings()
remove_empty_directories(maybe_empty_dirs) remove_empty_directories(Path(RECORD_DIR), maybe_empty_dirs)
self.truncate_wal() self.truncate_wal()

View File

@ -50,22 +50,36 @@ class SyncResult:
} }
def remove_empty_directories(paths: Iterable[Path]) -> None: def remove_empty_directories(root: Path, paths: Iterable[Path]) -> None:
""" """
Remove directories if they exist and are empty. Remove directories if they exist and are empty.
Silently ignores non-existent and non-empty directories. Silently ignores non-existent and non-empty directories.
Attempts to remove parent directories as well, stopping at the given root.
""" """
count = 0 count = 0
for path in paths: while True:
try: parents = set()
path.rmdir() for path in paths:
except FileNotFoundError: if path == root:
continue
except OSError as e:
if e.errno == errno.ENOTEMPTY:
continue continue
raise
count += 1 try:
path.rmdir()
count += 1
except FileNotFoundError:
pass
except OSError as e:
if e.errno == errno.ENOTEMPTY:
continue
raise
parents.add(path.parent)
if not parents:
break
paths = parents
logger.debug("Removed {count} empty directories") logger.debug("Removed {count} empty directories")