summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/database.c207
1 files changed, 104 insertions, 103 deletions
diff --git a/apps/database.c b/apps/database.c
index 7f605ba..615403d 100644
--- a/apps/database.c
+++ b/apps/database.c
@@ -115,138 +115,139 @@ void tagdb_shutdown(void)
/*** TagDatabase code ***/
void writetagdbheader() {
- lseek(tagdb_fd,0,SEEK_SET);
- write(tagdb_fd, &tagdbheader, 68);
+ lseek(tagdb_fd,0,SEEK_SET);
+ write(tagdb_fd, &tagdbheader, 68);
}
void getfentrybyoffset(int offset) {
- lseek(tagdb_fd,offset,SEEK_SET);
- fread(tagdb_fd,sbuf,tagdbheader.filelen);
- fread(tagdb_fd,&fe+sizeof(char *),12);
- fe.name=sbuf;
- currentfeoffset=offset;
- currentferecord=(offset-tagdbheader.filestart)/FILEENTRY_SIZE;
+ lseek(tagdb_fd,offset,SEEK_SET);
+ fread(tagdb_fd,sbuf,tagdbheader.filelen);
+ fread(tagdb_fd,&fe+sizeof(char *),12);
+ fe.name=sbuf;
+ currentfeoffset=offset;
+ currentferecord=(offset-tagdbheader.filestart)/FILEENTRY_SIZE;
}
#define getfentrybyrecord(_x_) getfentrybyoffset(FILERECORD2OFFSET(_x_))
int getfentrybyfilename(char *fname) {
- int min=0;
- int max=tagdbheader.filecount;
- while(min<max) {
- int mid=(min+max)/2;
- int compare;
- getfentrybyrecord(mid);
- compare=strcasecmp(fname,fe.name));
- if(compare==0)
+ int min=0;
+ int max=tagdbheader.filecount;
+ while(min<max) {
+ int mid=(min+max)/2;
+ int compare;
+ getfentrybyrecord(mid);
+ compare=strcasecmp(fname,fe.name));
+ if(compare==0)
return 1;
- else if(compare<0)
+ else if(compare<0)
max=mid;
- else
+ else
min=mid+1;
- }
- return 0;
+ }
+ return 0;
}
int getfentrybyhash(int hash) {
- int min=0;
- for(min=0;min<tagdbheader.filecount;min++) {
- getfentrybyrecord(min);
- if(hash==fe.hash)
+ int min=0;
+ for(min=0;min<tagdbheader.filecount;min++) {
+ getfentrybyrecord(min);
+ if(hash==fe.hash)
return 1;
- }
- return 0;
+ }
+ 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;
+ if(!getfentrybyfilename(fname))
+ return 0;
+ int restrecord = currentferecord+1;
+ if(currentferecord!=tagdbheader.filecount) /* file is not last entry */
+ 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;
}
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 i;
+ for(int 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) {
+ 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. (tagdb_shiftdown)");
- return 0;
+int tagdb_shiftdown(int targetoffset, int startingoffset, int bytes) {
+ int amount;
+ if(targetoffset>=startingoffset) {
+ splash(HZ*2,"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) {
+ splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftdown)");
+ return 0;
}
lseek(tagdb_fd,startingoffset,SEEK_SET);
- while(amount=read(tagdb_fd,sbuf,1024)) {
- int written;
- startingoffset+=amount;
- lseek(tagdb_fd,targetoffset,SEEK_SET);
- written=write(tagdb_fd,sbuf,amount);
- targetoffset+=written;
- if(amount!=written) {
- splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftdown)");
- return 0;
- }
- lseek(tagdb_fd,startingoffset,SEEK_SET);
- }
- ftruncate(tagdb_fd,lseek(tagdb_fd,0,SEEK_END) - (startingoffset-targetoffset));
- return 1;
+ bytes-=amount;
+ }
+ 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)");
+int tagdb_shiftup(int targetoffset, int startingoffset, int bytes) {
+ int amount,amount2;
+ int readpos,writepos,filelen;
+ int ok;
+ if(targetoffset<=startingoffset) {
+ splash(HZ*2,"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) {
+ splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftup)");
+ return 0;
}
- 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;
+ 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)");
+ return 0;
}
+ bytes-=amount;
+ } while (amount>0);
+ if(bytes==0)
+ return 1;
+ else {
+ splash(HZ*2,"Something went wrong, >.>;; (tagdb_shiftup)");
+ return 0;
+ }
}
/*** end TagDatabase code ***/