set default safeguards against mass deletion in remove orphan

This commit is contained in:
bobokun 2024-10-04 18:19:58 -04:00
parent c2667f73b6
commit aa82ba8db3
No known key found for this signature in database
GPG key ID: B73932169607D927
6 changed files with 30 additions and 14 deletions

View file

@ -61,11 +61,3 @@ repos:
language: script
pass_filenames: false
stages: [commit]
- repo: local
hooks:
- id: update-readme-version
name: Update readme version
entry: ./scripts/pre-commit/update-readme-version.sh
language: script
pass_filenames: false
stages: [commit]

View file

@ -1 +1 @@
4.1.11-develop4
4.1.11-develop5

View file

@ -277,6 +277,10 @@ orphaned:
- "/data/torrents/temp/**"
- "**/*.!qB"
- "**/*_unpackerred"
# Set your desired threshold for the maximum number of orphaned files qbm will delete in a single run. (-1 to disable safeguards)
# This will help reduce the number of accidental large amount orphaned deletions in a single run
# WARNING: Setting this variable to -1 will not safeguard against any deletions
max_orphaned_files_to_delete: 50
apprise:
# Apprise integration with webhooks

View file

@ -726,6 +726,14 @@ class Config:
self.orphaned["exclude_patterns"] = self.util.check_for_attribute(
self.data, "exclude_patterns", parent="orphaned", var_type="list", default_is_none=True, do_print=False
)
self.orphaned["max_orphaned_files_to_delete"] = self.util.check_for_attribute(
self.data,
"max_orphaned_files_to_delete",
parent="orphaned",
var_type="int",
default=50,
min_int=-1,
)
if self.commands["rem_orphaned"]:
exclude_orphaned = f"**{os.sep}{os.path.basename(self.orphaned_dir.rstrip(os.sep))}{os.sep}*"
(

View file

@ -32,8 +32,8 @@ class Category:
new_cat.extend(self.get_tracker_cat(torrent) or self.qbt.get_category(torrent.save_path))
if not torrent.auto_tmm and torrent_category:
logger.print_line(
f"{torrent.name} has Automatic Torrent Management disabled and already has a category"
f"{torrent_category}. Skipping..",
f"{torrent.name} has Automatic Torrent Management disabled and already has the category"
f" {torrent_category}. Skipping..",
"DEBUG",
)
continue

View file

@ -44,8 +44,9 @@ class RemoveOrphaned:
for fullpath in fullpathlist
]
)
orphaned_files = set(root_files.result()) - set(torrent_files)
root_files_set = set(root_files.result())
torrent_files_set = set(torrent_files)
orphaned_files = root_files_set - torrent_files_set
if self.config.orphaned["exclude_patterns"]:
logger.print_line("Processing orphan exclude patterns")
@ -59,7 +60,18 @@ class RemoveOrphaned:
orphaned_files = set(orphaned_files) - set(excluded_orphan_files)
if orphaned_files:
# Check the threshold before deleting orphaned files
max_orphaned_files_to_delete = self.config.orphaned.get("max_orphaned_files_to_delete")
if len(orphaned_files) and len(orphaned_files) > max_orphaned_files_to_delete and max_orphaned_files_to_delete != -1:
e = (
f"Too many orphaned files detected ({len(orphaned_files)}). "
f"Max Threshold for deletion is set to {max_orphaned_files_to_delete}. "
"Aborting deletion to avoid accidental data loss."
)
self.config.notify(e, "Remove Orphaned", False)
logger.warning(e)
return
elif orphaned_files:
orphaned_files = sorted(orphaned_files)
os.makedirs(self.orphaned_dir, exist_ok=True)
body = []