Commit f934cf2c63fe7f1b40d3c217c01a8c6b6556e670

Stefan Sperling 2018-02-12T21:27:40

make struct got_blob_object opaque

diff --git a/include/got_object.h b/include/got_object.h
index 339b6c1..7926d57 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -28,16 +28,7 @@ struct got_object_id {
 	u_int8_t sha1[SHA1_DIGEST_LENGTH];
 };
 
-struct got_blob_object {
-	FILE *f;
-	struct got_zstream_buf zb;
-	size_t hdrlen;
-	size_t blocksize;
-	uint8_t *read_buf;
-	int flags;
-#define GOT_BLOB_F_COMPRESSED	0x01
-	struct got_object_id id;
-};
+struct got_blob_object;
 
 struct got_tree_entry {
 	SIMPLEQ_ENTRY(got_tree_entry) entry;
@@ -93,5 +84,8 @@ void got_object_tree_close(struct got_tree_object *);
 const struct got_error *got_object_blob_open(struct got_blob_object **,
     struct got_repository *, struct got_object *, size_t);
 void got_object_blob_close(struct got_blob_object *);
+char *got_object_blob_id_str(struct got_blob_object*, char *, size_t);
+size_t got_object_blob_get_hdrlen(struct got_blob_object *);
+const uint8_t *got_object_blob_get_read_buf(struct got_blob_object *);
 const struct got_error *got_object_blob_read_block(size_t *,
     struct got_blob_object *);
diff --git a/lib/diff.c b/lib/diff.c
index 25249ba..0d41719 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -44,6 +44,7 @@ got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
 	char hex2[SHA1_DIGEST_STRING_LENGTH];
 	char *idstr1 = NULL, *idstr2 = NULL;
 	size_t len, hdrlen;
+	size_t size1, size2;
 	int res, flags = 0;
 
 	if (blob1) {
@@ -62,33 +63,37 @@ got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
 	} else
 		flags |= D_EMPTY2;
 
+	size1 = 0;
 	if (blob1) {
-		idstr1 = got_object_id_str(&blob1->id, hex1, sizeof(hex1));
-		hdrlen = blob1->hdrlen;
+		idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
+		hdrlen = got_object_blob_get_hdrlen(blob1);
 		do {
 			err = got_object_blob_read_block(&len, blob1);
 			if (err)
 				goto done;
 			if (len == 0)
 				break;
+			size1 += len;
 			/* Skip blob object header first time around. */
-			fwrite(blob1->read_buf + hdrlen, len - hdrlen, 1, f1);
+			fwrite(got_object_blob_get_read_buf(blob1) + hdrlen, len - hdrlen, 1, f1);
 			hdrlen = 0;
 		} while (len != 0);
 	} else
 		idstr1 = "/dev/null";
 
+	size2 = 0;
 	if (blob2) {
-		idstr2 = got_object_id_str(&blob2->id, hex2, sizeof(hex2));
-		hdrlen = blob2->hdrlen;
+		idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
+		hdrlen = got_object_blob_get_hdrlen(blob2);
 		do {
 			err = got_object_blob_read_block(&len, blob2);
 			if (err)
 				goto done;
 			if (len == 0)
 				break;
+			size2 += len;
 			/* Skip blob object header first time around. */
-			fwrite(blob2->read_buf + hdrlen, len - hdrlen, 1, f2);
+			fwrite(got_object_blob_get_read_buf(blob2) + hdrlen, len - hdrlen, 1, f2);
 			hdrlen = 0;
 		} while (len != 0);
 	} else
@@ -107,12 +112,12 @@ got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
 	/* XXX should stat buffers be passed in args instead of ds? */
 	ds.stb1.st_mode = S_IFREG;
 	if (blob1)
-		ds.stb1.st_size = blob1->zb.z.total_out;
+		ds.stb1.st_size = size1;
 	ds.stb1.st_mtime = 0; /* XXX */
 
 	ds.stb2.st_mode = S_IFREG;
 	if (blob2)
-		ds.stb2.st_size = blob2->zb.z.total_out;
+		ds.stb2.st_size = size2;
 	ds.stb2.st_mtime = 0; /* XXX */
 
 	memset(&args, 0, sizeof(args));
diff --git a/lib/object.c b/lib/object.c
index 273788d..d9ff8c0 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -741,6 +741,24 @@ got_object_blob_close(struct got_blob_object *blob)
 	free(blob);
 }
 
+char *
+got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
+{
+	return got_sha1_digest_to_str(blob->id.sha1, buf, size);
+}
+
+size_t
+got_object_blob_get_hdrlen(struct got_blob_object *blob)
+{
+	return blob->hdrlen;
+}
+
+const uint8_t *
+got_object_blob_get_read_buf(struct got_blob_object *blob)
+{
+	return blob->read_buf;
+}
+
 const struct got_error *
 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
 {
diff --git a/lib/object.h b/lib/object.h
index 7d64bfa..9b27d1b 100644
--- a/lib/object.h
+++ b/lib/object.h
@@ -28,3 +28,14 @@ struct got_object {
 	off_t pack_offset;	/* if packed */
 	struct got_delta_chain deltas; /* if deltified */
 };
+
+struct got_blob_object {
+	FILE *f;
+	struct got_zstream_buf zb;
+	size_t hdrlen;
+	size_t blocksize;
+	uint8_t *read_buf;
+	int flags;
+#define GOT_BLOB_F_COMPRESSED	0x01
+	struct got_object_id id;
+};
diff --git a/regress/repository/repository_test.c b/regress/repository/repository_test.c
index 9fc0d8e..0b04bd6 100644
--- a/regress/repository/repository_test.c
+++ b/regress/repository/repository_test.c
@@ -268,11 +268,12 @@ repo_read_blob(const char *repo_path)
 
 	test_printf("\n");
 	do {
+		const uint8_t *buf = got_object_blob_get_read_buf(blob);
 		err = got_object_blob_read_block(&len, blob);
 		if (err)
 			break;
 		for (i = 0; i < len; i++)
-			test_printf("%c", blob->read_buf[i]);
+			test_printf("%c", buf[i]);
 	} while (len != 0);
 	test_printf("\n");