@@ -1224,71 +1224,61 @@ int read_index(struct index_state *istate)
1224
1224
return read_index_from (istate , get_index_file ());
1225
1225
}
1226
1226
1227
- static void convert_from_disk ( struct ondisk_cache_entry * ondisk , struct cache_entry * ce )
1227
+ static struct cache_entry * create_from_disk ( struct ondisk_cache_entry * ondisk )
1228
1228
{
1229
+ struct cache_entry * ce ;
1229
1230
size_t len ;
1230
1231
const char * name ;
1232
+ unsigned int flags ;
1231
1233
1232
- ce -> ce_ctime .sec = ntohl (ondisk -> ctime .sec );
1233
- ce -> ce_mtime .sec = ntohl (ondisk -> mtime .sec );
1234
- ce -> ce_ctime .nsec = ntohl (ondisk -> ctime .nsec );
1235
- ce -> ce_mtime .nsec = ntohl (ondisk -> mtime .nsec );
1236
- ce -> ce_dev = ntohl (ondisk -> dev );
1237
- ce -> ce_ino = ntohl (ondisk -> ino );
1238
- ce -> ce_mode = ntohl (ondisk -> mode );
1239
- ce -> ce_uid = ntohl (ondisk -> uid );
1240
- ce -> ce_gid = ntohl (ondisk -> gid );
1241
- ce -> ce_size = ntohl (ondisk -> size );
1242
1234
/* On-disk flags are just 16 bits */
1243
- ce -> ce_flags = ntohs (ondisk -> flags );
1244
-
1245
- hashcpy (ce -> sha1 , ondisk -> sha1 );
1235
+ flags = ntohs (ondisk -> flags );
1236
+ len = flags & CE_NAMEMASK ;
1246
1237
1247
- len = ce -> ce_flags & CE_NAMEMASK ;
1248
-
1249
- if (ce -> ce_flags & CE_EXTENDED ) {
1238
+ if (flags & CE_EXTENDED ) {
1250
1239
struct ondisk_cache_entry_extended * ondisk2 ;
1251
1240
int extended_flags ;
1252
1241
ondisk2 = (struct ondisk_cache_entry_extended * )ondisk ;
1253
1242
extended_flags = ntohs (ondisk2 -> flags2 ) << 16 ;
1254
1243
/* We do not yet understand any bit out of CE_EXTENDED_FLAGS */
1255
1244
if (extended_flags & ~CE_EXTENDED_FLAGS )
1256
1245
die ("Unknown index entry format %08x" , extended_flags );
1257
- ce -> ce_flags |= extended_flags ;
1246
+ flags |= extended_flags ;
1258
1247
name = ondisk2 -> name ;
1259
1248
}
1260
1249
else
1261
1250
name = ondisk -> name ;
1262
1251
1263
1252
if (len == CE_NAMEMASK )
1264
1253
len = strlen (name );
1265
- /*
1266
- * NEEDSWORK: If the original index is crafted, this copy could
1267
- * go unchecked.
1268
- */
1269
- memcpy (ce -> name , name , len + 1 );
1270
- }
1271
1254
1272
- static inline size_t estimate_cache_size (size_t ondisk_size , unsigned int entries )
1273
- {
1274
- size_t fix_size_mem = offsetof(struct cache_entry , name );
1275
- size_t fix_size_dsk = offsetof(struct ondisk_cache_entry , name );
1276
- long per_entry = (fix_size_mem - fix_size_dsk + 7 ) & ~7 ;
1255
+ ce = xmalloc (cache_entry_size (len ));
1277
1256
1278
- /*
1279
- * Alignment can cause differences. This should be "alignof", but
1280
- * since that's a gcc'ism, just use the size of a pointer.
1281
- */
1282
- per_entry += sizeof (void * );
1283
- return ondisk_size + entries * per_entry ;
1257
+ ce -> ce_ctime .sec = ntohl (ondisk -> ctime .sec );
1258
+ ce -> ce_mtime .sec = ntohl (ondisk -> mtime .sec );
1259
+ ce -> ce_ctime .nsec = ntohl (ondisk -> ctime .nsec );
1260
+ ce -> ce_mtime .nsec = ntohl (ondisk -> mtime .nsec );
1261
+ ce -> ce_dev = ntohl (ondisk -> dev );
1262
+ ce -> ce_ino = ntohl (ondisk -> ino );
1263
+ ce -> ce_mode = ntohl (ondisk -> mode );
1264
+ ce -> ce_uid = ntohl (ondisk -> uid );
1265
+ ce -> ce_gid = ntohl (ondisk -> gid );
1266
+ ce -> ce_size = ntohl (ondisk -> size );
1267
+ ce -> ce_flags = flags ;
1268
+
1269
+ hashcpy (ce -> sha1 , ondisk -> sha1 );
1270
+
1271
+ memcpy (ce -> name , name , len );
1272
+ ce -> name [len ] = '\0' ;
1273
+ return ce ;
1284
1274
}
1285
1275
1286
1276
/* remember to discard_cache() before reading a different cache! */
1287
1277
int read_index_from (struct index_state * istate , const char * path )
1288
1278
{
1289
1279
int fd , i ;
1290
1280
struct stat st ;
1291
- unsigned long src_offset , dst_offset ;
1281
+ unsigned long src_offset ;
1292
1282
struct cache_header * hdr ;
1293
1283
void * mmap ;
1294
1284
size_t mmap_size ;
@@ -1327,29 +1317,18 @@ int read_index_from(struct index_state *istate, const char *path)
1327
1317
istate -> cache_nr = ntohl (hdr -> hdr_entries );
1328
1318
istate -> cache_alloc = alloc_nr (istate -> cache_nr );
1329
1319
istate -> cache = xcalloc (istate -> cache_alloc , sizeof (struct cache_entry * ));
1330
-
1331
- /*
1332
- * The disk format is actually larger than the in-memory format,
1333
- * due to space for nsec etc, so even though the in-memory one
1334
- * has room for a few more flags, we can allocate using the same
1335
- * index size
1336
- */
1337
- istate -> alloc = xmalloc (estimate_cache_size (mmap_size , istate -> cache_nr ));
1338
1320
istate -> initialized = 1 ;
1339
1321
1340
1322
src_offset = sizeof (* hdr );
1341
- dst_offset = 0 ;
1342
1323
for (i = 0 ; i < istate -> cache_nr ; i ++ ) {
1343
1324
struct ondisk_cache_entry * disk_ce ;
1344
1325
struct cache_entry * ce ;
1345
1326
1346
1327
disk_ce = (struct ondisk_cache_entry * )((char * )mmap + src_offset );
1347
- ce = (struct cache_entry * )((char * )istate -> alloc + dst_offset );
1348
- convert_from_disk (disk_ce , ce );
1328
+ ce = create_from_disk (disk_ce );
1349
1329
set_index_entry (istate , i , ce );
1350
1330
1351
1331
src_offset += ondisk_ce_size (ce );
1352
- dst_offset += ce_size (ce );
1353
1332
}
1354
1333
istate -> timestamp .sec = st .st_mtime ;
1355
1334
istate -> timestamp .nsec = ST_MTIME_NSEC (st );
@@ -1383,11 +1362,15 @@ int read_index_from(struct index_state *istate, const char *path)
1383
1362
1384
1363
int is_index_unborn (struct index_state * istate )
1385
1364
{
1386
- return (!istate -> cache_nr && !istate -> alloc && ! istate -> timestamp .sec );
1365
+ return (!istate -> cache_nr && !istate -> timestamp .sec );
1387
1366
}
1388
1367
1389
1368
int discard_index (struct index_state * istate )
1390
1369
{
1370
+ int i ;
1371
+
1372
+ for (i = 0 ; i < istate -> cache_nr ; i ++ )
1373
+ free (istate -> cache [i ]);
1391
1374
resolve_undo_clear_index (istate );
1392
1375
istate -> cache_nr = 0 ;
1393
1376
istate -> cache_changed = 0 ;
@@ -1396,8 +1379,6 @@ int discard_index(struct index_state *istate)
1396
1379
istate -> name_hash_initialized = 0 ;
1397
1380
free_hash (& istate -> name_hash );
1398
1381
cache_tree_free (& (istate -> cache_tree ));
1399
- free (istate -> alloc );
1400
- istate -> alloc = NULL ;
1401
1382
istate -> initialized = 0 ;
1402
1383
1403
1384
/* no need to throw away allocated active_cache */
0 commit comments