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