void File_Set_Config_Tristate(int* f, Tristate_Config* tristate_conf) { int n; char buf[BUFFERSIZE]; memset(buf, '\0', sizeof(buf)); sprintf( buf, "Type: %d", SENSOR_TRISTATE ); //参数长度 buf[BUFFERSIZE-1] = '\n'; n = yaffs_write((*f), buf, BUFFERSIZE ); memset(buf, '\0', sizeof(buf)); sprintf( buf, "%d %d %d %d", tristate_conf->testpointNo, tristate_conf->sensorName, tristate_conf->portType, 12 ); //测点编号,传感器类型,端口类型,端口地址 buf[BUFFERSIZE-1] = '\n'; n = yaffs_write((*f), buf, BUFFERSIZE ); memset(buf, '\0', sizeof(buf)); sprintf( buf, "%s", tristate_conf->location ); //安装地点 buf[BUFFERSIZE-1] = '\n'; n = yaffs_write((*f), buf, BUFFERSIZE ); memset(buf, '\0', sizeof(buf)); sprintf( buf, "%d %d %d", tristate_conf->current[0], tristate_conf->current[1], tristate_conf->current[2] ); buf[BUFFERSIZE-1] = '\n'; n = yaffs_write((*f), buf, BUFFERSIZE ); memset(buf, '\0', sizeof(buf)); sprintf( buf, "%d", tristate_conf->outputControl ); buf[BUFFERSIZE-1] = '\n'; n = yaffs_write((*f), buf, BUFFERSIZE ); return; }
void File_Set_Config_Switch(int* f, Switch_Config* switch_conf) { int n; char buf[BUFFERSIZE]; memset(buf, '\0', sizeof(buf)); sprintf( buf, "Type: %d", SENSOR_SWITCH ); //参数长度 buf[BUFFERSIZE-1] = '\n'; n = yaffs_write((*f), buf, BUFFERSIZE ); memset(buf, '\0', sizeof(buf)); sprintf( buf, "%d %d %d %d", switch_conf->testpointNo, switch_conf->sensorName, switch_conf->portType, 22 ); //测点编号,传感器类型,端口类型,端口地址 buf[BUFFERSIZE-1] = '\n'; n = yaffs_write((*f), buf, BUFFERSIZE ); memset(buf, '\0', sizeof(buf)); sprintf( buf, "%s", switch_conf->location ); //安装地点 buf[BUFFERSIZE-1] = '\n'; n = yaffs_write((*f), buf, BUFFERSIZE ); memset(buf, '\0', sizeof(buf)); sprintf( buf, "%d", switch_conf->outputControl ); buf[BUFFERSIZE-1] = '\n'; n = yaffs_write((*f), buf, BUFFERSIZE ); return; }
int overwrite_test(const char *path) { char aname[100]; char bname[100]; int i; int j; int a; int b; yaffs_StartUp(); yaffs_mount(path); sprintf(aname,"%s%s",path,"/a"); sprintf(bname,"%s%s",path,"/b"); b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE); for(j= 0; j < 500; j++){ yaffs_write(b,bname,100); a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE); for(i = 0; i < rand() % 20000; i++) yaffs_write(a,&a,sizeof(a)); yaffs_close(a); } return 0; }
void create_resized_file_of_size(const char *fn,int syze1,int reSyze, int syze2) { int h; int iterations; h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE); iterations = (syze1 + strlen(fn) -1)/ strlen(fn); while (iterations > 0) { yaffs_write(h,fn,strlen(fn)); iterations--; } yaffs_ftruncate(h,reSyze); yaffs_lseek(h,0,SEEK_SET); iterations = (syze2 + strlen(fn) -1)/ strlen(fn); while (iterations > 0) { yaffs_write(h,fn,strlen(fn)); iterations--; } yaffs_close (h); }
void yaffs_device_flush_test(const char *path) { char fn[100]; int h; int i; yaffs_StartUp(); yaffs_mount(path); do_some_file_stuff(path); // Open and add some data to a few files for(i = 0; i < 10; i++) { sprintf(fn,"%s/ff%d",path,i); h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IWRITE | S_IREAD); yaffs_write(h,xxzz,2000); yaffs_write(h,xxzz,2000); } yaffs_unmount(path); yaffs_mount(path); }
void small_overwrite_test(const char *mountpt,int nmounts) { char a[30]; int i; int j; int h0; int h1; sprintf(a,"%s/a",mountpt); yaffs_StartUp(); for(i = 0; i < nmounts; i++){ static char xx[8000]; printf("############### Iteration %d Start\n",i); if(1) yaffs_mount(mountpt); dump_directory_tree(mountpt); yaffs_mkdir(a,0); sprintf(xx,"%s/0",a); h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE); sprintf(xx,"%s/1",a); h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE); for(j = 0; j < 1000000; j+=1000){ yaffs_ftruncate(h0,j); yaffs_lseek(h0,j,SEEK_SET); yaffs_write(h0,xx,7000); yaffs_write(h1,xx,7000); if(early_exit) exit(0); } yaffs_close(h0); printf("########### %d\n",i); dump_directory_tree(mountpt); if(1) yaffs_unmount(mountpt); } }
static int yWriteFile(const char *fname, unsigned sz32) { int h; int r; int i; unsigned checksum = 0; printf("Writing file %s\n",fname); h = yaffs_open(fname,O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE); if(h < 0){ printf("could not open file %s\n",fname); return h; } xx[0] = sz32; checksum ^= xx[0]; if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){ goto WRITE_ERROR; } while(sz32> 0){ for(i = 0; i < XX_SIZE; i++){ xx[i] = sz32 + i; checksum ^= xx[i]; } if((r = yaffs_write(h,xx,sizeof(xx))) != sizeof(xx)){ goto WRITE_ERROR; } sz32--; } xx[0] = checksum; if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){ goto WRITE_ERROR; } yaffs_close(h); return 0; WRITE_ERROR: printf("ywrite error at position %d\n",(int)yaffs_lseek(h,0,SEEK_END)); yaffs_close(h); return -1; }
void make_a_file(char *yaffsName, char bval, int sizeOfFile) { int outh; int i; unsigned char buffer[100]; outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE); if (outh < 0) { printf("Error opening file: %d. %s\n", outh, yaffs_error_str()); return; } memset(buffer, bval, 100); do { i = sizeOfFile; if (i > 100) i = 100; sizeOfFile -= i; yaffs_write(outh, buffer, i); } while (sizeOfFile > 0); yaffs_close(outh); }
void fill_disk(const char *path,int nfiles) { int h; int n; int result; int f; static char xx[600]; char str[50]; for(n = 0; n < nfiles; n++) { sprintf(str,"%s/%d",path,n); h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE); printf("writing file %s handle %d ",str, h); while ((result = yaffs_write(h,xx,600)) == 600) { f = yaffs_freespace(path); } result = yaffs_close(h); printf(" close %d\n",result); } }
int cache_read_test(void) { int a,b,c; int i; int sizeOfFiles = 500000; char buffer[100]; yaffs_StartUp(); yaffs_mount("/boot"); make_a_file("/boot/a",'a',sizeOfFiles); make_a_file("/boot/b",'b',sizeOfFiles); a = yaffs_open("/boot/a",O_RDONLY,0); b = yaffs_open("/boot/b",O_RDONLY,0); c = yaffs_open("/boot/c", O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE); do{ i = sizeOfFiles; if (i > 100) i = 100; sizeOfFiles -= i; yaffs_read(a,buffer,i); yaffs_read(b,buffer,i); yaffs_write(c,buffer,i); } while(sizeOfFiles > 0); return 1; }
void leave_unlinked_file(const char *path,int maxIterations,int siz) { int i; char str[50]; int h; i = 0; do{ sprintf(str,"%s/%d",path,i); printf("create %s\n",str); h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE); if(h >= 0) { yaffs_unlink(str); } i++; } while(h < 0 && i < maxIterations); if(h >= 0) { for(i = 0; i < siz; i++) { yaffs_write(h,str,1); } } printf("Leaving file %s open\n",str); }
void write_10k(int h) { int i; const char *s="0123456789"; for(i = 0; i < 1000; i++) yaffs_write(h,s,10); }
void File_Set_Config_Analogue(int* f, Analogue_Config* analogue_conf) { int n; char buf[BUFFERSIZE]; memset(buf, '\0', sizeof(buf)); sprintf( buf, "Type: %d", SENSOR_ANALOGUE ); //参数长度 buf[BUFFERSIZE-1] = '\n'; n = yaffs_write((*f), buf, BUFFERSIZE ); memset(buf, '\0', sizeof(buf)); sprintf( buf, "%d %d %d %d", analogue_conf->testpointNo, analogue_conf->sensorName, analogue_conf->portType, 12 ); //测点编号,传感器类型,端口类型,端口地址 buf[BUFFERSIZE-1] = '\n'; n = yaffs_write((*f), buf, BUFFERSIZE ); memset(buf, '\0', sizeof(buf)); sprintf( buf, "%s", analogue_conf->location ); //安装地点 buf[BUFFERSIZE-1] = '\n'; n = yaffs_write((*f), buf, BUFFERSIZE ); memset(buf, '\0', sizeof(buf)); sprintf( buf, "%d %d", analogue_conf->alarm.minval, analogue_conf->alarm.maxval ); buf[BUFFERSIZE-1] = '\n'; n = yaffs_write((*f), buf, BUFFERSIZE ); memset(buf, '\0', sizeof(buf)); sprintf( buf, "%d %d", analogue_conf->liftAlarm.minval, analogue_conf->liftAlarm.maxval ); buf[BUFFERSIZE-1] = '\n'; n = yaffs_write((*f), buf, BUFFERSIZE ); memset(buf, '\0', sizeof(buf)); sprintf( buf, "%d %d", analogue_conf->measureRange.minval, analogue_conf->measureRange.maxval ); buf[BUFFERSIZE-1] = '\n'; n = yaffs_write((*f), buf, BUFFERSIZE ); memset(buf, '\0', sizeof(buf)); sprintf( buf, "%d %d", analogue_conf->outage.minval, analogue_conf->outage.maxval ); buf[BUFFERSIZE-1] = '\n'; n = yaffs_write((*f), buf, BUFFERSIZE ); memset(buf, '\0', sizeof(buf)); sprintf( buf, "%d %d", analogue_conf->recover.minval, analogue_conf->recover.maxval ); buf[BUFFERSIZE-1] = '\n'; n = yaffs_write((*f), buf, BUFFERSIZE ); memset(buf, '\0', sizeof(buf)); sprintf( buf, "%d %.3f", analogue_conf->outputControl, analogue_conf->coefficient ); buf[BUFFERSIZE-1] = '\n'; n = yaffs_write((*f), buf, BUFFERSIZE ); return; }
void random_write(int h) { static char buffer[12000]; int n; n = random() & 0x1FFF; yaffs_write(h,buffer,n); }
void make_pattern_file(char *fn,int size) { int outh; int marker; int i; outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE); yaffs_lseek(outh,size-1,SEEK_SET); yaffs_write(outh,"A",1); for(i = 0; i < size; i+=256) { marker = ~i; yaffs_lseek(outh,i,SEEK_SET); yaffs_write(outh,&marker,sizeof(marker)); } yaffs_close(outh); }
void V5M_SaveImageFile(char *str, char *buf, UINT32 len) { int h; yaffs_unlink(str); h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE); yaffs_write(h, buf, (unsigned int)len); yaffs_close(h); }
void SaveImageFile(char *str, char *buf, int len) { int h; yaffs_unlink(str); h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE); yaffs_write(h, buf, len); yaffs_close(h); }
void scan_deleted_files_test(const char *mountpt) { char fn[100]; char sub[100]; const char *p; int i; int j; int k; int h; sprintf(sub,"%s/sdir",mountpt); yaffs_StartUp(); for(j = 0; j < 10; j++) { printf("\n\n>>>>>>> Run %d <<<<<<<<<<<<<\n\n",j); yaffs_mount(mountpt); yaffs_mkdir(sub,0); p = (j & 0) ? mountpt: sub; for(i = 0; i < 100; i++) { sprintf(fn,"%s/%d",p,i); if(i & 1) { h = yaffs_open(fn,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE); for(k = 0; k < 1000; k++) yaffs_write(h,fn,100); yaffs_close(h); } else yaffs_mkdir(fn,0); } for(i = 0; i < 10; i++) { sprintf(fn,"%s/%d",p,i); if(i & 1) yaffs_unlink(fn); else yaffs_rmdir(fn); } yaffs_unmount(mountpt); } }
void cmd_yaffs_mwrite_file(char *fn, char *addr, int size) { int outh; outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE); if (outh < 0) printf("Error opening file: %d, %s\n", outh, yaffs_error_str()); yaffs_write(outh, addr, size); yaffs_close(outh); }
int truncate_test(void) { int a; int r; int i; int l; char y[10]; yaffs_StartUp(); yaffs_mount("/boot"); yaffs_unlink("/boot/trunctest"); a = yaffs_open("/boot/trunctest", O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE); yaffs_write(a,"abcdefghijklmnopqrstuvwzyz",26); yaffs_ftruncate(a,3); l= yaffs_lseek(a,0,SEEK_END); printf("truncated length is %d\n",l); yaffs_lseek(a,5,SEEK_SET); yaffs_write(a,"1",1); yaffs_lseek(a,0,SEEK_SET); r = yaffs_read(a,y,10); printf("read %d bytes:",r); for(i = 0; i < r; i++) printf("[%02X]",y[i]); printf("\n"); return 0; }
static void UpdateCounter(const char *name, unsigned *val, int initialise) { int inh=-1; int outh=-1; unsigned x[2]; int nread = 0; int nwritten = 0; x[0] = x[1] = 0; if(initialise){ x[0] = 0; x[1] = 1; } else { inh = yaffs_open(name,O_RDONLY, S_IREAD | S_IWRITE); if(inh >= 0){ nread = yaffs_read(inh,x,sizeof(x)); yaffs_close(inh); } if(nread != sizeof(x) || x[0] + 1 != x[1]){ printf("Error reading counter %s handle %d, x[0] %u x[1] %u last error %d\n", name, inh, x[0], x[1],yaffsfs_GetLastError()); FatalError(); } x[0]++; x[1]++; } outh = yaffs_open(fullTempCounterName, O_RDWR | O_TRUNC | O_CREAT, S_IREAD | S_IWRITE); if(outh >= 0){ nwritten = yaffs_write(outh,x,sizeof(x)); yaffs_close(outh); yaffs_rename(fullTempCounterName,name); } if(nwritten != sizeof(x)){ printf("Error writing counter %s handle %d, x[0] %u x[1] %u\n", name, inh, x[0], x[1]); FatalError(); } *val = x[0]; printf("##\n" "## Set counter %s to %u\n" "##\n", name,x[0]); }
void simple_rw_test(const char *mountpt) { int i; int h; char a[100]; int x; int result; sprintf(a,"%s/aaa",mountpt); yaffs_StartUp(); yaffs_mount(mountpt); yaffs_unlink(a); h = yaffs_open(a,O_CREAT| O_TRUNC | O_RDWR, S_IREAD | S_IWRITE); for(i = 100000;i < 200000; i++){ result = yaffs_write(h,&i,sizeof(i)); if(result != 4) { printf("write error\n"); exit(1); } } //yaffs_close(h); // h = yaffs_open(a,O_RDWR, S_IREAD | S_IWRITE); yaffs_lseek(h,0,SEEK_SET); for(i = 100000; i < 200000; i++){ result = yaffs_read(h,&x,sizeof(x)); if(result != 4 || x != i){ printf("read error %d %x %x\n",i,result,x); } } printf("Simple rw test passed\n"); }
void cmd_yaffs_mwrite_file(char *fn, char *addr, int size) { int outh; checkMount(); outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE); if (outh < 0) { printf("Error opening file: %d\n", outh); } yaffs_write(outh,addr,size); yaffs_close(outh); }
void seek_overwrite_test(const char *mountpt,int nmounts) { static char xx[5000]; char a[30]; int i; int j; int h0; sprintf(a,"%s/f",mountpt); yaffs_StartUp(); yaffs_mount(mountpt); for(i = 0; i < nmounts; i++){ h0 = yaffs_open(a, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE); for(j = 0; j < 100000; j++){ yaffs_lseek(h0,0,SEEK_SET); yaffs_write(h0,xx,5000); yaffs_lseek(h0,0x100000,SEEK_SET); yaffs_write(h0,xx,5000); if(early_exit) exit(0); } yaffs_close(h0); } }
int make_file2(const char *name1, const char *name2,int syz) { char xx[2500]; int i; int h1=-1,h2=-1; int n = 1; if(name1) h1 = yaffs_open(name1,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE); if(name2) h2 = yaffs_open(name2,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE); while(syz > 0 && n > 0){ i = (syz > 2500) ? 2500 : syz; n = yaffs_write(h1,xx,i); n = yaffs_write(h2,xx,i); syz -= 500; } yaffs_close(h1); yaffs_close(h2); return 0; }
int cache_bypass_bug_test(void) { // This test reporoduces a bug whereby YAFFS caching *was* buypassed // resulting in erroneous reads after writes. // This bug has been fixed. int a; int i; char buffer1[1000]; char buffer2[1000]; memset(buffer1,0,sizeof(buffer1)); memset(buffer2,0,sizeof(buffer2)); yaffs_StartUp(); yaffs_mount("/boot"); // Create a file of 2000 bytes. make_a_file("/boot/a",'X',2000); a = yaffs_open("/boot/a",O_RDWR, S_IREAD | S_IWRITE); // Write a short sequence to the file. // This will go into the cache. yaffs_lseek(a,0,SEEK_SET); yaffs_write(a,"abcdefghijklmnopqrstuvwxyz",20); // Read a short sequence from the file. // This will come from the cache. yaffs_lseek(a,0,SEEK_SET); yaffs_read(a,buffer1,30); // Read a page size sequence from the file. yaffs_lseek(a,0,SEEK_SET); yaffs_read(a,buffer2,512); printf("buffer 1 %s\n",buffer1); printf("buffer 2 %s\n",buffer2); if(strncmp(buffer1,buffer2,20)) { printf("Cache bypass bug detected!!!!!\n"); } return 1; }
/*============================================================================== * - cp() * * - copy a file form here to there */ int cp(int argc, char *argv[]) { int fd_in, fd_out; int read_byte, write_byte; char file_context[1024]; char old_path_name[PATH_LEN_MAX]; char new_path_name[PATH_LEN_MAX]; CHECK_ARG_NUM(3, "too few argument"); _make_abs_path(old_path_name, argv[1]); fd_in = yaffs_open(old_path_name, O_RDONLY, 0); CHECK_YAFFS_RETVAL(fd_in, "can't open file '%s'%s\n", old_path_name, ""); _make_abs_path(new_path_name, argv[2]); fd_out = yaffs_open(new_path_name, O_CREAT|O_RDWR|O_TRUNC, S_IREAD|S_IWRITE); if (fd_out == -1) { yaffs_close(fd_in); serial_printf("can't create file '%s'", new_path_name); return CMD_ERROR; } /* * read from fd_in and write to fd_out */ read_byte = yaffs_read(fd_in, file_context, 1024); while (read_byte > 0) { write_byte = yaffs_write(fd_out, file_context, read_byte); if (write_byte != read_byte) { serial_printf("copy file failed!"); break; } read_byte = yaffs_read(fd_in, file_context, 1024); } yaffs_close(fd_in); yaffs_close(fd_out); if (read_byte > 0) { return CMD_ERROR; } else { return CMD_OK; } }
void checkpoint_fill_test(const char *mountpt,int nmounts) { char a[50]; char b[50]; char c[50]; int i; int j; int h; sprintf(a,"%s/a",mountpt); yaffs_StartUp(); for(i = 0; i < nmounts; i++){ printf("############### Iteration %d Start\n",i); yaffs_mount(mountpt); dump_directory_tree(mountpt); yaffs_mkdir(a,0); sprintf(b,"%s/zz",a); h = yaffs_open(b,O_CREAT | O_RDWR,S_IREAD |S_IWRITE); while(yaffs_write(h,c,50) == 50){} yaffs_close(h); for(j = 0; j < 2; j++){ printf("touch %d\n",j); yaffs_touch(b); yaffs_unmount(mountpt); yaffs_mount(mountpt); } dump_directory_tree(mountpt); yaffs_unmount(mountpt); } }
void create_file_of_size(const char *fn,int syze) { int h; int n; char xx[200]; int iterations = (syze + strlen(fn) -1)/ strlen(fn); h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE); while (iterations > 0) { sprintf(xx,"%s %8d",fn,iterations); yaffs_write(h,xx,strlen(xx)); iterations--; } yaffs_close (h); }
void freespace_test(const char *mountpt) { int i; int h; char a[100]; char b[100]; int f0; int f1; int f2; int f3; sprintf(a,"%s/aaa",mountpt); yaffs_StartUp(); yaffs_mount(mountpt); f0 = yaffs_freespace(mountpt); h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE); for(i = 0; i < 100; i++) yaffs_write(h,a,100); yaffs_close(h); f1 = yaffs_freespace(mountpt); yaffs_unlink(a); f2 = yaffs_freespace(mountpt); yaffs_unmount(mountpt); yaffs_mount(mountpt); f3 = yaffs_freespace(mountpt); printf("%d\n%d\n%d\n%d\n",f0, f1,f2,f3); }