yaffs Commiting changes to yaffs_importer.py yaffs_browser.py and yaffs_tester.c
[yaffs2.git] / linux-tests / xattrtest.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <string.h>
5 #include <fcntl.h>
6 #include <sys/stat.h>
7
8
9 static void print_xattrib_val(const char *path, const char *name)
10 {
11         char buffer[100];
12         int n;
13         
14         n = getxattr(path,name,buffer,sizeof(buffer));
15         if(n >= 0){
16                 uint8_t *b = (uint8_t *)buffer;
17
18                 printf("%d bytes:",n);
19                 while(n > 0){
20                         printf("[%02X]",*b);
21                         b++;
22                         n--;
23                 }
24                 printf("\n");
25         } else
26                 printf(" Novalue result %d\n",n);
27 }
28
29 static void list_xattr(const char *path)
30 {
31         char list[1000];
32         int n=0;
33         int list_len;
34         int len;
35         
36         list_len = listxattr(path,list,sizeof(list));
37         printf("xattribs for %s, result is %d\n",path,list_len);
38         while(n < list_len){
39                 len = strlen(list + n);
40                 printf("\"%s\" value ",list+n);
41                 print_xattrib_val(path,list + n);
42                 n += (len + 1);
43         }
44         printf("end\n");
45         
46 }
47
48 void basic_xattr_test(const char *mountpt)
49 {
50         char name[100];
51         int h;
52         int result;
53         int val1;
54         int valread;
55
56         
57         strcpy(name,mountpt);
58         strcat(name,"/");
59         strcat(name,"xfile");
60
61         unlink(name);
62         h = open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
63         close(h);
64         
65         printf("Start\n");
66         list_xattr(name);
67
68         printf("Add an attribute\n");
69         val1 = 0x123456;
70         result = setxattr(name,"foo",&val1,sizeof(val1),0);
71         printf("wrote attribute foo: result %d\n",result);
72         list_xattr(name);
73         printf("Add an attribute\n");
74         val1 = 0x7890;
75         result = setxattr(name,"bar",&val1,sizeof(val1),0);
76         printf("wrote attribute bar: result %d\n",result);
77         list_xattr(name);
78         
79         printf("Get non-existanrt attribute\n");
80         print_xattrib_val(name,"not here");
81         
82         printf("Delete non existing attribute\n");
83         removexattr(name,"not here");
84         list_xattr(name);
85
86         printf("Remove foo\n");
87         removexattr(name,"foo");
88         list_xattr(name);
89
90         printf("Remove bar\n");
91         removexattr(name,"bar");
92         list_xattr(name);
93         
94 }
95         
96
97 int random_seed;
98 int simulate_power_failure;
99
100 int main(int argc, char *argv[])
101 {
102         basic_xattr_test("/mnt/");      
103 }