From dc01443085df51f190d0bf000c561747d6786a39 Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Tue, 7 Jan 2014 11:47:08 -0600 Subject: [PATCH] Fix regression in salt.utils.copyfile() In pull request #9599, I added code to make salt.utils.copyfile() set the ownership and permissions of the new file to those of the pre-existing file. However, this raises an exception when the destination file doesn't exist, which happens on file.managed states which are creating new files. This commit fixes that regression. --- salt/utils/__init__.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/salt/utils/__init__.py b/salt/utils/__init__.py index 5239a85706..885afbc598 100644 --- a/salt/utils/__init__.py +++ b/salt/utils/__init__.py @@ -642,10 +642,14 @@ def copyfile(source, dest, backup_mode='', cachedir=''): pass # Get current file stats to they can be replicated after the new file is # moved to the destination path. - fstat = os.stat(dest) + try: + fstat = os.stat(dest) + except OSError: + fstat = None shutil.move(tgt, dest) - os.chown(dest, fstat.st_uid, fstat.st_gid) - os.chmod(dest, fstat.st_mode) + if fstat is not None: + os.chown(dest, fstat.st_uid, fstat.st_gid) + os.chmod(dest, fstat.st_mode) # If SELINUX is available run a restorecon on the file rcon = which('restorecon') if rcon: