Added endianness support for YTIME_T
[yaffs2.git] / direct / test-framework / basic-tests / dtest.c
index 006aad7561b56b383cfe117fc02f22065cb0b164..60d17498c7481193f6f9a2eb9eb687dcee4bc008 100644 (file)
@@ -1,8 +1,7 @@
 /*
  * YAFFS: Yet another FFS. A NAND-flash specific file system.
  *
- * Copyright (C) 2002-2011 Aleph One Ltd.
- *   for Toby Churchill Ltd and Brightstar Engineering
+ * Copyright (C) 2002-2018 Aleph One Ltd.
  *
  * Created by Charles Manning <charles@aleph1.co.uk>
  *
 #include <unistd.h>
 #include <fcntl.h>
 #include <time.h>
+#include <ctype.h>
+
 
 #include "yaffsfs.h"
 
 #include "yaffs_guts.h" /* Only for dumping device innards */
+#include "yaffs_endian.h" /*For testing the swap_u64 macro */
 
 extern int yaffs_trace_mask;
 
@@ -118,9 +120,6 @@ int check_pattern_file(char *fn)
 }
 
 
-
-
-
 int dump_file_data(char *fn)
 {
        int h;
@@ -215,6 +214,8 @@ void verify_file_of_size(const char *fn,int syze)
                l = strlen(xx);
 
                result = yaffs_read(h,yy,l);
+               if (result)
+                       printf("result in line %d is %d", __LINE__, result);
                yy[l] = 0;
 
                if(strcmp(xx,yy)){
@@ -386,6 +387,8 @@ void scan_pattern_test(const char *path, int fsize, int niterations)
                        make_pattern_file(fn[j],fsize);
                        result = dump_file_data(fn[j]);
                        result = check_pattern_file(fn[j]);
+                       if (result)
+                               printf("result in line %d is %d", __LINE__, result);
                }
                yaffs_unmount(path);
        }
@@ -396,7 +399,7 @@ void fill_disk(const char *path,int nfiles)
        int h;
        int n;
        int result;
-       int f;
+       int f =0;
 
         static char xx[600];
        char str[50];
@@ -413,8 +416,9 @@ void fill_disk(const char *path,int nfiles)
                {
                        f = yaffs_freespace(path);
                }
+               printf("last freespace was %d\n", f);
                result = yaffs_close(h);
-               printf(" close %d\n",result);
+               printf(" close %d\n", result);
        }
 }
 
@@ -433,7 +437,7 @@ void fill_disk_and_delete(const char *path, int nfiles, int ncycles)
                {
                        sprintf(str,"%s/%d",path,j);
                        result = yaffs_unlink(str);
-                       printf("unlinking file %s, result %d\n",str,result);
+                       printf("unlinking file %s, result %d\n", str, result);
                }
        }
 }
@@ -514,7 +518,7 @@ void dumpDirFollow(const char *dname)
        yaffs_DIR *d;
        struct yaffs_dirent *de;
        struct yaffs_stat s;
-       char str[100];
+       char str[300];
 
        d = yaffs_opendir(dname);
 
@@ -530,7 +534,8 @@ void dumpDirFollow(const char *dname)
 
                        yaffs_lstat(str,&s);
 
-                       printf("%s ino %lld length %d mode %X ",de->d_name,(int)s.st_ino,s.st_size,s.st_mode);
+                       printf("%s ino %d length %d mode %X ",
+                               de->d_name, (int)s.st_ino, (int)s.st_size, s.st_mode);
                        switch(s.st_mode & S_IFMT)
                        {
                                case S_IFREG: printf("data file"); break;
@@ -577,8 +582,8 @@ void dump_directory_tree_worker(const char *dname,int recursive)
 
                        yaffs_lstat(str,&s);
 
-                       printf("%s inode %d obj %x length %lld mode %X ",
-                               str,s.st_ino,de->d_dont_use, s.st_size,s.st_mode);
+                       printf("%s inode %d length %d mode %X ",
+                               str, s.st_ino, (int)s.st_size, s.st_mode);
                        switch(s.st_mode & S_IFMT)
                        {
                                case S_IFREG: printf("data file"); break;
@@ -611,6 +616,63 @@ static void dump_directory_tree(const char *dname)
        printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
 }
 
+void dump_directory_tree_worker_fd(const char *dname,int recursive)
+{
+       int h;
+       struct yaffs_dirent *de;
+       struct yaffs_stat s;
+       char str[1000];
+
+       h = yaffs_open(dname, O_RDONLY, 0);
+
+       if(h < 0)
+       {
+               printf("open of dir failed\n");
+       }
+       else
+       {
+               printf("using fd %d\n", h);
+
+               while((de = yaffs_readdir_fd(h)) != NULL)
+               {
+                       sprintf(str,"%s/%s",dname,de->d_name);
+
+                       yaffs_lstat(str,&s);
+
+                       printf("%s inode %d length %d mode %X ",
+                               str,s.st_ino, (int)s.st_size, s.st_mode);
+                       switch(s.st_mode & S_IFMT)
+                       {
+                               case S_IFREG: printf("data file"); break;
+                               case S_IFDIR: printf("directory"); break;
+                               case S_IFLNK: printf("symlink -->");
+                                                         if(yaffs_readlink(str,str,100) < 0)
+                                                               printf("no alias");
+                                                         else
+                                                               printf("\"%s\"",str);
+                                                         break;
+                               default: printf("unknown"); break;
+                       }
+
+                       printf("\n");
+
+                       if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
+                               dump_directory_tree_worker_fd(str,1);
+
+               }
+
+               yaffs_close(h);
+       }
+
+}
+
+static void dump_directory_tree_fd(const char *dname)
+{
+       dump_directory_tree_worker_fd(dname,1);
+       printf("\n");
+       printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
+}
+
 void dumpDir(const char *dname)
 {      dump_directory_tree_worker(dname,0);
        printf("\n");
@@ -654,6 +716,9 @@ int long_test(int argc, char *argv[])
        mode_t temp_mode;
        struct yaffs_stat ystat;
 
+       (void) argc;
+       (void) argv;
+
        yaffs_start_up();
 
        yaffs_mount("/boot");
@@ -904,7 +969,7 @@ int huge_directory_test_on_path(char *path)
        int total = 0;
        int lastTotal = 0;
 
-       char str[100];
+       char str[300];
 
 
        yaffs_start_up();
@@ -950,6 +1015,8 @@ int huge_directory_test_on_path(char *path)
 
 int yaffs_scan_test(const char *path)
 {
+       (void) path;
+
        return 0;
 }
 
@@ -1043,7 +1110,11 @@ int resize_stress_test(const char *path)
                        {
                                //expand
                                r = yaffs_lseek(a,i * 500,SEEK_SET);
+                               if (r < 0)
+                                       printf("At line %d, r is %d\n", __LINE__, r);
                                r = yaffs_write(a,abuffer,1000);
+                               if (r < 0)
+                                       printf("At line %d, r is %d\n", __LINE__, r);
                        }
                        x++;
 
@@ -1168,6 +1239,8 @@ int resize_stress_test_no_grow_complex(const char *path,int iters)
                                //expand
                                r = yaffs_lseek(a,500,SEEK_END);
                                r = yaffs_write(a,abuffer,1000);
+                               if (r < 0)
+                                       printf("At line %d, r is %d\n", __LINE__, r);
                        }
                        x++;
 
@@ -1242,6 +1315,8 @@ int resize_stress_test_no_grow(const char *path,int iters)
                                //expand
                                r = yaffs_lseek(a,-500,SEEK_END);
                                r = yaffs_write(a,abuffer,1000);
+                               if (r < 0)
+                                       printf("At line %d, r is %d\n", __LINE__, r);
                        }
                        x++;
 
@@ -1273,6 +1348,9 @@ int directory_rename_test(void)
        printf("Do rename (should fail)\n");
 
        r = yaffs_rename("/ram/a","/ram/a/b/d");
+       if (r < 0)
+               printf("At line %d, r is %d\n", __LINE__, r);
+
        printf("\nDirectory look-up of /ram\n");
        dumpDir("/ram");
        dumpDir("/ram/a");
@@ -1377,13 +1455,13 @@ int free_space_check(void)
 {
        int f;
 
-               yaffs_start_up();
-               yaffs_mount("/boot");
-           fill_disk("/boot/",2);
-           f = yaffs_freespace("/boot");
+       yaffs_start_up();
+       yaffs_mount("/boot");
+       fill_disk("/boot/",2);
+       f = yaffs_freespace("/boot");
 
-           printf("%d free when disk full\n",f);
-           return 1;
+       printf("%d free when disk full\n",f);
+       return 1;
 }
 
 int truncate_test(void)
@@ -1416,7 +1494,7 @@ int truncate_test(void)
 
        r = yaffs_read(a,y,10);
 
-       printf("read %d bytes:",r);
+       printf("read %d bytes:", r);
 
        for(i = 0; i < r; i++) printf("[%02X]",y[i]);
 
@@ -1445,6 +1523,40 @@ void fill_disk_test(const char *mountpt)
 }
 
 
+void fill_n_file_test(const char *mountpt, int n, int syz)
+{
+       int i;
+       int l;
+
+       yaffs_start_up();
+
+       yaffs_format(mountpt, 0, 0, 0);
+       yaffs_mount(mountpt);
+
+
+       for(i = 0; i < n; i++)
+       {
+               int h;
+               char fname[200];
+
+               sprintf(fname, "%s/%d", mountpt, i);
+
+               h = yaffs_open(fname, O_CREAT | O_TRUNC | O_RDWR,  S_IREAD | S_IWRITE);
+
+               l = syz;
+
+               while(l > 0) {
+                       yaffs_write(h, fname, sizeof(fname));
+                       l -= sizeof(fname);
+               }
+
+               yaffs_close(h);
+
+       }
+
+}
+
+
 void fill_files_test(const char *mountpt)
 {
        int i;
@@ -1985,10 +2097,7 @@ void multi_mount_test(const char *mountpt,int nmounts)
        for(i = 0; i < nmounts; i++){
                int h0;
                int h1;
-               int len0;
-               int len1;
-
-               static char xx[1000];
+               char xx[1000];
 
                printf("############### Iteration %d   Start\n",i);
                if(1 || i == 0 || i == 5)
@@ -2016,8 +2125,8 @@ void multi_mount_test(const char *mountpt,int nmounts)
                   yaffs_write(h1,xx,1000);
                }
 #endif
-               len0 = yaffs_lseek(h0,0,SEEK_END);
-               len1 = yaffs_lseek(h1,0,SEEK_END);
+               yaffs_lseek(h0,0,SEEK_END);
+               yaffs_lseek(h1,0,SEEK_END);
 
                yaffs_lseek(h0,0,SEEK_SET);
                yaffs_lseek(h1,0,SEEK_SET);
@@ -2051,8 +2160,6 @@ void small_mount_test(const char *mountpt,int nmounts)
 
        int h0;
        int h1;
-       int len0;
-       int len1;
        int nread;
 
        sprintf(a,"%s/a",mountpt);
@@ -2063,7 +2170,7 @@ void small_mount_test(const char *mountpt,int nmounts)
 
        for(i = 0; i < nmounts; i++){
 
-               static char xx[1000];
+               char xx[1000];
 
                printf("############### Iteration %d   Start\n",i);
                if(1 || i == 0 || i == 5)
@@ -2091,8 +2198,8 @@ void small_mount_test(const char *mountpt,int nmounts)
                        yaffs_write(h1,xx,nread);
 
 
-               len0 = yaffs_lseek(h0,0,SEEK_END);
-               len1 = yaffs_lseek(h1,0,SEEK_END);
+               yaffs_lseek(h0,0,SEEK_END);
+               yaffs_lseek(h1,0,SEEK_END);
 
                yaffs_lseek(h0,0,SEEK_SET);
                yaffs_lseek(h1,0,SEEK_SET);
@@ -2288,7 +2395,7 @@ int make_file2(const char *name1, const char *name2,int syz)
 
 extern void SetCheckpointReservedBlocks(int n);
 
-void checkpoint_upgrade_test(const char *mountpt,int nmounts)
+void checkpoint_upgrade_test(const char *mountpt, int nmounts)
 {
 
        char a[50];
@@ -2298,10 +2405,9 @@ void checkpoint_upgrade_test(const char *mountpt,int nmounts)
 
        int j;
 
-       sprintf(a,"%s/a",mountpt);
-
-
+       (void) nmounts;
 
+       sprintf(a,"%s/a",mountpt);
 
        printf("Create start condition\n");
        yaffs_start_up();
@@ -2408,10 +2514,13 @@ void random_seek(int h)
        yaffs_lseek(h,n,SEEK_SET);
 }
 
-void random_truncate(int h, char * name)
+void random_truncate(int h, char *name)
 {
        int n;
        int flen;
+
+       (void) name;
+
        n = random() & 0xFFFFF;
        flen = yaffs_lseek(h,0,SEEK_END);
        if(n > flen)
@@ -2552,10 +2661,10 @@ void basic_utime_test(const char *mountpt)
        char name[100];
        int h;
        int result;
-       int val1;
        struct yaffs_utimbuf utb;
        struct yaffs_stat st;
 
+       //setup
        yaffs_start_up();
 
        yaffs_mount(mountpt);
@@ -2569,28 +2678,177 @@ void basic_utime_test(const char *mountpt)
        printf("created\n");
        h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
 
-       yaffs_fstat(h,&st); printf(" times %u %u %u\n",st.yst_atime, st.yst_ctime, st.yst_mtime);
+       yaffs_fstat(h,&st);
+       printf(" times before %llu %llu %llu\n",
+                       ( u64) st.yst_atime, ( u64) st.yst_ctime, ( u64) st.yst_mtime);
 
+       //here are the last access and modification times.
        utb.actime = 1000;
        utb.modtime = 2000;
+
+       //futime sets the last modification and access time of the file
        result = yaffs_futime(h,&utb);
-       printf("futime to a 1000 m 2000 result %d\n",result);
-       yaffs_fstat(h,&st); printf(" times %u %u %u\n",st.yst_atime, st.yst_ctime, st.yst_mtime);
+       printf("setting times using the futime function to a 1000 m 2000 result  %d\n",result);
+
+       //read the times from the file header
+       yaffs_fstat(h,&st);
+       printf(" times %llu %llu %llu\n",
+                       ( u64) st.yst_atime, ( u64) st.yst_ctime, ( u64) st.yst_mtime);
 
 
        utb.actime = 5000;
        utb.modtime = 8000;
        result = yaffs_utime(name, &utb);
        printf("utime to a 5000 m 8000 result %d\n",result);
-       yaffs_fstat(h,&st); printf(" times %u %u %u\n",st.yst_atime, st.yst_ctime, st.yst_mtime);
+       yaffs_fstat(h,&st);
+       printf(" times %llu %llu %llu\n",
+                       ( u64) st.yst_atime, ( u64) st.yst_ctime, ( u64) st.yst_mtime);
 
        result = yaffs_utime(name, NULL);
        printf("utime to NULL result %d\n",result);
-       yaffs_fstat(h,&st); printf(" times %u %u %u\n",st.yst_atime, st.yst_ctime, st.yst_mtime);
+       yaffs_fstat(h,&st);
+       printf(" times %llu %llu %llu\n",
+                       ( u64) st.yst_atime, ( u64) st.yst_ctime, ( u64) st.yst_mtime);
 
 
 }
 
+void print_binary(u64 val){
+       int count = 0;
+       for (int i= 63; i>=0; i --) {
+               if (count == 0){
+                       printf(" ");
+               }
+               if ((((u64)1) << i) & val) {
+                       printf("1");
+               } else {
+                       printf("0");
+               }
+               count = (count +1) % 8;
+       }
+}
+
+void testing_swap_u64() {
+       int numberOfFailedTests = 0;
+       for (int i =0; i < 8; i ++) {
+               u64 startingNumber = (0xffLLu << (i*8));
+               u64 expected = (0xffLLu << (64 - (i*8) -8));
+               u64 converted = swap_u64(startingNumber);
+               if (converted != expected) {
+                       numberOfFailedTests ++;
+                       printf("numbers do not match.\n");
+                       printf("0xff\t\t\t");
+                       print_binary(0xff);
+                       printf("\nStarting Number \t");
+            print_binary(startingNumber);
+                       printf("\nExpecting \t\t");
+                       print_binary(expected);
+                       printf("\nConverted \t\t");
+                       print_binary(converted);
+
+                       printf("\n");
+               }
+       }
+       if (numberOfFailedTests){
+               printf("testing_swap failed %d tests\n", numberOfFailedTests);
+       } else {
+               printf("testing_swap_u64 passed all tests\n");
+       }
+}
+
+
+void size_utime_test(const char *mountpt)
+{
+       char name[100];
+       int h;
+       int result;
+       struct yaffs_utimbuf utb;
+       struct yaffs_stat st;
+
+       //setup
+       yaffs_start_up();
+
+       yaffs_mount(mountpt);
+
+       strcpy(name,mountpt);
+       strcat(name,"/");
+       strcat(name,"xfile");
+
+       yaffs_unlink(name);
+
+       printf("created\n");
+       h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
+
+       yaffs_fstat(h,&st);
+       printf(" times before %llu %llu %llu\n",
+                          ( u64) st.yst_atime, ( u64) st.yst_ctime, ( u64) st.yst_mtime);
+
+       //first lets get the yaffs_object.
+
+       //then check that yaffs_stat also works.
+       //yaffs_stat already uses 64 bits for both wince and unix times.
+       //To see if we are using 32 or 64 bit time, save a large number into the time and
+       //see if it overflows.
+       long bitsInTime = 8*sizeof(st.yst_ctime);
+       printf("the times are %ld bits long\n", bitsInTime);
+
+       //two testcases
+       if (bitsInTime == 64) {
+               //no need to test the overflow. Just check that it can be retrieved intact.
+
+                       //use u64 variables in case utb truncates the values to 32 bit time by accident.
+                       u64 start = 0xfffff;
+                       u64 end =       0xffffff;
+
+               utb.actime =  start;
+               utb.modtime = end;
+
+               result = yaffs_futime(h,&utb);
+               yaffs_fstat(h,&st);
+               if (st.yst_atime == start && st.yst_mtime == end) {
+                       printf("successfully stored and retrevied a 64 bit number for atime and modtime\n");
+               } else {
+                       printf("failed to store and retrieve a 64 bit number for atime and modtime\n");
+
+               }
+       } else {
+               //it is a 32 bit number. Check to see that it overflowed.
+
+       }
+
+
+       //here are the last access and modification times.
+       utb.actime = 1000;
+       utb.modtime = 2000;
+
+       //futime sets the last modification and access time of the file
+       result = yaffs_futime(h,&utb);
+       printf("setting times using the futime function to a 1000 m 2000 result  %d\n",result);
+
+       //read the times from the file header
+       yaffs_fstat(h,&st);
+       printf(" times %llu %llu %llu\n",
+                   ( u64) st.yst_atime, ( u64) st.yst_ctime, ( u64) st.yst_mtime);
+
+
+       utb.actime = 5000;
+       utb.modtime = 8000;
+       result = yaffs_utime(name, &utb);
+       printf("utime to a 5000 m 8000 result %d\n",result);
+       yaffs_fstat(h,&st);
+       printf(" times %llu %llu %llu\n",
+                   ( u64) st.yst_atime, ( u64) st.yst_ctime, ( u64) st.yst_mtime);
+
+       result = yaffs_utime(name, NULL);
+       printf("utime to NULL result %d\n",result);
+       yaffs_fstat(h,&st);
+       printf(" times %llu %llu %llu\n",
+                   ( u64) st.yst_atime, ( u64) st.yst_ctime, ( u64) st.yst_mtime);
+
+
+}
+
+
 void basic_xattr_test(const char *mountpt)
 {
        char name[100];
@@ -2749,6 +3007,9 @@ void link_follow_test(const char *mountpt)
        result = yaffs_symlink(fn,sn);
        result = yaffs_link(sn,hn);
 
+       if (result < 0)
+               printf("At line %d result is %d\n", __LINE__, result);
+
        h =yaffs_open(hn,O_RDWR,0);
 
 }
@@ -2756,8 +3017,7 @@ void link_follow_test(const char *mountpt)
 void max_files_test(const char *mountpt)
 {
        char fn[100];
-       char sn[100];
-       char hn[100];
+
        int result;
        int h;
        int i;
@@ -2791,11 +3051,12 @@ void max_files_test(const char *mountpt)
                result = yaffs_close(h);
        }
 
-       yaffs_unmount(mountpt);
-
-       //h =yaffs_open(hn,O_RDWR,0);
+       if (result < 0)
+               printf("At line %d result is %d\n", __LINE__, result);
 
+       yaffs_unmount(mountpt);
 }
+
 void case_insensitive_test(const char *mountpt)
 {
         char fn[100];
@@ -2834,7 +3095,7 @@ void case_insensitive_test(const char *mountpt)
         h = yaffs_open(fn2, O_RDONLY, 0);
         ret = yaffs_read(h, buffer, 100);
 
-        if (ret != strlen(fn) + 1 || memcmp(buffer, fn, ret)){
+        if (ret != (int)(strlen(fn) + 1) || memcmp(buffer, fn, ret)){
                printf("wrong file read\n");
         } else {
                printf("File %s is the same as file %s\n", fn, fn2);
@@ -2867,7 +3128,6 @@ void start_twice(const char *mountpt)
 #define BUFFER_N 1100
 unsigned  xxbuffer[BUFFER_N];
 
-
 void set_buffer(int n)
 {
        int i;
@@ -2884,12 +3144,12 @@ void write_big_sparse_file(int h)
        int wrote;
 
        for(i = 0; i < N_WRITES; i++) {
-               printf("writing at %lld\n", offset);
+               printf("writing at %d\n", (int)offset);
                set_buffer(i);
                pos = yaffs_lseek(h, offset, SEEK_SET);
                if(pos != offset) {
-                       printf("mismatched seek pos %lld offset %lld\n",
-                               pos, offset);
+                       printf("mismatched seek pos %d offset %d\n",
+                               (int)pos, (int)offset);
                        perror("lseek64");
                        exit(1);
                }
@@ -2907,9 +3167,6 @@ void write_big_sparse_file(int h)
 
 }
 
-
-
-
 void verify_big_sparse_file(int h)
 {
        unsigned check_buffer[BUFFER_N];
@@ -2918,23 +3175,19 @@ void verify_big_sparse_file(int h)
        loff_t pos;
        int n = sizeof(check_buffer);
        int result;
-       const char * check_type;
        int checks_failed = 0;
        int checks_passed = 0;
 
        for(i = 0; i < N_WRITES * STRIDE; i++) {
                if(i % STRIDE) {
-                       check_type = "zero";
                        memset(xxbuffer,0, n);
                } else {
-                       check_type = "buffer";
                        set_buffer(i/STRIDE);
                }
-               //printf("%s checking %lld\n", check_type, offset);
                pos = yaffs_lseek(h, offset, SEEK_SET);
                if(pos != offset) {
-                       printf("mismatched seek pos %lld offset %lld\n",
-                               pos, offset);
+                       printf("mismatched seek pos %d offset %d\n",
+                               (int)pos, (int)offset);
                        perror("lseek64");
                        exit(1);
                }
@@ -2945,13 +3198,10 @@ void verify_big_sparse_file(int h)
                        exit(1);
                }
 
-
-
-
                if(memcmp(xxbuffer, check_buffer, n)) {
                        int j;
 
-                       printf("buffer at %lld mismatches\n", pos);
+                       printf("buffer at %d mismatches\n", (int)pos);
                        printf("xxbuffer ");
                        for(j = 0; j < 20; j++)
                                printf(" %d",xxbuffer[j]);
@@ -3017,7 +3267,7 @@ void large_file_test(const char *mountpt)
        yaffs_unmount(mountpt);
 
 
-       yaffs_mount_common(mountpt, 0, 1);
+       yaffs_mount3(mountpt, 0, 1);
        printf("mounted with no checkpt\n");
         dumpDir(mountpt);
        handle = yaffs_open(fullname, O_RDONLY, 0);
@@ -3025,13 +3275,13 @@ void large_file_test(const char *mountpt)
        yaffs_unmount(mountpt);
 
        /* Check resize by adding to the end, resizing back and verifying. */
-       yaffs_mount_common(mountpt, 0, 1);
+       yaffs_mount3(mountpt, 0, 1);
        printf("checking resize\n");
         dumpDir(mountpt);
        handle = yaffs_open(fullname, O_RDWR, 0);
 
        file_end = yaffs_lseek(handle, 0, SEEK_END);
-       printf("file_end %lld\n", file_end);
+       printf("file_end %d\n", (int)file_end);
        for(i = 0; i < 10000; i++)
                yaffs_write(handle, xx_buffer, sizeof(xx_buffer));
        yaffs_ftruncate(handle, file_end);
@@ -3068,8 +3318,6 @@ int  mk_file(const char *mp, const char *name)
 
 void xx_test(const char *mountpt)
 {
-       char xx_buffer[1000];
-
        yaffs_start_up();
 
        yaffs_format(mountpt,0,0,0);
@@ -3097,8 +3345,6 @@ void xx_test(const char *mountpt)
 
 void yy_test(const char *mountpt)
 {
-       char xx_buffer[1000];
-
        yaffs_start_up();
 
        yaffs_mount(mountpt);
@@ -3108,14 +3354,9 @@ void yy_test(const char *mountpt)
 
 void readdir_test(const char *mountpt)
 {
-       char xx_buffer[1000];
        int i;
-       int handle;
-       char fullname[100];
-
        yaffs_DIR *dirs[100];
 
-
        yaffs_trace_mask = 0;
 
        yaffs_start_up();
@@ -3142,7 +3383,6 @@ void readdir_test(const char *mountpt)
 
        yaffs_unmount(mountpt);
 
-
 }
 
 void format_test(const char *mountpt)
@@ -3152,21 +3392,25 @@ void format_test(const char *mountpt)
        yaffs_start_up();
 
        ret = yaffs_format(mountpt, 0, 0, 0);
-       printf("yaffs_format(...,0, 0, 0) of unmounted returned %d\n", ret);
+       printf("yaffs_format(...,0, 0, 0) of unmounted returned %d."
+               " Should return 0\n\n\n", ret);
 
        yaffs_mount(mountpt);
 
        ret = yaffs_format(mountpt, 0, 0, 0);
-       printf("yaffs_format(...,0, 0, 0) of mounted returned %d\n", ret);
+       printf("yaffs_format(...,0, 0, 0) of mounted returned %d."
+               " Should return -1 (busy)\n\n\n", ret);
 
        ret = yaffs_format(mountpt, 1, 0, 0);
-       printf("yaffs_format(...,1, 0, 0) of mounted returned %d\n", ret);
+       printf("yaffs_format(...,1, 0, 0) of mounted returned %d."
+               " Should return 0.\n\n\n", ret);
 
        ret = yaffs_mount(mountpt);
-       printf("mount should return 0 returned %d\n", ret);
+       printf("mount should return 0 returned %d\n\n\n", ret);
 
        ret = yaffs_format(mountpt, 1, 0, 1);
-       printf("yaffs_format(...,1, 0, 1) of mounted returned %d\n", ret);
+       printf("yaffs_format(...,1, 0, 1) of mounted returned %d."
+               " Should return 0.\n\n\n", ret);
 
        ret = yaffs_mount(mountpt);
        printf("mount should return -1 returned %d\n", ret);
@@ -3203,11 +3447,173 @@ void dir_rename_test(const char *mountpt)
 
 }
 
+
+void dir_fd_test(const char *mountpt)
+{
+       char name[100];
+       int h;
+       int i;
+
+       yaffs_start_up();
+       yaffs_mount(mountpt);
+
+       sprintf(name,"%s/directory",mountpt);
+       yaffs_mkdir(name, 0666);
+       for(i=0; i < 20; i++) {
+               sprintf(name,"%s/directory/file%d",mountpt, i);
+
+               h = yaffs_open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
+               yaffs_write(h, name, strlen(name));
+               yaffs_close(h);
+       }
+       sprintf(name,"%s/dddd",mountpt);
+       yaffs_mkdir(name, 0666);
+       for(i=0; i < 20; i++) {
+               sprintf(name,"%s/dddd/filezzz%d",mountpt, i);
+
+               h = yaffs_open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
+               yaffs_write(h, name, strlen(name));
+               yaffs_close(h);
+       }
+
+
+       dump_directory_tree(mountpt);
+       dump_directory_tree_fd(mountpt);
+       dump_directory_tree_fd(mountpt);
+
+}
+
+void create_delete_many_files_test(const char *mountpt)
+{
+
+       char fn[100];
+       int i;
+       int fsize;
+       char buffer[1000];
+       int h;
+       int wrote;
+
+
+       yaffs_start_up();
+       yaffs_mount(mountpt);
+
+       for(i = 1; i < 2000; i++) {
+               sprintf(fn,"%s/f%d",mountpt, i);
+               fsize = (i%10) * 10000 + 20000;
+               h = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, 0666);
+               while (fsize > 0) {
+                       wrote = yaffs_write(h, buffer, sizeof(buffer));
+                       if (wrote != sizeof(buffer)) {
+                               printf("Writing file %s, only wrote %d bytes\n", fn, wrote);
+                               break;
+                       }
+                       fsize -= wrote;
+               }
+               yaffs_unlink(fn);
+               yaffs_close(h);
+       }
+
+}
+
+void find_device_check(void)
+{
+       yaffs_start_up();
+       yaffs_mount("/nand");
+       yaffs_mount("/");
+       yaffs_mkdir("/nandxxx", 0666);
+       yaffs_mkdir("/nand/xxx", 0666);
+}
+
+
+void try_opendir(const char *str)
+{
+       yaffs_DIR *d;
+
+       d = yaffs_opendir(str);
+
+       printf("%s --> %p\n", str, d);
+
+       if (d)
+               yaffs_closedir(d);
+}
+
+void opendir_test(void)
+{
+       yaffs_start_up();
+       yaffs_mount("/nand/");
+       yaffs_symlink("x", "/nand/sym");
+       yaffs_mkdir("/nand/x",0666);
+       yaffs_mkdir("/nand/y",0666);
+       yaffs_mkdir("/nand/x/r",0666);
+       yaffs_mkdir("/nand/x/s",0666);
+       yaffs_mkdir("/nand/x/t",0666);
+
+       try_opendir("nand/x/.");
+       try_opendir("nand/x/r/..");
+       try_opendir("nand/x/./");
+       try_opendir("nand/x/r/../");
+       try_opendir("/nand/x");
+       try_opendir("/nand/x/");
+       try_opendir("nand/x");
+       try_opendir("nand/sym");
+       try_opendir("nand/sym/");
+
+}
+
+void try_rmdir(const char *str)
+{
+       int ret;
+
+       ret= yaffs_rmdir(str);
+
+       printf("rmdir(\"%s\") --> %d, errno %d\n", str, ret, yaffs_get_error());
+
+}
+
+void rmdir_test2(void)
+{
+       yaffs_start_up();
+
+       yaffs_mount("/nand/");
+       yaffs_mkdir("/nand/z",0666);
+       try_rmdir("/nand/z");
+       yaffs_mkdir("/nand/z",0666);
+       try_rmdir("/nand/z/");
+}
+
+
+void dump_yaffs_malloc_usage(void)
+{
+       unsigned current;
+       unsigned high_water;
+
+       yaffsfs_get_malloc_values(&current, &high_water);
+
+       printf("\n"
+              "***************************************\n"
+              "Dump of yaffs malloc usage during run\n"
+              "Currently allocated : %u bytes\n"
+              "High water allocated : %u bytes\n"
+              "Note, this is not accurate on all platforms\n",
+               current, high_water);
+
+}
+
+
 int random_seed;
 int simulate_power_failure;
 
 int main(int argc, char *argv[])
 {
+
+       //unlink("emfile-nand");
+
+
+       atexit(dump_yaffs_malloc_usage);
+
+       (void) argc;
+       (void) argv;
+
        random_seed = time(NULL);
        //return long_test(argc,argv);
 
@@ -3223,15 +3629,16 @@ int main(int argc, char *argv[])
 
        //rename_over_test("//////////////////flash///////////////////yaffs1///////////");
 
-       //fill_empty_files_test("/yaffs2/");
-       //resize_stress_test("/yaffs2");
-       //overwrite_test("/yaffs2");
+       //fill_empty_files_test("/nand/");
+       //resize_stress_test("/nand");
+       //overwrite_test("/nand");
 
-       //long_name_test("/yaffs2");
-       //link_test0("/yaffs2");
+       //long_name_test("/nand");
+       //link_test0("/nand");
        //link_test1("yaffs2");
         //scan_pattern_test("/flash",10000,10);
-       //short_scan_test("/flash/flash",40000,200);
+       //short_scan_test("/yflash2",40000,200);
+       //short_scan_test("/nand128MB",40000,200);
          //small_mount_test("/flash/flash",1000);
          //small_overwrite_test("/flash/flash",1000);
          //seek_overwrite_test("/flash/flash",1000);
@@ -3248,7 +3655,7 @@ int main(int argc, char *argv[])
        //long_test_on_path("/ram2k");
        // long_test_on_path("/flash");
        //simple_rw_test("/flash/flash");
-       //fill_disk_test("/flash/flash");
+       //fill_n_file_test("/nand128MB", 50, 128000000/50);
        // rename_over_test("/flash");
        //lookup_test("/flash");
        //freespace_test("/flash/flash");
@@ -3261,31 +3668,41 @@ int main(int argc, char *argv[])
 
         //check_resize_gc_bug("/flash");
 
-        //basic_xattr_test("/yaffs2");
-        //big_xattr_test("/yaffs2");
+        //basic_xattr_test("/nand");
+        //big_xattr_test("/nand");
 
         //null_name_test("yaffs2");
 
         //test_flash_traffic("yaffs2");
-        // link_follow_test("/yaffs2");
-        //basic_utime_test("/yaffs2");
+        // link_follow_test("/nand");
+        //basic_utime_test("/nand");
+
+
+       //format_test("/nand");
+
+       //max_files_test("/nand");
 
+        //start_twice("/nand");
 
-       //format_test("/yaffs2");
+        //large_file_test("/nand");
+        //readdir_test("/nand");
 
-       //max_files_test("/yaffs2");
+        basic_utime_test("/nand");
+        testing_swap_u64();
+        size_utime_test("/nand");
+        //case_insensitive_test("/nand");
 
-        //start_twice("/yaffs2");
+        //yy_test("/nand");
+        //dir_rename_test("/nand");
 
-        //large_file_test("/yaffs2");
-        //readdir_test("/yaffs2");
+       //dir_fd_test("/nand");
 
-        //basic_utime_test("/yaffs2");
-        //case_insensitive_test("/yaffs2");
+       //format_test("/nand");
 
-        //yy_test("/yaffs2");
-        dir_rename_test("/yaffs2");
+       //opendir_test();
+       //rmdir_test2();
 
+       //create_delete_many_files_test("/nand");
         return 0;
 
 }