From aac194e6395a7c8ad8ab178bc7a97b0c0e9dd86a Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Wed, 18 Jun 2014 14:54:44 -0500 Subject: [PATCH] Double check whether the parent directory really exists using stat() Fixes #7760 --- library/files/copy | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/library/files/copy b/library/files/copy index 9d7bedb2446..0a5fa699d03 100644 --- a/library/files/copy +++ b/library/files/copy @@ -190,6 +190,15 @@ def main(): md5sum_dest = module.md5(dest) else: if not os.path.exists(os.path.dirname(dest)): + try: + # os.path.exists() can return false in some + # circumstances where the directory does not have + # the execute bit for the current user set, in + # which case the stat() call will raise an OSError + os.stat(os.path.dirname(dest)) + except OSError, e: + if "permission denied" in str(e).lower(): + module.fail_json(msg="Destination directory %s is not accessible" % (os.path.dirname(dest))) module.fail_json(msg="Destination directory %s does not exist" % (os.path.dirname(dest))) if not os.access(os.path.dirname(dest), os.W_OK): module.fail_json(msg="Destination %s not writable" % (os.path.dirname(dest)))