#include <err.h>
#include <unistd.h>
#include <assert.h>
+#include <signal.h>
#include "tools.h"
-static char *tmpdir = NULL;
+static const char *tmpdir = NULL;
bool tools_verbose = false;
/* Ten minutes. */
if (tools_verbose)
printf("Running: %s\n", cmd);
+ /* Always flush buffers before fork! */
+ fflush(stdout);
gettimeofday(&start, NULL);
pid = fork();
if (pid == -1) {
return false;
}
-static int unlink_all(char *dir)
+static int unlink_all(const char *dir)
{
char cmd[strlen(dir) + sizeof("rm -rf ")];
sprintf(cmd, "rm -rf %s", dir);
return 0;
}
-char *temp_dir(const void *ctx)
+const char *temp_dir(const void *ctx)
{
/* For first call, create dir. */
while (!tmpdir) {
return tmpdir;
}
+int unlink_file_destructor(char *filename)
+{
+ unlink(filename);
+ return 0;
+}
+
char *maybe_temp_file(const void *ctx, const char *extension, bool keep,
const char *srcname)
{
struct stat st;
unsigned int count = 0;
- if (!keep)
- srcname = talloc_basename(ctx, srcname);
- else
- assert(srcname[0] == '/');
-
+ srcname = talloc_basename(ctx, srcname);
if (strrchr(srcname, '.'))
baselen = strrchr(srcname, '.') - srcname;
else
do {
f = talloc_asprintf(ctx, "%s/%.*s%s%s",
- keep ? "" : temp_dir(ctx),
+ temp_dir(ctx),
baselen, srcname,
suffix, extension);
talloc_free(suffix);
} while (lstat(f, &st) == 0);
if (tools_verbose)
- printf("Creating file %s\n", f);
+ printf("Creating %sfile %s\n", keep ? "" : "temporary ", f);
+
+ if (!keep)
+ talloc_set_destructor(f, unlink_file_destructor);
talloc_free(suffix);
return f;