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