From f09d00e12043234330a309537c234119d3aac409 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 4 Apr 2021 03:44:31 +0200 Subject: Fix change if ID is unique when generate one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libcontacts_save_contact.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/libcontacts_save_contact.c b/libcontacts_save_contact.c index 2e64c4a..2d9602b 100644 --- a/libcontacts_save_contact.c +++ b/libcontacts_save_contact.c @@ -51,18 +51,25 @@ libcontacts_save_contact(struct libcontacts_contact *contact, const struct passw tmppath = alloca(strlen(path) + sizeof("~")); stpcpy(stpcpy(tmppath, path), "~"); - fd = open(tmppath, oflags, 0666); - if (fd < 0) { - if ((oflags & O_EXCL) && errno == EEXIST) { + if (oflags & O_EXCL) { + fd = open(path, oflags, 0666); + if (fd < 0) { + if (errno != EEXIST) + goto fail; if (!num++) { basenam = contact->id; contact->id = NULL; } goto generate_id; } - goto fail; + close(fd); + oflags ^= O_EXCL ^ O_TRUNC; } + fd = open(tmppath, oflags, 0666); + if (fd < 0) + goto fail; + n = strlen(data); for (p = 0; p < n; p += (size_t)r) { r = write(fd, &data[p], n - p); -- cgit v1.2.3-70-g09d2