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