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