void copydupe(uint16_t c,osFile oldfh,osFile newfh) { char buf[300]; uint16_t size; osSeek(oldfh,dupebuf[c].offset,OFFSET_BEGINNING); if(osRead(oldfh,&size,sizeof(uint16_t))!=sizeof(uint16_t)) return; if(osRead(oldfh,buf,size) != size) return; if(!osWrite(newfh,&size,sizeof(uint16_t))) { uint32_t err=osError(); LogWrite(1,SYSTEMERR,"Failed to write to temporary dupe file"); LogWrite(1,SYSTEMERR,"Error: %s",osErrorMsg(err)); return; } if(!osWrite(newfh,buf,size)) { uint32_t err=osError(); LogWrite(1,SYSTEMERR,"Failed to write to temporary dupe file"); LogWrite(1,SYSTEMERR,"Error: %s",osErrorMsg(err)); return; } }
void AddDupeBuf(char *buf,uint16_t size) { uint32_t offset; uint32_t crc32,*crc32p; osSeek(dupefh,0,OFFSET_END); offset=osFTell(dupefh); crc32p=(uint32_t *)buf; crc32=*crc32p; if(!osWrite(dupefh,&size,sizeof(uint16_t))) { uint32_t err=osError(); LogWrite(1,SYSTEMERR,"Failed to write to dupe file"); LogWrite(1,SYSTEMERR,"Error: %s",osErrorMsg(err)); return; } if(!osWrite(dupefh,buf,size)) { uint32_t err=osError(); LogWrite(1,SYSTEMERR,"Failed to write to dupe file"); LogWrite(1,SYSTEMERR,"Error: %s",osErrorMsg(err)); return; } adddupeindex(offset,crc32); dupechanged=TRUE; }
// a simple test for OS layer void osTest() { SqlVFS *os = osGetVFS("unix"); SqlFile *file = osGetFileHandle(os); printf("%d\n", osOpen(os, "data", file, O_RDWR | O_CREAT)); printf("%d\n", osWrite(file, "123", 3, 0)); printf("%d\n", osWrite(file, "abcd", 4, 1)); printf("%d\n", osTruncate(file, 2)); char buf[20]; int ret = osRead(file, buf, 20, 0); printf("%d\n", ret); buf[ret] = 0; printf("%s\n", buf); }
void CloseDupeDB(void) { osFile newfh; uint32_t c; char duptemp[200]; if(!dupechanged) { osClose(dupefh); return; } strcpy(duptemp,config.cfg_DupeFile); strcat(duptemp,".tmp"); if(!(newfh=osOpen(duptemp,MODE_NEWFILE))) { uint32_t err=osError(); LogWrite(1,SYSTEMERR,"Failed to open temporary dupe file %s for writing",duptemp); LogWrite(1,SYSTEMERR,"Error: %s",osErrorMsg(err)); osFree(dupebuf); osClose(dupefh); return; } osWrite(newfh,DUPES_IDENTIFIER,4); for(c=dupeentrynum;c<dupeentrymax;c++) copydupe(c,dupefh,newfh); for(c=0;c<dupeentrynum;c++) copydupe(c,dupefh,newfh); osClose(dupefh); osClose(newfh); osFree(dupebuf); dupechanged=FALSE; osDelete(config.cfg_DupeFile); osRename(duptemp,config.cfg_DupeFile); return; }
bool OpenDupeDB(void) { char buf[300]; uint32_t offset,crc32,*crc32p; uint16_t size,res; if(!(dupebuf=osAlloc(config.cfg_DupeSize*sizeof(struct dupeentry)))) { LogWrite(1,SYSTEMERR,"Not enough memory for dupe-check buffer\n"); return(FALSE); } dupeentrynum=0; dupeentrymax=0; dupechanged=FALSE; if(!(dupefh=osOpen(config.cfg_DupeFile,MODE_READWRITE))) { uint32_t err=osError(); LogWrite(1,SYSTEMERR,"Failed to open dupe file %s in read/write mode",config.cfg_DupeFile); LogWrite(1,SYSTEMERR,"Error: %s",osErrorMsg(err)); return(FALSE); } res=osRead(dupefh,buf,4); buf[4]=0; if(res == 0) { /* New file */ LogWrite(3,TOSSINGINFO,"Creating new dupe file %s",config.cfg_DupeFile); strcpy(buf,DUPES_IDENTIFIER); osWrite(dupefh,buf,4); } else if(res != 4 || strcmp(buf,DUPES_IDENTIFIER)!=0) { LogWrite(1,SYSTEMERR,"Invalid format of dupe file %s, exiting...",config.cfg_DupeFile); osClose(dupefh); return(FALSE); } offset=4; while(osRead(dupefh,&size,sizeof(uint16_t))==sizeof(uint16_t)) { if(size == 0 || size > 300) /* Unreasonably big */ { LogWrite(1,SYSTEMERR,"Error in dupe file %s, exiting...",config.cfg_DupeFile); osClose(dupefh); return(FALSE); } if(osRead(dupefh,buf,(uint32_t)size) != size) { LogWrite(1,SYSTEMERR,"Error in dupe file %s, exiting...",config.cfg_DupeFile); osClose(dupefh); return(FALSE); } crc32p=(uint32_t *)buf; crc32=*crc32p; adddupeindex(offset,crc32); offset += size+2; } dupechanged=FALSE; return(TRUE); }