Skip to content
This repository was archived by the owner on Mar 15, 2025. It is now read-only.

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

Open
yakovlevtx wants to merge 1 commit into
xtream1101:mainfrom
yakovlevtx:use_json_temp
Open

Use a temporary file to prevent cache file corruption when interrupted.#45
yakovlevtx wants to merge 1 commit into
xtream1101:mainfrom
yakovlevtx:use_json_temp

Conversation

@yakovlevtx

Copy link
Copy Markdown

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
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
Copy Markdown
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')

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
Copy Markdown
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 subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants