send fetch progress over imsg
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
diff --git a/lib/fetch.c b/lib/fetch.c
index 6867529..8276f9a 100644
--- a/lib/fetch.c
+++ b/lib/fetch.c
@@ -253,8 +253,8 @@ got_fetch(char *uri, char *branch_filter, char *destdir)
char proto[GOT_PROTOMAX], host[GOT_HOSTMAX], port[GOT_PORTMAX];
char repo[GOT_REPOMAX], path[GOT_PATHMAX];
int imsg_fetchfds[2], imsg_idxfds[2], fetchfd;
- int packfd = -1, npackfd, idxfd = -1, nidxfd, status;
- struct got_object_id packhash;
+ int packfd = -1, npackfd, idxfd = -1, nidxfd, status, done = 0;
+ struct got_object_id *packhash = NULL;
const struct got_error *err;
struct imsgbuf ibuf;
pid_t pid;
@@ -323,9 +323,27 @@ got_fetch(char *uri, char *branch_filter, char *destdir)
npackfd = dup(packfd);
if (npackfd == -1)
return got_error_from_errno("dup");
- err = got_privsep_wait_fetch_done(&ibuf, &packhash);
- if (err != NULL)
- return err;
+ while (!done) {
+ struct got_object_id *id;
+ char *refname;
+ err = got_privsep_recv_fetch_progress(&done,
+ &id, &refname, &ibuf);
+ if (err != NULL)
+ return err;
+ if (done) {
+ packhash = got_object_id_dup(id);
+ if (packhash == NULL)
+ return got_error_from_errno(
+ "got_object_id_dup");
+ } else {
+ char *id_str;
+ /* TODO Use a progress callback */
+ err = got_object_id_str(&id_str, id);
+ if (err)
+ return err;
+ printf( "%.12s %s\n", id_str, refname);
+ }
+ }
if (waitpid(pid, &status, 0) == -1)
return got_error_from_errno("child exit");
@@ -340,7 +358,7 @@ got_fetch(char *uri, char *branch_filter, char *destdir)
return got_error_from_errno("close");
imsg_init(&ibuf, imsg_idxfds[0]);
- err = got_privsep_send_index_pack_req(&ibuf, npackfd, &packhash);
+ err = got_privsep_send_index_pack_req(&ibuf, npackfd, packhash);
if (err != NULL)
return err;
err = got_privsep_send_tmpfd(&ibuf, nidxfd);
@@ -355,7 +373,7 @@ got_fetch(char *uri, char *branch_filter, char *destdir)
if (waitpid(pid, &status, 0) == -1)
return got_error_from_errno("child exit");
- err = got_object_id_str(&id_str, &packhash);
+ err = got_object_id_str(&id_str, packhash);
if (err)
return err;
if (asprintf(&packpath, "objects/pack/pack-%s.pack", id_str) == -1)
@@ -374,6 +392,7 @@ got_fetch(char *uri, char *branch_filter, char *destdir)
free(idxpath);
free(packpath);
free(default_destdir);
+ free(packhash);
return NULL;
}
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index f02d6fb..4b14f8c 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -325,12 +325,10 @@ const struct got_error *got_privsep_wait_index_pack_done(struct imsgbuf *);
const struct got_error *got_privsep_send_fetch_req(struct imsgbuf *, int);
const struct got_error *got_privsep_send_fetch_progress(struct imsgbuf *,
struct got_object_id *, const char *);
-const struct got_error *got_privsep_recv_fetch_progress(struct got_object_id **,
- char **, struct imsgbuf *);
+const struct got_error *got_privsep_recv_fetch_progress(int *,
+ struct got_object_id **, char **, struct imsgbuf *);
const struct got_error *got_privsep_send_fetch_done(struct imsgbuf *,
struct got_object_id);
-const struct got_error *got_privsep_wait_fetch_done(struct imsgbuf *,
- struct got_object_id*);
const struct got_error *got_privsep_get_imsg_obj(struct got_object **,
struct imsg *, struct imsgbuf *);
const struct got_error *got_privsep_recv_obj(struct got_object **,
diff --git a/lib/privsep.c b/lib/privsep.c
index 868e4fb..642c62a 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -462,7 +462,7 @@ got_privsep_send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
const struct got_error *
-got_privsep_recv_fetch_progress(struct got_object_id **refid,
+got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
char **refname, struct imsgbuf *ibuf)
{
const struct got_error *err = NULL;
@@ -472,7 +472,8 @@ got_privsep_recv_fetch_progress(struct got_object_id **refid,
MIN(sizeof(struct got_imsg_error),
sizeof(struct got_imsg_fetch_progress));
- *refid = NULL;
+ *done = 0;
+ *id = NULL;
*refname = NULL;
err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
@@ -485,12 +486,12 @@ got_privsep_recv_fetch_progress(struct got_object_id **refid,
err = recv_imsg_error(&imsg, datalen);
break;
case GOT_IMSG_FETCH_PROGRESS:
- *refid = malloc(sizeof(**refid));
- if (*refid == NULL) {
+ *id = malloc(sizeof(**id));
+ if (*id == NULL) {
err = got_error_from_errno("malloc");
break;
}
- memcpy((*refid)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
+ memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
if (datalen <= SHA1_DIGEST_LENGTH) {
err = got_error(GOT_ERR_PRIVSEP_MSG);
break;
@@ -502,14 +503,27 @@ got_privsep_recv_fetch_progress(struct got_object_id **refid,
break;
}
break;
+ case GOT_IMSG_FETCH_DONE:
+ *id = malloc(sizeof(**id));
+ if (*id == NULL) {
+ err = got_error_from_errno("malloc");
+ break;
+ }
+ if (datalen != SHA1_DIGEST_LENGTH) {
+ err = got_error(GOT_ERR_PRIVSEP_MSG);
+ break;
+ }
+ memcpy((*id)->sha1, imsg.data, SHA1_DIGEST_LENGTH);
+ *done = 1;
+ break;
default:
err = got_error(GOT_ERR_PRIVSEP_MSG);
break;
}
if (err) {
- free(*refid);
- *refid = NULL;
+ free(*id);
+ *id = NULL;
free(*refname);
*refname = NULL;
}
@@ -518,27 +532,6 @@ got_privsep_recv_fetch_progress(struct got_object_id **refid,
}
const struct got_error *
-got_privsep_wait_fetch_done(struct imsgbuf *ibuf, struct got_object_id *hash)
-{
- const struct got_error *err = NULL;
- struct imsg imsg;
-
- err = got_privsep_recv_imsg(&imsg, ibuf, 0);
- if (err)
- return err;
- if (imsg.hdr.type == GOT_IMSG_FETCH_DONE &&
- imsg.hdr.len - sizeof(imsg.hdr) == SHA1_DIGEST_LENGTH) {
- memcpy(hash->sha1, imsg.data, SHA1_DIGEST_LENGTH);
- imsg_free(&imsg);
- return NULL;
- }
-
- imsg_free(&imsg);
- return got_error(GOT_ERR_PRIVSEP_MSG);
-}
-
-
-const struct got_error *
got_privsep_send_index_pack_req(struct imsgbuf *ibuf, int fd, struct got_object_id *hash)
{
const struct got_error *err = NULL;
diff --git a/libexec/got-fetch-pack/got-fetch-pack.c b/libexec/got-fetch-pack/got-fetch-pack.c
index f3c5837..fd5a895 100644
--- a/libexec/got-fetch-pack/got-fetch-pack.c
+++ b/libexec/got-fetch-pack/got-fetch-pack.c
@@ -282,7 +282,8 @@ got_tokenize_refline(char *line, char **sp, size_t nsp)
}
static const struct got_error *
-fetch_pack(int fd, int packfd, struct got_object_id *packid)
+fetch_pack(int fd, int packfd, struct got_object_id *packid,
+ struct imsgbuf *ibuf)
{
const struct got_error *err = NULL;
char buf[GOT_PKTMAX], *sp[3];
@@ -346,7 +347,9 @@ fetch_pack(int fd, int packfd, struct got_object_id *packid)
if (got_resolve_remote_ref(&have[nref], sp[1]) == -1)
memset(&have[nref], 0, sizeof(have[nref]));
- // TODO: send IMSG with progress.
+ err = got_privsep_send_fetch_progress(ibuf, &want[nref], sp[1]);
+ if (err)
+ goto done;
if (chattygit)
fprintf(stderr, "remote %s\n", sp[1]);
nref++;
@@ -403,7 +406,8 @@ fetch_pack(int fd, int packfd, struct got_object_id *packid)
}
buf[n] = 0;
- fprintf(stderr, "fetching...\n");
+ if (chattygit)
+ fprintf(stderr, "fetching...\n");
packsz = 0;
while (1) {
ssize_t w;
@@ -434,7 +438,7 @@ fetch_pack(int fd, int packfd, struct got_object_id *packid)
done:
free(have);
free(want);
- return 0;
+ return err;
}
@@ -487,7 +491,7 @@ main(int argc, char **argv)
}
packfd = imsg.fd;
- err = fetch_pack(fetchfd, packfd, &packid);
+ err = fetch_pack(fetchfd, packfd, &packid, &ibuf);
if (err)
goto done;
done: