parse object header
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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
diff --git a/include/got_error.h b/include/got_error.h
index b621824..3250730 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -21,6 +21,11 @@
#define GOT_ERR_NOT_ABSPATH 0x0003
#define GOT_ERR_BAD_PATH 0x0004
#define GOT_ERR_NOT_REF 0x0005
+#define GOT_ERR_IO 0x0006
+#define GOT_ERR_EOF 0x0007
+#define GOT_ERR_DECOMPRESSION 0x0008
+#define GOT_ERR_NO_SPACE 0x0009
+#define GOT_ERR_BAD_OBJ_HDR 0x0010
static const struct got_error {
int code;
@@ -32,6 +37,11 @@ static const struct got_error {
{ GOT_ERR_NOT_ABSPATH, "absolute path expected" },
{ GOT_ERR_BAD_PATH, "bad path" },
{ GOT_ERR_NOT_REF, "no such reference found" },
+ { GOT_ERR_IO, "input/output error" },
+ { GOT_ERR_EOF, "unexpected end of file" },
+ { GOT_ERR_DECOMPRESSION,"decompression failed" },
+ { GOT_ERR_NO_SPACE, "buffer too small" },
+ { GOT_ERR_BAD_OBJ_HDR, "bad object header" },
};
const struct got_error * got_error(int code);
diff --git a/include/got_object.h b/include/got_object.h
index fdfff2e..e0280c7 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -18,4 +18,19 @@ struct got_object_id {
u_int8_t sha1[SHA1_DIGEST_LENGTH];
};
+struct got_object {
+ int type;
+#define GOT_OBJ_TYPE_COMMIT 1
+#define GOT_OBJ_TYPE_TREE 2
+#define GOT_OBJ_TYPE_BLOB 3
+
+ size_t size;
+ struct got_object_id id;
+};
+
+struct got_repository;
+
const char * got_object_id_str(struct got_object_id *, char *, size_t);
+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/object.c b/lib/object.c
index 4e9d9cf..ff3a565 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -15,9 +15,28 @@
*/
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <sha1.h>
+#include <zlib.h>
+#include <ctype.h>
+#include <limits.h>
+#include "got_error.h"
#include "got_object.h"
+#include "got_repository.h"
+
+#ifndef MIN
+#define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
+#endif
+
+#ifndef nitems
+#define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
+#endif
+
+#define GOT_OBJ_TAG_COMMIT "commit"
+#define GOT_OBJ_TAG_TREE "tree"
+#define GOT_OBJ_TAG_BLOB "blob"
const char *
got_object_id_str(struct got_object_id *id, char *buf, size_t size)
@@ -39,3 +58,205 @@ got_object_id_str(struct got_object_id *id, char *buf, size_t size)
return buf;
}
+
+struct got_zstream_buf {
+ z_stream z;
+ char *inbuf;
+ size_t inlen;
+ char *outbuf;
+ size_t outlen;
+ int flags;
+#define GOT_ZSTREAM_F_HAVE_MORE 0x01
+};
+
+static void
+inflate_end(struct got_zstream_buf *zb)
+{
+ free(zb->inbuf);
+ free(zb->outbuf);
+ inflateEnd(&zb->z);
+}
+
+static const struct got_error *
+inflate_init(struct got_zstream_buf *zb, size_t bufsize)
+{
+ const struct got_error *err = NULL;
+
+ memset(zb, 0, sizeof(*zb));
+
+ zb->z.zalloc = Z_NULL;
+ zb->z.zfree = Z_NULL;
+ if (inflateInit(&zb->z) != Z_OK) {
+ err = got_error(GOT_ERR_IO);
+ goto done;
+ }
+
+ zb->inlen = zb->outlen = bufsize;
+
+ zb->inbuf = calloc(1, zb->inlen);
+ if (zb->inbuf == NULL) {
+ err = got_error(GOT_ERR_NO_MEM);
+ goto done;
+ }
+
+ zb->outbuf = calloc(1, zb->outlen);
+ if (zb->outbuf == NULL) {
+ err = got_error(GOT_ERR_NO_MEM);
+ goto done;
+ }
+
+done:
+ if (err)
+ inflate_end(zb);
+ return err;
+}
+
+static const struct got_error *
+inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
+{
+ size_t last_total_out = zb->z.total_out;
+ z_stream *z = &zb->z;
+ int n, ret;
+
+ z->next_out = zb->outbuf;
+ z->avail_out = zb->outlen;
+
+ if (z->avail_in == 0 && (zb->flags & GOT_ZSTREAM_F_HAVE_MORE) == 0) {
+ int i;
+ n = fread(zb->inbuf, 1, zb->inlen, f);
+ if (n == 0) {
+ if (ferror(f))
+ return got_error(GOT_ERR_IO);
+ *outlenp = 0;
+ return NULL;
+ }
+ z->next_in = zb->inbuf;
+ z->avail_in = n;
+ }
+
+ ret = inflate(z, Z_SYNC_FLUSH);
+ if (ret == Z_OK) {
+ if (z->avail_out == 0)
+ zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
+ else
+ zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
+ } else if (ret != Z_STREAM_END)
+ return got_error(GOT_ERR_DECOMPRESSION);
+
+ *outlenp = z->total_out - last_total_out;
+ return NULL;
+}
+
+static const struct got_error *
+parse_obj_header(struct got_object **obj, char *buf, size_t len)
+{
+ const char *obj_tags[] = {
+ GOT_OBJ_TAG_COMMIT,
+ GOT_OBJ_TAG_TREE,
+ GOT_OBJ_TAG_BLOB
+ };
+ const int obj_types[] = {
+ GOT_OBJ_TYPE_COMMIT,
+ GOT_OBJ_TYPE_TREE,
+ GOT_OBJ_TYPE_BLOB,
+ };
+ int type = 0;
+ size_t size = 0;
+ int i;
+ char *p = strchr(buf, '\0');
+
+ if (p == NULL)
+ return got_error(GOT_ERR_BAD_OBJ_HDR);
+
+ for (i = 0; i < nitems(obj_tags); i++) {
+ const char *tag = obj_tags[i];
+ const char *errstr;
+
+ if (strncmp(buf, tag, strlen(tag)) != 0)
+ continue;
+
+ type = obj_types[i];
+ if (len <= strlen(tag))
+ return got_error(GOT_ERR_BAD_OBJ_HDR);
+ size = strtonum(buf + strlen(tag), 0, LONG_MAX, &errstr);
+ if (errstr != NULL)
+ return got_error(GOT_ERR_BAD_OBJ_HDR);
+ break;
+ }
+
+ if (type == 0)
+ return got_error(GOT_ERR_BAD_OBJ_HDR);
+
+ *obj = calloc(1, sizeof(**obj));
+ (*obj)->type = type;
+ (*obj)->size = size;
+ return NULL;
+}
+
+static const struct got_error *
+read_object_header(struct got_object **obj, struct got_repository *repo,
+ const char *path)
+{
+ const struct got_error *err;
+ FILE *f;
+ struct got_zstream_buf zb;
+ char *p;
+ size_t outlen;
+ int i, ret;
+
+ f = fopen(path, "rb");
+ if (f == NULL)
+ return got_error(GOT_ERR_BAD_PATH);
+
+ err = inflate_init(&zb, 64);
+ if (err) {
+ fclose(f);
+ return err;
+ }
+
+ err = inflate_read(&zb, f, &outlen);
+ if (err)
+ goto done;
+
+ err = parse_obj_header(obj, zb.outbuf, outlen);
+done:
+ inflate_end(&zb);
+ fclose(f);
+ return err;
+}
+
+const struct got_error *
+got_object_open(struct got_object **obj, struct got_repository *repo,
+ struct got_object_id *id)
+{
+ const struct got_error *err = NULL;
+ char *path_objects = got_repo_get_path_objects(repo);
+ char hex[SHA1_DIGEST_STRING_LENGTH];
+ char *path = NULL;
+
+ if (path_objects == NULL)
+ return got_error(GOT_ERR_NO_MEM);
+
+ got_object_id_str(id, hex, sizeof(hex));
+
+ if (asprintf(&path, "%s/%.2x/%s",
+ path_objects, id->sha1[0], hex + 2) == -1) {
+ err = got_error(GOT_ERR_NO_MEM);
+ goto done;
+ }
+
+ err = read_object_header(obj, repo, path);
+ if (err == NULL)
+ memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
+
+done:
+ free(path);
+ free(path_objects);
+ return err;
+}
+
+void
+got_object_close(struct got_object *obj)
+{
+ free(obj);
+}
diff --git a/regress/repository/Makefile b/regress/repository/Makefile
index 4a4a51f..091f43b 100644
--- a/regress/repository/Makefile
+++ b/regress/repository/Makefile
@@ -4,7 +4,7 @@ PROG = repository_test
SRCS = path.c repository.c error.c refs.c object.c repository_test.c
CPPFLAGS = -I${.CURDIR}/../../include
-LDADD = -lutil
+LDADD = -lutil -lz
NOMAN = yes
diff --git a/regress/repository/repository_test.c b/regress/repository/repository_test.c
index eb48aec..aeb8b92 100644
--- a/regress/repository/repository_test.c
+++ b/regress/repository/repository_test.c
@@ -29,12 +29,13 @@
#define GOT_REPO_PATH "../../../"
static int
-repo_resolve_head_ref(const char *repo_path)
+repo_read_object_header(const char *repo_path)
{
const struct got_error *err;
struct got_repository *repo;
struct got_reference *head_ref;
struct got_object_id *id;
+ struct got_object *obj;
char buf[SHA1_DIGEST_STRING_LENGTH];
int ret;
@@ -48,6 +49,11 @@ repo_resolve_head_ref(const char *repo_path)
if (err != NULL || head_ref == NULL)
return 0;
printf("HEAD is at %s\n", got_object_id_str(id, buf, sizeof(buf)));
+ err = got_object_open(&obj, repo, id);
+ if (err != NULL || obj == NULL)
+ return 0;
+ printf("object type=%d size=%lu\n", obj->type, obj->size);
+ got_object_close(obj);
free(id);
got_ref_close(head_ref);
got_repo_close(repo);
@@ -69,7 +75,7 @@ main(int argc, const char *argv[])
return 1;
}
- RUN_TEST(repo_resolve_head_ref(repo_path), "resolve_head_ref");
+ RUN_TEST(repo_read_object_header(repo_path), "read_object_header");
return failure ? 1 : 0;
}