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

Conversation

yakovlevtx
Copy link

I had a few times where my .cache.json file was corrupted because I hit Ctrl-C during a download. This should prevent that.

SonarBeserk added a commit to SonarBeserk/humblebundle-downloader that referenced this pull request Nov 3, 2022
SonarBeserk added a commit to SonarBeserk/humblebundle-downloader that referenced this pull request Nov 3, 2022
@@ -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')

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants