summaryrefslogtreecommitdiffstats
path: root/apps/database.c
blob: 97fb2d70cc0d1ca33e1309a006e586733ec717c8 (plain)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 by Michiel van der Kolk
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include "file.h"
#include "screens.h"
#include "kernel.h"
#include "tree.h"
#include "lcd.h"
#include "font.h"
#include "settings.h"
#include "icons.h"
#include "status.h"
#include "debug.h"
#include "button.h"
#include "menu.h"
#include "main_menu.h"
#include "mpeg.h"
#include "misc.h"
#include "ata.h"
#include "filetypes.h"
#include "applimits.h"
#include "icons.h"
#include "lang.h"
#include "keyboard.h"
#include "database.h"
#include "autoconf.h"
#include "splash.h"

#if CONFIG_CODEC == SWCODEC
#include "playback.h"
#else
#include "mpeg.h"
#endif

#include "logf.h"

/* internal functions */
void writetagdbheader(void);
void writefentry(struct mp3entry *id);
void getfentrybyoffset(struct mp3entry *id,int offset);
void update_fentryoffsets(int start, int end);
void writerundbheader(void);
void getrundbentrybyoffset(struct mp3entry *id,int offset);
void writerundbentry(struct mp3entry *id);
int getfentrybyfilename(struct mp3entry *id);
void clearfileentryinfo(struct mp3entry *id);
void clearruntimeinfo(struct mp3entry *id);
int findrundbentry(struct mp3entry *id);

int getfentrybyhash(int hash);
int deletefentry(char *fname);
int tagdb_shiftdown(int targetoffset, int startingoffset, int bytes);
int tagdb_shiftup(int targetoffset, int startingoffset, int bytes);
                    
static char sbuf[MAX_PATH];

int tagdb_fd = -1;
int tagdb_initialized = 0;
struct tagdb_header tagdbheader;

int tagdb_init(void)
{
    unsigned char* ptr = (char*)&tagdbheader.version;
#ifdef ROCKBOX_LITTLE_ENDIAN
    int i, *p;
#endif

    tagdb_fd = open(ROCKBOX_DIR "/rockbox.tagdb", O_RDWR);
    if (tagdb_fd < 0) {
        DEBUGF("Failed opening database\n");
        return -1;
    }
    read(tagdb_fd, &tagdbheader, 68);

    if (ptr[0] != 'R' ||
        ptr[1] != 'D' ||
        ptr[2] != 'B')
    {
        gui_syncsplash(HZ,true,"Not a rockbox ID3 database!");
        return -1;
    }
#ifdef ROCKBOX_LITTLE_ENDIAN
    p=(int *)&tagdbheader;
    for(i=0;i<17;i++) {
        *p=BE32(*p);
        p++;
    }
#endif
    if ( (tagdbheader.version&0xFF) != TAGDB_VERSION)
    {
        gui_syncsplash(HZ,true,"Unsupported database version %d!",
               tagdbheader.version&0xFF);
        return -1;
    }

    if (tagdbheader.songstart > tagdbheader.filestart ||
        tagdbheader.albumstart > tagdbheader.songstart ||
        tagdbheader.artiststart > tagdbheader.albumstart)
    {
        gui_syncsplash(HZ,true,"Corrupt ID3 database!");
        return -1;
    }

    tagdb_initialized = 1;
    return 0;
}

void tagdb_shutdown(void)
{
    if (tagdb_fd >= 0)
        close(tagdb_fd);
    tagdb_initialized = 0;
}

/* NOTE: All these functions below are yet untested. */

/*** TagDatabase code ***/

void writetagdbheader(void)
{
    lseek(tagdb_fd,0,SEEK_SET);
    write(tagdb_fd, &tagdbheader, 68);
    fsync(tagdb_fd);
}

void writefentry(struct mp3entry *id)
{   long temp;
    if(!id->fileentryoffset)
        return;
    lseek(tagdb_fd,id->fileentryoffset,SEEK_SET);
    write(tagdb_fd,id->path,tagdbheader.filelen);
    temp=id->filehash;
    write(tagdb_fd,&temp,4);
    temp=id->songentryoffset;
    write(tagdb_fd,&temp,4);
    temp=id->rundbentryoffset;
    write(tagdb_fd,&temp,4);
}

void getfentrybyoffset(struct mp3entry *id,int offset)
{   
    clearfileentryinfo(id);
    lseek(tagdb_fd,offset,SEEK_SET);
    read(tagdb_fd,sbuf,tagdbheader.filelen);
    id->fileentryoffset=offset;
    read(tagdb_fd,&id->filehash,4);
    read(tagdb_fd,&id->songentryoffset,4);
    read(tagdb_fd,&id->rundbentryoffset,4);
}

#define getfentrybyrecord(_y_,_x_)  getfentrybyoffset(_y_,FILERECORD2OFFSET(_x_))

int getfentrybyfilename(struct mp3entry *id)
{
    int min=0;
    int max=tagdbheader.filecount;
    while(min<max) {
        int mid=(min+max)/2;
        int compare;
        getfentrybyrecord(id,mid);
        compare=strcasecmp(id->path,sbuf);
        if(compare==0)
            return 1;
        else if(compare<0)
            max=mid;
        else
            min=mid+1;
    }
    clearfileentryinfo(id);
    return 0;
}

#if 0
int getfentrybyhash(int hash)
{
    int min=0;
    for(min=0;min<tagdbheader.filecount;min++) {
        getfentrybyrecord(min);
        if(hash==fe.hash)
             return 1;
    }
    return 0;
}

int deletefentry(char *fname)
{
    if(!getfentrybyfilename(fname))
        return 0;
    int restrecord = currentferecord+1; 
    if(currentferecord!=tagdbheader.filecount) /* file is not last entry */
         tagdb_shiftdown(FILERECORD2OFFSET(currentferecord),
                         FILERECORD2OFFSET(restrecord),
                         (tagdbheader.filecount-restrecord)*FILEENTRY_SIZE);
    ftruncate(tagdb_fd,lseek(tagdb_fd,0,SEEK_END)-FILEENTRY_SIZE);
    tagdbheader.filecount--;
    update_fentryoffsets(restrecord,tagdbheader.filecount);
    writetagdbheader();
    return 1;
}

void update_fentryoffsets(int start, int end)
{
    int i;
    for(i=start;i<end;i++) {
        getfentrybyrecord(i);
        if(fe.songentry!=-1) {
             int p;
             lseek(tagdb_fd,fe.songentry+tagdbheader.songlen+8,SEEK_SET);
             read(tagdb_fd,&p,sizeof(int));
             if(p!=currentfeoffset) {
                  p=currentfeoffset;
                  lseek(tagdb_fd,fe.songentry+tagdbheader.songlen+8,SEEK_SET);
                  write(tagdb_fd,&p,sizeof(int));
             }
         }
         if(fe.rundbentry!=-1) {
              gui_syncsplash(HZ*2,true, "o.o.. found a rundbentry? o.o; didn't update "
                     "it, update the code o.o;");
         }
    }
}

int tagdb_shiftdown(int targetoffset, int startingoffset, int bytes)
{
    int amount;
    if(targetoffset>=startingoffset) {
        gui_syncsplash(HZ*2,true,"Woah. no beeping way. (tagdb_shiftdown)");
        return 0;
    }
    lseek(tagdb_fd,startingoffset,SEEK_SET);
    while((amount=read(tagdb_fd,sbuf,(bytes > 1024) ? 1024 : bytes))) {
        int written;
        startingoffset+=amount;
        lseek(tagdb_fd,targetoffset,SEEK_SET);
        written=write(tagdb_fd,sbuf,amount);
        targetoffset+=written;
        if(amount!=written) {
            gui_syncsplash(HZ*2,true,"Something went very wrong. expect database "
                   "corruption. (tagdb_shiftdown)");
            return 0;
        }
        lseek(tagdb_fd,startingoffset,SEEK_SET);
        bytes-=amount;
    }
    return 1;
}

int tagdb_shiftup(int targetoffset, int startingoffset, int bytes)
{
    int amount,amount2;
    int readpos,writepos,filelen;
    if(targetoffset<=startingoffset) {
        gui_syncsplash(HZ*2,true,"Um. no. (tagdb_shiftup)");
        return 0;
    }
    filelen=lseek(tagdb_fd,0,SEEK_END);
    readpos=startingoffset+bytes;
    do {
        amount=bytes>1024 ? 1024 : bytes;
        readpos-=amount;
        writepos=readpos+targetoffset-startingoffset;
        lseek(tagdb_fd,readpos,SEEK_SET);
        amount2=read(tagdb_fd,sbuf,amount);
        if(amount2!=amount) {
             gui_syncsplash(HZ*2,true,"Something went very wrong. expect database "
                    "corruption. (tagdb_shiftup)");
             return 0;
        }
        lseek(tagdb_fd,writepos,SEEK_SET);
        amount=write(tagdb_fd,sbuf,amount2);
        if(amount2!=amount) {
            gui_syncsplash(HZ*2,true,"Something went very wrong. expect database "
                   "corruption. (tagdb_shiftup)");
            return 0;
        }
        bytes-=amount;
    } while (amount>0);
    if(bytes==0)
        return 1;
    else {
        gui_syncsplash(HZ*2,true,"Something went wrong, >.>;; (tagdb_shiftup)");
        return 0;
    }
}
#endif

/*** end TagDatabase code ***/

int rundb_fd = -1;
int rundb_initialized = 0;
struct rundb_header rundbheader;

static long rundbsize;

/*** RuntimeDatabase code ***/

void rundb_unbuffer_track(struct mp3entry *id, bool last_track) {
    writeruntimeinfo(id);
    if(last_track) {
        fsync(rundb_fd);
        fsync(tagdb_fd);
    }
}

void rundb_track_change(struct mp3entry *id) {
    id->playcount++;
}

void rundb_buffer_track(struct mp3entry *id, bool last_track) {
    loadruntimeinfo(id);
    if(last_track) {
        fsync(rundb_fd);
        fsync(tagdb_fd);
    }
}

int rundb_init(void)
{
    unsigned char* ptr = (char*)&rundbheader.version;
#ifdef ROCKBOX_LITTLE_ENDIAN
    int i, *p;
#endif
    if(!tagdb_initialized) /* forget it.*/
        return -1;
        
    if(!global_settings.runtimedb) /* user doesn't care */
        return -1;
    
    rundb_fd = open(ROCKBOX_DIR "/rockbox.rundb", O_CREAT|O_RDWR);
    if (rundb_fd < 0) {
        DEBUGF("Failed opening database\n");
        return -1;
    }
    if(read(rundb_fd, &rundbheader, 8)!=8) {
        ptr[0]=ptr[1]='R';
        ptr[2]='D';
        ptr[3]=0x1;
        rundbheader.entrycount=0;
        writerundbheader();
    }

    if (ptr[0] != 'R' ||
        ptr[1] != 'R' ||
        ptr[2] != 'D')
    {
        gui_syncsplash(HZ,true,"Not a rockbox runtime database!");
        return -1;
    }
#ifdef ROCKBOX_LITTLE_ENDIAN
    p=(int *)&rundbheader;
    for(i=0;i<2;i++) {
        *p=BE32(*p);
        p++;
    }
#endif
    if ( (rundbheader.version&0xFF) != RUNDB_VERSION)
    {
        gui_syncsplash(HZ,true,"Unsupported runtime database version %d!",
               rundbheader.version&0xFF);
        return -1;
    }

    rundb_initialized = 1;
    audio_set_track_buffer_event(&rundb_buffer_track);
    audio_set_track_changed_event(&rundb_track_change);
    audio_set_track_unbuffer_event(&rundb_unbuffer_track);
    logf("rundb inited.");

    rundbsize=lseek(rundb_fd,0,SEEK_END);
    return 0;
}

void rundb_shutdown(void)
{
    if (rundb_fd >= 0)
        close(rundb_fd);
    rundb_initialized = 0;
    audio_set_track_buffer_event(NULL);
    audio_set_track_unbuffer_event(NULL);
    audio_set_track_changed_event(NULL);
}

void writerundbheader(void)
{
  lseek(rundb_fd,0,SEEK_SET);
  write(rundb_fd, &rundbheader, 8);
}

#define getrundbentrybyrecord(_y_,_x_)  getrundbentrybyoffset(_y_,8+_x_*20)

void getrundbentrybyoffset(struct mp3entry *id,int offset) {
    lseek(rundb_fd,offset+4,SEEK_SET); // skip fileentry offset
    id->rundbentryoffset=offset;
    read(rundb_fd,&id->rundbhash,4);
    read(rundb_fd,&id->rating,2);
    read(rundb_fd,&id->voladjust,2);
    read(rundb_fd,&id->playcount,4);
    read(rundb_fd,&id->lastplayed,4);
#ifdef ROCKBOX_LITTLE_ENDIAN
    id->rundbhash=BE32(id->rundbhash);
    id->rating=BE16(id->rating);
    id->voladjust=BE16(id->voladjust);
    id->playcount=BE32(id->playcount);
    id->lastplayed=BE32(id->lastplayed);
#endif
}

int getrundbentrybyhash(struct mp3entry *id)
{
    int min=0;
    for(min=0;min<rundbheader.entrycount;min++) {
        getrundbentrybyrecord(id,min);
        if(id->filehash==id->rundbhash)
             return 1;
    }
    clearruntimeinfo(id);
    return 0;
}

void writerundbentry(struct mp3entry *id)
{
    if(id->rundbhash==0) /* 0 = invalid rundb info. */
       return;
    lseek(rundb_fd,id->rundbentryoffset,SEEK_SET);
    write(rundb_fd,&id->fileentryoffset,4);
    write(rundb_fd,&id->rundbhash,4);
    write(rundb_fd,&id->rating,2);
    write(rundb_fd,&id->voladjust,2);
    write(rundb_fd,&id->playcount,4);
    write(rundb_fd,&id->lastplayed,4);
}

void writeruntimeinfo(struct mp3entry *id) {
    logf("rundb write");
    if(!id->rundbhash) 
        addrundbentry(id);
    else
        writerundbentry(id);
}

void clearfileentryinfo(struct mp3entry *id) {
    id->fileentryoffset=0;
    id->filehash=0;
    id->songentryoffset=0;
    id->rundbentryoffset=0;
}

void clearruntimeinfo(struct mp3entry *id) {
    id->rundbhash=0;
    id->rating=0;
    id->voladjust=0;
    id->playcount=0;
    id->lastplayed=0;
}

void loadruntimeinfo(struct mp3entry *id)
{
    logf("rundb load");
    clearruntimeinfo(id);
    clearfileentryinfo(id);
    if(!getfentrybyfilename(id)) {
        logf("tagdb fail: %s",id->path);
        return; /* file is not in tagdatabase, could not load. */
    }
    if(id->rundbentryoffset!=-1 && id->rundbentryoffset<rundbsize) {
        logf("load rundbentry: 0x%x", id->rundbentryoffset);
        getrundbentrybyoffset(id, id->rundbentryoffset);
        if(id->filehash != id->rundbhash) {
            logf("Rundb: Hash mismatch. trying to repair entry.",
                 id->filehash, id->rundbhash);
            findrundbentry(id);
        }
    }
    else 
#ifdef ROCKBOX_HAS_LOGF
        if(!findrundbentry(id))
            logf("rundb:no entry and not found.");
#else
        findrundbentry(id);
#endif
}

int findrundbentry(struct mp3entry *id) {
    if(getrundbentrybyhash(id)) {
        logf("Found existing rundb entry: 0x%x",id->rundbentryoffset);
        writefentry(id);
        return 1;
    }
    clearruntimeinfo(id);
    return 0;
}

void addrundbentry(struct mp3entry *id)
{
    /* first search for an entry with an equal hash. */
/*    if(findrundbentry(id)) 
           return; disabled cause this SHOULD have been checked at the buffer event.. */
    rundbheader.entrycount++;
    writerundbheader();
    id->rundbentryoffset=lseek(rundb_fd,0,SEEK_END);
    logf("Add rundb entry: 0x%x hash: 0x%x",id->rundbentryoffset,id->filehash);
    id->rundbhash=id->filehash;
    writefentry(id);
    writerundbentry(id);
    rundbsize=lseek(rundb_fd,0,SEEK_END);
}

/*** end RuntimeDatabase code ***/