From 4fd957620a5c0335f8798c11a089e57b162e1cc5 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Tue, 18 Mar 2014 20:41:42 -0400 Subject: [PATCH 1/2] now correctly checks absolute path for src= existance for links also updated docs to be a bit clearer on symlink behaviour --- files/file | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/files/file b/files/file index b1c57ff4d97..4296af2011f 100644 --- a/files/file +++ b/files/file @@ -81,8 +81,8 @@ options: default: null choices: [] description: - - path of the file to link to (applies only to C(state=link)). Will accept absolute, - relative and nonexisting paths. Relative paths are not expanded. + - path of the file to link to (applies only to C(state= link or hard)). Will accept absolute, + relative and nonexisting (with C(force)) paths. Relative paths are not expanded. seuser: required: false default: null @@ -266,8 +266,12 @@ def main(): elif state in ['link','hard']: - if not os.path.exists(src) and not force: - module.fail_json(path=path, src=src, msg='src file does not exist') + absrc = src + if not os.path.isabs(absrc): + absrc = os.path.normpath('%s/%s' % (os.path.dirname(path), absrc)) + + if not os.path.exists(absrc) and not force: + module.fail_json(path=path, src=src, msg='src file does not exist, use "force=yes" if you really want to create the link: %s' % absrc) if state == 'hard': if not os.path.isabs(src): From a395eb3aba4f759035109ef873cd1d8dad2a2fed Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 19 Mar 2014 09:11:17 -0400 Subject: [PATCH 2/2] fixed issue with tmp file (path can include filename) and added missing else which made it try to link x2 --- files/file | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/files/file b/files/file index 4296af2011f..e1bdf53375f 100644 --- a/files/file +++ b/files/file @@ -301,7 +301,7 @@ def main(): if changed and not module.check_mode: if prev_state != 'absent': # try to replace atomically - tmppath = ".%s.%s.%s.tmp" % (path,os.getpid(),time.time()) + tmppath = '/'.join([os.path.dirname(path), ".%s.%s.tmp" % (os.getpid(),time.time())]) try: if state == 'hard': os.link(src,tmppath) @@ -311,13 +311,14 @@ def main(): except OSError, e: os.unlink(tmppath) module.fail_json(path=path, msg='Error while replacing: %s' % str(e)) - try: - if state == 'hard': - os.link(src,path) - else: - os.symlink(src, path) - except OSError, e: - module.fail_json(path=path, msg='Error while linking: %s' % str(e)) + else: + try: + if state == 'hard': + os.link(src,path) + else: + os.symlink(src, path) + except OSError, e: + module.fail_json(path=path, msg='Error while linking: %s' % str(e)) changed = module.set_fs_attributes_if_different(file_args, changed) module.exit_json(dest=path, src=src, changed=changed)