2 * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
19 #include <sys/queue.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"
43 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
46 const struct got_error *
47 got_object_blob_create(struct got_object_id **id, struct got_repository *repo,
48 const char *ondisk_path)
50 const struct got_error *err = NULL, *unlock_err = NULL;
51 char *header = NULL, *objpath = NULL, *outpath = NULL;
52 FILE *blobfile = NULL, *outfile = NULL;
56 uint8_t digest[SHA1_DIGEST_LENGTH];
57 struct got_lockfile *lf = NULL;
58 size_t outlen = 0, headerlen = 0;
64 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
66 return got_error_from_errno();
68 if (fstat(fd, &sb) == -1) {
69 err = got_error_from_errno();
73 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
75 err = got_error_from_errno();
78 headerlen = strlen(header) + 1;
79 SHA1Update(&sha1_ctx, header, headerlen);
81 blobfile = got_opentemp();
82 if (blobfile == NULL) {
83 err = got_error_from_errno();
87 outlen = fwrite(header, 1, headerlen, blobfile);
88 if (outlen != headerlen) {
89 err = got_ferror(blobfile, GOT_ERR_IO);
97 inlen = read(fd, buf, sizeof(buf));
99 err = got_error_from_errno();
104 SHA1Update(&sha1_ctx, buf, inlen);
105 outlen = fwrite(buf, 1, inlen, blobfile);
106 if (outlen != inlen) {
107 err = got_ferror(blobfile, GOT_ERR_IO);
112 SHA1Final(digest, &sha1_ctx);
113 *id = malloc(sizeof(**id));
115 err = got_error_from_errno();
118 memcpy((*id)->sha1, digest, SHA1_DIGEST_LENGTH);
120 if (fflush(blobfile) != 0) {
121 err = got_error_from_errno();
126 err = got_object_get_path(&objpath, *id, repo);
130 err = got_opentemp_named(&outpath, &outfile, objpath);
134 err = got_deflate_to_file(&outlen, blobfile, outfile);
138 err = got_lockfile_lock(&lf, objpath);
142 if (rename(outpath, objpath) != 0) {
143 err = got_error_from_errno();
149 if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
150 err = got_error_from_errno();
157 if (unlink(outpath) != 0 && err == NULL)
158 err = got_error_from_errno();
161 if (fd != -1 && close(fd) != 0 && err == NULL)
162 err = got_error_from_errno();
163 if (blobfile && fclose(blobfile) != 0 && err == NULL)
164 err = got_error_from_errno();
165 if (outfile && fclose(outfile) != 0 && err == NULL)
166 err = got_error_from_errno();
172 unlock_err = got_lockfile_unlock(lf);
173 return err ? err : unlock_err;