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 a14a8cf6 2019-04-11 stsp #include <unistd.h>
32 44edeea7 2019-04-11 stsp #include <zlib.h>
34 44edeea7 2019-04-11 stsp #include "got_error.h"
35 44edeea7 2019-04-11 stsp #include "got_object.h"
36 44edeea7 2019-04-11 stsp #include "got_repository.h"
37 44edeea7 2019-04-11 stsp #include "got_opentemp.h"
38 324d37e7 2019-05-11 stsp #include "got_path.h"
39 4d5ee956 2022-07-02 jrick #include "got_sigs.h"
41 44edeea7 2019-04-11 stsp #include "got_lib_sha1.h"
42 44edeea7 2019-04-11 stsp #include "got_lib_deflate.h"
43 44edeea7 2019-04-11 stsp #include "got_lib_delta.h"
44 44edeea7 2019-04-11 stsp #include "got_lib_object.h"
45 6af1ccbd 2019-08-16 stsp #include "got_lib_object_parse.h"
46 44edeea7 2019-04-11 stsp #include "got_lib_lockfile.h"
48 336075a4 2022-06-25 op #include "got_lib_object_create.h"
50 4d5ee956 2022-07-02 jrick #include "buf.h"
52 44edeea7 2019-04-11 stsp #ifndef nitems
53 44edeea7 2019-04-11 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 ac1c5662 2019-04-13 stsp static const struct got_error *
57 76f564d5 2019-04-14 stsp create_object_file(struct got_object_id *id, FILE *content,
58 72840534 2022-01-19 stsp off_t content_len, struct got_repository *repo)
60 ac1c5662 2019-04-13 stsp const struct got_error *err = NULL, *unlock_err = NULL;
61 c6f826b4 2019-04-13 stsp char *objpath = NULL, *tmppath = NULL;
62 c6f826b4 2019-04-13 stsp FILE *tmpfile = NULL;
63 ac1c5662 2019-04-13 stsp struct got_lockfile *lf = NULL;
64 72840534 2022-01-19 stsp off_t tmplen = 0;
66 ac1c5662 2019-04-13 stsp err = got_object_get_path(&objpath, id, repo);
70 c6f826b4 2019-04-13 stsp err = got_opentemp_named(&tmppath, &tmpfile, objpath);
72 ac1c5662 2019-04-13 stsp char *parent_path;
73 ac1c5662 2019-04-13 stsp if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
75 ac1c5662 2019-04-13 stsp err = got_path_dirname(&parent_path, objpath);
78 ac1c5662 2019-04-13 stsp err = got_path_mkdir(parent_path);
79 ac1c5662 2019-04-13 stsp free(parent_path);
82 c6f826b4 2019-04-13 stsp err = got_opentemp_named(&tmppath, &tmpfile, objpath);
87 3818e3c4 2020-11-01 naddy if (fchmod(fileno(tmpfile), GOT_DEFAULT_FILE_MODE) != 0) {
88 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", tmppath);
92 72840534 2022-01-19 stsp err = got_deflate_to_file(&tmplen, content, content_len, tmpfile, NULL);
96 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, objpath, -1);
100 c6f826b4 2019-04-13 stsp if (rename(tmppath, objpath) != 0) {
101 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, objpath);
104 c6f826b4 2019-04-13 stsp free(tmppath);
105 c6f826b4 2019-04-13 stsp tmppath = NULL;
107 ac1c5662 2019-04-13 stsp free(objpath);
108 c6f826b4 2019-04-13 stsp if (tmppath) {
109 c6f826b4 2019-04-13 stsp if (unlink(tmppath) != 0 && err == NULL)
110 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", tmppath);
111 c6f826b4 2019-04-13 stsp free(tmppath);
113 56b63ca4 2021-01-22 stsp if (tmpfile && fclose(tmpfile) == EOF && err == NULL)
114 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
116 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
117 ac1c5662 2019-04-13 stsp return err ? err : unlock_err;
120 44edeea7 2019-04-11 stsp const struct got_error *
121 0a22ca1a 2020-09-23 stsp got_object_blob_file_create(struct got_object_id **id, FILE **blobfile,
122 72840534 2022-01-19 stsp off_t *blobsize, const char *ondisk_path)
124 ac1c5662 2019-04-13 stsp const struct got_error *err = NULL;
125 ac1c5662 2019-04-13 stsp char *header = NULL;
126 44edeea7 2019-04-11 stsp int fd = -1;
127 44edeea7 2019-04-11 stsp struct stat sb;
128 44edeea7 2019-04-11 stsp SHA1_CTX sha1_ctx;
129 ac1c5662 2019-04-13 stsp size_t headerlen = 0, n;
131 44edeea7 2019-04-11 stsp *id = NULL;
132 0a22ca1a 2020-09-23 stsp *blobfile = NULL;
133 72840534 2022-01-19 stsp *blobsize = 0;
135 44edeea7 2019-04-11 stsp SHA1Init(&sha1_ctx);
137 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
138 3d9a4ec4 2020-07-23 stsp if (fd == -1) {
139 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
140 3d9a4ec4 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
142 3d9a4ec4 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
143 3d9a4ec4 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
146 3d9a4ec4 2020-07-23 stsp } else if (fstat(fd, &sb) == -1) {
147 638f9024 2019-05-13 stsp err = got_error_from_errno2("fstat", ondisk_path);
151 44edeea7 2019-04-11 stsp if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
152 1367695b 2020-09-26 naddy (long long)sb.st_size) == -1) {
153 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
156 ffb286fd 2019-04-11 stsp headerlen = strlen(header) + 1;
157 ffb286fd 2019-04-11 stsp SHA1Update(&sha1_ctx, header, headerlen);
159 0a22ca1a 2020-09-23 stsp *blobfile = got_opentemp();
160 0a22ca1a 2020-09-23 stsp if (*blobfile == NULL) {
161 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
165 0a22ca1a 2020-09-23 stsp n = fwrite(header, 1, headerlen, *blobfile);
166 ac1c5662 2019-04-13 stsp if (n != headerlen) {
167 0a22ca1a 2020-09-23 stsp err = got_ferror(*blobfile, GOT_ERR_IO);
170 72840534 2022-01-19 stsp *blobsize += headerlen;
172 2f63b34c 2020-07-23 stsp char buf[PATH_MAX * 8];
173 a14a8cf6 2019-04-11 stsp ssize_t inlen;
175 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
176 3d9a4ec4 2020-07-23 stsp inlen = readlink(ondisk_path, buf, sizeof(buf));
177 3d9a4ec4 2020-07-23 stsp if (inlen == -1) {
178 3d9a4ec4 2020-07-23 stsp err = got_error_from_errno("readlink");
182 3d9a4ec4 2020-07-23 stsp inlen = read(fd, buf, sizeof(buf));
183 3d9a4ec4 2020-07-23 stsp if (inlen == -1) {
184 3d9a4ec4 2020-07-23 stsp err = got_error_from_errno("read");
188 a14a8cf6 2019-04-11 stsp if (inlen == 0)
189 a14a8cf6 2019-04-11 stsp break; /* EOF */
190 44edeea7 2019-04-11 stsp SHA1Update(&sha1_ctx, buf, inlen);
191 0a22ca1a 2020-09-23 stsp n = fwrite(buf, 1, inlen, *blobfile);
192 ac1c5662 2019-04-13 stsp if (n != inlen) {
193 0a22ca1a 2020-09-23 stsp err = got_ferror(*blobfile, GOT_ERR_IO);
196 72840534 2022-01-19 stsp *blobsize += n;
197 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(sb.st_mode))
201 44edeea7 2019-04-11 stsp *id = malloc(sizeof(**id));
202 44edeea7 2019-04-11 stsp if (*id == NULL) {
203 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
206 4be2a0b4 2019-04-14 stsp SHA1Final((*id)->sha1, &sha1_ctx);
208 0a22ca1a 2020-09-23 stsp if (fflush(*blobfile) != 0) {
209 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
212 0a22ca1a 2020-09-23 stsp rewind(*blobfile);
214 44edeea7 2019-04-11 stsp free(header);
215 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
216 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
219 0a22ca1a 2020-09-23 stsp *id = NULL;
220 0a22ca1a 2020-09-23 stsp if (*blobfile) {
221 0a22ca1a 2020-09-23 stsp fclose(*blobfile);
222 0a22ca1a 2020-09-23 stsp *blobfile = NULL;
225 0a22ca1a 2020-09-23 stsp return err;
228 0a22ca1a 2020-09-23 stsp const struct got_error *
229 0a22ca1a 2020-09-23 stsp got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
230 0a22ca1a 2020-09-23 stsp struct got_repository *repo)
232 0a22ca1a 2020-09-23 stsp const struct got_error *err = NULL;
233 0a22ca1a 2020-09-23 stsp FILE *blobfile = NULL;
234 72840534 2022-01-19 stsp off_t blobsize;
236 72840534 2022-01-19 stsp err = got_object_blob_file_create(id, &blobfile, &blobsize,
237 72840534 2022-01-19 stsp ondisk_path);
239 0a22ca1a 2020-09-23 stsp return err;
241 72840534 2022-01-19 stsp err = create_object_file(*id, blobfile, blobsize, repo);
242 0a22ca1a 2020-09-23 stsp if (fclose(blobfile) == EOF && err == NULL)
243 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
246 44edeea7 2019-04-11 stsp *id = NULL;
248 ac1c5662 2019-04-13 stsp return err;
251 f91abf81 2019-04-14 stsp static const struct got_error *
252 7aadece8 2020-05-17 stsp te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
255 f7b97ccb 2020-04-14 stsp mode_t mode;
258 f7b97ccb 2020-04-14 stsp * Some Git implementations are picky about modes seen in tree entries.
259 f7b97ccb 2020-04-14 stsp * For best compatibility we normalize the file/directory mode here.
261 7aadece8 2020-05-17 stsp if (S_ISREG(te->mode)) {
262 f7b97ccb 2020-04-14 stsp mode = GOT_DEFAULT_FILE_MODE;
263 7aadece8 2020-05-17 stsp if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
264 aaffadfc 2020-05-05 stsp mode |= S_IXUSR | S_IXGRP | S_IXOTH;
265 7aadece8 2020-05-17 stsp } else if (got_object_tree_entry_is_submodule(te))
266 7aadece8 2020-05-17 stsp mode = S_IFDIR | S_IFLNK;
267 3d9a4ec4 2020-07-23 stsp else if (S_ISLNK(te->mode))
268 3d9a4ec4 2020-07-23 stsp mode = S_IFLNK; /* Git leaves all the other bits unset. */
269 7aadece8 2020-05-17 stsp else if (S_ISDIR(te->mode))
270 aaffadfc 2020-05-05 stsp mode = S_IFDIR; /* Git leaves all the other bits unset. */
272 f7b97ccb 2020-04-14 stsp return got_error(GOT_ERR_BAD_FILETYPE);
274 f91abf81 2019-04-14 stsp ret = snprintf(buf, len, "%o ", mode);
275 f91abf81 2019-04-14 stsp if (ret == -1 || ret >= len)
276 f91abf81 2019-04-14 stsp return got_error(GOT_ERR_NO_SPACE);
277 f91abf81 2019-04-14 stsp return NULL;
281 6af1ccbd 2019-08-16 stsp * Git expects directory tree entries to be sorted with an imaginary slash
282 6af1ccbd 2019-08-16 stsp * appended to their name, and will break otherwise. Let's be nice.
283 56e0773d 2019-11-28 stsp * This function is intended to be used with mergesort(3) to sort an
284 56e0773d 2019-11-28 stsp * array of pointers to struct got_tree_entry objects.
287 56e0773d 2019-11-28 stsp sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
289 56e0773d 2019-11-28 stsp struct got_tree_entry * const *te1 = arg1;
290 56e0773d 2019-11-28 stsp struct got_tree_entry * const *te2 = arg2;
291 2c98ee28 2019-11-29 stsp char name1[NAME_MAX + 2];
292 2c98ee28 2019-11-29 stsp char name2[NAME_MAX + 2];
294 56e0773d 2019-11-28 stsp strlcpy(name1, (*te1)->name, sizeof(name1));
295 56e0773d 2019-11-28 stsp strlcpy(name2, (*te2)->name, sizeof(name2));
296 56e0773d 2019-11-28 stsp if (S_ISDIR((*te1)->mode))
297 56e0773d 2019-11-28 stsp strlcat(name1, "/", sizeof(name1));
298 56e0773d 2019-11-28 stsp if (S_ISDIR((*te2)->mode))
299 56e0773d 2019-11-28 stsp strlcat(name2, "/", sizeof(name2));
300 56e0773d 2019-11-28 stsp return strcmp(name1, name2);
303 f91abf81 2019-04-14 stsp const struct got_error *
304 f91abf81 2019-04-14 stsp got_object_tree_create(struct got_object_id **id,
305 56e0773d 2019-11-28 stsp struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
307 f91abf81 2019-04-14 stsp const struct got_error *err = NULL;
308 f91abf81 2019-04-14 stsp char modebuf[sizeof("100644 ")];
309 f91abf81 2019-04-14 stsp SHA1_CTX sha1_ctx;
310 f91abf81 2019-04-14 stsp char *header = NULL;
311 f91abf81 2019-04-14 stsp size_t headerlen, len = 0, n;
312 f91abf81 2019-04-14 stsp FILE *treefile = NULL;
313 72840534 2022-01-19 stsp off_t treesize = 0;
314 56e0773d 2019-11-28 stsp struct got_pathlist_entry *pe;
315 56e0773d 2019-11-28 stsp struct got_tree_entry **sorted_entries;
316 f91abf81 2019-04-14 stsp struct got_tree_entry *te;
319 f91abf81 2019-04-14 stsp *id = NULL;
321 51c32763 2019-05-09 stsp SHA1Init(&sha1_ctx);
323 56e0773d 2019-11-28 stsp sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
324 56e0773d 2019-11-28 stsp if (sorted_entries == NULL)
325 56e0773d 2019-11-28 stsp return got_error_from_errno("calloc");
328 56e0773d 2019-11-28 stsp TAILQ_FOREACH(pe, paths, entry)
329 56e0773d 2019-11-28 stsp sorted_entries[i++] = pe->data;
330 56e0773d 2019-11-28 stsp mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
331 56e0773d 2019-11-28 stsp sort_tree_entries_the_way_git_likes_it);
333 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
334 56e0773d 2019-11-28 stsp te = sorted_entries[i];
335 7aadece8 2020-05-17 stsp err = te_mode2str(modebuf, sizeof(modebuf), te);
338 f91abf81 2019-04-14 stsp len += strlen(modebuf) + strlen(te->name) + 1 +
339 f91abf81 2019-04-14 stsp SHA1_DIGEST_LENGTH;
342 f91abf81 2019-04-14 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
343 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
346 f91abf81 2019-04-14 stsp headerlen = strlen(header) + 1;
347 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, header, headerlen);
349 f91abf81 2019-04-14 stsp treefile = got_opentemp();
350 f91abf81 2019-04-14 stsp if (treefile == NULL) {
351 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
355 f91abf81 2019-04-14 stsp n = fwrite(header, 1, headerlen, treefile);
356 f91abf81 2019-04-14 stsp if (n != headerlen) {
357 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
360 72840534 2022-01-19 stsp treesize += headerlen;
362 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
363 56e0773d 2019-11-28 stsp te = sorted_entries[i];
364 7aadece8 2020-05-17 stsp err = te_mode2str(modebuf, sizeof(modebuf), te);
367 f91abf81 2019-04-14 stsp len = strlen(modebuf);
368 f91abf81 2019-04-14 stsp n = fwrite(modebuf, 1, len, treefile);
369 f91abf81 2019-04-14 stsp if (n != len) {
370 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
373 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, modebuf, len);
374 72840534 2022-01-19 stsp treesize += n;
376 f91abf81 2019-04-14 stsp len = strlen(te->name) + 1; /* must include NUL */
377 f91abf81 2019-04-14 stsp n = fwrite(te->name, 1, len, treefile);
378 f91abf81 2019-04-14 stsp if (n != len) {
379 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
382 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, te->name, len);
383 72840534 2022-01-19 stsp treesize += n;
385 f91abf81 2019-04-14 stsp len = SHA1_DIGEST_LENGTH;
386 56e0773d 2019-11-28 stsp n = fwrite(te->id.sha1, 1, len, treefile);
387 f91abf81 2019-04-14 stsp if (n != len) {
388 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
391 56e0773d 2019-11-28 stsp SHA1Update(&sha1_ctx, te->id.sha1, len);
392 72840534 2022-01-19 stsp treesize += n;
395 f91abf81 2019-04-14 stsp *id = malloc(sizeof(**id));
396 f91abf81 2019-04-14 stsp if (*id == NULL) {
397 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
400 4be2a0b4 2019-04-14 stsp SHA1Final((*id)->sha1, &sha1_ctx);
402 f91abf81 2019-04-14 stsp if (fflush(treefile) != 0) {
403 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
406 f91abf81 2019-04-14 stsp rewind(treefile);
408 72840534 2022-01-19 stsp err = create_object_file(*id, treefile, treesize, repo);
410 f91abf81 2019-04-14 stsp free(header);
411 56e0773d 2019-11-28 stsp free(sorted_entries);
412 56b63ca4 2021-01-22 stsp if (treefile && fclose(treefile) == EOF && err == NULL)
413 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
416 de18fc63 2019-05-09 stsp *id = NULL;
418 de18fc63 2019-05-09 stsp return err;
421 de18fc63 2019-05-09 stsp const struct got_error *
422 de18fc63 2019-05-09 stsp got_object_commit_create(struct got_object_id **id,
423 de18fc63 2019-05-09 stsp struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
424 de18fc63 2019-05-09 stsp int nparents, const char *author, time_t author_time,
425 de18fc63 2019-05-09 stsp const char *committer, time_t committer_time,
426 de18fc63 2019-05-09 stsp const char *logmsg, struct got_repository *repo)
428 de18fc63 2019-05-09 stsp const struct got_error *err = NULL;
429 de18fc63 2019-05-09 stsp SHA1_CTX sha1_ctx;
430 de18fc63 2019-05-09 stsp char *header = NULL, *tree_str = NULL;
431 de18fc63 2019-05-09 stsp char *author_str = NULL, *committer_str = NULL;
432 de18fc63 2019-05-09 stsp char *id_str = NULL;
433 de18fc63 2019-05-09 stsp size_t headerlen, len = 0, n;
434 de18fc63 2019-05-09 stsp FILE *commitfile = NULL;
435 72840534 2022-01-19 stsp off_t commitsize = 0;
436 de18fc63 2019-05-09 stsp struct got_object_qid *qid;
437 787c8eb6 2019-07-11 stsp char *msg0, *msg;
439 de18fc63 2019-05-09 stsp *id = NULL;
441 de18fc63 2019-05-09 stsp SHA1Init(&sha1_ctx);
443 787c8eb6 2019-07-11 stsp msg0 = strdup(logmsg);
444 787c8eb6 2019-07-11 stsp if (msg0 == NULL)
445 787c8eb6 2019-07-11 stsp return got_error_from_errno("strdup");
446 787c8eb6 2019-07-11 stsp msg = msg0;
448 10796104 2019-07-11 stsp while (isspace((unsigned char)msg[0]))
450 787c8eb6 2019-07-11 stsp len = strlen(msg);
451 10796104 2019-07-11 stsp while (len > 0 && isspace((unsigned char)msg[len - 1])) {
452 787c8eb6 2019-07-11 stsp msg[len - 1] = '\0';
456 de18fc63 2019-05-09 stsp if (asprintf(&author_str, "%s%s %lld +0000\n",
457 1367695b 2020-09-26 naddy GOT_COMMIT_LABEL_AUTHOR, author, (long long)author_time) == -1)
458 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
460 de18fc63 2019-05-09 stsp if (asprintf(&committer_str, "%s%s %lld +0000\n",
461 de18fc63 2019-05-09 stsp GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
462 1367695b 2020-09-26 naddy (long long)(committer ? committer_time : author_time))
464 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
468 de18fc63 2019-05-09 stsp len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
470 de18fc63 2019-05-09 stsp (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
471 787c8eb6 2019-07-11 stsp + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
473 de18fc63 2019-05-09 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
474 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
477 de18fc63 2019-05-09 stsp headerlen = strlen(header) + 1;
478 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, header, headerlen);
480 de18fc63 2019-05-09 stsp commitfile = got_opentemp();
481 de18fc63 2019-05-09 stsp if (commitfile == NULL) {
482 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
486 de18fc63 2019-05-09 stsp n = fwrite(header, 1, headerlen, commitfile);
487 de18fc63 2019-05-09 stsp if (n != headerlen) {
488 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
491 72840534 2022-01-19 stsp commitsize += headerlen;
493 de18fc63 2019-05-09 stsp err = got_object_id_str(&id_str, tree_id);
496 de18fc63 2019-05-09 stsp if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
498 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
501 de18fc63 2019-05-09 stsp len = strlen(tree_str);
502 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, tree_str, len);
503 de18fc63 2019-05-09 stsp n = fwrite(tree_str, 1, len, commitfile);
504 de18fc63 2019-05-09 stsp if (n != len) {
505 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
508 72840534 2022-01-19 stsp commitsize += n;
510 3ce1b845 2019-07-15 stsp if (parent_ids) {
511 f259c4c1 2021-09-24 stsp free(id_str);
512 f259c4c1 2021-09-24 stsp id_str = NULL;
513 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, parent_ids, entry) {
514 3ce1b845 2019-07-15 stsp char *parent_str = NULL;
516 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
519 3ce1b845 2019-07-15 stsp if (asprintf(&parent_str, "%s%s\n",
520 3ce1b845 2019-07-15 stsp GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
521 3ce1b845 2019-07-15 stsp err = got_error_from_errno("asprintf");
524 3ce1b845 2019-07-15 stsp len = strlen(parent_str);
525 3ce1b845 2019-07-15 stsp SHA1Update(&sha1_ctx, parent_str, len);
526 3ce1b845 2019-07-15 stsp n = fwrite(parent_str, 1, len, commitfile);
527 3ce1b845 2019-07-15 stsp if (n != len) {
528 3ce1b845 2019-07-15 stsp err = got_ferror(commitfile, GOT_ERR_IO);
529 3ce1b845 2019-07-15 stsp free(parent_str);
532 72840534 2022-01-19 stsp commitsize += n;
533 de18fc63 2019-05-09 stsp free(parent_str);
534 f259c4c1 2021-09-24 stsp free(id_str);
535 f259c4c1 2021-09-24 stsp id_str = NULL;
539 de18fc63 2019-05-09 stsp len = strlen(author_str);
540 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, author_str, len);
541 de18fc63 2019-05-09 stsp n = fwrite(author_str, 1, len, commitfile);
542 de18fc63 2019-05-09 stsp if (n != len) {
543 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
546 72840534 2022-01-19 stsp commitsize += n;
548 de18fc63 2019-05-09 stsp len = strlen(committer_str);
549 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, committer_str, len);
550 de18fc63 2019-05-09 stsp n = fwrite(committer_str, 1, len, commitfile);
551 de18fc63 2019-05-09 stsp if (n != len) {
552 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
555 72840534 2022-01-19 stsp commitsize += n;
557 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, "\n", 1);
558 de18fc63 2019-05-09 stsp n = fwrite("\n", 1, 1, commitfile);
559 de18fc63 2019-05-09 stsp if (n != 1) {
560 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
563 72840534 2022-01-19 stsp commitsize += n;
565 787c8eb6 2019-07-11 stsp len = strlen(msg);
566 787c8eb6 2019-07-11 stsp SHA1Update(&sha1_ctx, msg, len);
567 787c8eb6 2019-07-11 stsp n = fwrite(msg, 1, len, commitfile);
568 de18fc63 2019-05-09 stsp if (n != len) {
569 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
572 72840534 2022-01-19 stsp commitsize += n;
574 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, "\n", 1);
575 de18fc63 2019-05-09 stsp n = fwrite("\n", 1, 1, commitfile);
576 de18fc63 2019-05-09 stsp if (n != 1) {
577 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
580 72840534 2022-01-19 stsp commitsize += n;
582 de18fc63 2019-05-09 stsp *id = malloc(sizeof(**id));
583 de18fc63 2019-05-09 stsp if (*id == NULL) {
584 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
587 de18fc63 2019-05-09 stsp SHA1Final((*id)->sha1, &sha1_ctx);
589 de18fc63 2019-05-09 stsp if (fflush(commitfile) != 0) {
590 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
593 de18fc63 2019-05-09 stsp rewind(commitfile);
595 72840534 2022-01-19 stsp err = create_object_file(*id, commitfile, commitsize, repo);
597 f259c4c1 2021-09-24 stsp free(id_str);
598 787c8eb6 2019-07-11 stsp free(msg0);
599 de18fc63 2019-05-09 stsp free(header);
600 de18fc63 2019-05-09 stsp free(tree_str);
601 de18fc63 2019-05-09 stsp free(author_str);
602 de18fc63 2019-05-09 stsp free(committer_str);
603 56b63ca4 2021-01-22 stsp if (commitfile && fclose(commitfile) == EOF && err == NULL)
604 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("fclose");
607 8e7bd50a 2019-08-22 stsp *id = NULL;
609 8e7bd50a 2019-08-22 stsp return err;
612 8e7bd50a 2019-08-22 stsp const struct got_error *
613 8e7bd50a 2019-08-22 stsp got_object_tag_create(struct got_object_id **id,
614 8e7bd50a 2019-08-22 stsp const char *tag_name, struct got_object_id *object_id, const char *tagger,
615 91d845ad 2022-07-03 jrick time_t tagger_time, const char *tagmsg, const char *signer_id,
616 4d5ee956 2022-07-02 jrick struct got_repository *repo, int verbosity)
618 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
619 8e7bd50a 2019-08-22 stsp SHA1_CTX sha1_ctx;
620 8e7bd50a 2019-08-22 stsp char *header = NULL;
621 8e7bd50a 2019-08-22 stsp char *tag_str = NULL, *tagger_str = NULL;
622 8e7bd50a 2019-08-22 stsp char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
623 4d5ee956 2022-07-02 jrick size_t headerlen, len = 0, sig_len = 0, n;
624 8e7bd50a 2019-08-22 stsp FILE *tagfile = NULL;
625 72840534 2022-01-19 stsp off_t tagsize = 0;
626 8e7bd50a 2019-08-22 stsp char *msg0 = NULL, *msg;
627 8e7bd50a 2019-08-22 stsp const char *obj_type_str;
628 8e7bd50a 2019-08-22 stsp int obj_type;
629 4d5ee956 2022-07-02 jrick BUF *buf = NULL;
631 8e7bd50a 2019-08-22 stsp *id = NULL;
633 8e7bd50a 2019-08-22 stsp SHA1Init(&sha1_ctx);
635 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&id_str, object_id);
638 8e7bd50a 2019-08-22 stsp if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
639 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
643 8e7bd50a 2019-08-22 stsp err = got_object_get_type(&obj_type, repo, object_id);
647 8e7bd50a 2019-08-22 stsp switch (obj_type) {
648 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
649 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_BLOB;
651 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
652 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_TREE;
654 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
655 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_COMMIT;
657 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
658 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_TAG;
661 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_OBJ_TYPE);
665 8e7bd50a 2019-08-22 stsp if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
666 8e7bd50a 2019-08-22 stsp obj_type_str) == -1) {
667 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
671 8e7bd50a 2019-08-22 stsp if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
672 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
676 8e7bd50a 2019-08-22 stsp if (asprintf(&tagger_str, "%s%s %lld +0000\n",
677 1367695b 2020-09-26 naddy GOT_TAG_LABEL_TAGGER, tagger, (long long)tagger_time) == -1)
678 8e7bd50a 2019-08-22 stsp return got_error_from_errno("asprintf");
680 8e7bd50a 2019-08-22 stsp msg0 = strdup(tagmsg);
681 8e7bd50a 2019-08-22 stsp if (msg0 == NULL) {
682 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
685 8e7bd50a 2019-08-22 stsp msg = msg0;
687 8e7bd50a 2019-08-22 stsp while (isspace((unsigned char)msg[0]))
690 91d845ad 2022-07-03 jrick if (signer_id) {
691 4d5ee956 2022-07-02 jrick FILE *out;
692 4d5ee956 2022-07-02 jrick pid_t pid;
693 4d5ee956 2022-07-02 jrick size_t len;
694 4d5ee956 2022-07-02 jrick int in_fd, out_fd;
695 4d5ee956 2022-07-02 jrick int status;
697 4d5ee956 2022-07-02 jrick err = buf_alloc(&buf, 0);
699 4d5ee956 2022-07-02 jrick goto done;
701 4d5ee956 2022-07-02 jrick /* signed message */
702 4d5ee956 2022-07-02 jrick err = buf_puts(&len, buf, obj_str);
704 4d5ee956 2022-07-02 jrick goto done;
705 4d5ee956 2022-07-02 jrick err = buf_puts(&len, buf, type_str);
707 4d5ee956 2022-07-02 jrick goto done;
708 4d5ee956 2022-07-02 jrick err = buf_puts(&len, buf, tag_str);
710 4d5ee956 2022-07-02 jrick goto done;
711 4d5ee956 2022-07-02 jrick err = buf_puts(&len, buf, tagger_str);
713 4d5ee956 2022-07-02 jrick goto done;
714 4d5ee956 2022-07-02 jrick err = buf_putc(buf, '\n');
716 4d5ee956 2022-07-02 jrick goto done;
717 4d5ee956 2022-07-02 jrick err = buf_puts(&len, buf, msg);
719 4d5ee956 2022-07-02 jrick goto done;
720 4d5ee956 2022-07-02 jrick err = buf_putc(buf, '\n');
722 4d5ee956 2022-07-02 jrick goto done;
724 91d845ad 2022-07-03 jrick err = got_sigs_sign_tag_ssh(&pid, &in_fd, &out_fd, signer_id,
725 4d5ee956 2022-07-02 jrick verbosity);
727 4d5ee956 2022-07-02 jrick goto done;
728 4d5ee956 2022-07-02 jrick if (buf_write_fd(buf, in_fd) == -1) {
729 4d5ee956 2022-07-02 jrick err = got_error_from_errno("write");
730 4d5ee956 2022-07-02 jrick goto done;
732 4d5ee956 2022-07-02 jrick if (close(in_fd) == -1) {
733 4d5ee956 2022-07-02 jrick err = got_error_from_errno("close");
734 4d5ee956 2022-07-02 jrick goto done;
737 4d5ee956 2022-07-02 jrick if (waitpid(pid, &status, 0) == -1) {
738 4d5ee956 2022-07-02 jrick err = got_error_from_errno("waitpid");
739 4d5ee956 2022-07-02 jrick goto done;
741 91d845ad 2022-07-03 jrick if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
742 91d845ad 2022-07-03 jrick err = got_error(GOT_ERR_SIGNING_TAG);
743 91d845ad 2022-07-03 jrick goto done;
746 4d5ee956 2022-07-02 jrick out = fdopen(out_fd, "r");
747 4d5ee956 2022-07-02 jrick if (out == NULL) {
748 4d5ee956 2022-07-02 jrick err = got_error_from_errno("fdopen");
749 4d5ee956 2022-07-02 jrick goto done;
751 4d5ee956 2022-07-02 jrick buf_empty(buf);
752 4d5ee956 2022-07-02 jrick err = buf_load(&buf, out);
754 4d5ee956 2022-07-02 jrick goto done;
755 4d5ee956 2022-07-02 jrick sig_len = buf_len(buf) + 1;
756 4d5ee956 2022-07-02 jrick err = buf_putc(buf, '\0');
758 4d5ee956 2022-07-02 jrick goto done;
759 4d5ee956 2022-07-02 jrick if (close(out_fd) == -1) {
760 4d5ee956 2022-07-02 jrick err = got_error_from_errno("close");
761 4d5ee956 2022-07-02 jrick goto done;
765 4d5ee956 2022-07-02 jrick len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
766 4d5ee956 2022-07-02 jrick strlen(tagger_str) + 1 + strlen(msg) + 1 + sig_len;
767 8e7bd50a 2019-08-22 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
768 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
772 8e7bd50a 2019-08-22 stsp headerlen = strlen(header) + 1;
773 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, header, headerlen);
775 8e7bd50a 2019-08-22 stsp tagfile = got_opentemp();
776 8e7bd50a 2019-08-22 stsp if (tagfile == NULL) {
777 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_opentemp");
781 8e7bd50a 2019-08-22 stsp n = fwrite(header, 1, headerlen, tagfile);
782 8e7bd50a 2019-08-22 stsp if (n != headerlen) {
783 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
786 72840534 2022-01-19 stsp tagsize += headerlen;
787 8e7bd50a 2019-08-22 stsp len = strlen(obj_str);
788 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, obj_str, len);
789 8e7bd50a 2019-08-22 stsp n = fwrite(obj_str, 1, len, tagfile);
790 8e7bd50a 2019-08-22 stsp if (n != len) {
791 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
794 72840534 2022-01-19 stsp tagsize += n;
795 8e7bd50a 2019-08-22 stsp len = strlen(type_str);
796 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, type_str, len);
797 8e7bd50a 2019-08-22 stsp n = fwrite(type_str, 1, len, tagfile);
798 8e7bd50a 2019-08-22 stsp if (n != len) {
799 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
802 72840534 2022-01-19 stsp tagsize += n;
804 8e7bd50a 2019-08-22 stsp len = strlen(tag_str);
805 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, tag_str, len);
806 8e7bd50a 2019-08-22 stsp n = fwrite(tag_str, 1, len, tagfile);
807 8e7bd50a 2019-08-22 stsp if (n != len) {
808 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
811 72840534 2022-01-19 stsp tagsize += n;
813 8e7bd50a 2019-08-22 stsp len = strlen(tagger_str);
814 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, tagger_str, len);
815 8e7bd50a 2019-08-22 stsp n = fwrite(tagger_str, 1, len, tagfile);
816 8e7bd50a 2019-08-22 stsp if (n != len) {
817 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
820 72840534 2022-01-19 stsp tagsize += n;
822 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, "\n", 1);
823 8e7bd50a 2019-08-22 stsp n = fwrite("\n", 1, 1, tagfile);
824 8e7bd50a 2019-08-22 stsp if (n != 1) {
825 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
828 72840534 2022-01-19 stsp tagsize += n;
830 8e7bd50a 2019-08-22 stsp len = strlen(msg);
831 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, msg, len);
832 8e7bd50a 2019-08-22 stsp n = fwrite(msg, 1, len, tagfile);
833 8e7bd50a 2019-08-22 stsp if (n != len) {
834 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
837 72840534 2022-01-19 stsp tagsize += n;
839 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, "\n", 1);
840 8e7bd50a 2019-08-22 stsp n = fwrite("\n", 1, 1, tagfile);
841 8e7bd50a 2019-08-22 stsp if (n != 1) {
842 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
845 72840534 2022-01-19 stsp tagsize += n;
847 91d845ad 2022-07-03 jrick if (signer_id && buf_len(buf) > 0) {
848 4d5ee956 2022-07-02 jrick len = buf_len(buf);
849 4d5ee956 2022-07-02 jrick SHA1Update(&sha1_ctx, buf_get(buf), len);
850 4d5ee956 2022-07-02 jrick n = fwrite(buf_get(buf), 1, len, tagfile);
851 4d5ee956 2022-07-02 jrick if (n != len) {
852 4d5ee956 2022-07-02 jrick err = got_ferror(tagfile, GOT_ERR_IO);
853 4d5ee956 2022-07-02 jrick goto done;
855 4d5ee956 2022-07-02 jrick tagsize += n;
858 8e7bd50a 2019-08-22 stsp *id = malloc(sizeof(**id));
859 8e7bd50a 2019-08-22 stsp if (*id == NULL) {
860 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("malloc");
863 8e7bd50a 2019-08-22 stsp SHA1Final((*id)->sha1, &sha1_ctx);
865 8e7bd50a 2019-08-22 stsp if (fflush(tagfile) != 0) {
866 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("fflush");
869 8e7bd50a 2019-08-22 stsp rewind(tagfile);
871 72840534 2022-01-19 stsp err = create_object_file(*id, tagfile, tagsize, repo);
873 8e7bd50a 2019-08-22 stsp free(msg0);
874 8e7bd50a 2019-08-22 stsp free(header);
875 8e7bd50a 2019-08-22 stsp free(obj_str);
876 8e7bd50a 2019-08-22 stsp free(tagger_str);
878 4d5ee956 2022-07-02 jrick buf_release(buf);
879 56b63ca4 2021-01-22 stsp if (tagfile && fclose(tagfile) == EOF && err == NULL)
880 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
883 f91abf81 2019-04-14 stsp *id = NULL;
885 f91abf81 2019-04-14 stsp return err;