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>maint
parent
1fdfd05db2
commit
1c15afb934
23
apply.c
23
apply.c
|
@ -84,14 +84,11 @@ static void *read_patch_file(int fd, unsigned long *sizep)
|
||||||
buffer = xrealloc(buffer, alloc);
|
buffer = xrealloc(buffer, alloc);
|
||||||
nr = alloc - size;
|
nr = alloc - size;
|
||||||
}
|
}
|
||||||
nr = read(fd, buffer + size, nr);
|
nr = xread(fd, buffer + size, nr);
|
||||||
if (!nr)
|
if (!nr)
|
||||||
break;
|
break;
|
||||||
if (nr < 0) {
|
if (nr < 0)
|
||||||
if (errno == EAGAIN)
|
|
||||||
continue;
|
|
||||||
die("git-apply: read returned %s", strerror(errno));
|
die("git-apply: read returned %s", strerror(errno));
|
||||||
}
|
|
||||||
size += nr;
|
size += nr;
|
||||||
}
|
}
|
||||||
*sizep = size;
|
*sizep = size;
|
||||||
|
@ -1006,13 +1003,8 @@ static int read_old_data(struct stat *st, const char *path, void *buf, unsigned
|
||||||
return error("unable to open %s", path);
|
return error("unable to open %s", path);
|
||||||
got = 0;
|
got = 0;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int ret = read(fd, buf + got, size - got);
|
int ret = xread(fd, buf + got, size - got);
|
||||||
if (ret < 0) {
|
if (ret <= 0)
|
||||||
if (errno == EAGAIN)
|
|
||||||
continue;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (!ret)
|
|
||||||
break;
|
break;
|
||||||
got += ret;
|
got += ret;
|
||||||
}
|
}
|
||||||
|
@ -1600,12 +1592,9 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return -1;
|
return -1;
|
||||||
while (size) {
|
while (size) {
|
||||||
int written = write(fd, buf, size);
|
int written = xwrite(fd, buf, size);
|
||||||
if (written < 0) {
|
if (written < 0)
|
||||||
if (errno == EINTR || errno == EAGAIN)
|
|
||||||
continue;
|
|
||||||
die("writing file %s: %s", path, strerror(errno));
|
die("writing file %s: %s", path, strerror(errno));
|
||||||
}
|
|
||||||
if (!written)
|
if (!written)
|
||||||
die("out of space writing file %s", path);
|
die("out of space writing file %s", path);
|
||||||
buf += written;
|
buf += written;
|
||||||
|
|
|
@ -55,10 +55,8 @@ int main(int argc, char **argv)
|
||||||
die("git-cat-file %s: bad file", argv[2]);
|
die("git-cat-file %s: bad file", argv[2]);
|
||||||
|
|
||||||
while (size > 0) {
|
while (size > 0) {
|
||||||
long ret = write(1, buf, size);
|
long ret = xwrite(1, buf, size);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
if (errno == EAGAIN)
|
|
||||||
continue;
|
|
||||||
/* Ignore epipe */
|
/* Ignore epipe */
|
||||||
if (errno == EPIPE)
|
if (errno == EPIPE)
|
||||||
break;
|
break;
|
||||||
|
|
19
copy.c
19
copy.c
|
@ -6,32 +6,27 @@ int copy_fd(int ifd, int ofd)
|
||||||
int len;
|
int len;
|
||||||
char buffer[8192];
|
char buffer[8192];
|
||||||
char *buf = buffer;
|
char *buf = buffer;
|
||||||
len = read(ifd, buffer, sizeof(buffer));
|
len = xread(ifd, buffer, sizeof(buffer));
|
||||||
if (!len)
|
if (!len)
|
||||||
break;
|
break;
|
||||||
if (len < 0) {
|
if (len < 0) {
|
||||||
int read_error;
|
int read_error;
|
||||||
if (errno == EAGAIN)
|
|
||||||
continue;
|
|
||||||
read_error = errno;
|
read_error = errno;
|
||||||
close(ifd);
|
close(ifd);
|
||||||
return error("copy-fd: read returned %s",
|
return error("copy-fd: read returned %s",
|
||||||
strerror(read_error));
|
strerror(read_error));
|
||||||
}
|
}
|
||||||
while (1) {
|
while (len) {
|
||||||
int written = write(ofd, buf, len);
|
int written = xwrite(ofd, buf, len);
|
||||||
if (written > 0) {
|
if (written > 0) {
|
||||||
buf += written;
|
buf += written;
|
||||||
len -= written;
|
len -= written;
|
||||||
if (!len)
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
if (!written)
|
else if (!written)
|
||||||
return error("copy-fd: write returned 0");
|
return error("copy-fd: write returned 0");
|
||||||
if (errno == EAGAIN || errno == EINTR)
|
else
|
||||||
continue;
|
return error("copy-fd: write returned %s",
|
||||||
return error("copy-fd: write returned %s",
|
strerror(errno));
|
||||||
strerror(errno));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
close(ifd);
|
close(ifd);
|
||||||
|
|
|
@ -15,7 +15,7 @@ static int sha1flush(struct sha1file *f, unsigned int count)
|
||||||
void *buf = f->buffer;
|
void *buf = f->buffer;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int ret = write(f->fd, buf, count);
|
int ret = xwrite(f->fd, buf, count);
|
||||||
if (ret > 0) {
|
if (ret > 0) {
|
||||||
buf += ret;
|
buf += ret;
|
||||||
count -= ret;
|
count -= ret;
|
||||||
|
@ -25,8 +25,6 @@ static int sha1flush(struct sha1file *f, unsigned int count)
|
||||||
}
|
}
|
||||||
if (!ret)
|
if (!ret)
|
||||||
die("sha1 file '%s' write error. Out of diskspace", f->name);
|
die("sha1 file '%s' write error. Out of diskspace", f->name);
|
||||||
if (errno == EAGAIN || errno == EINTR)
|
|
||||||
continue;
|
|
||||||
die("sha1 file '%s' write error (%s)", f->name, strerror(errno));
|
die("sha1 file '%s' write error (%s)", f->name, strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,6 +84,28 @@ static inline void *xcalloc(size_t nmemb, size_t size)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline ssize_t xread(int fd, void *buf, size_t len)
|
||||||
|
{
|
||||||
|
ssize_t nr;
|
||||||
|
while (1) {
|
||||||
|
nr = read(fd, buf, len);
|
||||||
|
if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
|
||||||
|
continue;
|
||||||
|
return nr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline ssize_t xwrite(int fd, const void *buf, size_t len)
|
||||||
|
{
|
||||||
|
ssize_t nr;
|
||||||
|
while (1) {
|
||||||
|
nr = write(fd, buf, len);
|
||||||
|
if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
|
||||||
|
continue;
|
||||||
|
return nr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Sane ctype - no locale, and works with signed chars */
|
/* Sane ctype - no locale, and works with signed chars */
|
||||||
#undef isspace
|
#undef isspace
|
||||||
#undef isdigit
|
#undef isdigit
|
||||||
|
|
9
mktag.c
9
mktag.c
|
@ -116,14 +116,9 @@ int main(int argc, char **argv)
|
||||||
// Read the signature
|
// Read the signature
|
||||||
size = 0;
|
size = 0;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int ret = read(0, buffer + size, MAXSIZE - size);
|
int ret = xread(0, buffer + size, MAXSIZE - size);
|
||||||
if (!ret)
|
if (ret <= 0)
|
||||||
break;
|
break;
|
||||||
if (ret < 0) {
|
|
||||||
if (errno == EAGAIN)
|
|
||||||
continue;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
size += ret;
|
size += ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
pkt-line.c
11
pkt-line.c
|
@ -19,7 +19,7 @@
|
||||||
static void safe_write(int fd, const void *buf, unsigned n)
|
static void safe_write(int fd, const void *buf, unsigned n)
|
||||||
{
|
{
|
||||||
while (n) {
|
while (n) {
|
||||||
int ret = write(fd, buf, n);
|
int ret = xwrite(fd, buf, n);
|
||||||
if (ret > 0) {
|
if (ret > 0) {
|
||||||
buf += ret;
|
buf += ret;
|
||||||
n -= ret;
|
n -= ret;
|
||||||
|
@ -27,8 +27,6 @@ static void safe_write(int fd, const void *buf, unsigned n)
|
||||||
}
|
}
|
||||||
if (!ret)
|
if (!ret)
|
||||||
die("write error (disk full?)");
|
die("write error (disk full?)");
|
||||||
if (errno == EAGAIN || errno == EINTR)
|
|
||||||
continue;
|
|
||||||
die("write error (%s)", strerror(errno));
|
die("write error (%s)", strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,12 +66,9 @@ static void safe_read(int fd, void *buffer, unsigned size)
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
||||||
while (n < size) {
|
while (n < size) {
|
||||||
int ret = read(fd, buffer + n, size - n);
|
int ret = xread(fd, buffer + n, size - n);
|
||||||
if (ret < 0) {
|
if (ret < 0)
|
||||||
if (errno == EINTR || errno == EAGAIN)
|
|
||||||
continue;
|
|
||||||
die("read error (%s)", strerror(errno));
|
die("read error (%s)", strerror(errno));
|
||||||
}
|
|
||||||
if (!ret)
|
if (!ret)
|
||||||
die("unexpected EOF");
|
die("unexpected EOF");
|
||||||
n += ret;
|
n += ret;
|
||||||
|
|
|
@ -34,10 +34,8 @@ struct path_prefix {
|
||||||
static void reliable_write(void *buf, unsigned long size)
|
static void reliable_write(void *buf, unsigned long size)
|
||||||
{
|
{
|
||||||
while (size > 0) {
|
while (size > 0) {
|
||||||
long ret = write(1, buf, size);
|
long ret = xwrite(1, buf, size);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
if (errno == EAGAIN)
|
|
||||||
continue;
|
|
||||||
if (errno == EPIPE)
|
if (errno == EPIPE)
|
||||||
exit(0);
|
exit(0);
|
||||||
die("git-tar-tree: %s", strerror(errno));
|
die("git-tar-tree: %s", strerror(errno));
|
||||||
|
|
|
@ -31,12 +31,10 @@ static void * fill(int min)
|
||||||
offset = 0;
|
offset = 0;
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
int ret = read(0, buffer + len, sizeof(buffer) - len);
|
int ret = xread(0, buffer + len, sizeof(buffer) - len);
|
||||||
if (ret <= 0) {
|
if (ret <= 0) {
|
||||||
if (!ret)
|
if (!ret)
|
||||||
die("early EOF");
|
die("early EOF");
|
||||||
if (errno == EAGAIN || errno == EINTR)
|
|
||||||
continue;
|
|
||||||
die("read error on input: %s", strerror(errno));
|
die("read error on input: %s", strerror(errno));
|
||||||
}
|
}
|
||||||
len += ret;
|
len += ret;
|
||||||
|
@ -299,14 +297,9 @@ int main(int argc, char **argv)
|
||||||
|
|
||||||
/* Write the last part of the buffer to stdout */
|
/* Write the last part of the buffer to stdout */
|
||||||
while (len) {
|
while (len) {
|
||||||
int ret = write(1, buffer + offset, len);
|
int ret = xwrite(1, buffer + offset, len);
|
||||||
if (!ret)
|
if (ret <= 0)
|
||||||
break;
|
break;
|
||||||
if (ret < 0) {
|
|
||||||
if (errno == EAGAIN || errno == EINTR)
|
|
||||||
continue;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
len -= ret;
|
len -= ret;
|
||||||
offset += ret;
|
offset += ret;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue