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