2 * Test code for the "direct" interface.
13 void copy_in_a_file(char *yaffsName,char *inName)
16 unsigned char buffer[100];
18 inh = open(inName,O_RDONLY);
19 outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
21 while((ni = read(inh,buffer,100)) > 0)
23 no = yaffs_write(outh,buffer,ni);
26 printf("problem writing yaffs file\n");
35 void make_a_file(char *yaffsName,char bval,int sizeOfFile)
39 unsigned char buffer[100];
41 outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
43 memset(buffer,bval,100);
50 yaffs_write(outh,buffer,i);
52 } while (sizeOfFile > 0);
62 void fill_disk(char *path,int nfiles)
71 for(n = 0; n < nfiles; n++)
73 sprintf(str,"%s/%d",path,n);
75 h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
77 printf("writing file %s handle %d ",str, h);
79 while ((result = yaffs_write(h,xx,600)) == 600)
81 f = yaffs_freespace("/boot");
83 result = yaffs_close(h);
84 printf(" close %d\n",result);
88 void fill_disk_and_delete(char *path, int nfiles, int ncycles)
94 for(i = 0; i < ncycles; i++)
96 printf("@@@@@@@@@@@@@@ cycle %d\n",i);
97 fill_disk(path,nfiles);
99 for(j = 0; j < nfiles; j++)
101 sprintf(str,"%s/%d",path,j);
102 result = yaffs_unlink(str);
103 printf("unlinking file %s, result %d\n",str,result);
109 void fill_files(char *path,int flags, int maxIterations,int siz)
119 sprintf(str,"%s/%d",path,i);
120 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
125 for(j = 0; j < siz; j++)
127 yaffs_write(h,str,1);
136 } while(h >= 0 && i < maxIterations);
142 sprintf(str,"%s/%d",path,i);
143 printf("unlink %s\n",str);
145 } while(yaffs_unlink(str) >= 0);
149 void leave_unlinked_file(char *path,int maxIterations,int siz)
158 sprintf(str,"%s/%d",path,i);
159 printf("create %s\n",str);
160 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
166 } while(h < 0 && i < maxIterations);
170 for(i = 0; i < siz; i++)
172 yaffs_write(h,str,1);
176 printf("Leaving file %s open\n",str);
180 void dumpDirFollow(const char *dname)
187 d = yaffs_opendir(dname);
191 printf("opendir failed\n");
195 while((de = yaffs_readdir(d)) != NULL)
197 sprintf(str,"%s/%s",dname,de->d_name);
201 printf("%s length %d mode %X ",de->d_name,s.st_size,s.st_mode);
202 switch(s.st_mode & S_IFMT)
204 case S_IFREG: printf("data file"); break;
205 case S_IFDIR: printf("directory"); break;
206 case S_IFLNK: printf("symlink -->");
207 if(yaffs_readlink(str,str,100) < 0)
210 printf("\"%s\"",str);
212 default: printf("unknown"); break;
222 printf("Free space in %s is %d\n\n",dname,yaffs_freespace(dname));
225 void dumpDir(const char *dname)
232 d = yaffs_opendir(dname);
236 printf("opendir failed\n");
240 while((de = yaffs_readdir(d)) != NULL)
242 sprintf(str,"%s/%s",dname,de->d_name);
246 printf("%s length %d mode %X ",de->d_name,s.st_size,s.st_mode);
247 switch(s.st_mode & S_IFMT)
249 case S_IFREG: printf("data file"); break;
250 case S_IFDIR: printf("directory"); break;
251 case S_IFLNK: printf("symlink -->");
252 if(yaffs_readlink(str,str,100) < 0)
255 printf("\"%s\"",str);
257 default: printf("unknown"); break;
267 printf("Free space in %s is %d\n\n",dname,yaffs_freespace(dname));
272 static void PermissionsCheck(const char *path, mode_t tmode, int tflags,int expectedResult)
276 if(yaffs_chmod(path,tmode)< 0) printf("chmod failed\n");
278 fd = yaffs_open(path,tflags,0);
280 if((fd >= 0) != (expectedResult > 0))
282 printf("Permissions check %x %x %d failed\n",tmode,tflags,expectedResult);
286 printf("Permissions check %x %x %d OK\n",tmode,tflags,expectedResult);
295 int long_test(int argc, char *argv[])
306 struct yaffs_stat ystat;
310 yaffs_mount("/boot");
311 yaffs_mount("/data");
312 yaffs_mount("/flash");
315 printf("\nDirectory look-up of /boot\n");
317 printf("\nDirectory look-up of /data\n");
319 printf("\nDirectory look-up of /flash\n");
322 //leave_unlinked_file("/flash",20000,0);
323 //leave_unlinked_file("/data",20000,0);
325 leave_unlinked_file("/ram",20,0);
328 f = yaffs_open("/boot/b1", O_RDONLY,0);
330 printf("open /boot/b1 readonly, f=%d\n",f);
332 f = yaffs_open("/boot/b1", O_CREAT,S_IREAD | S_IWRITE);
334 printf("open /boot/b1 O_CREAT, f=%d\n",f);
337 r = yaffs_write(f,"hello",1);
338 printf("write %d attempted to write to a read-only file\n",r);
342 printf("close %d\n",r);
344 f = yaffs_open("/boot/b1", O_RDWR,0);
346 printf("open /boot/b1 O_RDWR,f=%d\n",f);
349 r = yaffs_write(f,"hello",2);
350 printf("write %d attempted to write to a writeable file\n",r);
351 r = yaffs_write(f,"world",3);
352 printf("write %d attempted to write to a writeable file\n",r);
354 r= yaffs_lseek(f,0,SEEK_END);
355 printf("seek end %d\n",r);
357 r = yaffs_read(f,buffer,10);
358 printf("read %d \"%s\"\n",r,buffer);
359 r= yaffs_lseek(f,0,SEEK_SET);
360 printf("seek set %d\n",r);
362 r = yaffs_read(f,buffer,10);
363 printf("read %d \"%s\"\n",r,buffer);
365 r = yaffs_read(f,buffer,10);
366 printf("read %d \"%s\"\n",r,buffer);
368 // Check values reading at end.
369 // A read past end of file should return 0 for 0 bytes read.
371 r= yaffs_lseek(f,0,SEEK_END);
372 r = yaffs_read(f,buffer,10);
373 printf("read at end returned %d\n",r);
374 r= yaffs_lseek(f,500,SEEK_END);
375 r = yaffs_read(f,buffer,10);
376 printf("read past end returned %d\n",r);
380 printf("close %d\n",r);
382 copy_in_a_file("/boot/yyfile","xxx");
384 // Create a file with a long name
386 copy_in_a_file("/boot/file with a long name","xxx");
389 printf("\nDirectory look-up of /boot\n");
393 r = yaffs_stat("/boot/file with a long name",&ystat);
397 r = yaffs_rename("/boot/file with a long name","/boot/r1");
399 printf("\nDirectory look-up of /boot\n");
403 r = yaffs_unlink("/boot/r1");
405 printf("\nDirectory look-up of /boot\n");
410 r = yaffs_mkdir("/boot/directory1",0);
412 printf("\nDirectory look-up of /boot\n");
414 printf("\nDirectory look-up of /boot/directory1\n");
415 dumpDir("/boot/directory1");
417 // add a file to the directory
418 copy_in_a_file("/boot/directory1/file with a long name","xxx");
420 printf("\nDirectory look-up of /boot\n");
422 printf("\nDirectory look-up of /boot/directory1\n");
423 dumpDir("/boot/directory1");
425 // Attempt to delete directory (should fail)
427 r = yaffs_rmdir("/boot/directory1");
429 printf("\nDirectory look-up of /boot\n");
431 printf("\nDirectory look-up of /boot/directory1\n");
432 dumpDir("/boot/directory1");
434 // Delete file first, then rmdir should work
435 r = yaffs_unlink("/boot/directory1/file with a long name");
436 r = yaffs_rmdir("/boot/directory1");
439 printf("\nDirectory look-up of /boot\n");
441 printf("\nDirectory look-up of /boot/directory1\n");
442 dumpDir("/boot/directory1");
445 fill_disk_and_delete("/boot",20,20);
447 printf("\nDirectory look-up of /boot\n");
451 yaffs_symlink("yyfile","/boot/slink");
453 yaffs_readlink("/boot/slink",str,100);
454 printf("symlink alias is %s\n",str);
459 printf("\nDirectory look-up of /boot\n");
461 printf("\nDirectory look-up of /boot (using stat instead of lstat)\n");
462 dumpDirFollow("/boot");
463 printf("\nDirectory look-up of /boot/directory1\n");
464 dumpDir("/boot/directory1");
466 h = yaffs_open("/boot/slink",O_RDWR,0);
468 printf("file length is %d\n",yaffs_lseek(h,0,SEEK_END));
472 yaffs_unlink("/boot/slink");
475 printf("\nDirectory look-up of /boot\n");
480 yaffs_stat("/boot/yyfile",&ystat);
481 temp_mode = ystat.st_mode;
483 yaffs_chmod("/boot/yyfile",0x55555);
484 printf("\nDirectory look-up of /boot\n");
487 yaffs_chmod("/boot/yyfile",temp_mode);
488 printf("\nDirectory look-up of /boot\n");
491 // Permission checks...
492 PermissionsCheck("/boot/yyfile",0, O_WRONLY,0);
493 PermissionsCheck("/boot/yyfile",0, O_RDONLY,0);
494 PermissionsCheck("/boot/yyfile",0, O_RDWR,0);
496 PermissionsCheck("/boot/yyfile",S_IREAD, O_WRONLY,0);
497 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDONLY,1);
498 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDWR,0);
500 PermissionsCheck("/boot/yyfile",S_IWRITE, O_WRONLY,1);
501 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDONLY,0);
502 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDWR,0);
504 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_WRONLY,1);
505 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDONLY,1);
506 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDWR,1);
508 yaffs_chmod("/boot/yyfile",temp_mode);
510 //create a zero-length file and unlink it (test for scan bug)
512 h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
515 yaffs_unlink("/boot/zlf");
518 yaffs_DumpDevStruct("/boot");
520 fill_disk_and_delete("/boot",20,20);
522 yaffs_DumpDevStruct("/boot");
524 fill_files("/boot",1,10000,0);
525 fill_files("/boot",1,10000,5000);
526 fill_files("/boot",2,10000,0);
527 fill_files("/boot",2,10000,5000);
529 leave_unlinked_file("/data",20000,0);
530 leave_unlinked_file("/data",20000,5000);
531 leave_unlinked_file("/data",20000,5000);
532 leave_unlinked_file("/data",20000,5000);
533 leave_unlinked_file("/data",20000,5000);
534 leave_unlinked_file("/data",20000,5000);
536 yaffs_DumpDevStruct("/boot");
537 yaffs_DumpDevStruct("/data");
547 int directory_rename_test(void)
553 yaffs_mkdir("/ram/a",0);
554 yaffs_mkdir("/ram/a/b",0);
555 yaffs_mkdir("/ram/c",0);
557 printf("\nDirectory look-up of /ram\n");
562 printf("Do rename (should fail)\n");
564 r = yaffs_rename("/ram/a","/ram/a/b/d");
565 printf("\nDirectory look-up of /ram\n");
570 printf("Do rename (should not fail)\n");
572 r = yaffs_rename("/ram/c","/ram/a/b/d");
573 printf("\nDirectory look-up of /ram\n");
583 int cache_read_test(void)
587 int sizeOfFiles = 500000;
592 yaffs_mount("/boot");
594 make_a_file("/boot/a",'a',sizeOfFiles);
595 make_a_file("/boot/b",'b',sizeOfFiles);
597 a = yaffs_open("/boot/a",O_RDONLY,0);
598 b = yaffs_open("/boot/b",O_RDONLY,0);
599 c = yaffs_open("/boot/c", O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
603 if (i > 100) i = 100;
605 yaffs_read(a,buffer,i);
606 yaffs_read(b,buffer,i);
607 yaffs_write(c,buffer,i);
608 } while(sizeOfFiles > 0);
616 int cache_bypass_bug_test(void)
618 // This test reporoduces a bug whereby YAFFS caching is buypassed
619 // resulting in erroneous reads after writes.
625 memset(buffer1,0,sizeof(buffer1));
626 memset(buffer2,0,sizeof(buffer2));
630 yaffs_mount("/boot");
632 // Create a file of 2000 bytes.
633 make_a_file("/boot/a",'X',2000);
635 a = yaffs_open("/boot/a",O_RDWR, S_IREAD | S_IWRITE);
637 // Write a short sequence to the file.
638 // This will go into the cache.
639 yaffs_lseek(a,0,SEEK_SET);
640 yaffs_write(a,"abcdefghijklmnopqrstuvwxyz",20);
642 // Read a short sequence from the file.
643 // This will come from the cache.
644 yaffs_lseek(a,0,SEEK_SET);
645 yaffs_read(a,buffer1,30);
647 // Read a page size sequence from the file.
648 yaffs_lseek(a,0,SEEK_SET);
649 yaffs_read(a,buffer2,512);
651 printf("buffer 1 %s\n",buffer1);
652 printf("buffer 2 %s\n",buffer2);
654 if(strncmp(buffer1,buffer2,20))
656 printf("Cache bypass bug detected!!!!!\n");
664 int free_space_check(void)
669 yaffs_mount("/boot");
670 fill_disk("/boot/",2);
671 f = yaffs_freespace("/boot");
673 printf("%d free when disk full\n",f);
678 int main(int argc, char *argv[])
680 //return long_test(argc,argv);
682 //return cache_read_test();
684 //return cache_bypass_bug_test();
686 return free_space_check();