Blob


1 /*
2 * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdint.h>
27 #include <sha1.h>
28 #include <unistd.h>
29 #include <zlib.h>
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_repository.h"
34 #include "got_opentemp.h"
35 #include "got_path.h"
37 #include "got_lib_sha1.h"
38 #include "got_lib_deflate.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_object.h"
41 #include "got_lib_lockfile.h"
43 #ifndef nitems
44 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
45 #endif
47 static const struct got_error *
48 create_object_file(struct got_object_id *id, FILE *content,
49 struct got_repository *repo)
50 {
51 const struct got_error *err = NULL, *unlock_err = NULL;
52 char *objpath = NULL, *tmppath = NULL;
53 FILE *tmpfile = NULL;
54 struct got_lockfile *lf = NULL;
55 size_t tmplen = 0;
57 err = got_object_get_path(&objpath, id, repo);
58 if (err)
59 return err;
61 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
62 if (err) {
63 char *parent_path;
64 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
65 goto done;
66 err = got_path_dirname(&parent_path, objpath);
67 if (err)
68 goto done;
69 err = got_path_mkdir(parent_path);
70 free(parent_path);
71 if (err)
72 goto done;
73 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
74 if (err)
75 goto done;
76 }
78 err = got_deflate_to_file(&tmplen, content, tmpfile);
79 if (err)
80 goto done;
82 err = got_lockfile_lock(&lf, objpath);
83 if (err)
84 goto done;
86 if (rename(tmppath, objpath) != 0) {
87 err = got_error_prefix_errno3("rename", tmppath, objpath);
88 goto done;
89 }
90 free(tmppath);
91 tmppath = NULL;
93 if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
94 err = got_error_prefix_errno2("chmod", objpath);
95 goto done;
96 }
97 done:
98 free(objpath);
99 if (tmppath) {
100 if (unlink(tmppath) != 0 && err == NULL)
101 err = got_error_prefix_errno2("unlink", tmppath);
102 free(tmppath);
104 if (tmpfile && fclose(tmpfile) != 0 && err == NULL)
105 err = got_error_prefix_errno("fclose");
106 if (lf)
107 unlock_err = got_lockfile_unlock(lf);
108 return err ? err : unlock_err;
111 const struct got_error *
112 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
113 struct got_repository *repo)
115 const struct got_error *err = NULL;
116 char *header = NULL;
117 FILE *blobfile = NULL;
118 int fd = -1;
119 struct stat sb;
120 SHA1_CTX sha1_ctx;
121 size_t headerlen = 0, n;
123 *id = NULL;
125 SHA1Init(&sha1_ctx);
127 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
128 if (fd == -1)
129 return got_error_prefix_errno2("open", ondisk_path);
131 if (fstat(fd, &sb) == -1) {
132 err = got_error_prefix_errno2("fstat", ondisk_path);
133 goto done;
136 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
137 sb.st_size) == -1) {
138 err = got_error_prefix_errno("asprintf");
139 goto done;
141 headerlen = strlen(header) + 1;
142 SHA1Update(&sha1_ctx, header, headerlen);
144 blobfile = got_opentemp();
145 if (blobfile == NULL) {
146 err = got_error_prefix_errno("got_opentemp");
147 goto done;
150 n = fwrite(header, 1, headerlen, blobfile);
151 if (n != headerlen) {
152 err = got_ferror(blobfile, GOT_ERR_IO);
153 goto done;
155 while (1) {
156 char buf[8192];
157 ssize_t inlen;
159 inlen = read(fd, buf, sizeof(buf));
160 if (inlen == -1) {
161 err = got_error_prefix_errno("read");
162 goto done;
164 if (inlen == 0)
165 break; /* EOF */
166 SHA1Update(&sha1_ctx, buf, inlen);
167 n = fwrite(buf, 1, inlen, blobfile);
168 if (n != inlen) {
169 err = got_ferror(blobfile, GOT_ERR_IO);
170 goto done;
174 *id = malloc(sizeof(**id));
175 if (*id == NULL) {
176 err = got_error_prefix_errno("malloc");
177 goto done;
179 SHA1Final((*id)->sha1, &sha1_ctx);
181 if (fflush(blobfile) != 0) {
182 err = got_error_prefix_errno("fflush");
183 goto done;
185 rewind(blobfile);
187 err = create_object_file(*id, blobfile, repo);
188 done:
189 free(header);
190 if (fd != -1 && close(fd) != 0 && err == NULL)
191 err = got_error_prefix_errno("close");
192 if (blobfile && fclose(blobfile) != 0 && err == NULL)
193 err = got_error_prefix_errno("fclose");
194 if (err) {
195 free(*id);
196 *id = NULL;
198 return err;
201 static const struct got_error *
202 mode2str(char *buf, size_t len, mode_t mode)
204 int ret;
205 ret = snprintf(buf, len, "%o ", mode);
206 if (ret == -1 || ret >= len)
207 return got_error(GOT_ERR_NO_SPACE);
208 return NULL;
211 const struct got_error *
212 got_object_tree_create(struct got_object_id **id,
213 struct got_tree_entries *entries, struct got_repository *repo)
215 const struct got_error *err = NULL;
216 char modebuf[sizeof("100644 ")];
217 SHA1_CTX sha1_ctx;
218 char *header = NULL;
219 size_t headerlen, len = 0, n;
220 FILE *treefile = NULL;
221 struct got_tree_entry *te;
223 *id = NULL;
225 SHA1Init(&sha1_ctx);
227 SIMPLEQ_FOREACH(te, &entries->head, entry) {
228 err = mode2str(modebuf, sizeof(modebuf), te->mode);
229 if (err)
230 return err;
231 len += strlen(modebuf) + strlen(te->name) + 1 +
232 SHA1_DIGEST_LENGTH;
235 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
236 err = got_error_prefix_errno("asprintf");
237 goto done;
239 headerlen = strlen(header) + 1;
240 SHA1Update(&sha1_ctx, header, headerlen);
242 treefile = got_opentemp();
243 if (treefile == NULL) {
244 err = got_error_prefix_errno("got_opentemp");
245 goto done;
248 n = fwrite(header, 1, headerlen, treefile);
249 if (n != headerlen) {
250 err = got_ferror(treefile, GOT_ERR_IO);
251 goto done;
254 SIMPLEQ_FOREACH(te, &entries->head, entry) {
255 err = mode2str(modebuf, sizeof(modebuf), te->mode);
256 if (err)
257 goto done;
258 len = strlen(modebuf);
259 n = fwrite(modebuf, 1, len, treefile);
260 if (n != len) {
261 err = got_ferror(treefile, GOT_ERR_IO);
262 goto done;
264 SHA1Update(&sha1_ctx, modebuf, len);
266 len = strlen(te->name) + 1; /* must include NUL */
267 n = fwrite(te->name, 1, len, treefile);
268 if (n != len) {
269 err = got_ferror(treefile, GOT_ERR_IO);
270 goto done;
272 SHA1Update(&sha1_ctx, te->name, len);
274 len = SHA1_DIGEST_LENGTH;
275 n = fwrite(te->id->sha1, 1, len, treefile);
276 if (n != len) {
277 err = got_ferror(treefile, GOT_ERR_IO);
278 goto done;
280 SHA1Update(&sha1_ctx, te->id->sha1, len);
283 *id = malloc(sizeof(**id));
284 if (*id == NULL) {
285 err = got_error_prefix_errno("malloc");
286 goto done;
288 SHA1Final((*id)->sha1, &sha1_ctx);
290 if (fflush(treefile) != 0) {
291 err = got_error_prefix_errno("fflush");
292 goto done;
294 rewind(treefile);
296 err = create_object_file(*id, treefile, repo);
297 done:
298 free(header);
299 if (treefile && fclose(treefile) != 0 && err == NULL)
300 err = got_error_prefix_errno("fclose");
301 if (err) {
302 free(*id);
303 *id = NULL;
305 return err;
308 const struct got_error *
309 got_object_commit_create(struct got_object_id **id,
310 struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
311 int nparents, const char *author, time_t author_time,
312 const char *committer, time_t committer_time,
313 const char *logmsg, struct got_repository *repo)
315 const struct got_error *err = NULL;
316 SHA1_CTX sha1_ctx;
317 char *header = NULL, *tree_str = NULL;
318 char *author_str = NULL, *committer_str = NULL;
319 char *id_str = NULL;
320 size_t headerlen, len = 0, n;
321 FILE *commitfile = NULL;
322 struct got_object_qid *qid;
324 *id = NULL;
326 SHA1Init(&sha1_ctx);
328 if (asprintf(&author_str, "%s%s %lld +0000\n",
329 GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
330 return got_error_prefix_errno("asprintf");
332 if (asprintf(&committer_str, "%s%s %lld +0000\n",
333 GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
334 committer ? committer_time : author_time)
335 == -1) {
336 err = got_error_prefix_errno("asprintf");
337 goto done;
340 len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
341 nparents *
342 (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
343 + strlen(author_str) + strlen(committer_str) + 2 + strlen(logmsg);
345 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
346 err = got_error_prefix_errno("asprintf");
347 goto done;
349 headerlen = strlen(header) + 1;
350 SHA1Update(&sha1_ctx, header, headerlen);
352 commitfile = got_opentemp();
353 if (commitfile == NULL) {
354 err = got_error_prefix_errno("got_opentemp");
355 goto done;
358 n = fwrite(header, 1, headerlen, commitfile);
359 if (n != headerlen) {
360 err = got_ferror(commitfile, GOT_ERR_IO);
361 goto done;
364 err = got_object_id_str(&id_str, tree_id);
365 if (err)
366 goto done;
367 if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
368 == -1) {
369 err = got_error_prefix_errno("asprintf");
370 goto done;
372 len = strlen(tree_str);
373 SHA1Update(&sha1_ctx, tree_str, len);
374 n = fwrite(tree_str, 1, len, commitfile);
375 if (n != len) {
376 err = got_ferror(commitfile, GOT_ERR_IO);
377 goto done;
380 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
381 char *parent_str = NULL;
383 free(id_str);
385 err = got_object_id_str(&id_str, qid->id);
386 if (err)
387 goto done;
388 if (asprintf(&parent_str, "%s%s\n", GOT_COMMIT_LABEL_PARENT,
389 id_str) == -1) {
390 err = got_error_prefix_errno("asprintf");
391 goto done;
393 len = strlen(parent_str);
394 SHA1Update(&sha1_ctx, parent_str, len);
395 n = fwrite(parent_str, 1, len, commitfile);
396 if (n != len) {
397 err = got_ferror(commitfile, GOT_ERR_IO);
398 free(parent_str);
399 goto done;
401 free(parent_str);
404 len = strlen(author_str);
405 SHA1Update(&sha1_ctx, author_str, len);
406 n = fwrite(author_str, 1, len, commitfile);
407 if (n != len) {
408 err = got_ferror(commitfile, GOT_ERR_IO);
409 goto done;
412 len = strlen(committer_str);
413 SHA1Update(&sha1_ctx, committer_str, len);
414 n = fwrite(committer_str, 1, len, commitfile);
415 if (n != len) {
416 err = got_ferror(commitfile, GOT_ERR_IO);
417 goto done;
420 SHA1Update(&sha1_ctx, "\n", 1);
421 n = fwrite("\n", 1, 1, commitfile);
422 if (n != 1) {
423 err = got_ferror(commitfile, GOT_ERR_IO);
424 goto done;
427 len = strlen(logmsg);
428 SHA1Update(&sha1_ctx, logmsg, len);
429 n = fwrite(logmsg, 1, len, commitfile);
430 if (n != len) {
431 err = got_ferror(commitfile, GOT_ERR_IO);
432 goto done;
435 SHA1Update(&sha1_ctx, "\n", 1);
436 n = fwrite("\n", 1, 1, commitfile);
437 if (n != 1) {
438 err = got_ferror(commitfile, GOT_ERR_IO);
439 goto done;
442 *id = malloc(sizeof(**id));
443 if (*id == NULL) {
444 err = got_error_prefix_errno("malloc");
445 goto done;
447 SHA1Final((*id)->sha1, &sha1_ctx);
449 if (fflush(commitfile) != 0) {
450 err = got_error_prefix_errno("fflush");
451 goto done;
453 rewind(commitfile);
455 err = create_object_file(*id, commitfile, repo);
456 done:
457 free(header);
458 free(tree_str);
459 free(author_str);
460 free(committer_str);
461 if (commitfile && fclose(commitfile) != 0 && err == NULL)
462 err = got_error_prefix_errno("fclose");
463 if (err) {
464 free(*id);
465 *id = NULL;
467 return err;