This way a new file mode can be applied if it has changed.
Since we are always writing the whole file (mode 'w') rather than
appending, we aren't losing anything by doing this.
Signed-off-by: Travis Rhoden <trhoden@redhat.com>
if path.startswith("/"):
path = path[1:]
path = os.path.join(directory, path)
+ if os.path.exists(path):
+ # Delete file in case we are changing its mode
+ os.unlink(path)
with os.fdopen(os.open(path, os.O_WRONLY | os.O_CREAT, mode), 'w') as f:
f.write(content)
os.chown(path, uid, gid)