Ensure remove files work when file was already removed

If a file disappears when you are removing it, this will ensure it
doesn't fail and continues as expected.
This commit is contained in:
Dag Wieers 2018-04-28 16:24:18 +02:00 committed by Toshio Kuratomi
commit 6a08b16c37

View file

@ -281,8 +281,9 @@ def main():
else: else:
try: try:
os.unlink(b_path) os.unlink(b_path)
except Exception as e: except OSError as e:
module.fail_json(path=path, msg="unlinking failed: %s " % to_native(e)) if e.errno != errno.ENOENT: # It may already have been removed
module.fail_json(path=path, msg="unlinking failed: %s " % to_native(e))
module.exit_json(path=path, changed=True, diff=diff) module.exit_json(path=path, changed=True, diff=diff)
else: else:
module.exit_json(path=path, changed=False) module.exit_json(path=path, changed=False)
@ -415,7 +416,11 @@ def main():
os.rmdir(b_path) os.rmdir(b_path)
elif prev_state == 'directory' and state == 'hard': elif prev_state == 'directory' and state == 'hard':
if os.path.exists(b_path): if os.path.exists(b_path):
os.remove(b_path) try:
os.unlink(b_path)
except OSError as e:
if e.errno != errno.ENOENT: # It may already have been removed
raise
if state == 'hard': if state == 'hard':
os.link(b_src, b_tmppath) os.link(b_src, b_tmppath)
else: else: