]> git.ipfire.org Git - thirdparty/git.git/blame - name-hash.c
name-hash.h: move declarations for name-hash.c from cache.h
[thirdparty/git.git] / name-hash.c
CommitLineData
96872bc2
LT
1/*
2 * name-hash.c
3 *
4 * Hashing names in the index state
5 *
6 * Copyright (C) 2008 Linus Torvalds
7 */
96872bc2 8#include "cache.h"
32a8f510 9#include "environment.h"
f394e093 10#include "gettext.h"
f5653856 11#include "name-hash.h"
07642942 12#include "thread-utils.h"
74ea5c95 13#include "trace.h"
6a9372f4 14#include "trace2.h"
4589bca8 15#include "sparse-index.h"
96872bc2 16
2092678c 17struct dir_entry {
e05881a4 18 struct hashmap_entry ent;
2092678c 19 struct dir_entry *parent;
2092678c
KB
20 int nr;
21 unsigned int namelen;
41284eb0 22 char name[FLEX_ARRAY];
2092678c
KB
23};
24
5cf88fd8 25static int dir_entry_cmp(const void *cmp_data UNUSED,
939af16e
EW
26 const struct hashmap_entry *eptr,
27 const struct hashmap_entry *entry_or_key,
56a14ea7 28 const void *keydata)
e05881a4 29{
939af16e 30 const struct dir_entry *e1, *e2;
56a14ea7
SB
31 const char *name = keydata;
32
939af16e
EW
33 e1 = container_of(eptr, const struct dir_entry, ent);
34 e2 = container_of(entry_or_key, const struct dir_entry, ent);
35
41284eb0
DT
36 return e1->namelen != e2->namelen || strncasecmp(e1->name,
37 name ? name : e2->name, e1->namelen);
e05881a4
KB
38}
39
846df809
JH
40static struct dir_entry *find_dir_entry__hash(struct index_state *istate,
41 const char *name, unsigned int namelen, unsigned int hash)
2092678c 42{
e05881a4 43 struct dir_entry key;
d22245a2 44 hashmap_entry_init(&key.ent, hash);
e05881a4 45 key.namelen = namelen;
404ab78e 46 return hashmap_get_entry(&istate->dir_hash, &key, ent, name);
2092678c
KB
47}
48
846df809
JH
49static struct dir_entry *find_dir_entry(struct index_state *istate,
50 const char *name, unsigned int namelen)
51{
52 return find_dir_entry__hash(istate, name, namelen, memihash(name, namelen));
53}
54
2092678c
KB
55static struct dir_entry *hash_dir_entry(struct index_state *istate,
56 struct cache_entry *ce, int namelen)
5102c617
JJ
57{
58 /*
59 * Throw each directory component in the hash for quick lookup
d28eec26 60 * during a git status. Directory components are stored without their
5102c617 61 * closing slash. Despite submodules being a directory, they never
d28eec26 62 * reach this point, because they are stored
2092678c 63 * in index_state.name_hash (as ordinary cache_entries).
5102c617 64 */
2092678c
KB
65 struct dir_entry *dir;
66
67 /* get length of parent directory */
68 while (namelen > 0 && !is_dir_sep(ce->name[namelen - 1]))
69 namelen--;
70 if (namelen <= 0)
71 return NULL;
d28eec26 72 namelen--;
2092678c
KB
73
74 /* lookup existing entry for that directory */
75 dir = find_dir_entry(istate, ce->name, namelen);
76 if (!dir) {
77 /* not found, create it and add to hash table */
96ffc06f 78 FLEX_ALLOC_MEM(dir, name, ce->name, namelen);
d22245a2 79 hashmap_entry_init(&dir->ent, memihash(ce->name, namelen));
2092678c 80 dir->namelen = namelen;
b94e5c1d 81 hashmap_add(&istate->dir_hash, &dir->ent);
2092678c
KB
82
83 /* recursively add missing parent directories */
d28eec26 84 dir->parent = hash_dir_entry(istate, ce, namelen);
5102c617 85 }
2092678c
KB
86 return dir;
87}
88
89static void add_dir_entry(struct index_state *istate, struct cache_entry *ce)
90{
91 /* Add reference to the directory entry (and parents if 0). */
92 struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce));
93 while (dir && !(dir->nr++))
94 dir = dir->parent;
95}
96
97static void remove_dir_entry(struct index_state *istate, struct cache_entry *ce)
98{
99 /*
1c8cca19
KB
100 * Release reference to the directory entry. If 0, remove and continue
101 * with parent directory.
2092678c
KB
102 */
103 struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce));
1c8cca19
KB
104 while (dir && !(--dir->nr)) {
105 struct dir_entry *parent = dir->parent;
28ee7941 106 hashmap_remove(&istate->dir_hash, &dir->ent, NULL);
1c8cca19
KB
107 free(dir);
108 dir = parent;
109 }
5102c617
JJ
110}
111
96872bc2
LT
112static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
113{
96872bc2
LT
114 if (ce->ce_flags & CE_HASHED)
115 return;
116 ce->ce_flags |= CE_HASHED;
5f116695
DS
117
118 if (!S_ISSPARSEDIR(ce->ce_mode)) {
119 hashmap_entry_init(&ce->ent, memihash(ce->name, ce_namelen(ce)));
120 hashmap_add(&istate->name_hash, &ce->ent);
121 }
5102c617 122
419a597f 123 if (ignore_case)
2092678c 124 add_dir_entry(istate, ce);
96872bc2
LT
125}
126
5cf88fd8 127static int cache_entry_cmp(const void *cmp_data UNUSED,
939af16e
EW
128 const struct hashmap_entry *eptr,
129 const struct hashmap_entry *entry_or_key,
7663cdc8 130 const void *remove)
419a597f 131{
939af16e
EW
132 const struct cache_entry *ce1, *ce2;
133
134 ce1 = container_of(eptr, const struct cache_entry, ent);
135 ce2 = container_of(entry_or_key, const struct cache_entry, ent);
136
419a597f
KB
137 /*
138 * For remove_name_hash, find the exact entry (pointer equality); for
7b359ea6 139 * index_file_exists, find all entries with matching hash code and
419a597f
KB
140 * decide whether the entry matches in same_name.
141 */
142 return remove ? !(ce1 == ce2) : 0;
143}
144
846df809
JH
145static int lazy_try_threaded = 1;
146static int lazy_nr_dir_threads;
147
846df809
JH
148/*
149 * Set a minimum number of cache_entries that we will handle per
15beaaa3 150 * thread and use that to decide how many threads to run (up to
846df809
JH
151 * the number on the system).
152 *
153 * For guidance setting the lower per-thread bound, see:
154 * t/helper/test-lazy-init-name-hash --analyze
155 */
156#define LAZY_THREAD_COST (2000)
157
158/*
159 * We use n mutexes to guard n partitions of the "istate->dir_hash"
160 * hashtable. Since "find" and "insert" operations will hash to a
161 * particular bucket and modify/search a single chain, we can say
162 * that "all chains mod n" are guarded by the same mutex -- rather
163 * than having a single mutex to guard the entire table. (This does
164 * require that we disable "rehashing" on the hashtable.)
165 *
166 * So, a larger value here decreases the probability of a collision
167 * and the time that each thread must wait for the mutex.
168 */
169#define LAZY_MAX_MUTEX (32)
170
171static pthread_mutex_t *lazy_dir_mutex_array;
172
173/*
174 * An array of lazy_entry items is used by the n threads in
175 * the directory parse (first) phase to (lock-free) store the
176 * intermediate results. These values are then referenced by
177 * the 2 threads in the second phase.
178 */
179struct lazy_entry {
180 struct dir_entry *dir;
181 unsigned int hash_dir;
182 unsigned int hash_name;
183};
184
185/*
186 * Decide if we want to use threads (if available) to load
187 * the hash tables. We set "lazy_nr_dir_threads" to zero when
188 * it is not worth it.
189 */
190static int lookup_lazy_params(struct index_state *istate)
191{
192 int nr_cpus;
193
194 lazy_nr_dir_threads = 0;
195
196 if (!lazy_try_threaded)
197 return 0;
198
199 /*
200 * If we are respecting case, just use the original
201 * code to build the "istate->name_hash". We don't
202 * need the complexity here.
203 */
204 if (!ignore_case)
205 return 0;
206
207 nr_cpus = online_cpus();
208 if (nr_cpus < 2)
209 return 0;
210
211 if (istate->cache_nr < 2 * LAZY_THREAD_COST)
212 return 0;
96872bc2 213
846df809
JH
214 if (istate->cache_nr < nr_cpus * LAZY_THREAD_COST)
215 nr_cpus = istate->cache_nr / LAZY_THREAD_COST;
216 lazy_nr_dir_threads = nr_cpus;
217 return lazy_nr_dir_threads;
218}
219
220/*
221 * Initialize n mutexes for use when searching and inserting
222 * into "istate->dir_hash". All "dir" threads are trying
223 * to insert partial pathnames into the hash as they iterate
224 * over their portions of the index, so lock contention is
225 * high.
226 *
227 * However, the hashmap is going to put items into bucket
228 * chains based on their hash values. Use that to create n
229 * mutexes and lock on mutex[bucket(hash) % n]. This will
77363a51 230 * decrease the collision rate by (hopefully) a factor of n.
846df809
JH
231 */
232static void init_dir_mutex(void)
233{
234 int j;
235
ca56dadb 236 CALLOC_ARRAY(lazy_dir_mutex_array, LAZY_MAX_MUTEX);
846df809
JH
237
238 for (j = 0; j < LAZY_MAX_MUTEX; j++)
239 init_recursive_mutex(&lazy_dir_mutex_array[j]);
240}
241
242static void cleanup_dir_mutex(void)
243{
244 int j;
245
246 for (j = 0; j < LAZY_MAX_MUTEX; j++)
247 pthread_mutex_destroy(&lazy_dir_mutex_array[j]);
248
249 free(lazy_dir_mutex_array);
250}
251
252static void lock_dir_mutex(int j)
253{
254 pthread_mutex_lock(&lazy_dir_mutex_array[j]);
255}
256
257static void unlock_dir_mutex(int j)
258{
259 pthread_mutex_unlock(&lazy_dir_mutex_array[j]);
260}
261
262static inline int compute_dir_lock_nr(
263 const struct hashmap *map,
264 unsigned int hash)
265{
266 return hashmap_bucket(map, hash) % LAZY_MAX_MUTEX;
267}
268
269static struct dir_entry *hash_dir_entry_with_parent_and_prefix(
270 struct index_state *istate,
271 struct dir_entry *parent,
272 struct strbuf *prefix)
273{
274 struct dir_entry *dir;
275 unsigned int hash;
276 int lock_nr;
277
278 /*
279 * Either we have a parent directory and path with slash(es)
280 * or the directory is an immediate child of the root directory.
281 */
282 assert((parent != NULL) ^ (strchr(prefix->buf, '/') == NULL));
283
284 if (parent)
285 hash = memihash_cont(parent->ent.hash,
286 prefix->buf + parent->namelen,
287 prefix->len - parent->namelen);
288 else
289 hash = memihash(prefix->buf, prefix->len);
290
291 lock_nr = compute_dir_lock_nr(&istate->dir_hash, hash);
292 lock_dir_mutex(lock_nr);
293
294 dir = find_dir_entry__hash(istate, prefix->buf, prefix->len, hash);
295 if (!dir) {
296 FLEX_ALLOC_MEM(dir, name, prefix->buf, prefix->len);
d22245a2 297 hashmap_entry_init(&dir->ent, hash);
846df809
JH
298 dir->namelen = prefix->len;
299 dir->parent = parent;
b94e5c1d 300 hashmap_add(&istate->dir_hash, &dir->ent);
846df809
JH
301
302 if (parent) {
303 unlock_dir_mutex(lock_nr);
304
305 /* All I really need here is an InterlockedIncrement(&(parent->nr)) */
306 lock_nr = compute_dir_lock_nr(&istate->dir_hash, parent->ent.hash);
307 lock_dir_mutex(lock_nr);
308 parent->nr++;
309 }
310 }
311
312 unlock_dir_mutex(lock_nr);
313
314 return dir;
315}
316
317/*
318 * handle_range_1() and handle_range_dir() are derived from
319 * clear_ce_flags_1() and clear_ce_flags_dir() in unpack-trees.c
320 * and handle the iteration over the entire array of index entries.
321 * They use recursion for adjacent entries in the same parent
322 * directory.
323 */
324static int handle_range_1(
325 struct index_state *istate,
326 int k_start,
327 int k_end,
328 struct dir_entry *parent,
329 struct strbuf *prefix,
330 struct lazy_entry *lazy_entries);
331
332static int handle_range_dir(
333 struct index_state *istate,
334 int k_start,
335 int k_end,
336 struct dir_entry *parent,
337 struct strbuf *prefix,
338 struct lazy_entry *lazy_entries,
339 struct dir_entry **dir_new_out)
340{
341 int rc, k;
342 int input_prefix_len = prefix->len;
343 struct dir_entry *dir_new;
344
345 dir_new = hash_dir_entry_with_parent_and_prefix(istate, parent, prefix);
346
347 strbuf_addch(prefix, '/');
348
349 /*
350 * Scan forward in the index array for index entries having the same
351 * path prefix (that are also in this directory).
352 */
2a1bd45b
KW
353 if (k_start + 1 >= k_end)
354 k = k_end;
355 else if (strncmp(istate->cache[k_start + 1]->name, prefix->buf, prefix->len) > 0)
846df809
JH
356 k = k_start + 1;
357 else if (strncmp(istate->cache[k_end - 1]->name, prefix->buf, prefix->len) == 0)
358 k = k_end;
359 else {
360 int begin = k_start;
361 int end = k_end;
568a05c5 362 assert(begin >= 0);
846df809 363 while (begin < end) {
568a05c5 364 int mid = begin + ((end - begin) >> 1);
846df809
JH
365 int cmp = strncmp(istate->cache[mid]->name, prefix->buf, prefix->len);
366 if (cmp == 0) /* mid has same prefix; look in second part */
367 begin = mid + 1;
368 else if (cmp > 0) /* mid is past group; look in first part */
369 end = mid;
370 else
371 die("cache entry out of order");
372 }
373 k = begin;
374 }
375
376 /*
377 * Recurse and process what we can of this subset [k_start, k).
378 */
379 rc = handle_range_1(istate, k_start, k, dir_new, prefix, lazy_entries);
380
381 strbuf_setlen(prefix, input_prefix_len);
382
383 *dir_new_out = dir_new;
384 return rc;
385}
386
387static int handle_range_1(
388 struct index_state *istate,
389 int k_start,
390 int k_end,
391 struct dir_entry *parent,
392 struct strbuf *prefix,
393 struct lazy_entry *lazy_entries)
394{
395 int input_prefix_len = prefix->len;
396 int k = k_start;
397
398 while (k < k_end) {
399 struct cache_entry *ce_k = istate->cache[k];
400 const char *name, *slash;
401
402 if (prefix->len && strncmp(ce_k->name, prefix->buf, prefix->len))
403 break;
404
405 name = ce_k->name + prefix->len;
406 slash = strchr(name, '/');
407
408 if (slash) {
409 int len = slash - name;
410 int processed;
411 struct dir_entry *dir_new;
412
413 strbuf_add(prefix, name, len);
414 processed = handle_range_dir(istate, k, k_end, parent, prefix, lazy_entries, &dir_new);
415 if (processed) {
416 k += processed;
417 strbuf_setlen(prefix, input_prefix_len);
418 continue;
419 }
420
421 strbuf_addch(prefix, '/');
422 processed = handle_range_1(istate, k, k_end, dir_new, prefix, lazy_entries);
423 k += processed;
424 strbuf_setlen(prefix, input_prefix_len);
425 continue;
426 }
427
428 /*
429 * It is too expensive to take a lock to insert "ce_k"
430 * into "istate->name_hash" and increment the ref-count
431 * on the "parent" dir. So we defer actually updating
432 * permanent data structures until phase 2 (where we
433 * can change the locking requirements) and simply
434 * accumulate our current results into the lazy_entries
435 * data array).
436 *
437 * We do not need to lock the lazy_entries array because
438 * we have exclusive access to the cells in the range
439 * [k_start,k_end) that this thread was given.
440 */
441 lazy_entries[k].dir = parent;
442 if (parent) {
443 lazy_entries[k].hash_name = memihash_cont(
444 parent->ent.hash,
445 ce_k->name + parent->namelen,
446 ce_namelen(ce_k) - parent->namelen);
447 lazy_entries[k].hash_dir = parent->ent.hash;
448 } else {
449 lazy_entries[k].hash_name = memihash(ce_k->name, ce_namelen(ce_k));
450 }
451
452 k++;
453 }
454
455 return k - k_start;
456}
457
458struct lazy_dir_thread_data {
459 pthread_t pthread;
460 struct index_state *istate;
461 struct lazy_entry *lazy_entries;
462 int k_start;
463 int k_end;
464};
465
466static void *lazy_dir_thread_proc(void *_data)
467{
468 struct lazy_dir_thread_data *d = _data;
469 struct strbuf prefix = STRBUF_INIT;
470 handle_range_1(d->istate, d->k_start, d->k_end, NULL, &prefix, d->lazy_entries);
471 strbuf_release(&prefix);
472 return NULL;
473}
474
475struct lazy_name_thread_data {
476 pthread_t pthread;
477 struct index_state *istate;
478 struct lazy_entry *lazy_entries;
479};
480
481static void *lazy_name_thread_proc(void *_data)
482{
483 struct lazy_name_thread_data *d = _data;
484 int k;
485
486 for (k = 0; k < d->istate->cache_nr; k++) {
487 struct cache_entry *ce_k = d->istate->cache[k];
488 ce_k->ce_flags |= CE_HASHED;
d22245a2 489 hashmap_entry_init(&ce_k->ent, d->lazy_entries[k].hash_name);
b94e5c1d 490 hashmap_add(&d->istate->name_hash, &ce_k->ent);
846df809
JH
491 }
492
493 return NULL;
494}
495
496static inline void lazy_update_dir_ref_counts(
497 struct index_state *istate,
498 struct lazy_entry *lazy_entries)
499{
500 int k;
501
502 for (k = 0; k < istate->cache_nr; k++) {
503 if (lazy_entries[k].dir)
504 lazy_entries[k].dir->nr++;
505 }
506}
507
508static void threaded_lazy_init_name_hash(
509 struct index_state *istate)
510{
2179045f 511 int err;
846df809
JH
512 int nr_each;
513 int k_start;
514 int t;
515 struct lazy_entry *lazy_entries;
516 struct lazy_dir_thread_data *td_dir;
517 struct lazy_name_thread_data *td_name;
518
07642942
NTND
519 if (!HAVE_THREADS)
520 return;
521
846df809
JH
522 k_start = 0;
523 nr_each = DIV_ROUND_UP(istate->cache_nr, lazy_nr_dir_threads);
524
ca56dadb
RS
525 CALLOC_ARRAY(lazy_entries, istate->cache_nr);
526 CALLOC_ARRAY(td_dir, lazy_nr_dir_threads);
527 CALLOC_ARRAY(td_name, 1);
846df809
JH
528
529 init_dir_mutex();
530
531 /*
532 * Phase 1:
533 * Build "istate->dir_hash" using n "dir" threads (and a read-only index).
534 */
535 for (t = 0; t < lazy_nr_dir_threads; t++) {
536 struct lazy_dir_thread_data *td_dir_t = td_dir + t;
537 td_dir_t->istate = istate;
538 td_dir_t->lazy_entries = lazy_entries;
539 td_dir_t->k_start = k_start;
540 k_start += nr_each;
541 if (k_start > istate->cache_nr)
542 k_start = istate->cache_nr;
543 td_dir_t->k_end = k_start;
2179045f
NTND
544 err = pthread_create(&td_dir_t->pthread, NULL, lazy_dir_thread_proc, td_dir_t);
545 if (err)
546 die(_("unable to create lazy_dir thread: %s"), strerror(err));
846df809
JH
547 }
548 for (t = 0; t < lazy_nr_dir_threads; t++) {
549 struct lazy_dir_thread_data *td_dir_t = td_dir + t;
550 if (pthread_join(td_dir_t->pthread, NULL))
551 die("unable to join lazy_dir_thread");
552 }
553
554 /*
555 * Phase 2:
556 * Iterate over all index entries and add them to the "istate->name_hash"
557 * using a single "name" background thread.
558 * (Testing showed it wasn't worth running more than 1 thread for this.)
559 *
560 * Meanwhile, finish updating the parent directory ref-counts for each
561 * index entry using the current thread. (This step is very fast and
562 * doesn't need threading.)
563 */
564 td_name->istate = istate;
565 td_name->lazy_entries = lazy_entries;
2179045f
NTND
566 err = pthread_create(&td_name->pthread, NULL, lazy_name_thread_proc, td_name);
567 if (err)
568 die(_("unable to create lazy_name thread: %s"), strerror(err));
846df809
JH
569
570 lazy_update_dir_ref_counts(istate, lazy_entries);
571
2179045f
NTND
572 err = pthread_join(td_name->pthread, NULL);
573 if (err)
574 die(_("unable to join lazy_name thread: %s"), strerror(err));
846df809
JH
575
576 cleanup_dir_mutex();
577
578 free(td_name);
579 free(td_dir);
580 free(lazy_entries);
581}
582
846df809
JH
583static void lazy_init_name_hash(struct index_state *istate)
584{
ca54d9ba 585
96872bc2
LT
586 if (istate->name_hash_initialized)
587 return;
c46c406a 588 trace_performance_enter();
6a9372f4 589 trace2_region_enter("index", "name-hash-init", istate->repo);
56a14ea7
SB
590 hashmap_init(&istate->name_hash, cache_entry_cmp, NULL, istate->cache_nr);
591 hashmap_init(&istate->dir_hash, dir_entry_cmp, NULL, istate->cache_nr);
846df809
JH
592
593 if (lookup_lazy_params(istate)) {
8b604d19
JH
594 /*
595 * Disable item counting and automatic rehashing because
596 * we do per-chain (mod n) locking rather than whole hashmap
597 * locking and we need to prevent the table-size from changing
598 * and bucket items from being redistributed.
599 */
600 hashmap_disable_item_counting(&istate->dir_hash);
846df809 601 threaded_lazy_init_name_hash(istate);
8b604d19 602 hashmap_enable_item_counting(&istate->dir_hash);
846df809
JH
603 } else {
604 int nr;
605 for (nr = 0; nr < istate->cache_nr; nr++)
606 hash_index_entry(istate, istate->cache[nr]);
607 }
608
96872bc2 609 istate->name_hash_initialized = 1;
6a9372f4 610 trace2_region_leave("index", "name-hash-init", istate->repo);
c46c406a 611 trace_performance_leave("initialize name hash");
96872bc2
LT
612}
613
846df809
JH
614/*
615 * A test routine for t/helper/ sources.
616 *
617 * Returns the number of threads used or 0 when
618 * the non-threaded code path was used.
619 *
620 * Requesting threading WILL NOT override guards
621 * in lookup_lazy_params().
622 */
623int test_lazy_init_name_hash(struct index_state *istate, int try_threaded)
624{
625 lazy_nr_dir_threads = 0;
626 lazy_try_threaded = try_threaded;
627
628 lazy_init_name_hash(istate);
629
630 return lazy_nr_dir_threads;
631}
632
96872bc2
LT
633void add_name_hash(struct index_state *istate, struct cache_entry *ce)
634{
96872bc2
LT
635 if (istate->name_hash_initialized)
636 hash_index_entry(istate, ce);
637}
638
2092678c
KB
639void remove_name_hash(struct index_state *istate, struct cache_entry *ce)
640{
419a597f
KB
641 if (!istate->name_hash_initialized || !(ce->ce_flags & CE_HASHED))
642 return;
643 ce->ce_flags &= ~CE_HASHED;
28ee7941 644 hashmap_remove(&istate->name_hash, &ce->ent, ce);
2092678c 645
419a597f
KB
646 if (ignore_case)
647 remove_dir_entry(istate, ce);
2092678c
KB
648}
649
cd2fef59
LT
650static int slow_same_name(const char *name1, int len1, const char *name2, int len2)
651{
652 if (len1 != len2)
653 return 0;
654
655 while (len1) {
656 unsigned char c1 = *name1++;
657 unsigned char c2 = *name2++;
658 len1--;
659 if (c1 != c2) {
660 c1 = toupper(c1);
661 c2 = toupper(c2);
662 if (c1 != c2)
663 return 0;
664 }
665 }
666 return 1;
667}
668
669static int same_name(const struct cache_entry *ce, const char *name, int namelen, int icase)
670{
671 int len = ce_namelen(ce);
672
673 /*
674 * Always do exact compare, even if we want a case-ignoring comparison;
675 * we do the quick exact one first, because it will be the common case.
676 */
be99ec97 677 if (len == namelen && !memcmp(name, ce->name, len))
cd2fef59
LT
678 return 1;
679
5102c617
JJ
680 if (!icase)
681 return 0;
682
2092678c 683 return slow_same_name(name, namelen, ce->name, len);
cd2fef59
LT
684}
685
41284eb0 686int index_dir_exists(struct index_state *istate, const char *name, int namelen)
db5360f3 687{
db5360f3
ES
688 struct dir_entry *dir;
689
690 lazy_init_name_hash(istate);
4589bca8 691 expand_to_path(istate, name, namelen, 0);
db5360f3 692 dir = find_dir_entry(istate, name, namelen);
41284eb0
DT
693 return dir && dir->nr;
694}
db5360f3 695
41284eb0
DT
696void adjust_dirname_case(struct index_state *istate, char *name)
697{
698 const char *startPtr = name;
699 const char *ptr = startPtr;
db5360f3 700
41284eb0 701 lazy_init_name_hash(istate);
4589bca8 702 expand_to_path(istate, name, strlen(name), 0);
41284eb0
DT
703 while (*ptr) {
704 while (*ptr && *ptr != '/')
705 ptr++;
706
707 if (*ptr == '/') {
708 struct dir_entry *dir;
709
c95525e9 710 dir = find_dir_entry(istate, name, ptr - name);
41284eb0
DT
711 if (dir) {
712 memcpy((void *)startPtr, dir->name + (startPtr - name), ptr - startPtr);
c95525e9 713 startPtr = ptr + 1;
41284eb0 714 }
c95525e9 715 ptr++;
41284eb0
DT
716 }
717 }
db5360f3
ES
718}
719
720struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int icase)
96872bc2 721{
96872bc2 722 struct cache_entry *ce;
f0e63c41 723 unsigned int hash = memihash(name, namelen);
96872bc2
LT
724
725 lazy_init_name_hash(istate);
4589bca8 726 expand_to_path(istate, name, namelen, icase);
96872bc2 727
f0e63c41
EW
728 ce = hashmap_get_entry_from_hash(&istate->name_hash, hash, NULL,
729 struct cache_entry, ent);
23dee69f 730 hashmap_for_each_entry_from(&istate->name_hash, ce, ent) {
419a597f
KB
731 if (same_name(ce, name, namelen, icase))
732 return ce;
96872bc2 733 }
df292c79 734 return NULL;
96872bc2 735}
2092678c 736
2092678c
KB
737void free_name_hash(struct index_state *istate)
738{
739 if (!istate->name_hash_initialized)
740 return;
741 istate->name_hash_initialized = 0;
2092678c 742
6da1a258
EN
743 hashmap_clear(&istate->name_hash);
744 hashmap_clear_and_free(&istate->dir_hash, struct dir_entry, ent);
2092678c 745}