From b753625dbf367fba8308873d0378e0de70bb2cb0 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Fri, 2 May 2014 14:45:51 -0500 Subject: [PATCH] Refuse to convert a non-empty directory into a link with the file module Also adds an integration test for the above. Fixes #7254 --- library/files/file | 9 +++- .../roles/test_file/tasks/main.yml | 45 ++++++++++++++++++- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/library/files/file b/library/files/file index 13beb646e77..d758d54b8cc 100644 --- a/library/files/file +++ b/library/files/file @@ -192,8 +192,13 @@ def main(): if state == 'hard': if not os.path.isabs(src): module.fail_json(msg="absolute paths are required") - - elif prev_state in ['file', 'hard', 'directory'] and not force: + elif prev_state == 'directory': + if not force: + module.fail_json(path=path, msg='refusing to convert between %s and %s for %s' % (prev_state, state, src)) + elif len(os.listdir(path)) > 0: + # refuse to replace a directory that has files in it + module.fail_json(path=path, msg='the directory %s is not empty, refusing to convert it' % path) + elif prev_state in ['file', 'hard'] and not force: module.fail_json(path=path, msg='refusing to convert between %s and %s for %s' % (prev_state, state, src)) if prev_state == 'absent': diff --git a/test/integration/roles/test_file/tasks/main.yml b/test/integration/roles/test_file/tasks/main.yml index 588c1b6747b..92457fcd795 100644 --- a/test/integration/roles/test_file/tasks/main.yml +++ b/test/integration/roles/test_file/tasks/main.yml @@ -183,5 +183,48 @@ that: - "file13_result.changed == true" -- name: remote directory foobar +- name: remove directory foobar file: path={{output_dir}}/foobar state=absent + register: file14_result + +- name: verify that the directory was removed + assert: + that: + - 'file14_result.changed == true' + - 'file14_result.state == "absent"' + +- name: create a test sub-directory + file: dest={{output_dir}}/sub1 state=directory + register: file15_result + +- name: verify that the new directory was created + assert: + that: + - 'file15_result.changed == true' + - 'file15_result.state == "directory"' + +- name: create test files in the sub-directory + file: dest={{output_dir}}/sub1/{{item}} state=touch + with_items: + - file1 + - file2 + - file3 + register: file16_result + +- name: verify the files were created + assert: + that: + - 'item.changed == true' + - 'item.state == "file"' + with_items: file16_result.results + +- name: try to force the sub-directory to a link + file: src={{output_dir}}/testing dest={{output_dir}}/sub1 state=link force=yes + register: file17_result + ignore_errors: true + +- name: verify the directory was not replaced with a link + assert: + that: + - 'file17_result.failed == true' + - 'file17_result.state == "directory"'