config.mak.uname: set FREAD_READS_DIRECTORIES for Linux and FreeBSD
[gitweb.git] / tempfile.c
index 0b5d8ce92985634aae1f89d3fb4c4e2a01f0aaa1..68437106703470f39a6079aaad7d3bb407ac3389 100644 (file)
@@ -120,7 +120,12 @@ int create_tempfile(struct tempfile *tempfile, const char *path)
        prepare_tempfile_object(tempfile);
 
        strbuf_add_absolute_path(&tempfile->filename, path);
-       tempfile->fd = open(tempfile->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);
+       tempfile->fd = open(tempfile->filename.buf,
+                           O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0666);
+       if (O_CLOEXEC && tempfile->fd < 0 && errno == EINVAL)
+               /* Try again w/o O_CLOEXEC: the kernel might not support it */
+               tempfile->fd = open(tempfile->filename.buf,
+                                   O_RDWR | O_CREAT | O_EXCL, 0666);
        if (tempfile->fd < 0) {
                strbuf_reset(&tempfile->filename);
                return -1;
@@ -137,6 +142,14 @@ int create_tempfile(struct tempfile *tempfile, const char *path)
        return tempfile->fd;
 }
 
+void register_tempfile(struct tempfile *tempfile, const char *path)
+{
+       prepare_tempfile_object(tempfile);
+       strbuf_add_absolute_path(&tempfile->filename, path);
+       tempfile->owner = getpid();
+       tempfile->active = 1;
+}
+
 int mks_tempfile_sm(struct tempfile *tempfile,
                    const char *template, int suffixlen, int mode)
 {
@@ -234,12 +247,13 @@ int close_tempfile(struct tempfile *tempfile)
        tempfile->fd = -1;
        if (fp) {
                tempfile->fp = NULL;
-
-               /*
-                * Note: no short-circuiting here; we want to fclose()
-                * in any case!
-                */
-               err = ferror(fp) | fclose(fp);
+               if (ferror(fp)) {
+                       err = -1;
+                       if (!fclose(fp))
+                               errno = EIO;
+               } else {
+                       err = fclose(fp);
+               }
        } else {
                err = close(fd);
        }