Use helper function for copying index entry information
We used to just memcpy() the index entry when we copied the stat() and SHA1 hash information, which worked well enough back when the index entry was just an exact bit-for-bit representation of the information on disk. However, these days we actually have various management information in the cache entry too, and we should be careful to not overwrite it when we copy the stat information from another index entry. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Linus Torvalds committed
Feb 22, 2008 at 20:41 UTC
eb7a2f1d50ca8620b087dd2751d0fe2505e7974f
2 files changed
+18
-2
cache.h
+17
-1
@@ -110,7 +110,6 @@ struct ondisk_cache_entry {
110
};
111
112
struct cache_entry {
113
- struct cache_entry *next;
113
unsigned int ce_ctime;
114
unsigned int ce_mtime;
115
unsigned int ce_dev;
@@ -121,6 +120,7 @@ struct cache_entry {
120
unsigned int ce_size;
121
unsigned int ce_flags;
122
unsigned char sha1[20];
123
+ struct cache_entry *next;
124
char name[FLEX_ARRAY]; /* more */
125
};
126
@@ -137,6 +137,22 @@ struct cache_entry {
137
#define CE_HASHED (0x100000)
138
#define CE_UNHASHED (0x200000)
139
140
+/*
141
+ * Copy the sha1 and stat state of a cache entry from one to
142
+ * another. But we never change the name, or the hash state!
143
+ */
144
+#define CE_STATE_MASK (CE_HASHED | CE_UNHASHED)
145
+static inline void copy_cache_entry(struct cache_entry *dst, struct cache_entry *src)
146
+{
147
+ unsigned int state = dst->ce_flags & CE_STATE_MASK;
148
+
149
+ /* Don't copy hash chain and name */
150
+ memcpy(dst, src, offsetof(struct cache_entry, next));
151
+
152
+ /* Restore the hash state */
153
+ dst->ce_flags = (dst->ce_flags & ~CE_STATE_MASK) | state;
154
+}
155
+
156
/*
157
* We don't actually *remove* it, we can just mark it invalid so that
158
* we won't find it in lookups.
unpack-trees.c
+1
-1
@@ -590,7 +590,7 @@ static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
590
* a match.
591
*/
592
if (same(old, merge)) {
593
- memcpy(merge, old, offsetof(struct cache_entry, name));
593
+ copy_cache_entry(merge, old);
594
} else {
595
verify_uptodate(old, o);
596
invalidate_ce_path(old);