File: | got/../lib/got_lib_fileindex.h |
Warning: | line 88, column 22 Access to field 'path' results in a dereference of a null pointer (loaded from variable 'e1') |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* | |||
2 | * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org> | |||
3 | * | |||
4 | * Permission to use, copy, modify, and distribute this software for any | |||
5 | * purpose with or without fee is hereby granted, provided that the above | |||
6 | * copyright notice and this permission notice appear in all copies. | |||
7 | * | |||
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |||
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |||
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |||
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |||
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |||
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |||
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |||
15 | */ | |||
16 | ||||
17 | #include <sys/queue.h> | |||
18 | #include <sys/tree.h> | |||
19 | #include <sys/stat.h> | |||
20 | ||||
21 | #include <errno(*__errno()).h> | |||
22 | #include <dirent.h> | |||
23 | #include <fcntl.h> | |||
24 | #include <stdio.h> | |||
25 | #include <stdlib.h> | |||
26 | #include <string.h> | |||
27 | #include <sha1.h> | |||
28 | #include <endian.h> | |||
29 | #include <limits.h> | |||
30 | #include <unistd.h> | |||
31 | #include <uuid.h> | |||
32 | ||||
33 | #include "got_error.h" | |||
34 | #include "got_object.h" | |||
35 | #include "got_path.h" | |||
36 | ||||
37 | #include "got_lib_fileindex.h" | |||
38 | #include "got_lib_worktree.h" | |||
39 | ||||
40 | /* got_fileindex_entry flags */ | |||
41 | #define GOT_FILEIDX_F_PATH_LEN0x00000fff 0x00000fff | |||
42 | #define GOT_FILEIDX_F_STAGE0x0000f000 0x0000f000 | |||
43 | #define GOT_FILEIDX_F_STAGE_SHIFT12 12 | |||
44 | #define GOT_FILEIDX_F_NOT_FLUSHED0x00010000 0x00010000 | |||
45 | #define GOT_FILEIDX_F_NO_BLOB0x00020000 0x00020000 | |||
46 | #define GOT_FILEIDX_F_NO_COMMIT0x00040000 0x00040000 | |||
47 | #define GOT_FILEIDX_F_NO_FILE_ON_DISK0x00080000 0x00080000 | |||
48 | #define GOT_FILEIDX_F_REMOVE_ON_FLUSH0x00100000 0x00100000 | |||
49 | ||||
50 | struct got_fileindex { | |||
51 | struct got_fileindex_tree entries; | |||
52 | int nentries; /* Does not include entries marked for removal. */ | |||
53 | #define GOT_FILEIDX_MAX_ENTRIES2147483647 INT_MAX2147483647 | |||
54 | }; | |||
55 | ||||
56 | mode_t | |||
57 | got_fileindex_entry_perms_get(struct got_fileindex_entry *ie) | |||
58 | { | |||
59 | return ((ie->mode & GOT_FILEIDX_MODE_PERMS0xfff0) >> | |||
60 | GOT_FILEIDX_MODE_PERMS_SHIFT4); | |||
61 | } | |||
62 | ||||
63 | static void | |||
64 | fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode) | |||
65 | { | |||
66 | ie->mode &= ~GOT_FILEIDX_MODE_PERMS0xfff0; | |||
67 | ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT4) & | |||
68 | GOT_FILEIDX_MODE_PERMS0xfff0); | |||
69 | } | |||
70 | ||||
71 | mode_t | |||
72 | got_fileindex_perms_to_st(struct got_fileindex_entry *ie) | |||
73 | { | |||
74 | mode_t perms = got_fileindex_entry_perms_get(ie); | |||
75 | int type = got_fileindex_entry_filetype_get(ie); | |||
76 | uint32_t ftype; | |||
77 | ||||
78 | if (type == GOT_FILEIDX_MODE_REGULAR_FILE1 || | |||
79 | type == GOT_FILEIDX_MODE_BAD_SYMLINK3) | |||
80 | ftype = S_IFREG0100000; | |||
81 | else | |||
82 | ftype = S_IFLNK0120000; | |||
83 | ||||
84 | return (ftype | (perms & (S_IRWXU0000700 | S_IRWXG0000070 | S_IRWXO0000007))); | |||
85 | } | |||
86 | ||||
87 | const struct got_error * | |||
88 | got_fileindex_entry_update(struct got_fileindex_entry *ie, | |||
89 | int wt_fd, const char *ondisk_path, uint8_t *blob_sha1, | |||
90 | uint8_t *commit_sha1, int update_timestamps) | |||
91 | { | |||
92 | struct stat sb; | |||
93 | ||||
94 | if (fstatat(wt_fd, ondisk_path, &sb, AT_SYMLINK_NOFOLLOW0x02) != 0) { | |||
95 | if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK0x00080000) && | |||
96 | errno(*__errno()) == ENOENT2)) | |||
97 | return got_error_from_errno2("fstatat", ondisk_path); | |||
98 | sb.st_mode = GOT_DEFAULT_FILE_MODE(0100000 | 0000400|0000200 | 0000040 | 0000004); | |||
99 | } else { | |||
100 | if (sb.st_mode & S_IFDIR0040000) | |||
101 | return got_error_set_errno(EISDIR21, ondisk_path); | |||
102 | ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK0x00080000; | |||
103 | } | |||
104 | ||||
105 | ||||
106 | if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK0x00080000) == 0) { | |||
107 | if (update_timestamps) { | |||
108 | ie->ctime_sec = sb.st_ctim.tv_sec; | |||
109 | ie->ctime_nsec = sb.st_ctim.tv_nsec; | |||
110 | ie->mtime_sec = sb.st_mtim.tv_sec; | |||
111 | ie->mtime_nsec = sb.st_mtim.tv_nsec; | |||
112 | } | |||
113 | ie->uid = sb.st_uid; | |||
114 | ie->gid = sb.st_gid; | |||
115 | ie->size = (sb.st_size & 0xffffffff); | |||
116 | if (S_ISLNK(sb.st_mode)((sb.st_mode & 0170000) == 0120000)) { | |||
117 | got_fileindex_entry_filetype_set(ie, | |||
118 | GOT_FILEIDX_MODE_SYMLINK2); | |||
119 | fileindex_entry_perms_set(ie, 0); | |||
120 | } else { | |||
121 | got_fileindex_entry_filetype_set(ie, | |||
122 | GOT_FILEIDX_MODE_REGULAR_FILE1); | |||
123 | fileindex_entry_perms_set(ie, | |||
124 | sb.st_mode & (S_IRWXU0000700 | S_IRWXG0000070 | S_IRWXO0000007)); | |||
125 | } | |||
126 | } | |||
127 | ||||
128 | if (blob_sha1) { | |||
129 | memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH20); | |||
130 | ie->flags &= ~GOT_FILEIDX_F_NO_BLOB0x00020000; | |||
131 | } else | |||
132 | ie->flags |= GOT_FILEIDX_F_NO_BLOB0x00020000; | |||
133 | ||||
134 | if (commit_sha1) { | |||
135 | memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH20); | |||
136 | ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT0x00040000; | |||
137 | } else | |||
138 | ie->flags |= GOT_FILEIDX_F_NO_COMMIT0x00040000; | |||
139 | ||||
140 | return NULL((void *)0); | |||
141 | } | |||
142 | ||||
143 | void | |||
144 | got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie) | |||
145 | { | |||
146 | ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK0x00080000; | |||
147 | } | |||
148 | ||||
149 | const struct got_error * | |||
150 | got_fileindex_entry_alloc(struct got_fileindex_entry **ie, | |||
151 | const char *relpath) | |||
152 | { | |||
153 | size_t len; | |||
154 | ||||
155 | *ie = calloc(1, sizeof(**ie)); | |||
156 | if (*ie == NULL((void *)0)) | |||
157 | return got_error_from_errno("calloc"); | |||
158 | ||||
159 | (*ie)->path = strdup(relpath); | |||
160 | if ((*ie)->path == NULL((void *)0)) { | |||
161 | const struct got_error *err = got_error_from_errno("strdup"); | |||
162 | free(*ie); | |||
163 | *ie = NULL((void *)0); | |||
164 | return err; | |||
165 | } | |||
166 | ||||
167 | len = strlen(relpath); | |||
168 | if (len > GOT_FILEIDX_F_PATH_LEN0x00000fff) | |||
169 | len = GOT_FILEIDX_F_PATH_LEN0x00000fff; | |||
170 | (*ie)->flags |= len; | |||
171 | ||||
172 | return NULL((void *)0); | |||
173 | } | |||
174 | ||||
175 | void | |||
176 | got_fileindex_entry_free(struct got_fileindex_entry *ie) | |||
177 | { | |||
178 | free(ie->path); | |||
179 | free(ie); | |||
180 | } | |||
181 | ||||
182 | size_t | |||
183 | got_fileindex_entry_path_len(const struct got_fileindex_entry *ie) | |||
184 | { | |||
185 | return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN0x00000fff); | |||
186 | } | |||
187 | ||||
188 | uint32_t | |||
189 | got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie) | |||
190 | { | |||
191 | return ((ie->flags & GOT_FILEIDX_F_STAGE0x0000f000) >> GOT_FILEIDX_F_STAGE_SHIFT12); | |||
192 | } | |||
193 | ||||
194 | void | |||
195 | got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage) | |||
196 | { | |||
197 | ie->flags &= ~GOT_FILEIDX_F_STAGE0x0000f000; | |||
198 | ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT12) & | |||
199 | GOT_FILEIDX_F_STAGE0x0000f000); | |||
200 | } | |||
201 | ||||
202 | int | |||
203 | got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie) | |||
204 | { | |||
205 | return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK0x0003); | |||
206 | } | |||
207 | ||||
208 | void | |||
209 | got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type) | |||
210 | { | |||
211 | ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK0x0003; | |||
212 | ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK0x0003); | |||
213 | } | |||
214 | ||||
215 | void | |||
216 | got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie, int type) | |||
217 | { | |||
218 | ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED0x000c; | |||
219 | ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT2) & | |||
220 | GOT_FILEIDX_MODE_FILE_TYPE_STAGED0x000c); | |||
221 | } | |||
222 | ||||
223 | int | |||
224 | got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie) | |||
225 | { | |||
226 | return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED0x000c) >> | |||
227 | GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT2; | |||
228 | } | |||
229 | ||||
230 | int | |||
231 | got_fileindex_entry_has_blob(struct got_fileindex_entry *ie) | |||
232 | { | |||
233 | return (ie->flags & GOT_FILEIDX_F_NO_BLOB0x00020000) == 0; | |||
234 | } | |||
235 | ||||
236 | int | |||
237 | got_fileindex_entry_has_commit(struct got_fileindex_entry *ie) | |||
238 | { | |||
239 | return (ie->flags & GOT_FILEIDX_F_NO_COMMIT0x00040000) == 0; | |||
240 | } | |||
241 | ||||
242 | int | |||
243 | got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie) | |||
244 | { | |||
245 | return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK0x00080000) == 0; | |||
246 | } | |||
247 | ||||
248 | static const struct got_error * | |||
249 | add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie) | |||
250 | { | |||
251 | if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES2147483647) | |||
252 | return got_error(GOT_ERR_NO_SPACE9); | |||
253 | ||||
254 | RB_INSERT(got_fileindex_tree, &fileindex->entries, ie)got_fileindex_tree_RB_INSERT(&fileindex->entries, ie); | |||
255 | fileindex->nentries++; | |||
256 | return NULL((void *)0); | |||
257 | } | |||
258 | ||||
259 | const struct got_error * | |||
260 | got_fileindex_entry_add(struct got_fileindex *fileindex, | |||
261 | struct got_fileindex_entry *ie) | |||
262 | { | |||
263 | /* Flag this entry until it gets written out to disk. */ | |||
264 | ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED0x00010000; | |||
265 | ||||
266 | return add_entry(fileindex, ie); | |||
267 | } | |||
268 | ||||
269 | void | |||
270 | got_fileindex_entry_remove(struct got_fileindex *fileindex, | |||
271 | struct got_fileindex_entry *ie) | |||
272 | { | |||
273 | /* | |||
274 | * Removing an entry from the RB tree immediately breaks | |||
275 | * in-progress iterations over file index entries. | |||
276 | * So flag this entry for removal and remove it once the index | |||
277 | * is written out to disk. Meanwhile, pretend this entry no longer | |||
278 | * exists if we get queried for it again before then. | |||
279 | */ | |||
280 | ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH0x00100000; | |||
281 | fileindex->nentries--; | |||
282 | } | |||
283 | ||||
284 | struct got_fileindex_entry * | |||
285 | got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path, | |||
286 | size_t path_len) | |||
287 | { | |||
288 | struct got_fileindex_entry *ie; | |||
289 | struct got_fileindex_entry key; | |||
290 | memset(&key, 0, sizeof(key)); | |||
291 | key.path = (char *)path; | |||
292 | key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN0x00000fff); | |||
293 | ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key)got_fileindex_tree_RB_FIND(&fileindex->entries, &key ); | |||
294 | if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH0x00100000)) | |||
295 | return NULL((void *)0); | |||
296 | return ie; | |||
297 | } | |||
298 | ||||
299 | const struct got_error * | |||
300 | got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex, | |||
301 | got_fileindex_cb cb, void *cb_arg) | |||
302 | { | |||
303 | const struct got_error *err; | |||
304 | struct got_fileindex_entry *ie, *tmp; | |||
305 | ||||
306 | RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp)for ((ie) = got_fileindex_tree_RB_MINMAX(&fileindex->entries , -1); ((ie) != ((void *)0)) && ((tmp) = got_fileindex_tree_RB_NEXT (ie), 1); (ie) = (tmp)) { | |||
307 | if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH0x00100000) | |||
308 | continue; | |||
309 | err = (*cb)(cb_arg, ie); | |||
310 | if (err) | |||
311 | return err; | |||
312 | } | |||
313 | return NULL((void *)0); | |||
314 | } | |||
315 | ||||
316 | struct got_fileindex * | |||
317 | got_fileindex_alloc(void) | |||
318 | { | |||
319 | struct got_fileindex *fileindex; | |||
320 | ||||
321 | fileindex = calloc(1, sizeof(*fileindex)); | |||
322 | if (fileindex == NULL((void *)0)) | |||
323 | return NULL((void *)0); | |||
324 | ||||
325 | RB_INIT(&fileindex->entries)do { (&fileindex->entries)->rbh_root = ((void *)0); } while (0); | |||
326 | return fileindex; | |||
327 | } | |||
328 | ||||
329 | void | |||
330 | got_fileindex_free(struct got_fileindex *fileindex) | |||
331 | { | |||
332 | struct got_fileindex_entry *ie; | |||
333 | ||||
334 | while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries)got_fileindex_tree_RB_MINMAX(&fileindex->entries, -1))) { | |||
335 | RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie)got_fileindex_tree_RB_REMOVE(&fileindex->entries, ie); | |||
336 | got_fileindex_entry_free(ie); | |||
337 | } | |||
338 | free(fileindex); | |||
339 | } | |||
340 | ||||
341 | static const struct got_error * | |||
342 | write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile) | |||
343 | { | |||
344 | size_t n; | |||
345 | ||||
346 | val = htobe64(val)(__uint64_t)(__builtin_constant_p(val) ? (__uint64_t)((((__uint64_t )(val) & 0xff) << 56) | ((__uint64_t)(val) & 0xff00ULL ) << 40 | ((__uint64_t)(val) & 0xff0000ULL) << 24 | ((__uint64_t)(val) & 0xff000000ULL) << 8 | (( __uint64_t)(val) & 0xff00000000ULL) >> 8 | ((__uint64_t )(val) & 0xff0000000000ULL) >> 24 | ((__uint64_t)(val ) & 0xff000000000000ULL) >> 40 | ((__uint64_t)(val) & 0xff00000000000000ULL) >> 56) : __swap64md(val)); | |||
347 | SHA1Update(ctx, (uint8_t *)&val, sizeof(val)); | |||
348 | n = fwrite(&val, 1, sizeof(val), outfile); | |||
349 | if (n != sizeof(val)) | |||
350 | return got_ferror(outfile, GOT_ERR_IO6); | |||
351 | return NULL((void *)0); | |||
352 | } | |||
353 | ||||
354 | static const struct got_error * | |||
355 | write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile) | |||
356 | { | |||
357 | size_t n; | |||
358 | ||||
359 | val = htobe32(val)(__uint32_t)(__builtin_constant_p(val) ? (__uint32_t)(((__uint32_t )(val) & 0xff) << 24 | ((__uint32_t)(val) & 0xff00 ) << 8 | ((__uint32_t)(val) & 0xff0000) >> 8 | ((__uint32_t)(val) & 0xff000000) >> 24) : __swap32md (val)); | |||
360 | SHA1Update(ctx, (uint8_t *)&val, sizeof(val)); | |||
361 | n = fwrite(&val, 1, sizeof(val), outfile); | |||
362 | if (n != sizeof(val)) | |||
363 | return got_ferror(outfile, GOT_ERR_IO6); | |||
364 | return NULL((void *)0); | |||
365 | } | |||
366 | ||||
367 | static const struct got_error * | |||
368 | write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile) | |||
369 | { | |||
370 | size_t n; | |||
371 | ||||
372 | val = htobe16(val)(__uint16_t)(__builtin_constant_p(val) ? (__uint16_t)(((__uint16_t )(val) & 0xffU) << 8 | ((__uint16_t)(val) & 0xff00U ) >> 8) : __swap16md(val)); | |||
373 | SHA1Update(ctx, (uint8_t *)&val, sizeof(val)); | |||
374 | n = fwrite(&val, 1, sizeof(val), outfile); | |||
375 | if (n != sizeof(val)) | |||
376 | return got_ferror(outfile, GOT_ERR_IO6); | |||
377 | return NULL((void *)0); | |||
378 | } | |||
379 | ||||
380 | static const struct got_error * | |||
381 | write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile) | |||
382 | { | |||
383 | size_t n, len, pad = 0; | |||
384 | static const uint8_t zero[8] = { 0 }; | |||
385 | ||||
386 | len = strlen(path); | |||
387 | while ((len + pad) % 8 != 0) | |||
388 | pad++; | |||
389 | if (pad == 0) | |||
390 | pad = 8; /* NUL-terminate */ | |||
391 | ||||
392 | SHA1Update(ctx, path, len); | |||
393 | n = fwrite(path, 1, len, outfile); | |||
394 | if (n != len) | |||
395 | return got_ferror(outfile, GOT_ERR_IO6); | |||
396 | SHA1Update(ctx, zero, pad); | |||
397 | n = fwrite(zero, 1, pad, outfile); | |||
398 | if (n != pad) | |||
399 | return got_ferror(outfile, GOT_ERR_IO6); | |||
400 | return NULL((void *)0); | |||
401 | } | |||
402 | ||||
403 | static const struct got_error * | |||
404 | write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie, | |||
405 | FILE *outfile) | |||
406 | { | |||
407 | const struct got_error *err; | |||
408 | size_t n; | |||
409 | uint32_t stage; | |||
410 | ||||
411 | err = write_fileindex_val64(ctx, ie->ctime_sec, outfile); | |||
412 | if (err) | |||
413 | return err; | |||
414 | err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile); | |||
415 | if (err) | |||
416 | return err; | |||
417 | err = write_fileindex_val64(ctx, ie->mtime_sec, outfile); | |||
418 | if (err) | |||
419 | return err; | |||
420 | err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile); | |||
421 | if (err) | |||
422 | return err; | |||
423 | ||||
424 | err = write_fileindex_val32(ctx, ie->uid, outfile); | |||
425 | if (err) | |||
426 | return err; | |||
427 | err = write_fileindex_val32(ctx, ie->gid, outfile); | |||
428 | if (err) | |||
429 | return err; | |||
430 | err = write_fileindex_val32(ctx, ie->size, outfile); | |||
431 | if (err) | |||
432 | return err; | |||
433 | ||||
434 | err = write_fileindex_val16(ctx, ie->mode, outfile); | |||
435 | if (err) | |||
436 | return err; | |||
437 | ||||
438 | SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH20); | |||
439 | n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH20, outfile); | |||
440 | if (n != SHA1_DIGEST_LENGTH20) | |||
441 | return got_ferror(outfile, GOT_ERR_IO6); | |||
442 | ||||
443 | SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH20); | |||
444 | n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH20, outfile); | |||
445 | if (n != SHA1_DIGEST_LENGTH20) | |||
446 | return got_ferror(outfile, GOT_ERR_IO6); | |||
447 | ||||
448 | err = write_fileindex_val32(ctx, ie->flags, outfile); | |||
449 | if (err) | |||
450 | return err; | |||
451 | ||||
452 | err = write_fileindex_path(ctx, ie->path, outfile); | |||
453 | if (err) | |||
454 | return err; | |||
455 | ||||
456 | stage = got_fileindex_entry_stage_get(ie); | |||
457 | if (stage == GOT_FILEIDX_STAGE_MODIFY1 || | |||
458 | stage == GOT_FILEIDX_STAGE_ADD2) { | |||
459 | SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH20); | |||
460 | n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH20, | |||
461 | outfile); | |||
462 | if (n != SHA1_DIGEST_LENGTH20) | |||
463 | return got_ferror(outfile, GOT_ERR_IO6); | |||
464 | } | |||
465 | ||||
466 | return NULL((void *)0); | |||
467 | } | |||
468 | ||||
469 | const struct got_error * | |||
470 | got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile) | |||
471 | { | |||
472 | const struct got_error *err = NULL((void *)0); | |||
473 | struct got_fileindex_hdr hdr; | |||
474 | SHA1_CTX ctx; | |||
475 | uint8_t sha1[SHA1_DIGEST_LENGTH20]; | |||
476 | size_t n; | |||
477 | struct got_fileindex_entry *ie, *tmp; | |||
478 | ||||
479 | SHA1Init(&ctx); | |||
480 | ||||
481 | hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE)(__uint32_t)(__builtin_constant_p(0x676f7449) ? (__uint32_t)( ((__uint32_t)(0x676f7449) & 0xff) << 24 | ((__uint32_t )(0x676f7449) & 0xff00) << 8 | ((__uint32_t)(0x676f7449 ) & 0xff0000) >> 8 | ((__uint32_t)(0x676f7449) & 0xff000000) >> 24) : __swap32md(0x676f7449)); | |||
482 | hdr.version = htobe32(GOT_FILE_INDEX_VERSION)(__uint32_t)(__builtin_constant_p(2) ? (__uint32_t)(((__uint32_t )(2) & 0xff) << 24 | ((__uint32_t)(2) & 0xff00) << 8 | ((__uint32_t)(2) & 0xff0000) >> 8 | ( (__uint32_t)(2) & 0xff000000) >> 24) : __swap32md(2 )); | |||
483 | hdr.nentries = htobe32(fileindex->nentries)(__uint32_t)(__builtin_constant_p(fileindex->nentries) ? ( __uint32_t)(((__uint32_t)(fileindex->nentries) & 0xff) << 24 | ((__uint32_t)(fileindex->nentries) & 0xff00 ) << 8 | ((__uint32_t)(fileindex->nentries) & 0xff0000 ) >> 8 | ((__uint32_t)(fileindex->nentries) & 0xff000000 ) >> 24) : __swap32md(fileindex->nentries)); | |||
484 | ||||
485 | SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature)); | |||
486 | SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version)); | |||
487 | SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries)); | |||
488 | n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile); | |||
489 | if (n != sizeof(hdr.signature)) | |||
490 | return got_ferror(outfile, GOT_ERR_IO6); | |||
491 | n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile); | |||
492 | if (n != sizeof(hdr.version)) | |||
493 | return got_ferror(outfile, GOT_ERR_IO6); | |||
494 | n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile); | |||
495 | if (n != sizeof(hdr.nentries)) | |||
496 | return got_ferror(outfile, GOT_ERR_IO6); | |||
497 | ||||
498 | RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp)for ((ie) = got_fileindex_tree_RB_MINMAX(&fileindex->entries , -1); ((ie) != ((void *)0)) && ((tmp) = got_fileindex_tree_RB_NEXT (ie), 1); (ie) = (tmp)) { | |||
499 | ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED0x00010000; | |||
500 | if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH0x00100000) { | |||
501 | RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie)got_fileindex_tree_RB_REMOVE(&fileindex->entries, ie); | |||
502 | got_fileindex_entry_free(ie); | |||
503 | continue; | |||
504 | } | |||
505 | err = write_fileindex_entry(&ctx, ie, outfile); | |||
506 | if (err) | |||
507 | return err; | |||
508 | } | |||
509 | ||||
510 | SHA1Final(sha1, &ctx); | |||
511 | n = fwrite(sha1, 1, sizeof(sha1), outfile); | |||
512 | if (n != sizeof(sha1)) | |||
513 | return got_ferror(outfile, GOT_ERR_IO6); | |||
514 | ||||
515 | if (fflush(outfile) != 0) | |||
516 | return got_error_from_errno("fflush"); | |||
517 | ||||
518 | return NULL((void *)0); | |||
519 | } | |||
520 | ||||
521 | static const struct got_error * | |||
522 | read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile) | |||
523 | { | |||
524 | size_t n; | |||
525 | ||||
526 | n = fread(val, 1, sizeof(*val), infile); | |||
527 | if (n != sizeof(*val)) | |||
528 | return got_ferror(infile, GOT_ERR_FILEIDX_BAD56); | |||
529 | SHA1Update(ctx, (uint8_t *)val, sizeof(*val)); | |||
530 | *val = be64toh(*val)(__uint64_t)(__builtin_constant_p(*val) ? (__uint64_t)((((__uint64_t )(*val) & 0xff) << 56) | ((__uint64_t)(*val) & 0xff00ULL ) << 40 | ((__uint64_t)(*val) & 0xff0000ULL) << 24 | ((__uint64_t)(*val) & 0xff000000ULL) << 8 | ( (__uint64_t)(*val) & 0xff00000000ULL) >> 8 | ((__uint64_t )(*val) & 0xff0000000000ULL) >> 24 | ((__uint64_t)( *val) & 0xff000000000000ULL) >> 40 | ((__uint64_t)( *val) & 0xff00000000000000ULL) >> 56) : __swap64md( *val)); | |||
531 | return NULL((void *)0); | |||
532 | } | |||
533 | ||||
534 | static const struct got_error * | |||
535 | read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile) | |||
536 | { | |||
537 | size_t n; | |||
538 | ||||
539 | n = fread(val, 1, sizeof(*val), infile); | |||
540 | if (n != sizeof(*val)) | |||
541 | return got_ferror(infile, GOT_ERR_FILEIDX_BAD56); | |||
542 | SHA1Update(ctx, (uint8_t *)val, sizeof(*val)); | |||
543 | *val = be32toh(*val)(__uint32_t)(__builtin_constant_p(*val) ? (__uint32_t)(((__uint32_t )(*val) & 0xff) << 24 | ((__uint32_t)(*val) & 0xff00 ) << 8 | ((__uint32_t)(*val) & 0xff0000) >> 8 | ((__uint32_t)(*val) & 0xff000000) >> 24) : __swap32md (*val)); | |||
544 | return NULL((void *)0); | |||
545 | } | |||
546 | ||||
547 | static const struct got_error * | |||
548 | read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile) | |||
549 | { | |||
550 | size_t n; | |||
551 | ||||
552 | n = fread(val, 1, sizeof(*val), infile); | |||
553 | if (n != sizeof(*val)) | |||
554 | return got_ferror(infile, GOT_ERR_FILEIDX_BAD56); | |||
555 | SHA1Update(ctx, (uint8_t *)val, sizeof(*val)); | |||
556 | *val = be16toh(*val)(__uint16_t)(__builtin_constant_p(*val) ? (__uint16_t)(((__uint16_t )(*val) & 0xffU) << 8 | ((__uint16_t)(*val) & 0xff00U ) >> 8) : __swap16md(*val)); | |||
557 | return NULL((void *)0); | |||
558 | } | |||
559 | ||||
560 | static const struct got_error * | |||
561 | read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile) | |||
562 | { | |||
563 | const struct got_error *err = NULL((void *)0); | |||
564 | const size_t chunk_size = 8; | |||
565 | size_t n, len = 0, totlen = chunk_size; | |||
566 | ||||
567 | *path = malloc(totlen); | |||
568 | if (*path == NULL((void *)0)) | |||
569 | return got_error_from_errno("malloc"); | |||
570 | ||||
571 | do { | |||
572 | if (len + chunk_size > totlen) { | |||
573 | char *p = reallocarray(*path, totlen + chunk_size, 1); | |||
574 | if (p == NULL((void *)0)) { | |||
575 | err = got_error_from_errno("reallocarray"); | |||
576 | break; | |||
577 | } | |||
578 | totlen += chunk_size; | |||
579 | *path = p; | |||
580 | } | |||
581 | n = fread(*path + len, 1, chunk_size, infile); | |||
582 | if (n != chunk_size) { | |||
583 | err = got_ferror(infile, GOT_ERR_FILEIDX_BAD56); | |||
584 | break; | |||
585 | } | |||
586 | SHA1Update(ctx, *path + len, chunk_size); | |||
587 | len += chunk_size; | |||
588 | } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL((void *)0)); | |||
589 | ||||
590 | if (err) { | |||
591 | free(*path); | |||
592 | *path = NULL((void *)0); | |||
593 | } | |||
594 | return err; | |||
595 | } | |||
596 | ||||
597 | static const struct got_error * | |||
598 | read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx, | |||
599 | FILE *infile, uint32_t version) | |||
600 | { | |||
601 | const struct got_error *err; | |||
602 | struct got_fileindex_entry *ie; | |||
603 | size_t n; | |||
604 | ||||
605 | *iep = NULL((void *)0); | |||
606 | ||||
607 | ie = calloc(1, sizeof(*ie)); | |||
608 | if (ie == NULL((void *)0)) | |||
609 | return got_error_from_errno("calloc"); | |||
610 | ||||
611 | err = read_fileindex_val64(&ie->ctime_sec, ctx, infile); | |||
612 | if (err) | |||
613 | goto done; | |||
614 | err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile); | |||
615 | if (err) | |||
616 | goto done; | |||
617 | err = read_fileindex_val64(&ie->mtime_sec, ctx, infile); | |||
618 | if (err) | |||
619 | goto done; | |||
620 | err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile); | |||
621 | if (err) | |||
622 | goto done; | |||
623 | ||||
624 | err = read_fileindex_val32(&ie->uid, ctx, infile); | |||
625 | if (err) | |||
626 | goto done; | |||
627 | err = read_fileindex_val32(&ie->gid, ctx, infile); | |||
628 | if (err) | |||
629 | goto done; | |||
630 | err = read_fileindex_val32(&ie->size, ctx, infile); | |||
631 | if (err) | |||
632 | goto done; | |||
633 | ||||
634 | err = read_fileindex_val16(&ie->mode, ctx, infile); | |||
635 | if (err) | |||
636 | goto done; | |||
637 | ||||
638 | n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH20, infile); | |||
639 | if (n != SHA1_DIGEST_LENGTH20) { | |||
640 | err = got_ferror(infile, GOT_ERR_FILEIDX_BAD56); | |||
641 | goto done; | |||
642 | } | |||
643 | SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH20); | |||
644 | ||||
645 | n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH20, infile); | |||
646 | if (n != SHA1_DIGEST_LENGTH20) { | |||
647 | err = got_ferror(infile, GOT_ERR_FILEIDX_BAD56); | |||
648 | goto done; | |||
649 | } | |||
650 | SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH20); | |||
651 | ||||
652 | err = read_fileindex_val32(&ie->flags, ctx, infile); | |||
653 | if (err) | |||
654 | goto done; | |||
655 | ||||
656 | err = read_fileindex_path(&ie->path, ctx, infile); | |||
657 | if (err) | |||
658 | goto done; | |||
659 | ||||
660 | if (version >= 2) { | |||
661 | uint32_t stage = got_fileindex_entry_stage_get(ie); | |||
662 | if (stage == GOT_FILEIDX_STAGE_MODIFY1 || | |||
663 | stage == GOT_FILEIDX_STAGE_ADD2) { | |||
664 | n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH20, | |||
665 | infile); | |||
666 | if (n != SHA1_DIGEST_LENGTH20) { | |||
667 | err = got_ferror(infile, GOT_ERR_FILEIDX_BAD56); | |||
668 | goto done; | |||
669 | } | |||
670 | SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH20); | |||
671 | } | |||
672 | } else { | |||
673 | /* GOT_FILE_INDEX_VERSION 1 does not support staging. */ | |||
674 | ie->flags &= ~GOT_FILEIDX_F_STAGE0x0000f000; | |||
675 | } | |||
676 | ||||
677 | done: | |||
678 | if (err) | |||
679 | got_fileindex_entry_free(ie); | |||
680 | else | |||
681 | *iep = ie; | |||
682 | return err; | |||
683 | } | |||
684 | ||||
685 | const struct got_error * | |||
686 | got_fileindex_read(struct got_fileindex *fileindex, FILE *infile) | |||
687 | { | |||
688 | const struct got_error *err = NULL((void *)0); | |||
689 | struct got_fileindex_hdr hdr; | |||
690 | SHA1_CTX ctx; | |||
691 | struct got_fileindex_entry *ie; | |||
692 | uint8_t sha1_expected[SHA1_DIGEST_LENGTH20]; | |||
693 | uint8_t sha1[SHA1_DIGEST_LENGTH20]; | |||
694 | size_t n; | |||
695 | int i; | |||
696 | ||||
697 | SHA1Init(&ctx); | |||
698 | ||||
699 | n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile); | |||
700 | if (n != sizeof(hdr.signature)) { | |||
| ||||
701 | if (n == 0) /* EOF */ | |||
702 | return NULL((void *)0); | |||
703 | return got_ferror(infile, GOT_ERR_FILEIDX_BAD56); | |||
704 | } | |||
705 | n = fread(&hdr.version, 1, sizeof(hdr.version), infile); | |||
706 | if (n != sizeof(hdr.version)) { | |||
707 | if (n == 0) /* EOF */ | |||
708 | return NULL((void *)0); | |||
709 | return got_ferror(infile, GOT_ERR_FILEIDX_BAD56); | |||
710 | } | |||
711 | n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile); | |||
712 | if (n != sizeof(hdr.nentries)) { | |||
713 | if (n == 0) /* EOF */ | |||
714 | return NULL((void *)0); | |||
715 | return got_ferror(infile, GOT_ERR_FILEIDX_BAD56); | |||
716 | } | |||
717 | ||||
718 | SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature)); | |||
719 | SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version)); | |||
720 | SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries)); | |||
721 | ||||
722 | hdr.signature = be32toh(hdr.signature)(__uint32_t)(__builtin_constant_p(hdr.signature) ? (__uint32_t )(((__uint32_t)(hdr.signature) & 0xff) << 24 | ((__uint32_t )(hdr.signature) & 0xff00) << 8 | ((__uint32_t)(hdr .signature) & 0xff0000) >> 8 | ((__uint32_t)(hdr.signature ) & 0xff000000) >> 24) : __swap32md(hdr.signature)); | |||
723 | hdr.version = be32toh(hdr.version)(__uint32_t)(__builtin_constant_p(hdr.version) ? (__uint32_t) (((__uint32_t)(hdr.version) & 0xff) << 24 | ((__uint32_t )(hdr.version) & 0xff00) << 8 | ((__uint32_t)(hdr.version ) & 0xff0000) >> 8 | ((__uint32_t)(hdr.version) & 0xff000000) >> 24) : __swap32md(hdr.version)); | |||
724 | hdr.nentries = be32toh(hdr.nentries)(__uint32_t)(__builtin_constant_p(hdr.nentries) ? (__uint32_t )(((__uint32_t)(hdr.nentries) & 0xff) << 24 | ((__uint32_t )(hdr.nentries) & 0xff00) << 8 | ((__uint32_t)(hdr. nentries) & 0xff0000) >> 8 | ((__uint32_t)(hdr.nentries ) & 0xff000000) >> 24) : __swap32md(hdr.nentries)); | |||
725 | ||||
726 | if (hdr.signature != GOT_FILE_INDEX_SIGNATURE0x676f7449) | |||
727 | return got_error(GOT_ERR_FILEIDX_SIG51); | |||
728 | if (hdr.version > GOT_FILE_INDEX_VERSION2) | |||
729 | return got_error(GOT_ERR_FILEIDX_VER52); | |||
730 | ||||
731 | for (i = 0; i < hdr.nentries; i++) { | |||
732 | err = read_fileindex_entry(&ie, &ctx, infile, hdr.version); | |||
733 | if (err) | |||
734 | return err; | |||
735 | err = add_entry(fileindex, ie); | |||
736 | if (err) | |||
737 | return err; | |||
738 | } | |||
739 | ||||
740 | n = fread(sha1_expected, 1, sizeof(sha1_expected), infile); | |||
741 | if (n != sizeof(sha1_expected)) | |||
742 | return got_ferror(infile, GOT_ERR_FILEIDX_BAD56); | |||
743 | SHA1Final(sha1, &ctx); | |||
744 | if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH20) != 0) | |||
745 | return got_error(GOT_ERR_FILEIDX_CSUM53); | |||
746 | ||||
747 | return NULL((void *)0); | |||
748 | } | |||
749 | ||||
750 | static struct got_fileindex_entry * | |||
751 | walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie) | |||
752 | { | |||
753 | struct got_fileindex_entry *next; | |||
754 | ||||
755 | next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie)got_fileindex_tree_RB_NEXT(ie); | |||
756 | ||||
757 | /* Skip entries which were added or removed by diff callbacks. */ | |||
758 | while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED0x00010000 | | |||
759 | GOT_FILEIDX_F_REMOVE_ON_FLUSH0x00100000))) | |||
760 | next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next)got_fileindex_tree_RB_NEXT(next); | |||
761 | ||||
762 | return next; | |||
763 | } | |||
764 | ||||
765 | static const struct got_error * | |||
766 | diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie, | |||
767 | struct got_tree_object *tree, const char *, const char *, | |||
768 | struct got_repository *, struct got_fileindex_diff_tree_cb *, void *); | |||
769 | ||||
770 | static const struct got_error * | |||
771 | walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex, | |||
772 | struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx, | |||
773 | const char *path, const char *entry_name, struct got_repository *repo, | |||
774 | struct got_fileindex_diff_tree_cb *cb, void *cb_arg) | |||
775 | { | |||
776 | const struct got_error *err = NULL((void *)0); | |||
777 | struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx); | |||
778 | ||||
779 | if (!got_object_tree_entry_is_submodule(te) && | |||
780 | S_ISDIR(got_tree_entry_get_mode(te))((got_tree_entry_get_mode(te) & 0170000) == 0040000)) { | |||
781 | char *subpath; | |||
782 | struct got_tree_object *subtree; | |||
783 | ||||
784 | if (asprintf(&subpath, "%s%s%s", path, | |||
785 | path[0] == '\0' ? "" : "/", | |||
786 | got_tree_entry_get_name(te)) == -1) | |||
787 | return got_error_from_errno("asprintf"); | |||
788 | ||||
789 | err = got_object_open_as_tree(&subtree, repo, | |||
790 | got_tree_entry_get_id(te)); | |||
791 | if (err) { | |||
792 | free(subpath); | |||
793 | return err; | |||
794 | } | |||
795 | ||||
796 | err = diff_fileindex_tree(fileindex, ie, subtree, subpath, | |||
797 | entry_name, repo, cb, cb_arg); | |||
798 | free(subpath); | |||
799 | got_object_tree_close(subtree); | |||
800 | if (err) | |||
801 | return err; | |||
802 | } | |||
803 | ||||
804 | (*tidx)++; | |||
805 | *next = got_object_tree_get_entry(tree, *tidx); | |||
806 | return NULL((void *)0); | |||
807 | } | |||
808 | ||||
809 | static const struct got_error * | |||
810 | diff_fileindex_tree(struct got_fileindex *fileindex, | |||
811 | struct got_fileindex_entry **ie, struct got_tree_object *tree, | |||
812 | const char *path, const char *entry_name, struct got_repository *repo, | |||
813 | struct got_fileindex_diff_tree_cb *cb, void *cb_arg) | |||
814 | { | |||
815 | const struct got_error *err = NULL((void *)0); | |||
816 | struct got_tree_entry *te = NULL((void *)0); | |||
817 | size_t path_len = strlen(path); | |||
818 | struct got_fileindex_entry *next; | |||
819 | int tidx = 0; | |||
820 | ||||
821 | te = got_object_tree_get_entry(tree, tidx); | |||
822 | while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) { | |||
823 | if (te && *ie) { | |||
824 | char *te_path; | |||
825 | const char *te_name = got_tree_entry_get_name(te); | |||
826 | int cmp; | |||
827 | if (asprintf(&te_path, "%s/%s", path, te_name) == -1) { | |||
828 | err = got_error_from_errno("asprintf"); | |||
829 | break; | |||
830 | } | |||
831 | cmp = got_path_cmp((*ie)->path, te_path, | |||
832 | got_fileindex_entry_path_len(*ie), strlen(te_path)); | |||
833 | free(te_path); | |||
834 | if (cmp == 0) { | |||
835 | if (got_path_is_child((*ie)->path, path, | |||
836 | path_len) && | |||
837 | !got_object_tree_entry_is_submodule(te) && | |||
838 | (entry_name == NULL((void *)0) || | |||
839 | strcmp(te_name, entry_name) == 0)) { | |||
840 | err = cb->diff_old_new(cb_arg, *ie, te, | |||
841 | path); | |||
842 | if (err || entry_name) | |||
843 | break; | |||
844 | } | |||
845 | *ie = walk_fileindex(fileindex, *ie); | |||
846 | err = walk_tree(&te, fileindex, ie, tree, &tidx, | |||
847 | path, entry_name, repo, cb, cb_arg); | |||
848 | } else if (cmp < 0) { | |||
849 | next = walk_fileindex(fileindex, *ie); | |||
850 | if (got_path_is_child((*ie)->path, path, | |||
851 | path_len) && entry_name == NULL((void *)0)) { | |||
852 | err = cb->diff_old(cb_arg, *ie, path); | |||
853 | if (err || entry_name) | |||
854 | break; | |||
855 | } | |||
856 | *ie = next; | |||
857 | } else { | |||
858 | if ((entry_name == NULL((void *)0) || | |||
859 | strcmp(te_name, entry_name) == 0)) { | |||
860 | err = cb->diff_new(cb_arg, te, path); | |||
861 | if (err || entry_name) | |||
862 | break; | |||
863 | } | |||
864 | err = walk_tree(&te, fileindex, ie, tree, &tidx, | |||
865 | path, entry_name, repo, cb, cb_arg); | |||
866 | } | |||
867 | if (err) | |||
868 | break; | |||
869 | } else if (*ie) { | |||
870 | next = walk_fileindex(fileindex, *ie); | |||
871 | if (got_path_is_child((*ie)->path, path, path_len) && | |||
872 | (entry_name == NULL((void *)0) || | |||
873 | (te && strcmp(got_tree_entry_get_name(te), | |||
874 | entry_name) == 0))) { | |||
875 | err = cb->diff_old(cb_arg, *ie, path); | |||
876 | if (err || entry_name) | |||
877 | break; | |||
878 | } | |||
879 | *ie = next; | |||
880 | } else if (te) { | |||
881 | if (!got_object_tree_entry_is_submodule(te) && | |||
882 | (entry_name == NULL((void *)0) || | |||
883 | strcmp(got_tree_entry_get_name(te), entry_name) | |||
884 | == 0)) { | |||
885 | err = cb->diff_new(cb_arg, te, path); | |||
886 | if (err || entry_name) | |||
887 | break; | |||
888 | } | |||
889 | err = walk_tree(&te, fileindex, ie, tree, &tidx, path, | |||
890 | entry_name, repo, cb, cb_arg); | |||
891 | if (err) | |||
892 | break; | |||
893 | } | |||
894 | } | |||
895 | ||||
896 | return err; | |||
897 | } | |||
898 | ||||
899 | const struct got_error * | |||
900 | got_fileindex_diff_tree(struct got_fileindex *fileindex, | |||
901 | struct got_tree_object *tree, const char *path, const char *entry_name, | |||
902 | struct got_repository *repo, | |||
903 | struct got_fileindex_diff_tree_cb *cb, void *cb_arg) | |||
904 | { | |||
905 | struct got_fileindex_entry *ie; | |||
906 | ie = RB_MIN(got_fileindex_tree, &fileindex->entries)got_fileindex_tree_RB_MINMAX(&fileindex->entries, -1); | |||
907 | while (ie && !got_path_is_child(ie->path, path, strlen(path))) | |||
908 | ie = walk_fileindex(fileindex, ie); | |||
909 | return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo, | |||
910 | cb, cb_arg); | |||
911 | } | |||
912 | ||||
913 | static const struct got_error * | |||
914 | diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **, | |||
915 | struct got_pathlist_head *, int, const char *, const char *, | |||
916 | struct got_repository *, struct got_fileindex_diff_dir_cb *, void *); | |||
917 | ||||
918 | static const struct got_error * | |||
919 | read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path) | |||
920 | { | |||
921 | const struct got_error *err = NULL((void *)0); | |||
922 | struct got_pathlist_entry *new = NULL((void *)0); | |||
923 | struct dirent *dep = NULL((void *)0); | |||
924 | struct dirent *de = NULL((void *)0); | |||
925 | ||||
926 | for (;;) { | |||
927 | de = malloc(sizeof(struct dirent) + NAME_MAX255 + 1); | |||
928 | if (de == NULL((void *)0)) { | |||
929 | err = got_error_from_errno("malloc"); | |||
930 | break; | |||
931 | } | |||
932 | ||||
933 | if (readdir_r(dir, de, &dep) != 0) { | |||
934 | err = got_error_from_errno("readdir_r"); | |||
935 | free(de); | |||
936 | break; | |||
937 | } | |||
938 | if (dep == NULL((void *)0)) { | |||
939 | free(de); | |||
940 | break; | |||
941 | } | |||
942 | ||||
943 | if (strcmp(de->d_name, ".") == 0 || | |||
944 | strcmp(de->d_name, "..") == 0 || | |||
945 | (path[0] == '\0' && | |||
946 | strcmp(de->d_name, GOT_WORKTREE_GOT_DIR".got") == 0)) { | |||
947 | free(de); | |||
948 | continue; | |||
949 | } | |||
950 | ||||
951 | err = got_pathlist_insert(&new, dirlist, de->d_name, de); | |||
952 | if (err) { | |||
953 | free(de); | |||
954 | break; | |||
955 | } | |||
956 | if (new == NULL((void *)0)) { | |||
957 | err = got_error(GOT_ERR_DIR_DUP_ENTRY59); | |||
958 | free(de); | |||
959 | break; | |||
960 | } | |||
961 | } | |||
962 | ||||
963 | return err; | |||
964 | } | |||
965 | ||||
966 | void | |||
967 | free_dirlist(struct got_pathlist_head *dirlist) | |||
968 | { | |||
969 | struct got_pathlist_entry *dle; | |||
970 | ||||
971 | TAILQ_FOREACH(dle, dirlist, entry)for((dle) = ((dirlist)->tqh_first); (dle) != ((void *)0); ( dle) = ((dle)->entry.tqe_next)) | |||
972 | free(dle->data); | |||
973 | got_pathlist_free(dirlist); | |||
974 | } | |||
975 | ||||
976 | static const struct got_error * | |||
977 | walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex, | |||
978 | struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd, | |||
979 | const char *path, const char *rootpath, struct got_repository *repo, | |||
980 | struct got_fileindex_diff_dir_cb *cb, void *cb_arg) | |||
981 | { | |||
982 | const struct got_error *err = NULL((void *)0); | |||
983 | struct dirent *de = dle->data; | |||
984 | DIR *subdir = NULL((void *)0); | |||
985 | int subdirfd = -1; | |||
986 | int type; | |||
987 | ||||
988 | *next = NULL((void *)0); | |||
989 | ||||
990 | if (de->d_type == DT_UNKNOWN0) { | |||
991 | /* Occurs on NFS mounts without "readdir plus" RPC. */ | |||
992 | char *dir_path; | |||
993 | if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1) | |||
994 | return got_error_from_errno("asprintf"); | |||
995 | err = got_path_dirent_type(&type, dir_path, de); | |||
996 | free(dir_path); | |||
997 | if (err) | |||
998 | return err; | |||
999 | } else | |||
1000 | type = de->d_type; | |||
1001 | ||||
1002 | if (type == DT_DIR4) { | |||
1003 | char *subpath; | |||
1004 | char *subdirpath; | |||
1005 | struct got_pathlist_head subdirlist; | |||
1006 | ||||
1007 | TAILQ_INIT(&subdirlist)do { (&subdirlist)->tqh_first = ((void *)0); (&subdirlist )->tqh_last = &(&subdirlist)->tqh_first; } while (0); | |||
1008 | ||||
1009 | if (asprintf(&subpath, "%s%s%s", path, | |||
1010 | path[0] == '\0' ? "" : "/", de->d_name) == -1) | |||
1011 | return got_error_from_errno("asprintf"); | |||
1012 | ||||
1013 | if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) { | |||
1014 | free(subpath); | |||
1015 | return got_error_from_errno("asprintf"); | |||
1016 | } | |||
1017 | ||||
1018 | subdirfd = openat(fd, de->d_name, | |||
1019 | O_RDONLY0x0000 | O_NOFOLLOW0x0100 | O_DIRECTORY0x20000); | |||
1020 | if (subdirfd == -1) { | |||
1021 | if (errno(*__errno()) == EACCES13) { | |||
1022 | *next = TAILQ_NEXT(dle, entry)((dle)->entry.tqe_next); | |||
1023 | return NULL((void *)0); | |||
1024 | } | |||
1025 | err = got_error_from_errno2("openat", subdirpath); | |||
1026 | free(subpath); | |||
1027 | free(subdirpath); | |||
1028 | return err; | |||
1029 | } | |||
1030 | ||||
1031 | subdir = fdopendir(subdirfd); | |||
1032 | if (subdir == NULL((void *)0)) | |||
1033 | return got_error_from_errno2("fdopendir", path); | |||
1034 | subdirfd = -1; | |||
1035 | err = read_dirlist(&subdirlist, subdir, subdirpath); | |||
1036 | if (err) { | |||
1037 | free(subpath); | |||
1038 | free(subdirpath); | |||
1039 | closedir(subdir); | |||
1040 | return err; | |||
1041 | } | |||
1042 | err = diff_fileindex_dir(fileindex, ie, &subdirlist, | |||
1043 | dirfd(subdir), rootpath, subpath, repo, cb, cb_arg); | |||
1044 | if (subdir && closedir(subdir) == -1 && err == NULL((void *)0)) | |||
1045 | err = got_error_from_errno2("closedir", subdirpath); | |||
1046 | free(subpath); | |||
1047 | free(subdirpath); | |||
1048 | free_dirlist(&subdirlist); | |||
1049 | if (err) | |||
1050 | return err; | |||
1051 | } | |||
1052 | ||||
1053 | *next = TAILQ_NEXT(dle, entry)((dle)->entry.tqe_next); | |||
1054 | return NULL((void *)0); | |||
1055 | } | |||
1056 | ||||
1057 | static const struct got_error * | |||
1058 | diff_fileindex_dir(struct got_fileindex *fileindex, | |||
1059 | struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist, | |||
1060 | int dirfd, const char *rootpath, const char *path, | |||
1061 | struct got_repository *repo, | |||
1062 | struct got_fileindex_diff_dir_cb *cb, void *cb_arg) | |||
1063 | { | |||
1064 | const struct got_error *err = NULL((void *)0); | |||
1065 | struct dirent *de = NULL((void *)0); | |||
1066 | size_t path_len = strlen(path); | |||
1067 | struct got_pathlist_entry *dle; | |||
1068 | ||||
1069 | if (cb->diff_traverse) { | |||
1070 | err = cb->diff_traverse(cb_arg, path, dirfd); | |||
1071 | if (err) | |||
1072 | return err; | |||
1073 | } | |||
1074 | ||||
1075 | dle = TAILQ_FIRST(dirlist)((dirlist)->tqh_first); | |||
1076 | while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) { | |||
1077 | if (dle && *ie) { | |||
1078 | char *de_path; | |||
1079 | int cmp; | |||
1080 | de = dle->data; | |||
1081 | if (asprintf(&de_path, "%s/%s", path, | |||
1082 | de->d_name) == -1) { | |||
1083 | err = got_error_from_errno("asprintf"); | |||
1084 | break; | |||
1085 | } | |||
1086 | cmp = got_path_cmp((*ie)->path, de_path, | |||
1087 | got_fileindex_entry_path_len(*ie), | |||
1088 | strlen(path) + 1 + de->d_namlen); | |||
1089 | free(de_path); | |||
1090 | if (cmp == 0) { | |||
1091 | err = cb->diff_old_new(cb_arg, *ie, de, path, | |||
1092 | dirfd); | |||
1093 | if (err) | |||
1094 | break; | |||
1095 | *ie = walk_fileindex(fileindex, *ie); | |||
1096 | err = walk_dir(&dle, fileindex, ie, dle, dirfd, | |||
1097 | path, rootpath, repo, cb, cb_arg); | |||
1098 | } else if (cmp < 0 ) { | |||
1099 | err = cb->diff_old(cb_arg, *ie, path); | |||
1100 | if (err) | |||
1101 | break; | |||
1102 | *ie = walk_fileindex(fileindex, *ie); | |||
1103 | } else { | |||
1104 | err = cb->diff_new(cb_arg, de, path, dirfd); | |||
1105 | if (err) | |||
1106 | break; | |||
1107 | err = walk_dir(&dle, fileindex, ie, dle, dirfd, | |||
1108 | path, rootpath, repo, cb, cb_arg); | |||
1109 | } | |||
1110 | if (err) | |||
1111 | break; | |||
1112 | } else if (*ie) { | |||
1113 | err = cb->diff_old(cb_arg, *ie, path); | |||
1114 | if (err) | |||
1115 | break; | |||
1116 | *ie = walk_fileindex(fileindex, *ie); | |||
1117 | } else if (dle) { | |||
1118 | de = dle->data; | |||
1119 | err = cb->diff_new(cb_arg, de, path, dirfd); | |||
1120 | if (err) | |||
1121 | break; | |||
1122 | err = walk_dir(&dle, fileindex, ie, dle, dirfd, path, | |||
1123 | rootpath, repo, cb, cb_arg); | |||
1124 | if (err) | |||
1125 | break; | |||
1126 | } | |||
1127 | } | |||
1128 | ||||
1129 | return err; | |||
1130 | } | |||
1131 | ||||
1132 | const struct got_error * | |||
1133 | got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd, | |||
1134 | const char *rootpath, const char *path, struct got_repository *repo, | |||
1135 | struct got_fileindex_diff_dir_cb *cb, void *cb_arg) | |||
1136 | { | |||
1137 | const struct got_error *err; | |||
1138 | struct got_fileindex_entry *ie; | |||
1139 | struct got_pathlist_head dirlist; | |||
1140 | int fd2; | |||
1141 | DIR *dir; | |||
1142 | ||||
1143 | TAILQ_INIT(&dirlist)do { (&dirlist)->tqh_first = ((void *)0); (&dirlist )->tqh_last = &(&dirlist)->tqh_first; } while ( 0); | |||
1144 | ||||
1145 | /* | |||
1146 | * Duplicate the file descriptor so we can call closedir() below | |||
1147 | * without closing the file descriptor passed in by our caller. | |||
1148 | */ | |||
1149 | fd2 = dup(fd); | |||
1150 | if (fd2 == -1) | |||
1151 | return got_error_from_errno2("dup", path); | |||
1152 | if (lseek(fd2, 0, SEEK_SET0) == -1) { | |||
1153 | err = got_error_from_errno2("lseek", path); | |||
1154 | close(fd2); | |||
1155 | return err; | |||
1156 | } | |||
1157 | dir = fdopendir(fd2); | |||
1158 | if (dir == NULL((void *)0)) { | |||
1159 | err = got_error_from_errno2("fdopendir", path); | |||
1160 | close(fd2); | |||
1161 | return err; | |||
1162 | } | |||
1163 | err = read_dirlist(&dirlist, dir, path); | |||
1164 | if (err) { | |||
1165 | closedir(dir); | |||
1166 | return err; | |||
1167 | } | |||
1168 | ||||
1169 | ie = RB_MIN(got_fileindex_tree, &fileindex->entries)got_fileindex_tree_RB_MINMAX(&fileindex->entries, -1); | |||
1170 | while (ie && !got_path_is_child(ie->path, path, strlen(path))) | |||
1171 | ie = walk_fileindex(fileindex, ie); | |||
1172 | err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir), | |||
1173 | rootpath, path, repo, cb, cb_arg); | |||
1174 | ||||
1175 | if (closedir(dir) == -1 && err == NULL((void *)0)) | |||
1176 | err = got_error_from_errno2("closedir", path); | |||
1177 | free_dirlist(&dirlist); | |||
1178 | return err; | |||
1179 | } | |||
1180 | ||||
1181 | RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp)void got_fileindex_tree_RB_INSERT_COLOR(struct got_fileindex_tree *head, struct got_fileindex_entry *elm) { struct got_fileindex_entry *parent, *gparent, *tmp; while ((parent = (elm)->entry.rbe_parent ) && (parent)->entry.rbe_color == 1) { gparent = ( parent)->entry.rbe_parent; if (parent == (gparent)->entry .rbe_left) { tmp = (gparent)->entry.rbe_right; if (tmp && (tmp)->entry.rbe_color == 1) { (tmp)->entry.rbe_color = 0; do { (parent)->entry.rbe_color = 0; (gparent)->entry .rbe_color = 1; } while (0); elm = gparent; continue; } if (( parent)->entry.rbe_right == elm) { do { (tmp) = (parent)-> entry.rbe_right; if (((parent)->entry.rbe_right = (tmp)-> entry.rbe_left)) { ((tmp)->entry.rbe_left)->entry.rbe_parent = (parent); } do {} while (0); if (((tmp)->entry.rbe_parent = (parent)->entry.rbe_parent)) { if ((parent) == ((parent )->entry.rbe_parent)->entry.rbe_left) ((parent)->entry .rbe_parent)->entry.rbe_left = (tmp); else ((parent)->entry .rbe_parent)->entry.rbe_right = (tmp); } else (head)->rbh_root = (tmp); (tmp)->entry.rbe_left = (parent); (parent)->entry .rbe_parent = (tmp); do {} while (0); if (((tmp)->entry.rbe_parent )) do {} while (0); } while (0); tmp = parent; parent = elm; elm = tmp; } do { (parent)->entry.rbe_color = 0; (gparent)-> entry.rbe_color = 1; } while (0); do { (tmp) = (gparent)-> entry.rbe_left; if (((gparent)->entry.rbe_left = (tmp)-> entry.rbe_right)) { ((tmp)->entry.rbe_right)->entry.rbe_parent = (gparent); } do {} while (0); if (((tmp)->entry.rbe_parent = (gparent)->entry.rbe_parent)) { if ((gparent) == ((gparent )->entry.rbe_parent)->entry.rbe_left) ((gparent)->entry .rbe_parent)->entry.rbe_left = (tmp); else ((gparent)-> entry.rbe_parent)->entry.rbe_right = (tmp); } else (head)-> rbh_root = (tmp); (tmp)->entry.rbe_right = (gparent); (gparent )->entry.rbe_parent = (tmp); do {} while (0); if (((tmp)-> entry.rbe_parent)) do {} while (0); } while (0); } else { tmp = (gparent)->entry.rbe_left; if (tmp && (tmp)-> entry.rbe_color == 1) { (tmp)->entry.rbe_color = 0; do { ( parent)->entry.rbe_color = 0; (gparent)->entry.rbe_color = 1; } while (0); elm = gparent; continue; } if ((parent)-> entry.rbe_left == elm) { do { (tmp) = (parent)->entry.rbe_left ; if (((parent)->entry.rbe_left = (tmp)->entry.rbe_right )) { ((tmp)->entry.rbe_right)->entry.rbe_parent = (parent ); } do {} while (0); if (((tmp)->entry.rbe_parent = (parent )->entry.rbe_parent)) { if ((parent) == ((parent)->entry .rbe_parent)->entry.rbe_left) ((parent)->entry.rbe_parent )->entry.rbe_left = (tmp); else ((parent)->entry.rbe_parent )->entry.rbe_right = (tmp); } else (head)->rbh_root = ( tmp); (tmp)->entry.rbe_right = (parent); (parent)->entry .rbe_parent = (tmp); do {} while (0); if (((tmp)->entry.rbe_parent )) do {} while (0); } while (0); tmp = parent; parent = elm; elm = tmp; } do { (parent)->entry.rbe_color = 0; (gparent)-> entry.rbe_color = 1; } while (0); do { (tmp) = (gparent)-> entry.rbe_right; if (((gparent)->entry.rbe_right = (tmp)-> entry.rbe_left)) { ((tmp)->entry.rbe_left)->entry.rbe_parent = (gparent); } do {} while (0); if (((tmp)->entry.rbe_parent = (gparent)->entry.rbe_parent)) { if ((gparent) == ((gparent )->entry.rbe_parent)->entry.rbe_left) ((gparent)->entry .rbe_parent)->entry.rbe_left = (tmp); else ((gparent)-> entry.rbe_parent)->entry.rbe_right = (tmp); } else (head)-> rbh_root = (tmp); (tmp)->entry.rbe_left = (gparent); (gparent )->entry.rbe_parent = (tmp); do {} while (0); if (((tmp)-> entry.rbe_parent)) do {} while (0); } while (0); } } (head-> rbh_root)->entry.rbe_color = 0; } void got_fileindex_tree_RB_REMOVE_COLOR (struct got_fileindex_tree *head, struct got_fileindex_entry * parent, struct got_fileindex_entry *elm) { struct got_fileindex_entry *tmp; while ((elm == ((void *)0) || (elm)->entry.rbe_color == 0) && elm != (head)->rbh_root) { if ((parent)-> entry.rbe_left == elm) { tmp = (parent)->entry.rbe_right; if ((tmp)->entry.rbe_color == 1) { do { (tmp)->entry.rbe_color = 0; (parent)->entry.rbe_color = 1; } while (0); do { (tmp ) = (parent)->entry.rbe_right; if (((parent)->entry.rbe_right = (tmp)->entry.rbe_left)) { ((tmp)->entry.rbe_left)-> entry.rbe_parent = (parent); } do {} while (0); if (((tmp)-> entry.rbe_parent = (parent)->entry.rbe_parent)) { if ((parent ) == ((parent)->entry.rbe_parent)->entry.rbe_left) ((parent )->entry.rbe_parent)->entry.rbe_left = (tmp); else ((parent )->entry.rbe_parent)->entry.rbe_right = (tmp); } else ( head)->rbh_root = (tmp); (tmp)->entry.rbe_left = (parent ); (parent)->entry.rbe_parent = (tmp); do {} while (0); if (((tmp)->entry.rbe_parent)) do {} while (0); } while (0); tmp = (parent)->entry.rbe_right; } if (((tmp)->entry.rbe_left == ((void *)0) || ((tmp)->entry.rbe_left)->entry.rbe_color == 0) && ((tmp)->entry.rbe_right == ((void *)0) || ((tmp)->entry.rbe_right)->entry.rbe_color == 0)) { (tmp )->entry.rbe_color = 1; elm = parent; parent = (elm)->entry .rbe_parent; } else { if ((tmp)->entry.rbe_right == ((void *)0) || ((tmp)->entry.rbe_right)->entry.rbe_color == 0 ) { struct got_fileindex_entry *oleft; if ((oleft = (tmp)-> entry.rbe_left)) (oleft)->entry.rbe_color = 0; (tmp)->entry .rbe_color = 1; do { (oleft) = (tmp)->entry.rbe_left; if ( ((tmp)->entry.rbe_left = (oleft)->entry.rbe_right)) { ( (oleft)->entry.rbe_right)->entry.rbe_parent = (tmp); } do {} while (0); if (((oleft)->entry.rbe_parent = (tmp)-> entry.rbe_parent)) { if ((tmp) == ((tmp)->entry.rbe_parent )->entry.rbe_left) ((tmp)->entry.rbe_parent)->entry. rbe_left = (oleft); else ((tmp)->entry.rbe_parent)->entry .rbe_right = (oleft); } else (head)->rbh_root = (oleft); ( oleft)->entry.rbe_right = (tmp); (tmp)->entry.rbe_parent = (oleft); do {} while (0); if (((oleft)->entry.rbe_parent )) do {} while (0); } while (0); tmp = (parent)->entry.rbe_right ; } (tmp)->entry.rbe_color = (parent)->entry.rbe_color; (parent)->entry.rbe_color = 0; if ((tmp)->entry.rbe_right ) ((tmp)->entry.rbe_right)->entry.rbe_color = 0; do { ( tmp) = (parent)->entry.rbe_right; if (((parent)->entry. rbe_right = (tmp)->entry.rbe_left)) { ((tmp)->entry.rbe_left )->entry.rbe_parent = (parent); } do {} while (0); if (((tmp )->entry.rbe_parent = (parent)->entry.rbe_parent)) { if ((parent) == ((parent)->entry.rbe_parent)->entry.rbe_left ) ((parent)->entry.rbe_parent)->entry.rbe_left = (tmp); else ((parent)->entry.rbe_parent)->entry.rbe_right = ( tmp); } else (head)->rbh_root = (tmp); (tmp)->entry.rbe_left = (parent); (parent)->entry.rbe_parent = (tmp); do {} while (0); if (((tmp)->entry.rbe_parent)) do {} while (0); } while (0); elm = (head)->rbh_root; break; } } else { tmp = (parent )->entry.rbe_left; if ((tmp)->entry.rbe_color == 1) { do { (tmp)->entry.rbe_color = 0; (parent)->entry.rbe_color = 1; } while (0); do { (tmp) = (parent)->entry.rbe_left; if (((parent)->entry.rbe_left = (tmp)->entry.rbe_right)) { ((tmp)->entry.rbe_right)->entry.rbe_parent = (parent); } do {} while (0); if (((tmp)->entry.rbe_parent = (parent )->entry.rbe_parent)) { if ((parent) == ((parent)->entry .rbe_parent)->entry.rbe_left) ((parent)->entry.rbe_parent )->entry.rbe_left = (tmp); else ((parent)->entry.rbe_parent )->entry.rbe_right = (tmp); } else (head)->rbh_root = ( tmp); (tmp)->entry.rbe_right = (parent); (parent)->entry .rbe_parent = (tmp); do {} while (0); if (((tmp)->entry.rbe_parent )) do {} while (0); } while (0); tmp = (parent)->entry.rbe_left ; } if (((tmp)->entry.rbe_left == ((void *)0) || ((tmp)-> entry.rbe_left)->entry.rbe_color == 0) && ((tmp)-> entry.rbe_right == ((void *)0) || ((tmp)->entry.rbe_right) ->entry.rbe_color == 0)) { (tmp)->entry.rbe_color = 1; elm = parent; parent = (elm)->entry.rbe_parent; } else { if ( (tmp)->entry.rbe_left == ((void *)0) || ((tmp)->entry.rbe_left )->entry.rbe_color == 0) { struct got_fileindex_entry *oright ; if ((oright = (tmp)->entry.rbe_right)) (oright)->entry .rbe_color = 0; (tmp)->entry.rbe_color = 1; do { (oright) = (tmp)->entry.rbe_right; if (((tmp)->entry.rbe_right = ( oright)->entry.rbe_left)) { ((oright)->entry.rbe_left)-> entry.rbe_parent = (tmp); } do {} while (0); if (((oright)-> entry.rbe_parent = (tmp)->entry.rbe_parent)) { if ((tmp) == ((tmp)->entry.rbe_parent)->entry.rbe_left) ((tmp)-> entry.rbe_parent)->entry.rbe_left = (oright); else ((tmp)-> entry.rbe_parent)->entry.rbe_right = (oright); } else (head )->rbh_root = (oright); (oright)->entry.rbe_left = (tmp ); (tmp)->entry.rbe_parent = (oright); do {} while (0); if (((oright)->entry.rbe_parent)) do {} while (0); } while ( 0); tmp = (parent)->entry.rbe_left; } (tmp)->entry.rbe_color = (parent)->entry.rbe_color; (parent)->entry.rbe_color = 0; if ((tmp)->entry.rbe_left) ((tmp)->entry.rbe_left )->entry.rbe_color = 0; do { (tmp) = (parent)->entry.rbe_left ; if (((parent)->entry.rbe_left = (tmp)->entry.rbe_right )) { ((tmp)->entry.rbe_right)->entry.rbe_parent = (parent ); } do {} while (0); if (((tmp)->entry.rbe_parent = (parent )->entry.rbe_parent)) { if ((parent) == ((parent)->entry .rbe_parent)->entry.rbe_left) ((parent)->entry.rbe_parent )->entry.rbe_left = (tmp); else ((parent)->entry.rbe_parent )->entry.rbe_right = (tmp); } else (head)->rbh_root = ( tmp); (tmp)->entry.rbe_right = (parent); (parent)->entry .rbe_parent = (tmp); do {} while (0); if (((tmp)->entry.rbe_parent )) do {} while (0); } while (0); elm = (head)->rbh_root; break ; } } } if (elm) (elm)->entry.rbe_color = 0; } struct got_fileindex_entry * got_fileindex_tree_RB_REMOVE(struct got_fileindex_tree *head , struct got_fileindex_entry *elm) { struct got_fileindex_entry *child, *parent, *old = elm; int color; if ((elm)->entry. rbe_left == ((void *)0)) child = (elm)->entry.rbe_right; else if ((elm)->entry.rbe_right == ((void *)0)) child = (elm)-> entry.rbe_left; else { struct got_fileindex_entry *left; elm = (elm)->entry.rbe_right; while ((left = (elm)->entry.rbe_left )) elm = left; child = (elm)->entry.rbe_right; parent = (elm )->entry.rbe_parent; color = (elm)->entry.rbe_color; if (child) (child)->entry.rbe_parent = parent; if (parent) { if ((parent)->entry.rbe_left == elm) (parent)->entry.rbe_left = child; else (parent)->entry.rbe_right = child; do {} while (0); } else (head)->rbh_root = child; if ((elm)->entry .rbe_parent == old) parent = elm; (elm)->entry = (old)-> entry; if ((old)->entry.rbe_parent) { if (((old)->entry .rbe_parent)->entry.rbe_left == old) ((old)->entry.rbe_parent )->entry.rbe_left = elm; else ((old)->entry.rbe_parent) ->entry.rbe_right = elm; do {} while (0); } else (head)-> rbh_root = elm; ((old)->entry.rbe_left)->entry.rbe_parent = elm; if ((old)->entry.rbe_right) ((old)->entry.rbe_right )->entry.rbe_parent = elm; if (parent) { left = parent; do { do {} while (0); } while ((left = (left)->entry.rbe_parent )); } goto color; } parent = (elm)->entry.rbe_parent; color = (elm)->entry.rbe_color; if (child) (child)->entry.rbe_parent = parent; if (parent) { if ((parent)->entry.rbe_left == elm ) (parent)->entry.rbe_left = child; else (parent)->entry .rbe_right = child; do {} while (0); } else (head)->rbh_root = child; color: if (color == 0) got_fileindex_tree_RB_REMOVE_COLOR (head, parent, child); return (old); } struct got_fileindex_entry * got_fileindex_tree_RB_INSERT(struct got_fileindex_tree *head , struct got_fileindex_entry *elm) { struct got_fileindex_entry *tmp; struct got_fileindex_entry *parent = ((void *)0); int comp = 0; tmp = (head)->rbh_root; while (tmp) { parent = tmp; comp = (got_fileindex_cmp)(elm, parent); if (comp < 0) tmp = ( tmp)->entry.rbe_left; else if (comp > 0) tmp = (tmp)-> entry.rbe_right; else return (tmp); } do { (elm)->entry.rbe_parent = parent; (elm)->entry.rbe_left = (elm)->entry.rbe_right = ((void *)0); (elm)->entry.rbe_color = 1; } while (0); if (parent != ((void *)0)) { if (comp < 0) (parent)->entry .rbe_left = elm; else (parent)->entry.rbe_right = elm; do { } while (0); } else (head)->rbh_root = elm; got_fileindex_tree_RB_INSERT_COLOR (head, elm); return (((void *)0)); } struct got_fileindex_entry * got_fileindex_tree_RB_FIND(struct got_fileindex_tree *head , struct got_fileindex_entry *elm) { struct got_fileindex_entry *tmp = (head)->rbh_root; int comp; while (tmp) { comp = got_fileindex_cmp (elm, tmp); if (comp < 0) tmp = (tmp)->entry.rbe_left; else if (comp > 0) tmp = (tmp)->entry.rbe_right; else return (tmp); } return (((void *)0)); } struct got_fileindex_entry * got_fileindex_tree_RB_NFIND(struct got_fileindex_tree *head, struct got_fileindex_entry *elm) { struct got_fileindex_entry *tmp = (head)->rbh_root; struct got_fileindex_entry *res = ((void *)0); int comp; while (tmp) { comp = got_fileindex_cmp (elm, tmp); if (comp < 0) { res = tmp; tmp = (tmp)->entry .rbe_left; } else if (comp > 0) tmp = (tmp)->entry.rbe_right ; else return (tmp); } return (res); } struct got_fileindex_entry * got_fileindex_tree_RB_NEXT(struct got_fileindex_entry *elm ) { if ((elm)->entry.rbe_right) { elm = (elm)->entry.rbe_right ; while ((elm)->entry.rbe_left) elm = (elm)->entry.rbe_left ; } else { if ((elm)->entry.rbe_parent && (elm == ( (elm)->entry.rbe_parent)->entry.rbe_left)) elm = (elm)-> entry.rbe_parent; else { while ((elm)->entry.rbe_parent && (elm == ((elm)->entry.rbe_parent)->entry.rbe_right)) elm = (elm)->entry.rbe_parent; elm = (elm)->entry.rbe_parent ; } } return (elm); } struct got_fileindex_entry * got_fileindex_tree_RB_PREV (struct got_fileindex_entry *elm) { if ((elm)->entry.rbe_left ) { elm = (elm)->entry.rbe_left; while ((elm)->entry.rbe_right ) elm = (elm)->entry.rbe_right; } else { if ((elm)->entry .rbe_parent && (elm == ((elm)->entry.rbe_parent)-> entry.rbe_right)) elm = (elm)->entry.rbe_parent; else { while ((elm)->entry.rbe_parent && (elm == ((elm)->entry .rbe_parent)->entry.rbe_left)) elm = (elm)->entry.rbe_parent ; elm = (elm)->entry.rbe_parent; } } return (elm); } struct got_fileindex_entry * got_fileindex_tree_RB_MINMAX(struct got_fileindex_tree *head, int val) { struct got_fileindex_entry *tmp = (head)-> rbh_root; struct got_fileindex_entry *parent = ((void *)0); while (tmp) { parent = tmp; if (val < 0) tmp = (tmp)->entry. rbe_left; else tmp = (tmp)->entry.rbe_right; } return (parent ); }; | |||
1 | /* | |||
2 | * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org> | |||
3 | * | |||
4 | * Permission to use, copy, modify, and distribute this software for any | |||
5 | * purpose with or without fee is hereby granted, provided that the above | |||
6 | * copyright notice and this permission notice appear in all copies. | |||
7 | * | |||
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |||
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |||
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |||
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |||
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |||
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |||
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |||
15 | */ | |||
16 | ||||
17 | /* | |||
18 | * State information for a tracked file in a work tree. | |||
19 | * When written to disk, multi-byte fields are written in big-endian. | |||
20 | * Some fields are based on results from stat(2). These are only used in | |||
21 | * order to detect modifications made to on-disk files, they are never | |||
22 | * applied back to the filesystem. | |||
23 | */ | |||
24 | struct got_fileindex_entry { | |||
25 | RB_ENTRY(got_fileindex_entry)struct { struct got_fileindex_entry *rbe_left; struct got_fileindex_entry *rbe_right; struct got_fileindex_entry *rbe_parent; int rbe_color ; } entry; | |||
26 | uint64_t ctime_sec; | |||
27 | uint64_t ctime_nsec; | |||
28 | uint64_t mtime_sec; | |||
29 | uint64_t mtime_nsec; | |||
30 | uint32_t uid; | |||
31 | uint32_t gid; | |||
32 | /* | |||
33 | * On-disk size is truncated to the lower 32 bits. | |||
34 | * The value is only used to check for modifications anyway. | |||
35 | */ | |||
36 | uint32_t size; | |||
37 | ||||
38 | uint16_t mode; | |||
39 | #define GOT_FILEIDX_MODE_FILE_TYPE0x000f 0x000f | |||
40 | #define GOT_FILEIDX_MODE_FILE_TYPE_ONDISK0x0003 0x0003 | |||
41 | #define GOT_FILEIDX_MODE_FILE_TYPE_STAGED0x000c 0x000c | |||
42 | #define GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT2 2 | |||
43 | #define GOT_FILEIDX_MODE_REGULAR_FILE1 1 | |||
44 | #define GOT_FILEIDX_MODE_SYMLINK2 2 | |||
45 | #define GOT_FILEIDX_MODE_BAD_SYMLINK3 3 | |||
46 | #define GOT_FILEIDX_MODE_PERMS0xfff0 0xfff0 | |||
47 | #define GOT_FILEIDX_MODE_PERMS_SHIFT4 4 | |||
48 | ||||
49 | /* SHA1 of corresponding blob in repository. */ | |||
50 | uint8_t blob_sha1[SHA1_DIGEST_LENGTH20]; | |||
51 | ||||
52 | /* SHA1 of corresponding base commit in repository. */ | |||
53 | uint8_t commit_sha1[SHA1_DIGEST_LENGTH20]; | |||
54 | ||||
55 | uint32_t flags; | |||
56 | ||||
57 | /* | |||
58 | * UNIX-style path, relative to work tree root. | |||
59 | * Variable length, and NUL-padded to a multiple of 8 on disk. | |||
60 | */ | |||
61 | char *path; | |||
62 | ||||
63 | /* | |||
64 | * (since GOT_FILE_INDEX_VERSION 2) | |||
65 | * SHA1 of staged blob in repository if stage equals either | |||
66 | * GOT_FILEIDX_STAGE_MODIFY or GOT_FILEIDX_STAGE_ADD. | |||
67 | * Otherwise, this field is not written to disk. | |||
68 | */ | |||
69 | uint8_t staged_blob_sha1[SHA1_DIGEST_LENGTH20]; | |||
70 | }; | |||
71 | ||||
72 | /* Modifications explicitly staged for commit. */ | |||
73 | #define GOT_FILEIDX_STAGE_NONE0 0 | |||
74 | #define GOT_FILEIDX_STAGE_MODIFY1 1 | |||
75 | #define GOT_FILEIDX_STAGE_ADD2 2 | |||
76 | #define GOT_FILEIDX_STAGE_DELETE3 3 | |||
77 | ||||
78 | struct got_fileindex; | |||
79 | ||||
80 | RB_HEAD(got_fileindex_tree, got_fileindex_entry)struct got_fileindex_tree { struct got_fileindex_entry *rbh_root ; }; | |||
81 | ||||
82 | size_t got_fileindex_entry_path_len(const struct got_fileindex_entry *); | |||
83 | ||||
84 | static inline int | |||
85 | got_fileindex_cmp(const struct got_fileindex_entry *e1, | |||
86 | const struct got_fileindex_entry *e2) | |||
87 | { | |||
88 | return got_path_cmp(e1->path, e2->path, | |||
| ||||
89 | got_fileindex_entry_path_len(e1), | |||
90 | got_fileindex_entry_path_len(e2)); | |||
91 | } | |||
92 | ||||
93 | RB_PROTOTYPE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp)void got_fileindex_tree_RB_INSERT_COLOR(struct got_fileindex_tree *, struct got_fileindex_entry *); void got_fileindex_tree_RB_REMOVE_COLOR (struct got_fileindex_tree *, struct got_fileindex_entry *, struct got_fileindex_entry *); struct got_fileindex_entry *got_fileindex_tree_RB_REMOVE (struct got_fileindex_tree *, struct got_fileindex_entry *); struct got_fileindex_entry *got_fileindex_tree_RB_INSERT(struct got_fileindex_tree *, struct got_fileindex_entry *); struct got_fileindex_entry *got_fileindex_tree_RB_FIND(struct got_fileindex_tree *, struct got_fileindex_entry *); struct got_fileindex_entry *got_fileindex_tree_RB_NFIND (struct got_fileindex_tree *, struct got_fileindex_entry *); struct got_fileindex_entry *got_fileindex_tree_RB_NEXT(struct got_fileindex_entry *); struct got_fileindex_entry *got_fileindex_tree_RB_PREV(struct got_fileindex_entry *); struct got_fileindex_entry *got_fileindex_tree_RB_MINMAX (struct got_fileindex_tree *, int);; | |||
94 | ||||
95 | /* On-disk file index header structure. */ | |||
96 | struct got_fileindex_hdr { | |||
97 | uint32_t signature; /* big-endian */ | |||
98 | #define GOT_FILE_INDEX_SIGNATURE0x676f7449 0x676f7449 /* 'g', 'o', 't', 'I' */ | |||
99 | uint32_t version; /* big-endian */ | |||
100 | #define GOT_FILE_INDEX_VERSION2 2 | |||
101 | uint32_t nentries; /* big-endian */ | |||
102 | /* list of concatenated fileindex entries */ | |||
103 | uint8_t sha1[SHA1_DIGEST_LENGTH20]; /* checksum of above on-disk data */ | |||
104 | }; | |||
105 | ||||
106 | mode_t got_fileindex_entry_perms_get(struct got_fileindex_entry *); | |||
107 | uint16_t got_fileindex_perms_from_st(struct stat *); | |||
108 | mode_t got_fileindex_perms_to_st(struct got_fileindex_entry *); | |||
109 | ||||
110 | const struct got_error *got_fileindex_entry_update(struct got_fileindex_entry *, | |||
111 | int, const char *, uint8_t *, uint8_t *, int); | |||
112 | const struct got_error *got_fileindex_entry_alloc(struct got_fileindex_entry **, | |||
113 | const char *); | |||
114 | void got_fileindex_entry_free(struct got_fileindex_entry *); | |||
115 | ||||
116 | struct got_fileindex *got_fileindex_alloc(void); | |||
117 | void got_fileindex_free(struct got_fileindex *); | |||
118 | const struct got_error *got_fileindex_write(struct got_fileindex *, FILE *); | |||
119 | const struct got_error *got_fileindex_entry_add(struct got_fileindex *, | |||
120 | struct got_fileindex_entry *); | |||
121 | void got_fileindex_entry_remove(struct got_fileindex *, | |||
122 | struct got_fileindex_entry *); | |||
123 | struct got_fileindex_entry *got_fileindex_entry_get(struct got_fileindex *, | |||
124 | const char *, size_t); | |||
125 | const struct got_error *got_fileindex_read(struct got_fileindex *, FILE *); | |||
126 | typedef const struct got_error *(*got_fileindex_cb)(void *, | |||
127 | struct got_fileindex_entry *); | |||
128 | const struct got_error *got_fileindex_for_each_entry_safe( | |||
129 | struct got_fileindex *, got_fileindex_cb cb, void *); | |||
130 | ||||
131 | typedef const struct got_error *(*got_fileindex_diff_tree_old_new_cb)(void *, | |||
132 | struct got_fileindex_entry *, struct got_tree_entry *, const char *); | |||
133 | typedef const struct got_error *(*got_fileindex_diff_tree_old_cb)(void *, | |||
134 | struct got_fileindex_entry *, const char *); | |||
135 | typedef const struct got_error *(*got_fileindex_diff_tree_new_cb)(void *, | |||
136 | struct got_tree_entry *, const char *); | |||
137 | struct got_fileindex_diff_tree_cb { | |||
138 | got_fileindex_diff_tree_old_new_cb diff_old_new; | |||
139 | got_fileindex_diff_tree_old_cb diff_old; | |||
140 | got_fileindex_diff_tree_new_cb diff_new; | |||
141 | }; | |||
142 | const struct got_error *got_fileindex_diff_tree(struct got_fileindex *, | |||
143 | struct got_tree_object *, const char *, const char *, | |||
144 | struct got_repository *, struct got_fileindex_diff_tree_cb *, void *); | |||
145 | ||||
146 | typedef const struct got_error *(*got_fileindex_diff_dir_old_new_cb)(void *, | |||
147 | struct got_fileindex_entry *, struct dirent *, const char *, int); | |||
148 | typedef const struct got_error *(*got_fileindex_diff_dir_old_cb)(void *, | |||
149 | struct got_fileindex_entry *, const char *); | |||
150 | typedef const struct got_error *(*got_fileindex_diff_dir_new_cb)(void *, | |||
151 | struct dirent *, const char *, int); | |||
152 | typedef const struct got_error *(*got_fileindex_diff_dir_traverse)(void *, | |||
153 | const char *, int); | |||
154 | struct got_fileindex_diff_dir_cb { | |||
155 | got_fileindex_diff_dir_old_new_cb diff_old_new; | |||
156 | got_fileindex_diff_dir_old_cb diff_old; | |||
157 | got_fileindex_diff_dir_new_cb diff_new; | |||
158 | got_fileindex_diff_dir_traverse diff_traverse; | |||
159 | }; | |||
160 | const struct got_error *got_fileindex_diff_dir(struct got_fileindex *, int, | |||
161 | const char *, const char *, struct got_repository *, | |||
162 | struct got_fileindex_diff_dir_cb *, void *); | |||
163 | ||||
164 | int got_fileindex_entry_has_blob(struct got_fileindex_entry *); | |||
165 | int got_fileindex_entry_has_commit(struct got_fileindex_entry *); | |||
166 | int got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *); | |||
167 | uint32_t got_fileindex_entry_stage_get(const struct got_fileindex_entry *); | |||
168 | void got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t); | |||
169 | int got_fileindex_entry_filetype_get(struct got_fileindex_entry *); | |||
170 | void got_fileindex_entry_filetype_set(struct got_fileindex_entry *, int); | |||
171 | void got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *, int); | |||
172 | int got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *); | |||
173 | ||||
174 | void got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *); |