Refactor/Duplicate code: Extract _fsync method
This commit is contained in:
parent
77e9ca1252
commit
dc501d5dc5
@ -394,11 +394,7 @@ class Collection(BaseCollection):
|
|||||||
delete=False, prefix=".Radicale.tmp-", newline=newline)
|
delete=False, prefix=".Radicale.tmp-", newline=newline)
|
||||||
try:
|
try:
|
||||||
yield tmp
|
yield tmp
|
||||||
if self.configuration.getboolean("storage", "fsync"):
|
self._fsync(tmp.fileno())
|
||||||
if os.name == "posix" and hasattr(fcntl, "F_FULLFSYNC"):
|
|
||||||
fcntl.fcntl(tmp.fileno(), fcntl.F_FULLFSYNC)
|
|
||||||
else:
|
|
||||||
os.fsync(tmp.fileno())
|
|
||||||
tmp.close()
|
tmp.close()
|
||||||
os.replace(tmp.name, path)
|
os.replace(tmp.name, path)
|
||||||
except:
|
except:
|
||||||
@ -416,6 +412,14 @@ class Collection(BaseCollection):
|
|||||||
return file_name
|
return file_name
|
||||||
raise FileExistsError(errno.EEXIST, "No usable file name found")
|
raise FileExistsError(errno.EEXIST, "No usable file name found")
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _fsync(cls, fd):
|
||||||
|
if cls.configuration.getboolean("storage", "fsync"):
|
||||||
|
if os.name == "posix" and hasattr(fcntl, "F_FULLFSYNC"):
|
||||||
|
fcntl.fcntl(fd, fcntl.F_FULLFSYNC)
|
||||||
|
else:
|
||||||
|
os.fsync(fd)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _sync_directory(cls, path):
|
def _sync_directory(cls, path):
|
||||||
"""Sync directory to disk.
|
"""Sync directory to disk.
|
||||||
@ -428,10 +432,7 @@ class Collection(BaseCollection):
|
|||||||
if os.name == "posix":
|
if os.name == "posix":
|
||||||
fd = os.open(path, 0)
|
fd = os.open(path, 0)
|
||||||
try:
|
try:
|
||||||
if hasattr(fcntl, "F_FULLFSYNC"):
|
cls._fsync(fd)
|
||||||
fcntl.fcntl(fd, fcntl.F_FULLFSYNC)
|
|
||||||
else:
|
|
||||||
os.fsync(fd)
|
|
||||||
finally:
|
finally:
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
|
|
||||||
@ -586,15 +587,9 @@ class Collection(BaseCollection):
|
|||||||
path = path_to_filesystem(self._filesystem_path, href)
|
path = path_to_filesystem(self._filesystem_path, href)
|
||||||
fs.append(open(path, "w", encoding=self.encoding, newline=""))
|
fs.append(open(path, "w", encoding=self.encoding, newline=""))
|
||||||
fs[-1].write(item.serialize())
|
fs[-1].write(item.serialize())
|
||||||
fsync_fn = lambda fd: None
|
|
||||||
if self.configuration.getboolean("storage", "fsync"):
|
|
||||||
if os.name == "posix" and hasattr(fcntl, "F_FULLFSYNC"):
|
|
||||||
fsync_fn = lambda fd: fcntl.fcntl(fd, fcntl.F_FULLFSYNC)
|
|
||||||
else:
|
|
||||||
fsync_fn = os.fsync
|
|
||||||
# sync everything at once because it's slightly faster.
|
# sync everything at once because it's slightly faster.
|
||||||
for f in fs:
|
for f in fs:
|
||||||
fsync_fn(f.fileno())
|
self._fsync(f.fileno())
|
||||||
f.close()
|
f.close()
|
||||||
self._sync_directory(self._filesystem_path)
|
self._sync_directory(self._filesystem_path)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user