-
Notifications
You must be signed in to change notification settings - Fork 56
/
isdk_xattr.c
618 lines (538 loc) · 16.2 KB
/
isdk_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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
Copyright (c) 2012 Riceball LEE([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
//xattr functions...
#include <errno.h>
#include <string.h>
#include "isdk_xattr.h"
#define XATTR_PREFIX "user."
#ifdef __FreeBSD__
#include <sys/extattr.h>
#elif defined(__SUN__) || defined(__sun__)
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#else
#include <sys/xattr.h>
#endif
#ifdef __FreeBSD__
/* FreeBSD compatibility API */
#define XATTR_XATTR_NOFOLLOW 0x0001
#define XATTR_XATTR_CREATE 0x0002
#define XATTR_XATTR_REPLACE 0x0004
#define XATTR_XATTR_NOSECURITY 0x0008
/* Converts a freebsd format attribute list into a NULL terminated list.
* While the man page on extattr_list_file says it is NULL terminated,
* it is actually the first byte that is the length of the
* following attribute.
*/
static void convert_bsd_list(char *namebuf, size_t size)
{
size_t offset = 0;
while(offset < size) {
int length = (int) namebuf[offset];
memmove(namebuf+offset, namebuf+offset+1, length);
namebuf[offset+length] = '\0';
offset += length+1;
}
}
ssize_t xattr_getxattr(const char *path, const char *name,
void *value, ssize_t size, uint32_t position,
int options)
{
if (position != 0 ||
!(options == 0 || options == XATTR_XATTR_NOFOLLOW)) {
return -1;
}
if (options & XATTR_XATTR_NOFOLLOW) {
return extattr_get_link(path, EXTATTR_NAMESPACE_USER,
name, value, size);
}
else {
return extattr_get_file(path, EXTATTR_NAMESPACE_USER,
name, value, size);
}
}
ssize_t xattr_setxattr(const char *path, const char *name,
void *value, ssize_t size, uint32_t position,
int options)
{
int rv = 0;
int nofollow;
if (position != 0) {
return -1;
}
nofollow = options & XATTR_XATTR_NOFOLLOW;
options &= ~XATTR_XATTR_NOFOLLOW;
if (options == XATTR_XATTR_CREATE ||
options == XATTR_XATTR_REPLACE) {
/* meh. FreeBSD doesn't really have this in it's
* API... Oh well.
*/
}
else if (options != 0) {
return -1;
}
if (nofollow) {
rv = extattr_set_link(path, EXTATTR_NAMESPACE_USER,
name, value, size);
}
else {
rv = extattr_set_file(path, EXTATTR_NAMESPACE_USER,
name, value, size);
}
/* freebsd returns the written length on success, not zero. */
if (rv >= 0) {
return 0;
}
else {
return rv;
}
}
ssize_t xattr_removexattr(const char *path, const char *name,
int options)
{
if (!(options == 0 ||
options == XATTR_XATTR_NOFOLLOW)) {
return -1;
}
if (options & XATTR_XATTR_NOFOLLOW) {
return extattr_delete_link(path, EXTATTR_NAMESPACE_USER, name);
}
else {
return extattr_delete_file(path, EXTATTR_NAMESPACE_USER, name);
}
}
ssize_t xattr_listxattr(const char *path, char *namebuf,
size_t size, int options)
{
ssize_t rv = 0;
if (!(options == 0 ||
options == XATTR_XATTR_NOFOLLOW)) {
return -1;
}
if (options & XATTR_XATTR_NOFOLLOW) {
rv = extattr_list_link(path, EXTATTR_NAMESPACE_USER, namebuf, size);
}
else {
rv = extattr_list_file(path, EXTATTR_NAMESPACE_USER, namebuf, size);
}
if (rv > 0 && namebuf) {
convert_bsd_list(namebuf, rv);
}
return rv;
}
ssize_t xattr_fgetxattr(int fd, const char *name, void *value,
ssize_t size, uint32_t position, int options)
{
if (position != 0 ||
!(options == 0 || options == XATTR_XATTR_NOFOLLOW)) {
return -1;
}
if (options & XATTR_XATTR_NOFOLLOW) {
return -1;
}
else {
return extattr_get_fd(fd, EXTATTR_NAMESPACE_USER, name, value, size);
}
}
ssize_t xattr_fsetxattr(int fd, const char *name, void *value,
ssize_t size, uint32_t position, int options)
{
int rv = 0;
int nofollow;
if (position != 0) {
return -1;
}
nofollow = options & XATTR_XATTR_NOFOLLOW;
options &= ~XATTR_XATTR_NOFOLLOW;
if (options == XATTR_XATTR_CREATE ||
options == XATTR_XATTR_REPLACE) {
/* freebsd noop */
}
else if (options != 0) {
return -1;
}
if (nofollow) {
return -1;
}
else {
rv = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER,
name, value, size);
}
/* freebsd returns the written length on success, not zero. */
if (rv >= 0) {
return 0;
}
else {
return rv;
}
}
ssize_t xattr_fremovexattr(int fd, const char *name, int options)
{
if (!(options == 0 ||
options == XATTR_XATTR_NOFOLLOW)) {
return -1;
}
if (options & XATTR_XATTR_NOFOLLOW) {
return -1;
}
else {
return extattr_delete_fd(fd, EXTATTR_NAMESPACE_USER, name);
}
}
ssize_t xattr_flistxattr(int fd, char *namebuf, size_t size, int options)
{
ssize_t rv = 0;
if (!(options == 0 ||
options == XATTR_XATTR_NOFOLLOW)) {
return -1;
}
if (options & XATTR_XATTR_NOFOLLOW) {
return -1;
}
else {
rv = extattr_list_fd(fd, EXTATTR_NAMESPACE_USER, namebuf, size);
}
if (rv > 0 && namebuf) {
convert_bsd_list(namebuf, rv);
}
return rv;
}
#elif defined(__SUN__) || defined(__sun__)
/* Solaris 9 and later compatibility API */
#define XATTR_XATTR_NOFOLLOW 0x0001
#define XATTR_XATTR_CREATE 0x0002
#define XATTR_XATTR_REPLACE 0x0004
#define XATTR_XATTR_NOSECURITY 0x0008
#define uint32_t unsigned int
ssize_t xattr_fgetxattr(int fd, const char *name, void *value,
ssize_t size, uint32_t position, int options)
{
int xfd;
ssize_t bytes;
struct stat statbuf;
/* XXX should check that name does not have / characters in it */
xfd = openat(fd, name, O_RDONLY | O_XATTR);
if (xfd == -1) {
return -1;
}
if (lseek(xfd, position, SEEK_SET) == -1) {
close(xfd);
return -1;
}
if (value == NULL) {
if (fstat(xfd, &statbuf) == -1) {
close(xfd);
return -1;
}
close(xfd);
return statbuf.st_size;
}
/* XXX should keep reading until the buffer is exhausted or EOF */
bytes = read(xfd, value, size);
close(xfd);
return bytes;
}
ssize_t xattr_getxattr(const char *path, const char *name,
void *value, ssize_t size, uint32_t position,
int options)
{
int fd;
ssize_t bytes;
if (position != 0 ||
!(options == 0 || options == XATTR_XATTR_NOFOLLOW)) {
return -1;
}
fd = open(path,
O_RDONLY |
((options & XATTR_XATTR_NOFOLLOW) ? O_NOFOLLOW : 0));
if (fd == -1) {
return -1;
}
bytes = xattr_fgetxattr(fd, name, value, size, position, options);
close(fd);
return bytes;
}
ssize_t xattr_fsetxattr(int fd, const char *name, void *value,
ssize_t size, uint32_t position, int options)
{
int xfd;
ssize_t bytes = 0;
/* XXX should check that name does not have / characters in it */
xfd = openat(fd, name, O_XATTR | O_TRUNC |
((options & XATTR_XATTR_CREATE) ? O_EXCL : 0) |
((options & XATTR_XATTR_NOFOLLOW) ? O_NOFOLLOW : 0) |
((options & XATTR_XATTR_REPLACE) ? O_RDWR : O_WRONLY|O_CREAT),
0644);
if (xfd == -1) {
return -1;
}
while (size > 0) {
bytes = write(xfd, value, size);
if (bytes == -1) {
close(xfd);
return -1;
}
size -= bytes;
value += bytes;
}
close(xfd);
return 0;
}
ssize_t xattr_setxattr(const char *path, const char *name,
void *value, ssize_t size, uint32_t position,
int options)
{
int fd;
ssize_t bytes;
if (position != 0) {
return -1;
}
fd = open(path,
O_RDONLY | (options & XATTR_XATTR_NOFOLLOW) ? O_NOFOLLOW : 0);
if (fd == -1) {
return -1;
}
bytes = xattr_fsetxattr(fd, name, value, size, position, options);
close(fd);
return bytes;
}
ssize_t xattr_fremovexattr(int fd, const char *name, int options)
{
int xfd, status;
/* XXX should check that name does not have / characters in it */
if (!(options == 0 || options == XATTR_XATTR_NOFOLLOW)) {
return -1;
}
if (options & XATTR_XATTR_NOFOLLOW) {
return -1;
}
xfd = openat(fd, ".", O_XATTR, 0644);
if (xfd == -1) {
return -1;
}
status = unlinkat(xfd, name, 0);
close(xfd);
return status;
}
ssize_t xattr_removexattr(const char *path, const char *name,
int options)
{
int fd;
ssize_t status;
fd = open(path,
O_RDONLY | ((options & XATTR_XATTR_NOFOLLOW) ? O_NOFOLLOW : 0));
if (fd == -1) {
return -1;
}
status = xattr_fremovexattr(fd, name, options);
close(fd);
return status;
}
ssize_t xattr_xflistxattr(int xfd, char *namebuf, size_t size, int options)
{
int esize;
DIR *dirp;
struct dirent *entry;
ssize_t nsize = 0;
dirp = fdopendir(xfd);
if (dirp == NULL) {
return (-1);
}
while (entry = readdir(dirp)) {
if (strcmp(entry->d_name, ".") == 0 ||
strcmp(entry->d_name, "..") == 0)
continue;
esize = strlen(entry->d_name);
if (nsize + esize + 1 <= size) {
snprintf((char *)(namebuf + nsize), esize + 1,
entry->d_name);
}
nsize += esize + 1; /* +1 for \0 */
}
closedir(dirp);
return nsize;
}
ssize_t xattr_flistxattr(int fd, char *namebuf, size_t size, int options)
{
int xfd;
xfd = openat(fd, ".", O_RDONLY);
return xattr_xflistxattr(xfd, namebuf, size, options);
}
ssize_t xattr_listxattr(const char *path, char *namebuf,
size_t size, int options)
{
int xfd;
xfd = attropen(path, ".", O_RDONLY);
return xattr_xflistxattr(xfd, namebuf, size, options);
}
#elif !defined(XATTR_NOFOLLOW)
/* Linux compatibility API */
#define XATTR_XATTR_NOFOLLOW 0x0001
#define XATTR_XATTR_CREATE 0x0002
#define XATTR_XATTR_REPLACE 0x0004
#define XATTR_XATTR_NOSECURITY 0x0008
ssize_t xattr_getxattr(const char *path, const char *name, void *value, ssize_t size, uint32_t position, int options) {
if (position != 0 || !(options == 0 || options == XATTR_XATTR_NOFOLLOW)) {
return -1;
}
if (options & XATTR_XATTR_NOFOLLOW) {
return lgetxattr(path, name, value, size);
} else {
return getxattr(path, name, value, size);
}
}
ssize_t xattr_setxattr(const char *path, const char *name, void *value, ssize_t size, uint32_t position, int options) {
int nofollow;
if (position != 0) {
return -1;
}
nofollow = options & XATTR_XATTR_NOFOLLOW;
options &= ~XATTR_XATTR_NOFOLLOW;
if (options == XATTR_XATTR_CREATE) {
options = XATTR_CREATE;
} else if (options == XATTR_XATTR_REPLACE) {
options = XATTR_REPLACE;
} else if (options != 0) {
return -1;
}
if (nofollow) {
return lsetxattr(path, name, value, size, options);
} else {
return setxattr(path, name, value, size, options);
}
}
ssize_t xattr_removexattr(const char *path, const char *name, int options) {
if (!(options == 0 || options == XATTR_XATTR_NOFOLLOW)) {
return -1;
}
if (options & XATTR_XATTR_NOFOLLOW) {
return lremovexattr(path, name);
} else {
return removexattr(path, name);
}
}
ssize_t xattr_listxattr(const char *path, char *namebuf, size_t size, int options) {
if (!(options == 0 || options == XATTR_XATTR_NOFOLLOW)) {
return -1;
}
if (options & XATTR_XATTR_NOFOLLOW) {
return llistxattr(path, namebuf, size);
} else {
return listxattr(path, namebuf, size);
}
}
ssize_t xattr_fgetxattr(int fd, const char *name, void *value, ssize_t size, uint32_t position, int options) {
if (position != 0 || !(options == 0 || options == XATTR_XATTR_NOFOLLOW)) {
return -1;
}
if (options & XATTR_XATTR_NOFOLLOW) {
return -1;
} else {
return fgetxattr(fd, name, value, size);
}
}
ssize_t xattr_fsetxattr(int fd, const char *name, void *value, ssize_t size, uint32_t position, int options) {
int nofollow;
if (position != 0) {
return -1;
}
nofollow = options & XATTR_XATTR_NOFOLLOW;
options &= ~XATTR_XATTR_NOFOLLOW;
if (options == XATTR_XATTR_CREATE) {
options = XATTR_CREATE;
} else if (options == XATTR_XATTR_REPLACE) {
options = XATTR_REPLACE;
} else if (options != 0) {
return -1;
}
if (options & XATTR_XATTR_NOFOLLOW) {
return -1;
} else {
return fsetxattr(fd, name, value, size, options);
}
}
ssize_t xattr_fremovexattr(int fd, const char *name, int options) {
if (!(options == 0 || options == XATTR_XATTR_NOFOLLOW)) {
return -1;
}
if (options & XATTR_XATTR_NOFOLLOW) {
return -1;
} else {
return fremovexattr(fd, name);
}
}
ssize_t xattr_flistxattr(int fd, char *namebuf, size_t size, int options) {
if (!(options == 0 || options == XATTR_XATTR_NOFOLLOW)) {
return -1;
}
if (options & XATTR_XATTR_NOFOLLOW) {
return -1;
} else {
return flistxattr(fd, namebuf, size);
}
}
#else /* Mac OS X assumed */
#define xattr_getxattr getxattr
#define xattr_fgetxattr fgetxattr
#define xattr_removexattr removexattr
#define xattr_fremovexattr fremovexattr
#define xattr_setxattr setxattr
#define xattr_fsetxattr fsetxattr
#define xattr_listxattr listxattr
#define xattr_flistxattr flistxattr
#endif
bool IsXattrExists(const char* aFile, const char* aKey)
{
ssize_t vLen = xattr_getxattr(aFile, aKey, NULL, 0, 0, 0);
return vLen >= 0;
}
#ifdef ISDK_XATTR_TEST_MAIN
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include "testhelp.h"
//gcc --std=c99 -I. -o test -DHAVE_UNISTD_H -DISDK_XATTR_TEST_MAIN isdk_xattr.c sds.c zmalloc.c
int main(void) {
{
int fd = open ("mytestfile", O_WRONLY | O_CREAT | O_NONBLOCK | O_NOCTTY,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (fd != -1) close(fd);
test_cond("touch('mytestfile')", fd != -1);
sds path = sdsnew("mytestfile"), key = sdsnew("mydname"), value = sdsnew("hi world!");
test_cond("SetXattr(mytestfile, mydname, 'hi world!')", SetXattr(path, key, value)== 0);
test_cond("IsXattrExists(mytestfile, mydname)", IsXattrExists(path, key));
test_cond("IsXattrExists(mytestfile, notexists)", !IsXattrExists(path, "notexists"));
test_cond("IsXattrExists(nosuchmytestfile, notexists)", !IsXattrExists("nosuchmytestfile", "notexists"));
sds result = GetXattr(path, key);
test_cond("GetXattr(mytestfile, mydname)",
result && sdslen(result) == 9 && memcmp(result, "hi world!\0", 10) == 0
);
sdsfree(path);
sdsfree(key);
sdsfree(value);
if (result) sdsfree(result);
remove("mytestfile");
}
test_report();
return 0;
}
#endif