1704ac54a02cc34eb241fea93a9ff307c9006d6f
[yaffs2.git] / direct / dtest.c
1 /*
2 * Test code for the "direct" interface. 
3 */
4
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10
11 #include "yaffsfs.h"
12
13 void dumpDir(const char *dname);
14
15 char xx[600];
16
17 void copy_in_a_file(char *yaffsName,char *inName)
18 {
19         int inh,outh;
20         unsigned char buffer[100];
21         int ni,no;
22         inh = open(inName,O_RDONLY);
23         outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
24         
25         while((ni = read(inh,buffer,100)) > 0)
26         {
27                 no = yaffs_write(outh,buffer,ni);
28                 if(ni != no)
29                 {
30                         printf("problem writing yaffs file\n");
31                 }
32                 
33         }
34         
35         yaffs_close(outh);
36         close(inh);
37 }
38
39 void make_a_file(char *yaffsName,char bval,int sizeOfFile)
40 {
41         int outh;
42         int i;
43         unsigned char buffer[100];
44
45         outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
46         
47         memset(buffer,bval,100);
48         
49         do{
50                 i = sizeOfFile;
51                 if(i > 100) i = 100;
52                 sizeOfFile -= i;
53                 
54                 yaffs_write(outh,buffer,i);
55                 
56         } while (sizeOfFile > 0);
57         
58                 
59         yaffs_close(outh);
60
61 }
62
63 void make_pattern_file(char *fn,int size)
64 {
65         int outh;
66         int marker;
67         int i;
68         outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
69         yaffs_lseek(outh,size-1,SEEK_SET);
70         yaffs_write(outh,"A",1);
71         
72         for(i = 0; i < size; i+=256)
73         {
74                 marker = ~i;
75                 yaffs_lseek(outh,i,SEEK_SET);
76                 yaffs_write(outh,&marker,sizeof(marker));
77         }
78         yaffs_close(outh);
79         
80 }
81
82 int check_pattern_file(char *fn)
83 {
84         int h;
85         int marker;
86         int i;
87         int size;
88         int ok = 1;
89         
90         h = yaffs_open(fn, O_RDWR,0);
91         size = yaffs_lseek(h,0,SEEK_END);
92                 
93         for(i = 0; i < size; i+=256)
94         {
95                 yaffs_lseek(h,i,SEEK_SET);
96                 yaffs_read(h,&marker,sizeof(marker));
97                 ok = (marker == ~i);
98                 if(!ok)
99                 {
100                    printf("pattern check failed on file %s, size %d at position %d. Got %x instead of %x\n",
101                                         fn,size,i,marker,~i);
102                 }
103         }
104         yaffs_close(h);
105         return ok;
106 }
107
108
109
110
111
112 int dump_file_data(char *fn)
113 {
114         int h;
115         int marker;
116         int i = 0;
117         int size;
118         int ok = 1;
119         unsigned char b;
120         
121         h = yaffs_open(fn, O_RDWR,0);
122
123                                 
124         printf("%s\n",fn);
125         while(yaffs_read(h,&b,1)> 0)
126         {
127                 printf("%02x",b);
128                 i++;
129                 if(i > 32) 
130                 {
131                    printf("\n");
132                    i = 0;;
133                  }
134         }
135         printf("\n");
136         yaffs_close(h);
137         return ok;
138 }
139
140
141
142 void dump_file(const char *fn)
143 {
144         int i;
145         int size;
146         int h;
147         
148         h = yaffs_open(fn,O_RDONLY,0);
149         if(h < 0)
150         {
151                 printf("*****\nDump file %s does not exist\n",fn);
152         }
153         else
154         {
155                 size = yaffs_lseek(h,0,SEEK_SET);
156                 printf("*****\nDump file %s size %d\n",fn,size);
157                 for(i = 0; i < size; i++)
158                 {
159                         
160                 }
161         }
162 }
163
164 void create_file_of_size(const char *fn,int syze)
165 {
166         int h;
167         int n;
168         
169         char xx[200];
170         
171         int iterations = (syze + strlen(fn) -1)/ strlen(fn);
172         
173         h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
174                 
175         while (iterations > 0)
176         {
177                 sprintf(xx,"%s %8d",fn,iterations);
178                 yaffs_write(h,xx,strlen(xx));
179                 iterations--;
180         }
181         yaffs_close (h);
182 }
183
184 void verify_file_of_size(const char *fn,int syze)
185 {
186         int h;
187         int n;
188         
189         char xx[200];
190         char yy[200];
191         int l;
192         
193         int iterations = (syze + strlen(fn) -1)/ strlen(fn);
194         
195         h = yaffs_open(fn, O_RDONLY, S_IREAD | S_IWRITE);
196                 
197         while (iterations > 0)
198         {
199                 sprintf(xx,"%s %8d",fn,iterations);
200                 l = strlen(xx);
201                 
202                 yaffs_read(h,yy,l);
203                 yy[l] = 0;
204                 
205                 if(strcmp(xx,yy)){
206                         printf("=====>>>>> verification of file %s failed near position %d\n",fn,yaffs_lseek(h,0,SEEK_CUR));
207                 }
208                 iterations--;
209         }
210         yaffs_close (h);
211 }
212
213 void create_resized_file_of_size(const char *fn,int syze1,int reSyze, int syze2)
214 {
215         int h;
216         int n;
217         
218         
219         int iterations;
220         
221         h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
222                 
223         iterations = (syze1 + strlen(fn) -1)/ strlen(fn);
224         while (iterations > 0)
225         {
226                 yaffs_write(h,fn,strlen(fn));
227                 iterations--;
228         }
229         
230         yaffs_truncate(h,reSyze);
231         
232         yaffs_lseek(h,0,SEEK_SET);
233         iterations = (syze2 + strlen(fn) -1)/ strlen(fn);
234         while (iterations > 0)
235         {
236                 yaffs_write(h,fn,strlen(fn));
237                 iterations--;
238         }
239         
240         yaffs_close (h);
241 }
242
243
244 void do_some_file_stuff(const char *path)
245 {
246
247         char fn[100];
248
249         sprintf(fn,"%s/%s",path,"f1");
250         create_file_of_size(fn,10000);
251
252         sprintf(fn,"%s/%s",path,"fdel");
253         create_file_of_size(fn,10000);
254         yaffs_unlink(fn);
255
256         sprintf(fn,"%s/%s",path,"f2");
257         
258         create_resized_file_of_size(fn,10000,3000,4000);
259 }
260
261 void yaffs_backward_scan_test(const char *path)
262 {
263         char fn[100];
264         
265         yaffs_StartUp();        
266         
267         yaffs_mount(path);
268         
269         do_some_file_stuff(path);
270         
271         sprintf(fn,"%s/ddd",path);
272         
273         yaffs_mkdir(fn,0);
274         
275         do_some_file_stuff(fn);
276         
277         yaffs_unmount(path);
278         
279         yaffs_mount(path);
280 }
281
282 char xxzz[2000];
283
284
285 void yaffs_device_flush_test(const char *path)
286 {
287         char fn[100];
288         int h;
289         int i;
290         
291         yaffs_StartUp();        
292         
293         yaffs_mount(path);
294         
295         do_some_file_stuff(path);
296         
297         // Open and add some data to a few files
298         for(i = 0; i < 10; i++) {
299         
300                 sprintf(fn,"%s/ff%d",path,i);
301
302                 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IWRITE | S_IREAD);
303                 yaffs_write(h,xxzz,2000);
304                 yaffs_write(h,xxzz,2000);
305         }
306         yaffs_unmount(path);
307         
308         yaffs_mount(path);
309 }
310
311
312
313 void short_scan_test(const char *path, int fsize, int niterations)
314 {
315         int i;
316         char fn[100];
317         
318         sprintf(fn,"%s/%s",path,"f1");
319         
320         yaffs_StartUp();
321         for(i = 0; i < niterations; i++)
322         {
323                 printf("\n*****************\nIteration %d\n",i);
324                 yaffs_mount(path);
325                 printf("\nmount: Directory look-up of %s\n",path);
326                 dumpDir(path);
327                 make_a_file(fn,1,fsize);
328                 yaffs_unmount(path);
329         }
330 }
331
332
333
334 void scan_pattern_test(const char *path, int fsize, int niterations)
335 {
336         int i;
337         int j;
338         char fn[3][100];
339         int result;
340         
341         sprintf(fn[0],"%s/%s",path,"f0");
342         sprintf(fn[1],"%s/%s",path,"f1");
343         sprintf(fn[2],"%s/%s",path,"f2");
344         
345         yaffs_StartUp();
346         
347         for(i = 0; i < niterations; i++)
348         {
349                 printf("\n*****************\nIteration %d\n",i);
350                 yaffs_mount(path);
351                 printf("\nmount: Directory look-up of %s\n",path);
352                 dumpDir(path);
353                 for(j = 0; j < 3; j++)
354                 {
355                         result = dump_file_data(fn[j]);
356                         result = check_pattern_file(fn[j]);
357                         make_pattern_file(fn[j],fsize); 
358                         result = dump_file_data(fn[j]);
359                         result = check_pattern_file(fn[j]);
360                 }
361                 yaffs_unmount(path);
362         }
363 }
364
365 void fill_disk(char *path,int nfiles)
366 {
367         int h;
368         int n;
369         int result;
370         int f;
371         
372         char str[50];
373         
374         for(n = 0; n < nfiles; n++)
375         {
376                 sprintf(str,"%s/%d",path,n);
377                 
378                 h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
379                 
380                 printf("writing file %s handle %d ",str, h);
381                 
382                 while ((result = yaffs_write(h,xx,600)) == 600)
383                 {
384                         f = yaffs_freespace(path);
385                 }
386                 result = yaffs_close(h);
387                 printf(" close %d\n",result);
388         }
389 }
390
391 void fill_disk_and_delete(char *path, int nfiles, int ncycles)
392 {
393         int i,j;
394         char str[50];
395         int result;
396         
397         for(i = 0; i < ncycles; i++)
398         {
399                 printf("@@@@@@@@@@@@@@ cycle %d\n",i);
400                 fill_disk(path,nfiles);
401                 
402                 for(j = 0; j < nfiles; j++)
403                 {
404                         sprintf(str,"%s/%d",path,j);
405                         result = yaffs_unlink(str);
406                         printf("unlinking file %s, result %d\n",str,result);
407                 }
408         }
409 }
410
411
412 void fill_files(char *path,int flags, int maxIterations,int siz)
413 {
414         int i;
415         int j;
416         char str[50];
417         int h;
418         
419         i = 0;
420         
421         do{
422                 sprintf(str,"%s/%d",path,i);
423                 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
424                 yaffs_close(h);
425
426                 if(h >= 0)
427                 {
428                         for(j = 0; j < siz; j++)
429                         {
430                                 yaffs_write(h,str,1);
431                         }
432                 }
433                 
434                 if( flags & 1)
435                 {
436                         yaffs_unlink(str);
437                 }
438                 i++;
439         } while(h >= 0 && i < maxIterations);
440         
441         if(flags & 2)
442         {
443                 i = 0;
444                 do{
445                         sprintf(str,"%s/%d",path,i);
446                         printf("unlink %s\n",str);
447                         i++;
448                 } while(yaffs_unlink(str) >= 0);
449         }
450 }
451
452 void leave_unlinked_file(char *path,int maxIterations,int siz)
453 {
454         int i;
455         char str[50];
456         int h;
457         
458         i = 0;
459         
460         do{
461                 sprintf(str,"%s/%d",path,i);
462                 printf("create %s\n",str);
463                 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
464                 if(h >= 0)
465                 {
466                         yaffs_unlink(str);
467                 }
468                 i++;
469         } while(h < 0 && i < maxIterations);
470         
471         if(h >= 0)
472         {
473                 for(i = 0; i < siz; i++)
474                 {
475                         yaffs_write(h,str,1);
476                 }
477         }
478         
479         printf("Leaving file %s open\n",str);
480
481 }
482
483 void dumpDirFollow(const char *dname)
484 {
485         yaffs_DIR *d;
486         yaffs_dirent *de;
487         struct yaffs_stat s;
488         char str[100];
489                         
490         d = yaffs_opendir(dname);
491         
492         if(!d)
493         {
494                 printf("opendir failed\n");
495         }
496         else
497         {
498                 while((de = yaffs_readdir(d)) != NULL)
499                 {
500                         sprintf(str,"%s/%s",dname,de->d_name);
501                         
502                         yaffs_stat(str,&s);
503                         
504                         printf("%s length %d mode %X ",de->d_name,(int)s.st_size,s.st_mode);
505                         switch(s.st_mode & S_IFMT)
506                         {
507                                 case S_IFREG: printf("data file"); break;
508                                 case S_IFDIR: printf("directory"); break;
509                                 case S_IFLNK: printf("symlink -->");
510                                                           if(yaffs_readlink(str,str,100) < 0)
511                                                                 printf("no alias");
512                                                           else
513                                                                 printf("\"%s\"",str);    
514                                                           break;
515                                 default: printf("unknown"); break;
516                         }
517                         
518                         printf("\n");           
519                 }
520                 
521                 yaffs_closedir(d);
522         }
523         printf("\n");
524         
525         printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
526
527 }
528
529
530 void dump_directory_tree_worker(const char *dname,int recursive)
531 {
532         yaffs_DIR *d;
533         yaffs_dirent *de;
534         struct yaffs_stat s;
535         char str[1000];
536                         
537         d = yaffs_opendir(dname);
538         
539         if(!d)
540         {
541                 printf("opendir failed\n");
542         }
543         else
544         {
545                 while((de = yaffs_readdir(d)) != NULL)
546                 {
547                         sprintf(str,"%s/%s",dname,de->d_name);
548                         
549                         yaffs_lstat(str,&s);
550                         
551                         printf("%s inode %d obj %x length %d mode %X ",str,s.st_ino,de->d_dont_use,(int)s.st_size,s.st_mode);
552                         switch(s.st_mode & S_IFMT)
553                         {
554                                 case S_IFREG: printf("data file"); break;
555                                 case S_IFDIR: printf("directory"); break;
556                                 case S_IFLNK: printf("symlink -->");
557                                                           if(yaffs_readlink(str,str,100) < 0)
558                                                                 printf("no alias");
559                                                           else
560                                                                 printf("\"%s\"",str);    
561                                                           break;
562                                 default: printf("unknown"); break;
563                         }
564                         
565                         printf("\n");
566
567                         if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
568                                 dump_directory_tree_worker(str,1);
569                                                         
570                 }
571                 
572                 yaffs_closedir(d);
573         }
574
575 }
576
577 static void dump_directory_tree(const char *dname)
578 {
579         dump_directory_tree_worker(dname,1);
580         printf("\n");
581         printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
582 }
583
584 void dumpDir(const char *dname)
585 {       dump_directory_tree_worker(dname,0);
586         printf("\n");
587         printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
588 }
589
590
591 static void PermissionsCheck(const char *path, mode_t tmode, int tflags,int expectedResult)
592 {
593         int fd;
594         
595         if(yaffs_chmod(path,tmode)< 0) printf("chmod failed\n");
596         
597         fd = yaffs_open(path,tflags,0);
598         
599         if((fd >= 0) != (expectedResult > 0))
600         {
601                 printf("Permissions check %x %x %d failed\n",tmode,tflags,expectedResult);
602         }
603         else
604         {
605                 printf("Permissions check %x %x %d OK\n",tmode,tflags,expectedResult);
606         }
607         
608         
609         yaffs_close(fd);
610         
611         
612 }
613
614 int long_test(int argc, char *argv[])
615 {
616
617         int f;
618         int r;
619         char buffer[20];
620         
621         char str[100];
622         
623         int h;
624         mode_t temp_mode;
625         struct yaffs_stat ystat;
626         
627         yaffs_StartUp();
628         
629         yaffs_mount("/boot");
630         yaffs_mount("/data");
631         yaffs_mount("/flash");
632         yaffs_mount("/ram");
633         
634         printf("\nDirectory look-up of /boot\n");
635         dumpDir("/boot");
636         printf("\nDirectory look-up of /data\n");
637         dumpDir("/data");
638         printf("\nDirectory look-up of /flash\n");
639         dumpDir("/flash");
640
641         //leave_unlinked_file("/flash",20000,0);
642         //leave_unlinked_file("/data",20000,0);
643         
644         leave_unlinked_file("/ram",20,0);
645         
646
647         f = yaffs_open("/boot/b1", O_RDONLY,0);
648         
649         printf("open /boot/b1 readonly, f=%d\n",f);
650         
651         f = yaffs_open("/boot/b1", O_CREAT,S_IREAD | S_IWRITE);
652         
653         printf("open /boot/b1 O_CREAT, f=%d\n",f);
654         
655         
656         r = yaffs_write(f,"hello",1);
657         printf("write %d attempted to write to a read-only file\n",r);
658         
659         r = yaffs_close(f);
660         
661         printf("close %d\n",r);
662
663         f = yaffs_open("/boot/b1", O_RDWR,0);
664         
665         printf("open /boot/b1 O_RDWR,f=%d\n",f);
666         
667         
668         r = yaffs_write(f,"hello",2);
669         printf("write %d attempted to write to a writeable file\n",r);
670         r = yaffs_write(f,"world",3);
671         printf("write %d attempted to write to a writeable file\n",r);
672         
673         r= yaffs_lseek(f,0,SEEK_END);
674         printf("seek end %d\n",r);
675         memset(buffer,0,20);
676         r = yaffs_read(f,buffer,10);
677         printf("read %d \"%s\"\n",r,buffer);
678         r= yaffs_lseek(f,0,SEEK_SET);
679         printf("seek set %d\n",r);
680         memset(buffer,0,20);
681         r = yaffs_read(f,buffer,10);
682         printf("read %d \"%s\"\n",r,buffer);
683         memset(buffer,0,20);
684         r = yaffs_read(f,buffer,10);
685         printf("read %d \"%s\"\n",r,buffer);
686
687         // Check values reading at end.
688         // A read past end of file should return 0 for 0 bytes read.
689                 
690         r= yaffs_lseek(f,0,SEEK_END);
691         r = yaffs_read(f,buffer,10);
692         printf("read at end returned  %d\n",r); 
693         r= yaffs_lseek(f,500,SEEK_END);
694         r = yaffs_read(f,buffer,10);
695         printf("read past end returned  %d\n",r);       
696         
697         r = yaffs_close(f);
698         
699         printf("close %d\n",r);
700         
701         copy_in_a_file("/boot/yyfile","xxx");
702         
703         // Create a file with a long name
704         
705         copy_in_a_file("/boot/file with a long name","xxx");
706         
707         
708         printf("\nDirectory look-up of /boot\n");
709         dumpDir("/boot");
710
711         // Check stat
712         r = yaffs_stat("/boot/file with a long name",&ystat);
713         
714         // Check rename
715         
716         r = yaffs_rename("/boot/file with a long name","/boot/r1");
717         
718         printf("\nDirectory look-up of /boot\n");
719         dumpDir("/boot");
720         
721         // Check unlink
722         r = yaffs_unlink("/boot/r1");
723         
724         printf("\nDirectory look-up of /boot\n");
725         dumpDir("/boot");
726
727         // Check mkdir
728         
729         r = yaffs_mkdir("/boot/directory1",0);
730         
731         printf("\nDirectory look-up of /boot\n");
732         dumpDir("/boot");
733         printf("\nDirectory look-up of /boot/directory1\n");
734         dumpDir("/boot/directory1");
735
736         // add a file to the directory                  
737         copy_in_a_file("/boot/directory1/file with a long name","xxx");
738         
739         printf("\nDirectory look-up of /boot\n");
740         dumpDir("/boot");
741         printf("\nDirectory look-up of /boot/directory1\n");
742         dumpDir("/boot/directory1");
743         
744         //  Attempt to delete directory (should fail)
745         
746         r = yaffs_rmdir("/boot/directory1");
747         
748         printf("\nDirectory look-up of /boot\n");
749         dumpDir("/boot");
750         printf("\nDirectory look-up of /boot/directory1\n");
751         dumpDir("/boot/directory1");
752         
753         // Delete file first, then rmdir should work
754         r = yaffs_unlink("/boot/directory1/file with a long name");
755         r = yaffs_rmdir("/boot/directory1");
756         
757         
758         printf("\nDirectory look-up of /boot\n");
759         dumpDir("/boot");
760         printf("\nDirectory look-up of /boot/directory1\n");
761         dumpDir("/boot/directory1");
762
763 #if 0
764         fill_disk_and_delete("/boot",20,20);
765                         
766         printf("\nDirectory look-up of /boot\n");
767         dumpDir("/boot");
768 #endif
769
770         yaffs_symlink("yyfile","/boot/slink");
771         
772         yaffs_readlink("/boot/slink",str,100);
773         printf("symlink alias is %s\n",str);
774         
775         
776         
777         
778         printf("\nDirectory look-up of /boot\n");
779         dumpDir("/boot");
780         printf("\nDirectory look-up of /boot (using stat instead of lstat)\n");
781         dumpDirFollow("/boot");
782         printf("\nDirectory look-up of /boot/directory1\n");
783         dumpDir("/boot/directory1");
784
785         h = yaffs_open("/boot/slink",O_RDWR,0);
786         
787         printf("file length is %d\n",(int)yaffs_lseek(h,0,SEEK_END));
788         
789         yaffs_close(h);
790         
791         yaffs_unlink("/boot/slink");
792
793         
794         printf("\nDirectory look-up of /boot\n");
795         dumpDir("/boot");
796         
797         // Check chmod
798         
799         yaffs_stat("/boot/yyfile",&ystat);
800         temp_mode = ystat.st_mode;
801         
802         yaffs_chmod("/boot/yyfile",0x55555);
803         printf("\nDirectory look-up of /boot\n");
804         dumpDir("/boot");
805         
806         yaffs_chmod("/boot/yyfile",temp_mode);
807         printf("\nDirectory look-up of /boot\n");
808         dumpDir("/boot");
809         
810         // Permission checks...
811         PermissionsCheck("/boot/yyfile",0, O_WRONLY,0);
812         PermissionsCheck("/boot/yyfile",0, O_RDONLY,0);
813         PermissionsCheck("/boot/yyfile",0, O_RDWR,0);
814
815         PermissionsCheck("/boot/yyfile",S_IREAD, O_WRONLY,0);
816         PermissionsCheck("/boot/yyfile",S_IREAD, O_RDONLY,1);
817         PermissionsCheck("/boot/yyfile",S_IREAD, O_RDWR,0);
818
819         PermissionsCheck("/boot/yyfile",S_IWRITE, O_WRONLY,1);
820         PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDONLY,0);
821         PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDWR,0);
822         
823         PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_WRONLY,1);
824         PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDONLY,1);
825         PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDWR,1);
826
827         yaffs_chmod("/boot/yyfile",temp_mode);
828         
829         //create a zero-length file and unlink it (test for scan bug)
830         
831         h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
832         yaffs_close(h);
833         
834         yaffs_unlink("/boot/zlf");
835         
836         
837         yaffs_DumpDevStruct("/boot");
838         
839         fill_disk_and_delete("/boot",20,20);
840         
841         yaffs_DumpDevStruct("/boot");
842         
843         fill_files("/boot",1,10000,0);
844         fill_files("/boot",1,10000,5000);
845         fill_files("/boot",2,10000,0);
846         fill_files("/boot",2,10000,5000);
847         
848         leave_unlinked_file("/data",20000,0);
849         leave_unlinked_file("/data",20000,5000);
850         leave_unlinked_file("/data",20000,5000);
851         leave_unlinked_file("/data",20000,5000);
852         leave_unlinked_file("/data",20000,5000);
853         leave_unlinked_file("/data",20000,5000);
854         
855         yaffs_DumpDevStruct("/boot");
856         yaffs_DumpDevStruct("/data");
857         
858                 
859                 
860         return 0;
861
862 }
863
864 int huge_directory_test_on_path(char *path)
865 {
866
867         yaffs_DIR *d;
868         yaffs_dirent *de;
869         struct yaffs_stat s;
870
871         int f;
872         int i;
873         int r;
874         int total = 0;
875         int lastTotal = 0;
876         char buffer[20];
877         
878         char str[100];
879         char name[100];
880         char name2[100];
881         
882         int h;
883         mode_t temp_mode;
884         struct yaffs_stat ystat;
885         
886         yaffs_StartUp();
887         
888         yaffs_mount(path);
889         
890         // Create a large number of files
891         
892         for(i = 0; i < 2000; i++)
893         {
894           sprintf(str,"%s/%d",path,i);
895           
896            f = yaffs_open(str,O_CREAT,S_IREAD | S_IWRITE);
897            yaffs_close(f);
898         }
899         
900         
901         
902         d = yaffs_opendir(path);
903         i = 0;
904         if (d) {
905         while((de = yaffs_readdir(d)) != NULL) {
906         if (total >lastTotal+100*9*1024||(i & 1023)==0){
907         printf("files = %d, total = %d\n",i, total);
908         lastTotal = total;
909         }
910                 i++;
911                 sprintf(str,"%s/%s",path,de->d_name);
912                 yaffs_lstat(str,&s);
913                 switch(s.st_mode & S_IFMT){
914                 case S_IFREG:
915         //printf("data file");
916         total += s.st_size;
917         break;
918         }
919         }
920         
921         yaffs_closedir(d);
922         }
923         
924         return 0;
925 }
926
927 int yaffs_scan_test(const char *path)
928 {
929 }
930
931
932 void rename_over_test(const char *mountpt)
933 {
934         int i;
935         char a[100];
936         char b[100];
937         
938         sprintf(a,"%s/a",mountpt);
939         sprintf(b,"%s/b",mountpt);
940         
941         yaffs_StartUp();
942         
943         yaffs_mount(mountpt);
944         i = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, 0); 
945         yaffs_close(i);
946         i = yaffs_open(b,O_CREAT | O_TRUNC | O_RDWR, 0);
947         yaffs_close(i);
948         yaffs_rename(a,b); // rename over
949         yaffs_rename(b,a); // rename back again (not renaimng over)
950         yaffs_rename(a,b); // rename back again (not renaimng over)
951         
952         
953         yaffs_unmount(mountpt);
954         
955 }
956
957 int resize_stress_test(const char *path)
958 {
959    int a,b,i,j;
960    int x;
961    int r;
962    char aname[100];
963    char bname[100];
964    
965    char abuffer[1000];
966    char bbuffer[1000];
967    
968    yaffs_StartUp();
969    
970    yaffs_mount(path);
971    
972    sprintf(aname,"%s%s",path,"/a");
973    sprintf(bname,"%s%s",path,"/b");
974    
975    memset(abuffer,'a',1000);
976    memset(bbuffer,'b',1000);
977    
978    a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
979    b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
980    
981    printf(" %s %d %s %d\n",aname,a,bname,b);
982   
983    x = 0;
984    
985    for(j = 0; j < 100; j++)
986    {
987                 yaffs_lseek(a,0,SEEK_END);
988
989                 
990                 for(i = 0; i <20000; i++)
991                 {
992                    //r =        yaffs_lseek(b,i,SEEK_SET);
993                         //r = yaffs_write(b,bbuffer,1000);
994                         
995                         if(x & 0x16)
996                         {
997                                 // shrink
998                                 int syz = yaffs_lseek(a,0,SEEK_END);
999                                 
1000                                 syz -= 500;
1001                                 if(syz < 0) syz = 0;
1002                                 yaffs_truncate(a,syz);
1003                                 
1004                         }
1005                         else
1006                         {
1007                                 //expand
1008                                 r = yaffs_lseek(a,i * 500,SEEK_SET);
1009                                 r = yaffs_write(a,abuffer,1000);
1010                         }
1011                         x++;
1012                         
1013                 }
1014    }
1015    
1016    return 0;
1017    
1018 }
1019
1020
1021 int resize_stress_test_no_grow_complex(const char *path,int iters)
1022 {
1023    int a,b,i,j;
1024    int x;
1025    int r;
1026    char aname[100];
1027    char bname[100];
1028    
1029    char abuffer[1000];
1030    char bbuffer[1000];
1031    
1032    yaffs_StartUp();
1033    
1034    yaffs_mount(path);
1035    
1036    sprintf(aname,"%s%s",path,"/a");
1037    sprintf(bname,"%s%s",path,"/b");
1038    
1039    memset(abuffer,'a',1000);
1040    memset(bbuffer,'b',1000);
1041    
1042    a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1043    b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1044    
1045    printf(" %s %d %s %d\n",aname,a,bname,b);
1046   
1047    x = 0;
1048    
1049    for(j = 0; j < iters; j++)
1050    {
1051                 yaffs_lseek(a,0,SEEK_END);
1052
1053                 
1054                 for(i = 0; i <20000; i++)
1055                 {
1056                    //r =        yaffs_lseek(b,i,SEEK_SET);
1057                         //r = yaffs_write(b,bbuffer,1000);
1058                         
1059                         if(!(x%20))
1060                         {
1061                                 // shrink
1062                                 int syz = yaffs_lseek(a,0,SEEK_END);
1063                                 
1064                                 while(syz > 4000)
1065                                 {
1066                                 
1067                                         syz -= 2050;
1068                                         if(syz < 0) syz = 0;
1069                                         yaffs_truncate(a,syz);
1070                                         syz = yaffs_lseek(a,0,SEEK_END);
1071                                         printf("shrink to %d\n",syz);
1072                                 }
1073                                 
1074                                 
1075                         }
1076                         else
1077                         {
1078                                 //expand
1079                                 r = yaffs_lseek(a,500,SEEK_END);
1080                                 r = yaffs_write(a,abuffer,1000);
1081                         }
1082                         x++;
1083                         
1084                                         
1085                 }
1086                 printf("file size is %d\n",yaffs_lseek(a,0,SEEK_END));
1087
1088    }
1089    
1090    return 0;
1091    
1092 }
1093
1094 int resize_stress_test_no_grow(const char *path,int iters)
1095 {
1096    int a,b,i,j;
1097    int x;
1098    int r;
1099    char aname[100];
1100    char bname[100];
1101    
1102    char abuffer[1000];
1103    char bbuffer[1000];
1104    
1105    yaffs_StartUp();
1106    
1107    yaffs_mount(path);
1108    
1109    sprintf(aname,"%s%s",path,"/a");
1110    sprintf(bname,"%s%s",path,"/b");
1111    
1112    memset(abuffer,'a',1000);
1113    memset(bbuffer,'b',1000);
1114    
1115    a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1116    b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1117    
1118    printf(" %s %d %s %d\n",aname,a,bname,b);
1119   
1120    x = 0;
1121    
1122    for(j = 0; j < iters; j++)
1123    {
1124                 yaffs_lseek(a,0,SEEK_END);
1125
1126                 
1127                 for(i = 0; i <20000; i++)
1128                 {
1129                    //r =        yaffs_lseek(b,i,SEEK_SET);
1130                         //r = yaffs_write(b,bbuffer,1000);
1131                         
1132                         if(!(x%20))
1133                         {
1134                                 // shrink
1135                                 int syz = yaffs_lseek(a,0,SEEK_END);
1136                                 
1137                                 while(syz > 4000)
1138                                 {
1139                                 
1140                                         syz -= 2050;
1141                                         if(syz < 0) syz = 0;
1142                                         yaffs_truncate(a,syz);
1143                                         syz = yaffs_lseek(a,0,SEEK_END);
1144                                         printf("shrink to %d\n",syz);
1145                                 }
1146                                 
1147                                 
1148                         }
1149                         else
1150                         {
1151                                 //expand
1152                                 r = yaffs_lseek(a,-500,SEEK_END);
1153                                 r = yaffs_write(a,abuffer,1000);
1154                         }
1155                         x++;
1156                         
1157                                         
1158                 }
1159                 printf("file size is %d\n",yaffs_lseek(a,0,SEEK_END));
1160
1161    }
1162    
1163    return 0;
1164    
1165 }
1166
1167 int directory_rename_test(void)
1168 {
1169         int r;
1170         yaffs_StartUp();
1171         
1172         yaffs_mount("/ram");
1173         yaffs_mkdir("/ram/a",0);
1174         yaffs_mkdir("/ram/a/b",0);
1175         yaffs_mkdir("/ram/c",0);
1176         
1177         printf("\nDirectory look-up of /ram\n");
1178         dumpDir("/ram");
1179         dumpDir("/ram/a");
1180         dumpDir("/ram/a/b");
1181
1182         printf("Do rename (should fail)\n");
1183                 
1184         r = yaffs_rename("/ram/a","/ram/a/b/d");
1185         printf("\nDirectory look-up of /ram\n");
1186         dumpDir("/ram");
1187         dumpDir("/ram/a");
1188         dumpDir("/ram/a/b");
1189
1190         printf("Do rename (should not fail)\n");
1191                 
1192         r = yaffs_rename("/ram/c","/ram/a/b/d");
1193         printf("\nDirectory look-up of /ram\n");
1194         dumpDir("/ram");
1195         dumpDir("/ram/a");
1196         dumpDir("/ram/a/b");
1197         
1198         
1199         return 1;
1200         
1201 }
1202
1203 int cache_read_test(void)
1204 {
1205         int a,b,c;
1206         int i;
1207         int sizeOfFiles = 500000;
1208         char buffer[100];
1209         
1210         yaffs_StartUp();
1211         
1212         yaffs_mount("/boot");
1213         
1214         make_a_file("/boot/a",'a',sizeOfFiles);
1215         make_a_file("/boot/b",'b',sizeOfFiles);
1216
1217         a = yaffs_open("/boot/a",O_RDONLY,0);
1218         b = yaffs_open("/boot/b",O_RDONLY,0);
1219         c = yaffs_open("/boot/c", O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
1220
1221         do{
1222                 i = sizeOfFiles;
1223                 if (i > 100) i = 100;
1224                 sizeOfFiles  -= i;
1225                 yaffs_read(a,buffer,i);
1226                 yaffs_read(b,buffer,i);
1227                 yaffs_write(c,buffer,i);
1228         } while(sizeOfFiles > 0);
1229         
1230         
1231         
1232         return 1;
1233         
1234 }
1235
1236 int cache_bypass_bug_test(void)
1237 {
1238         // This test reporoduces a bug whereby YAFFS caching *was* buypassed
1239         // resulting in erroneous reads after writes.
1240         // This bug has been fixed.
1241         
1242         int a;
1243         int i;
1244         char buffer1[1000];
1245         char buffer2[1000];
1246         
1247         memset(buffer1,0,sizeof(buffer1));
1248         memset(buffer2,0,sizeof(buffer2));
1249                 
1250         yaffs_StartUp();
1251         
1252         yaffs_mount("/boot");
1253         
1254         // Create a file of 2000 bytes.
1255         make_a_file("/boot/a",'X',2000);
1256
1257         a = yaffs_open("/boot/a",O_RDWR, S_IREAD | S_IWRITE);
1258         
1259         // Write a short sequence to the file.
1260         // This will go into the cache.
1261         yaffs_lseek(a,0,SEEK_SET);
1262         yaffs_write(a,"abcdefghijklmnopqrstuvwxyz",20); 
1263
1264         // Read a short sequence from the file.
1265         // This will come from the cache.
1266         yaffs_lseek(a,0,SEEK_SET);
1267         yaffs_read(a,buffer1,30); 
1268
1269         // Read a page size sequence from the file.
1270         yaffs_lseek(a,0,SEEK_SET);
1271         yaffs_read(a,buffer2,512); 
1272         
1273         printf("buffer 1 %s\n",buffer1);
1274         printf("buffer 2 %s\n",buffer2);
1275         
1276         if(strncmp(buffer1,buffer2,20))
1277         {
1278                 printf("Cache bypass bug detected!!!!!\n");
1279         }
1280         
1281         
1282         return 1;
1283 }
1284
1285
1286 int free_space_check(void)
1287 {
1288         int f;
1289         
1290                 yaffs_StartUp();
1291                 yaffs_mount("/boot");
1292             fill_disk("/boot/",2);
1293             f = yaffs_freespace("/boot");
1294             
1295             printf("%d free when disk full\n",f);           
1296             return 1;
1297 }
1298
1299 int truncate_test(void)
1300 {
1301         int a;
1302         int r;
1303         int i;
1304         int l;
1305
1306         char y[10];
1307
1308         yaffs_StartUp();
1309         yaffs_mount("/boot");
1310
1311         yaffs_unlink("/boot/trunctest");
1312         
1313         a = yaffs_open("/boot/trunctest", O_CREAT | O_TRUNC | O_RDWR,  S_IREAD | S_IWRITE);
1314         
1315         yaffs_write(a,"abcdefghijklmnopqrstuvwzyz",26);
1316         
1317         yaffs_truncate(a,3);
1318         l= yaffs_lseek(a,0,SEEK_END);
1319         
1320         printf("truncated length is %d\n",l);
1321
1322         yaffs_lseek(a,5,SEEK_SET);
1323         yaffs_write(a,"1",1);
1324
1325         yaffs_lseek(a,0,SEEK_SET);
1326         
1327         r = yaffs_read(a,y,10);
1328
1329         printf("read %d bytes:",r);
1330
1331         for(i = 0; i < r; i++) printf("[%02X]",y[i]);
1332
1333         printf("\n");
1334
1335         return 0;
1336
1337 }
1338
1339
1340
1341
1342
1343 void fill_disk_test(const char *mountpt)
1344 {
1345         int i;
1346         yaffs_StartUp();
1347         
1348         for(i = 0; i < 5; i++)
1349         {
1350                 yaffs_mount(mountpt);
1351                 fill_disk_and_delete(mountpt,100,i+1);
1352                 yaffs_unmount(mountpt);
1353         }
1354         
1355 }
1356
1357
1358
1359 void lookup_test(const char *mountpt)
1360 {
1361         int i;
1362         int h;
1363         char a[100];
1364         char b[100];
1365         
1366
1367         yaffs_DIR *d;
1368         yaffs_dirent *de;
1369         struct yaffs_stat s;
1370         char str[100];
1371
1372         yaffs_StartUp();
1373         
1374         yaffs_mount(mountpt);
1375                                 
1376         d = yaffs_opendir(mountpt);
1377         
1378         if(!d)
1379         {
1380                 printf("opendir failed\n");
1381         }
1382         else
1383         {
1384                 
1385                 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1386                 {
1387                         printf("unlinking %s\n",de->d_name);
1388                         yaffs_unlink(de->d_name);
1389                 }
1390                 
1391                 printf("%d files deleted\n",i);
1392         }
1393         
1394         
1395         for(i = 0; i < 2000; i++){
1396         sprintf(a,"%s/%d",mountpt,i);
1397                 h =  yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, 0);
1398                 yaffs_close(h);
1399         }
1400
1401         yaffs_rewinddir(d);
1402         for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1403         {
1404                 printf("%d  %s\n",i,de->d_name);
1405         }       
1406         
1407         printf("%d files listed\n\n\n",i);
1408         
1409         yaffs_rewinddir(d);
1410         yaffs_readdir(d);
1411         yaffs_readdir(d);
1412         yaffs_readdir(d);
1413         
1414         for(i = 0; i < 2000; i++){
1415                 sprintf(a,"%s/%d",mountpt,i);
1416                 yaffs_unlink(a);
1417         }
1418         
1419                 
1420         yaffs_unmount(mountpt);
1421         
1422 }
1423
1424 void link_test(const char *mountpt)
1425 {
1426         int i;
1427         int h;
1428         char a[100];
1429         char b[100];
1430         char c[100];
1431         
1432         int  f0;
1433         int f1;
1434         int f2;
1435         int f3;
1436         sprintf(a,"%s/aaa",mountpt);
1437         sprintf(b,"%s/bbb",mountpt);
1438         sprintf(c,"%s/ccc",mountpt);
1439         
1440         yaffs_StartUp();
1441         
1442         yaffs_mount(mountpt);
1443         
1444         
1445         h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1446         for(i = 0; i < 100; i++)
1447                 yaffs_write(h,a,100);
1448         
1449         yaffs_close(h);
1450         
1451         yaffs_unlink(b);
1452         yaffs_unlink(c);
1453         yaffs_link(a,b);
1454         yaffs_link(a,c);
1455         yaffs_unlink(b);
1456         yaffs_unlink(c);
1457         yaffs_unlink(a);
1458         
1459         
1460         yaffs_unmount(mountpt);
1461         yaffs_mount(mountpt);
1462         
1463         printf("link test done\n");     
1464         
1465 }
1466
1467 void freespace_test(const char *mountpt)
1468 {
1469         int i;
1470         int h;
1471         char a[100];
1472         char b[100];
1473         
1474         int  f0;
1475         int f1;
1476         int f2;
1477         int f3;
1478         sprintf(a,"%s/aaa",mountpt);
1479         
1480         yaffs_StartUp();
1481         
1482         yaffs_mount(mountpt);
1483         
1484         f0 = yaffs_freespace(mountpt);
1485         
1486         h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1487         
1488         for(i = 0; i < 100; i++)
1489                 yaffs_write(h,a,100);
1490         
1491         yaffs_close(h);
1492         
1493         f1 = yaffs_freespace(mountpt);
1494         
1495         yaffs_unlink(a);
1496         
1497         f2 = yaffs_freespace(mountpt);
1498         
1499                 
1500         yaffs_unmount(mountpt);
1501         yaffs_mount(mountpt);
1502         
1503         f3 = yaffs_freespace(mountpt);
1504         
1505         printf("%d\n%d\n%d\n%d\n",f0, f1,f2,f3);
1506         
1507         
1508 }
1509
1510 void simple_rw_test(const char *mountpt)
1511 {
1512         int i;
1513         int h;
1514         char a[100];
1515         
1516         int x;
1517         int result;
1518
1519         sprintf(a,"%s/aaa",mountpt);
1520         
1521         yaffs_StartUp();
1522         
1523         yaffs_mount(mountpt);
1524         
1525         yaffs_unlink(a);
1526         
1527         h = yaffs_open(a,O_CREAT| O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1528         
1529         for(i = 100000;i < 200000; i++){
1530                 result = yaffs_write(h,&i,sizeof(i));
1531                 
1532                 if(result != 4)
1533                 {
1534                         printf("write error\n");
1535                         exit(1);
1536                 }
1537         }
1538         
1539         //yaffs_close(h);
1540         
1541         // h = yaffs_open(a,O_RDWR, S_IREAD | S_IWRITE);
1542         
1543         
1544         yaffs_lseek(h,0,SEEK_SET);
1545         
1546         for(i = 100000; i < 200000; i++){
1547                 result = yaffs_read(h,&x,sizeof(x));
1548                 
1549                 if(result != 4 || x != i){
1550                         printf("read error %d %x %x\n",i,result,x);
1551                 }
1552         }
1553         
1554         printf("Simple rw test passed\n");
1555         
1556         
1557         
1558 }
1559
1560
1561 void scan_deleted_files_test(const char *mountpt)
1562 {
1563         char fn[100];
1564         char sub[100];
1565         
1566         const char *p;
1567         
1568         int i;
1569         int j;
1570         int k;
1571         int h;
1572         
1573         sprintf(sub,"%s/sdir",mountpt);
1574         yaffs_StartUp();
1575         
1576         for(j = 0; j < 10; j++)
1577         {
1578                 printf("\n\n>>>>>>> Run %d <<<<<<<<<<<<<\n\n",j);
1579                 yaffs_mount(mountpt);
1580                 yaffs_mkdir(sub,0);
1581                 
1582                 
1583                 p = (j & 0) ? mountpt: sub;
1584         
1585                 for(i = 0; i < 100; i++)
1586                 {
1587                   sprintf(fn,"%s/%d",p,i);  
1588                   
1589                   if(i & 1)
1590                   {
1591                           h = yaffs_open(fn,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1592                           for(k = 0; k < 1000; k++)
1593                                   yaffs_write(h,fn,100);
1594                           yaffs_close(h);
1595                   }
1596                   else
1597                         yaffs_mkdir(fn,0);
1598                 }
1599                 
1600                 for(i = 0; i < 10; i++)
1601                 {
1602                   sprintf(fn,"%s/%d",p,i);  
1603                   if(i & 1) 
1604                         yaffs_unlink(fn);
1605                   else
1606                         yaffs_rmdir(fn);
1607                   
1608                 }
1609                                 
1610                 yaffs_unmount(mountpt);
1611         }
1612         
1613         
1614         
1615
1616 }
1617
1618
1619 void write_10k(int h)
1620 {
1621    int i;
1622    const char *s="0123456789";
1623    for(i = 0; i < 1000; i++)
1624      yaffs_write(h,s,10);
1625
1626 }
1627 void write_200k_file(const char *fn, const char *fdel, const char *fdel1)
1628 {
1629    int h1;
1630    int i;
1631    int offs;
1632    
1633    h1 = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1634    
1635    for(i = 0; i < 100000; i+= 10000)
1636    {
1637         write_10k(h1);
1638    }
1639    
1640    offs = yaffs_lseek(h1,0,SEEK_CUR);
1641    if( offs != 100000)
1642    {
1643         printf("Could not write file\n");
1644    }
1645    
1646    yaffs_unlink(fdel);
1647    for(i = 0; i < 100000; i+= 10000)
1648    {
1649         write_10k(h1);
1650    }
1651    
1652    offs = yaffs_lseek(h1,0,SEEK_CUR);
1653    if( offs != 200000)
1654    {
1655         printf("Could not write file\n");
1656    }
1657    
1658    yaffs_close(h1);
1659    yaffs_unlink(fdel1);
1660    
1661 }
1662
1663
1664 void verify_200k_file(const char *fn)
1665 {
1666    int h1;
1667    int i;
1668    char x[11];
1669    const char *s="0123456789";
1670    int errCount = 0;
1671    
1672    h1 = yaffs_open(fn, O_RDONLY, 0);
1673    
1674    for(i = 0; i < 200000 && errCount < 10; i+= 10)
1675    {
1676         yaffs_read(h1,x,10);
1677         if(strncmp(x,s,10) != 0)
1678         {
1679                 printf("File %s verification failed at %d\n",fn,i);
1680                 errCount++;
1681         }
1682    }
1683    if(errCount >= 10)
1684         printf("Too many errors... aborted\n");
1685       
1686    yaffs_close(h1);        
1687         
1688 }
1689
1690
1691 void check_resize_gc_bug(const char *mountpt)
1692 {
1693
1694         char a[30];
1695         char b[30];
1696         char c[30];
1697         
1698         int i;
1699         
1700         sprintf(a,"%s/a",mountpt);
1701         sprintf(b,"%s/b",mountpt);
1702         sprintf(c,"%s/c",mountpt);
1703         
1704
1705         
1706         
1707         yaffs_StartUp();
1708         yaffs_mount(mountpt);
1709         yaffs_unlink(a);
1710         yaffs_unlink(b);
1711         
1712         for(i = 0; i < 50; i++)
1713         {  
1714            printf("A\n");write_200k_file(a,"",c);
1715            printf("B\n");verify_200k_file(a);
1716            printf("C\n");write_200k_file(b,a,c);
1717            printf("D\n");verify_200k_file(b);
1718            yaffs_unmount(mountpt);
1719            yaffs_mount(mountpt);
1720            printf("E\n");verify_200k_file(a);
1721            printf("F\n");verify_200k_file(b);
1722         }
1723                 
1724 }
1725
1726
1727 void multi_mount_test(const char *mountpt,int nmounts)
1728 {
1729
1730         char a[30];
1731         char b[30];
1732         char c[30];
1733         
1734         int i;
1735         int j;
1736         
1737         sprintf(a,"%s/a",mountpt);
1738
1739         yaffs_StartUp();
1740         
1741         for(i = 0; i < nmounts; i++){
1742                 int h0;
1743                 int h1;
1744                 int len0;
1745                 int len1;
1746                 
1747                 static char xx[1000];
1748                 
1749                 printf("############### Iteration %d   Start\n",i);
1750                 if(1 || i == 0 || i == 5) 
1751                         yaffs_mount(mountpt);
1752
1753                 dump_directory_tree(mountpt);
1754                 
1755                 
1756                 yaffs_mkdir(a,0);
1757                 
1758                 sprintf(xx,"%s/0",a);
1759                 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1760                 
1761                 sprintf(xx,"%s/1",a);
1762                 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1763                 
1764                 for(j = 0; j < 200; j++){
1765                    yaffs_write(h0,xx,1000);
1766                    yaffs_write(h1,xx,1000);
1767                 }
1768                 
1769                 len0 = yaffs_lseek(h0,0,SEEK_END);
1770                 len1 = yaffs_lseek(h1,0,SEEK_END);
1771                 
1772                 yaffs_lseek(h0,0,SEEK_SET);
1773                 yaffs_lseek(h1,0,SEEK_SET);
1774
1775                 for(j = 0; j < 200; j++){
1776                    yaffs_read(h0,xx,1000);
1777                    yaffs_read(h1,xx,1000);
1778                 }
1779                 
1780                 
1781                 yaffs_truncate(h0,0);
1782                 yaffs_close(h0);
1783                 yaffs_close(h1);
1784                 
1785                 printf("########### %d\n",i);
1786                 dump_directory_tree(mountpt);
1787
1788                 if(1 || i == 4 || i == nmounts -1)
1789                         yaffs_unmount(mountpt);
1790         }
1791 }
1792
1793
1794 void small_mount_test(const char *mountpt,int nmounts)
1795 {
1796
1797         char a[30];
1798         char b[30];
1799         char c[30];
1800         
1801         int i;
1802         int j;
1803
1804         int h0;
1805         int h1;
1806         int len0;
1807         int len1;
1808         int nread;
1809         
1810         sprintf(a,"%s/a",mountpt);
1811
1812         yaffs_StartUp();
1813         
1814         
1815         
1816         for(i = 0; i < nmounts; i++){
1817                 
1818                 static char xx[1000];
1819                 
1820                 printf("############### Iteration %d   Start\n",i);
1821                 if(1 || i == 0 || i == 5) 
1822                         yaffs_mount(mountpt);
1823
1824                 dump_directory_tree(mountpt);
1825                 
1826                 yaffs_mkdir(a,0);
1827                 
1828                 sprintf(xx,"%s/0",a);
1829                 if(i ==0){
1830                 
1831                         h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1832                         for(j = 0; j < 130; j++)
1833                                 yaffs_write(h0,xx,1000);
1834                         yaffs_close(h0);
1835                 }
1836                 
1837                 h0 = yaffs_open(xx,O_RDONLY,0);
1838                 
1839                 sprintf(xx,"%s/1",a);
1840                 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1841                 
1842                 while((nread = yaffs_read(h0,xx,1000)) > 0)
1843                         yaffs_write(h1,xx,nread);
1844                 
1845                 
1846                 len0 = yaffs_lseek(h0,0,SEEK_END);
1847                 len1 = yaffs_lseek(h1,0,SEEK_END);
1848                 
1849                 yaffs_lseek(h0,0,SEEK_SET);
1850                 yaffs_lseek(h1,0,SEEK_SET);
1851
1852                 for(j = 0; j < 200; j++){
1853                    yaffs_read(h0,xx,1000);
1854                    yaffs_read(h1,xx,1000);
1855                 }
1856                 
1857                 yaffs_close(h0);
1858                 yaffs_close(h1);
1859                 
1860                 printf("########### %d\n",i);
1861                 dump_directory_tree(mountpt);
1862
1863                 if(1 || i == 4 || i == nmounts -1)
1864                         yaffs_unmount(mountpt);
1865         }
1866 }
1867
1868
1869 int early_exit;
1870
1871 void small_overwrite_test(const char *mountpt,int nmounts)
1872 {
1873
1874         char a[30];
1875         char b[30];
1876         char c[30];
1877         
1878         int i;
1879         int j;
1880
1881         int h0;
1882         int h1;
1883         int len0;
1884         int len1;
1885         int nread;
1886         
1887         sprintf(a,"%s/a",mountpt);
1888
1889         yaffs_StartUp();
1890         
1891         
1892         
1893         for(i = 0; i < nmounts; i++){
1894                 
1895                 static char xx[8000];
1896                 
1897                 printf("############### Iteration %d   Start\n",i);
1898                 if(1)
1899                         yaffs_mount(mountpt);
1900
1901                 dump_directory_tree(mountpt);
1902                 
1903                 yaffs_mkdir(a,0);
1904                 
1905                 sprintf(xx,"%s/0",a);
1906                 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1907                 sprintf(xx,"%s/1",a);
1908                 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1909                 
1910                 for(j = 0; j < 1000000; j+=1000){
1911                         yaffs_truncate(h0,j);
1912                         yaffs_lseek(h0,j,SEEK_SET);
1913                         yaffs_write(h0,xx,7000);
1914                         yaffs_write(h1,xx,7000);
1915                         
1916                         if(early_exit)
1917                                 exit(0);
1918                 }
1919                 
1920                 yaffs_close(h0);
1921                 
1922                 printf("########### %d\n",i);
1923                 dump_directory_tree(mountpt);
1924
1925                 if(1)
1926                         yaffs_unmount(mountpt);
1927         }
1928 }
1929
1930
1931 void yaffs_touch(const char *fn)
1932 {
1933         yaffs_chmod(fn, S_IREAD | S_IWRITE);
1934 }
1935
1936 void checkpoint_fill_test(const char *mountpt,int nmounts)
1937 {
1938
1939         char a[50];
1940         char b[50];
1941         char c[50];
1942         
1943         int i;
1944         int j;
1945         int h;
1946         
1947         sprintf(a,"%s/a",mountpt);
1948         
1949
1950         
1951         
1952         yaffs_StartUp();
1953         
1954         for(i = 0; i < nmounts; i++){
1955                 printf("############### Iteration %d   Start\n",i);
1956                 yaffs_mount(mountpt);
1957                 dump_directory_tree(mountpt);
1958                 yaffs_mkdir(a,0);
1959                 
1960                 sprintf(b,"%s/zz",a);
1961                 
1962                 h = yaffs_open(b,O_CREAT | O_RDWR,S_IREAD |S_IWRITE);
1963                 
1964                 
1965                 while(yaffs_write(h,c,50) == 50){}
1966                 
1967                 yaffs_close(h);
1968                 
1969                 for(j = 0; j < 2; j++){
1970                         printf("touch %d\n",j);
1971                         yaffs_touch(b);
1972                         yaffs_unmount(mountpt);
1973                         yaffs_mount(mountpt);
1974                 }
1975
1976                 dump_directory_tree(mountpt);           
1977                 yaffs_unmount(mountpt);
1978         }
1979 }
1980
1981
1982 int make_file2(const char *name1, const char *name2,int syz)
1983 {
1984
1985         char xx[2500];
1986         int i;
1987         int h1=-1,h2=-1;
1988         int n = 1;
1989
1990
1991         if(name1)
1992                 h1 = yaffs_open(name1,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1993         if(name2)
1994                 h2 = yaffs_open(name2,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1995         
1996         while(syz > 0 && n > 0){
1997                 i = (syz > 2500) ? 2500 : syz;
1998                 n = yaffs_write(h1,xx,i);
1999                 n = yaffs_write(h2,xx,i);
2000                 syz -= 500;
2001         }
2002         yaffs_close(h1);
2003         yaffs_close(h2);
2004         
2005 }
2006
2007
2008 extern void SetCheckpointReservedBlocks(int n);
2009
2010 void checkpoint_upgrade_test(const char *mountpt,int nmounts)
2011 {
2012
2013         char a[50];
2014         char b[50];
2015         char c[50];
2016         char d[50];
2017         
2018         int i;
2019         int j;
2020         int h;
2021         
2022         sprintf(a,"%s/a",mountpt);
2023         
2024
2025         
2026         
2027         printf("Create start condition\n");
2028         yaffs_StartUp();
2029         SetCheckpointReservedBlocks(0);
2030         yaffs_mount(mountpt);
2031         yaffs_mkdir(a,0);
2032         sprintf(b,"%s/zz",a);
2033         sprintf(c,"%s/xx",a);
2034         make_file2(b,c,2000000);
2035         sprintf(d,"%s/aa",a);
2036         make_file2(d,NULL,500000000);
2037         dump_directory_tree(mountpt);
2038         
2039         printf("Umount/mount attempt full\n");
2040         yaffs_unmount(mountpt);
2041         
2042         SetCheckpointReservedBlocks(10);
2043         yaffs_mount(mountpt);
2044         
2045         printf("unlink small file\n");
2046         yaffs_unlink(c);
2047         dump_directory_tree(mountpt);
2048                 
2049         printf("Umount/mount attempt\n");
2050         yaffs_unmount(mountpt);
2051         yaffs_mount(mountpt);
2052         
2053         for(j = 0; j < 500; j++){
2054                 printf("***** touch %d\n",j);
2055                 dump_directory_tree(mountpt);
2056                 yaffs_touch(b);
2057                 yaffs_unmount(mountpt);
2058                 yaffs_mount(mountpt);
2059         }
2060
2061         for(j = 0; j < 500; j++){
2062                 printf("***** touch %d\n",j);
2063                 dump_directory_tree(mountpt);
2064                 yaffs_touch(b);
2065                 yaffs_unmount(mountpt);
2066                 yaffs_mount(mountpt);
2067         }
2068 }
2069         
2070 void huge_array_test(const char *mountpt,int n)
2071 {
2072
2073         char a[50];
2074
2075         
2076         int i;
2077         int j;
2078         int h;
2079         
2080         int fnum;
2081         
2082         sprintf(a,"mount point %s",mountpt);
2083         
2084
2085         
2086         yaffs_StartUp();
2087
2088         yaffs_mount(mountpt);
2089         
2090         while(n>0){
2091                 n--;
2092                 fnum = 0;
2093                 printf("\n\n START run\n\n");
2094                 while(yaffs_freespace(mountpt) > 25000000){
2095                         sprintf(a,"%s/file%d",mountpt,fnum);
2096                         fnum++;
2097                         printf("create file %s\n",a);
2098                         create_file_of_size(a,10000000);
2099                         printf("verifying file %s\n",a);
2100                         verify_file_of_size(a,10000000);
2101                 }
2102                 
2103                 printf("\n\n verification/deletion\n\n");
2104                 
2105                 for(i = 0; i < fnum; i++){
2106                         sprintf(a,"%s/file%d",mountpt,i);
2107                         printf("verifying file %s\n",a);
2108                         verify_file_of_size(a,10000000);
2109                         printf("deleting file %s\n",a);
2110                         yaffs_unlink(a);
2111                 }
2112                 printf("\n\n done \n\n");
2113                         
2114                    
2115         }
2116 }
2117         
2118
2119
2120 int main(int argc, char *argv[])
2121 {
2122         //return long_test(argc,argv);
2123         
2124         //return cache_read_test();
2125         
2126         //resize_stress_test_no_grow("/flash",20);
2127         
2128         //huge_directory_test_on_path("/ram2k");
2129         
2130          //yaffs_backward_scan_test("/flash/flash");
2131         // yaffs_device_flush_test("/flash/flash");
2132
2133          
2134          //scan_pattern_test("/flash",10000,10);
2135         //short_scan_test("/flash/flash",40000,200);
2136           //small_mount_test("/flash/flash",1000);
2137           small_overwrite_test("/flash/flash",1000);
2138          //checkpoint_fill_test("/flash/flash",20);
2139          //checkpoint_upgrade_test("/flash/flash",20);
2140          // huge_array_test("/flash/flash",10);
2141
2142
2143
2144         
2145         //long_test_on_path("/ram2k");
2146         // long_test_on_path("/flash");
2147         //simple_rw_test("/flash/flash");
2148         //fill_disk_test("/flash/flash");
2149         // rename_over_test("/flash");
2150         //lookup_test("/flash");
2151         //freespace_test("/flash/flash");
2152         
2153         //link_test("/flash/flash");
2154         
2155         
2156         
2157         
2158         // cache_bypass_bug_test();
2159         
2160          //free_space_check();
2161          
2162          //check_resize_gc_bug("/flash");
2163          
2164          return 0;
2165         
2166 }