2 44edeea7 2019-04-11 stsp * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
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>
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"
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"
46 44edeea7 2019-04-11 stsp #ifndef nitems
47 44edeea7 2019-04-11 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
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 72840534 2022-01-19 stsp off_t content_len, struct got_repository *repo)
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 72840534 2022-01-19 stsp off_t tmplen = 0;
60 ac1c5662 2019-04-13 stsp err = got_object_get_path(&objpath, id, repo);
64 c6f826b4 2019-04-13 stsp err = got_opentemp_named(&tmppath, &tmpfile, objpath);
66 ac1c5662 2019-04-13 stsp char *parent_path;
67 ac1c5662 2019-04-13 stsp if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
69 ac1c5662 2019-04-13 stsp err = got_path_dirname(&parent_path, objpath);
72 ac1c5662 2019-04-13 stsp err = got_path_mkdir(parent_path);
73 ac1c5662 2019-04-13 stsp free(parent_path);
76 c6f826b4 2019-04-13 stsp err = got_opentemp_named(&tmppath, &tmpfile, objpath);
81 3818e3c4 2020-11-01 naddy if (fchmod(fileno(tmpfile), GOT_DEFAULT_FILE_MODE) != 0) {
82 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", tmppath);
86 72840534 2022-01-19 stsp err = got_deflate_to_file(&tmplen, content, content_len, tmpfile, NULL);
90 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, objpath, -1);
94 c6f826b4 2019-04-13 stsp if (rename(tmppath, objpath) != 0) {
95 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, objpath);
98 c6f826b4 2019-04-13 stsp free(tmppath);
99 c6f826b4 2019-04-13 stsp tmppath = NULL;
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);
107 56b63ca4 2021-01-22 stsp if (tmpfile && fclose(tmpfile) == EOF && err == NULL)
108 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
110 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
111 ac1c5662 2019-04-13 stsp return err ? err : unlock_err;
114 44edeea7 2019-04-11 stsp const struct got_error *
115 0a22ca1a 2020-09-23 stsp got_object_blob_file_create(struct got_object_id **id, FILE **blobfile,
116 72840534 2022-01-19 stsp off_t *blobsize, const char *ondisk_path)
118 ac1c5662 2019-04-13 stsp const struct got_error *err = NULL;
119 ac1c5662 2019-04-13 stsp char *header = NULL;
120 44edeea7 2019-04-11 stsp int fd = -1;
121 44edeea7 2019-04-11 stsp struct stat sb;
122 44edeea7 2019-04-11 stsp SHA1_CTX sha1_ctx;
123 ac1c5662 2019-04-13 stsp size_t headerlen = 0, n;
125 44edeea7 2019-04-11 stsp *id = NULL;
126 0a22ca1a 2020-09-23 stsp *blobfile = NULL;
127 72840534 2022-01-19 stsp *blobsize = 0;
129 44edeea7 2019-04-11 stsp SHA1Init(&sha1_ctx);
131 8bd0cdad 2021-12-31 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
132 3d9a4ec4 2020-07-23 stsp if (fd == -1) {
133 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink())
134 3d9a4ec4 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
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);
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);
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");
150 ffb286fd 2019-04-11 stsp headerlen = strlen(header) + 1;
151 ffb286fd 2019-04-11 stsp SHA1Update(&sha1_ctx, header, headerlen);
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");
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);
164 72840534 2022-01-19 stsp *blobsize += headerlen;
166 2f63b34c 2020-07-23 stsp char buf[PATH_MAX * 8];
167 a14a8cf6 2019-04-11 stsp ssize_t inlen;
169 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
170 3d9a4ec4 2020-07-23 stsp inlen = readlink(ondisk_path, buf, sizeof(buf));
171 3d9a4ec4 2020-07-23 stsp if (inlen == -1) {
172 3d9a4ec4 2020-07-23 stsp err = got_error_from_errno("readlink");
176 3d9a4ec4 2020-07-23 stsp inlen = read(fd, buf, sizeof(buf));
177 3d9a4ec4 2020-07-23 stsp if (inlen == -1) {
178 3d9a4ec4 2020-07-23 stsp err = got_error_from_errno("read");
182 a14a8cf6 2019-04-11 stsp if (inlen == 0)
183 a14a8cf6 2019-04-11 stsp break; /* EOF */
184 44edeea7 2019-04-11 stsp SHA1Update(&sha1_ctx, buf, inlen);
185 0a22ca1a 2020-09-23 stsp n = fwrite(buf, 1, inlen, *blobfile);
186 ac1c5662 2019-04-13 stsp if (n != inlen) {
187 0a22ca1a 2020-09-23 stsp err = got_ferror(*blobfile, GOT_ERR_IO);
190 72840534 2022-01-19 stsp *blobsize += n;
191 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(sb.st_mode))
195 44edeea7 2019-04-11 stsp *id = malloc(sizeof(**id));
196 44edeea7 2019-04-11 stsp if (*id == NULL) {
197 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
200 4be2a0b4 2019-04-14 stsp SHA1Final((*id)->sha1, &sha1_ctx);
202 0a22ca1a 2020-09-23 stsp if (fflush(*blobfile) != 0) {
203 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
206 0a22ca1a 2020-09-23 stsp rewind(*blobfile);
208 44edeea7 2019-04-11 stsp free(header);
209 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
210 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
213 0a22ca1a 2020-09-23 stsp *id = NULL;
214 0a22ca1a 2020-09-23 stsp if (*blobfile) {
215 0a22ca1a 2020-09-23 stsp fclose(*blobfile);
216 0a22ca1a 2020-09-23 stsp *blobfile = NULL;
219 0a22ca1a 2020-09-23 stsp return err;
222 0a22ca1a 2020-09-23 stsp const struct got_error *
223 0a22ca1a 2020-09-23 stsp got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
224 0a22ca1a 2020-09-23 stsp struct got_repository *repo)
226 0a22ca1a 2020-09-23 stsp const struct got_error *err = NULL;
227 0a22ca1a 2020-09-23 stsp FILE *blobfile = NULL;
228 72840534 2022-01-19 stsp off_t blobsize;
230 72840534 2022-01-19 stsp err = got_object_blob_file_create(id, &blobfile, &blobsize,
231 72840534 2022-01-19 stsp ondisk_path);
233 0a22ca1a 2020-09-23 stsp return err;
235 72840534 2022-01-19 stsp err = create_object_file(*id, blobfile, blobsize, repo);
236 0a22ca1a 2020-09-23 stsp if (fclose(blobfile) == EOF && err == NULL)
237 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
240 44edeea7 2019-04-11 stsp *id = NULL;
242 ac1c5662 2019-04-13 stsp return err;
245 f91abf81 2019-04-14 stsp static const struct got_error *
246 7aadece8 2020-05-17 stsp te_mode2str(char *buf, size_t len, struct got_tree_entry *te)
249 f7b97ccb 2020-04-14 stsp mode_t mode;
252 f7b97ccb 2020-04-14 stsp * Some Git implementations are picky about modes seen in tree entries.
253 f7b97ccb 2020-04-14 stsp * For best compatibility we normalize the file/directory mode here.
255 7aadece8 2020-05-17 stsp if (S_ISREG(te->mode)) {
256 f7b97ccb 2020-04-14 stsp mode = GOT_DEFAULT_FILE_MODE;
257 7aadece8 2020-05-17 stsp if (te->mode & (S_IXUSR | S_IXGRP | S_IXOTH))
258 aaffadfc 2020-05-05 stsp mode |= S_IXUSR | S_IXGRP | S_IXOTH;
259 7aadece8 2020-05-17 stsp } else if (got_object_tree_entry_is_submodule(te))
260 7aadece8 2020-05-17 stsp mode = S_IFDIR | S_IFLNK;
261 3d9a4ec4 2020-07-23 stsp else if (S_ISLNK(te->mode))
262 3d9a4ec4 2020-07-23 stsp mode = S_IFLNK; /* Git leaves all the other bits unset. */
263 7aadece8 2020-05-17 stsp else if (S_ISDIR(te->mode))
264 aaffadfc 2020-05-05 stsp mode = S_IFDIR; /* Git leaves all the other bits unset. */
266 f7b97ccb 2020-04-14 stsp return got_error(GOT_ERR_BAD_FILETYPE);
268 f91abf81 2019-04-14 stsp ret = snprintf(buf, len, "%o ", mode);
269 f91abf81 2019-04-14 stsp if (ret == -1 || ret >= len)
270 f91abf81 2019-04-14 stsp return got_error(GOT_ERR_NO_SPACE);
271 f91abf81 2019-04-14 stsp return NULL;
275 6af1ccbd 2019-08-16 stsp * Git expects directory tree entries to be sorted with an imaginary slash
276 6af1ccbd 2019-08-16 stsp * appended to their name, and will break otherwise. Let's be nice.
277 56e0773d 2019-11-28 stsp * This function is intended to be used with mergesort(3) to sort an
278 56e0773d 2019-11-28 stsp * array of pointers to struct got_tree_entry objects.
281 56e0773d 2019-11-28 stsp sort_tree_entries_the_way_git_likes_it(const void *arg1, const void *arg2)
283 56e0773d 2019-11-28 stsp struct got_tree_entry * const *te1 = arg1;
284 56e0773d 2019-11-28 stsp struct got_tree_entry * const *te2 = arg2;
285 2c98ee28 2019-11-29 stsp char name1[NAME_MAX + 2];
286 2c98ee28 2019-11-29 stsp char name2[NAME_MAX + 2];
288 56e0773d 2019-11-28 stsp strlcpy(name1, (*te1)->name, sizeof(name1));
289 56e0773d 2019-11-28 stsp strlcpy(name2, (*te2)->name, sizeof(name2));
290 56e0773d 2019-11-28 stsp if (S_ISDIR((*te1)->mode))
291 56e0773d 2019-11-28 stsp strlcat(name1, "/", sizeof(name1));
292 56e0773d 2019-11-28 stsp if (S_ISDIR((*te2)->mode))
293 56e0773d 2019-11-28 stsp strlcat(name2, "/", sizeof(name2));
294 56e0773d 2019-11-28 stsp return strcmp(name1, name2);
297 f91abf81 2019-04-14 stsp const struct got_error *
298 f91abf81 2019-04-14 stsp got_object_tree_create(struct got_object_id **id,
299 56e0773d 2019-11-28 stsp struct got_pathlist_head *paths, int nentries, struct got_repository *repo)
301 f91abf81 2019-04-14 stsp const struct got_error *err = NULL;
302 f91abf81 2019-04-14 stsp char modebuf[sizeof("100644 ")];
303 f91abf81 2019-04-14 stsp SHA1_CTX sha1_ctx;
304 f91abf81 2019-04-14 stsp char *header = NULL;
305 f91abf81 2019-04-14 stsp size_t headerlen, len = 0, n;
306 f91abf81 2019-04-14 stsp FILE *treefile = NULL;
307 72840534 2022-01-19 stsp off_t treesize = 0;
308 56e0773d 2019-11-28 stsp struct got_pathlist_entry *pe;
309 56e0773d 2019-11-28 stsp struct got_tree_entry **sorted_entries;
310 f91abf81 2019-04-14 stsp struct got_tree_entry *te;
313 f91abf81 2019-04-14 stsp *id = NULL;
315 51c32763 2019-05-09 stsp SHA1Init(&sha1_ctx);
317 56e0773d 2019-11-28 stsp sorted_entries = calloc(nentries, sizeof(struct got_tree_entry *));
318 56e0773d 2019-11-28 stsp if (sorted_entries == NULL)
319 56e0773d 2019-11-28 stsp return got_error_from_errno("calloc");
322 56e0773d 2019-11-28 stsp TAILQ_FOREACH(pe, paths, entry)
323 56e0773d 2019-11-28 stsp sorted_entries[i++] = pe->data;
324 56e0773d 2019-11-28 stsp mergesort(sorted_entries, nentries, sizeof(struct got_tree_entry *),
325 56e0773d 2019-11-28 stsp sort_tree_entries_the_way_git_likes_it);
327 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
328 56e0773d 2019-11-28 stsp te = sorted_entries[i];
329 7aadece8 2020-05-17 stsp err = te_mode2str(modebuf, sizeof(modebuf), te);
332 f91abf81 2019-04-14 stsp len += strlen(modebuf) + strlen(te->name) + 1 +
333 f91abf81 2019-04-14 stsp SHA1_DIGEST_LENGTH;
336 f91abf81 2019-04-14 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
337 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
340 f91abf81 2019-04-14 stsp headerlen = strlen(header) + 1;
341 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, header, headerlen);
343 f91abf81 2019-04-14 stsp treefile = got_opentemp();
344 f91abf81 2019-04-14 stsp if (treefile == NULL) {
345 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
349 f91abf81 2019-04-14 stsp n = fwrite(header, 1, headerlen, treefile);
350 f91abf81 2019-04-14 stsp if (n != headerlen) {
351 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
354 72840534 2022-01-19 stsp treesize += headerlen;
356 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
357 56e0773d 2019-11-28 stsp te = sorted_entries[i];
358 7aadece8 2020-05-17 stsp err = te_mode2str(modebuf, sizeof(modebuf), te);
361 f91abf81 2019-04-14 stsp len = strlen(modebuf);
362 f91abf81 2019-04-14 stsp n = fwrite(modebuf, 1, len, treefile);
363 f91abf81 2019-04-14 stsp if (n != len) {
364 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
367 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, modebuf, len);
368 72840534 2022-01-19 stsp treesize += n;
370 f91abf81 2019-04-14 stsp len = strlen(te->name) + 1; /* must include NUL */
371 f91abf81 2019-04-14 stsp n = fwrite(te->name, 1, len, treefile);
372 f91abf81 2019-04-14 stsp if (n != len) {
373 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
376 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, te->name, len);
377 72840534 2022-01-19 stsp treesize += n;
379 f91abf81 2019-04-14 stsp len = SHA1_DIGEST_LENGTH;
380 56e0773d 2019-11-28 stsp n = fwrite(te->id.sha1, 1, len, treefile);
381 f91abf81 2019-04-14 stsp if (n != len) {
382 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
385 56e0773d 2019-11-28 stsp SHA1Update(&sha1_ctx, te->id.sha1, len);
386 72840534 2022-01-19 stsp treesize += n;
389 f91abf81 2019-04-14 stsp *id = malloc(sizeof(**id));
390 f91abf81 2019-04-14 stsp if (*id == NULL) {
391 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
394 4be2a0b4 2019-04-14 stsp SHA1Final((*id)->sha1, &sha1_ctx);
396 f91abf81 2019-04-14 stsp if (fflush(treefile) != 0) {
397 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
400 f91abf81 2019-04-14 stsp rewind(treefile);
402 72840534 2022-01-19 stsp err = create_object_file(*id, treefile, treesize, repo);
404 f91abf81 2019-04-14 stsp free(header);
405 56e0773d 2019-11-28 stsp free(sorted_entries);
406 56b63ca4 2021-01-22 stsp if (treefile && fclose(treefile) == EOF && err == NULL)
407 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
410 de18fc63 2019-05-09 stsp *id = NULL;
412 de18fc63 2019-05-09 stsp return err;
415 de18fc63 2019-05-09 stsp const struct got_error *
416 de18fc63 2019-05-09 stsp got_object_commit_create(struct got_object_id **id,
417 de18fc63 2019-05-09 stsp struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
418 de18fc63 2019-05-09 stsp int nparents, const char *author, time_t author_time,
419 de18fc63 2019-05-09 stsp const char *committer, time_t committer_time,
420 de18fc63 2019-05-09 stsp const char *logmsg, struct got_repository *repo)
422 de18fc63 2019-05-09 stsp const struct got_error *err = NULL;
423 de18fc63 2019-05-09 stsp SHA1_CTX sha1_ctx;
424 de18fc63 2019-05-09 stsp char *header = NULL, *tree_str = NULL;
425 de18fc63 2019-05-09 stsp char *author_str = NULL, *committer_str = NULL;
426 de18fc63 2019-05-09 stsp char *id_str = NULL;
427 de18fc63 2019-05-09 stsp size_t headerlen, len = 0, n;
428 de18fc63 2019-05-09 stsp FILE *commitfile = NULL;
429 72840534 2022-01-19 stsp off_t commitsize = 0;
430 de18fc63 2019-05-09 stsp struct got_object_qid *qid;
431 787c8eb6 2019-07-11 stsp char *msg0, *msg;
433 de18fc63 2019-05-09 stsp *id = NULL;
435 de18fc63 2019-05-09 stsp SHA1Init(&sha1_ctx);
437 787c8eb6 2019-07-11 stsp msg0 = strdup(logmsg);
438 787c8eb6 2019-07-11 stsp if (msg0 == NULL)
439 787c8eb6 2019-07-11 stsp return got_error_from_errno("strdup");
440 787c8eb6 2019-07-11 stsp msg = msg0;
442 10796104 2019-07-11 stsp while (isspace((unsigned char)msg[0]))
444 787c8eb6 2019-07-11 stsp len = strlen(msg);
445 10796104 2019-07-11 stsp while (len > 0 && isspace((unsigned char)msg[len - 1])) {
446 787c8eb6 2019-07-11 stsp msg[len - 1] = '\0';
450 de18fc63 2019-05-09 stsp if (asprintf(&author_str, "%s%s %lld +0000\n",
451 1367695b 2020-09-26 naddy GOT_COMMIT_LABEL_AUTHOR, author, (long long)author_time) == -1)
452 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
454 de18fc63 2019-05-09 stsp if (asprintf(&committer_str, "%s%s %lld +0000\n",
455 de18fc63 2019-05-09 stsp GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
456 1367695b 2020-09-26 naddy (long long)(committer ? committer_time : author_time))
458 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
462 de18fc63 2019-05-09 stsp len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
464 de18fc63 2019-05-09 stsp (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
465 787c8eb6 2019-07-11 stsp + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
467 de18fc63 2019-05-09 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
468 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
471 de18fc63 2019-05-09 stsp headerlen = strlen(header) + 1;
472 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, header, headerlen);
474 de18fc63 2019-05-09 stsp commitfile = got_opentemp();
475 de18fc63 2019-05-09 stsp if (commitfile == NULL) {
476 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
480 de18fc63 2019-05-09 stsp n = fwrite(header, 1, headerlen, commitfile);
481 de18fc63 2019-05-09 stsp if (n != headerlen) {
482 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
485 72840534 2022-01-19 stsp commitsize += headerlen;
487 de18fc63 2019-05-09 stsp err = got_object_id_str(&id_str, tree_id);
490 de18fc63 2019-05-09 stsp if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
492 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
495 de18fc63 2019-05-09 stsp len = strlen(tree_str);
496 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, tree_str, len);
497 de18fc63 2019-05-09 stsp n = fwrite(tree_str, 1, len, commitfile);
498 de18fc63 2019-05-09 stsp if (n != len) {
499 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
502 72840534 2022-01-19 stsp commitsize += n;
504 3ce1b845 2019-07-15 stsp if (parent_ids) {
505 f259c4c1 2021-09-24 stsp free(id_str);
506 f259c4c1 2021-09-24 stsp id_str = NULL;
507 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, parent_ids, entry) {
508 3ce1b845 2019-07-15 stsp char *parent_str = NULL;
510 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
513 3ce1b845 2019-07-15 stsp if (asprintf(&parent_str, "%s%s\n",
514 3ce1b845 2019-07-15 stsp GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
515 3ce1b845 2019-07-15 stsp err = got_error_from_errno("asprintf");
518 3ce1b845 2019-07-15 stsp len = strlen(parent_str);
519 3ce1b845 2019-07-15 stsp SHA1Update(&sha1_ctx, parent_str, len);
520 3ce1b845 2019-07-15 stsp n = fwrite(parent_str, 1, len, commitfile);
521 3ce1b845 2019-07-15 stsp if (n != len) {
522 3ce1b845 2019-07-15 stsp err = got_ferror(commitfile, GOT_ERR_IO);
523 3ce1b845 2019-07-15 stsp free(parent_str);
526 72840534 2022-01-19 stsp commitsize += n;
527 de18fc63 2019-05-09 stsp free(parent_str);
528 f259c4c1 2021-09-24 stsp free(id_str);
529 f259c4c1 2021-09-24 stsp id_str = NULL;
533 de18fc63 2019-05-09 stsp len = strlen(author_str);
534 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, author_str, len);
535 de18fc63 2019-05-09 stsp n = fwrite(author_str, 1, len, commitfile);
536 de18fc63 2019-05-09 stsp if (n != len) {
537 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
540 72840534 2022-01-19 stsp commitsize += n;
542 de18fc63 2019-05-09 stsp len = strlen(committer_str);
543 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, committer_str, len);
544 de18fc63 2019-05-09 stsp n = fwrite(committer_str, 1, len, commitfile);
545 de18fc63 2019-05-09 stsp if (n != len) {
546 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
549 72840534 2022-01-19 stsp commitsize += n;
551 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, "\n", 1);
552 de18fc63 2019-05-09 stsp n = fwrite("\n", 1, 1, commitfile);
553 de18fc63 2019-05-09 stsp if (n != 1) {
554 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
557 72840534 2022-01-19 stsp commitsize += n;
559 787c8eb6 2019-07-11 stsp len = strlen(msg);
560 787c8eb6 2019-07-11 stsp SHA1Update(&sha1_ctx, msg, len);
561 787c8eb6 2019-07-11 stsp n = fwrite(msg, 1, len, commitfile);
562 de18fc63 2019-05-09 stsp if (n != len) {
563 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
566 72840534 2022-01-19 stsp commitsize += n;
568 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, "\n", 1);
569 de18fc63 2019-05-09 stsp n = fwrite("\n", 1, 1, commitfile);
570 de18fc63 2019-05-09 stsp if (n != 1) {
571 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
574 72840534 2022-01-19 stsp commitsize += n;
576 de18fc63 2019-05-09 stsp *id = malloc(sizeof(**id));
577 de18fc63 2019-05-09 stsp if (*id == NULL) {
578 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
581 de18fc63 2019-05-09 stsp SHA1Final((*id)->sha1, &sha1_ctx);
583 de18fc63 2019-05-09 stsp if (fflush(commitfile) != 0) {
584 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
587 de18fc63 2019-05-09 stsp rewind(commitfile);
589 72840534 2022-01-19 stsp err = create_object_file(*id, commitfile, commitsize, repo);
591 f259c4c1 2021-09-24 stsp free(id_str);
592 787c8eb6 2019-07-11 stsp free(msg0);
593 de18fc63 2019-05-09 stsp free(header);
594 de18fc63 2019-05-09 stsp free(tree_str);
595 de18fc63 2019-05-09 stsp free(author_str);
596 de18fc63 2019-05-09 stsp free(committer_str);
597 56b63ca4 2021-01-22 stsp if (commitfile && fclose(commitfile) == EOF && err == NULL)
598 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("fclose");
601 8e7bd50a 2019-08-22 stsp *id = NULL;
603 8e7bd50a 2019-08-22 stsp return err;
606 8e7bd50a 2019-08-22 stsp const struct got_error *
607 8e7bd50a 2019-08-22 stsp got_object_tag_create(struct got_object_id **id,
608 8e7bd50a 2019-08-22 stsp const char *tag_name, struct got_object_id *object_id, const char *tagger,
609 8e7bd50a 2019-08-22 stsp time_t tagger_time, const char *tagmsg, struct got_repository *repo)
611 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
612 8e7bd50a 2019-08-22 stsp SHA1_CTX sha1_ctx;
613 8e7bd50a 2019-08-22 stsp char *header = NULL;
614 8e7bd50a 2019-08-22 stsp char *tag_str = NULL, *tagger_str = NULL;
615 8e7bd50a 2019-08-22 stsp char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
616 8e7bd50a 2019-08-22 stsp size_t headerlen, len = 0, n;
617 8e7bd50a 2019-08-22 stsp FILE *tagfile = NULL;
618 72840534 2022-01-19 stsp off_t tagsize = 0;
619 8e7bd50a 2019-08-22 stsp char *msg0 = NULL, *msg;
620 8e7bd50a 2019-08-22 stsp const char *obj_type_str;
621 8e7bd50a 2019-08-22 stsp int obj_type;
623 8e7bd50a 2019-08-22 stsp *id = NULL;
625 8e7bd50a 2019-08-22 stsp SHA1Init(&sha1_ctx);
627 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&id_str, object_id);
630 8e7bd50a 2019-08-22 stsp if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
631 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
635 8e7bd50a 2019-08-22 stsp err = got_object_get_type(&obj_type, repo, object_id);
639 8e7bd50a 2019-08-22 stsp switch (obj_type) {
640 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
641 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_BLOB;
643 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
644 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_TREE;
646 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
647 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_COMMIT;
649 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
650 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_TAG;
653 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_OBJ_TYPE);
657 8e7bd50a 2019-08-22 stsp if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
658 8e7bd50a 2019-08-22 stsp obj_type_str) == -1) {
659 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
663 8e7bd50a 2019-08-22 stsp if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
664 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
668 8e7bd50a 2019-08-22 stsp if (asprintf(&tagger_str, "%s%s %lld +0000\n",
669 1367695b 2020-09-26 naddy GOT_TAG_LABEL_TAGGER, tagger, (long long)tagger_time) == -1)
670 8e7bd50a 2019-08-22 stsp return got_error_from_errno("asprintf");
672 8e7bd50a 2019-08-22 stsp msg0 = strdup(tagmsg);
673 8e7bd50a 2019-08-22 stsp if (msg0 == NULL) {
674 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
677 8e7bd50a 2019-08-22 stsp msg = msg0;
679 8e7bd50a 2019-08-22 stsp while (isspace((unsigned char)msg[0]))
682 8e7bd50a 2019-08-22 stsp len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
683 8e7bd50a 2019-08-22 stsp strlen(tagger_str) + 1 + strlen(msg) + 1;
685 8e7bd50a 2019-08-22 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
686 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
690 8e7bd50a 2019-08-22 stsp headerlen = strlen(header) + 1;
691 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, header, headerlen);
693 8e7bd50a 2019-08-22 stsp tagfile = got_opentemp();
694 8e7bd50a 2019-08-22 stsp if (tagfile == NULL) {
695 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_opentemp");
699 8e7bd50a 2019-08-22 stsp n = fwrite(header, 1, headerlen, tagfile);
700 8e7bd50a 2019-08-22 stsp if (n != headerlen) {
701 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
704 72840534 2022-01-19 stsp tagsize += headerlen;
705 8e7bd50a 2019-08-22 stsp len = strlen(obj_str);
706 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, obj_str, len);
707 8e7bd50a 2019-08-22 stsp n = fwrite(obj_str, 1, len, tagfile);
708 8e7bd50a 2019-08-22 stsp if (n != len) {
709 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
712 72840534 2022-01-19 stsp tagsize += n;
713 8e7bd50a 2019-08-22 stsp len = strlen(type_str);
714 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, type_str, len);
715 8e7bd50a 2019-08-22 stsp n = fwrite(type_str, 1, len, tagfile);
716 8e7bd50a 2019-08-22 stsp if (n != len) {
717 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
720 72840534 2022-01-19 stsp tagsize += n;
722 8e7bd50a 2019-08-22 stsp len = strlen(tag_str);
723 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, tag_str, len);
724 8e7bd50a 2019-08-22 stsp n = fwrite(tag_str, 1, len, tagfile);
725 8e7bd50a 2019-08-22 stsp if (n != len) {
726 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
729 72840534 2022-01-19 stsp tagsize += n;
731 8e7bd50a 2019-08-22 stsp len = strlen(tagger_str);
732 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, tagger_str, len);
733 8e7bd50a 2019-08-22 stsp n = fwrite(tagger_str, 1, len, tagfile);
734 8e7bd50a 2019-08-22 stsp if (n != len) {
735 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
738 72840534 2022-01-19 stsp tagsize += n;
740 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, "\n", 1);
741 8e7bd50a 2019-08-22 stsp n = fwrite("\n", 1, 1, tagfile);
742 8e7bd50a 2019-08-22 stsp if (n != 1) {
743 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
746 72840534 2022-01-19 stsp tagsize += n;
748 8e7bd50a 2019-08-22 stsp len = strlen(msg);
749 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, msg, len);
750 8e7bd50a 2019-08-22 stsp n = fwrite(msg, 1, len, tagfile);
751 8e7bd50a 2019-08-22 stsp if (n != len) {
752 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
755 72840534 2022-01-19 stsp tagsize += n;
757 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, "\n", 1);
758 8e7bd50a 2019-08-22 stsp n = fwrite("\n", 1, 1, tagfile);
759 8e7bd50a 2019-08-22 stsp if (n != 1) {
760 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
763 72840534 2022-01-19 stsp tagsize += n;
765 8e7bd50a 2019-08-22 stsp *id = malloc(sizeof(**id));
766 8e7bd50a 2019-08-22 stsp if (*id == NULL) {
767 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("malloc");
770 8e7bd50a 2019-08-22 stsp SHA1Final((*id)->sha1, &sha1_ctx);
772 8e7bd50a 2019-08-22 stsp if (fflush(tagfile) != 0) {
773 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("fflush");
776 8e7bd50a 2019-08-22 stsp rewind(tagfile);
778 72840534 2022-01-19 stsp err = create_object_file(*id, tagfile, tagsize, repo);
780 8e7bd50a 2019-08-22 stsp free(msg0);
781 8e7bd50a 2019-08-22 stsp free(header);
782 8e7bd50a 2019-08-22 stsp free(obj_str);
783 8e7bd50a 2019-08-22 stsp free(tagger_str);
784 56b63ca4 2021-01-22 stsp if (tagfile && fclose(tagfile) == EOF && err == NULL)
785 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
788 f91abf81 2019-04-14 stsp *id = NULL;
790 f91abf81 2019-04-14 stsp return err;