1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
Index: firmware/common/dircache.c
===================================================================
--- firmware/common/dircache.c	(revision 30102)
+++ firmware/common/dircache.c	(working copy)
@@ -1,3 +1,7 @@
+/* 00:14 < pamaury> 1) It should put in the queue the bindings present before the rebuild
+ * 00:14 < pamaury> 2) It doesn't save filetime/filesize changes during the rebuild
+ */
+
 /***************************************************************************
  *             __________               __   ___.
  *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
@@ -23,6 +27,8 @@
    - Allow cache live updating while transparent rebuild is running.
 */
 
+#define LOGF_ENABLE
+
 #include "config.h"
 
 #include <stdio.h>
@@ -58,10 +64,13 @@
 #define MAX_OPEN_DIRS 8
 #endif
 
-#define MAX_PENDING_BINDINGS 2
+#define MAX_PENDING_BINDINGS 3
 struct fdbind_queue {
     char path[MAX_PATH];
     int fd;
+    /* To be used if fd has been closed. */
+    long filesize;
+    long startcluster;
 };
 
 /* Exported structures. */
@@ -828,11 +837,17 @@
     /* Background build, dircache has been previously allocated */
     if (dircache_size > 0)
     {
+        d_names_start = d_names_end;
+        dircache_size = 0;
+        reserve_used = 0;
         thread_enabled = true;
         dircache_initializing = true;
+        generate_dot_d_names();
+        
         queue_post(&dircache_queue, DIRCACHE_BUILD, 0);
         return 2;
     }
+    
     if (last_size > DIRCACHE_RESERVE && last_size < DIRCACHE_LIMIT )
     {
         allocated_size = last_size + DIRCACHE_RESERVE;
Index: apps/tagcache.c
===================================================================
--- apps/tagcache.c	(revision 30102)
+++ apps/tagcache.c	(working copy)
@@ -55,7 +55,7 @@
  * 
  */
 
-/*#define LOGF_ENABLE*/
+#define LOGF_ENABLE
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -94,6 +94,9 @@
 #define do_timed_yield() do { } while(0)
 #endif
 
+#define logfl() logf("L:%d", __LINE__);
+//#define logfl() logf("L:%d", __LINE__); sleep(HZ*2)
+
 #ifndef __PCTOOL__
 /* Tag Cache thread. */
 static struct event_queue tagcache_queue SHAREDBSS_ATTR;
@@ -206,15 +209,16 @@
 #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 +3797,6 @@
     hdr = buffer_alloc(tc_stat.ramcache_allocated + 128);
     memset(hdr, 0, sizeof(struct ramcache_header));
     memcpy(&current_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 +3821,7 @@
     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 +3847,6 @@
     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 +3872,7 @@
     }
     
     /* Create the header */
+    shdr.magic = TAGCACHE_STATEFILE_MAGIC;
     shdr.hdr = hdr;
     memcpy(&shdr.mh, &current_tcmh, sizeof current_tcmh);
     memcpy(&shdr.tc_stat, &tc_stat, sizeof tc_stat);
@@ -3917,14 +3921,18 @@
     }
 
     /* Master header copy should already match, this can be redundant to do. */
+    logfl();
     memcpy(&current_tcmh, &tcmh, sizeof current_tcmh);
+    logfl();
     
     idx = hdr->indices;
+    logfl();
 
     /* Load the master index table. */
     for (i = 0; i < tcmh.tch.entry_count; i++)
     {
-        rc = ecread_index_entry(fd, idx);
+    logfl();
+        rc = ecread_index_entry(fd, idx); // CRASH
         if (rc != sizeof(struct index_entry))
         {
             logf("read error #10");
@@ -3932,6 +3940,7 @@
             return false;
         }
     
+    logfl();
         bytesleft -= sizeof(struct index_entry);
         if (bytesleft < 0 || ((long)idx - (long)hdr->indices) >= tc_stat.ramcache_allocated)
         {
@@ -3943,6 +3952,7 @@
         idx++;
     }
 
+    logfl();
     close(fd);
 
     /* Load the tags. */
@@ -3959,13 +3969,16 @@
         p = (char *)((long)p & ~0x03) + 0x04;
         hdr->tags[tag] = p;
 
+    logfl();
         /* Check the header. */
         tch = (struct tagcache_header *)p;
         p += sizeof(struct tagcache_header);
         
+    logfl();
         if ( (fd = open_tag_fd(tch, tag, false)) < 0)
             return false;
         
+    logfl();
         for (hdr->entry_count[tag] = 0;
              hdr->entry_count[tag] < tch->entry_count;
              hdr->entry_count[tag]++)
@@ -3990,6 +4003,7 @@
                 return false;
             }
 
+    logfl();
             /* We have a special handling for the filename tags. */
             if (tag == tag_filename)
             {
@@ -4001,6 +4015,7 @@
                 // idx = &hdr->indices[hdr->entry_count[i]];
                 idx = &hdr->indices[fe->idx_id];
                 
+    logfl();
                 if (fe->tag_length >= (long)sizeof(buf)-1)
                 {
                     read(fd, buf, 10);
@@ -4011,6 +4026,7 @@
                     return false;
                 }
                 
+    logfl();
                 rc = read(fd, buf, fe->tag_length);
                 if (rc != fe->tag_length)
                 {
@@ -4031,6 +4047,7 @@
                     return false;
                 }
                 
+    logfl();
                 if (idx->tag_seek[tag] != pos)
                 {
                     logf("corrupt data structures!");
@@ -4041,7 +4058,9 @@
 # ifdef HAVE_DIRCACHE
                 if (dircache_is_enabled())
                 {
+    logfl();
                     dc = dircache_get_entry_id(buf);
+    logfl();
                     if (dc < 0)
                     {
                         logf("Entry no longer valid.");
@@ -4057,6 +4076,7 @@
                 else
 # endif
                 {
+    logfl();
                     /* This will be very slow unless dircache is enabled
                        or target is flash based, but do it anyway for
                        consistency. */
@@ -4076,6 +4096,7 @@
                 continue ;
             }
             
+    logfl();
             bytesleft -= sizeof(struct tagfile_entry) + fe->tag_length;
             if (bytesleft < 0)
             {
@@ -4086,6 +4107,7 @@
                 return false;
             }
 
+    logfl();
             p = fe->tag_data;
             rc = read(fd, fe->tag_data, fe->tag_length);
             p += rc;
Index: apps/tagcache.h
===================================================================
--- apps/tagcache.h	(revision 30102)
+++ apps/tagcache.h	(working copy)
@@ -52,6 +52,9 @@
 /* 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