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"
36 #include "got_lib_sha1.h"
37 #include "got_lib_deflate.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_object.h"
40 #include "got_lib_lockfile.h"
41 #include "got_lib_path.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_from_errno();
88 goto done;
89 }
90 free(tmppath);
91 tmppath = NULL;
93 if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
94 err = got_error_from_errno();
95 goto done;
96 }
97 done:
98 free(objpath);
99 if (tmppath) {
100 if (unlink(tmppath) != 0 && err == NULL)
101 err = got_error_from_errno();
102 free(tmppath);
104 if (tmpfile && fclose(tmpfile) != 0 && err == NULL)
105 err = got_error_from_errno();
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_from_errno();
131 if (fstat(fd, &sb) == -1) {
132 err = got_error_from_errno();
133 goto done;
136 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
137 sb.st_size) == -1) {
138 err = got_error_from_errno();
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_from_errno();
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_from_errno();
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_from_errno();
177 goto done;
179 SHA1Final((*id)->sha1, &sha1_ctx);
181 if (fflush(blobfile) != 0) {
182 err = got_error_from_errno();
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_from_errno();
192 if (blobfile && fclose(blobfile) != 0 && err == NULL)
193 err = got_error_from_errno();
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 SIMPLEQ_FOREACH(te, &entries->head, entry) {
226 err = mode2str(modebuf, sizeof(modebuf), te->mode);
227 if (err)
228 return err;
229 len += strlen(modebuf) + strlen(te->name) + 1 +
230 SHA1_DIGEST_LENGTH;
233 if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
234 err = got_error_from_errno();
235 goto done;
237 headerlen = strlen(header) + 1;
238 SHA1Update(&sha1_ctx, header, headerlen);
240 treefile = got_opentemp();
241 if (treefile == NULL) {
242 err = got_error_from_errno();
243 goto done;
246 n = fwrite(header, 1, headerlen, treefile);
247 if (n != headerlen) {
248 err = got_ferror(treefile, GOT_ERR_IO);
249 goto done;
252 SIMPLEQ_FOREACH(te, &entries->head, entry) {
253 err = mode2str(modebuf, sizeof(modebuf), te->mode);
254 if (err)
255 goto done;
256 len = strlen(modebuf);
257 n = fwrite(modebuf, 1, len, treefile);
258 if (n != len) {
259 err = got_ferror(treefile, GOT_ERR_IO);
260 goto done;
262 SHA1Update(&sha1_ctx, modebuf, len);
264 len = strlen(te->name) + 1; /* must include NUL */
265 n = fwrite(te->name, 1, len, treefile);
266 if (n != len) {
267 err = got_ferror(treefile, GOT_ERR_IO);
268 goto done;
270 SHA1Update(&sha1_ctx, te->name, len);
272 len = SHA1_DIGEST_LENGTH;
273 n = fwrite(te->id->sha1, 1, len, treefile);
274 if (n != len) {
275 err = got_ferror(treefile, GOT_ERR_IO);
276 goto done;
278 SHA1Update(&sha1_ctx, te->id->sha1, len);
281 *id = malloc(sizeof(**id));
282 if (*id == NULL) {
283 err = got_error_from_errno();
284 goto done;
286 SHA1Final((*id)->sha1, &sha1_ctx);
288 if (fflush(treefile) != 0) {
289 err = got_error_from_errno();
290 goto done;
292 rewind(treefile);
294 err = create_object_file(*id, treefile, repo);
295 done:
296 free(header);
297 if (treefile && fclose(treefile) != 0 && err == NULL)
298 err = got_error_from_errno();
299 if (err) {
300 free(*id);
301 *id = NULL;
303 return err;