zuul-cloner-shim: Use st_dev to check for filesystem

Rather than check mounts, check if the source and destination reside
on the same st_dev; if so use hardlinks, otherwise copy.

Change-Id: Ic5fdc899d70c67ddcfc19994c254a8efcd0fd3d6
This commit is contained in:
Ian Wienand 2017-10-11 10:59:40 +11:00
parent d9199fd008
commit e46b67a1d9

View File

@ -124,19 +124,6 @@ def readCloneMap(clone_map):
return clone_map
def get_fs(mounts, path):
longest_match = ''
real_path = os.path.realpath(path)
for mount_path in mounts.keys():
if (real_path.startswith(mount_path) and
len(mount_path) > len(longest_match)):
longest_match = mount_path
if longest_match:
return mounts[longest_match]
else:
return None
def main():
args = parseArgs()
@ -147,12 +134,6 @@ def main():
mapper = CloneMapper(clone_map, args.projects)
dests = mapper.expand(workspace=args.workspace)
mounts = {}
with open('/proc/mounts') as f:
for line in f.readlines():
fs, mount_path = line.split()[0:2]
mounts[mount_path] = fs
for project in args.projects:
src = os.path.join(os.path.expanduser(REPO_SRC_DIR), project)
if not os.path.exists(src):
@ -187,19 +168,21 @@ def main():
print("Creating %s" % d)
os.makedirs(d)
src_fs = get_fs(mounts, src)
dst_fs = get_fs(mounts, dst)
#
# Create (possible hard link) copy of the source directory
#
# Create hard link copy of the source directory
# Check if src & dst are on the same filesystem; if so we will
# hardlink, otherwise we copy
src_dev = os.stat(src).st_dev
dst_dev = os.stat(dst).st_dev
link = "l" if src_dev == dst_dev else ""
# note: don't use "-a" here as that implies "--preserve=all"
# which overwrites the permissions of dst from src ... this is
# fatal to ssh if dst is a home directory and we make it
# world-accessable. This should leave dst alone
if src_fs == dst_fs:
cmd = "cp -dRl %s/. %s" % (src, dst)
else:
cmd = "cp -dR %s/. %s" % (src, dst)
cmd = "cp -dR%s %s/. %s" % (link, src, dst)
print("%s" % cmd)
if os.system(cmd):
print("Error executing: %s" % cmd)