@@ -24,6 +24,11 @@ unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0;
2424
2525struct cache_tree * active_cache_tree = NULL ;
2626
27+ int cache_errno = 0 ;
28+
29+ static void * cache_mmap = NULL ;
30+ static size_t cache_mmap_size = 0 ;
31+
2732/*
2833 * This only updates the "non-critical" parts of the directory
2934 * cache, ie the parts that aren't tracked by GIT, and only used
@@ -577,22 +582,6 @@ int add_cache_entry(struct cache_entry *ce, int option)
577582 return 0 ;
578583}
579584
580- /* Three functions to allow overloaded pointer return; see linux/err.h */
581- static inline void * ERR_PTR (long error )
582- {
583- return (void * ) error ;
584- }
585-
586- static inline long PTR_ERR (const void * ptr )
587- {
588- return (long ) ptr ;
589- }
590-
591- static inline long IS_ERR (const void * ptr )
592- {
593- return (unsigned long )ptr > (unsigned long )-1000L ;
594- }
595-
596585/*
597586 * "refresh" does not calculate a new sha1 file or bring the
598587 * cache up-to-date for mode/content changes. But what it
@@ -604,26 +593,30 @@ static inline long IS_ERR(const void *ptr)
604593 * For example, you'd want to do this after doing a "git-read-tree",
605594 * to link up the stat cache details with the proper files.
606595 */
607- static struct cache_entry * refresh_entry (struct cache_entry * ce , int really )
596+ struct cache_entry * refresh_cache_entry (struct cache_entry * ce , int really )
608597{
609598 struct stat st ;
610599 struct cache_entry * updated ;
611600 int changed , size ;
612601
613- if (lstat (ce -> name , & st ) < 0 )
614- return ERR_PTR (- errno );
602+ if (lstat (ce -> name , & st ) < 0 ) {
603+ cache_errno = errno ;
604+ return NULL ;
605+ }
615606
616607 changed = ce_match_stat (ce , & st , really );
617608 if (!changed ) {
618609 if (really && assume_unchanged &&
619610 !(ce -> ce_flags & htons (CE_VALID )))
620611 ; /* mark this one VALID again */
621612 else
622- return NULL ;
613+ return ce ;
623614 }
624615
625- if (ce_modified (ce , & st , really ))
626- return ERR_PTR (- EINVAL );
616+ if (ce_modified (ce , & st , really )) {
617+ cache_errno = EINVAL ;
618+ return NULL ;
619+ }
627620
628621 size = ce_size (ce );
629622 updated = xmalloc (size );
@@ -666,13 +659,13 @@ int refresh_cache(unsigned int flags)
666659 continue ;
667660 }
668661
669- new = refresh_entry (ce , really );
670- if (! new )
662+ new = refresh_cache_entry (ce , really );
663+ if (new == ce )
671664 continue ;
672- if (IS_ERR ( new ) ) {
673- if (not_new && PTR_ERR ( new ) == - ENOENT )
665+ if (! new ) {
666+ if (not_new && cache_errno == ENOENT )
674667 continue ;
675- if (really && PTR_ERR ( new ) == - EINVAL ) {
668+ if (really && cache_errno == EINVAL ) {
676669 /* If we are doing --really-refresh that
677670 * means the index is not valid anymore.
678671 */
@@ -728,40 +721,44 @@ static int read_index_extension(const char *ext, void *data, unsigned long sz)
728721}
729722
730723int read_cache (void )
724+ {
725+ return read_cache_from (get_index_file ());
726+ }
727+
728+ /* remember to discard_cache() before reading a different cache! */
729+ int read_cache_from (const char * path )
731730{
732731 int fd , i ;
733732 struct stat st ;
734- unsigned long size , offset ;
735- void * map ;
733+ unsigned long offset ;
736734 struct cache_header * hdr ;
737735
738736 errno = EBUSY ;
739- if (active_cache )
737+ if (cache_mmap )
740738 return active_nr ;
741739
742740 errno = ENOENT ;
743741 index_file_timestamp = 0 ;
744- fd = open (get_index_file () , O_RDONLY );
742+ fd = open (path , O_RDONLY );
745743 if (fd < 0 ) {
746744 if (errno == ENOENT )
747745 return 0 ;
748746 die ("index file open failed (%s)" , strerror (errno ));
749747 }
750748
751- size = 0 ; /* avoid gcc warning */
752- map = MAP_FAILED ;
749+ cache_mmap = MAP_FAILED ;
753750 if (!fstat (fd , & st )) {
754- size = st .st_size ;
751+ cache_mmap_size = st .st_size ;
755752 errno = EINVAL ;
756- if (size >= sizeof (struct cache_header ) + 20 )
757- map = mmap (NULL , size , PROT_READ | PROT_WRITE , MAP_PRIVATE , fd , 0 );
753+ if (cache_mmap_size >= sizeof (struct cache_header ) + 20 )
754+ cache_mmap = mmap (NULL , cache_mmap_size , PROT_READ | PROT_WRITE , MAP_PRIVATE , fd , 0 );
758755 }
759756 close (fd );
760- if (map == MAP_FAILED )
757+ if (cache_mmap == MAP_FAILED )
761758 die ("index file mmap failed (%s)" , strerror (errno ));
762759
763- hdr = map ;
764- if (verify_hdr (hdr , size ) < 0 )
760+ hdr = cache_mmap ;
761+ if (verify_hdr (hdr , cache_mmap_size ) < 0 )
765762 goto unmap ;
766763
767764 active_nr = ntohl (hdr -> hdr_entries );
@@ -770,23 +767,23 @@ int read_cache(void)
770767
771768 offset = sizeof (* hdr );
772769 for (i = 0 ; i < active_nr ; i ++ ) {
773- struct cache_entry * ce = (struct cache_entry * ) ((char * ) map + offset );
770+ struct cache_entry * ce = (struct cache_entry * ) ((char * ) cache_mmap + offset );
774771 offset = offset + ce_size (ce );
775772 active_cache [i ] = ce ;
776773 }
777774 index_file_timestamp = st .st_mtime ;
778- while (offset <= size - 20 - 8 ) {
775+ while (offset <= cache_mmap_size - 20 - 8 ) {
779776 /* After an array of active_nr index entries,
780777 * there can be arbitrary number of extended
781778 * sections, each of which is prefixed with
782779 * extension name (4-byte) and section length
783780 * in 4-byte network byte order.
784781 */
785782 unsigned long extsize ;
786- memcpy (& extsize , (char * ) map + offset + 4 , 4 );
783+ memcpy (& extsize , (char * ) cache_mmap + offset + 4 , 4 );
787784 extsize = ntohl (extsize );
788- if (read_index_extension (((const char * ) map ) + offset ,
789- (char * ) map + offset + 8 ,
785+ if (read_index_extension (((const char * ) cache_mmap ) + offset ,
786+ (char * ) cache_mmap + offset + 8 ,
790787 extsize ) < 0 )
791788 goto unmap ;
792789 offset += 8 ;
@@ -795,7 +792,7 @@ int read_cache(void)
795792 return active_nr ;
796793
797794unmap :
798- munmap (map , size );
795+ munmap (cache_mmap , cache_mmap_size );
799796 errno = EINVAL ;
800797 die ("index file corrupt" );
801798}
0 commit comments