Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sha1.h>
24 #include <endian.h>
26 #include "got_error.h"
28 #include "got_fileindex_lib.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)
33 {
34 struct stat sb;
35 size_t len;
37 if (lstat(ondisk_path, &sb) != 0)
38 return got_error_from_errno();
40 *entry = calloc(1, sizeof(**entry));
41 if (*entry == NULL)
42 return got_error(GOT_ERR_NO_MEM);
44 (*entry)->path = strdup(relpath);
45 if ((*entry)->path == NULL) {
46 free(*entry);
47 *entry = NULL;
48 return got_error(GOT_ERR_NO_MEM);
49 }
51 (*entry)->ctime_sec = sb.st_ctime;
52 (*entry)->ctime_nsec = sb.st_ctimensec;
53 (*entry)->mtime_sec = sb.st_mtime;
54 (*entry)->mtime_nsec = sb.st_mtimensec;
55 (*entry)->uid = sb.st_uid;
56 (*entry)->gid = sb.st_gid;
57 (*entry)->size = (sb.st_size & 0xffffffff);
58 if (sb.st_mode & S_IFLNK)
59 (*entry)->mode = GOT_INDEX_ENTRY_MODE_SYMLINK;
60 else
61 (*entry)->mode = GOT_INDEX_ENTRY_MODE_REGULAR_FILE;
62 (*entry)->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
63 GOT_INDEX_ENTRY_MODE_PERMS_SHIFT);
64 memcpy((*entry)->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
65 len = strlen(relpath);
66 if (len > GOT_INDEX_ENTRY_F_PATH_LEN)
67 len = GOT_INDEX_ENTRY_F_PATH_LEN;
68 (*entry)->flags |= len;
70 return NULL;
71 }
73 void
74 got_fileindex_entry_close(struct got_fileindex_entry *entry)
75 {
76 free(entry->path);
77 free(entry);
78 }
80 const struct got_error *
81 got_fileindex_entry_add(struct got_fileindex *fileindex,
82 struct got_fileindex_entry *entry)
83 {
84 /* TODO keep entries sorted by name */
85 TAILQ_INSERT_TAIL(&fileindex->entries, entry, entry);
86 fileindex->nentries++;
87 return NULL;
88 }
90 struct got_fileindex *
91 got_fileindex_open(void)
92 {
93 struct got_fileindex *fileindex;
95 fileindex = calloc(1, sizeof(*fileindex));
96 if (fileindex)
97 TAILQ_INIT(&fileindex->entries);
98 return fileindex;
99 }
101 void
102 got_fileindex_close(struct got_fileindex *fileindex)
104 struct got_fileindex_entry *entry;
106 while (!TAILQ_EMPTY(&fileindex->entries)) {
107 entry = TAILQ_FIRST(&fileindex->entries);
108 TAILQ_REMOVE(&fileindex->entries, entry, entry);
109 got_fileindex_entry_close(entry);
110 fileindex->nentries--;
112 free(fileindex);
115 static const struct got_error *
116 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
118 uint8_t buf[sizeof(uint64_t)];
119 size_t n;
121 val = htobe64(val);
122 memcpy(buf, &val, sizeof(val));
123 SHA1Update(ctx, buf, sizeof(val));
124 n = fwrite(buf, 1, sizeof(val), outfile);
125 if (n != sizeof(val))
126 return got_ferror(outfile, GOT_ERR_IO);
127 return NULL;
130 static const struct got_error *
131 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
133 uint8_t buf[sizeof(uint32_t)];
134 size_t n;
136 val = htobe32(val);
137 memcpy(buf, &val, sizeof(val));
138 SHA1Update(ctx, buf, sizeof(val));
139 n = fwrite(buf, 1, sizeof(val), outfile);
140 if (n != sizeof(val))
141 return got_ferror(outfile, GOT_ERR_IO);
142 return NULL;
145 static const struct got_error *
146 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
148 uint8_t buf[sizeof(uint16_t)];
149 size_t n;
151 val = htobe16(val);
152 memcpy(buf, &val, sizeof(val));
153 SHA1Update(ctx, buf, sizeof(val));
154 n = fwrite(buf, 1, sizeof(val), outfile);
155 if (n != sizeof(val))
156 return got_ferror(outfile, GOT_ERR_IO);
157 return NULL;
160 static const struct got_error *
161 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
163 size_t n, len, pad;
164 static const uint8_t zero[8] = { 0 };
166 len = strlen(path);
167 pad = (len % 8);
169 SHA1Update(ctx, path, len);
170 n = fwrite(path, 1, len, outfile);
171 if (n != len)
172 return got_ferror(outfile, GOT_ERR_IO);
173 if (pad == 0)
174 return NULL;
175 SHA1Update(ctx, zero, pad);
176 n = fwrite(zero, 1, pad, outfile);
177 if (n != pad)
178 return got_ferror(outfile, GOT_ERR_IO);
179 return NULL;
182 static const struct got_error *
183 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
184 FILE *outfile)
186 const struct got_error *err;
187 size_t n;
189 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
190 if (err)
191 return err;
192 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
193 if (err)
194 return err;
195 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
196 if (err)
197 return err;
198 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
199 if (err)
200 return err;
202 err = write_fileindex_val32(ctx, entry->uid, outfile);
203 if (err)
204 return err;
205 err = write_fileindex_val32(ctx, entry->gid, outfile);
206 if (err)
207 return err;
208 err = write_fileindex_val32(ctx, entry->size, outfile);
209 if (err)
210 return err;
212 err = write_fileindex_val16(ctx, entry->mode, outfile);
213 if (err)
214 return err;
216 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
217 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
218 if (n != SHA1_DIGEST_LENGTH)
219 return got_ferror(outfile, GOT_ERR_IO);
221 err = write_fileindex_val32(ctx, entry->flags, outfile);
222 if (err)
223 return err;
225 err = write_fileindex_path(ctx, entry->path, outfile);
226 return err;
229 const struct got_error *
230 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
232 struct got_fileindex_hdr hdr;
233 struct got_fileindex_entry *entry;
234 SHA1_CTX ctx;
235 uint8_t sha1[SHA1_DIGEST_LENGTH];
236 size_t n;
237 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
238 sizeof(hdr.nentries);
239 uint8_t buf[len];
241 SHA1Init(&ctx);
243 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
244 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
245 hdr.nentries = htobe32(fileindex->nentries);
247 memcpy(buf, &hdr, len);
248 SHA1Update(&ctx, buf, len);
249 n = fwrite(buf, 1, len, outfile);
250 if (n != len)
251 return got_ferror(outfile, GOT_ERR_IO);
253 TAILQ_FOREACH(entry, &fileindex->entries, entry) {
254 const struct got_error *err;
255 err = write_fileindex_entry(&ctx, entry, outfile);
256 if (err)
257 return err;
260 SHA1Final(sha1, &ctx);
261 n = fwrite(sha1, 1, sizeof(sha1), outfile);
262 if (n != sizeof(sha1))
263 return got_ferror(outfile, GOT_ERR_IO);
265 return NULL;