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
|
#include "Python.h"
#include <sys/types.h>
#ifdef __FreeBSD__
#include <sys/extattr.h>
#elif defined(__SUN__) || defined(__sun__) || defined(__sun)
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <alloca.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
#define XATTR_CREATE 0x1
#define XATTR_REPLACE 0x2
/* Converts a freebsd format attribute list into a NULL terminated list.
* The first byte 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) (unsigned char)namebuf[offset];
memmove(namebuf+offset, namebuf+offset+1, length);
namebuf[offset+length] = '\0';
offset += length+1;
}
}
static ssize_t xattr_getxattr(const char *path, const char *name,
void *value, ssize_t size, u_int32_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);
}
}
static ssize_t xattr_setxattr(const char *path, const char *name,
void *value, ssize_t size, u_int32_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 its
* 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;
}
}
static 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);
}
}
static 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;
}
static ssize_t xattr_fgetxattr(int fd, const char *name, void *value,
ssize_t size, u_int32_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);
}
}
static ssize_t xattr_fsetxattr(int fd, const char *name, void *value,
ssize_t size, u_int32_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;
}
}
static 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);
}
}
static 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__) || 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 XATTR_CREATE 0x1
#define XATTR_REPLACE 0x2
#ifndef u_int32_t
#define u_int32_t uint32_t
#endif
static ssize_t xattr_fgetxattr(int fd, const char *name, void *value,
ssize_t size, u_int32_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;
}
static ssize_t xattr_getxattr(const char *path, const char *name,
void *value, ssize_t size, u_int32_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;
}
static ssize_t xattr_fsetxattr(int fd, const char *name, void *value,
ssize_t size, u_int32_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;
}
static ssize_t xattr_setxattr(const char *path, const char *name,
void *value, ssize_t size, u_int32_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;
}
static 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;
}
static 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;
}
static 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;
}
static ssize_t xattr_flistxattr(int fd, char *namebuf, size_t size, int options)
{
int xfd;
xfd = openat(fd, ".", O_RDONLY | O_XATTR);
return xattr_xflistxattr(xfd, namebuf, size, options);
}
static 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
static ssize_t xattr_getxattr(const char *path, const char *name, void *value, ssize_t size, u_int32_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);
}
}
static ssize_t xattr_setxattr(const char *path, const char *name, void *value, ssize_t size, u_int32_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);
}
}
static 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);
}
}
static 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);
}
}
static ssize_t xattr_fgetxattr(int fd, const char *name, void *value, ssize_t size, u_int32_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);
}
}
static ssize_t xattr_fsetxattr(int fd, const char *name, void *value, ssize_t size, u_int32_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 -1;
} else {
return fsetxattr(fd, name, value, size, options);
}
}
static 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);
}
}
static 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
/* define these for use in python (see below) */
#define XATTR_XATTR_NOFOLLOW XATTR_NOFOLLOW
#define XATTR_XATTR_CREATE XATTR_CREATE
#define XATTR_XATTR_REPLACE XATTR_REPLACE
#define XATTR_XATTR_NOSECURITY XATTR_NOSECURITY
#endif
#ifndef XATTR_MAXNAMELEN
#define XATTR_MAXNAMELEN 127
#endif
|