diff options
author | Mattias Andrée <maandree@kth.se> | 2021-04-04 03:44:31 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2021-04-04 03:44:31 +0200 |
commit | f09d00e12043234330a309537c234119d3aac409 (patch) | |
tree | 96046b775bcee1ade8f9151cf4a71d1c77321aea | |
parent | Full relay on umask to remove permissions for other users (diff) | |
download | libcontacts-f09d00e12043234330a309537c234119d3aac409.tar.gz libcontacts-f09d00e12043234330a309537c234119d3aac409.tar.bz2 libcontacts-f09d00e12043234330a309537c234119d3aac409.tar.xz |
Fix change if ID is unique when generate one
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r-- | libcontacts_save_contact.c | 15 |
1 files 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); |