Skip to content

Commit 1d1e489

Browse files
authored
Fixed inconsistent inclusion of padding files in tdef (#8479)
2 parents 0f3f5bb + 771a363 commit 1d1e489

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

src/tribler/core/libtorrent/torrentdef.py

Lines changed: 5 additions & 1 deletion
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 = cast(lt.torrent_info, self.torrent_info).files() # If we have metainfo, we have torrent_info
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/download_manager/test_download_state.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ def test_get_files_completion_zero_length_file(self) -> None:
155155
"""
156156
download = Download(TorrentDef.load_from_memory(TORRENT_WITH_DIRS_CONTENT), self.dlmngr,
157157
checkpoint_disabled=True, config=DownloadConfig(ConfigObj(StringIO(SPEC_CONTENT))))
158-
for file_spec in download.tdef.metainfo[b"info"][b"files"]:
159-
file_spec[b"length"] = 0
160-
download.handle = Mock(is_valid=Mock(return_value=True), file_progress=Mock(return_value=[]))
158+
download.tdef.metainfo[b"info"][b"files"][0][b"length"] = 0 # Other five files have length of 6
159+
download.handle = Mock(is_valid=Mock(return_value=True), file_progress=Mock(return_value=[0, 6, 6, 6, 6, 6]))
161160
download_state = DownloadState(download, Mock(), None)
162161

162+
self.assertEqual(6, len(download_state.get_files_completion()))
163163
for _, progress in download_state.get_files_completion():
164164
self.assertEqual(1.0, progress)
165165

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ def test_get_files_with_length(self) -> None:
397397
name_bytes = b"\xe8\xaf\xad\xe8\xa8\x80\xe5\xa4\x84\xe7\x90\x86"
398398
name_unicode = name_bytes.decode()
399399
tdef = TorrentDef(metainfo={b"info": {b"files": [{b"path.utf-8": [name_bytes], b"length": 123},
400-
{b"path.utf-8": [b"file.txt"], b"length": 456}]}})
400+
{b"path.utf-8": [b"file.txt"], b"length": 456}],
401+
b"name": b"", b"piece length": 128, b"pieces": b"\x00" * 100}})
401402

402403
self.assertEqual([(Path(name_unicode), 123), (Path('file.txt'), 456)], tdef.get_files_with_length())
403404

@@ -408,7 +409,8 @@ def test_get_files_with_length_valid_path_utf8(self) -> None:
408409
name_bytes = b"\xe8\xaf\xad\xe8\xa8\x80\xe5\xa4\x84\xe7\x90\x86"
409410
name_unicode = name_bytes.decode()
410411
tdef = TorrentDef(metainfo={b"info": {b"files": [{b"path": [name_bytes], b"length": 123},
411-
{b"path": [b"file.txt"], b"length": 456}]}})
412+
{b"path": [b"file.txt"], b"length": 456}],
413+
b"name": b"", b"piece length": 128, b"pieces": b"\x00" * 100}})
412414

413415
self.assertEqual([(Path(name_unicode), 123), (Path('file.txt'), 456)], tdef.get_files_with_length())
414416

@@ -418,7 +420,8 @@ def test_get_files_with_length_illegal_path(self) -> None:
418420
"""
419421
name_bytes = b"\xe8\xaf\xad\xe8\xa8\x80\xe5\xa4\x84\xe7\x90\x86"
420422
tdef = TorrentDef(metainfo={b"info": {b"files": [{b"path": [b"test\xff" + name_bytes], b"length": 123},
421-
{b"path": [b"file.txt"], b"length": 456}]}})
423+
{b"path": [b"file.txt"], b"length": 456}],
424+
b"name": b"", b"piece length": 128, b"pieces": b"\x00" * 100}})
422425

423426
self.assertEqual([(Path("test?????????????"), 123), (Path("file.txt"), 456)], tdef.get_files_with_length())
424427

@@ -428,10 +431,27 @@ def test_get_files_with_length_illegal_path_utf8(self) -> None:
428431
"""
429432
name_bytes = b"\xe8\xaf\xad\xe8\xa8\x80\xe5\xa4\x84\xe7\x90\x86"
430433
tdef = TorrentDef(metainfo={b"info": {b"files": [{b"path.utf-8": [b"test\xff" + name_bytes], b"length": 123},
431-
{b"path": [b"file.txt"], b"length": 456}]}})
434+
{b"path": [b"file.txt"], b"length": 456}],
435+
b"name": b"", b"piece length": 128, b"pieces": b"\x00" * 100}})
432436

433437
self.assertEqual([(Path("file.txt"), 456)], tdef.get_files_with_length())
434438

439+
def test_get_files_with_length_ignore_padding(self) -> None:
440+
"""
441+
Test if get_files_with_length ignores padding files.
442+
"""
443+
tdef = TorrentDef(metainfo={
444+
b"info": {
445+
b"name": b"torrent name",
446+
b"files": [{b"path": [b".pad"], b"length": 123, b"attr": b"p"},
447+
{b"path": [b"file.txt"], b"length": 456}],
448+
b"piece length": 128,
449+
b"pieces": b"\x00" * 100
450+
}
451+
})
452+
453+
self.assertEqual([(Path('file.txt'), 456)], tdef.get_files_with_length())
454+
435455
def test_load_torrent_info(self) -> None:
436456
"""
437457
Test if load_torrent_info() loads the torrent info.

0 commit comments

Comments
 (0)