Slowly removing the talloc dependency.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
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 \
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 \
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 \
#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>
#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>
#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>
#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>
#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,
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);
#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>
#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>
#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>
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",
#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>
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;
#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"
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);
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).
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;
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);
#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"
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");
#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>
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);
}
#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"
/* 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);
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);
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);
#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>
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)
#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>
{
pid_t pid;
int p[2];
- char *ret;
+ struct rbuf in;
int status, ms;
struct timespec start;
}
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");
*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. */
}
/* 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));
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;
+}
#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"
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.