Hash :
81984c6b
Author :
Date :
2019-04-11T21:59:42
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
/*
* 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 <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
const struct got_error *
got_object_blob_create(struct got_object_id **id, struct got_repository *repo,
const char *ondisk_path)
{
const struct got_error *err = NULL, *unlock_err = NULL;
char *header = NULL, *objpath = NULL, *outpath = NULL;
FILE *blobfile = NULL, *outfile = NULL;
int fd = -1;
struct stat sb;
SHA1_CTX sha1_ctx;
uint8_t digest[SHA1_DIGEST_LENGTH];
struct got_lockfile *lf = NULL;
size_t outlen = 0, headerlen = 0;
*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;
}
outlen = fwrite(header, 1, headerlen, blobfile);
if (outlen != headerlen) {
err = got_ferror(blobfile, GOT_ERR_IO);
goto done;
}
while (1) {
char buf[8192];
ssize_t inlen;
size_t outlen;
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);
outlen = fwrite(buf, 1, inlen, blobfile);
if (outlen != 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 = got_object_get_path(&objpath, *id, repo);
if (err)
goto done;
err = got_opentemp_named(&outpath, &outfile, objpath);
if (err)
goto done;
err = got_deflate_to_file(&outlen, blobfile, outfile);
if (err)
goto done;
err = got_lockfile_lock(&lf, objpath);
if (err)
goto done;
if (rename(outpath, objpath) != 0) {
err = got_error_from_errno();
goto done;
}
free(outpath);
outpath = NULL;
if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
err = got_error_from_errno();
goto done;
}
done:
free(header);
if (outpath) {
if (unlink(outpath) != 0 && err == NULL)
err = got_error_from_errno();
free(outpath);
}
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 (outfile && fclose(outfile) != 0 && err == NULL)
err = got_error_from_errno();
if (err) {
free(*id);
*id = NULL;
}
if (lf)
unlock_err = got_lockfile_unlock(lf);
return err ? err : unlock_err;
}