2 44edeea7 2019-04-11 stsp * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
4 44edeea7 2019-04-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 44edeea7 2019-04-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 44edeea7 2019-04-11 stsp * copyright notice and this permission notice appear in all copies.
8 44edeea7 2019-04-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 44edeea7 2019-04-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 44edeea7 2019-04-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 44edeea7 2019-04-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 44edeea7 2019-04-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 44edeea7 2019-04-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 44edeea7 2019-04-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 44edeea7 2019-04-11 stsp #include <sys/types.h>
18 44edeea7 2019-04-11 stsp #include <sys/stat.h>
19 44edeea7 2019-04-11 stsp #include <sys/queue.h>
20 4d5ee956 2022-07-02 jrick #include <sys/wait.h>
22 787c8eb6 2019-07-11 stsp #include <ctype.h>
23 51130c02 2019-04-13 stsp #include <errno.h>
24 44edeea7 2019-04-11 stsp #include <fcntl.h>
25 56e0773d 2019-11-28 stsp #include <limits.h>
26 44edeea7 2019-04-11 stsp #include <stdio.h>
27 44edeea7 2019-04-11 stsp #include <stdlib.h>
28 44edeea7 2019-04-11 stsp #include <string.h>
29 44edeea7 2019-04-11 stsp #include <stdint.h>
30 44edeea7 2019-04-11 stsp #include <sha1.h>
31 69c6accf 2023-02-04 op #include <sha2.h>
32 a14a8cf6 2019-04-11 stsp #include <unistd.h>
33 44edeea7 2019-04-11 stsp #include <zlib.h>
35 44edeea7 2019-04-11 stsp #include "got_error.h"
36 44edeea7 2019-04-11 stsp #include "got_object.h"
37 44edeea7 2019-04-11 stsp #include "got_repository.h"
38 44edeea7 2019-04-11 stsp #include "got_opentemp.h"
39 324d37e7 2019-05-11 stsp #include "got_path.h"
40 4d5ee956 2022-07-02 jrick #include "got_sigs.h"
42 1362b0e3 2023-02-04 op #include "got_lib_hash.h"
43 44edeea7 2019-04-11 stsp #include "got_lib_deflate.h"
44 44edeea7 2019-04-11 stsp #include "got_lib_delta.h"
45 44edeea7 2019-04-11 stsp #include "got_lib_object.h"
46 6af1ccbd 2019-08-16 stsp #include "got_lib_object_parse.h"
47 44edeea7 2019-04-11 stsp #include "got_lib_lockfile.h"
49 336075a4 2022-06-25 op #include "got_lib_object_create.h"
51 4d5ee956 2022-07-02 jrick #include "buf.h"
53 44edeea7 2019-04-11 stsp #ifndef nitems
54 44edeea7 2019-04-11 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
57 ac1c5662 2019-04-13 stsp static const struct got_error *
58 76f564d5 2019-04-14 stsp create_object_file(struct got_object_id *id, FILE *content,
59 72840534 2022-01-19 stsp off_t content_len, struct got_repository *repo)
61 ac1c5662 2019-04-13 stsp const struct got_error *err = NULL, *unlock_err = NULL;
62 c6f826b4 2019-04-13 stsp char *objpath = NULL, *tmppath = NULL;
63 c6f826b4 2019-04-13 stsp FILE *tmpfile = NULL;
64 ac1c5662 2019-04-13 stsp struct got_lockfile *lf = NULL;
65 72840534 2022-01-19 stsp off_t tmplen = 0;
67 ac1c5662 2019-04-13 stsp err = got_object_get_path(&objpath, id, repo);
71 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &tmpfile, objpath, "");
73 ac1c5662 2019-04-13 stsp char *parent_path;
74 ac1c5662 2019-04-13 stsp if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
76 ac1c5662 2019-04-13 stsp err = got_path_dirname(&parent_path, objpath);
79 ac1c5662 2019-04-13 stsp err = got_path_mkdir(parent_path);
80 ac1c5662 2019-04-13 stsp free(parent_path);
83 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &tmpfile, objpath, "");
88 3818e3c4 2020-11-01 naddy if (fchmod(fileno(tmpfile), GOT_DEFAULT_FILE_MODE) != 0) {
89 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", tmppath);
93 72840534 2022-01-19 stsp err = got_deflate_to_file(&tmplen, content, content_len, tmpfile, NULL);
97 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, objpath, -1);
101 c6f826b4 2019-04-13 stsp if (rename(tmppath, objpath) != 0) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, objpath);
105 c6f826b4 2019-04-13 stsp free(tmppath);
106 c6f826b4 2019-04-13 stsp tmppath = NULL;
108 ac1c5662 2019-04-13 stsp free(objpath);
109 c6f826b4 2019-04-13 stsp if (tmppath) {
110 c6f826b4 2019-04-13 stsp if (unlink(tmppath) != 0 && err == NULL)
111 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", tmppath);
112 c6f826b4 2019-04-13 stsp free(tmppath);
114 56b63ca4 2021-01-22 stsp if (tmpfile && fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
117 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
118 ac1c5662 2019-04-13 stsp return err ? err : unlock_err;
121 44edeea7 2019-04-11 stsp const struct got_error *
122 0a22ca1a 2020-09-23 stsp got_object_blob_file_create(struct got_object_id **id, FILE **blobfile,
123 72840534 2022-01-19 stsp off_t *blobsize, const char *ondisk_path)
125 ac1c5662 2019-04-13 stsp const struct got_error *err = NULL;
126 ac1c5662 2019-04-13 stsp char *header = NULL;
127 44edeea7 2019-04-11 stsp int fd = -1;
128 44edeea7 2019-04-11 stsp struct stat sb;
129 3093e2d7 2023-02-04 op SHA1_CTX hash_ctx;
130 ac1c5662 2019-04-13 stsp size_t headerlen = 0, n;
132 44edeea7 2019-04-11 stsp *id = NULL;
133 0a22ca1a 2020-09-23 stsp *blobfile = NULL;
134 72840534 2022-01-19 stsp *blobsize = 0;
136 3093e2d7 2023-02-04 op SHA1Init(&hash_ctx);
138 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
139 3d9a4ec4 2020-07-23 stsp if (fd == -1) {
140 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
141 3d9a4ec4 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
143 3d9a4ec4 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
144 3d9a4ec4 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
147 3d9a4ec4 2020-07-23 stsp } else if (fstat(fd, &sb) == -1) {
148 638f9024 2019-05-13 stsp err = got_error_from_errno2("fstat", ondisk_path);
152 44edeea7 2019-04-11 stsp if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
153 1367695b 2020-09-26 naddy (long long)sb.st_size) == -1) {
154 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
157 ffb286fd 2019-04-11 stsp headerlen = strlen(header) + 1;
158 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, header, headerlen);
160 0a22ca1a 2020-09-23 stsp *blobfile = got_opentemp();
161 0a22ca1a 2020-09-23 stsp if (*blobfile == NULL) {
162 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
166 0a22ca1a 2020-09-23 stsp n = fwrite(header, 1, headerlen, *blobfile);
167 ac1c5662 2019-04-13 stsp if (n != headerlen) {
168 0a22ca1a 2020-09-23 stsp err = got_ferror(*blobfile, GOT_ERR_IO);
171 72840534 2022-01-19 stsp *blobsize += headerlen;
173 2f63b34c 2020-07-23 stsp char buf[PATH_MAX * 8];
174 a14a8cf6 2019-04-11 stsp ssize_t inlen;
176 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
177 3d9a4ec4 2020-07-23 stsp inlen = readlink(ondisk_path, buf, sizeof(buf));
178 3d9a4ec4 2020-07-23 stsp if (inlen == -1) {
179 3d9a4ec4 2020-07-23 stsp err = got_error_from_errno("readlink");
183 3d9a4ec4 2020-07-23 stsp inlen = read(fd, buf, sizeof(buf));
184 3d9a4ec4 2020-07-23 stsp if (inlen == -1) {
185 3d9a4ec4 2020-07-23 stsp err = got_error_from_errno("read");
189 a14a8cf6 2019-04-11 stsp if (inlen == 0)
190 a14a8cf6 2019-04-11 stsp break; /* EOF */
191 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, buf, inlen);
192 0a22ca1a 2020-09-23 stsp n = fwrite(buf, 1, inlen, *blobfile);
193 ac1c5662 2019-04-13 stsp if (n != inlen) {
194 0a22ca1a 2020-09-23 stsp err = got_ferror(*blobfile, GOT_ERR_IO);
197 72840534 2022-01-19 stsp *blobsize += n;
198 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(sb.st_mode))
202 44edeea7 2019-04-11 stsp *id = malloc(sizeof(**id));
203 44edeea7 2019-04-11 stsp if (*id == NULL) {
204 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
207 3093e2d7 2023-02-04 op SHA1Final((*id)->hash, &hash_ctx);
208 dfe0a3d8 2023-02-04 op (*id)->algo = GOT_HASH_SHA1;
210 0a22ca1a 2020-09-23 stsp if (fflush(*blobfile) != 0) {
211 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
214 0a22ca1a 2020-09-23 stsp rewind(*blobfile);
216 44edeea7 2019-04-11 stsp free(header);
217 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
218 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
221 0a22ca1a 2020-09-23 stsp *id = NULL;
222 0a22ca1a 2020-09-23 stsp if (*blobfile) {
223 0a22ca1a 2020-09-23 stsp fclose(*blobfile);
224 0a22ca1a 2020-09-23 stsp *blobfile = NULL;
227 0a22ca1a 2020-09-23 stsp return err;
230 0a22ca1a 2020-09-23 stsp const struct got_error *
231 0a22ca1a 2020-09-23 stsp got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
232 0a22ca1a 2020-09-23 stsp struct got_repository *repo)
234 0a22ca1a 2020-09-23 stsp const struct got_error *err = NULL;
235 0a22ca1a 2020-09-23 stsp FILE *blobfile = NULL;
236 72840534 2022-01-19 stsp off_t blobsize;
238 72840534 2022-01-19 stsp err = got_object_blob_file_create(id, &blobfile, &blobsize,
239 72840534 2022-01-19 stsp ondisk_path);
241 0a22ca1a 2020-09-23 stsp return err;
243 72840534 2022-01-19 stsp err = create_object_file(*id, blobfile, blobsize, repo);
244 0a22ca1a 2020-09-23 stsp if (fclose(blobfile) == EOF && err == NULL)
245 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
248 44edeea7 2019-04-11 stsp *id = NULL;
250 ac1c5662 2019-04-13 stsp return err;
253 f91abf81 2019-04-14 stsp static const struct got_error *
254 7aadece8 2020-05-17 stsp te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
257 f7b97ccb 2020-04-14 stsp mode_t mode;
260 f7b97ccb 2020-04-14 stsp * Some Git implementations are picky about modes seen in tree entries.
261 f7b97ccb 2020-04-14 stsp * For best compatibility we normalize the file/directory mode here.
263 7aadece8 2020-05-17 stsp if (S_ISREG(te->mode)) {
264 f7b97ccb 2020-04-14 stsp mode = GOT_DEFAULT_FILE_MODE;
265 7aadece8 2020-05-17 stsp if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
266 aaffadfc 2020-05-05 stsp mode |= S_IXUSR | S_IXGRP | S_IXOTH;
267 7aadece8 2020-05-17 stsp } else if (got_object_tree_entry_is_submodule(te))
268 7aadece8 2020-05-17 stsp mode = S_IFDIR | S_IFLNK;
269 3d9a4ec4 2020-07-23 stsp else if (S_ISLNK(te->mode))
270 3d9a4ec4 2020-07-23 stsp mode = S_IFLNK; /* Git leaves all the other bits unset. */
271 7aadece8 2020-05-17 stsp else if (S_ISDIR(te->mode))
272 aaffadfc 2020-05-05 stsp mode = S_IFDIR; /* Git leaves all the other bits unset. */
274 f7b97ccb 2020-04-14 stsp return got_error(GOT_ERR_BAD_FILETYPE);
276 f91abf81 2019-04-14 stsp ret = snprintf(buf, len, "%o ", mode);
277 438d0cc3 2022-08-16 op if (ret < 0 || (size_t)ret >= len)
278 f91abf81 2019-04-14 stsp return got_error(GOT_ERR_NO_SPACE);
279 f91abf81 2019-04-14 stsp return NULL;
283 6af1ccbd 2019-08-16 stsp * Git expects directory tree entries to be sorted with an imaginary slash
284 6af1ccbd 2019-08-16 stsp * appended to their name, and will break otherwise. Let's be nice.
285 56e0773d 2019-11-28 stsp * This function is intended to be used with mergesort(3) to sort an
286 56e0773d 2019-11-28 stsp * array of pointers to struct got_tree_entry objects.
289 56e0773d 2019-11-28 stsp sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
291 56e0773d 2019-11-28 stsp struct got_tree_entry * const *te1 = arg1;
292 56e0773d 2019-11-28 stsp struct got_tree_entry * const *te2 = arg2;
293 2c98ee28 2019-11-29 stsp char name1[NAME_MAX + 2];
294 2c98ee28 2019-11-29 stsp char name2[NAME_MAX + 2];
296 56e0773d 2019-11-28 stsp strlcpy(name1, (*te1)->name, sizeof(name1));
297 56e0773d 2019-11-28 stsp strlcpy(name2, (*te2)->name, sizeof(name2));
298 56e0773d 2019-11-28 stsp if (S_ISDIR((*te1)->mode))
299 56e0773d 2019-11-28 stsp strlcat(name1, "/", sizeof(name1));
300 56e0773d 2019-11-28 stsp if (S_ISDIR((*te2)->mode))
301 56e0773d 2019-11-28 stsp strlcat(name2, "/", sizeof(name2));
302 56e0773d 2019-11-28 stsp return strcmp(name1, name2);
305 f91abf81 2019-04-14 stsp const struct got_error *
306 f91abf81 2019-04-14 stsp got_object_tree_create(struct got_object_id **id,
307 56e0773d 2019-11-28 stsp struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
309 f91abf81 2019-04-14 stsp const struct got_error *err = NULL;
310 f91abf81 2019-04-14 stsp char modebuf[sizeof("100644 ")];
311 3093e2d7 2023-02-04 op SHA1_CTX hash_ctx;
312 f91abf81 2019-04-14 stsp char *header = NULL;
313 f91abf81 2019-04-14 stsp size_t headerlen, len = 0, n;
314 f91abf81 2019-04-14 stsp FILE *treefile = NULL;
315 72840534 2022-01-19 stsp off_t treesize = 0;
316 56e0773d 2019-11-28 stsp struct got_pathlist_entry *pe;
317 56e0773d 2019-11-28 stsp struct got_tree_entry **sorted_entries;
318 f91abf81 2019-04-14 stsp struct got_tree_entry *te;
321 f91abf81 2019-04-14 stsp *id = NULL;
323 3093e2d7 2023-02-04 op SHA1Init(&hash_ctx);
325 56e0773d 2019-11-28 stsp sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
326 56e0773d 2019-11-28 stsp if (sorted_entries == NULL)
327 56e0773d 2019-11-28 stsp return got_error_from_errno("calloc");
330 56e0773d 2019-11-28 stsp TAILQ_FOREACH(pe, paths, entry)
331 56e0773d 2019-11-28 stsp sorted_entries[i++] = pe->data;
332 56e0773d 2019-11-28 stsp mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
333 56e0773d 2019-11-28 stsp sort_tree_entries_the_way_git_likes_it);
335 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
336 56e0773d 2019-11-28 stsp te = sorted_entries[i];
337 7aadece8 2020-05-17 stsp err = te_mode2str(modebuf, sizeof(modebuf), te);
340 f91abf81 2019-04-14 stsp len += strlen(modebuf) + strlen(te->name) + 1 +
341 f91abf81 2019-04-14 stsp SHA1_DIGEST_LENGTH;
344 f91abf81 2019-04-14 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
345 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
348 f91abf81 2019-04-14 stsp headerlen = strlen(header) + 1;
349 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, header, headerlen);
351 f91abf81 2019-04-14 stsp treefile = got_opentemp();
352 f91abf81 2019-04-14 stsp if (treefile == NULL) {
353 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
357 f91abf81 2019-04-14 stsp n = fwrite(header, 1, headerlen, treefile);
358 f91abf81 2019-04-14 stsp if (n != headerlen) {
359 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
362 72840534 2022-01-19 stsp treesize += headerlen;
364 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
365 56e0773d 2019-11-28 stsp te = sorted_entries[i];
366 7aadece8 2020-05-17 stsp err = te_mode2str(modebuf, sizeof(modebuf), te);
369 f91abf81 2019-04-14 stsp len = strlen(modebuf);
370 f91abf81 2019-04-14 stsp n = fwrite(modebuf, 1, len, treefile);
371 f91abf81 2019-04-14 stsp if (n != len) {
372 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
375 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, modebuf, len);
376 72840534 2022-01-19 stsp treesize += n;
378 f91abf81 2019-04-14 stsp len = strlen(te->name) + 1; /* must include NUL */
379 f91abf81 2019-04-14 stsp n = fwrite(te->name, 1, len, treefile);
380 f91abf81 2019-04-14 stsp if (n != len) {
381 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
384 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, te->name, len);
385 72840534 2022-01-19 stsp treesize += n;
387 f91abf81 2019-04-14 stsp len = SHA1_DIGEST_LENGTH;
388 3093e2d7 2023-02-04 op n = fwrite(te->id.hash, 1, len, treefile);
389 f91abf81 2019-04-14 stsp if (n != len) {
390 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
393 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, te->id.hash, len);
394 72840534 2022-01-19 stsp treesize += n;
397 f91abf81 2019-04-14 stsp *id = malloc(sizeof(**id));
398 f91abf81 2019-04-14 stsp if (*id == NULL) {
399 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
402 3093e2d7 2023-02-04 op SHA1Final((*id)->hash, &hash_ctx);
403 dfe0a3d8 2023-02-04 op (*id)->algo = GOT_HASH_SHA1;
405 f91abf81 2019-04-14 stsp if (fflush(treefile) != 0) {
406 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
409 f91abf81 2019-04-14 stsp rewind(treefile);
411 72840534 2022-01-19 stsp err = create_object_file(*id, treefile, treesize, repo);
413 f91abf81 2019-04-14 stsp free(header);
414 56e0773d 2019-11-28 stsp free(sorted_entries);
415 56b63ca4 2021-01-22 stsp if (treefile && fclose(treefile) == EOF && err == NULL)
416 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
419 de18fc63 2019-05-09 stsp *id = NULL;
421 de18fc63 2019-05-09 stsp return err;
424 de18fc63 2019-05-09 stsp const struct got_error *
425 de18fc63 2019-05-09 stsp got_object_commit_create(struct got_object_id **id,
426 de18fc63 2019-05-09 stsp struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
427 de18fc63 2019-05-09 stsp int nparents, const char *author, time_t author_time,
428 de18fc63 2019-05-09 stsp const char *committer, time_t committer_time,
429 de18fc63 2019-05-09 stsp const char *logmsg, struct got_repository *repo)
431 de18fc63 2019-05-09 stsp const struct got_error *err = NULL;
432 3093e2d7 2023-02-04 op SHA1_CTX hash_ctx;
433 de18fc63 2019-05-09 stsp char *header = NULL, *tree_str = NULL;
434 de18fc63 2019-05-09 stsp char *author_str = NULL, *committer_str = NULL;
435 de18fc63 2019-05-09 stsp char *id_str = NULL;
436 de18fc63 2019-05-09 stsp size_t headerlen, len = 0, n;
437 de18fc63 2019-05-09 stsp FILE *commitfile = NULL;
438 72840534 2022-01-19 stsp off_t commitsize = 0;
439 de18fc63 2019-05-09 stsp struct got_object_qid *qid;
440 787c8eb6 2019-07-11 stsp char *msg0, *msg;
442 de18fc63 2019-05-09 stsp *id = NULL;
444 3093e2d7 2023-02-04 op SHA1Init(&hash_ctx);
446 787c8eb6 2019-07-11 stsp msg0 = strdup(logmsg);
447 787c8eb6 2019-07-11 stsp if (msg0 == NULL)
448 787c8eb6 2019-07-11 stsp return got_error_from_errno("strdup");
449 787c8eb6 2019-07-11 stsp msg = msg0;
451 10796104 2019-07-11 stsp while (isspace((unsigned char)msg[0]))
453 787c8eb6 2019-07-11 stsp len = strlen(msg);
454 10796104 2019-07-11 stsp while (len > 0 && isspace((unsigned char)msg[len - 1])) {
455 787c8eb6 2019-07-11 stsp msg[len - 1] = '\0';
459 de18fc63 2019-05-09 stsp if (asprintf(&author_str, "%s%s %lld +0000\n",
460 c9e76cc4 2022-07-21 florian GOT_COMMIT_LABEL_AUTHOR, author, (long long)author_time) == -1) {
461 c9e76cc4 2022-07-21 florian err = got_error_from_errno("asprintf");
462 c9e76cc4 2022-07-21 florian goto done;
465 de18fc63 2019-05-09 stsp if (asprintf(&committer_str, "%s%s %lld +0000\n",
466 de18fc63 2019-05-09 stsp GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
467 1367695b 2020-09-26 naddy (long long)(committer ? committer_time : author_time))
469 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
473 de18fc63 2019-05-09 stsp len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
475 de18fc63 2019-05-09 stsp (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
476 787c8eb6 2019-07-11 stsp + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
478 de18fc63 2019-05-09 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
479 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
482 de18fc63 2019-05-09 stsp headerlen = strlen(header) + 1;
483 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, header, headerlen);
485 de18fc63 2019-05-09 stsp commitfile = got_opentemp();
486 de18fc63 2019-05-09 stsp if (commitfile == NULL) {
487 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
491 de18fc63 2019-05-09 stsp n = fwrite(header, 1, headerlen, commitfile);
492 de18fc63 2019-05-09 stsp if (n != headerlen) {
493 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
496 72840534 2022-01-19 stsp commitsize += headerlen;
498 de18fc63 2019-05-09 stsp err = got_object_id_str(&id_str, tree_id);
501 de18fc63 2019-05-09 stsp if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
503 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
506 de18fc63 2019-05-09 stsp len = strlen(tree_str);
507 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, tree_str, len);
508 de18fc63 2019-05-09 stsp n = fwrite(tree_str, 1, len, commitfile);
509 de18fc63 2019-05-09 stsp if (n != len) {
510 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
513 72840534 2022-01-19 stsp commitsize += n;
515 3ce1b845 2019-07-15 stsp if (parent_ids) {
516 f259c4c1 2021-09-24 stsp free(id_str);
517 f259c4c1 2021-09-24 stsp id_str = NULL;
518 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, parent_ids, entry) {
519 3ce1b845 2019-07-15 stsp char *parent_str = NULL;
521 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
524 3ce1b845 2019-07-15 stsp if (asprintf(&parent_str, "%s%s\n",
525 3ce1b845 2019-07-15 stsp GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
526 3ce1b845 2019-07-15 stsp err = got_error_from_errno("asprintf");
529 3ce1b845 2019-07-15 stsp len = strlen(parent_str);
530 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, parent_str, len);
531 3ce1b845 2019-07-15 stsp n = fwrite(parent_str, 1, len, commitfile);
532 3ce1b845 2019-07-15 stsp if (n != len) {
533 3ce1b845 2019-07-15 stsp err = got_ferror(commitfile, GOT_ERR_IO);
534 3ce1b845 2019-07-15 stsp free(parent_str);
537 72840534 2022-01-19 stsp commitsize += n;
538 de18fc63 2019-05-09 stsp free(parent_str);
539 f259c4c1 2021-09-24 stsp free(id_str);
540 f259c4c1 2021-09-24 stsp id_str = NULL;
544 de18fc63 2019-05-09 stsp len = strlen(author_str);
545 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, author_str, len);
546 de18fc63 2019-05-09 stsp n = fwrite(author_str, 1, len, commitfile);
547 de18fc63 2019-05-09 stsp if (n != len) {
548 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
551 72840534 2022-01-19 stsp commitsize += n;
553 de18fc63 2019-05-09 stsp len = strlen(committer_str);
554 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, committer_str, len);
555 de18fc63 2019-05-09 stsp n = fwrite(committer_str, 1, len, commitfile);
556 de18fc63 2019-05-09 stsp if (n != len) {
557 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
560 72840534 2022-01-19 stsp commitsize += n;
562 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, "\n", 1);
563 de18fc63 2019-05-09 stsp n = fwrite("\n", 1, 1, commitfile);
564 de18fc63 2019-05-09 stsp if (n != 1) {
565 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
568 72840534 2022-01-19 stsp commitsize += n;
570 787c8eb6 2019-07-11 stsp len = strlen(msg);
571 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, msg, len);
572 787c8eb6 2019-07-11 stsp n = fwrite(msg, 1, len, commitfile);
573 de18fc63 2019-05-09 stsp if (n != len) {
574 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
577 72840534 2022-01-19 stsp commitsize += n;
579 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, "\n", 1);
580 de18fc63 2019-05-09 stsp n = fwrite("\n", 1, 1, commitfile);
581 de18fc63 2019-05-09 stsp if (n != 1) {
582 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
585 72840534 2022-01-19 stsp commitsize += n;
587 de18fc63 2019-05-09 stsp *id = malloc(sizeof(**id));
588 de18fc63 2019-05-09 stsp if (*id == NULL) {
589 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
592 3093e2d7 2023-02-04 op SHA1Final((*id)->hash, &hash_ctx);
593 dfe0a3d8 2023-02-04 op (*id)->algo = GOT_HASH_SHA1;
595 de18fc63 2019-05-09 stsp if (fflush(commitfile) != 0) {
596 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
599 de18fc63 2019-05-09 stsp rewind(commitfile);
601 72840534 2022-01-19 stsp err = create_object_file(*id, commitfile, commitsize, repo);
603 f259c4c1 2021-09-24 stsp free(id_str);
604 787c8eb6 2019-07-11 stsp free(msg0);
605 de18fc63 2019-05-09 stsp free(header);
606 de18fc63 2019-05-09 stsp free(tree_str);
607 de18fc63 2019-05-09 stsp free(author_str);
608 de18fc63 2019-05-09 stsp free(committer_str);
609 56b63ca4 2021-01-22 stsp if (commitfile && fclose(commitfile) == EOF && err == NULL)
610 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("fclose");
613 8e7bd50a 2019-08-22 stsp *id = NULL;
615 8e7bd50a 2019-08-22 stsp return err;
618 8e7bd50a 2019-08-22 stsp const struct got_error *
619 8e7bd50a 2019-08-22 stsp got_object_tag_create(struct got_object_id **id,
620 8e7bd50a 2019-08-22 stsp const char *tag_name, struct got_object_id *object_id, const char *tagger,
621 91d845ad 2022-07-03 jrick time_t tagger_time, const char *tagmsg, const char *signer_id,
622 4d5ee956 2022-07-02 jrick struct got_repository *repo, int verbosity)
624 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
625 3093e2d7 2023-02-04 op SHA1_CTX hash_ctx;
626 8e7bd50a 2019-08-22 stsp char *header = NULL;
627 8e7bd50a 2019-08-22 stsp char *tag_str = NULL, *tagger_str = NULL;
628 8e7bd50a 2019-08-22 stsp char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
629 4d5ee956 2022-07-02 jrick size_t headerlen, len = 0, sig_len = 0, n;
630 8e7bd50a 2019-08-22 stsp FILE *tagfile = NULL;
631 72840534 2022-01-19 stsp off_t tagsize = 0;
632 8e7bd50a 2019-08-22 stsp char *msg0 = NULL, *msg;
633 8e7bd50a 2019-08-22 stsp const char *obj_type_str;
634 8e7bd50a 2019-08-22 stsp int obj_type;
635 4d5ee956 2022-07-02 jrick BUF *buf = NULL;
637 8e7bd50a 2019-08-22 stsp *id = NULL;
639 3093e2d7 2023-02-04 op SHA1Init(&hash_ctx);
641 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&id_str, object_id);
644 8e7bd50a 2019-08-22 stsp if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
645 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
649 8e7bd50a 2019-08-22 stsp err = got_object_get_type(&obj_type, repo, object_id);
653 8e7bd50a 2019-08-22 stsp switch (obj_type) {
654 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
655 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_BLOB;
657 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
658 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_TREE;
660 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
661 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_COMMIT;
663 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
664 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_TAG;
667 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_OBJ_TYPE);
671 8e7bd50a 2019-08-22 stsp if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
672 8e7bd50a 2019-08-22 stsp obj_type_str) == -1) {
673 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
677 8e7bd50a 2019-08-22 stsp if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
678 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
682 8e7bd50a 2019-08-22 stsp if (asprintf(&tagger_str, "%s%s %lld +0000\n",
683 1367695b 2020-09-26 naddy GOT_TAG_LABEL_TAGGER, tagger, (long long)tagger_time) == -1)
684 8e7bd50a 2019-08-22 stsp return got_error_from_errno("asprintf");
686 8e7bd50a 2019-08-22 stsp msg0 = strdup(tagmsg);
687 8e7bd50a 2019-08-22 stsp if (msg0 == NULL) {
688 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
691 8e7bd50a 2019-08-22 stsp msg = msg0;
693 8e7bd50a 2019-08-22 stsp while (isspace((unsigned char)msg[0]))
696 91d845ad 2022-07-03 jrick if (signer_id) {
697 4d5ee956 2022-07-02 jrick pid_t pid;
698 4d5ee956 2022-07-02 jrick size_t len;
699 4d5ee956 2022-07-02 jrick int in_fd, out_fd;
700 4d5ee956 2022-07-02 jrick int status;
702 4d5ee956 2022-07-02 jrick err = buf_alloc(&buf, 0);
704 4d5ee956 2022-07-02 jrick goto done;
706 4d5ee956 2022-07-02 jrick /* signed message */
707 4d5ee956 2022-07-02 jrick err = buf_puts(&len, buf, obj_str);
709 4d5ee956 2022-07-02 jrick goto done;
710 4d5ee956 2022-07-02 jrick err = buf_puts(&len, buf, type_str);
712 4d5ee956 2022-07-02 jrick goto done;
713 4d5ee956 2022-07-02 jrick err = buf_puts(&len, buf, tag_str);
715 4d5ee956 2022-07-02 jrick goto done;
716 4d5ee956 2022-07-02 jrick err = buf_puts(&len, buf, tagger_str);
718 4d5ee956 2022-07-02 jrick goto done;
719 4d5ee956 2022-07-02 jrick err = buf_putc(buf, '\n');
721 4d5ee956 2022-07-02 jrick goto done;
722 4d5ee956 2022-07-02 jrick err = buf_puts(&len, buf, msg);
724 4d5ee956 2022-07-02 jrick goto done;
725 4d5ee956 2022-07-02 jrick err = buf_putc(buf, '\n');
727 4d5ee956 2022-07-02 jrick goto done;
729 91d845ad 2022-07-03 jrick err = got_sigs_sign_tag_ssh(&pid, &in_fd, &out_fd, signer_id,
730 4d5ee956 2022-07-02 jrick verbosity);
732 4d5ee956 2022-07-02 jrick goto done;
733 4d5ee956 2022-07-02 jrick if (buf_write_fd(buf, in_fd) == -1) {
734 4d5ee956 2022-07-02 jrick err = got_error_from_errno("write");
735 4d5ee956 2022-07-02 jrick goto done;
737 4d5ee956 2022-07-02 jrick if (close(in_fd) == -1) {
738 4d5ee956 2022-07-02 jrick err = got_error_from_errno("close");
739 4d5ee956 2022-07-02 jrick goto done;
742 4d5ee956 2022-07-02 jrick if (waitpid(pid, &status, 0) == -1) {
743 4d5ee956 2022-07-02 jrick err = got_error_from_errno("waitpid");
744 4d5ee956 2022-07-02 jrick goto done;
746 91d845ad 2022-07-03 jrick if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
747 91d845ad 2022-07-03 jrick err = got_error(GOT_ERR_SIGNING_TAG);
748 4d5ee956 2022-07-02 jrick goto done;
751 4d5ee956 2022-07-02 jrick buf_empty(buf);
752 1bd8e3b1 2022-07-12 jrick err = buf_load_fd(&buf, out_fd);
754 4d5ee956 2022-07-02 jrick goto done;
755 40831471 2022-08-24 jrick sig_len = buf_len(buf);
756 4d5ee956 2022-07-02 jrick if (close(out_fd) == -1) {
757 4d5ee956 2022-07-02 jrick err = got_error_from_errno("close");
758 4d5ee956 2022-07-02 jrick goto done;
762 4d5ee956 2022-07-02 jrick len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
763 4d5ee956 2022-07-02 jrick strlen(tagger_str) + 1 + strlen(msg) + 1 + sig_len;
764 8e7bd50a 2019-08-22 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
765 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
769 8e7bd50a 2019-08-22 stsp headerlen = strlen(header) + 1;
770 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, header, headerlen);
772 8e7bd50a 2019-08-22 stsp tagfile = got_opentemp();
773 8e7bd50a 2019-08-22 stsp if (tagfile == NULL) {
774 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_opentemp");
778 8e7bd50a 2019-08-22 stsp n = fwrite(header, 1, headerlen, tagfile);
779 8e7bd50a 2019-08-22 stsp if (n != headerlen) {
780 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
783 72840534 2022-01-19 stsp tagsize += headerlen;
784 8e7bd50a 2019-08-22 stsp len = strlen(obj_str);
785 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, obj_str, len);
786 8e7bd50a 2019-08-22 stsp n = fwrite(obj_str, 1, len, tagfile);
787 8e7bd50a 2019-08-22 stsp if (n != len) {
788 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
791 72840534 2022-01-19 stsp tagsize += n;
792 8e7bd50a 2019-08-22 stsp len = strlen(type_str);
793 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, type_str, len);
794 8e7bd50a 2019-08-22 stsp n = fwrite(type_str, 1, len, tagfile);
795 8e7bd50a 2019-08-22 stsp if (n != len) {
796 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
799 72840534 2022-01-19 stsp tagsize += n;
801 8e7bd50a 2019-08-22 stsp len = strlen(tag_str);
802 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, tag_str, len);
803 8e7bd50a 2019-08-22 stsp n = fwrite(tag_str, 1, len, tagfile);
804 8e7bd50a 2019-08-22 stsp if (n != len) {
805 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
808 72840534 2022-01-19 stsp tagsize += n;
810 8e7bd50a 2019-08-22 stsp len = strlen(tagger_str);
811 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, tagger_str, len);
812 8e7bd50a 2019-08-22 stsp n = fwrite(tagger_str, 1, len, tagfile);
813 8e7bd50a 2019-08-22 stsp if (n != len) {
814 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
817 72840534 2022-01-19 stsp tagsize += n;
819 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, "\n", 1);
820 8e7bd50a 2019-08-22 stsp n = fwrite("\n", 1, 1, tagfile);
821 8e7bd50a 2019-08-22 stsp if (n != 1) {
822 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
825 72840534 2022-01-19 stsp tagsize += n;
827 8e7bd50a 2019-08-22 stsp len = strlen(msg);
828 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, msg, len);
829 8e7bd50a 2019-08-22 stsp n = fwrite(msg, 1, len, tagfile);
830 8e7bd50a 2019-08-22 stsp if (n != len) {
831 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
834 72840534 2022-01-19 stsp tagsize += n;
836 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, "\n", 1);
837 8e7bd50a 2019-08-22 stsp n = fwrite("\n", 1, 1, tagfile);
838 8e7bd50a 2019-08-22 stsp if (n != 1) {
839 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
842 72840534 2022-01-19 stsp tagsize += n;
844 91d845ad 2022-07-03 jrick if (signer_id && buf_len(buf) > 0) {
845 4d5ee956 2022-07-02 jrick len = buf_len(buf);
846 3093e2d7 2023-02-04 op SHA1Update(&hash_ctx, buf_get(buf), len);
847 4d5ee956 2022-07-02 jrick n = fwrite(buf_get(buf), 1, len, tagfile);
848 4d5ee956 2022-07-02 jrick if (n != len) {
849 4d5ee956 2022-07-02 jrick err = got_ferror(tagfile, GOT_ERR_IO);
850 4d5ee956 2022-07-02 jrick goto done;
852 4d5ee956 2022-07-02 jrick tagsize += n;
855 8e7bd50a 2019-08-22 stsp *id = malloc(sizeof(**id));
856 8e7bd50a 2019-08-22 stsp if (*id == NULL) {
857 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("malloc");
860 3093e2d7 2023-02-04 op SHA1Final((*id)->hash, &hash_ctx);
861 dfe0a3d8 2023-02-04 op (*id)->algo = GOT_HASH_SHA1;
863 8e7bd50a 2019-08-22 stsp if (fflush(tagfile) != 0) {
864 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("fflush");
867 8e7bd50a 2019-08-22 stsp rewind(tagfile);
869 72840534 2022-01-19 stsp err = create_object_file(*id, tagfile, tagsize, repo);
871 8e7bd50a 2019-08-22 stsp free(msg0);
872 8e7bd50a 2019-08-22 stsp free(header);
873 8e7bd50a 2019-08-22 stsp free(obj_str);
874 8e7bd50a 2019-08-22 stsp free(tagger_str);
876 4d5ee956 2022-07-02 jrick buf_release(buf);
877 56b63ca4 2021-01-22 stsp if (tagfile && fclose(tagfile) == EOF && err == NULL)
878 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
881 f91abf81 2019-04-14 stsp *id = NULL;
883 f91abf81 2019-04-14 stsp return err;