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 te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
207 int ret;
208 mode_t mode;
210 /*
211 * Some Git implementations are picky about modes seen in tree entries.
212 * For best compatibility we normalize the file/directory mode here.
213 * Note that we do not support committing symlinks.
214 */
215 if (S_ISREG(te->mode)) {
216 mode = GOT_DEFAULT_FILE_MODE;
217 if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
218 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
219 } else if (got_object_tree_entry_is_submodule(te))
220 mode = S_IFDIR | S_IFLNK;
221 else if (S_ISDIR(te->mode))
222 mode = S_IFDIR; /* Git leaves all the other bits unset. */
223 else
224 return got_error(GOT_ERR_BAD_FILETYPE);
226 ret = snprintf(buf, len, "%o ", mode);
227 if (ret == -1 || ret >= len)
228 return got_error(GOT_ERR_NO_SPACE);
229 return NULL;
232 /*
233 * Git expects directory tree entries to be sorted with an imaginary slash
234 * appended to their name, and will break otherwise. Let's be nice.
235 * This function is intended to be used with mergesort(3) to sort an
236 * array of pointers to struct got_tree_entry objects.
237 */
238 static int
239 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
241 struct got_tree_entry * const *te1 = arg1;
242 struct got_tree_entry * const *te2 = arg2;
243 char name1[NAME_MAX + 2];
244 char name2[NAME_MAX + 2];
246 strlcpy(name1, (*te1)->name, sizeof(name1));
247 strlcpy(name2, (*te2)->name, sizeof(name2));
248 if (S_ISDIR((*te1)->mode))
249 strlcat(name1, "/", sizeof(name1));
250 if (S_ISDIR((*te2)->mode))
251 strlcat(name2, "/", sizeof(name2));
252 return strcmp(name1, name2);
255 const struct got_error *
256 got_object_tree_create(struct got_object_id **id,
257 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
259 const struct got_error *err = NULL;
260 char modebuf[sizeof("100644 ")];
261 SHA1_CTX sha1_ctx;
262 char *header = NULL;
263 size_t headerlen, len = 0, n;
264 FILE *treefile = NULL;
265 struct got_pathlist_entry *pe;
266 struct got_tree_entry **sorted_entries;
267 struct got_tree_entry *te;
268 int i;
270 *id = NULL;
272 SHA1Init(&sha1_ctx);
274 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
275 if (sorted_entries == NULL)
276 return got_error_from_errno("calloc");
278 i = 0;
279 TAILQ_FOREACH(pe, paths, entry)
280 sorted_entries[i++] = pe->data;
281 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
282 sort_tree_entries_the_way_git_likes_it);
284 for (i = 0; i < nentries; i++) {
285 te = sorted_entries[i];
286 err = te_mode2str(modebuf, sizeof(modebuf), te);
287 if (err)
288 goto done;
289 len += strlen(modebuf) + strlen(te->name) + 1 +
290 SHA1_DIGEST_LENGTH;
293 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
294 err = got_error_from_errno("asprintf");
295 goto done;
297 headerlen = strlen(header) + 1;
298 SHA1Update(&sha1_ctx, header, headerlen);
300 treefile = got_opentemp();
301 if (treefile == NULL) {
302 err = got_error_from_errno("got_opentemp");
303 goto done;
306 n = fwrite(header, 1, headerlen, treefile);
307 if (n != headerlen) {
308 err = got_ferror(treefile, GOT_ERR_IO);
309 goto done;
312 for (i = 0; i < nentries; i++) {
313 te = sorted_entries[i];
314 err = te_mode2str(modebuf, sizeof(modebuf), te);
315 if (err)
316 goto done;
317 len = strlen(modebuf);
318 n = fwrite(modebuf, 1, len, treefile);
319 if (n != len) {
320 err = got_ferror(treefile, GOT_ERR_IO);
321 goto done;
323 SHA1Update(&sha1_ctx, modebuf, len);
325 len = strlen(te->name) + 1; /* must include NUL */
326 n = fwrite(te->name, 1, len, treefile);
327 if (n != len) {
328 err = got_ferror(treefile, GOT_ERR_IO);
329 goto done;
331 SHA1Update(&sha1_ctx, te->name, len);
333 len = SHA1_DIGEST_LENGTH;
334 n = fwrite(te->id.sha1, 1, len, treefile);
335 if (n != len) {
336 err = got_ferror(treefile, GOT_ERR_IO);
337 goto done;
339 SHA1Update(&sha1_ctx, te->id.sha1, len);
342 *id = malloc(sizeof(**id));
343 if (*id == NULL) {
344 err = got_error_from_errno("malloc");
345 goto done;
347 SHA1Final((*id)->sha1, &sha1_ctx);
349 if (fflush(treefile) != 0) {
350 err = got_error_from_errno("fflush");
351 goto done;
353 rewind(treefile);
355 err = create_object_file(*id, treefile, repo);
356 done:
357 free(header);
358 free(sorted_entries);
359 if (treefile && fclose(treefile) != 0 && err == NULL)
360 err = got_error_from_errno("fclose");
361 if (err) {
362 free(*id);
363 *id = NULL;
365 return err;
368 const struct got_error *
369 got_object_commit_create(struct got_object_id **id,
370 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
371 int nparents, const char *author, time_t author_time,
372 const char *committer, time_t committer_time,
373 const char *logmsg, struct got_repository *repo)
375 const struct got_error *err = NULL;
376 SHA1_CTX sha1_ctx;
377 char *header = NULL, *tree_str = NULL;
378 char *author_str = NULL, *committer_str = NULL;
379 char *id_str = NULL;
380 size_t headerlen, len = 0, n;
381 FILE *commitfile = NULL;
382 struct got_object_qid *qid;
383 char *msg0, *msg;
385 *id = NULL;
387 SHA1Init(&sha1_ctx);
389 msg0 = strdup(logmsg);
390 if (msg0 == NULL)
391 return got_error_from_errno("strdup");
392 msg = msg0;
394 while (isspace((unsigned char)msg[0]))
395 msg++;
396 len = strlen(msg);
397 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
398 msg[len - 1] = '\0';
399 len--;
402 if (asprintf(&author_str, "%s%s %lld +0000\n",
403 GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
404 return got_error_from_errno("asprintf");
406 if (asprintf(&committer_str, "%s%s %lld +0000\n",
407 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
408 committer ? committer_time : author_time)
409 == -1) {
410 err = got_error_from_errno("asprintf");
411 goto done;
414 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
415 nparents *
416 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
417 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
419 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
420 err = got_error_from_errno("asprintf");
421 goto done;
423 headerlen = strlen(header) + 1;
424 SHA1Update(&sha1_ctx, header, headerlen);
426 commitfile = got_opentemp();
427 if (commitfile == NULL) {
428 err = got_error_from_errno("got_opentemp");
429 goto done;
432 n = fwrite(header, 1, headerlen, commitfile);
433 if (n != headerlen) {
434 err = got_ferror(commitfile, GOT_ERR_IO);
435 goto done;
438 err = got_object_id_str(&id_str, tree_id);
439 if (err)
440 goto done;
441 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
442 == -1) {
443 err = got_error_from_errno("asprintf");
444 goto done;
446 len = strlen(tree_str);
447 SHA1Update(&sha1_ctx, tree_str, len);
448 n = fwrite(tree_str, 1, len, commitfile);
449 if (n != len) {
450 err = got_ferror(commitfile, GOT_ERR_IO);
451 goto done;
454 if (parent_ids) {
455 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
456 char *parent_str = NULL;
458 free(id_str);
460 err = got_object_id_str(&id_str, qid->id);
461 if (err)
462 goto done;
463 if (asprintf(&parent_str, "%s%s\n",
464 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
465 err = got_error_from_errno("asprintf");
466 goto done;
468 len = strlen(parent_str);
469 SHA1Update(&sha1_ctx, parent_str, len);
470 n = fwrite(parent_str, 1, len, commitfile);
471 if (n != len) {
472 err = got_ferror(commitfile, GOT_ERR_IO);
473 free(parent_str);
474 goto done;
476 free(parent_str);
480 len = strlen(author_str);
481 SHA1Update(&sha1_ctx, author_str, len);
482 n = fwrite(author_str, 1, len, commitfile);
483 if (n != len) {
484 err = got_ferror(commitfile, GOT_ERR_IO);
485 goto done;
488 len = strlen(committer_str);
489 SHA1Update(&sha1_ctx, committer_str, len);
490 n = fwrite(committer_str, 1, len, commitfile);
491 if (n != len) {
492 err = got_ferror(commitfile, GOT_ERR_IO);
493 goto done;
496 SHA1Update(&sha1_ctx, "\n", 1);
497 n = fwrite("\n", 1, 1, commitfile);
498 if (n != 1) {
499 err = got_ferror(commitfile, GOT_ERR_IO);
500 goto done;
503 len = strlen(msg);
504 SHA1Update(&sha1_ctx, msg, len);
505 n = fwrite(msg, 1, len, commitfile);
506 if (n != len) {
507 err = got_ferror(commitfile, GOT_ERR_IO);
508 goto done;
511 SHA1Update(&sha1_ctx, "\n", 1);
512 n = fwrite("\n", 1, 1, commitfile);
513 if (n != 1) {
514 err = got_ferror(commitfile, GOT_ERR_IO);
515 goto done;
518 *id = malloc(sizeof(**id));
519 if (*id == NULL) {
520 err = got_error_from_errno("malloc");
521 goto done;
523 SHA1Final((*id)->sha1, &sha1_ctx);
525 if (fflush(commitfile) != 0) {
526 err = got_error_from_errno("fflush");
527 goto done;
529 rewind(commitfile);
531 err = create_object_file(*id, commitfile, repo);
532 done:
533 free(msg0);
534 free(header);
535 free(tree_str);
536 free(author_str);
537 free(committer_str);
538 if (commitfile && fclose(commitfile) != 0 && err == NULL)
539 err = got_error_from_errno("fclose");
540 if (err) {
541 free(*id);
542 *id = NULL;
544 return err;
547 const struct got_error *
548 got_object_tag_create(struct got_object_id **id,
549 const char *tag_name, struct got_object_id *object_id, const char *tagger,
550 time_t tagger_time, const char *tagmsg, struct got_repository *repo)
552 const struct got_error *err = NULL;
553 SHA1_CTX sha1_ctx;
554 char *header = NULL;
555 char *tag_str = NULL, *tagger_str = NULL;
556 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
557 size_t headerlen, len = 0, n;
558 FILE *tagfile = NULL;
559 char *msg0 = NULL, *msg;
560 const char *obj_type_str;
561 int obj_type;
563 *id = NULL;
565 SHA1Init(&sha1_ctx);
567 err = got_object_id_str(&id_str, object_id);
568 if (err)
569 goto done;
570 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
571 err = got_error_from_errno("asprintf");
572 goto done;
575 err = got_object_get_type(&obj_type, repo, object_id);
576 if (err)
577 goto done;
579 switch (obj_type) {
580 case GOT_OBJ_TYPE_BLOB:
581 obj_type_str = GOT_OBJ_LABEL_BLOB;
582 break;
583 case GOT_OBJ_TYPE_TREE:
584 obj_type_str = GOT_OBJ_LABEL_TREE;
585 break;
586 case GOT_OBJ_TYPE_COMMIT:
587 obj_type_str = GOT_OBJ_LABEL_COMMIT;
588 break;
589 case GOT_OBJ_TYPE_TAG:
590 obj_type_str = GOT_OBJ_LABEL_TAG;
591 break;
592 default:
593 err = got_error(GOT_ERR_OBJ_TYPE);
594 goto done;
597 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
598 obj_type_str) == -1) {
599 err = got_error_from_errno("asprintf");
600 goto done;
603 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
604 err = got_error_from_errno("asprintf");
605 goto done;
608 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
609 GOT_TAG_LABEL_TAGGER, tagger, tagger_time) == -1)
610 return got_error_from_errno("asprintf");
612 msg0 = strdup(tagmsg);
613 if (msg0 == NULL) {
614 err = got_error_from_errno("strdup");
615 goto done;
617 msg = msg0;
619 while (isspace((unsigned char)msg[0]))
620 msg++;
622 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
623 strlen(tagger_str) + 1 + strlen(msg) + 1;
625 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
626 err = got_error_from_errno("asprintf");
627 goto done;
630 headerlen = strlen(header) + 1;
631 SHA1Update(&sha1_ctx, header, headerlen);
633 tagfile = got_opentemp();
634 if (tagfile == NULL) {
635 err = got_error_from_errno("got_opentemp");
636 goto done;
639 n = fwrite(header, 1, headerlen, tagfile);
640 if (n != headerlen) {
641 err = got_ferror(tagfile, GOT_ERR_IO);
642 goto done;
644 len = strlen(obj_str);
645 SHA1Update(&sha1_ctx, obj_str, len);
646 n = fwrite(obj_str, 1, len, tagfile);
647 if (n != len) {
648 err = got_ferror(tagfile, GOT_ERR_IO);
649 goto done;
651 len = strlen(type_str);
652 SHA1Update(&sha1_ctx, type_str, len);
653 n = fwrite(type_str, 1, len, tagfile);
654 if (n != len) {
655 err = got_ferror(tagfile, GOT_ERR_IO);
656 goto done;
659 len = strlen(tag_str);
660 SHA1Update(&sha1_ctx, tag_str, len);
661 n = fwrite(tag_str, 1, len, tagfile);
662 if (n != len) {
663 err = got_ferror(tagfile, GOT_ERR_IO);
664 goto done;
667 len = strlen(tagger_str);
668 SHA1Update(&sha1_ctx, tagger_str, len);
669 n = fwrite(tagger_str, 1, len, tagfile);
670 if (n != len) {
671 err = got_ferror(tagfile, GOT_ERR_IO);
672 goto done;
675 SHA1Update(&sha1_ctx, "\n", 1);
676 n = fwrite("\n", 1, 1, tagfile);
677 if (n != 1) {
678 err = got_ferror(tagfile, GOT_ERR_IO);
679 goto done;
682 len = strlen(msg);
683 SHA1Update(&sha1_ctx, msg, len);
684 n = fwrite(msg, 1, len, tagfile);
685 if (n != len) {
686 err = got_ferror(tagfile, GOT_ERR_IO);
687 goto done;
690 SHA1Update(&sha1_ctx, "\n", 1);
691 n = fwrite("\n", 1, 1, tagfile);
692 if (n != 1) {
693 err = got_ferror(tagfile, GOT_ERR_IO);
694 goto done;
697 *id = malloc(sizeof(**id));
698 if (*id == NULL) {
699 err = got_error_from_errno("malloc");
700 goto done;
702 SHA1Final((*id)->sha1, &sha1_ctx);
704 if (fflush(tagfile) != 0) {
705 err = got_error_from_errno("fflush");
706 goto done;
708 rewind(tagfile);
710 err = create_object_file(*id, tagfile, repo);
711 done:
712 free(msg0);
713 free(header);
714 free(obj_str);
715 free(tagger_str);
716 if (tagfile && fclose(tagfile) != 0 && err == NULL)
717 err = got_error_from_errno("fclose");
718 if (err) {
719 free(*id);
720 *id = NULL;
722 return err;