summaryrefslogtreecommitdiff
path: root/apps/database.c
diff options
context:
space:
mode:
authorMichiel Van Der Kolk <not.valid@email.address>2005-04-27 11:49:51 +0000
committerMichiel Van Der Kolk <not.valid@email.address>2005-04-27 11:49:51 +0000
commit67772bbf1b5bb90848d82ac3415738345088086e (patch)
tree962db2a67889ca8c5075b15ba3a00d754d5c45e1 /apps/database.c
parentfd83f521a92f91b82945ba1423bf67943a34083d (diff)
downloadrockbox-67772bbf1b5bb90848d82ac3415738345088086e.zip
rockbox-67772bbf1b5bb90848d82ac3415738345088086e.tar.gz
rockbox-67772bbf1b5bb90848d82ac3415738345088086e.tar.bz2
rockbox-67772bbf1b5bb90848d82ac3415738345088086e.tar.xz
More database maintenance functions..
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@6364 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/database.c')
-rw-r--r--apps/database.c119
1 files changed, 92 insertions, 27 deletions
diff --git a/apps/database.c b/apps/database.c
index 0d562d5..7f605ba 100644
--- a/apps/database.c
+++ b/apps/database.c
@@ -48,7 +48,7 @@
#ifdef NEW_DB_CODE
static char sbuf[1024];
static struct file_entry fe;
-static int currentferecord;
+static int currentfeoffset, currentferecord;
#endif
int tagdb_fd = -1;
@@ -111,6 +111,9 @@ void tagdb_shutdown(void)
/* NOTE: All these functions below are yet untested. */
#ifdef NEW_DB_CODE
+
+/*** TagDatabase code ***/
+
void writetagdbheader() {
lseek(tagdb_fd,0,SEEK_SET);
write(tagdb_fd, &tagdbheader, 68);
@@ -121,12 +124,11 @@ void getfentrybyoffset(int offset) {
fread(tagdb_fd,sbuf,tagdbheader.filelen);
fread(tagdb_fd,&fe+sizeof(char *),12);
fe.name=sbuf;
+ currentfeoffset=offset;
currentferecord=(offset-tagdbheader.filestart)/FILEENTRY_SIZE;
}
-void getfentrybyrecord(int record) {
- getfentrybyoffset(FILERECORD2OFFSET(record));
-}
+#define getfentrybyrecord(_x_) getfentrybyoffset(FILERECORD2OFFSET(_x_))
int getfentrybyfilename(char *fname) {
int min=0;
@@ -146,10 +148,56 @@ int getfentrybyfilename(char *fname) {
return 0;
}
-int shiftdown(int targetoffset, int startingoffset) {
+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 last entry */
+ ftruncate(tagdb_fd,lseek(tagdb_fd,0,SEEK_END)-FILEENTRY_SIZE);
+ else
+ shiftdown(FILERECORD2OFFSET(currentferecord),FILERECORD2OFFSET(restrecord));
+ tagdbheader.filecount--;
+ update_fentryoffsets(restrecord,tagdbheader.filecount);
+ writetagdbheader();
+ return 1;
+}
+
+int update_fentryoffsets(int start, int end) {
+ int i;
+ for(int i=start;i<end;i++) {
+ getfentrybyrecord(i);
+ if(fe.songentry!=-1) {
+ int *p;
+ void *songentry=(void *)sbuf+tagdbheader.filelen+2;
+ lseek(tagdb_fd,fe.songentry,SEEK_SET);
+ read(tagdb_fd,songentry,SONGENTRY_SIZE);
+ p=(int *)(songentry+tagdbheader.songlen+8);
+ if(*p!=currentfeoffset) {
+ *p=currentfeoffset;
+ lseek(tagdb_fd,fe.songentry,SEEK_SET);
+ write(tagdb_fd,songentry,SONGENTRY_SIZE);
+ }
+ }
+ if(fe.rundbentry!=-1) {
+ splash(HZ*2, "o.o.. found a rundbentry? o.o; didn't update it, update the code o.o;");
+ }
+ }
+}
+
+int tagdb_shiftdown(int targetoffset, int startingoffset) {
int amount;
if(targetoffset>=startingoffset) {
- splash(HZ*2,"Woah. no beeping way. (shiftdown)");
+ splash(HZ*2,"Woah. no beeping way. (tagdb_shiftdown)");
return 0;
}
lseek(tagdb_fd,startingoffset,SEEK_SET);
@@ -160,7 +208,7 @@ int shiftdown(int targetoffset, int startingoffset) {
written=write(tagdb_fd,sbuf,amount);
targetoffset+=written;
if(amount!=written) {
- splash(HZ*2,"Something went very wrong. expect database corruption.");
+ splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftdown)");
return 0;
}
lseek(tagdb_fd,startingoffset,SEEK_SET);
@@ -169,27 +217,44 @@ int shiftdown(int targetoffset, int startingoffset) {
return 1;
}
-int deletefentry(char *fname) {
- if(!getfentrybyfilename(fname))
- return 0;
- int restrecord = currentferecord+1;
- if(currentferecord==tagdbheader.filecount) /* file is last entry */
- ftruncate(tagdb_fd,lseek(tagdb_fd,0,SEEK_END)-FILEENTRY_SIZE);
- else
- shiftdown(FILERECORD2OFFSET(currentferecord),FILERECORD2OFFSET(restrecord));
- tagdbheader.filecount--;
- writetagdbheader();
- return 1;
-}
-
-int getfentrybyhash(int hash) {
- int min=0;
- for(min=0;min<tagdbheader.filecount;min++) {
- getfentrybyrecord(min);
- if(hash==fe.hash)
- return 1;
+int tagdb_shiftup(int targetoffset, int startingoffset) {
+ int amount,amount2;
+ int readpos,writepos,filelen;
+ int ok;
+ if(targetoffset<=startingoffset) {
+ splash(HZ*2,"Um. no. (tagdb_shiftup)");
+ }
+ filelen=lseek(tagdb_fd,0,SEEK_END);
+ readpos=filelen;
+ do {
+ amount=readpos-startingoffset>1024 ? 1024 : readpos-startingoffset;
+ readpos-=amount;
+ writepos=readpos+(targetoffset-startingoffset);
+ lseek(tagdb_fd,readpos,SEEK_SET);
+ amount2=read(tagdb_fd,sbuf,amount);
+ if(amount2!=amount) {
+ splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftup)");
+ }
+ lseek(tagdb_fd,writepos,SEEK_SET);
+ amount=write(tagdb_fd,sbuf,amount2);
+ if(amount2!=amount) {
+ splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftup)");
+ }
+ } while (amount>0);
+ if(amount==0)
+ return 1;
+ else {
+ splash(HZ*2,"Something went wrong, >.>;; (tagdb_shiftup)");
+ return 0;
}
- return 0;
}
+/*** end TagDatabase code ***/
+
+/*** RuntimeDatabase code ***/
+
+
+
+/*** end RuntimeDatabase code ***/
+
#endif