| 1 |
/* |
| 2 |
* tclMacOSXFCmd.c |
| 3 |
* |
| 4 |
* This file implements the MacOSX specific portion of file manipulation |
| 5 |
* subcommands of the "file" command. |
| 6 |
* |
| 7 |
* Copyright (c) 2003-2007 Daniel A. Steffen <[email protected]> |
| 8 |
* |
| 9 |
* See the file "license.terms" for information on usage and redistribution of |
| 10 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "tclInt.h" |
| 14 |
|
| 15 |
#ifdef HAVE_GETATTRLIST |
| 16 |
#include <sys/attr.h> |
| 17 |
#include <sys/paths.h> |
| 18 |
#include <libkern/OSByteOrder.h> |
| 19 |
#endif |
| 20 |
|
| 21 |
/* Darwin 8 copyfile API. */ |
| 22 |
#ifdef HAVE_COPYFILE |
| 23 |
#ifdef HAVE_COPYFILE_H |
| 24 |
#include <copyfile.h> |
| 25 |
#if defined(HAVE_WEAK_IMPORT) && (MAC_OS_X_VERSION_MIN_REQUIRED < 1040) |
| 26 |
/* Support for weakly importing copyfile. */ |
| 27 |
#define WEAK_IMPORT_COPYFILE |
| 28 |
extern int copyfile(const char *from, const char *to, |
| 29 |
copyfile_state_t state, copyfile_flags_t flags) |
| 30 |
WEAK_IMPORT_ATTRIBUTE; |
| 31 |
#endif /* HAVE_WEAK_IMPORT */ |
| 32 |
#else /* HAVE_COPYFILE_H */ |
| 33 |
int copyfile(const char *from, const char *to, |
| 34 |
void *state, uint32_t flags); |
| 35 |
#define COPYFILE_ACL (1<<0) |
| 36 |
#define COPYFILE_XATTR (1<<2) |
| 37 |
#define COPYFILE_NOFOLLOW_SRC (1<<18) |
| 38 |
#if defined(HAVE_WEAK_IMPORT) && (MAC_OS_X_VERSION_MIN_REQUIRED < 1040) |
| 39 |
/* Support for weakly importing copyfile. */ |
| 40 |
#define WEAK_IMPORT_COPYFILE |
| 41 |
extern int copyfile(const char *from, const char *to, |
| 42 |
void *state, uint32_t flags) |
| 43 |
WEAK_IMPORT_ATTRIBUTE; |
| 44 |
#endif /* HAVE_WEAK_IMPORT */ |
| 45 |
#endif /* HAVE_COPYFILE_H */ |
| 46 |
#endif /* HAVE_COPYFILE */ |
| 47 |
|
| 48 |
#ifdef WEAK_IMPORT_COPYFILE |
| 49 |
#define MayUseCopyFile() (copyfile != NULL) |
| 50 |
#elif defined(HAVE_COPYFILE) |
| 51 |
#define MayUseCopyFile() (1) |
| 52 |
#else |
| 53 |
#define MayUseCopyFile() (0) |
| 54 |
#endif |
| 55 |
|
| 56 |
#include <libkern/OSByteOrder.h> |
| 57 |
|
| 58 |
/* |
| 59 |
* Constants for file attributes subcommand. Need to be kept in sync with |
| 60 |
* tclUnixFCmd.c ! |
| 61 |
*/ |
| 62 |
|
| 63 |
enum { |
| 64 |
UNIX_GROUP_ATTRIBUTE, |
| 65 |
UNIX_OWNER_ATTRIBUTE, |
| 66 |
UNIX_PERMISSIONS_ATTRIBUTE, |
| 67 |
#ifdef HAVE_CHFLAGS |
| 68 |
UNIX_READONLY_ATTRIBUTE, |
| 69 |
#endif |
| 70 |
#ifdef MAC_OSX_TCL |
| 71 |
MACOSX_CREATOR_ATTRIBUTE, |
| 72 |
MACOSX_TYPE_ATTRIBUTE, |
| 73 |
MACOSX_HIDDEN_ATTRIBUTE, |
| 74 |
MACOSX_RSRCLENGTH_ATTRIBUTE, |
| 75 |
#endif |
| 76 |
}; |
| 77 |
|
| 78 |
typedef u_int32_t OSType; |
| 79 |
|
| 80 |
static int GetOSTypeFromObj(Tcl_Interp *interp, |
| 81 |
Tcl_Obj *objPtr, OSType *osTypePtr); |
| 82 |
static Tcl_Obj * NewOSTypeObj(const OSType newOSType); |
| 83 |
static int SetOSTypeFromAny(Tcl_Interp *interp, Tcl_Obj *objPtr); |
| 84 |
static void UpdateStringOfOSType(Tcl_Obj *objPtr); |
| 85 |
|
| 86 |
static const Tcl_ObjType tclOSTypeType = { |
| 87 |
"osType", /* name */ |
| 88 |
NULL, /* freeIntRepProc */ |
| 89 |
NULL, /* dupIntRepProc */ |
| 90 |
UpdateStringOfOSType, /* updateStringProc */ |
| 91 |
SetOSTypeFromAny /* setFromAnyProc */ |
| 92 |
}; |
| 93 |
|
| 94 |
enum { |
| 95 |
kIsInvisible = 0x4000, |
| 96 |
}; |
| 97 |
|
| 98 |
#define kFinfoIsInvisible (OSSwapHostToBigConstInt16(kIsInvisible)) |
| 99 |
|
| 100 |
typedef struct finderinfo { |
| 101 |
u_int32_t type; |
| 102 |
u_int32_t creator; |
| 103 |
u_int16_t fdFlags; |
| 104 |
u_int32_t location; |
| 105 |
u_int16_t reserved; |
| 106 |
u_int32_t extendedFileInfo[4]; |
| 107 |
} __attribute__ ((__packed__)) finderinfo; |
| 108 |
|
| 109 |
typedef struct { |
| 110 |
u_int64_t reserved1; /* Make sure data is 8-byte aligned */ |
| 111 |
u_int32_t reserved2; /* See [992f94d847] */ |
| 112 |
u_int32_t info_length; |
| 113 |
u_int32_t data[8]; |
| 114 |
} fileinfobuf; |
| 115 |
|
| 116 |
/* |
| 117 |
*---------------------------------------------------------------------- |
| 118 |
* |
| 119 |
* TclMacOSXGetFileAttribute |
| 120 |
* |
| 121 |
* Gets a MacOSX attribute of a file. Which attribute is controlled by |
| 122 |
* objIndex. The object will have ref count 0. |
| 123 |
* |
| 124 |
* Results: |
| 125 |
* Standard TCL result. Returns a new Tcl_Obj in attributePtrPtr if there |
| 126 |
* is no error. |
| 127 |
* |
| 128 |
* Side effects: |
| 129 |
* A new object is allocated. |
| 130 |
* |
| 131 |
*---------------------------------------------------------------------- |
| 132 |
*/ |
| 133 |
|
| 134 |
int |
| 135 |
TclMacOSXGetFileAttribute( |
| 136 |
Tcl_Interp *interp, /* The interp we are using for errors. */ |
| 137 |
int objIndex, /* The index of the attribute. */ |
| 138 |
Tcl_Obj *fileName, /* The name of the file (UTF-8). */ |
| 139 |
Tcl_Obj **attributePtrPtr) /* A pointer to return the object with. */ |
| 140 |
{ |
| 141 |
#ifdef HAVE_GETATTRLIST |
| 142 |
int result; |
| 143 |
Tcl_StatBuf statBuf; |
| 144 |
struct attrlist alist; |
| 145 |
fileinfobuf finfo; |
| 146 |
finderinfo *finder = (finderinfo *) &finfo.data; |
| 147 |
off_t *rsrcForkSize = (off_t *) &finfo.data; |
| 148 |
const char *native; |
| 149 |
|
| 150 |
result = TclpObjStat(fileName, &statBuf); |
| 151 |
|
| 152 |
if (result != 0) { |
| 153 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 154 |
"could not read \"%s\": %s", |
| 155 |
TclGetString(fileName), Tcl_PosixError(interp))); |
| 156 |
return TCL_ERROR; |
| 157 |
} |
| 158 |
|
| 159 |
if (S_ISDIR(statBuf.st_mode) && objIndex != MACOSX_HIDDEN_ATTRIBUTE) { |
| 160 |
/* |
| 161 |
* Directories only support attribute "-hidden". |
| 162 |
*/ |
| 163 |
|
| 164 |
errno = EISDIR; |
| 165 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 166 |
"invalid attribute: %s", Tcl_PosixError(interp))); |
| 167 |
return TCL_ERROR; |
| 168 |
} |
| 169 |
|
| 170 |
bzero(&alist, sizeof(struct attrlist)); |
| 171 |
alist.bitmapcount = ATTR_BIT_MAP_COUNT; |
| 172 |
if (objIndex == MACOSX_RSRCLENGTH_ATTRIBUTE) { |
| 173 |
alist.fileattr = ATTR_FILE_RSRCLENGTH; |
| 174 |
} else { |
| 175 |
alist.commonattr = ATTR_CMN_FNDRINFO; |
| 176 |
} |
| 177 |
native = (const char *)Tcl_FSGetNativePath(fileName); |
| 178 |
result = getattrlist(native, &alist, &finfo.info_length, |
| 179 |
sizeof(fileinfobuf) - offsetof(fileinfobuf, info_length), 0); |
| 180 |
|
| 181 |
if (result != 0) { |
| 182 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 183 |
"could not read attributes of \"%s\": %s", |
| 184 |
TclGetString(fileName), Tcl_PosixError(interp))); |
| 185 |
return TCL_ERROR; |
| 186 |
} |
| 187 |
|
| 188 |
switch (objIndex) { |
| 189 |
case MACOSX_CREATOR_ATTRIBUTE: |
| 190 |
*attributePtrPtr = NewOSTypeObj( |
| 191 |
OSSwapBigToHostInt32(finder->creator)); |
| 192 |
break; |
| 193 |
case MACOSX_TYPE_ATTRIBUTE: |
| 194 |
*attributePtrPtr = NewOSTypeObj( |
| 195 |
OSSwapBigToHostInt32(finder->type)); |
| 196 |
break; |
| 197 |
case MACOSX_HIDDEN_ATTRIBUTE: |
| 198 |
TclNewIntObj(*attributePtrPtr, |
| 199 |
(finder->fdFlags & kFinfoIsInvisible) != 0); |
| 200 |
break; |
| 201 |
case MACOSX_RSRCLENGTH_ATTRIBUTE: |
| 202 |
TclNewIntObj(*attributePtrPtr, *rsrcForkSize); |
| 203 |
break; |
| 204 |
} |
| 205 |
return TCL_OK; |
| 206 |
#else |
| 207 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 208 |
"Mac OS X file attributes not supported", -1)); |
| 209 |
Tcl_SetErrorCode(interp, "TCL", "UNSUPPORTED", (char *)NULL); |
| 210 |
return TCL_ERROR; |
| 211 |
#endif /* HAVE_GETATTRLIST */ |
| 212 |
} |
| 213 |
|
| 214 |
/* |
| 215 |
*--------------------------------------------------------------------------- |
| 216 |
* |
| 217 |
* TclMacOSXSetFileAttribute -- |
| 218 |
* |
| 219 |
* Sets a MacOSX attribute of a file. Which attribute is controlled by |
| 220 |
* objIndex. |
| 221 |
* |
| 222 |
* Results: |
| 223 |
* Standard TCL result. |
| 224 |
* |
| 225 |
* Side effects: |
| 226 |
* As above. |
| 227 |
* |
| 228 |
*--------------------------------------------------------------------------- |
| 229 |
*/ |
| 230 |
|
| 231 |
int |
| 232 |
TclMacOSXSetFileAttribute( |
| 233 |
Tcl_Interp *interp, /* The interp for error reporting. */ |
| 234 |
int objIndex, /* The index of the attribute. */ |
| 235 |
Tcl_Obj *fileName, /* The name of the file (UTF-8). */ |
| 236 |
Tcl_Obj *attributePtr) /* New owner for file. */ |
| 237 |
{ |
| 238 |
#ifdef HAVE_GETATTRLIST |
| 239 |
int result; |
| 240 |
Tcl_StatBuf statBuf; |
| 241 |
struct attrlist alist; |
| 242 |
fileinfobuf finfo; |
| 243 |
finderinfo *finder = (finderinfo *) &finfo.data; |
| 244 |
off_t *rsrcForkSize = (off_t *) &finfo.data; |
| 245 |
const char *native; |
| 246 |
|
| 247 |
result = TclpObjStat(fileName, &statBuf); |
| 248 |
|
| 249 |
if (result != 0) { |
| 250 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 251 |
"could not read \"%s\": %s", |
| 252 |
TclGetString(fileName), Tcl_PosixError(interp))); |
| 253 |
return TCL_ERROR; |
| 254 |
} |
| 255 |
|
| 256 |
if (S_ISDIR(statBuf.st_mode) && objIndex != MACOSX_HIDDEN_ATTRIBUTE) { |
| 257 |
/* |
| 258 |
* Directories only support attribute "-hidden". |
| 259 |
*/ |
| 260 |
|
| 261 |
errno = EISDIR; |
| 262 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 263 |
"invalid attribute: %s", Tcl_PosixError(interp))); |
| 264 |
return TCL_ERROR; |
| 265 |
} |
| 266 |
|
| 267 |
bzero(&alist, sizeof(struct attrlist)); |
| 268 |
alist.bitmapcount = ATTR_BIT_MAP_COUNT; |
| 269 |
if (objIndex == MACOSX_RSRCLENGTH_ATTRIBUTE) { |
| 270 |
alist.fileattr = ATTR_FILE_RSRCLENGTH; |
| 271 |
} else { |
| 272 |
alist.commonattr = ATTR_CMN_FNDRINFO; |
| 273 |
} |
| 274 |
native = (const char *)Tcl_FSGetNativePath(fileName); |
| 275 |
result = getattrlist(native, &alist, &finfo.info_length, |
| 276 |
sizeof(fileinfobuf) - offsetof(fileinfobuf, info_length), 0); |
| 277 |
|
| 278 |
if (result != 0) { |
| 279 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 280 |
"could not read attributes of \"%s\": %s", |
| 281 |
TclGetString(fileName), Tcl_PosixError(interp))); |
| 282 |
return TCL_ERROR; |
| 283 |
} |
| 284 |
|
| 285 |
if (objIndex != MACOSX_RSRCLENGTH_ATTRIBUTE) { |
| 286 |
OSType t; |
| 287 |
int h; |
| 288 |
|
| 289 |
switch (objIndex) { |
| 290 |
case MACOSX_CREATOR_ATTRIBUTE: |
| 291 |
if (GetOSTypeFromObj(interp, attributePtr, &t) != TCL_OK) { |
| 292 |
return TCL_ERROR; |
| 293 |
} |
| 294 |
finder->creator = OSSwapHostToBigInt32(t); |
| 295 |
break; |
| 296 |
case MACOSX_TYPE_ATTRIBUTE: |
| 297 |
if (GetOSTypeFromObj(interp, attributePtr, &t) != TCL_OK) { |
| 298 |
return TCL_ERROR; |
| 299 |
} |
| 300 |
finder->type = OSSwapHostToBigInt32(t); |
| 301 |
break; |
| 302 |
case MACOSX_HIDDEN_ATTRIBUTE: |
| 303 |
if (Tcl_GetBooleanFromObj(interp, attributePtr, &h) != TCL_OK) { |
| 304 |
return TCL_ERROR; |
| 305 |
} |
| 306 |
if (h) { |
| 307 |
finder->fdFlags |= kFinfoIsInvisible; |
| 308 |
} else { |
| 309 |
finder->fdFlags &= ~kFinfoIsInvisible; |
| 310 |
} |
| 311 |
break; |
| 312 |
} |
| 313 |
|
| 314 |
result = setattrlist(native, &alist, |
| 315 |
&finfo.data, sizeof(finfo.data), 0); |
| 316 |
|
| 317 |
if (result != 0) { |
| 318 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 319 |
"could not set attributes of \"%s\": %s", |
| 320 |
TclGetString(fileName), Tcl_PosixError(interp))); |
| 321 |
return TCL_ERROR; |
| 322 |
} |
| 323 |
} else { |
| 324 |
Tcl_WideInt newRsrcForkSize; |
| 325 |
|
| 326 |
if (TclGetWideIntFromObj(interp, attributePtr, |
| 327 |
&newRsrcForkSize) != TCL_OK) { |
| 328 |
return TCL_ERROR; |
| 329 |
} |
| 330 |
|
| 331 |
if (newRsrcForkSize != *rsrcForkSize) { |
| 332 |
Tcl_DString ds; |
| 333 |
|
| 334 |
/* |
| 335 |
* Only setting rsrclength to 0 to strip a file's resource fork is |
| 336 |
* supported. |
| 337 |
*/ |
| 338 |
|
| 339 |
if (newRsrcForkSize != 0) { |
| 340 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 341 |
"setting nonzero rsrclength not supported", -1)); |
| 342 |
Tcl_SetErrorCode(interp, "TCL", "UNSUPPORTED", (char *)NULL); |
| 343 |
return TCL_ERROR; |
| 344 |
} |
| 345 |
|
| 346 |
/* |
| 347 |
* Construct path to resource fork. |
| 348 |
*/ |
| 349 |
|
| 350 |
Tcl_DStringInit(&ds); |
| 351 |
Tcl_DStringAppend(&ds, native, -1); |
| 352 |
Tcl_DStringAppend(&ds, _PATH_RSRCFORKSPEC, -1); |
| 353 |
|
| 354 |
result = truncate(Tcl_DStringValue(&ds), 0); |
| 355 |
if (result != 0) { |
| 356 |
/* |
| 357 |
* truncate() on a valid resource fork path may fail with a |
| 358 |
* permission error in some OS releases, try truncating with |
| 359 |
* open() instead: |
| 360 |
*/ |
| 361 |
|
| 362 |
int fd = open(Tcl_DStringValue(&ds), O_WRONLY | O_TRUNC); |
| 363 |
|
| 364 |
if (fd > 0) { |
| 365 |
result = close(fd); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
Tcl_DStringFree(&ds); |
| 370 |
|
| 371 |
if (result != 0) { |
| 372 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 373 |
"could not truncate resource fork of \"%s\": %s", |
| 374 |
TclGetString(fileName), Tcl_PosixError(interp))); |
| 375 |
return TCL_ERROR; |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
return TCL_OK; |
| 380 |
#else |
| 381 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 382 |
"Mac OS X file attributes not supported", -1)); |
| 383 |
Tcl_SetErrorCode(interp, "TCL", "UNSUPPORTED", (char *)NULL); |
| 384 |
return TCL_ERROR; |
| 385 |
#endif |
| 386 |
} |
| 387 |
|
| 388 |
/* |
| 389 |
*--------------------------------------------------------------------------- |
| 390 |
* |
| 391 |
* TclMacOSXCopyFileAttributes -- |
| 392 |
* |
| 393 |
* Copy the MacOSX attributes and resource fork (if present) from one |
| 394 |
* file to another. |
| 395 |
* |
| 396 |
* Results: |
| 397 |
* Standard Tcl result. |
| 398 |
* |
| 399 |
* Side effects: |
| 400 |
* MacOSX attributes and resource fork are updated in the new file to |
| 401 |
* reflect the old file. |
| 402 |
* |
| 403 |
*--------------------------------------------------------------------------- |
| 404 |
*/ |
| 405 |
|
| 406 |
int |
| 407 |
TclMacOSXCopyFileAttributes( |
| 408 |
const char *src, /* Path name of source file (native). */ |
| 409 |
const char *dst, /* Path name of target file (native). */ |
| 410 |
const Tcl_StatBuf *statBufPtr) |
| 411 |
/* Stat info for source file */ |
| 412 |
{ |
| 413 |
if (MayUseCopyFile()) { |
| 414 |
#ifdef HAVE_COPYFILE |
| 415 |
if (0 == copyfile(src, dst, NULL, (S_ISLNK(statBufPtr->st_mode) |
| 416 |
? COPYFILE_XATTR | COPYFILE_NOFOLLOW_SRC |
| 417 |
: COPYFILE_XATTR | COPYFILE_ACL))) { |
| 418 |
return TCL_OK; |
| 419 |
} |
| 420 |
#endif /* HAVE_COPYFILE */ |
| 421 |
} else { |
| 422 |
#if (!defined(HAVE_COPYFILE) || defined(WEAK_IMPORT_COPYFILE)) && defined(HAVE_GETATTRLIST) |
| 423 |
struct attrlist alist; |
| 424 |
fileinfobuf finfo; |
| 425 |
off_t *rsrcForkSize = (off_t *) &finfo.data; |
| 426 |
Tcl_DString srcBuf, dstBuf; |
| 427 |
int result; |
| 428 |
|
| 429 |
bzero(&alist, sizeof(struct attrlist)); |
| 430 |
alist.bitmapcount = ATTR_BIT_MAP_COUNT; |
| 431 |
alist.commonattr = ATTR_CMN_FNDRINFO; |
| 432 |
|
| 433 |
if (getattrlist(src, &alist, &finfo.info_length, |
| 434 |
sizeof(fileinfobuf) - offsetof(fileinfobuf, info_length), 0)) { |
| 435 |
return TCL_ERROR; |
| 436 |
} |
| 437 |
if (setattrlist(dst, &alist, &finfo.data, sizeof(finfo.data), 0)) { |
| 438 |
return TCL_ERROR; |
| 439 |
} |
| 440 |
|
| 441 |
/* |
| 442 |
* If we're a directory, we're done as they never have resource forks. |
| 443 |
*/ |
| 444 |
|
| 445 |
if (S_ISDIR(statBufPtr->st_mode)) { |
| 446 |
return TCL_OK; |
| 447 |
} |
| 448 |
|
| 449 |
/* |
| 450 |
* We only copy a non-empty resource fork, so determine if that's the |
| 451 |
* case first. |
| 452 |
*/ |
| 453 |
|
| 454 |
alist.commonattr = 0; |
| 455 |
alist.fileattr = ATTR_FILE_RSRCLENGTH; |
| 456 |
if (getattrlist(src, &alist, &finfo.info_length, |
| 457 |
sizeof(fileinfobuf) - offsetof(fileinfobuf, info_length), 0)) { |
| 458 |
return TCL_ERROR; |
| 459 |
} else if (*rsrcForkSize == 0) { |
| 460 |
return TCL_OK; |
| 461 |
} |
| 462 |
|
| 463 |
/* |
| 464 |
* Construct paths to resource forks. |
| 465 |
*/ |
| 466 |
|
| 467 |
Tcl_DStringInit(&srcBuf); |
| 468 |
Tcl_DStringAppend(&srcBuf, src, -1); |
| 469 |
Tcl_DStringAppend(&srcBuf, _PATH_RSRCFORKSPEC, -1); |
| 470 |
Tcl_DStringInit(&dstBuf); |
| 471 |
Tcl_DStringAppend(&dstBuf, dst, -1); |
| 472 |
Tcl_DStringAppend(&dstBuf, _PATH_RSRCFORKSPEC, -1); |
| 473 |
|
| 474 |
/* |
| 475 |
* Do the copy. |
| 476 |
*/ |
| 477 |
|
| 478 |
result = TclUnixCopyFile(Tcl_DStringValue(&srcBuf), |
| 479 |
Tcl_DStringValue(&dstBuf), statBufPtr, 1); |
| 480 |
Tcl_DStringFree(&srcBuf); |
| 481 |
Tcl_DStringFree(&dstBuf); |
| 482 |
if (result == 0) { |
| 483 |
return TCL_OK; |
| 484 |
} |
| 485 |
#endif /* (!HAVE_COPYFILE || WEAK_IMPORT_COPYFILE) && HAVE_GETATTRLIST */ |
| 486 |
} |
| 487 |
return TCL_ERROR; |
| 488 |
} |
| 489 |
|
| 490 |
/* |
| 491 |
*---------------------------------------------------------------------- |
| 492 |
* |
| 493 |
* TclMacOSXMatchType -- |
| 494 |
* |
| 495 |
* This routine is used by the globbing code to check if a file matches a |
| 496 |
* given mac type and/or creator code. |
| 497 |
* |
| 498 |
* Results: |
| 499 |
* The return value is 1, 0 or -1 indicating whether the file matches the |
| 500 |
* given criteria, does not match them, or an error occurred (in which |
| 501 |
* case an error is left in interp). |
| 502 |
* |
| 503 |
* Side effects: |
| 504 |
* None. |
| 505 |
* |
| 506 |
*---------------------------------------------------------------------- |
| 507 |
*/ |
| 508 |
|
| 509 |
int |
| 510 |
TclMacOSXMatchType( |
| 511 |
Tcl_Interp *interp, /* Interpreter to receive errors. */ |
| 512 |
const char *pathName, /* Native path to check. */ |
| 513 |
const char *fileName, /* Native filename to check. */ |
| 514 |
Tcl_StatBuf *statBufPtr, /* Stat info for file to check */ |
| 515 |
Tcl_GlobTypeData *types) /* Type description to match against. */ |
| 516 |
{ |
| 517 |
#ifdef HAVE_GETATTRLIST |
| 518 |
struct attrlist alist; |
| 519 |
fileinfobuf finfo; |
| 520 |
finderinfo *finder = (finderinfo *) &finfo.data; |
| 521 |
OSType osType; |
| 522 |
|
| 523 |
bzero(&alist, sizeof(struct attrlist)); |
| 524 |
alist.bitmapcount = ATTR_BIT_MAP_COUNT; |
| 525 |
alist.commonattr = ATTR_CMN_FNDRINFO; |
| 526 |
if (getattrlist(pathName, &alist, &finfo.info_length, |
| 527 |
sizeof(fileinfobuf) - offsetof(fileinfobuf, info_length), 0)) { |
| 528 |
return 0; |
| 529 |
} |
| 530 |
if ((types->perm & TCL_GLOB_PERM_HIDDEN) && |
| 531 |
!((finder->fdFlags & kFinfoIsInvisible) || (*fileName == '.'))) { |
| 532 |
return 0; |
| 533 |
} |
| 534 |
if (S_ISDIR(statBufPtr->st_mode) |
| 535 |
&& (types->macType || types->macCreator)) { |
| 536 |
/* |
| 537 |
* Directories don't support types or creators. |
| 538 |
*/ |
| 539 |
|
| 540 |
return 0; |
| 541 |
} |
| 542 |
if (types->macType) { |
| 543 |
if (GetOSTypeFromObj(interp, types->macType, &osType) != TCL_OK) { |
| 544 |
return -1; |
| 545 |
} |
| 546 |
if (osType != OSSwapBigToHostInt32(finder->type)) { |
| 547 |
return 0; |
| 548 |
} |
| 549 |
} |
| 550 |
if (types->macCreator) { |
| 551 |
if (GetOSTypeFromObj(interp, types->macCreator, &osType) != TCL_OK) { |
| 552 |
return -1; |
| 553 |
} |
| 554 |
if (osType != OSSwapBigToHostInt32(finder->creator)) { |
| 555 |
return 0; |
| 556 |
} |
| 557 |
} |
| 558 |
#endif |
| 559 |
return 1; |
| 560 |
} |
| 561 |
|
| 562 |
/* |
| 563 |
*---------------------------------------------------------------------- |
| 564 |
* |
| 565 |
* GetOSTypeFromObj -- |
| 566 |
* |
| 567 |
* Attempt to return an OSType from the Tcl object "objPtr". |
| 568 |
* |
| 569 |
* Results: |
| 570 |
* Standard TCL result. If an error occurs during conversion, an error |
| 571 |
* message is left in interp->objResult. |
| 572 |
* |
| 573 |
* Side effects: |
| 574 |
* The string representation of objPtr will be updated if necessary. |
| 575 |
* |
| 576 |
*---------------------------------------------------------------------- |
| 577 |
*/ |
| 578 |
|
| 579 |
static int |
| 580 |
GetOSTypeFromObj( |
| 581 |
Tcl_Interp *interp, /* Used for error reporting if not NULL. */ |
| 582 |
Tcl_Obj *objPtr, /* The object from which to get an OSType. */ |
| 583 |
OSType *osTypePtr) /* Place to store resulting OSType. */ |
| 584 |
{ |
| 585 |
int result = TCL_OK; |
| 586 |
|
| 587 |
if (objPtr->typePtr != &tclOSTypeType) { |
| 588 |
result = SetOSTypeFromAny(interp, objPtr); |
| 589 |
} |
| 590 |
*osTypePtr = (OSType) objPtr->internalRep.longValue; |
| 591 |
return result; |
| 592 |
} |
| 593 |
|
| 594 |
/* |
| 595 |
*---------------------------------------------------------------------- |
| 596 |
* |
| 597 |
* NewOSTypeObj -- |
| 598 |
* |
| 599 |
* Create a new OSType object. |
| 600 |
* |
| 601 |
* Results: |
| 602 |
* The newly created OSType object is returned, it has ref count 0. |
| 603 |
* |
| 604 |
* Side effects: |
| 605 |
* None. |
| 606 |
* |
| 607 |
*---------------------------------------------------------------------- |
| 608 |
*/ |
| 609 |
|
| 610 |
static Tcl_Obj * |
| 611 |
NewOSTypeObj( |
| 612 |
const OSType osType) /* OSType used to initialize the new |
| 613 |
* object. */ |
| 614 |
{ |
| 615 |
Tcl_Obj *objPtr; |
| 616 |
|
| 617 |
TclNewObj(objPtr); |
| 618 |
TclInvalidateStringRep(objPtr); |
| 619 |
objPtr->internalRep.longValue = (long) osType; |
| 620 |
objPtr->typePtr = &tclOSTypeType; |
| 621 |
return objPtr; |
| 622 |
} |
| 623 |
|
| 624 |
/* |
| 625 |
*---------------------------------------------------------------------- |
| 626 |
* |
| 627 |
* SetOSTypeFromAny -- |
| 628 |
* |
| 629 |
* Attempts to force the internal representation for a Tcl object to |
| 630 |
* tclOSTypeType, specifically. |
| 631 |
* |
| 632 |
* Results: |
| 633 |
* The return value is a standard object Tcl result. If an error occurs |
| 634 |
* during conversion, an error message is left in the interpreter's |
| 635 |
* result unless "interp" is NULL. |
| 636 |
* |
| 637 |
*---------------------------------------------------------------------- |
| 638 |
*/ |
| 639 |
|
| 640 |
static int |
| 641 |
SetOSTypeFromAny( |
| 642 |
Tcl_Interp *interp, /* Tcl interpreter */ |
| 643 |
Tcl_Obj *objPtr) /* Pointer to the object to convert */ |
| 644 |
{ |
| 645 |
const char *string; |
| 646 |
int result = TCL_OK; |
| 647 |
Tcl_DString ds; |
| 648 |
Tcl_Encoding encoding = Tcl_GetEncoding(NULL, "macRoman"); |
| 649 |
int length; |
| 650 |
|
| 651 |
string = TclGetStringFromObj(objPtr, &length); |
| 652 |
Tcl_UtfToExternalDString(encoding, string, length, &ds); |
| 653 |
|
| 654 |
if (Tcl_DStringLength(&ds) > 4) { |
| 655 |
if (interp) { |
| 656 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 657 |
"expected Macintosh OS type but got \"%s\": ", string)); |
| 658 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "MAC_OSTYPE", (char *)NULL); |
| 659 |
} |
| 660 |
result = TCL_ERROR; |
| 661 |
} else { |
| 662 |
OSType osType; |
| 663 |
char bytes[4] = {'\0','\0','\0','\0'}; |
| 664 |
|
| 665 |
memcpy(bytes, Tcl_DStringValue(&ds), Tcl_DStringLength(&ds)); |
| 666 |
osType = (OSType) bytes[0] << 24 | |
| 667 |
(OSType) bytes[1] << 16 | |
| 668 |
(OSType) bytes[2] << 8 | |
| 669 |
(OSType) bytes[3]; |
| 670 |
TclFreeIntRep(objPtr); |
| 671 |
objPtr->internalRep.longValue = (long) osType; |
| 672 |
objPtr->typePtr = &tclOSTypeType; |
| 673 |
} |
| 674 |
Tcl_DStringFree(&ds); |
| 675 |
Tcl_FreeEncoding(encoding); |
| 676 |
return result; |
| 677 |
} |
| 678 |
|
| 679 |
/* |
| 680 |
*---------------------------------------------------------------------- |
| 681 |
* |
| 682 |
* UpdateStringOfOSType -- |
| 683 |
* |
| 684 |
* Update the string representation for an OSType object. Note: This |
| 685 |
* function does not free an existing old string rep so storage will be |
| 686 |
* lost if this has not already been done. |
| 687 |
* |
| 688 |
* Results: |
| 689 |
* None. |
| 690 |
* |
| 691 |
* Side effects: |
| 692 |
* The object's string is set to a valid string that results from the |
| 693 |
* OSType-to-string conversion. |
| 694 |
* |
| 695 |
*---------------------------------------------------------------------- |
| 696 |
*/ |
| 697 |
|
| 698 |
static void |
| 699 |
UpdateStringOfOSType( |
| 700 |
Tcl_Obj *objPtr) /* OSType object whose string rep to |
| 701 |
* update. */ |
| 702 |
{ |
| 703 |
OSType osType = (OSType) objPtr->internalRep.longValue; |
| 704 |
Tcl_DString ds; |
| 705 |
Tcl_Encoding encoding = Tcl_GetEncoding(NULL, "macRoman"); |
| 706 |
unsigned len; |
| 707 |
char src[5]; |
| 708 |
|
| 709 |
src[0] = (char) (osType >> 24); |
| 710 |
src[1] = (char) (osType >> 16); |
| 711 |
src[2] = (char) (osType >> 8); |
| 712 |
src[3] = (char) (osType); |
| 713 |
src[4] = '\0'; |
| 714 |
Tcl_ExternalToUtfDString(encoding, src, -1, &ds); |
| 715 |
len = (unsigned)Tcl_DStringLength(&ds) + 1; |
| 716 |
objPtr->bytes = ckalloc(len); |
| 717 |
memcpy(objPtr->bytes, Tcl_DStringValue(&ds), len); |
| 718 |
objPtr->length = Tcl_DStringLength(&ds); |
| 719 |
Tcl_DStringFree(&ds); |
| 720 |
Tcl_FreeEncoding(encoding); |
| 721 |
} |
| 722 |
|
| 723 |
/* |
| 724 |
* Local Variables: |
| 725 |
* mode: c |
| 726 |
* c-basic-offset: 4 |
| 727 |
* fill-column: 78 |
| 728 |
* End: |
| 729 |
*/ |