mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-24 03:11:24 -07:00
When atomically replacing files, copy attributes
This commit is contained in:
parent
523f2bf327
commit
7a8009f979
3 changed files with 16 additions and 8 deletions
|
@ -594,6 +594,20 @@ class AnsibleModule(object):
|
|||
self.fail_json(msg='Could not make backup of %s to %s: %s' % (fn, backupdest, e))
|
||||
return backupdest
|
||||
|
||||
def atomic_replace(self, src, dest):
|
||||
'''atomically replace dest with src, copying attributes from dest'''
|
||||
st = os.stat(dest)
|
||||
os.chmod(src, st.st_mode & 07777)
|
||||
try:
|
||||
os.chown(src, st.st_uid, st.st_gid)
|
||||
except OSError, e:
|
||||
if e.errno != errno.EPERM:
|
||||
raise
|
||||
if self.selinux_enabled():
|
||||
context = self.selinux_context(dest)
|
||||
self.set_context_if_different(src, context, False)
|
||||
os.rename(src, dest)
|
||||
|
||||
|
||||
# == END DYNAMICALLY INSERTED CODE ===
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue