Commit efd2a263f541b0617f5acd893f91f73edb07e84d

Stefan Sperling 2018-01-19T16:18:44

add some stub code for packed ref-delta objects

diff --git a/lib/delta.c b/lib/delta.c
new file mode 100644
index 0000000..d7921f1
--- /dev/null
+++ b/lib/delta.c
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/queue.h>
+
+#include <stdio.h>
+#include <zlib.h>
+#include <sha1.h>
+
+#include "got_error.h"
+#include "got_repository.h"
+#include "got_object.h"
+
+#include "delta.h"
+
+const struct got_error *
+got_delta_apply(struct got_repository *repo, FILE *infile, size_t size,
+    struct got_object *base_obj, FILE *outfile)
+{
+	return got_error(GOT_ERR_NOT_IMPL);
+}
diff --git a/lib/delta.h b/lib/delta.h
new file mode 100644
index 0000000..88d8a5d
--- /dev/null
+++ b/lib/delta.h
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+const struct got_error *
+got_delta_apply(struct got_repository *, FILE *, size_t, struct got_object *,
+    FILE *);
diff --git a/lib/pack.c b/lib/pack.c
index b7ef020..c4087fb 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -35,6 +35,7 @@
 #include "got_sha1.h"
 #include "pack.h"
 #include "path.h"
+#include "delta.h"
 
 #define GOT_PACK_PREFIX		"pack-"
 #define GOT_PACKFILE_SUFFIX	".pack"
@@ -515,11 +516,11 @@ dump_plain_object(FILE *infile, uint8_t type, size_t size, FILE *outfile)
 
 		n = fread(data, len, 1, infile);
 		if (n != 1)
-			return got_ferror(infile, GOT_ERR_BAD_PACKIDX);
+			return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
 
 		n = fwrite(data, len, 1, outfile);
 		if (n != 1)
-			return got_ferror(outfile, GOT_ERR_BAD_PACKIDX);
+			return got_ferror(outfile, GOT_ERR_IO);
 
 		size -= len;
 	}
@@ -528,6 +529,35 @@ dump_plain_object(FILE *infile, uint8_t type, size_t size, FILE *outfile)
 	return NULL;
 }
 
+static const struct got_error *
+dump_ref_delta_object(struct got_repository *repo, FILE *infile, uint8_t type,
+    size_t size, FILE *outfile)
+{
+	const struct got_error *err = NULL;
+	struct got_object_id base_id;
+	struct got_object *base_obj;
+	int n;
+
+	if (size < sizeof(base_id))
+		return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
+
+	n = fread(&base_id, sizeof(base_id), 1, infile);
+	if (n != 1)
+		return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
+
+	size -= sizeof(base_id);
+	if (size <= 0)
+		return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
+
+	err = got_object_open(&base_obj, repo, &base_id);
+	if (err)
+		return err;
+
+	err = got_delta_apply(repo, infile, size, base_obj, outfile);
+	got_object_close(base_obj);
+	return err;
+}
+
 const struct got_error *
 got_packfile_extract_object(FILE **f, struct got_object *obj,
     struct got_repository *repo)
@@ -562,6 +592,9 @@ got_packfile_extract_object(FILE **f, struct got_object *obj,
 		err = dump_plain_object(packfile, obj->type, obj->size, *f);
 		break;
 	case GOT_OBJ_TYPE_REF_DELTA:
+		err = dump_ref_delta_object(repo, packfile, obj->type,
+		    obj->size, *f);
+		break;
 	case GOT_OBJ_TYPE_TAG:
 	case GOT_OBJ_TYPE_OFFSET_DELTA:
 	default:
diff --git a/regress/repository/Makefile b/regress/repository/Makefile
index 5ed8a9e..c506dc7 100644
--- a/regress/repository/Makefile
+++ b/regress/repository/Makefile
@@ -2,7 +2,7 @@
 
 PROG = repository_test
 SRCS = path.c repository.c error.c refs.c object.c sha1.c diff.c \
-	diffreg.c xmalloc.c pack.c repository_test.c
+	diffreg.c xmalloc.c pack.c delta.c repository_test.c
 
 CPPFLAGS = -I${.CURDIR}/../../include
 LDADD = -lutil -lz