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