tempfile: factor out activation
authorJeff King <peff@peff.net>
Tue, 5 Sep 2017 12:14:47 +0000 (08:14 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 6 Sep 2017 08:19:53 +0000 (17:19 +0900)
There are a few steps required to "activate" a tempfile
struct. Let's pull these out into a function. That saves a
few repeated lines now, but more importantly will make it
easier to change the activation scheme later.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
tempfile.c
index 813cf6a81c86d4146f2486c480a4afe041066429..0e6c6b9c18f1b43d273eba5c7b9d1e03c301477c 100644 (file)
@@ -113,6 +113,12 @@ static void prepare_tempfile_object(struct tempfile *tempfile)
        }
 }
 
+static void activate_tempfile(struct tempfile *tempfile)
+{
+       tempfile->owner = getpid();
+       tempfile->active = 1;
+}
+
 /* Make sure errno contains a meaningful value on error */
 int create_tempfile(struct tempfile *tempfile, const char *path)
 {
@@ -129,8 +135,7 @@ int create_tempfile(struct tempfile *tempfile, const char *path)
                strbuf_reset(&tempfile->filename);
                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 +150,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,
@@ -160,8 +164,7 @@ int mks_tempfile_sm(struct tempfile *tempfile,
                strbuf_reset(&tempfile->filename);
                return -1;
        }
-       tempfile->owner = getpid();
-       tempfile->active = 1;
+       activate_tempfile(tempfile);
        return tempfile->fd;
 }
 
@@ -182,8 +185,7 @@ int mks_tempfile_tsm(struct tempfile *tempfile,
                strbuf_reset(&tempfile->filename);
                return -1;
        }
-       tempfile->owner = getpid();
-       tempfile->active = 1;
+       activate_tempfile(tempfile);
        return tempfile->fd;
 }