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 7aadece8 2020-05-17 stsp te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
206 f91abf81 2019-04-14 stsp {
207 f91abf81 2019-04-14 stsp int ret;
208 f7b97ccb 2020-04-14 stsp mode_t mode;
209 f7b97ccb 2020-04-14 stsp
210 f7b97ccb 2020-04-14 stsp /*
211 f7b97ccb 2020-04-14 stsp * Some Git implementations are picky about modes seen in tree entries.
212 f7b97ccb 2020-04-14 stsp * For best compatibility we normalize the file/directory mode here.
213 7aadece8 2020-05-17 stsp * Note that we do not support committing symlinks.
214 f7b97ccb 2020-04-14 stsp */
215 7aadece8 2020-05-17 stsp if (S_ISREG(te->mode)) {
216 f7b97ccb 2020-04-14 stsp mode = GOT_DEFAULT_FILE_MODE;
217 7aadece8 2020-05-17 stsp if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
218 aaffadfc 2020-05-05 stsp mode |= S_IXUSR | S_IXGRP | S_IXOTH;
219 7aadece8 2020-05-17 stsp } else if (got_object_tree_entry_is_submodule(te))
220 7aadece8 2020-05-17 stsp mode = S_IFDIR | S_IFLNK;
221 7aadece8 2020-05-17 stsp else if (S_ISDIR(te->mode))
222 aaffadfc 2020-05-05 stsp mode = S_IFDIR; /* Git leaves all the other bits unset. */
223 f7b97ccb 2020-04-14 stsp else
224 f7b97ccb 2020-04-14 stsp return got_error(GOT_ERR_BAD_FILETYPE);
225 f7b97ccb 2020-04-14 stsp
226 f91abf81 2019-04-14 stsp ret = snprintf(buf, len, "%o ", mode);
227 f91abf81 2019-04-14 stsp if (ret == -1 || ret >= len)
228 f91abf81 2019-04-14 stsp return got_error(GOT_ERR_NO_SPACE);
229 f91abf81 2019-04-14 stsp return NULL;
230 6af1ccbd 2019-08-16 stsp }
231 6af1ccbd 2019-08-16 stsp
232 6af1ccbd 2019-08-16 stsp /*
233 6af1ccbd 2019-08-16 stsp * Git expects directory tree entries to be sorted with an imaginary slash
234 6af1ccbd 2019-08-16 stsp * appended to their name, and will break otherwise. Let's be nice.
235 56e0773d 2019-11-28 stsp * This function is intended to be used with mergesort(3) to sort an
236 56e0773d 2019-11-28 stsp * array of pointers to struct got_tree_entry objects.
237 6af1ccbd 2019-08-16 stsp */
238 56e0773d 2019-11-28 stsp static int
239 56e0773d 2019-11-28 stsp sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
240 6af1ccbd 2019-08-16 stsp {
241 56e0773d 2019-11-28 stsp struct got_tree_entry * const *te1 = arg1;
242 56e0773d 2019-11-28 stsp struct got_tree_entry * const *te2 = arg2;
243 2c98ee28 2019-11-29 stsp char name1[NAME_MAX + 2];
244 2c98ee28 2019-11-29 stsp char name2[NAME_MAX + 2];
245 6af1ccbd 2019-08-16 stsp
246 56e0773d 2019-11-28 stsp strlcpy(name1, (*te1)->name, sizeof(name1));
247 56e0773d 2019-11-28 stsp strlcpy(name2, (*te2)->name, sizeof(name2));
248 56e0773d 2019-11-28 stsp if (S_ISDIR((*te1)->mode))
249 56e0773d 2019-11-28 stsp strlcat(name1, "/", sizeof(name1));
250 56e0773d 2019-11-28 stsp if (S_ISDIR((*te2)->mode))
251 56e0773d 2019-11-28 stsp strlcat(name2, "/", sizeof(name2));
252 56e0773d 2019-11-28 stsp return strcmp(name1, name2);
253 f91abf81 2019-04-14 stsp }
254 f91abf81 2019-04-14 stsp
255 f91abf81 2019-04-14 stsp const struct got_error *
256 f91abf81 2019-04-14 stsp got_object_tree_create(struct got_object_id **id,
257 56e0773d 2019-11-28 stsp struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
258 f91abf81 2019-04-14 stsp {
259 f91abf81 2019-04-14 stsp const struct got_error *err = NULL;
260 f91abf81 2019-04-14 stsp char modebuf[sizeof("100644 ")];
261 f91abf81 2019-04-14 stsp SHA1_CTX sha1_ctx;
262 f91abf81 2019-04-14 stsp char *header = NULL;
263 f91abf81 2019-04-14 stsp size_t headerlen, len = 0, n;
264 f91abf81 2019-04-14 stsp FILE *treefile = NULL;
265 56e0773d 2019-11-28 stsp struct got_pathlist_entry *pe;
266 56e0773d 2019-11-28 stsp struct got_tree_entry **sorted_entries;
267 f91abf81 2019-04-14 stsp struct got_tree_entry *te;
268 56e0773d 2019-11-28 stsp int i;
269 f91abf81 2019-04-14 stsp
270 f91abf81 2019-04-14 stsp *id = NULL;
271 51c32763 2019-05-09 stsp
272 51c32763 2019-05-09 stsp SHA1Init(&sha1_ctx);
273 f91abf81 2019-04-14 stsp
274 56e0773d 2019-11-28 stsp sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
275 56e0773d 2019-11-28 stsp if (sorted_entries == NULL)
276 56e0773d 2019-11-28 stsp return got_error_from_errno("calloc");
277 6af1ccbd 2019-08-16 stsp
278 56e0773d 2019-11-28 stsp i = 0;
279 56e0773d 2019-11-28 stsp TAILQ_FOREACH(pe, paths, entry)
280 56e0773d 2019-11-28 stsp sorted_entries[i++] = pe->data;
281 56e0773d 2019-11-28 stsp mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
282 56e0773d 2019-11-28 stsp sort_tree_entries_the_way_git_likes_it);
283 56e0773d 2019-11-28 stsp
284 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
285 56e0773d 2019-11-28 stsp te = sorted_entries[i];
286 7aadece8 2020-05-17 stsp err = te_mode2str(modebuf, sizeof(modebuf), te);
287 f91abf81 2019-04-14 stsp if (err)
288 6af1ccbd 2019-08-16 stsp goto done;
289 f91abf81 2019-04-14 stsp len += strlen(modebuf) + strlen(te->name) + 1 +
290 f91abf81 2019-04-14 stsp SHA1_DIGEST_LENGTH;
291 f91abf81 2019-04-14 stsp }
292 f91abf81 2019-04-14 stsp
293 f91abf81 2019-04-14 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
294 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
295 f91abf81 2019-04-14 stsp goto done;
296 f91abf81 2019-04-14 stsp }
297 f91abf81 2019-04-14 stsp headerlen = strlen(header) + 1;
298 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, header, headerlen);
299 f91abf81 2019-04-14 stsp
300 f91abf81 2019-04-14 stsp treefile = got_opentemp();
301 f91abf81 2019-04-14 stsp if (treefile == NULL) {
302 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
303 f91abf81 2019-04-14 stsp goto done;
304 f91abf81 2019-04-14 stsp }
305 f91abf81 2019-04-14 stsp
306 f91abf81 2019-04-14 stsp n = fwrite(header, 1, headerlen, treefile);
307 f91abf81 2019-04-14 stsp if (n != headerlen) {
308 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
309 f91abf81 2019-04-14 stsp goto done;
310 f91abf81 2019-04-14 stsp }
311 f91abf81 2019-04-14 stsp
312 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
313 56e0773d 2019-11-28 stsp te = sorted_entries[i];
314 7aadece8 2020-05-17 stsp err = te_mode2str(modebuf, sizeof(modebuf), te);
315 f91abf81 2019-04-14 stsp if (err)
316 f91abf81 2019-04-14 stsp goto done;
317 f91abf81 2019-04-14 stsp len = strlen(modebuf);
318 f91abf81 2019-04-14 stsp n = fwrite(modebuf, 1, len, treefile);
319 f91abf81 2019-04-14 stsp if (n != len) {
320 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
321 f91abf81 2019-04-14 stsp goto done;
322 f91abf81 2019-04-14 stsp }
323 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, modebuf, len);
324 f91abf81 2019-04-14 stsp
325 f91abf81 2019-04-14 stsp len = strlen(te->name) + 1; /* must include NUL */
326 f91abf81 2019-04-14 stsp n = fwrite(te->name, 1, len, treefile);
327 f91abf81 2019-04-14 stsp if (n != len) {
328 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
329 f91abf81 2019-04-14 stsp goto done;
330 f91abf81 2019-04-14 stsp }
331 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, te->name, len);
332 f91abf81 2019-04-14 stsp
333 f91abf81 2019-04-14 stsp len = SHA1_DIGEST_LENGTH;
334 56e0773d 2019-11-28 stsp n = fwrite(te->id.sha1, 1, len, treefile);
335 f91abf81 2019-04-14 stsp if (n != len) {
336 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
337 f91abf81 2019-04-14 stsp goto done;
338 f91abf81 2019-04-14 stsp }
339 56e0773d 2019-11-28 stsp SHA1Update(&sha1_ctx, te->id.sha1, len);
340 f91abf81 2019-04-14 stsp }
341 f91abf81 2019-04-14 stsp
342 f91abf81 2019-04-14 stsp *id = malloc(sizeof(**id));
343 f91abf81 2019-04-14 stsp if (*id == NULL) {
344 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
345 f91abf81 2019-04-14 stsp goto done;
346 f91abf81 2019-04-14 stsp }
347 4be2a0b4 2019-04-14 stsp SHA1Final((*id)->sha1, &sha1_ctx);
348 f91abf81 2019-04-14 stsp
349 f91abf81 2019-04-14 stsp if (fflush(treefile) != 0) {
350 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
351 f91abf81 2019-04-14 stsp goto done;
352 f91abf81 2019-04-14 stsp }
353 f91abf81 2019-04-14 stsp rewind(treefile);
354 f91abf81 2019-04-14 stsp
355 76f564d5 2019-04-14 stsp err = create_object_file(*id, treefile, repo);
356 f91abf81 2019-04-14 stsp done:
357 f91abf81 2019-04-14 stsp free(header);
358 56e0773d 2019-11-28 stsp free(sorted_entries);
359 f91abf81 2019-04-14 stsp if (treefile && fclose(treefile) != 0 && err == NULL)
360 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
361 de18fc63 2019-05-09 stsp if (err) {
362 de18fc63 2019-05-09 stsp free(*id);
363 de18fc63 2019-05-09 stsp *id = NULL;
364 de18fc63 2019-05-09 stsp }
365 de18fc63 2019-05-09 stsp return err;
366 de18fc63 2019-05-09 stsp }
367 de18fc63 2019-05-09 stsp
368 de18fc63 2019-05-09 stsp const struct got_error *
369 de18fc63 2019-05-09 stsp got_object_commit_create(struct got_object_id **id,
370 de18fc63 2019-05-09 stsp struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
371 de18fc63 2019-05-09 stsp int nparents, const char *author, time_t author_time,
372 de18fc63 2019-05-09 stsp const char *committer, time_t committer_time,
373 de18fc63 2019-05-09 stsp const char *logmsg, struct got_repository *repo)
374 de18fc63 2019-05-09 stsp {
375 de18fc63 2019-05-09 stsp const struct got_error *err = NULL;
376 de18fc63 2019-05-09 stsp SHA1_CTX sha1_ctx;
377 de18fc63 2019-05-09 stsp char *header = NULL, *tree_str = NULL;
378 de18fc63 2019-05-09 stsp char *author_str = NULL, *committer_str = NULL;
379 de18fc63 2019-05-09 stsp char *id_str = NULL;
380 de18fc63 2019-05-09 stsp size_t headerlen, len = 0, n;
381 de18fc63 2019-05-09 stsp FILE *commitfile = NULL;
382 de18fc63 2019-05-09 stsp struct got_object_qid *qid;
383 787c8eb6 2019-07-11 stsp char *msg0, *msg;
384 de18fc63 2019-05-09 stsp
385 de18fc63 2019-05-09 stsp *id = NULL;
386 de18fc63 2019-05-09 stsp
387 de18fc63 2019-05-09 stsp SHA1Init(&sha1_ctx);
388 787c8eb6 2019-07-11 stsp
389 787c8eb6 2019-07-11 stsp msg0 = strdup(logmsg);
390 787c8eb6 2019-07-11 stsp if (msg0 == NULL)
391 787c8eb6 2019-07-11 stsp return got_error_from_errno("strdup");
392 787c8eb6 2019-07-11 stsp msg = msg0;
393 787c8eb6 2019-07-11 stsp
394 10796104 2019-07-11 stsp while (isspace((unsigned char)msg[0]))
395 787c8eb6 2019-07-11 stsp msg++;
396 787c8eb6 2019-07-11 stsp len = strlen(msg);
397 10796104 2019-07-11 stsp while (len > 0 && isspace((unsigned char)msg[len - 1])) {
398 787c8eb6 2019-07-11 stsp msg[len - 1] = '\0';
399 787c8eb6 2019-07-11 stsp len--;
400 787c8eb6 2019-07-11 stsp }
401 de18fc63 2019-05-09 stsp
402 de18fc63 2019-05-09 stsp if (asprintf(&author_str, "%s%s %lld +0000\n",
403 de18fc63 2019-05-09 stsp GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
404 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
405 de18fc63 2019-05-09 stsp
406 de18fc63 2019-05-09 stsp if (asprintf(&committer_str, "%s%s %lld +0000\n",
407 de18fc63 2019-05-09 stsp GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
408 de18fc63 2019-05-09 stsp committer ? committer_time : author_time)
409 de18fc63 2019-05-09 stsp == -1) {
410 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
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 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
415 de18fc63 2019-05-09 stsp nparents *
416 de18fc63 2019-05-09 stsp (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
417 787c8eb6 2019-07-11 stsp + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
418 de18fc63 2019-05-09 stsp
419 de18fc63 2019-05-09 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
420 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
421 de18fc63 2019-05-09 stsp goto done;
422 de18fc63 2019-05-09 stsp }
423 de18fc63 2019-05-09 stsp headerlen = strlen(header) + 1;
424 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, header, headerlen);
425 de18fc63 2019-05-09 stsp
426 de18fc63 2019-05-09 stsp commitfile = got_opentemp();
427 de18fc63 2019-05-09 stsp if (commitfile == NULL) {
428 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
429 de18fc63 2019-05-09 stsp goto done;
430 de18fc63 2019-05-09 stsp }
431 de18fc63 2019-05-09 stsp
432 de18fc63 2019-05-09 stsp n = fwrite(header, 1, headerlen, commitfile);
433 de18fc63 2019-05-09 stsp if (n != headerlen) {
434 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
435 de18fc63 2019-05-09 stsp goto done;
436 de18fc63 2019-05-09 stsp }
437 de18fc63 2019-05-09 stsp
438 de18fc63 2019-05-09 stsp err = got_object_id_str(&id_str, tree_id);
439 de18fc63 2019-05-09 stsp if (err)
440 de18fc63 2019-05-09 stsp goto done;
441 de18fc63 2019-05-09 stsp if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
442 de18fc63 2019-05-09 stsp == -1) {
443 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
444 de18fc63 2019-05-09 stsp goto done;
445 de18fc63 2019-05-09 stsp }
446 de18fc63 2019-05-09 stsp len = strlen(tree_str);
447 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, tree_str, len);
448 de18fc63 2019-05-09 stsp n = fwrite(tree_str, 1, len, commitfile);
449 de18fc63 2019-05-09 stsp if (n != len) {
450 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
451 de18fc63 2019-05-09 stsp goto done;
452 de18fc63 2019-05-09 stsp }
453 de18fc63 2019-05-09 stsp
454 3ce1b845 2019-07-15 stsp if (parent_ids) {
455 3ce1b845 2019-07-15 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
456 3ce1b845 2019-07-15 stsp char *parent_str = NULL;
457 de18fc63 2019-05-09 stsp
458 3ce1b845 2019-07-15 stsp free(id_str);
459 de18fc63 2019-05-09 stsp
460 3ce1b845 2019-07-15 stsp err = got_object_id_str(&id_str, qid->id);
461 3ce1b845 2019-07-15 stsp if (err)
462 3ce1b845 2019-07-15 stsp goto done;
463 3ce1b845 2019-07-15 stsp if (asprintf(&parent_str, "%s%s\n",
464 3ce1b845 2019-07-15 stsp GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
465 3ce1b845 2019-07-15 stsp err = got_error_from_errno("asprintf");
466 3ce1b845 2019-07-15 stsp goto done;
467 3ce1b845 2019-07-15 stsp }
468 3ce1b845 2019-07-15 stsp len = strlen(parent_str);
469 3ce1b845 2019-07-15 stsp SHA1Update(&sha1_ctx, parent_str, len);
470 3ce1b845 2019-07-15 stsp n = fwrite(parent_str, 1, len, commitfile);
471 3ce1b845 2019-07-15 stsp if (n != len) {
472 3ce1b845 2019-07-15 stsp err = got_ferror(commitfile, GOT_ERR_IO);
473 3ce1b845 2019-07-15 stsp free(parent_str);
474 3ce1b845 2019-07-15 stsp goto done;
475 3ce1b845 2019-07-15 stsp }
476 de18fc63 2019-05-09 stsp free(parent_str);
477 de18fc63 2019-05-09 stsp }
478 de18fc63 2019-05-09 stsp }
479 de18fc63 2019-05-09 stsp
480 de18fc63 2019-05-09 stsp len = strlen(author_str);
481 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, author_str, len);
482 de18fc63 2019-05-09 stsp n = fwrite(author_str, 1, len, commitfile);
483 de18fc63 2019-05-09 stsp if (n != len) {
484 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
485 de18fc63 2019-05-09 stsp goto done;
486 de18fc63 2019-05-09 stsp }
487 de18fc63 2019-05-09 stsp
488 de18fc63 2019-05-09 stsp len = strlen(committer_str);
489 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, committer_str, len);
490 de18fc63 2019-05-09 stsp n = fwrite(committer_str, 1, len, commitfile);
491 de18fc63 2019-05-09 stsp if (n != len) {
492 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
493 de18fc63 2019-05-09 stsp goto done;
494 de18fc63 2019-05-09 stsp }
495 de18fc63 2019-05-09 stsp
496 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, "\n", 1);
497 de18fc63 2019-05-09 stsp n = fwrite("\n", 1, 1, commitfile);
498 de18fc63 2019-05-09 stsp if (n != 1) {
499 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
500 de18fc63 2019-05-09 stsp goto done;
501 de18fc63 2019-05-09 stsp }
502 de18fc63 2019-05-09 stsp
503 787c8eb6 2019-07-11 stsp len = strlen(msg);
504 787c8eb6 2019-07-11 stsp SHA1Update(&sha1_ctx, msg, len);
505 787c8eb6 2019-07-11 stsp n = fwrite(msg, 1, len, commitfile);
506 de18fc63 2019-05-09 stsp if (n != len) {
507 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
508 de18fc63 2019-05-09 stsp goto done;
509 de18fc63 2019-05-09 stsp }
510 de18fc63 2019-05-09 stsp
511 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, "\n", 1);
512 de18fc63 2019-05-09 stsp n = fwrite("\n", 1, 1, commitfile);
513 de18fc63 2019-05-09 stsp if (n != 1) {
514 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
515 de18fc63 2019-05-09 stsp goto done;
516 de18fc63 2019-05-09 stsp }
517 de18fc63 2019-05-09 stsp
518 de18fc63 2019-05-09 stsp *id = malloc(sizeof(**id));
519 de18fc63 2019-05-09 stsp if (*id == NULL) {
520 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
521 de18fc63 2019-05-09 stsp goto done;
522 de18fc63 2019-05-09 stsp }
523 de18fc63 2019-05-09 stsp SHA1Final((*id)->sha1, &sha1_ctx);
524 de18fc63 2019-05-09 stsp
525 de18fc63 2019-05-09 stsp if (fflush(commitfile) != 0) {
526 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
527 de18fc63 2019-05-09 stsp goto done;
528 de18fc63 2019-05-09 stsp }
529 de18fc63 2019-05-09 stsp rewind(commitfile);
530 de18fc63 2019-05-09 stsp
531 de18fc63 2019-05-09 stsp err = create_object_file(*id, commitfile, repo);
532 de18fc63 2019-05-09 stsp done:
533 787c8eb6 2019-07-11 stsp free(msg0);
534 de18fc63 2019-05-09 stsp free(header);
535 de18fc63 2019-05-09 stsp free(tree_str);
536 de18fc63 2019-05-09 stsp free(author_str);
537 de18fc63 2019-05-09 stsp free(committer_str);
538 de18fc63 2019-05-09 stsp if (commitfile && fclose(commitfile) != 0 && err == NULL)
539 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("fclose");
540 8e7bd50a 2019-08-22 stsp if (err) {
541 8e7bd50a 2019-08-22 stsp free(*id);
542 8e7bd50a 2019-08-22 stsp *id = NULL;
543 8e7bd50a 2019-08-22 stsp }
544 8e7bd50a 2019-08-22 stsp return err;
545 8e7bd50a 2019-08-22 stsp }
546 8e7bd50a 2019-08-22 stsp
547 8e7bd50a 2019-08-22 stsp const struct got_error *
548 8e7bd50a 2019-08-22 stsp got_object_tag_create(struct got_object_id **id,
549 8e7bd50a 2019-08-22 stsp const char *tag_name, struct got_object_id *object_id, const char *tagger,
550 8e7bd50a 2019-08-22 stsp time_t tagger_time, const char *tagmsg, struct got_repository *repo)
551 8e7bd50a 2019-08-22 stsp {
552 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
553 8e7bd50a 2019-08-22 stsp SHA1_CTX sha1_ctx;
554 8e7bd50a 2019-08-22 stsp char *header = NULL;
555 8e7bd50a 2019-08-22 stsp char *tag_str = NULL, *tagger_str = NULL;
556 8e7bd50a 2019-08-22 stsp char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
557 8e7bd50a 2019-08-22 stsp size_t headerlen, len = 0, n;
558 8e7bd50a 2019-08-22 stsp FILE *tagfile = NULL;
559 8e7bd50a 2019-08-22 stsp char *msg0 = NULL, *msg;
560 8e7bd50a 2019-08-22 stsp const char *obj_type_str;
561 8e7bd50a 2019-08-22 stsp int obj_type;
562 8e7bd50a 2019-08-22 stsp
563 8e7bd50a 2019-08-22 stsp *id = NULL;
564 8e7bd50a 2019-08-22 stsp
565 8e7bd50a 2019-08-22 stsp SHA1Init(&sha1_ctx);
566 8e7bd50a 2019-08-22 stsp
567 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&id_str, object_id);
568 8e7bd50a 2019-08-22 stsp if (err)
569 8e7bd50a 2019-08-22 stsp goto done;
570 8e7bd50a 2019-08-22 stsp if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
571 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
572 8e7bd50a 2019-08-22 stsp goto done;
573 8e7bd50a 2019-08-22 stsp }
574 8e7bd50a 2019-08-22 stsp
575 8e7bd50a 2019-08-22 stsp err = got_object_get_type(&obj_type, repo, object_id);
576 8e7bd50a 2019-08-22 stsp if (err)
577 8e7bd50a 2019-08-22 stsp goto done;
578 8e7bd50a 2019-08-22 stsp
579 8e7bd50a 2019-08-22 stsp switch (obj_type) {
580 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
581 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_BLOB;
582 8e7bd50a 2019-08-22 stsp break;
583 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
584 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_TREE;
585 8e7bd50a 2019-08-22 stsp break;
586 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
587 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_COMMIT;
588 8e7bd50a 2019-08-22 stsp break;
589 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
590 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_TAG;
591 8e7bd50a 2019-08-22 stsp break;
592 8e7bd50a 2019-08-22 stsp default:
593 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_OBJ_TYPE);
594 8e7bd50a 2019-08-22 stsp goto done;
595 8e7bd50a 2019-08-22 stsp }
596 8e7bd50a 2019-08-22 stsp
597 8e7bd50a 2019-08-22 stsp if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
598 8e7bd50a 2019-08-22 stsp obj_type_str) == -1) {
599 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
600 8e7bd50a 2019-08-22 stsp goto done;
601 8e7bd50a 2019-08-22 stsp }
602 8e7bd50a 2019-08-22 stsp
603 8e7bd50a 2019-08-22 stsp if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
604 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
605 8e7bd50a 2019-08-22 stsp goto done;
606 8e7bd50a 2019-08-22 stsp }
607 8e7bd50a 2019-08-22 stsp
608 8e7bd50a 2019-08-22 stsp if (asprintf(&tagger_str, "%s%s %lld +0000\n",
609 2575b0eb 2019-08-22 stsp GOT_TAG_LABEL_TAGGER, tagger, tagger_time) == -1)
610 8e7bd50a 2019-08-22 stsp return got_error_from_errno("asprintf");
611 8e7bd50a 2019-08-22 stsp
612 8e7bd50a 2019-08-22 stsp msg0 = strdup(tagmsg);
613 8e7bd50a 2019-08-22 stsp if (msg0 == NULL) {
614 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
615 8e7bd50a 2019-08-22 stsp goto done;
616 8e7bd50a 2019-08-22 stsp }
617 8e7bd50a 2019-08-22 stsp msg = msg0;
618 8e7bd50a 2019-08-22 stsp
619 8e7bd50a 2019-08-22 stsp while (isspace((unsigned char)msg[0]))
620 8e7bd50a 2019-08-22 stsp msg++;
621 8e7bd50a 2019-08-22 stsp
622 8e7bd50a 2019-08-22 stsp len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
623 8e7bd50a 2019-08-22 stsp strlen(tagger_str) + 1 + strlen(msg) + 1;
624 8e7bd50a 2019-08-22 stsp
625 8e7bd50a 2019-08-22 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
626 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
627 8e7bd50a 2019-08-22 stsp goto done;
628 8e7bd50a 2019-08-22 stsp }
629 8e7bd50a 2019-08-22 stsp
630 8e7bd50a 2019-08-22 stsp headerlen = strlen(header) + 1;
631 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, header, headerlen);
632 8e7bd50a 2019-08-22 stsp
633 8e7bd50a 2019-08-22 stsp tagfile = got_opentemp();
634 8e7bd50a 2019-08-22 stsp if (tagfile == NULL) {
635 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_opentemp");
636 8e7bd50a 2019-08-22 stsp goto done;
637 8e7bd50a 2019-08-22 stsp }
638 8e7bd50a 2019-08-22 stsp
639 8e7bd50a 2019-08-22 stsp n = fwrite(header, 1, headerlen, tagfile);
640 8e7bd50a 2019-08-22 stsp if (n != headerlen) {
641 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
642 8e7bd50a 2019-08-22 stsp goto done;
643 8e7bd50a 2019-08-22 stsp }
644 8e7bd50a 2019-08-22 stsp len = strlen(obj_str);
645 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, obj_str, len);
646 8e7bd50a 2019-08-22 stsp n = fwrite(obj_str, 1, len, tagfile);
647 8e7bd50a 2019-08-22 stsp if (n != len) {
648 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
649 8e7bd50a 2019-08-22 stsp goto done;
650 8e7bd50a 2019-08-22 stsp }
651 8e7bd50a 2019-08-22 stsp len = strlen(type_str);
652 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, type_str, len);
653 8e7bd50a 2019-08-22 stsp n = fwrite(type_str, 1, len, tagfile);
654 8e7bd50a 2019-08-22 stsp if (n != len) {
655 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
656 8e7bd50a 2019-08-22 stsp goto done;
657 8e7bd50a 2019-08-22 stsp }
658 8e7bd50a 2019-08-22 stsp
659 8e7bd50a 2019-08-22 stsp len = strlen(tag_str);
660 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, tag_str, len);
661 8e7bd50a 2019-08-22 stsp n = fwrite(tag_str, 1, len, tagfile);
662 8e7bd50a 2019-08-22 stsp if (n != len) {
663 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
664 8e7bd50a 2019-08-22 stsp goto done;
665 8e7bd50a 2019-08-22 stsp }
666 8e7bd50a 2019-08-22 stsp
667 8e7bd50a 2019-08-22 stsp len = strlen(tagger_str);
668 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, tagger_str, len);
669 8e7bd50a 2019-08-22 stsp n = fwrite(tagger_str, 1, len, tagfile);
670 8e7bd50a 2019-08-22 stsp if (n != len) {
671 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
672 8e7bd50a 2019-08-22 stsp goto done;
673 8e7bd50a 2019-08-22 stsp }
674 8e7bd50a 2019-08-22 stsp
675 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, "\n", 1);
676 8e7bd50a 2019-08-22 stsp n = fwrite("\n", 1, 1, tagfile);
677 8e7bd50a 2019-08-22 stsp if (n != 1) {
678 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
679 8e7bd50a 2019-08-22 stsp goto done;
680 8e7bd50a 2019-08-22 stsp }
681 8e7bd50a 2019-08-22 stsp
682 8e7bd50a 2019-08-22 stsp len = strlen(msg);
683 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, msg, len);
684 8e7bd50a 2019-08-22 stsp n = fwrite(msg, 1, len, tagfile);
685 8e7bd50a 2019-08-22 stsp if (n != len) {
686 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
687 8e7bd50a 2019-08-22 stsp goto done;
688 8e7bd50a 2019-08-22 stsp }
689 8e7bd50a 2019-08-22 stsp
690 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, "\n", 1);
691 8e7bd50a 2019-08-22 stsp n = fwrite("\n", 1, 1, tagfile);
692 8e7bd50a 2019-08-22 stsp if (n != 1) {
693 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
694 8e7bd50a 2019-08-22 stsp goto done;
695 8e7bd50a 2019-08-22 stsp }
696 8e7bd50a 2019-08-22 stsp
697 8e7bd50a 2019-08-22 stsp *id = malloc(sizeof(**id));
698 8e7bd50a 2019-08-22 stsp if (*id == NULL) {
699 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("malloc");
700 8e7bd50a 2019-08-22 stsp goto done;
701 8e7bd50a 2019-08-22 stsp }
702 8e7bd50a 2019-08-22 stsp SHA1Final((*id)->sha1, &sha1_ctx);
703 8e7bd50a 2019-08-22 stsp
704 8e7bd50a 2019-08-22 stsp if (fflush(tagfile) != 0) {
705 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("fflush");
706 8e7bd50a 2019-08-22 stsp goto done;
707 8e7bd50a 2019-08-22 stsp }
708 8e7bd50a 2019-08-22 stsp rewind(tagfile);
709 8e7bd50a 2019-08-22 stsp
710 8e7bd50a 2019-08-22 stsp err = create_object_file(*id, tagfile, repo);
711 8e7bd50a 2019-08-22 stsp done:
712 8e7bd50a 2019-08-22 stsp free(msg0);
713 8e7bd50a 2019-08-22 stsp free(header);
714 8e7bd50a 2019-08-22 stsp free(obj_str);
715 8e7bd50a 2019-08-22 stsp free(tagger_str);
716 8e7bd50a 2019-08-22 stsp if (tagfile && fclose(tagfile) != 0 && err == NULL)
717 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
718 f91abf81 2019-04-14 stsp if (err) {
719 f91abf81 2019-04-14 stsp free(*id);
720 f91abf81 2019-04-14 stsp *id = NULL;
721 f91abf81 2019-04-14 stsp }
722 f91abf81 2019-04-14 stsp return err;
723 f91abf81 2019-04-14 stsp }