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>
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_repository.h"
34 #include "got_opentemp.h"
36 #include "got_lib_sha1.h"
37 #include "got_lib_deflate.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_object.h"
40 #include "got_lib_lockfile.h"
41 #include "got_lib_path.h"
44 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
47 static const struct got_error *
48 create_loose_object(struct got_object_id *id, FILE *content,
49 struct got_repository *repo)
51 const struct got_error *err = NULL, *unlock_err = NULL;
52 char *objpath = NULL, *tmppath = NULL;
54 struct got_lockfile *lf = NULL;
57 err = got_object_get_path(&objpath, id, repo);
61 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
64 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
66 err = got_path_dirname(&parent_path, objpath);
69 err = got_path_mkdir(parent_path);
73 err = got_opentemp_named(&tmppath, &tmpfile, objpath);
78 err = got_deflate_to_file(&tmplen, content, tmpfile);
82 err = got_lockfile_lock(&lf, objpath);
86 if (rename(tmppath, objpath) != 0) {
87 err = got_error_from_errno();
93 if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
94 err = got_error_from_errno();
100 if (unlink(tmppath) != 0 && err == NULL)
101 err = got_error_from_errno();
104 if (tmpfile && fclose(tmpfile) != 0 && err == NULL)
105 err = got_error_from_errno();
107 unlock_err = got_lockfile_unlock(lf);
108 return err ? err : unlock_err;
111 const struct got_error *
112 got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
113 struct got_repository *repo)
115 const struct got_error *err = NULL;
117 FILE *blobfile = NULL;
121 uint8_t digest[SHA1_DIGEST_LENGTH];
122 size_t headerlen = 0, n;
128 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
130 return got_error_from_errno();
132 if (fstat(fd, &sb) == -1) {
133 err = got_error_from_errno();
137 if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
139 err = got_error_from_errno();
142 headerlen = strlen(header) + 1;
143 SHA1Update(&sha1_ctx, header, headerlen);
145 blobfile = got_opentemp();
146 if (blobfile == NULL) {
147 err = got_error_from_errno();
151 n = fwrite(header, 1, headerlen, blobfile);
152 if (n != headerlen) {
153 err = got_ferror(blobfile, GOT_ERR_IO);
160 inlen = read(fd, buf, sizeof(buf));
162 err = got_error_from_errno();
167 SHA1Update(&sha1_ctx, buf, inlen);
168 n = fwrite(buf, 1, inlen, blobfile);
170 err = got_ferror(blobfile, GOT_ERR_IO);
175 SHA1Final(digest, &sha1_ctx);
176 *id = malloc(sizeof(**id));
178 err = got_error_from_errno();
181 memcpy((*id)->sha1, digest, SHA1_DIGEST_LENGTH);
183 if (fflush(blobfile) != 0) {
184 err = got_error_from_errno();
189 err = create_loose_object(*id, blobfile, repo);
192 if (fd != -1 && close(fd) != 0 && err == NULL)
193 err = got_error_from_errno();
194 if (blobfile && fclose(blobfile) != 0 && err == NULL)
195 err = got_error_from_errno();