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