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 <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdint.h>
28 #include <sha1.h>
29 #include <unistd.h>
30 #include <zlib.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_repository.h"
35 #include "got_opentemp.h"
36 #include "got_path.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"
45 #ifndef nitems
46 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
47 #endif
49 static const struct got_error *
50 create_object_file(struct got_object_id *id, FILE *content,
51 struct got_repository *repo)
52 {
53 const struct got_error *err = NULL, *unlock_err = NULL;
54 char *objpath = NULL, *tmppath = NULL;
55 FILE *tmpfile = NULL;
56 struct got_lockfile *lf = NULL;
57 size_t tmplen = 0;
59 err = got_object_get_path(&objpath, id, repo);
60 if (err)
61 return err;
63 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
64 if (err) {
65 char *parent_path;
66 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
67 goto done;
68 err = got_path_dirname(&parent_path, objpath);
69 if (err)
70 goto done;
71 err = got_path_mkdir(parent_path);
72 free(parent_path);
73 if (err)
74 goto done;
75 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
76 if (err)
77 goto done;
78 }
80 err = got_deflate_to_file(&tmplen, content, tmpfile);
81 if (err)
82 goto done;
84 err = got_lockfile_lock(&lf, objpath);
85 if (err)
86 goto done;
88 if (rename(tmppath, objpath) != 0) {
89 err = got_error_from_errno3("rename", tmppath, objpath);
90 goto done;
91 }
92 free(tmppath);
93 tmppath = NULL;
95 if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
96 err = got_error_from_errno2("chmod", objpath);
97 goto done;
98 }
99 done:
100 free(objpath);
101 if (tmppath) {
102 if (unlink(tmppath) != 0 && err == NULL)
103 err = got_error_from_errno2("unlink", tmppath);
104 free(tmppath);
106 if (tmpfile && fclose(tmpfile) != 0 && err == NULL)
107 err = got_error_from_errno("fclose");
108 if (lf)
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;
118 char *header = NULL;
119 FILE *blobfile = NULL;
120 int fd = -1;
121 struct stat sb;
122 SHA1_CTX sha1_ctx;
123 size_t headerlen = 0, n;
125 *id = NULL;
127 SHA1Init(&sha1_ctx);
129 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
130 if (fd == -1)
131 return got_error_from_errno2("open", ondisk_path);
133 if (fstat(fd, &sb) == -1) {
134 err = got_error_from_errno2("fstat", ondisk_path);
135 goto done;
138 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
139 sb.st_size) == -1) {
140 err = got_error_from_errno("asprintf");
141 goto done;
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");
149 goto done;
152 n = fwrite(header, 1, headerlen, blobfile);
153 if (n != headerlen) {
154 err = got_ferror(blobfile, GOT_ERR_IO);
155 goto done;
157 for (;;) {
158 char buf[8192];
159 ssize_t inlen;
161 inlen = read(fd, buf, sizeof(buf));
162 if (inlen == -1) {
163 err = got_error_from_errno("read");
164 goto done;
166 if (inlen == 0)
167 break; /* EOF */
168 SHA1Update(&sha1_ctx, buf, inlen);
169 n = fwrite(buf, 1, inlen, blobfile);
170 if (n != inlen) {
171 err = got_ferror(blobfile, GOT_ERR_IO);
172 goto done;
176 *id = malloc(sizeof(**id));
177 if (*id == NULL) {
178 err = got_error_from_errno("malloc");
179 goto done;
181 SHA1Final((*id)->sha1, &sha1_ctx);
183 if (fflush(blobfile) != 0) {
184 err = got_error_from_errno("fflush");
185 goto done;
187 rewind(blobfile);
189 err = create_object_file(*id, blobfile, repo);
190 done:
191 free(header);
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");
196 if (err) {
197 free(*id);
198 *id = NULL;
200 return err;
203 static const struct got_error *
204 mode2str(char *buf, size_t len, mode_t mode)
206 int ret;
207 ret = snprintf(buf, len, "%o ", mode);
208 if (ret == -1 || ret >= len)
209 return got_error(GOT_ERR_NO_SPACE);
210 return NULL;
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");
222 } else {
223 *name = strdup(te->name);
224 if (*name == NULL)
225 err = got_error_from_errno("strdup");
227 return err;
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;
235 char *name;
236 int cmp;
238 if (SIMPLEQ_EMPTY(&sorted->head)) {
239 SIMPLEQ_INSERT_HEAD(&sorted->head, new, entry);
240 sorted->nentries++;
241 return NULL;
244 err = te_git_name(&name, new);
245 if (err)
246 return err;
248 te = SIMPLEQ_FIRST(&sorted->head);
249 while (te) {
250 char *te_name;
251 if (prev == NULL) {
252 prev = te;
253 te = SIMPLEQ_NEXT(te, entry);
254 continue;
256 err = te_git_name(&te_name, te);
257 if (err)
258 goto done;
259 cmp = strcmp(te_name, name);
260 free(te_name);
261 if (cmp == 0) {
262 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
263 goto done;
265 if (cmp > 0) {
266 SIMPLEQ_INSERT_AFTER(&sorted->head, prev, new, entry);
267 sorted->nentries++;
268 break;
270 prev = te;
271 te = SIMPLEQ_NEXT(te, entry);
274 if (te == NULL) {
275 SIMPLEQ_INSERT_TAIL(&sorted->head, new, entry);
276 sorted->nentries++;
278 done:
279 free(name);
280 return err;
283 /*
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.
286 */
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));
295 if (*sorted == NULL)
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);
304 if (err)
305 break;
306 err = insert_tree_entry(*sorted, new);
307 if (err)
308 break;
311 if (err) {
312 free(*sorted);
313 *sorted = NULL;
316 return err;
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 ")];
325 SHA1_CTX sha1_ctx;
326 char *header = NULL;
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;
332 *id = NULL;
334 SHA1Init(&sha1_ctx);
336 err = sort_tree_entries_the_way_git_likes_it(&entries, tree_entries);
337 if (err)
338 return err;
340 SIMPLEQ_FOREACH(te, &entries->head, entry) {
341 err = mode2str(modebuf, sizeof(modebuf), te->mode);
342 if (err)
343 goto done;
344 len += strlen(modebuf) + strlen(te->name) + 1 +
345 SHA1_DIGEST_LENGTH;
348 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
349 err = got_error_from_errno("asprintf");
350 goto done;
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");
358 goto done;
361 n = fwrite(header, 1, headerlen, treefile);
362 if (n != headerlen) {
363 err = got_ferror(treefile, GOT_ERR_IO);
364 goto done;
367 SIMPLEQ_FOREACH(te, &entries->head, entry) {
368 err = mode2str(modebuf, sizeof(modebuf), te->mode);
369 if (err)
370 goto done;
371 len = strlen(modebuf);
372 n = fwrite(modebuf, 1, len, treefile);
373 if (n != len) {
374 err = got_ferror(treefile, GOT_ERR_IO);
375 goto done;
377 SHA1Update(&sha1_ctx, modebuf, len);
379 len = strlen(te->name) + 1; /* must include NUL */
380 n = fwrite(te->name, 1, len, treefile);
381 if (n != len) {
382 err = got_ferror(treefile, GOT_ERR_IO);
383 goto done;
385 SHA1Update(&sha1_ctx, te->name, len);
387 len = SHA1_DIGEST_LENGTH;
388 n = fwrite(te->id->sha1, 1, len, treefile);
389 if (n != len) {
390 err = got_ferror(treefile, GOT_ERR_IO);
391 goto done;
393 SHA1Update(&sha1_ctx, te->id->sha1, len);
396 *id = malloc(sizeof(**id));
397 if (*id == NULL) {
398 err = got_error_from_errno("malloc");
399 goto done;
401 SHA1Final((*id)->sha1, &sha1_ctx);
403 if (fflush(treefile) != 0) {
404 err = got_error_from_errno("fflush");
405 goto done;
407 rewind(treefile);
409 err = create_object_file(*id, treefile, repo);
410 done:
411 free(header);
412 got_object_tree_entries_close(entries);
413 free(entries);
414 if (treefile && fclose(treefile) != 0 && err == NULL)
415 err = got_error_from_errno("fclose");
416 if (err) {
417 free(*id);
418 *id = NULL;
420 return err;
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;
431 SHA1_CTX sha1_ctx;
432 char *header = NULL, *tree_str = NULL;
433 char *author_str = NULL, *committer_str = NULL;
434 char *id_str = NULL;
435 size_t headerlen, len = 0, n;
436 FILE *commitfile = NULL;
437 struct got_object_qid *qid;
438 char *msg0, *msg;
440 *id = NULL;
442 SHA1Init(&sha1_ctx);
444 msg0 = strdup(logmsg);
445 if (msg0 == NULL)
446 return got_error_from_errno("strdup");
447 msg = msg0;
449 while (isspace((unsigned char)msg[0]))
450 msg++;
451 len = strlen(msg);
452 while (len > 0 && isspace((unsigned char)msg[len - 1])) {
453 msg[len - 1] = '\0';
454 len--;
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)
464 == -1) {
465 err = got_error_from_errno("asprintf");
466 goto done;
469 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
470 nparents *
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");
476 goto done;
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");
484 goto done;
487 n = fwrite(header, 1, headerlen, commitfile);
488 if (n != headerlen) {
489 err = got_ferror(commitfile, GOT_ERR_IO);
490 goto done;
493 err = got_object_id_str(&id_str, tree_id);
494 if (err)
495 goto done;
496 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
497 == -1) {
498 err = got_error_from_errno("asprintf");
499 goto done;
501 len = strlen(tree_str);
502 SHA1Update(&sha1_ctx, tree_str, len);
503 n = fwrite(tree_str, 1, len, commitfile);
504 if (n != len) {
505 err = got_ferror(commitfile, GOT_ERR_IO);
506 goto done;
509 if (parent_ids) {
510 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
511 char *parent_str = NULL;
513 free(id_str);
515 err = got_object_id_str(&id_str, qid->id);
516 if (err)
517 goto done;
518 if (asprintf(&parent_str, "%s%s\n",
519 GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
520 err = got_error_from_errno("asprintf");
521 goto done;
523 len = strlen(parent_str);
524 SHA1Update(&sha1_ctx, parent_str, len);
525 n = fwrite(parent_str, 1, len, commitfile);
526 if (n != len) {
527 err = got_ferror(commitfile, GOT_ERR_IO);
528 free(parent_str);
529 goto done;
531 free(parent_str);
535 len = strlen(author_str);
536 SHA1Update(&sha1_ctx, author_str, len);
537 n = fwrite(author_str, 1, len, commitfile);
538 if (n != len) {
539 err = got_ferror(commitfile, GOT_ERR_IO);
540 goto done;
543 len = strlen(committer_str);
544 SHA1Update(&sha1_ctx, committer_str, len);
545 n = fwrite(committer_str, 1, len, commitfile);
546 if (n != len) {
547 err = got_ferror(commitfile, GOT_ERR_IO);
548 goto done;
551 SHA1Update(&sha1_ctx, "\n", 1);
552 n = fwrite("\n", 1, 1, commitfile);
553 if (n != 1) {
554 err = got_ferror(commitfile, GOT_ERR_IO);
555 goto done;
558 len = strlen(msg);
559 SHA1Update(&sha1_ctx, msg, len);
560 n = fwrite(msg, 1, len, commitfile);
561 if (n != len) {
562 err = got_ferror(commitfile, GOT_ERR_IO);
563 goto done;
566 SHA1Update(&sha1_ctx, "\n", 1);
567 n = fwrite("\n", 1, 1, commitfile);
568 if (n != 1) {
569 err = got_ferror(commitfile, GOT_ERR_IO);
570 goto done;
573 *id = malloc(sizeof(**id));
574 if (*id == NULL) {
575 err = got_error_from_errno("malloc");
576 goto done;
578 SHA1Final((*id)->sha1, &sha1_ctx);
580 if (fflush(commitfile) != 0) {
581 err = got_error_from_errno("fflush");
582 goto done;
584 rewind(commitfile);
586 err = create_object_file(*id, commitfile, repo);
587 done:
588 free(msg0);
589 free(header);
590 free(tree_str);
591 free(author_str);
592 free(committer_str);
593 if (commitfile && fclose(commitfile) != 0 && err == NULL)
594 err = got_error_from_errno("fclose");
595 if (err) {
596 free(*id);
597 *id = NULL;
599 return err;