tempfile: factor out deactivation
[gitweb.git] / tempfile.c
index 813cf6a81c86d4146f2486c480a4afe041066429..9d7f0a2f2b82038fbad1674ca716b6754858d619 100644 (file)
@@ -113,6 +113,18 @@ static void prepare_tempfile_object(struct tempfile *tempfile)
        }
 }
 
+static void activate_tempfile(struct tempfile *tempfile)
+{
+       tempfile->owner = getpid();
+       tempfile->active = 1;
+}
+
+static void deactivate_tempfile(struct tempfile *tempfile)
+{
+       tempfile->active = 0;
+       strbuf_reset(&tempfile->filename);
+}
+
 /* Make sure errno contains a meaningful value on error */
 int create_tempfile(struct tempfile *tempfile, const char *path)
 {
@@ -126,11 +138,10 @@ int create_tempfile(struct tempfile *tempfile, const char *path)
                tempfile->fd = open(tempfile->filename.buf,
                                    O_RDWR | O_CREAT | O_EXCL, 0666);
        if (tempfile->fd < 0) {
-               strbuf_reset(&tempfile->filename);
+               deactivate_tempfile(tempfile);
                return -1;
        }
-       tempfile->owner = getpid();
-       tempfile->active = 1;
+       activate_tempfile(tempfile);
        if (adjust_shared_perm(tempfile->filename.buf)) {
                int save_errno = errno;
                error("cannot fix permission bits on %s", tempfile->filename.buf);
@@ -145,8 +156,7 @@ 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;
+       activate_tempfile(tempfile);
 }
 
 int mks_tempfile_sm(struct tempfile *tempfile,
@@ -157,11 +167,10 @@ int mks_tempfile_sm(struct tempfile *tempfile,
        strbuf_add_absolute_path(&tempfile->filename, template);
        tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
        if (tempfile->fd < 0) {
-               strbuf_reset(&tempfile->filename);
+               deactivate_tempfile(tempfile);
                return -1;
        }
-       tempfile->owner = getpid();
-       tempfile->active = 1;
+       activate_tempfile(tempfile);
        return tempfile->fd;
 }
 
@@ -179,11 +188,10 @@ int mks_tempfile_tsm(struct tempfile *tempfile,
        strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, template);
        tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
        if (tempfile->fd < 0) {
-               strbuf_reset(&tempfile->filename);
+               deactivate_tempfile(tempfile);
                return -1;
        }
-       tempfile->owner = getpid();
-       tempfile->active = 1;
+       activate_tempfile(tempfile);
        return tempfile->fd;
 }
 
@@ -289,8 +297,7 @@ int rename_tempfile(struct tempfile *tempfile, const char *path)
                return -1;
        }
 
-       tempfile->active = 0;
-       strbuf_reset(&tempfile->filename);
+       deactivate_tempfile(tempfile);
        return 0;
 }
 
@@ -301,6 +308,5 @@ void delete_tempfile(struct tempfile *tempfile)
 
        close_tempfile_gently(tempfile);
        unlink_or_warn(tempfile->filename.buf);
-       tempfile->active = 0;
-       strbuf_reset(&tempfile->filename);
+       deactivate_tempfile(tempfile);
 }