]> git.apps.os.sepia.ceph.com Git - ceph.git/commit
cephadm: add write_new function for robust file writes
authorJohn Mulligan <jmulligan@redhat.com>
Tue, 6 Jun 2023 00:12:10 +0000 (20:12 -0400)
committerAdam King <adking@redhat.com>
Thu, 31 Aug 2023 17:35:13 +0000 (13:35 -0400)
commitf4138e33e6303efec34f8aeac7a2d999585b3a0d
treefc851e9a577037b5d1392be5c71376e9972af7e2
parentd5b6e497e819bec89e52079c8aa3441d67d94170
cephadm: add write_new function for robust file writes

The cephadm code has a very common pattern made of at least one of
the three following steps:
* call fchown on the open file to set ownership
* call fchmod on the open file to set permissions
* rename the file from a temp name to final name

Add the write_new function to encapsulate these common actions.
If owner is not None then fchown will be called.
If perms is not None then fchmod will be called.
An optional encoding value may be passed.
It always uses a temporary file as a temporary file ensures that
there can never be a partially written file even in the event of
a power outage or system crash.
Encapsulating this all into a function also allows us to make
changes to this approach in the future without touching every
call site using `open(..., "w")` etc.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
(cherry picked from commit 7a8bfb91af246e36242aee90c3f9245fcdf6a318)
src/cephadm/cephadm.py