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