summaryrefslogtreecommitdiff
path: root/tempfile.c
diff options
context:
space:
mode:
authorMichael Haggerty <mhagger@alum.mit.edu>2015-08-10 11:47:43 +0200
committerJunio C Hamano <gitster@pobox.com>2015-08-10 12:57:14 -0700
commit354ab1120665f691c17b21fdb04c4362c8088dfa (patch)
tree834868915e64ab1df72cee35700aed5dcf079fef /tempfile.c
parent7eba6ce5c79447579689218d8dab59a8434fd7c7 (diff)
downloadgit-354ab1120665f691c17b21fdb04c4362c8088dfa.tar.gz
tempfile: add several functions for creating temporary files
Add several functions for creating temporary files with automatically-generated names, analogous to mkstemps(), but also arranging for the files to be deleted on program exit. The functions are named according to a pattern depending how they operate. They will be used to replace many places in the code where temporary files are created and cleaned up ad-hoc. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'tempfile.c')
-rw-r--r--tempfile.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/tempfile.c b/tempfile.c
index d840f0452a..0b5d8ce929 100644
--- a/tempfile.c
+++ b/tempfile.c
@@ -137,6 +137,59 @@ int create_tempfile(struct tempfile *tempfile, const char *path)
return tempfile->fd;
}
+int mks_tempfile_sm(struct tempfile *tempfile,
+ const char *template, int suffixlen, int mode)
+{
+ prepare_tempfile_object(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);
+ return -1;
+ }
+ tempfile->owner = getpid();
+ tempfile->active = 1;
+ return tempfile->fd;
+}
+
+int mks_tempfile_tsm(struct tempfile *tempfile,
+ const char *template, int suffixlen, int mode)
+{
+ const char *tmpdir;
+
+ prepare_tempfile_object(tempfile);
+
+ tmpdir = getenv("TMPDIR");
+ if (!tmpdir)
+ tmpdir = "/tmp";
+
+ 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);
+ return -1;
+ }
+ tempfile->owner = getpid();
+ tempfile->active = 1;
+ return tempfile->fd;
+}
+
+int xmks_tempfile_m(struct tempfile *tempfile, const char *template, int mode)
+{
+ int fd;
+ struct strbuf full_template = STRBUF_INIT;
+
+ strbuf_add_absolute_path(&full_template, template);
+ fd = mks_tempfile_m(tempfile, full_template.buf, mode);
+ if (fd < 0)
+ die_errno("Unable to create temporary file '%s'",
+ full_template.buf);
+
+ strbuf_release(&full_template);
+ return fd;
+}
+
FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode)
{
if (!tempfile->active)