pull/383/merge
Azat Khuzhin 8 years ago
commit 548607f4cd

@ -7,7 +7,7 @@ from collections import namedtuple
import os import os
import shutil import shutil
import sys import sys
from tempfile import NamedTemporaryFile import fcntl
from time import time from time import time
if sys.version_info[0] == 3: if sys.version_info[0] == 3:
@ -75,10 +75,13 @@ def load(config):
config['data_path'], config['data_path'],
'r', encoding='utf-8', 'r', encoding='utf-8',
errors='replace') as f: errors='replace') as f:
return dict( fcntl.flock(f.fileno(), fcntl.LOCK_SH)
d = dict(
imap( imap(
tupleize, tupleize,
ifilter(correct_length, imap(parse, f)))) ifilter(correct_length, imap(parse, f))))
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
return d
except (IOError, EOFError): except (IOError, EOFError):
return load_backup(config) return load_backup(config)
@ -117,24 +120,11 @@ def save(config, data):
"""Save data and create backup, creating a new data file if necessary.""" """Save data and create backup, creating a new data file if necessary."""
create_dir(os.path.dirname(config['data_path'])) create_dir(os.path.dirname(config['data_path']))
# atomically save by writing to temporary file and moving to destination with open(config['data_path'], 'w', encoding='utf-8') as f:
try: fcntl.flock(f.fileno(), fcntl.LOCK_EX)
temp = NamedTemporaryFile(delete=False) for path, weight in data.items():
# Windows cannot reuse the same open file name f.write(unico("%s\t%s\n" % (weight, path)))
temp.close() fcntl.flock(f.fileno(), fcntl.LOCK_UN)
with open(temp.name, 'w', encoding='utf-8', errors='replace') as f:
for path, weight in data.items():
f.write(unico("%s\t%s\n" % (weight, path)))
f.flush()
os.fsync(f)
except IOError as ex:
print("Error saving autojump data (disk full?)" % ex, file=sys.stderr)
sys.exit(1)
# move temp_file -> autojump.txt
move_file(temp.name, config['data_path'])
# create backup file if it doesn't exist or is older than BACKUP_THRESHOLD # create backup file if it doesn't exist or is older than BACKUP_THRESHOLD
if not os.path.exists(config['backup_path']) or \ if not os.path.exists(config['backup_path']) or \

Loading…
Cancel
Save