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 44edeea7 2019-04-11 stsp #include "got_lib_lockfile.h"
43 44edeea7 2019-04-11 stsp
44 44edeea7 2019-04-11 stsp #ifndef nitems
45 44edeea7 2019-04-11 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
46 44edeea7 2019-04-11 stsp #endif
47 44edeea7 2019-04-11 stsp
48 ac1c5662 2019-04-13 stsp static const struct got_error *
49 76f564d5 2019-04-14 stsp create_object_file(struct got_object_id *id, FILE *content,
50 ac1c5662 2019-04-13 stsp struct got_repository *repo)
51 ac1c5662 2019-04-13 stsp {
52 ac1c5662 2019-04-13 stsp const struct got_error *err = NULL, *unlock_err = NULL;
53 c6f826b4 2019-04-13 stsp char *objpath = NULL, *tmppath = NULL;
54 c6f826b4 2019-04-13 stsp FILE *tmpfile = NULL;
55 ac1c5662 2019-04-13 stsp struct got_lockfile *lf = NULL;
56 c6f826b4 2019-04-13 stsp size_t tmplen = 0;
57 ac1c5662 2019-04-13 stsp
58 ac1c5662 2019-04-13 stsp err = got_object_get_path(&objpath, id, repo);
59 ac1c5662 2019-04-13 stsp if (err)
60 ac1c5662 2019-04-13 stsp return err;
61 ac1c5662 2019-04-13 stsp
62 c6f826b4 2019-04-13 stsp err = got_opentemp_named(&tmppath, &tmpfile, objpath);
63 ac1c5662 2019-04-13 stsp if (err) {
64 ac1c5662 2019-04-13 stsp char *parent_path;
65 ac1c5662 2019-04-13 stsp if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
66 ac1c5662 2019-04-13 stsp goto done;
67 ac1c5662 2019-04-13 stsp err = got_path_dirname(&parent_path, objpath);
68 ac1c5662 2019-04-13 stsp if (err)
69 ac1c5662 2019-04-13 stsp goto done;
70 ac1c5662 2019-04-13 stsp err = got_path_mkdir(parent_path);
71 ac1c5662 2019-04-13 stsp free(parent_path);
72 ac1c5662 2019-04-13 stsp if (err)
73 ac1c5662 2019-04-13 stsp goto done;
74 c6f826b4 2019-04-13 stsp err = got_opentemp_named(&tmppath, &tmpfile, objpath);
75 ac1c5662 2019-04-13 stsp if (err)
76 ac1c5662 2019-04-13 stsp goto done;
77 ac1c5662 2019-04-13 stsp }
78 ac1c5662 2019-04-13 stsp
79 c6f826b4 2019-04-13 stsp err = got_deflate_to_file(&tmplen, content, tmpfile);
80 ac1c5662 2019-04-13 stsp if (err)
81 ac1c5662 2019-04-13 stsp goto done;
82 ac1c5662 2019-04-13 stsp
83 ac1c5662 2019-04-13 stsp err = got_lockfile_lock(&lf, objpath);
84 ac1c5662 2019-04-13 stsp if (err)
85 ac1c5662 2019-04-13 stsp goto done;
86 ac1c5662 2019-04-13 stsp
87 c6f826b4 2019-04-13 stsp if (rename(tmppath, objpath) != 0) {
88 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, objpath);
89 ac1c5662 2019-04-13 stsp goto done;
90 ac1c5662 2019-04-13 stsp }
91 c6f826b4 2019-04-13 stsp free(tmppath);
92 c6f826b4 2019-04-13 stsp tmppath = NULL;
93 ac1c5662 2019-04-13 stsp
94 ac1c5662 2019-04-13 stsp if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
95 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", objpath);
96 ac1c5662 2019-04-13 stsp goto done;
97 ac1c5662 2019-04-13 stsp }
98 ac1c5662 2019-04-13 stsp done:
99 ac1c5662 2019-04-13 stsp free(objpath);
100 c6f826b4 2019-04-13 stsp if (tmppath) {
101 c6f826b4 2019-04-13 stsp if (unlink(tmppath) != 0 && err == NULL)
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", tmppath);
103 c6f826b4 2019-04-13 stsp free(tmppath);
104 ac1c5662 2019-04-13 stsp }
105 c6f826b4 2019-04-13 stsp if (tmpfile && fclose(tmpfile) != 0 && err == NULL)
106 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
107 ac1c5662 2019-04-13 stsp if (lf)
108 ac1c5662 2019-04-13 stsp unlock_err = got_lockfile_unlock(lf);
109 ac1c5662 2019-04-13 stsp return err ? err : unlock_err;
110 ac1c5662 2019-04-13 stsp }
111 ac1c5662 2019-04-13 stsp
112 44edeea7 2019-04-11 stsp const struct got_error *
113 f970685c 2019-04-13 stsp got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
114 f970685c 2019-04-13 stsp struct got_repository *repo)
115 44edeea7 2019-04-11 stsp {
116 ac1c5662 2019-04-13 stsp const struct got_error *err = NULL;
117 ac1c5662 2019-04-13 stsp char *header = NULL;
118 ac1c5662 2019-04-13 stsp FILE *blobfile = NULL;
119 44edeea7 2019-04-11 stsp int fd = -1;
120 44edeea7 2019-04-11 stsp struct stat sb;
121 44edeea7 2019-04-11 stsp SHA1_CTX sha1_ctx;
122 ac1c5662 2019-04-13 stsp size_t headerlen = 0, n;
123 44edeea7 2019-04-11 stsp
124 44edeea7 2019-04-11 stsp *id = NULL;
125 44edeea7 2019-04-11 stsp
126 44edeea7 2019-04-11 stsp SHA1Init(&sha1_ctx);
127 44edeea7 2019-04-11 stsp
128 44edeea7 2019-04-11 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
129 44edeea7 2019-04-11 stsp if (fd == -1)
130 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
131 44edeea7 2019-04-11 stsp
132 44edeea7 2019-04-11 stsp if (fstat(fd, &sb) == -1) {
133 638f9024 2019-05-13 stsp err = got_error_from_errno2("fstat", ondisk_path);
134 44edeea7 2019-04-11 stsp goto done;
135 44edeea7 2019-04-11 stsp }
136 44edeea7 2019-04-11 stsp
137 44edeea7 2019-04-11 stsp if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
138 44edeea7 2019-04-11 stsp sb.st_size) == -1) {
139 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
140 44edeea7 2019-04-11 stsp goto done;
141 44edeea7 2019-04-11 stsp }
142 ffb286fd 2019-04-11 stsp headerlen = strlen(header) + 1;
143 ffb286fd 2019-04-11 stsp SHA1Update(&sha1_ctx, header, headerlen);
144 44edeea7 2019-04-11 stsp
145 81984c6b 2019-04-11 stsp blobfile = got_opentemp();
146 81984c6b 2019-04-11 stsp if (blobfile == NULL) {
147 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
148 44edeea7 2019-04-11 stsp goto done;
149 81984c6b 2019-04-11 stsp }
150 44edeea7 2019-04-11 stsp
151 ac1c5662 2019-04-13 stsp n = fwrite(header, 1, headerlen, blobfile);
152 ac1c5662 2019-04-13 stsp if (n != headerlen) {
153 f16c2465 2019-04-11 stsp err = got_ferror(blobfile, GOT_ERR_IO);
154 f16c2465 2019-04-11 stsp goto done;
155 f16c2465 2019-04-11 stsp }
156 656b1f76 2019-05-11 jcs for (;;) {
157 44edeea7 2019-04-11 stsp char buf[8192];
158 a14a8cf6 2019-04-11 stsp ssize_t inlen;
159 44edeea7 2019-04-11 stsp
160 a14a8cf6 2019-04-11 stsp inlen = read(fd, buf, sizeof(buf));
161 a14a8cf6 2019-04-11 stsp if (inlen == -1) {
162 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
163 a14a8cf6 2019-04-11 stsp goto done;
164 44edeea7 2019-04-11 stsp }
165 a14a8cf6 2019-04-11 stsp if (inlen == 0)
166 a14a8cf6 2019-04-11 stsp break; /* EOF */
167 44edeea7 2019-04-11 stsp SHA1Update(&sha1_ctx, buf, inlen);
168 ac1c5662 2019-04-13 stsp n = fwrite(buf, 1, inlen, blobfile);
169 ac1c5662 2019-04-13 stsp if (n != inlen) {
170 44edeea7 2019-04-11 stsp err = got_ferror(blobfile, GOT_ERR_IO);
171 44edeea7 2019-04-11 stsp goto done;
172 44edeea7 2019-04-11 stsp }
173 44edeea7 2019-04-11 stsp }
174 44edeea7 2019-04-11 stsp
175 44edeea7 2019-04-11 stsp *id = malloc(sizeof(**id));
176 44edeea7 2019-04-11 stsp if (*id == NULL) {
177 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
178 44edeea7 2019-04-11 stsp goto done;
179 44edeea7 2019-04-11 stsp }
180 4be2a0b4 2019-04-14 stsp SHA1Final((*id)->sha1, &sha1_ctx);
181 44edeea7 2019-04-11 stsp
182 44edeea7 2019-04-11 stsp if (fflush(blobfile) != 0) {
183 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
184 44edeea7 2019-04-11 stsp goto done;
185 44edeea7 2019-04-11 stsp }
186 44edeea7 2019-04-11 stsp rewind(blobfile);
187 44edeea7 2019-04-11 stsp
188 76f564d5 2019-04-14 stsp err = create_object_file(*id, blobfile, repo);
189 44edeea7 2019-04-11 stsp done:
190 44edeea7 2019-04-11 stsp free(header);
191 44edeea7 2019-04-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
192 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
193 44edeea7 2019-04-11 stsp if (blobfile && fclose(blobfile) != 0 && err == NULL)
194 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
195 44edeea7 2019-04-11 stsp if (err) {
196 44edeea7 2019-04-11 stsp free(*id);
197 44edeea7 2019-04-11 stsp *id = NULL;
198 44edeea7 2019-04-11 stsp }
199 ac1c5662 2019-04-13 stsp return err;
200 44edeea7 2019-04-11 stsp }
201 f91abf81 2019-04-14 stsp
202 f91abf81 2019-04-14 stsp static const struct got_error *
203 f91abf81 2019-04-14 stsp mode2str(char *buf, size_t len, mode_t mode)
204 f91abf81 2019-04-14 stsp {
205 f91abf81 2019-04-14 stsp int ret;
206 f91abf81 2019-04-14 stsp ret = snprintf(buf, len, "%o ", mode);
207 f91abf81 2019-04-14 stsp if (ret == -1 || ret >= len)
208 f91abf81 2019-04-14 stsp return got_error(GOT_ERR_NO_SPACE);
209 f91abf81 2019-04-14 stsp return NULL;
210 f91abf81 2019-04-14 stsp }
211 f91abf81 2019-04-14 stsp
212 f91abf81 2019-04-14 stsp const struct got_error *
213 f91abf81 2019-04-14 stsp got_object_tree_create(struct got_object_id **id,
214 f91abf81 2019-04-14 stsp struct got_tree_entries *entries, struct got_repository *repo)
215 f91abf81 2019-04-14 stsp {
216 f91abf81 2019-04-14 stsp const struct got_error *err = NULL;
217 f91abf81 2019-04-14 stsp char modebuf[sizeof("100644 ")];
218 f91abf81 2019-04-14 stsp SHA1_CTX sha1_ctx;
219 f91abf81 2019-04-14 stsp char *header = NULL;
220 f91abf81 2019-04-14 stsp size_t headerlen, len = 0, n;
221 f91abf81 2019-04-14 stsp FILE *treefile = NULL;
222 f91abf81 2019-04-14 stsp struct got_tree_entry *te;
223 f91abf81 2019-04-14 stsp
224 f91abf81 2019-04-14 stsp *id = NULL;
225 51c32763 2019-05-09 stsp
226 51c32763 2019-05-09 stsp SHA1Init(&sha1_ctx);
227 f91abf81 2019-04-14 stsp
228 f91abf81 2019-04-14 stsp SIMPLEQ_FOREACH(te, &entries->head, entry) {
229 f91abf81 2019-04-14 stsp err = mode2str(modebuf, sizeof(modebuf), te->mode);
230 f91abf81 2019-04-14 stsp if (err)
231 f91abf81 2019-04-14 stsp return err;
232 f91abf81 2019-04-14 stsp len += strlen(modebuf) + strlen(te->name) + 1 +
233 f91abf81 2019-04-14 stsp SHA1_DIGEST_LENGTH;
234 f91abf81 2019-04-14 stsp }
235 f91abf81 2019-04-14 stsp
236 f91abf81 2019-04-14 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
237 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
238 f91abf81 2019-04-14 stsp goto done;
239 f91abf81 2019-04-14 stsp }
240 f91abf81 2019-04-14 stsp headerlen = strlen(header) + 1;
241 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, header, headerlen);
242 f91abf81 2019-04-14 stsp
243 f91abf81 2019-04-14 stsp treefile = got_opentemp();
244 f91abf81 2019-04-14 stsp if (treefile == NULL) {
245 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
246 f91abf81 2019-04-14 stsp goto done;
247 f91abf81 2019-04-14 stsp }
248 f91abf81 2019-04-14 stsp
249 f91abf81 2019-04-14 stsp n = fwrite(header, 1, headerlen, treefile);
250 f91abf81 2019-04-14 stsp if (n != headerlen) {
251 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
252 f91abf81 2019-04-14 stsp goto done;
253 f91abf81 2019-04-14 stsp }
254 f91abf81 2019-04-14 stsp
255 f91abf81 2019-04-14 stsp SIMPLEQ_FOREACH(te, &entries->head, entry) {
256 f91abf81 2019-04-14 stsp err = mode2str(modebuf, sizeof(modebuf), te->mode);
257 f91abf81 2019-04-14 stsp if (err)
258 f91abf81 2019-04-14 stsp goto done;
259 f91abf81 2019-04-14 stsp len = strlen(modebuf);
260 f91abf81 2019-04-14 stsp n = fwrite(modebuf, 1, len, treefile);
261 f91abf81 2019-04-14 stsp if (n != len) {
262 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
263 f91abf81 2019-04-14 stsp goto done;
264 f91abf81 2019-04-14 stsp }
265 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, modebuf, len);
266 f91abf81 2019-04-14 stsp
267 f91abf81 2019-04-14 stsp len = strlen(te->name) + 1; /* must include NUL */
268 f91abf81 2019-04-14 stsp n = fwrite(te->name, 1, len, treefile);
269 f91abf81 2019-04-14 stsp if (n != len) {
270 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
271 f91abf81 2019-04-14 stsp goto done;
272 f91abf81 2019-04-14 stsp }
273 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, te->name, len);
274 f91abf81 2019-04-14 stsp
275 f91abf81 2019-04-14 stsp len = SHA1_DIGEST_LENGTH;
276 f91abf81 2019-04-14 stsp n = fwrite(te->id->sha1, 1, len, treefile);
277 f91abf81 2019-04-14 stsp if (n != len) {
278 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
279 f91abf81 2019-04-14 stsp goto done;
280 f91abf81 2019-04-14 stsp }
281 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, te->id->sha1, len);
282 f91abf81 2019-04-14 stsp }
283 f91abf81 2019-04-14 stsp
284 f91abf81 2019-04-14 stsp *id = malloc(sizeof(**id));
285 f91abf81 2019-04-14 stsp if (*id == NULL) {
286 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
287 f91abf81 2019-04-14 stsp goto done;
288 f91abf81 2019-04-14 stsp }
289 4be2a0b4 2019-04-14 stsp SHA1Final((*id)->sha1, &sha1_ctx);
290 f91abf81 2019-04-14 stsp
291 f91abf81 2019-04-14 stsp if (fflush(treefile) != 0) {
292 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
293 f91abf81 2019-04-14 stsp goto done;
294 f91abf81 2019-04-14 stsp }
295 f91abf81 2019-04-14 stsp rewind(treefile);
296 f91abf81 2019-04-14 stsp
297 76f564d5 2019-04-14 stsp err = create_object_file(*id, treefile, repo);
298 f91abf81 2019-04-14 stsp done:
299 f91abf81 2019-04-14 stsp free(header);
300 f91abf81 2019-04-14 stsp if (treefile && fclose(treefile) != 0 && err == NULL)
301 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
302 de18fc63 2019-05-09 stsp if (err) {
303 de18fc63 2019-05-09 stsp free(*id);
304 de18fc63 2019-05-09 stsp *id = NULL;
305 de18fc63 2019-05-09 stsp }
306 de18fc63 2019-05-09 stsp return err;
307 de18fc63 2019-05-09 stsp }
308 de18fc63 2019-05-09 stsp
309 de18fc63 2019-05-09 stsp const struct got_error *
310 de18fc63 2019-05-09 stsp got_object_commit_create(struct got_object_id **id,
311 de18fc63 2019-05-09 stsp struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
312 de18fc63 2019-05-09 stsp int nparents, const char *author, time_t author_time,
313 de18fc63 2019-05-09 stsp const char *committer, time_t committer_time,
314 de18fc63 2019-05-09 stsp const char *logmsg, struct got_repository *repo)
315 de18fc63 2019-05-09 stsp {
316 de18fc63 2019-05-09 stsp const struct got_error *err = NULL;
317 de18fc63 2019-05-09 stsp SHA1_CTX sha1_ctx;
318 de18fc63 2019-05-09 stsp char *header = NULL, *tree_str = NULL;
319 de18fc63 2019-05-09 stsp char *author_str = NULL, *committer_str = NULL;
320 de18fc63 2019-05-09 stsp char *id_str = NULL;
321 de18fc63 2019-05-09 stsp size_t headerlen, len = 0, n;
322 de18fc63 2019-05-09 stsp FILE *commitfile = NULL;
323 de18fc63 2019-05-09 stsp struct got_object_qid *qid;
324 787c8eb6 2019-07-11 stsp char *msg0, *msg;
325 de18fc63 2019-05-09 stsp
326 de18fc63 2019-05-09 stsp *id = NULL;
327 de18fc63 2019-05-09 stsp
328 de18fc63 2019-05-09 stsp SHA1Init(&sha1_ctx);
329 787c8eb6 2019-07-11 stsp
330 787c8eb6 2019-07-11 stsp msg0 = strdup(logmsg);
331 787c8eb6 2019-07-11 stsp if (msg0 == NULL)
332 787c8eb6 2019-07-11 stsp return got_error_from_errno("strdup");
333 787c8eb6 2019-07-11 stsp msg = msg0;
334 787c8eb6 2019-07-11 stsp
335 10796104 2019-07-11 stsp while (isspace((unsigned char)msg[0]))
336 787c8eb6 2019-07-11 stsp msg++;
337 787c8eb6 2019-07-11 stsp len = strlen(msg);
338 10796104 2019-07-11 stsp while (len > 0 && isspace((unsigned char)msg[len - 1])) {
339 787c8eb6 2019-07-11 stsp msg[len - 1] = '\0';
340 787c8eb6 2019-07-11 stsp len--;
341 787c8eb6 2019-07-11 stsp }
342 de18fc63 2019-05-09 stsp
343 de18fc63 2019-05-09 stsp if (asprintf(&author_str, "%s%s %lld +0000\n",
344 de18fc63 2019-05-09 stsp GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
345 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
346 de18fc63 2019-05-09 stsp
347 de18fc63 2019-05-09 stsp if (asprintf(&committer_str, "%s%s %lld +0000\n",
348 de18fc63 2019-05-09 stsp GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
349 de18fc63 2019-05-09 stsp committer ? committer_time : author_time)
350 de18fc63 2019-05-09 stsp == -1) {
351 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
352 de18fc63 2019-05-09 stsp goto done;
353 de18fc63 2019-05-09 stsp }
354 de18fc63 2019-05-09 stsp
355 de18fc63 2019-05-09 stsp len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
356 de18fc63 2019-05-09 stsp nparents *
357 de18fc63 2019-05-09 stsp (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
358 787c8eb6 2019-07-11 stsp + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
359 de18fc63 2019-05-09 stsp
360 de18fc63 2019-05-09 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
361 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
362 de18fc63 2019-05-09 stsp goto done;
363 de18fc63 2019-05-09 stsp }
364 de18fc63 2019-05-09 stsp headerlen = strlen(header) + 1;
365 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, header, headerlen);
366 de18fc63 2019-05-09 stsp
367 de18fc63 2019-05-09 stsp commitfile = got_opentemp();
368 de18fc63 2019-05-09 stsp if (commitfile == NULL) {
369 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
370 de18fc63 2019-05-09 stsp goto done;
371 de18fc63 2019-05-09 stsp }
372 de18fc63 2019-05-09 stsp
373 de18fc63 2019-05-09 stsp n = fwrite(header, 1, headerlen, commitfile);
374 de18fc63 2019-05-09 stsp if (n != headerlen) {
375 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
376 de18fc63 2019-05-09 stsp goto done;
377 de18fc63 2019-05-09 stsp }
378 de18fc63 2019-05-09 stsp
379 de18fc63 2019-05-09 stsp err = got_object_id_str(&id_str, tree_id);
380 de18fc63 2019-05-09 stsp if (err)
381 de18fc63 2019-05-09 stsp goto done;
382 de18fc63 2019-05-09 stsp if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
383 de18fc63 2019-05-09 stsp == -1) {
384 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
385 de18fc63 2019-05-09 stsp goto done;
386 de18fc63 2019-05-09 stsp }
387 de18fc63 2019-05-09 stsp len = strlen(tree_str);
388 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, tree_str, len);
389 de18fc63 2019-05-09 stsp n = fwrite(tree_str, 1, len, commitfile);
390 de18fc63 2019-05-09 stsp if (n != len) {
391 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
392 de18fc63 2019-05-09 stsp goto done;
393 de18fc63 2019-05-09 stsp }
394 de18fc63 2019-05-09 stsp
395 de18fc63 2019-05-09 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
396 de18fc63 2019-05-09 stsp char *parent_str = NULL;
397 de18fc63 2019-05-09 stsp
398 de18fc63 2019-05-09 stsp free(id_str);
399 de18fc63 2019-05-09 stsp
400 de18fc63 2019-05-09 stsp err = got_object_id_str(&id_str, qid->id);
401 de18fc63 2019-05-09 stsp if (err)
402 de18fc63 2019-05-09 stsp goto done;
403 de18fc63 2019-05-09 stsp if (asprintf(&parent_str, "%s%s\n", GOT_COMMIT_LABEL_PARENT,
404 de18fc63 2019-05-09 stsp id_str) == -1) {
405 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
406 de18fc63 2019-05-09 stsp goto done;
407 de18fc63 2019-05-09 stsp }
408 de18fc63 2019-05-09 stsp len = strlen(parent_str);
409 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, parent_str, len);
410 de18fc63 2019-05-09 stsp n = fwrite(parent_str, 1, len, commitfile);
411 de18fc63 2019-05-09 stsp if (n != len) {
412 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
413 de18fc63 2019-05-09 stsp free(parent_str);
414 de18fc63 2019-05-09 stsp goto done;
415 de18fc63 2019-05-09 stsp }
416 de18fc63 2019-05-09 stsp free(parent_str);
417 de18fc63 2019-05-09 stsp }
418 de18fc63 2019-05-09 stsp
419 de18fc63 2019-05-09 stsp len = strlen(author_str);
420 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, author_str, len);
421 de18fc63 2019-05-09 stsp n = fwrite(author_str, 1, len, commitfile);
422 de18fc63 2019-05-09 stsp if (n != len) {
423 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
424 de18fc63 2019-05-09 stsp goto done;
425 de18fc63 2019-05-09 stsp }
426 de18fc63 2019-05-09 stsp
427 de18fc63 2019-05-09 stsp len = strlen(committer_str);
428 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, committer_str, len);
429 de18fc63 2019-05-09 stsp n = fwrite(committer_str, 1, len, commitfile);
430 de18fc63 2019-05-09 stsp if (n != len) {
431 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
432 de18fc63 2019-05-09 stsp goto done;
433 de18fc63 2019-05-09 stsp }
434 de18fc63 2019-05-09 stsp
435 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, "\n", 1);
436 de18fc63 2019-05-09 stsp n = fwrite("\n", 1, 1, commitfile);
437 de18fc63 2019-05-09 stsp if (n != 1) {
438 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
439 de18fc63 2019-05-09 stsp goto done;
440 de18fc63 2019-05-09 stsp }
441 de18fc63 2019-05-09 stsp
442 787c8eb6 2019-07-11 stsp len = strlen(msg);
443 787c8eb6 2019-07-11 stsp SHA1Update(&sha1_ctx, msg, len);
444 787c8eb6 2019-07-11 stsp n = fwrite(msg, 1, len, commitfile);
445 de18fc63 2019-05-09 stsp if (n != len) {
446 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
447 de18fc63 2019-05-09 stsp goto done;
448 de18fc63 2019-05-09 stsp }
449 de18fc63 2019-05-09 stsp
450 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, "\n", 1);
451 de18fc63 2019-05-09 stsp n = fwrite("\n", 1, 1, commitfile);
452 de18fc63 2019-05-09 stsp if (n != 1) {
453 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
454 de18fc63 2019-05-09 stsp goto done;
455 de18fc63 2019-05-09 stsp }
456 de18fc63 2019-05-09 stsp
457 de18fc63 2019-05-09 stsp *id = malloc(sizeof(**id));
458 de18fc63 2019-05-09 stsp if (*id == NULL) {
459 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
460 de18fc63 2019-05-09 stsp goto done;
461 de18fc63 2019-05-09 stsp }
462 de18fc63 2019-05-09 stsp SHA1Final((*id)->sha1, &sha1_ctx);
463 de18fc63 2019-05-09 stsp
464 de18fc63 2019-05-09 stsp if (fflush(commitfile) != 0) {
465 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
466 de18fc63 2019-05-09 stsp goto done;
467 de18fc63 2019-05-09 stsp }
468 de18fc63 2019-05-09 stsp rewind(commitfile);
469 de18fc63 2019-05-09 stsp
470 de18fc63 2019-05-09 stsp err = create_object_file(*id, commitfile, repo);
471 de18fc63 2019-05-09 stsp done:
472 787c8eb6 2019-07-11 stsp free(msg0);
473 de18fc63 2019-05-09 stsp free(header);
474 de18fc63 2019-05-09 stsp free(tree_str);
475 de18fc63 2019-05-09 stsp free(author_str);
476 de18fc63 2019-05-09 stsp free(committer_str);
477 de18fc63 2019-05-09 stsp if (commitfile && fclose(commitfile) != 0 && err == NULL)
478 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
479 f91abf81 2019-04-14 stsp if (err) {
480 f91abf81 2019-04-14 stsp free(*id);
481 f91abf81 2019-04-14 stsp *id = NULL;
482 f91abf81 2019-04-14 stsp }
483 f91abf81 2019-04-14 stsp return err;
484 f91abf81 2019-04-14 stsp }