convert readn and readpkt 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
diff --git a/include/got_error.h b/include/got_error.h
index d9fae38..55d93ab 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -136,6 +136,7 @@
#define GOT_ERR_PARSE_URI 119
#define GOT_ERR_BAD_PROTO 120
#define GOT_ERR_ADDRINFO 121
+#define GOT_ERR_BAD_PACKET 122
static const struct got_error {
int code;
@@ -278,6 +279,7 @@ static const struct got_error {
{ GOT_ERR_PARSE_URI, "failed to parse uri" },
{ GOT_ERR_BAD_PROTO, "unknown protocol" },
{ GOT_ERR_ADDRINFO, "getaddrinfo failed" },
+ { GOT_ERR_BAD_PACKET, "bad packet received" },
};
/*
diff --git a/libexec/got-fetch-pack/got-fetch-pack.c b/libexec/got-fetch-pack/got-fetch-pack.c
index a12b4c4..d7501b5 100644
--- a/libexec/got-fetch-pack/got-fetch-pack.c
+++ b/libexec/got-fetch-pack/got-fetch-pack.c
@@ -58,21 +58,21 @@ static int chattygit;
static char *fetchbranch;
static struct got_object_id zhash = {.sha1={0}};
-static int
-readn(int fd, void *buf, size_t n)
+static const struct got_error *
+readn(ssize_t *off, int fd, void *buf, size_t n)
{
- ssize_t r, off;
+ ssize_t r;
- off = 0;
- while (off != n) {
- r = read(fd, buf + off, n - off);
- if (r < 0)
- return -1;
+ *off = 0;
+ while (*off != n) {
+ r = read(fd, buf + *off, n - *off);
+ if (r == -1)
+ return got_error_from_errno("read");
if (r == 0)
- return off;
- off += r;
+ return NULL;
+ *off += r;
}
- return off;
+ return NULL;
}
static int
@@ -84,34 +84,45 @@ flushpkt(int fd)
}
-static int
-readpkt(int fd, char *buf, int nbuf)
+static const struct got_error *
+readpkt(int *outlen, int fd, char *buf, int nbuf)
{
+ const struct got_error *err = NULL;
char len[5];
char *e;
- int n, r;
+ int n;
+ ssize_t r;
+
+ *outlen = 0;
+
+ err = readn(&r, fd, len, 4);
+ if (err)
+ return err;
- if (readn(fd, len, 4) == -1) {
- return -1;
- }
len[4] = 0;
n = strtol(len, &e, 16);
if (n == 0) {
if (chattygit)
fprintf(stderr, "readpkt: 0000\n");
- return 0;
+ return NULL;
}
if (e != len + 4 || n <= 4)
- err(1, "invalid packet line length");
+ return got_error(GOT_ERR_BAD_PACKET);
n -= 4;
if (n >= nbuf)
- err(1, "buffer too small");
- if ((r = readn(fd, buf, n)) != n)
- return -1;
+ return got_error(GOT_ERR_NO_SPACE);
+
+ err = readn(&r, fd, buf, n);
+ if (err)
+ return err;
+ if (r != n)
+ return got_error(GOT_ERR_BAD_PACKET);
buf[n] = 0;
if (chattygit)
fprintf(stderr, "readpkt: %s:\t%.*s\n", len, nbuf, buf);
- return n;
+
+ *outlen = n;
+ return NULL;
}
static int
@@ -159,6 +170,7 @@ match_remote_ref(struct got_pathlist_head *have_refs, struct got_object_id *id,
static const struct got_error *
check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
{
+ const struct got_error *err = NULL;
SHA1_CTX ctx;
uint8_t hexpect[SHA1_DIGEST_LENGTH];
char s1[SHA1_DIGEST_STRING_LENGTH + 1];
@@ -175,7 +187,9 @@ check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
nr = sizeof(buf);
if (sz - n - 20 < sizeof(buf))
nr = sz - n - 20;
- r = readn(fd, buf, nr);
+ err = readn(&r, fd, buf, nr);
+ if (err)
+ return err;
if (r != nr)
return got_error(GOT_ERR_BAD_PACKFILE);
SHA1Update(&ctx, buf, nr);
@@ -183,7 +197,10 @@ check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
}
SHA1Final(hcomp, &ctx);
- if (readn(fd, hexpect, sizeof(hexpect)) != sizeof(hexpect))
+ err = readn(&r, fd, hexpect, sizeof(hexpect));
+ if (err)
+ return err;
+ if (r != sizeof(hexpect))
return got_error(GOT_ERR_BAD_PACKFILE);
if (memcmp(hcomp, hexpect, SHA1_DIGEST_LENGTH) != 0) {
got_sha1_digest_to_str(hcomp, s1, sizeof(s1));
@@ -414,11 +431,9 @@ fetch_pack(int fd, int packfd, struct got_object_id *packid,
if (chattygit)
fprintf(stderr, "starting fetch\n");
while (1) {
- n = readpkt(fd, buf, sizeof(buf));
- if (n == -1) {
- err = got_error_from_errno("readpkt");
+ err = readpkt(&n, fd, buf, sizeof(buf));
+ if (err)
goto done;
- }
if (n == 0)
break;
if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
@@ -534,34 +549,31 @@ fetch_pack(int fd, int packfd, struct got_object_id *packid,
if (!req)
return 0;
- if ((n = readpkt(fd, buf, sizeof(buf))) == -1) {
- err = got_error_from_errno("readpkt");
+ err = readpkt(&n, fd, buf, sizeof(buf));
+ if (err)
goto done;
- }
buf[n] = 0;
if (chattygit)
fprintf(stderr, "fetching...\n");
packsz = 0;
while (1) {
- ssize_t w;
- n = readn(fd, buf, sizeof buf);
- if (n == 0)
- break;
- if (n == -1) {
- err = got_error_from_errno("readn");
+ ssize_t r, w;
+ err = readn(&r, fd, buf, sizeof buf);
+ if (err)
goto done;
- }
- w = write(packfd, buf, n);
+ if (r == 0)
+ break;
+ w = write(packfd, buf, r);
if (w == -1) {
err = got_error_from_errno("write");
goto done;
}
- if (w != n) {
+ if (w != r) {
err = got_error(GOT_ERR_IO);
goto done;
}
- packsz += n;
+ packsz += r;
}
if (lseek(packfd, 0, SEEK_SET) == -1) {
err = got_error_from_errno("lseek");