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, mode_t te_mode)
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 or submodules.
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 (S_ISDIR(te_mode))
220 mode = S_IFDIR; /* Git leaves all the other bits unset. */
221 else
222 return got_error(GOT_ERR_BAD_FILETYPE);
224 ret = snprintf(buf, len, "%o ", mode);
225 if (ret == -1 || ret >= len)
226 return got_error(GOT_ERR_NO_SPACE);
227 return NULL;
230 /*
231 * Git expects directory tree entries to be sorted with an imaginary slash
232 * appended to their name, and will break otherwise. Let's be nice.
233 * This function is intended to be used with mergesort(3) to sort an
234 * array of pointers to struct got_tree_entry objects.
235 */
236 static int
237 sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
239 struct got_tree_entry * const *te1 = arg1;
240 struct got_tree_entry * const *te2 = arg2;
241 char name1[NAME_MAX + 2];
242 char name2[NAME_MAX + 2];
244 strlcpy(name1, (*te1)->name, sizeof(name1));
245 strlcpy(name2, (*te2)->name, sizeof(name2));
246 if (S_ISDIR((*te1)->mode))
247 strlcat(name1, "/", sizeof(name1));
248 if (S_ISDIR((*te2)->mode))
249 strlcat(name2, "/", sizeof(name2));
250 return strcmp(name1, name2);
253 const struct got_error *
254 got_object_tree_create(struct got_object_id **id,
255 struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
257 const struct got_error *err = NULL;
258 char modebuf[sizeof("100644 ")];
259 SHA1_CTX sha1_ctx;
260 char *header = NULL;
261 size_t headerlen, len = 0, n;
262 FILE *treefile = NULL;
263 struct got_pathlist_entry *pe;
264 struct got_tree_entry **sorted_entries;
265 struct got_tree_entry *te;
266 int i;
268 *id = NULL;
270 SHA1Init(&sha1_ctx);
272 sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
273 if (sorted_entries == NULL)
274 return got_error_from_errno("calloc");
276 i = 0;
277 TAILQ_FOREACH(pe, paths, entry)
278 sorted_entries[i++] = pe->data;
279 mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
280 sort_tree_entries_the_way_git_likes_it);
282 for (i = 0; i < nentries; i++) {
283 te = sorted_entries[i];
284 err = te_mode2str(modebuf, sizeof(modebuf), te->mode);
285 if (err)
286 goto done;
287 len += strlen(modebuf) + strlen(te->name) + 1 +
288 SHA1_DIGEST_LENGTH;
291 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
292 err = got_error_from_errno("asprintf");
293 goto done;
295 headerlen = strlen(header) + 1;
296 SHA1Update(&sha1_ctx, header, headerlen);
298 treefile = got_opentemp();
299 if (treefile == NULL) {
300 err = got_error_from_errno("got_opentemp");
301 goto done;
304 n = fwrite(header, 1, headerlen, treefile);
305 if (n != headerlen) {
306 err = got_ferror(treefile, GOT_ERR_IO);
307 goto done;
310 for (i = 0; i < nentries; i++) {
311 te = sorted_entries[i];
312 err = te_mode2str(modebuf, sizeof(modebuf), te->mode);
313 if (err)
314 goto done;
315 len = strlen(modebuf);
316 n = fwrite(modebuf, 1, len, treefile);
317 if (n != len) {
318 err = got_ferror(treefile, GOT_ERR_IO);
319 goto done;
321 SHA1Update(&sha1_ctx, modebuf, len);
323 len = strlen(te->name) + 1; /* must include NUL */
324 n = fwrite(te->name, 1, len, treefile);
325 if (n != len) {
326 err = got_ferror(treefile, GOT_ERR_IO);
327 goto done;
329 SHA1Update(&sha1_ctx, te->name, len);
331 len = SHA1_DIGEST_LENGTH;
332 n = fwrite(te->id.sha1, 1, len, treefile);
333 if (n != len) {
334 err = got_ferror(treefile, GOT_ERR_IO);
335 goto done;
337 SHA1Update(&sha1_ctx, te->id.sha1, len);
340 *id = malloc(sizeof(**id));
341 if (*id == NULL) {
342 err = got_error_from_errno("malloc");
343 goto done;
345 SHA1Final((*id)->sha1, &sha1_ctx);
347 if (fflush(treefile) != 0) {
348 err = got_error_from_errno("fflush");
349 goto done;
351 rewind(treefile);
353 err = create_object_file(*id, treefile, repo);
354 done:
355 free(header);
356 free(sorted_entries);
357 if (treefile && fclose(treefile) != 0 && err == NULL)
358 err = got_error_from_errno("fclose");
359 if (err) {
360 free(*id);
361 *id = NULL;
363 return err;
366 const struct got_error *
367 got_object_commit_create(struct got_object_id **id,
368 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
369 int nparents, const char *author, time_t author_time,
370 const char *committer, time_t committer_time,
371 const char *logmsg, struct got_repository *repo)
373 const struct got_error *err = NULL;
374 SHA1_CTX sha1_ctx;
375 char *header = NULL, *tree_str = NULL;
376 char *author_str = NULL, *committer_str = NULL;
377 char *id_str = NULL;
378 size_t headerlen, len = 0, n;
379 FILE *commitfile = NULL;
380 struct got_object_qid *qid;
381 char *msg0, *msg;
383 *id = NULL;
385 SHA1Init(&sha1_ctx);
387 msg0 = strdup(logmsg);
388 if (msg0 == NULL)
389 return got_error_from_errno("strdup");
390 msg = msg0;
392 while (isspace((unsigned char)msg[0]))
393 msg++;
394 len = strlen(msg);
395 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
396 msg[len - 1] = '\0';
397 len--;
400 if (asprintf(&author_str, "%s%s %lld +0000\n",
401 GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
402 return got_error_from_errno("asprintf");
404 if (asprintf(&committer_str, "%s%s %lld +0000\n",
405 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
406 committer ? committer_time : author_time)
407 == -1) {
408 err = got_error_from_errno("asprintf");
409 goto done;
412 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
413 nparents *
414 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
415 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
417 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
418 err = got_error_from_errno("asprintf");
419 goto done;
421 headerlen = strlen(header) + 1;
422 SHA1Update(&sha1_ctx, header, headerlen);
424 commitfile = got_opentemp();
425 if (commitfile == NULL) {
426 err = got_error_from_errno("got_opentemp");
427 goto done;
430 n = fwrite(header, 1, headerlen, commitfile);
431 if (n != headerlen) {
432 err = got_ferror(commitfile, GOT_ERR_IO);
433 goto done;
436 err = got_object_id_str(&id_str, tree_id);
437 if (err)
438 goto done;
439 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
440 == -1) {
441 err = got_error_from_errno("asprintf");
442 goto done;
444 len = strlen(tree_str);
445 SHA1Update(&sha1_ctx, tree_str, len);
446 n = fwrite(tree_str, 1, len, commitfile);
447 if (n != len) {
448 err = got_ferror(commitfile, GOT_ERR_IO);
449 goto done;
452 if (parent_ids) {
453 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
454 char *parent_str = NULL;
456 free(id_str);
458 err = got_object_id_str(&id_str, qid->id);
459 if (err)
460 goto done;
461 if (asprintf(&parent_str, "%s%s\n",
462 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
463 err = got_error_from_errno("asprintf");
464 goto done;
466 len = strlen(parent_str);
467 SHA1Update(&sha1_ctx, parent_str, len);
468 n = fwrite(parent_str, 1, len, commitfile);
469 if (n != len) {
470 err = got_ferror(commitfile, GOT_ERR_IO);
471 free(parent_str);
472 goto done;
474 free(parent_str);
478 len = strlen(author_str);
479 SHA1Update(&sha1_ctx, author_str, len);
480 n = fwrite(author_str, 1, len, commitfile);
481 if (n != len) {
482 err = got_ferror(commitfile, GOT_ERR_IO);
483 goto done;
486 len = strlen(committer_str);
487 SHA1Update(&sha1_ctx, committer_str, len);
488 n = fwrite(committer_str, 1, len, commitfile);
489 if (n != len) {
490 err = got_ferror(commitfile, GOT_ERR_IO);
491 goto done;
494 SHA1Update(&sha1_ctx, "\n", 1);
495 n = fwrite("\n", 1, 1, commitfile);
496 if (n != 1) {
497 err = got_ferror(commitfile, GOT_ERR_IO);
498 goto done;
501 len = strlen(msg);
502 SHA1Update(&sha1_ctx, msg, len);
503 n = fwrite(msg, 1, len, commitfile);
504 if (n != len) {
505 err = got_ferror(commitfile, GOT_ERR_IO);
506 goto done;
509 SHA1Update(&sha1_ctx, "\n", 1);
510 n = fwrite("\n", 1, 1, commitfile);
511 if (n != 1) {
512 err = got_ferror(commitfile, GOT_ERR_IO);
513 goto done;
516 *id = malloc(sizeof(**id));
517 if (*id == NULL) {
518 err = got_error_from_errno("malloc");
519 goto done;
521 SHA1Final((*id)->sha1, &sha1_ctx);
523 if (fflush(commitfile) != 0) {
524 err = got_error_from_errno("fflush");
525 goto done;
527 rewind(commitfile);
529 err = create_object_file(*id, commitfile, repo);
530 done:
531 free(msg0);
532 free(header);
533 free(tree_str);
534 free(author_str);
535 free(committer_str);
536 if (commitfile && fclose(commitfile) != 0 && err == NULL)
537 err = got_error_from_errno("fclose");
538 if (err) {
539 free(*id);
540 *id = NULL;
542 return err;
545 const struct got_error *
546 got_object_tag_create(struct got_object_id **id,
547 const char *tag_name, struct got_object_id *object_id, const char *tagger,
548 time_t tagger_time, const char *tagmsg, struct got_repository *repo)
550 const struct got_error *err = NULL;
551 SHA1_CTX sha1_ctx;
552 char *header = NULL;
553 char *tag_str = NULL, *tagger_str = NULL;
554 char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
555 size_t headerlen, len = 0, n;
556 FILE *tagfile = NULL;
557 char *msg0 = NULL, *msg;
558 const char *obj_type_str;
559 int obj_type;
561 *id = NULL;
563 SHA1Init(&sha1_ctx);
565 err = got_object_id_str(&id_str, object_id);
566 if (err)
567 goto done;
568 if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
569 err = got_error_from_errno("asprintf");
570 goto done;
573 err = got_object_get_type(&obj_type, repo, object_id);
574 if (err)
575 goto done;
577 switch (obj_type) {
578 case GOT_OBJ_TYPE_BLOB:
579 obj_type_str = GOT_OBJ_LABEL_BLOB;
580 break;
581 case GOT_OBJ_TYPE_TREE:
582 obj_type_str = GOT_OBJ_LABEL_TREE;
583 break;
584 case GOT_OBJ_TYPE_COMMIT:
585 obj_type_str = GOT_OBJ_LABEL_COMMIT;
586 break;
587 case GOT_OBJ_TYPE_TAG:
588 obj_type_str = GOT_OBJ_LABEL_TAG;
589 break;
590 default:
591 err = got_error(GOT_ERR_OBJ_TYPE);
592 goto done;
595 if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
596 obj_type_str) == -1) {
597 err = got_error_from_errno("asprintf");
598 goto done;
601 if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
602 err = got_error_from_errno("asprintf");
603 goto done;
606 if (asprintf(&tagger_str, "%s%s %lld +0000\n",
607 GOT_TAG_LABEL_TAGGER, tagger, tagger_time) == -1)
608 return got_error_from_errno("asprintf");
610 msg0 = strdup(tagmsg);
611 if (msg0 == NULL) {
612 err = got_error_from_errno("strdup");
613 goto done;
615 msg = msg0;
617 while (isspace((unsigned char)msg[0]))
618 msg++;
620 len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
621 strlen(tagger_str) + 1 + strlen(msg) + 1;
623 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
624 err = got_error_from_errno("asprintf");
625 goto done;
628 headerlen = strlen(header) + 1;
629 SHA1Update(&sha1_ctx, header, headerlen);
631 tagfile = got_opentemp();
632 if (tagfile == NULL) {
633 err = got_error_from_errno("got_opentemp");
634 goto done;
637 n = fwrite(header, 1, headerlen, tagfile);
638 if (n != headerlen) {
639 err = got_ferror(tagfile, GOT_ERR_IO);
640 goto done;
642 len = strlen(obj_str);
643 SHA1Update(&sha1_ctx, obj_str, len);
644 n = fwrite(obj_str, 1, len, tagfile);
645 if (n != len) {
646 err = got_ferror(tagfile, GOT_ERR_IO);
647 goto done;
649 len = strlen(type_str);
650 SHA1Update(&sha1_ctx, type_str, len);
651 n = fwrite(type_str, 1, len, tagfile);
652 if (n != len) {
653 err = got_ferror(tagfile, GOT_ERR_IO);
654 goto done;
657 len = strlen(tag_str);
658 SHA1Update(&sha1_ctx, tag_str, len);
659 n = fwrite(tag_str, 1, len, tagfile);
660 if (n != len) {
661 err = got_ferror(tagfile, GOT_ERR_IO);
662 goto done;
665 len = strlen(tagger_str);
666 SHA1Update(&sha1_ctx, tagger_str, len);
667 n = fwrite(tagger_str, 1, len, tagfile);
668 if (n != len) {
669 err = got_ferror(tagfile, GOT_ERR_IO);
670 goto done;
673 SHA1Update(&sha1_ctx, "\n", 1);
674 n = fwrite("\n", 1, 1, tagfile);
675 if (n != 1) {
676 err = got_ferror(tagfile, GOT_ERR_IO);
677 goto done;
680 len = strlen(msg);
681 SHA1Update(&sha1_ctx, msg, len);
682 n = fwrite(msg, 1, len, tagfile);
683 if (n != len) {
684 err = got_ferror(tagfile, GOT_ERR_IO);
685 goto done;
688 SHA1Update(&sha1_ctx, "\n", 1);
689 n = fwrite("\n", 1, 1, tagfile);
690 if (n != 1) {
691 err = got_ferror(tagfile, GOT_ERR_IO);
692 goto done;
695 *id = malloc(sizeof(**id));
696 if (*id == NULL) {
697 err = got_error_from_errno("malloc");
698 goto done;
700 SHA1Final((*id)->sha1, &sha1_ctx);
702 if (fflush(tagfile) != 0) {
703 err = got_error_from_errno("fflush");
704 goto done;
706 rewind(tagfile);
708 err = create_object_file(*id, tagfile, repo);
709 done:
710 free(msg0);
711 free(header);
712 free(obj_str);
713 free(tagger_str);
714 if (tagfile && fclose(tagfile) != 0 && err == NULL)
715 err = got_error_from_errno("fclose");
716 if (err) {
717 free(*id);
718 *id = NULL;
720 return err;