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 const struct got_error *
48 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
49 struct got_repository *repo)
50 {
51 const struct got_error *err = NULL, *unlock_err = NULL;
52 char *header = NULL, *objpath = NULL, *outpath = NULL;
53 FILE *blobfile = NULL, *outfile = NULL;
54 int fd = -1;
55 struct stat sb;
56 SHA1_CTX sha1_ctx;
57 uint8_t digest[SHA1_DIGEST_LENGTH];
58 struct got_lockfile *lf = NULL;
59 size_t outlen = 0, headerlen = 0;
61 *id = NULL;
63 SHA1Init(&sha1_ctx);
65 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
66 if (fd == -1)
67 return got_error_from_errno();
69 if (fstat(fd, &sb) == -1) {
70 err = got_error_from_errno();
71 goto done;
72 }
74 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
75 sb.st_size) == -1) {
76 err = got_error_from_errno();
77 goto done;
78 }
79 headerlen = strlen(header) + 1;
80 SHA1Update(&sha1_ctx, header, headerlen);
82 blobfile = got_opentemp();
83 if (blobfile == NULL) {
84 err = got_error_from_errno();
85 goto done;
86 }
88 outlen = fwrite(header, 1, headerlen, blobfile);
89 if (outlen != headerlen) {
90 err = got_ferror(blobfile, GOT_ERR_IO);
91 goto done;
92 }
93 while (1) {
94 char buf[8192];
95 ssize_t inlen;
96 size_t outlen;
98 inlen = read(fd, buf, sizeof(buf));
99 if (inlen == -1) {
100 err = got_error_from_errno();
101 goto done;
103 if (inlen == 0)
104 break; /* EOF */
105 SHA1Update(&sha1_ctx, buf, inlen);
106 outlen = fwrite(buf, 1, inlen, blobfile);
107 if (outlen != inlen) {
108 err = got_ferror(blobfile, GOT_ERR_IO);
109 goto done;
113 SHA1Final(digest, &sha1_ctx);
114 *id = malloc(sizeof(**id));
115 if (*id == NULL) {
116 err = got_error_from_errno();
117 goto done;
119 memcpy((*id)->sha1, digest, SHA1_DIGEST_LENGTH);
121 if (fflush(blobfile) != 0) {
122 err = got_error_from_errno();
123 goto done;
125 rewind(blobfile);
127 err = got_object_get_path(&objpath, *id, repo);
128 if (err)
129 goto done;
131 err = got_opentemp_named(&outpath, &outfile, objpath);
132 if (err) {
133 char *parent_path;
134 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
135 goto done;
136 err = got_path_dirname(&parent_path, objpath);
137 if (err)
138 goto done;
139 err = got_path_mkdir(parent_path);
140 free(parent_path);
141 if (err)
142 goto done;
143 err = got_opentemp_named(&outpath, &outfile, objpath);
144 if (err)
145 goto done;
148 err = got_deflate_to_file(&outlen, blobfile, outfile);
149 if (err)
150 goto done;
152 err = got_lockfile_lock(&lf, objpath);
153 if (err)
154 goto done;
156 if (rename(outpath, objpath) != 0) {
157 err = got_error_from_errno();
158 goto done;
160 free(outpath);
161 outpath = NULL;
163 if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
164 err = got_error_from_errno();
165 goto done;
167 done:
168 free(header);
169 free(objpath);
170 if (outpath) {
171 if (unlink(outpath) != 0 && err == NULL)
172 err = got_error_from_errno();
173 free(outpath);
175 if (fd != -1 && close(fd) != 0 && err == NULL)
176 err = got_error_from_errno();
177 if (blobfile && fclose(blobfile) != 0 && err == NULL)
178 err = got_error_from_errno();
179 if (outfile && fclose(outfile) != 0 && err == NULL)
180 err = got_error_from_errno();
181 if (err) {
182 free(*id);
183 *id = NULL;
185 if (lf)
186 unlock_err = got_lockfile_unlock(lf);
187 return err ? err : unlock_err;