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