@matheusbsbs / matheus-bagliano / commits / 4aab5b46f4
Fork 0

Make read-cache.c "the_index" free.

This makes all low-level functions defined in read-cache.c to take an explicit index_state structure as their first parameter, to specify which index to work on. These functions traditionally operated on "the_index" and were named foo_cache(); the counterparts this patch introduces are called foo_index(). The traditional foo_cache() functions are made into macros that give "the_index" to their corresponding foo_index() functions. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 1, 2007 at 23:26 UTC 4aab5b46f44a7ba860e07a52be506b7b57b2bd5f
2 files changed +155 -121
cache.h
+27 -12
@@ -154,12 +154,27 @@ struct index_state {
154
155 extern struct index_state the_index;
156
157 +#ifndef NO_THE_INDEX_COMPATIBILITY_MACROS
158 #define active_cache (the_index.cache)
159 #define active_nr (the_index.cache_nr)
160 #define active_alloc (the_index.cache_alloc)
161 #define active_cache_changed (the_index.cache_changed)
162 #define active_cache_tree (the_index.cache_tree)
163
164 +#define read_cache() read_index(&the_index)
165 +#define read_cache_from(path) read_index_from(&the_index, (path))
166 +#define write_cache(newfd, cache, entries) write_index(&the_index, (newfd))
167 +#define discard_cache() discard_index(&the_index)
168 +#define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen))
169 +#define add_cache_entry(ce, option) add_index_entry(&the_index, (ce), (option))
170 +#define remove_cache_entry_at(pos) remove_index_entry_at(&the_index, (pos))
171 +#define remove_file_from_cache(path) remove_file_from_index(&the_index, (path))
172 +#define add_file_to_cache(path, verbose) add_file_to_index(&the_index, (path), (verbose))
173 +#define refresh_cache(flags) refresh_index(&the_index, flags)
174 +#define ce_match_stat(ce, st, really) ie_match_stat(&the_index, (ce), (st), (really))
175 +#define ce_modified(ce, st, really) ie_modified(&the_index, (ce), (st), (really))
176 +#endif
177 +
178 enum object_type {
179 OBJ_BAD = -1,
180 OBJ_NONE = 0,
@@ -208,23 +223,23 @@ extern void verify_non_filename(const char *prefix, const char *name);
223 #define alloc_nr(x) (((x)+16)*3/2)
224
225 /* Initialize and use the cache information */
211 -extern int read_cache(void);
212 -extern int read_cache_from(const char *path);
213 -extern int write_cache(int newfd, struct cache_entry **cache, int entries);
214 -extern int discard_cache(void);
226 +extern int read_index(struct index_state *);
227 +extern int read_index_from(struct index_state *, const char *path);
228 +extern int write_index(struct index_state *, int newfd);
229 +extern int discard_index(struct index_state *);
230 extern int verify_path(const char *path);
216 -extern int cache_name_pos(const char *name, int namelen);
231 +extern int index_name_pos(struct index_state *, const char *name, int namelen);
232 #define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */
233 #define ADD_CACHE_OK_TO_REPLACE 2 /* Ok to replace file/directory */
234 #define ADD_CACHE_SKIP_DFCHECK 4 /* Ok to skip DF conflict checks */
220 -extern int add_cache_entry(struct cache_entry *ce, int option);
235 +extern int add_index_entry(struct index_state *, struct cache_entry *ce, int option);
236 extern struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really);
222 -extern int remove_cache_entry_at(int pos);
223 -extern int remove_file_from_cache(const char *path);
224 -extern int add_file_to_cache(const char *path, int verbose);
237 +extern int remove_index_entry_at(struct index_state *, int pos);
238 +extern int remove_file_from_index(struct index_state *, const char *path);
239 +extern int add_file_to_index(struct index_state *, const char *path, int verbose);
240 extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
226 -extern int ce_match_stat(struct cache_entry *ce, struct stat *st, int);
227 -extern int ce_modified(struct cache_entry *ce, struct stat *st, int);
241 +extern int ie_match_stat(struct index_state *, struct cache_entry *, struct stat *, int);
242 +extern int ie_modified(struct index_state *, struct cache_entry *, struct stat *, int);
243 extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);
244 extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, enum object_type type, const char *path);
245 extern int read_pipe(int fd, char** return_buf, unsigned long* return_size);
@@ -236,7 +251,7 @@ extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
251 #define REFRESH_UNMERGED 0x0002 /* allow unmerged */
252 #define REFRESH_QUIET 0x0004 /* be quiet about it */
253 #define REFRESH_IGNORE_MISSING 0x0008 /* ignore non-existent */
239 -extern int refresh_cache(unsigned int flags);
254 +extern int refresh_index(struct index_state *, unsigned int flags);
255
256 struct lock_file {
257 struct lock_file *next;
read-cache.c
+128 -109
@@ -3,6 +3,7 @@
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 +#define NO_THE_INDEX_COMPATIBILITY_MACROS
7 #include "cache.h"
8 #include "cache-tree.h"
9 #include "refs.h"
@@ -20,9 +21,6 @@
21 #define CACHE_EXT_TREE 0x54524545 /* "TREE" */
22
23 struct index_state the_index;
23 -#define index_file_timestamp (the_index.timestamp)
24 -#define cache_mmap (the_index.mmap)
25 -#define cache_mmap_size (the_index.mmap_size)
24
25 /*
26 * This only updates the "non-critical" parts of the directory
@@ -192,7 +190,8 @@ static int ce_match_stat_basic(struct cache_entry *ce, struct stat *st)
190 return changed;
191 }
192
195 -int ce_match_stat(struct cache_entry *ce, struct stat *st, int options)
193 +int ie_match_stat(struct index_state *istate,
194 + struct cache_entry *ce, struct stat *st, int options)
195 {
196 unsigned int changed;
197 int ignore_valid = options & 01;
@@ -224,8 +223,8 @@ int ce_match_stat(struct cache_entry *ce, struct stat *st, int options)
223 * carefully than others.
224 */
225 if (!changed &&
227 - index_file_timestamp &&
228 - index_file_timestamp <= ntohl(ce->ce_mtime.sec)) {
226 + istate->timestamp &&
227 + istate->timestamp <= ntohl(ce->ce_mtime.sec)) {
228 if (assume_racy_is_modified)
229 changed |= DATA_CHANGED;
230 else
@@ -235,10 +234,11 @@ int ce_match_stat(struct cache_entry *ce, struct stat *st, int options)
234 return changed;
235 }
236
238 -int ce_modified(struct cache_entry *ce, struct stat *st, int really)
237 +int ie_modified(struct index_state *istate,
238 + struct cache_entry *ce, struct stat *st, int really)
239 {
240 int changed, changed_fs;
241 - changed = ce_match_stat(ce, st, really);
241 + changed = ie_match_stat(istate, ce, st, really);
242 if (!changed)
243 return 0;
244 /*
@@ -306,15 +306,15 @@ int cache_name_compare(const char *name1, int flags1, const char *name2, int fla
306 return 0;
307 }
308
309 -int cache_name_pos(const char *name, int namelen)
309 +int index_name_pos(struct index_state *istate, const char *name, int namelen)
310 {
311 int first, last;
312
313 first = 0;
314 - last = active_nr;
314 + last = istate->cache_nr;
315 while (last > first) {
316 int next = (last + first) >> 1;
317 - struct cache_entry *ce = active_cache[next];
317 + struct cache_entry *ce = istate->cache[next];
318 int cmp = cache_name_compare(name, namelen, ce->name, ntohs(ce->ce_flags));
319 if (!cmp)
320 return next;
@@ -328,27 +328,29 @@ int cache_name_pos(const char *name, int namelen)
328 }
329
330 /* Remove entry, return true if there are more entries to go.. */
331 -int remove_cache_entry_at(int pos)
331 +int remove_index_entry_at(struct index_state *istate, int pos)
332 {
333 - active_cache_changed = 1;
334 - active_nr--;
335 - if (pos >= active_nr)
333 + istate->cache_changed = 1;
334 + istate->cache_nr--;
335 + if (pos >= istate->cache_nr)
336 return 0;
337 - memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
337 + memmove(istate->cache + pos,
338 + istate->cache + pos + 1,
339 + (istate->cache_nr - pos) * sizeof(struct cache_entry *));
340 return 1;
341 }
342
341 -int remove_file_from_cache(const char *path)
343 +int remove_file_from_index(struct index_state *istate, const char *path)
344 {
343 - int pos = cache_name_pos(path, strlen(path));
345 + int pos = index_name_pos(istate, path, strlen(path));
346 if (pos < 0)
347 pos = -pos-1;
346 - while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
347 - remove_cache_entry_at(pos);
348 + while (pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path))
349 + remove_index_entry_at(istate, pos);
350 return 0;
351 }
352
351 -int add_file_to_cache(const char *path, int verbose)
353 +int add_file_to_index(struct index_state *istate, const char *path, int verbose)
354 {
355 int size, namelen;
356 struct stat st;
@@ -378,19 +380,19 @@ int add_file_to_cache(const char *path, int verbose)
380 * from it, otherwise assume unexecutable regular file.
381 */
382 struct cache_entry *ent;
381 - int pos = cache_name_pos(path, namelen);
383 + int pos = index_name_pos(istate, path, namelen);
384
383 - ent = (0 <= pos) ? active_cache[pos] : NULL;
385 + ent = (0 <= pos) ? istate->cache[pos] : NULL;
386 ce->ce_mode = ce_mode_from_stat(ent, st.st_mode);
387 }
388
389 if (index_path(ce->sha1, path, &st, 1))
390 die("unable to index file %s", path);
389 - if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))
391 + if (add_index_entry(istate, ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))
392 die("unable to add %s to index",path);
393 if (verbose)
394 printf("add '%s'\n", path);
393 - cache_tree_invalidate_path(active_cache_tree, path);
395 + cache_tree_invalidate_path(istate->cache_tree, path);
396 return 0;
397 }
398
@@ -494,15 +496,16 @@ inside:
496 * Do we have another file that has the beginning components being a
497 * proper superset of the name we're trying to add?
498 */
497 -static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replace)
499 +static int has_file_name(struct index_state *istate,
500 + const struct cache_entry *ce, int pos, int ok_to_replace)
501 {
502 int retval = 0;
503 int len = ce_namelen(ce);
504 int stage = ce_stage(ce);
505 const char *name = ce->name;
506
504 - while (pos < active_nr) {
505 - struct cache_entry *p = active_cache[pos++];
507 + while (pos < istate->cache_nr) {
508 + struct cache_entry *p = istate->cache[pos++];
509
510 if (len >= ce_namelen(p))
511 break;
@@ -517,7 +520,7 @@ static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replac
520 retval = -1;
521 if (!ok_to_replace)
522 break;
520 - remove_cache_entry_at(--pos);
523 + remove_index_entry_at(istate, --pos);
524 }
525 return retval;
526 }
@@ -526,7 +529,8 @@ static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replac
529 * Do we have another file with a pathname that is a proper
530 * subset of the name we're trying to add?
531 */
529 -static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace)
532 +static int has_dir_name(struct index_state *istate,
533 + const struct cache_entry *ce, int pos, int ok_to_replace)
534 {
535 int retval = 0;
536 int stage = ce_stage(ce);
@@ -544,7 +548,7 @@ static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace
548 }
549 len = slash - name;
550
547 - pos = cache_name_pos(name, ntohs(create_ce_flags(len, stage)));
551 + pos = index_name_pos(istate, name, ntohs(create_ce_flags(len, stage)));
552 if (pos >= 0) {
553 /*
554 * Found one, but not so fast. This could
@@ -554,11 +558,11 @@ static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace
558 * it is Ok to have a directory at the same
559 * path.
560 */
557 - if (stage || active_cache[pos]->ce_mode) {
561 + if (stage || istate->cache[pos]->ce_mode) {
562 retval = -1;
563 if (!ok_to_replace)
564 break;
561 - remove_cache_entry_at(pos);
565 + remove_index_entry_at(istate, pos);
566 continue;
567 }
568 }
@@ -570,8 +574,8 @@ static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace
574 * already matches the sub-directory, then we know
575 * we're ok, and we can exit.
576 */
573 - while (pos < active_nr) {
574 - struct cache_entry *p = active_cache[pos];
577 + while (pos < istate->cache_nr) {
578 + struct cache_entry *p = istate->cache[pos];
579 if ((ce_namelen(p) <= len) ||
580 (p->name[len] != '/') ||
581 memcmp(p->name, name, len))
@@ -598,7 +602,9 @@ static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace
602 * from the cache so the caller should recompute the insert position.
603 * When this happens, we return non-zero.
604 */
601 -static int check_file_directory_conflict(const struct cache_entry *ce, int pos, int ok_to_replace)
605 +static int check_file_directory_conflict(struct index_state *istate,
606 + const struct cache_entry *ce,
607 + int pos, int ok_to_replace)
608 {
609 int retval;
610
@@ -613,28 +619,28 @@ static int check_file_directory_conflict(const struct cache_entry *ce, int pos,
619 * first, since removing those will not change the position
620 * in the array.
621 */
616 - retval = has_file_name(ce, pos, ok_to_replace);
622 + retval = has_file_name(istate, ce, pos, ok_to_replace);
623
624 /*
625 * Then check if the path might have a clashing sub-directory
626 * before it.
627 */
622 - return retval + has_dir_name(ce, pos, ok_to_replace);
628 + return retval + has_dir_name(istate, ce, pos, ok_to_replace);
629 }
630
625 -int add_cache_entry(struct cache_entry *ce, int option)
631 +int add_index_entry(struct index_state *istate, struct cache_entry *ce, int option)
632 {
633 int pos;
634 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
635 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
636 int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
637
632 - pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
638 + pos = index_name_pos(istate, ce->name, ntohs(ce->ce_flags));
639
640 /* existing match? Just replace it. */
641 if (pos >= 0) {
636 - active_cache_changed = 1;
637 - active_cache[pos] = ce;
642 + istate->cache_changed = 1;
643 + istate->cache[pos] = ce;
644 return 0;
645 }
646 pos = -pos-1;
@@ -643,10 +649,10 @@ int add_cache_entry(struct cache_entry *ce, int option)
649 * Inserting a merged entry ("stage 0") into the index
650 * will always replace all non-merged entries..
651 */
646 - if (pos < active_nr && ce_stage(ce) == 0) {
647 - while (ce_same_name(active_cache[pos], ce)) {
652 + if (pos < istate->cache_nr && ce_stage(ce) == 0) {
653 + while (ce_same_name(istate->cache[pos], ce)) {
654 ok_to_add = 1;
649 - if (!remove_cache_entry_at(pos))
655 + if (!remove_index_entry_at(istate, pos))
656 break;
657 }
658 }
@@ -657,25 +663,29 @@ int add_cache_entry(struct cache_entry *ce, int option)
663 return -1;
664
665 if (!skip_df_check &&
660 - check_file_directory_conflict(ce, pos, ok_to_replace)) {
666 + check_file_directory_conflict(istate, ce, pos, ok_to_replace)) {
667 if (!ok_to_replace)
662 - return error("'%s' appears as both a file and as a directory", ce->name);
663 - pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
668 + return error("'%s' appears as both a file and as a directory",
669 + ce->name);
670 + pos = index_name_pos(istate, ce->name, ntohs(ce->ce_flags));
671 pos = -pos-1;
672 }
673
674 /* Make sure the array is big enough .. */
668 - if (active_nr == active_alloc) {
669 - active_alloc = alloc_nr(active_alloc);
670 - active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));
675 + if (istate->cache_nr == istate->cache_alloc) {
676 + istate->cache_alloc = alloc_nr(istate->cache_alloc);
677 + istate->cache = xrealloc(istate->cache,
678 + istate->cache_alloc * sizeof(struct cache_entry *));
679 }
680
681 /* Add it in.. */
674 - active_nr++;
675 - if (active_nr > pos)
676 - memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
677 - active_cache[pos] = ce;
678 - active_cache_changed = 1;
682 + istate->cache_nr++;
683 + if (istate->cache_nr > pos)
684 + memmove(istate->cache + pos + 1,
685 + istate->cache + pos,
686 + (istate->cache_nr - pos - 1) * sizeof(ce));
687 + istate->cache[pos] = ce;
688 + istate->cache_changed = 1;
689 return 0;
690 }
691
@@ -690,7 +700,8 @@ int add_cache_entry(struct cache_entry *ce, int option)
700 * For example, you'd want to do this after doing a "git-read-tree",
701 * to link up the stat cache details with the proper files.
702 */
693 -static struct cache_entry *refresh_cache_ent(struct cache_entry *ce, int really, int *err)
703 +static struct cache_entry *refresh_cache_ent(struct index_state *istate,
704 + struct cache_entry *ce, int really, int *err)
705 {
706 struct stat st;
707 struct cache_entry *updated;
@@ -702,7 +713,7 @@ static struct cache_entry *refresh_cache_ent(struct cache_entry *ce, int really,
713 return NULL;
714 }
715
705 - changed = ce_match_stat(ce, &st, really);
716 + changed = ie_match_stat(istate, ce, &st, really);
717 if (!changed) {
718 if (really && assume_unchanged &&
719 !(ce->ce_flags & htons(CE_VALID)))
@@ -711,7 +722,7 @@ static struct cache_entry *refresh_cache_ent(struct cache_entry *ce, int really,
722 return ce;
723 }
724
714 - if (ce_modified(ce, &st, really)) {
725 + if (ie_modified(istate, ce, &st, really)) {
726 if (err)
727 *err = EINVAL;
728 return NULL;
@@ -734,7 +745,7 @@ static struct cache_entry *refresh_cache_ent(struct cache_entry *ce, int really,
745 return updated;
746 }
747
737 -int refresh_cache(unsigned int flags)
748 +int refresh_index(struct index_state *istate, unsigned int flags)
749 {
750 int i;
751 int has_errors = 0;
@@ -743,14 +754,14 @@ int refresh_cache(unsigned int flags)
754 int quiet = (flags & REFRESH_QUIET) != 0;
755 int not_new = (flags & REFRESH_IGNORE_MISSING) != 0;
756
746 - for (i = 0; i < active_nr; i++) {
757 + for (i = 0; i < istate->cache_nr; i++) {
758 struct cache_entry *ce, *new;
759 int cache_errno = 0;
760
750 - ce = active_cache[i];
761 + ce = istate->cache[i];
762 if (ce_stage(ce)) {
752 - while ((i < active_nr) &&
753 - ! strcmp(active_cache[i]->name, ce->name))
763 + while ((i < istate->cache_nr) &&
764 + ! strcmp(istate->cache[i]->name, ce->name))
765 i++;
766 i--;
767 if (allow_unmerged)
@@ -760,7 +771,7 @@ int refresh_cache(unsigned int flags)
771 continue;
772 }
773
763 - new = refresh_cache_ent(ce, really, &cache_errno);
774 + new = refresh_cache_ent(istate, ce, really, &cache_errno);
775 if (new == ce)
776 continue;
777 if (!new) {
@@ -771,7 +782,7 @@ int refresh_cache(unsigned int flags)
782 * means the index is not valid anymore.
783 */
784 ce->ce_flags &= ~htons(CE_VALID);
774 - active_cache_changed = 1;
785 + istate->cache_changed = 1;
786 }
787 if (quiet)
788 continue;
@@ -779,18 +790,18 @@ int refresh_cache(unsigned int flags)
790 has_errors = 1;
791 continue;
792 }
782 - active_cache_changed = 1;
783 - /* You can NOT just free active_cache[i] here, since it
793 + istate->cache_changed = 1;
794 + /* You can NOT just free istate->cache[i] here, since it
795 * might not be necessarily malloc()ed but can also come
796 * from mmap(). */
786 - active_cache[i] = new;
797 + istate->cache[i] = new;
798 }
799 return has_errors;
800 }
801
802 struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really)
803 {
793 - return refresh_cache_ent(ce, really, NULL);
804 + return refresh_cache_ent(&the_index, ce, really, NULL);
805 }
806
807 static int verify_hdr(struct cache_header *hdr, unsigned long size)
@@ -810,11 +821,12 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
821 return 0;
822 }
823
813 -static int read_index_extension(const char *ext, void *data, unsigned long sz)
824 +static int read_index_extension(struct index_state *istate,
825 + const char *ext, void *data, unsigned long sz)
826 {
827 switch (CACHE_EXT(ext)) {
828 case CACHE_EXT_TREE:
817 - active_cache_tree = cache_tree_read(data, sz);
829 + istate->cache_tree = cache_tree_read(data, sz);
830 break;
831 default:
832 if (*ext < 'A' || 'Z' < *ext)
@@ -826,13 +838,13 @@ static int read_index_extension(const char *ext, void *data, unsigned long sz)
838 return 0;
839 }
840
829 -int read_cache(void)
841 +int read_index(struct index_state *istate)
842 {
831 - return read_cache_from(get_index_file());
843 + return read_index_from(istate, get_index_file());
844 }
845
846 /* remember to discard_cache() before reading a different cache! */
835 -int read_cache_from(const char *path)
847 +int read_index_from(struct index_state *istate, const char *path)
848 {
849 int fd, i;
850 struct stat st;
@@ -840,11 +852,11 @@ int read_cache_from(const char *path)
852 struct cache_header *hdr;
853
854 errno = EBUSY;
843 - if (cache_mmap)
844 - return active_nr;
855 + if (istate->mmap)
856 + return istate->cache_nr;
857
858 errno = ENOENT;
847 - index_file_timestamp = 0;
859 + istate->timestamp = 0;
860 fd = open(path, O_RDONLY);
861 if (fd < 0) {
862 if (errno == ENOENT)
@@ -853,32 +865,35 @@ int read_cache_from(const char *path)
865 }
866
867 if (!fstat(fd, &st)) {
856 - cache_mmap_size = xsize_t(st.st_size);
868 + istate->mmap_size = xsize_t(st.st_size);
869 errno = EINVAL;
858 - if (cache_mmap_size >= sizeof(struct cache_header) + 20)
859 - cache_mmap = xmmap(NULL, cache_mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
870 + if (istate->mmap_size >= sizeof(struct cache_header) + 20)
871 + istate->mmap = xmmap(NULL, istate->mmap_size,
872 + PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
873 else
874 die("index file smaller than expected");
875 } else
876 die("cannot stat the open index (%s)", strerror(errno));
877 close(fd);
878
866 - hdr = cache_mmap;
867 - if (verify_hdr(hdr, cache_mmap_size) < 0)
879 + hdr = istate->mmap;
880 + if (verify_hdr(hdr, istate->mmap_size) < 0)
881 goto unmap;
882
870 - active_nr = ntohl(hdr->hdr_entries);
871 - active_alloc = alloc_nr(active_nr);
872 - active_cache = xcalloc(active_alloc, sizeof(struct cache_entry *));
883 + istate->cache_nr = ntohl(hdr->hdr_entries);
884 + istate->cache_alloc = alloc_nr(istate->cache_nr);
885 + istate->cache = xcalloc(istate->cache_alloc, sizeof(struct cache_entry *));
886
887 offset = sizeof(*hdr);
875 - for (i = 0; i < active_nr; i++) {
876 - struct cache_entry *ce = (struct cache_entry *) ((char *) cache_mmap + offset);
888 + for (i = 0; i < istate->cache_nr; i++) {
889 + struct cache_entry *ce;
890 +
891 + ce = (struct cache_entry *)((char *)(istate->mmap) + offset);
892 offset = offset + ce_size(ce);
878 - active_cache[i] = ce;
893 + istate->cache[i] = ce;
894 }
880 - index_file_timestamp = st.st_mtime;
881 - while (offset <= cache_mmap_size - 20 - 8) {
895 + istate->timestamp = st.st_mtime;
896 + while (offset <= istate->mmap_size - 20 - 8) {
897 /* After an array of active_nr index entries,
898 * there can be arbitrary number of extended
899 * sections, each of which is prefixed with
@@ -886,35 +901,37 @@ int read_cache_from(const char *path)
901 * in 4-byte network byte order.
902 */
903 unsigned long extsize;
889 - memcpy(&extsize, (char *) cache_mmap + offset + 4, 4);
904 + memcpy(&extsize, (char *)(istate->mmap) + offset + 4, 4);
905 extsize = ntohl(extsize);
891 - if (read_index_extension(((const char *) cache_mmap) + offset,
892 - (char *) cache_mmap + offset + 8,
906 + if (read_index_extension(istate,
907 + ((const char *) (istate->mmap)) + offset,
908 + (char *) (istate->mmap) + offset + 8,
909 extsize) < 0)
910 goto unmap;
911 offset += 8;
912 offset += extsize;
913 }
898 - return active_nr;
914 + return istate->cache_nr;
915
916 unmap:
901 - munmap(cache_mmap, cache_mmap_size);
917 + munmap(istate->mmap, istate->mmap_size);
918 errno = EINVAL;
919 die("index file corrupt");
920 }
921
906 -int discard_cache(void)
922 +int discard_index(struct index_state *istate)
923 {
924 int ret;
925
910 - active_nr = active_cache_changed = 0;
911 - index_file_timestamp = 0;
912 - cache_tree_free(&active_cache_tree);
913 - if (cache_mmap == NULL)
926 + istate->cache_nr = 0;
927 + istate->cache_changed = 0;
928 + istate->timestamp = 0;
929 + cache_tree_free(&(istate->cache_tree));
930 + if (istate->mmap == NULL)
931 return 0;
915 - ret = munmap(cache_mmap, cache_mmap_size);
916 - cache_mmap = NULL;
917 - cache_mmap_size = 0;
932 + ret = munmap(istate->mmap, istate->mmap_size);
933 + istate->mmap = NULL;
934 + istate->mmap_size = 0;
935
936 /* no need to throw away allocated active_cache */
937 return ret;
@@ -1033,11 +1050,13 @@ static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
1050 }
1051 }
1052
1036 -int write_cache(int newfd, struct cache_entry **cache, int entries)
1053 +int write_index(struct index_state *istate, int newfd)
1054 {
1055 SHA_CTX c;
1056 struct cache_header hdr;
1057 int i, removed;
1058 + struct cache_entry **cache = istate->cache;
1059 + int entries = istate->cache_nr;
1060
1061 for (i = removed = 0; i < entries; i++)
1062 if (!cache[i]->ce_mode)
@@ -1055,17 +1074,17 @@ int write_cache(int newfd, struct cache_entry **cache, int entries)
1074 struct cache_entry *ce = cache[i];
1075 if (!ce->ce_mode)
1076 continue;
1058 - if (index_file_timestamp &&
1059 - index_file_timestamp <= ntohl(ce->ce_mtime.sec))
1077 + if (istate->timestamp &&
1078 + istate->timestamp <= ntohl(ce->ce_mtime.sec))
1079 ce_smudge_racily_clean_entry(ce);
1080 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
1081 return -1;
1082 }
1083
1084 /* Write extension data here */
1066 - if (active_cache_tree) {
1085 + if (istate->cache_tree) {
1086 unsigned long sz;
1068 - void *data = cache_tree_write(active_cache_tree, &sz);
1087 + void *data = cache_tree_write(istate->cache_tree, &sz);
1088 if (data &&
1089 !write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sz) &&
1090 !ce_write(&c, newfd, data, sz))