Browse Source

tools: use tal/grab_file

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Rusty Russell 11 years ago
parent
commit
eca1044689

+ 1 - 0
tools/Makefile

@@ -10,6 +10,7 @@ DEP_OBJS = ccan/err/err.o \
 	ccan/str/str.o \
 	ccan/str/str.o \
 	ccan/take/take.o \
 	ccan/take/take.o \
 	ccan/tal/tal.o \
 	ccan/tal/tal.o \
+	ccan/tal/grab_file/grab_file.o \
 	ccan/tal/link/link.o \
 	ccan/tal/link/link.o \
 	ccan/tal/path/path.o \
 	ccan/tal/path/path.o \
 	ccan/tal/str/str.o \
 	ccan/tal/str/str.o \

+ 1 - 0
tools/ccanlint/Makefile

@@ -23,6 +23,7 @@ CORE_OBJS := \
 	ccan/strmap/strmap.o \
 	ccan/strmap/strmap.o \
 	ccan/take/take.o \
 	ccan/take/take.o \
 	ccan/tal/tal.o \
 	ccan/tal/tal.o \
+	ccan/tal/grab_file/grab_file.o \
 	ccan/tal/link/link.o \
 	ccan/tal/link/link.o \
 	ccan/tal/path/path.o \
 	ccan/tal/path/path.o \
 	ccan/tal/str/str.o \
 	ccan/tal/str/str.o \

+ 2 - 1
tools/ccanlint/tests/info_documentation_exists.c

@@ -12,6 +12,7 @@
 #include <err.h>
 #include <err.h>
 #include <ccan/str/str.h>
 #include <ccan/str/str.h>
 #include <ccan/noerr/noerr.h>
 #include <ccan/noerr/noerr.h>
+#include <ccan/tal/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,
@@ -50,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 = tal_grab_file(m, m->info_file->fullname, NULL);
+	oldcontents = grab_file(m, m->info_file->fullname);
 	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);

+ 2 - 1
tools/ccanlint/tests/tests_coverage.c

@@ -2,6 +2,7 @@
 #include <tools/tools.h>
 #include <tools/tools.h>
 #include <ccan/str/str.h>
 #include <ccan/str/str.h>
 #include <ccan/tal/path/path.h>
 #include <ccan/tal/path/path.h>
+#include <ccan/tal/grab_file/grab_file.h>
 #include <ccan/foreach/foreach.h>
 #include <ccan/foreach/foreach.h>
 #include <sys/types.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/stat.h>
@@ -99,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 = tal_grab_file(score, filename, NULL);
+				file = grab_file(score, filename);
 				if (!file) {
 				if (!file) {
 					score->error = tal_fmt(score,
 					score->error = tal_fmt(score,
 							       "Reading %s",
 							       "Reading %s",

+ 2 - 1
tools/ccanlint/tests/tests_pass_valgrind.c

@@ -3,6 +3,7 @@
 #include <ccan/str/str.h>
 #include <ccan/str/str.h>
 #include <ccan/take/take.h>
 #include <ccan/take/take.h>
 #include <ccan/foreach/foreach.h>
 #include <ccan/foreach/foreach.h>
+#include <ccan/tal/grab_file/grab_file.h>
 #include "tests_pass.h"
 #include "tests_pass.h"
 #include <sys/types.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/stat.h>
@@ -180,7 +181,7 @@ static void do_run_tests_vg(struct manifest *m,
 				continue;
 				continue;
 			}
 			}
 
 
-			output = tal_grab_file(i, i->valgrind_log, NULL);
+			output = grab_file(i, i->valgrind_log);
 			/* No valgrind errors? */
 			/* No valgrind errors? */
 			if (!output || output[0] == '\0') {
 			if (!output || output[0] == '\0') {
 				err = NULL;
 				err = NULL;

+ 4 - 4
tools/depends.c

@@ -2,6 +2,7 @@
 #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/rbuf/rbuf.h>
 #include <ccan/tal/path/path.h>
 #include <ccan/tal/path/path.h>
+#include <ccan/tal/grab_file/grab_file.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"
@@ -42,11 +43,10 @@ lines_from_cmd(const void *ctx, const char *format, ...)
 char *compile_info(const void *ctx, const char *dir)
 char *compile_info(const void *ctx, const char *dir)
 {
 {
 	char *info_c_file, *info, *compiled, *output;
 	char *info_c_file, *info, *compiled, *output;
-	size_t len;
 	int fd;
 	int fd;
 
 
 	/* Copy it to a file with proper .c suffix. */
 	/* Copy it to a file with proper .c suffix. */
-	info = tal_grab_file(ctx, tal_fmt(ctx, "%s/_info", dir), &len);
+	info = grab_file(ctx, tal_fmt(ctx, "%s/_info", dir));
 	if (!info)
 	if (!info)
 		return NULL;
 		return NULL;
 
 
@@ -54,7 +54,7 @@ char *compile_info(const void *ctx, const char *dir)
 	fd = open(info_c_file, O_WRONLY|O_CREAT|O_EXCL, 0600);
 	fd = open(info_c_file, O_WRONLY|O_CREAT|O_EXCL, 0600);
 	if (fd < 0)
 	if (fd < 0)
 		return NULL;
 		return NULL;
-	if (!write_all(fd, info, len))
+	if (!write_all(fd, info, tal_count(info)-1))
 		return NULL;
 		return NULL;
 
 
 	if (close(fd) != 0)
 	if (close(fd) != 0)
@@ -126,7 +126,7 @@ static char **get_one_safe_deps(const void *ctx,
 	bool correct_style = false;
 	bool correct_style = false;
 
 
 	fname = path_join(ctx, dir, "_info");
 	fname = path_join(ctx, dir, "_info");
-	raw = tal_grab_file(fname, fname, NULL);
+	raw = grab_file(fname, fname);
 	if (!raw)
 	if (!raw)
 		errx(1, "Could not open %s", fname);
 		errx(1, "Could not open %s", fname);
 
 

+ 2 - 1
tools/doc_extract.c

@@ -1,6 +1,7 @@
 /* This merely extracts, doesn't do XML or anything. */
 /* This merely extracts, doesn't do XML or anything. */
 #include <ccan/str/str.h>
 #include <ccan/str/str.h>
 #include <ccan/err/err.h>
 #include <ccan/err/err.h>
+#include <ccan/tal/grab_file/grab_file.h>
 #include "tools.h"
 #include "tools.h"
 #include <string.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdio.h>
@@ -45,7 +46,7 @@ int main(int argc, char *argv[])
 		struct list_head *list;
 		struct list_head *list;
 		struct doc_section *d;
 		struct doc_section *d;
 
 
-		file = tal_grab_file(NULL, argv[i], NULL);
+		file = grab_file(NULL, argv[i]);
 		if (!file)
 		if (!file)
 			err(1, "Reading file %s", argv[i]);
 			err(1, "Reading file %s", argv[i]);
 		lines = tal_strsplit(file, file, "\n", STR_EMPTY_OK);
 		lines = tal_strsplit(file, file, "\n", STR_EMPTY_OK);

+ 3 - 2
tools/manifest.c

@@ -3,6 +3,7 @@
 #include "tools.h"
 #include "tools.h"
 #include <ccan/str/str.h>
 #include <ccan/str/str.h>
 #include <ccan/tal/link/link.h>
 #include <ccan/tal/link/link.h>
+#include <ccan/tal/grab_file/grab_file.h>
 #include <ccan/tal/path/path.h>
 #include <ccan/tal/path/path.h>
 #include <ccan/hash/hash.h>
 #include <ccan/hash/hash.h>
 #include <ccan/htable/htable_type.h>
 #include <ccan/htable/htable_type.h>
@@ -43,10 +44,10 @@ 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 = tal_grab_file(f, f->fullname,
-					       &f->contents_size);
+		f->contents = grab_file(f, f->fullname);
 		if (!f->contents)
 		if (!f->contents)
 			err(1, "Reading file %s", f->fullname);
 			err(1, "Reading file %s", f->fullname);
+		f->contents_size = tal_count(f->contents) - 1;
 	}
 	}
 	return f->contents;
 	return f->contents;
 }
 }

+ 4 - 3
tools/namespacize.c

@@ -13,6 +13,7 @@
 #include "ccan/take/take.h"
 #include "ccan/take/take.h"
 #include "ccan/rbuf/rbuf.h"
 #include "ccan/rbuf/rbuf.h"
 #include "ccan/tal/path/path.h"
 #include "ccan/tal/path/path.h"
+#include "ccan/tal/grab_file/grab_file.h"
 #include "ccan/err/err.h"
 #include "ccan/err/err.h"
 #include "tools.h"
 #include "tools.h"
 
 
@@ -259,7 +260,7 @@ static void analyze_headers(const char *dir, struct replace **repl)
 	hdr = tal_fmt(dir, "%s.h",
 	hdr = tal_fmt(dir, "%s.h",
 		      path_join(NULL, dir, take(path_basename(NULL, dir))));
 		      path_join(NULL, dir, take(path_basename(NULL, dir))));
 
 
-	contents = tal_grab_file(dir, hdr, NULL);
+	contents = grab_file(dir, hdr);
 	if (!contents)
 	if (!contents)
 		err(1, "Reading %s", hdr);
 		err(1, "Reading %s", hdr);
 
 
@@ -334,7 +335,7 @@ static const char *rewrite_file(const char *filename,
 	int fd;
 	int fd;
 
 
 	verbose("Rewriting %s\n", filename);
 	verbose("Rewriting %s\n", filename);
-	file = tal_grab_file(filename, filename, NULL);
+	file = grab_file(filename, filename);
 	if (!file)
 	if (!file)
 		err(1, "Reading file %s", filename);
 		err(1, "Reading file %s", filename);
 
 
@@ -431,7 +432,7 @@ static struct replace *read_replacement_file(const char *depdir)
 	char *replname = path_join(depdir, depdir, ".namespacize");
 	char *replname = path_join(depdir, depdir, ".namespacize");
 	char *file, **line;
 	char *file, **line;
 
 
-	file = tal_grab_file(replname, replname, NULL);
+	file = grab_file(replname, replname);
 	if (!file) {
 	if (!file) {
 		if (errno != ENOENT)
 		if (errno != ENOENT)
 			err(1, "Opening %s", replname);
 			err(1, "Opening %s", replname);

+ 2 - 1
tools/read_config_header.c

@@ -1,6 +1,7 @@
 #include <ccan/err/err.h>
 #include <ccan/err/err.h>
 #include <ccan/str/str.h>
 #include <ccan/str/str.h>
 #include <ccan/tal/path/path.h>
 #include <ccan/tal/path/path.h>
+#include <ccan/tal/grab_file/grab_file.h>
 #include "read_config_header.h"
 #include "read_config_header.h"
 #include "tools.h"
 #include "tools.h"
 #include <string.h>
 #include <string.h>
@@ -94,7 +95,7 @@ char *read_config_header(const char *ccan_dir, bool verbose)
 	unsigned int i;
 	unsigned int i;
 	char *config_header;
 	char *config_header;
 
 
-	config_header = tal_grab_file(NULL, fname, NULL);
+	config_header = grab_file(NULL, fname);
 	tal_free(fname);
 	tal_free(fname);
 
 
 	if (!config_header)
 	if (!config_header)

+ 3 - 23
tools/tools.c

@@ -6,6 +6,7 @@
 #include <ccan/noerr/noerr.h>
 #include <ccan/noerr/noerr.h>
 #include <ccan/time/time.h>
 #include <ccan/time/time.h>
 #include <ccan/tal/path/path.h>
 #include <ccan/tal/path/path.h>
+#include <ccan/tal/grab_file/grab_file.h>
 #include <sys/stat.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/types.h>
 #include <sys/time.h>
 #include <sys/time.h>
@@ -218,7 +219,6 @@ char *temp_file(const void *ctx, const char *extension, const char *srcname)
 bool move_file(const char *oldname, const char *newname)
 bool move_file(const char *oldname, const char *newname)
 {
 {
 	char *contents;
 	char *contents;
-	size_t size;
 	int fd;
 	int fd;
 	bool ret;
 	bool ret;
 
 
@@ -233,7 +233,7 @@ bool move_file(const char *oldname, const char *newname)
 	}
 	}
 
 
 	/* Try copy and delete: not atomic! */
 	/* Try copy and delete: not atomic! */
-	contents = tal_grab_file(NULL, oldname, &size);
+	contents = grab_file(NULL, oldname);
 	if (!contents) {
 	if (!contents) {
 		if (tools_verbose)
 		if (tools_verbose)
 			printf("read failed: %s\n", strerror(errno));
 			printf("read failed: %s\n", strerror(errno));
@@ -248,7 +248,7 @@ bool move_file(const char *oldname, const char *newname)
 		goto free;
 		goto free;
 	}
 	}
 
 
-	ret = write_all(fd, contents, size);
+	ret = write_all(fd, contents, tal_count(contents)-1);
 	if (close(fd) != 0)
 	if (close(fd) != 0)
 		ret = false;
 		ret = false;
 
 
@@ -272,23 +272,3 @@ void *do_tal_realloc(void *p, size_t size)
 	tal_resize((char **)&p, size);
 	tal_resize((char **)&p, size);
 	return p;
 	return p;
 }
 }
-
-void *tal_grab_file(const void *ctx, const char *filename, size_t *size)
-{
-	struct rbuf rbuf;
-	char *buf = tal_arr(ctx, char, 0);
-
-	if (!rbuf_open(&rbuf, filename, buf, 0))
-		return tal_free(buf);
-
-	if (!rbuf_fill_all(&rbuf, do_tal_realloc) && errno)
-		rbuf.buf = tal_free(rbuf.buf);
-	else {
-		rbuf.buf[rbuf.len] = '\0';
-		if (size)
-			*size = rbuf.len;
-	}
-	close(rbuf.fd);
-
-	return rbuf.buf;
-}

+ 0 - 1
tools/tools.h

@@ -57,7 +57,6 @@ void keep_temp_dir(void);
 bool move_file(const char *oldname, const char *newname);
 bool move_file(const char *oldname, const char *newname);
 
 
 void *do_tal_realloc(void *p, size_t size);
 void *do_tal_realloc(void *p, size_t size);
-void *tal_grab_file(const void *ctx, const char *filename, size_t *size);
 
 
 /* Freed on exit: a good parent for auto cleanup. */
 /* Freed on exit: a good parent for auto cleanup. */
 tal_t *autofree(void);
 tal_t *autofree(void);