Write object headers with stored file extents
[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 #include <unistd.h>
23 #include <sys/xattr.h>
24
25 static void print_xattrib_val(const char *path, const char *name)
26 {
27         char buffer[100];
28         int n;
29         
30         n = getxattr(path,name,buffer,sizeof(buffer));
31         if(n >= 0){
32                 uint8_t *b = (uint8_t *)buffer;
33
34                 printf("%d bytes:",n);
35                 while(n > 0){
36                         printf("[%02X]",*b);
37                         b++;
38                         n--;
39                 }
40                 printf("\n");
41         } else
42                 printf(" Novalue result %d\n",n);
43 }
44
45 static void list_xattr(const char *path)
46 {
47         char list[1000];
48         int n=0;
49         int list_len;
50         int len;
51         
52         list_len = listxattr(path,list,sizeof(list));
53         printf("xattribs for %s, result is %d\n",path,list_len);
54         while(n < list_len){
55                 len = strlen(list + n);
56                 printf("\"%s\" value ",list+n);
57                 print_xattrib_val(path,list + n);
58                 n += (len + 1);
59         }
60         printf("end\n");
61         
62 }
63
64 void basic_xattr_test(const char *mountpt)
65 {
66         char name[100];
67         int h;
68         int result;
69         int val1;
70         
71         strcpy(name,mountpt);
72         strcat(name,"/");
73         strcat(name,"xfile");
74
75         unlink(name);
76         h = open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
77         close(h);
78         
79         printf("Start\n");
80         list_xattr(name);
81
82         printf("Add an attribute\n");
83         val1 = 0x123456;
84         result = setxattr(name,"foo",&val1,sizeof(val1),0);
85         printf("wrote attribute foo: result %d\n",result);
86         list_xattr(name);
87         printf("Add an attribute\n");
88         val1 = 0x7890;
89         result = setxattr(name,"bar",&val1,sizeof(val1),0);
90         printf("wrote attribute bar: result %d\n",result);
91         list_xattr(name);
92         
93         printf("Get non-existanrt attribute\n");
94         print_xattrib_val(name,"not here");
95         
96         printf("Delete non existing attribute\n");
97         removexattr(name,"not here");
98         list_xattr(name);
99
100         printf("Remove foo\n");
101         removexattr(name,"foo");
102         list_xattr(name);
103
104         printf("Remove bar\n");
105         removexattr(name,"bar");
106         list_xattr(name);
107         
108 }
109         
110
111 int random_seed;
112 int simulate_power_failure;
113
114 int main(int argc, char *argv[])
115 {
116         basic_xattr_test("/mnt/");      
117         return 0;
118 }