2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 c48c4a9c 2018-03-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 c48c4a9c 2018-03-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 c48c4a9c 2018-03-11 stsp * copyright notice and this permission notice appear in all copies.
8 c48c4a9c 2018-03-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c48c4a9c 2018-03-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c48c4a9c 2018-03-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c48c4a9c 2018-03-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c48c4a9c 2018-03-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c48c4a9c 2018-03-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c48c4a9c 2018-03-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 c48c4a9c 2018-03-11 stsp #include <sys/queue.h>
18 133d2798 2019-01-08 stsp #include <sys/tree.h>
19 c48c4a9c 2018-03-11 stsp #include <sys/stat.h>
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 c48c4a9c 2018-03-11 stsp #include <stdio.h>
23 c48c4a9c 2018-03-11 stsp #include <stdlib.h>
24 c48c4a9c 2018-03-11 stsp #include <string.h>
25 c48c4a9c 2018-03-11 stsp #include <sha1.h>
26 c34b20a2 2018-03-12 stsp #include <endian.h>
27 133d2798 2019-01-08 stsp #include <limits.h>
28 c442a90d 2019-03-10 stsp #include <uuid.h>
30 c48c4a9c 2018-03-11 stsp #include "got_error.h"
31 8da9e5f4 2019-01-12 stsp #include "got_object.h"
33 133d2798 2019-01-08 stsp #include "got_lib_path.h"
34 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
35 b25ae4fa 2019-02-04 stsp #include "got_lib_worktree.h"
37 133d2798 2019-01-08 stsp struct got_fileindex {
38 133d2798 2019-01-08 stsp struct got_fileindex_tree entries;
39 133d2798 2019-01-08 stsp int nentries;
40 133d2798 2019-01-08 stsp #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
43 c48c4a9c 2018-03-11 stsp const struct got_error *
44 51514078 2018-12-25 stsp got_fileindex_entry_update(struct got_fileindex_entry *entry,
45 02c07007 2019-02-10 stsp const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
46 02c07007 2019-02-10 stsp int update_timestamps)
48 c48c4a9c 2018-03-11 stsp struct stat sb;
50 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) != 0)
51 c48c4a9c 2018-03-11 stsp return got_error_from_errno();
53 02c07007 2019-02-10 stsp if (update_timestamps) {
54 02c07007 2019-02-10 stsp entry->ctime_sec = sb.st_ctime;
55 02c07007 2019-02-10 stsp entry->ctime_nsec = sb.st_ctimensec;
56 02c07007 2019-02-10 stsp entry->mtime_sec = sb.st_mtime;
57 02c07007 2019-02-10 stsp entry->mtime_nsec = sb.st_mtimensec;
59 51514078 2018-12-25 stsp entry->uid = sb.st_uid;
60 51514078 2018-12-25 stsp entry->gid = sb.st_gid;
61 51514078 2018-12-25 stsp entry->size = (sb.st_size & 0xffffffff);
62 51514078 2018-12-25 stsp if (sb.st_mode & S_IFLNK)
63 a7f9d64d 2019-01-13 stsp entry->mode = GOT_FILEIDX_MODE_SYMLINK;
65 a7f9d64d 2019-01-13 stsp entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
66 51514078 2018-12-25 stsp entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
67 a7f9d64d 2019-01-13 stsp GOT_FILEIDX_MODE_PERMS_SHIFT);
68 51514078 2018-12-25 stsp memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
69 51514078 2018-12-25 stsp memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
71 51514078 2018-12-25 stsp return NULL;
74 51514078 2018-12-25 stsp const struct got_error *
75 51514078 2018-12-25 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
76 51514078 2018-12-25 stsp const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
77 51514078 2018-12-25 stsp uint8_t *commit_sha1)
81 c48c4a9c 2018-03-11 stsp *entry = calloc(1, sizeof(**entry));
82 c48c4a9c 2018-03-11 stsp if (*entry == NULL)
83 0a585a0d 2018-03-17 stsp return got_error_from_errno();
85 c34b20a2 2018-03-12 stsp (*entry)->path = strdup(relpath);
86 c48c4a9c 2018-03-11 stsp if ((*entry)->path == NULL) {
87 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
88 c48c4a9c 2018-03-11 stsp free(*entry);
89 c48c4a9c 2018-03-11 stsp *entry = NULL;
93 c34b20a2 2018-03-12 stsp len = strlen(relpath);
94 a7f9d64d 2019-01-13 stsp if (len > GOT_FILEIDX_F_PATH_LEN)
95 a7f9d64d 2019-01-13 stsp len = GOT_FILEIDX_F_PATH_LEN;
96 c48c4a9c 2018-03-11 stsp (*entry)->flags |= len;
98 51514078 2018-12-25 stsp return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
99 02c07007 2019-02-10 stsp commit_sha1, 1);
103 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(struct got_fileindex_entry *entry)
105 c48c4a9c 2018-03-11 stsp free(entry->path);
106 c48c4a9c 2018-03-11 stsp free(entry);
109 50952927 2019-01-12 stsp static const struct got_error *
110 50952927 2019-01-12 stsp add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
112 133d2798 2019-01-08 stsp if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
113 133d2798 2019-01-08 stsp return got_error(GOT_ERR_NO_SPACE);
115 133d2798 2019-01-08 stsp RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
116 133d2798 2019-01-08 stsp fileindex->nentries++;
117 133d2798 2019-01-08 stsp return NULL;
120 50952927 2019-01-12 stsp const struct got_error *
121 50952927 2019-01-12 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
122 50952927 2019-01-12 stsp struct got_fileindex_entry *entry)
124 50952927 2019-01-12 stsp /* Flag this entry until it gets written out to disk. */
125 e288864f 2019-03-26 stsp entry->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
127 50952927 2019-01-12 stsp return add_entry(fileindex, entry);
131 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
132 512f0d0e 2019-01-02 stsp struct got_fileindex_entry *entry)
134 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
135 133d2798 2019-01-08 stsp fileindex->nentries--;
138 51514078 2018-12-25 stsp struct got_fileindex_entry *
139 51514078 2018-12-25 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
141 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
142 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
143 133d2798 2019-01-08 stsp key.path = (char *)path;
144 133d2798 2019-01-08 stsp return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
147 512f0d0e 2019-01-02 stsp const struct got_error *
148 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
149 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
151 133d2798 2019-01-08 stsp const struct got_error *err;
152 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry, *tmp;
154 133d2798 2019-01-08 stsp RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
155 133d2798 2019-01-08 stsp err = (*cb)(cb_arg, entry);
157 133d2798 2019-01-08 stsp return err;
159 b504a804 2019-01-08 stsp return NULL;
162 133d2798 2019-01-08 stsp struct got_fileindex *
163 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
165 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
167 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
168 133d2798 2019-01-08 stsp if (fileindex == NULL)
169 133d2798 2019-01-08 stsp return NULL;
171 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
172 133d2798 2019-01-08 stsp return fileindex;
176 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
178 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
180 133d2798 2019-01-08 stsp while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
181 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
182 133d2798 2019-01-08 stsp got_fileindex_entry_free(entry);
184 9d31a1d8 2018-03-11 stsp free(fileindex);
187 c34b20a2 2018-03-12 stsp static const struct got_error *
188 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
192 c34b20a2 2018-03-12 stsp val = htobe64(val);
193 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
194 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
195 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
196 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
197 c34b20a2 2018-03-12 stsp return NULL;
200 c34b20a2 2018-03-12 stsp static const struct got_error *
201 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
205 c34b20a2 2018-03-12 stsp val = htobe32(val);
206 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
207 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
208 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
209 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
210 c34b20a2 2018-03-12 stsp return NULL;
213 c34b20a2 2018-03-12 stsp static const struct got_error *
214 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
218 c34b20a2 2018-03-12 stsp val = htobe16(val);
219 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
220 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
221 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
222 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
223 c34b20a2 2018-03-12 stsp return NULL;
226 c34b20a2 2018-03-12 stsp static const struct got_error *
227 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
229 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
230 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
232 c34b20a2 2018-03-12 stsp len = strlen(path);
233 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
235 3c5b70f2 2018-12-27 stsp if (pad == 0)
236 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
238 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
239 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
240 c34b20a2 2018-03-12 stsp if (n != len)
241 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
242 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
243 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
244 c34b20a2 2018-03-12 stsp if (n != pad)
245 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
246 c34b20a2 2018-03-12 stsp return NULL;
249 c34b20a2 2018-03-12 stsp static const struct got_error *
250 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
251 c34b20a2 2018-03-12 stsp FILE *outfile)
253 c34b20a2 2018-03-12 stsp const struct got_error *err;
256 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
258 c34b20a2 2018-03-12 stsp return err;
259 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
261 c34b20a2 2018-03-12 stsp return err;
262 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
264 c34b20a2 2018-03-12 stsp return err;
265 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
267 c34b20a2 2018-03-12 stsp return err;
269 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
271 c34b20a2 2018-03-12 stsp return err;
272 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
274 c34b20a2 2018-03-12 stsp return err;
275 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
277 c34b20a2 2018-03-12 stsp return err;
279 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
281 c34b20a2 2018-03-12 stsp return err;
283 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
284 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
285 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
286 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
288 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
289 fc76cabb 2018-12-25 stsp n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
290 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
291 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
293 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
295 c34b20a2 2018-03-12 stsp return err;
297 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
298 c34b20a2 2018-03-12 stsp return err;
301 9d31a1d8 2018-03-11 stsp const struct got_error *
302 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
304 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
305 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
306 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
307 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
309 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
311 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
313 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
314 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
315 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
317 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
318 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
319 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
320 a5744d71 2019-01-12 stsp n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
321 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.signature))
322 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
323 a5744d71 2019-01-12 stsp n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
324 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.version))
325 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
326 a5744d71 2019-01-12 stsp n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
327 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.nentries))
328 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
330 133d2798 2019-01-08 stsp RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
331 e288864f 2019-03-26 stsp entry->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
332 133d2798 2019-01-08 stsp err = write_fileindex_entry(&ctx, entry, outfile);
334 133d2798 2019-01-08 stsp return err;
337 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
338 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
339 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
340 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
342 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
343 27d0e5bd 2019-01-12 stsp return got_error_from_errno();
345 52a74475 2018-12-24 stsp return NULL;
348 52a74475 2018-12-24 stsp static const struct got_error *
349 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
353 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
354 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
355 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
356 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
357 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
358 52a74475 2018-12-24 stsp return NULL;
361 52a74475 2018-12-24 stsp static const struct got_error *
362 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
366 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
367 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
368 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
369 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
370 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
371 52a74475 2018-12-24 stsp return NULL;
374 52a74475 2018-12-24 stsp static const struct got_error *
375 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
379 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
380 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
381 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
382 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
383 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
384 52a74475 2018-12-24 stsp return NULL;
387 52a74475 2018-12-24 stsp static const struct got_error *
388 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
390 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
391 52a74475 2018-12-24 stsp uint8_t buf[8];
392 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
394 52a74475 2018-12-24 stsp *path = malloc(totlen);
395 52a74475 2018-12-24 stsp if (*path == NULL)
396 52a74475 2018-12-24 stsp return got_error_from_errno();
399 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
400 52a74475 2018-12-24 stsp if (n != sizeof(buf))
401 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
402 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
403 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
404 52a74475 2018-12-24 stsp if (p == NULL) {
405 52a74475 2018-12-24 stsp err = got_error_from_errno();
408 52a74475 2018-12-24 stsp totlen += sizeof(buf);
411 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
412 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
413 52a74475 2018-12-24 stsp len += sizeof(buf);
414 60619907 2018-12-27 stsp } while (memchr(buf, '\0', sizeof(buf)) == NULL);
417 52a74475 2018-12-24 stsp free(*path);
418 52a74475 2018-12-24 stsp *path = NULL;
420 52a74475 2018-12-24 stsp return err;
423 52a74475 2018-12-24 stsp static const struct got_error *
424 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
425 52a74475 2018-12-24 stsp FILE *infile)
427 52a74475 2018-12-24 stsp const struct got_error *err;
428 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
431 52a74475 2018-12-24 stsp *entryp = NULL;
433 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
434 52a74475 2018-12-24 stsp if (entry == NULL)
435 52a74475 2018-12-24 stsp return got_error_from_errno();
437 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
440 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
443 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
446 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
450 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
453 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
456 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
460 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
464 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
465 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
466 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
469 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
471 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
472 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
473 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
476 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
478 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
482 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
485 52a74475 2018-12-24 stsp free(entry);
487 52a74475 2018-12-24 stsp *entryp = entry;
488 52a74475 2018-12-24 stsp return err;
491 52a74475 2018-12-24 stsp const struct got_error *
492 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
494 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
495 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
496 52a74475 2018-12-24 stsp SHA1_CTX ctx;
497 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
498 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
499 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
503 52a74475 2018-12-24 stsp SHA1Init(&ctx);
505 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
506 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
507 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
508 b6d05318 2019-01-13 stsp return NULL;
509 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
511 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
512 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
513 51514078 2018-12-25 stsp if (n == 0) /* EOF */
514 51514078 2018-12-25 stsp return NULL;
515 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
517 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
518 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
519 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
520 b6d05318 2019-01-13 stsp return NULL;
521 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
524 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
525 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
526 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
528 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
529 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
530 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
532 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
533 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
534 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
535 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
537 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
538 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
540 52a74475 2018-12-24 stsp return err;
541 50952927 2019-01-12 stsp err = add_entry(fileindex, entry);
543 52a74475 2018-12-24 stsp return err;
546 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
547 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
548 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
549 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
550 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
551 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
553 9d31a1d8 2018-03-11 stsp return NULL;
556 c2ac9456 2019-03-15 stsp static struct got_fileindex_entry *
557 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
559 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
561 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
563 50952927 2019-01-12 stsp /* Skip entries which were newly added by diff callbacks. */
564 e288864f 2019-03-26 stsp while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
565 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
567 50952927 2019-01-12 stsp return next;
570 8da9e5f4 2019-01-12 stsp static const struct got_error *
571 9d2a8e53 2019-01-28 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
572 9d2a8e53 2019-01-28 stsp struct got_tree_object *, const char *, struct got_repository *,
573 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *, void *);
575 9d2a8e53 2019-01-28 stsp static const struct got_error *
576 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
577 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_entry *te,
578 8da9e5f4 2019-01-12 stsp const char *path, struct got_repository *repo,
579 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
581 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
583 bd4792ec 2019-01-13 stsp if (S_ISDIR(te->mode)) {
584 8da9e5f4 2019-01-12 stsp char *subpath;
585 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
587 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
588 8da9e5f4 2019-01-12 stsp path[0] == '\0' ? "" : "/", te->name) == -1)
589 8da9e5f4 2019-01-12 stsp return got_error_from_errno();
591 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
593 8da9e5f4 2019-01-12 stsp free(subpath);
594 8da9e5f4 2019-01-12 stsp return err;
597 8da9e5f4 2019-01-12 stsp err = diff_fileindex_tree(fileindex, ie, subtree,
598 8da9e5f4 2019-01-12 stsp subpath, repo, cb, cb_arg);
599 8da9e5f4 2019-01-12 stsp free(subpath);
600 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
602 8da9e5f4 2019-01-12 stsp return err;
605 bd4792ec 2019-01-13 stsp *next = SIMPLEQ_NEXT(te, entry);
606 8da9e5f4 2019-01-12 stsp return NULL;
609 8da9e5f4 2019-01-12 stsp static const struct got_error *
610 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
611 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree,
612 8da9e5f4 2019-01-12 stsp const char *path, struct got_repository *repo,
613 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
615 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
616 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
617 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
618 8da9e5f4 2019-01-12 stsp const struct got_tree_entries *entries;
619 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
621 8da9e5f4 2019-01-12 stsp entries = got_object_tree_get_entries(tree);
622 8da9e5f4 2019-01-12 stsp te = SIMPLEQ_FIRST(&entries->head);
623 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
624 8da9e5f4 2019-01-12 stsp if (te && *ie) {
625 18831e78 2019-02-10 stsp char *te_path;
627 18831e78 2019-02-10 stsp if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
628 18831e78 2019-02-10 stsp err = got_error_from_errno();
631 18831e78 2019-02-10 stsp cmp = got_path_cmp((*ie)->path, te_path);
632 18831e78 2019-02-10 stsp free(te_path);
633 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
634 8da9e5f4 2019-01-12 stsp err = cb->diff_old_new(cb_arg, *ie, te,
638 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
639 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
640 8da9e5f4 2019-01-12 stsp path, repo, cb, cb_arg);
641 50952927 2019-01-12 stsp } else if (cmp < 0 ) {
642 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
643 8da9e5f4 2019-01-12 stsp err = cb->diff_old(cb_arg, *ie, path);
646 8da9e5f4 2019-01-12 stsp *ie = next;
648 8da9e5f4 2019-01-12 stsp err = cb->diff_new(cb_arg, te, path);
651 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
652 8da9e5f4 2019-01-12 stsp path, repo, cb, cb_arg);
656 8da9e5f4 2019-01-12 stsp } else if (*ie) {
657 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
658 8da9e5f4 2019-01-12 stsp err = cb->diff_old(cb_arg, *ie, path);
661 8da9e5f4 2019-01-12 stsp *ie = next;
662 8da9e5f4 2019-01-12 stsp } else if (te) {
663 8da9e5f4 2019-01-12 stsp err = cb->diff_new(cb_arg, te, path);
666 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
673 8da9e5f4 2019-01-12 stsp return err;
676 8da9e5f4 2019-01-12 stsp const struct got_error *
677 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
678 8da9e5f4 2019-01-12 stsp struct got_tree_object *tree, struct got_repository *repo,
679 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
681 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *min;
682 8da9e5f4 2019-01-12 stsp min = RB_MIN(got_fileindex_tree, &fileindex->entries);
683 8da9e5f4 2019-01-12 stsp return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
686 d1f6d47b 2019-02-04 stsp static const struct got_error *
687 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **, DIR *,
688 c7f4312f 2019-02-05 stsp const char *, const char *, struct got_repository *,
689 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *, void *);
691 d1f6d47b 2019-02-04 stsp static const struct got_error *
692 f5d3d7af 2019-02-05 stsp walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
693 f5d3d7af 2019-02-05 stsp struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
694 c7f4312f 2019-02-05 stsp const char *path, DIR *dir, const char *rootpath,
695 c7f4312f 2019-02-05 stsp struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
696 c7f4312f 2019-02-05 stsp void *cb_arg)
698 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
699 f5d3d7af 2019-02-05 stsp struct dirent *de = dle->data;
701 f5d3d7af 2019-02-05 stsp if (de->d_type == DT_DIR) {
702 d1f6d47b 2019-02-04 stsp char *subpath;
703 c7f4312f 2019-02-05 stsp char *subdirpath;
704 d1f6d47b 2019-02-04 stsp DIR *subdir;
706 d1f6d47b 2019-02-04 stsp if (asprintf(&subpath, "%s%s%s", path,
707 f5d3d7af 2019-02-05 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
708 d1f6d47b 2019-02-04 stsp return got_error_from_errno();
710 c7f4312f 2019-02-05 stsp if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
711 c7f4312f 2019-02-05 stsp free(subpath);
712 c7f4312f 2019-02-05 stsp return got_error_from_errno();
715 c7f4312f 2019-02-05 stsp subdir = opendir(subdirpath);
716 d1f6d47b 2019-02-04 stsp if (subdir == NULL) {
717 d1f6d47b 2019-02-04 stsp free(subpath);
718 c7f4312f 2019-02-05 stsp free(subdirpath);
719 d1f6d47b 2019-02-04 stsp return got_error_from_errno();
722 c7f4312f 2019-02-05 stsp err = diff_fileindex_dir(fileindex, ie, subdir, rootpath,
723 c7f4312f 2019-02-05 stsp subpath, repo, cb, cb_arg);
724 d1f6d47b 2019-02-04 stsp free(subpath);
725 c7f4312f 2019-02-05 stsp free(subdirpath);
726 d1f6d47b 2019-02-04 stsp closedir(subdir);
728 d1f6d47b 2019-02-04 stsp return err;
731 f5d3d7af 2019-02-05 stsp *next = TAILQ_NEXT(dle, entry);
732 d1f6d47b 2019-02-04 stsp return NULL;
735 d1f6d47b 2019-02-04 stsp static const struct got_error *
736 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *fileindex,
737 c7f4312f 2019-02-05 stsp struct got_fileindex_entry **ie, DIR *dir, const char *rootpath,
738 c7f4312f 2019-02-05 stsp const char *path, struct got_repository *repo,
739 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
741 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
742 d1f6d47b 2019-02-04 stsp struct dirent *de = NULL;
743 d1f6d47b 2019-02-04 stsp size_t path_len = strlen(path);
744 d1f6d47b 2019-02-04 stsp struct got_fileindex_entry *next;
745 f5d3d7af 2019-02-05 stsp struct got_pathlist_head dirlist;
746 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *dle;
748 f5d3d7af 2019-02-05 stsp TAILQ_INIT(&dirlist);
750 500cd40f 2019-02-04 stsp while (1) {
751 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *new = NULL;
752 ed83bff7 2019-02-05 stsp struct dirent *dep = NULL;
754 ed83bff7 2019-02-05 stsp de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
755 ed83bff7 2019-02-05 stsp if (de == NULL) {
756 ed83bff7 2019-02-05 stsp err = got_error_from_errno();
760 ed83bff7 2019-02-05 stsp if (readdir_r(dir, de, &dep) != 0) {
761 ed83bff7 2019-02-05 stsp err = got_error_from_errno();
765 ed83bff7 2019-02-05 stsp if (dep == NULL) {
770 b1ec3986 2019-02-04 stsp if (strcmp(de->d_name, ".") == 0 ||
771 b25ae4fa 2019-02-04 stsp strcmp(de->d_name, "..") == 0 ||
772 b25ae4fa 2019-02-04 stsp (path[0] == '\0' &&
773 ed83bff7 2019-02-05 stsp strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
778 f5d3d7af 2019-02-05 stsp err = got_pathlist_insert(&new, &dirlist, de->d_name, de);
781 f5d3d7af 2019-02-05 stsp if (new == NULL) {
782 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_DIR_DUP_ENTRY);
787 f5d3d7af 2019-02-05 stsp dle = TAILQ_FIRST(&dirlist);
788 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
789 500cd40f 2019-02-04 stsp if (dle && *ie) {
790 18831e78 2019-02-10 stsp char *de_path;
792 f5d3d7af 2019-02-05 stsp de = dle->data;
793 18831e78 2019-02-10 stsp if (asprintf(&de_path, "%s/%s", path,
794 18831e78 2019-02-10 stsp de->d_name) == -1) {
795 18831e78 2019-02-10 stsp err = got_error_from_errno();
798 18831e78 2019-02-10 stsp cmp = got_path_cmp((*ie)->path, de_path);
799 18831e78 2019-02-10 stsp free(de_path);
800 d1f6d47b 2019-02-04 stsp if (cmp == 0) {
801 f5d3d7af 2019-02-05 stsp err = cb->diff_old_new(cb_arg, *ie, de, path);
804 d1f6d47b 2019-02-04 stsp *ie = walk_fileindex(fileindex, *ie);
805 500cd40f 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
806 c7f4312f 2019-02-05 stsp dir, rootpath, repo, cb, cb_arg);
807 d1f6d47b 2019-02-04 stsp } else if (cmp < 0 ) {
808 d1f6d47b 2019-02-04 stsp next = walk_fileindex(fileindex, *ie);
809 d1f6d47b 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
812 d1f6d47b 2019-02-04 stsp *ie = next;
814 f5d3d7af 2019-02-05 stsp err = cb->diff_new(cb_arg, de, path);
817 500cd40f 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
818 c7f4312f 2019-02-05 stsp dir, rootpath, repo, cb, cb_arg);
822 554b91b1 2019-02-04 stsp } else if (*ie) {
823 554b91b1 2019-02-04 stsp next = walk_fileindex(fileindex, *ie);
824 554b91b1 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
827 554b91b1 2019-02-04 stsp *ie = next;
828 554b91b1 2019-02-04 stsp } else if (dle) {
829 763e1377 2019-02-05 stsp de = dle->data;
830 f5d3d7af 2019-02-05 stsp err = cb->diff_new(cb_arg, de, path);
833 554b91b1 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path, dir,
834 c7f4312f 2019-02-05 stsp rootpath, repo, cb, cb_arg);
840 ed83bff7 2019-02-05 stsp TAILQ_FOREACH(dle, &dirlist, entry)
841 ed83bff7 2019-02-05 stsp free(dle->data);
842 f5d3d7af 2019-02-05 stsp got_pathlist_free(&dirlist);
843 d1f6d47b 2019-02-04 stsp return err;
846 d1f6d47b 2019-02-04 stsp const struct got_error *
847 c7f4312f 2019-02-05 stsp got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
848 927df6b7 2019-02-10 stsp const char *rootpath, const char *path, struct got_repository *repo,
849 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
851 d1f6d47b 2019-02-04 stsp struct got_fileindex_entry *min;
852 d1f6d47b 2019-02-04 stsp min = RB_MIN(got_fileindex_tree, &fileindex->entries);
853 927df6b7 2019-02-10 stsp return diff_fileindex_dir(fileindex, &min, rootdir, rootpath, path,
854 c7f4312f 2019-02-05 stsp repo, cb, cb_arg);
857 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);