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