Blob


1 /*
2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sha1.h>
23 #include <zlib.h>
24 #include <ctype.h>
25 #include <limits.h>
27 #include "got_error.h"
28 #include "got_object.h"
29 #include "got_repository.h"
30 #include "got_sha1.h"
32 #ifndef MIN
33 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
34 #endif
36 #ifndef nitems
37 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
38 #endif
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 "
49 const char *
50 got_object_id_str(struct got_object_id *id, char *buf, size_t size)
51 {
52 char *p = buf;
53 char hex[3];
54 int i;
56 if (size < SHA1_DIGEST_STRING_LENGTH)
57 return NULL;
59 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
60 snprintf(hex, sizeof(hex), "%.2x", id->sha1[i]);
61 p[0] = hex[0];
62 p[1] = hex[1];
63 p += 2;
64 }
65 p[0] = '\0';
67 return buf;
68 }
70 struct got_zstream_buf {
71 z_stream z;
72 char *inbuf;
73 size_t inlen;
74 char *outbuf;
75 size_t outlen;
76 int flags;
77 #define GOT_ZSTREAM_F_HAVE_MORE 0x01
78 };
80 static void
81 inflate_end(struct got_zstream_buf *zb)
82 {
83 free(zb->inbuf);
84 free(zb->outbuf);
85 inflateEnd(&zb->z);
86 }
88 static const struct got_error *
89 inflate_init(struct got_zstream_buf *zb, size_t bufsize)
90 {
91 const struct got_error *err = NULL;
93 memset(zb, 0, sizeof(*zb));
95 zb->z.zalloc = Z_NULL;
96 zb->z.zfree = Z_NULL;
97 if (inflateInit(&zb->z) != Z_OK) {
98 err = got_error(GOT_ERR_IO);
99 goto done;
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);
107 goto done;
110 zb->outbuf = calloc(1, zb->outlen);
111 if (zb->outbuf == NULL) {
112 err = got_error(GOT_ERR_NO_MEM);
113 goto done;
116 done:
117 if (err)
118 inflate_end(zb);
119 return err;
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;
127 int n, ret;
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) {
133 int i;
134 n = fread(zb->inbuf, 1, zb->inlen, f);
135 if (n == 0) {
136 if (ferror(f))
137 return got_error(GOT_ERR_IO);
138 *outlenp = 0;
139 return NULL;
141 z->next_in = zb->inbuf;
142 z->avail_in = n;
145 ret = inflate(z, Z_SYNC_FLUSH);
146 if (ret == Z_OK) {
147 if (z->avail_out == 0)
148 zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
149 else
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;
155 return NULL;
158 static const struct got_error *
159 parse_object_header(struct got_object **obj, char *buf, size_t len)
161 const char *obj_tags[] = {
162 GOT_OBJ_TAG_COMMIT,
163 GOT_OBJ_TAG_TREE,
164 GOT_OBJ_TAG_BLOB
165 };
166 const int obj_types[] = {
167 GOT_OBJ_TYPE_COMMIT,
168 GOT_OBJ_TYPE_TREE,
169 GOT_OBJ_TYPE_BLOB,
170 };
171 int type = 0;
172 size_t size = 0, hdrlen = 0;
173 int i;
174 char *p = strchr(buf, '\0');
176 if (p == NULL)
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);
184 const char *errstr;
186 if (strncmp(buf, tag, tlen) != 0)
187 continue;
189 type = obj_types[i];
190 if (len <= tlen)
191 return got_error(GOT_ERR_BAD_OBJ_HDR);
192 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
193 if (errstr != NULL)
194 return got_error(GOT_ERR_BAD_OBJ_HDR);
195 break;
198 if (type == 0)
199 return got_error(GOT_ERR_BAD_OBJ_HDR);
201 *obj = calloc(1, sizeof(**obj));
202 (*obj)->type = type;
203 (*obj)->hdrlen = hdrlen;
204 (*obj)->size = size;
205 return NULL;
208 static const struct got_error *
209 read_object_header(struct got_object **obj, struct got_repository *repo,
210 const char *path)
212 const struct got_error *err;
213 FILE *f;
214 struct got_zstream_buf zb;
215 size_t outlen;
216 int i, ret;
218 f = fopen(path, "rb");
219 if (f == NULL)
220 return got_error(GOT_ERR_BAD_PATH);
222 err = inflate_init(&zb, 64);
223 if (err) {
224 fclose(f);
225 return err;
228 err = inflate_read(&zb, f, &outlen);
229 if (err)
230 goto done;
232 err = parse_object_header(obj, zb.outbuf, outlen);
233 done:
234 inflate_end(&zb);
235 fclose(f);
236 return err;
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);
256 free(path_objects);
257 return err;
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;
265 char *path = NULL;
267 err = object_path(&path, id, repo);
268 if (err)
269 return err;
271 err = read_object_header(obj, repo, path);
272 if (err == NULL)
273 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
274 done:
275 free(path);
276 return err;
279 void
280 got_object_close(struct got_object *obj)
282 free(obj);
285 static int
286 commit_object_valid(struct got_commit_object *commit)
288 int i;
289 int n;
291 if (commit == NULL)
292 return 0;
294 n = 0;
295 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
296 if (commit->tree_id.sha1[i] == 0)
297 n++;
299 if (n == SHA1_DIGEST_LENGTH)
300 return 0;
302 return 1;
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;
309 char *s = buf;
310 size_t tlen;
311 ssize_t remain = (ssize_t)len;
313 *commit = calloc(1, sizeof(**commit));
314 if (*commit == NULL)
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) {
321 remain -= tlen;
322 if (remain < SHA1_DIGEST_STRING_LENGTH) {
323 err = got_error(GOT_ERR_BAD_OBJ_DATA);
324 goto done;
326 s += tlen;
327 if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
328 err = got_error(GOT_ERR_BAD_OBJ_DATA);
329 goto done;
331 remain -= SHA1_DIGEST_STRING_LENGTH;
332 s += SHA1_DIGEST_STRING_LENGTH;
333 } else {
334 err = got_error(GOT_ERR_BAD_OBJ_DATA);
335 goto done;
338 tlen = strlen(GOT_COMMIT_TAG_PARENT);
339 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
340 struct got_parent_id *pid;
342 remain -= tlen;
343 if (remain < SHA1_DIGEST_STRING_LENGTH) {
344 err = got_error(GOT_ERR_BAD_OBJ_DATA);
345 goto done;
348 pid = calloc(1, sizeof(*pid));
349 if (pid == NULL) {
350 err = got_error(GOT_ERR_NO_MEM);
351 goto done;
353 s += tlen;
354 if (!got_parse_sha1_digest(pid->id.sha1, s)) {
355 err = got_error(GOT_ERR_BAD_OBJ_DATA);
356 goto done;
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) {
366 char *p;
368 remain -= tlen;
369 if (remain <= 0) {
370 err = got_error(GOT_ERR_BAD_OBJ_DATA);
371 goto done;
373 s += tlen;
374 p = strchr(s, '\n');
375 if (p == NULL) {
376 err = got_error(GOT_ERR_BAD_OBJ_DATA);
377 goto done;
379 *p = '\0';
380 (*commit)->author = strdup(s);
381 if ((*commit)->author == NULL) {
382 err = got_error(GOT_ERR_NO_MEM);
383 goto done;
385 s += strlen((*commit)->author) + 1;
388 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
389 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
390 char *p;
392 remain -= tlen;
393 if (remain <= 0) {
394 err = got_error(GOT_ERR_BAD_OBJ_DATA);
395 goto done;
397 s += tlen;
398 p = strchr(s, '\n');
399 if (p == NULL) {
400 err = got_error(GOT_ERR_BAD_OBJ_DATA);
401 goto done;
403 *p = '\0';
404 (*commit)->committer = strdup(s);
405 if ((*commit)->committer == NULL) {
406 err = got_error(GOT_ERR_NO_MEM);
407 goto done;
409 s += strlen((*commit)->committer) + 1;
412 (*commit)->logmsg = strdup(s);
413 done:
414 if (err)
415 got_object_commit_close(*commit);
416 return err;
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;
424 FILE *f;
425 struct got_zstream_buf zb;
426 size_t len;
427 char *p;
428 int i, ret;
430 f = fopen(path, "rb");
431 if (f == NULL)
432 return got_error(GOT_ERR_BAD_PATH);
434 err = inflate_init(&zb, 8192);
435 if (err) {
436 fclose(f);
437 return err;
440 do {
441 err = inflate_read(&zb, f, &len);
442 if (err || len == 0)
443 break;
444 } while (len < obj->hdrlen + obj->size);
446 if (len < obj->hdrlen + obj->size) {
447 err = got_error(GOT_ERR_BAD_OBJ_DATA);
448 goto done;
451 /* Skip object header. */
452 len -= obj->hdrlen;
453 err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
454 done:
455 inflate_end(&zb);
456 fclose(f);
457 return err;
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;
465 char *path = 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);
471 if (err)
472 return err;
474 err = read_commit_object(commit, repo, obj, path);
475 free(path);
476 return err;
479 void
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);
487 free(pid);
490 free(commit->author);
491 free(commit->committer);
492 free(commit->logmsg);
493 free(commit);