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