@matheusbsbs / matheus-bagliano / commits / 7230e6d042
Fork 0

Add write_or_die(), a helper function

The little helper write_or_die() won't come back with bad news about full disks or broken pipes. It either succeeds or terminates the program, making additional error handling unnecessary. This patch adds the new function and uses it to replace two similar ones (the one in tar-tree originally has been copied from cat-file btw.). I chose to add the fd parameter which both lacked to make write_or_die() just as flexible as write() and thus suitable for lib-ification. There is a regression: error messages emitted by this function don't show the program name, while the replaced two functions did. That's acceptable, I think; a lot of other functions do the same. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <junkio@cox.net>

Rene Scharfe committed Aug 21, 2006 at 20:43 UTC 7230e6d042ae385377f09c4d226d9b1aa7a2c13b
5 files changed +32 -46
Makefile
+1
@@ -248,6 +248,7 @@ LIB_OBJS = \
248 server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
249 tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
250 fetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \
251 + write_or_die.o \
252 alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS)
253
254 BUILTIN_OBJS = \
builtin-cat-file.c
+6 -23
@@ -9,23 +9,6 @@
9 #include "tree.h"
10 #include "builtin.h"
11
12 -static void flush_buffer(const char *buf, unsigned long size)
13 -{
14 - while (size > 0) {
15 - long ret = xwrite(1, buf, size);
16 - if (ret < 0) {
17 - /* Ignore epipe */
18 - if (errno == EPIPE)
19 - break;
20 - die("git-cat-file: %s", strerror(errno));
21 - } else if (!ret) {
22 - die("git-cat-file: disk full?");
23 - }
24 - size -= ret;
25 - buf += ret;
26 - }
27 -}
28 -
12 static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
13 {
14 /* the parser in tag.c is useless here. */
@@ -42,7 +25,7 @@ static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long
25 /* Found the tagger line. Copy out the contents
26 * of the buffer so far.
27 */
45 - flush_buffer(buf, cp - buf);
28 + write_or_die(1, buf, cp - buf);
29
30 /*
31 * Do something intelligent, like pretty-printing
@@ -61,18 +44,18 @@ static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long
44 sp++;
45 if (sp == cp) {
46 /* give up */
64 - flush_buffer(tagger,
47 + write_or_die(1, tagger,
48 cp - tagger);
49 break;
50 }
51 while (sp < cp &&
52 !('0' <= *sp && *sp <= '9'))
53 sp++;
71 - flush_buffer(tagger, sp - tagger);
54 + write_or_die(1, tagger, sp - tagger);
55 date = strtoul(sp, &ep, 10);
56 tz = strtol(ep, NULL, 10);
57 sp = show_date(date, tz);
75 - flush_buffer(sp, strlen(sp));
58 + write_or_die(1, sp, strlen(sp));
59 xwrite(1, "\n", 1);
60 break;
61 }
@@ -90,7 +73,7 @@ static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long
73 * remainder as is.
74 */
75 if (cp < endp)
93 - flush_buffer(cp, endp - cp);
76 + write_or_die(1, cp, endp - cp);
77 }
78
79 int cmd_cat_file(int argc, const char **argv, const char *prefix)
@@ -162,6 +145,6 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
145 if (!buf)
146 die("git-cat-file %s: bad file", argv[2]);
147
165 - flush_buffer(buf, size);
148 + write_or_die(1, buf, size);
149 return 0;
150 }
builtin-tar-tree.c
+4 -23
@@ -22,30 +22,11 @@ static unsigned long offset;
22 static time_t archive_time;
23 static int tar_umask;
24
25 -/* tries hard to write, either succeeds or dies in the attempt */
26 -static void reliable_write(const void *data, unsigned long size)
27 -{
28 - const char *buf = data;
29 -
30 - while (size > 0) {
31 - long ret = xwrite(1, buf, size);
32 - if (ret < 0) {
33 - if (errno == EPIPE)
34 - exit(0);
35 - die("git-tar-tree: %s", strerror(errno));
36 - } else if (!ret) {
37 - die("git-tar-tree: disk full?");
38 - }
39 - size -= ret;
40 - buf += ret;
41 - }
42 -}
43 -
25 /* writes out the whole block, but only if it is full */
26 static void write_if_needed(void)
27 {
28 if (offset == BLOCKSIZE) {
48 - reliable_write(block, BLOCKSIZE);
29 + write_or_die(1, block, BLOCKSIZE);
30 offset = 0;
31 }
32 }
@@ -70,7 +51,7 @@ static void write_blocked(const void *data, unsigned long size)
51 write_if_needed();
52 }
53 while (size >= BLOCKSIZE) {
73 - reliable_write(buf, BLOCKSIZE);
54 + write_or_die(1, buf, BLOCKSIZE);
55 size -= BLOCKSIZE;
56 buf += BLOCKSIZE;
57 }
@@ -94,10 +75,10 @@ static void write_trailer(void)
75 {
76 int tail = BLOCKSIZE - offset;
77 memset(block + offset, 0, tail);
97 - reliable_write(block, BLOCKSIZE);
78 + write_or_die(1, block, BLOCKSIZE);
79 if (tail < 2 * RECORDSIZE) {
80 memset(block, 0, offset);
100 - reliable_write(block, BLOCKSIZE);
81 + write_or_die(1, block, BLOCKSIZE);
82 }
83 }
84
cache.h
+1
@@ -388,6 +388,7 @@ extern char git_default_name[MAX_GITNAME];
388 extern char git_commit_encoding[MAX_ENCODING_LENGTH];
389
390 extern int copy_fd(int ifd, int ofd);
391 +extern void write_or_die(int fd, const void *buf, size_t count);
392
393 /* Finish off pack transfer receiving end */
394 extern int receive_unpack_pack(int fd[2], const char *me, int quiet, int);
write_or_die.c new
+20
@@ -0,0 +1,20 @@
1 +#include "cache.h"
2 +
3 +void write_or_die(int fd, const void *buf, size_t count)
4 +{
5 + const char *p = buf;
6 + ssize_t written;
7 +
8 + while (count > 0) {
9 + written = xwrite(fd, p, count);
10 + if (written == 0)
11 + die("disk full?");
12 + else if (written < 0) {
13 + if (errno == EPIPE)
14 + exit(0);
15 + die("write error (%s)", strerror(errno));
16 + }
17 + count -= written;
18 + p += written;
19 + }
20 +}