2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
27 #include "got_error.h"
28 #include "got_object.h"
29 #include "got_repository.h"
33 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
37 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
40 #define GOT_OBJ_TAG_COMMIT "commit"
41 #define GOT_OBJ_TAG_TREE "tree"
42 #define GOT_OBJ_TAG_BLOB "blob"
44 #define GOT_COMMIT_TAG_TREE "tree "
45 #define GOT_COMMIT_TAG_PARENT "parent "
46 #define GOT_COMMIT_TAG_AUTHOR "author "
47 #define GOT_COMMIT_TAG_COMMITTER "committer "
50 got_object_id_str(struct got_object_id *id, char *buf, size_t size)
56 if (size < SHA1_DIGEST_STRING_LENGTH)
59 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
60 snprintf(hex, sizeof(hex), "%.2x", id->sha1[i]);
70 struct got_zstream_buf {
77 #define GOT_ZSTREAM_F_HAVE_MORE 0x01
81 inflate_end(struct got_zstream_buf *zb)
88 static const struct got_error *
89 inflate_init(struct got_zstream_buf *zb, size_t bufsize)
91 const struct got_error *err = NULL;
93 memset(zb, 0, sizeof(*zb));
95 zb->z.zalloc = Z_NULL;
97 if (inflateInit(&zb->z) != Z_OK) {
98 err = got_error(GOT_ERR_IO);
102 zb->inlen = zb->outlen = bufsize;
104 zb->inbuf = calloc(1, zb->inlen);
105 if (zb->inbuf == NULL) {
106 err = got_error(GOT_ERR_NO_MEM);
110 zb->outbuf = calloc(1, zb->outlen);
111 if (zb->outbuf == NULL) {
112 err = got_error(GOT_ERR_NO_MEM);
122 static const struct got_error *
123 inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
125 size_t last_total_out = zb->z.total_out;
126 z_stream *z = &zb->z;
129 z->next_out = zb->outbuf;
130 z->avail_out = zb->outlen;
132 if (z->avail_in == 0 && (zb->flags & GOT_ZSTREAM_F_HAVE_MORE) == 0) {
134 n = fread(zb->inbuf, 1, zb->inlen, f);
137 return got_error(GOT_ERR_IO);
141 z->next_in = zb->inbuf;
145 ret = inflate(z, Z_SYNC_FLUSH);
147 if (z->avail_out == 0)
148 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
150 zb->flags &= ~GOT_ZSTREAM_F_HAVE_MORE;
151 } else if (ret != Z_STREAM_END)
152 return got_error(GOT_ERR_DECOMPRESSION);
154 *outlenp = z->total_out - last_total_out;
158 static const struct got_error *
159 parse_object_header(struct got_object **obj, char *buf, size_t len)
161 const char *obj_tags[] = {
166 const int obj_types[] = {
172 size_t size = 0, hdrlen = 0;
174 char *p = strchr(buf, '\0');
177 return got_error(GOT_ERR_BAD_OBJ_HDR);
179 hdrlen = strlen(buf) + 1 /* '\0' */;
181 for (i = 0; i < nitems(obj_tags); i++) {
182 const char *tag = obj_tags[i];
183 size_t tlen = strlen(tag);
186 if (strncmp(buf, tag, tlen) != 0)
191 return got_error(GOT_ERR_BAD_OBJ_HDR);
192 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
194 return got_error(GOT_ERR_BAD_OBJ_HDR);
199 return got_error(GOT_ERR_BAD_OBJ_HDR);
201 *obj = calloc(1, sizeof(**obj));
203 (*obj)->hdrlen = hdrlen;
208 static const struct got_error *
209 read_object_header(struct got_object **obj, struct got_repository *repo,
212 const struct got_error *err;
214 struct got_zstream_buf zb;
218 f = fopen(path, "rb");
220 return got_error(GOT_ERR_BAD_PATH);
222 err = inflate_init(&zb, 64);
228 err = inflate_read(&zb, f, &outlen);
232 err = parse_object_header(obj, zb.outbuf, outlen);
239 static const struct got_error *
240 object_path(char **path, struct got_object_id *id,
241 struct got_repository *repo)
243 const struct got_error *err = NULL;
244 char hex[SHA1_DIGEST_STRING_LENGTH];
245 char *path_objects = got_repo_get_path_objects(repo);
247 if (path_objects == NULL)
248 return got_error(GOT_ERR_NO_MEM);
250 got_object_id_str(id, hex, sizeof(hex));
252 if (asprintf(path, "%s/%.2x/%s", path_objects,
253 id->sha1[0], hex + 2) == -1)
254 err = got_error(GOT_ERR_NO_MEM);
260 const struct got_error *
261 got_object_open(struct got_object **obj, struct got_repository *repo,
262 struct got_object_id *id)
264 const struct got_error *err = NULL;
267 err = object_path(&path, id, repo);
271 err = read_object_header(obj, repo, path);
273 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
280 got_object_close(struct got_object *obj)
286 commit_object_valid(struct got_commit_object *commit)
295 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
296 if (commit->tree_id.sha1[i] == 0)
299 if (n == SHA1_DIGEST_LENGTH)
305 static const struct got_error *
306 parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
308 const struct got_error *err = NULL;
311 ssize_t remain = (ssize_t)len;
313 *commit = calloc(1, sizeof(**commit));
315 return got_error(GOT_ERR_NO_MEM);
317 SIMPLEQ_INIT(&(*commit)->parent_ids);
319 tlen = strlen(GOT_COMMIT_TAG_TREE);
320 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
322 if (remain < SHA1_DIGEST_STRING_LENGTH) {
323 err = got_error(GOT_ERR_BAD_OBJ_DATA);
327 if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
328 err = got_error(GOT_ERR_BAD_OBJ_DATA);
331 remain -= SHA1_DIGEST_STRING_LENGTH;
332 s += SHA1_DIGEST_STRING_LENGTH;
334 err = got_error(GOT_ERR_BAD_OBJ_DATA);
338 tlen = strlen(GOT_COMMIT_TAG_PARENT);
339 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
340 struct got_parent_id *pid;
343 if (remain < SHA1_DIGEST_STRING_LENGTH) {
344 err = got_error(GOT_ERR_BAD_OBJ_DATA);
348 pid = calloc(1, sizeof(*pid));
350 err = got_error(GOT_ERR_NO_MEM);
354 if (!got_parse_sha1_digest(pid->id.sha1, s)) {
355 err = got_error(GOT_ERR_BAD_OBJ_DATA);
358 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
359 (*commit)->nparents++;
361 s += SHA1_DIGEST_STRING_LENGTH;
364 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
365 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
370 err = got_error(GOT_ERR_BAD_OBJ_DATA);
376 err = got_error(GOT_ERR_BAD_OBJ_DATA);
380 (*commit)->author = strdup(s);
381 if ((*commit)->author == NULL) {
382 err = got_error(GOT_ERR_NO_MEM);
385 s += strlen((*commit)->author) + 1;
388 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
389 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
394 err = got_error(GOT_ERR_BAD_OBJ_DATA);
400 err = got_error(GOT_ERR_BAD_OBJ_DATA);
404 (*commit)->committer = strdup(s);
405 if ((*commit)->committer == NULL) {
406 err = got_error(GOT_ERR_NO_MEM);
409 s += strlen((*commit)->committer) + 1;
412 (*commit)->logmsg = strdup(s);
415 got_object_commit_close(*commit);
419 static const struct got_error *
420 read_commit_object(struct got_commit_object **commit,
421 struct got_repository *repo, struct got_object *obj, const char *path)
423 const struct got_error *err = NULL;
425 struct got_zstream_buf zb;
430 f = fopen(path, "rb");
432 return got_error(GOT_ERR_BAD_PATH);
434 err = inflate_init(&zb, 8192);
441 err = inflate_read(&zb, f, &len);
444 } while (len < obj->hdrlen + obj->size);
446 if (len < obj->hdrlen + obj->size) {
447 err = got_error(GOT_ERR_BAD_OBJ_DATA);
451 /* Skip object header. */
453 err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
460 const struct got_error *
461 got_object_commit_open(struct got_commit_object **commit,
462 struct got_repository *repo, struct got_object *obj)
464 const struct got_error *err = NULL;
467 if (obj->type != GOT_OBJ_TYPE_COMMIT)
468 return got_error(GOT_ERR_OBJ_TYPE);
470 err = object_path(&path, &obj->id, repo);
474 err = read_commit_object(commit, repo, obj, path);
480 got_object_commit_close(struct got_commit_object *commit)
482 struct got_parent_id *pid;
484 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
485 pid = SIMPLEQ_FIRST(&commit->parent_ids);
486 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
490 free(commit->author);
491 free(commit->committer);
492 free(commit->logmsg);