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