2 * Copyright (c) 2018 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/queue.h>
26 #include "got_error.h"
28 #include "got_lib_fileindex.h"
30 const struct got_error *
31 got_fileindex_entry_open(struct got_fileindex_entry **entry,
32 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1)
37 if (lstat(ondisk_path, &sb) != 0)
38 return got_error_from_errno();
40 *entry = calloc(1, sizeof(**entry));
42 return got_error_from_errno();
44 (*entry)->path = strdup(relpath);
45 if ((*entry)->path == NULL) {
46 const struct got_error *err = got_error_from_errno();
52 (*entry)->ctime_sec = sb.st_ctime;
53 (*entry)->ctime_nsec = sb.st_ctimensec;
54 (*entry)->mtime_sec = sb.st_mtime;
55 (*entry)->mtime_nsec = sb.st_mtimensec;
56 (*entry)->uid = sb.st_uid;
57 (*entry)->gid = sb.st_gid;
58 (*entry)->size = (sb.st_size & 0xffffffff);
59 if (sb.st_mode & S_IFLNK)
60 (*entry)->mode = GOT_INDEX_ENTRY_MODE_SYMLINK;
62 (*entry)->mode = GOT_INDEX_ENTRY_MODE_REGULAR_FILE;
63 (*entry)->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
64 GOT_INDEX_ENTRY_MODE_PERMS_SHIFT);
65 memcpy((*entry)->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
66 len = strlen(relpath);
67 if (len > GOT_INDEX_ENTRY_F_PATH_LEN)
68 len = GOT_INDEX_ENTRY_F_PATH_LEN;
69 (*entry)->flags |= len;
75 got_fileindex_entry_close(struct got_fileindex_entry *entry)
81 const struct got_error *
82 got_fileindex_entry_add(struct got_fileindex *fileindex,
83 struct got_fileindex_entry *entry)
85 /* TODO keep entries sorted by name */
86 TAILQ_INSERT_TAIL(&fileindex->entries, entry, entry);
87 fileindex->nentries++;
91 struct got_fileindex *
92 got_fileindex_open(void)
94 struct got_fileindex *fileindex;
96 fileindex = calloc(1, sizeof(*fileindex));
98 TAILQ_INIT(&fileindex->entries);
103 got_fileindex_close(struct got_fileindex *fileindex)
105 struct got_fileindex_entry *entry;
107 while (!TAILQ_EMPTY(&fileindex->entries)) {
108 entry = TAILQ_FIRST(&fileindex->entries);
109 TAILQ_REMOVE(&fileindex->entries, entry, entry);
110 got_fileindex_entry_close(entry);
111 fileindex->nentries--;
116 static const struct got_error *
117 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
119 uint8_t buf[sizeof(uint64_t)];
123 memcpy(buf, &val, sizeof(val));
124 SHA1Update(ctx, buf, sizeof(val));
125 n = fwrite(buf, 1, sizeof(val), outfile);
126 if (n != sizeof(val))
127 return got_ferror(outfile, GOT_ERR_IO);
131 static const struct got_error *
132 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
134 uint8_t buf[sizeof(uint32_t)];
138 memcpy(buf, &val, sizeof(val));
139 SHA1Update(ctx, buf, sizeof(val));
140 n = fwrite(buf, 1, sizeof(val), outfile);
141 if (n != sizeof(val))
142 return got_ferror(outfile, GOT_ERR_IO);
146 static const struct got_error *
147 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
149 uint8_t buf[sizeof(uint16_t)];
153 memcpy(buf, &val, sizeof(val));
154 SHA1Update(ctx, buf, sizeof(val));
155 n = fwrite(buf, 1, sizeof(val), outfile);
156 if (n != sizeof(val))
157 return got_ferror(outfile, GOT_ERR_IO);
161 static const struct got_error *
162 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
165 static const uint8_t zero[8] = { 0 };
170 SHA1Update(ctx, path, len);
171 n = fwrite(path, 1, len, outfile);
173 return got_ferror(outfile, GOT_ERR_IO);
176 SHA1Update(ctx, zero, pad);
177 n = fwrite(zero, 1, pad, outfile);
179 return got_ferror(outfile, GOT_ERR_IO);
183 static const struct got_error *
184 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
187 const struct got_error *err;
190 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
193 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
196 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
199 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
203 err = write_fileindex_val32(ctx, entry->uid, outfile);
206 err = write_fileindex_val32(ctx, entry->gid, outfile);
209 err = write_fileindex_val32(ctx, entry->size, outfile);
213 err = write_fileindex_val16(ctx, entry->mode, outfile);
217 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
218 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
219 if (n != SHA1_DIGEST_LENGTH)
220 return got_ferror(outfile, GOT_ERR_IO);
222 err = write_fileindex_val32(ctx, entry->flags, outfile);
226 err = write_fileindex_path(ctx, entry->path, outfile);
230 const struct got_error *
231 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
233 struct got_fileindex_hdr hdr;
234 struct got_fileindex_entry *entry;
236 uint8_t sha1[SHA1_DIGEST_LENGTH];
238 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
239 sizeof(hdr.nentries);
244 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
245 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
246 hdr.nentries = htobe32(fileindex->nentries);
248 memcpy(buf, &hdr, len);
249 SHA1Update(&ctx, buf, len);
250 n = fwrite(buf, 1, len, outfile);
252 return got_ferror(outfile, GOT_ERR_IO);
254 TAILQ_FOREACH(entry, &fileindex->entries, entry) {
255 const struct got_error *err;
256 err = write_fileindex_entry(&ctx, entry, outfile);
261 SHA1Final(sha1, &ctx);
262 n = fwrite(sha1, 1, sizeof(sha1), outfile);
263 if (n != sizeof(sha1))
264 return got_ferror(outfile, GOT_ERR_IO);