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>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_repository.h"
32 #include "got_opentemp.h"
34 #include "got_lib_sha1.h"
35 #include "got_lib_deflate.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_object.h"
38 #include "got_lib_lockfile.h"
39 #include "got_lib_path.h"
42 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
45 const struct got_error *
46 got_object_blob_create(struct got_object_id **id, struct got_repository *repo,
47 const char *ondisk_path)
49 const struct got_error *err = NULL, *unlock_err = NULL;
50 char *header = NULL, *blobpath = NULL, *objpath = NULL, *outpath = NULL;
51 FILE *infile = NULL, *blobfile = NULL, *outfile = NULL;
55 uint8_t digest[SHA1_DIGEST_LENGTH];
56 struct got_lockfile *lf = NULL;
63 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
65 return got_error_from_errno();
67 if (fstat(fd, &sb) == -1) {
68 err = got_error_from_errno();
72 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
74 err = got_error_from_errno();
77 SHA1Update(&sha1_ctx, header, strlen(header) + 1);
79 infile = fdopen(fd, "r");
81 err = got_error_from_errno();
86 err = got_opentemp_named(&blobpath, &blobfile, "/tmp/got-blob-create");
94 inlen = fread(buf, 1, sizeof(buf), infile);
97 err = got_error_from_errno();
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);
110 SHA1Final(digest, &sha1_ctx);
111 *id = malloc(sizeof(**id));
113 err = got_error_from_errno();
116 memcpy((*id)->sha1, digest, SHA1_DIGEST_LENGTH);
118 if (fflush(blobfile) != 0) {
119 err = got_error_from_errno();
124 err = got_object_get_path(&objpath, *id, repo);
128 err = got_opentemp_named(&outpath, &outfile, objpath);
132 err = got_deflate_to_file(&outlen, blobfile, outfile);
136 err = got_lockfile_lock(&lf, objpath);
140 if (rename(outpath, objpath) != 0) {
141 err = got_error_from_errno();
147 if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
148 err = got_error_from_errno();
155 if (unlink(outpath) != 0 && err == NULL)
156 err = got_error_from_errno();
159 if (fd != -1 && close(fd) != 0 && err == NULL)
160 err = got_error_from_errno();
161 if (infile && fclose(infile) != 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;