Commit aabf300e authored by Rusty Russell's avatar Rusty Russell

tools: use rbuf instead of grab_file.

Slowly removing the talloc dependency.
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 44b37ee8
...@@ -2,8 +2,8 @@ ALL_TOOLS = tools/configurator/configurator tools/ccan_depends tools/doc_extract ...@@ -2,8 +2,8 @@ ALL_TOOLS = tools/configurator/configurator tools/ccan_depends tools/doc_extract
LDLIBS = -lrt LDLIBS = -lrt
DEP_OBJS = ccan/err/err.o \ DEP_OBJS = ccan/err/err.o \
ccan/foreach/foreach.o \ ccan/foreach/foreach.o \
ccan/grab_file/grab_file.o \
ccan/noerr/noerr.o \ ccan/noerr/noerr.o \
ccan/rbuf/rbuf.o \
ccan/read_write_all/read_write_all.o \ ccan/read_write_all/read_write_all.o \
ccan/str/debug.o \ ccan/str/debug.o \
ccan/str/str.o \ ccan/str/str.o \
......
...@@ -6,7 +6,6 @@ CORE_OBJS := \ ...@@ -6,7 +6,6 @@ CORE_OBJS := \
ccan/autodata/autodata.o \ ccan/autodata/autodata.o \
ccan/dgraph/dgraph.o \ ccan/dgraph/dgraph.o \
ccan/foreach/foreach.o \ ccan/foreach/foreach.o \
ccan/grab_file/grab_file.o \
ccan/hash/hash.o \ ccan/hash/hash.o \
ccan/htable/htable.o \ ccan/htable/htable.o \
ccan/ilog/ilog.o \ ccan/ilog/ilog.o \
...@@ -17,6 +16,7 @@ CORE_OBJS := \ ...@@ -17,6 +16,7 @@ CORE_OBJS := \
ccan/opt/parse.o \ ccan/opt/parse.o \
ccan/opt/usage.o \ ccan/opt/usage.o \
ccan/ptr_valid/ptr_valid.o \ ccan/ptr_valid/ptr_valid.o \
ccan/rbuf/rbuf.o \
ccan/read_write_all/read_write_all.o \ ccan/read_write_all/read_write_all.o \
ccan/str/str.o ccan/str/debug.o \ ccan/str/str.o ccan/str/debug.o \
ccan/str_talloc/str_talloc.o \ ccan/str_talloc/str_talloc.o \
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include <assert.h> #include <assert.h>
#include <ccan/lbalance/lbalance.h> #include <ccan/lbalance/lbalance.h>
#include <ccan/tlist/tlist.h> #include <ccan/tlist/tlist.h>
#include <ccan/grab_file/grab_file.h>
#include <ccan/time/time.h> #include <ccan/time/time.h>
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
......
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <ccan/opt/opt.h> #include <ccan/opt/opt.h>
#include <ccan/foreach/foreach.h> #include <ccan/foreach/foreach.h>
#include <ccan/grab_file/grab_file.h>
#include <ccan/cast/cast.h> #include <ccan/cast/cast.h>
#include <ccan/tlist/tlist.h> #include <ccan/tlist/tlist.h>
#include <ccan/strmap/strmap.h> #include <ccan/strmap/strmap.h>
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#include <ccan/talloc_link/talloc_link.h> #include <ccan/talloc_link/talloc_link.h>
#include <ccan/hash/hash.h> #include <ccan/hash/hash.h>
#include <ccan/htable/htable_type.h> #include <ccan/htable/htable_type.h>
#include <ccan/grab_file/grab_file.h>
#include <ccan/noerr/noerr.h> #include <ccan/noerr/noerr.h>
#include <ccan/foreach/foreach.h> #include <ccan/foreach/foreach.h>
#include <ccan/asort/asort.h> #include <ccan/asort/asort.h>
......
#include <tools/ccanlint/ccanlint.h> #include <tools/ccanlint/ccanlint.h>
#include <tools/tools.h> #include <tools/tools.h>
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <ccan/grab_file/grab_file.h>
#include <ccan/str/str.h> #include <ccan/str/str.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#include <ccan/str/str.h> #include <ccan/str/str.h>
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <ccan/noerr/noerr.h> #include <ccan/noerr/noerr.h>
#include <ccan/grab_file/grab_file.h>
static void check_info_documentation_exists(struct manifest *m, static void check_info_documentation_exists(struct manifest *m,
unsigned int *timeleft, unsigned int *timeleft,
...@@ -52,7 +51,7 @@ static void create_info_template_doc(struct manifest *m, struct score *score) ...@@ -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"); 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) { if (!oldcontents) {
unlink_noerr("_info.new"); unlink_noerr("_info.new");
err(1, "Reading %s", m->info_file->fullname); err(1, "Reading %s", m->info_file->fullname);
......
#include <tools/ccanlint/ccanlint.h> #include <tools/ccanlint/ccanlint.h>
#include <tools/tools.h> #include <tools/tools.h>
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <ccan/grab_file/grab_file.h>
#include <ccan/str/str.h> #include <ccan/str/str.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
#include <ccan/htable/htable_type.h> #include <ccan/htable/htable_type.h>
#include <ccan/foreach/foreach.h> #include <ccan/foreach/foreach.h>
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <ccan/grab_file/grab_file.h>
#include <ccan/str/str.h> #include <ccan/str/str.h>
#include <ccan/str_talloc/str_talloc.h> #include <ccan/str_talloc/str_talloc.h>
#include <ccan/hash/hash.h> #include <ccan/hash/hash.h>
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
#include <tools/tools.h> #include <tools/tools.h>
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <ccan/str_talloc/str_talloc.h> #include <ccan/str_talloc/str_talloc.h>
#include <ccan/grab_file/grab_file.h>
#include <ccan/str/str.h> #include <ccan/str/str.h>
#include <ccan/foreach/foreach.h> #include <ccan/foreach/foreach.h>
#include <sys/types.h> #include <sys/types.h>
...@@ -101,7 +100,7 @@ static void analyze_coverage(struct manifest *m, bool full_gcov, ...@@ -101,7 +100,7 @@ static void analyze_coverage(struct manifest *m, bool full_gcov,
apostrophe = strchr(filename, '\''); apostrophe = strchr(filename, '\'');
*apostrophe = '\0'; *apostrophe = '\0';
if (lines_matter) { if (lines_matter) {
file = grab_file(score, filename, NULL); file = talloc_grab_file(score, filename, NULL);
if (!file) { if (!file) {
score->error = talloc_asprintf(score, score->error = talloc_asprintf(score,
"Reading %s", "Reading %s",
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <ccan/str/str.h> #include <ccan/str/str.h>
#include <ccan/foreach/foreach.h> #include <ccan/foreach/foreach.h>
#include <ccan/grab_file/grab_file.h>
#include <ccan/str_talloc/str_talloc.h> #include <ccan/str_talloc/str_talloc.h>
#include "tests_pass.h" #include "tests_pass.h"
#include <sys/types.h> #include <sys/types.h>
...@@ -180,7 +179,7 @@ static void do_run_tests_vg(struct manifest *m, ...@@ -180,7 +179,7 @@ static void do_run_tests_vg(struct manifest *m,
continue; continue;
} }
output = grab_file(i, i->valgrind_log, NULL); output = talloc_grab_file(i, i->valgrind_log, NULL);
/* No valgrind errors? */ /* No valgrind errors? */
if (!output || output[0] == '\0') { if (!output || output[0] == '\0') {
err = NULL; err = NULL;
......
#include <ccan/str/str.h> #include <ccan/str/str.h>
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <ccan/grab_file/grab_file.h>
#include <ccan/str_talloc/str_talloc.h> #include <ccan/str_talloc/str_talloc.h>
#include <ccan/read_write_all/read_write_all.h> #include <ccan/read_write_all/read_write_all.h>
#include <ccan/rbuf/rbuf.h>
#include <ccan/compiler/compiler.h> #include <ccan/compiler/compiler.h>
#include <ccan/err/err.h> #include <ccan/err/err.h>
#include "tools.h" #include "tools.h"
...@@ -17,8 +17,9 @@ static char ** PRINTF_FMT(2, 3) ...@@ -17,8 +17,9 @@ static char ** PRINTF_FMT(2, 3)
lines_from_cmd(const void *ctx, const char *format, ...) lines_from_cmd(const void *ctx, const char *format, ...)
{ {
va_list ap; va_list ap;
char *cmd, *buffer; char *cmd;
FILE *p; FILE *p;
struct rbuf in;
va_start(ap, format); va_start(ap, format);
cmd = talloc_vasprintf(ctx, format, ap); cmd = talloc_vasprintf(ctx, format, ap);
...@@ -28,12 +29,13 @@ lines_from_cmd(const void *ctx, const char *format, ...) ...@@ -28,12 +29,13 @@ lines_from_cmd(const void *ctx, const char *format, ...)
if (!p) if (!p)
err(1, "Executing '%s'", cmd); err(1, "Executing '%s'", cmd);
buffer = grab_fd(ctx, fileno(p), NULL); /* FIXME: Use rbuf_read_str(&in, '\n') rather than strsplit! */
if (!buffer) 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); err(1, "Reading from '%s'", cmd);
pclose(p); pclose(p);
return strsplit(ctx, buffer, "\n"); return strsplit(ctx, in.buf, "\n");
} }
/* Be careful about trying to compile over running programs (parallel make). /* Be careful about trying to compile over running programs (parallel make).
...@@ -45,7 +47,8 @@ char *compile_info(const void *ctx, const char *dir) ...@@ -45,7 +47,8 @@ char *compile_info(const void *ctx, const char *dir)
int fd; int fd;
/* Copy it to a file with proper .c suffix. */ /* 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) if (!info)
return NULL; return NULL;
...@@ -126,7 +129,7 @@ static char **get_one_safe_deps(const void *ctx, ...@@ -126,7 +129,7 @@ static char **get_one_safe_deps(const void *ctx,
bool correct_style = false; bool correct_style = false;
fname = talloc_asprintf(ctx, "%s/_info", dir); fname = talloc_asprintf(ctx, "%s/_info", dir);
raw = grab_file(fname, fname, NULL); raw = talloc_grab_file(fname, fname, NULL);
if (!raw) if (!raw)
errx(1, "Could not open %s", fname); errx(1, "Could not open %s", fname);
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
#include <ccan/str/str.h> #include <ccan/str/str.h>
#include <ccan/str_talloc/str_talloc.h> #include <ccan/str_talloc/str_talloc.h>
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <ccan/grab_file/grab_file.h>
#include <ccan/err/err.h> #include <ccan/err/err.h>
#include "tools.h"
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include "doc_extract.h" #include "doc_extract.h"
...@@ -47,7 +47,7 @@ int main(int argc, char *argv[]) ...@@ -47,7 +47,7 @@ int main(int argc, char *argv[])
struct list_head *list; struct list_head *list;
struct doc_section *d; struct doc_section *d;
file = grab_file(NULL, argv[i], NULL); file = talloc_grab_file(NULL, argv[i], NULL);
if (!file) if (!file)
err(1, "Reading file %s", argv[i]); err(1, "Reading file %s", argv[i]);
lines = strsplit(file, file, "\n"); lines = strsplit(file, file, "\n");
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include <ccan/talloc_link/talloc_link.h> #include <ccan/talloc_link/talloc_link.h>
#include <ccan/hash/hash.h> #include <ccan/hash/hash.h>
#include <ccan/htable/htable_type.h> #include <ccan/htable/htable_type.h>
#include <ccan/grab_file/grab_file.h>
#include <ccan/noerr/noerr.h> #include <ccan/noerr/noerr.h>
#include <ccan/foreach/foreach.h> #include <ccan/foreach/foreach.h>
#include <ccan/asort/asort.h> #include <ccan/asort/asort.h>
...@@ -45,7 +44,8 @@ static struct htable_manifest *manifests; ...@@ -45,7 +44,8 @@ static struct htable_manifest *manifests;
const char *get_ccan_file_contents(struct ccan_file *f) const char *get_ccan_file_contents(struct ccan_file *f)
{ {
if (!f->contents) { 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) if (!f->contents)
err(1, "Reading file %s", f->fullname); err(1, "Reading file %s", f->fullname);
} }
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#include <fcntl.h> #include <fcntl.h>
#include "ccan/str/str.h" #include "ccan/str/str.h"
#include "ccan/str_talloc/str_talloc.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/talloc/talloc.h"
#include "ccan/err/err.h" #include "ccan/err/err.h"
#include "tools.h" #include "tools.h"
...@@ -262,7 +262,7 @@ static void analyze_headers(const char *dir, struct replace **repl) ...@@ -262,7 +262,7 @@ static void analyze_headers(const char *dir, struct replace **repl)
/* Get hold of header, assume that's it. */ /* Get hold of header, assume that's it. */
hdr = talloc_asprintf(dir, "%s/%s.h", dir, talloc_basename(dir, dir)); 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) if (!contents)
err(1, "Reading %s", hdr); err(1, "Reading %s", hdr);
...@@ -338,7 +338,7 @@ static const char *rewrite_file(const char *filename, ...@@ -338,7 +338,7 @@ static const char *rewrite_file(const char *filename,
int fd; int fd;
verbose("Rewriting %s\n", filename); verbose("Rewriting %s\n", filename);
file = grab_file(filename, filename, NULL); file = talloc_grab_file(filename, filename, NULL);
if (!file) if (!file)
err(1, "Reading file %s", filename); err(1, "Reading file %s", filename);
...@@ -439,7 +439,7 @@ static struct replace *read_replacement_file(const char *depdir) ...@@ -439,7 +439,7 @@ static struct replace *read_replacement_file(const char *depdir)
char *replname = talloc_asprintf(depdir, "%s/.namespacize", depdir); char *replname = talloc_asprintf(depdir, "%s/.namespacize", depdir);
char *file, **line; char *file, **line;
file = grab_file(replname, replname, NULL); file = talloc_grab_file(replname, replname, NULL);
if (!file) { if (!file) {
if (errno != ENOENT) if (errno != ENOENT)
err(1, "Opening %s", replname); err(1, "Opening %s", replname);
......
#include <ccan/err/err.h> #include <ccan/err/err.h>
#include <ccan/grab_file/grab_file.h>
#include <ccan/str/str.h> #include <ccan/str/str.h>
#include <ccan/str_talloc/str_talloc.h> #include <ccan/str_talloc/str_talloc.h>
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
...@@ -98,7 +97,7 @@ char *read_config_header(const char *ccan_dir, ...@@ -98,7 +97,7 @@ char *read_config_header(const char *ccan_dir,
unsigned int i; unsigned int i;
char *config_header; char *config_header;
config_header = grab_file(NULL, fname, NULL); config_header = talloc_grab_file(NULL, fname, NULL);
talloc_free(fname); talloc_free(fname);
if (!config_header) if (!config_header)
......
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <ccan/grab_file/grab_file.h>
#include <ccan/err/err.h> #include <ccan/err/err.h>
#include <ccan/noerr/noerr.h> #include <ccan/noerr/noerr.h>
#include <ccan/rbuf/rbuf.h>
#include <ccan/read_write_all/read_write_all.h> #include <ccan/read_write_all/read_write_all.h>
#include <ccan/noerr/noerr.h> #include <ccan/noerr/noerr.h>
#include <ccan/time/time.h> #include <ccan/time/time.h>
...@@ -71,7 +71,7 @@ char *run_with_timeout(const void *ctx, const char *cmd, ...@@ -71,7 +71,7 @@ char *run_with_timeout(const void *ctx, const char *cmd,
{ {
pid_t pid; pid_t pid;
int p[2]; int p[2];
char *ret; struct rbuf in;
int status, ms; int status, ms;
struct timespec start; struct timespec start;
...@@ -117,7 +117,12 @@ char *run_with_timeout(const void *ctx, const char *cmd, ...@@ -117,7 +117,12 @@ char *run_with_timeout(const void *ctx, const char *cmd,
} }
close(p[1]); 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... */ /* This shouldn't fail... */
if (waitpid(pid, &status, 0) != pid) if (waitpid(pid, &status, 0) != pid)
err(1, "Failed to wait for child"); err(1, "Failed to wait for child");
...@@ -129,14 +134,14 @@ char *run_with_timeout(const void *ctx, const char *cmd, ...@@ -129,14 +134,14 @@ char *run_with_timeout(const void *ctx, const char *cmd,
*timeout_ms -= ms; *timeout_ms -= ms;
close(p[0]); close(p[0]);
if (tools_verbose) { if (tools_verbose) {
printf("%s", ret); printf("%s", in.buf);
printf("Finished: %u ms, %s %u\n", ms, printf("Finished: %u ms, %s %u\n", ms,
WIFEXITED(status) ? "exit status" : "killed by signal", WIFEXITED(status) ? "exit status" : "killed by signal",
WIFEXITED(status) ? WEXITSTATUS(status) WIFEXITED(status) ? WEXITSTATUS(status)
: WTERMSIG(status)); : WTERMSIG(status));
} }
*ok = (WIFEXITED(status) && WEXITSTATUS(status) == 0); *ok = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
return ret; return in.buf;
} }
/* Tallocs *output off ctx; return false if command fails. */ /* Tallocs *output off ctx; return false if command fails. */
...@@ -259,7 +264,7 @@ bool move_file(const char *oldname, const char *newname) ...@@ -259,7 +264,7 @@ bool move_file(const char *oldname, const char *newname)
} }
/* Try copy and delete: not atomic! */ /* Try copy and delete: not atomic! */
contents = grab_file(NULL, oldname, &size); contents = talloc_grab_file(NULL, oldname, &size);
if (!contents) { if (!contents) {
if (tools_verbose) if (tools_verbose)
printf("read failed: %s\n", strerror(errno)); printf("read failed: %s\n", strerror(errno));
...@@ -292,3 +297,30 @@ free: ...@@ -292,3 +297,30 @@ free:
talloc_free(contents); talloc_free(contents);
return ret; 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;
}
#ifndef CCAN_TOOLS_H #ifndef CCAN_TOOLS_H
#define CCAN_TOOLS_H #define CCAN_TOOLS_H
#include <stdbool.h>
#include <ccan/compiler/compiler.h>
#include "config.h" #include "config.h"
#include <ccan/compiler/compiler.h>
#include <ccan/rbuf/rbuf.h>
#include <stdlib.h>
#include <stdbool.h>
#ifndef CCAN_COMPILER #ifndef CCAN_COMPILER
#define CCAN_COMPILER "cc" #define CCAN_COMPILER "cc"
...@@ -54,6 +56,9 @@ char *run_with_timeout(const void *ctx, const char *cmd, ...@@ -54,6 +56,9 @@ char *run_with_timeout(const void *ctx, const char *cmd,
const char *temp_dir(const void *ctx); const char *temp_dir(const void *ctx);
bool move_file(const char *oldname, const char *newname); 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. /* From compile.c.
* *
* These all compile into a temporary dir, and return the filename. * These all compile into a temporary dir, and return the filename.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment