4e92dfc09f0f71e2fc36b124981a5dd8a760c584
[yaffs2.git] / linux-tests / xattrtest.c
1 /*
2  * YAFFS: Yet another Flash File System . 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 Lesser General Public License version 2.1 as
11  * published by the Free Software Foundation.
12  *
13  * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stdint.h>
19 #include <string.h>
20 #include <fcntl.h>
21 #include <sys/stat.h>
22
23
24 static void print_xattrib_val(const char *path, const char *name)
25 {
26         char buffer[100];
27         int n;
28         
29         n = getxattr(path,name,buffer,sizeof(buffer));
30         if(n >= 0){
31                 uint8_t *b = (uint8_t *)buffer;
32
33                 printf("%d bytes:",n);
34                 while(n > 0){
35                         printf("[%02X]",*b);
36                         b++;
37                         n--;
38                 }
39                 printf("\n");
40         } else
41                 printf(" Novalue result %d\n",n);
42 }
43
44 static void list_xattr(const char *path)
45 {
46         char list[1000];
47         int n=0;
48         int list_len;
49         int len;
50         
51         list_len = listxattr(path,list,sizeof(list));
52         printf("xattribs for %s, result is %d\n",path,list_len);
53         while(n < list_len){
54                 len = strlen(list + n);
55                 printf("\"%s\" value ",list+n);
56                 print_xattrib_val(path,list + n);
57                 n += (len + 1);
58         }
59         printf("end\n");
60         
61 }
62
63 void basic_xattr_test(const char *mountpt)
64 {
65         char name[100];
66         int h;
67         int result;
68         int val1;
69         int valread;
70
71         
72         strcpy(name,mountpt);
73         strcat(name,"/");
74         strcat(name,"xfile");
75
76         unlink(name);
77         h = open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
78         close(h);
79         
80         printf("Start\n");
81         list_xattr(name);
82
83         printf("Add an attribute\n");
84         val1 = 0x123456;
85         result = setxattr(name,"foo",&val1,sizeof(val1),0);
86         printf("wrote attribute foo: result %d\n",result);
87         list_xattr(name);
88         printf("Add an attribute\n");
89         val1 = 0x7890;
90         result = setxattr(name,"bar",&val1,sizeof(val1),0);
91         printf("wrote attribute bar: result %d\n",result);
92         list_xattr(name);
93         
94         printf("Get non-existanrt attribute\n");
95         print_xattrib_val(name,"not here");
96         
97         printf("Delete non existing attribute\n");
98         removexattr(name,"not here");
99         list_xattr(name);
100
101         printf("Remove foo\n");
102         removexattr(name,"foo");
103         list_xattr(name);
104
105         printf("Remove bar\n");
106         removexattr(name,"bar");
107         list_xattr(name);
108         
109 }
110         
111
112 int random_seed;
113 int simulate_power_failure;
114
115 int main(int argc, char *argv[])
116 {
117         basic_xattr_test("/mnt/");      
118 }