- if (safe_create_leading_directories(logfile->buf) < 0) {
- strbuf_addf(err, "unable to create directory for '%s': "
- "%s", logfile->buf, strerror(errno));
- return -1;
- }
- logfd = open(logfile->buf, O_APPEND | O_WRONLY | O_CREAT, 0666);
- if (logfd < 0) {
- if (errno == EISDIR) {
- /*
- * The directory that is in the way might be
- * empty. Try to remove it.
- */
- if (remove_empty_directories(logfile)) {
- strbuf_addf(err, "there are still logs under "
- "'%s'", logfile->buf);
- return -1;
- }
- logfd = open(logfile->buf, O_APPEND | O_WRONLY | O_CREAT, 0666);
- }
-
- if (logfd < 0) {
+ if (raceproof_create_file(logfile->buf, open_or_create_logfile, &logfd)) {
+ if (errno == ENOENT)
+ strbuf_addf(err, "unable to create directory for '%s': "
+ "%s", logfile->buf, strerror(errno));
+ else if (errno == EISDIR)
+ strbuf_addf(err, "there are still logs under '%s'",
+ logfile->buf);
+ else