| 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 |
#include "tclInt.h" |
| 51 |
#include "tclPort.h" |
| 52 |
#include <utime.h> |
| 53 |
#include <grp.h> |
| 54 |
#ifndef HAVE_ST_BLKSIZE |
| 55 |
#ifndef NO_FSTATFS |
| 56 |
#include <sys/statfs.h> |
| 57 |
#endif |
| 58 |
#endif |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#define DOTREE_PRED 1 |
| 66 |
#define DOTREE_POSTD 2 |
| 67 |
#define DOTREE_F 3 |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
static int GetGroupAttribute _ANSI_ARGS_((Tcl_Interp *interp, |
| 74 |
int objIndex, Tcl_Obj *fileName, |
| 75 |
Tcl_Obj **attributePtrPtr)); |
| 76 |
static int GetOwnerAttribute _ANSI_ARGS_((Tcl_Interp *interp, |
| 77 |
int objIndex, Tcl_Obj *fileName, |
| 78 |
Tcl_Obj **attributePtrPtr)); |
| 79 |
static int GetPermissionsAttribute _ANSI_ARGS_(( |
| 80 |
Tcl_Interp *interp, int objIndex, |
| 81 |
Tcl_Obj *fileName, Tcl_Obj **attributePtrPtr)); |
| 82 |
static int SetGroupAttribute _ANSI_ARGS_((Tcl_Interp *interp, |
| 83 |
int objIndex, Tcl_Obj *fileName, |
| 84 |
Tcl_Obj *attributePtr)); |
| 85 |
static int SetOwnerAttribute _ANSI_ARGS_((Tcl_Interp *interp, |
| 86 |
int objIndex, Tcl_Obj *fileName, |
| 87 |
Tcl_Obj *attributePtr)); |
| 88 |
static int SetPermissionsAttribute _ANSI_ARGS_(( |
| 89 |
Tcl_Interp *interp, int objIndex, |
| 90 |
Tcl_Obj *fileName, Tcl_Obj *attributePtr)); |
| 91 |
static int GetModeFromPermString _ANSI_ARGS_(( |
| 92 |
Tcl_Interp *interp, char *modeStringPtr, |
| 93 |
mode_t *modePtr)); |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
typedef int (TraversalProc) _ANSI_ARGS_((Tcl_DString *srcPtr, |
| 100 |
Tcl_DString *dstPtr, CONST Tcl_StatBuf *statBufPtr, int type, |
| 101 |
Tcl_DString *errorPtr)); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
enum { |
| 108 |
UNIX_GROUP_ATTRIBUTE, |
| 109 |
UNIX_OWNER_ATTRIBUTE, |
| 110 |
UNIX_PERMISSIONS_ATTRIBUTE |
| 111 |
}; |
| 112 |
|
| 113 |
CONST char *tclpFileAttrStrings[] = { |
| 114 |
"-group", |
| 115 |
"-owner", |
| 116 |
"-permissions", |
| 117 |
(char *) NULL |
| 118 |
}; |
| 119 |
|
| 120 |
CONST TclFileAttrProcs tclpFileAttrProcs[] = { |
| 121 |
{GetGroupAttribute, SetGroupAttribute}, |
| 122 |
{GetOwnerAttribute, SetOwnerAttribute}, |
| 123 |
{GetPermissionsAttribute, SetPermissionsAttribute} |
| 124 |
}; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
static int CopyFile _ANSI_ARGS_((CONST char *src, |
| 131 |
CONST char *dst, CONST Tcl_StatBuf *statBufPtr)); |
| 132 |
static int CopyFileAtts _ANSI_ARGS_((CONST char *src, |
| 133 |
CONST char *dst, CONST Tcl_StatBuf *statBufPtr)); |
| 134 |
static int DoCopyFile _ANSI_ARGS_((CONST char *srcPtr, |
| 135 |
CONST char *dstPtr)); |
| 136 |
static int DoCreateDirectory _ANSI_ARGS_((CONST char *pathPtr)); |
| 137 |
static int DoRemoveDirectory _ANSI_ARGS_((Tcl_DString *pathPtr, |
| 138 |
int recursive, Tcl_DString *errorPtr)); |
| 139 |
static int DoRenameFile _ANSI_ARGS_((CONST char *src, |
| 140 |
CONST char *dst)); |
| 141 |
static int TraversalCopy _ANSI_ARGS_((Tcl_DString *srcPtr, |
| 142 |
Tcl_DString *dstPtr, CONST Tcl_StatBuf *statBufPtr, |
| 143 |
int type, Tcl_DString *errorPtr)); |
| 144 |
static int TraversalDelete _ANSI_ARGS_((Tcl_DString *srcPtr, |
| 145 |
Tcl_DString *dstPtr, CONST Tcl_StatBuf *statBufPtr, |
| 146 |
int type, Tcl_DString *errorPtr)); |
| 147 |
static int TraverseUnixTree _ANSI_ARGS_(( |
| 148 |
TraversalProc *traversalProc, |
| 149 |
Tcl_DString *sourcePtr, Tcl_DString *destPtr, |
| 150 |
Tcl_DString *errorPtr)); |
| 151 |
|
| 152 |
#ifdef PURIFY |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
static char * Realpath _ANSI_ARGS_((CONST char *path, |
| 160 |
char *resolved)); |
| 161 |
|
| 162 |
char * |
| 163 |
Realpath(path, resolved) |
| 164 |
CONST char *path; |
| 165 |
char *resolved; |
| 166 |
{ |
| 167 |
memset(resolved, 0, MAXPATHLEN); |
| 168 |
return realpath(path, resolved); |
| 169 |
} |
| 170 |
#else |
| 171 |
#define Realpath realpath |
| 172 |
#endif |
| 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 |
int |
| 211 |
TclpObjRenameFile(srcPathPtr, destPathPtr) |
| 212 |
Tcl_Obj *srcPathPtr; |
| 213 |
Tcl_Obj *destPathPtr; |
| 214 |
{ |
| 215 |
return DoRenameFile(Tcl_FSGetNativePath(srcPathPtr), |
| 216 |
Tcl_FSGetNativePath(destPathPtr)); |
| 217 |
} |
| 218 |
|
| 219 |
static int |
| 220 |
DoRenameFile(src, dst) |
| 221 |
CONST char *src; |
| 222 |
|
| 223 |
CONST char *dst; |
| 224 |
|
| 225 |
{ |
| 226 |
if (rename(src, dst) == 0) { |
| 227 |
return TCL_OK; |
| 228 |
} |
| 229 |
if (errno == ENOTEMPTY) { |
| 230 |
errno = EEXIST; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
if (errno == EIO) { |
| 240 |
errno = EINVAL; |
| 241 |
} |
| 242 |
|
| 243 |
#ifndef NO_REALPATH |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
if (errno == EINVAL) { |
| 252 |
char srcPath[MAXPATHLEN], dstPath[MAXPATHLEN]; |
| 253 |
DIR *dirPtr; |
| 254 |
Tcl_DirEntry *dirEntPtr; |
| 255 |
|
| 256 |
if ((Realpath((char *) src, srcPath) != NULL) |
| 257 |
&& (Realpath((char *) dst, dstPath) != NULL) |
| 258 |
&& (strncmp(srcPath, dstPath, strlen(srcPath)) != 0)) { |
| 259 |
dirPtr = opendir(dst); |
| 260 |
if (dirPtr != NULL) { |
| 261 |
while (1) { |
| 262 |
dirEntPtr = TclOSreaddir(dirPtr); |
| 263 |
if (dirEntPtr == NULL) { |
| 264 |
break; |
| 265 |
} |
| 266 |
if ((strcmp(dirEntPtr->d_name, ".") != 0) && |
| 267 |
(strcmp(dirEntPtr->d_name, "..") != 0)) { |
| 268 |
errno = EEXIST; |
| 269 |
closedir(dirPtr); |
| 270 |
return TCL_ERROR; |
| 271 |
} |
| 272 |
} |
| 273 |
closedir(dirPtr); |
| 274 |
} |
| 275 |
} |
| 276 |
errno = EINVAL; |
| 277 |
} |
| 278 |
#endif |
| 279 |
|
| 280 |
if (strcmp(src, "/") == 0) { |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
errno = EINVAL; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
return TCL_ERROR; |
| 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 |
int |
| 327 |
TclpObjCopyFile(srcPathPtr, destPathPtr) |
| 328 |
Tcl_Obj *srcPathPtr; |
| 329 |
Tcl_Obj *destPathPtr; |
| 330 |
{ |
| 331 |
return DoCopyFile(Tcl_FSGetNativePath(srcPathPtr), |
| 332 |
Tcl_FSGetNativePath(destPathPtr)); |
| 333 |
} |
| 334 |
|
| 335 |
static int |
| 336 |
DoCopyFile(src, dst) |
| 337 |
CONST char *src; |
| 338 |
CONST char *dst; |
| 339 |
{ |
| 340 |
Tcl_StatBuf srcStatBuf, dstStatBuf; |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
if (TclOSlstat(src, &srcStatBuf) != 0) { |
| 347 |
return TCL_ERROR; |
| 348 |
} |
| 349 |
if (S_ISDIR(srcStatBuf.st_mode)) { |
| 350 |
errno = EISDIR; |
| 351 |
return TCL_ERROR; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
if (TclOSlstat(dst, &dstStatBuf) == 0) { |
| 360 |
if (S_ISDIR(dstStatBuf.st_mode)) { |
| 361 |
errno = EISDIR; |
| 362 |
return TCL_ERROR; |
| 363 |
} |
| 364 |
} |
| 365 |
if (unlink(dst) != 0) { |
| 366 |
if (errno != ENOENT) { |
| 367 |
return TCL_ERROR; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
switch ((int) (srcStatBuf.st_mode & S_IFMT)) { |
| 372 |
#ifndef DJGPP |
| 373 |
case S_IFLNK: { |
| 374 |
char link[MAXPATHLEN]; |
| 375 |
int length; |
| 376 |
|
| 377 |
length = readlink(src, link, sizeof(link)); |
| 378 |
if (length == -1) { |
| 379 |
return TCL_ERROR; |
| 380 |
} |
| 381 |
link[length] = '\0'; |
| 382 |
if (symlink(link, dst) < 0) { |
| 383 |
return TCL_ERROR; |
| 384 |
} |
| 385 |
break; |
| 386 |
} |
| 387 |
#endif |
| 388 |
case S_IFBLK: |
| 389 |
case S_IFCHR: { |
| 390 |
if (mknod(dst, srcStatBuf.st_mode, |
| 391 |
srcStatBuf.st_rdev) < 0) { |
| 392 |
return TCL_ERROR; |
| 393 |
} |
| 394 |
return CopyFileAtts(src, dst, &srcStatBuf); |
| 395 |
} |
| 396 |
case S_IFIFO: { |
| 397 |
if (mkfifo(dst, srcStatBuf.st_mode) < 0) { |
| 398 |
return TCL_ERROR; |
| 399 |
} |
| 400 |
return CopyFileAtts(src, dst, &srcStatBuf); |
| 401 |
} |
| 402 |
default: { |
| 403 |
return CopyFile(src, dst, &srcStatBuf); |
| 404 |
} |
| 405 |
} |
| 406 |
return TCL_OK; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
static int |
| 427 |
CopyFile(src, dst, statBufPtr) |
| 428 |
CONST char *src; |
| 429 |
CONST char *dst; |
| 430 |
|
| 431 |
CONST Tcl_StatBuf *statBufPtr; |
| 432 |
|
| 433 |
{ |
| 434 |
int srcFd; |
| 435 |
int dstFd; |
| 436 |
u_int blockSize; |
| 437 |
char *buffer; |
| 438 |
size_t nread; |
| 439 |
|
| 440 |
if ((srcFd = TclOSopen(src, O_RDONLY, 0)) < 0) { |
| 441 |
return TCL_ERROR; |
| 442 |
} |
| 443 |
|
| 444 |
dstFd = TclOSopen(dst, O_CREAT|O_TRUNC|O_WRONLY, |
| 445 |
statBufPtr->st_mode); |
| 446 |
if (dstFd < 0) { |
| 447 |
close(srcFd); |
| 448 |
return TCL_ERROR; |
| 449 |
} |
| 450 |
|
| 451 |
#ifdef HAVE_ST_BLKSIZE |
| 452 |
blockSize = statBufPtr->st_blksize; |
| 453 |
#else |
| 454 |
#ifndef NO_FSTATFS |
| 455 |
{ |
| 456 |
struct statfs fs; |
| 457 |
if (fstatfs(srcFd, &fs, sizeof(fs), 0) == 0) { |
| 458 |
blockSize = fs.f_bsize; |
| 459 |
} else { |
| 460 |
blockSize = 4096; |
| 461 |
} |
| 462 |
} |
| 463 |
#else |
| 464 |
blockSize = 4096; |
| 465 |
#endif |
| 466 |
#endif |
| 467 |
|
| 468 |
buffer = ckalloc(blockSize); |
| 469 |
while (1) { |
| 470 |
nread = read(srcFd, buffer, blockSize); |
| 471 |
if ((nread == -1) || (nread == 0)) { |
| 472 |
break; |
| 473 |
} |
| 474 |
if (write(dstFd, buffer, nread) != nread) { |
| 475 |
nread = (size_t) -1; |
| 476 |
break; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
ckfree(buffer); |
| 481 |
close(srcFd); |
| 482 |
if ((close(dstFd) != 0) || (nread == -1)) { |
| 483 |
unlink(dst); |
| 484 |
return TCL_ERROR; |
| 485 |
} |
| 486 |
if (CopyFileAtts(src, dst, statBufPtr) == TCL_ERROR) { |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
unlink(dst); |
| 494 |
return TCL_ERROR; |
| 495 |
} |
| 496 |
return TCL_OK; |
| 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 |
int |
| 522 |
TclpObjDeleteFile(pathPtr) |
| 523 |
Tcl_Obj *pathPtr; |
| 524 |
{ |
| 525 |
return TclpDeleteFile(Tcl_FSGetNativePath(pathPtr)); |
| 526 |
} |
| 527 |
|
| 528 |
int |
| 529 |
TclpDeleteFile(path) |
| 530 |
CONST char *path; |
| 531 |
{ |
| 532 |
if (unlink(path) != 0) { |
| 533 |
return TCL_ERROR; |
| 534 |
} |
| 535 |
return TCL_OK; |
| 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 |
int |
| 565 |
TclpObjCreateDirectory(pathPtr) |
| 566 |
Tcl_Obj *pathPtr; |
| 567 |
{ |
| 568 |
return DoCreateDirectory(Tcl_FSGetNativePath(pathPtr)); |
| 569 |
} |
| 570 |
|
| 571 |
static int |
| 572 |
DoCreateDirectory(path) |
| 573 |
CONST char *path; |
| 574 |
{ |
| 575 |
mode_t mode; |
| 576 |
|
| 577 |
mode = umask(0); |
| 578 |
umask(mode); |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
mode = (0777 & ~mode) | S_IRUSR | S_IWUSR | S_IXUSR; |
| 585 |
|
| 586 |
if (mkdir(path, mode) != 0) { |
| 587 |
return TCL_ERROR; |
| 588 |
} |
| 589 |
return TCL_OK; |
| 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 |
int |
| 619 |
TclpObjCopyDirectory(srcPathPtr, destPathPtr, errorPtr) |
| 620 |
Tcl_Obj *srcPathPtr; |
| 621 |
Tcl_Obj *destPathPtr; |
| 622 |
Tcl_Obj **errorPtr; |
| 623 |
{ |
| 624 |
Tcl_DString ds; |
| 625 |
Tcl_DString srcString, dstString; |
| 626 |
int ret; |
| 627 |
Tcl_Obj *transPtr; |
| 628 |
|
| 629 |
transPtr = Tcl_FSGetTranslatedPath(NULL,srcPathPtr); |
| 630 |
Tcl_UtfToExternalDString(NULL, |
| 631 |
(transPtr != NULL ? Tcl_GetString(transPtr) : NULL), |
| 632 |
-1, &srcString); |
| 633 |
if (transPtr != NULL) { |
| 634 |
Tcl_DecrRefCount(transPtr); |
| 635 |
} |
| 636 |
transPtr = Tcl_FSGetTranslatedPath(NULL,destPathPtr); |
| 637 |
Tcl_UtfToExternalDString(NULL, |
| 638 |
(transPtr != NULL ? Tcl_GetString(transPtr) : NULL), |
| 639 |
-1, &dstString); |
| 640 |
if (transPtr != NULL) { |
| 641 |
Tcl_DecrRefCount(transPtr); |
| 642 |
} |
| 643 |
|
| 644 |
ret = TraverseUnixTree(TraversalCopy, &srcString, &dstString, &ds); |
| 645 |
|
| 646 |
Tcl_DStringFree(&srcString); |
| 647 |
Tcl_DStringFree(&dstString); |
| 648 |
|
| 649 |
if (ret != TCL_OK) { |
| 650 |
*errorPtr = Tcl_NewStringObj(Tcl_DStringValue(&ds), -1); |
| 651 |
Tcl_DStringFree(&ds); |
| 652 |
Tcl_IncrRefCount(*errorPtr); |
| 653 |
} |
| 654 |
return ret; |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
int |
| 685 |
TclpObjRemoveDirectory(pathPtr, recursive, errorPtr) |
| 686 |
Tcl_Obj *pathPtr; |
| 687 |
int recursive; |
| 688 |
Tcl_Obj **errorPtr; |
| 689 |
{ |
| 690 |
Tcl_DString ds; |
| 691 |
Tcl_DString pathString; |
| 692 |
int ret; |
| 693 |
Tcl_Obj *transPtr = Tcl_FSGetTranslatedPath(NULL, pathPtr); |
| 694 |
|
| 695 |
Tcl_UtfToExternalDString(NULL, |
| 696 |
(transPtr != NULL ? Tcl_GetString(transPtr) : NULL), |
| 697 |
-1, &pathString); |
| 698 |
if (transPtr != NULL) { |
| 699 |
Tcl_DecrRefCount(transPtr); |
| 700 |
} |
| 701 |
ret = DoRemoveDirectory(&pathString, recursive, &ds); |
| 702 |
Tcl_DStringFree(&pathString); |
| 703 |
|
| 704 |
if (ret != TCL_OK) { |
| 705 |
*errorPtr = Tcl_NewStringObj(Tcl_DStringValue(&ds), -1); |
| 706 |
Tcl_DStringFree(&ds); |
| 707 |
Tcl_IncrRefCount(*errorPtr); |
| 708 |
} |
| 709 |
return ret; |
| 710 |
} |
| 711 |
|
| 712 |
static int |
| 713 |
DoRemoveDirectory(pathPtr, recursive, errorPtr) |
| 714 |
Tcl_DString *pathPtr; |
| 715 |
|
| 716 |
int recursive; |
| 717 |
|
| 718 |
|
| 719 |
Tcl_DString *errorPtr; |
| 720 |
|
| 721 |
|
| 722 |
{ |
| 723 |
CONST char *path; |
| 724 |
mode_t oldPerm = 0; |
| 725 |
int result; |
| 726 |
|
| 727 |
path = Tcl_DStringValue(pathPtr); |
| 728 |
|
| 729 |
if (recursive != 0) { |
| 730 |
|
| 731 |
Tcl_StatBuf statBuf; |
| 732 |
int newPerm; |
| 733 |
|
| 734 |
if (TclOSstat(path, &statBuf) == 0) { |
| 735 |
oldPerm = (mode_t) (statBuf.st_mode & 0x00007FFF); |
| 736 |
} |
| 737 |
|
| 738 |
newPerm = oldPerm | (64+128+256); |
| 739 |
chmod(path, (mode_t) newPerm); |
| 740 |
} |
| 741 |
|
| 742 |
if (rmdir(path) == 0) { |
| 743 |
return TCL_OK; |
| 744 |
} |
| 745 |
if (errno == ENOTEMPTY) { |
| 746 |
errno = EEXIST; |
| 747 |
} |
| 748 |
|
| 749 |
result = TCL_OK; |
| 750 |
if ((errno != EEXIST) || (recursive == 0)) { |
| 751 |
if (errorPtr != NULL) { |
| 752 |
Tcl_ExternalToUtfDString(NULL, path, -1, errorPtr); |
| 753 |
} |
| 754 |
result = TCL_ERROR; |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
if (result == TCL_OK) { |
| 763 |
result = TraverseUnixTree(TraversalDelete, pathPtr, NULL, errorPtr); |
| 764 |
} |
| 765 |
|
| 766 |
if ((result != TCL_OK) && (recursive != 0)) { |
| 767 |
|
| 768 |
chmod(path, oldPerm); |
| 769 |
} |
| 770 |
return result; |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
static int |
| 796 |
TraverseUnixTree(traverseProc, sourcePtr, targetPtr, errorPtr) |
| 797 |
TraversalProc *traverseProc; |
| 798 |
|
| 799 |
Tcl_DString *sourcePtr; |
| 800 |
|
| 801 |
Tcl_DString *targetPtr; |
| 802 |
|
| 803 |
Tcl_DString *errorPtr; |
| 804 |
|
| 805 |
|
| 806 |
{ |
| 807 |
Tcl_StatBuf statBuf; |
| 808 |
CONST char *source, *errfile; |
| 809 |
int result, sourceLen; |
| 810 |
int targetLen; |
| 811 |
Tcl_DirEntry *dirEntPtr; |
| 812 |
DIR *dirPtr; |
| 813 |
|
| 814 |
errfile = NULL; |
| 815 |
result = TCL_OK; |
| 816 |
targetLen = 0; |
| 817 |
|
| 818 |
source = Tcl_DStringValue(sourcePtr); |
| 819 |
if (TclOSlstat(source, &statBuf) != 0) { |
| 820 |
errfile = source; |
| 821 |
goto end; |
| 822 |
} |
| 823 |
if (!S_ISDIR(statBuf.st_mode)) { |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
return (*traverseProc)(sourcePtr, targetPtr, &statBuf, DOTREE_F, |
| 829 |
errorPtr); |
| 830 |
} |
| 831 |
dirPtr = opendir(source); |
| 832 |
if (dirPtr == NULL) { |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
errfile = source; |
| 838 |
goto end; |
| 839 |
} |
| 840 |
result = (*traverseProc)(sourcePtr, targetPtr, &statBuf, DOTREE_PRED, |
| 841 |
errorPtr); |
| 842 |
if (result != TCL_OK) { |
| 843 |
closedir(dirPtr); |
| 844 |
return result; |
| 845 |
} |
| 846 |
|
| 847 |
Tcl_DStringAppend(sourcePtr, "/", 1); |
| 848 |
sourceLen = Tcl_DStringLength(sourcePtr); |
| 849 |
|
| 850 |
if (targetPtr != NULL) { |
| 851 |
Tcl_DStringAppend(targetPtr, "/", 1); |
| 852 |
targetLen = Tcl_DStringLength(targetPtr); |
| 853 |
} |
| 854 |
|
| 855 |
while ((dirEntPtr = TclOSreaddir(dirPtr)) != NULL) { |
| 856 |
if ((dirEntPtr->d_name[0] == '.') |
| 857 |
&& ((dirEntPtr->d_name[1] == '\0') |
| 858 |
|| (strcmp(dirEntPtr->d_name, "..") == 0))) { |
| 859 |
continue; |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
Tcl_DStringAppend(sourcePtr, dirEntPtr->d_name, -1); |
| 867 |
if (targetPtr != NULL) { |
| 868 |
Tcl_DStringAppend(targetPtr, dirEntPtr->d_name, -1); |
| 869 |
} |
| 870 |
result = TraverseUnixTree(traverseProc, sourcePtr, targetPtr, |
| 871 |
errorPtr); |
| 872 |
if (result != TCL_OK) { |
| 873 |
break; |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
Tcl_DStringSetLength(sourcePtr, sourceLen); |
| 881 |
if (targetPtr != NULL) { |
| 882 |
Tcl_DStringSetLength(targetPtr, targetLen); |
| 883 |
} |
| 884 |
} |
| 885 |
closedir(dirPtr); |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
Tcl_DStringSetLength(sourcePtr, sourceLen - 1); |
| 892 |
if (targetPtr != NULL) { |
| 893 |
Tcl_DStringSetLength(targetPtr, targetLen - 1); |
| 894 |
} |
| 895 |
|
| 896 |
if (result == TCL_OK) { |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
result = (*traverseProc)(sourcePtr, targetPtr, &statBuf, DOTREE_POSTD, |
| 903 |
errorPtr); |
| 904 |
} |
| 905 |
end: |
| 906 |
if (errfile != NULL) { |
| 907 |
if (errorPtr != NULL) { |
| 908 |
Tcl_ExternalToUtfDString(NULL, errfile, -1, errorPtr); |
| 909 |
} |
| 910 |
result = TCL_ERROR; |
| 911 |
} |
| 912 |
|
| 913 |
return result; |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
static int |
| 935 |
TraversalCopy(srcPtr, dstPtr, statBufPtr, type, errorPtr) |
| 936 |
Tcl_DString *srcPtr; |
| 937 |
Tcl_DString *dstPtr; |
| 938 |
CONST Tcl_StatBuf *statBufPtr; |
| 939 |
|
| 940 |
int type; |
| 941 |
Tcl_DString *errorPtr; |
| 942 |
|
| 943 |
|
| 944 |
{ |
| 945 |
switch (type) { |
| 946 |
case DOTREE_F: |
| 947 |
if (DoCopyFile(Tcl_DStringValue(srcPtr), |
| 948 |
Tcl_DStringValue(dstPtr)) == TCL_OK) { |
| 949 |
return TCL_OK; |
| 950 |
} |
| 951 |
break; |
| 952 |
|
| 953 |
case DOTREE_PRED: |
| 954 |
if (DoCreateDirectory(Tcl_DStringValue(dstPtr)) == TCL_OK) { |
| 955 |
return TCL_OK; |
| 956 |
} |
| 957 |
break; |
| 958 |
|
| 959 |
case DOTREE_POSTD: |
| 960 |
if (CopyFileAtts(Tcl_DStringValue(srcPtr), |
| 961 |
Tcl_DStringValue(dstPtr), statBufPtr) == TCL_OK) { |
| 962 |
return TCL_OK; |
| 963 |
} |
| 964 |
break; |
| 965 |
|
| 966 |
} |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
if (errorPtr != NULL) { |
| 974 |
Tcl_ExternalToUtfDString(NULL, Tcl_DStringValue(dstPtr), |
| 975 |
Tcl_DStringLength(dstPtr), errorPtr); |
| 976 |
} |
| 977 |
return TCL_ERROR; |
| 978 |
} |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
static int |
| 1000 |
TraversalDelete(srcPtr, ignore, statBufPtr, type, errorPtr) |
| 1001 |
Tcl_DString *srcPtr; |
| 1002 |
Tcl_DString *ignore; |
| 1003 |
CONST Tcl_StatBuf *statBufPtr; |
| 1004 |
|
| 1005 |
int type; |
| 1006 |
Tcl_DString *errorPtr; |
| 1007 |
|
| 1008 |
|
| 1009 |
{ |
| 1010 |
switch (type) { |
| 1011 |
case DOTREE_F: { |
| 1012 |
if (TclpDeleteFile(Tcl_DStringValue(srcPtr)) == 0) { |
| 1013 |
return TCL_OK; |
| 1014 |
} |
| 1015 |
break; |
| 1016 |
} |
| 1017 |
case DOTREE_PRED: { |
| 1018 |
return TCL_OK; |
| 1019 |
} |
| 1020 |
case DOTREE_POSTD: { |
| 1021 |
if (DoRemoveDirectory(srcPtr, 0, NULL) == 0) { |
| 1022 |
return TCL_OK; |
| 1023 |
} |
| 1024 |
break; |
| 1025 |
} |
| 1026 |
} |
| 1027 |
if (errorPtr != NULL) { |
| 1028 |
Tcl_ExternalToUtfDString(NULL, Tcl_DStringValue(srcPtr), |
| 1029 |
Tcl_DStringLength(srcPtr), errorPtr); |
| 1030 |
} |
| 1031 |
return TCL_ERROR; |
| 1032 |
} |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
static int |
| 1054 |
CopyFileAtts(src, dst, statBufPtr) |
| 1055 |
CONST char *src; |
| 1056 |
CONST char *dst; |
| 1057 |
CONST Tcl_StatBuf *statBufPtr; |
| 1058 |
|
| 1059 |
{ |
| 1060 |
struct utimbuf tval; |
| 1061 |
mode_t newMode; |
| 1062 |
|
| 1063 |
newMode = statBufPtr->st_mode |
| 1064 |
& (S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO); |
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
if (chmod(dst, newMode)) { |
| 1076 |
newMode &= ~(S_ISUID | S_ISGID); |
| 1077 |
if (chmod(dst, newMode)) { |
| 1078 |
return TCL_ERROR; |
| 1079 |
} |
| 1080 |
} |
| 1081 |
|
| 1082 |
tval.actime = statBufPtr->st_atime; |
| 1083 |
tval.modtime = statBufPtr->st_mtime; |
| 1084 |
|
| 1085 |
if (utime(dst, &tval)) { |
| 1086 |
return TCL_ERROR; |
| 1087 |
} |
| 1088 |
return TCL_OK; |
| 1089 |
} |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
static int |
| 1110 |
GetGroupAttribute(interp, objIndex, fileName, attributePtrPtr) |
| 1111 |
Tcl_Interp *interp; |
| 1112 |
int objIndex; |
| 1113 |
Tcl_Obj *fileName; |
| 1114 |
Tcl_Obj **attributePtrPtr; |
| 1115 |
{ |
| 1116 |
Tcl_StatBuf statBuf; |
| 1117 |
struct group *groupPtr; |
| 1118 |
int result; |
| 1119 |
|
| 1120 |
result = TclpObjStat(fileName, &statBuf); |
| 1121 |
|
| 1122 |
if (result != 0) { |
| 1123 |
Tcl_AppendResult(interp, "could not read \"", |
| 1124 |
Tcl_GetString(fileName), "\": ", |
| 1125 |
Tcl_PosixError(interp), (char *) NULL); |
| 1126 |
return TCL_ERROR; |
| 1127 |
} |
| 1128 |
|
| 1129 |
groupPtr = getgrgid(statBuf.st_gid); |
| 1130 |
if (groupPtr == NULL) { |
| 1131 |
*attributePtrPtr = Tcl_NewIntObj((int) statBuf.st_gid); |
| 1132 |
} else { |
| 1133 |
Tcl_DString ds; |
| 1134 |
CONST char *utf; |
| 1135 |
|
| 1136 |
utf = Tcl_ExternalToUtfDString(NULL, groupPtr->gr_name, -1, &ds); |
| 1137 |
*attributePtrPtr = Tcl_NewStringObj(utf, -1); |
| 1138 |
Tcl_DStringFree(&ds); |
| 1139 |
} |
| 1140 |
endgrent(); |
| 1141 |
return TCL_OK; |
| 1142 |
} |
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
static int |
| 1162 |
GetOwnerAttribute(interp, objIndex, fileName, attributePtrPtr) |
| 1163 |
Tcl_Interp *interp; |
| 1164 |
int objIndex; |
| 1165 |
Tcl_Obj *fileName; |
| 1166 |
Tcl_Obj **attributePtrPtr; |
| 1167 |
{ |
| 1168 |
Tcl_StatBuf statBuf; |
| 1169 |
struct passwd *pwPtr; |
| 1170 |
int result; |
| 1171 |
|
| 1172 |
result = TclpObjStat(fileName, &statBuf); |
| 1173 |
|
| 1174 |
if (result != 0) { |
| 1175 |
Tcl_AppendResult(interp, "could not read \"", |
| 1176 |
Tcl_GetString(fileName), "\": ", |
| 1177 |
Tcl_PosixError(interp), (char *) NULL); |
| 1178 |
return TCL_ERROR; |
| 1179 |
} |
| 1180 |
|
| 1181 |
pwPtr = getpwuid(statBuf.st_uid); |
| 1182 |
if (pwPtr == NULL) { |
| 1183 |
*attributePtrPtr = Tcl_NewIntObj((int) statBuf.st_uid); |
| 1184 |
} else { |
| 1185 |
Tcl_DString ds; |
| 1186 |
CONST char *utf; |
| 1187 |
|
| 1188 |
utf = Tcl_ExternalToUtfDString(NULL, pwPtr->pw_name, -1, &ds); |
| 1189 |
*attributePtrPtr = Tcl_NewStringObj(utf, Tcl_DStringLength(&ds)); |
| 1190 |
Tcl_DStringFree(&ds); |
| 1191 |
} |
| 1192 |
endpwent(); |
| 1193 |
return TCL_OK; |
| 1194 |
} |
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
static int |
| 1214 |
GetPermissionsAttribute(interp, objIndex, fileName, attributePtrPtr) |
| 1215 |
Tcl_Interp *interp; |
| 1216 |
int objIndex; |
| 1217 |
Tcl_Obj *fileName; |
| 1218 |
Tcl_Obj **attributePtrPtr; |
| 1219 |
{ |
| 1220 |
Tcl_StatBuf statBuf; |
| 1221 |
char returnString[7]; |
| 1222 |
int result; |
| 1223 |
|
| 1224 |
result = TclpObjStat(fileName, &statBuf); |
| 1225 |
|
| 1226 |
if (result != 0) { |
| 1227 |
Tcl_AppendResult(interp, "could not read \"", |
| 1228 |
Tcl_GetString(fileName), "\": ", |
| 1229 |
Tcl_PosixError(interp), (char *) NULL); |
| 1230 |
return TCL_ERROR; |
| 1231 |
} |
| 1232 |
|
| 1233 |
sprintf(returnString, "%0#5lo", (long) (statBuf.st_mode & 0x00007FFF)); |
| 1234 |
|
| 1235 |
*attributePtrPtr = Tcl_NewStringObj(returnString, -1); |
| 1236 |
|
| 1237 |
return TCL_OK; |
| 1238 |
} |
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
static int |
| 1257 |
SetGroupAttribute(interp, objIndex, fileName, attributePtr) |
| 1258 |
Tcl_Interp *interp; |
| 1259 |
int objIndex; |
| 1260 |
Tcl_Obj *fileName; |
| 1261 |
Tcl_Obj *attributePtr; |
| 1262 |
{ |
| 1263 |
long gid; |
| 1264 |
int result; |
| 1265 |
CONST char *native; |
| 1266 |
|
| 1267 |
if (Tcl_GetLongFromObj(NULL, attributePtr, &gid) != TCL_OK) { |
| 1268 |
Tcl_DString ds; |
| 1269 |
struct group *groupPtr; |
| 1270 |
CONST char *string; |
| 1271 |
int length; |
| 1272 |
|
| 1273 |
string = Tcl_GetStringFromObj(attributePtr, &length); |
| 1274 |
|
| 1275 |
native = Tcl_UtfToExternalDString(NULL, string, length, &ds); |
| 1276 |
groupPtr = getgrnam(native); |
| 1277 |
Tcl_DStringFree(&ds); |
| 1278 |
|
| 1279 |
if (groupPtr == NULL) { |
| 1280 |
endgrent(); |
| 1281 |
Tcl_AppendResult(interp, "could not set group for file \"", |
| 1282 |
Tcl_GetString(fileName), "\": group \"", |
| 1283 |
string, "\" does not exist", |
| 1284 |
(char *) NULL); |
| 1285 |
return TCL_ERROR; |
| 1286 |
} |
| 1287 |
gid = groupPtr->gr_gid; |
| 1288 |
} |
| 1289 |
|
| 1290 |
native = Tcl_FSGetNativePath(fileName); |
| 1291 |
result = chown(native, (uid_t) -1, (gid_t) gid); |
| 1292 |
|
| 1293 |
endgrent(); |
| 1294 |
if (result != 0) { |
| 1295 |
Tcl_AppendResult(interp, "could not set group for file \"", |
| 1296 |
Tcl_GetString(fileName), "\": ", Tcl_PosixError(interp), |
| 1297 |
(char *) NULL); |
| 1298 |
return TCL_ERROR; |
| 1299 |
} |
| 1300 |
return TCL_OK; |
| 1301 |
} |
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
static int |
| 1320 |
SetOwnerAttribute(interp, objIndex, fileName, attributePtr) |
| 1321 |
Tcl_Interp *interp; |
| 1322 |
int objIndex; |
| 1323 |
Tcl_Obj *fileName; |
| 1324 |
Tcl_Obj *attributePtr; |
| 1325 |
{ |
| 1326 |
long uid; |
| 1327 |
int result; |
| 1328 |
CONST char *native; |
| 1329 |
|
| 1330 |
if (Tcl_GetLongFromObj(NULL, attributePtr, &uid) != TCL_OK) { |
| 1331 |
Tcl_DString ds; |
| 1332 |
struct passwd *pwPtr; |
| 1333 |
CONST char *string; |
| 1334 |
int length; |
| 1335 |
|
| 1336 |
string = Tcl_GetStringFromObj(attributePtr, &length); |
| 1337 |
|
| 1338 |
native = Tcl_UtfToExternalDString(NULL, string, length, &ds); |
| 1339 |
pwPtr = getpwnam(native); |
| 1340 |
Tcl_DStringFree(&ds); |
| 1341 |
|
| 1342 |
if (pwPtr == NULL) { |
| 1343 |
Tcl_AppendResult(interp, "could not set owner for file \"", |
| 1344 |
Tcl_GetString(fileName), "\": user \"", |
| 1345 |
string, "\" does not exist", |
| 1346 |
(char *) NULL); |
| 1347 |
return TCL_ERROR; |
| 1348 |
} |
| 1349 |
uid = pwPtr->pw_uid; |
| 1350 |
} |
| 1351 |
|
| 1352 |
native = Tcl_FSGetNativePath(fileName); |
| 1353 |
result = chown(native, (uid_t) uid, (gid_t) -1); |
| 1354 |
|
| 1355 |
if (result != 0) { |
| 1356 |
Tcl_AppendResult(interp, "could not set owner for file \"", |
| 1357 |
Tcl_GetString(fileName), "\": ", |
| 1358 |
Tcl_PosixError(interp), (char *) NULL); |
| 1359 |
return TCL_ERROR; |
| 1360 |
} |
| 1361 |
return TCL_OK; |
| 1362 |
} |
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
static int |
| 1381 |
SetPermissionsAttribute(interp, objIndex, fileName, attributePtr) |
| 1382 |
Tcl_Interp *interp; |
| 1383 |
int objIndex; |
| 1384 |
Tcl_Obj *fileName; |
| 1385 |
Tcl_Obj *attributePtr; |
| 1386 |
{ |
| 1387 |
long mode; |
| 1388 |
mode_t newMode; |
| 1389 |
int result; |
| 1390 |
CONST char *native; |
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
if (Tcl_GetLongFromObj(NULL, attributePtr, &mode) == TCL_OK) { |
| 1396 |
newMode = (mode_t) (mode & 0x00007FFF); |
| 1397 |
} else { |
| 1398 |
Tcl_StatBuf buf; |
| 1399 |
char *modeStringPtr = Tcl_GetString(attributePtr); |
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
result = TclpObjStat(fileName, &buf); |
| 1408 |
if (result != 0) { |
| 1409 |
Tcl_AppendResult(interp, "could not read \"", |
| 1410 |
Tcl_GetString(fileName), "\": ", |
| 1411 |
Tcl_PosixError(interp), (char *) NULL); |
| 1412 |
return TCL_ERROR; |
| 1413 |
} |
| 1414 |
newMode = (mode_t) (buf.st_mode & 0x00007FFF); |
| 1415 |
|
| 1416 |
if (GetModeFromPermString(NULL, modeStringPtr, &newMode) != TCL_OK) { |
| 1417 |
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), |
| 1418 |
"unknown permission string format \"", |
| 1419 |
modeStringPtr, "\"", (char *) NULL); |
| 1420 |
return TCL_ERROR; |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
native = Tcl_FSGetNativePath(fileName); |
| 1425 |
result = chmod(native, newMode); |
| 1426 |
if (result != 0) { |
| 1427 |
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), |
| 1428 |
"could not set permissions for file \"", |
| 1429 |
Tcl_GetString(fileName), "\": ", |
| 1430 |
Tcl_PosixError(interp), (char *) NULL); |
| 1431 |
return TCL_ERROR; |
| 1432 |
} |
| 1433 |
return TCL_OK; |
| 1434 |
} |
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
Tcl_Obj* |
| 1453 |
TclpObjListVolumes(void) |
| 1454 |
{ |
| 1455 |
Tcl_Obj *resultPtr = Tcl_NewStringObj("/",1); |
| 1456 |
|
| 1457 |
Tcl_IncrRefCount(resultPtr); |
| 1458 |
return resultPtr; |
| 1459 |
} |
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
static int |
| 1480 |
GetModeFromPermString(interp, modeStringPtr, modePtr) |
| 1481 |
Tcl_Interp *interp; |
| 1482 |
char *modeStringPtr; |
| 1483 |
mode_t *modePtr; |
| 1484 |
{ |
| 1485 |
mode_t newMode; |
| 1486 |
mode_t oldMode; |
| 1487 |
|
| 1488 |
|
| 1489 |
int i,n, who, op, what, op_found, who_found; |
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
if (strlen(modeStringPtr) != 9) { |
| 1495 |
goto chmodStyleCheck; |
| 1496 |
} |
| 1497 |
|
| 1498 |
newMode = 0; |
| 1499 |
for (i = 0; i < 9; i++) { |
| 1500 |
switch (*(modeStringPtr+i)) { |
| 1501 |
case 'r': |
| 1502 |
if ((i%3) != 0) { |
| 1503 |
goto chmodStyleCheck; |
| 1504 |
} |
| 1505 |
newMode |= (1<<(8-i)); |
| 1506 |
break; |
| 1507 |
case 'w': |
| 1508 |
if ((i%3) != 1) { |
| 1509 |
goto chmodStyleCheck; |
| 1510 |
} |
| 1511 |
newMode |= (1<<(8-i)); |
| 1512 |
break; |
| 1513 |
case 'x': |
| 1514 |
if ((i%3) != 2) { |
| 1515 |
goto chmodStyleCheck; |
| 1516 |
} |
| 1517 |
newMode |= (1<<(8-i)); |
| 1518 |
break; |
| 1519 |
case 's': |
| 1520 |
if (((i%3) != 2) || (i > 5)) { |
| 1521 |
goto chmodStyleCheck; |
| 1522 |
} |
| 1523 |
newMode |= (1<<(8-i)); |
| 1524 |
newMode |= (1<<(11-(i/3))); |
| 1525 |
break; |
| 1526 |
case 'S': |
| 1527 |
if (((i%3) != 2) || (i > 5)) { |
| 1528 |
goto chmodStyleCheck; |
| 1529 |
} |
| 1530 |
newMode |= (1<<(11-(i/3))); |
| 1531 |
break; |
| 1532 |
case 't': |
| 1533 |
if (i != 8) { |
| 1534 |
goto chmodStyleCheck; |
| 1535 |
} |
| 1536 |
newMode |= (1<<(8-i)); |
| 1537 |
newMode |= (1<<9); |
| 1538 |
break; |
| 1539 |
case 'T': |
| 1540 |
if (i != 8) { |
| 1541 |
goto chmodStyleCheck; |
| 1542 |
} |
| 1543 |
newMode |= (1<<9); |
| 1544 |
break; |
| 1545 |
case '-': |
| 1546 |
break; |
| 1547 |
default: |
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
goto chmodStyleCheck; |
| 1552 |
} |
| 1553 |
} |
| 1554 |
*modePtr = newMode; |
| 1555 |
return TCL_OK; |
| 1556 |
|
| 1557 |
chmodStyleCheck: |
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
for (n = 0 ; *(modeStringPtr+n) != '\0' ; n = n + i) { |
| 1563 |
oldMode = *modePtr; |
| 1564 |
who = op = what = op_found = who_found = 0; |
| 1565 |
for (i = 0 ; *(modeStringPtr+n+i) != '\0' ; i++ ) { |
| 1566 |
if (!who_found) { |
| 1567 |
|
| 1568 |
switch (*(modeStringPtr+n+i)) { |
| 1569 |
case 'u' : |
| 1570 |
who |= 0x9c0; |
| 1571 |
continue; |
| 1572 |
case 'g' : |
| 1573 |
who |= 0x438; |
| 1574 |
continue; |
| 1575 |
case 'o' : |
| 1576 |
who |= 0x207; |
| 1577 |
continue; |
| 1578 |
case 'a' : |
| 1579 |
who |= 0xfff; |
| 1580 |
continue; |
| 1581 |
} |
| 1582 |
} |
| 1583 |
who_found = 1; |
| 1584 |
if (who == 0) { |
| 1585 |
who = 0xfff; |
| 1586 |
} |
| 1587 |
if (!op_found) { |
| 1588 |
|
| 1589 |
switch (*(modeStringPtr+n+i)) { |
| 1590 |
case '+' : |
| 1591 |
op = 1; |
| 1592 |
op_found = 1; |
| 1593 |
continue; |
| 1594 |
case '-' : |
| 1595 |
op = 2; |
| 1596 |
op_found = 1; |
| 1597 |
continue; |
| 1598 |
case '=' : |
| 1599 |
op = 3; |
| 1600 |
op_found = 1; |
| 1601 |
continue; |
| 1602 |
default : |
| 1603 |
return TCL_ERROR; |
| 1604 |
} |
| 1605 |
} |
| 1606 |
|
| 1607 |
switch (*(modeStringPtr+n+i)) { |
| 1608 |
case 'r' : |
| 1609 |
what |= 0x124; |
| 1610 |
continue; |
| 1611 |
case 'w' : |
| 1612 |
what |= 0x92; |
| 1613 |
continue; |
| 1614 |
case 'x' : |
| 1615 |
what |= 0x49; |
| 1616 |
continue; |
| 1617 |
case 's' : |
| 1618 |
what |= 0xc00; |
| 1619 |
continue; |
| 1620 |
case 't' : |
| 1621 |
what |= 0x200; |
| 1622 |
continue; |
| 1623 |
case ',' : |
| 1624 |
break; |
| 1625 |
default : |
| 1626 |
return TCL_ERROR; |
| 1627 |
} |
| 1628 |
if (*(modeStringPtr+n+i) == ',') { |
| 1629 |
i++; |
| 1630 |
break; |
| 1631 |
} |
| 1632 |
} |
| 1633 |
switch (op) { |
| 1634 |
case 1 : |
| 1635 |
*modePtr = oldMode | (who & what); |
| 1636 |
continue; |
| 1637 |
case 2 : |
| 1638 |
*modePtr = oldMode & ~(who & what); |
| 1639 |
continue; |
| 1640 |
case 3 : |
| 1641 |
*modePtr = (oldMode & ~who) | (who & what); |
| 1642 |
continue; |
| 1643 |
} |
| 1644 |
} |
| 1645 |
return TCL_OK; |
| 1646 |
} |
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
int |
| 1669 |
TclpObjNormalizePath(interp, pathPtr, nextCheckpoint) |
| 1670 |
Tcl_Interp *interp; |
| 1671 |
Tcl_Obj *pathPtr; |
| 1672 |
int nextCheckpoint; |
| 1673 |
{ |
| 1674 |
char *currentPathEndPosition; |
| 1675 |
int pathLen; |
| 1676 |
char cur; |
| 1677 |
char *path = Tcl_GetStringFromObj(pathPtr, &pathLen); |
| 1678 |
#ifndef NO_REALPATH |
| 1679 |
char normPath[MAXPATHLEN]; |
| 1680 |
Tcl_DString ds; |
| 1681 |
CONST char *nativePath; |
| 1682 |
#endif |
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
currentPathEndPosition = path + nextCheckpoint; |
| 1689 |
if (*currentPathEndPosition == '/') { |
| 1690 |
currentPathEndPosition++; |
| 1691 |
} |
| 1692 |
|
| 1693 |
#ifndef NO_REALPATH |
| 1694 |
|
| 1695 |
if (nextCheckpoint == 0) { |
| 1696 |
char *lastDir = strrchr(currentPathEndPosition, '/'); |
| 1697 |
if (lastDir != NULL) { |
| 1698 |
nativePath = Tcl_UtfToExternalDString(NULL, path, |
| 1699 |
lastDir - path, &ds); |
| 1700 |
if (Realpath(nativePath, normPath) != NULL) { |
| 1701 |
nextCheckpoint = lastDir - path; |
| 1702 |
goto wholeStringOk; |
| 1703 |
} |
| 1704 |
} |
| 1705 |
} |
| 1706 |
|
| 1707 |
#endif |
| 1708 |
|
| 1709 |
while (1) { |
| 1710 |
cur = *currentPathEndPosition; |
| 1711 |
if ((cur == '/') && (path != currentPathEndPosition)) { |
| 1712 |
|
| 1713 |
Tcl_DString ds; |
| 1714 |
CONST char *nativePath; |
| 1715 |
int accessOk; |
| 1716 |
|
| 1717 |
nativePath = Tcl_UtfToExternalDString(NULL, path, |
| 1718 |
currentPathEndPosition - path, &ds); |
| 1719 |
accessOk = access(nativePath, F_OK); |
| 1720 |
Tcl_DStringFree(&ds); |
| 1721 |
if (accessOk != 0) { |
| 1722 |
|
| 1723 |
break; |
| 1724 |
} |
| 1725 |
|
| 1726 |
nextCheckpoint = currentPathEndPosition - path; |
| 1727 |
} else if (cur == 0) { |
| 1728 |
|
| 1729 |
break; |
| 1730 |
} |
| 1731 |
currentPathEndPosition++; |
| 1732 |
} |
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
#ifndef NO_REALPATH |
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
if (nextCheckpoint == 0) return 0; |
| 1748 |
|
| 1749 |
nativePath = Tcl_UtfToExternalDString(NULL, path, nextCheckpoint, &ds); |
| 1750 |
if (Realpath(nativePath, normPath) != NULL) { |
| 1751 |
int newNormLen; |
| 1752 |
wholeStringOk: |
| 1753 |
newNormLen = strlen(normPath); |
| 1754 |
if ((newNormLen == Tcl_DStringLength(&ds)) |
| 1755 |
&& (strcmp(normPath, nativePath) == 0)) { |
| 1756 |
|
| 1757 |
Tcl_DStringFree(&ds); |
| 1758 |
if (path[nextCheckpoint] != '\0') { |
| 1759 |
nextCheckpoint++; |
| 1760 |
} |
| 1761 |
return nextCheckpoint; |
| 1762 |
} |
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
Tcl_DStringFree(&ds); |
| 1769 |
Tcl_ExternalToUtfDString(NULL, normPath, (int) newNormLen, &ds); |
| 1770 |
|
| 1771 |
if (path[nextCheckpoint] != '\0') { |
| 1772 |
|
| 1773 |
int normLen = Tcl_DStringLength(&ds); |
| 1774 |
Tcl_DStringAppend(&ds, path + nextCheckpoint, |
| 1775 |
pathLen - nextCheckpoint); |
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
nextCheckpoint = normLen + 1; |
| 1781 |
} else { |
| 1782 |
|
| 1783 |
nextCheckpoint = Tcl_DStringLength(&ds); |
| 1784 |
} |
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
Tcl_SetStringObj(pathPtr, Tcl_DStringValue(&ds), |
| 1789 |
Tcl_DStringLength(&ds)); |
| 1790 |
} |
| 1791 |
Tcl_DStringFree(&ds); |
| 1792 |
#endif |
| 1793 |
|
| 1794 |
return nextCheckpoint; |
| 1795 |
} |
| 1796 |
|
| 1797 |
|
| 1798 |
|