diff options
-rw-r--r-- | apps/database.c | 88 |
1 files changed, 57 insertions, 31 deletions
diff --git a/apps/database.c b/apps/database.c index f4d6e47fba..a69385dd03 100644 --- a/apps/database.c +++ b/apps/database.c | |||
@@ -5,7 +5,7 @@ | |||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
7 | * \/ \/ \/ \/ \/ | 7 | * \/ \/ \/ \/ \/ |
8 | * $Id | 8 | * $Id$ |
9 | * | 9 | * |
10 | * Copyright (C) 2005 by Michiel van der Kolk | 10 | * Copyright (C) 2005 by Michiel van der Kolk |
11 | * | 11 | * |
@@ -98,7 +98,8 @@ int tagdb_init(void) | |||
98 | #endif | 98 | #endif |
99 | if ( (tagdbheader.version&0xFF) != TAGDB_VERSION) | 99 | if ( (tagdbheader.version&0xFF) != TAGDB_VERSION) |
100 | { | 100 | { |
101 | splash(HZ,true,"Unsupported database version %d!", tagdbheader.version&0xFF); | 101 | splash(HZ,true,"Unsupported database version %d!", |
102 | tagdbheader.version&0xFF); | ||
102 | return -1; | 103 | return -1; |
103 | } | 104 | } |
104 | 105 | ||
@@ -125,20 +126,23 @@ void tagdb_shutdown(void) | |||
125 | 126 | ||
126 | /*** TagDatabase code ***/ | 127 | /*** TagDatabase code ***/ |
127 | 128 | ||
128 | void writetagdbheader(void) { | 129 | void writetagdbheader(void) |
130 | { | ||
129 | lseek(tagdb_fd,0,SEEK_SET); | 131 | lseek(tagdb_fd,0,SEEK_SET); |
130 | write(tagdb_fd, &tagdbheader, 68); | 132 | write(tagdb_fd, &tagdbheader, 68); |
131 | fsync(tagdb_fd); | 133 | fsync(tagdb_fd); |
132 | } | 134 | } |
133 | 135 | ||
134 | void writefentry(void) { | 136 | void writefentry(void) |
137 | { | ||
135 | lseek(tagdb_fd,currentfeoffset,SEEK_SET); | 138 | lseek(tagdb_fd,currentfeoffset,SEEK_SET); |
136 | write(tagdb_fd,sbuf,tagdbheader.filelen); | 139 | write(tagdb_fd,sbuf,tagdbheader.filelen); |
137 | write(tagdb_fd,&fe.hash,12); | 140 | write(tagdb_fd,&fe.hash,12); |
138 | fsync(tagdb_fd); | 141 | fsync(tagdb_fd); |
139 | } | 142 | } |
140 | 143 | ||
141 | void getfentrybyoffset(int offset) { | 144 | void getfentrybyoffset(int offset) |
145 | { | ||
142 | memset(&fe,0,sizeof(struct file_entry)); | 146 | memset(&fe,0,sizeof(struct file_entry)); |
143 | lseek(tagdb_fd,offset,SEEK_SET); | 147 | lseek(tagdb_fd,offset,SEEK_SET); |
144 | read(tagdb_fd,sbuf,tagdbheader.filelen); | 148 | read(tagdb_fd,sbuf,tagdbheader.filelen); |
@@ -150,7 +154,8 @@ void getfentrybyoffset(int offset) { | |||
150 | 154 | ||
151 | #define getfentrybyrecord(_x_) getfentrybyoffset(FILERECORD2OFFSET(_x_)) | 155 | #define getfentrybyrecord(_x_) getfentrybyoffset(FILERECORD2OFFSET(_x_)) |
152 | 156 | ||
153 | int getfentrybyfilename(char *fname) { | 157 | int getfentrybyfilename(char *fname) |
158 | { | ||
154 | int min=0; | 159 | int min=0; |
155 | int max=tagdbheader.filecount; | 160 | int max=tagdbheader.filecount; |
156 | while(min<max) { | 161 | while(min<max) { |
@@ -168,7 +173,8 @@ int getfentrybyfilename(char *fname) { | |||
168 | return 0; | 173 | return 0; |
169 | } | 174 | } |
170 | 175 | ||
171 | int getfentrybyhash(int hash) { | 176 | int getfentrybyhash(int hash) |
177 | { | ||
172 | int min=0; | 178 | int min=0; |
173 | for(min=0;min<tagdbheader.filecount;min++) { | 179 | for(min=0;min<tagdbheader.filecount;min++) { |
174 | getfentrybyrecord(min); | 180 | getfentrybyrecord(min); |
@@ -178,12 +184,15 @@ int getfentrybyhash(int hash) { | |||
178 | return 0; | 184 | return 0; |
179 | } | 185 | } |
180 | 186 | ||
181 | int deletefentry(char *fname) { | 187 | int deletefentry(char *fname) |
188 | { | ||
182 | if(!getfentrybyfilename(fname)) | 189 | if(!getfentrybyfilename(fname)) |
183 | return 0; | 190 | return 0; |
184 | int restrecord = currentferecord+1; | 191 | int restrecord = currentferecord+1; |
185 | if(currentferecord!=tagdbheader.filecount) /* file is not last entry */ | 192 | if(currentferecord!=tagdbheader.filecount) /* file is not last entry */ |
186 | tagdb_shiftdown(FILERECORD2OFFSET(currentferecord),FILERECORD2OFFSET(restrecord),(tagdbheader.filecount-restrecord)*FILEENTRY_SIZE); | 193 | tagdb_shiftdown(FILERECORD2OFFSET(currentferecord), |
194 | FILERECORD2OFFSET(restrecord), | ||
195 | (tagdbheader.filecount-restrecord)*FILEENTRY_SIZE); | ||
187 | ftruncate(tagdb_fd,lseek(tagdb_fd,0,SEEK_END)-FILEENTRY_SIZE); | 196 | ftruncate(tagdb_fd,lseek(tagdb_fd,0,SEEK_END)-FILEENTRY_SIZE); |
188 | tagdbheader.filecount--; | 197 | tagdbheader.filecount--; |
189 | update_fentryoffsets(restrecord,tagdbheader.filecount); | 198 | update_fentryoffsets(restrecord,tagdbheader.filecount); |
@@ -191,7 +200,8 @@ int deletefentry(char *fname) { | |||
191 | return 1; | 200 | return 1; |
192 | } | 201 | } |
193 | 202 | ||
194 | void update_fentryoffsets(int start, int end) { | 203 | void update_fentryoffsets(int start, int end) |
204 | { | ||
195 | int i; | 205 | int i; |
196 | for(i=start;i<end;i++) { | 206 | for(i=start;i<end;i++) { |
197 | getfentrybyrecord(i); | 207 | getfentrybyrecord(i); |
@@ -206,12 +216,14 @@ void update_fentryoffsets(int start, int end) { | |||
206 | } | 216 | } |
207 | } | 217 | } |
208 | if(fe.rundbentry!=-1) { | 218 | if(fe.rundbentry!=-1) { |
209 | splash(HZ*2,true, "o.o.. found a rundbentry? o.o; didn't update it, update the code o.o;"); | 219 | splash(HZ*2,true, "o.o.. found a rundbentry? o.o; didn't update " |
220 | "it, update the code o.o;"); | ||
210 | } | 221 | } |
211 | } | 222 | } |
212 | } | 223 | } |
213 | 224 | ||
214 | int tagdb_shiftdown(int targetoffset, int startingoffset, int bytes) { | 225 | int tagdb_shiftdown(int targetoffset, int startingoffset, int bytes) |
226 | { | ||
215 | int amount; | 227 | int amount; |
216 | if(targetoffset>=startingoffset) { | 228 | if(targetoffset>=startingoffset) { |
217 | splash(HZ*2,true,"Woah. no beeping way. (tagdb_shiftdown)"); | 229 | splash(HZ*2,true,"Woah. no beeping way. (tagdb_shiftdown)"); |
@@ -225,7 +237,8 @@ int tagdb_shiftdown(int targetoffset, int startingoffset, int bytes) { | |||
225 | written=write(tagdb_fd,sbuf,amount); | 237 | written=write(tagdb_fd,sbuf,amount); |
226 | targetoffset+=written; | 238 | targetoffset+=written; |
227 | if(amount!=written) { | 239 | if(amount!=written) { |
228 | splash(HZ*2,true,"Something went very wrong. expect database corruption. (tagdb_shiftdown)"); | 240 | splash(HZ*2,true,"Something went very wrong. expect database " |
241 | "corruption. (tagdb_shiftdown)"); | ||
229 | return 0; | 242 | return 0; |
230 | } | 243 | } |
231 | lseek(tagdb_fd,startingoffset,SEEK_SET); | 244 | lseek(tagdb_fd,startingoffset,SEEK_SET); |
@@ -234,7 +247,8 @@ int tagdb_shiftdown(int targetoffset, int startingoffset, int bytes) { | |||
234 | return 1; | 247 | return 1; |
235 | } | 248 | } |
236 | 249 | ||
237 | int tagdb_shiftup(int targetoffset, int startingoffset, int bytes) { | 250 | int tagdb_shiftup(int targetoffset, int startingoffset, int bytes) |
251 | { | ||
238 | int amount,amount2; | 252 | int amount,amount2; |
239 | int readpos,writepos,filelen; | 253 | int readpos,writepos,filelen; |
240 | if(targetoffset<=startingoffset) { | 254 | if(targetoffset<=startingoffset) { |
@@ -250,13 +264,15 @@ int tagdb_shiftup(int targetoffset, int startingoffset, int bytes) { | |||
250 | lseek(tagdb_fd,readpos,SEEK_SET); | 264 | lseek(tagdb_fd,readpos,SEEK_SET); |
251 | amount2=read(tagdb_fd,sbuf,amount); | 265 | amount2=read(tagdb_fd,sbuf,amount); |
252 | if(amount2!=amount) { | 266 | if(amount2!=amount) { |
253 | splash(HZ*2,true,"Something went very wrong. expect database corruption. (tagdb_shiftup)"); | 267 | splash(HZ*2,true,"Something went very wrong. expect database " |
268 | "corruption. (tagdb_shiftup)"); | ||
254 | return 0; | 269 | return 0; |
255 | } | 270 | } |
256 | lseek(tagdb_fd,writepos,SEEK_SET); | 271 | lseek(tagdb_fd,writepos,SEEK_SET); |
257 | amount=write(tagdb_fd,sbuf,amount2); | 272 | amount=write(tagdb_fd,sbuf,amount2); |
258 | if(amount2!=amount) { | 273 | if(amount2!=amount) { |
259 | splash(HZ*2,true,"Something went very wrong. expect database corruption. (tagdb_shiftup)"); | 274 | splash(HZ*2,true,"Something went very wrong. expect database " |
275 | "corruption. (tagdb_shiftup)"); | ||
260 | return 0; | 276 | return 0; |
261 | } | 277 | } |
262 | bytes-=amount; | 278 | bytes-=amount; |
@@ -280,7 +296,8 @@ static struct rundb_entry rundbentry; | |||
280 | 296 | ||
281 | /*** RuntimeDatabase code ***/ | 297 | /*** RuntimeDatabase code ***/ |
282 | 298 | ||
283 | void rundb_track_changed(struct track_info *ti) { | 299 | void rundb_track_changed(struct track_info *ti) |
300 | { | ||
284 | increaseplaycount(); | 301 | increaseplaycount(); |
285 | logf("rundb new track: %s", ti->id3.path); | 302 | logf("rundb new track: %s", ti->id3.path); |
286 | loadruntimeinfo(ti->id3.path); | 303 | loadruntimeinfo(ti->id3.path); |
@@ -327,7 +344,8 @@ int rundb_init(void) | |||
327 | #endif | 344 | #endif |
328 | if ( (rundbheader.version&0xFF) != RUNDB_VERSION) | 345 | if ( (rundbheader.version&0xFF) != RUNDB_VERSION) |
329 | { | 346 | { |
330 | splash(HZ,true,"Unsupported runtime database version %d!", rundbheader.version&0xFF); | 347 | splash(HZ,true,"Unsupported runtime database version %d!", |
348 | rundbheader.version&0xFF); | ||
331 | return -1; | 349 | return -1; |
332 | } | 350 | } |
333 | 351 | ||
@@ -339,7 +357,8 @@ int rundb_init(void) | |||
339 | #endif | 357 | #endif |
340 | } | 358 | } |
341 | 359 | ||
342 | void writerundbheader(void) { | 360 | void writerundbheader(void) |
361 | { | ||
343 | lseek(rundb_fd,0,SEEK_SET); | 362 | lseek(rundb_fd,0,SEEK_SET); |
344 | write(rundb_fd, &rundbheader, 8); | 363 | write(rundb_fd, &rundbheader, 8); |
345 | fsync(rundb_fd); | 364 | fsync(rundb_fd); |
@@ -361,7 +380,8 @@ void getrundbentrybyoffset(int offset) { | |||
361 | #endif | 380 | #endif |
362 | } | 381 | } |
363 | 382 | ||
364 | int getrundbentrybyhash(int hash) { | 383 | int getrundbentrybyhash(int hash) |
384 | { | ||
365 | int min=0; | 385 | int min=0; |
366 | for(min=0;min<rundbheader.entrycount;min++) { | 386 | for(min=0;min<rundbheader.entrycount;min++) { |
367 | getrundbentrybyrecord(min); | 387 | getrundbentrybyrecord(min); |
@@ -372,15 +392,17 @@ int getrundbentrybyhash(int hash) { | |||
372 | return 0; | 392 | return 0; |
373 | } | 393 | } |
374 | 394 | ||
375 | void writerundbentry(void) { | 395 | void writerundbentry(void) |
376 | if(rundbentry.hash==0) // 0 = invalid rundb info. | 396 | { |
397 | if(rundbentry.hash==0) /* 0 = invalid rundb info. */ | ||
377 | return; | 398 | return; |
378 | lseek(rundb_fd,currentreoffset,SEEK_SET); | 399 | lseek(rundb_fd,currentreoffset,SEEK_SET); |
379 | write(rundb_fd,&rundbentry,20); | 400 | write(rundb_fd,&rundbentry,20); |
380 | fsync(rundb_fd); | 401 | fsync(rundb_fd); |
381 | } | 402 | } |
382 | 403 | ||
383 | void loadruntimeinfo(char *filename) { | 404 | void loadruntimeinfo(char *filename) |
405 | { | ||
384 | memset(&rundbentry,0,sizeof(struct rundb_entry)); | 406 | memset(&rundbentry,0,sizeof(struct rundb_entry)); |
385 | valid_file=0; | 407 | valid_file=0; |
386 | if(!getfentrybyfilename(filename)) | 408 | if(!getfentrybyfilename(filename)) |
@@ -390,16 +412,18 @@ void loadruntimeinfo(char *filename) { | |||
390 | logf("load rundbentry: 0x%x",fe.rundbentry); | 412 | logf("load rundbentry: 0x%x",fe.rundbentry); |
391 | getrundbentrybyoffset(fe.rundbentry); | 413 | getrundbentrybyoffset(fe.rundbentry); |
392 | if(fe.hash!=rundbentry.hash) { | 414 | if(fe.hash!=rundbentry.hash) { |
393 | logf("Rundb: Hash mismatch. trying to repair entry.",fe.hash,rundbentry.hash); | 415 | logf("Rundb: Hash mismatch. trying to repair entry.", |
416 | fe.hash,rundbentry.hash); | ||
394 | addrundbentry(); | 417 | addrundbentry(); |
395 | } | 418 | } |
396 | } | 419 | } |
397 | else // add new rundb entry. | 420 | else /* add new rundb entry. */ |
398 | addrundbentry(); | 421 | addrundbentry(); |
399 | } | 422 | } |
400 | 423 | ||
401 | void addrundbentry() { | 424 | void addrundbentry() |
402 | // first search for an entry with an equal hash. | 425 | { |
426 | /* first search for an entry with an equal hash. */ | ||
403 | if(getrundbentrybyhash(fe.hash)) { | 427 | if(getrundbentrybyhash(fe.hash)) { |
404 | logf("Found existing rundb entry: 0x%x",currentreoffset); | 428 | logf("Found existing rundb entry: 0x%x",currentreoffset); |
405 | fe.rundbentry=currentreoffset; | 429 | fe.rundbentry=currentreoffset; |
@@ -417,15 +441,17 @@ void addrundbentry() { | |||
417 | rundbsize=lseek(rundb_fd,0,SEEK_END); | 441 | rundbsize=lseek(rundb_fd,0,SEEK_END); |
418 | } | 442 | } |
419 | 443 | ||
420 | void increaseplaycount(void) { | 444 | void increaseplaycount(void) |
421 | if(rundbentry.hash==0) // 0 = invalid rundb info. | 445 | { |
446 | if(rundbentry.hash==0) /* 0 = invalid rundb info. */ | ||
422 | return; | 447 | return; |
423 | rundbentry.playcount++; | 448 | rundbentry.playcount++; |
424 | writerundbentry(); | 449 | writerundbentry(); |
425 | } | 450 | } |
426 | 451 | ||
427 | void setrating(int rating) { | 452 | void setrating(int rating) |
428 | if(rundbentry.hash==0) // 0 = invalid rundb info. | 453 | { |
454 | if(rundbentry.hash==0) /* 0 = invalid rundb info. */ | ||
429 | return; | 455 | return; |
430 | rundbentry.rating=rating; | 456 | rundbentry.rating=rating; |
431 | writerundbentry(); | 457 | writerundbentry(); |