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_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)
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_from_errno();
44 (*entry)->path = strdup(relpath);
45 if ((*entry)->path == NULL) {
46 const struct got_error *err = got_error_from_errno();
47 free(*entry);
48 *entry = NULL;
49 return err;
50 }
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;
61 else
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;
71 return NULL;
72 }
74 void
75 got_fileindex_entry_close(struct got_fileindex_entry *entry)
76 {
77 free(entry->path);
78 free(entry);
79 }
81 const struct got_error *
82 got_fileindex_entry_add(struct got_fileindex *fileindex,
83 struct got_fileindex_entry *entry)
84 {
85 /* TODO keep entries sorted by name */
86 TAILQ_INSERT_TAIL(&fileindex->entries, entry, entry);
87 fileindex->nentries++;
88 return NULL;
89 }
91 struct got_fileindex *
92 got_fileindex_open(void)
93 {
94 struct got_fileindex *fileindex;
96 fileindex = calloc(1, sizeof(*fileindex));
97 if (fileindex)
98 TAILQ_INIT(&fileindex->entries);
99 return fileindex;
102 void
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--;
113 free(fileindex);
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)];
120 size_t n;
122 val = htobe64(val);
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);
128 return NULL;
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)];
135 size_t n;
137 val = htobe32(val);
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);
143 return NULL;
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)];
150 size_t n;
152 val = htobe16(val);
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);
158 return NULL;
161 static const struct got_error *
162 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
164 size_t n, len, pad;
165 static const uint8_t zero[8] = { 0 };
167 len = strlen(path);
168 pad = (len % 8);
170 SHA1Update(ctx, path, len);
171 n = fwrite(path, 1, len, outfile);
172 if (n != len)
173 return got_ferror(outfile, GOT_ERR_IO);
174 if (pad == 0)
175 return NULL;
176 SHA1Update(ctx, zero, pad);
177 n = fwrite(zero, 1, pad, outfile);
178 if (n != pad)
179 return got_ferror(outfile, GOT_ERR_IO);
180 return NULL;
183 static const struct got_error *
184 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
185 FILE *outfile)
187 const struct got_error *err;
188 size_t n;
190 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
191 if (err)
192 return err;
193 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
194 if (err)
195 return err;
196 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
197 if (err)
198 return err;
199 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
200 if (err)
201 return err;
203 err = write_fileindex_val32(ctx, entry->uid, outfile);
204 if (err)
205 return err;
206 err = write_fileindex_val32(ctx, entry->gid, outfile);
207 if (err)
208 return err;
209 err = write_fileindex_val32(ctx, entry->size, outfile);
210 if (err)
211 return err;
213 err = write_fileindex_val16(ctx, entry->mode, outfile);
214 if (err)
215 return err;
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);
223 if (err)
224 return err;
226 err = write_fileindex_path(ctx, entry->path, outfile);
227 return err;
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;
235 SHA1_CTX ctx;
236 uint8_t sha1[SHA1_DIGEST_LENGTH];
237 size_t n;
238 const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
239 sizeof(hdr.nentries);
240 uint8_t buf[len];
242 SHA1Init(&ctx);
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);
251 if (n != len)
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);
257 if (err)
258 return err;
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);
266 return NULL;