Fix unmatched temporary buffer allocations
[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         yaffs_start_up();
2666
2667         yaffs_mount(mountpt);
2668
2669         strcpy(name,mountpt);
2670         strcat(name,"/");
2671         strcat(name,"xfile");
2672
2673         yaffs_unlink(name);
2674
2675         printf("created\n");
2676         h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2677
2678         yaffs_fstat(h,&st);
2679         printf(" times %lu %lu %lu\n",
2680                         st.yst_atime, st.yst_ctime, st.yst_mtime);
2681
2682         utb.actime = 1000;
2683         utb.modtime = 2000;
2684         result = yaffs_futime(h,&utb);
2685         printf("futime to a 1000 m 2000 result %d\n",result);
2686         yaffs_fstat(h,&st);
2687         printf(" times %lu %lu %lu\n",
2688                         st.yst_atime, st.yst_ctime, st.yst_mtime);
2689
2690
2691         utb.actime = 5000;
2692         utb.modtime = 8000;
2693         result = yaffs_utime(name, &utb);
2694         printf("utime to a 5000 m 8000 result %d\n",result);
2695         yaffs_fstat(h,&st);
2696         printf(" times %lu %lu %lu\n",
2697                         st.yst_atime, st.yst_ctime, st.yst_mtime);
2698
2699         result = yaffs_utime(name, NULL);
2700         printf("utime to NULL result %d\n",result);
2701         yaffs_fstat(h,&st);
2702         printf(" times %lu %lu %lu\n",
2703                         st.yst_atime, st.yst_ctime, st.yst_mtime);
2704
2705
2706 }
2707
2708 void basic_xattr_test(const char *mountpt)
2709 {
2710         char name[100];
2711         int h;
2712         int result;
2713         int val1;
2714
2715         yaffs_start_up();
2716
2717         yaffs_mount(mountpt);
2718
2719         strcpy(name,mountpt);
2720         strcat(name,"/");
2721         strcat(name,"xfile");
2722
2723         yaffs_unlink(name);
2724         h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2725         yaffs_close(h);
2726
2727         printf("Start\n");
2728         list_xattr(name);
2729
2730         printf("Add an attribute\n");
2731         val1 = 0x123456;
2732         result = yaffs_setxattr(name,"foo",&val1,sizeof(val1),0);
2733         printf("wrote attribute foo: result %d\n",result);
2734         list_xattr(name);
2735         printf("Add an attribute\n");
2736         val1 = 0x7890;
2737         result = yaffs_setxattr(name,"bar",&val1,sizeof(val1),0);
2738         printf("wrote attribute bar: result %d\n",result);
2739         list_xattr(name);
2740
2741         printf("Get non-existanrt attribute\n");
2742         print_xattrib_val(name,"not here");
2743
2744         printf("Delete non existing attribute\n");
2745         yaffs_removexattr(name,"not here");
2746         list_xattr(name);
2747
2748         printf("Remove foo\n");
2749         yaffs_removexattr(name,"foo");
2750         list_xattr(name);
2751
2752         printf("Remove bar\n");
2753         yaffs_removexattr(name,"bar");
2754         list_xattr(name);
2755
2756 }
2757
2758 void big_xattr_test(const char *mountpt)
2759 {
2760         char name[100];
2761         int h;
2762         int result;
2763         char val[1000];
2764
2765         yaffs_start_up();
2766
2767         yaffs_mount(mountpt);
2768
2769         strcpy(name,mountpt);
2770         strcat(name,"/");
2771         strcat(name,"xfile");
2772
2773         yaffs_unlink(name);
2774         h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2775         yaffs_close(h);
2776
2777         printf("Start\n");
2778         list_xattr(name);
2779
2780         printf("Add a large  attribute\n");
2781         memset(val,0x1,sizeof(val));
2782         result = yaffs_setxattr(name,"aaa",val,200,0);
2783         printf("wrote attribute aaa: result %d\n",result);
2784         list_xattr(name);
2785
2786         printf("Add a large  attribute\n");
2787         memset(val,0x2,sizeof(val));
2788         result = yaffs_setxattr(name,"bbb",val,1000,0);
2789         printf("wrote attribute bbb: result %d\n",result);
2790         list_xattr(name);
2791
2792         printf("Replace attribute\n");
2793         memset(val,0x3,sizeof(val));
2794         result = yaffs_setxattr(name,"aaa",val,1000,0);
2795         printf("wrote attribute aaa: result %d\n",result);
2796         list_xattr(name);
2797
2798 }
2799
2800
2801 void dump_dev_stats(struct yaffs_dev *dev, const char * str)
2802 {
2803         printf("%s\n",str);
2804         printf( "space free %d erased %d "
2805                 "nand reads %d writes %d erases %d "
2806                 "gc all %d passive %d oldestdirty %d blocks %d copies %d \n",
2807                 dev->n_free_chunks, dev->n_erased_blocks * dev->param.chunks_per_block,
2808                 dev->n_page_reads, dev->n_page_writes, dev->n_erasures,
2809                 dev->all_gcs, dev->passive_gc_count, dev->oldest_dirty_gc_count, dev->n_gc_blocks, dev->n_gc_copies);
2810 }
2811
2812 void test_flash_traffic(const char *mountpt)
2813 {
2814         char name0[100];
2815         char name1[100];
2816         int i;
2817         struct yaffs_dev *dev;
2818
2819         yaffs_trace_mask = 0;
2820
2821         yaffs_start_up();
2822
2823         yaffs_mount(mountpt);
2824
2825         dev = yaffs_getdev(mountpt);
2826
2827         strcpy(name0,mountpt);
2828         strcat(name0,"/x");
2829
2830         strcpy(name1,mountpt);
2831         strcat(name1,"/y");
2832
2833         dump_dev_stats(dev,"start");
2834         create_file_of_size(name0,32 * 1024 * 1024);
2835         dump_dev_stats(dev,"32MB written");
2836         for(i = 0; i < 20; i++)
2837                 create_file_of_size(name1,1024 * 1024);
2838         dump_dev_stats(dev,"20x 1MB files written");
2839
2840 }
2841
2842 void link_follow_test(const char *mountpt)
2843 {
2844         char fn[100];
2845         char sn[100];
2846         char hn[100];
2847         int result;
2848         int h;
2849
2850         yaffs_trace_mask = 0;
2851
2852         yaffs_start_up();
2853
2854         yaffs_mount(mountpt);
2855
2856         sprintf(fn,"%s/file",mountpt);
2857         sprintf(sn,"%s/sym",mountpt);
2858         sprintf(hn,"%s/hl-sym",mountpt);
2859
2860         h = yaffs_open(fn,O_CREAT| O_RDWR, S_IREAD | S_IWRITE);
2861         result = yaffs_close(h);
2862
2863         result = yaffs_symlink(fn,sn);
2864         result = yaffs_link(sn,hn);
2865
2866         if (result < 0)
2867                 printf("At line %d result is %d\n", __LINE__, result);
2868
2869         h =yaffs_open(hn,O_RDWR,0);
2870
2871 }
2872
2873 void max_files_test(const char *mountpt)
2874 {
2875         char fn[100];
2876
2877         int result;
2878         int h;
2879         int i;
2880
2881         yaffs_trace_mask = 0;
2882
2883         yaffs_start_up();
2884
2885         yaffs_mount(mountpt);
2886
2887         for(i = 0; i < 5000; i++) {
2888                 sprintf(fn,"%s/file%d", mountpt, i);
2889                 yaffs_unlink(fn);
2890                 h = yaffs_open(fn,O_CREAT| O_RDWR, S_IREAD | S_IWRITE);
2891                 if(h < 0)
2892                         printf("File %s not created\n", fn);
2893                 yaffs_write(h,fn,100);
2894                 result = yaffs_close(h);
2895         }
2896         for(i = 0; i < 5; i++){
2897                 sprintf(fn,"%s/file%d",mountpt, i);
2898                 yaffs_unlink(fn);
2899         }
2900
2901         for(i = 1000; i < 1010; i++){
2902                 sprintf(fn,"%s/file%d",mountpt, i);
2903                 h = yaffs_open(fn,O_CREAT| O_RDWR, S_IREAD | S_IWRITE);
2904                 yaffs_write(h,fn,100);
2905                 if(h < 0)
2906                         printf("File %s not created\n", fn);
2907                 result = yaffs_close(h);
2908         }
2909
2910         if (result < 0)
2911                 printf("At line %d result is %d\n", __LINE__, result);
2912
2913         yaffs_unmount(mountpt);
2914 }
2915
2916 void case_insensitive_test(const char *mountpt)
2917 {
2918         char fn[100];
2919         char fn2[100];
2920         char buffer[100];
2921         int ret;
2922         struct yaffs_stat s;
2923         int h;
2924         char *x;
2925
2926         yaffs_trace_mask = 0;
2927
2928         yaffs_start_up();
2929
2930         yaffs_mount(mountpt);
2931         dump_directory_tree(mountpt);
2932
2933         sprintf(fn,"%s/Abc.Txt",mountpt);
2934         yaffs_unlink(fn);
2935         h = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2936
2937         ret = yaffs_write(h,fn, strlen(fn) + 1);
2938
2939         ret = yaffs_close(h);
2940
2941         dump_directory_tree(mountpt);
2942
2943
2944         strcpy(fn2, fn);
2945         x = fn2;
2946         while(*x) {
2947                 *x = toupper(*x);
2948                 x++;
2949         }
2950
2951         h = yaffs_open(fn2, O_RDONLY, 0);
2952         ret = yaffs_read(h, buffer, 100);
2953
2954         if (ret != (int)(strlen(fn) + 1) || memcmp(buffer, fn, ret)){
2955                 printf("wrong file read\n");
2956         } else {
2957                 printf("File %s is the same as file %s\n", fn, fn2);
2958         }
2959
2960         ret = yaffs_stat(fn2, &s);
2961
2962         printf("renaming\n");
2963
2964         ret = yaffs_rename(fn, fn2);
2965         dump_directory_tree(mountpt);
2966
2967 }
2968
2969 void start_twice(const char *mountpt)
2970 {
2971          printf("About to do first yaffs_start\n");
2972          yaffs_start_up();
2973          printf("started\n");
2974          printf("First mount returns %d\n", yaffs_mount(mountpt));
2975          printf("About to do second yaffs_start\n");
2976          yaffs_start_up();
2977          printf("started\n");
2978          printf("Second mount returns %d\n", yaffs_mount(mountpt));
2979 }
2980
2981 #define N_WRITES 2000
2982 #define STRIDE   2000
2983
2984 #define BUFFER_N 1100
2985 unsigned  xxbuffer[BUFFER_N];
2986
2987 void set_buffer(int n)
2988 {
2989         int i;
2990         for(i = 0; i < BUFFER_N; i++)
2991                 xxbuffer[i] = i + n;
2992 }
2993
2994 void write_big_sparse_file(int h)
2995 {
2996         int i;
2997         loff_t offset = 0;
2998         loff_t pos;
2999         int n = sizeof(xxbuffer);
3000         int wrote;
3001
3002         for(i = 0; i < N_WRITES; i++) {
3003                 printf("writing at %d\n", (int)offset);
3004                 set_buffer(i);
3005                 pos = yaffs_lseek(h, offset, SEEK_SET);
3006                 if(pos != offset) {
3007                         printf("mismatched seek pos %d offset %d\n",
3008                                 (int)pos, (int)offset);
3009                         perror("lseek64");
3010                         exit(1);
3011                 }
3012                 wrote = yaffs_write(h, xxbuffer, n);
3013
3014                 if(wrote != n) {
3015                         printf("mismatched write wrote %d n %d\n", wrote, n);
3016                         exit(1);
3017                 }
3018
3019                 offset += (STRIDE * sizeof(xxbuffer));
3020         }
3021
3022         yaffs_ftruncate(h, offset);
3023
3024 }
3025
3026 void verify_big_sparse_file(int h)
3027 {
3028         unsigned check_buffer[BUFFER_N];
3029         int i;
3030         loff_t offset = 0;
3031         loff_t pos;
3032         int n = sizeof(check_buffer);
3033         int result;
3034         int checks_failed = 0;
3035         int checks_passed = 0;
3036
3037         for(i = 0; i < N_WRITES * STRIDE; i++) {
3038                 if(i % STRIDE) {
3039                         memset(xxbuffer,0, n);
3040                 } else {
3041                         set_buffer(i/STRIDE);
3042                 }
3043                 pos = yaffs_lseek(h, offset, SEEK_SET);
3044                 if(pos != offset) {
3045                         printf("mismatched seek pos %d offset %d\n",
3046                                 (int)pos, (int)offset);
3047                         perror("lseek64");
3048                         exit(1);
3049                 }
3050                 result = yaffs_read(h, check_buffer, n);
3051
3052                 if(result != n) {
3053                         printf("mismatched read result %d n %d\n", result, n);
3054                         exit(1);
3055                 }
3056
3057                 if(memcmp(xxbuffer, check_buffer, n)) {
3058                         int j;
3059
3060                         printf("buffer at %d mismatches\n", (int)pos);
3061                         printf("xxbuffer ");
3062                         for(j = 0; j < 20; j++)
3063                                 printf(" %d",xxbuffer[j]);
3064                         printf("\n");
3065                         printf("check_buffer ");
3066                         for(j = 0; j < 20; j++)
3067                                 printf(" %d",check_buffer[j]);
3068                         printf("\n");
3069
3070                         checks_failed++;
3071                 } else {
3072                         checks_passed++;
3073                 }
3074
3075                 offset += sizeof(xxbuffer);
3076         }
3077
3078         printf("%d checks passed, %d checks failed\n", checks_passed, checks_failed);
3079
3080 }
3081
3082
3083 void large_file_test(const char *mountpt)
3084 {
3085         char xx_buffer[1000];
3086         int i;
3087         int handle;
3088         char fullname[100];
3089         loff_t file_end;
3090
3091         yaffs_trace_mask = 0;
3092
3093         yaffs_start_up();
3094
3095         yaffs_mount(mountpt);
3096         printf("mounted\n");
3097         dumpDir(mountpt);
3098
3099         sprintf(fullname, "%s/%s", mountpt, "big-test-file");
3100
3101         handle = yaffs_open(fullname, O_RDONLY, 0);
3102
3103         handle = yaffs_open(fullname, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
3104
3105         if(handle < 0) {
3106                 perror("opening file");
3107                 exit(1);
3108         }
3109
3110         write_big_sparse_file(handle);
3111         verify_big_sparse_file(handle);
3112
3113         yaffs_close(handle);
3114
3115         printf("Job done\n");
3116         yaffs_unmount(mountpt);
3117
3118         yaffs_mount(mountpt);
3119         printf("mounted again\n");
3120         dumpDir(mountpt);
3121         handle = yaffs_open(fullname, O_RDONLY, 0);
3122         verify_big_sparse_file(handle);
3123         yaffs_unmount(mountpt);
3124
3125
3126         yaffs_mount3(mountpt, 0, 1);
3127         printf("mounted with no checkpt\n");
3128         dumpDir(mountpt);
3129         handle = yaffs_open(fullname, O_RDONLY, 0);
3130         verify_big_sparse_file(handle);
3131         yaffs_unmount(mountpt);
3132
3133         /* Check resize by adding to the end, resizing back and verifying. */
3134         yaffs_mount3(mountpt, 0, 1);
3135         printf("checking resize\n");
3136         dumpDir(mountpt);
3137         handle = yaffs_open(fullname, O_RDWR, 0);
3138
3139         file_end = yaffs_lseek(handle, 0, SEEK_END);
3140         printf("file_end %d\n", (int)file_end);
3141         for(i = 0; i < 10000; i++)
3142                 yaffs_write(handle, xx_buffer, sizeof(xx_buffer));
3143         yaffs_ftruncate(handle, file_end);
3144
3145         verify_big_sparse_file(handle);
3146         yaffs_unmount(mountpt);
3147
3148 }
3149
3150
3151 int  mk_dir(const char *mp, const char *name)
3152 {
3153         char full_name[100];
3154
3155         sprintf(full_name, "%s/%s", mp, name);
3156
3157         return yaffs_mkdir(full_name, S_IREAD| S_IWRITE);
3158 }
3159
3160 int  mk_file(const char *mp, const char *name)
3161 {
3162         char full_name[100];
3163         int h;
3164
3165         sprintf(full_name, "%s/%s", mp, name);
3166
3167         h = yaffs_open(full_name, O_RDWR | O_CREAT | O_TRUNC, S_IREAD| S_IWRITE);
3168
3169         yaffs_write(h, name, strlen(name));
3170
3171         yaffs_close(h);
3172         return 0;
3173 }
3174
3175 void xx_test(const char *mountpt)
3176 {
3177         yaffs_start_up();
3178
3179         yaffs_format(mountpt,0,0,0);
3180
3181         yaffs_mount(mountpt);
3182         printf("mounted\n");
3183         dumpDir(mountpt);
3184
3185         printf("create files\n");
3186
3187         mk_dir(mountpt, "foo");
3188         mk_file(mountpt, "foo/f1");
3189         mk_file(mountpt, "foo/f2");
3190         mk_file(mountpt, "foo/f3");
3191         mk_file(mountpt, "foo/f4");
3192         dump_directory_tree(mountpt);
3193
3194         printf("unmount and remount\n");
3195
3196         /* Unmount/remount */
3197         yaffs_unmount(mountpt);
3198         yaffs_mount(mountpt);
3199         dump_directory_tree(mountpt);
3200 }
3201
3202 void yy_test(const char *mountpt)
3203 {
3204         yaffs_start_up();
3205
3206         yaffs_mount(mountpt);
3207         dump_directory_tree(mountpt);
3208 }
3209
3210
3211 void readdir_test(const char *mountpt)
3212 {
3213         int i;
3214         yaffs_DIR *dirs[100];
3215
3216         yaffs_trace_mask = 0;
3217
3218         yaffs_start_up();
3219
3220         yaffs_mount(mountpt);
3221
3222         for(i = 0; i < 100; i++) {
3223                  dirs[i] = yaffs_opendir(mountpt);
3224                  printf("%2d %p,", i, dirs[i]);
3225         }
3226
3227         printf("\n");
3228
3229         for(i = 0; i < 100; i++) {
3230                  if(dirs[i])
3231                           yaffs_closedir(dirs[i]);
3232         }
3233
3234
3235         for(i = 0; i < 100; i++) {
3236                  dirs[i] = yaffs_opendir(mountpt);
3237                  printf("%2d %p,", i, dirs[i]);
3238         }
3239
3240         yaffs_unmount(mountpt);
3241
3242 }
3243
3244 void format_test(const char *mountpt)
3245 {
3246         int ret;
3247
3248         yaffs_start_up();
3249
3250         ret = yaffs_format(mountpt, 0, 0, 0);
3251         printf("yaffs_format(...,0, 0, 0) of unmounted returned %d."
3252                 " Should return 0\n\n\n", ret);
3253
3254         yaffs_mount(mountpt);
3255
3256         ret = yaffs_format(mountpt, 0, 0, 0);
3257         printf("yaffs_format(...,0, 0, 0) of mounted returned %d."
3258                 " Should return -1 (busy)\n\n\n", ret);
3259
3260         ret = yaffs_format(mountpt, 1, 0, 0);
3261         printf("yaffs_format(...,1, 0, 0) of mounted returned %d."
3262                 " Should return 0.\n\n\n", ret);
3263
3264         ret = yaffs_mount(mountpt);
3265         printf("mount should return 0 returned %d\n\n\n", ret);
3266
3267         ret = yaffs_format(mountpt, 1, 0, 1);
3268         printf("yaffs_format(...,1, 0, 1) of mounted returned %d."
3269                 " Should return 0.\n\n\n", ret);
3270
3271         ret = yaffs_mount(mountpt);
3272         printf("mount should return -1 returned %d\n", ret);
3273 }
3274
3275 void dir_rename_test(const char *mountpt)
3276 {
3277          char fname[100];
3278          char dname[100];
3279          int h;
3280          int ret;
3281
3282          yaffs_start_up();
3283          yaffs_mount(mountpt);
3284
3285          sprintf(fname,"%s/file",mountpt);
3286          sprintf(dname,"%s/directory",mountpt);
3287
3288          h = yaffs_open(fname,O_CREAT | O_RDWR | O_TRUNC, 0666);
3289          yaffs_close(h);
3290
3291          yaffs_mkdir(dname, 0666);
3292
3293          dump_directory_tree(mountpt);
3294
3295          printf("Try to rename %s to %s\n", fname, dname);
3296          ret = yaffs_rename(fname, dname);
3297          printf("result %d, %d\n", ret, yaffs_get_error());
3298
3299          printf("Try to rename %s to %s\n", dname, fname);
3300          ret = yaffs_rename(dname, fname);
3301          printf("result %d, %d\n", ret, yaffs_get_error());
3302
3303
3304 }
3305
3306
3307 void dir_fd_test(const char *mountpt)
3308 {
3309         char name[100];
3310         int h;
3311         int i;
3312
3313         yaffs_start_up();
3314         yaffs_mount(mountpt);
3315
3316         sprintf(name,"%s/directory",mountpt);
3317         yaffs_mkdir(name, 0666);
3318         for(i=0; i < 20; i++) {
3319                 sprintf(name,"%s/directory/file%d",mountpt, i);
3320
3321                 h = yaffs_open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
3322                 yaffs_write(h, name, strlen(name));
3323                 yaffs_close(h);
3324         }
3325         sprintf(name,"%s/dddd",mountpt);
3326         yaffs_mkdir(name, 0666);
3327         for(i=0; i < 20; i++) {
3328                 sprintf(name,"%s/dddd/filezzz%d",mountpt, i);
3329
3330                 h = yaffs_open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
3331                 yaffs_write(h, name, strlen(name));
3332                 yaffs_close(h);
3333         }
3334
3335
3336         dump_directory_tree(mountpt);
3337         dump_directory_tree_fd(mountpt);
3338         dump_directory_tree_fd(mountpt);
3339
3340 }
3341
3342 void create_delete_many_files_test(const char *mountpt)
3343 {
3344
3345         char fn[100];
3346         int i;
3347         int fsize;
3348         char buffer[1000];
3349         int h;
3350         int wrote;
3351
3352
3353         yaffs_start_up();
3354         yaffs_mount(mountpt);
3355
3356         for(i = 1; i < 2000; i++) {
3357                 sprintf(fn,"%s/f%d",mountpt, i);
3358                 fsize = (i%10) * 10000 + 20000;
3359                 h = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, 0666);
3360                 while (fsize > 0) {
3361                         wrote = yaffs_write(h, buffer, sizeof(buffer));
3362                         if (wrote != sizeof(buffer)) {
3363                                 printf("Writing file %s, only wrote %d bytes\n", fn, wrote);
3364                                 break;
3365                         }
3366                         fsize -= wrote;
3367                 }
3368                 yaffs_unlink(fn);
3369                 yaffs_close(h);
3370         }
3371
3372 }
3373
3374 void find_device_check(void)
3375 {
3376         yaffs_start_up();
3377         yaffs_mount("/nand");
3378         yaffs_mount("/");
3379         yaffs_mkdir("/nandxxx", 0666);
3380         yaffs_mkdir("/nand/xxx", 0666);
3381 }
3382
3383
3384 void try_opendir(const char *str)
3385 {
3386         yaffs_DIR *d;
3387
3388         d = yaffs_opendir(str);
3389
3390         printf("%s --> %p\n", str, d);
3391
3392         if (d)
3393                 yaffs_closedir(d);
3394 }
3395
3396 void opendir_test(void)
3397 {
3398         yaffs_start_up();
3399         yaffs_mount("/nand/");
3400         yaffs_symlink("x", "/nand/sym");
3401         yaffs_mkdir("/nand/x",0666);
3402         yaffs_mkdir("/nand/y",0666);
3403         yaffs_mkdir("/nand/x/r",0666);
3404         yaffs_mkdir("/nand/x/s",0666);
3405         yaffs_mkdir("/nand/x/t",0666);
3406
3407         try_opendir("nand/x/.");
3408         try_opendir("nand/x/r/..");
3409         try_opendir("nand/x/./");
3410         try_opendir("nand/x/r/../");
3411         try_opendir("/nand/x");
3412         try_opendir("/nand/x/");
3413         try_opendir("nand/x");
3414         try_opendir("nand/sym");
3415         try_opendir("nand/sym/");
3416
3417 }
3418
3419 void try_rmdir(const char *str)
3420 {
3421         int ret;
3422
3423         ret= yaffs_rmdir(str);
3424
3425         printf("rmdir(\"%s\") --> %d, errno %d\n", str, ret, yaffs_get_error());
3426
3427 }
3428
3429 void rmdir_test2(void)
3430 {
3431         yaffs_start_up();
3432
3433         yaffs_mount("/nand/");
3434         yaffs_mkdir("/nand/z",0666);
3435         try_rmdir("/nand/z");
3436         yaffs_mkdir("/nand/z",0666);
3437         try_rmdir("/nand/z/");
3438 }
3439
3440
3441 void dump_yaffs_malloc_usage(void)
3442 {
3443         unsigned current;
3444         unsigned high_water;
3445
3446         yaffsfs_get_malloc_values(&current, &high_water);
3447
3448         printf("\n"
3449                "***************************************\n"
3450                "Dump of yaffs malloc usage during run\n"
3451                "Currently allocated : %u bytes\n"
3452                "High water allocated : %u bytes\n"
3453                "Note, this is not accurate on all platforms\n",
3454                 current, high_water);
3455
3456 }
3457
3458
3459 int random_seed;
3460 int simulate_power_failure;
3461
3462 int main(int argc, char *argv[])
3463 {
3464
3465         //unlink("emfile-nand");
3466
3467
3468         atexit(dump_yaffs_malloc_usage);
3469
3470         (void) argc;
3471         (void) argv;
3472
3473         random_seed = time(NULL);
3474         //return long_test(argc,argv);
3475
3476         //return cache_read_test();
3477
3478         // resize_stress_test_no_grow("/flash/flash",20);
3479         //root_perm_remount("/flash/flash");
3480
3481         //huge_directory_test_on_path("/ram2k");
3482
3483          //yaffs_backward_scan_test("/flash/flash");
3484         // yaffs_device_flush_test("/flash/flash");
3485
3486         //rename_over_test("//////////////////flash///////////////////yaffs1///////////");
3487
3488         //fill_empty_files_test("/nand/");
3489         //resize_stress_test("/nand");
3490         //overwrite_test("/nand");
3491
3492         //long_name_test("/nand");
3493         //link_test0("/nand");
3494         //link_test1("yaffs2");
3495          //scan_pattern_test("/flash",10000,10);
3496         //short_scan_test("/yflash2",40000,200);
3497         //short_scan_test("/nand128MB",40000,200);
3498           //small_mount_test("/flash/flash",1000);
3499           //small_overwrite_test("/flash/flash",1000);
3500           //seek_overwrite_test("/flash/flash",1000);
3501          //checkpoint_fill_test("/flash/flash",20);
3502          //checkpoint_upgrade_test("/flash/flash",20);
3503           //small_overwrite_test("/flash/flash",1000);
3504           //checkpoint_fill_test("/flash/flash",20);
3505         //random_small_file_test("/flash/flash",10000);
3506          // huge_array_test("/flash/flash",10);
3507
3508
3509         // handle_test("yaffs2/");
3510
3511         //long_test_on_path("/ram2k");
3512         // long_test_on_path("/flash");
3513         //simple_rw_test("/flash/flash");
3514          fill_n_file_test("/nand128MB", 50, 128000000/50);
3515         // rename_over_test("/flash");
3516         //lookup_test("/flash");
3517         //freespace_test("/flash/flash");
3518
3519         //link_test("/flash/flash");
3520
3521         // cache_bypass_bug_test();
3522
3523          //free_space_check();
3524
3525          //check_resize_gc_bug("/flash");
3526
3527          //basic_xattr_test("/nand");
3528          //big_xattr_test("/nand");
3529
3530          //null_name_test("yaffs2");
3531
3532          //test_flash_traffic("yaffs2");
3533          // link_follow_test("/nand");
3534          //basic_utime_test("/nand");
3535
3536
3537         //format_test("/nand");
3538
3539         //max_files_test("/nand");
3540
3541          //start_twice("/nand");
3542
3543          //large_file_test("/nand");
3544          //readdir_test("/nand");
3545
3546          //basic_utime_test("/nand");
3547          //case_insensitive_test("/nand");
3548
3549          //yy_test("/nand");
3550          //dir_rename_test("/nand");
3551
3552         //dir_fd_test("/nand");
3553
3554         //format_test("/nand");
3555
3556         //opendir_test();
3557         //rmdir_test2();
3558
3559         //create_delete_many_files_test("/nand");
3560          return 0;
3561
3562 }