tempfile: add mks_tempfile_dt()

Add a function to create a temporary file with a certain name in a
temporary directory created using mkdtemp(3).  Its result is more
sightly than the paths created by mks_tempfile_ts(), which include
a random prefix.  That's useful for files passed to a program that
displays their name, e.g. an external diff tool.

Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
René Scharfe
2022-04-20 22:26:09 +02:00
committed by Junio C Hamano
parent 6cd33dceed
commit 2c2db194bd
2 changed files with 76 additions and 0 deletions

View File

@@ -82,6 +82,7 @@ struct tempfile {
FILE *volatile fp;
volatile pid_t owner;
struct strbuf filename;
size_t directorylen;
};
/*
@@ -198,6 +199,18 @@ static inline struct tempfile *xmks_tempfile(const char *filename_template)
return xmks_tempfile_m(filename_template, 0600);
}
/*
* Attempt to create a temporary directory in $TMPDIR and to create and
* open a file in that new directory. Derive the directory name from the
* template in the manner of mkdtemp(). Arrange for directory and file
* to be deleted if the program exits before they are deleted
* explicitly. On success return a tempfile whose "filename" member
* contains the full path of the file and its "fd" member is open for
* writing the file. On error return NULL and set errno appropriately.
*/
struct tempfile *mks_tempfile_dt(const char *directory_template,
const char *filename);
/*
* Associate a stdio stream with the temporary file (which must still
* be open). Return `NULL` (*without* deleting the file) on error. The