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