register_tempfile(): new function to handle an existing temporary file
authorMichael Haggerty <mhagger@alum.mit.edu>
Mon, 10 Aug 2015 09:47:44 +0000 (11:47 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 10 Aug 2015 19:57:14 +0000 (12:57 -0700)
Allow an existing file to be registered with the tempfile-handling
infrastructure; in particular, arrange for it to be deleted on program
exit. This can be used if the temporary file has to be created in a
more complicated way than just open(). For example:

* If the file itself needs to be created via the lockfile API
* If it is not a regular file (e.g., a socket)

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
tempfile.c
tempfile.h
index 0b5d8ce92985634aae1f89d3fb4c4e2a01f0aaa1..0af7ebf016745c4a114a12d207381342b29ccf4f 100644 (file)
@@ -137,6 +137,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)
 {
index a30e12c7e5c386d66aeb7a347e8476f9e3bb6d21..4219fe41bd3e2ad16f0b1caf55eedca0b2d9986e 100644 (file)
@@ -92,6 +92,14 @@ struct tempfile {
  */
 extern int create_tempfile(struct tempfile *tempfile, const char *path);
 
+/*
+ * Register an existing file as a tempfile, meaning that it will be
+ * deleted when the program exits. The tempfile is considered closed,
+ * but it can be worked with like any other closed tempfile (for
+ * example, it can be opened using reopen_tempfile()).
+ */
+extern void register_tempfile(struct tempfile *tempfile, const char *path);
+
 
 /*
  * mks_tempfile functions