diff options
author | Jürg Billeter <j@bitron.ch> | 2019-02-26 11:01:14 +0000 |
---|---|---|
committer | Jürg Billeter <j@bitron.ch> | 2019-02-26 11:01:14 +0000 |
commit | 9b10fb916a4b54aa758ff13147848ab9e904594a (patch) | |
tree | ebb3e4e11c7468d583fa8aa3305d3b9568254eb0 | |
parent | d6ec082a3017882e69b070f1f5435b5a5eead29f (diff) | |
parent | b660a8c619dfa51c1797db0e22b16071fa54883b (diff) | |
download | buildstream-9b10fb916a4b54aa758ff13147848ab9e904594a.tar.gz |
Merge branch 'juerg/unlink' into 'master'
utils.py: safe_link(): Unlink only if target already exists
See merge request BuildStream/buildstream!1185
-rw-r--r-- | buildstream/utils.py | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/buildstream/utils.py b/buildstream/utils.py index a4e84097b..204afca0c 100644 --- a/buildstream/utils.py +++ b/buildstream/utils.py @@ -280,7 +280,7 @@ def safe_copy(src, dest, *, result=None): .format(src, dest, e)) from e -def safe_link(src, dest, *, result=None): +def safe_link(src, dest, *, result=None, _unlink=False): """Try to create a hardlink, but resort to copying in the case of cross device links. Args: @@ -292,19 +292,23 @@ def safe_link(src, dest, *, result=None): UtilError: In the case of unexpected system call failures """ - # First unlink the target if it exists - try: - os.unlink(dest) - except OSError as e: - if e.errno != errno.ENOENT: - raise UtilError("Failed to remove destination file '{}': {}" - .format(dest, e)) from e + if _unlink: + # First unlink the target if it exists + try: + os.unlink(dest) + except OSError as e: + if e.errno != errno.ENOENT: + raise UtilError("Failed to remove destination file '{}': {}" + .format(dest, e)) from e # If we can't link it due to cross-device hardlink, copy try: os.link(src, dest) except OSError as e: - if e.errno == errno.EXDEV: + if e.errno == errno.EEXIST and not _unlink: + # Target exists already, unlink and try again + safe_link(src, dest, result=result, _unlink=True) + elif e.errno == errno.EXDEV: safe_copy(src, dest) else: raise UtilError("Failed to link '{} -> {}': {}" |