Implement open() support for offset delta objects
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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
diff --git a/include/got_object.h b/include/got_object.h
index 204947b..54b890a 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -80,15 +80,20 @@ struct got_object {
size_t size;
struct got_object_id id;
- char *path_packfile;
- off_t pack_offset;
+ char *path_packfile; /* if packed */
+ off_t pack_offset; /* if packed */
+
+ /* If type is OFFSET_DELTA: */
+ int base_type;
+ uint64_t base_size;
+ off_t base_obj_offset;
};
struct got_repository;
char *got_object_id_str(struct got_object_id *, char *, size_t);
int got_object_id_cmp(struct got_object_id *, struct got_object_id *);
-const char *got_object_get_type_tag(int);
+int got_object_get_type(struct got_object *);
const struct got_error *got_object_open(struct got_object **,
struct got_repository *, struct got_object_id *);
void got_object_close(struct got_object *);
diff --git a/lib/diff.c b/lib/diff.c
index d99ed9f..3bf0c3c 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -232,7 +232,7 @@ diff_added_tree(struct got_object_id *id, struct got_repository *repo)
if (err)
goto done;
- if (treeobj->type != GOT_OBJ_TYPE_TREE) {
+ if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
err = got_error(GOT_ERR_OBJ_TYPE);
goto done;
}
@@ -312,7 +312,7 @@ diff_deleted_tree(struct got_object_id *id, struct got_repository *repo)
if (err)
goto done;
- if (treeobj->type != GOT_OBJ_TYPE_TREE) {
+ if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
err = got_error(GOT_ERR_OBJ_TYPE);
goto done;
}
diff --git a/lib/object.c b/lib/object.c
index 96d336a..9f2bc6f 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -61,19 +61,21 @@ got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
}
-const char *
-got_object_get_type_tag(int type)
+int
+got_object_get_type(struct got_object *obj)
{
- switch (type) {
+ switch (obj->type) {
case GOT_OBJ_TYPE_COMMIT:
- return GOT_OBJ_TAG_COMMIT;
case GOT_OBJ_TYPE_TREE:
- return GOT_OBJ_TAG_TREE;
case GOT_OBJ_TYPE_BLOB:
- return GOT_OBJ_TAG_BLOB;
+ case GOT_OBJ_TYPE_TAG:
+ return obj->type;
+ case GOT_OBJ_TYPE_REF_DELTA:
+ case GOT_OBJ_TYPE_OFFSET_DELTA:
+ return obj->base_type;
}
- return NULL;
+ abort();
}
static void
diff --git a/lib/pack.c b/lib/pack.c
index 3ec264f..a91b594 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -388,6 +388,98 @@ open_plain_object(struct got_object **obj, const char *path_packfile,
}
static const struct got_error *
+decode_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
+{
+ int64_t o = 0;
+ uint8_t offN;
+ size_t n;
+ int i = 0;
+
+ do {
+ /* We do not support offset values which don't fit in 64 bit. */
+ if (i > 8)
+ return got_error(GOT_ERR_NO_SPACE);
+
+ n = fread(&offN, sizeof(offN), 1, packfile);
+ if (n != 1)
+ return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
+
+ if (i == 0)
+ o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
+ else {
+ int j;
+ int64_t v = 128;
+ o <<= 7;
+ o |= (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
+ o += v;
+ for (j = 0; j < i; j++) {
+ v <<= 7;
+ o += v;
+ }
+ }
+ i++;
+ } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
+
+ *offset = o;
+ *len = i * sizeof(offN);
+ return NULL;
+}
+
+static const struct got_error *
+open_offset_delta_object(struct got_object **obj, struct got_repository *repo,
+ const char *path_packfile, FILE *packfile, struct got_object_id *id,
+ off_t offset, size_t size)
+{
+ const struct got_error *err = NULL;
+ int64_t negoffset;
+ size_t negofflen;
+ off_t base_obj_offset;
+ struct got_object *base_obj;
+ struct got_object_id base_id;
+ uint8_t base_type;
+ uint64_t base_size;
+ size_t base_tslen;
+
+ err = decode_negative_offset(&negoffset, &negofflen, packfile);
+ if (err)
+ return err;
+
+ /* Compute the base object's offset (must be in the same pack file). */
+ base_obj_offset = (offset - negoffset);
+ if (base_obj_offset <= 0)
+ return got_error(GOT_ERR_BAD_PACKFILE);
+
+ if (fseeko(packfile, base_obj_offset, SEEK_SET) != 0)
+ return got_error_from_errno();
+
+ err = decode_type_and_size(&base_type, &base_size, &base_tslen,
+ packfile);
+ if (err)
+ return err;
+
+ *obj = calloc(1, sizeof(**obj));
+ if (*obj == NULL)
+ return got_error(GOT_ERR_NO_MEM);
+
+ (*obj)->path_packfile = strdup(path_packfile);
+ if ((*obj)->path_packfile == NULL) {
+ free(*obj);
+ return got_error(GOT_ERR_NO_MEM);
+ }
+ (*obj)->type = GOT_OBJ_TYPE_OFFSET_DELTA;
+ (*obj)->flags = GOT_OBJ_FLAG_PACKED;
+ (*obj)->hdrlen = 0;
+ (*obj)->size = size;
+ memcpy(&(*obj)->id, id, sizeof((*obj)->id));
+ (*obj)->pack_offset = offset;
+ (*obj)->base_type = base_type;
+ (*obj)->base_size = base_size;
+ (*obj)->base_obj_offset = base_obj_offset;
+
+ return NULL;
+}
+
+static const struct got_error *
open_packed_object(struct got_object **obj, struct got_repository *repo,
const char *path_packdir, struct got_packidx_v2_hdr *packidx,
struct got_object_id *id)
@@ -447,9 +539,14 @@ open_packed_object(struct got_object **obj, struct got_repository *repo,
offset + tslen, size);
break;
+ case GOT_OBJ_TYPE_OFFSET_DELTA:
+ err = open_offset_delta_object(obj, repo, path_packfile,
+ packfile, id, offset + tslen, size);
+ break;
+
case GOT_OBJ_TYPE_REF_DELTA:
case GOT_OBJ_TYPE_TAG:
- case GOT_OBJ_TYPE_OFFSET_DELTA:
+ break;
default:
err = got_error(GOT_ERR_NOT_IMPL);
goto done;
@@ -547,7 +644,7 @@ dump_ref_delta_object(struct got_repository *repo, FILE *infile, uint8_t type,
const struct got_error *err = NULL;
struct got_object_id base_id;
struct got_object *base_obj;
- int n;
+ size_t n;
if (size < sizeof(base_id))
return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
diff --git a/lib/pack.h b/lib/pack.h
index 18a6071..189b564 100644
--- a/lib/pack.h
+++ b/lib/pack.h
@@ -107,6 +107,9 @@ struct got_packfile_object_data_offset_delta {
* 2^14 + ... + 2^(7 * (n-1)) is added to the result.
*/
uint8_t *offset; /* variable length */
+#define GOT_PACK_OBJ_DELTA_OFF_MORE 0x80
+#define GOT_PACK_OBJ_DELTA_OFF_VAL_MASK 0x7f
+ uint8_t *delta_data; /* compressed */
};
struct got_packfile_obj_data {
diff --git a/regress/repository/repository_test.c b/regress/repository/repository_test.c
index 8fd3099..6495ec4 100644
--- a/regress/repository/repository_test.c
+++ b/regress/repository/repository_test.c
@@ -49,7 +49,7 @@ print_parent_commits(struct got_commit_object *commit,
err = got_object_open(&obj, repo, &pid->id);
if (err != NULL)
return err;
- if (obj->type != GOT_OBJ_TYPE_COMMIT)
+ if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
return got_error(GOT_ERR_OBJ_TYPE);
print_commit_object(obj, repo);
got_object_close(obj);
@@ -89,7 +89,7 @@ print_tree_object(struct got_object *obj, char *parent,
if (err != NULL)
break;
- if (treeobj->type != GOT_OBJ_TYPE_TREE) {
+ if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
err = got_error(GOT_ERR_OBJ_TYPE);
got_object_close(treeobj);
break;
@@ -142,7 +142,7 @@ print_commit_object(struct got_object *obj, struct got_repository *repo)
err = got_object_open(&treeobj, repo, &commit->tree_id);
if (err != NULL)
return err;
- if (treeobj->type == GOT_OBJ_TYPE_TREE) {
+ if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
print_tree_object(treeobj, "", repo);
printf("\n");
}
@@ -178,7 +178,7 @@ repo_read_log(const char *repo_path)
err = got_object_open(&obj, repo, id);
if (err != NULL || obj == NULL)
return 0;
- if (obj->type == GOT_OBJ_TYPE_COMMIT)
+ if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
print_commit_object(obj, repo);
got_object_close(obj);
free(id);
@@ -209,7 +209,7 @@ repo_read_blob(const char *repo_path)
err = got_object_open(&obj, repo, &id);
if (err != NULL || obj == NULL)
return 0;
- if (obj->type != GOT_OBJ_TYPE_BLOB)
+ if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
return 0;
err = got_object_blob_open(&blob, repo, obj, 64);