module_utils/atomic_move(): Restore owner/group

Manually restore owner and group if both src and dest are on a separate
file system.
pull/6593/head
Till Maas 11 years ago
parent 7edee91aba
commit 8f778a83df

@ -977,11 +977,16 @@ class AnsibleModule(object):
try: # leaves tmp file behind when sudo and not root try: # leaves tmp file behind when sudo and not root
if os.getenv("SUDO_USER") and os.getuid() != 0: if os.getenv("SUDO_USER") and os.getuid() != 0:
# cleanup will happen by 'rm' of tempdir # cleanup will happen by 'rm' of tempdir
shutil.copy(src, tmp_dest) # copy2 will preserve some metadata
shutil.copy2(src, tmp_dest)
else: else:
shutil.move(src, tmp_dest) shutil.move(src, tmp_dest)
if self.selinux_enabled(): if self.selinux_enabled():
self.set_context_if_different(tmp_dest, context, False) self.set_context_if_different(
tmp_dest, context, False)
# Reset owners, they are not preserved by shutil.copy2(), which
# is what shutil.move() falls back to.
os.chown(tmp_dest, st.st_uid, st.st_gid)
os.rename(tmp_dest, dest) os.rename(tmp_dest, dest)
except (shutil.Error, OSError, IOError), e: except (shutil.Error, OSError, IOError), e:
self.cleanup(tmp_dest) self.cleanup(tmp_dest)

Loading…
Cancel
Save