Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a temporary file to prevent cache file corruption when interrupted. #45

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion humblebundle_downloader/download_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __init__(self, library_path, cookie_path=None, cookie_auth=None,
def start(self):

self.cache_file = os.path.join(self.library_path, '.cache.json')
self.cache_file_temp = os.path.join(self.library_path, '.tmp.cache.json')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.cache_file_temp = os.path.join(self.library_path, '.tmp.cache.json')
self.cache_file_temp = os.path.join(self.library_path, '.cache.json.tmp')

self.cache_data = self._load_cache_data(self.cache_file)
self.purchase_keys = self.purchase_keys if self.purchase_keys else self._get_purchase_keys() # noqa: E501

Expand Down Expand Up @@ -310,11 +311,13 @@ def _update_cache_data(self, cache_file_key, file_info):
# quits it can keep track of the progress
# Note: Only safe because of single thread,
# need to change if refactor to multi threading
with open(self.cache_file, 'w') as outfile:
with open(self.cache_file_temp, 'w') as outfile:
json.dump(
self.cache_data, outfile,
sort_keys=True, indent=4,
)
outfile.close() #explicitly close outfile and flush output buffer.
os.rename(self.cache_file_temp,self.cache_file) #rename temp file to real file.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename should fail if the destination file already exists. I think you need to rename the old file, rename the new file, then delete the old file.


def _process_download(self, open_r, cache_file_key, file_info,
local_filename, rename_str=None):
Expand Down