-
Notifications
You must be signed in to change notification settings - Fork 1
/
xattr-util.c
247 lines (232 loc) · 6.42 KB
/
xattr-util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/xattr.h>
#include <linux/xattr.h>
#include <sys/types.h>
#define CMDLS "-l"
#define CMDSET "-s"
#define CMDGET "-g"
#define CMDREM "-r"
#define USAGE_GENERAL "<command> <opt args> <path>"
#define USAGE_LIST CMDLS " <path>"
#define USAGE_SET CMDSET " <Attr Name> <Attr Value> <path>"
#define USAGE_GET CMDGET " <Attr Name> <path>"
#define USAGE_REM CMDREM " <Attr Name> <path>"
#ifdef linux
/* Linux is missing ENOATTR error, using ENODATA instead */
#define ENOATTR ENODATA
#endif
/* Add namespace defintion for older kernels. Normally included in linux/xattr.h */
#ifndef XATTR_USER_PREFIX
#define XATTR_USER_PREFIX "user."
#define XATTR_USER_PREFIX_LEN (sizeof (XATTR_USER_PREFIX) - 1)
#endif
static void printUsageGeneral(char* pgmName){
fprintf(stderr, "Usage: %s %s\n",
pgmName, USAGE_GENERAL);
}
static void printUsageList(char* pgmName){
fprintf(stderr, "Usage: %s %s\n",
pgmName, USAGE_LIST);
}
static void printUsageSet(char* pgmName){
fprintf(stderr, "Usage: %s %s\n",
pgmName, USAGE_SET);
}
static void printUsageGet(char* pgmName){
fprintf(stderr, "Usage: %s %s\n",
pgmName, USAGE_GET);
}
static void printUsageRem(char* pgmName){
fprintf(stderr, "Usage: %s %s\n",
pgmName, USAGE_REM);
}
int main(int argc, char* argv[]){
/* Local vars */
char* lst = NULL;
char* chr = NULL;
char* start = NULL;
char* tmpstr = NULL;
char* tmpval = NULL;
ssize_t cnt = 0;
ssize_t lstsize = 0;
ssize_t valsize = 0;
/* Check base input */
if(argc < 2){
printUsageGeneral(argv[0]);
exit(EXIT_FAILURE);
}
/* Parse Command */
if(!strcmp(argv[1], CMDLS)){
/* List Case */
/* Check proper input */
if(argc != 3){
printUsageList(argv[0]);
exit(EXIT_FAILURE);
}
/* Call xattr list to get size */
lstsize = listxattr(argv[2], NULL, 0);
if(lstsize < 0){
perror("listxattr error");
fprintf(stderr, "path = %s\n", argv[2]);
exit(EXIT_FAILURE);
}
/* Malloc space for list items */
lst = malloc(sizeof(*lst)*lstsize);
if(!lst){
perror("malloc of 'lst' error");
exit(EXIT_FAILURE);
}
/* Call xattr list to get data */
lstsize = listxattr(argv[2], lst, lstsize);
if(lstsize < 0 || !lst){
perror("listxattr error");
fprintf(stderr, "path = %s\n", argv[2]);
exit(EXIT_FAILURE);
}
/* Tokenize null seperated array on \0 */
chr = start = lst;
cnt = 0;
while(cnt < lstsize){
/* Print attribute names, one per line */
fprintf(stdout, "%s\n", start);
while(*chr != '\0'){
chr++;
cnt++;
}
start = ++chr;
cnt++;
}
}
else if(!strcmp(argv[1], CMDSET)){
/* Set Case */
/* Check proper input */
if(argc != 5){
printUsageSet(argv[0]);
exit(EXIT_FAILURE);
}
/* Append necessary 'user.' namespace prefix to beginning of name string */
tmpstr = malloc(strlen(argv[2]) + XATTR_USER_PREFIX_LEN + 1);
if(!tmpstr){
perror("malloc of 'tmpstr' error");
exit(EXIT_FAILURE);
}
strcpy(tmpstr, XATTR_USER_PREFIX);
strcat(tmpstr, argv[2]);
/* Set attribute */
if(setxattr(argv[4], tmpstr, argv[3], strlen(argv[3]), 0)){
perror("setxattr error");
fprintf(stderr, "path = %s\n", argv[4]);
fprintf(stderr, "name = %s\n", tmpstr);
fprintf(stderr, "value = %s\n", argv[3]);
fprintf(stderr, "size = %zd\n", strlen(argv[3]));
exit(EXIT_FAILURE);
}
/* Cleanup */
free(tmpstr);
}
else if(!strcmp(argv[1], CMDGET)){
/* Get Case */
/* Check proper input */
if(argc != 4){
printUsageGet(argv[0]);
exit(EXIT_FAILURE);
}
/* Append necessary 'user.' namespace prefix to beginning of name string */
tmpstr = malloc(strlen(argv[2]) + XATTR_USER_PREFIX_LEN + 1);
if(!tmpstr){
perror("malloc of 'tmpstr' error");
exit(EXIT_FAILURE);
}
strcpy(tmpstr, XATTR_USER_PREFIX);
strcat(tmpstr, argv[2]);
/* Get attribute value size */
valsize = getxattr(argv[3], tmpstr, NULL, 0);
if(valsize < 0){
if(errno == ENOATTR){
fprintf(stdout, "No %s attribute set on %s\n", tmpstr, argv[3]);
free(tmpstr);
return EXIT_SUCCESS;
}
else{
perror("getxattr error");
fprintf(stderr, "path = %s\n", argv[3]);
fprintf(stderr, "name = %s\n", tmpstr);
fprintf(stderr, "value = %s\n", "NULL");
fprintf(stderr, "size = %zd\n", valsize);
exit(EXIT_FAILURE);
}
}
/* Malloc Value Space */
tmpval = malloc(sizeof(*tmpval)*(valsize+1));
if(!tmpval){
perror("malloc of 'tmpval' error");
exit(EXIT_FAILURE);
}
/* Get attribute value */
valsize = getxattr(argv[3], tmpstr, tmpval, valsize);
if(valsize < 0){
if(errno == ENOATTR){
fprintf(stdout, "No %s attribute set on %s\n", tmpstr, argv[3]);
free(tmpval);
free(tmpstr);
return EXIT_SUCCESS;
}
else{
perror("getxattr error");
fprintf(stderr, "path = %s\n", argv[3]);
fprintf(stderr, "name = %s\n", tmpstr);
fprintf(stderr, "value = %s\n", tmpval);
fprintf(stderr, "size = %zd\n", valsize);
exit(EXIT_FAILURE);
}
}
/* Print Value */
tmpval[valsize] = '\0';
fprintf(stdout, "%s = %s\n", tmpstr, tmpval);
/* Clean Up */
free(tmpval);
free(tmpstr);
}
else if(!strcmp(argv[1], CMDREM)){
/* Remove Case */
/* Check proper input */
if(argc != 4){
printUsageRem(argv[0]);
exit(EXIT_FAILURE);
}
/* Append necessary 'user.' namespace prefix to beginning of name string */
tmpstr = malloc(strlen(argv[2]) + XATTR_USER_PREFIX_LEN + 1);
if(!tmpstr){
perror("malloc of 'tmpstr' error");
exit(EXIT_FAILURE);
}
strcpy(tmpstr, XATTR_USER_PREFIX);
strcat(tmpstr, argv[2]);
/* Remove Attr */
if(removexattr(argv[3], tmpstr)){
if(errno == ENOATTR){
fprintf(stdout, "No %s attribute set on %s\n", tmpstr, argv[3]);
free(tmpstr);
return EXIT_SUCCESS;
}
else{
perror("removexattr error");
fprintf(stderr, "path = %s\n", argv[3]);
fprintf(stderr, "name = %s\n", tmpstr);
exit(EXIT_FAILURE);
}
}
/* Cleanup */
free(tmpstr);
}
else{
/* Bad Case */
fprintf(stderr, "Unrecognized option\n");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}