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