From 63bceea014e091a2f2907e49f7c3b33f042106f4 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 10 Sep 2010 12:36:26 +0930 Subject: [PATCH] ccanlint: use familiar names for temporary files, show them with -vv. --- tools/ccanlint/compulsory_tests/build.c | 2 +- tools/ccanlint/compulsory_tests/check_build.c | 6 ++- .../compulsory_tests/compile_test_helpers.c | 2 +- tools/compile.c | 5 ++- tools/depends.c | 4 +- tools/tools.c | 37 ++++++++++++------- tools/tools.h | 8 ++-- 7 files changed, 38 insertions(+), 26 deletions(-) diff --git a/tools/ccanlint/compulsory_tests/build.c b/tools/ccanlint/compulsory_tests/build.c index a8f7bcb6..c78e500b 100644 --- a/tools/ccanlint/compulsory_tests/build.c +++ b/tools/ccanlint/compulsory_tests/build.c @@ -44,7 +44,7 @@ static void *do_build(struct manifest *m, build.total_score = 0; return NULL; } - filename = link_objects(m, obj_list(m), &err); + filename = link_objects(m, m->basename, false, obj_list(m), &err); if (filename && keep) { char *realname = talloc_asprintf(m, "%s.o", m->dir); /* We leave this object file around, all built. */ diff --git a/tools/ccanlint/compulsory_tests/check_build.c b/tools/ccanlint/compulsory_tests/check_build.c index 5bdec553..42eda13a 100644 --- a/tools/ccanlint/compulsory_tests/check_build.c +++ b/tools/ccanlint/compulsory_tests/check_build.c @@ -51,9 +51,10 @@ static void *check_use_build(struct manifest *m, { char *contents; char *tmpfile; + char *basename = talloc_asprintf(m, "%s/example.c", m->dir); int fd; - tmpfile = temp_file(m, ".c"); + tmpfile = maybe_temp_file(m, ".c", keep, basename); fd = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0600); if (fd < 0) @@ -74,7 +75,8 @@ static void *check_use_build(struct manifest *m, close(fd); return compile_and_link(m, tmpfile, ccan_dir, obj_list(m), "", - lib_list(m), temp_file(m, "")); + lib_list(m), + maybe_temp_file(m, "", keep, tmpfile)); } static const char *describe_use_build(struct manifest *m, void *check_result) diff --git a/tools/ccanlint/compulsory_tests/compile_test_helpers.c b/tools/ccanlint/compulsory_tests/compile_test_helpers.c index 94daaa1b..207c0b2d 100644 --- a/tools/ccanlint/compulsory_tests/compile_test_helpers.c +++ b/tools/ccanlint/compulsory_tests/compile_test_helpers.c @@ -25,7 +25,7 @@ static char *compile(struct manifest *m, bool keep, struct ccan_file *cfile) { - cfile->compiled = maybe_temp_file(m, "", keep, cfile->fullname); + cfile->compiled = maybe_temp_file(m, ".o", keep, cfile->fullname); return compile_object(m, cfile->fullname, ccan_dir, "", cfile->compiled); } diff --git a/tools/compile.c b/tools/compile.c index 7ff04225..6707c4a6 100644 --- a/tools/compile.c +++ b/tools/compile.c @@ -5,9 +5,10 @@ bool compile_verbose = false; /* Compile multiple object files into a single. Returns errmsg if fails. */ -char *link_objects(const void *ctx, const char *objs, char **errmsg) +char *link_objects(const void *ctx, const char *basename, bool in_pwd, + const char *objs, char **errmsg) { - char *file = temp_file(ctx, ".o"); + char *file = maybe_temp_file(ctx, ".o", in_pwd, basename); if (compile_verbose) printf("Linking objects into %s\n", file); diff --git a/tools/depends.c b/tools/depends.c index 6ac31cdc..e2666d97 100644 --- a/tools/depends.c +++ b/tools/depends.c @@ -48,7 +48,7 @@ static char *compile_info(const void *ctx, const char *dir) if (!info) return NULL; - info_c_file = temp_file(ctx, ".c"); + info_c_file = maybe_temp_file(ctx, ".c", false, "_info"); fd = open(info_c_file, O_WRONLY|O_CREAT|O_EXCL, 0600); if (fd < 0) return NULL; @@ -61,7 +61,7 @@ static char *compile_info(const void *ctx, const char *dir) ccandir = talloc_dirname(ctx, dir); *strrchr(ccandir, '/') = '\0'; - compiled = temp_file(ctx, ""); + compiled = maybe_temp_file(ctx, "", false, "info"); if (compile_and_link(ctx, info_c_file, ccandir, "", "", "", compiled)) return NULL; diff --git a/tools/tools.c b/tools/tools.c index a4944bd1..d27db719 100644 --- a/tools/tools.c +++ b/tools/tools.c @@ -14,10 +14,10 @@ #include #include #include +#include #include "tools.h" static char *tmpdir = NULL; -static unsigned int count; bool tools_verbose = false; /* Ten minutes. */ @@ -210,28 +210,37 @@ char *temp_dir(const void *ctx) return tmpdir; } -char *temp_file(const void *ctx, const char *extension) -{ - char *f = talloc_asprintf(ctx, "%s/%u%s", - temp_dir(ctx), count++, extension); - if (tools_verbose) - printf("Created temporary file %s\n", f); - return f; -} - char *maybe_temp_file(const void *ctx, const char *extension, bool keep, const char *srcname) { size_t baselen; - char *f; + char *f, *suffix = talloc_strdup(ctx, ""); + struct stat st; + unsigned int count = 0; if (!keep) - return temp_file(ctx, extension); + srcname = talloc_basename(ctx, srcname); + else + assert(srcname[0] == '/'); + + if (strrchr(srcname, '.')) + baselen = strrchr(srcname, '.') - srcname; + else + baselen = strlen(srcname); + + do { + f = talloc_asprintf(ctx, "%s/%.*s%s%s", + keep ? "" : temp_dir(ctx), + baselen, srcname, + suffix, extension); + talloc_free(suffix); + suffix = talloc_asprintf(ctx, "-%u", ++count); + } while (!keep && lstat(f, &st) == 0); - baselen = strrchr(srcname, '.') - srcname; - f = talloc_asprintf(ctx, "%.*s%s", baselen, srcname, extension); if (tools_verbose) printf("Creating file %s\n", f); + + talloc_free(suffix); return f; } diff --git a/tools/tools.h b/tools/tools.h index a7a25c99..9bc7e1fc 100644 --- a/tools/tools.h +++ b/tools/tools.h @@ -35,7 +35,6 @@ char *run_command(const void *ctx, unsigned int *time_ms, const char *fmt, ...); char *run_with_timeout(const void *ctx, const char *cmd, bool *ok, unsigned *timeout_ms); char *temp_dir(const void *ctx); -char *temp_file(const void *ctx, const char *extension); bool move_file(const char *oldname, const char *newname); /* From compile.c. @@ -46,7 +45,8 @@ bool move_file(const char *oldname, const char *newname); /* If set, say what we're compiling to. */ extern bool compile_verbose; /* Compile multiple object files into a single. */ -char *link_objects(const void *ctx, const char *objs, char **errmsg); +char *link_objects(const void *ctx, const char *basename, bool in_pwd, + const char *objs, char **errmsg); /* Compile a single C file to an object file. Returns errmsg if fails. */ char *compile_object(const void *ctx, const char *cfile, const char *ccandir, const char *extra_cflags, @@ -57,8 +57,8 @@ char *compile_and_link(const void *ctx, const char *cfile, const char *ccandir, const char *objs, const char *extra_cflags, const char *libs, const char *outfile); -/* If keep is false, return a temporary file. Otherwise, base it on srcname */ -char *maybe_temp_file(const void *ctx, const char *extension, bool keep, +/* If in_pwd is false, return a file int temp_dir, otherwise a local file. */ +char *maybe_temp_file(const void *ctx, const char *extension, bool in_pwd, const char *srcname); /* Default wait for run_command. Should never time out. */ -- 2.39.2