From c9961f4435896425406f42dbf053470302ad03c5 Mon Sep 17 00:00:00 2001 From: derrod Date: Tue, 21 Apr 2020 19:56:34 +0200 Subject: [PATCH] [downloader] Fix patching games with reused chunks --- legendary/downloader/manager.py | 12 ++++++++---- legendary/downloader/workers.py | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/legendary/downloader/manager.py b/legendary/downloader/manager.py index 6e93c29..496b2ef 100644 --- a/legendary/downloader/manager.py +++ b/legendary/downloader/manager.py @@ -146,12 +146,16 @@ class DLManager(Process): break continue - while task.chunk_guid in in_buffer: - res = in_buffer[task.chunk_guid] + while (task.chunk_guid in in_buffer) or task.chunk_file: + if task.chunk_file: # re-using from an old file + res_shm = None + else: + res = in_buffer[task.chunk_guid] + res_shm = res.shm try: self.writer_queue.put(WriterTask( - filename=current_file, shared_memory=res.shm, + filename=current_file, shared_memory=res_shm, chunk_offset=task.chunk_offset, chunk_size=task.chunk_size, chunk_guid=task.chunk_guid, release_memory=task.cleanup, old_file=task.chunk_file # todo on-disk cache @@ -160,7 +164,7 @@ class DLManager(Process): self.log.warning(f'Adding to queue failed: {e!r}') break - if task.cleanup: + if task.cleanup and not task.chunk_file: del in_buffer[task.chunk_guid] try: diff --git a/legendary/downloader/workers.py b/legendary/downloader/workers.py index 676a310..9744430 100644 --- a/legendary/downloader/workers.py +++ b/legendary/downloader/workers.py @@ -172,7 +172,7 @@ class FileWorker(Process): continue try: - os.rename(os.path.join(self.base_path, j.temporary_filename), full_path) + os.rename(os.path.join(self.base_path, j.old_filename), full_path) except OSError as e: self.log.error(f'Renaming file failed: {e!r}') self.o_q.put(WriterTaskResult(success=False, filename=j.filename)) @@ -212,7 +212,7 @@ class FileWorker(Process): post_write = time.time() elif j.old_file: pre_write = time.time() - with open(os.path.join(self.base_path, j.cache_file), 'rb') as f: + with open(os.path.join(self.base_path, j.old_file), 'rb') as f: if j.chunk_offset: f.seek(j.chunk_offset) current_file.write(f.read(j.chunk_size))