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