Blob


1 /*
2 * Copyright (c) 2019 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/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdint.h>
29 #include <sha1.h>
30 #include <unistd.h>
31 #include <zlib.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
36 #include "got_opentemp.h"
37 #include "got_path.h"
39 #include "got_lib_sha1.h"
40 #include "got_lib_deflate.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_object.h"
43 #include "got_lib_object_parse.h"
44 #include "got_lib_lockfile.h"
46 #ifndef nitems
47 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
48 #endif
50 static const struct got_error *
51 create_object_file(struct got_object_id *id, FILE *content,
52 struct got_repository *repo)
53 {
54 const struct got_error *err = NULL, *unlock_err = NULL;
55 char *objpath = NULL, *tmppath = NULL;
56 FILE *tmpfile = NULL;
57 struct got_lockfile *lf = NULL;
58 size_t tmplen = 0;
60 err = got_object_get_path(&objpath, id, repo);
61 if (err)
62 return err;
64 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
65 if (err) {
66 char *parent_path;
67 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
68 goto done;
69 err = got_path_dirname(&parent_path, objpath);
70 if (err)
71 goto done;
72 err = got_path_mkdir(parent_path);
73 free(parent_path);
74 if (err)
75 goto done;
76 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
77 if (err)
78 goto done;
79 }
81 err = got_deflate_to_file(&tmplen, content, tmpfile);
82 if (err)
83 goto done;
85 err = got_lockfile_lock(&lf, objpath);
86 if (err)
87 goto done;
89 if (rename(tmppath, objpath) != 0) {
90 err = got_error_from_errno3("rename", tmppath, objpath);
91 goto done;
92 }
93 free(tmppath);
94 tmppath = NULL;
96 if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
97 err = got_error_from_errno2("chmod", objpath);
98 goto done;
99 }
100 done:
101 free(objpath);
102 if (tmppath) {
103 if (unlink(tmppath) != 0 && err == NULL)
104 err = got_error_from_errno2("unlink", tmppath);
105 free(tmppath);
107 if (tmpfile && fclose(tmpfile) != 0 && err == NULL)
108 err = got_error_from_errno("fclose");
109 if (lf)
110 unlock_err = got_lockfile_unlock(lf);
111 return err ? err : unlock_err;
114 const struct got_error *
115 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
116 struct got_repository *repo)
118 const struct got_error *err = NULL;
119 char *header = NULL;
120 FILE *blobfile = NULL;
121 int fd = -1;
122 struct stat sb;
123 SHA1_CTX sha1_ctx;
124 size_t headerlen = 0, n;
126 *id = NULL;
128 SHA1Init(&sha1_ctx);
130 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
131 if (fd == -1)
132 return got_error_from_errno2("open", ondisk_path);
134 if (fstat(fd, &sb) == -1) {
135 err = got_error_from_errno2("fstat", ondisk_path);
136 goto done;
139 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
140 sb.st_size) == -1) {
141 err = got_error_from_errno("asprintf");
142 goto done;
144 headerlen = strlen(header) + 1;
145 SHA1Update(&sha1_ctx, header, headerlen);
147 blobfile = got_opentemp();
148 if (blobfile == NULL) {
149 err = got_error_from_errno("got_opentemp");
150 goto done;
153 n = fwrite(header, 1, headerlen, blobfile);
154 if (n != headerlen) {
155 err = got_ferror(blobfile, GOT_ERR_IO);
156 goto done;
158 for (;;) {
159 char buf[8192];
160 ssize_t inlen;
162 inlen = read(fd, buf, sizeof(buf));
163 if (inlen == -1) {
164 err = got_error_from_errno("read");
165 goto done;
167 if (inlen == 0)
168 break; /* EOF */
169 SHA1Update(&sha1_ctx, buf, inlen);
170 n = fwrite(buf, 1, inlen, blobfile);
171 if (n != inlen) {
172 err = got_ferror(blobfile, GOT_ERR_IO);
173 goto done;
177 *id = malloc(sizeof(**id));
178 if (*id == NULL) {
179 err = got_error_from_errno("malloc");
180 goto done;
182 SHA1Final((*id)->sha1, &sha1_ctx);
184 if (fflush(blobfile) != 0) {
185 err = got_error_from_errno("fflush");
186 goto done;
188 rewind(blobfile);
190 err = create_object_file(*id, blobfile, repo);
191 done:
192 free(header);
193 if (fd != -1 && close(fd) != 0 && err == NULL)
194 err = got_error_from_errno("close");
195 if (blobfile && fclose(blobfile) != 0 && err == NULL)
196 err = got_error_from_errno("fclose");
197 if (err) {
198 free(*id);
199 *id = NULL;
201 return err;
204 static const struct got_error *
205 mode2str(char *buf, size_t len, mode_t mode)
207 int ret;
208 ret = snprintf(buf, len, "%o ", mode);
209 if (ret == -1 || ret >= len)
210 return got_error(GOT_ERR_NO_SPACE);
211 return NULL;
214 /*
215 * Git expects directory tree entries to be sorted with an imaginary slash
216 * appended to their name, and will break otherwise. Let's be nice.
217 * This function is intended to be used with mergesort(3) to sort an
218 * array of pointers to struct got_tree_entry objects.
219 */
220 static int
221 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
223 struct got_tree_entry * const *te1 = arg1;
224 struct got_tree_entry * const *te2 = arg2;
225 char name1[NAME_MAX + 2];
226 char name2[NAME_MAX + 2];
228 strlcpy(name1, (*te1)->name, sizeof(name1));
229 strlcpy(name2, (*te2)->name, sizeof(name2));
230 if (S_ISDIR((*te1)->mode))
231 strlcat(name1, "/", sizeof(name1));
232 if (S_ISDIR((*te2)->mode))
233 strlcat(name2, "/", sizeof(name2));
234 return strcmp(name1, name2);
237 const struct got_error *
238 got_object_tree_create(struct got_object_id **id,
239 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
241 const struct got_error *err = NULL;
242 char modebuf[sizeof("100644 ")];
243 SHA1_CTX sha1_ctx;
244 char *header = NULL;
245 size_t headerlen, len = 0, n;
246 FILE *treefile = NULL;
247 struct got_pathlist_entry *pe;
248 struct got_tree_entry **sorted_entries;
249 struct got_tree_entry *te;
250 int i;
252 *id = NULL;
254 SHA1Init(&sha1_ctx);
256 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
257 if (sorted_entries == NULL)
258 return got_error_from_errno("calloc");
260 i = 0;
261 TAILQ_FOREACH(pe, paths, entry)
262 sorted_entries[i++] = pe->data;
263 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
264 sort_tree_entries_the_way_git_likes_it);
266 for (i = 0; i < nentries; i++) {
267 te = sorted_entries[i];
268 err = mode2str(modebuf, sizeof(modebuf), te->mode);
269 if (err)
270 goto done;
271 len += strlen(modebuf) + strlen(te->name) + 1 +
272 SHA1_DIGEST_LENGTH;
275 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
276 err = got_error_from_errno("asprintf");
277 goto done;
279 headerlen = strlen(header) + 1;
280 SHA1Update(&sha1_ctx, header, headerlen);
282 treefile = got_opentemp();
283 if (treefile == NULL) {
284 err = got_error_from_errno("got_opentemp");
285 goto done;
288 n = fwrite(header, 1, headerlen, treefile);
289 if (n != headerlen) {
290 err = got_ferror(treefile, GOT_ERR_IO);
291 goto done;
294 for (i = 0; i < nentries; i++) {
295 te = sorted_entries[i];
296 err = mode2str(modebuf, sizeof(modebuf), te->mode);
297 if (err)
298 goto done;
299 len = strlen(modebuf);
300 n = fwrite(modebuf, 1, len, treefile);
301 if (n != len) {
302 err = got_ferror(treefile, GOT_ERR_IO);
303 goto done;
305 SHA1Update(&sha1_ctx, modebuf, len);
307 len = strlen(te->name) + 1; /* must include NUL */
308 n = fwrite(te->name, 1, len, treefile);
309 if (n != len) {
310 err = got_ferror(treefile, GOT_ERR_IO);
311 goto done;
313 SHA1Update(&sha1_ctx, te->name, len);
315 len = SHA1_DIGEST_LENGTH;
316 n = fwrite(te->id.sha1, 1, len, treefile);
317 if (n != len) {
318 err = got_ferror(treefile, GOT_ERR_IO);
319 goto done;
321 SHA1Update(&sha1_ctx, te->id.sha1, len);
324 *id = malloc(sizeof(**id));
325 if (*id == NULL) {
326 err = got_error_from_errno("malloc");
327 goto done;
329 SHA1Final((*id)->sha1, &sha1_ctx);
331 if (fflush(treefile) != 0) {
332 err = got_error_from_errno("fflush");
333 goto done;
335 rewind(treefile);
337 err = create_object_file(*id, treefile, repo);
338 done:
339 free(header);
340 free(sorted_entries);
341 if (treefile && fclose(treefile) != 0 && err == NULL)
342 err = got_error_from_errno("fclose");
343 if (err) {
344 free(*id);
345 *id = NULL;
347 return err;
350 const struct got_error *
351 got_object_commit_create(struct got_object_id **id,
352 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
353 int nparents, const char *author, time_t author_time,
354 const char *committer, time_t committer_time,
355 const char *logmsg, struct got_repository *repo)
357 const struct got_error *err = NULL;
358 SHA1_CTX sha1_ctx;
359 char *header = NULL, *tree_str = NULL;
360 char *author_str = NULL, *committer_str = NULL;
361 char *id_str = NULL;
362 size_t headerlen, len = 0, n;
363 FILE *commitfile = NULL;
364 struct got_object_qid *qid;
365 char *msg0, *msg;
367 *id = NULL;
369 SHA1Init(&sha1_ctx);
371 msg0 = strdup(logmsg);
372 if (msg0 == NULL)
373 return got_error_from_errno("strdup");
374 msg = msg0;
376 while (isspace((unsigned char)msg[0]))
377 msg++;
378 len = strlen(msg);
379 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
380 msg[len - 1] = '\0';
381 len--;
384 if (asprintf(&author_str, "%s%s %lld +0000\n",
385 GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
386 return got_error_from_errno("asprintf");
388 if (asprintf(&committer_str, "%s%s %lld +0000\n",
389 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
390 committer ? committer_time : author_time)
391 == -1) {
392 err = got_error_from_errno("asprintf");
393 goto done;
396 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
397 nparents *
398 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
399 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
401 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
402 err = got_error_from_errno("asprintf");
403 goto done;
405 headerlen = strlen(header) + 1;
406 SHA1Update(&sha1_ctx, header, headerlen);
408 commitfile = got_opentemp();
409 if (commitfile == NULL) {
410 err = got_error_from_errno("got_opentemp");
411 goto done;
414 n = fwrite(header, 1, headerlen, commitfile);
415 if (n != headerlen) {
416 err = got_ferror(commitfile, GOT_ERR_IO);
417 goto done;
420 err = got_object_id_str(&id_str, tree_id);
421 if (err)
422 goto done;
423 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
424 == -1) {
425 err = got_error_from_errno("asprintf");
426 goto done;
428 len = strlen(tree_str);
429 SHA1Update(&sha1_ctx, tree_str, len);
430 n = fwrite(tree_str, 1, len, commitfile);
431 if (n != len) {
432 err = got_ferror(commitfile, GOT_ERR_IO);
433 goto done;
436 if (parent_ids) {
437 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
438 char *parent_str = NULL;
440 free(id_str);
442 err = got_object_id_str(&id_str, qid->id);
443 if (err)
444 goto done;
445 if (asprintf(&parent_str, "%s%s\n",
446 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
447 err = got_error_from_errno("asprintf");
448 goto done;
450 len = strlen(parent_str);
451 SHA1Update(&sha1_ctx, parent_str, len);
452 n = fwrite(parent_str, 1, len, commitfile);
453 if (n != len) {
454 err = got_ferror(commitfile, GOT_ERR_IO);
455 free(parent_str);
456 goto done;
458 free(parent_str);
462 len = strlen(author_str);
463 SHA1Update(&sha1_ctx, author_str, len);
464 n = fwrite(author_str, 1, len, commitfile);
465 if (n != len) {
466 err = got_ferror(commitfile, GOT_ERR_IO);
467 goto done;
470 len = strlen(committer_str);
471 SHA1Update(&sha1_ctx, committer_str, len);
472 n = fwrite(committer_str, 1, len, commitfile);
473 if (n != len) {
474 err = got_ferror(commitfile, GOT_ERR_IO);
475 goto done;
478 SHA1Update(&sha1_ctx, "\n", 1);
479 n = fwrite("\n", 1, 1, commitfile);
480 if (n != 1) {
481 err = got_ferror(commitfile, GOT_ERR_IO);
482 goto done;
485 len = strlen(msg);
486 SHA1Update(&sha1_ctx, msg, len);
487 n = fwrite(msg, 1, len, commitfile);
488 if (n != len) {
489 err = got_ferror(commitfile, GOT_ERR_IO);
490 goto done;
493 SHA1Update(&sha1_ctx, "\n", 1);
494 n = fwrite("\n", 1, 1, commitfile);
495 if (n != 1) {
496 err = got_ferror(commitfile, GOT_ERR_IO);
497 goto done;
500 *id = malloc(sizeof(**id));
501 if (*id == NULL) {
502 err = got_error_from_errno("malloc");
503 goto done;
505 SHA1Final((*id)->sha1, &sha1_ctx);
507 if (fflush(commitfile) != 0) {
508 err = got_error_from_errno("fflush");
509 goto done;
511 rewind(commitfile);
513 err = create_object_file(*id, commitfile, repo);
514 done:
515 free(msg0);
516 free(header);
517 free(tree_str);
518 free(author_str);
519 free(committer_str);
520 if (commitfile && fclose(commitfile) != 0 && err == NULL)
521 err = got_error_from_errno("fclose");
522 if (err) {
523 free(*id);
524 *id = NULL;
526 return err;
529 const struct got_error *
530 got_object_tag_create(struct got_object_id **id,
531 const char *tag_name, struct got_object_id *object_id, const char *tagger,
532 time_t tagger_time, const char *tagmsg, struct got_repository *repo)
534 const struct got_error *err = NULL;
535 SHA1_CTX sha1_ctx;
536 char *header = NULL;
537 char *tag_str = NULL, *tagger_str = NULL;
538 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
539 size_t headerlen, len = 0, n;
540 FILE *tagfile = NULL;
541 char *msg0 = NULL, *msg;
542 const char *obj_type_str;
543 int obj_type;
545 *id = NULL;
547 SHA1Init(&sha1_ctx);
549 err = got_object_id_str(&id_str, object_id);
550 if (err)
551 goto done;
552 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
553 err = got_error_from_errno("asprintf");
554 goto done;
557 err = got_object_get_type(&obj_type, repo, object_id);
558 if (err)
559 goto done;
561 switch (obj_type) {
562 case GOT_OBJ_TYPE_BLOB:
563 obj_type_str = GOT_OBJ_LABEL_BLOB;
564 break;
565 case GOT_OBJ_TYPE_TREE:
566 obj_type_str = GOT_OBJ_LABEL_TREE;
567 break;
568 case GOT_OBJ_TYPE_COMMIT:
569 obj_type_str = GOT_OBJ_LABEL_COMMIT;
570 break;
571 case GOT_OBJ_TYPE_TAG:
572 obj_type_str = GOT_OBJ_LABEL_TAG;
573 break;
574 default:
575 err = got_error(GOT_ERR_OBJ_TYPE);
576 goto done;
579 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
580 obj_type_str) == -1) {
581 err = got_error_from_errno("asprintf");
582 goto done;
585 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
586 err = got_error_from_errno("asprintf");
587 goto done;
590 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
591 GOT_TAG_LABEL_TAGGER, tagger, tagger_time) == -1)
592 return got_error_from_errno("asprintf");
594 msg0 = strdup(tagmsg);
595 if (msg0 == NULL) {
596 err = got_error_from_errno("strdup");
597 goto done;
599 msg = msg0;
601 while (isspace((unsigned char)msg[0]))
602 msg++;
604 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
605 strlen(tagger_str) + 1 + strlen(msg) + 1;
607 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
608 err = got_error_from_errno("asprintf");
609 goto done;
612 headerlen = strlen(header) + 1;
613 SHA1Update(&sha1_ctx, header, headerlen);
615 tagfile = got_opentemp();
616 if (tagfile == NULL) {
617 err = got_error_from_errno("got_opentemp");
618 goto done;
621 n = fwrite(header, 1, headerlen, tagfile);
622 if (n != headerlen) {
623 err = got_ferror(tagfile, GOT_ERR_IO);
624 goto done;
626 len = strlen(obj_str);
627 SHA1Update(&sha1_ctx, obj_str, len);
628 n = fwrite(obj_str, 1, len, tagfile);
629 if (n != len) {
630 err = got_ferror(tagfile, GOT_ERR_IO);
631 goto done;
633 len = strlen(type_str);
634 SHA1Update(&sha1_ctx, type_str, len);
635 n = fwrite(type_str, 1, len, tagfile);
636 if (n != len) {
637 err = got_ferror(tagfile, GOT_ERR_IO);
638 goto done;
641 len = strlen(tag_str);
642 SHA1Update(&sha1_ctx, tag_str, len);
643 n = fwrite(tag_str, 1, len, tagfile);
644 if (n != len) {
645 err = got_ferror(tagfile, GOT_ERR_IO);
646 goto done;
649 len = strlen(tagger_str);
650 SHA1Update(&sha1_ctx, tagger_str, len);
651 n = fwrite(tagger_str, 1, len, tagfile);
652 if (n != len) {
653 err = got_ferror(tagfile, GOT_ERR_IO);
654 goto done;
657 SHA1Update(&sha1_ctx, "\n", 1);
658 n = fwrite("\n", 1, 1, tagfile);
659 if (n != 1) {
660 err = got_ferror(tagfile, GOT_ERR_IO);
661 goto done;
664 len = strlen(msg);
665 SHA1Update(&sha1_ctx, msg, len);
666 n = fwrite(msg, 1, len, tagfile);
667 if (n != len) {
668 err = got_ferror(tagfile, GOT_ERR_IO);
669 goto done;
672 SHA1Update(&sha1_ctx, "\n", 1);
673 n = fwrite("\n", 1, 1, tagfile);
674 if (n != 1) {
675 err = got_ferror(tagfile, GOT_ERR_IO);
676 goto done;
679 *id = malloc(sizeof(**id));
680 if (*id == NULL) {
681 err = got_error_from_errno("malloc");
682 goto done;
684 SHA1Final((*id)->sha1, &sha1_ctx);
686 if (fflush(tagfile) != 0) {
687 err = got_error_from_errno("fflush");
688 goto done;
690 rewind(tagfile);
692 err = create_object_file(*id, tagfile, repo);
693 done:
694 free(msg0);
695 free(header);
696 free(obj_str);
697 free(tagger_str);
698 if (tagfile && fclose(tagfile) != 0 && err == NULL)
699 err = got_error_from_errno("fclose");
700 if (err) {
701 free(*id);
702 *id = NULL;
704 return err;