Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit eca1044

Browse files
committedJun 10, 2014
tools: use tal/grab_file
Signed-off-by: Rusty Russell <[email protected]>
1 parent f3eecc2 commit eca1044

File tree

12 files changed

+26
-38
lines changed

12 files changed

+26
-38
lines changed
 

‎tools/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ DEP_OBJS = ccan/err/err.o \
1010
ccan/str/str.o \
1111
ccan/take/take.o \
1212
ccan/tal/tal.o \
13+
ccan/tal/grab_file/grab_file.o \
1314
ccan/tal/link/link.o \
1415
ccan/tal/path/path.o \
1516
ccan/tal/str/str.o \

‎tools/ccanlint/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ CORE_OBJS := \
2323
ccan/strmap/strmap.o \
2424
ccan/take/take.o \
2525
ccan/tal/tal.o \
26+
ccan/tal/grab_file/grab_file.o \
2627
ccan/tal/link/link.o \
2728
ccan/tal/path/path.o \
2829
ccan/tal/str/str.o \

‎tools/ccanlint/tests/info_documentation_exists.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <err.h>
1313
#include <ccan/str/str.h>
1414
#include <ccan/noerr/noerr.h>
15+
#include <ccan/tal/grab_file/grab_file.h>
1516

1617
static void check_info_documentation_exists(struct manifest *m,
1718
unsigned int *timeleft,
@@ -50,7 +51,7 @@ static void create_info_template_doc(struct manifest *m, struct score *score)
5051
err(1, "Writing to _info.new to insert documentation");
5152
}
5253

53-
oldcontents = tal_grab_file(m, m->info_file->fullname, NULL);
54+
oldcontents = grab_file(m, m->info_file->fullname);
5455
if (!oldcontents) {
5556
unlink_noerr("_info.new");
5657
err(1, "Reading %s", m->info_file->fullname);

‎tools/ccanlint/tests/tests_coverage.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <tools/tools.h>
33
#include <ccan/str/str.h>
44
#include <ccan/tal/path/path.h>
5+
#include <ccan/tal/grab_file/grab_file.h>
56
#include <ccan/foreach/foreach.h>
67
#include <sys/types.h>
78
#include <sys/stat.h>
@@ -99,7 +100,7 @@ static void analyze_coverage(struct manifest *m, bool full_gcov,
99100
apostrophe = strchr(filename, '\'');
100101
*apostrophe = '\0';
101102
if (lines_matter) {
102-
file = tal_grab_file(score, filename, NULL);
103+
file = grab_file(score, filename);
103104
if (!file) {
104105
score->error = tal_fmt(score,
105106
"Reading %s",

‎tools/ccanlint/tests/tests_pass_valgrind.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <ccan/str/str.h>
44
#include <ccan/take/take.h>
55
#include <ccan/foreach/foreach.h>
6+
#include <ccan/tal/grab_file/grab_file.h>
67
#include "tests_pass.h"
78
#include <sys/types.h>
89
#include <sys/stat.h>
@@ -180,7 +181,7 @@ static void do_run_tests_vg(struct manifest *m,
180181
continue;
181182
}
182183

183-
output = tal_grab_file(i, i->valgrind_log, NULL);
184+
output = grab_file(i, i->valgrind_log);
184185
/* No valgrind errors? */
185186
if (!output || output[0] == '\0') {
186187
err = NULL;

‎tools/depends.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <ccan/read_write_all/read_write_all.h>
33
#include <ccan/rbuf/rbuf.h>
44
#include <ccan/tal/path/path.h>
5+
#include <ccan/tal/grab_file/grab_file.h>
56
#include <ccan/compiler/compiler.h>
67
#include <ccan/err/err.h>
78
#include "tools.h"
@@ -42,19 +43,18 @@ lines_from_cmd(const void *ctx, const char *format, ...)
4243
char *compile_info(const void *ctx, const char *dir)
4344
{
4445
char *info_c_file, *info, *compiled, *output;
45-
size_t len;
4646
int fd;
4747

4848
/* Copy it to a file with proper .c suffix. */
49-
info = tal_grab_file(ctx, tal_fmt(ctx, "%s/_info", dir), &len);
49+
info = grab_file(ctx, tal_fmt(ctx, "%s/_info", dir));
5050
if (!info)
5151
return NULL;
5252

5353
info_c_file = temp_file(ctx, ".c", "_info");
5454
fd = open(info_c_file, O_WRONLY|O_CREAT|O_EXCL, 0600);
5555
if (fd < 0)
5656
return NULL;
57-
if (!write_all(fd, info, len))
57+
if (!write_all(fd, info, tal_count(info)-1))
5858
return NULL;
5959

6060
if (close(fd) != 0)
@@ -126,7 +126,7 @@ static char **get_one_safe_deps(const void *ctx,
126126
bool correct_style = false;
127127

128128
fname = path_join(ctx, dir, "_info");
129-
raw = tal_grab_file(fname, fname, NULL);
129+
raw = grab_file(fname, fname);
130130
if (!raw)
131131
errx(1, "Could not open %s", fname);
132132

‎tools/doc_extract.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* This merely extracts, doesn't do XML or anything. */
22
#include <ccan/str/str.h>
33
#include <ccan/err/err.h>
4+
#include <ccan/tal/grab_file/grab_file.h>
45
#include "tools.h"
56
#include <string.h>
67
#include <stdio.h>
@@ -45,7 +46,7 @@ int main(int argc, char *argv[])
4546
struct list_head *list;
4647
struct doc_section *d;
4748

48-
file = tal_grab_file(NULL, argv[i], NULL);
49+
file = grab_file(NULL, argv[i]);
4950
if (!file)
5051
err(1, "Reading file %s", argv[i]);
5152
lines = tal_strsplit(file, file, "\n", STR_EMPTY_OK);

‎tools/manifest.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "tools.h"
44
#include <ccan/str/str.h>
55
#include <ccan/tal/link/link.h>
6+
#include <ccan/tal/grab_file/grab_file.h>
67
#include <ccan/tal/path/path.h>
78
#include <ccan/hash/hash.h>
89
#include <ccan/htable/htable_type.h>
@@ -43,10 +44,10 @@ static struct htable_manifest *manifests;
4344
const char *get_ccan_file_contents(struct ccan_file *f)
4445
{
4546
if (!f->contents) {
46-
f->contents = tal_grab_file(f, f->fullname,
47-
&f->contents_size);
47+
f->contents = grab_file(f, f->fullname);
4848
if (!f->contents)
4949
err(1, "Reading file %s", f->fullname);
50+
f->contents_size = tal_count(f->contents) - 1;
5051
}
5152
return f->contents;
5253
}

‎tools/namespacize.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "ccan/take/take.h"
1414
#include "ccan/rbuf/rbuf.h"
1515
#include "ccan/tal/path/path.h"
16+
#include "ccan/tal/grab_file/grab_file.h"
1617
#include "ccan/err/err.h"
1718
#include "tools.h"
1819

@@ -259,7 +260,7 @@ static void analyze_headers(const char *dir, struct replace **repl)
259260
hdr = tal_fmt(dir, "%s.h",
260261
path_join(NULL, dir, take(path_basename(NULL, dir))));
261262

262-
contents = tal_grab_file(dir, hdr, NULL);
263+
contents = grab_file(dir, hdr);
263264
if (!contents)
264265
err(1, "Reading %s", hdr);
265266

@@ -334,7 +335,7 @@ static const char *rewrite_file(const char *filename,
334335
int fd;
335336

336337
verbose("Rewriting %s\n", filename);
337-
file = tal_grab_file(filename, filename, NULL);
338+
file = grab_file(filename, filename);
338339
if (!file)
339340
err(1, "Reading file %s", filename);
340341

@@ -431,7 +432,7 @@ static struct replace *read_replacement_file(const char *depdir)
431432
char *replname = path_join(depdir, depdir, ".namespacize");
432433
char *file, **line;
433434

434-
file = tal_grab_file(replname, replname, NULL);
435+
file = grab_file(replname, replname);
435436
if (!file) {
436437
if (errno != ENOENT)
437438
err(1, "Opening %s", replname);

‎tools/read_config_header.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <ccan/err/err.h>
22
#include <ccan/str/str.h>
33
#include <ccan/tal/path/path.h>
4+
#include <ccan/tal/grab_file/grab_file.h>
45
#include "read_config_header.h"
56
#include "tools.h"
67
#include <string.h>
@@ -94,7 +95,7 @@ char *read_config_header(const char *ccan_dir, bool verbose)
9495
unsigned int i;
9596
char *config_header;
9697

97-
config_header = tal_grab_file(NULL, fname, NULL);
98+
config_header = grab_file(NULL, fname);
9899
tal_free(fname);
99100

100101
if (!config_header)

‎tools/tools.c

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <ccan/noerr/noerr.h>
77
#include <ccan/time/time.h>
88
#include <ccan/tal/path/path.h>
9+
#include <ccan/tal/grab_file/grab_file.h>
910
#include <sys/stat.h>
1011
#include <sys/types.h>
1112
#include <sys/time.h>
@@ -218,7 +219,6 @@ char *temp_file(const void *ctx, const char *extension, const char *srcname)
218219
bool move_file(const char *oldname, const char *newname)
219220
{
220221
char *contents;
221-
size_t size;
222222
int fd;
223223
bool ret;
224224

@@ -233,7 +233,7 @@ bool move_file(const char *oldname, const char *newname)
233233
}
234234

235235
/* Try copy and delete: not atomic! */
236-
contents = tal_grab_file(NULL, oldname, &size);
236+
contents = grab_file(NULL, oldname);
237237
if (!contents) {
238238
if (tools_verbose)
239239
printf("read failed: %s\n", strerror(errno));
@@ -248,7 +248,7 @@ bool move_file(const char *oldname, const char *newname)
248248
goto free;
249249
}
250250

251-
ret = write_all(fd, contents, size);
251+
ret = write_all(fd, contents, tal_count(contents)-1);
252252
if (close(fd) != 0)
253253
ret = false;
254254

@@ -272,23 +272,3 @@ void *do_tal_realloc(void *p, size_t size)
272272
tal_resize((char **)&p, size);
273273
return p;
274274
}
275-
276-
void *tal_grab_file(const void *ctx, const char *filename, size_t *size)
277-
{
278-
struct rbuf rbuf;
279-
char *buf = tal_arr(ctx, char, 0);
280-
281-
if (!rbuf_open(&rbuf, filename, buf, 0))
282-
return tal_free(buf);
283-
284-
if (!rbuf_fill_all(&rbuf, do_tal_realloc) && errno)
285-
rbuf.buf = tal_free(rbuf.buf);
286-
else {
287-
rbuf.buf[rbuf.len] = '\0';
288-
if (size)
289-
*size = rbuf.len;
290-
}
291-
close(rbuf.fd);
292-
293-
return rbuf.buf;
294-
}

‎tools/tools.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ void keep_temp_dir(void);
5757
bool move_file(const char *oldname, const char *newname);
5858

5959
void *do_tal_realloc(void *p, size_t size);
60-
void *tal_grab_file(const void *ctx, const char *filename, size_t *size);
6160

6261
/* Freed on exit: a good parent for auto cleanup. */
6362
tal_t *autofree(void);

0 commit comments

Comments
 (0)
Please sign in to comment.