Unarchive should work when parent directory is not writable

Correct unarchive so that the checks for writeability are
sensible.

Added a test for when parent directory is not writable
This commit is contained in:
Will Thames 2014-08-05 10:41:26 +10:00
parent 2d54448064
commit d9b895b319
2 changed files with 29 additions and 7 deletions

View file

@ -115,8 +115,6 @@ class TgzFile(object):
self.zipflag = 'z'
def is_unarchived(self):
dirof = os.path.dirname(self.dest)
destbase = os.path.basename(self.dest)
cmd = '%s -v -C "%s" --diff -%sf "%s"' % (self.cmd_path, self.dest, self.zipflag, self.src)
rc, out, err = self.module.run_command(cmd)
unarchived = (rc == 0)
@ -220,10 +218,10 @@ def main():
)
# is dest OK to receive tar file?
if not os.path.exists(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)))
if not os.path.isdir(dest):
module.fail_json(msg="Destination '%s' is not a directory" % dest)
if not os.access(dest, os.W_OK):
module.fail_json(msg="Destination '%s' not writable" % dest)
handler = pick_handler(src, dest, module)

View file

@ -110,4 +110,28 @@
file: path={{output_dir}}/test-unarchive-zip state=absent
- name: remove our test file for the archive
file: path={{output_dir}}/foo-unarchive.txt state=absent
file: path={{output_dir}}/foo-unarchive.txt state=absent
- name: check if /tmp/foo-unarchive.text exists
stat: path=/tmp/foo-unarchive.txt
ignore_errors: True
register: unarchive04
- name: fail if the proposed destination file exists for safey
fail: msg="/tmp/foo-unarchive.txt already exists, aborting"
when: unarchive04.stat.exists
- name: try unarchiving to /tmp
unarchive: src={{output_dir}}/test-unarchive.tar.gz dest=/tmp copy=no
register: unarchive05
- name: verify that the file was marked as changed
assert:
that:
- "unarchive05.changed == true"
- name: verify that the file was unarchived
file: path=/tmp/foo-unarchive.txt state=file
- name: remove our unarchive destination
file: path=/tmp/foo-unarchive.txt state=absent