Fix copyright
[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-2018 Aleph One Ltd.
5  *
6  * Created by Charles Manning <charles@aleph1.co.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1 as
10  * published by the Free Software Foundation.
11  *
12  * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <stdint.h>
18 #include <string.h>
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #include <sys/xattr.h>
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         
70         strcpy(name,mountpt);
71         strcat(name,"/");
72         strcat(name,"xfile");
73
74         unlink(name);
75         h = open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
76         close(h);
77         
78         printf("Start\n");
79         list_xattr(name);
80
81         printf("Add an attribute\n");
82         val1 = 0x123456;
83         result = setxattr(name,"foo",&val1,sizeof(val1),0);
84         printf("wrote attribute foo: result %d\n",result);
85         list_xattr(name);
86         printf("Add an attribute\n");
87         val1 = 0x7890;
88         result = setxattr(name,"bar",&val1,sizeof(val1),0);
89         printf("wrote attribute bar: result %d\n",result);
90         list_xattr(name);
91         
92         printf("Get non-existanrt attribute\n");
93         print_xattrib_val(name,"not here");
94         
95         printf("Delete non existing attribute\n");
96         removexattr(name,"not here");
97         list_xattr(name);
98
99         printf("Remove foo\n");
100         removexattr(name,"foo");
101         list_xattr(name);
102
103         printf("Remove bar\n");
104         removexattr(name,"bar");
105         list_xattr(name);
106         
107 }
108         
109
110 int random_seed;
111 int simulate_power_failure;
112
113 int main(int argc, char *argv[])
114 {
115         basic_xattr_test("/mnt/");      
116         return 0;
117 }