Optimize the _link_file function to not call os.stat redundantly.

This will eliminate 2 calls to os.stat per one invocation of the _link_file function.
Assuming during the compose build 50000 files are linked, this change will eliminate 100000 redundant calls to os.stat.

Jira: RHELCMP-797
Signed-off-by: Bohdan Khomutskyi <bkhomuts@redhat.com>
This commit is contained in:
Bohdan Khomutskyi 2020-05-19 16:11:43 +02:00
parent 4ba65710c2
commit 694b7f3d28

View File

@ -213,12 +213,13 @@ class Linker(kobo.log.LoggingBase):
relative = link_type != "abspath-symlink" relative = link_type != "abspath-symlink"
self.symlink(src, dst, relative) self.symlink(src, dst, relative)
elif link_type == "hardlink-or-copy": elif link_type == "hardlink-or-copy":
src_stat = os.stat(src) try:
dst_stat = os.stat(os.path.dirname(dst))
if src_stat.st_dev == dst_stat.st_dev:
self.hardlink(src, dst) self.hardlink(src, dst)
else: except OSError as ex:
if ex.errno == errno.EXDEV:
self.copy(src, dst) self.copy(src, dst)
else:
raise
else: else:
raise ValueError("Unknown link_type: %s" % link_type) raise ValueError("Unknown link_type: %s" % link_type)