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 03df25b3 2019-05-11 stsp #include <errno.h>
22 d1f6d47b 2019-02-04 stsp #include <dirent.h>
23 c48c4a9c 2018-03-11 stsp #include <stdio.h>
24 c48c4a9c 2018-03-11 stsp #include <stdlib.h>
25 c48c4a9c 2018-03-11 stsp #include <string.h>
26 c48c4a9c 2018-03-11 stsp #include <sha1.h>
27 c34b20a2 2018-03-12 stsp #include <endian.h>
28 133d2798 2019-01-08 stsp #include <limits.h>
29 c442a90d 2019-03-10 stsp #include <uuid.h>
31 c48c4a9c 2018-03-11 stsp #include "got_error.h"
32 8da9e5f4 2019-01-12 stsp #include "got_object.h"
33 324d37e7 2019-05-11 stsp #include "got_path.h"
35 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
36 b25ae4fa 2019-02-04 stsp #include "got_lib_worktree.h"
38 eb983b4b 2019-03-26 stsp /* got_fileindex_entry flags */
39 eb983b4b 2019-03-26 stsp #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
40 83718700 2019-08-03 stsp #define GOT_FILEIDX_F_STAGE 0x0000f000
41 3cd04235 2019-08-03 stsp #define GOT_FILEIDX_F_STAGE_SHIFT 12
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
54 26a7fe28 2019-07-27 stsp got_fileindex_perms_from_st(struct stat *sb)
56 26a7fe28 2019-07-27 stsp uint16_t perms = (sb->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
57 26a7fe28 2019-07-27 stsp return (perms << GOT_FILEIDX_MODE_PERMS_SHIFT);
61 26a7fe28 2019-07-27 stsp got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
63 26a7fe28 2019-07-27 stsp mode_t perms = (ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT);
64 26a7fe28 2019-07-27 stsp return (perms & (S_IRWXU | S_IRWXG | S_IRWXO));
67 c48c4a9c 2018-03-11 stsp const struct got_error *
68 3f762da0 2019-08-03 stsp got_fileindex_entry_update(struct got_fileindex_entry *ie,
69 02c07007 2019-02-10 stsp const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
70 02c07007 2019-02-10 stsp int update_timestamps)
72 c48c4a9c 2018-03-11 stsp struct stat sb;
74 13d9040b 2019-03-27 stsp if (lstat(ondisk_path, &sb) != 0) {
75 3f762da0 2019-08-03 stsp if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0)
76 638f9024 2019-05-13 stsp return got_error_from_errno2("lstat", ondisk_path);
78 03df25b3 2019-05-11 stsp if (sb.st_mode & S_IFDIR)
79 03df25b3 2019-05-11 stsp return got_error_set_errno(EISDIR, ondisk_path);
80 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
84 3f762da0 2019-08-03 stsp if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
85 13d9040b 2019-03-27 stsp if (update_timestamps) {
86 3f762da0 2019-08-03 stsp ie->ctime_sec = sb.st_ctime;
87 3f762da0 2019-08-03 stsp ie->ctime_nsec = sb.st_ctimensec;
88 3f762da0 2019-08-03 stsp ie->mtime_sec = sb.st_mtime;
89 3f762da0 2019-08-03 stsp ie->mtime_nsec = sb.st_mtimensec;
91 3f762da0 2019-08-03 stsp ie->uid = sb.st_uid;
92 3f762da0 2019-08-03 stsp ie->gid = sb.st_gid;
93 3f762da0 2019-08-03 stsp ie->size = (sb.st_size & 0xffffffff);
94 13d9040b 2019-03-27 stsp if (sb.st_mode & S_IFLNK)
95 3f762da0 2019-08-03 stsp ie->mode = GOT_FILEIDX_MODE_SYMLINK;
97 3f762da0 2019-08-03 stsp ie->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
98 3f762da0 2019-08-03 stsp ie->mode |= got_fileindex_perms_from_st(&sb);
101 ddce0520 2019-03-26 stsp if (blob_sha1) {
102 3f762da0 2019-08-03 stsp memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
103 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
105 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NO_BLOB;
107 ddce0520 2019-03-26 stsp if (commit_sha1) {
108 3f762da0 2019-08-03 stsp memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
109 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
111 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
113 51514078 2018-12-25 stsp return NULL;
117 3f762da0 2019-08-03 stsp got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
119 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
122 51514078 2018-12-25 stsp const struct got_error *
123 3f762da0 2019-08-03 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
124 51514078 2018-12-25 stsp const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
125 51514078 2018-12-25 stsp uint8_t *commit_sha1)
127 51514078 2018-12-25 stsp size_t len;
129 3f762da0 2019-08-03 stsp *ie = calloc(1, sizeof(**ie));
130 3f762da0 2019-08-03 stsp if (*ie == NULL)
131 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
133 3f762da0 2019-08-03 stsp (*ie)->path = strdup(relpath);
134 3f762da0 2019-08-03 stsp if ((*ie)->path == NULL) {
135 638f9024 2019-05-13 stsp const struct got_error *err = got_error_from_errno("strdup");
137 3f762da0 2019-08-03 stsp *ie = NULL;
138 0a585a0d 2018-03-17 stsp return err;
141 4d555405 2019-08-03 stsp len = strlen(relpath);
142 a7f9d64d 2019-01-13 stsp if (len > GOT_FILEIDX_F_PATH_LEN)
143 a7f9d64d 2019-01-13 stsp len = GOT_FILEIDX_F_PATH_LEN;
144 3f762da0 2019-08-03 stsp (*ie)->flags |= len;
146 3f762da0 2019-08-03 stsp return got_fileindex_entry_update(*ie, ondisk_path, blob_sha1,
147 02c07007 2019-02-10 stsp commit_sha1, 1);
151 3f762da0 2019-08-03 stsp got_fileindex_entry_free(struct got_fileindex_entry *ie)
153 3f762da0 2019-08-03 stsp free(ie->path);
158 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
160 4d555405 2019-08-03 stsp return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
164 0cb83759 2019-08-03 stsp got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
166 df335242 2019-08-03 stsp return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
170 0cb83759 2019-08-03 stsp got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
172 0cb83759 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_STAGE;
173 0cb83759 2019-08-03 stsp ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
174 0cb83759 2019-08-03 stsp GOT_FILEIDX_F_STAGE);
178 d00136be 2019-03-26 stsp got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
180 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
184 d00136be 2019-03-26 stsp got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
186 d00136be 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
190 2ec1f75b 2019-03-26 stsp got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
192 2ec1f75b 2019-03-26 stsp return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
195 50952927 2019-01-12 stsp static const struct got_error *
196 3f762da0 2019-08-03 stsp add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
198 133d2798 2019-01-08 stsp if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
199 133d2798 2019-01-08 stsp return got_error(GOT_ERR_NO_SPACE);
201 3f762da0 2019-08-03 stsp RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
202 133d2798 2019-01-08 stsp fileindex->nentries++;
203 133d2798 2019-01-08 stsp return NULL;
206 50952927 2019-01-12 stsp const struct got_error *
207 50952927 2019-01-12 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
208 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie)
210 50952927 2019-01-12 stsp /* Flag this entry until it gets written out to disk. */
211 3f762da0 2019-08-03 stsp ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
213 3f762da0 2019-08-03 stsp return add_entry(fileindex, ie);
217 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
218 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie)
220 3f762da0 2019-08-03 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
221 133d2798 2019-01-08 stsp fileindex->nentries--;
224 51514078 2018-12-25 stsp struct got_fileindex_entry *
225 d6c87207 2019-08-02 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
226 d6c87207 2019-08-02 stsp size_t path_len)
228 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
229 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
230 133d2798 2019-01-08 stsp key.path = (char *)path;
231 4d555405 2019-08-03 stsp key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
232 133d2798 2019-01-08 stsp return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
235 512f0d0e 2019-01-02 stsp const struct got_error *
236 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
237 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
239 133d2798 2019-01-08 stsp const struct got_error *err;
240 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie, *tmp;
242 3f762da0 2019-08-03 stsp RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
243 3f762da0 2019-08-03 stsp err = (*cb)(cb_arg, ie);
245 133d2798 2019-01-08 stsp return err;
247 b504a804 2019-01-08 stsp return NULL;
250 133d2798 2019-01-08 stsp struct got_fileindex *
251 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
253 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
255 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
256 133d2798 2019-01-08 stsp if (fileindex == NULL)
257 133d2798 2019-01-08 stsp return NULL;
259 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
260 133d2798 2019-01-08 stsp return fileindex;
264 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
266 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
268 3f762da0 2019-08-03 stsp while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
269 3f762da0 2019-08-03 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
270 3f762da0 2019-08-03 stsp got_fileindex_entry_free(ie);
272 9d31a1d8 2018-03-11 stsp free(fileindex);
275 c34b20a2 2018-03-12 stsp static const struct got_error *
276 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
280 c34b20a2 2018-03-12 stsp val = htobe64(val);
281 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
282 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
283 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
284 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
285 c34b20a2 2018-03-12 stsp return NULL;
288 c34b20a2 2018-03-12 stsp static const struct got_error *
289 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
293 c34b20a2 2018-03-12 stsp val = htobe32(val);
294 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
295 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
296 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
297 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
298 c34b20a2 2018-03-12 stsp return NULL;
301 c34b20a2 2018-03-12 stsp static const struct got_error *
302 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
306 c34b20a2 2018-03-12 stsp val = htobe16(val);
307 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
308 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
309 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
310 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
311 c34b20a2 2018-03-12 stsp return NULL;
314 c34b20a2 2018-03-12 stsp static const struct got_error *
315 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
317 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
318 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
320 c34b20a2 2018-03-12 stsp len = strlen(path);
321 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
323 3c5b70f2 2018-12-27 stsp if (pad == 0)
324 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
326 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
327 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
328 c34b20a2 2018-03-12 stsp if (n != len)
329 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
330 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
331 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
332 c34b20a2 2018-03-12 stsp if (n != pad)
333 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
334 c34b20a2 2018-03-12 stsp return NULL;
337 c34b20a2 2018-03-12 stsp static const struct got_error *
338 3f762da0 2019-08-03 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
339 c34b20a2 2018-03-12 stsp FILE *outfile)
341 c34b20a2 2018-03-12 stsp const struct got_error *err;
343 df335242 2019-08-03 stsp uint32_t stage;
345 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
347 c34b20a2 2018-03-12 stsp return err;
348 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
350 c34b20a2 2018-03-12 stsp return err;
351 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
353 c34b20a2 2018-03-12 stsp return err;
354 3f762da0 2019-08-03 stsp err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
356 c34b20a2 2018-03-12 stsp return err;
358 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->uid, outfile);
360 c34b20a2 2018-03-12 stsp return err;
361 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->gid, outfile);
363 c34b20a2 2018-03-12 stsp return err;
364 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->size, outfile);
366 c34b20a2 2018-03-12 stsp return err;
368 3f762da0 2019-08-03 stsp err = write_fileindex_val16(ctx, ie->mode, outfile);
370 c34b20a2 2018-03-12 stsp return err;
372 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
373 3f762da0 2019-08-03 stsp n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
374 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
375 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
377 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
378 3f762da0 2019-08-03 stsp n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
379 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
380 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
382 3f762da0 2019-08-03 stsp err = write_fileindex_val32(ctx, ie->flags, outfile);
384 c34b20a2 2018-03-12 stsp return err;
386 3f762da0 2019-08-03 stsp err = write_fileindex_path(ctx, ie->path, outfile);
388 df335242 2019-08-03 stsp return err;
390 0cb83759 2019-08-03 stsp stage = got_fileindex_entry_stage_get(ie);
391 df335242 2019-08-03 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
392 df335242 2019-08-03 stsp stage == GOT_FILEIDX_STAGE_ADD) {
393 df335242 2019-08-03 stsp SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
394 df335242 2019-08-03 stsp n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
396 df335242 2019-08-03 stsp if (n != SHA1_DIGEST_LENGTH)
397 df335242 2019-08-03 stsp return got_ferror(outfile, GOT_ERR_IO);
400 df335242 2019-08-03 stsp return NULL;
403 9d31a1d8 2018-03-11 stsp const struct got_error *
404 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
406 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
407 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
408 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
409 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
411 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
413 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
415 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
416 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
417 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
419 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
420 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
421 a5744d71 2019-01-12 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
422 a5744d71 2019-01-12 stsp n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
423 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.signature))
424 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
425 a5744d71 2019-01-12 stsp n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
426 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.version))
427 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
428 a5744d71 2019-01-12 stsp n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
429 a5744d71 2019-01-12 stsp if (n != sizeof(hdr.nentries))
430 a5744d71 2019-01-12 stsp return got_ferror(outfile, GOT_ERR_IO);
432 3f762da0 2019-08-03 stsp RB_FOREACH(ie, got_fileindex_tree, &fileindex->entries) {
433 3f762da0 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
434 3f762da0 2019-08-03 stsp err = write_fileindex_entry(&ctx, ie, outfile);
436 133d2798 2019-01-08 stsp return err;
439 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
440 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
441 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
442 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
444 27d0e5bd 2019-01-12 stsp if (fflush(outfile) != 0)
445 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
447 52a74475 2018-12-24 stsp return NULL;
450 52a74475 2018-12-24 stsp static const struct got_error *
451 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
455 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
456 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
457 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
458 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
459 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
460 52a74475 2018-12-24 stsp return NULL;
463 52a74475 2018-12-24 stsp static const struct got_error *
464 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
468 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
469 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
470 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
471 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
472 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
473 52a74475 2018-12-24 stsp return NULL;
476 52a74475 2018-12-24 stsp static const struct got_error *
477 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
481 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
482 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
483 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
484 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
485 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
486 52a74475 2018-12-24 stsp return NULL;
489 52a74475 2018-12-24 stsp static const struct got_error *
490 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
492 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
493 6bf2c316 2019-08-02 stsp const size_t chunk_size = 8;
494 6bf2c316 2019-08-02 stsp size_t n, len = 0, totlen = chunk_size;
496 52a74475 2018-12-24 stsp *path = malloc(totlen);
497 52a74475 2018-12-24 stsp if (*path == NULL)
498 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
501 6bf2c316 2019-08-02 stsp if (len + chunk_size > totlen) {
502 6bf2c316 2019-08-02 stsp char *p = reallocarray(*path, totlen + chunk_size, 1);
503 52a74475 2018-12-24 stsp if (p == NULL) {
504 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
507 6bf2c316 2019-08-02 stsp totlen += chunk_size;
510 6bf2c316 2019-08-02 stsp n = fread(*path + len, 1, chunk_size, infile);
511 6bf2c316 2019-08-02 stsp if (n != chunk_size) {
512 6bf2c316 2019-08-02 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
515 6bf2c316 2019-08-02 stsp SHA1Update(ctx, *path + len, chunk_size);
516 6bf2c316 2019-08-02 stsp len += chunk_size;
517 6bf2c316 2019-08-02 stsp } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
520 52a74475 2018-12-24 stsp free(*path);
521 52a74475 2018-12-24 stsp *path = NULL;
523 52a74475 2018-12-24 stsp return err;
526 52a74475 2018-12-24 stsp static const struct got_error *
527 3f762da0 2019-08-03 stsp read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
528 df335242 2019-08-03 stsp FILE *infile, uint32_t version)
530 52a74475 2018-12-24 stsp const struct got_error *err;
531 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
534 3f762da0 2019-08-03 stsp *iep = NULL;
536 3f762da0 2019-08-03 stsp ie = calloc(1, sizeof(*ie));
537 3f762da0 2019-08-03 stsp if (ie == NULL)
538 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
540 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
543 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
546 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
549 3f762da0 2019-08-03 stsp err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
553 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->uid, ctx, infile);
556 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->gid, ctx, infile);
559 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->size, ctx, infile);
563 3f762da0 2019-08-03 stsp err = read_fileindex_val16(&ie->mode, ctx, infile);
567 3f762da0 2019-08-03 stsp n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
568 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
569 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
572 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
574 3f762da0 2019-08-03 stsp n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
575 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
576 27793341 2019-01-12 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
579 3f762da0 2019-08-03 stsp SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
581 3f762da0 2019-08-03 stsp err = read_fileindex_val32(&ie->flags, ctx, infile);
585 3f762da0 2019-08-03 stsp err = read_fileindex_path(&ie->path, ctx, infile);
589 df335242 2019-08-03 stsp if (version >= 2) {
590 0cb83759 2019-08-03 stsp uint32_t stage = got_fileindex_entry_stage_get(ie);
591 df335242 2019-08-03 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
592 df335242 2019-08-03 stsp stage == GOT_FILEIDX_STAGE_ADD) {
593 df335242 2019-08-03 stsp n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
595 df335242 2019-08-03 stsp if (n != SHA1_DIGEST_LENGTH) {
596 df335242 2019-08-03 stsp err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
599 df335242 2019-08-03 stsp SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
602 df335242 2019-08-03 stsp /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
603 df335242 2019-08-03 stsp ie->flags &= ~GOT_FILEIDX_F_STAGE;
608 3f762da0 2019-08-03 stsp got_fileindex_entry_free(ie);
611 52a74475 2018-12-24 stsp return err;
614 52a74475 2018-12-24 stsp const struct got_error *
615 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
617 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
618 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
619 52a74475 2018-12-24 stsp SHA1_CTX ctx;
620 3f762da0 2019-08-03 stsp struct got_fileindex_entry *ie;
621 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
622 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
626 52a74475 2018-12-24 stsp SHA1Init(&ctx);
628 b6d05318 2019-01-13 stsp n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
629 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.signature)) {
630 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
631 b6d05318 2019-01-13 stsp return NULL;
632 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
634 b6d05318 2019-01-13 stsp n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
635 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.version)) {
636 51514078 2018-12-25 stsp if (n == 0) /* EOF */
637 51514078 2018-12-25 stsp return NULL;
638 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
640 b6d05318 2019-01-13 stsp n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
641 b6d05318 2019-01-13 stsp if (n != sizeof(hdr.nentries)) {
642 b6d05318 2019-01-13 stsp if (n == 0) /* EOF */
643 b6d05318 2019-01-13 stsp return NULL;
644 b6d05318 2019-01-13 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
647 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
648 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
649 b6d05318 2019-01-13 stsp SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
651 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
652 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
653 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
655 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
656 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
657 df335242 2019-08-03 stsp if (hdr.version > GOT_FILE_INDEX_VERSION)
658 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
660 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
661 df335242 2019-08-03 stsp err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
663 52a74475 2018-12-24 stsp return err;
664 3f762da0 2019-08-03 stsp err = add_entry(fileindex, ie);
666 52a74475 2018-12-24 stsp return err;
669 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
670 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
671 27793341 2019-01-12 stsp return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
672 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
673 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
674 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
676 9d31a1d8 2018-03-11 stsp return NULL;
679 c2ac9456 2019-03-15 stsp static struct got_fileindex_entry *
680 50952927 2019-01-12 stsp walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
682 50952927 2019-01-12 stsp struct got_fileindex_entry *next;
684 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
686 50952927 2019-01-12 stsp /* Skip entries which were newly added by diff callbacks. */
687 e288864f 2019-03-26 stsp while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
688 50952927 2019-01-12 stsp next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
690 50952927 2019-01-12 stsp return next;
693 8da9e5f4 2019-01-12 stsp static const struct got_error *
694 9d2a8e53 2019-01-28 stsp diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
695 30a076bc 2019-07-27 stsp const struct got_tree_entries *, const char *, const char *,
696 c4cdcb68 2019-04-03 stsp struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
698 9d2a8e53 2019-01-28 stsp static const struct got_error *
699 8da9e5f4 2019-01-12 stsp walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
700 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry **ie, struct got_tree_entry *te,
701 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
702 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
704 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
706 bd4792ec 2019-01-13 stsp if (S_ISDIR(te->mode)) {
707 8da9e5f4 2019-01-12 stsp char *subpath;
708 8da9e5f4 2019-01-12 stsp struct got_tree_object *subtree;
710 8da9e5f4 2019-01-12 stsp if (asprintf(&subpath, "%s%s%s", path,
711 8da9e5f4 2019-01-12 stsp path[0] == '\0' ? "" : "/", te->name) == -1)
712 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
714 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
716 8da9e5f4 2019-01-12 stsp free(subpath);
717 8da9e5f4 2019-01-12 stsp return err;
720 30a076bc 2019-07-27 stsp err = diff_fileindex_tree(fileindex, ie,
721 30a076bc 2019-07-27 stsp got_object_tree_get_entries(subtree), subpath, entry_name,
722 30a076bc 2019-07-27 stsp repo, cb, cb_arg);
723 8da9e5f4 2019-01-12 stsp free(subpath);
724 8da9e5f4 2019-01-12 stsp got_object_tree_close(subtree);
726 8da9e5f4 2019-01-12 stsp return err;
729 bd4792ec 2019-01-13 stsp *next = SIMPLEQ_NEXT(te, entry);
730 8da9e5f4 2019-01-12 stsp return NULL;
733 8da9e5f4 2019-01-12 stsp static const struct got_error *
734 8da9e5f4 2019-01-12 stsp diff_fileindex_tree(struct got_fileindex *fileindex,
735 30a076bc 2019-07-27 stsp struct got_fileindex_entry **ie, const struct got_tree_entries *entries,
736 c4cdcb68 2019-04-03 stsp const char *path, const char *entry_name, struct got_repository *repo,
737 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
739 8da9e5f4 2019-01-12 stsp const struct got_error *err = NULL;
740 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te = NULL;
741 8da9e5f4 2019-01-12 stsp size_t path_len = strlen(path);
742 8da9e5f4 2019-01-12 stsp struct got_fileindex_entry *next;
744 8da9e5f4 2019-01-12 stsp te = SIMPLEQ_FIRST(&entries->head);
745 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
746 8da9e5f4 2019-01-12 stsp if (te && *ie) {
747 18831e78 2019-02-10 stsp char *te_path;
749 18831e78 2019-02-10 stsp if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
750 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
753 d572f586 2019-08-02 stsp cmp = got_path_cmp((*ie)->path, te_path,
754 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(*ie), strlen(te_path));
755 18831e78 2019-02-10 stsp free(te_path);
756 8da9e5f4 2019-01-12 stsp if (cmp == 0) {
757 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
758 c4cdcb68 2019-04-03 stsp path_len) && (entry_name == NULL ||
759 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
760 c4cdcb68 2019-04-03 stsp err = cb->diff_old_new(cb_arg, *ie, te,
762 c4cdcb68 2019-04-03 stsp if (err || entry_name)
765 50952927 2019-01-12 stsp *ie = walk_fileindex(fileindex, *ie);
766 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
767 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
768 c4cdcb68 2019-04-03 stsp } else if (cmp < 0) {
769 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
770 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path,
771 c4cdcb68 2019-04-03 stsp path_len) && (entry_name == NULL ||
772 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
773 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
774 c4cdcb68 2019-04-03 stsp if (err || entry_name)
777 8da9e5f4 2019-01-12 stsp *ie = next;
779 c4cdcb68 2019-04-03 stsp if ((entry_name == NULL ||
780 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
781 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
782 c4cdcb68 2019-04-03 stsp if (err || entry_name)
785 8da9e5f4 2019-01-12 stsp err = walk_tree(&te, fileindex, ie, te,
786 c4cdcb68 2019-04-03 stsp path, entry_name, repo, cb, cb_arg);
790 8da9e5f4 2019-01-12 stsp } else if (*ie) {
791 50952927 2019-01-12 stsp next = walk_fileindex(fileindex, *ie);
792 c4cdcb68 2019-04-03 stsp if (got_path_is_child((*ie)->path, path, path_len) &&
793 c4cdcb68 2019-04-03 stsp (entry_name == NULL ||
794 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0)) {
795 c4cdcb68 2019-04-03 stsp err = cb->diff_old(cb_arg, *ie, path);
796 c4cdcb68 2019-04-03 stsp if (err || entry_name)
799 8da9e5f4 2019-01-12 stsp *ie = next;
800 8da9e5f4 2019-01-12 stsp } else if (te) {
801 c4cdcb68 2019-04-03 stsp if (entry_name == NULL ||
802 c4cdcb68 2019-04-03 stsp strcmp(te->name, entry_name) == 0) {
803 c4cdcb68 2019-04-03 stsp err = cb->diff_new(cb_arg, te, path);
804 c4cdcb68 2019-04-03 stsp if (err || entry_name)
807 c4cdcb68 2019-04-03 stsp err = walk_tree(&te, fileindex, ie, te, path,
808 c4cdcb68 2019-04-03 stsp entry_name, repo, cb, cb_arg);
814 8da9e5f4 2019-01-12 stsp return err;
817 8da9e5f4 2019-01-12 stsp const struct got_error *
818 8da9e5f4 2019-01-12 stsp got_fileindex_diff_tree(struct got_fileindex *fileindex,
819 c4cdcb68 2019-04-03 stsp struct got_tree_object *tree, const char *path, const char *entry_name,
820 c4cdcb68 2019-04-03 stsp struct got_repository *repo,
821 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
823 30a076bc 2019-07-27 stsp struct got_fileindex_entry *ie;
824 30a076bc 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
825 30a076bc 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
826 30a076bc 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
827 30a076bc 2019-07-27 stsp return diff_fileindex_tree(fileindex, &ie,
828 30a076bc 2019-07-27 stsp got_object_tree_get_entries(tree), path, entry_name, repo,
829 30a076bc 2019-07-27 stsp cb, cb_arg);
832 d1f6d47b 2019-02-04 stsp static const struct got_error *
833 39beb6da 2019-07-27 stsp diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
834 c577a9ce 2019-07-27 stsp struct got_pathlist_head *, const char *, const char *,
835 c577a9ce 2019-07-27 stsp struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
837 c577a9ce 2019-07-27 stsp static const struct got_error *
838 c577a9ce 2019-07-27 stsp read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
840 c577a9ce 2019-07-27 stsp const struct got_error *err = NULL;
841 c577a9ce 2019-07-27 stsp struct got_pathlist_entry *new = NULL;
842 c577a9ce 2019-07-27 stsp struct dirent *dep = NULL;
843 c577a9ce 2019-07-27 stsp struct dirent *de = NULL;
846 c577a9ce 2019-07-27 stsp de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
847 c577a9ce 2019-07-27 stsp if (de == NULL) {
848 c577a9ce 2019-07-27 stsp err = got_error_from_errno("malloc");
852 c577a9ce 2019-07-27 stsp if (readdir_r(dir, de, &dep) != 0) {
853 c577a9ce 2019-07-27 stsp err = got_error_from_errno("readdir_r");
857 c577a9ce 2019-07-27 stsp if (dep == NULL) {
862 c577a9ce 2019-07-27 stsp if (strcmp(de->d_name, ".") == 0 ||
863 c577a9ce 2019-07-27 stsp strcmp(de->d_name, "..") == 0 ||
864 c577a9ce 2019-07-27 stsp (path[0] == '\0' &&
865 c577a9ce 2019-07-27 stsp strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
870 c577a9ce 2019-07-27 stsp err = got_pathlist_insert(&new, dirlist, de->d_name, de);
875 c577a9ce 2019-07-27 stsp if (new == NULL) {
876 c577a9ce 2019-07-27 stsp err = got_error(GOT_ERR_DIR_DUP_ENTRY);
882 c577a9ce 2019-07-27 stsp return err;
886 c577a9ce 2019-07-27 stsp free_dirlist(struct got_pathlist_head *dirlist)
888 c577a9ce 2019-07-27 stsp struct got_pathlist_entry *dle;
890 c577a9ce 2019-07-27 stsp TAILQ_FOREACH(dle, dirlist, entry)
891 c577a9ce 2019-07-27 stsp free(dle->data);
892 c577a9ce 2019-07-27 stsp got_pathlist_free(dirlist);
895 d1f6d47b 2019-02-04 stsp static const struct got_error *
896 f5d3d7af 2019-02-05 stsp walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
897 f5d3d7af 2019-02-05 stsp struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
898 39beb6da 2019-07-27 stsp const char *path, const char *rootpath, struct got_repository *repo,
899 39beb6da 2019-07-27 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
901 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
902 f5d3d7af 2019-02-05 stsp struct dirent *de = dle->data;
904 573463cc 2019-04-06 stsp *next = NULL;
906 f5d3d7af 2019-02-05 stsp if (de->d_type == DT_DIR) {
907 d1f6d47b 2019-02-04 stsp char *subpath;
908 c7f4312f 2019-02-05 stsp char *subdirpath;
909 d1f6d47b 2019-02-04 stsp DIR *subdir;
910 c577a9ce 2019-07-27 stsp struct got_pathlist_head subdirlist;
912 c577a9ce 2019-07-27 stsp TAILQ_INIT(&subdirlist);
914 d1f6d47b 2019-02-04 stsp if (asprintf(&subpath, "%s%s%s", path,
915 f5d3d7af 2019-02-05 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
916 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
918 c7f4312f 2019-02-05 stsp if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
919 c7f4312f 2019-02-05 stsp free(subpath);
920 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
923 c7f4312f 2019-02-05 stsp subdir = opendir(subdirpath);
924 d1f6d47b 2019-02-04 stsp if (subdir == NULL) {
925 d1f6d47b 2019-02-04 stsp free(subpath);
926 c7f4312f 2019-02-05 stsp free(subdirpath);
927 638f9024 2019-05-13 stsp return got_error_from_errno2("opendir", subdirpath);
930 c577a9ce 2019-07-27 stsp err = read_dirlist(&subdirlist, subdir, subdirpath);
932 c577a9ce 2019-07-27 stsp free(subpath);
933 c577a9ce 2019-07-27 stsp free(subdirpath);
934 c577a9ce 2019-07-27 stsp closedir(subdir);
935 c577a9ce 2019-07-27 stsp return err;
937 39beb6da 2019-07-27 stsp err = diff_fileindex_dir(fileindex, ie, &subdirlist, rootpath,
938 39beb6da 2019-07-27 stsp subpath, repo, cb, cb_arg);
939 d1f6d47b 2019-02-04 stsp free(subpath);
940 c7f4312f 2019-02-05 stsp free(subdirpath);
941 d1f6d47b 2019-02-04 stsp closedir(subdir);
942 c577a9ce 2019-07-27 stsp free_dirlist(&subdirlist);
944 d1f6d47b 2019-02-04 stsp return err;
947 f5d3d7af 2019-02-05 stsp *next = TAILQ_NEXT(dle, entry);
948 d1f6d47b 2019-02-04 stsp return NULL;
951 d1f6d47b 2019-02-04 stsp static const struct got_error *
952 d1f6d47b 2019-02-04 stsp diff_fileindex_dir(struct got_fileindex *fileindex,
953 39beb6da 2019-07-27 stsp struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
954 39beb6da 2019-07-27 stsp const char *rootpath, const char *path, struct got_repository *repo,
955 39beb6da 2019-07-27 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
957 d1f6d47b 2019-02-04 stsp const struct got_error *err = NULL;
958 d1f6d47b 2019-02-04 stsp struct dirent *de = NULL;
959 d1f6d47b 2019-02-04 stsp size_t path_len = strlen(path);
960 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *dle;
962 c577a9ce 2019-07-27 stsp dle = TAILQ_FIRST(dirlist);
963 500cd40f 2019-02-04 stsp while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
964 500cd40f 2019-02-04 stsp if (dle && *ie) {
965 18831e78 2019-02-10 stsp char *de_path;
967 f5d3d7af 2019-02-05 stsp de = dle->data;
968 18831e78 2019-02-10 stsp if (asprintf(&de_path, "%s/%s", path,
969 18831e78 2019-02-10 stsp de->d_name) == -1) {
970 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
973 d572f586 2019-08-02 stsp cmp = got_path_cmp((*ie)->path, de_path,
974 4d555405 2019-08-03 stsp got_fileindex_entry_path_len(*ie),
975 4d555405 2019-08-03 stsp strlen(path) + 1 + de->d_namlen);
976 18831e78 2019-02-10 stsp free(de_path);
977 d1f6d47b 2019-02-04 stsp if (cmp == 0) {
978 f5d3d7af 2019-02-05 stsp err = cb->diff_old_new(cb_arg, *ie, de, path);
981 d1f6d47b 2019-02-04 stsp *ie = walk_fileindex(fileindex, *ie);
982 500cd40f 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
983 39beb6da 2019-07-27 stsp rootpath, repo, cb, cb_arg);
984 d1f6d47b 2019-02-04 stsp } else if (cmp < 0 ) {
985 d1f6d47b 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
988 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
990 f5d3d7af 2019-02-05 stsp err = cb->diff_new(cb_arg, de, path);
993 500cd40f 2019-02-04 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
994 39beb6da 2019-07-27 stsp rootpath, repo, cb, cb_arg);
998 554b91b1 2019-02-04 stsp } else if (*ie) {
999 554b91b1 2019-02-04 stsp err = cb->diff_old(cb_arg, *ie, path);
1002 c577a9ce 2019-07-27 stsp *ie = walk_fileindex(fileindex, *ie);
1003 554b91b1 2019-02-04 stsp } else if (dle) {
1004 763e1377 2019-02-05 stsp de = dle->data;
1005 f5d3d7af 2019-02-05 stsp err = cb->diff_new(cb_arg, de, path);
1008 39beb6da 2019-07-27 stsp err = walk_dir(&dle, fileindex, ie, dle, path,
1009 c7f4312f 2019-02-05 stsp rootpath, repo, cb, cb_arg);
1015 d1f6d47b 2019-02-04 stsp return err;
1018 d1f6d47b 2019-02-04 stsp const struct got_error *
1019 c7f4312f 2019-02-05 stsp got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
1020 927df6b7 2019-02-10 stsp const char *rootpath, const char *path, struct got_repository *repo,
1021 c7f4312f 2019-02-05 stsp struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1023 c577a9ce 2019-07-27 stsp const struct got_error *err;
1024 c577a9ce 2019-07-27 stsp struct got_fileindex_entry *ie;
1025 c577a9ce 2019-07-27 stsp struct got_pathlist_head dirlist;
1027 c577a9ce 2019-07-27 stsp TAILQ_INIT(&dirlist);
1028 c577a9ce 2019-07-27 stsp err = read_dirlist(&dirlist, rootdir, path);
1030 c577a9ce 2019-07-27 stsp return err;
1031 c577a9ce 2019-07-27 stsp ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1032 c577a9ce 2019-07-27 stsp while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1033 c577a9ce 2019-07-27 stsp ie = walk_fileindex(fileindex, ie);
1034 39beb6da 2019-07-27 stsp err = diff_fileindex_dir(fileindex, &ie, &dirlist, rootpath, path,
1035 39beb6da 2019-07-27 stsp repo, cb, cb_arg);
1036 c577a9ce 2019-07-27 stsp free_dirlist(&dirlist);
1037 c577a9ce 2019-07-27 stsp return err;
1040 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);