@matheusbsbs / matheus-bagliano / commits / 1c15afb934
Fork 0

xread/xwrite: do not worry about EINTR at calling sites.

We had errno==EINTR check after read(2)/write(2) sprinkled all over the places, always doing continue. Consolidate them into xread()/xwrite() wrapper routines. Credits for suggestion goes to HPA -- bugs are mine. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Dec 19, 2005 at 16:18 UTC 1c15afb9343bca82e687d008ec983a9110ac9c40
9 files changed +46 -63
apply.c
+6 -17
@@ -84,14 +84,11 @@ static void *read_patch_file(int fd, unsigned long *sizep)
84 buffer = xrealloc(buffer, alloc);
85 nr = alloc - size;
86 }
87 - nr = read(fd, buffer + size, nr);
87 + nr = xread(fd, buffer + size, nr);
88 if (!nr)
89 break;
90 - if (nr < 0) {
91 - if (errno == EAGAIN)
92 - continue;
90 + if (nr < 0)
91 die("git-apply: read returned %s", strerror(errno));
94 - }
92 size += nr;
93 }
94 *sizep = size;
@@ -1006,13 +1003,8 @@ static int read_old_data(struct stat *st, const char *path, void *buf, unsigned
1003 return error("unable to open %s", path);
1004 got = 0;
1005 for (;;) {
1009 - int ret = read(fd, buf + got, size - got);
1010 - if (ret < 0) {
1011 - if (errno == EAGAIN)
1012 - continue;
1013 - break;
1014 - }
1015 - if (!ret)
1006 + int ret = xread(fd, buf + got, size - got);
1007 + if (ret <= 0)
1008 break;
1009 got += ret;
1010 }
@@ -1600,12 +1592,9 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
1592 if (fd < 0)
1593 return -1;
1594 while (size) {
1603 - int written = write(fd, buf, size);
1604 - if (written < 0) {
1605 - if (errno == EINTR || errno == EAGAIN)
1606 - continue;
1595 + int written = xwrite(fd, buf, size);
1596 + if (written < 0)
1597 die("writing file %s: %s", path, strerror(errno));
1608 - }
1598 if (!written)
1599 die("out of space writing file %s", path);
1600 buf += written;
cat-file.c
+1 -3
@@ -55,10 +55,8 @@ int main(int argc, char **argv)
55 die("git-cat-file %s: bad file", argv[2]);
56
57 while (size > 0) {
58 - long ret = write(1, buf, size);
58 + long ret = xwrite(1, buf, size);
59 if (ret < 0) {
60 - if (errno == EAGAIN)
61 - continue;
60 /* Ignore epipe */
61 if (errno == EPIPE)
62 break;
copy.c
+7 -12
@@ -6,32 +6,27 @@ int copy_fd(int ifd, int ofd)
6 int len;
7 char buffer[8192];
8 char *buf = buffer;
9 - len = read(ifd, buffer, sizeof(buffer));
9 + len = xread(ifd, buffer, sizeof(buffer));
10 if (!len)
11 break;
12 if (len < 0) {
13 int read_error;
14 - if (errno == EAGAIN)
15 - continue;
14 read_error = errno;
15 close(ifd);
16 return error("copy-fd: read returned %s",
17 strerror(read_error));
18 }
21 - while (1) {
22 - int written = write(ofd, buf, len);
19 + while (len) {
20 + int written = xwrite(ofd, buf, len);
21 if (written > 0) {
22 buf += written;
23 len -= written;
26 - if (!len)
27 - break;
24 }
29 - if (!written)
25 + else if (!written)
26 return error("copy-fd: write returned 0");
31 - if (errno == EAGAIN || errno == EINTR)
32 - continue;
33 - return error("copy-fd: write returned %s",
34 - strerror(errno));
27 + else
28 + return error("copy-fd: write returned %s",
29 + strerror(errno));
30 }
31 }
32 close(ifd);
csum-file.c
+1 -3
@@ -15,7 +15,7 @@ static int sha1flush(struct sha1file *f, unsigned int count)
15 void *buf = f->buffer;
16
17 for (;;) {
18 - int ret = write(f->fd, buf, count);
18 + int ret = xwrite(f->fd, buf, count);
19 if (ret > 0) {
20 buf += ret;
21 count -= ret;
@@ -25,8 +25,6 @@ static int sha1flush(struct sha1file *f, unsigned int count)
25 }
26 if (!ret)
27 die("sha1 file '%s' write error. Out of diskspace", f->name);
28 - if (errno == EAGAIN || errno == EINTR)
29 - continue;
28 die("sha1 file '%s' write error (%s)", f->name, strerror(errno));
29 }
30 }
git-compat-util.h
+22
@@ -84,6 +84,28 @@ static inline void *xcalloc(size_t nmemb, size_t size)
84 return ret;
85 }
86
87 +static inline ssize_t xread(int fd, void *buf, size_t len)
88 +{
89 + ssize_t nr;
90 + while (1) {
91 + nr = read(fd, buf, len);
92 + if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
93 + continue;
94 + return nr;
95 + }
96 +}
97 +
98 +static inline ssize_t xwrite(int fd, const void *buf, size_t len)
99 +{
100 + ssize_t nr;
101 + while (1) {
102 + nr = write(fd, buf, len);
103 + if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
104 + continue;
105 + return nr;
106 + }
107 +}
108 +
109 /* Sane ctype - no locale, and works with signed chars */
110 #undef isspace
111 #undef isdigit
mktag.c
+2 -7
@@ -116,14 +116,9 @@ int main(int argc, char **argv)
116 // Read the signature
117 size = 0;
118 for (;;) {
119 - int ret = read(0, buffer + size, MAXSIZE - size);
120 - if (!ret)
119 + int ret = xread(0, buffer + size, MAXSIZE - size);
120 + if (ret <= 0)
121 break;
122 - if (ret < 0) {
123 - if (errno == EAGAIN)
124 - continue;
125 - break;
126 - }
122 size += ret;
123 }
124
pkt-line.c
+3 -8
@@ -19,7 +19,7 @@
19 static void safe_write(int fd, const void *buf, unsigned n)
20 {
21 while (n) {
22 - int ret = write(fd, buf, n);
22 + int ret = xwrite(fd, buf, n);
23 if (ret > 0) {
24 buf += ret;
25 n -= ret;
@@ -27,8 +27,6 @@ static void safe_write(int fd, const void *buf, unsigned n)
27 }
28 if (!ret)
29 die("write error (disk full?)");
30 - if (errno == EAGAIN || errno == EINTR)
31 - continue;
30 die("write error (%s)", strerror(errno));
31 }
32 }
@@ -68,12 +66,9 @@ static void safe_read(int fd, void *buffer, unsigned size)
66 int n = 0;
67
68 while (n < size) {
71 - int ret = read(fd, buffer + n, size - n);
72 - if (ret < 0) {
73 - if (errno == EINTR || errno == EAGAIN)
74 - continue;
69 + int ret = xread(fd, buffer + n, size - n);
70 + if (ret < 0)
71 die("read error (%s)", strerror(errno));
76 - }
72 if (!ret)
73 die("unexpected EOF");
74 n += ret;
tar-tree.c
+1 -3
@@ -34,10 +34,8 @@ struct path_prefix {
34 static void reliable_write(void *buf, unsigned long size)
35 {
36 while (size > 0) {
37 - long ret = write(1, buf, size);
37 + long ret = xwrite(1, buf, size);
38 if (ret < 0) {
39 - if (errno == EAGAIN)
40 - continue;
39 if (errno == EPIPE)
40 exit(0);
41 die("git-tar-tree: %s", strerror(errno));
unpack-objects.c
+3 -10
@@ -31,12 +31,10 @@ static void * fill(int min)
31 offset = 0;
32 }
33 do {
34 - int ret = read(0, buffer + len, sizeof(buffer) - len);
34 + int ret = xread(0, buffer + len, sizeof(buffer) - len);
35 if (ret <= 0) {
36 if (!ret)
37 die("early EOF");
38 - if (errno == EAGAIN || errno == EINTR)
39 - continue;
38 die("read error on input: %s", strerror(errno));
39 }
40 len += ret;
@@ -299,14 +297,9 @@ int main(int argc, char **argv)
297
298 /* Write the last part of the buffer to stdout */
299 while (len) {
302 - int ret = write(1, buffer + offset, len);
303 - if (!ret)
304 - break;
305 - if (ret < 0) {
306 - if (errno == EAGAIN || errno == EINTR)
307 - continue;
300 + int ret = xwrite(1, buffer + offset, len);
301 + if (ret <= 0)
302 break;
309 - }
303 len -= ret;
304 offset += ret;
305 }