mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-08-02 09:02:15 +03:00
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:
+24
-10
@@ -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.
|
||||
Silently ignores non-existent and non-empty directories.
|
||||
Attempts to remove parent directories as well, stopping at the given root.
|
||||
"""
|
||||
count = 0
|
||||
for path in paths:
|
||||
try:
|
||||
path.rmdir()
|
||||
except FileNotFoundError:
|
||||
continue
|
||||
except OSError as e:
|
||||
if e.errno == errno.ENOTEMPTY:
|
||||
while True:
|
||||
parents = set()
|
||||
for path in paths:
|
||||
if path == root:
|
||||
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")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user