8100406266e582141b5834377d78c28d94d933bc
[yaffs2.git] / direct / dtest.c
1 /*
2 * Test code for the "direct" interface. 
3 */
4
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10
11 #include "yaffsfs.h"
12
13 void dumpDir(const char *dname);
14
15 char xx[600];
16
17 void copy_in_a_file(char *yaffsName,char *inName)
18 {
19         int inh,outh;
20         unsigned char buffer[100];
21         int ni,no;
22         inh = open(inName,O_RDONLY);
23         outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
24         
25         while((ni = read(inh,buffer,100)) > 0)
26         {
27                 no = yaffs_write(outh,buffer,ni);
28                 if(ni != no)
29                 {
30                         printf("problem writing yaffs file\n");
31                 }
32                 
33         }
34         
35         yaffs_close(outh);
36         close(inh);
37 }
38
39 void make_a_file(char *yaffsName,char bval,int sizeOfFile)
40 {
41         int outh;
42         int i;
43         unsigned char buffer[100];
44
45         outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
46         
47         memset(buffer,bval,100);
48         
49         do{
50                 i = sizeOfFile;
51                 if(i > 100) i = 100;
52                 sizeOfFile -= i;
53                 
54                 yaffs_write(outh,buffer,i);
55                 
56         } while (sizeOfFile > 0);
57         
58                 
59         yaffs_close(outh);
60
61 }
62
63 void make_pattern_file(char *fn,int size)
64 {
65         int outh;
66         int marker;
67         int i;
68         outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
69         yaffs_lseek(outh,size-1,SEEK_SET);
70         yaffs_write(outh,"A",1);
71         
72         for(i = 0; i < size; i+=256)
73         {
74                 marker = ~i;
75                 yaffs_lseek(outh,i,SEEK_SET);
76                 yaffs_write(outh,&marker,sizeof(marker));
77         }
78         yaffs_close(outh);
79         
80 }
81
82 int check_pattern_file(char *fn)
83 {
84         int h;
85         int marker;
86         int i;
87         int size;
88         int ok = 1;
89         
90         h = yaffs_open(fn, O_RDWR,0);
91         size = yaffs_lseek(h,0,SEEK_END);
92                 
93         for(i = 0; i < size; i+=256)
94         {
95                 yaffs_lseek(h,i,SEEK_SET);
96                 yaffs_read(h,&marker,sizeof(marker));
97                 ok = (marker == ~i);
98                 if(!ok)
99                 {
100                    printf("pattern check failed on file %s, size %d at position %d. Got %x instead of %x\n",
101                                         fn,size,i,marker,~i);
102                 }
103         }
104         yaffs_close(h);
105         return ok;
106 }
107
108
109
110
111
112 int dump_file_data(char *fn)
113 {
114         int h;
115         int marker;
116         int i = 0;
117         int size;
118         int ok = 1;
119         unsigned char b;
120         
121         h = yaffs_open(fn, O_RDWR,0);
122
123                                 
124         printf("%s\n",fn);
125         while(yaffs_read(h,&b,1)> 0)
126         {
127                 printf("%02x",b);
128                 i++;
129                 if(i > 32) 
130                 {
131                    printf("\n");
132                    i = 0;;
133                  }
134         }
135         printf("\n");
136         yaffs_close(h);
137         return ok;
138 }
139
140
141
142 void dump_file(const char *fn)
143 {
144         int i;
145         int size;
146         int h;
147         
148         h = yaffs_open(fn,O_RDONLY,0);
149         if(h < 0)
150         {
151                 printf("*****\nDump file %s does not exist\n",fn);
152         }
153         else
154         {
155                 size = yaffs_lseek(h,0,SEEK_SET);
156                 printf("*****\nDump file %s size %d\n",fn,size);
157                 for(i = 0; i < size; i++)
158                 {
159                         
160                 }
161         }
162 }
163
164 void create_file_of_size(const char *fn,int syze)
165 {
166         int h;
167         int n;
168         
169         
170         int iterations = (syze + strlen(fn) -1)/ strlen(fn);
171         
172         h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
173                 
174         while (iterations > 0)
175         {
176                 yaffs_write(h,fn,strlen(fn));
177                 iterations--;
178         }
179         yaffs_close (h);
180 }
181
182 void create_resized_file_of_size(const char *fn,int syze1,int reSyze, int syze2)
183 {
184         int h;
185         int n;
186         
187         
188         int iterations;
189         
190         h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
191                 
192         iterations = (syze1 + strlen(fn) -1)/ strlen(fn);
193         while (iterations > 0)
194         {
195                 yaffs_write(h,fn,strlen(fn));
196                 iterations--;
197         }
198         
199         yaffs_truncate(h,reSyze);
200         
201         yaffs_lseek(h,0,SEEK_SET);
202         iterations = (syze2 + strlen(fn) -1)/ strlen(fn);
203         while (iterations > 0)
204         {
205                 yaffs_write(h,fn,strlen(fn));
206                 iterations--;
207         }
208         
209         yaffs_close (h);
210 }
211
212
213 void do_some_file_stuff(const char *path)
214 {
215
216         char fn[100];
217
218         sprintf(fn,"%s/%s",path,"f1");
219         create_file_of_size(fn,10000);
220
221         sprintf(fn,"%s/%s",path,"fdel");
222         create_file_of_size(fn,10000);
223         yaffs_unlink(fn);
224
225         sprintf(fn,"%s/%s",path,"f2");
226         
227         create_resized_file_of_size(fn,10000,3000,4000);
228 }
229
230 void yaffs_backward_scan_test(const char *path)
231 {
232         char fn[100];
233         
234         yaffs_StartUp();        
235         
236         yaffs_mount(path);
237         
238         do_some_file_stuff(path);
239         
240         sprintf(fn,"%s/ddd",path);
241         
242         yaffs_mkdir(fn,0);
243         
244         do_some_file_stuff(fn);
245         
246         yaffs_unmount(path);
247         
248         yaffs_mount(path);
249 }
250
251
252
253 void short_scan_test(const char *path, int fsize, int niterations)
254 {
255         int i;
256         char fn[100];
257         
258         sprintf(fn,"%s/%s",path,"f1");
259         
260         yaffs_StartUp();
261         for(i = 0; i < niterations; i++)
262         {
263                 printf("\n*****************\nIteration %d\n",i);
264                 yaffs_mount(path);
265                 printf("\nmount: Directory look-up of %s\n",path);
266                 dumpDir(path);
267                 make_a_file(fn,1,fsize);
268                 yaffs_unmount(path);
269         }
270 }
271
272
273
274 void scan_pattern_test(const char *path, int fsize, int niterations)
275 {
276         int i;
277         int j;
278         char fn[3][100];
279         int result;
280         
281         sprintf(fn[0],"%s/%s",path,"f0");
282         sprintf(fn[1],"%s/%s",path,"f1");
283         sprintf(fn[2],"%s/%s",path,"f2");
284         
285         yaffs_StartUp();
286         
287         for(i = 0; i < niterations; i++)
288         {
289                 printf("\n*****************\nIteration %d\n",i);
290                 yaffs_mount(path);
291                 printf("\nmount: Directory look-up of %s\n",path);
292                 dumpDir(path);
293                 for(j = 0; j < 3; j++)
294                 {
295                         result = dump_file_data(fn[j]);
296                         result = check_pattern_file(fn[j]);
297                         make_pattern_file(fn[j],fsize); 
298                         result = dump_file_data(fn[j]);
299                         result = check_pattern_file(fn[j]);
300                 }
301                 yaffs_unmount(path);
302         }
303 }
304
305 void fill_disk(char *path,int nfiles)
306 {
307         int h;
308         int n;
309         int result;
310         int f;
311         
312         char str[50];
313         
314         for(n = 0; n < nfiles; n++)
315         {
316                 sprintf(str,"%s/%d",path,n);
317                 
318                 h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
319                 
320                 printf("writing file %s handle %d ",str, h);
321                 
322                 while ((result = yaffs_write(h,xx,600)) == 600)
323                 {
324                         f = yaffs_freespace(path);
325                 }
326                 result = yaffs_close(h);
327                 printf(" close %d\n",result);
328         }
329 }
330
331 void fill_disk_and_delete(char *path, int nfiles, int ncycles)
332 {
333         int i,j;
334         char str[50];
335         int result;
336         
337         for(i = 0; i < ncycles; i++)
338         {
339                 printf("@@@@@@@@@@@@@@ cycle %d\n",i);
340                 fill_disk(path,nfiles);
341                 
342                 for(j = 0; j < nfiles; j++)
343                 {
344                         sprintf(str,"%s/%d",path,j);
345                         result = yaffs_unlink(str);
346                         printf("unlinking file %s, result %d\n",str,result);
347                 }
348         }
349 }
350
351
352 void fill_files(char *path,int flags, int maxIterations,int siz)
353 {
354         int i;
355         int j;
356         char str[50];
357         int h;
358         
359         i = 0;
360         
361         do{
362                 sprintf(str,"%s/%d",path,i);
363                 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
364                 yaffs_close(h);
365
366                 if(h >= 0)
367                 {
368                         for(j = 0; j < siz; j++)
369                         {
370                                 yaffs_write(h,str,1);
371                         }
372                 }
373                 
374                 if( flags & 1)
375                 {
376                         yaffs_unlink(str);
377                 }
378                 i++;
379         } while(h >= 0 && i < maxIterations);
380         
381         if(flags & 2)
382         {
383                 i = 0;
384                 do{
385                         sprintf(str,"%s/%d",path,i);
386                         printf("unlink %s\n",str);
387                         i++;
388                 } while(yaffs_unlink(str) >= 0);
389         }
390 }
391
392 void leave_unlinked_file(char *path,int maxIterations,int siz)
393 {
394         int i;
395         char str[50];
396         int h;
397         
398         i = 0;
399         
400         do{
401                 sprintf(str,"%s/%d",path,i);
402                 printf("create %s\n",str);
403                 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
404                 if(h >= 0)
405                 {
406                         yaffs_unlink(str);
407                 }
408                 i++;
409         } while(h < 0 && i < maxIterations);
410         
411         if(h >= 0)
412         {
413                 for(i = 0; i < siz; i++)
414                 {
415                         yaffs_write(h,str,1);
416                 }
417         }
418         
419         printf("Leaving file %s open\n",str);
420
421 }
422
423 void dumpDirFollow(const char *dname)
424 {
425         yaffs_DIR *d;
426         yaffs_dirent *de;
427         struct yaffs_stat s;
428         char str[100];
429                         
430         d = yaffs_opendir(dname);
431         
432         if(!d)
433         {
434                 printf("opendir failed\n");
435         }
436         else
437         {
438                 while((de = yaffs_readdir(d)) != NULL)
439                 {
440                         sprintf(str,"%s/%s",dname,de->d_name);
441                         
442                         yaffs_stat(str,&s);
443                         
444                         printf("%s length %d mode %X ",de->d_name,(int)s.st_size,s.st_mode);
445                         switch(s.st_mode & S_IFMT)
446                         {
447                                 case S_IFREG: printf("data file"); break;
448                                 case S_IFDIR: printf("directory"); break;
449                                 case S_IFLNK: printf("symlink -->");
450                                                           if(yaffs_readlink(str,str,100) < 0)
451                                                                 printf("no alias");
452                                                           else
453                                                                 printf("\"%s\"",str);    
454                                                           break;
455                                 default: printf("unknown"); break;
456                         }
457                         
458                         printf("\n");           
459                 }
460                 
461                 yaffs_closedir(d);
462         }
463         printf("\n");
464         
465         printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
466
467 }
468
469
470 void dumpDir(const char *dname)
471 {
472         yaffs_DIR *d;
473         yaffs_dirent *de;
474         struct yaffs_stat s;
475         char str[100];
476                         
477         d = yaffs_opendir(dname);
478         
479         if(!d)
480         {
481                 printf("opendir failed\n");
482         }
483         else
484         {
485                 while((de = yaffs_readdir(d)) != NULL)
486                 {
487                         sprintf(str,"%s/%s",dname,de->d_name);
488                         
489                         yaffs_lstat(str,&s);
490                         
491                         printf("%s length %d mode %X ",de->d_name,(int)s.st_size,s.st_mode);
492                         switch(s.st_mode & S_IFMT)
493                         {
494                                 case S_IFREG: printf("data file"); break;
495                                 case S_IFDIR: printf("directory"); break;
496                                 case S_IFLNK: printf("symlink -->");
497                                                           if(yaffs_readlink(str,str,100) < 0)
498                                                                 printf("no alias");
499                                                           else
500                                                                 printf("\"%s\"",str);    
501                                                           break;
502                                 default: printf("unknown"); break;
503                         }
504                         
505                         printf("\n");           
506                 }
507                 
508                 yaffs_closedir(d);
509         }
510         printf("\n");
511         
512         printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
513
514 }
515
516
517 static void PermissionsCheck(const char *path, mode_t tmode, int tflags,int expectedResult)
518 {
519         int fd;
520         
521         if(yaffs_chmod(path,tmode)< 0) printf("chmod failed\n");
522         
523         fd = yaffs_open(path,tflags,0);
524         
525         if((fd >= 0) != (expectedResult > 0))
526         {
527                 printf("Permissions check %x %x %d failed\n",tmode,tflags,expectedResult);
528         }
529         else
530         {
531                 printf("Permissions check %x %x %d OK\n",tmode,tflags,expectedResult);
532         }
533         
534         
535         yaffs_close(fd);
536         
537         
538 }
539
540 int long_test(int argc, char *argv[])
541 {
542
543         int f;
544         int r;
545         char buffer[20];
546         
547         char str[100];
548         
549         int h;
550         mode_t temp_mode;
551         struct yaffs_stat ystat;
552         
553         yaffs_StartUp();
554         
555         yaffs_mount("/boot");
556         yaffs_mount("/data");
557         yaffs_mount("/flash");
558         yaffs_mount("/ram");
559         
560         printf("\nDirectory look-up of /boot\n");
561         dumpDir("/boot");
562         printf("\nDirectory look-up of /data\n");
563         dumpDir("/data");
564         printf("\nDirectory look-up of /flash\n");
565         dumpDir("/flash");
566
567         //leave_unlinked_file("/flash",20000,0);
568         //leave_unlinked_file("/data",20000,0);
569         
570         leave_unlinked_file("/ram",20,0);
571         
572
573         f = yaffs_open("/boot/b1", O_RDONLY,0);
574         
575         printf("open /boot/b1 readonly, f=%d\n",f);
576         
577         f = yaffs_open("/boot/b1", O_CREAT,S_IREAD | S_IWRITE);
578         
579         printf("open /boot/b1 O_CREAT, f=%d\n",f);
580         
581         
582         r = yaffs_write(f,"hello",1);
583         printf("write %d attempted to write to a read-only file\n",r);
584         
585         r = yaffs_close(f);
586         
587         printf("close %d\n",r);
588
589         f = yaffs_open("/boot/b1", O_RDWR,0);
590         
591         printf("open /boot/b1 O_RDWR,f=%d\n",f);
592         
593         
594         r = yaffs_write(f,"hello",2);
595         printf("write %d attempted to write to a writeable file\n",r);
596         r = yaffs_write(f,"world",3);
597         printf("write %d attempted to write to a writeable file\n",r);
598         
599         r= yaffs_lseek(f,0,SEEK_END);
600         printf("seek end %d\n",r);
601         memset(buffer,0,20);
602         r = yaffs_read(f,buffer,10);
603         printf("read %d \"%s\"\n",r,buffer);
604         r= yaffs_lseek(f,0,SEEK_SET);
605         printf("seek set %d\n",r);
606         memset(buffer,0,20);
607         r = yaffs_read(f,buffer,10);
608         printf("read %d \"%s\"\n",r,buffer);
609         memset(buffer,0,20);
610         r = yaffs_read(f,buffer,10);
611         printf("read %d \"%s\"\n",r,buffer);
612
613         // Check values reading at end.
614         // A read past end of file should return 0 for 0 bytes read.
615                 
616         r= yaffs_lseek(f,0,SEEK_END);
617         r = yaffs_read(f,buffer,10);
618         printf("read at end returned  %d\n",r); 
619         r= yaffs_lseek(f,500,SEEK_END);
620         r = yaffs_read(f,buffer,10);
621         printf("read past end returned  %d\n",r);       
622         
623         r = yaffs_close(f);
624         
625         printf("close %d\n",r);
626         
627         copy_in_a_file("/boot/yyfile","xxx");
628         
629         // Create a file with a long name
630         
631         copy_in_a_file("/boot/file with a long name","xxx");
632         
633         
634         printf("\nDirectory look-up of /boot\n");
635         dumpDir("/boot");
636
637         // Check stat
638         r = yaffs_stat("/boot/file with a long name",&ystat);
639         
640         // Check rename
641         
642         r = yaffs_rename("/boot/file with a long name","/boot/r1");
643         
644         printf("\nDirectory look-up of /boot\n");
645         dumpDir("/boot");
646         
647         // Check unlink
648         r = yaffs_unlink("/boot/r1");
649         
650         printf("\nDirectory look-up of /boot\n");
651         dumpDir("/boot");
652
653         // Check mkdir
654         
655         r = yaffs_mkdir("/boot/directory1",0);
656         
657         printf("\nDirectory look-up of /boot\n");
658         dumpDir("/boot");
659         printf("\nDirectory look-up of /boot/directory1\n");
660         dumpDir("/boot/directory1");
661
662         // add a file to the directory                  
663         copy_in_a_file("/boot/directory1/file with a long name","xxx");
664         
665         printf("\nDirectory look-up of /boot\n");
666         dumpDir("/boot");
667         printf("\nDirectory look-up of /boot/directory1\n");
668         dumpDir("/boot/directory1");
669         
670         //  Attempt to delete directory (should fail)
671         
672         r = yaffs_rmdir("/boot/directory1");
673         
674         printf("\nDirectory look-up of /boot\n");
675         dumpDir("/boot");
676         printf("\nDirectory look-up of /boot/directory1\n");
677         dumpDir("/boot/directory1");
678         
679         // Delete file first, then rmdir should work
680         r = yaffs_unlink("/boot/directory1/file with a long name");
681         r = yaffs_rmdir("/boot/directory1");
682         
683         
684         printf("\nDirectory look-up of /boot\n");
685         dumpDir("/boot");
686         printf("\nDirectory look-up of /boot/directory1\n");
687         dumpDir("/boot/directory1");
688
689 #if 0
690         fill_disk_and_delete("/boot",20,20);
691                         
692         printf("\nDirectory look-up of /boot\n");
693         dumpDir("/boot");
694 #endif
695
696         yaffs_symlink("yyfile","/boot/slink");
697         
698         yaffs_readlink("/boot/slink",str,100);
699         printf("symlink alias is %s\n",str);
700         
701         
702         
703         
704         printf("\nDirectory look-up of /boot\n");
705         dumpDir("/boot");
706         printf("\nDirectory look-up of /boot (using stat instead of lstat)\n");
707         dumpDirFollow("/boot");
708         printf("\nDirectory look-up of /boot/directory1\n");
709         dumpDir("/boot/directory1");
710
711         h = yaffs_open("/boot/slink",O_RDWR,0);
712         
713         printf("file length is %d\n",(int)yaffs_lseek(h,0,SEEK_END));
714         
715         yaffs_close(h);
716         
717         yaffs_unlink("/boot/slink");
718
719         
720         printf("\nDirectory look-up of /boot\n");
721         dumpDir("/boot");
722         
723         // Check chmod
724         
725         yaffs_stat("/boot/yyfile",&ystat);
726         temp_mode = ystat.st_mode;
727         
728         yaffs_chmod("/boot/yyfile",0x55555);
729         printf("\nDirectory look-up of /boot\n");
730         dumpDir("/boot");
731         
732         yaffs_chmod("/boot/yyfile",temp_mode);
733         printf("\nDirectory look-up of /boot\n");
734         dumpDir("/boot");
735         
736         // Permission checks...
737         PermissionsCheck("/boot/yyfile",0, O_WRONLY,0);
738         PermissionsCheck("/boot/yyfile",0, O_RDONLY,0);
739         PermissionsCheck("/boot/yyfile",0, O_RDWR,0);
740
741         PermissionsCheck("/boot/yyfile",S_IREAD, O_WRONLY,0);
742         PermissionsCheck("/boot/yyfile",S_IREAD, O_RDONLY,1);
743         PermissionsCheck("/boot/yyfile",S_IREAD, O_RDWR,0);
744
745         PermissionsCheck("/boot/yyfile",S_IWRITE, O_WRONLY,1);
746         PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDONLY,0);
747         PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDWR,0);
748         
749         PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_WRONLY,1);
750         PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDONLY,1);
751         PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDWR,1);
752
753         yaffs_chmod("/boot/yyfile",temp_mode);
754         
755         //create a zero-length file and unlink it (test for scan bug)
756         
757         h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
758         yaffs_close(h);
759         
760         yaffs_unlink("/boot/zlf");
761         
762         
763         yaffs_DumpDevStruct("/boot");
764         
765         fill_disk_and_delete("/boot",20,20);
766         
767         yaffs_DumpDevStruct("/boot");
768         
769         fill_files("/boot",1,10000,0);
770         fill_files("/boot",1,10000,5000);
771         fill_files("/boot",2,10000,0);
772         fill_files("/boot",2,10000,5000);
773         
774         leave_unlinked_file("/data",20000,0);
775         leave_unlinked_file("/data",20000,5000);
776         leave_unlinked_file("/data",20000,5000);
777         leave_unlinked_file("/data",20000,5000);
778         leave_unlinked_file("/data",20000,5000);
779         leave_unlinked_file("/data",20000,5000);
780         
781         yaffs_DumpDevStruct("/boot");
782         yaffs_DumpDevStruct("/data");
783         
784                 
785                 
786         return 0;
787
788 }
789
790 int huge_directory_test_on_path(char *path)
791 {
792
793         yaffs_DIR *d;
794         yaffs_dirent *de;
795         struct yaffs_stat s;
796
797         int f;
798         int i;
799         int r;
800         int total = 0;
801         int lastTotal = 0;
802         char buffer[20];
803         
804         char str[100];
805         char name[100];
806         char name2[100];
807         
808         int h;
809         mode_t temp_mode;
810         struct yaffs_stat ystat;
811         
812         yaffs_StartUp();
813         
814         yaffs_mount(path);
815         
816         // Create a large number of files
817         
818         for(i = 0; i < 2000; i++)
819         {
820           sprintf(str,"%s/%d",path,i);
821           
822            f = yaffs_open(str,O_CREAT,S_IREAD | S_IWRITE);
823            yaffs_close(f);
824         }
825         
826         
827         
828         d = yaffs_opendir(path);
829         i = 0;
830         if (d) {
831         while((de = yaffs_readdir(d)) != NULL) {
832         if (total >lastTotal+100*9*1024||(i & 1023)==0){
833         printf("files = %d, total = %d\n",i, total);
834         lastTotal = total;
835         }
836                 i++;
837                 sprintf(str,"%s/%s",path,de->d_name);
838                 yaffs_lstat(str,&s);
839                 switch(s.st_mode & S_IFMT){
840                 case S_IFREG:
841         //printf("data file");
842         total += s.st_size;
843         break;
844         }
845         }
846         
847         yaffs_closedir(d);
848         }
849         
850         return 0;
851 }
852
853 int yaffs_scan_test(const char *path)
854 {
855 }
856
857
858
859 int resize_stress_test(const char *path)
860 {
861    int a,b,i,j;
862    int x;
863    int r;
864    char aname[100];
865    char bname[100];
866    
867    char abuffer[1000];
868    char bbuffer[1000];
869    
870    yaffs_StartUp();
871    
872    yaffs_mount(path);
873    
874    sprintf(aname,"%s%s",path,"/a");
875    sprintf(bname,"%s%s",path,"/b");
876    
877    memset(abuffer,'a',1000);
878    memset(bbuffer,'b',1000);
879    
880    a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
881    b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
882    
883    printf(" %s %d %s %d\n",aname,a,bname,b);
884   
885    x = 0;
886    
887    for(j = 0; j < 100; j++)
888    {
889                 yaffs_lseek(a,0,SEEK_END);
890
891                 
892                 for(i = 0; i <20000; i++)
893                 {
894                    //r =        yaffs_lseek(b,i,SEEK_SET);
895                         //r = yaffs_write(b,bbuffer,1000);
896                         
897                         if(x & 0x16)
898                         {
899                                 // shrink
900                                 int syz = yaffs_lseek(a,0,SEEK_END);
901                                 
902                                 syz -= 500;
903                                 if(syz < 0) syz = 0;
904                                 yaffs_truncate(a,syz);
905                                 
906                         }
907                         else
908                         {
909                                 //expand
910                                 r = yaffs_lseek(a,i * 500,SEEK_SET);
911                                 r = yaffs_write(a,abuffer,1000);
912                         }
913                         x++;
914                         
915                 }
916    }
917    
918    return 0;
919    
920 }
921
922
923 int resize_stress_test_no_grow_complex(const char *path,int iters)
924 {
925    int a,b,i,j;
926    int x;
927    int r;
928    char aname[100];
929    char bname[100];
930    
931    char abuffer[1000];
932    char bbuffer[1000];
933    
934    yaffs_StartUp();
935    
936    yaffs_mount(path);
937    
938    sprintf(aname,"%s%s",path,"/a");
939    sprintf(bname,"%s%s",path,"/b");
940    
941    memset(abuffer,'a',1000);
942    memset(bbuffer,'b',1000);
943    
944    a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
945    b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
946    
947    printf(" %s %d %s %d\n",aname,a,bname,b);
948   
949    x = 0;
950    
951    for(j = 0; j < iters; j++)
952    {
953                 yaffs_lseek(a,0,SEEK_END);
954
955                 
956                 for(i = 0; i <20000; i++)
957                 {
958                    //r =        yaffs_lseek(b,i,SEEK_SET);
959                         //r = yaffs_write(b,bbuffer,1000);
960                         
961                         if(!(x%20))
962                         {
963                                 // shrink
964                                 int syz = yaffs_lseek(a,0,SEEK_END);
965                                 
966                                 while(syz > 4000)
967                                 {
968                                 
969                                         syz -= 2050;
970                                         if(syz < 0) syz = 0;
971                                         yaffs_truncate(a,syz);
972                                         syz = yaffs_lseek(a,0,SEEK_END);
973                                         printf("shrink to %d\n",syz);
974                                 }
975                                 
976                                 
977                         }
978                         else
979                         {
980                                 //expand
981                                 r = yaffs_lseek(a,500,SEEK_END);
982                                 r = yaffs_write(a,abuffer,1000);
983                         }
984                         x++;
985                         
986                                         
987                 }
988                 printf("file size is %d\n",yaffs_lseek(a,0,SEEK_END));
989
990    }
991    
992    return 0;
993    
994 }
995
996 int resize_stress_test_no_grow(const char *path,int iters)
997 {
998    int a,b,i,j;
999    int x;
1000    int r;
1001    char aname[100];
1002    char bname[100];
1003    
1004    char abuffer[1000];
1005    char bbuffer[1000];
1006    
1007    yaffs_StartUp();
1008    
1009    yaffs_mount(path);
1010    
1011    sprintf(aname,"%s%s",path,"/a");
1012    sprintf(bname,"%s%s",path,"/b");
1013    
1014    memset(abuffer,'a',1000);
1015    memset(bbuffer,'b',1000);
1016    
1017    a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1018    b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1019    
1020    printf(" %s %d %s %d\n",aname,a,bname,b);
1021   
1022    x = 0;
1023    
1024    for(j = 0; j < iters; j++)
1025    {
1026                 yaffs_lseek(a,0,SEEK_END);
1027
1028                 
1029                 for(i = 0; i <20000; i++)
1030                 {
1031                    //r =        yaffs_lseek(b,i,SEEK_SET);
1032                         //r = yaffs_write(b,bbuffer,1000);
1033                         
1034                         if(!(x%20))
1035                         {
1036                                 // shrink
1037                                 int syz = yaffs_lseek(a,0,SEEK_END);
1038                                 
1039                                 while(syz > 4000)
1040                                 {
1041                                 
1042                                         syz -= 2050;
1043                                         if(syz < 0) syz = 0;
1044                                         yaffs_truncate(a,syz);
1045                                         syz = yaffs_lseek(a,0,SEEK_END);
1046                                         printf("shrink to %d\n",syz);
1047                                 }
1048                                 
1049                                 
1050                         }
1051                         else
1052                         {
1053                                 //expand
1054                                 r = yaffs_lseek(a,-500,SEEK_END);
1055                                 r = yaffs_write(a,abuffer,1000);
1056                         }
1057                         x++;
1058                         
1059                                         
1060                 }
1061                 printf("file size is %d\n",yaffs_lseek(a,0,SEEK_END));
1062
1063    }
1064    
1065    return 0;
1066    
1067 }
1068
1069 int directory_rename_test(void)
1070 {
1071         int r;
1072         yaffs_StartUp();
1073         
1074         yaffs_mount("/ram");
1075         yaffs_mkdir("/ram/a",0);
1076         yaffs_mkdir("/ram/a/b",0);
1077         yaffs_mkdir("/ram/c",0);
1078         
1079         printf("\nDirectory look-up of /ram\n");
1080         dumpDir("/ram");
1081         dumpDir("/ram/a");
1082         dumpDir("/ram/a/b");
1083
1084         printf("Do rename (should fail)\n");
1085                 
1086         r = yaffs_rename("/ram/a","/ram/a/b/d");
1087         printf("\nDirectory look-up of /ram\n");
1088         dumpDir("/ram");
1089         dumpDir("/ram/a");
1090         dumpDir("/ram/a/b");
1091
1092         printf("Do rename (should not fail)\n");
1093                 
1094         r = yaffs_rename("/ram/c","/ram/a/b/d");
1095         printf("\nDirectory look-up of /ram\n");
1096         dumpDir("/ram");
1097         dumpDir("/ram/a");
1098         dumpDir("/ram/a/b");
1099         
1100         
1101         return 1;
1102         
1103 }
1104
1105 int cache_read_test(void)
1106 {
1107         int a,b,c;
1108         int i;
1109         int sizeOfFiles = 500000;
1110         char buffer[100];
1111         
1112         yaffs_StartUp();
1113         
1114         yaffs_mount("/boot");
1115         
1116         make_a_file("/boot/a",'a',sizeOfFiles);
1117         make_a_file("/boot/b",'b',sizeOfFiles);
1118
1119         a = yaffs_open("/boot/a",O_RDONLY,0);
1120         b = yaffs_open("/boot/b",O_RDONLY,0);
1121         c = yaffs_open("/boot/c", O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
1122
1123         do{
1124                 i = sizeOfFiles;
1125                 if (i > 100) i = 100;
1126                 sizeOfFiles  -= i;
1127                 yaffs_read(a,buffer,i);
1128                 yaffs_read(b,buffer,i);
1129                 yaffs_write(c,buffer,i);
1130         } while(sizeOfFiles > 0);
1131         
1132         
1133         
1134         return 1;
1135         
1136 }
1137
1138 int cache_bypass_bug_test(void)
1139 {
1140         // This test reporoduces a bug whereby YAFFS caching *was* buypassed
1141         // resulting in erroneous reads after writes.
1142         // This bug has been fixed.
1143         
1144         int a;
1145         int i;
1146         char buffer1[1000];
1147         char buffer2[1000];
1148         
1149         memset(buffer1,0,sizeof(buffer1));
1150         memset(buffer2,0,sizeof(buffer2));
1151                 
1152         yaffs_StartUp();
1153         
1154         yaffs_mount("/boot");
1155         
1156         // Create a file of 2000 bytes.
1157         make_a_file("/boot/a",'X',2000);
1158
1159         a = yaffs_open("/boot/a",O_RDWR, S_IREAD | S_IWRITE);
1160         
1161         // Write a short sequence to the file.
1162         // This will go into the cache.
1163         yaffs_lseek(a,0,SEEK_SET);
1164         yaffs_write(a,"abcdefghijklmnopqrstuvwxyz",20); 
1165
1166         // Read a short sequence from the file.
1167         // This will come from the cache.
1168         yaffs_lseek(a,0,SEEK_SET);
1169         yaffs_read(a,buffer1,30); 
1170
1171         // Read a page size sequence from the file.
1172         yaffs_lseek(a,0,SEEK_SET);
1173         yaffs_read(a,buffer2,512); 
1174         
1175         printf("buffer 1 %s\n",buffer1);
1176         printf("buffer 2 %s\n",buffer2);
1177         
1178         if(strncmp(buffer1,buffer2,20))
1179         {
1180                 printf("Cache bypass bug detected!!!!!\n");
1181         }
1182         
1183         
1184         return 1;
1185 }
1186
1187
1188 int free_space_check(void)
1189 {
1190         int f;
1191         
1192                 yaffs_StartUp();
1193                 yaffs_mount("/boot");
1194             fill_disk("/boot/",2);
1195             f = yaffs_freespace("/boot");
1196             
1197             printf("%d free when disk full\n",f);           
1198             return 1;
1199 }
1200
1201 int truncate_test(void)
1202 {
1203         int a;
1204         int r;
1205         int i;
1206         int l;
1207
1208         char y[10];
1209
1210         yaffs_StartUp();
1211         yaffs_mount("/boot");
1212
1213         yaffs_unlink("/boot/trunctest");
1214         
1215         a = yaffs_open("/boot/trunctest", O_CREAT | O_TRUNC | O_RDWR,  S_IREAD | S_IWRITE);
1216         
1217         yaffs_write(a,"abcdefghijklmnopqrstuvwzyz",26);
1218         
1219         yaffs_truncate(a,3);
1220         l= yaffs_lseek(a,0,SEEK_END);
1221         
1222         printf("truncated length is %d\n",l);
1223
1224         yaffs_lseek(a,5,SEEK_SET);
1225         yaffs_write(a,"1",1);
1226
1227         yaffs_lseek(a,0,SEEK_SET);
1228         
1229         r = yaffs_read(a,y,10);
1230
1231         printf("read %d bytes:",r);
1232
1233         for(i = 0; i < r; i++) printf("[%02X]",y[i]);
1234
1235         printf("\n");
1236
1237         return 0;
1238
1239 }
1240
1241
1242
1243
1244
1245 void fill_disk_test(const char *mountpt)
1246 {
1247         int i;
1248         yaffs_StartUp();
1249         
1250         for(i = 0; i < 5; i++)
1251         {
1252                 yaffs_mount(mountpt);
1253                 fill_disk_and_delete(mountpt,100,i+1);
1254                 yaffs_unmount(mountpt);
1255         }
1256         
1257 }
1258 void scan_deleted_files_test(const char *mountpt)
1259 {
1260         char fn[100];
1261         char sub[100];
1262         
1263         const char *p;
1264         
1265         int i;
1266         int j;
1267         int k;
1268         int h;
1269         
1270         sprintf(sub,"%s/sdir",mountpt);
1271         yaffs_StartUp();
1272         
1273         for(j = 0; j < 10; j++)
1274         {
1275                 printf("\n\n>>>>>>> Run %d <<<<<<<<<<<<<\n\n",j);
1276                 yaffs_mount(mountpt);
1277                 yaffs_mkdir(sub,0);
1278                 
1279                 
1280                 p = (j & 0) ? mountpt: sub;
1281         
1282                 for(i = 0; i < 100; i++)
1283                 {
1284                   sprintf(fn,"%s/%d",p,i);  
1285                   
1286                   if(i & 1)
1287                   {
1288                           h = yaffs_open(fn,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1289                           for(k = 0; k < 1000; k++)
1290                                   yaffs_write(h,fn,100);
1291                           yaffs_close(h);
1292                   }
1293                   else
1294                         yaffs_mkdir(fn,0);
1295                 }
1296                 
1297                 for(i = 0; i < 10; i++)
1298                 {
1299                   sprintf(fn,"%s/%d",p,i);  
1300                   if(i & 1) 
1301                         yaffs_unlink(fn);
1302                   else
1303                         yaffs_rmdir(fn);
1304                   
1305                 }
1306                                 
1307                 yaffs_unmount(mountpt);
1308         }
1309         
1310         
1311         
1312
1313 }
1314
1315 int main(int argc, char *argv[])
1316 {
1317         //return long_test(argc,argv);
1318         
1319         //return cache_read_test();
1320         
1321         //resize_stress_test_no_grow("/flash",2);
1322         
1323         //huge_directory_test_on_path("/ram2k");
1324         
1325          //yaffs_backward_scan_test("/flash")   ;
1326          
1327          //scan_pattern_test("/flash",10000,10);
1328         //short_scan_test("/flash",40000,200);
1329
1330         
1331         //long_test_on_path("/ram2k");
1332         //long_test_on_path("/flash");
1333         fill_disk_test("/flash");
1334         
1335         
1336         
1337         // cache_bypass_bug_test();
1338         
1339          //free_space_check();
1340          
1341          return 0;
1342         
1343 }