Make writing keys.dat safer by creating a backup file #715

Merged
yurivict merged 3 commits from some_small_changes into master 2014-12-26 03:09:17 +01:00
Showing only changes of commit 5beaeff2e2 - Show all commits

View File

@ -21,6 +21,7 @@ import stat
import threading import threading
import time import time
import shutil # used for moving the data folder and copying keys.dat import shutil # used for moving the data folder and copying keys.dat
import datetime
from os import path, environ from os import path, environ
from struct import Struct from struct import Struct
@ -801,13 +802,14 @@ def openKeysFile():
def writeKeysFile(): def writeKeysFile():
fileName = shared.appdata + 'keys.dat' fileName = shared.appdata + 'keys.dat'
fileNameBak = fileName + "." + datetime.datetime.now().strftime("%Y%j%H%M%S%f") + '.bak'
# create a backup copy to prevent the accidental loss due to the disk write failure # create a backup copy to prevent the accidental loss due to the disk write failure
shutil.copyfile(fileName, fileName + '.bak') shutil.copyfile(fileName, fileNameBak)
# write the file # write the file
with open(fileName, 'wb') as configfile: with open(fileName, 'wb') as configfile:
shared.config.write(configfile) shared.config.write(configfile)
# delete a backup # delete a backup
os.remove(fileName + '.bak') os.remove(fileNameBak)
helper_startup.loadConfig() helper_startup.loadConfig()
from debug import logger from debug import logger