]> git.ozlabs.org Git - ccan/commitdiff
tools: use rbuf instead of grab_file.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 3 Dec 2012 11:36:40 +0000 (22:06 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 3 Dec 2012 11:36:40 +0000 (22:06 +1030)
Slowly removing the talloc dependency.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
18 files changed:
tools/Makefile
tools/ccanlint/Makefile
tools/ccanlint/async.c
tools/ccanlint/ccanlint.c
tools/ccanlint/file_analysis.c
tools/ccanlint/tests/avoids_cpp_reserved.c
tools/ccanlint/tests/info_documentation_exists.c
tools/ccanlint/tests/main_header_compiles.c
tools/ccanlint/tests/reduce_features.c
tools/ccanlint/tests/tests_coverage.c
tools/ccanlint/tests/tests_pass_valgrind.c
tools/depends.c
tools/doc_extract.c
tools/manifest.c
tools/namespacize.c
tools/read_config_header.c
tools/tools.c
tools/tools.h

index d3d1904e395caf281fd87ca1b135d69e2945733a..32fc03a768aaf433149c09f4aef20eae8d4be0f1 100644 (file)
@@ -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 \
index d13cc8a5f8b3e91d2cf8f3c1917d2097386b2aee..eca8ca7c6b7fbc421ec999718efdccf43035b1ee 100644 (file)
@@ -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 \
index 680f947c357305aa945d7db765c215323aa47636..9ae0148d83706e346d70e96aaf96ac6deea93c97 100644 (file)
@@ -12,7 +12,6 @@
 #include <assert.h>
 #include <ccan/lbalance/lbalance.h>
 #include <ccan/tlist/tlist.h>
-#include <ccan/grab_file/grab_file.h>
 #include <ccan/time/time.h>
 #include <ccan/talloc/talloc.h>
 
index d1ca7f2c2b5d4e75615bef5e47731dcbefec8749..218fd0e13c2461b2685bf9c04b56bd13aa586810 100644 (file)
@@ -31,7 +31,6 @@
 #include <ccan/talloc/talloc.h>
 #include <ccan/opt/opt.h>
 #include <ccan/foreach/foreach.h>
-#include <ccan/grab_file/grab_file.h>
 #include <ccan/cast/cast.h>
 #include <ccan/tlist/tlist.h>
 #include <ccan/strmap/strmap.h>
index 852ecbfce851c31d85078d4a21d3e7657dcc6e8d..f11b76b3b3f9c969e275de05784659caa0d4ee29 100644 (file)
@@ -6,7 +6,6 @@
 #include <ccan/talloc_link/talloc_link.h>
 #include <ccan/hash/hash.h>
 #include <ccan/htable/htable_type.h>
-#include <ccan/grab_file/grab_file.h>
 #include <ccan/noerr/noerr.h>
 #include <ccan/foreach/foreach.h>
 #include <ccan/asort/asort.h>
index e094f270b1461bdfa80512e38b62fb9295719117..048b6ef9fdc31f3e307783aaf31e18b5b67f2b38 100644 (file)
@@ -1,7 +1,6 @@
 #include <tools/ccanlint/ccanlint.h>
 #include <tools/tools.h>
 #include <ccan/talloc/talloc.h>
-#include <ccan/grab_file/grab_file.h>
 #include <ccan/str/str.h>
 #include <sys/types.h>
 #include <sys/stat.h>
index 0c0ed451494db5604e489fa036a55050360015b4..2646e98a8e41c07cdb8d48c427bf8a7082c775ad 100644 (file)
@@ -13,7 +13,6 @@
 #include <ccan/str/str.h>
 #include <ccan/talloc/talloc.h>
 #include <ccan/noerr/noerr.h>
-#include <ccan/grab_file/grab_file.h>
 
 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);
index c6c51837a8f5833f2b7ce735d5d10c147a92f67d..c20d73cae0fcde2327268a33176a9e98163cc7e5 100644 (file)
@@ -1,7 +1,6 @@
 #include <tools/ccanlint/ccanlint.h>
 #include <tools/tools.h>
 #include <ccan/talloc/talloc.h>
-#include <ccan/grab_file/grab_file.h>
 #include <ccan/str/str.h>
 #include <sys/types.h>
 #include <sys/stat.h>
index d7f5c24209f845ae792233e53dd3c9b20d7729af..5093af9f95f4b328273ce9bf831b376c41972ae1 100644 (file)
@@ -3,7 +3,6 @@
 #include <ccan/htable/htable_type.h>
 #include <ccan/foreach/foreach.h>
 #include <ccan/talloc/talloc.h>
-#include <ccan/grab_file/grab_file.h>
 #include <ccan/str/str.h>
 #include <ccan/str_talloc/str_talloc.h>
 #include <ccan/hash/hash.h>
index 3d5216ffebd9495568b9045d4d5c3e9eca42cc06..f5e9f04349077b61c1092f167c1d3034afb8b8d2 100644 (file)
@@ -2,7 +2,6 @@
 #include <tools/tools.h>
 #include <ccan/talloc/talloc.h>
 #include <ccan/str_talloc/str_talloc.h>
-#include <ccan/grab_file/grab_file.h>
 #include <ccan/str/str.h>
 #include <ccan/foreach/foreach.h>
 #include <sys/types.h>
@@ -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",
index 773a85e66a5a17aca87595bb0ca8f67441158a2e..73a05410524ce5c55ca7e49ff952e904690ac600 100644 (file)
@@ -3,7 +3,6 @@
 #include <ccan/talloc/talloc.h>
 #include <ccan/str/str.h>
 #include <ccan/foreach/foreach.h>
-#include <ccan/grab_file/grab_file.h>
 #include <ccan/str_talloc/str_talloc.h>
 #include "tests_pass.h"
 #include <sys/types.h>
@@ -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;
index a7c353c27d04571473808b67692219cc46cf2b12..1a62286572bf6344c005a9c1a870659016498262 100644 (file)
@@ -1,8 +1,8 @@
 #include <ccan/str/str.h>
 #include <ccan/talloc/talloc.h>
-#include <ccan/grab_file/grab_file.h>
 #include <ccan/str_talloc/str_talloc.h>
 #include <ccan/read_write_all/read_write_all.h>
+#include <ccan/rbuf/rbuf.h>
 #include <ccan/compiler/compiler.h>
 #include <ccan/err/err.h>
 #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);
 
index aa0c5a045c40d74ebd56feeac3b6e16a4df1261e..85c8a6c425a9d229db2db93dd6e811d57a86a467 100644 (file)
@@ -2,8 +2,8 @@
 #include <ccan/str/str.h>
 #include <ccan/str_talloc/str_talloc.h>
 #include <ccan/talloc/talloc.h>
-#include <ccan/grab_file/grab_file.h>
 #include <ccan/err/err.h>
+#include "tools.h"
 #include <string.h>
 #include <stdio.h>
 #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");
index cf4c987e956a261ea42772d16e3f1a5faab8d682..b4db234f67501bf72525dec54aa549c8218c66d2 100644 (file)
@@ -7,7 +7,6 @@
 #include <ccan/talloc_link/talloc_link.h>
 #include <ccan/hash/hash.h>
 #include <ccan/htable/htable_type.h>
-#include <ccan/grab_file/grab_file.h>
 #include <ccan/noerr/noerr.h>
 #include <ccan/foreach/foreach.h>
 #include <ccan/asort/asort.h>
@@ -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);
        }
index 0cbd67342d40205419d522f8c7adfcf55acb0b73..f06d2f617c124f8b5f2f7aa74621164ab4c5302b 100644 (file)
@@ -11,7 +11,7 @@
 #include <fcntl.h>
 #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);
index e71db697d5b160959f1e864eb5c5fee54d554e11..54f3a3c655d6a3e476a09b01219ab4a7d87f1649 100644 (file)
@@ -1,5 +1,4 @@
 #include <ccan/err/err.h>
-#include <ccan/grab_file/grab_file.h>
 #include <ccan/str/str.h>
 #include <ccan/str_talloc/str_talloc.h>
 #include <ccan/talloc/talloc.h>
@@ -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)
index 77349d93c33b463e4dc5e18617d90d5229f497b4..f3c4fe8821b8b10e351a8ecb35334a025ffd6d87 100644 (file)
@@ -1,7 +1,7 @@
 #include <ccan/talloc/talloc.h>
-#include <ccan/grab_file/grab_file.h>
 #include <ccan/err/err.h>
 #include <ccan/noerr/noerr.h>
+#include <ccan/rbuf/rbuf.h>
 #include <ccan/read_write_all/read_write_all.h>
 #include <ccan/noerr/noerr.h>
 #include <ccan/time/time.h>
@@ -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;
+}
index 1e9e22424906284458ea14f789c05ff3859916db..1637ff51227a698420d7181c17542510ae966583 100644 (file)
@@ -1,8 +1,10 @@
 #ifndef CCAN_TOOLS_H
 #define CCAN_TOOLS_H
-#include <stdbool.h>
-#include <ccan/compiler/compiler.h>
 #include "config.h"
+#include <ccan/compiler/compiler.h>
+#include <ccan/rbuf/rbuf.h>
+#include <stdlib.h>
+#include <stdbool.h>
 
 #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.