summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 1fdfd05)
raw | patch | inline | side by side (parent: 1fdfd05)
author | Junio C Hamano <junkio@cox.net> | |
Tue, 20 Dec 2005 00:18:28 +0000 (16:18 -0800) | ||
committer | Junio C Hamano <junkio@cox.net> | |
Tue, 20 Dec 2005 02:28:16 +0000 (18:28 -0800) |
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>
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>
apply.c | patch | blob | history | |
cat-file.c | patch | blob | history | |
copy.c | patch | blob | history | |
csum-file.c | patch | blob | history | |
git-compat-util.h | patch | blob | history | |
mktag.c | patch | blob | history | |
pkt-line.c | patch | blob | history | |
tar-tree.c | patch | blob | history | |
unpack-objects.c | patch | blob | history |
index 1742ab28e95936eed52e910930a8a04869c5418b..d5e7bfdb4de1fe7990ae11e190537fd1c490d54c 100644 (file)
--- a/apply.c
+++ b/apply.c
buffer = xrealloc(buffer, alloc);
nr = alloc - size;
}
- nr = read(fd, buffer + size, nr);
+ nr = xread(fd, buffer + size, nr);
if (!nr)
break;
- if (nr < 0) {
- if (errno == EAGAIN)
- continue;
+ if (nr < 0)
die("git-apply: read returned %s", strerror(errno));
- }
size += nr;
}
*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);
got = 0;
for (;;) {
- int ret = read(fd, buf + got, size - got);
- if (ret < 0) {
- if (errno == EAGAIN)
- continue;
- break;
- }
- if (!ret)
+ int ret = xread(fd, buf + got, size - got);
+ if (ret <= 0)
break;
got += ret;
}
@@ -1600,12 +1592,9 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
if (fd < 0)
return -1;
while (size) {
- int written = write(fd, buf, size);
- if (written < 0) {
- if (errno == EINTR || errno == EAGAIN)
- continue;
+ int written = xwrite(fd, buf, size);
+ if (written < 0)
die("writing file %s: %s", path, strerror(errno));
- }
if (!written)
die("out of space writing file %s", path);
buf += written;
diff --git a/cat-file.c b/cat-file.c
index 7594108c6e5f7d8830c7b5ca6f45951b8b4bcc23..96d66b43043ee1e2381bfb80bb2bc1c7063ecc38 100644 (file)
--- a/cat-file.c
+++ b/cat-file.c
die("git-cat-file %s: bad file", argv[2]);
while (size > 0) {
- long ret = write(1, buf, size);
+ long ret = xwrite(1, buf, size);
if (ret < 0) {
- if (errno == EAGAIN)
- continue;
/* Ignore epipe */
if (errno == EPIPE)
break;
index e1cd5d06503d93f6b400a3911e6b61ff327190ca..7100eedbc3277b9b0997311459d054ba06950b61 100644 (file)
--- a/copy.c
+++ b/copy.c
int len;
char buffer[8192];
char *buf = buffer;
- len = read(ifd, buffer, sizeof(buffer));
+ len = xread(ifd, buffer, sizeof(buffer));
if (!len)
break;
if (len < 0) {
int read_error;
- if (errno == EAGAIN)
- continue;
read_error = errno;
close(ifd);
return error("copy-fd: read returned %s",
strerror(read_error));
}
- while (1) {
- int written = write(ofd, buf, len);
+ while (len) {
+ int written = xwrite(ofd, buf, len);
if (written > 0) {
buf += written;
len -= written;
- if (!len)
- break;
}
- if (!written)
+ else if (!written)
return error("copy-fd: write returned 0");
- if (errno == EAGAIN || errno == EINTR)
- continue;
- return error("copy-fd: write returned %s",
- strerror(errno));
+ else
+ return error("copy-fd: write returned %s",
+ strerror(errno));
}
}
close(ifd);
diff --git a/csum-file.c b/csum-file.c
index c66b9eb10bfad8fee8b7b779cbb4a8e7c86480af..5f9249aeedfa7d4156cf412da6a7e7d77f33ff86 100644 (file)
--- a/csum-file.c
+++ b/csum-file.c
void *buf = f->buffer;
for (;;) {
- int ret = write(f->fd, buf, count);
+ int ret = xwrite(f->fd, buf, count);
if (ret > 0) {
buf += ret;
count -= ret;
}
if (!ret)
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));
}
}
diff --git a/git-compat-util.h b/git-compat-util.h
index ead0ede5872e3c1ca10596010d4827ed726ff394..0c98c9937df14bfa8be4f58cae84aa16029be883 100644 (file)
--- a/git-compat-util.h
+++ b/git-compat-util.h
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 */
#undef isspace
#undef isdigit
index 97e270a5761f071aeb3666b595b5cd6a8cbd4098..fc6a9bf5f34158605add585439848367deaefac5 100644 (file)
--- a/mktag.c
+++ b/mktag.c
// Read the signature
size = 0;
for (;;) {
- int ret = read(0, buffer + size, MAXSIZE - size);
- if (!ret)
+ int ret = xread(0, buffer + size, MAXSIZE - size);
+ if (ret <= 0)
break;
- if (ret < 0) {
- if (errno == EAGAIN)
- continue;
- break;
- }
size += ret;
}
diff --git a/pkt-line.c b/pkt-line.c
index 69473046bf717c9b97470d7652c15377dd9fc9d5..bb3bab05cd203e114b24b778cb5f55218abdec95 100644 (file)
--- a/pkt-line.c
+++ b/pkt-line.c
static void safe_write(int fd, const void *buf, unsigned n)
{
while (n) {
- int ret = write(fd, buf, n);
+ int ret = xwrite(fd, buf, n);
if (ret > 0) {
buf += ret;
n -= ret;
}
if (!ret)
die("write error (disk full?)");
- if (errno == EAGAIN || errno == EINTR)
- continue;
die("write error (%s)", strerror(errno));
}
}
int n = 0;
while (n < size) {
- int ret = read(fd, buffer + n, size - n);
- if (ret < 0) {
- if (errno == EINTR || errno == EAGAIN)
- continue;
+ int ret = xread(fd, buffer + n, size - n);
+ if (ret < 0)
die("read error (%s)", strerror(errno));
- }
if (!ret)
die("unexpected EOF");
n += ret;
diff --git a/tar-tree.c b/tar-tree.c
index bacb23ae6365767dfdc93c6041aa9f3cb673b27b..96bd1438d9033e5bca3841fb85a531ea27070faf 100644 (file)
--- a/tar-tree.c
+++ b/tar-tree.c
static void reliable_write(void *buf, unsigned long size)
{
while (size > 0) {
- long ret = write(1, buf, size);
+ long ret = xwrite(1, buf, size);
if (ret < 0) {
- if (errno == EAGAIN)
- continue;
if (errno == EPIPE)
exit(0);
die("git-tar-tree: %s", strerror(errno));
diff --git a/unpack-objects.c b/unpack-objects.c
index cfd61ae6b08c0635da4044ec3ab09f40c3178632..5c5cb12f6fa2a9a8fe061c2e75380c8b9e7ebaf1 100644 (file)
--- a/unpack-objects.c
+++ b/unpack-objects.c
offset = 0;
}
do {
- int ret = read(0, buffer + len, sizeof(buffer) - len);
+ int ret = xread(0, buffer + len, sizeof(buffer) - len);
if (ret <= 0) {
if (!ret)
die("early EOF");
- if (errno == EAGAIN || errno == EINTR)
- continue;
die("read error on input: %s", strerror(errno));
}
len += ret;
/* Write the last part of the buffer to stdout */
while (len) {
- int ret = write(1, buffer + offset, len);
- if (!ret)
- break;
- if (ret < 0) {
- if (errno == EAGAIN || errno == EINTR)
- continue;
+ int ret = xwrite(1, buffer + offset, len);
+ if (ret <= 0)
break;
- }
len -= ret;
offset += ret;
}