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);
319 const struct got_error *
320 got_object_tree_create(struct got_object_id **id,
321 struct got_tree_entries *tree_entries, struct got_repository *repo)
323 const struct got_error *err = NULL;
324 char modebuf[sizeof("100644 ")];
327 size_t headerlen, len = 0, n;
328 FILE *treefile = NULL;
329 struct got_tree_entries *entries; /* sorted according to Git rules */
330 struct got_tree_entry *te;
336 err = sort_tree_entries_the_way_git_likes_it(&entries, tree_entries);
340 SIMPLEQ_FOREACH(te, &entries->head, entry) {
341 err = mode2str(modebuf, sizeof(modebuf), te->mode);
344 len += strlen(modebuf) + strlen(te->name) + 1 +
348 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
349 err = got_error_from_errno("asprintf");
352 headerlen = strlen(header) + 1;
353 SHA1Update(&sha1_ctx, header, headerlen);
355 treefile = got_opentemp();
356 if (treefile == NULL) {
357 err = got_error_from_errno("got_opentemp");
361 n = fwrite(header, 1, headerlen, treefile);
362 if (n != headerlen) {
363 err = got_ferror(treefile, GOT_ERR_IO);
367 SIMPLEQ_FOREACH(te, &entries->head, entry) {
368 err = mode2str(modebuf, sizeof(modebuf), te->mode);
371 len = strlen(modebuf);
372 n = fwrite(modebuf, 1, len, treefile);
374 err = got_ferror(treefile, GOT_ERR_IO);
377 SHA1Update(&sha1_ctx, modebuf, len);
379 len = strlen(te->name) + 1; /* must include NUL */
380 n = fwrite(te->name, 1, len, treefile);
382 err = got_ferror(treefile, GOT_ERR_IO);
385 SHA1Update(&sha1_ctx, te->name, len);
387 len = SHA1_DIGEST_LENGTH;
388 n = fwrite(te->id->sha1, 1, len, treefile);
390 err = got_ferror(treefile, GOT_ERR_IO);
393 SHA1Update(&sha1_ctx, te->id->sha1, len);
396 *id = malloc(sizeof(**id));
398 err = got_error_from_errno("malloc");
401 SHA1Final((*id)->sha1, &sha1_ctx);
403 if (fflush(treefile) != 0) {
404 err = got_error_from_errno("fflush");
409 err = create_object_file(*id, treefile, repo);
412 got_object_tree_entries_close(entries);
414 if (treefile && fclose(treefile) != 0 && err == NULL)
415 err = got_error_from_errno("fclose");
423 const struct got_error *
424 got_object_commit_create(struct got_object_id **id,
425 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
426 int nparents, const char *author, time_t author_time,
427 const char *committer, time_t committer_time,
428 const char *logmsg, struct got_repository *repo)
430 const struct got_error *err = NULL;
432 char *header = NULL, *tree_str = NULL;
433 char *author_str = NULL, *committer_str = NULL;
435 size_t headerlen, len = 0, n;
436 FILE *commitfile = NULL;
437 struct got_object_qid *qid;
444 msg0 = strdup(logmsg);
446 return got_error_from_errno("strdup");
449 while (isspace((unsigned char)msg[0]))
452 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
457 if (asprintf(&author_str, "%s%s %lld +0000\n",
458 GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
459 return got_error_from_errno("asprintf");
461 if (asprintf(&committer_str, "%s%s %lld +0000\n",
462 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
463 committer ? committer_time : author_time)
465 err = got_error_from_errno("asprintf");
469 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
471 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
472 + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
474 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
475 err = got_error_from_errno("asprintf");
478 headerlen = strlen(header) + 1;
479 SHA1Update(&sha1_ctx, header, headerlen);
481 commitfile = got_opentemp();
482 if (commitfile == NULL) {
483 err = got_error_from_errno("got_opentemp");
487 n = fwrite(header, 1, headerlen, commitfile);
488 if (n != headerlen) {
489 err = got_ferror(commitfile, GOT_ERR_IO);
493 err = got_object_id_str(&id_str, tree_id);
496 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
498 err = got_error_from_errno("asprintf");
501 len = strlen(tree_str);
502 SHA1Update(&sha1_ctx, tree_str, len);
503 n = fwrite(tree_str, 1, len, commitfile);
505 err = got_ferror(commitfile, GOT_ERR_IO);
510 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
511 char *parent_str = NULL;
515 err = got_object_id_str(&id_str, qid->id);
518 if (asprintf(&parent_str, "%s%s\n",
519 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
520 err = got_error_from_errno("asprintf");
523 len = strlen(parent_str);
524 SHA1Update(&sha1_ctx, parent_str, len);
525 n = fwrite(parent_str, 1, len, commitfile);
527 err = got_ferror(commitfile, GOT_ERR_IO);
535 len = strlen(author_str);
536 SHA1Update(&sha1_ctx, author_str, len);
537 n = fwrite(author_str, 1, len, commitfile);
539 err = got_ferror(commitfile, GOT_ERR_IO);
543 len = strlen(committer_str);
544 SHA1Update(&sha1_ctx, committer_str, len);
545 n = fwrite(committer_str, 1, len, commitfile);
547 err = got_ferror(commitfile, GOT_ERR_IO);
551 SHA1Update(&sha1_ctx, "\n", 1);
552 n = fwrite("\n", 1, 1, commitfile);
554 err = got_ferror(commitfile, GOT_ERR_IO);
559 SHA1Update(&sha1_ctx, msg, len);
560 n = fwrite(msg, 1, len, commitfile);
562 err = got_ferror(commitfile, GOT_ERR_IO);
566 SHA1Update(&sha1_ctx, "\n", 1);
567 n = fwrite("\n", 1, 1, commitfile);
569 err = got_ferror(commitfile, GOT_ERR_IO);
573 *id = malloc(sizeof(**id));
575 err = got_error_from_errno("malloc");
578 SHA1Final((*id)->sha1, &sha1_ctx);
580 if (fflush(commitfile) != 0) {
581 err = got_error_from_errno("fflush");
586 err = create_object_file(*id, commitfile, repo);
593 if (commitfile && fclose(commitfile) != 0 && err == NULL)
594 err = got_error_from_errno("fclose");