@@ -1202,71 +1202,61 @@ int read_index(struct index_state *istate)
1202
1202
return read_index_from (istate , get_index_file ());
1203
1203
}
1204
1204
1205
- static void convert_from_disk ( struct ondisk_cache_entry * ondisk , struct cache_entry * ce )
1205
+ static struct cache_entry * create_from_disk ( struct ondisk_cache_entry * ondisk )
1206
1206
{
1207
+ struct cache_entry * ce ;
1207
1208
size_t len ;
1208
1209
const char * name ;
1210
+ unsigned int flags ;
1209
1211
1210
- ce -> ce_ctime .sec = ntohl (ondisk -> ctime .sec );
1211
- ce -> ce_mtime .sec = ntohl (ondisk -> mtime .sec );
1212
- ce -> ce_ctime .nsec = ntohl (ondisk -> ctime .nsec );
1213
- ce -> ce_mtime .nsec = ntohl (ondisk -> mtime .nsec );
1214
- ce -> ce_dev = ntohl (ondisk -> dev );
1215
- ce -> ce_ino = ntohl (ondisk -> ino );
1216
- ce -> ce_mode = ntohl (ondisk -> mode );
1217
- ce -> ce_uid = ntohl (ondisk -> uid );
1218
- ce -> ce_gid = ntohl (ondisk -> gid );
1219
- ce -> ce_size = ntohl (ondisk -> size );
1220
1212
/* On-disk flags are just 16 bits */
1221
- ce -> ce_flags = ntohs (ondisk -> flags );
1222
-
1223
- hashcpy (ce -> sha1 , ondisk -> sha1 );
1213
+ flags = ntohs (ondisk -> flags );
1214
+ len = flags & CE_NAMEMASK ;
1224
1215
1225
- len = ce -> ce_flags & CE_NAMEMASK ;
1226
-
1227
- if (ce -> ce_flags & CE_EXTENDED ) {
1216
+ if (flags & CE_EXTENDED ) {
1228
1217
struct ondisk_cache_entry_extended * ondisk2 ;
1229
1218
int extended_flags ;
1230
1219
ondisk2 = (struct ondisk_cache_entry_extended * )ondisk ;
1231
1220
extended_flags = ntohs (ondisk2 -> flags2 ) << 16 ;
1232
1221
/* We do not yet understand any bit out of CE_EXTENDED_FLAGS */
1233
1222
if (extended_flags & ~CE_EXTENDED_FLAGS )
1234
1223
die ("Unknown index entry format %08x" , extended_flags );
1235
- ce -> ce_flags |= extended_flags ;
1224
+ flags |= extended_flags ;
1236
1225
name = ondisk2 -> name ;
1237
1226
}
1238
1227
else
1239
1228
name = ondisk -> name ;
1240
1229
1241
1230
if (len == CE_NAMEMASK )
1242
1231
len = strlen (name );
1243
- /*
1244
- * NEEDSWORK: If the original index is crafted, this copy could
1245
- * go unchecked.
1246
- */
1247
- memcpy (ce -> name , name , len + 1 );
1248
- }
1249
1232
1250
- static inline size_t estimate_cache_size (size_t ondisk_size , unsigned int entries )
1251
- {
1252
- long per_entry ;
1233
+ ce = xmalloc (cache_entry_size (len ));
1253
1234
1254
- per_entry = sizeof (struct cache_entry ) - sizeof (struct ondisk_cache_entry );
1235
+ ce -> ce_ctime .sec = ntohl (ondisk -> ctime .sec );
1236
+ ce -> ce_mtime .sec = ntohl (ondisk -> mtime .sec );
1237
+ ce -> ce_ctime .nsec = ntohl (ondisk -> ctime .nsec );
1238
+ ce -> ce_mtime .nsec = ntohl (ondisk -> mtime .nsec );
1239
+ ce -> ce_dev = ntohl (ondisk -> dev );
1240
+ ce -> ce_ino = ntohl (ondisk -> ino );
1241
+ ce -> ce_mode = ntohl (ondisk -> mode );
1242
+ ce -> ce_uid = ntohl (ondisk -> uid );
1243
+ ce -> ce_gid = ntohl (ondisk -> gid );
1244
+ ce -> ce_size = ntohl (ondisk -> size );
1245
+ ce -> ce_flags = flags ;
1255
1246
1256
- /*
1257
- * Alignment can cause differences. This should be "alignof", but
1258
- * since that's a gcc'ism, just use the size of a pointer.
1259
- */
1260
- per_entry += sizeof (void * );
1261
- return ondisk_size + entries * per_entry ;
1247
+ hashcpy (ce -> sha1 , ondisk -> sha1 );
1248
+
1249
+ memcpy (ce -> name , name , len );
1250
+ ce -> name [len ] = '\0' ;
1251
+ return ce ;
1262
1252
}
1263
1253
1264
1254
/* remember to discard_cache() before reading a different cache! */
1265
1255
int read_index_from (struct index_state * istate , const char * path )
1266
1256
{
1267
1257
int fd , i ;
1268
1258
struct stat st ;
1269
- unsigned long src_offset , dst_offset ;
1259
+ unsigned long src_offset ;
1270
1260
struct cache_header * hdr ;
1271
1261
void * mmap ;
1272
1262
size_t mmap_size ;
@@ -1305,29 +1295,18 @@ int read_index_from(struct index_state *istate, const char *path)
1305
1295
istate -> cache_nr = ntohl (hdr -> hdr_entries );
1306
1296
istate -> cache_alloc = alloc_nr (istate -> cache_nr );
1307
1297
istate -> cache = xcalloc (istate -> cache_alloc , sizeof (struct cache_entry * ));
1308
-
1309
- /*
1310
- * The disk format is actually larger than the in-memory format,
1311
- * due to space for nsec etc, so even though the in-memory one
1312
- * has room for a few more flags, we can allocate using the same
1313
- * index size
1314
- */
1315
- istate -> alloc = xmalloc (estimate_cache_size (mmap_size , istate -> cache_nr ));
1316
1298
istate -> initialized = 1 ;
1317
1299
1318
1300
src_offset = sizeof (* hdr );
1319
- dst_offset = 0 ;
1320
1301
for (i = 0 ; i < istate -> cache_nr ; i ++ ) {
1321
1302
struct ondisk_cache_entry * disk_ce ;
1322
1303
struct cache_entry * ce ;
1323
1304
1324
1305
disk_ce = (struct ondisk_cache_entry * )((char * )mmap + src_offset );
1325
- ce = (struct cache_entry * )((char * )istate -> alloc + dst_offset );
1326
- convert_from_disk (disk_ce , ce );
1306
+ ce = create_from_disk (disk_ce );
1327
1307
set_index_entry (istate , i , ce );
1328
1308
1329
1309
src_offset += ondisk_ce_size (ce );
1330
- dst_offset += ce_size (ce );
1331
1310
}
1332
1311
istate -> timestamp .sec = st .st_mtime ;
1333
1312
istate -> timestamp .nsec = ST_MTIME_NSEC (st );
@@ -1361,11 +1340,15 @@ int read_index_from(struct index_state *istate, const char *path)
1361
1340
1362
1341
int is_index_unborn (struct index_state * istate )
1363
1342
{
1364
- return (!istate -> cache_nr && !istate -> alloc && ! istate -> timestamp .sec );
1343
+ return (!istate -> cache_nr && !istate -> timestamp .sec );
1365
1344
}
1366
1345
1367
1346
int discard_index (struct index_state * istate )
1368
1347
{
1348
+ int i ;
1349
+
1350
+ for (i = 0 ; i < istate -> cache_nr ; i ++ )
1351
+ free (istate -> cache [i ]);
1369
1352
resolve_undo_clear_index (istate );
1370
1353
istate -> cache_nr = 0 ;
1371
1354
istate -> cache_changed = 0 ;
@@ -1374,8 +1357,6 @@ int discard_index(struct index_state *istate)
1374
1357
istate -> name_hash_initialized = 0 ;
1375
1358
free_hash (& istate -> name_hash );
1376
1359
cache_tree_free (& (istate -> cache_tree ));
1377
- free (istate -> alloc );
1378
- istate -> alloc = NULL ;
1379
1360
istate -> initialized = 0 ;
1380
1361
1381
1362
/* no need to throw away allocated active_cache */
0 commit comments