aboutsummaryrefslogtreecommitdiffstats
path: root/libcontacts_save_contact.c
diff options
context:
space:
mode:
Diffstat (limited to 'libcontacts_save_contact.c')
-rw-r--r--libcontacts_save_contact.c15
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);