Commit Diff


commit - 92dc95a8ef154c5ab569feff59be53cea26f5a90
commit + 1d72a2a0ee2d5c4546c0471e054c0d81e407bc72
blob - ffdb2f264540f878337fd2792f8b41eafa1d5b55
blob + bc538754a1763f2c475adf6b02dd67afde0541ef
--- lib/fetch.c
+++ lib/fetch.c
@@ -613,6 +613,13 @@ got_fetch_pack(struct got_object_id **pack_hash, struc
 		err = got_error_from_errno("calloc");
 		goto done;
 	}
+
+	*pack_hash = calloc(1, sizeof(**pack_hash));
+	if (*pack_hash == NULL) {
+		err = got_error_from_errno("calloc");
+		goto done;
+	}
+
 	while (!done) {
 		struct got_object_id *id = NULL;
 		char *refname = NULL;
@@ -621,19 +628,14 @@ got_fetch_pack(struct got_object_id **pack_hash, struc
 
 		err = got_privsep_recv_fetch_progress(&done,
 		    &id, &refname, symrefs, &server_progress,
-		    &packfile_size_cur, &fetchibuf);
+		    &packfile_size_cur, (*pack_hash)->sha1, &fetchibuf);
 		if (err != NULL)
 			goto done;
-		if (done) {
-			if (packfile_size > 0)
-				*pack_hash = id;
-			else
-				free(id);
-		} else if (refname && id) {
+		if (!done && refname && id) {
 			err = got_pathlist_insert(NULL, refs, refname, id);
 			if (err)
 				goto done;
-		} else if (server_progress) {
+		} else if (!done && server_progress) {
 			char *p;
 			/*
 			 * XXX git-daemon tends to send batched output with
@@ -672,7 +674,7 @@ got_fetch_pack(struct got_object_id **pack_hash, struc
 			free(server_progress);
 			if (err)
 				goto done;
-		} else if (packfile_size_cur != packfile_size) {
+		} else if (!done && packfile_size_cur != packfile_size) {
 			err = progress_cb(progress_arg, NULL,
 			    packfile_size_cur, 0, 0, 0, 0);
 			if (err)
@@ -691,9 +693,11 @@ got_fetch_pack(struct got_object_id **pack_hash, struc
 	}
 
 	/* If zero data was fetched without error we are already up-to-date. */
-	if (packfile_size == 0)
+	if (packfile_size == 0) {
+		free(*pack_hash);
+		*pack_hash = NULL;
 		goto done;
-	else if (packfile_size < sizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
+	} else if (packfile_size < sizeof(pack_hdr) + SHA1_DIGEST_LENGTH) {
 		err = got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
 		goto done;
 	} else {
blob - b6668b257a2454bd0f92457ae8cd5ccb9069e2a9
blob + 950723fdd3cab55b86ef0eb87c5739432be13b7c
--- lib/got_lib_privsep.h
+++ lib/got_lib_privsep.h
@@ -414,8 +414,8 @@ const struct got_error *got_privsep_send_fetch_req(str
     struct got_pathlist_head *, int, int);
 const struct got_error *got_privsep_send_fetch_outfd(struct imsgbuf *, int);
 const struct got_error *got_privsep_recv_fetch_progress(int *,
-    struct got_object_id **, char **, struct got_pathlist_head *,
-    char **, off_t *, struct imsgbuf *);
+    struct got_object_id **, char **, struct got_pathlist_head *, char **,
+    off_t *, uint8_t *, struct imsgbuf *);
 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 **,
blob - 7a87cf1648983cbf4f1f078d5daff69f534db236
blob + 79edb658b827574e323a216f2a813c4418040d70
--- lib/privsep.c
+++ lib/privsep.c
@@ -568,7 +568,7 @@ got_privsep_send_fetch_outfd(struct imsgbuf *ibuf, int
 const struct got_error *
 got_privsep_recv_fetch_progress(int *done, struct got_object_id **id,
     char **refname, struct got_pathlist_head *symrefs, char **server_progress,
-    off_t *packfile_size, struct imsgbuf *ibuf)
+    off_t *packfile_size, uint8_t *pack_sha1, struct imsgbuf *ibuf)
 {
 	const struct got_error *err = NULL;
 	struct imsg imsg;
@@ -583,6 +583,7 @@ got_privsep_recv_fetch_progress(int *done, struct got_
 	*refname = NULL;
 	*server_progress = NULL;
 	*packfile_size = 0;
+	memset(pack_sha1, 0, SHA1_DIGEST_LENGTH);
 
 	err = got_privsep_recv_imsg(&imsg, ibuf, 0);
 	if (err)
@@ -697,16 +698,11 @@ got_privsep_recv_fetch_progress(int *done, struct got_
 		memcpy(packfile_size, imsg.data, sizeof(*packfile_size));
 		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);
+		memcpy(pack_sha1, imsg.data, SHA1_DIGEST_LENGTH);
 		*done = 1;
 		break;
 	default:
blob - 22347c7306d44201b88b5b26b0759e20e8d05c0e
blob + 8f12b8a31ccaad03d37aa324b719c8c96c8f50d1
--- libexec/got-fetch-pack/got-fetch-pack.c
+++ libexec/got-fetch-pack/got-fetch-pack.c
@@ -498,10 +498,10 @@ send_fetch_download_progress(struct imsgbuf *ibuf, off
 }
 
 static const struct got_error *
-send_fetch_done(struct imsgbuf *ibuf, struct got_object_id hash)
+send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
 {
 	if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
-	    hash.sha1, SHA1_DIGEST_LENGTH) == -1)
+	    pack_sha1, SHA1_DIGEST_LENGTH) == -1)
 		return got_error_from_errno("imsg_compose FETCH");
 	return got_privsep_flush_imsg(ibuf);
 }
@@ -649,7 +649,7 @@ send_fetch_ref(struct imsgbuf *ibuf, struct got_object
 }
 
 static const struct got_error *
-fetch_pack(int fd, int packfd, struct got_object_id *packid,
+fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
     struct got_pathlist_head *have_refs, int fetch_all_branches,
     struct got_pathlist_head *wanted_branches,
     struct got_pathlist_head *wanted_refs, int list_refs_only,
@@ -670,7 +670,6 @@ fetch_pack(int fd, int packfd, struct got_object_id *p
 	int sent_my_capabilites = 0, have_sidebands = 0;
 	int found_branch = 0;
 	SHA1_CTX sha1_ctx;
-	uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
 	uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
 	size_t sha1_buf_len = 0;
 	ssize_t w;
@@ -1107,7 +1106,7 @@ main(int argc, char **argv)
 {
 	const struct got_error *err = NULL;
 	int fetchfd, packfd = -1, i;
-	struct got_object_id packid;
+	uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
 	struct imsgbuf ibuf;
 	struct imsg imsg;
 	struct got_pathlist_head have_refs;
@@ -1310,7 +1309,7 @@ main(int argc, char **argv)
 	}
 	packfd = imsg.fd;
 
-	err = fetch_pack(fetchfd, packfd, &packid, &have_refs,
+	err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
 	    fetch_req.fetch_all_branches, &wanted_branches,
 	    &wanted_refs, fetch_req.list_refs_only, &ibuf);
 done:
@@ -1329,7 +1328,7 @@ done:
 	if (err != NULL)
 		got_privsep_send_error(&ibuf, err);
 	else
-		err = send_fetch_done(&ibuf, packid);
+		err = send_fetch_done(&ibuf, pack_sha1);
 	if (err != NULL) {
 		fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
 		got_privsep_send_error(&ibuf, err);