Hash :
c6f826b4
Author :
Date :
2019-04-13T18:08:40
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
/*
* Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/queue.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sha1.h>
#include <unistd.h>
#include <zlib.h>
#include "got_error.h"
#include "got_object.h"
#include "got_repository.h"
#include "got_opentemp.h"
#include "got_lib_sha1.h"
#include "got_lib_deflate.h"
#include "got_lib_delta.h"
#include "got_lib_object.h"
#include "got_lib_lockfile.h"
#include "got_lib_path.h"
#ifndef nitems
#define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
#endif
static const struct got_error *
create_loose_object(struct got_object_id *id, FILE *content,
struct got_repository *repo)
{
const struct got_error *err = NULL, *unlock_err = NULL;
char *objpath = NULL, *tmppath = NULL;
FILE *tmpfile = NULL;
struct got_lockfile *lf = NULL;
size_t tmplen = 0;
err = got_object_get_path(&objpath, id, repo);
if (err)
return err;
err = got_opentemp_named(&tmppath, &tmpfile, objpath);
if (err) {
char *parent_path;
if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
goto done;
err = got_path_dirname(&parent_path, objpath);
if (err)
goto done;
err = got_path_mkdir(parent_path);
free(parent_path);
if (err)
goto done;
err = got_opentemp_named(&tmppath, &tmpfile, objpath);
if (err)
goto done;
}
err = got_deflate_to_file(&tmplen, content, tmpfile);
if (err)
goto done;
err = got_lockfile_lock(&lf, objpath);
if (err)
goto done;
if (rename(tmppath, objpath) != 0) {
err = got_error_from_errno();
goto done;
}
free(tmppath);
tmppath = NULL;
if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
err = got_error_from_errno();
goto done;
}
done:
free(objpath);
if (tmppath) {
if (unlink(tmppath) != 0 && err == NULL)
err = got_error_from_errno();
free(tmppath);
}
if (tmpfile && fclose(tmpfile) != 0 && err == NULL)
err = got_error_from_errno();
if (lf)
unlock_err = got_lockfile_unlock(lf);
return err ? err : unlock_err;
}
const struct got_error *
got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
struct got_repository *repo)
{
const struct got_error *err = NULL;
char *header = NULL;
FILE *blobfile = NULL;
int fd = -1;
struct stat sb;
SHA1_CTX sha1_ctx;
uint8_t digest[SHA1_DIGEST_LENGTH];
size_t headerlen = 0, n;
*id = NULL;
SHA1Init(&sha1_ctx);
fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
if (fd == -1)
return got_error_from_errno();
if (fstat(fd, &sb) == -1) {
err = got_error_from_errno();
goto done;
}
if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
sb.st_size) == -1) {
err = got_error_from_errno();
goto done;
}
headerlen = strlen(header) + 1;
SHA1Update(&sha1_ctx, header, headerlen);
blobfile = got_opentemp();
if (blobfile == NULL) {
err = got_error_from_errno();
goto done;
}
n = fwrite(header, 1, headerlen, blobfile);
if (n != headerlen) {
err = got_ferror(blobfile, GOT_ERR_IO);
goto done;
}
while (1) {
char buf[8192];
ssize_t inlen;
inlen = read(fd, buf, sizeof(buf));
if (inlen == -1) {
err = got_error_from_errno();
goto done;
}
if (inlen == 0)
break; /* EOF */
SHA1Update(&sha1_ctx, buf, inlen);
n = fwrite(buf, 1, inlen, blobfile);
if (n != inlen) {
err = got_ferror(blobfile, GOT_ERR_IO);
goto done;
}
}
SHA1Final(digest, &sha1_ctx);
*id = malloc(sizeof(**id));
if (*id == NULL) {
err = got_error_from_errno();
goto done;
}
memcpy((*id)->sha1, digest, SHA1_DIGEST_LENGTH);
if (fflush(blobfile) != 0) {
err = got_error_from_errno();
goto done;
}
rewind(blobfile);
err = create_loose_object(*id, blobfile, repo);
done:
free(header);
if (fd != -1 && close(fd) != 0 && err == NULL)
err = got_error_from_errno();
if (blobfile && fclose(blobfile) != 0 && err == NULL)
err = got_error_from_errno();
if (err) {
free(*id);
*id = NULL;
}
return err;
}