Skip to content

Commit ecee858

Browse files
committed
Fix inconsistent inclusion of padding files in tdef
1 parent 0f3f5bb commit ecee858

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/tribler/core/libtorrent/torrentdef.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,12 @@ def _get_all_files_as_unicode_with_length(self) -> Generator[tuple[Path, int], N
638638
metainfo_v1 = cast(InfoDict, self.metainfo[b"info"])
639639
# Multi-file v1 torrent
640640
files = cast(FileDict, metainfo_v1[b"files"])
641+
storage = self.torrent_info.files()
641642

642-
for file_dict in files:
643+
for index, file_dict in enumerate(files):
644+
# Ignore padding files, to align with v2 metainfo
645+
if storage.file_flags(index) != 0:
646+
continue
643647
if b"path.utf-8" in file_dict:
644648
# This file has an utf-8 encoded list of elements.
645649
# We assume that it is correctly encoded and use it normally.

src/tribler/test_unit/core/libtorrent/test_torrentdef.py

+16
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,22 @@ def test_get_files_with_length_illegal_path_utf8(self) -> None:
432432

433433
self.assertEqual([(Path("file.txt"), 456)], tdef.get_files_with_length())
434434

435+
def test_get_files_with_length_ignore_padding(self) -> None:
436+
"""
437+
Test if get_files_with_length ignores padding files.
438+
"""
439+
tdef = TorrentDef(metainfo={
440+
b"info": {
441+
b"name": b"torrent name",
442+
b"files": [{b"path": [b".pad"], b"length": 123, b"attr": b"p"},
443+
{b"path": [b"file.txt"], b"length": 456}],
444+
b"piece length": 128,
445+
b"pieces": b"\x00" * 100 # 100 = ceil((123+456)/128) * 20
446+
}
447+
})
448+
449+
self.assertEqual([(Path('file.txt'), 456)], tdef.get_files_with_length())
450+
435451
def test_load_torrent_info(self) -> None:
436452
"""
437453
Test if load_torrent_info() loads the torrent info.

0 commit comments

Comments
 (0)