Skip to content

Commit 7b09256

Browse files
flound1129claude
andcommitted
fix(core): prevent skipped files from being allocated and written to disk
Two related issues when setting file priorities to Skip (0): 1. file_priorities were not included in add_torrent_params, so libtorrent allocated storage for all files (including skipped ones) before set_file_priorities() was called post-add. Now passed at add time for torrents with metadata; magnets continue to use on_metadata_received(). 2. Boundary pieces needed by adjacent wanted files leave corrupt partial data on disk for newly-skipped files. set_file_priorities() now removes any existing non-empty file when its priority is first set to 0. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent e3825c8 commit 7b09256

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

deluge/core/torrent.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,15 +535,32 @@ def set_file_priorities(self, file_priorities):
535535
log.debug('Unable to set new file priorities.')
536536
file_priorities = self.handle.get_file_priorities()
537537

538-
if 0 in self.options['file_priorities']:
539-
# Previously marked a file 'skip' so check for any 0's now >0.
540-
for index, priority in enumerate(self.options['file_priorities']):
538+
prev_priorities = self.options['file_priorities']
539+
if prev_priorities:
540+
for index, priority in enumerate(prev_priorities):
541541
if priority == 0 and file_priorities[index] > 0:
542542
# Changed priority from skip to download so update state.
543543
self.is_finished = False
544544
self.update_state()
545545
break
546546

547+
# Remove any partial files that were written for files now being skipped.
548+
# Boundary pieces for adjacent wanted files can leave corrupt partial data
549+
# on disk for skipped files; delete them so they don't appear as corrupt files.
550+
if 0 in file_priorities:
551+
ti = self.torrent_info
552+
save_path = self.options['download_location']
553+
for index, priority in enumerate(file_priorities):
554+
was_not_skipped = not prev_priorities or prev_priorities[index] != 0
555+
if priority == 0 and was_not_skipped:
556+
file_path = os.path.join(save_path, ti.file_at(index).path)
557+
if os.path.exists(file_path) and os.path.getsize(file_path) > 0:
558+
log.debug('Removing partial skipped file: %s', file_path)
559+
try:
560+
os.remove(file_path)
561+
except OSError as ex:
562+
log.warning('Unable to remove skipped file %s: %s', file_path, ex)
563+
547564
# Store the priorities.
548565
self.options['file_priorities'] = file_priorities
549566

deluge/core/torrentmanager.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,13 @@ def _build_torrent_params(
477477
add_torrent_params['name'] = options['name']
478478
if options['pre_allocate_storage']:
479479
add_torrent_params['storage_mode'] = lt.storage_mode_t.storage_mode_allocate
480+
# Pass file priorities at add time so libtorrent's storage layer respects them
481+
# before any space is allocated or pieces are queued. Without this, libtorrent
482+
# allocates storage for all files (including skipped ones) and may begin
483+
# downloading them before set_file_priorities() is called post-add.
484+
# Only set for torrents with metadata; magnets get priorities via on_metadata_received().
485+
if options['file_priorities'] and torrent_info:
486+
add_torrent_params['file_priorities'] = options['file_priorities']
480487
if resume_data:
481488
add_torrent_params['resume_data'] = resume_data
482489

0 commit comments

Comments
 (0)