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