strbuf_read_file enhancement, and use it.
* make strbuf_read_file take a size hint (works like strbuf_read) * use it in a couple of places. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Pierre Habouzit committed
Sep 27, 2007 at 15:25 UTC
387e7e19d7eb5444be8da8e99ed7491989dc1cbb
5 files changed
+14
-31
builtin-apply.c
+2
-10
@@ -1425,8 +1425,6 @@ static void show_stats(struct patch *patch)
1425
1426
static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
1427
{
1428
- int fd;
1429
-
1428
switch (st->st_mode & S_IFMT) {
1429
case S_IFLNK:
1430
strbuf_grow(buf, st->st_size);
@@ -1435,14 +1433,8 @@ static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
1433
strbuf_setlen(buf, st->st_size);
1434
return 0;
1435
case S_IFREG:
1438
- fd = open(path, O_RDONLY);
1439
- if (fd < 0)
1440
- return error("unable to open %s", path);
1441
- if (strbuf_read(buf, fd, st->st_size) < 0) {
1442
- close(fd);
1443
- return -1;
1444
- }
1445
- close(fd);
1436
+ if (strbuf_read_file(buf, path, st->st_size) != st->st_size)
1437
+ return error("unable to open or read %s", path);
1438
convert_to_git(path, buf->buf, buf->len, buf);
1439
return 0;
1440
default:
builtin-blame.c
+2
-6
@@ -2002,7 +2002,6 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
2002
unsigned char head_sha1[20];
2003
struct strbuf buf;
2004
const char *ident;
2005
- int fd;
2005
time_t now;
2006
int size, len;
2007
struct cache_entry *ce;
@@ -2041,11 +2040,8 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
2040
mode = canon_mode(st.st_mode);
2041
switch (st.st_mode & S_IFMT) {
2042
case S_IFREG:
2044
- fd = open(read_from, O_RDONLY);
2045
- if (fd < 0)
2046
- die("cannot open %s", read_from);
2047
- if (strbuf_read(&buf, fd, 0) != xsize_t(st.st_size))
2048
- die("cannot read %s", read_from);
2043
+ if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
2044
+ die("cannot open or read %s", read_from);
2045
break;
2046
case S_IFLNK:
2047
if (readlink(read_from, buf.buf, buf.alloc) != fin_size)
builtin-tag.c
+7
-12
@@ -51,7 +51,7 @@ static void launch_editor(const char *path, struct strbuf *buffer)
51
if (run_command(&child))
52
die("There was a problem with the editor %s.", editor);
53
54
- if (strbuf_read_file(buffer, path) < 0)
54
+ if (strbuf_read_file(buffer, path, 0) < 0)
55
die("could not read message file '%s': %s",
56
path, strerror(errno));
57
}
@@ -356,8 +356,6 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
356
continue;
357
}
358
if (!strcmp(arg, "-F")) {
359
- int fd;
360
-
359
annotate = 1;
360
i++;
361
if (i == argc)
@@ -365,17 +363,14 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
363
if (message)
364
die("only one -F or -m option is allowed.");
365
368
- if (!strcmp(argv[i], "-"))
369
- fd = 0;
370
- else {
371
- fd = open(argv[i], O_RDONLY);
372
- if (fd < 0)
373
- die("could not open '%s': %s",
366
+ if (!strcmp(argv[i], "-")) {
367
+ if (strbuf_read(&buf, 0, 1024) < 0)
368
+ die("cannot read %s", argv[i]);
369
+ } else {
370
+ if (strbuf_read_file(&buf, argv[i], 1024) < 0)
371
+ die("could not open or read '%s': %s",
372
argv[i], strerror(errno));
373
}
376
- if (strbuf_read(&buf, fd, 1024) < 0) {
377
- die("cannot read %s", argv[i]);
378
- }
374
message = 1;
375
continue;
376
}
strbuf.c
+2
-2
@@ -186,14 +186,14 @@ int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
186
return 0;
187
}
188
189
-int strbuf_read_file(struct strbuf *sb, const char *path)
189
+int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
190
{
191
int fd, len;
192
193
fd = open(path, O_RDONLY);
194
if (fd < 0)
195
return -1;
196
- len = strbuf_read(sb, fd, 0);
196
+ len = strbuf_read(sb, fd, hint);
197
close(fd);
198
if (len < 0)
199
return -1;
strbuf.h
+1
-1
@@ -108,7 +108,7 @@ extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
108
extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
109
/* XXX: if read fails, any partial read is undone */
110
extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
111
-extern int strbuf_read_file(struct strbuf *sb, const char *path);
111
+extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
112
113
extern int strbuf_getline(struct strbuf *, FILE *, int);
114