Fix kernel compilation probs
[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 void rename_over_test(const char *mountpt)
859 {
860         int i;
861         char a[100];
862         char b[100];
863         
864         sprintf(a,"%s/a",mountpt);
865         sprintf(b,"%s/b",mountpt);
866         
867         yaffs_StartUp();
868         
869         yaffs_mount(mountpt);
870         i = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, 0); 
871         yaffs_close(i);
872         i = yaffs_open(b,O_CREAT | O_TRUNC | O_RDWR, 0);
873         yaffs_close(i);
874         yaffs_rename(a,b); // rename over
875         yaffs_rename(b,a); // rename back again (not renaimng over)
876         yaffs_rename(a,b); // rename back again (not renaimng over)
877         
878         
879         yaffs_unmount(mountpt);
880         
881 }
882
883 int resize_stress_test(const char *path)
884 {
885    int a,b,i,j;
886    int x;
887    int r;
888    char aname[100];
889    char bname[100];
890    
891    char abuffer[1000];
892    char bbuffer[1000];
893    
894    yaffs_StartUp();
895    
896    yaffs_mount(path);
897    
898    sprintf(aname,"%s%s",path,"/a");
899    sprintf(bname,"%s%s",path,"/b");
900    
901    memset(abuffer,'a',1000);
902    memset(bbuffer,'b',1000);
903    
904    a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
905    b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
906    
907    printf(" %s %d %s %d\n",aname,a,bname,b);
908   
909    x = 0;
910    
911    for(j = 0; j < 100; j++)
912    {
913                 yaffs_lseek(a,0,SEEK_END);
914
915                 
916                 for(i = 0; i <20000; i++)
917                 {
918                    //r =        yaffs_lseek(b,i,SEEK_SET);
919                         //r = yaffs_write(b,bbuffer,1000);
920                         
921                         if(x & 0x16)
922                         {
923                                 // shrink
924                                 int syz = yaffs_lseek(a,0,SEEK_END);
925                                 
926                                 syz -= 500;
927                                 if(syz < 0) syz = 0;
928                                 yaffs_truncate(a,syz);
929                                 
930                         }
931                         else
932                         {
933                                 //expand
934                                 r = yaffs_lseek(a,i * 500,SEEK_SET);
935                                 r = yaffs_write(a,abuffer,1000);
936                         }
937                         x++;
938                         
939                 }
940    }
941    
942    return 0;
943    
944 }
945
946
947 int resize_stress_test_no_grow_complex(const char *path,int iters)
948 {
949    int a,b,i,j;
950    int x;
951    int r;
952    char aname[100];
953    char bname[100];
954    
955    char abuffer[1000];
956    char bbuffer[1000];
957    
958    yaffs_StartUp();
959    
960    yaffs_mount(path);
961    
962    sprintf(aname,"%s%s",path,"/a");
963    sprintf(bname,"%s%s",path,"/b");
964    
965    memset(abuffer,'a',1000);
966    memset(bbuffer,'b',1000);
967    
968    a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
969    b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
970    
971    printf(" %s %d %s %d\n",aname,a,bname,b);
972   
973    x = 0;
974    
975    for(j = 0; j < iters; j++)
976    {
977                 yaffs_lseek(a,0,SEEK_END);
978
979                 
980                 for(i = 0; i <20000; i++)
981                 {
982                    //r =        yaffs_lseek(b,i,SEEK_SET);
983                         //r = yaffs_write(b,bbuffer,1000);
984                         
985                         if(!(x%20))
986                         {
987                                 // shrink
988                                 int syz = yaffs_lseek(a,0,SEEK_END);
989                                 
990                                 while(syz > 4000)
991                                 {
992                                 
993                                         syz -= 2050;
994                                         if(syz < 0) syz = 0;
995                                         yaffs_truncate(a,syz);
996                                         syz = yaffs_lseek(a,0,SEEK_END);
997                                         printf("shrink to %d\n",syz);
998                                 }
999                                 
1000                                 
1001                         }
1002                         else
1003                         {
1004                                 //expand
1005                                 r = yaffs_lseek(a,500,SEEK_END);
1006                                 r = yaffs_write(a,abuffer,1000);
1007                         }
1008                         x++;
1009                         
1010                                         
1011                 }
1012                 printf("file size is %d\n",yaffs_lseek(a,0,SEEK_END));
1013
1014    }
1015    
1016    return 0;
1017    
1018 }
1019
1020 int resize_stress_test_no_grow(const char *path,int iters)
1021 {
1022    int a,b,i,j;
1023    int x;
1024    int r;
1025    char aname[100];
1026    char bname[100];
1027    
1028    char abuffer[1000];
1029    char bbuffer[1000];
1030    
1031    yaffs_StartUp();
1032    
1033    yaffs_mount(path);
1034    
1035    sprintf(aname,"%s%s",path,"/a");
1036    sprintf(bname,"%s%s",path,"/b");
1037    
1038    memset(abuffer,'a',1000);
1039    memset(bbuffer,'b',1000);
1040    
1041    a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1042    b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1043    
1044    printf(" %s %d %s %d\n",aname,a,bname,b);
1045   
1046    x = 0;
1047    
1048    for(j = 0; j < iters; j++)
1049    {
1050                 yaffs_lseek(a,0,SEEK_END);
1051
1052                 
1053                 for(i = 0; i <20000; i++)
1054                 {
1055                    //r =        yaffs_lseek(b,i,SEEK_SET);
1056                         //r = yaffs_write(b,bbuffer,1000);
1057                         
1058                         if(!(x%20))
1059                         {
1060                                 // shrink
1061                                 int syz = yaffs_lseek(a,0,SEEK_END);
1062                                 
1063                                 while(syz > 4000)
1064                                 {
1065                                 
1066                                         syz -= 2050;
1067                                         if(syz < 0) syz = 0;
1068                                         yaffs_truncate(a,syz);
1069                                         syz = yaffs_lseek(a,0,SEEK_END);
1070                                         printf("shrink to %d\n",syz);
1071                                 }
1072                                 
1073                                 
1074                         }
1075                         else
1076                         {
1077                                 //expand
1078                                 r = yaffs_lseek(a,-500,SEEK_END);
1079                                 r = yaffs_write(a,abuffer,1000);
1080                         }
1081                         x++;
1082                         
1083                                         
1084                 }
1085                 printf("file size is %d\n",yaffs_lseek(a,0,SEEK_END));
1086
1087    }
1088    
1089    return 0;
1090    
1091 }
1092
1093 int directory_rename_test(void)
1094 {
1095         int r;
1096         yaffs_StartUp();
1097         
1098         yaffs_mount("/ram");
1099         yaffs_mkdir("/ram/a",0);
1100         yaffs_mkdir("/ram/a/b",0);
1101         yaffs_mkdir("/ram/c",0);
1102         
1103         printf("\nDirectory look-up of /ram\n");
1104         dumpDir("/ram");
1105         dumpDir("/ram/a");
1106         dumpDir("/ram/a/b");
1107
1108         printf("Do rename (should fail)\n");
1109                 
1110         r = yaffs_rename("/ram/a","/ram/a/b/d");
1111         printf("\nDirectory look-up of /ram\n");
1112         dumpDir("/ram");
1113         dumpDir("/ram/a");
1114         dumpDir("/ram/a/b");
1115
1116         printf("Do rename (should not fail)\n");
1117                 
1118         r = yaffs_rename("/ram/c","/ram/a/b/d");
1119         printf("\nDirectory look-up of /ram\n");
1120         dumpDir("/ram");
1121         dumpDir("/ram/a");
1122         dumpDir("/ram/a/b");
1123         
1124         
1125         return 1;
1126         
1127 }
1128
1129 int cache_read_test(void)
1130 {
1131         int a,b,c;
1132         int i;
1133         int sizeOfFiles = 500000;
1134         char buffer[100];
1135         
1136         yaffs_StartUp();
1137         
1138         yaffs_mount("/boot");
1139         
1140         make_a_file("/boot/a",'a',sizeOfFiles);
1141         make_a_file("/boot/b",'b',sizeOfFiles);
1142
1143         a = yaffs_open("/boot/a",O_RDONLY,0);
1144         b = yaffs_open("/boot/b",O_RDONLY,0);
1145         c = yaffs_open("/boot/c", O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
1146
1147         do{
1148                 i = sizeOfFiles;
1149                 if (i > 100) i = 100;
1150                 sizeOfFiles  -= i;
1151                 yaffs_read(a,buffer,i);
1152                 yaffs_read(b,buffer,i);
1153                 yaffs_write(c,buffer,i);
1154         } while(sizeOfFiles > 0);
1155         
1156         
1157         
1158         return 1;
1159         
1160 }
1161
1162 int cache_bypass_bug_test(void)
1163 {
1164         // This test reporoduces a bug whereby YAFFS caching *was* buypassed
1165         // resulting in erroneous reads after writes.
1166         // This bug has been fixed.
1167         
1168         int a;
1169         int i;
1170         char buffer1[1000];
1171         char buffer2[1000];
1172         
1173         memset(buffer1,0,sizeof(buffer1));
1174         memset(buffer2,0,sizeof(buffer2));
1175                 
1176         yaffs_StartUp();
1177         
1178         yaffs_mount("/boot");
1179         
1180         // Create a file of 2000 bytes.
1181         make_a_file("/boot/a",'X',2000);
1182
1183         a = yaffs_open("/boot/a",O_RDWR, S_IREAD | S_IWRITE);
1184         
1185         // Write a short sequence to the file.
1186         // This will go into the cache.
1187         yaffs_lseek(a,0,SEEK_SET);
1188         yaffs_write(a,"abcdefghijklmnopqrstuvwxyz",20); 
1189
1190         // Read a short sequence from the file.
1191         // This will come from the cache.
1192         yaffs_lseek(a,0,SEEK_SET);
1193         yaffs_read(a,buffer1,30); 
1194
1195         // Read a page size sequence from the file.
1196         yaffs_lseek(a,0,SEEK_SET);
1197         yaffs_read(a,buffer2,512); 
1198         
1199         printf("buffer 1 %s\n",buffer1);
1200         printf("buffer 2 %s\n",buffer2);
1201         
1202         if(strncmp(buffer1,buffer2,20))
1203         {
1204                 printf("Cache bypass bug detected!!!!!\n");
1205         }
1206         
1207         
1208         return 1;
1209 }
1210
1211
1212 int free_space_check(void)
1213 {
1214         int f;
1215         
1216                 yaffs_StartUp();
1217                 yaffs_mount("/boot");
1218             fill_disk("/boot/",2);
1219             f = yaffs_freespace("/boot");
1220             
1221             printf("%d free when disk full\n",f);           
1222             return 1;
1223 }
1224
1225 int truncate_test(void)
1226 {
1227         int a;
1228         int r;
1229         int i;
1230         int l;
1231
1232         char y[10];
1233
1234         yaffs_StartUp();
1235         yaffs_mount("/boot");
1236
1237         yaffs_unlink("/boot/trunctest");
1238         
1239         a = yaffs_open("/boot/trunctest", O_CREAT | O_TRUNC | O_RDWR,  S_IREAD | S_IWRITE);
1240         
1241         yaffs_write(a,"abcdefghijklmnopqrstuvwzyz",26);
1242         
1243         yaffs_truncate(a,3);
1244         l= yaffs_lseek(a,0,SEEK_END);
1245         
1246         printf("truncated length is %d\n",l);
1247
1248         yaffs_lseek(a,5,SEEK_SET);
1249         yaffs_write(a,"1",1);
1250
1251         yaffs_lseek(a,0,SEEK_SET);
1252         
1253         r = yaffs_read(a,y,10);
1254
1255         printf("read %d bytes:",r);
1256
1257         for(i = 0; i < r; i++) printf("[%02X]",y[i]);
1258
1259         printf("\n");
1260
1261         return 0;
1262
1263 }
1264
1265
1266
1267
1268
1269 void fill_disk_test(const char *mountpt)
1270 {
1271         int i;
1272         yaffs_StartUp();
1273         
1274         for(i = 0; i < 5; i++)
1275         {
1276                 yaffs_mount(mountpt);
1277                 fill_disk_and_delete(mountpt,100,i+1);
1278                 yaffs_unmount(mountpt);
1279         }
1280         
1281 }
1282
1283
1284
1285 void lookup_test(const char *mountpt)
1286 {
1287         int i;
1288         int h;
1289         char a[100];
1290         char b[100];
1291         
1292
1293         yaffs_DIR *d;
1294         yaffs_dirent *de;
1295         struct yaffs_stat s;
1296         char str[100];
1297
1298         yaffs_StartUp();
1299         
1300         yaffs_mount(mountpt);
1301                                 
1302         d = yaffs_opendir(mountpt);
1303         
1304         if(!d)
1305         {
1306                 printf("opendir failed\n");
1307         }
1308         else
1309         {
1310                 
1311                 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1312                 {
1313                         printf("unlinking %s\n",de->d_name);
1314                         yaffs_unlink(de->d_name);
1315                 }
1316                 
1317                 printf("%d files deleted\n",i);
1318         }
1319         
1320         
1321         for(i = 0; i < 2000; i++){
1322         sprintf(a,"%s/%d",mountpt,i);
1323                 h =  yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, 0);
1324                 yaffs_close(h);
1325         }
1326
1327         yaffs_rewinddir(d);
1328         for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1329         {
1330                 printf("%d  %s\n",i,de->d_name);
1331         }       
1332         
1333         printf("%d files listed\n\n\n",i);
1334         
1335         yaffs_rewinddir(d);
1336         yaffs_readdir(d);
1337         yaffs_readdir(d);
1338         yaffs_readdir(d);
1339         
1340         for(i = 0; i < 2000; i++){
1341                 sprintf(a,"%s/%d",mountpt,i);
1342                 yaffs_unlink(a);
1343         }
1344         
1345                 
1346         yaffs_unmount(mountpt);
1347         
1348 }
1349
1350 void freespace_test(const char *mountpt)
1351 {
1352         int i;
1353         int h;
1354         char a[100];
1355         char b[100];
1356         
1357         int  f0;
1358         int f1;
1359         int f2;
1360         int f3;
1361         sprintf(a,"%s/aaa",mountpt);
1362         
1363         yaffs_StartUp();
1364         
1365         yaffs_mount(mountpt);
1366         
1367         f0 = yaffs_freespace(mountpt);
1368         
1369         h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1370         
1371         for(i = 0; i < 100; i++)
1372                 yaffs_write(h,a,100);
1373         
1374         yaffs_close(h);
1375         
1376         f1 = yaffs_freespace(mountpt);
1377         
1378         yaffs_unlink(a);
1379         
1380         f2 = yaffs_freespace(mountpt);
1381         
1382                 
1383         yaffs_unmount(mountpt);
1384         yaffs_mount(mountpt);
1385         
1386         f3 = yaffs_freespace(mountpt);
1387         
1388         printf("%d\n%d\n%d\n%d\n",f0, f1,f2,f3);
1389         
1390         
1391 }
1392
1393 void simple_rw_test(const char *mountpt)
1394 {
1395         int i;
1396         int h;
1397         char a[100];
1398         
1399         int x;
1400         int result;
1401
1402         sprintf(a,"%s/aaa",mountpt);
1403         
1404         yaffs_StartUp();
1405         
1406         yaffs_mount(mountpt);
1407         
1408         yaffs_unlink(a);
1409         
1410         h = yaffs_open(a,O_CREAT| O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1411         
1412         for(i = 100000;i < 200000; i++){
1413                 result = yaffs_write(h,&i,sizeof(i));
1414                 
1415                 if(result != 4)
1416                 {
1417                         printf("write error\n");
1418                         exit(1);
1419                 }
1420         }
1421         
1422         //yaffs_close(h);
1423         
1424         // h = yaffs_open(a,O_RDWR, S_IREAD | S_IWRITE);
1425         
1426         
1427         yaffs_lseek(h,0,SEEK_SET);
1428         
1429         for(i = 100000; i < 200000; i++){
1430                 result = yaffs_read(h,&x,sizeof(x));
1431                 
1432                 if(result != 4 || x != i){
1433                         printf("read error %d %x %x\n",i,result,x);
1434                 }
1435         }
1436         
1437         printf("Simple rw test passed\n");
1438         
1439         
1440         
1441 }
1442
1443
1444 void scan_deleted_files_test(const char *mountpt)
1445 {
1446         char fn[100];
1447         char sub[100];
1448         
1449         const char *p;
1450         
1451         int i;
1452         int j;
1453         int k;
1454         int h;
1455         
1456         sprintf(sub,"%s/sdir",mountpt);
1457         yaffs_StartUp();
1458         
1459         for(j = 0; j < 10; j++)
1460         {
1461                 printf("\n\n>>>>>>> Run %d <<<<<<<<<<<<<\n\n",j);
1462                 yaffs_mount(mountpt);
1463                 yaffs_mkdir(sub,0);
1464                 
1465                 
1466                 p = (j & 0) ? mountpt: sub;
1467         
1468                 for(i = 0; i < 100; i++)
1469                 {
1470                   sprintf(fn,"%s/%d",p,i);  
1471                   
1472                   if(i & 1)
1473                   {
1474                           h = yaffs_open(fn,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1475                           for(k = 0; k < 1000; k++)
1476                                   yaffs_write(h,fn,100);
1477                           yaffs_close(h);
1478                   }
1479                   else
1480                         yaffs_mkdir(fn,0);
1481                 }
1482                 
1483                 for(i = 0; i < 10; i++)
1484                 {
1485                   sprintf(fn,"%s/%d",p,i);  
1486                   if(i & 1) 
1487                         yaffs_unlink(fn);
1488                   else
1489                         yaffs_rmdir(fn);
1490                   
1491                 }
1492                                 
1493                 yaffs_unmount(mountpt);
1494         }
1495         
1496         
1497         
1498
1499 }
1500
1501
1502 void write_10k(int h)
1503 {
1504    int i;
1505    const char *s="0123456789";
1506    for(i = 0; i < 1000; i++)
1507      yaffs_write(h,s,10);
1508
1509 }
1510 void write_200k_file(const char *fn, const char *fdel, const char *fdel1)
1511 {
1512    int h1;
1513    int h2;
1514    int i;
1515    
1516    h1 = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1517    
1518    for(i = 0; i < 100000; i+= 10000)
1519    {
1520         write_10k(h1);
1521         write_10k(h2);
1522    }
1523    
1524    if(yaffs_lseek(h1,0,SEEK_SET) != 1000000)
1525    {
1526         printf("Could not write file\n");
1527    }
1528    
1529    yaffs_unlink(fdel);
1530    for(i = 0; i < 100000; i+= 10000)
1531    {
1532         write_10k(h1);
1533         write_10k(h2);
1534    }
1535    
1536    if(yaffs_lseek(h1,0,SEEK_SET) != 2000000)
1537    {
1538         printf("Could not write file\n");
1539    }
1540    
1541    yaffs_close(h1);
1542    yaffs_close(h2);
1543    yaffs_unlink(fdel1);
1544    
1545    
1546 }
1547
1548
1549 void verify_200k_file(const char *fn)
1550 {
1551    int h1;
1552    int i;
1553    char x[11];
1554    const char *s="0123456789";
1555    
1556    h1 = yaffs_open(fn, O_RDONLY, 0);
1557    
1558    for(i = 0; i < 200000; i+= 10)
1559    {
1560         yaffs_read(h1,x,10);
1561         if(strncmp(x,s,10) != 0)
1562         {
1563                 printf("File verification failed at %d\n",i);
1564         }
1565    }
1566       
1567    yaffs_close(h1);        
1568         
1569 }
1570
1571
1572 void check_resize_gc_bug(const char *mountpt)
1573 {
1574
1575         char a[30];
1576         char b[30];
1577         char c[30];
1578         
1579         int i;
1580         
1581         sprintf(a,"%s/a",mountpt);
1582         sprintf(b,"%s/b",mountpt);
1583         sprintf(c,"%s/c",mountpt);
1584         
1585
1586         
1587         
1588         yaffs_StartUp();
1589         yaffs_mount(mountpt);
1590         yaffs_unlink(a);
1591         yaffs_unlink(b);
1592         
1593         for(i = 0; i < 50; i++)
1594         {  
1595            printf("A\n");write_200k_file(a,"",c);
1596            printf("B\n");verify_200k_file(a);
1597            printf("C\n");write_200k_file(b,a,c);
1598            printf("D\n");verify_200k_file(b);
1599            yaffs_unmount(mountpt);
1600            yaffs_mount(mountpt);
1601            printf("E\n");verify_200k_file(a);
1602            printf("F\n");verify_200k_file(b);
1603         }
1604                 
1605 }
1606
1607
1608 int main(int argc, char *argv[])
1609 {
1610         //return long_test(argc,argv);
1611         
1612         //return cache_read_test();
1613         
1614         //resize_stress_test_no_grow("/flash",20);
1615         
1616         //huge_directory_test_on_path("/ram2k");
1617         
1618          //yaffs_backward_scan_test("/flash")   ;
1619          
1620          //scan_pattern_test("/flash",10000,10);
1621         //short_scan_test("/flash",40000,200);
1622
1623         
1624         //long_test_on_path("/ram2k");
1625         // long_test_on_path("/flash");
1626         simple_rw_test("/flash/flash");
1627         fill_disk_test("/flash/flash");
1628         // rename_over_test("/flash");
1629         //lookup_test("/flash");
1630         freespace_test("/flash/flash");
1631         
1632         
1633         
1634         // cache_bypass_bug_test();
1635         
1636          //free_space_check();
1637          
1638          //check_resize_gc_bug("/flash");
1639          
1640          return 0;
1641         
1642 }