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
parent f3399a5e34
commit 6a08b16c37

View file

@ -281,8 +281,9 @@ def main():
else:
try:
os.unlink(b_path)
except Exception as e:
module.fail_json(path=path, msg="unlinking failed: %s " % to_native(e))
except OSError as 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)
else:
module.exit_json(path=path, changed=False)
@ -415,7 +416,11 @@ def main():
os.rmdir(b_path)
elif prev_state == 'directory' and state == 'hard':
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':
os.link(b_src, b_tmppath)
else: