void large_file_test() { int pagesize = 1024*1024; // megabyte sized segments int64_t num_segments = 5000; int64_t i; char fn[1024]; printf("Large file test... Creating a %"I64d"MB file...\n",pagesize*num_segments/(1024*1024)); filename(fn,sizeof(fn),"large_file"); AFFILE *af = af_open(fn,O_CREAT|O_RDWR|O_TRUNC,0666); unsigned char *buf = (unsigned char *)malloc(pagesize); memset(buf,'E', pagesize); af_enable_compression(af,opt_compression_type,opt_compression_level); af_set_pagesize(af,pagesize); af_set_maxsize(af,(int64_t)pagesize * 600); for(i=0;i<num_segments;i++){ sprintf((char *)buf,"%"I64d" page is put here",i); if(i%25==0) printf("\rWriting page %"I64d"\r",i); if(af_write(af,buf,pagesize)!=pagesize){ err(1,"Can't write page %"I64d,i); } } printf("\n\n"); /* Now let's just read some test locations */ for(i=0;i<num_segments;i+=num_segments/25){ // check a few places int r; af_seek(af,pagesize*i,SEEK_SET); r = af_read(af,buf,1024); // just read a bit if(r!=1024){ err(1,"Tried to read 1024 bytes; got %d\n",r); } if(atoi((char *)buf)!=i){ err(1,"at page %"I64d", expected %"I64d", got %s\n",i,i,buf); } printf("Page %"I64d" validates\n",i); } af_close(af); if(unlink("large_file.aff")){ err(1,"Can't delete large_file.aff"); } printf("Large file test passes\n"); }
AFFILE *open_testfile(const char *base,int wipe) { int flags = O_CREAT|O_RDWR; char fn[1024]; filename(fn,sizeof(fn),base); printf("%s = %s\n",base,fn); if(wipe){ unlink(fn); // make sure it is gone flags |= O_TRUNC; } AFFILE *af = af_open(fn,flags,0666); if(!af) err(1,"af_open"); if(wipe){ af_enable_compression(af,opt_compression_type,opt_compression_level); af_set_pagesize(af,1024); af_set_maxsize(af,(int64_t)65536); // force splitting of raw and afd files } return af; }
/* Add a file to the AFF system. * if fname==0, create a new one and copy over the relevant metadata... */ static int afd_add_file(AFFILE *af,const char *fname_) { struct afd_private *ap = AFD_PRIVATE(af); const char *segs_to_copy[] = {AF_BADFLAG, AF_CASE_NUM, AF_IMAGE_GID, AF_ACQUISITION_ISO_COUNTRY, AF_ACQUISITION_COMMAND_LINE, AF_ACQUISITION_DATE, AF_ACQUISITION_NOTES, AF_ACQUISITION_DEVICE, AF_ACQUISITION_TECHNICIAN, AF_DEVICE_MANUFACTURER, AF_DEVICE_MODEL, AF_DEVICE_SN, AF_DEVICE_FIRMWARE, AF_DEVICE_SOURCE, AF_CYLINDERS, AF_HEADS, AF_SECTORS_PER_TRACK, AF_LBA_SIZE, AF_HPA_PRESENT, AF_DCO_PRESENT, AF_LOCATION_IN_COMPUTER, AF_DEVICE_CAPABILITIES, 0}; char fname[MAXPATHLEN+1]; memset(fname,0,sizeof(fname)); if(fname_){ strlcpy(fname,fname_,sizeof(fname)); } else { aff_filename(af,fname,sizeof(fname),ap->num_afs); } int new_file = access(fname,F_OK)!=0; // Is this a new file? AFFILE *af2 = af_open(fname,af->openflags|AF_NO_CRYPTO,af->openmode); if(af2==0){ (*af->error_reporter)("open(%s,%d,%d) failed: %s\n", fname,af->openflags,af->openmode,strerror(errno)); return -1; // this is bad } ap->num_afs += 1; ap->afs = (AFFILE **)realloc(ap->afs,sizeof(AFFILE *) * ap->num_afs); ap->afs[ap->num_afs-1] = af2; if(new_file){ /* Copy over configuration from AFD vnode*/ af_enable_compression(af2,af->compression_type,af->compression_level); af_set_pagesize(af2,af->image_pagesize); // af_set_sectorsize(af2,af->image_sectorsize); af_update_seg(af,AF_AFF_FILE_TYPE,0,(const u_char *)"AFD",3); /* If this is the second file, copy over additional metadata from first... */ if(ap->num_afs>1){ AFFILE *af0 = ap->afs[0]; memcpy(af2->badflag,af0->badflag,af->image_sectorsize); af2->bytes_memcpy += af->image_sectorsize; for(const char **segname=segs_to_copy;*segname;segname++){ unsigned char data[65536]; // big enough for most metadata size_t datalen = sizeof(data); unsigned long arg=0; if(af_get_seg(af0,*segname,&arg,data,&datalen)==0){ int r = af_update_seg(af2,*segname,arg,data,datalen); if(r!=0){ (*af->error_reporter)("afd_add_file: could not update %s in %s (r=%d)", *segname,af_filename(af2),r); } } } } } return 0; }
void sparse_test() { printf("Sparse test...\n"); char buf[1024]; char fn[1024]; uint64_t mult = (uint64_t)3 * (uint64_t)1000000000; // 3GB in AFFILE *af = af_open(filename(fn,sizeof(fn),"sparse"),O_CREAT|O_RDWR|O_TRUNC,0666); af_enable_compression(af,opt_compression_type,opt_compression_level); af_set_maxsize(af,(int64_t)1024*1024*256); af_set_pagesize(af,1024*1024*16); for(u_int i=0;i<10;i++){ uint64_t pos = mult*i; memset(buf,0,sizeof(buf)); snprintf(buf,sizeof(buf),"This is at location=%"I64u"\n",pos); af_seek(af,pos,SEEK_SET); af_write(af,(unsigned char *)buf,sizeof(buf)); } /* Now verify */ for(u_int i=0;i<10;i++){ uint64_t pos = mult*i; uint64_t q; af_seek(af,pos,SEEK_SET); af_read(af,(unsigned char *)buf,sizeof(buf)); char *cc = strchr(buf,'='); if(!cc){ printf("Garbage read at location %"I64u"\n.",pos); exit(1); } if(sscanf(cc+1,"%"I64u,&q)!=1){ printf("Could not decode value at location %"I64u"(%s)\n",pos,cc+1); exit(1); } if(pos!=q){ printf("Wrong value at location %"I64u"; read %"I64u" in error.\n", pos,q); exit(1); } } /* Now seek to somewhere that no data has been written and see if we get 0s. */ memset(buf,'g',sizeof(buf)); af_seek(af,mult/2,SEEK_SET); ssize_t r = af_read(af,(unsigned char *)buf,sizeof(buf)); if(r!=sizeof(buf)){ err(1,"Tried to read %zd bytes at mult/2; got %zd bytes\n",sizeof(buf),r); } for(u_int i=0;i<sizeof(buf);i++){ if(buf[i]!=0) err(1,"data error; buf[%d]=%d\n",i,buf[i]); } /* Now try to read the last page in the file */ unsigned char big_buf[65536]; af_seek(af,9*mult,SEEK_SET); r = af_read(af,big_buf,sizeof(big_buf)); if(r!=sizeof(buf)){ errx(1,"Tried to read %zd bytes at the end of the file; got %zd bytes (should get %zd)", sizeof(big_buf),r,sizeof(buf)); } /* Now see if we can read past the end of the file */ af_seek(af,11*mult,SEEK_SET); r = af_read(af,(unsigned char *)buf,sizeof(buf)); if(r!=0) errx(1,"Tried to read past end of file; got %zd bytes (should get 0)",r); af_close(af); printf("\nSprase test passes.\n"); printf("=====================\n\n"); }