Skip to content

Commit

Permalink
Merge pull request #3 from yakovlevtx/use_json_temp
Browse files Browse the repository at this point in the history
Use a temporary file to prevent cache file corruption when interrupted.
  • Loading branch information
missytake authored Jan 31, 2022
2 parents 4d65dda + 58adb5c commit 9e94440
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion humblebundle_downloader/download_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,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')
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 @@ -343,11 +344,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.

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

0 comments on commit 9e94440

Please sign in to comment.