Blame


1 44edeea7 2019-04-11 stsp /*
2 44edeea7 2019-04-11 stsp * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
3 44edeea7 2019-04-11 stsp *
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.
7 44edeea7 2019-04-11 stsp *
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.
15 44edeea7 2019-04-11 stsp */
16 44edeea7 2019-04-11 stsp
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>
20 44edeea7 2019-04-11 stsp
21 51130c02 2019-04-13 stsp #include <errno.h>
22 44edeea7 2019-04-11 stsp #include <fcntl.h>
23 44edeea7 2019-04-11 stsp #include <stdio.h>
24 44edeea7 2019-04-11 stsp #include <stdlib.h>
25 44edeea7 2019-04-11 stsp #include <string.h>
26 44edeea7 2019-04-11 stsp #include <stdint.h>
27 44edeea7 2019-04-11 stsp #include <sha1.h>
28 a14a8cf6 2019-04-11 stsp #include <unistd.h>
29 44edeea7 2019-04-11 stsp #include <zlib.h>
30 44edeea7 2019-04-11 stsp
31 44edeea7 2019-04-11 stsp #include "got_error.h"
32 44edeea7 2019-04-11 stsp #include "got_object.h"
33 44edeea7 2019-04-11 stsp #include "got_repository.h"
34 44edeea7 2019-04-11 stsp #include "got_opentemp.h"
35 44edeea7 2019-04-11 stsp
36 44edeea7 2019-04-11 stsp #include "got_lib_sha1.h"
37 44edeea7 2019-04-11 stsp #include "got_lib_deflate.h"
38 44edeea7 2019-04-11 stsp #include "got_lib_delta.h"
39 44edeea7 2019-04-11 stsp #include "got_lib_object.h"
40 44edeea7 2019-04-11 stsp #include "got_lib_lockfile.h"
41 44edeea7 2019-04-11 stsp #include "got_lib_path.h"
42 44edeea7 2019-04-11 stsp
43 44edeea7 2019-04-11 stsp #ifndef nitems
44 44edeea7 2019-04-11 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
45 44edeea7 2019-04-11 stsp #endif
46 44edeea7 2019-04-11 stsp
47 ac1c5662 2019-04-13 stsp static const struct got_error *
48 ac1c5662 2019-04-13 stsp create_loose_object(struct got_object_id *id, FILE *content,
49 ac1c5662 2019-04-13 stsp struct got_repository *repo)
50 ac1c5662 2019-04-13 stsp {
51 ac1c5662 2019-04-13 stsp const struct got_error *err = NULL, *unlock_err = NULL;
52 ac1c5662 2019-04-13 stsp char *objpath = NULL, *outpath = NULL;
53 ac1c5662 2019-04-13 stsp FILE *outfile = NULL;
54 ac1c5662 2019-04-13 stsp struct got_lockfile *lf = NULL;
55 ac1c5662 2019-04-13 stsp size_t outlen = 0;
56 ac1c5662 2019-04-13 stsp
57 ac1c5662 2019-04-13 stsp err = got_object_get_path(&objpath, id, repo);
58 ac1c5662 2019-04-13 stsp if (err)
59 ac1c5662 2019-04-13 stsp return err;
60 ac1c5662 2019-04-13 stsp
61 ac1c5662 2019-04-13 stsp err = got_opentemp_named(&outpath, &outfile, objpath);
62 ac1c5662 2019-04-13 stsp if (err) {
63 ac1c5662 2019-04-13 stsp char *parent_path;
64 ac1c5662 2019-04-13 stsp if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
65 ac1c5662 2019-04-13 stsp goto done;
66 ac1c5662 2019-04-13 stsp err = got_path_dirname(&parent_path, objpath);
67 ac1c5662 2019-04-13 stsp if (err)
68 ac1c5662 2019-04-13 stsp goto done;
69 ac1c5662 2019-04-13 stsp err = got_path_mkdir(parent_path);
70 ac1c5662 2019-04-13 stsp free(parent_path);
71 ac1c5662 2019-04-13 stsp if (err)
72 ac1c5662 2019-04-13 stsp goto done;
73 ac1c5662 2019-04-13 stsp err = got_opentemp_named(&outpath, &outfile, objpath);
74 ac1c5662 2019-04-13 stsp if (err)
75 ac1c5662 2019-04-13 stsp goto done;
76 ac1c5662 2019-04-13 stsp }
77 ac1c5662 2019-04-13 stsp
78 ac1c5662 2019-04-13 stsp err = got_deflate_to_file(&outlen, content, outfile);
79 ac1c5662 2019-04-13 stsp if (err)
80 ac1c5662 2019-04-13 stsp goto done;
81 ac1c5662 2019-04-13 stsp
82 ac1c5662 2019-04-13 stsp err = got_lockfile_lock(&lf, objpath);
83 ac1c5662 2019-04-13 stsp if (err)
84 ac1c5662 2019-04-13 stsp goto done;
85 ac1c5662 2019-04-13 stsp
86 ac1c5662 2019-04-13 stsp if (rename(outpath, objpath) != 0) {
87 ac1c5662 2019-04-13 stsp err = got_error_from_errno();
88 ac1c5662 2019-04-13 stsp goto done;
89 ac1c5662 2019-04-13 stsp }
90 ac1c5662 2019-04-13 stsp free(outpath);
91 ac1c5662 2019-04-13 stsp outpath = NULL;
92 ac1c5662 2019-04-13 stsp
93 ac1c5662 2019-04-13 stsp if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
94 ac1c5662 2019-04-13 stsp err = got_error_from_errno();
95 ac1c5662 2019-04-13 stsp goto done;
96 ac1c5662 2019-04-13 stsp }
97 ac1c5662 2019-04-13 stsp done:
98 ac1c5662 2019-04-13 stsp free(objpath);
99 ac1c5662 2019-04-13 stsp if (outpath) {
100 ac1c5662 2019-04-13 stsp if (unlink(outpath) != 0 && err == NULL)
101 ac1c5662 2019-04-13 stsp err = got_error_from_errno();
102 ac1c5662 2019-04-13 stsp free(outpath);
103 ac1c5662 2019-04-13 stsp }
104 ac1c5662 2019-04-13 stsp if (outfile && fclose(outfile) != 0 && err == NULL)
105 ac1c5662 2019-04-13 stsp err = got_error_from_errno();
106 ac1c5662 2019-04-13 stsp if (lf)
107 ac1c5662 2019-04-13 stsp unlock_err = got_lockfile_unlock(lf);
108 ac1c5662 2019-04-13 stsp return err ? err : unlock_err;
109 ac1c5662 2019-04-13 stsp }
110 ac1c5662 2019-04-13 stsp
111 44edeea7 2019-04-11 stsp const struct got_error *
112 f970685c 2019-04-13 stsp got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
113 f970685c 2019-04-13 stsp struct got_repository *repo)
114 44edeea7 2019-04-11 stsp {
115 ac1c5662 2019-04-13 stsp const struct got_error *err = NULL;
116 ac1c5662 2019-04-13 stsp char *header = NULL;
117 ac1c5662 2019-04-13 stsp FILE *blobfile = NULL;
118 44edeea7 2019-04-11 stsp int fd = -1;
119 44edeea7 2019-04-11 stsp struct stat sb;
120 44edeea7 2019-04-11 stsp SHA1_CTX sha1_ctx;
121 44edeea7 2019-04-11 stsp uint8_t digest[SHA1_DIGEST_LENGTH];
122 ac1c5662 2019-04-13 stsp size_t headerlen = 0, n;
123 44edeea7 2019-04-11 stsp
124 44edeea7 2019-04-11 stsp *id = NULL;
125 44edeea7 2019-04-11 stsp
126 44edeea7 2019-04-11 stsp SHA1Init(&sha1_ctx);
127 44edeea7 2019-04-11 stsp
128 44edeea7 2019-04-11 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
129 44edeea7 2019-04-11 stsp if (fd == -1)
130 44edeea7 2019-04-11 stsp return got_error_from_errno();
131 44edeea7 2019-04-11 stsp
132 44edeea7 2019-04-11 stsp if (fstat(fd, &sb) == -1) {
133 44edeea7 2019-04-11 stsp err = got_error_from_errno();
134 44edeea7 2019-04-11 stsp goto done;
135 44edeea7 2019-04-11 stsp }
136 44edeea7 2019-04-11 stsp
137 44edeea7 2019-04-11 stsp if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
138 44edeea7 2019-04-11 stsp sb.st_size) == -1) {
139 44edeea7 2019-04-11 stsp err = got_error_from_errno();
140 44edeea7 2019-04-11 stsp goto done;
141 44edeea7 2019-04-11 stsp }
142 ffb286fd 2019-04-11 stsp headerlen = strlen(header) + 1;
143 ffb286fd 2019-04-11 stsp SHA1Update(&sha1_ctx, header, headerlen);
144 44edeea7 2019-04-11 stsp
145 81984c6b 2019-04-11 stsp blobfile = got_opentemp();
146 81984c6b 2019-04-11 stsp if (blobfile == NULL) {
147 81984c6b 2019-04-11 stsp err = got_error_from_errno();
148 44edeea7 2019-04-11 stsp goto done;
149 81984c6b 2019-04-11 stsp }
150 44edeea7 2019-04-11 stsp
151 ac1c5662 2019-04-13 stsp n = fwrite(header, 1, headerlen, blobfile);
152 ac1c5662 2019-04-13 stsp if (n != headerlen) {
153 f16c2465 2019-04-11 stsp err = got_ferror(blobfile, GOT_ERR_IO);
154 f16c2465 2019-04-11 stsp goto done;
155 f16c2465 2019-04-11 stsp }
156 44edeea7 2019-04-11 stsp while (1) {
157 44edeea7 2019-04-11 stsp char buf[8192];
158 a14a8cf6 2019-04-11 stsp ssize_t inlen;
159 44edeea7 2019-04-11 stsp
160 a14a8cf6 2019-04-11 stsp inlen = read(fd, buf, sizeof(buf));
161 a14a8cf6 2019-04-11 stsp if (inlen == -1) {
162 a14a8cf6 2019-04-11 stsp err = got_error_from_errno();
163 a14a8cf6 2019-04-11 stsp goto done;
164 44edeea7 2019-04-11 stsp }
165 a14a8cf6 2019-04-11 stsp if (inlen == 0)
166 a14a8cf6 2019-04-11 stsp break; /* EOF */
167 44edeea7 2019-04-11 stsp SHA1Update(&sha1_ctx, buf, inlen);
168 ac1c5662 2019-04-13 stsp n = fwrite(buf, 1, inlen, blobfile);
169 ac1c5662 2019-04-13 stsp if (n != inlen) {
170 44edeea7 2019-04-11 stsp err = got_ferror(blobfile, GOT_ERR_IO);
171 44edeea7 2019-04-11 stsp goto done;
172 44edeea7 2019-04-11 stsp }
173 44edeea7 2019-04-11 stsp }
174 44edeea7 2019-04-11 stsp
175 44edeea7 2019-04-11 stsp SHA1Final(digest, &sha1_ctx);
176 44edeea7 2019-04-11 stsp *id = malloc(sizeof(**id));
177 44edeea7 2019-04-11 stsp if (*id == NULL) {
178 44edeea7 2019-04-11 stsp err = got_error_from_errno();
179 44edeea7 2019-04-11 stsp goto done;
180 44edeea7 2019-04-11 stsp }
181 44edeea7 2019-04-11 stsp memcpy((*id)->sha1, digest, SHA1_DIGEST_LENGTH);
182 44edeea7 2019-04-11 stsp
183 44edeea7 2019-04-11 stsp if (fflush(blobfile) != 0) {
184 44edeea7 2019-04-11 stsp err = got_error_from_errno();
185 44edeea7 2019-04-11 stsp goto done;
186 44edeea7 2019-04-11 stsp }
187 44edeea7 2019-04-11 stsp rewind(blobfile);
188 44edeea7 2019-04-11 stsp
189 ac1c5662 2019-04-13 stsp err = create_loose_object(*id, blobfile, repo);
190 44edeea7 2019-04-11 stsp done:
191 44edeea7 2019-04-11 stsp free(header);
192 44edeea7 2019-04-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
193 44edeea7 2019-04-11 stsp err = got_error_from_errno();
194 44edeea7 2019-04-11 stsp if (blobfile && fclose(blobfile) != 0 && err == NULL)
195 44edeea7 2019-04-11 stsp err = got_error_from_errno();
196 44edeea7 2019-04-11 stsp if (err) {
197 44edeea7 2019-04-11 stsp free(*id);
198 44edeea7 2019-04-11 stsp *id = NULL;
199 44edeea7 2019-04-11 stsp }
200 ac1c5662 2019-04-13 stsp return err;
201 44edeea7 2019-04-11 stsp }