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