From: Rusty Russell Date: Mon, 3 Dec 2012 11:36:40 +0000 (+1030) Subject: tools: use rbuf instead of grab_file. X-Git-Url: http://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=aabf300e324f7da5134d7ad45afba11225045c24 tools: use rbuf instead of grab_file. Slowly removing the talloc dependency. Signed-off-by: Rusty Russell --- diff --git a/tools/Makefile b/tools/Makefile index d3d1904e..32fc03a7 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -2,8 +2,8 @@ ALL_TOOLS = tools/configurator/configurator tools/ccan_depends tools/doc_extract LDLIBS = -lrt DEP_OBJS = ccan/err/err.o \ ccan/foreach/foreach.o \ - ccan/grab_file/grab_file.o \ ccan/noerr/noerr.o \ + ccan/rbuf/rbuf.o \ ccan/read_write_all/read_write_all.o \ ccan/str/debug.o \ ccan/str/str.o \ diff --git a/tools/ccanlint/Makefile b/tools/ccanlint/Makefile index d13cc8a5..eca8ca7c 100644 --- a/tools/ccanlint/Makefile +++ b/tools/ccanlint/Makefile @@ -6,7 +6,6 @@ CORE_OBJS := \ ccan/autodata/autodata.o \ ccan/dgraph/dgraph.o \ ccan/foreach/foreach.o \ - ccan/grab_file/grab_file.o \ ccan/hash/hash.o \ ccan/htable/htable.o \ ccan/ilog/ilog.o \ @@ -17,6 +16,7 @@ CORE_OBJS := \ ccan/opt/parse.o \ ccan/opt/usage.o \ ccan/ptr_valid/ptr_valid.o \ + ccan/rbuf/rbuf.o \ ccan/read_write_all/read_write_all.o \ ccan/str/str.o ccan/str/debug.o \ ccan/str_talloc/str_talloc.o \ diff --git a/tools/ccanlint/async.c b/tools/ccanlint/async.c index 680f947c..9ae0148d 100644 --- a/tools/ccanlint/async.c +++ b/tools/ccanlint/async.c @@ -12,7 +12,6 @@ #include #include #include -#include #include #include diff --git a/tools/ccanlint/ccanlint.c b/tools/ccanlint/ccanlint.c index d1ca7f2c..218fd0e1 100644 --- a/tools/ccanlint/ccanlint.c +++ b/tools/ccanlint/ccanlint.c @@ -31,7 +31,6 @@ #include #include #include -#include #include #include #include diff --git a/tools/ccanlint/file_analysis.c b/tools/ccanlint/file_analysis.c index 852ecbfc..f11b76b3 100644 --- a/tools/ccanlint/file_analysis.c +++ b/tools/ccanlint/file_analysis.c @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include diff --git a/tools/ccanlint/tests/avoids_cpp_reserved.c b/tools/ccanlint/tests/avoids_cpp_reserved.c index e094f270..048b6ef9 100644 --- a/tools/ccanlint/tests/avoids_cpp_reserved.c +++ b/tools/ccanlint/tests/avoids_cpp_reserved.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include diff --git a/tools/ccanlint/tests/info_documentation_exists.c b/tools/ccanlint/tests/info_documentation_exists.c index 0c0ed451..2646e98a 100644 --- a/tools/ccanlint/tests/info_documentation_exists.c +++ b/tools/ccanlint/tests/info_documentation_exists.c @@ -13,7 +13,6 @@ #include #include #include -#include static void check_info_documentation_exists(struct manifest *m, unsigned int *timeleft, @@ -52,7 +51,7 @@ static void create_info_template_doc(struct manifest *m, struct score *score) err(1, "Writing to _info.new to insert documentation"); } - oldcontents = grab_file(m, m->info_file->fullname, NULL); + oldcontents = talloc_grab_file(m, m->info_file->fullname, NULL); if (!oldcontents) { unlink_noerr("_info.new"); err(1, "Reading %s", m->info_file->fullname); diff --git a/tools/ccanlint/tests/main_header_compiles.c b/tools/ccanlint/tests/main_header_compiles.c index c6c51837..c20d73ca 100644 --- a/tools/ccanlint/tests/main_header_compiles.c +++ b/tools/ccanlint/tests/main_header_compiles.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include diff --git a/tools/ccanlint/tests/reduce_features.c b/tools/ccanlint/tests/reduce_features.c index d7f5c242..5093af9f 100644 --- a/tools/ccanlint/tests/reduce_features.c +++ b/tools/ccanlint/tests/reduce_features.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include diff --git a/tools/ccanlint/tests/tests_coverage.c b/tools/ccanlint/tests/tests_coverage.c index 3d5216ff..f5e9f043 100644 --- a/tools/ccanlint/tests/tests_coverage.c +++ b/tools/ccanlint/tests/tests_coverage.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include @@ -101,7 +100,7 @@ static void analyze_coverage(struct manifest *m, bool full_gcov, apostrophe = strchr(filename, '\''); *apostrophe = '\0'; if (lines_matter) { - file = grab_file(score, filename, NULL); + file = talloc_grab_file(score, filename, NULL); if (!file) { score->error = talloc_asprintf(score, "Reading %s", diff --git a/tools/ccanlint/tests/tests_pass_valgrind.c b/tools/ccanlint/tests/tests_pass_valgrind.c index 773a85e6..73a05410 100644 --- a/tools/ccanlint/tests/tests_pass_valgrind.c +++ b/tools/ccanlint/tests/tests_pass_valgrind.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include "tests_pass.h" #include @@ -180,7 +179,7 @@ static void do_run_tests_vg(struct manifest *m, continue; } - output = grab_file(i, i->valgrind_log, NULL); + output = talloc_grab_file(i, i->valgrind_log, NULL); /* No valgrind errors? */ if (!output || output[0] == '\0') { err = NULL; diff --git a/tools/depends.c b/tools/depends.c index a7c353c2..1a622865 100644 --- a/tools/depends.c +++ b/tools/depends.c @@ -1,8 +1,8 @@ #include #include -#include #include #include +#include #include #include #include "tools.h" @@ -17,8 +17,9 @@ static char ** PRINTF_FMT(2, 3) lines_from_cmd(const void *ctx, const char *format, ...) { va_list ap; - char *cmd, *buffer; + char *cmd; FILE *p; + struct rbuf in; va_start(ap, format); cmd = talloc_vasprintf(ctx, format, ap); @@ -28,12 +29,13 @@ lines_from_cmd(const void *ctx, const char *format, ...) if (!p) err(1, "Executing '%s'", cmd); - buffer = grab_fd(ctx, fileno(p), NULL); - if (!buffer) + /* FIXME: Use rbuf_read_str(&in, '\n') rather than strsplit! */ + rbuf_init(&in, fileno(p), talloc_array(ctx, char, 4096), 4096); + if (!rbuf_read_str(&in, 0, do_talloc_realloc) && errno) err(1, "Reading from '%s'", cmd); pclose(p); - return strsplit(ctx, buffer, "\n"); + return strsplit(ctx, in.buf, "\n"); } /* Be careful about trying to compile over running programs (parallel make). @@ -45,7 +47,8 @@ char *compile_info(const void *ctx, const char *dir) int fd; /* Copy it to a file with proper .c suffix. */ - info = grab_file(ctx, talloc_asprintf(ctx, "%s/_info", dir), &len); + info = talloc_grab_file(ctx, talloc_asprintf(ctx, "%s/_info", dir), + &len); if (!info) return NULL; @@ -126,7 +129,7 @@ static char **get_one_safe_deps(const void *ctx, bool correct_style = false; fname = talloc_asprintf(ctx, "%s/_info", dir); - raw = grab_file(fname, fname, NULL); + raw = talloc_grab_file(fname, fname, NULL); if (!raw) errx(1, "Could not open %s", fname); diff --git a/tools/doc_extract.c b/tools/doc_extract.c index aa0c5a04..85c8a6c4 100644 --- a/tools/doc_extract.c +++ b/tools/doc_extract.c @@ -2,8 +2,8 @@ #include #include #include -#include #include +#include "tools.h" #include #include #include "doc_extract.h" @@ -47,7 +47,7 @@ int main(int argc, char *argv[]) struct list_head *list; struct doc_section *d; - file = grab_file(NULL, argv[i], NULL); + file = talloc_grab_file(NULL, argv[i], NULL); if (!file) err(1, "Reading file %s", argv[i]); lines = strsplit(file, file, "\n"); diff --git a/tools/manifest.c b/tools/manifest.c index cf4c987e..b4db234f 100644 --- a/tools/manifest.c +++ b/tools/manifest.c @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -45,7 +44,8 @@ static struct htable_manifest *manifests; const char *get_ccan_file_contents(struct ccan_file *f) { if (!f->contents) { - f->contents = grab_file(f, f->fullname, &f->contents_size); + f->contents = talloc_grab_file(f, f->fullname, + &f->contents_size); if (!f->contents) err(1, "Reading file %s", f->fullname); } diff --git a/tools/namespacize.c b/tools/namespacize.c index 0cbd6734..f06d2f61 100644 --- a/tools/namespacize.c +++ b/tools/namespacize.c @@ -11,7 +11,7 @@ #include #include "ccan/str/str.h" #include "ccan/str_talloc/str_talloc.h" -#include "ccan/grab_file/grab_file.h" +#include "ccan/rbuf/rbuf.h" #include "ccan/talloc/talloc.h" #include "ccan/err/err.h" #include "tools.h" @@ -262,7 +262,7 @@ static void analyze_headers(const char *dir, struct replace **repl) /* Get hold of header, assume that's it. */ hdr = talloc_asprintf(dir, "%s/%s.h", dir, talloc_basename(dir, dir)); - contents = grab_file(dir, hdr, NULL); + contents = talloc_grab_file(dir, hdr, NULL); if (!contents) err(1, "Reading %s", hdr); @@ -338,7 +338,7 @@ static const char *rewrite_file(const char *filename, int fd; verbose("Rewriting %s\n", filename); - file = grab_file(filename, filename, NULL); + file = talloc_grab_file(filename, filename, NULL); if (!file) err(1, "Reading file %s", filename); @@ -439,7 +439,7 @@ static struct replace *read_replacement_file(const char *depdir) char *replname = talloc_asprintf(depdir, "%s/.namespacize", depdir); char *file, **line; - file = grab_file(replname, replname, NULL); + file = talloc_grab_file(replname, replname, NULL); if (!file) { if (errno != ENOENT) err(1, "Opening %s", replname); diff --git a/tools/read_config_header.c b/tools/read_config_header.c index e71db697..54f3a3c6 100644 --- a/tools/read_config_header.c +++ b/tools/read_config_header.c @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -98,7 +97,7 @@ char *read_config_header(const char *ccan_dir, unsigned int i; char *config_header; - config_header = grab_file(NULL, fname, NULL); + config_header = talloc_grab_file(NULL, fname, NULL); talloc_free(fname); if (!config_header) diff --git a/tools/tools.c b/tools/tools.c index 77349d93..f3c4fe88 100644 --- a/tools/tools.c +++ b/tools/tools.c @@ -1,7 +1,7 @@ #include -#include #include #include +#include #include #include #include @@ -71,7 +71,7 @@ char *run_with_timeout(const void *ctx, const char *cmd, { pid_t pid; int p[2]; - char *ret; + struct rbuf in; int status, ms; struct timespec start; @@ -117,7 +117,12 @@ char *run_with_timeout(const void *ctx, const char *cmd, } close(p[1]); - ret = grab_fd(ctx, p[0], NULL); + rbuf_init(&in, p[0], talloc_array(ctx, char, 4096), 4096); + if (!rbuf_read_str(&in, 0, do_talloc_realloc) && errno) { + talloc_free(in.buf); + in.buf = NULL; + } + /* This shouldn't fail... */ if (waitpid(pid, &status, 0) != pid) err(1, "Failed to wait for child"); @@ -129,14 +134,14 @@ char *run_with_timeout(const void *ctx, const char *cmd, *timeout_ms -= ms; close(p[0]); if (tools_verbose) { - printf("%s", ret); + printf("%s", in.buf); printf("Finished: %u ms, %s %u\n", ms, WIFEXITED(status) ? "exit status" : "killed by signal", WIFEXITED(status) ? WEXITSTATUS(status) : WTERMSIG(status)); } *ok = (WIFEXITED(status) && WEXITSTATUS(status) == 0); - return ret; + return in.buf; } /* Tallocs *output off ctx; return false if command fails. */ @@ -259,7 +264,7 @@ bool move_file(const char *oldname, const char *newname) } /* Try copy and delete: not atomic! */ - contents = grab_file(NULL, oldname, &size); + contents = talloc_grab_file(NULL, oldname, &size); if (!contents) { if (tools_verbose) printf("read failed: %s\n", strerror(errno)); @@ -292,3 +297,30 @@ free: talloc_free(contents); return ret; } + +void *do_talloc_realloc(void *p, size_t size) +{ + return talloc_realloc(NULL, p, char, size); +} + +void *talloc_grab_file(const void *ctx, const char *filename, size_t *size) +{ + struct rbuf rbuf; + char *buf = talloc_size(ctx, 4096); + + if (!rbuf_open(&rbuf, filename, buf, 4096)) { + talloc_free(buf); + return NULL; + } + if (!rbuf_fill_all(&rbuf, do_talloc_realloc)) { + talloc_free(rbuf.buf); + rbuf.buf = NULL; + } else { + rbuf.buf[rbuf.len] = '\0'; + if (size) + *size = rbuf.len; + } + close(rbuf.fd); + + return rbuf.buf; +} diff --git a/tools/tools.h b/tools/tools.h index 1e9e2242..1637ff51 100644 --- a/tools/tools.h +++ b/tools/tools.h @@ -1,8 +1,10 @@ #ifndef CCAN_TOOLS_H #define CCAN_TOOLS_H -#include -#include #include "config.h" +#include +#include +#include +#include #ifndef CCAN_COMPILER #define CCAN_COMPILER "cc" @@ -54,6 +56,9 @@ char *run_with_timeout(const void *ctx, const char *cmd, const char *temp_dir(const void *ctx); bool move_file(const char *oldname, const char *newname); +void *do_talloc_realloc(void *p, size_t size); +void *talloc_grab_file(const void *ctx, const char *filename, size_t *size); + /* From compile.c. * * These all compile into a temporary dir, and return the filename.