Skip to content

Commit 1f7bcbe

Browse files
skshetryefiop
authored andcommitted
reflink: use os.open instead of builtin.open
improves performance for Linux by as much as 50%.
1 parent d7fe0a6 commit 1f7bcbe

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/dvc_objects/fs/system.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,28 @@ def reflink(src, dst):
8888
FICLONE = 0x40049409
8989

9090
def reflink(src, dst):
91+
src_fd = os.open(src, os.O_RDONLY)
92+
93+
try:
94+
dst_fd = os.open(dst, os.O_WRONLY | os.O_CREAT | os.O_TRUNC)
95+
except OSError:
96+
os.close(src_fd)
97+
raise
98+
9199
try:
92-
with open(src, "rb") as s, open(dst, "wb+") as d:
93-
fcntl.ioctl(d.fileno(), FICLONE, s.fileno())
100+
fcntl.ioctl(dst_fd, FICLONE, src_fd)
94101
except OSError:
102+
os.close(src_fd)
103+
os.close(dst_fd)
95104
try:
96105
os.unlink(dst)
97106
except OSError:
98107
pass
99108
raise
109+
else:
110+
os.close(src_fd)
111+
os.close(dst_fd)
112+
100113
else:
101114

102115
def reflink(src, dst):

0 commit comments

Comments
 (0)