yaffs: Improve xattrib to not modify if there is no space
[yaffs2.git] / direct / dtest.c
index cf537f782986dcd8aaa44f3c3a9e60b2453ab1af..34112c84ecc861dceb5cc28697199b5839b0a0b1 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
  *
- * Copyright (C) 2002 Aleph One Ltd.
+ * Copyright (C) 2002-2010 Aleph One Ltd.
  *   for Toby Churchill Ltd and Brightstar Engineering
  *
  * Created by Charles Manning <charles@aleph1.co.uk>
@@ -1036,6 +1036,36 @@ int resize_stress_test(const char *path)
    
 }
 
+
+int overwrite_test(const char *path)
+{
+   char aname[100];
+   char bname[100];
+   int i;
+   int j;   
+   int a;
+   int b;
+   yaffs_StartUp();
+   
+   yaffs_mount(path);
+   
+   sprintf(aname,"%s%s",path,"/a");
+   sprintf(bname,"%s%s",path,"/b");
+   
+   b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
+   for(j= 0; j < 500; j++){
+       yaffs_write(b,bname,100);
+       a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
+       for(i = 0; i < rand() % 20000; i++)
+               yaffs_write(a,&a,sizeof(a));
+       yaffs_close(a);
+   }
+   
+   return 0;
+   
+}
+
+
 int root_perm_remount(const char *path)
 {
    struct yaffs_stat s;
@@ -2457,6 +2487,142 @@ void rmdir_test(const char *mountpt)
        yaffs_rmdir(name);
        yaffs_unmount(mountpt);
 }
+
+
+
+static void print_xattrib_val(const char *path, const char *name)
+{
+       char buffer[1000];
+       int n;
+
+       n = yaffs_getxattr(path,name,buffer,sizeof(buffer));
+       if(n >= 0){
+               __u8 *b = (__u8 *)buffer;
+
+               printf("%d bytes:",n);
+               if(n > 10)
+                       n = 10;
+               while(n > 0){
+                       printf("[%02X]",*b);
+                       b++;
+                       n--;
+               }
+               printf("\n");
+       } else
+               printf(" Novalue result %d\n",n);
+}
+
+static void list_xattr(const char *path)
+{
+       char list[1000];
+       int n=0;
+       int list_len;
+       int len;
+
+       list_len = yaffs_listxattr(path,list,sizeof(list));
+       printf("xattribs for %s, result is %d\n",path,list_len);
+       while(n < list_len){
+               len = strlen(list + n);
+               printf("\"%s\" value ",list+n);
+               print_xattrib_val(path,list + n);
+               n += (len + 1);
+       }
+       printf("end\n");
+}
+
+void basic_xattr_test(const char *mountpt)
+{
+       char name[100];
+       int h;
+       int result;
+       int val1;
+       int valread;
+
+       yaffs_StartUp();
+
+       yaffs_mount(mountpt);
+
+       strcpy(name,mountpt);
+       strcat(name,"/");
+       strcat(name,"xfile");
+
+       yaffs_unlink(name);
+       h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
+       yaffs_close(h);
+
+       printf("Start\n");
+       list_xattr(name);
+
+       printf("Add an attribute\n");
+       val1 = 0x123456;
+       result = yaffs_setxattr(name,"foo",&val1,sizeof(val1),0);
+       printf("wrote attribute foo: result %d\n",result);
+       list_xattr(name);
+       printf("Add an attribute\n");
+       val1 = 0x7890;
+       result = yaffs_setxattr(name,"bar",&val1,sizeof(val1),0);
+       printf("wrote attribute bar: result %d\n",result);
+       list_xattr(name);
+
+       printf("Get non-existanrt attribute\n");
+       print_xattrib_val(name,"not here");
+
+       printf("Delete non existing attribute\n");
+       yaffs_removexattr(name,"not here");
+       list_xattr(name);
+
+       printf("Remove foo\n");
+       yaffs_removexattr(name,"foo");
+       list_xattr(name);
+
+       printf("Remove bar\n");
+       yaffs_removexattr(name,"bar");
+       list_xattr(name);
+
+}
+
+void big_xattr_test(const char *mountpt)
+{
+       char name[100];
+       int h;
+       int result;
+       char val[1000];
+       char valread[1000];
+
+       yaffs_StartUp();
+
+       yaffs_mount(mountpt);
+
+       strcpy(name,mountpt);
+       strcat(name,"/");
+       strcat(name,"xfile");
+
+       yaffs_unlink(name);
+       h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
+       yaffs_close(h);
+
+       printf("Start\n");
+       list_xattr(name);
+
+       printf("Add a large  attribute\n");
+       memset(val,0x1,sizeof(val));
+       result = yaffs_setxattr(name,"aaa",val,200,0);
+       printf("wrote attribute aaa: result %d\n",result);
+       list_xattr(name);
+
+       printf("Add a large  attribute\n");
+       memset(val,0x2,sizeof(val));
+       result = yaffs_setxattr(name,"bbb",val,1000,0);
+       printf("wrote attribute bbb: result %d\n",result);
+       list_xattr(name);
+
+       printf("Replace attribute\n");
+       memset(val,0x3,sizeof(val));
+       result = yaffs_setxattr(name,"aaa",val,1000,0);
+       printf("wrote attribute aaa: result %d\n",result);
+       list_xattr(name);
+
+}
        
 
 int random_seed;
@@ -2480,7 +2646,8 @@ int main(int argc, char *argv[])
        //rename_over_test("//////////////////flash///////////////////yaffs1///////////");
        
        //fill_empty_files_test("/yaffs2/");
-       resize_stress_test("/yaffs2");
+       //resize_stress_test("/yaffs2");
+       //overwrite_test("/yaffs2");
        
        //long_name_test("/yaffs2");
        //link_test0("/yaffs2");
@@ -2519,6 +2686,9 @@ int main(int argc, char *argv[])
         
         //check_resize_gc_bug("/flash");
         
+        basic_xattr_test("/yaffs2");
+        big_xattr_test("/yaffs2");
+
         return 0;
        
 }