forked from distributed-system-analysis/smallfile
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync_files.py
74 lines (59 loc) · 2.08 KB
/
sync_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
import os
import pickle
import shutil
import time
class SyncFileException(Exception):
pass
notyet = '.notyet'
def touch(fpath):
try:
with open(fpath, 'w') as sgf:
sgf.write('hi')
sgf.flush()
os.fsync(sgf.fileno())
except OSError as e:
if e.errno != EEXIST:
raise e
def write_sync_file(fpath, contents):
with open(fpath+notyet, 'w') as sgf:
sgf.write(contents)
sgf.flush()
os.fsync(sgf.fileno()) # file should close when you exit block
os.rename(fpath+notyet, fpath)
def write_pickle(fpath, obj):
with open(fpath+notyet, 'wb') as result_file:
pickle.dump(obj, result_file)
result_file.flush()
os.fsync(result_file.fileno()) # or else reader may not see data
os.rename(fpath+notyet, fpath)
# create directory if it's not already there
def ensure_dir_exists(dirpath):
if not os.path.exists(dirpath):
parent_path = os.path.dirname(dirpath)
if parent_path == dirpath:
raise SMFSyncFileException('ensure_dir_exists: ' +
'cannot obtain parent path ' +
'of non-existent path: ' +
dirpath)
ensure_dir_exists(parent_path)
try:
os.mkdir(dirpath)
except os.error as e:
if e.errno != errno.EEXIST: # workaround for filesystem bug
raise e
else:
if not os.path.isdir(dirpath):
raise SMFSyncFileException('%s already exists and is not a directory!'
% dirpath)
# avoid exception if file we wish to delete is not there
def ensure_deleted(fn):
try:
if os.path.lexists(fn):
os.unlink(fn)
except Exception as e:
# could be race condition with other client processes/hosts
# if was race condition, file will no longer be there
if os.path.exists(fn):
raise SMFSyncFileException('exception while ensuring %s deleted: %s'
% (fn, str(e)))