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