nikstur / userborn

Declaratively bear (manage) Linux users and groups
MIT License
48 stars 1 forks source link

Fix atomic write #2

Closed Mic92 closed 2 months ago

nikstur commented 2 months ago

Can you provide a description of what that fixes and how? I fear I don't really get it from the code.

And more concretely: why do I want to always create a new .tmp file instead of just overwriting an existing one?

Mic92 commented 2 months ago

I updated the commit description:

atomic_rename: fix potential race condition

When two threads override the same file at the same time,
they might end up both opening the same tmp file i.e. `/etc/passwd.tmp`
and than writing their content to the same.

The new approach opens the tmp file with `create_new` flag, which
fails if the file already exists. In that case, it increments the
index and tries again.