diff options
Diffstat (limited to 'src/buildstream/utils.py')
-rw-r--r-- | src/buildstream/utils.py | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/src/buildstream/utils.py b/src/buildstream/utils.py index 99c22d169..9c6761ccc 100644 --- a/src/buildstream/utils.py +++ b/src/buildstream/utils.py @@ -363,19 +363,20 @@ def sha256sum(filename: str) -> str: return h.hexdigest() -def safe_copy(src: str, dest: str, *, result: Optional[FileListResult] = None) -> None: - """Copy a file while preserving attributes +def safe_copy(src: str, dest: str, *, copystat: bool = True, result: Optional[FileListResult] = None) -> None: + """Copy a file while optionally preserving attributes Args: src: The source filename dest: The destination filename + copystat: Whether to preserve attributes result: An optional collective result Raises: UtilError: In the case of unexpected system call failures - This is almost the same as shutil.copy2(), except that - we unlink *dest* before overwriting it if it exists, just + This is almost the same as shutil.copy2() when copystat is True, + except that we unlink *dest* before overwriting it if it exists, just incase *dest* is a hardlink to a different file. """ # First unlink the target if it exists @@ -385,22 +386,24 @@ def safe_copy(src: str, dest: str, *, result: Optional[FileListResult] = None) - if e.errno != errno.ENOENT: raise UtilError("Failed to remove destination file '{}': {}".format(dest, e)) from e - shutil.copyfile(src, dest) try: - shutil.copystat(src, dest) - except PermissionError: - # If we failed to copy over some file stats, dont treat - # it as an unrecoverable error, but provide some feedback - # we can use for a warning. - # - # This has a tendency of happening when attempting to copy - # over extended file attributes. - if result: - result.failed_attributes.append(dest) - - except shutil.Error as e: + shutil.copyfile(src, dest) + except (OSError, shutil.Error) as e: raise UtilError("Failed to copy '{} -> {}': {}".format(src, dest, e)) from e + if copystat: + try: + shutil.copystat(src, dest) + except PermissionError: + # If we failed to copy over some file stats, dont treat + # it as an unrecoverable error, but provide some feedback + # we can use for a warning. + # + # This has a tendency of happening when attempting to copy + # over extended file attributes. + if result: + result.failed_attributes.append(dest) + def safe_link(src: str, dest: str, *, result: Optional[FileListResult] = None, _unlink=False) -> None: """Try to create a hardlink, but resort to copying in the case of cross device links. |