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