From 337e922685d11f80fa9e7b164511e043044b6302 Mon Sep 17 00:00:00 2001 From: Miika Pekkarinen Date: Fri, 1 Jul 2011 13:48:28 +0000 Subject: [PATCH] Allocate indices directly in ramcache header. Add version code to tagcache state dump header. Debug: hdr-structure corruption might happen after rebuilding of dircache. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30109 a1c6a512-1295-4272-9138-f99709370657 --- apps/tagcache.c | 13 +++++++------ apps/tagcache.h | 3 +++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/apps/tagcache.c b/apps/tagcache.c index f2423243b7..089bba13aa 100644 --- a/apps/tagcache.c +++ b/apps/tagcache.c @@ -206,15 +206,16 @@ static struct master_header current_tcmh; #ifdef HAVE_TC_RAMCACHE /* Header is created when loading database to ram. */ struct ramcache_header { - struct index_entry *indices; /* Master index file content */ char *tags[TAG_COUNT]; /* Tag file content (not including filename tag) */ int entry_count[TAG_COUNT]; /* Number of entries in the indices. */ + struct index_entry indices[0]; /* Master index file content */ }; # ifdef HAVE_EEPROM_SETTINGS struct statefile_header { + int32_t magic; /* Statefile version number */ struct master_header mh; /* Header from the master index */ - struct ramcache_header *hdr; + struct ramcache_header *hdr; /* Old load address of hdr for relocation */ struct tagcache_stat tc_stat; }; # endif @@ -3793,7 +3794,6 @@ static bool allocate_tagcache(void) hdr = buffer_alloc(tc_stat.ramcache_allocated + 128); memset(hdr, 0, sizeof(struct ramcache_header)); memcpy(¤t_tcmh, &tcmh, sizeof current_tcmh); - hdr->indices = (struct index_entry *)(hdr + 1); logf("tagcache: %d bytes allocated.", tc_stat.ramcache_allocated); return true; @@ -3818,6 +3818,7 @@ static bool tagcache_dumpload(void) hdr = buffer_alloc(0); rc = read(fd, &shdr, sizeof(struct statefile_header)); if (rc != sizeof(struct statefile_header) + || shdr.magic != TAGCACHE_STATEFILE_MAGIC || shdr.mh.tch.magic != TAGCACHE_MAGIC) { logf("incorrect statefile"); @@ -3843,7 +3844,6 @@ static bool tagcache_dumpload(void) memcpy(&tc_stat, &shdr.tc_stat, sizeof(struct tagcache_stat)); /* Now fix the pointers */ - hdr->indices = (struct index_entry *)((long)hdr->indices + offpos); for (i = 0; i < TAG_COUNT; i++) hdr->tags[i] += offpos; @@ -3869,6 +3869,7 @@ static bool tagcache_dumpsave(void) } /* Create the header */ + shdr.magic = TAGCACHE_STATEFILE_MAGIC; shdr.hdr = hdr; memcpy(&shdr.mh, ¤t_tcmh, sizeof current_tcmh); memcpy(&shdr.tc_stat, &tc_stat, sizeof tc_stat); @@ -3924,6 +3925,8 @@ static bool load_tagcache(void) /* Load the master index table. */ for (i = 0; i < tcmh.tch.entry_count; i++) { + /* DEBUG: After tagcache commit and dircache rebuild, hdr-sturcture + * may become corrupt. */ rc = ecread_index_entry(fd, idx); if (rc != sizeof(struct index_entry)) { @@ -3997,8 +4000,6 @@ static bool load_tagcache(void) int dc; # endif - // FIXME: This is wrong! - // idx = &hdr->indices[hdr->entry_count[i]]; idx = &hdr->indices[fe->idx_id]; if (fe->tag_length >= (long)sizeof(buf)-1) diff --git a/apps/tagcache.h b/apps/tagcache.h index 6f23bfcb0d..1dad012ebe 100644 --- a/apps/tagcache.h +++ b/apps/tagcache.h @@ -52,6 +52,9 @@ enum tag_type { tag_artist = 0, tag_album, tag_genre, tag_title, /* Tag Cache Header version 'TCHxx'. Increment when changing internal structures. */ #define TAGCACHE_MAGIC 0x5443480e +/* Dump store/restore header version 'TCSxx'. */ +#define TAGCACHE_STATEFILE_MAGIC 0x54435301 + /* How much to allocate extra space for ramcache. */ #define TAGCACHE_RESERVE 32768