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 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 c48c4a9c 2018-03-11 stsp #include <sys/stat.h>
22 03df25b3 2019-05-11 stsp #include <errno.h>
23 d1f6d47b 2019-02-04 stsp #include <dirent.h>
24 6fc93f37 2019-12-13 stsp #include <fcntl.h>
25 c48c4a9c 2018-03-11 stsp #include <stdio.h>
26 c48c4a9c 2018-03-11 stsp #include <stdlib.h>
27 fff2d774 2023-07-29 thomas #include <stdint.h>
28 c48c4a9c 2018-03-11 stsp #include <string.h>
29 133d2798 2019-01-08 stsp #include <limits.h>
30 6fc93f37 2019-12-13 stsp #include <unistd.h>
32 c48c4a9c 2018-03-11 stsp #include "got_error.h"
33 8da9e5f4 2019-01-12 stsp #include "got_object.h"
34 324d37e7 2019-05-11 stsp #include "got_path.h"
36 b16893ba 2023-02-24 thomas #include "got_lib_hash.h"
37 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
38 b25ae4fa 2019-02-04 stsp #include "got_lib_worktree.h"
40 eb983b4b 2019-03-26 stsp /* got_fileindex_entry flags */
41 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
42 83718700 2019-08-03 stsp #define GOT_FILEIDX_F_STAGE 0x0000f000
43 3cd04235 2019-08-03 stsp #define GOT_FILEIDX_F_STAGE_SHIFT 12
44 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
45 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NO_BLOB 0x00020000
46 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
47 2ec1f75b 2019-03-26 stsp #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
48 a46b9f33 2020-01-28 stsp #define GOT_FILEIDX_F_REMOVE_ON_FLUSH 0x00100000
49 a769b60b 2021-06-27 stsp #define GOT_FILEIDX_F_SKIPPED 0x00200000
51 133d2798 2019-01-08 stsp struct got_fileindex {
52 133d2798 2019-01-08 stsp struct got_fileindex_tree entries;
53 a46b9f33 2020-01-28 stsp int nentries; /* Does not include entries marked for removal. */
54 fff2d774 2023-07-29 thomas #define GOT_FILEIDX_MAX_ENTRIES INT32_MAX
58 0aeb8099 2020-07-23 stsp got_fileindex_entry_perms_get(struct got_fileindex_entry *ie)
60 4723f050 2020-07-23 stsp return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
61 4723f050 2020-07-23 stsp GOT_FILEIDX_MODE_PERMS_SHIFT);
65 cf34e6e7 2020-07-23 stsp fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
67 4723f050 2020-07-23 stsp ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
68 4723f050 2020-07-23 stsp ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
69 4723f050 2020-07-23 stsp GOT_FILEIDX_MODE_PERMS);
73 26a7fe28 2019-07-27 stsp got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
75 0aeb8099 2020-07-23 stsp mode_t perms = got_fileindex_entry_perms_get(ie);
76 4723f050 2020-07-23 stsp int type = got_fileindex_entry_filetype_get(ie);
77 4723f050 2020-07-23 stsp uint32_t ftype;
79 4723f050 2020-07-23 stsp if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
80 4723f050 2020-07-23 stsp type == GOT_FILEIDX_MODE_BAD_SYMLINK)
81 4723f050 2020-07-23 stsp ftype = S_IFREG;
83 4723f050 2020-07-23 stsp ftype = S_IFLNK;
85 4723f050 2020-07-23 stsp return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
88 c48c4a9c 2018-03-11 stsp const struct got_error *
89 3f762da0 2019-08-03 stsp got_fileindex_entry_update(struct got_fileindex_entry *ie,
90 437adc9d 2020-12-10 yzhong int wt_fd, const char *ondisk_path, uint8_t *blob_sha1,
91 437adc9d 2020-12-10 yzhong uint8_t *commit_sha1, int update_timestamps)
93 c48c4a9c 2018-03-11 stsp struct stat sb;
95 437adc9d 2020-12-10 yzhong if (fstatat(wt_fd, ondisk_path, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
96 b15816dd 2019-08-11 stsp if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
97 b15816dd 2019-08-11 stsp errno == ENOENT))
98 437adc9d 2020-12-10 yzhong return got_error_from_errno2("fstatat", ondisk_path);
99 aa9ad276 2020-07-25 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
101 03df25b3 2019-05-11 stsp if (sb.st_mode & S_IFDIR)
102 03df25b3 2019-05-11 stsp return got_error_set_errno(EISDIR, ondisk_path);
103 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
106 3f762da0 2019-08-03 stsp if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
107 13d9040b 2019-03-27 stsp if (update_timestamps) {
108 0823ffc2 2020-09-10 naddy ie->ctime_sec = sb.st_ctim.tv_sec;
109 0823ffc2 2020-09-10 naddy ie->ctime_nsec = sb.st_ctim.tv_nsec;
110 0823ffc2 2020-09-10 naddy ie->mtime_sec = sb.st_mtim.tv_sec;
111 0823ffc2 2020-09-10 naddy ie->mtime_nsec = sb.st_mtim.tv_nsec;
113 3f762da0 2019-08-03 stsp ie->uid = sb.st_uid;
114 3f762da0 2019-08-03 stsp ie->gid = sb.st_gid;
115 3f762da0 2019-08-03 stsp ie->size = (sb.st_size & 0xffffffff);
116 4723f050 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
117 4723f050 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
118 4723f050 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
119 cf34e6e7 2020-07-23 stsp fileindex_entry_perms_set(ie, 0);
121 4723f050 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
122 4723f050 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
123 cf34e6e7 2020-07-23 stsp fileindex_entry_perms_set(ie,
124 4723f050 2020-07-23 stsp sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
128 ddce0520 2019-03-26 stsp if (blob_sha1) {
129 0529f8df 2023-03-10 thomas memmove(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
130 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
132 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NO_BLOB;
134 ddce0520 2019-03-26 stsp if (commit_sha1) {
135 3f762da0 2019-08-03 stsp memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
136 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
138 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
140 51514078 2018-12-25 stsp return NULL;
144 3f762da0 2019-08-03 stsp got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
146 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
150 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(struct got_fileindex_entry *ie)
152 a769b60b 2021-06-27 stsp ie->flags |= GOT_FILEIDX_F_SKIPPED;
155 51514078 2018-12-25 stsp const struct got_error *
156 3f762da0 2019-08-03 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
157 3969253a 2020-03-07 stsp const char *relpath)
159 51514078 2018-12-25 stsp size_t len;
161 3f762da0 2019-08-03 stsp *ie = calloc(1, sizeof(**ie));
162 3f762da0 2019-08-03 stsp if (*ie == NULL)
163 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
165 3f762da0 2019-08-03 stsp (*ie)->path = strdup(relpath);
166 3f762da0 2019-08-03 stsp if ((*ie)->path == NULL) {
167 638f9024 2019-05-13 stsp const struct got_error *err = got_error_from_errno("strdup");
169 3f762da0 2019-08-03 stsp *ie = NULL;
170 0a585a0d 2018-03-17 stsp return err;
173 4d555405 2019-08-03 stsp len = strlen(relpath);
174 a7f9d64d 2019-01-13 stsp if (len > GOT_FILEIDX_F_PATH_LEN)
175 a7f9d64d 2019-01-13 stsp len = GOT_FILEIDX_F_PATH_LEN;
176 3f762da0 2019-08-03 stsp (*ie)->flags |= len;
178 3969253a 2020-03-07 stsp return NULL;
182 3f762da0 2019-08-03 stsp got_fileindex_entry_free(struct got_fileindex_entry *ie)
184 3f762da0 2019-08-03 stsp free(ie->path);
189 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
191 4d555405 2019-08-03 stsp return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
195 0cb83759 2019-08-03 stsp got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
197 df335242 2019-08-03 stsp return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
201 0cb83759 2019-08-03 stsp got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
203 0cb83759 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_STAGE;
204 0cb83759 2019-08-03 stsp ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
205 0cb83759 2019-08-03 stsp GOT_FILEIDX_F_STAGE);
209 2e1fa222 2020-07-23 stsp got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
211 f5f1f9c2 2020-07-23 stsp return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
215 2e1fa222 2020-07-23 stsp got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
217 6131ab45 2020-07-23 stsp ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
218 6131ab45 2020-07-23 stsp ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
222 48b4f239 2021-12-31 thomas got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie,
223 48b4f239 2021-12-31 thomas int type)
225 984c073d 2020-07-23 stsp ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
226 984c073d 2020-07-23 stsp ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
227 984c073d 2020-07-23 stsp GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
231 984c073d 2020-07-23 stsp got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
233 984c073d 2020-07-23 stsp return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
234 984c073d 2020-07-23 stsp GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
238 d00136be 2019-03-26 stsp got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
240 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
244 d00136be 2019-03-26 stsp got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
246 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
250 2ec1f75b 2019-03-26 stsp got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
252 2ec1f75b 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
256 a769b60b 2021-06-27 stsp got_fileindex_entry_was_skipped(struct got_fileindex_entry *ie)
258 a769b60b 2021-06-27 stsp return (ie->flags & GOT_FILEIDX_F_SKIPPED) != 0;
261 50952927 2019-01-12 stsp static const struct got_error *
262 3f762da0 2019-08-03 stsp add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
264 133d2798 2019-01-08 stsp if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
265 133d2798 2019-01-08 stsp return got_error(GOT_ERR_NO_SPACE);
267 9fad5d8c 2022-04-16 thomas if (RB_INSERT(got_fileindex_tree, &fileindex->entries, ie) != NULL)
268 9fad5d8c 2022-04-16 thomas return got_error_path(ie->path, GOT_ERR_FILEIDX_DUP_ENTRY);
270 133d2798 2019-01-08 stsp fileindex->nentries++;
271 133d2798 2019-01-08 stsp return NULL;
274 50952927 2019-01-12 stsp const struct got_error *
275 50952927 2019-01-12 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
276 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie)
278 50952927 2019-01-12 stsp /* Flag this entry until it gets written out to disk. */
279 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
281 3f762da0 2019-08-03 stsp return add_entry(fileindex, ie);
285 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
286 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie)
289 a46b9f33 2020-01-28 stsp * Removing an entry from the RB tree immediately breaks
290 a46b9f33 2020-01-28 stsp * in-progress iterations over file index entries.
291 8bd8568c 2020-04-24 stsp * So flag this entry for removal and remove it once the index
292 8bd8568c 2020-04-24 stsp * is written out to disk. Meanwhile, pretend this entry no longer
293 a46b9f33 2020-01-28 stsp * exists if we get queried for it again before then.
295 a46b9f33 2020-01-28 stsp ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
296 133d2798 2019-01-08 stsp fileindex->nentries--;
299 51514078 2018-12-25 stsp struct got_fileindex_entry *
300 d6c87207 2019-08-02 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
301 d6c87207 2019-08-02 stsp size_t path_len)
303 a46b9f33 2020-01-28 stsp struct got_fileindex_entry *ie;
304 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
305 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
306 133d2798 2019-01-08 stsp key.path = (char *)path;
307 4d555405 2019-08-03 stsp key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
308 a46b9f33 2020-01-28 stsp ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
309 a46b9f33 2020-01-28 stsp if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
310 a46b9f33 2020-01-28 stsp return NULL;
314 512f0d0e 2019-01-02 stsp const struct got_error *
315 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
316 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
318 133d2798 2019-01-08 stsp const struct got_error *err;
319 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie, *tmp;
321 3f762da0 2019-08-03 stsp RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
322 a46b9f33 2020-01-28 stsp if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
324 3f762da0 2019-08-03 stsp err = (*cb)(cb_arg, ie);
326 133d2798 2019-01-08 stsp return err;
328 b504a804 2019-01-08 stsp return NULL;
331 133d2798 2019-01-08 stsp struct got_fileindex *
332 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
334 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
336 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
337 133d2798 2019-01-08 stsp if (fileindex == NULL)
338 133d2798 2019-01-08 stsp return NULL;
340 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
341 133d2798 2019-01-08 stsp return fileindex;
345 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
347 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
349 3f762da0 2019-08-03 stsp while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
350 3f762da0 2019-08-03 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
351 3f762da0 2019-08-03 stsp got_fileindex_entry_free(ie);
353 9d31a1d8 2018-03-11 stsp free(fileindex);
356 c34b20a2 2018-03-12 stsp static const struct got_error *
357 b16893ba 2023-02-24 thomas write_fileindex_val64(struct got_hash *ctx, uint64_t val, FILE *outfile)
361 c34b20a2 2018-03-12 stsp val = htobe64(val);
362 b16893ba 2023-02-24 thomas got_hash_update(ctx, &val, sizeof(val));
363 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
364 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
365 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
366 c34b20a2 2018-03-12 stsp return NULL;
369 c34b20a2 2018-03-12 stsp static const struct got_error *
370 b16893ba 2023-02-24 thomas write_fileindex_val32(struct got_hash *ctx, uint32_t val, FILE *outfile)
374 c34b20a2 2018-03-12 stsp val = htobe32(val);
375 b16893ba 2023-02-24 thomas got_hash_update(ctx, &val, sizeof(val));
376 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
377 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
378 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
379 c34b20a2 2018-03-12 stsp return NULL;
382 c34b20a2 2018-03-12 stsp static const struct got_error *
383 b16893ba 2023-02-24 thomas write_fileindex_val16(struct got_hash *ctx, uint16_t val, FILE *outfile)
387 c34b20a2 2018-03-12 stsp val = htobe16(val);
388 b16893ba 2023-02-24 thomas got_hash_update(ctx, &val, sizeof(val));
389 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
390 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
391 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
392 c34b20a2 2018-03-12 stsp return NULL;
395 c34b20a2 2018-03-12 stsp static const struct got_error *
396 b16893ba 2023-02-24 thomas write_fileindex_path(struct got_hash *ctx, const char *path, FILE *outfile)
398 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
399 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
401 c34b20a2 2018-03-12 stsp len = strlen(path);
402 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
404 3c5b70f2 2018-12-27 stsp if (pad == 0)
405 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
407 b16893ba 2023-02-24 thomas got_hash_update(ctx, path, len);
408 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
409 c34b20a2 2018-03-12 stsp if (n != len)
410 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
411 b16893ba 2023-02-24 thomas got_hash_update(ctx, zero, pad);
412 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
413 c34b20a2 2018-03-12 stsp if (n != pad)
414 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
415 c34b20a2 2018-03-12 stsp return NULL;
418 c34b20a2 2018-03-12 stsp static const struct got_error *
419 b16893ba 2023-02-24 thomas write_fileindex_entry(struct got_hash *ctx, struct got_fileindex_entry *ie,
420 c34b20a2 2018-03-12 stsp FILE *outfile)
422 c34b20a2 2018-03-12 stsp const struct got_error *err;
424 df335242 2019-08-03 stsp uint32_t stage;
426 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
428 c34b20a2 2018-03-12 stsp return err;
429 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
431 c34b20a2 2018-03-12 stsp return err;
432 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
434 c34b20a2 2018-03-12 stsp return err;
435 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
437 c34b20a2 2018-03-12 stsp return err;
439 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->uid, outfile);
441 c34b20a2 2018-03-12 stsp return err;
442 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->gid, outfile);
444 c34b20a2 2018-03-12 stsp return err;
445 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->size, outfile);
447 c34b20a2 2018-03-12 stsp return err;
449 3f762da0 2019-08-03 stsp err = write_fileindex_val16(ctx, ie->mode, outfile);
451 c34b20a2 2018-03-12 stsp return err;
453 b16893ba 2023-02-24 thomas got_hash_update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
454 3f762da0 2019-08-03 stsp n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
455 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
456 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
458 b16893ba 2023-02-24 thomas got_hash_update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
459 3f762da0 2019-08-03 stsp n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
460 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
461 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
463 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->flags, outfile);
465 c34b20a2 2018-03-12 stsp return err;
467 3f762da0 2019-08-03 stsp err = write_fileindex_path(ctx, ie->path, outfile);
469 df335242 2019-08-03 stsp return err;
471 0cb83759 2019-08-03 stsp stage = got_fileindex_entry_stage_get(ie);
472 df335242 2019-08-03 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
473 df335242 2019-08-03 stsp stage == GOT_FILEIDX_STAGE_ADD) {
474 b16893ba 2023-02-24 thomas got_hash_update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
475 df335242 2019-08-03 stsp n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
477 df335242 2019-08-03 stsp if (n != SHA1_DIGEST_LENGTH)
478 df335242 2019-08-03 stsp return got_ferror(outfile, GOT_ERR_IO);
481 df335242 2019-08-03 stsp return NULL;
484 9d31a1d8 2018-03-11 stsp const struct got_error *
485 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
487 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
488 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
489 b16893ba 2023-02-24 thomas struct got_hash ctx;
490 b16893ba 2023-02-24 thomas uint8_t hash[GOT_HASH_DIGEST_MAXLEN];
492 8bd8568c 2020-04-24 stsp struct got_fileindex_entry *ie, *tmp;
494 b16893ba 2023-02-24 thomas got_hash_init(&ctx, GOT_HASH_SHA1);
496 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
497 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
498 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
500 b16893ba 2023-02-24 thomas got_hash_update(&ctx, &hdr.signature, sizeof(hdr.signature));
501 b16893ba 2023-02-24 thomas got_hash_update(&ctx, &hdr.version, sizeof(hdr.version));
502 b16893ba 2023-02-24 thomas got_hash_update(&ctx, &hdr.nentries, sizeof(hdr.nentries));
503 a5744d71 2019-01-12 stsp n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
504 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.signature))
505 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
506 a5744d71 2019-01-12 stsp n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
507 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.version))
508 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
509 a5744d71 2019-01-12 stsp n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
510 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.nentries))
511 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
513 8bd8568c 2020-04-24 stsp RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
514 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
515 a769b60b 2021-06-27 stsp ie->flags &= ~GOT_FILEIDX_F_SKIPPED;
516 8bd8568c 2020-04-24 stsp if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
517 8bd8568c 2020-04-24 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
518 8bd8568c 2020-04-24 stsp got_fileindex_entry_free(ie);
521 3f762da0 2019-08-03 stsp err = write_fileindex_entry(&ctx, ie, outfile);
523 133d2798 2019-01-08 stsp return err;
526 b16893ba 2023-02-24 thomas got_hash_final(&ctx, hash);
527 b16893ba 2023-02-24 thomas n = fwrite(hash, 1, SHA1_DIGEST_LENGTH, outfile);
528 b16893ba 2023-02-24 thomas if (n != SHA1_DIGEST_LENGTH)
529 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
531 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
532 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
534 52a74475 2018-12-24 stsp return NULL;
537 52a74475 2018-12-24 stsp static const struct got_error *
538 b16893ba 2023-02-24 thomas read_fileindex_val64(uint64_t *val, struct got_hash *ctx, FILE *infile)
542 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
543 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
544 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
545 b16893ba 2023-02-24 thomas got_hash_update(ctx, val, sizeof(*val));
546 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
547 52a74475 2018-12-24 stsp return NULL;
550 52a74475 2018-12-24 stsp static const struct got_error *
551 b16893ba 2023-02-24 thomas read_fileindex_val32(uint32_t *val, struct got_hash *ctx, FILE *infile)
555 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
556 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
557 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
558 b16893ba 2023-02-24 thomas got_hash_update(ctx, val, sizeof(*val));
559 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
560 52a74475 2018-12-24 stsp return NULL;
563 52a74475 2018-12-24 stsp static const struct got_error *
564 b16893ba 2023-02-24 thomas read_fileindex_val16(uint16_t *val, struct got_hash *ctx, FILE *infile)
568 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
569 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
570 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
571 b16893ba 2023-02-24 thomas got_hash_update(ctx, val, sizeof(*val));
572 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
573 52a74475 2018-12-24 stsp return NULL;
576 52a74475 2018-12-24 stsp static const struct got_error *
577 b16893ba 2023-02-24 thomas read_fileindex_path(char **path, struct got_hash *ctx, FILE *infile)
579 6bf2c316 2019-08-02 stsp const size_t chunk_size = 8;
580 dcc9ed63 2023-07-29 thomas char p[PATH_MAX];
581 dcc9ed63 2023-07-29 thomas size_t n, len = 0;
584 dcc9ed63 2023-07-29 thomas if (len + chunk_size > sizeof(p))
585 dcc9ed63 2023-07-29 thomas return got_error(GOT_ERR_FILEIDX_BAD);
587 dcc9ed63 2023-07-29 thomas n = fread(&p[len], 1, chunk_size, infile);
588 dcc9ed63 2023-07-29 thomas if (n != chunk_size)
589 dcc9ed63 2023-07-29 thomas return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
591 dcc9ed63 2023-07-29 thomas got_hash_update(ctx, &p[len], chunk_size);
592 6bf2c316 2019-08-02 stsp len += chunk_size;
593 dcc9ed63 2023-07-29 thomas } while (memchr(&p[len - chunk_size], '\0', chunk_size) == NULL);
595 dcc9ed63 2023-07-29 thomas *path = strdup(p);
596 dcc9ed63 2023-07-29 thomas if (*path == NULL)
597 dcc9ed63 2023-07-29 thomas return got_error_from_errno("strdup");
598 dcc9ed63 2023-07-29 thomas return NULL;
601 52a74475 2018-12-24 stsp static const struct got_error *
602 b16893ba 2023-02-24 thomas read_fileindex_entry(struct got_fileindex_entry **iep, struct got_hash *ctx,
603 df335242 2019-08-03 stsp FILE *infile, uint32_t version)
605 52a74475 2018-12-24 stsp const struct got_error *err;
606 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
609 3f762da0 2019-08-03 stsp *iep = NULL;
611 3f762da0 2019-08-03 stsp ie = calloc(1, sizeof(*ie));
612 3f762da0 2019-08-03 stsp if (ie == NULL)
613 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
615 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
618 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
621 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
624 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
628 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->uid, ctx, infile);
631 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->gid, ctx, infile);
634 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->size, ctx, infile);
638 3f762da0 2019-08-03 stsp err = read_fileindex_val16(&ie->mode, ctx, infile);
642 3f762da0 2019-08-03 stsp n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
643 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
644 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
647 b16893ba 2023-02-24 thomas got_hash_update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
649 3f762da0 2019-08-03 stsp n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
650 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
651 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
654 b16893ba 2023-02-24 thomas got_hash_update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
656 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->flags, ctx, infile);
660 3f762da0 2019-08-03 stsp err = read_fileindex_path(&ie->path, ctx, infile);
664 df335242 2019-08-03 stsp if (version >= 2) {
665 0cb83759 2019-08-03 stsp uint32_t stage = got_fileindex_entry_stage_get(ie);
666 df335242 2019-08-03 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
667 df335242 2019-08-03 stsp stage == GOT_FILEIDX_STAGE_ADD) {
668 df335242 2019-08-03 stsp n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
670 df335242 2019-08-03 stsp if (n != SHA1_DIGEST_LENGTH) {
671 df335242 2019-08-03 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
674 b16893ba 2023-02-24 thomas got_hash_update(ctx, ie->staged_blob_sha1,
675 b16893ba 2023-02-24 thomas SHA1_DIGEST_LENGTH);
678 df335242 2019-08-03 stsp /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
679 df335242 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_STAGE;
684 3f762da0 2019-08-03 stsp got_fileindex_entry_free(ie);
687 52a74475 2018-12-24 stsp return err;
690 52a74475 2018-12-24 stsp const struct got_error *
691 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
693 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
694 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
695 b16893ba 2023-02-24 thomas struct got_hash ctx;
696 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
697 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
698 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
702 b16893ba 2023-02-24 thomas got_hash_init(&ctx, GOT_HASH_SHA1);
704 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
705 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
706 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
707 b6d05318 2019-01-13 stsp return NULL;
708 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
710 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
711 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
712 51514078 2018-12-25 stsp if (n == 0) /* EOF */
713 51514078 2018-12-25 stsp return NULL;
714 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
716 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
717 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
718 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
719 b6d05318 2019-01-13 stsp return NULL;
720 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
723 b16893ba 2023-02-24 thomas got_hash_update(&ctx, &hdr.signature, sizeof(hdr.signature));
724 b16893ba 2023-02-24 thomas got_hash_update(&ctx, &hdr.version, sizeof(hdr.version));
725 b16893ba 2023-02-24 thomas got_hash_update(&ctx, &hdr.nentries, sizeof(hdr.nentries));
727 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
728 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
729 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
731 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
732 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
733 df335242 2019-08-03 stsp if (hdr.version > GOT_FILE_INDEX_VERSION)
734 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
736 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
737 df335242 2019-08-03 stsp err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
739 52a74475 2018-12-24 stsp return err;
740 3f762da0 2019-08-03 stsp err = add_entry(fileindex, ie);
741 991857c7 2023-04-14 thomas if (err) {
742 991857c7 2023-04-14 thomas got_fileindex_entry_free(ie);
743 52a74475 2018-12-24 stsp return err;
747 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
748 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
749 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
750 b16893ba 2023-02-24 thomas got_hash_final(&ctx, sha1);
751 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
752 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
754 9d31a1d8 2018-03-11 stsp return NULL;
757 c2ac9456 2019-03-15 stsp static struct got_fileindex_entry *
758 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
760 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
762 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
764 a46b9f33 2020-01-28 stsp /* Skip entries which were added or removed by diff callbacks. */
765 a46b9f33 2020-01-28 stsp while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
766 a46b9f33 2020-01-28 stsp GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
767 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
769 50952927 2019-01-12 stsp return next;
772 8da9e5f4 2019-01-12 stsp static const struct got_error *
773 56e0773d 2019-11-28 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
774 56e0773d 2019-11-28 stsp struct got_tree_object *tree, const char *, const char *,
775 c4cdcb68 2019-04-03 stsp struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
777 9d2a8e53 2019-01-28 stsp static const struct got_error *
778 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
779 56e0773d 2019-11-28 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
780 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
781 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
783 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
784 56e0773d 2019-11-28 stsp struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
786 56e0773d 2019-11-28 stsp if (!got_object_tree_entry_is_submodule(te) &&
787 56e0773d 2019-11-28 stsp S_ISDIR(got_tree_entry_get_mode(te))) {
788 8da9e5f4 2019-01-12 stsp char *subpath;
789 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
791 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
792 56e0773d 2019-11-28 stsp path[0] == '\0' ? "" : "/",
793 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1)
794 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
796 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo,
797 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
799 8da9e5f4 2019-01-12 stsp free(subpath);
800 8da9e5f4 2019-01-12 stsp return err;
803 56e0773d 2019-11-28 stsp err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
804 56e0773d 2019-11-28 stsp entry_name, repo, cb, cb_arg);
805 8da9e5f4 2019-01-12 stsp free(subpath);
806 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
808 8da9e5f4 2019-01-12 stsp return err;
812 56e0773d 2019-11-28 stsp *next = got_object_tree_get_entry(tree, *tidx);
813 8da9e5f4 2019-01-12 stsp return NULL;
816 8da9e5f4 2019-01-12 stsp static const struct got_error *
817 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
818 56e0773d 2019-11-28 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree,
819 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
820 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
822 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
823 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
824 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
825 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
826 56e0773d 2019-11-28 stsp int tidx = 0;
828 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, tidx);
829 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
830 8da9e5f4 2019-01-12 stsp if (te && *ie) {
831 18831e78 2019-02-10 stsp char *te_path;
832 56e0773d 2019-11-28 stsp const char *te_name = got_tree_entry_get_name(te);
834 56e0773d 2019-11-28 stsp if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
835 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
838 d572f586 2019-08-02 stsp cmp = got_path_cmp((*ie)->path, te_path,
839 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(*ie), strlen(te_path));
840 18831e78 2019-02-10 stsp free(te_path);
841 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
842 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
843 63c5ca5d 2019-08-24 stsp path_len) &&
844 63c5ca5d 2019-08-24 stsp !got_object_tree_entry_is_submodule(te) &&
845 63c5ca5d 2019-08-24 stsp (entry_name == NULL ||
846 56e0773d 2019-11-28 stsp strcmp(te_name, entry_name) == 0)) {
847 c4cdcb68 2019-04-03 stsp err = cb->diff_old_new(cb_arg, *ie, te,
849 c4cdcb68 2019-04-03 stsp if (err || entry_name)
852 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
853 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx,
854 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
855 c4cdcb68 2019-04-03 stsp } else if (cmp < 0) {
856 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
857 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
858 194cb7cb 2021-01-19 stsp path_len) && entry_name == NULL) {
859 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
860 c4cdcb68 2019-04-03 stsp if (err || entry_name)
863 8da9e5f4 2019-01-12 stsp *ie = next;
865 c4cdcb68 2019-04-03 stsp if ((entry_name == NULL ||
866 56e0773d 2019-11-28 stsp strcmp(te_name, entry_name) == 0)) {
867 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
868 c4cdcb68 2019-04-03 stsp if (err || entry_name)
871 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx,
872 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
876 8da9e5f4 2019-01-12 stsp } else if (*ie) {
877 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
878 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path, path_len) &&
879 c4cdcb68 2019-04-03 stsp (entry_name == NULL ||
880 56e0773d 2019-11-28 stsp (te && strcmp(got_tree_entry_get_name(te),
881 56e0773d 2019-11-28 stsp entry_name) == 0))) {
882 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
883 c4cdcb68 2019-04-03 stsp if (err || entry_name)
886 8da9e5f4 2019-01-12 stsp *ie = next;
887 8da9e5f4 2019-01-12 stsp } else if (te) {
888 63c5ca5d 2019-08-24 stsp if (!got_object_tree_entry_is_submodule(te) &&
889 63c5ca5d 2019-08-24 stsp (entry_name == NULL ||
890 56e0773d 2019-11-28 stsp strcmp(got_tree_entry_get_name(te), entry_name)
892 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
893 c4cdcb68 2019-04-03 stsp if (err || entry_name)
896 56e0773d 2019-11-28 stsp err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
897 c4cdcb68 2019-04-03 stsp entry_name, repo, cb, cb_arg);
903 8da9e5f4 2019-01-12 stsp return err;
906 8da9e5f4 2019-01-12 stsp const struct got_error *
907 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
908 c4cdcb68 2019-04-03 stsp struct got_tree_object *tree, const char *path, const char *entry_name,
909 c4cdcb68 2019-04-03 stsp struct got_repository *repo,
910 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
912 30a076bc 2019-07-27 stsp struct got_fileindex_entry *ie;
913 30a076bc 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
914 30a076bc 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
915 30a076bc 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
916 56e0773d 2019-11-28 stsp return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
917 30a076bc 2019-07-27 stsp cb, cb_arg);
920 d1f6d47b 2019-02-04 stsp static const struct got_error *
921 39beb6da 2019-07-27 stsp diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
922 6fc93f37 2019-12-13 stsp struct got_pathlist_head *, int, const char *, const char *,
923 c577a9ce 2019-07-27 stsp struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
925 c577a9ce 2019-07-27 stsp static const struct got_error *
926 965988c5 2019-12-16 stsp read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
928 c577a9ce 2019-07-27 stsp const struct got_error *err = NULL;
929 c577a9ce 2019-07-27 stsp struct got_pathlist_entry *new = NULL;
930 c577a9ce 2019-07-27 stsp struct dirent *dep = NULL;
931 c577a9ce 2019-07-27 stsp struct dirent *de = NULL;
934 c577a9ce 2019-07-27 stsp de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
935 c577a9ce 2019-07-27 stsp if (de == NULL) {
936 c577a9ce 2019-07-27 stsp err = got_error_from_errno("malloc");
940 c577a9ce 2019-07-27 stsp if (readdir_r(dir, de, &dep) != 0) {
941 c577a9ce 2019-07-27 stsp err = got_error_from_errno("readdir_r");
945 c577a9ce 2019-07-27 stsp if (dep == NULL) {
950 c577a9ce 2019-07-27 stsp if (strcmp(de->d_name, ".") == 0 ||
951 c577a9ce 2019-07-27 stsp strcmp(de->d_name, "..") == 0 ||
952 c577a9ce 2019-07-27 stsp (path[0] == '\0' &&
953 ad10f64e 2023-07-19 thomas strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0) ||
954 ad10f64e 2023-07-19 thomas (path[0] == '\0' &&
955 ad10f64e 2023-07-19 thomas strcmp(de->d_name, GOT_WORKTREE_CVG_DIR) == 0)) {
960 c577a9ce 2019-07-27 stsp err = got_pathlist_insert(&new, dirlist, de->d_name, de);
965 c577a9ce 2019-07-27 stsp if (new == NULL) {
966 c577a9ce 2019-07-27 stsp err = got_error(GOT_ERR_DIR_DUP_ENTRY);
972 c577a9ce 2019-07-27 stsp return err;
975 8528beef 2021-10-15 thomas static int
976 8528beef 2021-10-15 thomas have_tracked_file_in_dir(struct got_fileindex *fileindex, const char *path)
978 8528beef 2021-10-15 thomas struct got_fileindex_entry *ie;
979 8528beef 2021-10-15 thomas size_t path_len = strlen(path);
982 8528beef 2021-10-15 thomas ie = RB_ROOT(&fileindex->entries);
983 8528beef 2021-10-15 thomas while (ie) {
984 8528beef 2021-10-15 thomas if (got_path_is_child(ie->path, path, path_len))
985 8528beef 2021-10-15 thomas return 1;
986 8528beef 2021-10-15 thomas cmp = got_path_cmp(path, ie->path, path_len,
987 8528beef 2021-10-15 thomas got_fileindex_entry_path_len(ie));
988 8528beef 2021-10-15 thomas if (cmp < 0)
989 8528beef 2021-10-15 thomas ie = RB_LEFT(ie, entry);
990 8528beef 2021-10-15 thomas else if (cmp > 0)
991 8528beef 2021-10-15 thomas ie = RB_RIGHT(ie, entry);
996 8528beef 2021-10-15 thomas return 0;
999 d1f6d47b 2019-02-04 stsp static const struct got_error *
1000 f5d3d7af 2019-02-05 stsp walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
1001 965988c5 2019-12-16 stsp struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
1002 39beb6da 2019-07-27 stsp const char *path, const char *rootpath, struct got_repository *repo,
1003 6092c299 2021-10-04 thomas int ignore, struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1005 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
1006 f5d3d7af 2019-02-05 stsp struct dirent *de = dle->data;
1007 965988c5 2019-12-16 stsp DIR *subdir = NULL;
1008 6fc93f37 2019-12-13 stsp int subdirfd = -1;
1010 573463cc 2019-04-06 stsp *next = NULL;
1012 8528beef 2021-10-15 thomas /* Must traverse ignored directories if they contain tracked files. */
1013 1f480907 2022-01-27 thomas if (de->d_type == DT_DIR && ignore &&
1014 8528beef 2021-10-15 thomas have_tracked_file_in_dir(fileindex, path))
1015 8528beef 2021-10-15 thomas ignore = 0;
1017 1f480907 2022-01-27 thomas if (de->d_type == DT_DIR && !ignore) {
1018 d1f6d47b 2019-02-04 stsp char *subpath;
1019 c7f4312f 2019-02-05 stsp char *subdirpath;
1020 c577a9ce 2019-07-27 stsp struct got_pathlist_head subdirlist;
1022 c577a9ce 2019-07-27 stsp TAILQ_INIT(&subdirlist);
1024 d1f6d47b 2019-02-04 stsp if (asprintf(&subpath, "%s%s%s", path,
1025 f5d3d7af 2019-02-05 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
1026 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1028 c7f4312f 2019-02-05 stsp if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1029 c7f4312f 2019-02-05 stsp free(subpath);
1030 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1033 965988c5 2019-12-16 stsp subdirfd = openat(fd, de->d_name,
1034 fc63f50d 2021-12-31 thomas O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1035 6fc93f37 2019-12-13 stsp if (subdirfd == -1) {
1036 40b289d7 2019-09-07 stsp if (errno == EACCES) {
1037 40b289d7 2019-09-07 stsp *next = TAILQ_NEXT(dle, entry);
1038 40b289d7 2019-09-07 stsp return NULL;
1040 965988c5 2019-12-16 stsp err = got_error_from_errno2("openat", subdirpath);
1041 d1f6d47b 2019-02-04 stsp free(subpath);
1042 c7f4312f 2019-02-05 stsp free(subdirpath);
1043 ef5e02fd 2019-08-11 stsp return err;
1046 965988c5 2019-12-16 stsp subdir = fdopendir(subdirfd);
1047 815470d4 2024-01-31 thomas if (subdir == NULL) {
1048 815470d4 2024-01-31 thomas err = got_error_from_errno2("fdopendir", path);
1049 815470d4 2024-01-31 thomas close(subdirfd);
1050 815470d4 2024-01-31 thomas free(subpath);
1051 815470d4 2024-01-31 thomas free(subdirpath);
1052 815470d4 2024-01-31 thomas return err;
1054 965988c5 2019-12-16 stsp subdirfd = -1;
1055 965988c5 2019-12-16 stsp err = read_dirlist(&subdirlist, subdir, subdirpath);
1056 c577a9ce 2019-07-27 stsp if (err) {
1057 c577a9ce 2019-07-27 stsp free(subpath);
1058 c577a9ce 2019-07-27 stsp free(subdirpath);
1059 965988c5 2019-12-16 stsp closedir(subdir);
1060 c577a9ce 2019-07-27 stsp return err;
1062 965988c5 2019-12-16 stsp err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1063 965988c5 2019-12-16 stsp dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1064 965988c5 2019-12-16 stsp if (subdir && closedir(subdir) == -1 && err == NULL)
1065 965988c5 2019-12-16 stsp err = got_error_from_errno2("closedir", subdirpath);
1066 d1f6d47b 2019-02-04 stsp free(subpath);
1067 c7f4312f 2019-02-05 stsp free(subdirpath);
1068 21c2d8be 2023-01-10 thomas got_pathlist_free(&subdirlist, GOT_PATHLIST_FREE_DATA);
1070 d1f6d47b 2019-02-04 stsp return err;
1073 f5d3d7af 2019-02-05 stsp *next = TAILQ_NEXT(dle, entry);
1074 d1f6d47b 2019-02-04 stsp return NULL;
1077 d1f6d47b 2019-02-04 stsp static const struct got_error *
1078 1f480907 2022-01-27 thomas dirent_type_fixup(struct dirent *de, const char *rootpath, const char *path)
1080 1f480907 2022-01-27 thomas const struct got_error *err;
1081 1f480907 2022-01-27 thomas char *dir_path;
1082 1f480907 2022-01-27 thomas int type;
1084 1f480907 2022-01-27 thomas if (de->d_type != DT_UNKNOWN)
1085 1f480907 2022-01-27 thomas return NULL;
1087 1f480907 2022-01-27 thomas /* DT_UNKNOWN occurs on NFS mounts without "readdir plus" RPC. */
1088 1f480907 2022-01-27 thomas if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
1089 1f480907 2022-01-27 thomas return got_error_from_errno("asprintf");
1090 1f480907 2022-01-27 thomas err = got_path_dirent_type(&type, dir_path, de);
1091 1f480907 2022-01-27 thomas free(dir_path);
1092 1f480907 2022-01-27 thomas if (err)
1093 1f480907 2022-01-27 thomas return err;
1095 1f480907 2022-01-27 thomas de->d_type = type;
1096 1f480907 2022-01-27 thomas return NULL;
1099 1f480907 2022-01-27 thomas static const struct got_error *
1100 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *fileindex,
1101 39beb6da 2019-07-27 stsp struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1102 6fc93f37 2019-12-13 stsp int dirfd, const char *rootpath, const char *path,
1103 6fc93f37 2019-12-13 stsp struct got_repository *repo,
1104 39beb6da 2019-07-27 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1106 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
1107 d1f6d47b 2019-02-04 stsp struct dirent *de = NULL;
1108 d1f6d47b 2019-02-04 stsp size_t path_len = strlen(path);
1109 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *dle;
1110 6092c299 2021-10-04 thomas int ignore;
1112 3143d852 2020-06-25 stsp if (cb->diff_traverse) {
1113 3143d852 2020-06-25 stsp err = cb->diff_traverse(cb_arg, path, dirfd);
1115 3143d852 2020-06-25 stsp return err;
1118 c577a9ce 2019-07-27 stsp dle = TAILQ_FIRST(dirlist);
1119 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1120 500cd40f 2019-02-04 stsp if (dle && *ie) {
1121 18831e78 2019-02-10 stsp char *de_path;
1123 f5d3d7af 2019-02-05 stsp de = dle->data;
1124 1f480907 2022-01-27 thomas err = dirent_type_fixup(de, rootpath, path);
1125 1f480907 2022-01-27 thomas if (err)
1127 18831e78 2019-02-10 stsp if (asprintf(&de_path, "%s/%s", path,
1128 18831e78 2019-02-10 stsp de->d_name) == -1) {
1129 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1132 d572f586 2019-08-02 stsp cmp = got_path_cmp((*ie)->path, de_path,
1133 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(*ie),
1134 dd038bc6 2021-09-21 thomas.ad strlen(path) + 1 + strlen(de->d_name));
1135 18831e78 2019-02-10 stsp free(de_path);
1136 d1f6d47b 2019-02-04 stsp if (cmp == 0) {
1137 7f91a133 2019-12-13 stsp err = cb->diff_old_new(cb_arg, *ie, de, path,
1141 d1f6d47b 2019-02-04 stsp *ie = walk_fileindex(fileindex, *ie);
1142 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1143 6092c299 2021-10-04 thomas path, rootpath, repo, 0, cb, cb_arg);
1144 d1f6d47b 2019-02-04 stsp } else if (cmp < 0 ) {
1145 d1f6d47b 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
1148 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
1150 6092c299 2021-10-04 thomas err = cb->diff_new(&ignore, cb_arg, de, path,
1154 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1155 6092c299 2021-10-04 thomas path, rootpath, repo, ignore, cb, cb_arg);
1159 554b91b1 2019-02-04 stsp } else if (*ie) {
1160 554b91b1 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
1163 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
1164 554b91b1 2019-02-04 stsp } else if (dle) {
1165 763e1377 2019-02-05 stsp de = dle->data;
1166 1f480907 2022-01-27 thomas err = dirent_type_fixup(de, rootpath, path);
1167 1f480907 2022-01-27 thomas if (err)
1169 6092c299 2021-10-04 thomas err = cb->diff_new(&ignore, cb_arg, de, path, dirfd);
1172 6fc93f37 2019-12-13 stsp err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1173 6092c299 2021-10-04 thomas rootpath, repo, ignore, cb, cb_arg);
1179 d1f6d47b 2019-02-04 stsp return err;
1182 d1f6d47b 2019-02-04 stsp const struct got_error *
1183 965988c5 2019-12-16 stsp got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1184 927df6b7 2019-02-10 stsp const char *rootpath, const char *path, struct got_repository *repo,
1185 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1187 c577a9ce 2019-07-27 stsp const struct got_error *err;
1188 c577a9ce 2019-07-27 stsp struct got_fileindex_entry *ie;
1189 c577a9ce 2019-07-27 stsp struct got_pathlist_head dirlist;
1193 c577a9ce 2019-07-27 stsp TAILQ_INIT(&dirlist);
1196 965988c5 2019-12-16 stsp * Duplicate the file descriptor so we can call closedir() below
1197 965988c5 2019-12-16 stsp * without closing the file descriptor passed in by our caller.
1199 965988c5 2019-12-16 stsp fd2 = dup(fd);
1200 965988c5 2019-12-16 stsp if (fd2 == -1)
1201 965988c5 2019-12-16 stsp return got_error_from_errno2("dup", path);
1202 3dcf3e74 2020-01-28 stsp if (lseek(fd2, 0, SEEK_SET) == -1) {
1203 3dcf3e74 2020-01-28 stsp err = got_error_from_errno2("lseek", path);
1204 3dcf3e74 2020-01-28 stsp close(fd2);
1205 3dcf3e74 2020-01-28 stsp return err;
1207 965988c5 2019-12-16 stsp dir = fdopendir(fd2);
1208 3dcf3e74 2020-01-28 stsp if (dir == NULL) {
1209 3dcf3e74 2020-01-28 stsp err = got_error_from_errno2("fdopendir", path);
1210 3dcf3e74 2020-01-28 stsp close(fd2);
1211 3dcf3e74 2020-01-28 stsp return err;
1213 965988c5 2019-12-16 stsp err = read_dirlist(&dirlist, dir, path);
1214 965988c5 2019-12-16 stsp if (err) {
1215 965988c5 2019-12-16 stsp closedir(dir);
1216 c577a9ce 2019-07-27 stsp return err;
1219 c577a9ce 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1220 c577a9ce 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1221 c577a9ce 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
1222 965988c5 2019-12-16 stsp err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1223 965988c5 2019-12-16 stsp rootpath, path, repo, cb, cb_arg);
1225 965988c5 2019-12-16 stsp if (closedir(dir) == -1 && err == NULL)
1226 965988c5 2019-12-16 stsp err = got_error_from_errno2("closedir", path);
1227 21c2d8be 2023-01-10 thomas got_pathlist_free(&dirlist, GOT_PATHLIST_FREE_DATA);
1228 c577a9ce 2019-07-27 stsp return err;
1231 43010591 2023-02-17 thomas struct got_object_id *
1232 43010591 2023-02-17 thomas got_fileindex_entry_get_staged_blob_id(struct got_object_id *id,
1233 43010591 2023-02-17 thomas struct got_fileindex_entry *ie)
1235 43010591 2023-02-17 thomas memset(id, 0, sizeof(*id));
1236 43010591 2023-02-17 thomas memcpy(id->sha1, ie->staged_blob_sha1, sizeof(ie->staged_blob_sha1));
1237 43010591 2023-02-17 thomas return id;
1240 43010591 2023-02-17 thomas struct got_object_id *
1241 43010591 2023-02-17 thomas got_fileindex_entry_get_blob_id(struct got_object_id *id,
1242 43010591 2023-02-17 thomas struct got_fileindex_entry *ie)
1244 43010591 2023-02-17 thomas memset(id, 0, sizeof(*id));
1245 43010591 2023-02-17 thomas memcpy(id->sha1, ie->blob_sha1, sizeof(ie->blob_sha1));
1246 43010591 2023-02-17 thomas return id;
1249 43010591 2023-02-17 thomas struct got_object_id *
1250 43010591 2023-02-17 thomas got_fileindex_entry_get_commit_id(struct got_object_id *id,
1251 43010591 2023-02-17 thomas struct got_fileindex_entry *ie)
1253 43010591 2023-02-17 thomas memset(id, 0, sizeof(*id));
1254 43010591 2023-02-17 thomas memcpy(id->sha1, ie->commit_sha1, sizeof(ie->commit_sha1));
1255 43010591 2023-02-17 thomas return id;
1258 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);