Blame


1 44edeea7 2019-04-11 stsp /*
2 44edeea7 2019-04-11 stsp * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
3 44edeea7 2019-04-11 stsp *
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.
7 44edeea7 2019-04-11 stsp *
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.
15 44edeea7 2019-04-11 stsp */
16 44edeea7 2019-04-11 stsp
17 44edeea7 2019-04-11 stsp #include <sys/types.h>
18 44edeea7 2019-04-11 stsp #include <sys/stat.h>
19 871bd038 2022-07-03 thomas #include <sys/queue.h>
20 871bd038 2022-07-03 thomas #include <sys/wait.h>
21 44edeea7 2019-04-11 stsp
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 a14a8cf6 2019-04-11 stsp #include <unistd.h>
31 44edeea7 2019-04-11 stsp #include <zlib.h>
32 44edeea7 2019-04-11 stsp
33 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
34 dd038bc6 2021-09-21 thomas.ad
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 871bd038 2022-07-03 thomas #include "got_sigs.h"
41 44edeea7 2019-04-11 stsp
42 44edeea7 2019-04-11 stsp #include "got_lib_sha1.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"
48 ef20f542 2022-06-26 thomas
49 ef20f542 2022-06-26 thomas #include "got_lib_object_create.h"
50 44edeea7 2019-04-11 stsp
51 871bd038 2022-07-03 thomas #include "buf.h"
52 871bd038 2022-07-03 thomas
53 44edeea7 2019-04-11 stsp #ifndef nitems
54 44edeea7 2019-04-11 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
55 44edeea7 2019-04-11 stsp #endif
56 44edeea7 2019-04-11 stsp
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 e8f02263 2022-01-23 thomas off_t content_len, struct got_repository *repo)
60 ac1c5662 2019-04-13 stsp {
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 e8f02263 2022-01-23 thomas off_t tmplen = 0;
66 ac1c5662 2019-04-13 stsp
67 ac1c5662 2019-04-13 stsp err = got_object_get_path(&objpath, id, repo);
68 ac1c5662 2019-04-13 stsp if (err)
69 ac1c5662 2019-04-13 stsp return err;
70 ac1c5662 2019-04-13 stsp
71 c6f826b4 2019-04-13 stsp err = got_opentemp_named(&tmppath, &tmpfile, objpath);
72 ac1c5662 2019-04-13 stsp if (err) {
73 ac1c5662 2019-04-13 stsp char *parent_path;
74 ac1c5662 2019-04-13 stsp if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
75 ac1c5662 2019-04-13 stsp goto done;
76 ac1c5662 2019-04-13 stsp err = got_path_dirname(&parent_path, objpath);
77 ac1c5662 2019-04-13 stsp if (err)
78 ac1c5662 2019-04-13 stsp goto done;
79 ac1c5662 2019-04-13 stsp err = got_path_mkdir(parent_path);
80 ac1c5662 2019-04-13 stsp free(parent_path);
81 ac1c5662 2019-04-13 stsp if (err)
82 ac1c5662 2019-04-13 stsp goto done;
83 c6f826b4 2019-04-13 stsp err = got_opentemp_named(&tmppath, &tmpfile, objpath);
84 ac1c5662 2019-04-13 stsp if (err)
85 ac1c5662 2019-04-13 stsp goto done;
86 3818e3c4 2020-11-01 naddy }
87 3818e3c4 2020-11-01 naddy
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);
90 3818e3c4 2020-11-01 naddy goto done;
91 ac1c5662 2019-04-13 stsp }
92 ac1c5662 2019-04-13 stsp
93 e8f02263 2022-01-23 thomas err = got_deflate_to_file(&tmplen, content, content_len, tmpfile, NULL);
94 ac1c5662 2019-04-13 stsp if (err)
95 ac1c5662 2019-04-13 stsp goto done;
96 ac1c5662 2019-04-13 stsp
97 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, objpath, -1);
98 ac1c5662 2019-04-13 stsp if (err)
99 ac1c5662 2019-04-13 stsp goto done;
100 ac1c5662 2019-04-13 stsp
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);
103 ac1c5662 2019-04-13 stsp goto done;
104 ac1c5662 2019-04-13 stsp }
105 c6f826b4 2019-04-13 stsp free(tmppath);
106 c6f826b4 2019-04-13 stsp tmppath = NULL;
107 ac1c5662 2019-04-13 stsp done:
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);
113 ac1c5662 2019-04-13 stsp }
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");
116 ac1c5662 2019-04-13 stsp if (lf)
117 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
118 ac1c5662 2019-04-13 stsp return err ? err : unlock_err;
119 ac1c5662 2019-04-13 stsp }
120 ac1c5662 2019-04-13 stsp
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 e8f02263 2022-01-23 thomas off_t *blobsize, const char *ondisk_path)
124 44edeea7 2019-04-11 stsp {
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 44edeea7 2019-04-11 stsp SHA1_CTX sha1_ctx;
130 ac1c5662 2019-04-13 stsp size_t headerlen = 0, n;
131 44edeea7 2019-04-11 stsp
132 44edeea7 2019-04-11 stsp *id = NULL;
133 0a22ca1a 2020-09-23 stsp *blobfile = NULL;
134 e8f02263 2022-01-23 thomas *blobsize = 0;
135 44edeea7 2019-04-11 stsp
136 44edeea7 2019-04-11 stsp SHA1Init(&sha1_ctx);
137 44edeea7 2019-04-11 stsp
138 06340621 2021-12-31 thomas fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
139 3d9a4ec4 2020-07-23 stsp if (fd == -1) {
140 3dc1dc04 2021-09-27 thomas if (!got_err_open_nofollow_on_symlink())
141 3d9a4ec4 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
142 44edeea7 2019-04-11 stsp
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);
145 3d9a4ec4 2020-07-23 stsp goto done;
146 3d9a4ec4 2020-07-23 stsp }
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);
149 44edeea7 2019-04-11 stsp goto done;
150 44edeea7 2019-04-11 stsp }
151 44edeea7 2019-04-11 stsp
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");
155 44edeea7 2019-04-11 stsp goto done;
156 44edeea7 2019-04-11 stsp }
157 ffb286fd 2019-04-11 stsp headerlen = strlen(header) + 1;
158 ffb286fd 2019-04-11 stsp SHA1Update(&sha1_ctx, header, headerlen);
159 44edeea7 2019-04-11 stsp
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");
163 44edeea7 2019-04-11 stsp goto done;
164 81984c6b 2019-04-11 stsp }
165 44edeea7 2019-04-11 stsp
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);
169 f16c2465 2019-04-11 stsp goto done;
170 f16c2465 2019-04-11 stsp }
171 e8f02263 2022-01-23 thomas *blobsize += headerlen;
172 656b1f76 2019-05-11 jcs for (;;) {
173 2f63b34c 2020-07-23 stsp char buf[PATH_MAX * 8];
174 a14a8cf6 2019-04-11 stsp ssize_t inlen;
175 44edeea7 2019-04-11 stsp
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");
180 3d9a4ec4 2020-07-23 stsp goto done;
181 3d9a4ec4 2020-07-23 stsp }
182 3d9a4ec4 2020-07-23 stsp } else {
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");
186 3d9a4ec4 2020-07-23 stsp goto done;
187 3d9a4ec4 2020-07-23 stsp }
188 44edeea7 2019-04-11 stsp }
189 a14a8cf6 2019-04-11 stsp if (inlen == 0)
190 a14a8cf6 2019-04-11 stsp break; /* EOF */
191 44edeea7 2019-04-11 stsp SHA1Update(&sha1_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);
195 44edeea7 2019-04-11 stsp goto done;
196 44edeea7 2019-04-11 stsp }
197 e8f02263 2022-01-23 thomas *blobsize += n;
198 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(sb.st_mode))
199 3d9a4ec4 2020-07-23 stsp break;
200 44edeea7 2019-04-11 stsp }
201 44edeea7 2019-04-11 stsp
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");
205 44edeea7 2019-04-11 stsp goto done;
206 44edeea7 2019-04-11 stsp }
207 4be2a0b4 2019-04-14 stsp SHA1Final((*id)->sha1, &sha1_ctx);
208 44edeea7 2019-04-11 stsp
209 0a22ca1a 2020-09-23 stsp if (fflush(*blobfile) != 0) {
210 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
211 44edeea7 2019-04-11 stsp goto done;
212 44edeea7 2019-04-11 stsp }
213 0a22ca1a 2020-09-23 stsp rewind(*blobfile);
214 44edeea7 2019-04-11 stsp done:
215 44edeea7 2019-04-11 stsp free(header);
216 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
217 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
218 0a22ca1a 2020-09-23 stsp if (err) {
219 0a22ca1a 2020-09-23 stsp free(*id);
220 0a22ca1a 2020-09-23 stsp *id = NULL;
221 0a22ca1a 2020-09-23 stsp if (*blobfile) {
222 0a22ca1a 2020-09-23 stsp fclose(*blobfile);
223 0a22ca1a 2020-09-23 stsp *blobfile = NULL;
224 0a22ca1a 2020-09-23 stsp }
225 0a22ca1a 2020-09-23 stsp }
226 0a22ca1a 2020-09-23 stsp return err;
227 0a22ca1a 2020-09-23 stsp }
228 0a22ca1a 2020-09-23 stsp
229 0a22ca1a 2020-09-23 stsp const struct got_error *
230 0a22ca1a 2020-09-23 stsp got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
231 0a22ca1a 2020-09-23 stsp struct got_repository *repo)
232 0a22ca1a 2020-09-23 stsp {
233 0a22ca1a 2020-09-23 stsp const struct got_error *err = NULL;
234 0a22ca1a 2020-09-23 stsp FILE *blobfile = NULL;
235 e8f02263 2022-01-23 thomas off_t blobsize;
236 0a22ca1a 2020-09-23 stsp
237 e8f02263 2022-01-23 thomas err = got_object_blob_file_create(id, &blobfile, &blobsize,
238 e8f02263 2022-01-23 thomas ondisk_path);
239 0a22ca1a 2020-09-23 stsp if (err)
240 0a22ca1a 2020-09-23 stsp return err;
241 0a22ca1a 2020-09-23 stsp
242 e8f02263 2022-01-23 thomas err = create_object_file(*id, blobfile, blobsize, repo);
243 0a22ca1a 2020-09-23 stsp if (fclose(blobfile) == EOF && err == NULL)
244 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
245 44edeea7 2019-04-11 stsp if (err) {
246 44edeea7 2019-04-11 stsp free(*id);
247 44edeea7 2019-04-11 stsp *id = NULL;
248 44edeea7 2019-04-11 stsp }
249 ac1c5662 2019-04-13 stsp return err;
250 44edeea7 2019-04-11 stsp }
251 f91abf81 2019-04-14 stsp
252 f91abf81 2019-04-14 stsp static const struct got_error *
253 7aadece8 2020-05-17 stsp te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
254 f91abf81 2019-04-14 stsp {
255 f91abf81 2019-04-14 stsp int ret;
256 f7b97ccb 2020-04-14 stsp mode_t mode;
257 f7b97ccb 2020-04-14 stsp
258 f7b97ccb 2020-04-14 stsp /*
259 f7b97ccb 2020-04-14 stsp * Some Git implementations are picky about modes seen in tree entries.
260 f7b97ccb 2020-04-14 stsp * For best compatibility we normalize the file/directory mode here.
261 f7b97ccb 2020-04-14 stsp */
262 7aadece8 2020-05-17 stsp if (S_ISREG(te->mode)) {
263 f7b97ccb 2020-04-14 stsp mode = GOT_DEFAULT_FILE_MODE;
264 7aadece8 2020-05-17 stsp if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
265 aaffadfc 2020-05-05 stsp mode |= S_IXUSR | S_IXGRP | S_IXOTH;
266 7aadece8 2020-05-17 stsp } else if (got_object_tree_entry_is_submodule(te))
267 7aadece8 2020-05-17 stsp mode = S_IFDIR | S_IFLNK;
268 3d9a4ec4 2020-07-23 stsp else if (S_ISLNK(te->mode))
269 3d9a4ec4 2020-07-23 stsp mode = S_IFLNK; /* Git leaves all the other bits unset. */
270 7aadece8 2020-05-17 stsp else if (S_ISDIR(te->mode))
271 aaffadfc 2020-05-05 stsp mode = S_IFDIR; /* Git leaves all the other bits unset. */
272 f7b97ccb 2020-04-14 stsp else
273 f7b97ccb 2020-04-14 stsp return got_error(GOT_ERR_BAD_FILETYPE);
274 f7b97ccb 2020-04-14 stsp
275 f91abf81 2019-04-14 stsp ret = snprintf(buf, len, "%o ", mode);
276 f91abf81 2019-04-14 stsp if (ret == -1 || ret >= len)
277 f91abf81 2019-04-14 stsp return got_error(GOT_ERR_NO_SPACE);
278 f91abf81 2019-04-14 stsp return NULL;
279 6af1ccbd 2019-08-16 stsp }
280 6af1ccbd 2019-08-16 stsp
281 6af1ccbd 2019-08-16 stsp /*
282 6af1ccbd 2019-08-16 stsp * Git expects directory tree entries to be sorted with an imaginary slash
283 6af1ccbd 2019-08-16 stsp * appended to their name, and will break otherwise. Let's be nice.
284 56e0773d 2019-11-28 stsp * This function is intended to be used with mergesort(3) to sort an
285 56e0773d 2019-11-28 stsp * array of pointers to struct got_tree_entry objects.
286 6af1ccbd 2019-08-16 stsp */
287 56e0773d 2019-11-28 stsp static int
288 56e0773d 2019-11-28 stsp sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
289 6af1ccbd 2019-08-16 stsp {
290 56e0773d 2019-11-28 stsp struct got_tree_entry * const *te1 = arg1;
291 56e0773d 2019-11-28 stsp struct got_tree_entry * const *te2 = arg2;
292 2c98ee28 2019-11-29 stsp char name1[NAME_MAX + 2];
293 2c98ee28 2019-11-29 stsp char name2[NAME_MAX + 2];
294 6af1ccbd 2019-08-16 stsp
295 56e0773d 2019-11-28 stsp strlcpy(name1, (*te1)->name, sizeof(name1));
296 56e0773d 2019-11-28 stsp strlcpy(name2, (*te2)->name, sizeof(name2));
297 56e0773d 2019-11-28 stsp if (S_ISDIR((*te1)->mode))
298 56e0773d 2019-11-28 stsp strlcat(name1, "/", sizeof(name1));
299 56e0773d 2019-11-28 stsp if (S_ISDIR((*te2)->mode))
300 56e0773d 2019-11-28 stsp strlcat(name2, "/", sizeof(name2));
301 56e0773d 2019-11-28 stsp return strcmp(name1, name2);
302 f91abf81 2019-04-14 stsp }
303 f91abf81 2019-04-14 stsp
304 f91abf81 2019-04-14 stsp const struct got_error *
305 f91abf81 2019-04-14 stsp got_object_tree_create(struct got_object_id **id,
306 56e0773d 2019-11-28 stsp struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
307 f91abf81 2019-04-14 stsp {
308 f91abf81 2019-04-14 stsp const struct got_error *err = NULL;
309 f91abf81 2019-04-14 stsp char modebuf[sizeof("100644 ")];
310 f91abf81 2019-04-14 stsp SHA1_CTX sha1_ctx;
311 f91abf81 2019-04-14 stsp char *header = NULL;
312 f91abf81 2019-04-14 stsp size_t headerlen, len = 0, n;
313 f91abf81 2019-04-14 stsp FILE *treefile = NULL;
314 e8f02263 2022-01-23 thomas off_t treesize = 0;
315 56e0773d 2019-11-28 stsp struct got_pathlist_entry *pe;
316 56e0773d 2019-11-28 stsp struct got_tree_entry **sorted_entries;
317 f91abf81 2019-04-14 stsp struct got_tree_entry *te;
318 56e0773d 2019-11-28 stsp int i;
319 f91abf81 2019-04-14 stsp
320 f91abf81 2019-04-14 stsp *id = NULL;
321 51c32763 2019-05-09 stsp
322 51c32763 2019-05-09 stsp SHA1Init(&sha1_ctx);
323 f91abf81 2019-04-14 stsp
324 56e0773d 2019-11-28 stsp sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
325 56e0773d 2019-11-28 stsp if (sorted_entries == NULL)
326 56e0773d 2019-11-28 stsp return got_error_from_errno("calloc");
327 6af1ccbd 2019-08-16 stsp
328 56e0773d 2019-11-28 stsp i = 0;
329 56e0773d 2019-11-28 stsp TAILQ_FOREACH(pe, paths, entry)
330 56e0773d 2019-11-28 stsp sorted_entries[i++] = pe->data;
331 56e0773d 2019-11-28 stsp mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
332 56e0773d 2019-11-28 stsp sort_tree_entries_the_way_git_likes_it);
333 56e0773d 2019-11-28 stsp
334 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
335 56e0773d 2019-11-28 stsp te = sorted_entries[i];
336 7aadece8 2020-05-17 stsp err = te_mode2str(modebuf, sizeof(modebuf), te);
337 f91abf81 2019-04-14 stsp if (err)
338 6af1ccbd 2019-08-16 stsp goto done;
339 f91abf81 2019-04-14 stsp len += strlen(modebuf) + strlen(te->name) + 1 +
340 f91abf81 2019-04-14 stsp SHA1_DIGEST_LENGTH;
341 f91abf81 2019-04-14 stsp }
342 f91abf81 2019-04-14 stsp
343 f91abf81 2019-04-14 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
344 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
345 f91abf81 2019-04-14 stsp goto done;
346 f91abf81 2019-04-14 stsp }
347 f91abf81 2019-04-14 stsp headerlen = strlen(header) + 1;
348 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, header, headerlen);
349 f91abf81 2019-04-14 stsp
350 f91abf81 2019-04-14 stsp treefile = got_opentemp();
351 f91abf81 2019-04-14 stsp if (treefile == NULL) {
352 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
353 f91abf81 2019-04-14 stsp goto done;
354 f91abf81 2019-04-14 stsp }
355 f91abf81 2019-04-14 stsp
356 f91abf81 2019-04-14 stsp n = fwrite(header, 1, headerlen, treefile);
357 f91abf81 2019-04-14 stsp if (n != headerlen) {
358 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
359 f91abf81 2019-04-14 stsp goto done;
360 f91abf81 2019-04-14 stsp }
361 e8f02263 2022-01-23 thomas treesize += headerlen;
362 f91abf81 2019-04-14 stsp
363 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
364 56e0773d 2019-11-28 stsp te = sorted_entries[i];
365 7aadece8 2020-05-17 stsp err = te_mode2str(modebuf, sizeof(modebuf), te);
366 f91abf81 2019-04-14 stsp if (err)
367 f91abf81 2019-04-14 stsp goto done;
368 f91abf81 2019-04-14 stsp len = strlen(modebuf);
369 f91abf81 2019-04-14 stsp n = fwrite(modebuf, 1, len, treefile);
370 f91abf81 2019-04-14 stsp if (n != len) {
371 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
372 f91abf81 2019-04-14 stsp goto done;
373 f91abf81 2019-04-14 stsp }
374 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, modebuf, len);
375 e8f02263 2022-01-23 thomas treesize += n;
376 f91abf81 2019-04-14 stsp
377 f91abf81 2019-04-14 stsp len = strlen(te->name) + 1; /* must include NUL */
378 f91abf81 2019-04-14 stsp n = fwrite(te->name, 1, len, treefile);
379 f91abf81 2019-04-14 stsp if (n != len) {
380 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
381 f91abf81 2019-04-14 stsp goto done;
382 f91abf81 2019-04-14 stsp }
383 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, te->name, len);
384 e8f02263 2022-01-23 thomas treesize += n;
385 f91abf81 2019-04-14 stsp
386 f91abf81 2019-04-14 stsp len = SHA1_DIGEST_LENGTH;
387 56e0773d 2019-11-28 stsp n = fwrite(te->id.sha1, 1, len, treefile);
388 f91abf81 2019-04-14 stsp if (n != len) {
389 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
390 f91abf81 2019-04-14 stsp goto done;
391 f91abf81 2019-04-14 stsp }
392 56e0773d 2019-11-28 stsp SHA1Update(&sha1_ctx, te->id.sha1, len);
393 e8f02263 2022-01-23 thomas treesize += n;
394 f91abf81 2019-04-14 stsp }
395 f91abf81 2019-04-14 stsp
396 f91abf81 2019-04-14 stsp *id = malloc(sizeof(**id));
397 f91abf81 2019-04-14 stsp if (*id == NULL) {
398 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
399 f91abf81 2019-04-14 stsp goto done;
400 f91abf81 2019-04-14 stsp }
401 4be2a0b4 2019-04-14 stsp SHA1Final((*id)->sha1, &sha1_ctx);
402 f91abf81 2019-04-14 stsp
403 f91abf81 2019-04-14 stsp if (fflush(treefile) != 0) {
404 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
405 f91abf81 2019-04-14 stsp goto done;
406 f91abf81 2019-04-14 stsp }
407 f91abf81 2019-04-14 stsp rewind(treefile);
408 f91abf81 2019-04-14 stsp
409 e8f02263 2022-01-23 thomas err = create_object_file(*id, treefile, treesize, repo);
410 f91abf81 2019-04-14 stsp done:
411 f91abf81 2019-04-14 stsp free(header);
412 56e0773d 2019-11-28 stsp free(sorted_entries);
413 56b63ca4 2021-01-22 stsp if (treefile && fclose(treefile) == EOF && err == NULL)
414 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
415 de18fc63 2019-05-09 stsp if (err) {
416 de18fc63 2019-05-09 stsp free(*id);
417 de18fc63 2019-05-09 stsp *id = NULL;
418 de18fc63 2019-05-09 stsp }
419 de18fc63 2019-05-09 stsp return err;
420 de18fc63 2019-05-09 stsp }
421 de18fc63 2019-05-09 stsp
422 de18fc63 2019-05-09 stsp const struct got_error *
423 de18fc63 2019-05-09 stsp got_object_commit_create(struct got_object_id **id,
424 de18fc63 2019-05-09 stsp struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
425 de18fc63 2019-05-09 stsp int nparents, const char *author, time_t author_time,
426 de18fc63 2019-05-09 stsp const char *committer, time_t committer_time,
427 de18fc63 2019-05-09 stsp const char *logmsg, struct got_repository *repo)
428 de18fc63 2019-05-09 stsp {
429 de18fc63 2019-05-09 stsp const struct got_error *err = NULL;
430 de18fc63 2019-05-09 stsp SHA1_CTX sha1_ctx;
431 de18fc63 2019-05-09 stsp char *header = NULL, *tree_str = NULL;
432 de18fc63 2019-05-09 stsp char *author_str = NULL, *committer_str = NULL;
433 de18fc63 2019-05-09 stsp char *id_str = NULL;
434 de18fc63 2019-05-09 stsp size_t headerlen, len = 0, n;
435 de18fc63 2019-05-09 stsp FILE *commitfile = NULL;
436 e8f02263 2022-01-23 thomas off_t commitsize = 0;
437 de18fc63 2019-05-09 stsp struct got_object_qid *qid;
438 787c8eb6 2019-07-11 stsp char *msg0, *msg;
439 de18fc63 2019-05-09 stsp
440 de18fc63 2019-05-09 stsp *id = NULL;
441 de18fc63 2019-05-09 stsp
442 de18fc63 2019-05-09 stsp SHA1Init(&sha1_ctx);
443 787c8eb6 2019-07-11 stsp
444 787c8eb6 2019-07-11 stsp msg0 = strdup(logmsg);
445 787c8eb6 2019-07-11 stsp if (msg0 == NULL)
446 787c8eb6 2019-07-11 stsp return got_error_from_errno("strdup");
447 787c8eb6 2019-07-11 stsp msg = msg0;
448 787c8eb6 2019-07-11 stsp
449 10796104 2019-07-11 stsp while (isspace((unsigned char)msg[0]))
450 787c8eb6 2019-07-11 stsp msg++;
451 787c8eb6 2019-07-11 stsp len = strlen(msg);
452 10796104 2019-07-11 stsp while (len > 0 && isspace((unsigned char)msg[len - 1])) {
453 787c8eb6 2019-07-11 stsp msg[len - 1] = '\0';
454 787c8eb6 2019-07-11 stsp len--;
455 787c8eb6 2019-07-11 stsp }
456 de18fc63 2019-05-09 stsp
457 de18fc63 2019-05-09 stsp if (asprintf(&author_str, "%s%s %lld +0000\n",
458 1367695b 2020-09-26 naddy GOT_COMMIT_LABEL_AUTHOR, author, (long long)author_time) == -1)
459 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
460 de18fc63 2019-05-09 stsp
461 de18fc63 2019-05-09 stsp if (asprintf(&committer_str, "%s%s %lld +0000\n",
462 de18fc63 2019-05-09 stsp GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
463 1367695b 2020-09-26 naddy (long long)(committer ? committer_time : author_time))
464 de18fc63 2019-05-09 stsp == -1) {
465 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
466 de18fc63 2019-05-09 stsp goto done;
467 de18fc63 2019-05-09 stsp }
468 de18fc63 2019-05-09 stsp
469 de18fc63 2019-05-09 stsp len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
470 de18fc63 2019-05-09 stsp nparents *
471 de18fc63 2019-05-09 stsp (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
472 787c8eb6 2019-07-11 stsp + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
473 de18fc63 2019-05-09 stsp
474 de18fc63 2019-05-09 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
475 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
476 de18fc63 2019-05-09 stsp goto done;
477 de18fc63 2019-05-09 stsp }
478 de18fc63 2019-05-09 stsp headerlen = strlen(header) + 1;
479 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, header, headerlen);
480 de18fc63 2019-05-09 stsp
481 de18fc63 2019-05-09 stsp commitfile = got_opentemp();
482 de18fc63 2019-05-09 stsp if (commitfile == NULL) {
483 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
484 de18fc63 2019-05-09 stsp goto done;
485 de18fc63 2019-05-09 stsp }
486 de18fc63 2019-05-09 stsp
487 de18fc63 2019-05-09 stsp n = fwrite(header, 1, headerlen, commitfile);
488 de18fc63 2019-05-09 stsp if (n != headerlen) {
489 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
490 de18fc63 2019-05-09 stsp goto done;
491 de18fc63 2019-05-09 stsp }
492 e8f02263 2022-01-23 thomas commitsize += headerlen;
493 de18fc63 2019-05-09 stsp
494 de18fc63 2019-05-09 stsp err = got_object_id_str(&id_str, tree_id);
495 de18fc63 2019-05-09 stsp if (err)
496 de18fc63 2019-05-09 stsp goto done;
497 de18fc63 2019-05-09 stsp if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
498 de18fc63 2019-05-09 stsp == -1) {
499 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
500 de18fc63 2019-05-09 stsp goto done;
501 de18fc63 2019-05-09 stsp }
502 de18fc63 2019-05-09 stsp len = strlen(tree_str);
503 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, tree_str, len);
504 de18fc63 2019-05-09 stsp n = fwrite(tree_str, 1, len, commitfile);
505 de18fc63 2019-05-09 stsp if (n != len) {
506 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
507 de18fc63 2019-05-09 stsp goto done;
508 de18fc63 2019-05-09 stsp }
509 e8f02263 2022-01-23 thomas commitsize += n;
510 de18fc63 2019-05-09 stsp
511 3ce1b845 2019-07-15 stsp if (parent_ids) {
512 10604dce 2021-09-24 thomas free(id_str);
513 10604dce 2021-09-24 thomas id_str = NULL;
514 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, parent_ids, entry) {
515 3ce1b845 2019-07-15 stsp char *parent_str = NULL;
516 de18fc63 2019-05-09 stsp
517 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
518 3ce1b845 2019-07-15 stsp if (err)
519 3ce1b845 2019-07-15 stsp goto done;
520 3ce1b845 2019-07-15 stsp if (asprintf(&parent_str, "%s%s\n",
521 3ce1b845 2019-07-15 stsp GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
522 3ce1b845 2019-07-15 stsp err = got_error_from_errno("asprintf");
523 3ce1b845 2019-07-15 stsp goto done;
524 3ce1b845 2019-07-15 stsp }
525 3ce1b845 2019-07-15 stsp len = strlen(parent_str);
526 3ce1b845 2019-07-15 stsp SHA1Update(&sha1_ctx, parent_str, len);
527 3ce1b845 2019-07-15 stsp n = fwrite(parent_str, 1, len, commitfile);
528 3ce1b845 2019-07-15 stsp if (n != len) {
529 3ce1b845 2019-07-15 stsp err = got_ferror(commitfile, GOT_ERR_IO);
530 3ce1b845 2019-07-15 stsp free(parent_str);
531 3ce1b845 2019-07-15 stsp goto done;
532 3ce1b845 2019-07-15 stsp }
533 e8f02263 2022-01-23 thomas commitsize += n;
534 de18fc63 2019-05-09 stsp free(parent_str);
535 10604dce 2021-09-24 thomas free(id_str);
536 10604dce 2021-09-24 thomas id_str = NULL;
537 de18fc63 2019-05-09 stsp }
538 de18fc63 2019-05-09 stsp }
539 de18fc63 2019-05-09 stsp
540 de18fc63 2019-05-09 stsp len = strlen(author_str);
541 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, author_str, len);
542 de18fc63 2019-05-09 stsp n = fwrite(author_str, 1, len, commitfile);
543 de18fc63 2019-05-09 stsp if (n != len) {
544 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
545 de18fc63 2019-05-09 stsp goto done;
546 de18fc63 2019-05-09 stsp }
547 e8f02263 2022-01-23 thomas commitsize += n;
548 de18fc63 2019-05-09 stsp
549 de18fc63 2019-05-09 stsp len = strlen(committer_str);
550 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, committer_str, len);
551 de18fc63 2019-05-09 stsp n = fwrite(committer_str, 1, len, commitfile);
552 de18fc63 2019-05-09 stsp if (n != len) {
553 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
554 de18fc63 2019-05-09 stsp goto done;
555 de18fc63 2019-05-09 stsp }
556 e8f02263 2022-01-23 thomas commitsize += n;
557 de18fc63 2019-05-09 stsp
558 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, "\n", 1);
559 de18fc63 2019-05-09 stsp n = fwrite("\n", 1, 1, commitfile);
560 de18fc63 2019-05-09 stsp if (n != 1) {
561 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
562 de18fc63 2019-05-09 stsp goto done;
563 de18fc63 2019-05-09 stsp }
564 e8f02263 2022-01-23 thomas commitsize += n;
565 de18fc63 2019-05-09 stsp
566 787c8eb6 2019-07-11 stsp len = strlen(msg);
567 787c8eb6 2019-07-11 stsp SHA1Update(&sha1_ctx, msg, len);
568 787c8eb6 2019-07-11 stsp n = fwrite(msg, 1, len, commitfile);
569 de18fc63 2019-05-09 stsp if (n != len) {
570 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
571 de18fc63 2019-05-09 stsp goto done;
572 de18fc63 2019-05-09 stsp }
573 e8f02263 2022-01-23 thomas commitsize += n;
574 de18fc63 2019-05-09 stsp
575 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, "\n", 1);
576 de18fc63 2019-05-09 stsp n = fwrite("\n", 1, 1, commitfile);
577 de18fc63 2019-05-09 stsp if (n != 1) {
578 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
579 de18fc63 2019-05-09 stsp goto done;
580 de18fc63 2019-05-09 stsp }
581 e8f02263 2022-01-23 thomas commitsize += n;
582 de18fc63 2019-05-09 stsp
583 de18fc63 2019-05-09 stsp *id = malloc(sizeof(**id));
584 de18fc63 2019-05-09 stsp if (*id == NULL) {
585 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
586 de18fc63 2019-05-09 stsp goto done;
587 de18fc63 2019-05-09 stsp }
588 de18fc63 2019-05-09 stsp SHA1Final((*id)->sha1, &sha1_ctx);
589 de18fc63 2019-05-09 stsp
590 de18fc63 2019-05-09 stsp if (fflush(commitfile) != 0) {
591 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
592 de18fc63 2019-05-09 stsp goto done;
593 de18fc63 2019-05-09 stsp }
594 de18fc63 2019-05-09 stsp rewind(commitfile);
595 de18fc63 2019-05-09 stsp
596 e8f02263 2022-01-23 thomas err = create_object_file(*id, commitfile, commitsize, repo);
597 de18fc63 2019-05-09 stsp done:
598 10604dce 2021-09-24 thomas free(id_str);
599 787c8eb6 2019-07-11 stsp free(msg0);
600 de18fc63 2019-05-09 stsp free(header);
601 de18fc63 2019-05-09 stsp free(tree_str);
602 de18fc63 2019-05-09 stsp free(author_str);
603 de18fc63 2019-05-09 stsp free(committer_str);
604 56b63ca4 2021-01-22 stsp if (commitfile && fclose(commitfile) == EOF && err == NULL)
605 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("fclose");
606 8e7bd50a 2019-08-22 stsp if (err) {
607 8e7bd50a 2019-08-22 stsp free(*id);
608 8e7bd50a 2019-08-22 stsp *id = NULL;
609 8e7bd50a 2019-08-22 stsp }
610 8e7bd50a 2019-08-22 stsp return err;
611 8e7bd50a 2019-08-22 stsp }
612 8e7bd50a 2019-08-22 stsp
613 8e7bd50a 2019-08-22 stsp const struct got_error *
614 8e7bd50a 2019-08-22 stsp got_object_tag_create(struct got_object_id **id,
615 8e7bd50a 2019-08-22 stsp const char *tag_name, struct got_object_id *object_id, const char *tagger,
616 64313a9c 2022-07-03 thomas time_t tagger_time, const char *tagmsg, const char *signer_id,
617 871bd038 2022-07-03 thomas struct got_repository *repo, int verbosity)
618 8e7bd50a 2019-08-22 stsp {
619 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
620 8e7bd50a 2019-08-22 stsp SHA1_CTX sha1_ctx;
621 8e7bd50a 2019-08-22 stsp char *header = NULL;
622 8e7bd50a 2019-08-22 stsp char *tag_str = NULL, *tagger_str = NULL;
623 8e7bd50a 2019-08-22 stsp char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
624 871bd038 2022-07-03 thomas size_t headerlen, len = 0, sig_len = 0, n;
625 8e7bd50a 2019-08-22 stsp FILE *tagfile = NULL;
626 e8f02263 2022-01-23 thomas off_t tagsize = 0;
627 8e7bd50a 2019-08-22 stsp char *msg0 = NULL, *msg;
628 8e7bd50a 2019-08-22 stsp const char *obj_type_str;
629 8e7bd50a 2019-08-22 stsp int obj_type;
630 871bd038 2022-07-03 thomas BUF *buf = NULL;
631 8e7bd50a 2019-08-22 stsp
632 8e7bd50a 2019-08-22 stsp *id = NULL;
633 8e7bd50a 2019-08-22 stsp
634 8e7bd50a 2019-08-22 stsp SHA1Init(&sha1_ctx);
635 8e7bd50a 2019-08-22 stsp
636 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&id_str, object_id);
637 8e7bd50a 2019-08-22 stsp if (err)
638 8e7bd50a 2019-08-22 stsp goto done;
639 8e7bd50a 2019-08-22 stsp if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
640 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
641 8e7bd50a 2019-08-22 stsp goto done;
642 8e7bd50a 2019-08-22 stsp }
643 8e7bd50a 2019-08-22 stsp
644 8e7bd50a 2019-08-22 stsp err = got_object_get_type(&obj_type, repo, object_id);
645 8e7bd50a 2019-08-22 stsp if (err)
646 8e7bd50a 2019-08-22 stsp goto done;
647 8e7bd50a 2019-08-22 stsp
648 8e7bd50a 2019-08-22 stsp switch (obj_type) {
649 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
650 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_BLOB;
651 8e7bd50a 2019-08-22 stsp break;
652 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
653 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_TREE;
654 8e7bd50a 2019-08-22 stsp break;
655 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
656 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_COMMIT;
657 8e7bd50a 2019-08-22 stsp break;
658 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
659 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_TAG;
660 8e7bd50a 2019-08-22 stsp break;
661 8e7bd50a 2019-08-22 stsp default:
662 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_OBJ_TYPE);
663 8e7bd50a 2019-08-22 stsp goto done;
664 8e7bd50a 2019-08-22 stsp }
665 8e7bd50a 2019-08-22 stsp
666 8e7bd50a 2019-08-22 stsp if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
667 8e7bd50a 2019-08-22 stsp obj_type_str) == -1) {
668 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
669 8e7bd50a 2019-08-22 stsp goto done;
670 8e7bd50a 2019-08-22 stsp }
671 8e7bd50a 2019-08-22 stsp
672 8e7bd50a 2019-08-22 stsp if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
673 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
674 8e7bd50a 2019-08-22 stsp goto done;
675 8e7bd50a 2019-08-22 stsp }
676 8e7bd50a 2019-08-22 stsp
677 8e7bd50a 2019-08-22 stsp if (asprintf(&tagger_str, "%s%s %lld +0000\n",
678 1367695b 2020-09-26 naddy GOT_TAG_LABEL_TAGGER, tagger, (long long)tagger_time) == -1)
679 8e7bd50a 2019-08-22 stsp return got_error_from_errno("asprintf");
680 8e7bd50a 2019-08-22 stsp
681 8e7bd50a 2019-08-22 stsp msg0 = strdup(tagmsg);
682 8e7bd50a 2019-08-22 stsp if (msg0 == NULL) {
683 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
684 8e7bd50a 2019-08-22 stsp goto done;
685 8e7bd50a 2019-08-22 stsp }
686 8e7bd50a 2019-08-22 stsp msg = msg0;
687 8e7bd50a 2019-08-22 stsp
688 8e7bd50a 2019-08-22 stsp while (isspace((unsigned char)msg[0]))
689 8e7bd50a 2019-08-22 stsp msg++;
690 8e7bd50a 2019-08-22 stsp
691 64313a9c 2022-07-03 thomas if (signer_id) {
692 871bd038 2022-07-03 thomas FILE *out;
693 871bd038 2022-07-03 thomas pid_t pid;
694 871bd038 2022-07-03 thomas size_t len;
695 871bd038 2022-07-03 thomas int in_fd, out_fd;
696 871bd038 2022-07-03 thomas int status;
697 871bd038 2022-07-03 thomas
698 871bd038 2022-07-03 thomas err = buf_alloc(&buf, 0);
699 871bd038 2022-07-03 thomas if (err)
700 871bd038 2022-07-03 thomas goto done;
701 871bd038 2022-07-03 thomas
702 871bd038 2022-07-03 thomas /* signed message */
703 871bd038 2022-07-03 thomas err = buf_puts(&len, buf, obj_str);
704 871bd038 2022-07-03 thomas if (err)
705 871bd038 2022-07-03 thomas goto done;
706 871bd038 2022-07-03 thomas err = buf_puts(&len, buf, type_str);
707 871bd038 2022-07-03 thomas if (err)
708 871bd038 2022-07-03 thomas goto done;
709 871bd038 2022-07-03 thomas err = buf_puts(&len, buf, tag_str);
710 871bd038 2022-07-03 thomas if (err)
711 871bd038 2022-07-03 thomas goto done;
712 871bd038 2022-07-03 thomas err = buf_puts(&len, buf, tagger_str);
713 871bd038 2022-07-03 thomas if (err)
714 871bd038 2022-07-03 thomas goto done;
715 871bd038 2022-07-03 thomas err = buf_putc(buf, '\n');
716 871bd038 2022-07-03 thomas if (err)
717 871bd038 2022-07-03 thomas goto done;
718 871bd038 2022-07-03 thomas err = buf_puts(&len, buf, msg);
719 871bd038 2022-07-03 thomas if (err)
720 871bd038 2022-07-03 thomas goto done;
721 871bd038 2022-07-03 thomas err = buf_putc(buf, '\n');
722 871bd038 2022-07-03 thomas if (err)
723 871bd038 2022-07-03 thomas goto done;
724 871bd038 2022-07-03 thomas
725 64313a9c 2022-07-03 thomas err = got_sigs_sign_tag_ssh(&pid, &in_fd, &out_fd, signer_id,
726 871bd038 2022-07-03 thomas verbosity);
727 871bd038 2022-07-03 thomas if (err)
728 871bd038 2022-07-03 thomas goto done;
729 871bd038 2022-07-03 thomas if (buf_write_fd(buf, in_fd) == -1) {
730 871bd038 2022-07-03 thomas err = got_error_from_errno("write");
731 871bd038 2022-07-03 thomas goto done;
732 871bd038 2022-07-03 thomas }
733 871bd038 2022-07-03 thomas if (close(in_fd) == -1) {
734 871bd038 2022-07-03 thomas err = got_error_from_errno("close");
735 871bd038 2022-07-03 thomas goto done;
736 871bd038 2022-07-03 thomas }
737 8e7bd50a 2019-08-22 stsp
738 871bd038 2022-07-03 thomas if (waitpid(pid, &status, 0) == -1) {
739 871bd038 2022-07-03 thomas err = got_error_from_errno("waitpid");
740 871bd038 2022-07-03 thomas goto done;
741 871bd038 2022-07-03 thomas }
742 64313a9c 2022-07-03 thomas if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
743 64313a9c 2022-07-03 thomas err = got_error(GOT_ERR_SIGNING_TAG);
744 64313a9c 2022-07-03 thomas goto done;
745 64313a9c 2022-07-03 thomas }
746 871bd038 2022-07-03 thomas
747 871bd038 2022-07-03 thomas out = fdopen(out_fd, "r");
748 871bd038 2022-07-03 thomas if (out == NULL) {
749 871bd038 2022-07-03 thomas err = got_error_from_errno("fdopen");
750 871bd038 2022-07-03 thomas goto done;
751 871bd038 2022-07-03 thomas }
752 871bd038 2022-07-03 thomas buf_empty(buf);
753 871bd038 2022-07-03 thomas err = buf_load(&buf, out);
754 871bd038 2022-07-03 thomas if (err)
755 871bd038 2022-07-03 thomas goto done;
756 871bd038 2022-07-03 thomas sig_len = buf_len(buf) + 1;
757 871bd038 2022-07-03 thomas err = buf_putc(buf, '\0');
758 871bd038 2022-07-03 thomas if (err)
759 871bd038 2022-07-03 thomas goto done;
760 871bd038 2022-07-03 thomas if (close(out_fd) == -1) {
761 871bd038 2022-07-03 thomas err = got_error_from_errno("close");
762 871bd038 2022-07-03 thomas goto done;
763 871bd038 2022-07-03 thomas }
764 871bd038 2022-07-03 thomas }
765 871bd038 2022-07-03 thomas
766 871bd038 2022-07-03 thomas len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
767 871bd038 2022-07-03 thomas strlen(tagger_str) + 1 + strlen(msg) + 1 + sig_len;
768 8e7bd50a 2019-08-22 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
769 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
770 8e7bd50a 2019-08-22 stsp goto done;
771 8e7bd50a 2019-08-22 stsp }
772 8e7bd50a 2019-08-22 stsp
773 8e7bd50a 2019-08-22 stsp headerlen = strlen(header) + 1;
774 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, header, headerlen);
775 8e7bd50a 2019-08-22 stsp
776 8e7bd50a 2019-08-22 stsp tagfile = got_opentemp();
777 8e7bd50a 2019-08-22 stsp if (tagfile == NULL) {
778 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_opentemp");
779 8e7bd50a 2019-08-22 stsp goto done;
780 8e7bd50a 2019-08-22 stsp }
781 8e7bd50a 2019-08-22 stsp
782 8e7bd50a 2019-08-22 stsp n = fwrite(header, 1, headerlen, tagfile);
783 8e7bd50a 2019-08-22 stsp if (n != headerlen) {
784 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
785 8e7bd50a 2019-08-22 stsp goto done;
786 8e7bd50a 2019-08-22 stsp }
787 e8f02263 2022-01-23 thomas tagsize += headerlen;
788 8e7bd50a 2019-08-22 stsp len = strlen(obj_str);
789 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, obj_str, len);
790 8e7bd50a 2019-08-22 stsp n = fwrite(obj_str, 1, len, tagfile);
791 8e7bd50a 2019-08-22 stsp if (n != len) {
792 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
793 8e7bd50a 2019-08-22 stsp goto done;
794 8e7bd50a 2019-08-22 stsp }
795 e8f02263 2022-01-23 thomas tagsize += n;
796 8e7bd50a 2019-08-22 stsp len = strlen(type_str);
797 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, type_str, len);
798 8e7bd50a 2019-08-22 stsp n = fwrite(type_str, 1, len, tagfile);
799 8e7bd50a 2019-08-22 stsp if (n != len) {
800 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
801 8e7bd50a 2019-08-22 stsp goto done;
802 8e7bd50a 2019-08-22 stsp }
803 e8f02263 2022-01-23 thomas tagsize += n;
804 8e7bd50a 2019-08-22 stsp
805 8e7bd50a 2019-08-22 stsp len = strlen(tag_str);
806 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, tag_str, len);
807 8e7bd50a 2019-08-22 stsp n = fwrite(tag_str, 1, len, tagfile);
808 8e7bd50a 2019-08-22 stsp if (n != len) {
809 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
810 8e7bd50a 2019-08-22 stsp goto done;
811 8e7bd50a 2019-08-22 stsp }
812 e8f02263 2022-01-23 thomas tagsize += n;
813 8e7bd50a 2019-08-22 stsp
814 8e7bd50a 2019-08-22 stsp len = strlen(tagger_str);
815 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, tagger_str, len);
816 8e7bd50a 2019-08-22 stsp n = fwrite(tagger_str, 1, len, tagfile);
817 8e7bd50a 2019-08-22 stsp if (n != len) {
818 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
819 8e7bd50a 2019-08-22 stsp goto done;
820 8e7bd50a 2019-08-22 stsp }
821 e8f02263 2022-01-23 thomas tagsize += n;
822 8e7bd50a 2019-08-22 stsp
823 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, "\n", 1);
824 8e7bd50a 2019-08-22 stsp n = fwrite("\n", 1, 1, tagfile);
825 8e7bd50a 2019-08-22 stsp if (n != 1) {
826 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
827 8e7bd50a 2019-08-22 stsp goto done;
828 8e7bd50a 2019-08-22 stsp }
829 e8f02263 2022-01-23 thomas tagsize += n;
830 8e7bd50a 2019-08-22 stsp
831 8e7bd50a 2019-08-22 stsp len = strlen(msg);
832 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, msg, len);
833 8e7bd50a 2019-08-22 stsp n = fwrite(msg, 1, len, tagfile);
834 8e7bd50a 2019-08-22 stsp if (n != len) {
835 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
836 8e7bd50a 2019-08-22 stsp goto done;
837 8e7bd50a 2019-08-22 stsp }
838 e8f02263 2022-01-23 thomas tagsize += n;
839 8e7bd50a 2019-08-22 stsp
840 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, "\n", 1);
841 8e7bd50a 2019-08-22 stsp n = fwrite("\n", 1, 1, tagfile);
842 8e7bd50a 2019-08-22 stsp if (n != 1) {
843 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
844 8e7bd50a 2019-08-22 stsp goto done;
845 8e7bd50a 2019-08-22 stsp }
846 e8f02263 2022-01-23 thomas tagsize += n;
847 8e7bd50a 2019-08-22 stsp
848 64313a9c 2022-07-03 thomas if (signer_id && buf_len(buf) > 0) {
849 871bd038 2022-07-03 thomas len = buf_len(buf);
850 871bd038 2022-07-03 thomas SHA1Update(&sha1_ctx, buf_get(buf), len);
851 871bd038 2022-07-03 thomas n = fwrite(buf_get(buf), 1, len, tagfile);
852 871bd038 2022-07-03 thomas if (n != len) {
853 871bd038 2022-07-03 thomas err = got_ferror(tagfile, GOT_ERR_IO);
854 871bd038 2022-07-03 thomas goto done;
855 871bd038 2022-07-03 thomas }
856 871bd038 2022-07-03 thomas tagsize += n;
857 871bd038 2022-07-03 thomas }
858 871bd038 2022-07-03 thomas
859 8e7bd50a 2019-08-22 stsp *id = malloc(sizeof(**id));
860 8e7bd50a 2019-08-22 stsp if (*id == NULL) {
861 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("malloc");
862 8e7bd50a 2019-08-22 stsp goto done;
863 8e7bd50a 2019-08-22 stsp }
864 8e7bd50a 2019-08-22 stsp SHA1Final((*id)->sha1, &sha1_ctx);
865 8e7bd50a 2019-08-22 stsp
866 8e7bd50a 2019-08-22 stsp if (fflush(tagfile) != 0) {
867 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("fflush");
868 8e7bd50a 2019-08-22 stsp goto done;
869 8e7bd50a 2019-08-22 stsp }
870 8e7bd50a 2019-08-22 stsp rewind(tagfile);
871 8e7bd50a 2019-08-22 stsp
872 e8f02263 2022-01-23 thomas err = create_object_file(*id, tagfile, tagsize, repo);
873 8e7bd50a 2019-08-22 stsp done:
874 8e7bd50a 2019-08-22 stsp free(msg0);
875 8e7bd50a 2019-08-22 stsp free(header);
876 8e7bd50a 2019-08-22 stsp free(obj_str);
877 8e7bd50a 2019-08-22 stsp free(tagger_str);
878 871bd038 2022-07-03 thomas if (buf)
879 871bd038 2022-07-03 thomas buf_release(buf);
880 56b63ca4 2021-01-22 stsp if (tagfile && fclose(tagfile) == EOF && err == NULL)
881 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
882 f91abf81 2019-04-14 stsp if (err) {
883 f91abf81 2019-04-14 stsp free(*id);
884 f91abf81 2019-04-14 stsp *id = NULL;
885 f91abf81 2019-04-14 stsp }
886 f91abf81 2019-04-14 stsp return err;
887 f91abf81 2019-04-14 stsp }