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 <fcntl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdint.h>
26 #include <sha1.h>
27 #include <zlib.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_repository.h"
32 #include "got_opentemp.h"
34 #include "got_lib_sha1.h"
35 #include "got_lib_deflate.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_object.h"
38 #include "got_lib_lockfile.h"
39 #include "got_lib_path.h"
41 #ifndef nitems
42 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
43 #endif
45 const struct got_error *
46 got_object_blob_create(struct got_object_id **id, struct got_repository *repo,
47 const char *ondisk_path)
48 {
49 const struct got_error *err = NULL, *unlock_err = NULL;
50 char *header = NULL, *blobpath = NULL, *objpath = NULL, *outpath = NULL;
51 FILE *infile = NULL, *blobfile = NULL, *outfile = NULL;
52 int fd = -1;
53 struct stat sb;
54 SHA1_CTX sha1_ctx;
55 uint8_t digest[SHA1_DIGEST_LENGTH];
56 struct got_lockfile *lf = NULL;
57 size_t outlen = 0;
59 *id = NULL;
61 SHA1Init(&sha1_ctx);
63 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
64 if (fd == -1)
65 return got_error_from_errno();
67 if (fstat(fd, &sb) == -1) {
68 err = got_error_from_errno();
69 goto done;
70 }
72 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
73 sb.st_size) == -1) {
74 err = got_error_from_errno();
75 goto done;
76 }
77 SHA1Update(&sha1_ctx, header, strlen(header) + 1);
79 infile = fdopen(fd, "r");
80 if (infile == NULL) {
81 err = got_error_from_errno();
82 goto done;
83 }
85 err = got_opentemp_named(&blobpath, &blobfile, "/tmp/got-blob-create");
86 if (err)
87 goto done;
89 while (1) {
90 char buf[8192];
91 size_t inlen, outlen;
93 inlen = fread(buf, 1, sizeof(buf), infile);
94 if (inlen == 0) {
95 if (ferror(infile)) {
96 err = got_error_from_errno();
97 goto done;
98 }
99 break; /* EOF */
101 SHA1Update(&sha1_ctx, buf, inlen);
102 outlen = fwrite(buf, 1, inlen, blobfile);
103 if (outlen != inlen) {
104 err = got_ferror(blobfile, GOT_ERR_IO);
105 goto done;
109 SHA1Final(digest, &sha1_ctx);
110 *id = malloc(sizeof(**id));
111 if (*id == NULL) {
112 err = got_error_from_errno();
113 goto done;
115 memcpy((*id)->sha1, digest, SHA1_DIGEST_LENGTH);
117 if (fflush(blobfile) != 0) {
118 err = got_error_from_errno();
119 goto done;
121 rewind(blobfile);
123 err = got_object_get_path(&objpath, *id, repo);
124 if (err)
125 goto done;
127 err = got_opentemp_named(&outpath, &outfile, objpath);
128 if (err)
129 goto done;
131 err = got_deflate_to_file(&outlen, blobfile, outfile);
132 if (err)
133 goto done;
135 err = got_lockfile_lock(&lf, objpath);
136 if (err)
137 goto done;
139 if (rename(outpath, objpath) != 0) {
140 err = got_error_from_errno();
141 goto done;
143 free(outpath);
144 outpath = NULL;
146 if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
147 err = got_error_from_errno();
148 goto done;
150 done:
151 free(header);
152 free(blobpath);
153 if (outpath) {
154 if (unlink(outpath) != 0 && err == NULL)
155 err = got_error_from_errno();
156 free(outpath);
158 if (fd != -1 && close(fd) != 0 && err == NULL)
159 err = got_error_from_errno();
160 if (infile && fclose(infile) != 0 && err == NULL)
161 err = got_error_from_errno();
162 if (blobfile && fclose(blobfile) != 0 && err == NULL)
163 err = got_error_from_errno();
164 if (outfile && fclose(outfile) != 0 && err == NULL)
165 err = got_error_from_errno();
166 if (err) {
167 free(*id);
168 *id = NULL;
170 if (lf)
171 unlock_err = got_lockfile_unlock(lf);
172 return err ? err : unlock_err;