convert writepkt to struct got_error
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
diff --git a/libexec/got-fetch-pack/got-fetch-pack.c b/libexec/got-fetch-pack/got-fetch-pack.c
index d7501b5..7dba862 100644
--- a/libexec/got-fetch-pack/got-fetch-pack.c
+++ b/libexec/got-fetch-pack/got-fetch-pack.c
@@ -125,18 +125,25 @@ readpkt(int *outlen, int fd, char *buf, int nbuf)
return NULL;
}
-static int
+static const struct got_error *
writepkt(int fd, char *buf, int nbuf)
{
char len[5];
int i;
+ ssize_t w;
if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
- return -1;
- if (write(fd, len, 4) != 4)
- return -1;
- if (write(fd, buf, nbuf) != nbuf)
- return -1;
+ return got_error(GOT_ERR_NO_SPACE);
+ w = write(fd, len, 4);
+ if (w == -1)
+ return got_error_from_errno("write");
+ if (w != 4)
+ return got_error(GOT_ERR_IO);
+ w = write(fd, buf, nbuf);
+ if (w == -1)
+ return got_error_from_errno("write");
+ if (w != nbuf)
+ return got_error(GOT_ERR_IO);
if (chattygit) {
fprintf(stderr, "writepkt: %s:\t", len);
fwrite(buf, 1, nbuf, stderr);
@@ -146,7 +153,7 @@ writepkt(int fd, char *buf, int nbuf)
}
fputc('\n', stderr);
}
- return 0;
+ return NULL;
}
static const struct got_error *
@@ -516,10 +523,9 @@ fetch_pack(int fd, int packfd, struct got_object_id *packid,
err = got_error(GOT_ERR_NO_SPACE);
goto done;
}
- if (writepkt(fd, buf, n) == -1) {
- err = got_error_from_errno("writepkt");
+ err = writepkt(fd, buf, n);
+ if (err)
goto done;
- }
req = 1;
}
flushpkt(fd);
@@ -532,20 +538,18 @@ fetch_pack(int fd, int packfd, struct got_object_id *packid,
err = got_error(GOT_ERR_NO_SPACE);
goto done;
}
- if (writepkt(fd, buf, n + 1) == -1) {
- err = got_error_from_errno("writepkt");
+ err = writepkt(fd, buf, n + 1);
+ if (err)
goto done;
- }
}
if (!req) {
fprintf(stderr, "up to date\n");
flushpkt(fd);
}
n = snprintf(buf, sizeof(buf), "done\n");
- if (writepkt(fd, buf, n) == -1) {
- err = got_error_from_errno("writepkt");
+ err = writepkt(fd, buf, n);
+ if (err)
goto done;
- }
if (!req)
return 0;