From 63e6c00864a8a4134c9bd3187d2422a6511c84b0 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Thu, 1 Jul 2021 20:46:43 +0200 Subject: vyos.util: fix IsADirectoryError and SameFileError for copy_file Commit 5303ec39 ("vyos.util: add new helper copy_file()") added a new helper function to copy a file from A -> B and create the destination directory if required. It did also throw an excpetion if the destination file already existed and consisted of the same file - this is now ignored and we always copy the source to the destination. --- python/vyos/util.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/python/vyos/util.py b/python/vyos/util.py index c318d58de..446285a1b 100644 --- a/python/vyos/util.py +++ b/python/vyos/util.py @@ -231,8 +231,14 @@ def copy_file(source, destination, mkdstdir=False, user=None, group=None): dirname = os.path.dirname(destination) if not os.path.isdir(dirname): makedir(dirname, user, group) - - shutil.copyfile(source, destination) + try: + filename = os.path.basename(source) + if not destination.endswith('/'): + destination + '/' + shutil.copyfile(source, destination + filename) + except shutil.SameFileError: + # We don't care if we copy the same file again + pass chown(destination, user, group) def read_json(fname, defaultonfailure=None): -- cgit v1.2.3