2 * Copyright (c) 2019 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/types.h>
19 #include <sys/queue.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_repository.h"
35 #include "got_opentemp.h"
38 #include "got_lib_sha1.h"
39 #include "got_lib_deflate.h"
40 #include "got_lib_delta.h"
41 #include "got_lib_object.h"
42 #include "got_lib_object_parse.h"
43 #include "got_lib_lockfile.h"
46 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
49 static const struct got_error *
50 create_object_file(struct got_object_id *id, FILE *content,
51 struct got_repository *repo)
53 const struct got_error *err = NULL, *unlock_err = NULL;
54 char *objpath = NULL, *tmppath = NULL;
56 struct got_lockfile *lf = NULL;
59 err = got_object_get_path(&objpath, id, repo);
63 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
66 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
68 err = got_path_dirname(&parent_path, objpath);
71 err = got_path_mkdir(parent_path);
75 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
80 err = got_deflate_to_file(&tmplen, content, tmpfile);
84 err = got_lockfile_lock(&lf, objpath);
88 if (rename(tmppath, objpath) != 0) {
89 err = got_error_from_errno3("rename", tmppath, objpath);
95 if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
96 err = got_error_from_errno2("chmod", objpath);
102 if (unlink(tmppath) != 0 && err == NULL)
103 err = got_error_from_errno2("unlink", tmppath);
106 if (tmpfile && fclose(tmpfile) != 0 && err == NULL)
107 err = got_error_from_errno("fclose");
109 unlock_err = got_lockfile_unlock(lf);
110 return err ? err : unlock_err;
113 const struct got_error *
114 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
115 struct got_repository *repo)
117 const struct got_error *err = NULL;
119 FILE *blobfile = NULL;
123 size_t headerlen = 0, n;
129 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
131 return got_error_from_errno2("open", ondisk_path);
133 if (fstat(fd, &sb) == -1) {
134 err = got_error_from_errno2("fstat", ondisk_path);
138 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
140 err = got_error_from_errno("asprintf");
143 headerlen = strlen(header) + 1;
144 SHA1Update(&sha1_ctx, header, headerlen);
146 blobfile = got_opentemp();
147 if (blobfile == NULL) {
148 err = got_error_from_errno("got_opentemp");
152 n = fwrite(header, 1, headerlen, blobfile);
153 if (n != headerlen) {
154 err = got_ferror(blobfile, GOT_ERR_IO);
161 inlen = read(fd, buf, sizeof(buf));
163 err = got_error_from_errno("read");
168 SHA1Update(&sha1_ctx, buf, inlen);
169 n = fwrite(buf, 1, inlen, blobfile);
171 err = got_ferror(blobfile, GOT_ERR_IO);
176 *id = malloc(sizeof(**id));
178 err = got_error_from_errno("malloc");
181 SHA1Final((*id)->sha1, &sha1_ctx);
183 if (fflush(blobfile) != 0) {
184 err = got_error_from_errno("fflush");
189 err = create_object_file(*id, blobfile, repo);
192 if (fd != -1 && close(fd) != 0 && err == NULL)
193 err = got_error_from_errno("close");
194 if (blobfile && fclose(blobfile) != 0 && err == NULL)
195 err = got_error_from_errno("fclose");
203 static const struct got_error *
204 mode2str(char *buf, size_t len, mode_t mode)
207 ret = snprintf(buf, len, "%o ", mode);
208 if (ret == -1 || ret >= len)
209 return got_error(GOT_ERR_NO_SPACE);
214 static const struct got_error *
215 te_git_name(char **name, struct got_tree_entry *te)
217 const struct got_error *err = NULL;
219 if (S_ISDIR(te->mode)) {
220 if (asprintf(name, "%s/", te->name) == -1)
221 err = got_error_from_errno("asprintf");
223 *name = strdup(te->name);
225 err = got_error_from_errno("strdup");
230 static const struct got_error *
231 insert_tree_entry(struct got_tree_entries *sorted, struct got_tree_entry *new)
233 const struct got_error *err = NULL;
234 struct got_tree_entry *te = NULL, *prev = NULL;
238 if (SIMPLEQ_EMPTY(&sorted->head)) {
239 SIMPLEQ_INSERT_HEAD(&sorted->head, new, entry);
244 err = te_git_name(&name, new);
248 te = SIMPLEQ_FIRST(&sorted->head);
253 te = SIMPLEQ_NEXT(te, entry);
256 err = te_git_name(&te_name, te);
259 cmp = strcmp(te_name, name);
262 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
266 SIMPLEQ_INSERT_AFTER(&sorted->head, prev, new, entry);
271 te = SIMPLEQ_NEXT(te, entry);
275 SIMPLEQ_INSERT_TAIL(&sorted->head, new, entry);
284 * Git expects directory tree entries to be sorted with an imaginary slash
285 * appended to their name, and will break otherwise. Let's be nice.
287 static const struct got_error *
288 sort_tree_entries_the_way_git_likes_it(struct got_tree_entries **sorted,
289 struct got_tree_entries *tree_entries)
291 const struct got_error *err = NULL;
292 struct got_tree_entry *te;
294 *sorted = malloc(sizeof(**sorted));
296 return got_error_from_errno("malloc");
298 (*sorted)->nentries = 0;
299 SIMPLEQ_INIT(&(*sorted)->head);
301 SIMPLEQ_FOREACH(te, &tree_entries->head, entry) {
302 struct got_tree_entry *new;
303 err = got_object_tree_entry_dup(&new, te);
306 err = insert_tree_entry(*sorted, new);
308 got_object_tree_entry_close(new);
321 const struct got_error *
322 got_object_tree_create(struct got_object_id **id,
323 struct got_tree_entries *tree_entries, struct got_repository *repo)
325 const struct got_error *err = NULL;
326 char modebuf[sizeof("100644 ")];
329 size_t headerlen, len = 0, n;
330 FILE *treefile = NULL;
331 struct got_tree_entries *entries; /* sorted according to Git rules */
332 struct got_tree_entry *te;
338 err = sort_tree_entries_the_way_git_likes_it(&entries, tree_entries);
342 SIMPLEQ_FOREACH(te, &entries->head, entry) {
343 err = mode2str(modebuf, sizeof(modebuf), te->mode);
346 len += strlen(modebuf) + strlen(te->name) + 1 +
350 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
351 err = got_error_from_errno("asprintf");
354 headerlen = strlen(header) + 1;
355 SHA1Update(&sha1_ctx, header, headerlen);
357 treefile = got_opentemp();
358 if (treefile == NULL) {
359 err = got_error_from_errno("got_opentemp");
363 n = fwrite(header, 1, headerlen, treefile);
364 if (n != headerlen) {
365 err = got_ferror(treefile, GOT_ERR_IO);
369 SIMPLEQ_FOREACH(te, &entries->head, entry) {
370 err = mode2str(modebuf, sizeof(modebuf), te->mode);
373 len = strlen(modebuf);
374 n = fwrite(modebuf, 1, len, treefile);
376 err = got_ferror(treefile, GOT_ERR_IO);
379 SHA1Update(&sha1_ctx, modebuf, len);
381 len = strlen(te->name) + 1; /* must include NUL */
382 n = fwrite(te->name, 1, len, treefile);
384 err = got_ferror(treefile, GOT_ERR_IO);
387 SHA1Update(&sha1_ctx, te->name, len);
389 len = SHA1_DIGEST_LENGTH;
390 n = fwrite(te->id->sha1, 1, len, treefile);
392 err = got_ferror(treefile, GOT_ERR_IO);
395 SHA1Update(&sha1_ctx, te->id->sha1, len);
398 *id = malloc(sizeof(**id));
400 err = got_error_from_errno("malloc");
403 SHA1Final((*id)->sha1, &sha1_ctx);
405 if (fflush(treefile) != 0) {
406 err = got_error_from_errno("fflush");
411 err = create_object_file(*id, treefile, repo);
414 got_object_tree_entries_close(entries);
416 if (treefile && fclose(treefile) != 0 && err == NULL)
417 err = got_error_from_errno("fclose");
425 const struct got_error *
426 got_object_commit_create(struct got_object_id **id,
427 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
428 int nparents, const char *author, time_t author_time,
429 const char *committer, time_t committer_time,
430 const char *logmsg, struct got_repository *repo)
432 const struct got_error *err = NULL;
434 char *header = NULL, *tree_str = NULL;
435 char *author_str = NULL, *committer_str = NULL;
437 size_t headerlen, len = 0, n;
438 FILE *commitfile = NULL;
439 struct got_object_qid *qid;
446 msg0 = strdup(logmsg);
448 return got_error_from_errno("strdup");
451 while (isspace((unsigned char)msg[0]))
454 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
459 if (asprintf(&author_str, "%s%s %lld +0000\n",
460 GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
461 return got_error_from_errno("asprintf");
463 if (asprintf(&committer_str, "%s%s %lld +0000\n",
464 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
465 committer ? committer_time : author_time)
467 err = got_error_from_errno("asprintf");
471 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
473 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
474 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
476 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
477 err = got_error_from_errno("asprintf");
480 headerlen = strlen(header) + 1;
481 SHA1Update(&sha1_ctx, header, headerlen);
483 commitfile = got_opentemp();
484 if (commitfile == NULL) {
485 err = got_error_from_errno("got_opentemp");
489 n = fwrite(header, 1, headerlen, commitfile);
490 if (n != headerlen) {
491 err = got_ferror(commitfile, GOT_ERR_IO);
495 err = got_object_id_str(&id_str, tree_id);
498 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
500 err = got_error_from_errno("asprintf");
503 len = strlen(tree_str);
504 SHA1Update(&sha1_ctx, tree_str, len);
505 n = fwrite(tree_str, 1, len, commitfile);
507 err = got_ferror(commitfile, GOT_ERR_IO);
512 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
513 char *parent_str = NULL;
517 err = got_object_id_str(&id_str, qid->id);
520 if (asprintf(&parent_str, "%s%s\n",
521 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
522 err = got_error_from_errno("asprintf");
525 len = strlen(parent_str);
526 SHA1Update(&sha1_ctx, parent_str, len);
527 n = fwrite(parent_str, 1, len, commitfile);
529 err = got_ferror(commitfile, GOT_ERR_IO);
537 len = strlen(author_str);
538 SHA1Update(&sha1_ctx, author_str, len);
539 n = fwrite(author_str, 1, len, commitfile);
541 err = got_ferror(commitfile, GOT_ERR_IO);
545 len = strlen(committer_str);
546 SHA1Update(&sha1_ctx, committer_str, len);
547 n = fwrite(committer_str, 1, len, commitfile);
549 err = got_ferror(commitfile, GOT_ERR_IO);
553 SHA1Update(&sha1_ctx, "\n", 1);
554 n = fwrite("\n", 1, 1, commitfile);
556 err = got_ferror(commitfile, GOT_ERR_IO);
561 SHA1Update(&sha1_ctx, msg, len);
562 n = fwrite(msg, 1, len, commitfile);
564 err = got_ferror(commitfile, GOT_ERR_IO);
568 SHA1Update(&sha1_ctx, "\n", 1);
569 n = fwrite("\n", 1, 1, commitfile);
571 err = got_ferror(commitfile, GOT_ERR_IO);
575 *id = malloc(sizeof(**id));
577 err = got_error_from_errno("malloc");
580 SHA1Final((*id)->sha1, &sha1_ctx);
582 if (fflush(commitfile) != 0) {
583 err = got_error_from_errno("fflush");
588 err = create_object_file(*id, commitfile, repo);
595 if (commitfile && fclose(commitfile) != 0 && err == NULL)
596 err = got_error_from_errno("fclose");