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 eb983b4b 2019-03-26 stsp /* got_fileindex_entry flags */
38 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
39 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_STAGE 0x00003000
40 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_EXTENDED 0x00004000
41 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_ASSUME_VALID 0x00008000
42 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
43 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NO_BLOB 0x00020000
44 ddce0520 2019-03-26 stsp #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
45 2ec1f75b 2019-03-26 stsp #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
47 133d2798 2019-01-08 stsp struct got_fileindex {
48 133d2798 2019-01-08 stsp struct got_fileindex_tree entries;
49 133d2798 2019-01-08 stsp int nentries;
50 133d2798 2019-01-08 stsp #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
53 c48c4a9c 2018-03-11 stsp const struct got_error *
54 51514078 2018-12-25 stsp got_fileindex_entry_update(struct got_fileindex_entry *entry,
55 02c07007 2019-02-10 stsp const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
56 02c07007 2019-02-10 stsp int update_timestamps)
58 c48c4a9c 2018-03-11 stsp struct stat sb;
60 13d9040b 2019-03-27 stsp if (lstat(ondisk_path, &sb) != 0) {
61 13d9040b 2019-03-27 stsp if ((entry->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0)
62 13d9040b 2019-03-27 stsp return got_error_from_errno();
64 13d9040b 2019-03-27 stsp entry->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
66 13d9040b 2019-03-27 stsp if ((entry->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
67 13d9040b 2019-03-27 stsp if (update_timestamps) {
68 13d9040b 2019-03-27 stsp entry->ctime_sec = sb.st_ctime;
69 13d9040b 2019-03-27 stsp entry->ctime_nsec = sb.st_ctimensec;
70 13d9040b 2019-03-27 stsp entry->mtime_sec = sb.st_mtime;
71 13d9040b 2019-03-27 stsp entry->mtime_nsec = sb.st_mtimensec;
73 13d9040b 2019-03-27 stsp entry->uid = sb.st_uid;
74 13d9040b 2019-03-27 stsp entry->gid = sb.st_gid;
75 13d9040b 2019-03-27 stsp entry->size = (sb.st_size & 0xffffffff);
76 13d9040b 2019-03-27 stsp if (sb.st_mode & S_IFLNK)
77 13d9040b 2019-03-27 stsp entry->mode = GOT_FILEIDX_MODE_SYMLINK;
79 13d9040b 2019-03-27 stsp entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
80 13d9040b 2019-03-27 stsp entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
81 13d9040b 2019-03-27 stsp GOT_FILEIDX_MODE_PERMS_SHIFT);
84 ddce0520 2019-03-26 stsp if (blob_sha1) {
85 ddce0520 2019-03-26 stsp memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
86 ddce0520 2019-03-26 stsp entry->flags &= ~GOT_FILEIDX_F_NO_BLOB;
88 ddce0520 2019-03-26 stsp entry->flags |= GOT_FILEIDX_F_NO_BLOB;
90 ddce0520 2019-03-26 stsp if (commit_sha1) {
91 ddce0520 2019-03-26 stsp memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
92 ddce0520 2019-03-26 stsp entry->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
94 ddce0520 2019-03-26 stsp entry->flags |= GOT_FILEIDX_F_NO_COMMIT;
96 51514078 2018-12-25 stsp return NULL;
100 2ec1f75b 2019-03-26 stsp got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *entry)
102 2ec1f75b 2019-03-26 stsp entry->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
105 51514078 2018-12-25 stsp const struct got_error *
106 51514078 2018-12-25 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
107 51514078 2018-12-25 stsp const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
108 51514078 2018-12-25 stsp uint8_t *commit_sha1)
110 51514078 2018-12-25 stsp size_t len;
112 c48c4a9c 2018-03-11 stsp *entry = calloc(1, sizeof(**entry));
113 c48c4a9c 2018-03-11 stsp if (*entry == NULL)
114 0a585a0d 2018-03-17 stsp return got_error_from_errno();
116 c34b20a2 2018-03-12 stsp (*entry)->path = strdup(relpath);
117 c48c4a9c 2018-03-11 stsp if ((*entry)->path == NULL) {
118 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
119 c48c4a9c 2018-03-11 stsp free(*entry);
120 c48c4a9c 2018-03-11 stsp *entry = NULL;
121 0a585a0d 2018-03-17 stsp return err;
124 c34b20a2 2018-03-12 stsp len = strlen(relpath);
125 a7f9d64d 2019-01-13 stsp if (len > GOT_FILEIDX_F_PATH_LEN)
126 a7f9d64d 2019-01-13 stsp len = GOT_FILEIDX_F_PATH_LEN;
127 c48c4a9c 2018-03-11 stsp (*entry)->flags |= len;
129 51514078 2018-12-25 stsp return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
130 02c07007 2019-02-10 stsp commit_sha1, 1);
134 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(struct got_fileindex_entry *entry)
136 c48c4a9c 2018-03-11 stsp free(entry->path);
137 c48c4a9c 2018-03-11 stsp free(entry);
141 d00136be 2019-03-26 stsp got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
143 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
147 d00136be 2019-03-26 stsp got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
149 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
153 2ec1f75b 2019-03-26 stsp got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
155 2ec1f75b 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
158 50952927 2019-01-12 stsp static const struct got_error *
159 50952927 2019-01-12 stsp add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
161 133d2798 2019-01-08 stsp if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
162 133d2798 2019-01-08 stsp return got_error(GOT_ERR_NO_SPACE);
164 133d2798 2019-01-08 stsp RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
165 133d2798 2019-01-08 stsp fileindex->nentries++;
166 133d2798 2019-01-08 stsp return NULL;
169 50952927 2019-01-12 stsp const struct got_error *
170 50952927 2019-01-12 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
171 50952927 2019-01-12 stsp struct got_fileindex_entry *entry)
173 50952927 2019-01-12 stsp /* Flag this entry until it gets written out to disk. */
174 e288864f 2019-03-26 stsp entry->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
176 50952927 2019-01-12 stsp return add_entry(fileindex, entry);
180 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
181 512f0d0e 2019-01-02 stsp struct got_fileindex_entry *entry)
183 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
184 133d2798 2019-01-08 stsp fileindex->nentries--;
187 51514078 2018-12-25 stsp struct got_fileindex_entry *
188 51514078 2018-12-25 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
190 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
191 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
192 133d2798 2019-01-08 stsp key.path = (char *)path;
193 133d2798 2019-01-08 stsp return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
196 512f0d0e 2019-01-02 stsp const struct got_error *
197 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
198 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
200 133d2798 2019-01-08 stsp const struct got_error *err;
201 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry, *tmp;
203 133d2798 2019-01-08 stsp RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
204 133d2798 2019-01-08 stsp err = (*cb)(cb_arg, entry);
206 133d2798 2019-01-08 stsp return err;
208 b504a804 2019-01-08 stsp return NULL;
211 133d2798 2019-01-08 stsp struct got_fileindex *
212 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
214 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
216 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
217 133d2798 2019-01-08 stsp if (fileindex == NULL)
218 133d2798 2019-01-08 stsp return NULL;
220 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
221 133d2798 2019-01-08 stsp return fileindex;
225 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
227 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
229 133d2798 2019-01-08 stsp while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
230 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
231 133d2798 2019-01-08 stsp got_fileindex_entry_free(entry);
233 9d31a1d8 2018-03-11 stsp free(fileindex);
236 c34b20a2 2018-03-12 stsp static const struct got_error *
237 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
241 c34b20a2 2018-03-12 stsp val = htobe64(val);
242 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
243 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
244 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
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_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
254 c34b20a2 2018-03-12 stsp val = htobe32(val);
255 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
256 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
257 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
258 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
259 c34b20a2 2018-03-12 stsp return NULL;
262 c34b20a2 2018-03-12 stsp static const struct got_error *
263 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
267 c34b20a2 2018-03-12 stsp val = htobe16(val);
268 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
269 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
270 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
271 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
272 c34b20a2 2018-03-12 stsp return NULL;
275 c34b20a2 2018-03-12 stsp static const struct got_error *
276 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
278 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
279 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
281 c34b20a2 2018-03-12 stsp len = strlen(path);
282 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
284 3c5b70f2 2018-12-27 stsp if (pad == 0)
285 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
287 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
288 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
289 c34b20a2 2018-03-12 stsp if (n != len)
290 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
291 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
292 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
293 c34b20a2 2018-03-12 stsp if (n != pad)
294 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
295 c34b20a2 2018-03-12 stsp return NULL;
298 c34b20a2 2018-03-12 stsp static const struct got_error *
299 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
300 c34b20a2 2018-03-12 stsp FILE *outfile)
302 c34b20a2 2018-03-12 stsp const struct got_error *err;
305 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
307 c34b20a2 2018-03-12 stsp return err;
308 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
310 c34b20a2 2018-03-12 stsp return err;
311 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
313 c34b20a2 2018-03-12 stsp return err;
314 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
316 c34b20a2 2018-03-12 stsp return err;
318 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
320 c34b20a2 2018-03-12 stsp return err;
321 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
323 c34b20a2 2018-03-12 stsp return err;
324 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
326 c34b20a2 2018-03-12 stsp return err;
328 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
330 c34b20a2 2018-03-12 stsp return err;
332 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
333 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
334 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
335 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
337 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
338 fc76cabb 2018-12-25 stsp n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
339 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
340 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
342 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
344 c34b20a2 2018-03-12 stsp return err;
346 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
347 c34b20a2 2018-03-12 stsp return err;
350 9d31a1d8 2018-03-11 stsp const struct got_error *
351 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
353 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
354 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
355 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
356 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
358 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
360 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
362 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
363 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
364 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
366 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
367 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
368 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
369 a5744d71 2019-01-12 stsp n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
370 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.signature))
371 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
372 a5744d71 2019-01-12 stsp n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
373 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.version))
374 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
375 a5744d71 2019-01-12 stsp n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
376 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.nentries))
377 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
379 133d2798 2019-01-08 stsp RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
380 e288864f 2019-03-26 stsp entry->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
381 133d2798 2019-01-08 stsp err = write_fileindex_entry(&ctx, entry, outfile);
383 133d2798 2019-01-08 stsp return err;
386 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
387 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
388 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
389 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
391 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
392 27d0e5bd 2019-01-12 stsp return got_error_from_errno();
394 52a74475 2018-12-24 stsp return NULL;
397 52a74475 2018-12-24 stsp static const struct got_error *
398 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
402 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
403 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
404 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
405 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
406 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
407 52a74475 2018-12-24 stsp return NULL;
410 52a74475 2018-12-24 stsp static const struct got_error *
411 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
415 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
416 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
417 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
418 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
419 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
420 52a74475 2018-12-24 stsp return NULL;
423 52a74475 2018-12-24 stsp static const struct got_error *
424 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
428 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
429 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
430 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
431 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
432 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
433 52a74475 2018-12-24 stsp return NULL;
436 52a74475 2018-12-24 stsp static const struct got_error *
437 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
439 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
440 52a74475 2018-12-24 stsp uint8_t buf[8];
441 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
443 52a74475 2018-12-24 stsp *path = malloc(totlen);
444 52a74475 2018-12-24 stsp if (*path == NULL)
445 52a74475 2018-12-24 stsp return got_error_from_errno();
448 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
449 52a74475 2018-12-24 stsp if (n != sizeof(buf))
450 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
451 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
452 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
453 52a74475 2018-12-24 stsp if (p == NULL) {
454 52a74475 2018-12-24 stsp err = got_error_from_errno();
457 52a74475 2018-12-24 stsp totlen += sizeof(buf);
460 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
461 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
462 52a74475 2018-12-24 stsp len += sizeof(buf);
463 60619907 2018-12-27 stsp } while (memchr(buf, '\0', sizeof(buf)) == NULL);
466 52a74475 2018-12-24 stsp free(*path);
467 52a74475 2018-12-24 stsp *path = NULL;
469 52a74475 2018-12-24 stsp return err;
472 52a74475 2018-12-24 stsp static const struct got_error *
473 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
474 52a74475 2018-12-24 stsp FILE *infile)
476 52a74475 2018-12-24 stsp const struct got_error *err;
477 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
480 52a74475 2018-12-24 stsp *entryp = NULL;
482 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
483 52a74475 2018-12-24 stsp if (entry == NULL)
484 52a74475 2018-12-24 stsp return got_error_from_errno();
486 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
489 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
492 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
495 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
499 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
502 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
505 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
509 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
513 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
514 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
515 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
518 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
520 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
521 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
522 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
525 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
527 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
531 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
534 52a74475 2018-12-24 stsp free(entry);
536 52a74475 2018-12-24 stsp *entryp = entry;
537 52a74475 2018-12-24 stsp return err;
540 52a74475 2018-12-24 stsp const struct got_error *
541 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
543 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
544 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
545 52a74475 2018-12-24 stsp SHA1_CTX ctx;
546 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
547 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
548 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
552 52a74475 2018-12-24 stsp SHA1Init(&ctx);
554 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
555 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
556 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
557 b6d05318 2019-01-13 stsp return NULL;
558 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
560 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
561 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
562 51514078 2018-12-25 stsp if (n == 0) /* EOF */
563 51514078 2018-12-25 stsp return NULL;
564 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
566 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
567 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
568 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
569 b6d05318 2019-01-13 stsp return NULL;
570 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
573 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
574 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
575 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
577 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
578 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
579 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
581 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
582 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
583 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
584 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
586 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
587 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
589 52a74475 2018-12-24 stsp return err;
590 50952927 2019-01-12 stsp err = add_entry(fileindex, entry);
592 52a74475 2018-12-24 stsp return err;
595 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
596 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
597 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
598 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
599 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
600 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
602 9d31a1d8 2018-03-11 stsp return NULL;
605 c2ac9456 2019-03-15 stsp static struct got_fileindex_entry *
606 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
608 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
610 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
612 50952927 2019-01-12 stsp /* Skip entries which were newly added by diff callbacks. */
613 e288864f 2019-03-26 stsp while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
614 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
616 50952927 2019-01-12 stsp return next;
619 8da9e5f4 2019-01-12 stsp static const struct got_error *
620 9d2a8e53 2019-01-28 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
621 c4cdcb68 2019-04-03 stsp struct got_tree_object *, const char *, const char *,
622 c4cdcb68 2019-04-03 stsp struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
624 9d2a8e53 2019-01-28 stsp static const struct got_error *
625 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
626 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_entry *te,
627 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
628 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
630 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
632 bd4792ec 2019-01-13 stsp if (S_ISDIR(te->mode)) {
633 8da9e5f4 2019-01-12 stsp char *subpath;
634 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
636 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
637 8da9e5f4 2019-01-12 stsp path[0] == '\0' ? "" : "/", te->name) == -1)
638 8da9e5f4 2019-01-12 stsp return got_error_from_errno();
640 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
642 8da9e5f4 2019-01-12 stsp free(subpath);
643 8da9e5f4 2019-01-12 stsp return err;
646 8da9e5f4 2019-01-12 stsp err = diff_fileindex_tree(fileindex, ie, subtree,
647 c4cdcb68 2019-04-03 stsp subpath, entry_name, repo, cb, cb_arg);
648 8da9e5f4 2019-01-12 stsp free(subpath);
649 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
651 8da9e5f4 2019-01-12 stsp return err;
654 bd4792ec 2019-01-13 stsp *next = SIMPLEQ_NEXT(te, entry);
655 8da9e5f4 2019-01-12 stsp return NULL;
658 8da9e5f4 2019-01-12 stsp static const struct got_error *
659 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
660 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_object *tree,
661 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
662 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
664 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
665 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
666 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
667 8da9e5f4 2019-01-12 stsp const struct got_tree_entries *entries;
668 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
670 8da9e5f4 2019-01-12 stsp entries = got_object_tree_get_entries(tree);
671 8da9e5f4 2019-01-12 stsp te = SIMPLEQ_FIRST(&entries->head);
672 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
673 8da9e5f4 2019-01-12 stsp if (te && *ie) {
674 18831e78 2019-02-10 stsp char *te_path;
676 18831e78 2019-02-10 stsp if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
677 18831e78 2019-02-10 stsp err = got_error_from_errno();
680 18831e78 2019-02-10 stsp cmp = got_path_cmp((*ie)->path, te_path);
681 18831e78 2019-02-10 stsp free(te_path);
682 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
683 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
684 c4cdcb68 2019-04-03 stsp path_len) && (entry_name == NULL ||
685 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
686 c4cdcb68 2019-04-03 stsp err = cb->diff_old_new(cb_arg, *ie, te,
688 c4cdcb68 2019-04-03 stsp if (err || entry_name)
691 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
692 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
693 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
694 c4cdcb68 2019-04-03 stsp } else if (cmp < 0) {
695 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
696 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
697 c4cdcb68 2019-04-03 stsp path_len) && (entry_name == NULL ||
698 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
699 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
700 c4cdcb68 2019-04-03 stsp if (err || entry_name)
703 8da9e5f4 2019-01-12 stsp *ie = next;
705 c4cdcb68 2019-04-03 stsp if ((entry_name == NULL ||
706 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
707 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
708 c4cdcb68 2019-04-03 stsp if (err || entry_name)
711 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
712 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
716 8da9e5f4 2019-01-12 stsp } else if (*ie) {
717 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
718 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path, path_len) &&
719 c4cdcb68 2019-04-03 stsp (entry_name == NULL ||
720 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
721 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
722 c4cdcb68 2019-04-03 stsp if (err || entry_name)
725 8da9e5f4 2019-01-12 stsp *ie = next;
726 8da9e5f4 2019-01-12 stsp } else if (te) {
727 c4cdcb68 2019-04-03 stsp if (entry_name == NULL ||
728 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0) {
729 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
730 c4cdcb68 2019-04-03 stsp if (err || entry_name)
733 c4cdcb68 2019-04-03 stsp err = walk_tree(&te, fileindex, ie, te, path,
734 c4cdcb68 2019-04-03 stsp entry_name, repo, cb, cb_arg);
740 8da9e5f4 2019-01-12 stsp return err;
743 8da9e5f4 2019-01-12 stsp const struct got_error *
744 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
745 c4cdcb68 2019-04-03 stsp struct got_tree_object *tree, const char *path, const char *entry_name,
746 c4cdcb68 2019-04-03 stsp struct got_repository *repo,
747 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
749 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *min;
750 8da9e5f4 2019-01-12 stsp min = RB_MIN(got_fileindex_tree, &fileindex->entries);
751 c4cdcb68 2019-04-03 stsp return diff_fileindex_tree(fileindex, &min, tree, path, entry_name,
752 c4cdcb68 2019-04-03 stsp repo, cb, cb_arg);
755 d1f6d47b 2019-02-04 stsp static const struct got_error *
756 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **, DIR *,
757 c7f4312f 2019-02-05 stsp const char *, const char *, struct got_repository *,
758 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *, void *);
760 d1f6d47b 2019-02-04 stsp static const struct got_error *
761 f5d3d7af 2019-02-05 stsp walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
762 f5d3d7af 2019-02-05 stsp struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
763 c7f4312f 2019-02-05 stsp const char *path, DIR *dir, const char *rootpath,
764 c7f4312f 2019-02-05 stsp struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
765 c7f4312f 2019-02-05 stsp void *cb_arg)
767 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
768 f5d3d7af 2019-02-05 stsp struct dirent *de = dle->data;
770 573463cc 2019-04-06 stsp *next = NULL;
772 f5d3d7af 2019-02-05 stsp if (de->d_type == DT_DIR) {
773 d1f6d47b 2019-02-04 stsp char *subpath;
774 c7f4312f 2019-02-05 stsp char *subdirpath;
775 d1f6d47b 2019-02-04 stsp DIR *subdir;
777 d1f6d47b 2019-02-04 stsp if (asprintf(&subpath, "%s%s%s", path,
778 f5d3d7af 2019-02-05 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
779 d1f6d47b 2019-02-04 stsp return got_error_from_errno();
781 c7f4312f 2019-02-05 stsp if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
782 c7f4312f 2019-02-05 stsp free(subpath);
783 c7f4312f 2019-02-05 stsp return got_error_from_errno();
786 c7f4312f 2019-02-05 stsp subdir = opendir(subdirpath);
787 d1f6d47b 2019-02-04 stsp if (subdir == NULL) {
788 d1f6d47b 2019-02-04 stsp free(subpath);
789 c7f4312f 2019-02-05 stsp free(subdirpath);
790 d1f6d47b 2019-02-04 stsp return got_error_from_errno();
793 c7f4312f 2019-02-05 stsp err = diff_fileindex_dir(fileindex, ie, subdir, rootpath,
794 c7f4312f 2019-02-05 stsp subpath, repo, cb, cb_arg);
795 d1f6d47b 2019-02-04 stsp free(subpath);
796 c7f4312f 2019-02-05 stsp free(subdirpath);
797 d1f6d47b 2019-02-04 stsp closedir(subdir);
799 d1f6d47b 2019-02-04 stsp return err;
802 f5d3d7af 2019-02-05 stsp *next = TAILQ_NEXT(dle, entry);
803 d1f6d47b 2019-02-04 stsp return NULL;
806 d1f6d47b 2019-02-04 stsp static const struct got_error *
807 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *fileindex,
808 c7f4312f 2019-02-05 stsp struct got_fileindex_entry **ie, DIR *dir, const char *rootpath,
809 c7f4312f 2019-02-05 stsp const char *path, struct got_repository *repo,
810 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
812 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
813 d1f6d47b 2019-02-04 stsp struct dirent *de = NULL;
814 d1f6d47b 2019-02-04 stsp size_t path_len = strlen(path);
815 d1f6d47b 2019-02-04 stsp struct got_fileindex_entry *next;
816 f5d3d7af 2019-02-05 stsp struct got_pathlist_head dirlist;
817 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *dle;
819 f5d3d7af 2019-02-05 stsp TAILQ_INIT(&dirlist);
821 500cd40f 2019-02-04 stsp while (1) {
822 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *new = NULL;
823 ed83bff7 2019-02-05 stsp struct dirent *dep = NULL;
825 ed83bff7 2019-02-05 stsp de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
826 ed83bff7 2019-02-05 stsp if (de == NULL) {
827 ed83bff7 2019-02-05 stsp err = got_error_from_errno();
831 ed83bff7 2019-02-05 stsp if (readdir_r(dir, de, &dep) != 0) {
832 ed83bff7 2019-02-05 stsp err = got_error_from_errno();
836 ed83bff7 2019-02-05 stsp if (dep == NULL) {
841 b1ec3986 2019-02-04 stsp if (strcmp(de->d_name, ".") == 0 ||
842 b25ae4fa 2019-02-04 stsp strcmp(de->d_name, "..") == 0 ||
843 b25ae4fa 2019-02-04 stsp (path[0] == '\0' &&
844 ed83bff7 2019-02-05 stsp strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
849 f5d3d7af 2019-02-05 stsp err = got_pathlist_insert(&new, &dirlist, de->d_name, de);
854 f5d3d7af 2019-02-05 stsp if (new == NULL) {
855 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_DIR_DUP_ENTRY);
861 f5d3d7af 2019-02-05 stsp dle = TAILQ_FIRST(&dirlist);
862 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
863 500cd40f 2019-02-04 stsp if (dle && *ie) {
864 18831e78 2019-02-10 stsp char *de_path;
866 f5d3d7af 2019-02-05 stsp de = dle->data;
867 18831e78 2019-02-10 stsp if (asprintf(&de_path, "%s/%s", path,
868 18831e78 2019-02-10 stsp de->d_name) == -1) {
869 18831e78 2019-02-10 stsp err = got_error_from_errno();
872 18831e78 2019-02-10 stsp cmp = got_path_cmp((*ie)->path, de_path);
873 18831e78 2019-02-10 stsp free(de_path);
874 d1f6d47b 2019-02-04 stsp if (cmp == 0) {
875 f5d3d7af 2019-02-05 stsp err = cb->diff_old_new(cb_arg, *ie, de, path);
878 d1f6d47b 2019-02-04 stsp *ie = walk_fileindex(fileindex, *ie);
879 500cd40f 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
880 c7f4312f 2019-02-05 stsp dir, rootpath, repo, cb, cb_arg);
881 d1f6d47b 2019-02-04 stsp } else if (cmp < 0 ) {
882 d1f6d47b 2019-02-04 stsp next = walk_fileindex(fileindex, *ie);
883 d1f6d47b 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
886 d1f6d47b 2019-02-04 stsp *ie = next;
888 f5d3d7af 2019-02-05 stsp err = cb->diff_new(cb_arg, de, path);
891 500cd40f 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
892 c7f4312f 2019-02-05 stsp dir, rootpath, repo, cb, cb_arg);
896 554b91b1 2019-02-04 stsp } else if (*ie) {
897 554b91b1 2019-02-04 stsp next = walk_fileindex(fileindex, *ie);
898 554b91b1 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
901 554b91b1 2019-02-04 stsp *ie = next;
902 554b91b1 2019-02-04 stsp } else if (dle) {
903 763e1377 2019-02-05 stsp de = dle->data;
904 f5d3d7af 2019-02-05 stsp err = cb->diff_new(cb_arg, de, path);
907 554b91b1 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path, dir,
908 c7f4312f 2019-02-05 stsp rootpath, repo, cb, cb_arg);
914 ed83bff7 2019-02-05 stsp TAILQ_FOREACH(dle, &dirlist, entry)
915 ed83bff7 2019-02-05 stsp free(dle->data);
916 f5d3d7af 2019-02-05 stsp got_pathlist_free(&dirlist);
917 d1f6d47b 2019-02-04 stsp return err;
920 d1f6d47b 2019-02-04 stsp const struct got_error *
921 c7f4312f 2019-02-05 stsp got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
922 927df6b7 2019-02-10 stsp const char *rootpath, const char *path, struct got_repository *repo,
923 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
925 d1f6d47b 2019-02-04 stsp struct got_fileindex_entry *min;
926 d1f6d47b 2019-02-04 stsp min = RB_MIN(got_fileindex_tree, &fileindex->entries);
927 927df6b7 2019-02-10 stsp return diff_fileindex_dir(fileindex, &min, rootdir, rootpath, path,
928 c7f4312f 2019-02-05 stsp repo, cb, cb_arg);
931 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);