forked from tblah/crossxattr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xattr.c
341 lines (255 loc) · 9.27 KB
/
xattr.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
Crossxattr
==========
Wrappers for xattr related system calls so they have a common interface across GNU+Linux & FreeBSD
Copyright (c) 2015 Tom Eccles
github.com/tblah/crossxattr
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "errExit.h"
#define ERR_STR_SIZE 200
// FreeBSD support
#if ( defined __FreeBSD__ )
#include <sys/types.h>
#include <sys/extattr.h>
#include <errno.h>
#include <sys/param.h>
#include <sys/vnode.h>
#include <stdio.h>
void checkReturnValue( const char* callerName, ssize_t ret ) {
/*
Errno documentation can be found online here:
https://www.freebsd.org/cgi/man.cgi?query=errno&apropos=0&sektion=0&manpath=FreeBSD+10.2-RELEASE&arch=default&format=html
They can of course also be accessed via man on FreeBSD
*/
if ( ret >= 0 ) {
return;
}
char errStr[ERR_STR_SIZE];
int snprintfRet = 0;
switch ( errno ) {
case EFAULT:
snprintfRet = snprintf( errStr, ERR_STR_SIZE, "EFAULT in %s", callerName );
break;
case ENAMETOOLONG:
snprintfRet = snprintf( errStr, ERR_STR_SIZE, "ENAMETOOLONG: in %s", callerName );
break;
case ENOATTR:
snprintfRet = snprintf( errStr, ERR_STR_SIZE, "ENOATTR in %s", callerName );
break;
case ENOTDIR:
snprintfRet = snprintf( errStr, ERR_STR_SIZE, "ENOTDIR in %s", callerName );
break;
case ENOENT:
snprintfRet = snprintf( errStr, ERR_STR_SIZE, "ENOENT in %s", callerName );
break;
case EACCES:
snprintfRet = snprintf( errStr, ERR_STR_SIZE, "EACCES in %s", callerName );
break;
default:
snprintfRet = snprintf( errStr, ERR_STR_SIZE, "Unknown error %i in %s", errno, callerName);
}
if ( snprintfRet < 0 ) {
errExit( "snprintf");
}
errExit( errStr );
}
ssize_t getAttr( const char* path, const char* attrname, void* data, size_t nbytes ) {
ssize_t ret = extattr_get_file( path, EXTATTR_NAMESPACE_USER, attrname, data, nbytes );
checkReturnValue( "getAttr", ret );
return ret;
}
ssize_t setAttr( const char* path, const char *attrname, const void* data, size_t nbytes ) {
ssize_t ret = extattr_set_file( path, EXTATTR_NAMESPACE_USER, attrname, data, nbytes );
checkReturnValue( "setAttr", ret );
return ret;
}
ssize_t deleteAttr( const char* path, const char* attrname ) {
ssize_t ret = extattr_delete_file( path, EXTATTR_NAMESPACE_USER, attrname );
checkReturnValue( "deleteAttr", ret );
return ret;
}
ssize_t listAttrs( const char* path, void* data, size_t nbytes ) {
/*
extattr_list_file() returns a list of attributes present in the requested namespace.
Each list entry consists of a single byte containing the length of the attribute name.
The attribute name is not terminated by ASCII 0 (nul).
*/
ssize_t ret = extattr_list_file( path, EXTATTR_NAMESPACE_USER, data, nbytes );
checkReturnValue( "listAttrs", ret );
// Do the string manipulation
char* bitstring = data;// Cast the void pointer to a string
int len = (int) bitstring[0];// Bootstrap the first pointer
for ( int i = 1; i < nbytes; i++ ) {
if ( i == len ) {
// Set the position of the next pointer
len = (int) bitstring[i];
bitstring[i] = '\0';
}
bitstring[i-1] = bitstring[i];
}
return ret;
}
#endif // FreeBSD
// GNU+Linux
#ifdef __gnu_linux__
#include <sys/types.h>
#include <attr/xattr.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
void checkReturnValue( const char* callerName, ssize_t ret ) {
if ( ret != -1 ) {
return;
}
char errStr[ERR_STR_SIZE];
int snprintfRet = 0;
switch ( errno ) {
case ENOATTR:
snprintfRet = snprintf( errStr, ERR_STR_SIZE, "ENOATTR in %s", callerName );
break;
case ENOTSUP:
snprintfRet = snprintf( errStr, ERR_STR_SIZE, "ENOSUP in %s", callerName );
break;
case ERANGE:
snprintfRet = snprintf( errStr, ERR_STR_SIZE, "ERANGE in %s", callerName );
break;
case EDQUOT:
snprintfRet = snprintf( errStr, ERR_STR_SIZE, "EDQUOT in %s", callerName );
break;
case ENOSPC:
snprintfRet = snprintf( errStr, ERR_STR_SIZE, "ENOSPC in %s", callerName );
break;
case EACCES:
snprintfRet = snprintf( errStr, ERR_STR_SIZE, "EACCES in %s", callerName );
break;
case EFAULT:
snprintfRet = snprintf( errStr, ERR_STR_SIZE, "EFAULT in %s", callerName );
break;
case ENOENT:
snprintfRet = snprintf( errStr, ERR_STR_SIZE, "ENOENT in %s", callerName );
break;
default:
snprintfRet = snprintf( errStr, ERR_STR_SIZE, "Unknown error %i in %s", errno, callerName);
}
if ( snprintfRet < 0 ) {
errExit( "snprintf" );
}
errExit( errStr );
}
char* addNamespacePrefix( const char* str ) {
// adds "user." to the beginning of the name to specify the namespace
// strlen( "user." ) == 5
// +1 for null = 6
char* ret = ( char* ) malloc( strlen( str ) + 6 );
if ( ret == NULL )
errExit( "allocating namespace prefix memory" );
int snprintfRet = snprintf( ret, strlen( str ) + 6 , "user.%s", str );
if ( snprintfRet < 0 )
errExit( "snprintf in addNamespacePrefix" );
return ret;
}
ssize_t getAttr( const char* path, const char* attrname, void* data, size_t nbytes ) {
char* name = addNamespacePrefix( attrname );
ssize_t ret = getxattr( path, name, data, nbytes );
free( name );
name = NULL;
checkReturnValue( "getAttr", ret );
return ret;
}
ssize_t setAttr( const char* path, const char *attrname, const void* data, size_t nbytes ) {
char* name = addNamespacePrefix( attrname );
// flags = 0 means it will be created if it does not exist or replaced if
// it does
ssize_t ret = setxattr( path, name, data, nbytes, 0 );
free( name );
name = NULL;
checkReturnValue( "setAttr", ret );
return ret;
}
ssize_t deleteAttr( const char* path, const char* attrname ) {
char* name = addNamespacePrefix( attrname );
ssize_t ret = removexattr( path, name );
free( name );
name = NULL;
checkReturnValue( "deleteAttr", ret );
return ret;
}
char* seekToInterestingPart( const char* s, size_t* entriesRemaining ) {
// seeks past just the namespace part if its a usernamespace string,
// otherwise it skips to the next one and tries again
if ( (*entriesRemaining) < 1 )
return NULL;
(*entriesRemaining)--;
if ( strncmp( "user.", s, 5 ) == 0 ) {
// this is in the user namespace so return the address of the name
return strchr( s, '.' ) + 1;
} else {
// this is not in the user namespace so try the next entry
return seekToInterestingPart( s + strlen( s ) + 1, entriesRemaining );
}
return NULL;
}
ssize_t numCharInStr( char c, char* s, ssize_t len ) {
ssize_t ret = 0;
for ( ssize_t i = 0; i < len; i++ )
if ( s[i] == c )
ret++;
return ret;
}
ssize_t listAttrs( const char* path, void* data, size_t nbytes ) {
ssize_t listxattrRet = listxattr( path, (char*) data, nbytes );
checkReturnValue( "listAttrs", listxattrRet);
ssize_t numEntries = numCharInStr( '.', data, listxattrRet );
// remove non-user namespace list items and remove the namespace name
// specifier
// first allocate a list of pointers to each string. Worst case every
// attribute which is found is in the USER namespace so we need a
// dynamically array which is this long
char** usefulBits = (char**) malloc( sizeof(char*) * numEntries );
if ( usefulBits == NULL )
errExit( "usefulBits malloc in listAttrs" );
// fill usefulBits with NULL for now
for ( ssize_t i = 0; i < numEntries; i++ )
usefulBits[i] = NULL;
// now put in the relevant pointers
size_t numEntriesRemaining = numEntries;
usefulBits[0] = seekToInterestingPart( (char*) data, &numEntriesRemaining );
for ( ssize_t i = 1; i < numEntries; i++ ) { // don't do this too many times
// returns NULL if we have run out
usefulBits[i] = seekToInterestingPart( usefulBits[i-1] + strlen(usefulBits[i-1]) + 6, &numEntriesRemaining );
}
// work out how many entries we have
ssize_t numInterestingEntries = 0;
for ( ssize_t i = 0; i < numEntries; i++ ) {
if ( usefulBits[i] == NULL ) {
// this is all of them
break;
} else {
numInterestingEntries++;
}
}
// now we overwrite data with the newly edited list. This will definitely
// fit because we have removed things from what was previously in data
// for the same reason we can be sure that we won't be overwriting ourself
// these for loops could all be combined however this form is better for readability
char* currPos = (char*) data;
for ( ssize_t i = 0; i < numInterestingEntries; i++ ) {
size_t size = strlen( usefulBits[i] );
strcpy( currPos, usefulBits[i] );
currPos += ( size + 2 );
}
free( usefulBits );
return numInterestingEntries;
}
#endif // GNU+Linux