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 6af1ccbd 2019-08-16 stsp if (err)
308 6af1ccbd 2019-08-16 stsp break;
309 6af1ccbd 2019-08-16 stsp }
310 6af1ccbd 2019-08-16 stsp
311 6af1ccbd 2019-08-16 stsp if (err) {
312 6af1ccbd 2019-08-16 stsp free(*sorted);
313 6af1ccbd 2019-08-16 stsp *sorted = NULL;
314 6af1ccbd 2019-08-16 stsp }
315 6af1ccbd 2019-08-16 stsp
316 6af1ccbd 2019-08-16 stsp return err;
317 f91abf81 2019-04-14 stsp }
318 f91abf81 2019-04-14 stsp
319 f91abf81 2019-04-14 stsp const struct got_error *
320 f91abf81 2019-04-14 stsp got_object_tree_create(struct got_object_id **id,
321 6af1ccbd 2019-08-16 stsp struct got_tree_entries *tree_entries, struct got_repository *repo)
322 f91abf81 2019-04-14 stsp {
323 f91abf81 2019-04-14 stsp const struct got_error *err = NULL;
324 f91abf81 2019-04-14 stsp char modebuf[sizeof("100644 ")];
325 f91abf81 2019-04-14 stsp SHA1_CTX sha1_ctx;
326 f91abf81 2019-04-14 stsp char *header = NULL;
327 f91abf81 2019-04-14 stsp size_t headerlen, len = 0, n;
328 f91abf81 2019-04-14 stsp FILE *treefile = NULL;
329 6af1ccbd 2019-08-16 stsp struct got_tree_entries *entries; /* sorted according to Git rules */
330 f91abf81 2019-04-14 stsp struct got_tree_entry *te;
331 f91abf81 2019-04-14 stsp
332 f91abf81 2019-04-14 stsp *id = NULL;
333 51c32763 2019-05-09 stsp
334 51c32763 2019-05-09 stsp SHA1Init(&sha1_ctx);
335 f91abf81 2019-04-14 stsp
336 6af1ccbd 2019-08-16 stsp err = sort_tree_entries_the_way_git_likes_it(&entries, tree_entries);
337 6af1ccbd 2019-08-16 stsp if (err)
338 6af1ccbd 2019-08-16 stsp return err;
339 6af1ccbd 2019-08-16 stsp
340 f91abf81 2019-04-14 stsp SIMPLEQ_FOREACH(te, &entries->head, entry) {
341 f91abf81 2019-04-14 stsp err = mode2str(modebuf, sizeof(modebuf), te->mode);
342 f91abf81 2019-04-14 stsp if (err)
343 6af1ccbd 2019-08-16 stsp goto done;
344 f91abf81 2019-04-14 stsp len += strlen(modebuf) + strlen(te->name) + 1 +
345 f91abf81 2019-04-14 stsp SHA1_DIGEST_LENGTH;
346 f91abf81 2019-04-14 stsp }
347 f91abf81 2019-04-14 stsp
348 f91abf81 2019-04-14 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
349 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
350 f91abf81 2019-04-14 stsp goto done;
351 f91abf81 2019-04-14 stsp }
352 f91abf81 2019-04-14 stsp headerlen = strlen(header) + 1;
353 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, header, headerlen);
354 f91abf81 2019-04-14 stsp
355 f91abf81 2019-04-14 stsp treefile = got_opentemp();
356 f91abf81 2019-04-14 stsp if (treefile == NULL) {
357 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
358 f91abf81 2019-04-14 stsp goto done;
359 f91abf81 2019-04-14 stsp }
360 f91abf81 2019-04-14 stsp
361 f91abf81 2019-04-14 stsp n = fwrite(header, 1, headerlen, treefile);
362 f91abf81 2019-04-14 stsp if (n != headerlen) {
363 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
364 f91abf81 2019-04-14 stsp goto done;
365 f91abf81 2019-04-14 stsp }
366 f91abf81 2019-04-14 stsp
367 f91abf81 2019-04-14 stsp SIMPLEQ_FOREACH(te, &entries->head, entry) {
368 f91abf81 2019-04-14 stsp err = mode2str(modebuf, sizeof(modebuf), te->mode);
369 f91abf81 2019-04-14 stsp if (err)
370 f91abf81 2019-04-14 stsp goto done;
371 f91abf81 2019-04-14 stsp len = strlen(modebuf);
372 f91abf81 2019-04-14 stsp n = fwrite(modebuf, 1, len, treefile);
373 f91abf81 2019-04-14 stsp if (n != len) {
374 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
375 f91abf81 2019-04-14 stsp goto done;
376 f91abf81 2019-04-14 stsp }
377 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, modebuf, len);
378 f91abf81 2019-04-14 stsp
379 f91abf81 2019-04-14 stsp len = strlen(te->name) + 1; /* must include NUL */
380 f91abf81 2019-04-14 stsp n = fwrite(te->name, 1, len, treefile);
381 f91abf81 2019-04-14 stsp if (n != len) {
382 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
383 f91abf81 2019-04-14 stsp goto done;
384 f91abf81 2019-04-14 stsp }
385 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, te->name, len);
386 f91abf81 2019-04-14 stsp
387 f91abf81 2019-04-14 stsp len = SHA1_DIGEST_LENGTH;
388 f91abf81 2019-04-14 stsp n = fwrite(te->id->sha1, 1, len, treefile);
389 f91abf81 2019-04-14 stsp if (n != len) {
390 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
391 f91abf81 2019-04-14 stsp goto done;
392 f91abf81 2019-04-14 stsp }
393 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, te->id->sha1, len);
394 f91abf81 2019-04-14 stsp }
395 f91abf81 2019-04-14 stsp
396 f91abf81 2019-04-14 stsp *id = malloc(sizeof(**id));
397 f91abf81 2019-04-14 stsp if (*id == NULL) {
398 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
399 f91abf81 2019-04-14 stsp goto done;
400 f91abf81 2019-04-14 stsp }
401 4be2a0b4 2019-04-14 stsp SHA1Final((*id)->sha1, &sha1_ctx);
402 f91abf81 2019-04-14 stsp
403 f91abf81 2019-04-14 stsp if (fflush(treefile) != 0) {
404 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
405 f91abf81 2019-04-14 stsp goto done;
406 f91abf81 2019-04-14 stsp }
407 f91abf81 2019-04-14 stsp rewind(treefile);
408 f91abf81 2019-04-14 stsp
409 76f564d5 2019-04-14 stsp err = create_object_file(*id, treefile, repo);
410 f91abf81 2019-04-14 stsp done:
411 f91abf81 2019-04-14 stsp free(header);
412 6af1ccbd 2019-08-16 stsp got_object_tree_entries_close(entries);
413 6af1ccbd 2019-08-16 stsp free(entries);
414 f91abf81 2019-04-14 stsp if (treefile && fclose(treefile) != 0 && err == NULL)
415 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
416 de18fc63 2019-05-09 stsp if (err) {
417 de18fc63 2019-05-09 stsp free(*id);
418 de18fc63 2019-05-09 stsp *id = NULL;
419 de18fc63 2019-05-09 stsp }
420 de18fc63 2019-05-09 stsp return err;
421 de18fc63 2019-05-09 stsp }
422 de18fc63 2019-05-09 stsp
423 de18fc63 2019-05-09 stsp const struct got_error *
424 de18fc63 2019-05-09 stsp got_object_commit_create(struct got_object_id **id,
425 de18fc63 2019-05-09 stsp struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
426 de18fc63 2019-05-09 stsp int nparents, const char *author, time_t author_time,
427 de18fc63 2019-05-09 stsp const char *committer, time_t committer_time,
428 de18fc63 2019-05-09 stsp const char *logmsg, struct got_repository *repo)
429 de18fc63 2019-05-09 stsp {
430 de18fc63 2019-05-09 stsp const struct got_error *err = NULL;
431 de18fc63 2019-05-09 stsp SHA1_CTX sha1_ctx;
432 de18fc63 2019-05-09 stsp char *header = NULL, *tree_str = NULL;
433 de18fc63 2019-05-09 stsp char *author_str = NULL, *committer_str = NULL;
434 de18fc63 2019-05-09 stsp char *id_str = NULL;
435 de18fc63 2019-05-09 stsp size_t headerlen, len = 0, n;
436 de18fc63 2019-05-09 stsp FILE *commitfile = NULL;
437 de18fc63 2019-05-09 stsp struct got_object_qid *qid;
438 787c8eb6 2019-07-11 stsp char *msg0, *msg;
439 de18fc63 2019-05-09 stsp
440 de18fc63 2019-05-09 stsp *id = NULL;
441 de18fc63 2019-05-09 stsp
442 de18fc63 2019-05-09 stsp SHA1Init(&sha1_ctx);
443 787c8eb6 2019-07-11 stsp
444 787c8eb6 2019-07-11 stsp msg0 = strdup(logmsg);
445 787c8eb6 2019-07-11 stsp if (msg0 == NULL)
446 787c8eb6 2019-07-11 stsp return got_error_from_errno("strdup");
447 787c8eb6 2019-07-11 stsp msg = msg0;
448 787c8eb6 2019-07-11 stsp
449 10796104 2019-07-11 stsp while (isspace((unsigned char)msg[0]))
450 787c8eb6 2019-07-11 stsp msg++;
451 787c8eb6 2019-07-11 stsp len = strlen(msg);
452 10796104 2019-07-11 stsp while (len > 0 && isspace((unsigned char)msg[len - 1])) {
453 787c8eb6 2019-07-11 stsp msg[len - 1] = '\0';
454 787c8eb6 2019-07-11 stsp len--;
455 787c8eb6 2019-07-11 stsp }
456 de18fc63 2019-05-09 stsp
457 de18fc63 2019-05-09 stsp if (asprintf(&author_str, "%s%s %lld +0000\n",
458 de18fc63 2019-05-09 stsp GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
459 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
460 de18fc63 2019-05-09 stsp
461 de18fc63 2019-05-09 stsp if (asprintf(&committer_str, "%s%s %lld +0000\n",
462 de18fc63 2019-05-09 stsp GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
463 de18fc63 2019-05-09 stsp committer ? committer_time : author_time)
464 de18fc63 2019-05-09 stsp == -1) {
465 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
466 de18fc63 2019-05-09 stsp goto done;
467 de18fc63 2019-05-09 stsp }
468 de18fc63 2019-05-09 stsp
469 de18fc63 2019-05-09 stsp len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
470 de18fc63 2019-05-09 stsp nparents *
471 de18fc63 2019-05-09 stsp (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
472 787c8eb6 2019-07-11 stsp + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
473 de18fc63 2019-05-09 stsp
474 de18fc63 2019-05-09 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
475 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
476 de18fc63 2019-05-09 stsp goto done;
477 de18fc63 2019-05-09 stsp }
478 de18fc63 2019-05-09 stsp headerlen = strlen(header) + 1;
479 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, header, headerlen);
480 de18fc63 2019-05-09 stsp
481 de18fc63 2019-05-09 stsp commitfile = got_opentemp();
482 de18fc63 2019-05-09 stsp if (commitfile == NULL) {
483 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
484 de18fc63 2019-05-09 stsp goto done;
485 de18fc63 2019-05-09 stsp }
486 de18fc63 2019-05-09 stsp
487 de18fc63 2019-05-09 stsp n = fwrite(header, 1, headerlen, commitfile);
488 de18fc63 2019-05-09 stsp if (n != headerlen) {
489 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
490 de18fc63 2019-05-09 stsp goto done;
491 de18fc63 2019-05-09 stsp }
492 de18fc63 2019-05-09 stsp
493 de18fc63 2019-05-09 stsp err = got_object_id_str(&id_str, tree_id);
494 de18fc63 2019-05-09 stsp if (err)
495 de18fc63 2019-05-09 stsp goto done;
496 de18fc63 2019-05-09 stsp if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
497 de18fc63 2019-05-09 stsp == -1) {
498 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
499 de18fc63 2019-05-09 stsp goto done;
500 de18fc63 2019-05-09 stsp }
501 de18fc63 2019-05-09 stsp len = strlen(tree_str);
502 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, tree_str, len);
503 de18fc63 2019-05-09 stsp n = fwrite(tree_str, 1, len, commitfile);
504 de18fc63 2019-05-09 stsp if (n != len) {
505 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
506 de18fc63 2019-05-09 stsp goto done;
507 de18fc63 2019-05-09 stsp }
508 de18fc63 2019-05-09 stsp
509 3ce1b845 2019-07-15 stsp if (parent_ids) {
510 3ce1b845 2019-07-15 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
511 3ce1b845 2019-07-15 stsp char *parent_str = NULL;
512 de18fc63 2019-05-09 stsp
513 3ce1b845 2019-07-15 stsp free(id_str);
514 de18fc63 2019-05-09 stsp
515 3ce1b845 2019-07-15 stsp err = got_object_id_str(&id_str, qid->id);
516 3ce1b845 2019-07-15 stsp if (err)
517 3ce1b845 2019-07-15 stsp goto done;
518 3ce1b845 2019-07-15 stsp if (asprintf(&parent_str, "%s%s\n",
519 3ce1b845 2019-07-15 stsp GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
520 3ce1b845 2019-07-15 stsp err = got_error_from_errno("asprintf");
521 3ce1b845 2019-07-15 stsp goto done;
522 3ce1b845 2019-07-15 stsp }
523 3ce1b845 2019-07-15 stsp len = strlen(parent_str);
524 3ce1b845 2019-07-15 stsp SHA1Update(&sha1_ctx, parent_str, len);
525 3ce1b845 2019-07-15 stsp n = fwrite(parent_str, 1, len, commitfile);
526 3ce1b845 2019-07-15 stsp if (n != len) {
527 3ce1b845 2019-07-15 stsp err = got_ferror(commitfile, GOT_ERR_IO);
528 3ce1b845 2019-07-15 stsp free(parent_str);
529 3ce1b845 2019-07-15 stsp goto done;
530 3ce1b845 2019-07-15 stsp }
531 de18fc63 2019-05-09 stsp free(parent_str);
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 de18fc63 2019-05-09 stsp
543 de18fc63 2019-05-09 stsp len = strlen(committer_str);
544 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, committer_str, len);
545 de18fc63 2019-05-09 stsp n = fwrite(committer_str, 1, len, commitfile);
546 de18fc63 2019-05-09 stsp if (n != len) {
547 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
548 de18fc63 2019-05-09 stsp goto done;
549 de18fc63 2019-05-09 stsp }
550 de18fc63 2019-05-09 stsp
551 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, "\n", 1);
552 de18fc63 2019-05-09 stsp n = fwrite("\n", 1, 1, commitfile);
553 de18fc63 2019-05-09 stsp if (n != 1) {
554 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
555 de18fc63 2019-05-09 stsp goto done;
556 de18fc63 2019-05-09 stsp }
557 de18fc63 2019-05-09 stsp
558 787c8eb6 2019-07-11 stsp len = strlen(msg);
559 787c8eb6 2019-07-11 stsp SHA1Update(&sha1_ctx, msg, len);
560 787c8eb6 2019-07-11 stsp n = fwrite(msg, 1, len, commitfile);
561 de18fc63 2019-05-09 stsp if (n != len) {
562 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
563 de18fc63 2019-05-09 stsp goto done;
564 de18fc63 2019-05-09 stsp }
565 de18fc63 2019-05-09 stsp
566 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, "\n", 1);
567 de18fc63 2019-05-09 stsp n = fwrite("\n", 1, 1, commitfile);
568 de18fc63 2019-05-09 stsp if (n != 1) {
569 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
570 de18fc63 2019-05-09 stsp goto done;
571 de18fc63 2019-05-09 stsp }
572 de18fc63 2019-05-09 stsp
573 de18fc63 2019-05-09 stsp *id = malloc(sizeof(**id));
574 de18fc63 2019-05-09 stsp if (*id == NULL) {
575 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
576 de18fc63 2019-05-09 stsp goto done;
577 de18fc63 2019-05-09 stsp }
578 de18fc63 2019-05-09 stsp SHA1Final((*id)->sha1, &sha1_ctx);
579 de18fc63 2019-05-09 stsp
580 de18fc63 2019-05-09 stsp if (fflush(commitfile) != 0) {
581 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
582 de18fc63 2019-05-09 stsp goto done;
583 de18fc63 2019-05-09 stsp }
584 de18fc63 2019-05-09 stsp rewind(commitfile);
585 de18fc63 2019-05-09 stsp
586 de18fc63 2019-05-09 stsp err = create_object_file(*id, commitfile, repo);
587 de18fc63 2019-05-09 stsp done:
588 787c8eb6 2019-07-11 stsp free(msg0);
589 de18fc63 2019-05-09 stsp free(header);
590 de18fc63 2019-05-09 stsp free(tree_str);
591 de18fc63 2019-05-09 stsp free(author_str);
592 de18fc63 2019-05-09 stsp free(committer_str);
593 de18fc63 2019-05-09 stsp if (commitfile && fclose(commitfile) != 0 && err == NULL)
594 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
595 f91abf81 2019-04-14 stsp if (err) {
596 f91abf81 2019-04-14 stsp free(*id);
597 f91abf81 2019-04-14 stsp *id = NULL;
598 f91abf81 2019-04-14 stsp }
599 f91abf81 2019-04-14 stsp return err;
600 f91abf81 2019-04-14 stsp }