storage/reflink: _fsync_dir() -> _fsync_path()

Make it work when passed a file, too.
This commit is contained in:
Rusty Bird 2019-06-28 10:29:29 +00:00
parent 35b4b915e7
commit df23720e4e
No known key found for this signature in database
GPG Key ID: 469D78F47AAF2ADF

View File

@ -379,12 +379,12 @@ def _replace_file(dst):
_remove_file(tmp.name) _remove_file(tmp.name)
raise raise
def _fsync_dir(path): def _fsync_path(path):
dir_fd = os.open(path, os.O_RDONLY | os.O_DIRECTORY) fd = os.open(path, os.O_RDONLY) # works for a file or a directory
try: try:
os.fsync(dir_fd) os.fsync(fd)
finally: finally:
os.close(dir_fd) os.close(fd)
def _make_dir(path): def _make_dir(path):
''' mkdir path, ignoring FileExistsError; return whether we ''' mkdir path, ignoring FileExistsError; return whether we
@ -392,7 +392,7 @@ def _make_dir(path):
''' '''
with suppress(FileExistsError): with suppress(FileExistsError):
os.mkdir(path) os.mkdir(path)
_fsync_dir(os.path.dirname(path)) _fsync_path(os.path.dirname(path))
LOGGER.info('Created directory: %s', path) LOGGER.info('Created directory: %s', path)
return True return True
return False return False
@ -400,13 +400,13 @@ def _make_dir(path):
def _remove_file(path): def _remove_file(path):
with suppress(FileNotFoundError): with suppress(FileNotFoundError):
os.remove(path) os.remove(path)
_fsync_dir(os.path.dirname(path)) _fsync_path(os.path.dirname(path))
LOGGER.info('Removed file: %s', path) LOGGER.info('Removed file: %s', path)
def _remove_empty_dir(path): def _remove_empty_dir(path):
try: try:
os.rmdir(path) os.rmdir(path)
_fsync_dir(os.path.dirname(path)) _fsync_path(os.path.dirname(path))
LOGGER.info('Removed empty directory: %s', path) LOGGER.info('Removed empty directory: %s', path)
except OSError as ex: except OSError as ex:
if ex.errno not in (errno.ENOENT, errno.ENOTEMPTY): if ex.errno not in (errno.ENOENT, errno.ENOTEMPTY):
@ -416,9 +416,9 @@ def _rename_file(src, dst):
os.rename(src, dst) os.rename(src, dst)
dst_dir = os.path.dirname(dst) dst_dir = os.path.dirname(dst)
src_dir = os.path.dirname(src) src_dir = os.path.dirname(src)
_fsync_dir(dst_dir) _fsync_path(dst_dir)
if src_dir != dst_dir: if src_dir != dst_dir:
_fsync_dir(src_dir) _fsync_path(src_dir)
LOGGER.info('Renamed file: %s -> %s', src, dst) LOGGER.info('Renamed file: %s -> %s', src, dst)
def _resize_file(path, size): def _resize_file(path, size):