| 1 |
/* |
| 2 |
* tclIOUtil.c -- |
| 3 |
* |
| 4 |
* This file contains the implementation of Tcl's generic filesystem |
| 5 |
* code, which supports a pluggable filesystem architecture allowing both |
| 6 |
* platform specific filesystems and 'virtual filesystems'. All |
| 7 |
* filesystem access should go through the functions defined in this |
| 8 |
* file. Most of this code was contributed by Vince Darley. |
| 9 |
* |
| 10 |
* Parts of this file are based on code contributed by Karl Lehenbauer, |
| 11 |
* Mark Diekhans and Peter da Silva. |
| 12 |
* |
| 13 |
* Copyright (c) 1991-1994 The Regents of the University of California. |
| 14 |
* Copyright (c) 1994-1997 Sun Microsystems, Inc. |
| 15 |
* Copyright (c) 2001-2004 Vincent Darley. |
| 16 |
* |
| 17 |
* See the file "license.terms" for information on usage and redistribution of |
| 18 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "tclInt.h" |
| 22 |
#ifdef _WIN32 |
| 23 |
# include "tclWinInt.h" |
| 24 |
#endif |
| 25 |
#include "tclFileSystem.h" |
| 26 |
|
| 27 |
#ifdef TCL_TEMPLOAD_NO_UNLINK |
| 28 |
#ifndef NO_FSTATFS |
| 29 |
#include <sys/statfs.h> |
| 30 |
#endif |
| 31 |
#endif |
| 32 |
|
| 33 |
/* |
| 34 |
* struct FilesystemRecord -- |
| 35 |
* |
| 36 |
* A filesystem record is used to keep track of each filesystem currently |
| 37 |
* registered with the core, in a linked list. |
| 38 |
*/ |
| 39 |
|
| 40 |
typedef struct FilesystemRecord { |
| 41 |
ClientData clientData; /* Client specific data for the new filesystem |
| 42 |
* (can be NULL) */ |
| 43 |
const Tcl_Filesystem *fsPtr;/* Pointer to filesystem dispatch table. */ |
| 44 |
struct FilesystemRecord *nextPtr; |
| 45 |
/* The next filesystem registered to Tcl, or |
| 46 |
* NULL if no more. */ |
| 47 |
struct FilesystemRecord *prevPtr; |
| 48 |
/* The previous filesystem registered to Tcl, |
| 49 |
* or NULL if no more. */ |
| 50 |
} FilesystemRecord; |
| 51 |
|
| 52 |
/* |
| 53 |
* This structure holds per-thread private copy of the current directory |
| 54 |
* maintained by the global cwdPathPtr. This structure holds per-thread |
| 55 |
* private copies of some global data. This way we avoid most of the |
| 56 |
* synchronization calls which boosts performance, at cost of having to update |
| 57 |
* this information each time the corresponding epoch counter changes. |
| 58 |
*/ |
| 59 |
|
| 60 |
typedef struct ThreadSpecificData { |
| 61 |
int initialized; |
| 62 |
size_t cwdPathEpoch; |
| 63 |
size_t filesystemEpoch; |
| 64 |
Tcl_Obj *cwdPathPtr; |
| 65 |
ClientData cwdClientData; |
| 66 |
FilesystemRecord *filesystemList; |
| 67 |
size_t claims; |
| 68 |
} ThreadSpecificData; |
| 69 |
|
| 70 |
/* |
| 71 |
* Prototypes for functions defined later in this file. |
| 72 |
*/ |
| 73 |
|
| 74 |
static Tcl_NRPostProc EvalFileCallback; |
| 75 |
static FilesystemRecord*FsGetFirstFilesystem(void); |
| 76 |
static void FsThrExitProc(ClientData cd); |
| 77 |
static Tcl_Obj * FsListMounts(Tcl_Obj *pathPtr, const char *pattern); |
| 78 |
static void FsAddMountsToGlobResult(Tcl_Obj *resultPtr, |
| 79 |
Tcl_Obj *pathPtr, const char *pattern, |
| 80 |
Tcl_GlobTypeData *types); |
| 81 |
static void FsUpdateCwd(Tcl_Obj *cwdObj, ClientData clientData); |
| 82 |
static void FsRecacheFilesystemList(void); |
| 83 |
static void Claim(void); |
| 84 |
static void Disclaim(void); |
| 85 |
|
| 86 |
static void * DivertFindSymbol(Tcl_Interp *interp, |
| 87 |
Tcl_LoadHandle loadHandle, const char *symbol); |
| 88 |
static void DivertUnloadFile(Tcl_LoadHandle loadHandle); |
| 89 |
|
| 90 |
/* |
| 91 |
* These form part of the native filesystem support. They are needed here |
| 92 |
* because we have a few native filesystem functions (which are the same for |
| 93 |
* win/unix) in this file. There is no need to place them in tclInt.h, because |
| 94 |
* they are not (and should not be) used anywhere else. |
| 95 |
*/ |
| 96 |
|
| 97 |
MODULE_SCOPE const char *const tclpFileAttrStrings[]; |
| 98 |
MODULE_SCOPE const TclFileAttrProcs tclpFileAttrProcs[]; |
| 99 |
|
| 100 |
/* |
| 101 |
* Declare the native filesystem support. These functions should be considered |
| 102 |
* private to Tcl, and should really not be called directly by any code other |
| 103 |
* than this file (i.e. neither by Tcl's core nor by extensions). Similarly, |
| 104 |
* the old string-based Tclp... native filesystem functions should not be |
| 105 |
* called. |
| 106 |
* |
| 107 |
* The correct API to use now is the Tcl_FS... set of functions, which ensure |
| 108 |
* correct and complete virtual filesystem support. |
| 109 |
* |
| 110 |
* We cannot make all of these static, since some of them are implemented in |
| 111 |
* the platform-specific directories. |
| 112 |
*/ |
| 113 |
|
| 114 |
static Tcl_FSFilesystemSeparatorProc NativeFilesystemSeparator; |
| 115 |
static Tcl_FSFreeInternalRepProc NativeFreeInternalRep; |
| 116 |
static Tcl_FSFileAttrStringsProc NativeFileAttrStrings; |
| 117 |
static Tcl_FSFileAttrsGetProc NativeFileAttrsGet; |
| 118 |
static Tcl_FSFileAttrsSetProc NativeFileAttrsSet; |
| 119 |
|
| 120 |
/* |
| 121 |
* The only reason these functions are not static is that they are either |
| 122 |
* called by code in the native (win/unix) directories or they are actually |
| 123 |
* implemented in those directories. They should simply not be called by code |
| 124 |
* outside Tcl's native filesystem core i.e. they should be considered |
| 125 |
* 'static' to Tcl's filesystem code (if we ever built the native filesystem |
| 126 |
* support into a separate code library, this could actually be enforced). |
| 127 |
*/ |
| 128 |
|
| 129 |
Tcl_FSFilesystemPathTypeProc TclpFilesystemPathType; |
| 130 |
Tcl_FSInternalToNormalizedProc TclpNativeToNormalized; |
| 131 |
Tcl_FSStatProc TclpObjStat; |
| 132 |
Tcl_FSAccessProc TclpObjAccess; |
| 133 |
Tcl_FSMatchInDirectoryProc TclpMatchInDirectory; |
| 134 |
Tcl_FSChdirProc TclpObjChdir; |
| 135 |
Tcl_FSLstatProc TclpObjLstat; |
| 136 |
Tcl_FSCopyFileProc TclpObjCopyFile; |
| 137 |
Tcl_FSDeleteFileProc TclpObjDeleteFile; |
| 138 |
Tcl_FSRenameFileProc TclpObjRenameFile; |
| 139 |
Tcl_FSCreateDirectoryProc TclpObjCreateDirectory; |
| 140 |
Tcl_FSCopyDirectoryProc TclpObjCopyDirectory; |
| 141 |
Tcl_FSRemoveDirectoryProc TclpObjRemoveDirectory; |
| 142 |
Tcl_FSLinkProc TclpObjLink; |
| 143 |
Tcl_FSListVolumesProc TclpObjListVolumes; |
| 144 |
|
| 145 |
/* |
| 146 |
* Define the native filesystem dispatch table. If necessary, it is ok to make |
| 147 |
* this non-static, but it should only be accessed by the functions actually |
| 148 |
* listed within it (or perhaps other helper functions of them). Anything |
| 149 |
* which is not part of this 'native filesystem implementation' should not be |
| 150 |
* delving inside here! |
| 151 |
*/ |
| 152 |
|
| 153 |
const Tcl_Filesystem tclNativeFilesystem = { |
| 154 |
"native", |
| 155 |
sizeof(Tcl_Filesystem), |
| 156 |
TCL_FILESYSTEM_VERSION_2, |
| 157 |
TclNativePathInFilesystem, |
| 158 |
TclNativeDupInternalRep, |
| 159 |
NativeFreeInternalRep, |
| 160 |
TclpNativeToNormalized, |
| 161 |
TclNativeCreateNativeRep, |
| 162 |
TclpObjNormalizePath, |
| 163 |
TclpFilesystemPathType, |
| 164 |
NativeFilesystemSeparator, |
| 165 |
TclpObjStat, |
| 166 |
TclpObjAccess, |
| 167 |
TclpOpenFileChannel, |
| 168 |
TclpMatchInDirectory, |
| 169 |
TclpUtime, |
| 170 |
#ifndef S_IFLNK |
| 171 |
NULL, |
| 172 |
#else |
| 173 |
TclpObjLink, |
| 174 |
#endif /* S_IFLNK */ |
| 175 |
TclpObjListVolumes, |
| 176 |
NativeFileAttrStrings, |
| 177 |
NativeFileAttrsGet, |
| 178 |
NativeFileAttrsSet, |
| 179 |
TclpObjCreateDirectory, |
| 180 |
TclpObjRemoveDirectory, |
| 181 |
TclpObjDeleteFile, |
| 182 |
TclpObjCopyFile, |
| 183 |
TclpObjRenameFile, |
| 184 |
TclpObjCopyDirectory, |
| 185 |
TclpObjLstat, |
| 186 |
/* Needs casts since we're using version_2. */ |
| 187 |
(Tcl_FSLoadFileProc *)(void *) TclpDlopen, |
| 188 |
(Tcl_FSGetCwdProc *) TclpGetNativeCwd, |
| 189 |
TclpObjChdir |
| 190 |
}; |
| 191 |
|
| 192 |
/* |
| 193 |
* Define the tail of the linked list. Note that for unconventional uses of |
| 194 |
* Tcl without a native filesystem, we may in the future wish to modify the |
| 195 |
* current approach of hard-coding the native filesystem in the lookup list |
| 196 |
* 'filesystemList' below. |
| 197 |
* |
| 198 |
* We initialize the record so that it thinks one file uses it. This means it |
| 199 |
* will never be freed. |
| 200 |
*/ |
| 201 |
|
| 202 |
static FilesystemRecord nativeFilesystemRecord = { |
| 203 |
NULL, |
| 204 |
&tclNativeFilesystem, |
| 205 |
NULL, |
| 206 |
NULL |
| 207 |
}; |
| 208 |
|
| 209 |
/* |
| 210 |
* This is incremented each time we modify the linked list of filesystems. Any |
| 211 |
* time it changes, all cached filesystem representations are suspect and must |
| 212 |
* be freed. For multithreading builds, change of the filesystem epoch will |
| 213 |
* trigger cache cleanup in all threads. |
| 214 |
*/ |
| 215 |
|
| 216 |
static size_t theFilesystemEpoch = 1; |
| 217 |
|
| 218 |
/* |
| 219 |
* Stores the linked list of filesystems. A 1:1 copy of this list is also |
| 220 |
* maintained in the TSD for each thread. This is to avoid synchronization |
| 221 |
* issues. |
| 222 |
*/ |
| 223 |
|
| 224 |
static FilesystemRecord *filesystemList = &nativeFilesystemRecord; |
| 225 |
TCL_DECLARE_MUTEX(filesystemMutex) |
| 226 |
|
| 227 |
/* |
| 228 |
* Used to implement Tcl_FSGetCwd in a file-system independent way. |
| 229 |
*/ |
| 230 |
|
| 231 |
static Tcl_Obj *cwdPathPtr = NULL; |
| 232 |
static size_t cwdPathEpoch = 0; |
| 233 |
static ClientData cwdClientData = NULL; |
| 234 |
TCL_DECLARE_MUTEX(cwdMutex) |
| 235 |
|
| 236 |
static Tcl_ThreadDataKey fsDataKey; |
| 237 |
|
| 238 |
/* |
| 239 |
* One of these structures is used each time we successfully load a file from |
| 240 |
* a file system by way of making a temporary copy of the file on the native |
| 241 |
* filesystem. We need to store both the actual unloadProc/clientData |
| 242 |
* combination which was used, and the original and modified filenames, so |
| 243 |
* that we can correctly undo the entire operation when we want to unload the |
| 244 |
* code. |
| 245 |
*/ |
| 246 |
|
| 247 |
typedef struct FsDivertLoad { |
| 248 |
Tcl_LoadHandle loadHandle; |
| 249 |
Tcl_FSUnloadFileProc *unloadProcPtr; |
| 250 |
Tcl_Obj *divertedFile; |
| 251 |
const Tcl_Filesystem *divertedFilesystem; |
| 252 |
ClientData divertedFileNativeRep; |
| 253 |
} FsDivertLoad; |
| 254 |
|
| 255 |
/* |
| 256 |
* The following functions are obsolete string based APIs, and should be |
| 257 |
* removed in a future release (Tcl 9 would be a good time). |
| 258 |
*/ |
| 259 |
|
| 260 |
/* Obsolete */ |
| 261 |
int |
| 262 |
Tcl_Stat( |
| 263 |
const char *path, /* Path of file to stat (in current CP). */ |
| 264 |
struct stat *oldStyleBuf) /* Filled with results of stat call. */ |
| 265 |
{ |
| 266 |
int ret; |
| 267 |
Tcl_StatBuf buf; |
| 268 |
Tcl_Obj *pathPtr = Tcl_NewStringObj(path,-1); |
| 269 |
|
| 270 |
Tcl_IncrRefCount(pathPtr); |
| 271 |
ret = Tcl_FSStat(pathPtr, &buf); |
| 272 |
Tcl_DecrRefCount(pathPtr); |
| 273 |
if (ret != -1) { |
| 274 |
#ifndef TCL_WIDE_INT_IS_LONG |
| 275 |
Tcl_WideInt tmp1, tmp2, tmp3 = 0; |
| 276 |
|
| 277 |
# define OUT_OF_RANGE(x) \ |
| 278 |
(((Tcl_WideInt)(x)) < Tcl_LongAsWide(LONG_MIN) || \ |
| 279 |
((Tcl_WideInt)(x)) > Tcl_LongAsWide(LONG_MAX)) |
| 280 |
# define OUT_OF_URANGE(x) \ |
| 281 |
(((Tcl_WideUInt)(x)) > ((Tcl_WideUInt)ULONG_MAX)) |
| 282 |
|
| 283 |
/* |
| 284 |
* Perform the result-buffer overflow check manually. |
| 285 |
* |
| 286 |
* Note that ino_t/ino64_t is unsigned... |
| 287 |
* |
| 288 |
* Workaround gcc warning of "comparison is always false due to |
| 289 |
* limited range of data type" by assigning to tmp var of type |
| 290 |
* Tcl_WideInt. |
| 291 |
*/ |
| 292 |
|
| 293 |
tmp1 = buf.st_ino; |
| 294 |
tmp2 = buf.st_size; |
| 295 |
#ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
| 296 |
tmp3 = buf.st_blocks; |
| 297 |
#endif |
| 298 |
|
| 299 |
if (OUT_OF_URANGE(tmp1) || OUT_OF_RANGE(tmp2) || OUT_OF_RANGE(tmp3)) { |
| 300 |
#if defined(EFBIG) |
| 301 |
errno = EFBIG; |
| 302 |
#elif defined(EOVERFLOW) |
| 303 |
errno = EOVERFLOW; |
| 304 |
#else |
| 305 |
#error "What status should be returned for file size out of range?" |
| 306 |
#endif |
| 307 |
return -1; |
| 308 |
} |
| 309 |
|
| 310 |
# undef OUT_OF_RANGE |
| 311 |
# undef OUT_OF_URANGE |
| 312 |
#endif /* !TCL_WIDE_INT_IS_LONG */ |
| 313 |
|
| 314 |
/* |
| 315 |
* Copy across all supported fields, with possible type coercion on |
| 316 |
* those fields that change between the normal and lf64 versions of |
| 317 |
* the stat structure (on Solaris at least). This is slow when the |
| 318 |
* structure sizes coincide, but that's what you get for using an |
| 319 |
* obsolete interface. |
| 320 |
*/ |
| 321 |
|
| 322 |
oldStyleBuf->st_mode = buf.st_mode; |
| 323 |
oldStyleBuf->st_ino = (ino_t) buf.st_ino; |
| 324 |
oldStyleBuf->st_dev = buf.st_dev; |
| 325 |
oldStyleBuf->st_rdev = buf.st_rdev; |
| 326 |
oldStyleBuf->st_nlink = buf.st_nlink; |
| 327 |
oldStyleBuf->st_uid = buf.st_uid; |
| 328 |
oldStyleBuf->st_gid = buf.st_gid; |
| 329 |
oldStyleBuf->st_size = (off_t) buf.st_size; |
| 330 |
oldStyleBuf->st_atime = Tcl_GetAccessTimeFromStat(&buf); |
| 331 |
oldStyleBuf->st_mtime = Tcl_GetModificationTimeFromStat(&buf); |
| 332 |
oldStyleBuf->st_ctime = Tcl_GetChangeTimeFromStat(&buf); |
| 333 |
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
| 334 |
oldStyleBuf->st_blksize = buf.st_blksize; |
| 335 |
#endif |
| 336 |
#ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
| 337 |
#ifdef HAVE_BLKCNT_T |
| 338 |
oldStyleBuf->st_blocks = (blkcnt_t) buf.st_blocks; |
| 339 |
#else |
| 340 |
oldStyleBuf->st_blocks = (unsigned long) buf.st_blocks; |
| 341 |
#endif |
| 342 |
#endif |
| 343 |
} |
| 344 |
return ret; |
| 345 |
} |
| 346 |
|
| 347 |
/* Obsolete */ |
| 348 |
int |
| 349 |
Tcl_Access( |
| 350 |
const char *path, /* Path of file to access (in current CP). */ |
| 351 |
int mode) /* Permission setting. */ |
| 352 |
{ |
| 353 |
int ret; |
| 354 |
Tcl_Obj *pathPtr = Tcl_NewStringObj(path,-1); |
| 355 |
|
| 356 |
Tcl_IncrRefCount(pathPtr); |
| 357 |
ret = Tcl_FSAccess(pathPtr,mode); |
| 358 |
Tcl_DecrRefCount(pathPtr); |
| 359 |
|
| 360 |
return ret; |
| 361 |
} |
| 362 |
|
| 363 |
/* Obsolete */ |
| 364 |
Tcl_Channel |
| 365 |
Tcl_OpenFileChannel( |
| 366 |
Tcl_Interp *interp, /* Interpreter for error reporting; can be |
| 367 |
* NULL. */ |
| 368 |
const char *path, /* Name of file to open. */ |
| 369 |
const char *modeString, /* A list of POSIX open modes or a string such |
| 370 |
* as "rw". */ |
| 371 |
int permissions) /* If the open involves creating a file, with |
| 372 |
* what modes to create it? */ |
| 373 |
{ |
| 374 |
Tcl_Channel ret; |
| 375 |
Tcl_Obj *pathPtr = Tcl_NewStringObj(path,-1); |
| 376 |
|
| 377 |
Tcl_IncrRefCount(pathPtr); |
| 378 |
ret = Tcl_FSOpenFileChannel(interp, pathPtr, modeString, permissions); |
| 379 |
Tcl_DecrRefCount(pathPtr); |
| 380 |
|
| 381 |
return ret; |
| 382 |
} |
| 383 |
|
| 384 |
/* Obsolete */ |
| 385 |
int |
| 386 |
Tcl_Chdir( |
| 387 |
const char *dirName) |
| 388 |
{ |
| 389 |
int ret; |
| 390 |
Tcl_Obj *pathPtr = Tcl_NewStringObj(dirName,-1); |
| 391 |
Tcl_IncrRefCount(pathPtr); |
| 392 |
ret = Tcl_FSChdir(pathPtr); |
| 393 |
Tcl_DecrRefCount(pathPtr); |
| 394 |
return ret; |
| 395 |
} |
| 396 |
|
| 397 |
/* Obsolete */ |
| 398 |
char * |
| 399 |
Tcl_GetCwd( |
| 400 |
Tcl_Interp *interp, |
| 401 |
Tcl_DString *cwdPtr) |
| 402 |
{ |
| 403 |
Tcl_Obj *cwd = Tcl_FSGetCwd(interp); |
| 404 |
|
| 405 |
if (cwd == NULL) { |
| 406 |
return NULL; |
| 407 |
} |
| 408 |
Tcl_DStringInit(cwdPtr); |
| 409 |
TclDStringAppendObj(cwdPtr, cwd); |
| 410 |
Tcl_DecrRefCount(cwd); |
| 411 |
return Tcl_DStringValue(cwdPtr); |
| 412 |
} |
| 413 |
|
| 414 |
int |
| 415 |
Tcl_EvalFile( |
| 416 |
Tcl_Interp *interp, /* Interpreter in which to process file. */ |
| 417 |
const char *fileName) /* Name of file to process. Tilde-substitution |
| 418 |
* will be performed on this name. */ |
| 419 |
{ |
| 420 |
int ret; |
| 421 |
Tcl_Obj *pathPtr = Tcl_NewStringObj(fileName,-1); |
| 422 |
|
| 423 |
Tcl_IncrRefCount(pathPtr); |
| 424 |
ret = Tcl_FSEvalFile(interp, pathPtr); |
| 425 |
Tcl_DecrRefCount(pathPtr); |
| 426 |
return ret; |
| 427 |
} |
| 428 |
|
| 429 |
/* |
| 430 |
* Now move on to the basic filesystem implementation. |
| 431 |
*/ |
| 432 |
|
| 433 |
static void |
| 434 |
FsThrExitProc( |
| 435 |
ClientData cd) |
| 436 |
{ |
| 437 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)cd; |
| 438 |
FilesystemRecord *fsRecPtr = NULL, *tmpFsRecPtr = NULL; |
| 439 |
|
| 440 |
/* |
| 441 |
* Trash the cwd copy. |
| 442 |
*/ |
| 443 |
|
| 444 |
if (tsdPtr->cwdPathPtr != NULL) { |
| 445 |
Tcl_DecrRefCount(tsdPtr->cwdPathPtr); |
| 446 |
tsdPtr->cwdPathPtr = NULL; |
| 447 |
} |
| 448 |
if (tsdPtr->cwdClientData != NULL) { |
| 449 |
NativeFreeInternalRep(tsdPtr->cwdClientData); |
| 450 |
} |
| 451 |
|
| 452 |
/* |
| 453 |
* Trash the filesystems cache. |
| 454 |
*/ |
| 455 |
|
| 456 |
fsRecPtr = tsdPtr->filesystemList; |
| 457 |
while (fsRecPtr != NULL) { |
| 458 |
tmpFsRecPtr = fsRecPtr->nextPtr; |
| 459 |
fsRecPtr->fsPtr = NULL; |
| 460 |
ckfree(fsRecPtr); |
| 461 |
fsRecPtr = tmpFsRecPtr; |
| 462 |
} |
| 463 |
tsdPtr->filesystemList = NULL; |
| 464 |
tsdPtr->initialized = 0; |
| 465 |
} |
| 466 |
|
| 467 |
int |
| 468 |
TclFSCwdIsNative(void) |
| 469 |
{ |
| 470 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&fsDataKey); |
| 471 |
|
| 472 |
/* if not yet initialized - ensure we'll once obtain cwd */ |
| 473 |
if (!tsdPtr->cwdPathEpoch) { |
| 474 |
Tcl_Obj *temp = Tcl_FSGetCwd(NULL); |
| 475 |
if (temp) { Tcl_DecrRefCount(temp); } |
| 476 |
} |
| 477 |
|
| 478 |
if (tsdPtr->cwdClientData != NULL) { |
| 479 |
return 1; |
| 480 |
} else { |
| 481 |
return 0; |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
/* |
| 486 |
*---------------------------------------------------------------------- |
| 487 |
* |
| 488 |
* TclFSCwdPointerEquals -- |
| 489 |
* |
| 490 |
* Check whether the current working directory is equal to the path |
| 491 |
* given. |
| 492 |
* |
| 493 |
* Results: |
| 494 |
* 1 (equal) or 0 (unequal) as appropriate. |
| 495 |
* |
| 496 |
* Side effects: |
| 497 |
* If the paths are equal, but are not the same object, this method will |
| 498 |
* modify the given pathPtrPtr to refer to the same object. In this case |
| 499 |
* the object pointed to by pathPtrPtr will have its refCount |
| 500 |
* decremented, and it will be adjusted to point to the cwd (with a new |
| 501 |
* refCount). |
| 502 |
* |
| 503 |
*---------------------------------------------------------------------- |
| 504 |
*/ |
| 505 |
|
| 506 |
int |
| 507 |
TclFSCwdPointerEquals( |
| 508 |
Tcl_Obj **pathPtrPtr) |
| 509 |
{ |
| 510 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&fsDataKey); |
| 511 |
|
| 512 |
Tcl_MutexLock(&cwdMutex); |
| 513 |
if (tsdPtr->cwdPathPtr == NULL |
| 514 |
|| tsdPtr->cwdPathEpoch != cwdPathEpoch) { |
| 515 |
if (tsdPtr->cwdPathPtr != NULL) { |
| 516 |
Tcl_DecrRefCount(tsdPtr->cwdPathPtr); |
| 517 |
} |
| 518 |
if (tsdPtr->cwdClientData != NULL) { |
| 519 |
NativeFreeInternalRep(tsdPtr->cwdClientData); |
| 520 |
} |
| 521 |
if (cwdPathPtr == NULL) { |
| 522 |
tsdPtr->cwdPathPtr = NULL; |
| 523 |
} else { |
| 524 |
tsdPtr->cwdPathPtr = Tcl_DuplicateObj(cwdPathPtr); |
| 525 |
Tcl_IncrRefCount(tsdPtr->cwdPathPtr); |
| 526 |
} |
| 527 |
if (cwdClientData == NULL) { |
| 528 |
tsdPtr->cwdClientData = NULL; |
| 529 |
} else { |
| 530 |
tsdPtr->cwdClientData = TclNativeDupInternalRep(cwdClientData); |
| 531 |
} |
| 532 |
tsdPtr->cwdPathEpoch = cwdPathEpoch; |
| 533 |
} |
| 534 |
Tcl_MutexUnlock(&cwdMutex); |
| 535 |
|
| 536 |
if (tsdPtr->initialized == 0) { |
| 537 |
Tcl_CreateThreadExitHandler(FsThrExitProc, tsdPtr); |
| 538 |
tsdPtr->initialized = 1; |
| 539 |
} |
| 540 |
|
| 541 |
if (pathPtrPtr == NULL) { |
| 542 |
return (tsdPtr->cwdPathPtr == NULL); |
| 543 |
} |
| 544 |
|
| 545 |
if (tsdPtr->cwdPathPtr == *pathPtrPtr) { |
| 546 |
return 1; |
| 547 |
} else { |
| 548 |
int len1, len2; |
| 549 |
const char *str1, *str2; |
| 550 |
|
| 551 |
str1 = TclGetStringFromObj(tsdPtr->cwdPathPtr, &len1); |
| 552 |
str2 = TclGetStringFromObj(*pathPtrPtr, &len2); |
| 553 |
if ((len1 == len2) && !memcmp(str1, str2, len1)) { |
| 554 |
/* |
| 555 |
* They are equal, but different objects. Update so they will be |
| 556 |
* the same object in the future. |
| 557 |
*/ |
| 558 |
|
| 559 |
Tcl_DecrRefCount(*pathPtrPtr); |
| 560 |
*pathPtrPtr = tsdPtr->cwdPathPtr; |
| 561 |
Tcl_IncrRefCount(*pathPtrPtr); |
| 562 |
return 1; |
| 563 |
} else { |
| 564 |
return 0; |
| 565 |
} |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
static void |
| 570 |
FsRecacheFilesystemList(void) |
| 571 |
{ |
| 572 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&fsDataKey); |
| 573 |
FilesystemRecord *fsRecPtr, *tmpFsRecPtr = NULL, *toFree = NULL, *list; |
| 574 |
|
| 575 |
/* |
| 576 |
* Trash the current cache. |
| 577 |
*/ |
| 578 |
|
| 579 |
fsRecPtr = tsdPtr->filesystemList; |
| 580 |
while (fsRecPtr != NULL) { |
| 581 |
tmpFsRecPtr = fsRecPtr->nextPtr; |
| 582 |
fsRecPtr->nextPtr = toFree; |
| 583 |
toFree = fsRecPtr; |
| 584 |
fsRecPtr = tmpFsRecPtr; |
| 585 |
} |
| 586 |
|
| 587 |
/* |
| 588 |
* Locate tail of the global filesystem list. |
| 589 |
*/ |
| 590 |
|
| 591 |
Tcl_MutexLock(&filesystemMutex); |
| 592 |
fsRecPtr = filesystemList; |
| 593 |
while (fsRecPtr != NULL) { |
| 594 |
tmpFsRecPtr = fsRecPtr; |
| 595 |
fsRecPtr = fsRecPtr->nextPtr; |
| 596 |
} |
| 597 |
|
| 598 |
/* |
| 599 |
* Refill the cache honouring the order. |
| 600 |
*/ |
| 601 |
|
| 602 |
list = NULL; |
| 603 |
fsRecPtr = tmpFsRecPtr; |
| 604 |
while (fsRecPtr != NULL) { |
| 605 |
tmpFsRecPtr = (FilesystemRecord *)ckalloc(sizeof(FilesystemRecord)); |
| 606 |
*tmpFsRecPtr = *fsRecPtr; |
| 607 |
tmpFsRecPtr->nextPtr = list; |
| 608 |
tmpFsRecPtr->prevPtr = NULL; |
| 609 |
list = tmpFsRecPtr; |
| 610 |
fsRecPtr = fsRecPtr->prevPtr; |
| 611 |
} |
| 612 |
tsdPtr->filesystemList = list; |
| 613 |
tsdPtr->filesystemEpoch = theFilesystemEpoch; |
| 614 |
Tcl_MutexUnlock(&filesystemMutex); |
| 615 |
|
| 616 |
while (toFree) { |
| 617 |
FilesystemRecord *next = toFree->nextPtr; |
| 618 |
toFree->fsPtr = NULL; |
| 619 |
ckfree(toFree); |
| 620 |
toFree = next; |
| 621 |
} |
| 622 |
|
| 623 |
/* |
| 624 |
* Make sure the above gets released on thread exit. |
| 625 |
*/ |
| 626 |
|
| 627 |
if (tsdPtr->initialized == 0) { |
| 628 |
Tcl_CreateThreadExitHandler(FsThrExitProc, tsdPtr); |
| 629 |
tsdPtr->initialized = 1; |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
static FilesystemRecord * |
| 634 |
FsGetFirstFilesystem(void) |
| 635 |
{ |
| 636 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&fsDataKey); |
| 637 |
if (tsdPtr->filesystemList == NULL || ((tsdPtr->claims == 0) |
| 638 |
&& (tsdPtr->filesystemEpoch != theFilesystemEpoch))) { |
| 639 |
FsRecacheFilesystemList(); |
| 640 |
} |
| 641 |
return tsdPtr->filesystemList; |
| 642 |
} |
| 643 |
|
| 644 |
/* |
| 645 |
* The epoch can be changed by filesystems being added or removed, by changing |
| 646 |
* the "system encoding" and by env(HOME) changing. |
| 647 |
*/ |
| 648 |
|
| 649 |
int |
| 650 |
TclFSEpochOk( |
| 651 |
size_t filesystemEpoch) |
| 652 |
{ |
| 653 |
return (filesystemEpoch == 0 || filesystemEpoch == theFilesystemEpoch); |
| 654 |
} |
| 655 |
|
| 656 |
static void |
| 657 |
Claim(void) |
| 658 |
{ |
| 659 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&fsDataKey); |
| 660 |
|
| 661 |
tsdPtr->claims++; |
| 662 |
} |
| 663 |
|
| 664 |
static void |
| 665 |
Disclaim(void) |
| 666 |
{ |
| 667 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&fsDataKey); |
| 668 |
|
| 669 |
tsdPtr->claims--; |
| 670 |
} |
| 671 |
|
| 672 |
size_t |
| 673 |
TclFSEpoch(void) |
| 674 |
{ |
| 675 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&fsDataKey); |
| 676 |
|
| 677 |
return tsdPtr->filesystemEpoch; |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
/* |
| 682 |
* If non-NULL, clientData is owned by us and must be freed later. |
| 683 |
*/ |
| 684 |
|
| 685 |
static void |
| 686 |
FsUpdateCwd( |
| 687 |
Tcl_Obj *cwdObj, |
| 688 |
ClientData clientData) |
| 689 |
{ |
| 690 |
int len; |
| 691 |
const char *str = NULL; |
| 692 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&fsDataKey); |
| 693 |
|
| 694 |
if (cwdObj != NULL) { |
| 695 |
str = TclGetStringFromObj(cwdObj, &len); |
| 696 |
} |
| 697 |
|
| 698 |
Tcl_MutexLock(&cwdMutex); |
| 699 |
if (cwdPathPtr != NULL) { |
| 700 |
Tcl_DecrRefCount(cwdPathPtr); |
| 701 |
} |
| 702 |
if (cwdClientData != NULL) { |
| 703 |
NativeFreeInternalRep(cwdClientData); |
| 704 |
} |
| 705 |
|
| 706 |
if (cwdObj == NULL) { |
| 707 |
cwdPathPtr = NULL; |
| 708 |
cwdClientData = NULL; |
| 709 |
} else { |
| 710 |
/* |
| 711 |
* This must be stored as string obj! |
| 712 |
*/ |
| 713 |
|
| 714 |
cwdPathPtr = Tcl_NewStringObj(str, len); |
| 715 |
Tcl_IncrRefCount(cwdPathPtr); |
| 716 |
cwdClientData = TclNativeDupInternalRep(clientData); |
| 717 |
} |
| 718 |
|
| 719 |
if (++cwdPathEpoch == 0) { |
| 720 |
++cwdPathEpoch; |
| 721 |
} |
| 722 |
tsdPtr->cwdPathEpoch = cwdPathEpoch; |
| 723 |
Tcl_MutexUnlock(&cwdMutex); |
| 724 |
|
| 725 |
if (tsdPtr->cwdPathPtr) { |
| 726 |
Tcl_DecrRefCount(tsdPtr->cwdPathPtr); |
| 727 |
} |
| 728 |
if (tsdPtr->cwdClientData) { |
| 729 |
NativeFreeInternalRep(tsdPtr->cwdClientData); |
| 730 |
} |
| 731 |
|
| 732 |
if (cwdObj == NULL) { |
| 733 |
tsdPtr->cwdPathPtr = NULL; |
| 734 |
tsdPtr->cwdClientData = NULL; |
| 735 |
} else { |
| 736 |
tsdPtr->cwdPathPtr = Tcl_NewStringObj(str, len); |
| 737 |
tsdPtr->cwdClientData = clientData; |
| 738 |
Tcl_IncrRefCount(tsdPtr->cwdPathPtr); |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
/* |
| 743 |
*---------------------------------------------------------------------- |
| 744 |
* |
| 745 |
* TclFinalizeFilesystem -- |
| 746 |
* |
| 747 |
* Clean up the filesystem. After this, calls to all Tcl_FS... functions |
| 748 |
* will fail. |
| 749 |
* |
| 750 |
* We will later call TclResetFilesystem to restore the FS to a pristine |
| 751 |
* state. |
| 752 |
* |
| 753 |
* Results: |
| 754 |
* None. |
| 755 |
* |
| 756 |
* Side effects: |
| 757 |
* Frees any memory allocated by the filesystem. |
| 758 |
* |
| 759 |
*---------------------------------------------------------------------- |
| 760 |
*/ |
| 761 |
|
| 762 |
void |
| 763 |
TclFinalizeFilesystem(void) |
| 764 |
{ |
| 765 |
FilesystemRecord *fsRecPtr; |
| 766 |
|
| 767 |
/* |
| 768 |
* Assumption that only one thread is active now. Otherwise we would need |
| 769 |
* to put various mutexes around this code. |
| 770 |
*/ |
| 771 |
|
| 772 |
if (cwdPathPtr != NULL) { |
| 773 |
Tcl_DecrRefCount(cwdPathPtr); |
| 774 |
cwdPathPtr = NULL; |
| 775 |
cwdPathEpoch = 0; |
| 776 |
} |
| 777 |
if (cwdClientData != NULL) { |
| 778 |
NativeFreeInternalRep(cwdClientData); |
| 779 |
cwdClientData = NULL; |
| 780 |
} |
| 781 |
|
| 782 |
/* |
| 783 |
* Remove all filesystems, freeing any allocated memory that is no longer |
| 784 |
* needed. |
| 785 |
*/ |
| 786 |
|
| 787 |
fsRecPtr = filesystemList; |
| 788 |
while (fsRecPtr != NULL) { |
| 789 |
FilesystemRecord *tmpFsRecPtr = fsRecPtr->nextPtr; |
| 790 |
|
| 791 |
/* The native filesystem is static, so we don't free it. */ |
| 792 |
|
| 793 |
if (fsRecPtr != &nativeFilesystemRecord) { |
| 794 |
ckfree(fsRecPtr); |
| 795 |
} |
| 796 |
fsRecPtr = tmpFsRecPtr; |
| 797 |
} |
| 798 |
if (++theFilesystemEpoch == 0) { |
| 799 |
++theFilesystemEpoch; |
| 800 |
} |
| 801 |
filesystemList = NULL; |
| 802 |
|
| 803 |
/* |
| 804 |
* Now filesystemList is NULL. This means that any attempt to use the |
| 805 |
* filesystem is likely to fail. |
| 806 |
*/ |
| 807 |
|
| 808 |
#ifdef _WIN32 |
| 809 |
TclWinEncodingsCleanup(); |
| 810 |
#endif |
| 811 |
} |
| 812 |
|
| 813 |
/* |
| 814 |
*---------------------------------------------------------------------- |
| 815 |
* |
| 816 |
* TclResetFilesystem -- |
| 817 |
* |
| 818 |
* Restore the filesystem to a pristine state. |
| 819 |
* |
| 820 |
* Results: |
| 821 |
* None. |
| 822 |
* |
| 823 |
* Side effects: |
| 824 |
* None. |
| 825 |
* |
| 826 |
*---------------------------------------------------------------------- |
| 827 |
*/ |
| 828 |
|
| 829 |
void |
| 830 |
TclResetFilesystem(void) |
| 831 |
{ |
| 832 |
filesystemList = &nativeFilesystemRecord; |
| 833 |
if (++theFilesystemEpoch == 0) { |
| 834 |
++theFilesystemEpoch; |
| 835 |
} |
| 836 |
|
| 837 |
#ifdef _WIN32 |
| 838 |
/* |
| 839 |
* Cleans up the win32 API filesystem proc lookup table. This must happen |
| 840 |
* very late in finalization so that deleting of copied dlls can occur. |
| 841 |
*/ |
| 842 |
|
| 843 |
TclWinResetInterfaces(); |
| 844 |
#endif |
| 845 |
} |
| 846 |
|
| 847 |
/* |
| 848 |
*---------------------------------------------------------------------- |
| 849 |
* |
| 850 |
* Tcl_FSRegister -- |
| 851 |
* |
| 852 |
* Insert the filesystem function table at the head of the list of |
| 853 |
* functions which are used during calls to all file-system operations. |
| 854 |
* The filesystem will be added even if it is already in the list. (You |
| 855 |
* can use Tcl_FSData to check if it is in the list, provided the |
| 856 |
* ClientData used was not NULL). |
| 857 |
* |
| 858 |
* Note that the filesystem handling is head-to-tail of the list. Each |
| 859 |
* filesystem is asked in turn whether it can handle a particular |
| 860 |
* request, until one of them says 'yes'. At that point no further |
| 861 |
* filesystems are asked. |
| 862 |
* |
| 863 |
* In particular this means if you want to add a diagnostic filesystem |
| 864 |
* (which simply reports all fs activity), it must be at the head of the |
| 865 |
* list: i.e. it must be the last registered. |
| 866 |
* |
| 867 |
* Results: |
| 868 |
* Normally TCL_OK; TCL_ERROR if memory for a new node in the list could |
| 869 |
* not be allocated. |
| 870 |
* |
| 871 |
* Side effects: |
| 872 |
* Memory allocated and modifies the link list for filesystems. |
| 873 |
* |
| 874 |
*---------------------------------------------------------------------- |
| 875 |
*/ |
| 876 |
|
| 877 |
int |
| 878 |
Tcl_FSRegister( |
| 879 |
ClientData clientData, /* Client specific data for this fs. */ |
| 880 |
const Tcl_Filesystem *fsPtr)/* The filesystem record for the new fs. */ |
| 881 |
{ |
| 882 |
FilesystemRecord *newFilesystemPtr; |
| 883 |
|
| 884 |
if (fsPtr == NULL) { |
| 885 |
return TCL_ERROR; |
| 886 |
} |
| 887 |
|
| 888 |
newFilesystemPtr = (FilesystemRecord *)ckalloc(sizeof(FilesystemRecord)); |
| 889 |
|
| 890 |
newFilesystemPtr->clientData = clientData; |
| 891 |
newFilesystemPtr->fsPtr = fsPtr; |
| 892 |
|
| 893 |
/* |
| 894 |
* Is this lock and wait strictly speaking necessary? Since any iterators |
| 895 |
* out there will have grabbed a copy of the head of the list and be |
| 896 |
* iterating away from that, if we add a new element to the head of the |
| 897 |
* list, it can't possibly have any effect on any of their loops. In fact |
| 898 |
* it could be better not to wait, since we are adjusting the filesystem |
| 899 |
* epoch, any cached representations calculated by existing iterators are |
| 900 |
* going to have to be thrown away anyway. |
| 901 |
* |
| 902 |
* However, since registering and unregistering filesystems is a very rare |
| 903 |
* action, this is not a very important point. |
| 904 |
*/ |
| 905 |
|
| 906 |
Tcl_MutexLock(&filesystemMutex); |
| 907 |
|
| 908 |
newFilesystemPtr->nextPtr = filesystemList; |
| 909 |
newFilesystemPtr->prevPtr = NULL; |
| 910 |
if (filesystemList) { |
| 911 |
filesystemList->prevPtr = newFilesystemPtr; |
| 912 |
} |
| 913 |
filesystemList = newFilesystemPtr; |
| 914 |
|
| 915 |
/* |
| 916 |
* Increment the filesystem epoch counter, since existing paths might |
| 917 |
* conceivably now belong to different filesystems. |
| 918 |
*/ |
| 919 |
|
| 920 |
if (++theFilesystemEpoch == 0) { |
| 921 |
++theFilesystemEpoch; |
| 922 |
} |
| 923 |
Tcl_MutexUnlock(&filesystemMutex); |
| 924 |
|
| 925 |
return TCL_OK; |
| 926 |
} |
| 927 |
|
| 928 |
/* |
| 929 |
*---------------------------------------------------------------------- |
| 930 |
* |
| 931 |
* Tcl_FSUnregister -- |
| 932 |
* |
| 933 |
* Remove the passed filesystem from the list of filesystem function |
| 934 |
* tables. It also ensures that the built-in (native) filesystem is not |
| 935 |
* removable, although we may wish to change that decision in the future |
| 936 |
* to allow a smaller Tcl core, in which the native filesystem is not |
| 937 |
* used at all (we could, say, initialise Tcl completely over a network |
| 938 |
* connection). |
| 939 |
* |
| 940 |
* Results: |
| 941 |
* TCL_OK if the function pointer was successfully removed, TCL_ERROR |
| 942 |
* otherwise. |
| 943 |
* |
| 944 |
* Side effects: |
| 945 |
* Memory may be deallocated (or will be later, once no "path" objects |
| 946 |
* refer to this filesystem), but the list of registered filesystems is |
| 947 |
* updated immediately. |
| 948 |
* |
| 949 |
*---------------------------------------------------------------------- |
| 950 |
*/ |
| 951 |
|
| 952 |
int |
| 953 |
Tcl_FSUnregister( |
| 954 |
const Tcl_Filesystem *fsPtr) /* The filesystem record to remove. */ |
| 955 |
{ |
| 956 |
int retVal = TCL_ERROR; |
| 957 |
FilesystemRecord *fsRecPtr; |
| 958 |
|
| 959 |
Tcl_MutexLock(&filesystemMutex); |
| 960 |
|
| 961 |
/* |
| 962 |
* Traverse the 'filesystemList' looking for the particular node whose |
| 963 |
* 'fsPtr' member matches 'fsPtr' and remove that one from the list. |
| 964 |
* Ensure that the "default" node cannot be removed. |
| 965 |
*/ |
| 966 |
|
| 967 |
fsRecPtr = filesystemList; |
| 968 |
while ((retVal == TCL_ERROR) && (fsRecPtr != &nativeFilesystemRecord)) { |
| 969 |
if (fsRecPtr->fsPtr == fsPtr) { |
| 970 |
if (fsRecPtr->prevPtr) { |
| 971 |
fsRecPtr->prevPtr->nextPtr = fsRecPtr->nextPtr; |
| 972 |
} else { |
| 973 |
filesystemList = fsRecPtr->nextPtr; |
| 974 |
} |
| 975 |
if (fsRecPtr->nextPtr) { |
| 976 |
fsRecPtr->nextPtr->prevPtr = fsRecPtr->prevPtr; |
| 977 |
} |
| 978 |
|
| 979 |
/* |
| 980 |
* Increment the filesystem epoch counter, since existing paths |
| 981 |
* might conceivably now belong to different filesystems. This |
| 982 |
* should also ensure that paths which have cached the filesystem |
| 983 |
* which is about to be deleted do not reference that filesystem |
| 984 |
* (which would of course lead to memory exceptions). |
| 985 |
*/ |
| 986 |
|
| 987 |
if (++theFilesystemEpoch == 0) { |
| 988 |
++theFilesystemEpoch; |
| 989 |
} |
| 990 |
|
| 991 |
ckfree(fsRecPtr); |
| 992 |
|
| 993 |
retVal = TCL_OK; |
| 994 |
} else { |
| 995 |
fsRecPtr = fsRecPtr->nextPtr; |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
Tcl_MutexUnlock(&filesystemMutex); |
| 1000 |
return retVal; |
| 1001 |
} |
| 1002 |
|
| 1003 |
/* |
| 1004 |
*---------------------------------------------------------------------- |
| 1005 |
* |
| 1006 |
* Tcl_FSMatchInDirectory -- |
| 1007 |
* |
| 1008 |
* This routine is used by the globbing code to search a directory for |
| 1009 |
* all files which match a given pattern. The appropriate function for |
| 1010 |
* the filesystem to which pathPtr belongs will be called. If pathPtr |
| 1011 |
* does not belong to any filesystem and if it is NULL or the empty |
| 1012 |
* string, then we assume the pattern is to be matched in the current |
| 1013 |
* working directory. To avoid have the Tcl_FSMatchInDirectoryProc for |
| 1014 |
* each filesystem from having to deal with this issue, we create a |
| 1015 |
* pathPtr on the fly (equal to the cwd), and then remove it from the |
| 1016 |
* results returned. This makes filesystems easy to write, since they can |
| 1017 |
* assume the pathPtr passed to them is an ordinary path. In fact this |
| 1018 |
* means we could remove such special case handling from Tcl's native |
| 1019 |
* filesystems. |
| 1020 |
* |
| 1021 |
* If 'pattern' is NULL, then pathPtr is assumed to be a fully specified |
| 1022 |
* path of a single file/directory which must be checked for existence |
| 1023 |
* and correct type. |
| 1024 |
* |
| 1025 |
* Results: |
| 1026 |
* |
| 1027 |
* The return value is a standard Tcl result indicating whether an error |
| 1028 |
* occurred in globbing. Error messages are placed in interp, but good |
| 1029 |
* results are placed in the resultPtr given. |
| 1030 |
* |
| 1031 |
* Recursive searches, e.g. |
| 1032 |
* glob -dir $dir -join * pkgIndex.tcl |
| 1033 |
* which must recurse through each directory matching '*' are handled |
| 1034 |
* internally by Tcl, by passing specific flags in a modified 'types' |
| 1035 |
* parameter. This means the actual filesystem only ever sees patterns |
| 1036 |
* which match in a single directory. |
| 1037 |
* |
| 1038 |
* Side effects: |
| 1039 |
* The interpreter may have an error message inserted into it. |
| 1040 |
* |
| 1041 |
*---------------------------------------------------------------------- |
| 1042 |
*/ |
| 1043 |
|
| 1044 |
int |
| 1045 |
Tcl_FSMatchInDirectory( |
| 1046 |
Tcl_Interp *interp, /* Interpreter to receive error messages, but |
| 1047 |
* may be NULL. */ |
| 1048 |
Tcl_Obj *resultPtr, /* List object to receive results. */ |
| 1049 |
Tcl_Obj *pathPtr, /* Contains path to directory to search. */ |
| 1050 |
const char *pattern, /* Pattern to match against. */ |
| 1051 |
Tcl_GlobTypeData *types) /* Object containing list of acceptable types. |
| 1052 |
* May be NULL. In particular the directory |
| 1053 |
* flag is very important. */ |
| 1054 |
{ |
| 1055 |
const Tcl_Filesystem *fsPtr; |
| 1056 |
Tcl_Obj *cwd, *tmpResultPtr, **elemsPtr; |
| 1057 |
int resLength, i, ret = -1; |
| 1058 |
|
| 1059 |
if (types != NULL && (types->type & TCL_GLOB_TYPE_MOUNT)) { |
| 1060 |
/* |
| 1061 |
* We don't currently allow querying of mounts by external code (a |
| 1062 |
* valuable future step), so since we're the only function that |
| 1063 |
* actually knows about mounts, this means we're being called |
| 1064 |
* recursively by ourself. Return no matches. |
| 1065 |
*/ |
| 1066 |
|
| 1067 |
return TCL_OK; |
| 1068 |
} |
| 1069 |
|
| 1070 |
if (pathPtr != NULL) { |
| 1071 |
fsPtr = Tcl_FSGetFileSystemForPath(pathPtr); |
| 1072 |
} else { |
| 1073 |
fsPtr = NULL; |
| 1074 |
} |
| 1075 |
|
| 1076 |
/* |
| 1077 |
* Check if we've successfully mapped the path to a filesystem within |
| 1078 |
* which to search. |
| 1079 |
*/ |
| 1080 |
|
| 1081 |
if (fsPtr != NULL) { |
| 1082 |
if (fsPtr->matchInDirectoryProc == NULL) { |
| 1083 |
Tcl_SetErrno(ENOENT); |
| 1084 |
return -1; |
| 1085 |
} |
| 1086 |
ret = fsPtr->matchInDirectoryProc(interp, resultPtr, pathPtr, pattern, |
| 1087 |
types); |
| 1088 |
if (ret == TCL_OK && pattern != NULL) { |
| 1089 |
FsAddMountsToGlobResult(resultPtr, pathPtr, pattern, types); |
| 1090 |
} |
| 1091 |
return ret; |
| 1092 |
} |
| 1093 |
|
| 1094 |
/* |
| 1095 |
* If the path isn't empty, we have no idea how to match files in a |
| 1096 |
* directory which belongs to no known filesystem. |
| 1097 |
*/ |
| 1098 |
|
| 1099 |
if (pathPtr != NULL && TclGetString(pathPtr)[0] != '\0') { |
| 1100 |
Tcl_SetErrno(ENOENT); |
| 1101 |
return -1; |
| 1102 |
} |
| 1103 |
|
| 1104 |
/* |
| 1105 |
* We have an empty or NULL path. This is defined to mean we must search |
| 1106 |
* for files within the current 'cwd'. We therefore use that, but then |
| 1107 |
* since the proc we call will return results which include the cwd we |
| 1108 |
* must then trim it off the front of each path in the result. We choose |
| 1109 |
* to deal with this here (in the generic code), since if we don't, every |
| 1110 |
* single filesystem's implementation of Tcl_FSMatchInDirectory will have |
| 1111 |
* to deal with it for us. |
| 1112 |
*/ |
| 1113 |
|
| 1114 |
cwd = Tcl_FSGetCwd(NULL); |
| 1115 |
if (cwd == NULL) { |
| 1116 |
if (interp != NULL) { |
| 1117 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1118 |
"glob couldn't determine the current working directory", |
| 1119 |
-1)); |
| 1120 |
} |
| 1121 |
return TCL_ERROR; |
| 1122 |
} |
| 1123 |
|
| 1124 |
fsPtr = Tcl_FSGetFileSystemForPath(cwd); |
| 1125 |
if (fsPtr != NULL && fsPtr->matchInDirectoryProc != NULL) { |
| 1126 |
TclNewObj(tmpResultPtr); |
| 1127 |
Tcl_IncrRefCount(tmpResultPtr); |
| 1128 |
ret = fsPtr->matchInDirectoryProc(interp, tmpResultPtr, cwd, pattern, |
| 1129 |
types); |
| 1130 |
if (ret == TCL_OK) { |
| 1131 |
FsAddMountsToGlobResult(tmpResultPtr, cwd, pattern, types); |
| 1132 |
|
| 1133 |
/* |
| 1134 |
* Note that we know resultPtr and tmpResultPtr are distinct. |
| 1135 |
*/ |
| 1136 |
|
| 1137 |
ret = TclListObjGetElements(interp, tmpResultPtr, |
| 1138 |
&resLength, &elemsPtr); |
| 1139 |
for (i=0 ; ret==TCL_OK && i<resLength ; i++) { |
| 1140 |
ret = Tcl_ListObjAppendElement(interp, resultPtr, |
| 1141 |
TclFSMakePathRelative(interp, elemsPtr[i], cwd)); |
| 1142 |
} |
| 1143 |
} |
| 1144 |
TclDecrRefCount(tmpResultPtr); |
| 1145 |
} |
| 1146 |
Tcl_DecrRefCount(cwd); |
| 1147 |
return ret; |
| 1148 |
} |
| 1149 |
|
| 1150 |
/* |
| 1151 |
*---------------------------------------------------------------------- |
| 1152 |
* |
| 1153 |
* FsAddMountsToGlobResult -- |
| 1154 |
* |
| 1155 |
* This routine is used by the globbing code to take the results of a |
| 1156 |
* directory listing and add any mounted paths to that listing. This is |
| 1157 |
* required so that simple things like 'glob *' merge mounts and listings |
| 1158 |
* correctly. |
| 1159 |
* |
| 1160 |
* Results: |
| 1161 |
* None. |
| 1162 |
* |
| 1163 |
* Side effects: |
| 1164 |
* Modifies the resultPtr. |
| 1165 |
* |
| 1166 |
*---------------------------------------------------------------------- |
| 1167 |
*/ |
| 1168 |
|
| 1169 |
static void |
| 1170 |
FsAddMountsToGlobResult( |
| 1171 |
Tcl_Obj *resultPtr, /* The current list of matching paths; must |
| 1172 |
* not be shared! */ |
| 1173 |
Tcl_Obj *pathPtr, /* The directory in question. */ |
| 1174 |
const char *pattern, /* Pattern to match against. */ |
| 1175 |
Tcl_GlobTypeData *types) /* Object containing list of acceptable types. |
| 1176 |
* May be NULL. In particular the directory |
| 1177 |
* flag is very important. */ |
| 1178 |
{ |
| 1179 |
int mLength, gLength, i; |
| 1180 |
int dir = (types == NULL || (types->type & TCL_GLOB_TYPE_DIR)); |
| 1181 |
Tcl_Obj *mounts = FsListMounts(pathPtr, pattern); |
| 1182 |
|
| 1183 |
if (mounts == NULL) { |
| 1184 |
return; |
| 1185 |
} |
| 1186 |
|
| 1187 |
if (TclListObjLength(NULL, mounts, &mLength) != TCL_OK || mLength == 0) { |
| 1188 |
goto endOfMounts; |
| 1189 |
} |
| 1190 |
if (TclListObjLength(NULL, resultPtr, &gLength) != TCL_OK) { |
| 1191 |
goto endOfMounts; |
| 1192 |
} |
| 1193 |
for (i=0 ; i<mLength ; i++) { |
| 1194 |
Tcl_Obj *mElt; |
| 1195 |
int j; |
| 1196 |
int found = 0; |
| 1197 |
|
| 1198 |
Tcl_ListObjIndex(NULL, mounts, i, &mElt); |
| 1199 |
|
| 1200 |
for (j=0 ; j<gLength ; j++) { |
| 1201 |
Tcl_Obj *gElt; |
| 1202 |
|
| 1203 |
Tcl_ListObjIndex(NULL, resultPtr, j, &gElt); |
| 1204 |
if (Tcl_FSEqualPaths(mElt, gElt)) { |
| 1205 |
found = 1; |
| 1206 |
if (!dir) { |
| 1207 |
/* |
| 1208 |
* We don't want to list this. |
| 1209 |
*/ |
| 1210 |
|
| 1211 |
Tcl_ListObjReplace(NULL, resultPtr, j, 1, 0, NULL); |
| 1212 |
gLength--; |
| 1213 |
} |
| 1214 |
break; /* Break out of for loop. */ |
| 1215 |
} |
| 1216 |
} |
| 1217 |
if (!found && dir) { |
| 1218 |
Tcl_Obj *norm; |
| 1219 |
int len, mlen; |
| 1220 |
|
| 1221 |
/* |
| 1222 |
* We know mElt is absolute normalized and lies inside pathPtr, so |
| 1223 |
* now we must add to the result the right representation of mElt, |
| 1224 |
* i.e. the representation which is relative to pathPtr. |
| 1225 |
*/ |
| 1226 |
|
| 1227 |
norm = Tcl_FSGetNormalizedPath(NULL, pathPtr); |
| 1228 |
if (norm != NULL) { |
| 1229 |
const char *path, *mount; |
| 1230 |
|
| 1231 |
mount = TclGetStringFromObj(mElt, &mlen); |
| 1232 |
path = TclGetStringFromObj(norm, &len); |
| 1233 |
if (path[len-1] == '/') { |
| 1234 |
/* |
| 1235 |
* Deal with the root of the volume. |
| 1236 |
*/ |
| 1237 |
|
| 1238 |
len--; |
| 1239 |
} |
| 1240 |
len++; /* account for '/' in the mElt [Bug 1602539] */ |
| 1241 |
mElt = TclNewFSPathObj(pathPtr, mount + len, mlen - len); |
| 1242 |
Tcl_ListObjAppendElement(NULL, resultPtr, mElt); |
| 1243 |
} |
| 1244 |
/* |
| 1245 |
* No need to increment gLength, since we don't want to compare |
| 1246 |
* mounts against mounts. |
| 1247 |
*/ |
| 1248 |
} |
| 1249 |
} |
| 1250 |
|
| 1251 |
endOfMounts: |
| 1252 |
Tcl_DecrRefCount(mounts); |
| 1253 |
} |
| 1254 |
|
| 1255 |
/* |
| 1256 |
*---------------------------------------------------------------------- |
| 1257 |
* |
| 1258 |
* Tcl_FSMountsChanged -- |
| 1259 |
* |
| 1260 |
* Notify the filesystem that the available mounted filesystems (or |
| 1261 |
* within any one filesystem type, the number or location of mount |
| 1262 |
* points) have changed. |
| 1263 |
* |
| 1264 |
* Results: |
| 1265 |
* None. |
| 1266 |
* |
| 1267 |
* Side effects: |
| 1268 |
* The global filesystem variable 'theFilesystemEpoch' is incremented. |
| 1269 |
* The effect of this is to make all cached path representations invalid. |
| 1270 |
* Clearly it should only therefore be called when it is really required! |
| 1271 |
* There are a few circumstances when it should be called: |
| 1272 |
* |
| 1273 |
* (1) when a new filesystem is registered or unregistered. Strictly |
| 1274 |
* speaking this is only necessary if the new filesystem accepts file |
| 1275 |
* paths as is (normally the filesystem itself is really a shell which |
| 1276 |
* hasn't yet had any mount points established and so its |
| 1277 |
* 'pathInFilesystem' proc will always fail). However, for safety, Tcl |
| 1278 |
* always calls this for you in these circumstances. |
| 1279 |
* |
| 1280 |
* (2) when additional mount points are established inside any existing |
| 1281 |
* filesystem (except the native fs) |
| 1282 |
* |
| 1283 |
* (3) when any filesystem (except the native fs) changes the list of |
| 1284 |
* available volumes. |
| 1285 |
* |
| 1286 |
* (4) when the mapping from a string representation of a file to a full, |
| 1287 |
* normalized path changes. For example, if 'env(HOME)' is modified, then |
| 1288 |
* any path containing '~' will map to a different filesystem location. |
| 1289 |
* Therefore all such paths need to have their internal representation |
| 1290 |
* invalidated. |
| 1291 |
* |
| 1292 |
* Tcl has no control over (2) and (3), so any registered filesystem must |
| 1293 |
* make sure it calls this function when those situations occur. |
| 1294 |
* |
| 1295 |
* (Note: the reason for the exception in 2,3 for the native filesystem |
| 1296 |
* is that the native filesystem by default claims all unknown files even |
| 1297 |
* if it really doesn't understand them or if they don't exist). |
| 1298 |
* |
| 1299 |
*---------------------------------------------------------------------- |
| 1300 |
*/ |
| 1301 |
|
| 1302 |
void |
| 1303 |
Tcl_FSMountsChanged( |
| 1304 |
const Tcl_Filesystem *fsPtr) |
| 1305 |
{ |
| 1306 |
/* |
| 1307 |
* We currently don't do anything with this parameter. We could in the |
| 1308 |
* future only invalidate files for this filesystem or otherwise take more |
| 1309 |
* advanced action. |
| 1310 |
*/ |
| 1311 |
|
| 1312 |
(void)fsPtr; |
| 1313 |
|
| 1314 |
/* |
| 1315 |
* Increment the filesystem epoch counter, since existing paths might now |
| 1316 |
* belong to different filesystems. |
| 1317 |
*/ |
| 1318 |
|
| 1319 |
Tcl_MutexLock(&filesystemMutex); |
| 1320 |
if (++theFilesystemEpoch == 0) { |
| 1321 |
++theFilesystemEpoch; |
| 1322 |
} |
| 1323 |
Tcl_MutexUnlock(&filesystemMutex); |
| 1324 |
} |
| 1325 |
|
| 1326 |
/* |
| 1327 |
*---------------------------------------------------------------------- |
| 1328 |
* |
| 1329 |
* Tcl_FSData -- |
| 1330 |
* |
| 1331 |
* Retrieve the clientData field for the filesystem given, or NULL if |
| 1332 |
* that filesystem is not registered. |
| 1333 |
* |
| 1334 |
* Results: |
| 1335 |
* A clientData value, or NULL. Note that if the filesystem was |
| 1336 |
* registered with a NULL clientData field, this function will return |
| 1337 |
* that NULL value. |
| 1338 |
* |
| 1339 |
* Side effects: |
| 1340 |
* None. |
| 1341 |
* |
| 1342 |
*---------------------------------------------------------------------- |
| 1343 |
*/ |
| 1344 |
|
| 1345 |
ClientData |
| 1346 |
Tcl_FSData( |
| 1347 |
const Tcl_Filesystem *fsPtr) /* The filesystem record to query. */ |
| 1348 |
{ |
| 1349 |
ClientData retVal = NULL; |
| 1350 |
FilesystemRecord *fsRecPtr = FsGetFirstFilesystem(); |
| 1351 |
|
| 1352 |
/* |
| 1353 |
* Traverse the list of filesystems look for a particular one. If found, |
| 1354 |
* return that filesystem's clientData (originally provided when calling |
| 1355 |
* Tcl_FSRegister). |
| 1356 |
*/ |
| 1357 |
|
| 1358 |
while ((retVal == NULL) && (fsRecPtr != NULL)) { |
| 1359 |
if (fsRecPtr->fsPtr == fsPtr) { |
| 1360 |
retVal = fsRecPtr->clientData; |
| 1361 |
} |
| 1362 |
fsRecPtr = fsRecPtr->nextPtr; |
| 1363 |
} |
| 1364 |
|
| 1365 |
return retVal; |
| 1366 |
} |
| 1367 |
|
| 1368 |
/* |
| 1369 |
*--------------------------------------------------------------------------- |
| 1370 |
* |
| 1371 |
* TclFSNormalizeToUniquePath -- |
| 1372 |
* |
| 1373 |
* Takes a path specification containing no ../, ./ sequences, and |
| 1374 |
* converts it into a unique path for the given platform. On Unix, this |
| 1375 |
* means the path must be free of symbolic links/aliases, and on Windows |
| 1376 |
* it means we want the long form, with that long form's case-dependence |
| 1377 |
* (which gives us a unique, case-dependent path). |
| 1378 |
* |
| 1379 |
* Results: |
| 1380 |
* The pathPtr is modified in place. The return value is the last byte |
| 1381 |
* offset which was recognised in the path string. |
| 1382 |
* |
| 1383 |
* Side effects: |
| 1384 |
* None (beyond the memory allocation for the result). |
| 1385 |
* |
| 1386 |
* Special notes: |
| 1387 |
* If the filesystem-specific normalizePathProcs can re-introduce ../, ./ |
| 1388 |
* sequences into the path, then this function will not return the |
| 1389 |
* correct result. This may be possible with symbolic links on Unix. |
| 1390 |
* |
| 1391 |
* Important assumption: if startAt is non-zero, it must point to a |
| 1392 |
* directory separator that we know exists and is already normalized (so |
| 1393 |
* it is important not to point to the char just after the separator). |
| 1394 |
* |
| 1395 |
*--------------------------------------------------------------------------- |
| 1396 |
*/ |
| 1397 |
|
| 1398 |
int |
| 1399 |
TclFSNormalizeToUniquePath( |
| 1400 |
Tcl_Interp *interp, /* Used for error messages. */ |
| 1401 |
Tcl_Obj *pathPtr, /* The path to normalize in place. */ |
| 1402 |
int startAt) /* Start at this char-offset. */ |
| 1403 |
{ |
| 1404 |
FilesystemRecord *fsRecPtr, *firstFsRecPtr; |
| 1405 |
|
| 1406 |
/* |
| 1407 |
* Call each of the "normalise path" functions in succession. This is a |
| 1408 |
* special case, in which if we have a native filesystem handler, we call |
| 1409 |
* it first. This is because the root of Tcl's filesystem is always a |
| 1410 |
* native filesystem (i.e. '/' on Unix is native). |
| 1411 |
*/ |
| 1412 |
|
| 1413 |
firstFsRecPtr = FsGetFirstFilesystem(); |
| 1414 |
|
| 1415 |
Claim(); |
| 1416 |
for (fsRecPtr=firstFsRecPtr; fsRecPtr!=NULL; fsRecPtr=fsRecPtr->nextPtr) { |
| 1417 |
if (fsRecPtr->fsPtr != &tclNativeFilesystem) { |
| 1418 |
continue; |
| 1419 |
} |
| 1420 |
|
| 1421 |
/* |
| 1422 |
* TODO: Assume that we always find the native file system; it should |
| 1423 |
* always be there... |
| 1424 |
*/ |
| 1425 |
|
| 1426 |
if (fsRecPtr->fsPtr->normalizePathProc != NULL) { |
| 1427 |
startAt = fsRecPtr->fsPtr->normalizePathProc(interp, pathPtr, |
| 1428 |
startAt); |
| 1429 |
} |
| 1430 |
break; |
| 1431 |
} |
| 1432 |
|
| 1433 |
for (fsRecPtr=firstFsRecPtr; fsRecPtr!=NULL; fsRecPtr=fsRecPtr->nextPtr) { |
| 1434 |
/* |
| 1435 |
* Skip the native system next time through. |
| 1436 |
*/ |
| 1437 |
|
| 1438 |
if (fsRecPtr->fsPtr == &tclNativeFilesystem) { |
| 1439 |
continue; |
| 1440 |
} |
| 1441 |
|
| 1442 |
if (fsRecPtr->fsPtr->normalizePathProc != NULL) { |
| 1443 |
startAt = fsRecPtr->fsPtr->normalizePathProc(interp, pathPtr, |
| 1444 |
startAt); |
| 1445 |
} |
| 1446 |
|
| 1447 |
/* |
| 1448 |
* We could add an efficiency check like this: |
| 1449 |
* if (retVal == length-of(pathPtr)) {break;} |
| 1450 |
* but there's not much benefit. |
| 1451 |
*/ |
| 1452 |
} |
| 1453 |
Disclaim(); |
| 1454 |
|
| 1455 |
return startAt; |
| 1456 |
} |
| 1457 |
|
| 1458 |
/* |
| 1459 |
*--------------------------------------------------------------------------- |
| 1460 |
* |
| 1461 |
* TclGetOpenMode -- |
| 1462 |
* |
| 1463 |
* This routine is an obsolete, limited version of TclGetOpenModeEx() |
| 1464 |
* below. It exists only to satisfy any extensions imprudently using it |
| 1465 |
* via Tcl's internal stubs table. |
| 1466 |
* |
| 1467 |
* Results: |
| 1468 |
* Same as TclGetOpenModeEx(). |
| 1469 |
* |
| 1470 |
* Side effects: |
| 1471 |
* Same as TclGetOpenModeEx(). |
| 1472 |
* |
| 1473 |
*--------------------------------------------------------------------------- |
| 1474 |
*/ |
| 1475 |
|
| 1476 |
int |
| 1477 |
TclGetOpenMode( |
| 1478 |
Tcl_Interp *interp, /* Interpreter to use for error reporting - |
| 1479 |
* may be NULL. */ |
| 1480 |
const char *modeString, /* Mode string, e.g. "r+" or "RDONLY CREAT" */ |
| 1481 |
int *seekFlagPtr) /* Set this to 1 if the caller should seek to |
| 1482 |
* EOF during the opening of the file. */ |
| 1483 |
{ |
| 1484 |
int binary = 0; |
| 1485 |
return TclGetOpenModeEx(interp, modeString, seekFlagPtr, &binary); |
| 1486 |
} |
| 1487 |
|
| 1488 |
/* |
| 1489 |
*--------------------------------------------------------------------------- |
| 1490 |
* |
| 1491 |
* TclGetOpenModeEx -- |
| 1492 |
* |
| 1493 |
* Computes a POSIX mode mask for opening a file, from a given string, |
| 1494 |
* and also sets flags to indicate whether the caller should seek to EOF |
| 1495 |
* after opening the file, and whether the caller should configure the |
| 1496 |
* channel for binary data. |
| 1497 |
* |
| 1498 |
* Results: |
| 1499 |
* On success, returns mode to pass to "open". If an error occurs, the |
| 1500 |
* return value is -1 and if interp is not NULL, sets interp's result |
| 1501 |
* object to an error message. |
| 1502 |
* |
| 1503 |
* Side effects: |
| 1504 |
* Sets the integer referenced by seekFlagPtr to 1 to tell the caller to |
| 1505 |
* seek to EOF after opening the file, or to 0 otherwise. Sets the |
| 1506 |
* integer referenced by binaryPtr to 1 to tell the caller to seek to |
| 1507 |
* configure the channel for binary data, or to 0 otherwise. |
| 1508 |
* |
| 1509 |
* Special note: |
| 1510 |
* This code is based on a prototype implementation contributed by Mark |
| 1511 |
* Diekhans. |
| 1512 |
* |
| 1513 |
*--------------------------------------------------------------------------- |
| 1514 |
*/ |
| 1515 |
|
| 1516 |
int |
| 1517 |
TclGetOpenModeEx( |
| 1518 |
Tcl_Interp *interp, /* Interpreter to use for error reporting - |
| 1519 |
* may be NULL. */ |
| 1520 |
const char *modeString, /* Mode string, e.g. "r+" or "RDONLY CREAT" */ |
| 1521 |
int *seekFlagPtr, /* Set this to 1 if the caller should seek to |
| 1522 |
* EOF during the opening of the file. */ |
| 1523 |
int *binaryPtr) /* Set this to 1 if the caller should |
| 1524 |
* configure the opened channel for binary |
| 1525 |
* operations. */ |
| 1526 |
{ |
| 1527 |
int mode, modeArgc, c, i, gotRW; |
| 1528 |
const char **modeArgv, *flag; |
| 1529 |
|
| 1530 |
/* |
| 1531 |
* Check for the simpler fopen-like access modes (e.g. "r"). They are |
| 1532 |
* distinguished from the POSIX access modes by the presence of a |
| 1533 |
* lower-case first letter. |
| 1534 |
*/ |
| 1535 |
|
| 1536 |
*seekFlagPtr = 0; |
| 1537 |
*binaryPtr = 0; |
| 1538 |
mode = O_RDONLY; |
| 1539 |
|
| 1540 |
/* |
| 1541 |
* Guard against international characters before using byte oriented |
| 1542 |
* routines. |
| 1543 |
*/ |
| 1544 |
|
| 1545 |
if (!(modeString[0] & 0x80) |
| 1546 |
&& islower(UCHAR(modeString[0]))) { /* INTL: ISO only. */ |
| 1547 |
switch (modeString[0]) { |
| 1548 |
case 'r': |
| 1549 |
break; |
| 1550 |
case 'w': |
| 1551 |
mode = O_WRONLY|O_CREAT|O_TRUNC; |
| 1552 |
break; |
| 1553 |
case 'a': |
| 1554 |
/* |
| 1555 |
* Added O_APPEND for proper automatic seek-to-end-on-write by the |
| 1556 |
* OS. [Bug 680143] |
| 1557 |
*/ |
| 1558 |
|
| 1559 |
mode = O_WRONLY|O_CREAT|O_APPEND; |
| 1560 |
*seekFlagPtr = 1; |
| 1561 |
break; |
| 1562 |
default: |
| 1563 |
goto error; |
| 1564 |
} |
| 1565 |
i = 1; |
| 1566 |
while (i<3 && modeString[i]) { |
| 1567 |
if (modeString[i] == modeString[i-1]) { |
| 1568 |
goto error; |
| 1569 |
} |
| 1570 |
switch (modeString[i++]) { |
| 1571 |
case '+': |
| 1572 |
/* |
| 1573 |
* Must remove the O_APPEND flag so that the seek command |
| 1574 |
* works. [Bug 1773127] |
| 1575 |
*/ |
| 1576 |
|
| 1577 |
mode = (mode & ~(O_ACCMODE|O_APPEND)) | O_RDWR; |
| 1578 |
break; |
| 1579 |
case 'b': |
| 1580 |
*binaryPtr = 1; |
| 1581 |
break; |
| 1582 |
default: |
| 1583 |
goto error; |
| 1584 |
} |
| 1585 |
} |
| 1586 |
if (modeString[i] != 0) { |
| 1587 |
goto error; |
| 1588 |
} |
| 1589 |
return mode; |
| 1590 |
|
| 1591 |
error: |
| 1592 |
*seekFlagPtr = 0; |
| 1593 |
*binaryPtr = 0; |
| 1594 |
if (interp != NULL) { |
| 1595 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1596 |
"illegal access mode \"%s\"", modeString)); |
| 1597 |
} |
| 1598 |
return -1; |
| 1599 |
} |
| 1600 |
|
| 1601 |
/* |
| 1602 |
* The access modes are specified using a list of POSIX modes such as |
| 1603 |
* O_CREAT. |
| 1604 |
* |
| 1605 |
* IMPORTANT NOTE: We rely on Tcl_SplitList working correctly when a NULL |
| 1606 |
* interpreter is passed in. |
| 1607 |
*/ |
| 1608 |
|
| 1609 |
if (Tcl_SplitList(interp, modeString, &modeArgc, &modeArgv) != TCL_OK) { |
| 1610 |
if (interp != NULL) { |
| 1611 |
Tcl_AddErrorInfo(interp, |
| 1612 |
"\n while processing open access modes \""); |
| 1613 |
Tcl_AddErrorInfo(interp, modeString); |
| 1614 |
Tcl_AddErrorInfo(interp, "\""); |
| 1615 |
} |
| 1616 |
return -1; |
| 1617 |
} |
| 1618 |
|
| 1619 |
gotRW = 0; |
| 1620 |
for (i = 0; i < modeArgc; i++) { |
| 1621 |
flag = modeArgv[i]; |
| 1622 |
c = flag[0]; |
| 1623 |
if ((c == 'R') && (strcmp(flag, "RDONLY") == 0)) { |
| 1624 |
mode = (mode & ~O_ACCMODE) | O_RDONLY; |
| 1625 |
gotRW = 1; |
| 1626 |
} else if ((c == 'W') && (strcmp(flag, "WRONLY") == 0)) { |
| 1627 |
mode = (mode & ~O_ACCMODE) | O_WRONLY; |
| 1628 |
gotRW = 1; |
| 1629 |
} else if ((c == 'R') && (strcmp(flag, "RDWR") == 0)) { |
| 1630 |
mode = (mode & ~O_ACCMODE) | O_RDWR; |
| 1631 |
gotRW = 1; |
| 1632 |
} else if ((c == 'A') && (strcmp(flag, "APPEND") == 0)) { |
| 1633 |
mode |= O_APPEND; |
| 1634 |
*seekFlagPtr = 1; |
| 1635 |
} else if ((c == 'C') && (strcmp(flag, "CREAT") == 0)) { |
| 1636 |
mode |= O_CREAT; |
| 1637 |
} else if ((c == 'E') && (strcmp(flag, "EXCL") == 0)) { |
| 1638 |
mode |= O_EXCL; |
| 1639 |
|
| 1640 |
} else if ((c == 'N') && (strcmp(flag, "NOCTTY") == 0)) { |
| 1641 |
#ifdef O_NOCTTY |
| 1642 |
mode |= O_NOCTTY; |
| 1643 |
#else |
| 1644 |
if (interp != NULL) { |
| 1645 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1646 |
"access mode \"%s\" not supported by this system", |
| 1647 |
flag)); |
| 1648 |
} |
| 1649 |
ckfree(modeArgv); |
| 1650 |
return -1; |
| 1651 |
#endif |
| 1652 |
|
| 1653 |
} else if ((c == 'N') && (strcmp(flag, "NONBLOCK") == 0)) { |
| 1654 |
#ifdef O_NONBLOCK |
| 1655 |
mode |= O_NONBLOCK; |
| 1656 |
#else |
| 1657 |
if (interp != NULL) { |
| 1658 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1659 |
"access mode \"%s\" not supported by this system", |
| 1660 |
flag)); |
| 1661 |
} |
| 1662 |
ckfree(modeArgv); |
| 1663 |
return -1; |
| 1664 |
#endif |
| 1665 |
|
| 1666 |
} else if ((c == 'T') && (strcmp(flag, "TRUNC") == 0)) { |
| 1667 |
mode |= O_TRUNC; |
| 1668 |
} else if ((c == 'B') && (strcmp(flag, "BINARY") == 0)) { |
| 1669 |
*binaryPtr = 1; |
| 1670 |
} else { |
| 1671 |
|
| 1672 |
if (interp != NULL) { |
| 1673 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1674 |
"invalid access mode \"%s\": must be RDONLY, WRONLY, " |
| 1675 |
"RDWR, APPEND, BINARY, CREAT, EXCL, NOCTTY, NONBLOCK," |
| 1676 |
" or TRUNC", flag)); |
| 1677 |
} |
| 1678 |
ckfree(modeArgv); |
| 1679 |
return -1; |
| 1680 |
} |
| 1681 |
} |
| 1682 |
|
| 1683 |
ckfree(modeArgv); |
| 1684 |
|
| 1685 |
if (!gotRW) { |
| 1686 |
if (interp != NULL) { |
| 1687 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1688 |
"access mode must include either RDONLY, WRONLY, or RDWR", |
| 1689 |
-1)); |
| 1690 |
} |
| 1691 |
return -1; |
| 1692 |
} |
| 1693 |
return mode; |
| 1694 |
} |
| 1695 |
|
| 1696 |
/* |
| 1697 |
*---------------------------------------------------------------------- |
| 1698 |
* |
| 1699 |
* Tcl_FSEvalFile, Tcl_FSEvalFileEx, TclNREvalFile -- |
| 1700 |
* |
| 1701 |
* Read in a file and process the entire file as one gigantic Tcl |
| 1702 |
* command. Tcl_FSEvalFile is Tcl_FSEvalFileEx without encoding argument. |
| 1703 |
* TclNREvalFile is an NRE-enabled version of Tcl_FSEvalFileEx. |
| 1704 |
* |
| 1705 |
* Results: |
| 1706 |
* A standard Tcl result, which is either the result of executing the |
| 1707 |
* file or an error indicating why the file couldn't be read. |
| 1708 |
* |
| 1709 |
* Side effects: |
| 1710 |
* Depends on the commands in the file. During the evaluation of the |
| 1711 |
* contents of the file, iPtr->scriptFile is made to point to pathPtr |
| 1712 |
* (the old value is cached and replaced when this function returns). |
| 1713 |
* |
| 1714 |
*---------------------------------------------------------------------- |
| 1715 |
*/ |
| 1716 |
|
| 1717 |
int |
| 1718 |
Tcl_FSEvalFile( |
| 1719 |
Tcl_Interp *interp, /* Interpreter in which to process file. */ |
| 1720 |
Tcl_Obj *pathPtr) /* Path of file to process. Tilde-substitution |
| 1721 |
* will be performed on this name. */ |
| 1722 |
{ |
| 1723 |
return Tcl_FSEvalFileEx(interp, pathPtr, NULL); |
| 1724 |
} |
| 1725 |
|
| 1726 |
int |
| 1727 |
Tcl_FSEvalFileEx( |
| 1728 |
Tcl_Interp *interp, /* Interpreter in which to process file. */ |
| 1729 |
Tcl_Obj *pathPtr, /* Path of file to process. Tilde-substitution |
| 1730 |
* will be performed on this name. */ |
| 1731 |
const char *encodingName) /* If non-NULL, then use this encoding for the |
| 1732 |
* file. NULL means use the system encoding. */ |
| 1733 |
{ |
| 1734 |
int length, result = TCL_ERROR; |
| 1735 |
Tcl_StatBuf statBuf; |
| 1736 |
Tcl_Obj *oldScriptFile; |
| 1737 |
Interp *iPtr; |
| 1738 |
const char *string; |
| 1739 |
Tcl_Channel chan; |
| 1740 |
Tcl_Obj *objPtr; |
| 1741 |
|
| 1742 |
if (Tcl_FSGetNormalizedPath(interp, pathPtr) == NULL) { |
| 1743 |
return result; |
| 1744 |
} |
| 1745 |
|
| 1746 |
if (Tcl_FSStat(pathPtr, &statBuf) == -1) { |
| 1747 |
Tcl_SetErrno(errno); |
| 1748 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1749 |
"couldn't read file \"%s\": %s", |
| 1750 |
TclGetString(pathPtr), Tcl_PosixError(interp))); |
| 1751 |
return result; |
| 1752 |
} |
| 1753 |
chan = Tcl_FSOpenFileChannel(interp, pathPtr, "r", 0644); |
| 1754 |
if (chan == NULL) { |
| 1755 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1756 |
"couldn't read file \"%s\": %s", |
| 1757 |
TclGetString(pathPtr), Tcl_PosixError(interp))); |
| 1758 |
return result; |
| 1759 |
} |
| 1760 |
|
| 1761 |
/* |
| 1762 |
* The eofchar is \32 (^Z). This is the usual on Windows, but we effect |
| 1763 |
* this cross-platform to allow for scripted documents. [Bug: 2040] |
| 1764 |
*/ |
| 1765 |
|
| 1766 |
Tcl_SetChannelOption(interp, chan, "-eofchar", "\32 {}"); |
| 1767 |
|
| 1768 |
/* |
| 1769 |
* If the encoding is specified, set it for the channel. Else don't touch |
| 1770 |
* it (and use the system encoding) Report error on unknown encoding. |
| 1771 |
*/ |
| 1772 |
|
| 1773 |
if (encodingName != NULL) { |
| 1774 |
if (Tcl_SetChannelOption(interp, chan, "-encoding", encodingName) |
| 1775 |
!= TCL_OK) { |
| 1776 |
Tcl_Close(interp,chan); |
| 1777 |
return result; |
| 1778 |
} |
| 1779 |
} |
| 1780 |
|
| 1781 |
TclNewObj(objPtr); |
| 1782 |
Tcl_IncrRefCount(objPtr); |
| 1783 |
|
| 1784 |
/* |
| 1785 |
* Try to read first character of stream, so we can check for utf-8 BOM to |
| 1786 |
* be handled especially. |
| 1787 |
*/ |
| 1788 |
|
| 1789 |
if (Tcl_ReadChars(chan, objPtr, 1, 0) < 0) { |
| 1790 |
Tcl_Close(interp, chan); |
| 1791 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1792 |
"couldn't read file \"%s\": %s", |
| 1793 |
TclGetString(pathPtr), Tcl_PosixError(interp))); |
| 1794 |
goto end; |
| 1795 |
} |
| 1796 |
string = TclGetString(objPtr); |
| 1797 |
|
| 1798 |
/* |
| 1799 |
* If first character is not a BOM, append the remaining characters, |
| 1800 |
* otherwise replace them. [Bug 3466099] |
| 1801 |
*/ |
| 1802 |
|
| 1803 |
if (Tcl_ReadChars(chan, objPtr, -1, |
| 1804 |
memcmp(string, "\xEF\xBB\xBF", 3)) < 0) { |
| 1805 |
Tcl_Close(interp, chan); |
| 1806 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1807 |
"couldn't read file \"%s\": %s", |
| 1808 |
TclGetString(pathPtr), Tcl_PosixError(interp))); |
| 1809 |
goto end; |
| 1810 |
} |
| 1811 |
|
| 1812 |
if (Tcl_Close(interp, chan) != TCL_OK) { |
| 1813 |
goto end; |
| 1814 |
} |
| 1815 |
|
| 1816 |
iPtr = (Interp *) interp; |
| 1817 |
oldScriptFile = iPtr->scriptFile; |
| 1818 |
iPtr->scriptFile = pathPtr; |
| 1819 |
Tcl_IncrRefCount(iPtr->scriptFile); |
| 1820 |
string = TclGetStringFromObj(objPtr, &length); |
| 1821 |
|
| 1822 |
/* |
| 1823 |
* TIP #280 Force the evaluator to open a frame for a sourced file. |
| 1824 |
*/ |
| 1825 |
|
| 1826 |
iPtr->evalFlags |= TCL_EVAL_FILE; |
| 1827 |
result = TclEvalEx(interp, string, length, 0, 1, NULL, string); |
| 1828 |
|
| 1829 |
/* |
| 1830 |
* Now we have to be careful; the script may have changed the |
| 1831 |
* iPtr->scriptFile value, so we must reset it without assuming it still |
| 1832 |
* points to 'pathPtr'. |
| 1833 |
*/ |
| 1834 |
|
| 1835 |
if (iPtr->scriptFile != NULL) { |
| 1836 |
Tcl_DecrRefCount(iPtr->scriptFile); |
| 1837 |
} |
| 1838 |
iPtr->scriptFile = oldScriptFile; |
| 1839 |
|
| 1840 |
if (result == TCL_RETURN) { |
| 1841 |
result = TclUpdateReturnInfo(iPtr); |
| 1842 |
} else if (result == TCL_ERROR) { |
| 1843 |
/* |
| 1844 |
* Record information telling where the error occurred. |
| 1845 |
*/ |
| 1846 |
|
| 1847 |
const char *pathString = TclGetStringFromObj(pathPtr, &length); |
| 1848 |
int limit = 150; |
| 1849 |
int overflow = (length > limit); |
| 1850 |
|
| 1851 |
Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf( |
| 1852 |
"\n (file \"%.*s%s\" line %d)", |
| 1853 |
(overflow ? limit : length), pathString, |
| 1854 |
(overflow ? "..." : ""), Tcl_GetErrorLine(interp))); |
| 1855 |
} |
| 1856 |
|
| 1857 |
end: |
| 1858 |
Tcl_DecrRefCount(objPtr); |
| 1859 |
return result; |
| 1860 |
} |
| 1861 |
|
| 1862 |
int |
| 1863 |
TclNREvalFile( |
| 1864 |
Tcl_Interp *interp, /* Interpreter in which to process file. */ |
| 1865 |
Tcl_Obj *pathPtr, /* Path of file to process. Tilde-substitution |
| 1866 |
* will be performed on this name. */ |
| 1867 |
const char *encodingName) /* If non-NULL, then use this encoding for the |
| 1868 |
* file. NULL means use the system encoding. */ |
| 1869 |
{ |
| 1870 |
Tcl_StatBuf statBuf; |
| 1871 |
Tcl_Obj *oldScriptFile, *objPtr; |
| 1872 |
Interp *iPtr; |
| 1873 |
Tcl_Channel chan; |
| 1874 |
const char *string; |
| 1875 |
|
| 1876 |
if (Tcl_FSGetNormalizedPath(interp, pathPtr) == NULL) { |
| 1877 |
return TCL_ERROR; |
| 1878 |
} |
| 1879 |
|
| 1880 |
if (Tcl_FSStat(pathPtr, &statBuf) == -1) { |
| 1881 |
Tcl_SetErrno(errno); |
| 1882 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1883 |
"couldn't read file \"%s\": %s", |
| 1884 |
TclGetString(pathPtr), Tcl_PosixError(interp))); |
| 1885 |
return TCL_ERROR; |
| 1886 |
} |
| 1887 |
chan = Tcl_FSOpenFileChannel(interp, pathPtr, "r", 0644); |
| 1888 |
if (chan == NULL) { |
| 1889 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1890 |
"couldn't read file \"%s\": %s", |
| 1891 |
TclGetString(pathPtr), Tcl_PosixError(interp))); |
| 1892 |
return TCL_ERROR; |
| 1893 |
} |
| 1894 |
|
| 1895 |
/* |
| 1896 |
* The eofchar is \32 (^Z). This is the usual on Windows, but we effect |
| 1897 |
* this cross-platform to allow for scripted documents. [Bug: 2040] |
| 1898 |
*/ |
| 1899 |
|
| 1900 |
Tcl_SetChannelOption(interp, chan, "-eofchar", "\32 {}"); |
| 1901 |
|
| 1902 |
/* |
| 1903 |
* If the encoding is specified, set it for the channel. Else don't touch |
| 1904 |
* it (and use the system encoding) Report error on unknown encoding. |
| 1905 |
*/ |
| 1906 |
|
| 1907 |
if (encodingName != NULL) { |
| 1908 |
if (Tcl_SetChannelOption(interp, chan, "-encoding", encodingName) |
| 1909 |
!= TCL_OK) { |
| 1910 |
Tcl_Close(interp,chan); |
| 1911 |
return TCL_ERROR; |
| 1912 |
} |
| 1913 |
} |
| 1914 |
|
| 1915 |
TclNewObj(objPtr); |
| 1916 |
Tcl_IncrRefCount(objPtr); |
| 1917 |
|
| 1918 |
/* |
| 1919 |
* Try to read first character of stream, so we can check for utf-8 BOM to |
| 1920 |
* be handled especially. |
| 1921 |
*/ |
| 1922 |
|
| 1923 |
if (Tcl_ReadChars(chan, objPtr, 1, 0) < 0) { |
| 1924 |
Tcl_Close(interp, chan); |
| 1925 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1926 |
"couldn't read file \"%s\": %s", |
| 1927 |
TclGetString(pathPtr), Tcl_PosixError(interp))); |
| 1928 |
Tcl_DecrRefCount(objPtr); |
| 1929 |
return TCL_ERROR; |
| 1930 |
} |
| 1931 |
string = TclGetString(objPtr); |
| 1932 |
|
| 1933 |
/* |
| 1934 |
* If first character is not a BOM, append the remaining characters, |
| 1935 |
* otherwise replace them. [Bug 3466099] |
| 1936 |
*/ |
| 1937 |
|
| 1938 |
if (Tcl_ReadChars(chan, objPtr, -1, |
| 1939 |
memcmp(string, "\xEF\xBB\xBF", 3)) < 0) { |
| 1940 |
Tcl_Close(interp, chan); |
| 1941 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1942 |
"couldn't read file \"%s\": %s", |
| 1943 |
TclGetString(pathPtr), Tcl_PosixError(interp))); |
| 1944 |
Tcl_DecrRefCount(objPtr); |
| 1945 |
return TCL_ERROR; |
| 1946 |
} |
| 1947 |
|
| 1948 |
if (Tcl_Close(interp, chan) != TCL_OK) { |
| 1949 |
Tcl_DecrRefCount(objPtr); |
| 1950 |
return TCL_ERROR; |
| 1951 |
} |
| 1952 |
|
| 1953 |
iPtr = (Interp *) interp; |
| 1954 |
oldScriptFile = iPtr->scriptFile; |
| 1955 |
iPtr->scriptFile = pathPtr; |
| 1956 |
Tcl_IncrRefCount(iPtr->scriptFile); |
| 1957 |
|
| 1958 |
/* |
| 1959 |
* TIP #280: Force the evaluator to open a frame for a sourced file. |
| 1960 |
*/ |
| 1961 |
|
| 1962 |
iPtr->evalFlags |= TCL_EVAL_FILE; |
| 1963 |
TclNRAddCallback(interp, EvalFileCallback, oldScriptFile, pathPtr, objPtr, |
| 1964 |
NULL); |
| 1965 |
return TclNREvalObjEx(interp, objPtr, 0, NULL, INT_MIN); |
| 1966 |
} |
| 1967 |
|
| 1968 |
static int |
| 1969 |
EvalFileCallback( |
| 1970 |
ClientData data[], |
| 1971 |
Tcl_Interp *interp, |
| 1972 |
int result) |
| 1973 |
{ |
| 1974 |
Interp *iPtr = (Interp *) interp; |
| 1975 |
Tcl_Obj *oldScriptFile = (Tcl_Obj *)data[0]; |
| 1976 |
Tcl_Obj *pathPtr = (Tcl_Obj *)data[1]; |
| 1977 |
Tcl_Obj *objPtr = (Tcl_Obj *)data[2]; |
| 1978 |
|
| 1979 |
/* |
| 1980 |
* Now we have to be careful; the script may have changed the |
| 1981 |
* iPtr->scriptFile value, so we must reset it without assuming it still |
| 1982 |
* points to 'pathPtr'. |
| 1983 |
*/ |
| 1984 |
|
| 1985 |
if (iPtr->scriptFile != NULL) { |
| 1986 |
Tcl_DecrRefCount(iPtr->scriptFile); |
| 1987 |
} |
| 1988 |
iPtr->scriptFile = oldScriptFile; |
| 1989 |
|
| 1990 |
if (result == TCL_RETURN) { |
| 1991 |
result = TclUpdateReturnInfo(iPtr); |
| 1992 |
} else if (result == TCL_ERROR) { |
| 1993 |
/* |
| 1994 |
* Record information telling where the error occurred. |
| 1995 |
*/ |
| 1996 |
|
| 1997 |
int length; |
| 1998 |
const char *pathString = TclGetStringFromObj(pathPtr, &length); |
| 1999 |
const int limit = 150; |
| 2000 |
int overflow = (length > limit); |
| 2001 |
|
| 2002 |
Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf( |
| 2003 |
"\n (file \"%.*s%s\" line %d)", |
| 2004 |
(overflow ? limit : length), pathString, |
| 2005 |
(overflow ? "..." : ""), Tcl_GetErrorLine(interp))); |
| 2006 |
} |
| 2007 |
|
| 2008 |
Tcl_DecrRefCount(objPtr); |
| 2009 |
return result; |
| 2010 |
} |
| 2011 |
|
| 2012 |
/* |
| 2013 |
*---------------------------------------------------------------------- |
| 2014 |
* |
| 2015 |
* Tcl_GetErrno -- |
| 2016 |
* |
| 2017 |
* Gets the current value of the Tcl error code variable. This is |
| 2018 |
* currently the global variable "errno" but could in the future change |
| 2019 |
* to something else. |
| 2020 |
* |
| 2021 |
* Results: |
| 2022 |
* The value of the Tcl error code variable. |
| 2023 |
* |
| 2024 |
* Side effects: |
| 2025 |
* None. Note that the value of the Tcl error code variable is UNDEFINED |
| 2026 |
* if a call to Tcl_SetErrno did not precede this call. |
| 2027 |
* |
| 2028 |
*---------------------------------------------------------------------- |
| 2029 |
*/ |
| 2030 |
|
| 2031 |
int |
| 2032 |
Tcl_GetErrno(void) |
| 2033 |
{ |
| 2034 |
/* |
| 2035 |
* On some platforms, errno is really a thread local (implemented by the C |
| 2036 |
* library). |
| 2037 |
*/ |
| 2038 |
|
| 2039 |
return errno; |
| 2040 |
} |
| 2041 |
|
| 2042 |
/* |
| 2043 |
*---------------------------------------------------------------------- |
| 2044 |
* |
| 2045 |
* Tcl_SetErrno -- |
| 2046 |
* |
| 2047 |
* Sets the Tcl error code variable to the supplied value. On some saner |
| 2048 |
* platforms this is actually a thread-local (this is implemented in the |
| 2049 |
* C library) but this is *really* unsafe to assume! |
| 2050 |
* |
| 2051 |
* Results: |
| 2052 |
* None. |
| 2053 |
* |
| 2054 |
* Side effects: |
| 2055 |
* Modifies the value of the Tcl error code variable. |
| 2056 |
* |
| 2057 |
*---------------------------------------------------------------------- |
| 2058 |
*/ |
| 2059 |
|
| 2060 |
void |
| 2061 |
Tcl_SetErrno( |
| 2062 |
int err) /* The new value. */ |
| 2063 |
{ |
| 2064 |
/* |
| 2065 |
* On some platforms, errno is really a thread local (implemented by the C |
| 2066 |
* library). |
| 2067 |
*/ |
| 2068 |
|
| 2069 |
errno = err; |
| 2070 |
} |
| 2071 |
|
| 2072 |
/* |
| 2073 |
*---------------------------------------------------------------------- |
| 2074 |
* |
| 2075 |
* Tcl_PosixError -- |
| 2076 |
* |
| 2077 |
* This function is typically called after UNIX kernel calls return |
| 2078 |
* errors. It stores machine-readable information about the error in |
| 2079 |
* errorCode field of interp and returns an information string for the |
| 2080 |
* caller's use. |
| 2081 |
* |
| 2082 |
* Results: |
| 2083 |
* The return value is a human-readable string describing the error. |
| 2084 |
* |
| 2085 |
* Side effects: |
| 2086 |
* The errorCode field of the interp is set. |
| 2087 |
* |
| 2088 |
*---------------------------------------------------------------------- |
| 2089 |
*/ |
| 2090 |
|
| 2091 |
const char * |
| 2092 |
Tcl_PosixError( |
| 2093 |
Tcl_Interp *interp) /* Interpreter whose errorCode field is to be |
| 2094 |
* set. */ |
| 2095 |
{ |
| 2096 |
const char *id, *msg; |
| 2097 |
|
| 2098 |
msg = Tcl_ErrnoMsg(errno); |
| 2099 |
id = Tcl_ErrnoId(); |
| 2100 |
if (interp) { |
| 2101 |
Tcl_SetErrorCode(interp, "POSIX", id, msg, (char *)NULL); |
| 2102 |
} |
| 2103 |
return msg; |
| 2104 |
} |
| 2105 |
|
| 2106 |
/* |
| 2107 |
*---------------------------------------------------------------------- |
| 2108 |
* |
| 2109 |
* Tcl_FSStat -- |
| 2110 |
* |
| 2111 |
* This function replaces the library version of stat and lstat. |
| 2112 |
* |
| 2113 |
* The appropriate function for the filesystem to which pathPtr belongs |
| 2114 |
* will be called. |
| 2115 |
* |
| 2116 |
* Results: |
| 2117 |
* See stat documentation. |
| 2118 |
* |
| 2119 |
* Side effects: |
| 2120 |
* See stat documentation. |
| 2121 |
* |
| 2122 |
*---------------------------------------------------------------------- |
| 2123 |
*/ |
| 2124 |
|
| 2125 |
int |
| 2126 |
Tcl_FSStat( |
| 2127 |
Tcl_Obj *pathPtr, /* Path of file to stat (in current CP). */ |
| 2128 |
Tcl_StatBuf *buf) /* Filled with results of stat call. */ |
| 2129 |
{ |
| 2130 |
const Tcl_Filesystem *fsPtr = Tcl_FSGetFileSystemForPath(pathPtr); |
| 2131 |
|
| 2132 |
if (fsPtr != NULL && fsPtr->statProc != NULL) { |
| 2133 |
return fsPtr->statProc(pathPtr, buf); |
| 2134 |
} |
| 2135 |
Tcl_SetErrno(ENOENT); |
| 2136 |
return -1; |
| 2137 |
} |
| 2138 |
|
| 2139 |
/* |
| 2140 |
*---------------------------------------------------------------------- |
| 2141 |
* |
| 2142 |
* Tcl_FSLstat -- |
| 2143 |
* |
| 2144 |
* This function replaces the library version of lstat. The appropriate |
| 2145 |
* function for the filesystem to which pathPtr belongs will be called. |
| 2146 |
* If no 'lstat' function is listed, but a 'stat' function is, then Tcl |
| 2147 |
* will fall back on the stat function. |
| 2148 |
* |
| 2149 |
* Results: |
| 2150 |
* See lstat documentation. |
| 2151 |
* |
| 2152 |
* Side effects: |
| 2153 |
* See lstat documentation. |
| 2154 |
* |
| 2155 |
*---------------------------------------------------------------------- |
| 2156 |
*/ |
| 2157 |
|
| 2158 |
int |
| 2159 |
Tcl_FSLstat( |
| 2160 |
Tcl_Obj *pathPtr, /* Path of file to stat (in current CP). */ |
| 2161 |
Tcl_StatBuf *buf) /* Filled with results of stat call. */ |
| 2162 |
{ |
| 2163 |
const Tcl_Filesystem *fsPtr = Tcl_FSGetFileSystemForPath(pathPtr); |
| 2164 |
|
| 2165 |
if (fsPtr != NULL) { |
| 2166 |
if (fsPtr->lstatProc != NULL) { |
| 2167 |
return fsPtr->lstatProc(pathPtr, buf); |
| 2168 |
} |
| 2169 |
if (fsPtr->statProc != NULL) { |
| 2170 |
return fsPtr->statProc(pathPtr, buf); |
| 2171 |
} |
| 2172 |
} |
| 2173 |
Tcl_SetErrno(ENOENT); |
| 2174 |
return -1; |
| 2175 |
} |
| 2176 |
|
| 2177 |
/* |
| 2178 |
*---------------------------------------------------------------------- |
| 2179 |
* |
| 2180 |
* Tcl_FSAccess -- |
| 2181 |
* |
| 2182 |
* This function replaces the library version of access. The appropriate |
| 2183 |
* function for the filesystem to which pathPtr belongs will be called. |
| 2184 |
* |
| 2185 |
* Results: |
| 2186 |
* See access documentation. |
| 2187 |
* |
| 2188 |
* Side effects: |
| 2189 |
* See access documentation. |
| 2190 |
* |
| 2191 |
*---------------------------------------------------------------------- |
| 2192 |
*/ |
| 2193 |
|
| 2194 |
int |
| 2195 |
Tcl_FSAccess( |
| 2196 |
Tcl_Obj *pathPtr, /* Path of file to access (in current CP). */ |
| 2197 |
int mode) /* Permission setting. */ |
| 2198 |
{ |
| 2199 |
const Tcl_Filesystem *fsPtr = Tcl_FSGetFileSystemForPath(pathPtr); |
| 2200 |
|
| 2201 |
if (fsPtr != NULL && fsPtr->accessProc != NULL) { |
| 2202 |
return fsPtr->accessProc(pathPtr, mode); |
| 2203 |
} |
| 2204 |
Tcl_SetErrno(ENOENT); |
| 2205 |
return -1; |
| 2206 |
} |
| 2207 |
|
| 2208 |
/* |
| 2209 |
*---------------------------------------------------------------------- |
| 2210 |
* |
| 2211 |
* Tcl_FSOpenFileChannel -- |
| 2212 |
* |
| 2213 |
* The appropriate function for the filesystem to which pathPtr belongs |
| 2214 |
* will be called. |
| 2215 |
* |
| 2216 |
* Results: |
| 2217 |
* The new channel or NULL, if the named file could not be opened. |
| 2218 |
* |
| 2219 |
* Side effects: |
| 2220 |
* May open the channel and may cause creation of a file on the file |
| 2221 |
* system. |
| 2222 |
* |
| 2223 |
*---------------------------------------------------------------------- |
| 2224 |
*/ |
| 2225 |
|
| 2226 |
Tcl_Channel |
| 2227 |
Tcl_FSOpenFileChannel( |
| 2228 |
Tcl_Interp *interp, /* Interpreter for error reporting; can be |
| 2229 |
* NULL. */ |
| 2230 |
Tcl_Obj *pathPtr, /* Name of file to open. */ |
| 2231 |
const char *modeString, /* A list of POSIX open modes or a string such |
| 2232 |
* as "rw". */ |
| 2233 |
int permissions) /* If the open involves creating a file, with |
| 2234 |
* what modes to create it? */ |
| 2235 |
{ |
| 2236 |
const Tcl_Filesystem *fsPtr; |
| 2237 |
Tcl_Channel retVal = NULL; |
| 2238 |
|
| 2239 |
fsPtr = Tcl_FSGetFileSystemForPath(pathPtr); |
| 2240 |
if (fsPtr != NULL && fsPtr->openFileChannelProc != NULL) { |
| 2241 |
int mode, seekFlag, binary; |
| 2242 |
|
| 2243 |
/* |
| 2244 |
* Parse the mode, picking up whether we want to seek to start with |
| 2245 |
* and/or set the channel automatically into binary mode. |
| 2246 |
*/ |
| 2247 |
|
| 2248 |
mode = TclGetOpenModeEx(interp, modeString, &seekFlag, &binary); |
| 2249 |
if (mode == -1) { |
| 2250 |
return NULL; |
| 2251 |
} |
| 2252 |
|
| 2253 |
/* |
| 2254 |
* Do the actual open() call. |
| 2255 |
*/ |
| 2256 |
|
| 2257 |
retVal = fsPtr->openFileChannelProc(interp, pathPtr, mode, |
| 2258 |
permissions); |
| 2259 |
if (retVal == NULL) { |
| 2260 |
return NULL; |
| 2261 |
} |
| 2262 |
|
| 2263 |
/* |
| 2264 |
* Apply appropriate flags parsed out above. |
| 2265 |
*/ |
| 2266 |
|
| 2267 |
if (seekFlag && (Tcl_Seek(retVal, 0, SEEK_END) < 0)) { |
| 2268 |
if (interp != NULL) { |
| 2269 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 2270 |
"could not seek to end of file while opening \"%s\": %s", |
| 2271 |
TclGetString(pathPtr), Tcl_PosixError(interp))); |
| 2272 |
} |
| 2273 |
Tcl_Close(NULL, retVal); |
| 2274 |
return NULL; |
| 2275 |
} |
| 2276 |
if (binary) { |
| 2277 |
Tcl_SetChannelOption(interp, retVal, "-translation", "binary"); |
| 2278 |
} |
| 2279 |
return retVal; |
| 2280 |
} |
| 2281 |
|
| 2282 |
/* |
| 2283 |
* File doesn't belong to any filesystem that can open it. |
| 2284 |
*/ |
| 2285 |
|
| 2286 |
Tcl_SetErrno(ENOENT); |
| 2287 |
if (interp != NULL) { |
| 2288 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 2289 |
"couldn't open \"%s\": %s", |
| 2290 |
TclGetString(pathPtr), Tcl_PosixError(interp))); |
| 2291 |
} |
| 2292 |
return NULL; |
| 2293 |
} |
| 2294 |
|
| 2295 |
/* |
| 2296 |
*---------------------------------------------------------------------- |
| 2297 |
* |
| 2298 |
* Tcl_FSUtime -- |
| 2299 |
* |
| 2300 |
* This function replaces the library version of utime. The appropriate |
| 2301 |
* function for the filesystem to which pathPtr belongs will be called. |
| 2302 |
* |
| 2303 |
* Results: |
| 2304 |
* See utime documentation. |
| 2305 |
* |
| 2306 |
* Side effects: |
| 2307 |
* See utime documentation. |
| 2308 |
* |
| 2309 |
*---------------------------------------------------------------------- |
| 2310 |
*/ |
| 2311 |
|
| 2312 |
int |
| 2313 |
Tcl_FSUtime( |
| 2314 |
Tcl_Obj *pathPtr, /* File to change access/modification |
| 2315 |
* times. */ |
| 2316 |
struct utimbuf *tval) /* Structure containing access/modification |
| 2317 |
* times to use. Should not be modified. */ |
| 2318 |
{ |
| 2319 |
const Tcl_Filesystem *fsPtr = Tcl_FSGetFileSystemForPath(pathPtr); |
| 2320 |
|
| 2321 |
if (fsPtr != NULL && fsPtr->utimeProc != NULL) { |
| 2322 |
return fsPtr->utimeProc(pathPtr, tval); |
| 2323 |
} |
| 2324 |
/* TODO: set errno here? Tcl_SetErrno(ENOENT); */ |
| 2325 |
return -1; |
| 2326 |
} |
| 2327 |
|
| 2328 |
/* |
| 2329 |
*---------------------------------------------------------------------- |
| 2330 |
* |
| 2331 |
* NativeFileAttrStrings -- |
| 2332 |
* |
| 2333 |
* This function implements the platform dependent 'file attributes' |
| 2334 |
* subcommand, for the native filesystem, for listing the set of possible |
| 2335 |
* attribute strings. This function is part of Tcl's native filesystem |
| 2336 |
* support, and is placed here because it is shared by Unix and Windows |
| 2337 |
* code. |
| 2338 |
* |
| 2339 |
* Results: |
| 2340 |
* An array of strings |
| 2341 |
* |
| 2342 |
* Side effects: |
| 2343 |
* None. |
| 2344 |
* |
| 2345 |
*---------------------------------------------------------------------- |
| 2346 |
*/ |
| 2347 |
|
| 2348 |
static const char *const * |
| 2349 |
NativeFileAttrStrings( |
| 2350 |
Tcl_Obj *pathPtr, |
| 2351 |
Tcl_Obj **objPtrRef) |
| 2352 |
{ |
| 2353 |
return tclpFileAttrStrings; |
| 2354 |
} |
| 2355 |
|
| 2356 |
/* |
| 2357 |
*---------------------------------------------------------------------- |
| 2358 |
* |
| 2359 |
* NativeFileAttrsGet -- |
| 2360 |
* |
| 2361 |
* This function implements the platform dependent 'file attributes' |
| 2362 |
* subcommand, for the native filesystem, for 'get' operations. This |
| 2363 |
* function is part of Tcl's native filesystem support, and is placed |
| 2364 |
* here because it is shared by Unix and Windows code. |
| 2365 |
* |
| 2366 |
* Results: |
| 2367 |
* Standard Tcl return code. The object placed in objPtrRef (if TCL_OK |
| 2368 |
* was returned) is likely to have a refCount of zero. Either way we must |
| 2369 |
* either store it somewhere (e.g. the Tcl result), or Incr/Decr its |
| 2370 |
* refCount to ensure it is properly freed. |
| 2371 |
* |
| 2372 |
* Side effects: |
| 2373 |
* None. |
| 2374 |
* |
| 2375 |
*---------------------------------------------------------------------- |
| 2376 |
*/ |
| 2377 |
|
| 2378 |
static int |
| 2379 |
NativeFileAttrsGet( |
| 2380 |
Tcl_Interp *interp, /* The interpreter for error reporting. */ |
| 2381 |
int index, /* index of the attribute command. */ |
| 2382 |
Tcl_Obj *pathPtr, /* path of file we are operating on. */ |
| 2383 |
Tcl_Obj **objPtrRef) /* for output. */ |
| 2384 |
{ |
| 2385 |
return tclpFileAttrProcs[index].getProc(interp, index, pathPtr,objPtrRef); |
| 2386 |
} |
| 2387 |
|
| 2388 |
/* |
| 2389 |
*---------------------------------------------------------------------- |
| 2390 |
* |
| 2391 |
* NativeFileAttrsSet -- |
| 2392 |
* |
| 2393 |
* This function implements the platform dependent 'file attributes' |
| 2394 |
* subcommand, for the native filesystem, for 'set' operations. This |
| 2395 |
* function is part of Tcl's native filesystem support, and is placed |
| 2396 |
* here because it is shared by Unix and Windows code. |
| 2397 |
* |
| 2398 |
* Results: |
| 2399 |
* Standard Tcl return code. |
| 2400 |
* |
| 2401 |
* Side effects: |
| 2402 |
* None. |
| 2403 |
* |
| 2404 |
*---------------------------------------------------------------------- |
| 2405 |
*/ |
| 2406 |
|
| 2407 |
static int |
| 2408 |
NativeFileAttrsSet( |
| 2409 |
Tcl_Interp *interp, /* The interpreter for error reporting. */ |
| 2410 |
int index, /* index of the attribute command. */ |
| 2411 |
Tcl_Obj *pathPtr, /* path of file we are operating on. */ |
| 2412 |
Tcl_Obj *objPtr) /* set to this value. */ |
| 2413 |
{ |
| 2414 |
return tclpFileAttrProcs[index].setProc(interp, index, pathPtr, objPtr); |
| 2415 |
} |
| 2416 |
|
| 2417 |
/* |
| 2418 |
*---------------------------------------------------------------------- |
| 2419 |
* |
| 2420 |
* Tcl_FSFileAttrStrings -- |
| 2421 |
* |
| 2422 |
* This function implements part of the hookable 'file attributes' |
| 2423 |
* subcommand. The appropriate function for the filesystem to which |
| 2424 |
* pathPtr belongs will be called. |
| 2425 |
* |
| 2426 |
* Results: |
| 2427 |
* The called function may either return an array of strings, or may |
| 2428 |
* instead return NULL and place a Tcl list into the given objPtrRef. |
| 2429 |
* Tcl will take that list and first increment its refCount before using |
| 2430 |
* it. On completion of that use, Tcl will decrement its refCount. Hence |
| 2431 |
* if the list should be disposed of by Tcl when done, it should have a |
| 2432 |
* refCount of zero, and if the list should not be disposed of, the |
| 2433 |
* filesystem should ensure it retains a refCount on the object. |
| 2434 |
* |
| 2435 |
* Side effects: |
| 2436 |
* None. |
| 2437 |
* |
| 2438 |
*---------------------------------------------------------------------- |
| 2439 |
*/ |
| 2440 |
|
| 2441 |
const char *const * |
| 2442 |
Tcl_FSFileAttrStrings( |
| 2443 |
Tcl_Obj *pathPtr, |
| 2444 |
Tcl_Obj **objPtrRef) |
| 2445 |
{ |
| 2446 |
const Tcl_Filesystem *fsPtr = Tcl_FSGetFileSystemForPath(pathPtr); |
| 2447 |
|
| 2448 |
if (fsPtr != NULL && fsPtr->fileAttrStringsProc != NULL) { |
| 2449 |
return fsPtr->fileAttrStringsProc(pathPtr, objPtrRef); |
| 2450 |
} |
| 2451 |
Tcl_SetErrno(ENOENT); |
| 2452 |
return NULL; |
| 2453 |
} |
| 2454 |
|
| 2455 |
/* |
| 2456 |
*---------------------------------------------------------------------- |
| 2457 |
* |
| 2458 |
* TclFSFileAttrIndex -- |
| 2459 |
* |
| 2460 |
* Helper function for converting an attribute name to an index into the |
| 2461 |
* attribute table. |
| 2462 |
* |
| 2463 |
* Results: |
| 2464 |
* Tcl result code, index written to *indexPtr on result==TCL_OK |
| 2465 |
* |
| 2466 |
* Side effects: |
| 2467 |
* None. |
| 2468 |
* |
| 2469 |
*---------------------------------------------------------------------- |
| 2470 |
*/ |
| 2471 |
|
| 2472 |
int |
| 2473 |
TclFSFileAttrIndex( |
| 2474 |
Tcl_Obj *pathPtr, /* File whose attributes are to be indexed |
| 2475 |
* into. */ |
| 2476 |
const char *attributeName, /* The attribute being looked for. */ |
| 2477 |
int *indexPtr) /* Where to write the found index. */ |
| 2478 |
{ |
| 2479 |
Tcl_Obj *listObj = NULL; |
| 2480 |
const char *const *attrTable; |
| 2481 |
|
| 2482 |
/* |
| 2483 |
* Get the attribute table for the file. |
| 2484 |
*/ |
| 2485 |
|
| 2486 |
attrTable = Tcl_FSFileAttrStrings(pathPtr, &listObj); |
| 2487 |
if (listObj != NULL) { |
| 2488 |
Tcl_IncrRefCount(listObj); |
| 2489 |
} |
| 2490 |
|
| 2491 |
if (attrTable != NULL) { |
| 2492 |
/* |
| 2493 |
* It's a constant attribute table, so use T_GIFO. |
| 2494 |
*/ |
| 2495 |
|
| 2496 |
Tcl_Obj *tmpObj = Tcl_NewStringObj(attributeName, -1); |
| 2497 |
int result; |
| 2498 |
|
| 2499 |
result = Tcl_GetIndexFromObj(NULL, tmpObj, attrTable, NULL, TCL_EXACT, |
| 2500 |
indexPtr); |
| 2501 |
TclDecrRefCount(tmpObj); |
| 2502 |
if (listObj != NULL) { |
| 2503 |
TclDecrRefCount(listObj); |
| 2504 |
} |
| 2505 |
return result; |
| 2506 |
} else if (listObj != NULL) { |
| 2507 |
/* |
| 2508 |
* It's a non-constant attribute list, so do a literal search. |
| 2509 |
*/ |
| 2510 |
|
| 2511 |
int i, objc; |
| 2512 |
Tcl_Obj **objv; |
| 2513 |
|
| 2514 |
if (TclListObjGetElements(NULL, listObj, &objc, &objv) != TCL_OK) { |
| 2515 |
TclDecrRefCount(listObj); |
| 2516 |
return TCL_ERROR; |
| 2517 |
} |
| 2518 |
for (i=0 ; i<objc ; i++) { |
| 2519 |
if (!strcmp(attributeName, TclGetString(objv[i]))) { |
| 2520 |
TclDecrRefCount(listObj); |
| 2521 |
*indexPtr = i; |
| 2522 |
return TCL_OK; |
| 2523 |
} |
| 2524 |
} |
| 2525 |
TclDecrRefCount(listObj); |
| 2526 |
return TCL_ERROR; |
| 2527 |
} else { |
| 2528 |
return TCL_ERROR; |
| 2529 |
} |
| 2530 |
} |
| 2531 |
|
| 2532 |
/* |
| 2533 |
*---------------------------------------------------------------------- |
| 2534 |
* |
| 2535 |
* Tcl_FSFileAttrsGet -- |
| 2536 |
* |
| 2537 |
* This function implements read access for the hookable 'file |
| 2538 |
* attributes' subcommand. The appropriate function for the filesystem to |
| 2539 |
* which pathPtr belongs will be called. |
| 2540 |
* |
| 2541 |
* Results: |
| 2542 |
* Standard Tcl return code. The object placed in objPtrRef (if TCL_OK |
| 2543 |
* was returned) is likely to have a refCount of zero. Either way we must |
| 2544 |
* either store it somewhere (e.g. the Tcl result), or Incr/Decr its |
| 2545 |
* refCount to ensure it is properly freed. |
| 2546 |
* |
| 2547 |
* Side effects: |
| 2548 |
* None. |
| 2549 |
* |
| 2550 |
*---------------------------------------------------------------------- |
| 2551 |
*/ |
| 2552 |
|
| 2553 |
int |
| 2554 |
Tcl_FSFileAttrsGet( |
| 2555 |
Tcl_Interp *interp, /* The interpreter for error reporting. */ |
| 2556 |
int index, /* index of the attribute command. */ |
| 2557 |
Tcl_Obj *pathPtr, /* filename we are operating on. */ |
| 2558 |
Tcl_Obj **objPtrRef) /* for output. */ |
| 2559 |
{ |
| 2560 |
const Tcl_Filesystem *fsPtr = Tcl_FSGetFileSystemForPath(pathPtr); |
| 2561 |
|
| 2562 |
if (fsPtr != NULL && fsPtr->fileAttrsGetProc != NULL) { |
| 2563 |
return fsPtr->fileAttrsGetProc(interp, index, pathPtr, objPtrRef); |
| 2564 |
} |
| 2565 |
Tcl_SetErrno(ENOENT); |
| 2566 |
return -1; |
| 2567 |
} |
| 2568 |
|
| 2569 |
/* |
| 2570 |
*---------------------------------------------------------------------- |
| 2571 |
* |
| 2572 |
* Tcl_FSFileAttrsSet -- |
| 2573 |
* |
| 2574 |
* This function implements write access for the hookable 'file |
| 2575 |
* attributes' subcommand. The appropriate function for the filesystem to |
| 2576 |
* which pathPtr belongs will be called. |
| 2577 |
* |
| 2578 |
* Results: |
| 2579 |
* Standard Tcl return code. |
| 2580 |
* |
| 2581 |
* Side effects: |
| 2582 |
* None. |
| 2583 |
* |
| 2584 |
*---------------------------------------------------------------------- |
| 2585 |
*/ |
| 2586 |
|
| 2587 |
int |
| 2588 |
Tcl_FSFileAttrsSet( |
| 2589 |
Tcl_Interp *interp, /* The interpreter for error reporting. */ |
| 2590 |
int index, /* index of the attribute command. */ |
| 2591 |
Tcl_Obj *pathPtr, /* filename we are operating on. */ |
| 2592 |
Tcl_Obj *objPtr) /* Input value. */ |
| 2593 |
{ |
| 2594 |
const Tcl_Filesystem *fsPtr = Tcl_FSGetFileSystemForPath(pathPtr); |
| 2595 |
|
| 2596 |
if (fsPtr != NULL && fsPtr->fileAttrsSetProc != NULL) { |
| 2597 |
return fsPtr->fileAttrsSetProc(interp, index, pathPtr, objPtr); |
| 2598 |
} |
| 2599 |
Tcl_SetErrno(ENOENT); |
| 2600 |
return -1; |
| 2601 |
} |
| 2602 |
|
| 2603 |
/* |
| 2604 |
*---------------------------------------------------------------------- |
| 2605 |
* |
| 2606 |
* Tcl_FSGetCwd -- |
| 2607 |
* |
| 2608 |
* This function replaces the library version of getcwd(). |
| 2609 |
* |
| 2610 |
* Most VFS's will *not* implement a 'cwdProc'. Tcl now maintains its own |
| 2611 |
* record (in a Tcl_Obj) of the cwd, and an attempt is made to synch this |
| 2612 |
* with the cwd's containing filesystem, if that filesystem provides a |
| 2613 |
* cwdProc (e.g. the native filesystem). |
| 2614 |
* |
| 2615 |
* Note that if Tcl's cwd is not in the native filesystem, then of course |
| 2616 |
* Tcl's cwd and the native cwd are different: extensions should |
| 2617 |
* therefore ensure they only access the cwd through this function to |
| 2618 |
* avoid confusion. |
| 2619 |
* |
| 2620 |
* If a global cwdPathPtr already exists, it is cached in the thread's |
| 2621 |
* private data structures and reference to the cached copy is returned, |
| 2622 |
* subject to a synchronisation attempt in that cwdPathPtr's fs. |
| 2623 |
* |
| 2624 |
* Otherwise, the chain of functions that have been "inserted" into the |
| 2625 |
* filesystem will be called in succession until either a value other |
| 2626 |
* than NULL is returned, or the entire list is visited. |
| 2627 |
* |
| 2628 |
* Results: |
| 2629 |
* The result is a pointer to a Tcl_Obj specifying the current directory, |
| 2630 |
* or NULL if the current directory could not be determined. If NULL is |
| 2631 |
* returned, an error message is left in the interp's result. |
| 2632 |
* |
| 2633 |
* The result already has its refCount incremented for the caller. When |
| 2634 |
* it is no longer needed, that refCount should be decremented. |
| 2635 |
* |
| 2636 |
* Side effects: |
| 2637 |
* Various objects may be freed and allocated. |
| 2638 |
* |
| 2639 |
*---------------------------------------------------------------------- |
| 2640 |
*/ |
| 2641 |
|
| 2642 |
Tcl_Obj * |
| 2643 |
Tcl_FSGetCwd( |
| 2644 |
Tcl_Interp *interp) |
| 2645 |
{ |
| 2646 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&fsDataKey); |
| 2647 |
|
| 2648 |
if (TclFSCwdPointerEquals(NULL)) { |
| 2649 |
FilesystemRecord *fsRecPtr; |
| 2650 |
Tcl_Obj *retVal = NULL; |
| 2651 |
|
| 2652 |
/* |
| 2653 |
* We've never been called before, try to find a cwd. Call each of the |
| 2654 |
* "Tcl_GetCwd" function in succession. A non-NULL return value |
| 2655 |
* indicates the particular function has succeeded. |
| 2656 |
*/ |
| 2657 |
|
| 2658 |
fsRecPtr = FsGetFirstFilesystem(); |
| 2659 |
Claim(); |
| 2660 |
for (; (retVal == NULL) && (fsRecPtr != NULL); |
| 2661 |
fsRecPtr = fsRecPtr->nextPtr) { |
| 2662 |
ClientData retCd; |
| 2663 |
TclFSGetCwdProc2 *proc2; |
| 2664 |
if (fsRecPtr->fsPtr->getCwdProc == NULL) { |
| 2665 |
continue; |
| 2666 |
} |
| 2667 |
|
| 2668 |
if (fsRecPtr->fsPtr->version == TCL_FILESYSTEM_VERSION_1) { |
| 2669 |
retVal = fsRecPtr->fsPtr->getCwdProc(interp); |
| 2670 |
continue; |
| 2671 |
} |
| 2672 |
|
| 2673 |
proc2 = (TclFSGetCwdProc2 *) fsRecPtr->fsPtr->getCwdProc; |
| 2674 |
retCd = proc2(NULL); |
| 2675 |
if (retCd != NULL) { |
| 2676 |
Tcl_Obj *norm; |
| 2677 |
|
| 2678 |
/* |
| 2679 |
* Looks like a new current directory. |
| 2680 |
*/ |
| 2681 |
|
| 2682 |
retVal = fsRecPtr->fsPtr->internalToNormalizedProc(retCd); |
| 2683 |
Tcl_IncrRefCount(retVal); |
| 2684 |
norm = TclFSNormalizeAbsolutePath(interp,retVal); |
| 2685 |
if (norm != NULL) { |
| 2686 |
/* |
| 2687 |
* We found a cwd, which is now in our global storage. We |
| 2688 |
* must make a copy. Norm already has a refCount of 1. |
| 2689 |
* |
| 2690 |
* Threading issue: note that multiple threads at system |
| 2691 |
* startup could in principle call this function |
| 2692 |
* simultaneously. They will therefore each set the |
| 2693 |
* cwdPathPtr independently. That behaviour is a bit |
| 2694 |
* peculiar, but should be fine. Once we have a cwd, we'll |
| 2695 |
* always be in the 'else' branch below which is simpler. |
| 2696 |
*/ |
| 2697 |
|
| 2698 |
FsUpdateCwd(norm, retCd); |
| 2699 |
Tcl_DecrRefCount(norm); |
| 2700 |
} else { |
| 2701 |
fsRecPtr->fsPtr->freeInternalRepProc(retCd); |
| 2702 |
} |
| 2703 |
Tcl_DecrRefCount(retVal); |
| 2704 |
retVal = NULL; |
| 2705 |
Disclaim(); |
| 2706 |
goto cdDidNotChange; |
| 2707 |
} else if (interp != NULL) { |
| 2708 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 2709 |
"error getting working directory name: %s", |
| 2710 |
Tcl_PosixError(interp))); |
| 2711 |
} |
| 2712 |
} |
| 2713 |
Disclaim(); |
| 2714 |
|
| 2715 |
/* |
| 2716 |
* Now the 'cwd' may NOT be normalized, at least on some platforms. |
| 2717 |
* For the sake of efficiency, we want a completely normalized cwd at |
| 2718 |
* all times. |
| 2719 |
* |
| 2720 |
* Finally, if retVal is NULL, we do not have a cwd, which could be |
| 2721 |
* problematic. |
| 2722 |
*/ |
| 2723 |
|
| 2724 |
if (retVal != NULL) { |
| 2725 |
Tcl_Obj *norm = TclFSNormalizeAbsolutePath(interp, retVal); |
| 2726 |
|
| 2727 |
if (norm != NULL) { |
| 2728 |
/* |
| 2729 |
* We found a cwd, which is now in our global storage. We must |
| 2730 |
* make a copy. Norm already has a refCount of 1. |
| 2731 |
* |
| 2732 |
* Threading issue: note that multiple threads at system |
| 2733 |
* startup could in principle call this function |
| 2734 |
* simultaneously. They will therefore each set the cwdPathPtr |
| 2735 |
* independently. That behaviour is a bit peculiar, but should |
| 2736 |
* be fine. Once we have a cwd, we'll always be in the 'else' |
| 2737 |
* branch below which is simpler. |
| 2738 |
*/ |
| 2739 |
|
| 2740 |
ClientData cd = (ClientData) Tcl_FSGetNativePath(norm); |
| 2741 |
|
| 2742 |
FsUpdateCwd(norm, TclNativeDupInternalRep(cd)); |
| 2743 |
Tcl_DecrRefCount(norm); |
| 2744 |
} |
| 2745 |
Tcl_DecrRefCount(retVal); |
| 2746 |
} |
| 2747 |
} else { |
| 2748 |
/* |
| 2749 |
* We already have a cwd cached, but we want to give the filesystem it |
| 2750 |
* is in a chance to check whether that cwd has changed, or is perhaps |
| 2751 |
* no longer accessible. This allows an error to be thrown if, say, |
| 2752 |
* the permissions on that directory have changed. |
| 2753 |
*/ |
| 2754 |
|
| 2755 |
const Tcl_Filesystem *fsPtr = |
| 2756 |
Tcl_FSGetFileSystemForPath(tsdPtr->cwdPathPtr); |
| 2757 |
ClientData retCd = NULL; |
| 2758 |
Tcl_Obj *retVal, *norm; |
| 2759 |
|
| 2760 |
/* |
| 2761 |
* If the filesystem couldn't be found, or if no cwd function exists |
| 2762 |
* for this filesystem, then we simply assume the cached cwd is ok. |
| 2763 |
* If we do call a cwd, we must watch for errors (if the cwd returns |
| 2764 |
* NULL). This ensures that, say, on Unix if the permissions of the |
| 2765 |
* cwd change, 'pwd' does actually throw the correct error in Tcl. |
| 2766 |
* (This is tested for in the test suite on Unix). |
| 2767 |
*/ |
| 2768 |
|
| 2769 |
if (fsPtr == NULL || fsPtr->getCwdProc == NULL) { |
| 2770 |
goto cdDidNotChange; |
| 2771 |
} |
| 2772 |
|
| 2773 |
if (fsPtr->version == TCL_FILESYSTEM_VERSION_1) { |
| 2774 |
retVal = fsPtr->getCwdProc(interp); |
| 2775 |
} else { |
| 2776 |
/* |
| 2777 |
* New API. |
| 2778 |
*/ |
| 2779 |
|
| 2780 |
TclFSGetCwdProc2 *proc2 = (TclFSGetCwdProc2 *) fsPtr->getCwdProc; |
| 2781 |
|
| 2782 |
retCd = proc2(tsdPtr->cwdClientData); |
| 2783 |
if (retCd == NULL && interp != NULL) { |
| 2784 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 2785 |
"error getting working directory name: %s", |
| 2786 |
Tcl_PosixError(interp))); |
| 2787 |
} |
| 2788 |
|
| 2789 |
if (retCd == tsdPtr->cwdClientData) { |
| 2790 |
goto cdDidNotChange; |
| 2791 |
} |
| 2792 |
|
| 2793 |
/* |
| 2794 |
* Looks like a new current directory. |
| 2795 |
*/ |
| 2796 |
|
| 2797 |
retVal = fsPtr->internalToNormalizedProc(retCd); |
| 2798 |
Tcl_IncrRefCount(retVal); |
| 2799 |
} |
| 2800 |
|
| 2801 |
/* |
| 2802 |
* Check if the 'cwd' function returned an error; if so, reset the |
| 2803 |
* cwd. |
| 2804 |
*/ |
| 2805 |
|
| 2806 |
if (retVal == NULL) { |
| 2807 |
FsUpdateCwd(NULL, NULL); |
| 2808 |
goto cdDidNotChange; |
| 2809 |
} |
| 2810 |
|
| 2811 |
/* |
| 2812 |
* Normalize the path. |
| 2813 |
*/ |
| 2814 |
|
| 2815 |
norm = TclFSNormalizeAbsolutePath(interp, retVal); |
| 2816 |
|
| 2817 |
/* |
| 2818 |
* Check whether cwd has changed from the value previously stored in |
| 2819 |
* cwdPathPtr. Really 'norm' shouldn't be NULL, but we are careful. |
| 2820 |
*/ |
| 2821 |
|
| 2822 |
if (norm == NULL) { |
| 2823 |
/* Do nothing */ |
| 2824 |
if (retCd != NULL) { |
| 2825 |
fsPtr->freeInternalRepProc(retCd); |
| 2826 |
} |
| 2827 |
} else if (norm == tsdPtr->cwdPathPtr) { |
| 2828 |
goto cdEqual; |
| 2829 |
} else { |
| 2830 |
/* |
| 2831 |
* Note that both 'norm' and 'tsdPtr->cwdPathPtr' are normalized |
| 2832 |
* paths. Therefore we can be more efficient than calling |
| 2833 |
* 'Tcl_FSEqualPaths', and in addition avoid a nasty infinite loop |
| 2834 |
* bug when trying to normalize tsdPtr->cwdPathPtr. |
| 2835 |
*/ |
| 2836 |
|
| 2837 |
int len1, len2; |
| 2838 |
const char *str1, *str2; |
| 2839 |
|
| 2840 |
str1 = TclGetStringFromObj(tsdPtr->cwdPathPtr, &len1); |
| 2841 |
str2 = TclGetStringFromObj(norm, &len2); |
| 2842 |
if ((len1 == len2) && (strcmp(str1, str2) == 0)) { |
| 2843 |
/* |
| 2844 |
* If the paths were equal, we can be more efficient and |
| 2845 |
* retain the old path object which will probably already be |
| 2846 |
* shared. In this case we can simply free the normalized path |
| 2847 |
* we just calculated. |
| 2848 |
*/ |
| 2849 |
|
| 2850 |
cdEqual: |
| 2851 |
Tcl_DecrRefCount(norm); |
| 2852 |
if (retCd != NULL) { |
| 2853 |
fsPtr->freeInternalRepProc(retCd); |
| 2854 |
} |
| 2855 |
} else { |
| 2856 |
FsUpdateCwd(norm, retCd); |
| 2857 |
Tcl_DecrRefCount(norm); |
| 2858 |
} |
| 2859 |
} |
| 2860 |
Tcl_DecrRefCount(retVal); |
| 2861 |
} |
| 2862 |
|
| 2863 |
cdDidNotChange: |
| 2864 |
if (tsdPtr->cwdPathPtr != NULL) { |
| 2865 |
Tcl_IncrRefCount(tsdPtr->cwdPathPtr); |
| 2866 |
} |
| 2867 |
|
| 2868 |
return tsdPtr->cwdPathPtr; |
| 2869 |
} |
| 2870 |
|
| 2871 |
/* |
| 2872 |
*---------------------------------------------------------------------- |
| 2873 |
* |
| 2874 |
* Tcl_FSChdir -- |
| 2875 |
* |
| 2876 |
* This function replaces the library version of chdir(). |
| 2877 |
* |
| 2878 |
* The path is normalized and then passed to the filesystem which claims |
| 2879 |
* it. |
| 2880 |
* |
| 2881 |
* Results: |
| 2882 |
* See chdir() documentation. If successful, we keep a record of the |
| 2883 |
* successful path in cwdPathPtr for subsequent calls to getcwd. |
| 2884 |
* |
| 2885 |
* Side effects: |
| 2886 |
* See chdir() documentation. The global cwdPathPtr may change value. |
| 2887 |
* |
| 2888 |
*---------------------------------------------------------------------- |
| 2889 |
*/ |
| 2890 |
|
| 2891 |
int |
| 2892 |
Tcl_FSChdir( |
| 2893 |
Tcl_Obj *pathPtr) |
| 2894 |
{ |
| 2895 |
const Tcl_Filesystem *fsPtr, *oldFsPtr = NULL; |
| 2896 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&fsDataKey); |
| 2897 |
int retVal = -1; |
| 2898 |
|
| 2899 |
if (tsdPtr->cwdPathPtr != NULL) { |
| 2900 |
oldFsPtr = Tcl_FSGetFileSystemForPath(tsdPtr->cwdPathPtr); |
| 2901 |
} |
| 2902 |
if (Tcl_FSGetNormalizedPath(NULL, pathPtr) == NULL) { |
| 2903 |
Tcl_SetErrno(ENOENT); |
| 2904 |
return retVal; |
| 2905 |
} |
| 2906 |
|
| 2907 |
fsPtr = Tcl_FSGetFileSystemForPath(pathPtr); |
| 2908 |
if (fsPtr != NULL) { |
| 2909 |
if (fsPtr->chdirProc != NULL) { |
| 2910 |
/* |
| 2911 |
* If this fails, an appropriate errno will have been stored using |
| 2912 |
* 'Tcl_SetErrno()'. |
| 2913 |
*/ |
| 2914 |
|
| 2915 |
retVal = fsPtr->chdirProc(pathPtr); |
| 2916 |
} else { |
| 2917 |
/* |
| 2918 |
* Fallback on stat-based implementation. |
| 2919 |
*/ |
| 2920 |
|
| 2921 |
Tcl_StatBuf buf; |
| 2922 |
|
| 2923 |
/* |
| 2924 |
* If the file can be stat'ed and is a directory and is readable, |
| 2925 |
* then we can chdir. If any of these actions fail, then |
| 2926 |
* 'Tcl_SetErrno()' should automatically have been called to set |
| 2927 |
* an appropriate error code. |
| 2928 |
*/ |
| 2929 |
|
| 2930 |
if ((Tcl_FSStat(pathPtr, &buf) == 0) && (S_ISDIR(buf.st_mode)) |
| 2931 |
&& (Tcl_FSAccess(pathPtr, R_OK) == 0)) { |
| 2932 |
/* |
| 2933 |
* We allow the chdir. |
| 2934 |
*/ |
| 2935 |
|
| 2936 |
retVal = 0; |
| 2937 |
} |
| 2938 |
} |
| 2939 |
} else { |
| 2940 |
Tcl_SetErrno(ENOENT); |
| 2941 |
} |
| 2942 |
|
| 2943 |
/* |
| 2944 |
* The cwd changed, or an error was thrown. If an error was thrown, we can |
| 2945 |
* just continue (and that will report the error to the user). If there |
| 2946 |
* was no error we must assume that the cwd was actually changed to the |
| 2947 |
* normalized value we calculated above, and we must therefore cache that |
| 2948 |
* information. |
| 2949 |
* |
| 2950 |
* If the filesystem in question has a getCwdProc, then the correct logic |
| 2951 |
* which performs the part below is already part of the Tcl_FSGetCwd() |
| 2952 |
* call, so no need to replicate it again. This will have a side effect |
| 2953 |
* though. The private authoritative representation of the current working |
| 2954 |
* directory stored in cwdPathPtr in static memory will be out-of-sync |
| 2955 |
* with the real OS-maintained value. The first call to Tcl_FSGetCwd will |
| 2956 |
* however recalculate the private copy to match the OS-value so |
| 2957 |
* everything will work right. |
| 2958 |
* |
| 2959 |
* However, if there is no getCwdProc, then we _must_ update our private |
| 2960 |
* storage of the cwd, since this is the only opportunity to do that! |
| 2961 |
* |
| 2962 |
* Note: We currently call this block of code irrespective of whether |
| 2963 |
* there was a getCwdProc or not, but the code should all in principle |
| 2964 |
* work if we only call this block if fsPtr->getCwdProc == NULL. |
| 2965 |
*/ |
| 2966 |
|
| 2967 |
if (retVal == 0) { |
| 2968 |
/* |
| 2969 |
* Note that this normalized path may be different to what we found |
| 2970 |
* above (or at least a different object), if the filesystem epoch |
| 2971 |
* changed recently. This can actually happen with scripted documents |
| 2972 |
* very easily. Therefore we ask for the normalized path again (the |
| 2973 |
* correct value will have been cached as a result of the |
| 2974 |
* Tcl_FSGetFileSystemForPath call above anyway). |
| 2975 |
*/ |
| 2976 |
|
| 2977 |
Tcl_Obj *normDirName = Tcl_FSGetNormalizedPath(NULL, pathPtr); |
| 2978 |
|
| 2979 |
if (normDirName == NULL) { |
| 2980 |
/* Not really true, but what else to do? */ |
| 2981 |
Tcl_SetErrno(ENOENT); |
| 2982 |
return -1; |
| 2983 |
} |
| 2984 |
if (normDirName != pathPtr) { Tcl_IncrRefCount(normDirName); } |
| 2985 |
|
| 2986 |
if (fsPtr == &tclNativeFilesystem) { |
| 2987 |
/* |
| 2988 |
* For the native filesystem, we keep a cache of the native |
| 2989 |
* representation of the cwd. But, we want to do that for the |
| 2990 |
* exact format that is returned by 'getcwd' (so that we can later |
| 2991 |
* compare the two representations for equality), which might not |
| 2992 |
* be exactly the same char-string as the native representation of |
| 2993 |
* the fully normalized path (e.g. on Windows there's a |
| 2994 |
* forward-slash vs backslash difference). Hence we ask for this |
| 2995 |
* again here. On Unix it might actually be true that we always |
| 2996 |
* have the correct form in the native rep in which case we could |
| 2997 |
* simply use: |
| 2998 |
* cd = Tcl_FSGetNativePath(pathPtr); |
| 2999 |
* instead. This should be examined by someone on Unix. |
| 3000 |
*/ |
| 3001 |
|
| 3002 |
ClientData cd; |
| 3003 |
ClientData oldcd = tsdPtr->cwdClientData; |
| 3004 |
|
| 3005 |
/* |
| 3006 |
* Assumption we are using a filesystem version 2. |
| 3007 |
*/ |
| 3008 |
|
| 3009 |
TclFSGetCwdProc2 *proc2 = (TclFSGetCwdProc2 *) fsPtr->getCwdProc; |
| 3010 |
|
| 3011 |
cd = proc2(oldcd); |
| 3012 |
if (cd != oldcd) { |
| 3013 |
FsUpdateCwd(normDirName, cd); |
| 3014 |
} |
| 3015 |
} else { |
| 3016 |
FsUpdateCwd(normDirName, NULL); |
| 3017 |
} |
| 3018 |
|
| 3019 |
/* |
| 3020 |
* If the filesystem changed between old and new cwd |
| 3021 |
* force filesystem refresh on path objects. |
| 3022 |
*/ |
| 3023 |
if (oldFsPtr != NULL && fsPtr != oldFsPtr) { |
| 3024 |
Tcl_FSMountsChanged(NULL); |
| 3025 |
} |
| 3026 |
if (normDirName != pathPtr) { Tcl_DecrRefCount(normDirName); } |
| 3027 |
} |
| 3028 |
|
| 3029 |
return retVal; |
| 3030 |
} |
| 3031 |
|
| 3032 |
/* |
| 3033 |
*---------------------------------------------------------------------- |
| 3034 |
* |
| 3035 |
* Tcl_FSLoadFile -- |
| 3036 |
* |
| 3037 |
* Dynamically loads a binary code file into memory and returns the |
| 3038 |
* addresses of two functions within that file, if they are defined. The |
| 3039 |
* appropriate function for the filesystem to which pathPtr belongs will |
| 3040 |
* be called. |
| 3041 |
* |
| 3042 |
* Note that the native filesystem doesn't actually assume 'pathPtr' is a |
| 3043 |
* path. Rather it assumes pathPtr is either a path or just the name |
| 3044 |
* (tail) of a file which can be found somewhere in the environment's |
| 3045 |
* loadable path. This behaviour is not very compatible with virtual |
| 3046 |
* filesystems (and has other problems documented in the load man-page), |
| 3047 |
* so it is advised that full paths are always used. |
| 3048 |
* |
| 3049 |
* Results: |
| 3050 |
* A standard Tcl completion code. If an error occurs, an error message |
| 3051 |
* is left in the interp's result. |
| 3052 |
* |
| 3053 |
* Side effects: |
| 3054 |
* New code suddenly appears in memory. This may later be unloaded by |
| 3055 |
* passing the clientData to the unloadProc. |
| 3056 |
* |
| 3057 |
*---------------------------------------------------------------------- |
| 3058 |
*/ |
| 3059 |
|
| 3060 |
int |
| 3061 |
Tcl_FSLoadFile( |
| 3062 |
Tcl_Interp *interp, /* Used for error reporting. */ |
| 3063 |
Tcl_Obj *pathPtr, /* Name of the file containing the desired |
| 3064 |
* code. */ |
| 3065 |
const char *sym1, const char *sym2, |
| 3066 |
/* Names of two functions to look up in the |
| 3067 |
* file's symbol table. */ |
| 3068 |
Tcl_PackageInitProc **proc1Ptr, Tcl_PackageInitProc **proc2Ptr, |
| 3069 |
/* Where to return the addresses corresponding |
| 3070 |
* to sym1 and sym2. */ |
| 3071 |
Tcl_LoadHandle *handlePtr, /* Filled with token for dynamically loaded |
| 3072 |
* file which will be passed back to |
| 3073 |
* (*unloadProcPtr)() to unload the file. */ |
| 3074 |
Tcl_FSUnloadFileProc **unloadProcPtr) |
| 3075 |
/* Filled with address of Tcl_FSUnloadFileProc |
| 3076 |
* function which should be used for this |
| 3077 |
* file. */ |
| 3078 |
{ |
| 3079 |
const char *symbols[3]; |
| 3080 |
void *procPtrs[2]; |
| 3081 |
int res; |
| 3082 |
|
| 3083 |
/* |
| 3084 |
* Initialize the arrays. |
| 3085 |
*/ |
| 3086 |
|
| 3087 |
symbols[0] = sym1; |
| 3088 |
symbols[1] = sym2; |
| 3089 |
symbols[2] = NULL; |
| 3090 |
|
| 3091 |
/* |
| 3092 |
* Perform the load. |
| 3093 |
*/ |
| 3094 |
|
| 3095 |
res = Tcl_LoadFile(interp, pathPtr, symbols, 0, procPtrs, handlePtr); |
| 3096 |
if (res == TCL_OK) { |
| 3097 |
*proc1Ptr = (Tcl_PackageInitProc *) procPtrs[0]; |
| 3098 |
*proc2Ptr = (Tcl_PackageInitProc *) procPtrs[1]; |
| 3099 |
} else { |
| 3100 |
*proc1Ptr = *proc2Ptr = NULL; |
| 3101 |
} |
| 3102 |
|
| 3103 |
return res; |
| 3104 |
} |
| 3105 |
|
| 3106 |
/* |
| 3107 |
*---------------------------------------------------------------------- |
| 3108 |
* |
| 3109 |
* Tcl_LoadFile -- |
| 3110 |
* |
| 3111 |
* Dynamically loads a binary code file into memory and returns the |
| 3112 |
* addresses of a number of given functions within that file, if they are |
| 3113 |
* defined. The appropriate function for the filesystem to which pathPtr |
| 3114 |
* belongs will be called. |
| 3115 |
* |
| 3116 |
* Note that the native filesystem doesn't actually assume 'pathPtr' is a |
| 3117 |
* path. Rather it assumes pathPtr is either a path or just the name |
| 3118 |
* (tail) of a file which can be found somewhere in the environment's |
| 3119 |
* loadable path. This behaviour is not very compatible with virtual |
| 3120 |
* filesystems (and has other problems documented in the load man-page), |
| 3121 |
* so it is advised that full paths are always used. |
| 3122 |
* |
| 3123 |
* Results: |
| 3124 |
* A standard Tcl completion code. If an error occurs, an error message |
| 3125 |
* is left in the interp's result. |
| 3126 |
* |
| 3127 |
* Side effects: |
| 3128 |
* New code suddenly appears in memory. This may later be unloaded by |
| 3129 |
* calling TclFS_UnloadFile. |
| 3130 |
* |
| 3131 |
*---------------------------------------------------------------------- |
| 3132 |
*/ |
| 3133 |
|
| 3134 |
/* |
| 3135 |
* Workaround for issue with modern HPUX which do allow the unlink (no ETXTBSY |
| 3136 |
* error) yet somehow trash some internal data structures which prevents the |
| 3137 |
* second and further shared libraries from getting properly loaded. Only the |
| 3138 |
* first is ok. We try to get around the issue by not unlinking, |
| 3139 |
* i.e. emulating the behaviour of the older HPUX which denied removal. |
| 3140 |
* |
| 3141 |
* Doing the unlink is also an issue within docker containers, whose AUFS |
| 3142 |
* bungles this as well, see |
| 3143 |
* https://github.com/dotcloud/docker/issues/1911 |
| 3144 |
* |
| 3145 |
* For these situations the change below makes the execution of the unlink |
| 3146 |
* semi-controllable at runtime. |
| 3147 |
* |
| 3148 |
* An AUFS filesystem (if it can be detected) will force avoidance of |
| 3149 |
* unlink. The env variable TCL_TEMPLOAD_NO_UNLINK allows detection of a |
| 3150 |
* users general request (unlink and not. |
| 3151 |
* |
| 3152 |
* By default the unlink is done (if not in AUFS). However if the variable is |
| 3153 |
* present and set to true (any integer > 0) then the unlink is skipped. |
| 3154 |
*/ |
| 3155 |
|
| 3156 |
#ifdef _WIN32 |
| 3157 |
#define getenv(x) _wgetenv(L##x) |
| 3158 |
#define atoi(x) _wtoi(x) |
| 3159 |
#else |
| 3160 |
#define WCHAR char |
| 3161 |
#endif |
| 3162 |
|
| 3163 |
static int |
| 3164 |
skipUnlink (Tcl_Obj* shlibFile) |
| 3165 |
{ |
| 3166 |
/* Order of testing: |
| 3167 |
* 1. On hpux we generally want to skip unlink in general |
| 3168 |
* |
| 3169 |
* Outside of hpux then: |
| 3170 |
* 2. For a general user request (TCL_TEMPLOAD_NO_UNLINK present, non-empty, => int) |
| 3171 |
* 3. For general AUFS environment (statfs, if available). |
| 3172 |
* |
| 3173 |
* Ad 2: This variable can disable/override the AUFS detection, i.e. for |
| 3174 |
* testing if a newer AUFS does not have the bug any more. |
| 3175 |
* |
| 3176 |
* Ad 3: This is conditionally compiled in. Condition currently must be set manually. |
| 3177 |
* This part needs proper tests in the configure(.in). |
| 3178 |
*/ |
| 3179 |
|
| 3180 |
#ifdef hpux |
| 3181 |
return 1; |
| 3182 |
#else |
| 3183 |
WCHAR *skipstr; |
| 3184 |
|
| 3185 |
skipstr = getenv("TCL_TEMPLOAD_NO_UNLINK"); |
| 3186 |
if (skipstr && (skipstr[0] != '\0')) { |
| 3187 |
return atoi(skipstr); |
| 3188 |
} |
| 3189 |
|
| 3190 |
#ifdef TCL_TEMPLOAD_NO_UNLINK |
| 3191 |
#ifndef NO_FSTATFS |
| 3192 |
{ |
| 3193 |
struct statfs fs; |
| 3194 |
/* Have fstatfs. May not have the AUFS super magic ... Indeed our build |
| 3195 |
* box is too old to have it directly in the headers. Define taken from |
| 3196 |
* http://mooon.googlecode.com/svn/trunk/linux_include/linux/aufs_type.h |
| 3197 |
* http://aufs.sourceforge.net/ |
| 3198 |
* Better reference will be gladly taken. |
| 3199 |
*/ |
| 3200 |
#ifndef AUFS_SUPER_MAGIC |
| 3201 |
#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's') |
| 3202 |
#endif /* AUFS_SUPER_MAGIC */ |
| 3203 |
if ((statfs(TclGetString(shlibFile), &fs) == 0) && |
| 3204 |
(fs.f_type == AUFS_SUPER_MAGIC)) { |
| 3205 |
return 1; |
| 3206 |
} |
| 3207 |
} |
| 3208 |
#endif /* ... NO_FSTATFS */ |
| 3209 |
#endif /* ... TCL_TEMPLOAD_NO_UNLINK */ |
| 3210 |
|
| 3211 |
/* Fallback: !hpux, no EV override, no AUFS (detection, nor detected): |
| 3212 |
* Don't skip */ |
| 3213 |
return 0; |
| 3214 |
#endif /* hpux */ |
| 3215 |
} |
| 3216 |
|
| 3217 |
int |
| 3218 |
Tcl_LoadFile( |
| 3219 |
Tcl_Interp *interp, /* Used for error reporting. */ |
| 3220 |
Tcl_Obj *pathPtr, /* Name of the file containing the desired |
| 3221 |
* code. */ |
| 3222 |
const char *const symbols[],/* Names of functions to look up in the file's |
| 3223 |
* symbol table. */ |
| 3224 |
int flags, /* Flags */ |
| 3225 |
void *procVPtrs, /* Where to return the addresses corresponding |
| 3226 |
* to symbols[]. */ |
| 3227 |
Tcl_LoadHandle *handlePtr) /* Filled with token for shared library |
| 3228 |
* information which can be used in |
| 3229 |
* TclpFindSymbol. */ |
| 3230 |
{ |
| 3231 |
void **procPtrs = (void **) procVPtrs; |
| 3232 |
const Tcl_Filesystem *fsPtr = Tcl_FSGetFileSystemForPath(pathPtr); |
| 3233 |
const Tcl_Filesystem *copyFsPtr; |
| 3234 |
Tcl_FSUnloadFileProc *unloadProcPtr; |
| 3235 |
Tcl_Obj *copyToPtr; |
| 3236 |
Tcl_LoadHandle newLoadHandle = NULL; |
| 3237 |
Tcl_LoadHandle divertedLoadHandle = NULL; |
| 3238 |
Tcl_FSUnloadFileProc *newUnloadProcPtr = NULL; |
| 3239 |
FsDivertLoad *tvdlPtr; |
| 3240 |
int retVal; |
| 3241 |
int i; |
| 3242 |
|
| 3243 |
if (fsPtr == NULL) { |
| 3244 |
Tcl_SetErrno(ENOENT); |
| 3245 |
return TCL_ERROR; |
| 3246 |
} |
| 3247 |
|
| 3248 |
if (fsPtr->loadFileProc != NULL) { |
| 3249 |
retVal = ((Tcl_FSLoadFileProc2 *)(void *)(fsPtr->loadFileProc)) |
| 3250 |
(interp, pathPtr, handlePtr, &unloadProcPtr, flags); |
| 3251 |
|
| 3252 |
if (retVal == TCL_OK) { |
| 3253 |
if (*handlePtr == NULL) { |
| 3254 |
return TCL_ERROR; |
| 3255 |
} |
| 3256 |
if (interp) { |
| 3257 |
Tcl_ResetResult(interp); |
| 3258 |
} |
| 3259 |
goto resolveSymbols; |
| 3260 |
} |
| 3261 |
if (Tcl_GetErrno() != EXDEV) { |
| 3262 |
return retVal; |
| 3263 |
} |
| 3264 |
} |
| 3265 |
|
| 3266 |
/* |
| 3267 |
* The filesystem doesn't support 'load'. Fall to the following: |
| 3268 |
*/ |
| 3269 |
|
| 3270 |
/* |
| 3271 |
* Make sure the file is accessible. |
| 3272 |
*/ |
| 3273 |
|
| 3274 |
if (Tcl_FSAccess(pathPtr, R_OK) != 0) { |
| 3275 |
if (interp) { |
| 3276 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 3277 |
"couldn't load library \"%s\": %s", |
| 3278 |
TclGetString(pathPtr), Tcl_PosixError(interp))); |
| 3279 |
} |
| 3280 |
return TCL_ERROR; |
| 3281 |
} |
| 3282 |
|
| 3283 |
#ifdef TCL_LOAD_FROM_MEMORY |
| 3284 |
/* |
| 3285 |
* The platform supports loading a dynamic shared object from memory. |
| 3286 |
* Create a sufficiently large buffer, read the file into it, and then load |
| 3287 |
* the dynamic shared object from the buffer: |
| 3288 |
*/ |
| 3289 |
|
| 3290 |
{ |
| 3291 |
int ret; |
| 3292 |
size_t size; |
| 3293 |
void *buffer; |
| 3294 |
Tcl_StatBuf statBuf; |
| 3295 |
Tcl_Channel data; |
| 3296 |
|
| 3297 |
ret = Tcl_FSStat(pathPtr, &statBuf); |
| 3298 |
if (ret < 0) { |
| 3299 |
goto mustCopyToTempAnyway; |
| 3300 |
} |
| 3301 |
size = statBuf.st_size; |
| 3302 |
|
| 3303 |
/* |
| 3304 |
* Tcl_Read takes an int: Determine whether the file size <= INT_MAX |
| 3305 |
*/ |
| 3306 |
|
| 3307 |
if (size > INT_MAX) { |
| 3308 |
goto mustCopyToTempAnyway; |
| 3309 |
} |
| 3310 |
data = Tcl_FSOpenFileChannel(interp, pathPtr, "rb", 0666); |
| 3311 |
if (!data) { |
| 3312 |
if (interp) { |
| 3313 |
Tcl_ResetResult(interp); |
| 3314 |
} |
| 3315 |
goto mustCopyToTempAnyway; |
| 3316 |
} |
| 3317 |
buffer = TclpLoadMemoryGetBuffer(size); |
| 3318 |
if (!buffer) { |
| 3319 |
Tcl_Close(interp, data); |
| 3320 |
goto mustCopyToTempAnyway; |
| 3321 |
} |
| 3322 |
ret = Tcl_Read(data, buffer, size); |
| 3323 |
Tcl_Close(interp, data); |
| 3324 |
ret = TclpLoadMemory(buffer, size, ret, TclGetString(pathPtr), handlePtr, |
| 3325 |
&unloadProcPtr, flags); |
| 3326 |
if (ret == TCL_OK && *handlePtr != NULL) { |
| 3327 |
goto resolveSymbols; |
| 3328 |
} |
| 3329 |
} |
| 3330 |
|
| 3331 |
mustCopyToTempAnyway: |
| 3332 |
#endif /* TCL_LOAD_FROM_MEMORY */ |
| 3333 |
|
| 3334 |
/* |
| 3335 |
* Get a temporary filename, first to copy the file into, and then to load. |
| 3336 |
*/ |
| 3337 |
|
| 3338 |
copyToPtr = TclpTempFileNameForLibrary(interp, pathPtr); |
| 3339 |
if (copyToPtr == NULL) { |
| 3340 |
return TCL_ERROR; |
| 3341 |
} |
| 3342 |
Tcl_IncrRefCount(copyToPtr); |
| 3343 |
|
| 3344 |
copyFsPtr = Tcl_FSGetFileSystemForPath(copyToPtr); |
| 3345 |
if ((copyFsPtr == NULL) || (copyFsPtr == fsPtr)) { |
| 3346 |
/* |
| 3347 |
* We already know we can't use Tcl_FSLoadFile from this filesystem, |
| 3348 |
* and we must avoid a possible infinite loop. Try to delete the file |
| 3349 |
* we probably created, and then exit. |
| 3350 |
*/ |
| 3351 |
|
| 3352 |
Tcl_FSDeleteFile(copyToPtr); |
| 3353 |
Tcl_DecrRefCount(copyToPtr); |
| 3354 |
if (interp) { |
| 3355 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3356 |
"couldn't load from current filesystem", -1)); |
| 3357 |
} |
| 3358 |
return TCL_ERROR; |
| 3359 |
} |
| 3360 |
|
| 3361 |
if (TclCrossFilesystemCopy(interp, pathPtr, copyToPtr) != TCL_OK) { |
| 3362 |
/* |
| 3363 |
* Cross-platform copy failed. |
| 3364 |
*/ |
| 3365 |
|
| 3366 |
Tcl_FSDeleteFile(copyToPtr); |
| 3367 |
Tcl_DecrRefCount(copyToPtr); |
| 3368 |
return TCL_ERROR; |
| 3369 |
} |
| 3370 |
|
| 3371 |
#ifndef _WIN32 |
| 3372 |
/* |
| 3373 |
* Do we need to set appropriate permissions on the file? This may be |
| 3374 |
* required on some systems. On Unix we could loop over the file |
| 3375 |
* attributes, and set any that are called "-permissions" to 0700. However |
| 3376 |
* we just do this directly, like this: |
| 3377 |
*/ |
| 3378 |
|
| 3379 |
{ |
| 3380 |
int index; |
| 3381 |
Tcl_Obj *perm; |
| 3382 |
|
| 3383 |
TclNewLiteralStringObj(perm, "0700"); |
| 3384 |
Tcl_IncrRefCount(perm); |
| 3385 |
if (TclFSFileAttrIndex(copyToPtr, "-permissions", &index) == TCL_OK) { |
| 3386 |
Tcl_FSFileAttrsSet(NULL, index, copyToPtr, perm); |
| 3387 |
} |
| 3388 |
Tcl_DecrRefCount(perm); |
| 3389 |
} |
| 3390 |
#endif |
| 3391 |
|
| 3392 |
/* |
| 3393 |
* We need to reset the result now, because the cross-filesystem copy may |
| 3394 |
* have stored the number of bytes in the result. |
| 3395 |
*/ |
| 3396 |
|
| 3397 |
if (interp) { |
| 3398 |
Tcl_ResetResult(interp); |
| 3399 |
} |
| 3400 |
|
| 3401 |
retVal = Tcl_LoadFile(interp, copyToPtr, symbols, flags, procPtrs, |
| 3402 |
&newLoadHandle); |
| 3403 |
if (retVal != TCL_OK) { |
| 3404 |
/* |
| 3405 |
* The file didn't load successfully. |
| 3406 |
*/ |
| 3407 |
|
| 3408 |
Tcl_FSDeleteFile(copyToPtr); |
| 3409 |
Tcl_DecrRefCount(copyToPtr); |
| 3410 |
return retVal; |
| 3411 |
} |
| 3412 |
|
| 3413 |
/* |
| 3414 |
* Try to delete the file immediately - this is possible in some OSes, and |
| 3415 |
* avoids any worries about leaving the copy laying around on exit. |
| 3416 |
*/ |
| 3417 |
|
| 3418 |
if ( |
| 3419 |
!skipUnlink (copyToPtr) && |
| 3420 |
(Tcl_FSDeleteFile(copyToPtr) == TCL_OK)) { |
| 3421 |
Tcl_DecrRefCount(copyToPtr); |
| 3422 |
|
| 3423 |
/* |
| 3424 |
* We tell our caller about the real shared library which was loaded. |
| 3425 |
* Note that this does mean that the package list maintained by 'load' |
| 3426 |
* will store the original (vfs) path alongside the temporary load |
| 3427 |
* handle and unload proc ptr. |
| 3428 |
*/ |
| 3429 |
|
| 3430 |
*handlePtr = newLoadHandle; |
| 3431 |
if (interp) { |
| 3432 |
Tcl_ResetResult(interp); |
| 3433 |
} |
| 3434 |
return TCL_OK; |
| 3435 |
} |
| 3436 |
|
| 3437 |
/* |
| 3438 |
* When we unload this file, we need to divert the unloading so we can |
| 3439 |
* unload and cleanup the temporary file correctly. |
| 3440 |
*/ |
| 3441 |
|
| 3442 |
tvdlPtr = (FsDivertLoad *)ckalloc(sizeof(FsDivertLoad)); |
| 3443 |
|
| 3444 |
/* |
| 3445 |
* Remember three pieces of information. This allows us to cleanup the |
| 3446 |
* diverted load completely, on platforms which allow proper unloading of |
| 3447 |
* code. |
| 3448 |
*/ |
| 3449 |
|
| 3450 |
tvdlPtr->loadHandle = newLoadHandle; |
| 3451 |
tvdlPtr->unloadProcPtr = newUnloadProcPtr; |
| 3452 |
|
| 3453 |
if (copyFsPtr != &tclNativeFilesystem) { |
| 3454 |
/* |
| 3455 |
* copyToPtr is already incremented for this reference. |
| 3456 |
*/ |
| 3457 |
|
| 3458 |
tvdlPtr->divertedFile = copyToPtr; |
| 3459 |
|
| 3460 |
/* |
| 3461 |
* This is the filesystem we loaded it into. Since we have a reference |
| 3462 |
* to 'copyToPtr', we already have a refCount on this filesystem, so |
| 3463 |
* we don't need to worry about it disappearing on us. |
| 3464 |
*/ |
| 3465 |
|
| 3466 |
tvdlPtr->divertedFilesystem = copyFsPtr; |
| 3467 |
tvdlPtr->divertedFileNativeRep = NULL; |
| 3468 |
} else { |
| 3469 |
/* |
| 3470 |
* We need the native rep. |
| 3471 |
*/ |
| 3472 |
|
| 3473 |
tvdlPtr->divertedFileNativeRep = TclNativeDupInternalRep( |
| 3474 |
Tcl_FSGetInternalRep(copyToPtr, copyFsPtr)); |
| 3475 |
|
| 3476 |
/* |
| 3477 |
* We don't need or want references to the copied Tcl_Obj or the |
| 3478 |
* filesystem if it is the native one. |
| 3479 |
*/ |
| 3480 |
|
| 3481 |
tvdlPtr->divertedFile = NULL; |
| 3482 |
tvdlPtr->divertedFilesystem = NULL; |
| 3483 |
Tcl_DecrRefCount(copyToPtr); |
| 3484 |
} |
| 3485 |
|
| 3486 |
copyToPtr = NULL; |
| 3487 |
|
| 3488 |
divertedLoadHandle = (Tcl_LoadHandle)ckalloc(sizeof(struct Tcl_LoadHandle_)); |
| 3489 |
divertedLoadHandle->clientData = tvdlPtr; |
| 3490 |
divertedLoadHandle->findSymbolProcPtr = DivertFindSymbol; |
| 3491 |
divertedLoadHandle->unloadFileProcPtr = DivertUnloadFile; |
| 3492 |
*handlePtr = divertedLoadHandle; |
| 3493 |
|
| 3494 |
if (interp) { |
| 3495 |
Tcl_ResetResult(interp); |
| 3496 |
} |
| 3497 |
return retVal; |
| 3498 |
|
| 3499 |
resolveSymbols: |
| 3500 |
/* |
| 3501 |
* At this point, *handlePtr is already set up to the handle for the |
| 3502 |
* loaded library. We now try to resolve the symbols. |
| 3503 |
*/ |
| 3504 |
|
| 3505 |
if (symbols != NULL) { |
| 3506 |
for (i=0 ; symbols[i] != NULL; i++) { |
| 3507 |
procPtrs[i] = Tcl_FindSymbol(interp, *handlePtr, symbols[i]); |
| 3508 |
if (procPtrs[i] == NULL) { |
| 3509 |
/* |
| 3510 |
* At least one symbol in the list was not found. Unload the |
| 3511 |
* file, and report the problem back to the caller. |
| 3512 |
* (Tcl_FindSymbol should already have left an appropriate |
| 3513 |
* error message.) |
| 3514 |
*/ |
| 3515 |
|
| 3516 |
(*handlePtr)->unloadFileProcPtr(*handlePtr); |
| 3517 |
*handlePtr = NULL; |
| 3518 |
return TCL_ERROR; |
| 3519 |
} |
| 3520 |
} |
| 3521 |
} |
| 3522 |
return TCL_OK; |
| 3523 |
} |
| 3524 |
|
| 3525 |
/* |
| 3526 |
*---------------------------------------------------------------------- |
| 3527 |
* |
| 3528 |
* DivertFindSymbol -- |
| 3529 |
* |
| 3530 |
* Find a symbol in a shared library loaded by copy-from-VFS. |
| 3531 |
* |
| 3532 |
*---------------------------------------------------------------------- |
| 3533 |
*/ |
| 3534 |
|
| 3535 |
static void * |
| 3536 |
DivertFindSymbol( |
| 3537 |
Tcl_Interp *interp, /* Tcl interpreter */ |
| 3538 |
Tcl_LoadHandle loadHandle, /* Handle to the diverted module */ |
| 3539 |
const char *symbol) /* Symbol to resolve */ |
| 3540 |
{ |
| 3541 |
FsDivertLoad *tvdlPtr = (FsDivertLoad *) loadHandle->clientData; |
| 3542 |
Tcl_LoadHandle originalHandle = tvdlPtr->loadHandle; |
| 3543 |
|
| 3544 |
return originalHandle->findSymbolProcPtr(interp, originalHandle, symbol); |
| 3545 |
} |
| 3546 |
|
| 3547 |
/* |
| 3548 |
*---------------------------------------------------------------------- |
| 3549 |
* |
| 3550 |
* DivertUnloadFile -- |
| 3551 |
* |
| 3552 |
* Unloads a file that has been loaded by copying from VFS to the native |
| 3553 |
* filesystem. |
| 3554 |
* |
| 3555 |
* Parameters: |
| 3556 |
* loadHandle -- Handle of the file to unload |
| 3557 |
* |
| 3558 |
*---------------------------------------------------------------------- |
| 3559 |
*/ |
| 3560 |
|
| 3561 |
static void |
| 3562 |
DivertUnloadFile( |
| 3563 |
Tcl_LoadHandle loadHandle) |
| 3564 |
{ |
| 3565 |
FsDivertLoad *tvdlPtr = (FsDivertLoad *) loadHandle->clientData; |
| 3566 |
Tcl_LoadHandle originalHandle; |
| 3567 |
|
| 3568 |
/* |
| 3569 |
* This test should never trigger, since we give the client data in the |
| 3570 |
* function above. |
| 3571 |
*/ |
| 3572 |
|
| 3573 |
if (tvdlPtr == NULL) { |
| 3574 |
return; |
| 3575 |
} |
| 3576 |
originalHandle = tvdlPtr->loadHandle; |
| 3577 |
|
| 3578 |
/* |
| 3579 |
* Call the real 'unloadfile' proc we actually used. It is very important |
| 3580 |
* that we call this first, so that the shared library is actually |
| 3581 |
* unloaded by the OS. Otherwise, the following 'delete' may well fail |
| 3582 |
* because the shared library is still in use. |
| 3583 |
*/ |
| 3584 |
|
| 3585 |
originalHandle->unloadFileProcPtr(originalHandle); |
| 3586 |
|
| 3587 |
/* |
| 3588 |
* What filesystem contains the temp copy of the library? |
| 3589 |
*/ |
| 3590 |
|
| 3591 |
if (tvdlPtr->divertedFilesystem == NULL) { |
| 3592 |
/* |
| 3593 |
* It was the native filesystem, and we have a special function |
| 3594 |
* available just for this purpose, which we know works even at this |
| 3595 |
* late stage. |
| 3596 |
*/ |
| 3597 |
|
| 3598 |
TclpDeleteFile(tvdlPtr->divertedFileNativeRep); |
| 3599 |
NativeFreeInternalRep(tvdlPtr->divertedFileNativeRep); |
| 3600 |
} else { |
| 3601 |
/* |
| 3602 |
* Remove the temporary file we created. Note, we may crash here |
| 3603 |
* because encodings have been taken down already. |
| 3604 |
*/ |
| 3605 |
|
| 3606 |
if (tvdlPtr->divertedFilesystem->deleteFileProc(tvdlPtr->divertedFile) |
| 3607 |
!= TCL_OK) { |
| 3608 |
/* |
| 3609 |
* The above may have failed because the filesystem, or something |
| 3610 |
* it depends upon (e.g. encodings) have been taken down because |
| 3611 |
* Tcl is exiting. |
| 3612 |
* |
| 3613 |
* We may need to work out how to delete this file more robustly |
| 3614 |
* (or give the filesystem the information it needs to delete the |
| 3615 |
* file more robustly). |
| 3616 |
* |
| 3617 |
* In particular, one problem might be that the filesystem cannot |
| 3618 |
* extract the information it needs from the above path object |
| 3619 |
* because Tcl's entire filesystem apparatus (the code in this |
| 3620 |
* file) has been finalized, and it refuses to pass the internal |
| 3621 |
* representation to the filesystem. |
| 3622 |
*/ |
| 3623 |
} |
| 3624 |
|
| 3625 |
/* |
| 3626 |
* And free up the allocations. This will also of course remove a |
| 3627 |
* refCount from the Tcl_Filesystem to which this file belongs, which |
| 3628 |
* could then free up the filesystem if we are exiting. |
| 3629 |
*/ |
| 3630 |
|
| 3631 |
Tcl_DecrRefCount(tvdlPtr->divertedFile); |
| 3632 |
} |
| 3633 |
|
| 3634 |
ckfree(tvdlPtr); |
| 3635 |
ckfree(loadHandle); |
| 3636 |
} |
| 3637 |
|
| 3638 |
/* |
| 3639 |
*---------------------------------------------------------------------- |
| 3640 |
* |
| 3641 |
* Tcl_FindSymbol -- |
| 3642 |
* |
| 3643 |
* Find a symbol in a loaded library |
| 3644 |
* |
| 3645 |
* Results: |
| 3646 |
* Returns a pointer to the symbol if found. If not found, returns NULL |
| 3647 |
* and leaves an error message in the interpreter result. |
| 3648 |
* |
| 3649 |
* This function was once filesystem-specific, but has been made portable by |
| 3650 |
* having TclpDlopen return a structure that includes procedure pointers. |
| 3651 |
* |
| 3652 |
*---------------------------------------------------------------------- |
| 3653 |
*/ |
| 3654 |
|
| 3655 |
void * |
| 3656 |
Tcl_FindSymbol( |
| 3657 |
Tcl_Interp *interp, /* Tcl interpreter */ |
| 3658 |
Tcl_LoadHandle loadHandle, /* Handle to the loaded library */ |
| 3659 |
const char *symbol) /* Name of the symbol to resolve */ |
| 3660 |
{ |
| 3661 |
return loadHandle->findSymbolProcPtr(interp, loadHandle, symbol); |
| 3662 |
} |
| 3663 |
|
| 3664 |
/* |
| 3665 |
*---------------------------------------------------------------------- |
| 3666 |
* |
| 3667 |
* Tcl_FSUnloadFile -- |
| 3668 |
* |
| 3669 |
* Unloads a library given its handle. Checks first that the library |
| 3670 |
* supports unloading. |
| 3671 |
* |
| 3672 |
*---------------------------------------------------------------------- |
| 3673 |
*/ |
| 3674 |
|
| 3675 |
int |
| 3676 |
Tcl_FSUnloadFile( |
| 3677 |
Tcl_Interp *interp, /* Tcl interpreter */ |
| 3678 |
Tcl_LoadHandle handle) /* Handle of the file to unload */ |
| 3679 |
{ |
| 3680 |
if (handle->unloadFileProcPtr == NULL) { |
| 3681 |
if (interp != NULL) { |
| 3682 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3683 |
"cannot unload: filesystem does not support unloading", |
| 3684 |
-1)); |
| 3685 |
} |
| 3686 |
return TCL_ERROR; |
| 3687 |
} |
| 3688 |
if (handle->unloadFileProcPtr != NULL) { |
| 3689 |
handle->unloadFileProcPtr(handle); |
| 3690 |
} |
| 3691 |
return TCL_OK; |
| 3692 |
} |
| 3693 |
|
| 3694 |
/* |
| 3695 |
*--------------------------------------------------------------------------- |
| 3696 |
* |
| 3697 |
* Tcl_FSLink -- |
| 3698 |
* |
| 3699 |
* This function replaces the library version of readlink() and can also |
| 3700 |
* be used to make links. The appropriate function for the filesystem to |
| 3701 |
* which pathPtr belongs will be called. |
| 3702 |
* |
| 3703 |
* Results: |
| 3704 |
* If toPtr is NULL, then the result is a Tcl_Obj specifying the contents |
| 3705 |
* of the symbolic link given by 'pathPtr', or NULL if the symbolic link |
| 3706 |
* could not be read. The result is owned by the caller, which should |
| 3707 |
* call Tcl_DecrRefCount when the result is no longer needed. |
| 3708 |
* |
| 3709 |
* If toPtr is non-NULL, then the result is toPtr if the link action was |
| 3710 |
* successful, or NULL if not. In this case the result has no additional |
| 3711 |
* reference count, and need not be freed. The actual action to perform |
| 3712 |
* is given by the 'linkAction' flags, which is an or'd combination of: |
| 3713 |
* |
| 3714 |
* TCL_CREATE_SYMBOLIC_LINK |
| 3715 |
* TCL_CREATE_HARD_LINK |
| 3716 |
* |
| 3717 |
* Note that most filesystems will not support linking across to |
| 3718 |
* different filesystems, so this function will usually fail unless toPtr |
| 3719 |
* is in the same FS as pathPtr. |
| 3720 |
* |
| 3721 |
* Side effects: |
| 3722 |
* See readlink() documentation. A new filesystem link object may appear. |
| 3723 |
* |
| 3724 |
*--------------------------------------------------------------------------- |
| 3725 |
*/ |
| 3726 |
|
| 3727 |
Tcl_Obj * |
| 3728 |
Tcl_FSLink( |
| 3729 |
Tcl_Obj *pathPtr, /* Path of file to readlink or link. */ |
| 3730 |
Tcl_Obj *toPtr, /* NULL or path to be linked to. */ |
| 3731 |
int linkAction) /* Action to perform. */ |
| 3732 |
{ |
| 3733 |
const Tcl_Filesystem *fsPtr = Tcl_FSGetFileSystemForPath(pathPtr); |
| 3734 |
|
| 3735 |
if (fsPtr != NULL && fsPtr->linkProc != NULL) { |
| 3736 |
return fsPtr->linkProc(pathPtr, toPtr, linkAction); |
| 3737 |
} |
| 3738 |
|
| 3739 |
/* |
| 3740 |
* If S_IFLNK isn't defined it means that the machine doesn't support |
| 3741 |
* symbolic links, so the file can't possibly be a symbolic link. Generate |
| 3742 |
* an EINVAL error, which is what happens on machines that do support |
| 3743 |
* symbolic links when you invoke readlink on a file that isn't a symbolic |
| 3744 |
* link. |
| 3745 |
*/ |
| 3746 |
|
| 3747 |
#ifndef S_IFLNK |
| 3748 |
errno = EINVAL; /* TODO: Change to Tcl_SetErrno()? */ |
| 3749 |
#else |
| 3750 |
Tcl_SetErrno(ENOENT); |
| 3751 |
#endif /* S_IFLNK */ |
| 3752 |
return NULL; |
| 3753 |
} |
| 3754 |
|
| 3755 |
/* |
| 3756 |
*--------------------------------------------------------------------------- |
| 3757 |
* |
| 3758 |
* Tcl_FSListVolumes -- |
| 3759 |
* |
| 3760 |
* Lists the currently mounted volumes. The chain of functions that have |
| 3761 |
* been "inserted" into the filesystem will be called in succession; each |
| 3762 |
* may return a list of volumes, all of which are added to the result |
| 3763 |
* until all mounted file systems are listed. |
| 3764 |
* |
| 3765 |
* Notice that we assume the lists returned by each filesystem (if non |
| 3766 |
* NULL) have been given a refCount for us already. However, we are NOT |
| 3767 |
* allowed to hang on to the list itself (it belongs to the filesystem we |
| 3768 |
* called). Therefore we quite naturally add its contents to the result |
| 3769 |
* we are building, and then decrement the refCount. |
| 3770 |
* |
| 3771 |
* Results: |
| 3772 |
* The list of volumes, in an object which has refCount 0. |
| 3773 |
* |
| 3774 |
* Side effects: |
| 3775 |
* None |
| 3776 |
* |
| 3777 |
*--------------------------------------------------------------------------- |
| 3778 |
*/ |
| 3779 |
|
| 3780 |
Tcl_Obj * |
| 3781 |
Tcl_FSListVolumes(void) |
| 3782 |
{ |
| 3783 |
FilesystemRecord *fsRecPtr; |
| 3784 |
Tcl_Obj *resultPtr; |
| 3785 |
|
| 3786 |
TclNewObj(resultPtr); |
| 3787 |
/* |
| 3788 |
* Call each of the "listVolumes" function in succession. A non-NULL |
| 3789 |
* return value indicates the particular function has succeeded. We call |
| 3790 |
* all the functions registered, since we want a list of all drives from |
| 3791 |
* all filesystems. |
| 3792 |
*/ |
| 3793 |
|
| 3794 |
fsRecPtr = FsGetFirstFilesystem(); |
| 3795 |
Claim(); |
| 3796 |
while (fsRecPtr != NULL) { |
| 3797 |
if (fsRecPtr->fsPtr->listVolumesProc != NULL) { |
| 3798 |
Tcl_Obj *thisFsVolumes = fsRecPtr->fsPtr->listVolumesProc(); |
| 3799 |
|
| 3800 |
if (thisFsVolumes != NULL) { |
| 3801 |
Tcl_ListObjAppendList(NULL, resultPtr, thisFsVolumes); |
| 3802 |
Tcl_DecrRefCount(thisFsVolumes); |
| 3803 |
} |
| 3804 |
} |
| 3805 |
fsRecPtr = fsRecPtr->nextPtr; |
| 3806 |
} |
| 3807 |
Disclaim(); |
| 3808 |
|
| 3809 |
return resultPtr; |
| 3810 |
} |
| 3811 |
|
| 3812 |
/* |
| 3813 |
*--------------------------------------------------------------------------- |
| 3814 |
* |
| 3815 |
* FsListMounts -- |
| 3816 |
* |
| 3817 |
* List all mounts within the given directory, which match the given |
| 3818 |
* pattern. |
| 3819 |
* |
| 3820 |
* Results: |
| 3821 |
* The list of mounts, in a list object which has refCount 0, or NULL if |
| 3822 |
* we didn't even find any filesystems to try to list mounts. |
| 3823 |
* |
| 3824 |
* Side effects: |
| 3825 |
* None |
| 3826 |
* |
| 3827 |
*--------------------------------------------------------------------------- |
| 3828 |
*/ |
| 3829 |
|
| 3830 |
static Tcl_Obj * |
| 3831 |
FsListMounts( |
| 3832 |
Tcl_Obj *pathPtr, /* Contains path to directory to search. */ |
| 3833 |
const char *pattern) /* Pattern to match against. */ |
| 3834 |
{ |
| 3835 |
FilesystemRecord *fsRecPtr; |
| 3836 |
Tcl_GlobTypeData mountsOnly = { TCL_GLOB_TYPE_MOUNT, 0, NULL, NULL }; |
| 3837 |
Tcl_Obj *resultPtr = NULL; |
| 3838 |
|
| 3839 |
/* |
| 3840 |
* Call each of the "matchInDirectory" functions in succession, with the |
| 3841 |
* specific type information 'mountsOnly'. A non-NULL return value |
| 3842 |
* indicates the particular function has succeeded. We call all the |
| 3843 |
* functions registered, since we want a list from each filesystems. |
| 3844 |
*/ |
| 3845 |
|
| 3846 |
fsRecPtr = FsGetFirstFilesystem(); |
| 3847 |
Claim(); |
| 3848 |
while (fsRecPtr != NULL) { |
| 3849 |
if (fsRecPtr->fsPtr != &tclNativeFilesystem && |
| 3850 |
fsRecPtr->fsPtr->matchInDirectoryProc != NULL) { |
| 3851 |
if (resultPtr == NULL) { |
| 3852 |
TclNewObj(resultPtr); |
| 3853 |
} |
| 3854 |
fsRecPtr->fsPtr->matchInDirectoryProc(NULL, resultPtr, pathPtr, |
| 3855 |
pattern, &mountsOnly); |
| 3856 |
} |
| 3857 |
fsRecPtr = fsRecPtr->nextPtr; |
| 3858 |
} |
| 3859 |
Disclaim(); |
| 3860 |
|
| 3861 |
return resultPtr; |
| 3862 |
} |
| 3863 |
|
| 3864 |
/* |
| 3865 |
*--------------------------------------------------------------------------- |
| 3866 |
* |
| 3867 |
* Tcl_FSSplitPath -- |
| 3868 |
* |
| 3869 |
* This function takes the given Tcl_Obj, which should be a valid path, |
| 3870 |
* and returns a Tcl List object containing each segment of that path as |
| 3871 |
* an element. |
| 3872 |
* |
| 3873 |
* Results: |
| 3874 |
* Returns list object with refCount of zero. If the passed-in lenPtr is |
| 3875 |
* non-NULL, we use it to return the number of elements in the returned |
| 3876 |
* list. |
| 3877 |
* |
| 3878 |
* Side effects: |
| 3879 |
* None. |
| 3880 |
* |
| 3881 |
*--------------------------------------------------------------------------- |
| 3882 |
*/ |
| 3883 |
|
| 3884 |
Tcl_Obj * |
| 3885 |
Tcl_FSSplitPath( |
| 3886 |
Tcl_Obj *pathPtr, /* Path to split. */ |
| 3887 |
int *lenPtr) /* int to store number of path elements. */ |
| 3888 |
{ |
| 3889 |
Tcl_Obj *result = NULL; /* Needed only to prevent gcc warnings. */ |
| 3890 |
const Tcl_Filesystem *fsPtr; |
| 3891 |
char separator = '/'; |
| 3892 |
int driveNameLength; |
| 3893 |
const char *p; |
| 3894 |
|
| 3895 |
/* |
| 3896 |
* Perform platform specific splitting. |
| 3897 |
*/ |
| 3898 |
|
| 3899 |
if (TclFSGetPathType(pathPtr, &fsPtr, |
| 3900 |
&driveNameLength) == TCL_PATH_ABSOLUTE) { |
| 3901 |
if (fsPtr == &tclNativeFilesystem) { |
| 3902 |
return TclpNativeSplitPath(pathPtr, lenPtr); |
| 3903 |
} |
| 3904 |
} else { |
| 3905 |
return TclpNativeSplitPath(pathPtr, lenPtr); |
| 3906 |
} |
| 3907 |
|
| 3908 |
/* |
| 3909 |
* We assume separators are single characters. |
| 3910 |
*/ |
| 3911 |
|
| 3912 |
if (fsPtr->filesystemSeparatorProc != NULL) { |
| 3913 |
Tcl_Obj *sep = fsPtr->filesystemSeparatorProc(pathPtr); |
| 3914 |
|
| 3915 |
if (sep != NULL) { |
| 3916 |
Tcl_IncrRefCount(sep); |
| 3917 |
separator = TclGetString(sep)[0]; |
| 3918 |
Tcl_DecrRefCount(sep); |
| 3919 |
} |
| 3920 |
} |
| 3921 |
|
| 3922 |
/* |
| 3923 |
* Place the drive name as first element of the result list. The drive |
| 3924 |
* name may contain strange characters, like colons and multiple forward |
| 3925 |
* slashes (for example 'ftp://' is a valid vfs drive name) |
| 3926 |
*/ |
| 3927 |
|
| 3928 |
TclNewObj(result); |
| 3929 |
p = TclGetString(pathPtr); |
| 3930 |
Tcl_ListObjAppendElement(NULL, result, |
| 3931 |
Tcl_NewStringObj(p, driveNameLength)); |
| 3932 |
p += driveNameLength; |
| 3933 |
|
| 3934 |
/* |
| 3935 |
* Add the remaining path elements to the list. |
| 3936 |
*/ |
| 3937 |
|
| 3938 |
for (;;) { |
| 3939 |
const char *elementStart = p; |
| 3940 |
int length; |
| 3941 |
|
| 3942 |
while ((*p != '\0') && (*p != separator)) { |
| 3943 |
p++; |
| 3944 |
} |
| 3945 |
length = p - elementStart; |
| 3946 |
if (length > 0) { |
| 3947 |
Tcl_Obj *nextElt; |
| 3948 |
|
| 3949 |
if (elementStart[0] == '~') { |
| 3950 |
TclNewLiteralStringObj(nextElt, "./"); |
| 3951 |
Tcl_AppendToObj(nextElt, elementStart, length); |
| 3952 |
} else { |
| 3953 |
nextElt = Tcl_NewStringObj(elementStart, length); |
| 3954 |
} |
| 3955 |
Tcl_ListObjAppendElement(NULL, result, nextElt); |
| 3956 |
} |
| 3957 |
if (*p++ == '\0') { |
| 3958 |
break; |
| 3959 |
} |
| 3960 |
} |
| 3961 |
|
| 3962 |
/* |
| 3963 |
* Compute the number of elements in the result. |
| 3964 |
*/ |
| 3965 |
|
| 3966 |
if (lenPtr != NULL) { |
| 3967 |
TclListObjLength(NULL, result, lenPtr); |
| 3968 |
} |
| 3969 |
return result; |
| 3970 |
} |
| 3971 |
/* |
| 3972 |
*---------------------------------------------------------------------- |
| 3973 |
* |
| 3974 |
* TclGetPathType -- |
| 3975 |
* |
| 3976 |
* Helper function used by FSGetPathType. |
| 3977 |
* |
| 3978 |
* Results: |
| 3979 |
* Returns one of TCL_PATH_ABSOLUTE, TCL_PATH_RELATIVE, or |
| 3980 |
* TCL_PATH_VOLUME_RELATIVE. The filesystem reference will be set if and |
| 3981 |
* only if it is non-NULL and the function's return value is |
| 3982 |
* TCL_PATH_ABSOLUTE. |
| 3983 |
* |
| 3984 |
* Side effects: |
| 3985 |
* None. |
| 3986 |
* |
| 3987 |
*---------------------------------------------------------------------- |
| 3988 |
*/ |
| 3989 |
|
| 3990 |
Tcl_PathType |
| 3991 |
TclGetPathType( |
| 3992 |
Tcl_Obj *pathPtr, /* Path to determine type for. */ |
| 3993 |
const Tcl_Filesystem **filesystemPtrPtr, |
| 3994 |
/* If absolute path and this is not NULL, then |
| 3995 |
* set to the filesystem which claims this |
| 3996 |
* path. */ |
| 3997 |
int *driveNameLengthPtr, /* If the path is absolute, and this is |
| 3998 |
* non-NULL, then set to the length of the |
| 3999 |
* driveName. */ |
| 4000 |
Tcl_Obj **driveNameRef) /* If the path is absolute, and this is |
| 4001 |
* non-NULL, then set to the name of the |
| 4002 |
* drive, network-volume which contains the |
| 4003 |
* path, already with a refCount for the |
| 4004 |
* caller. */ |
| 4005 |
{ |
| 4006 |
int pathLen; |
| 4007 |
const char *path = TclGetStringFromObj(pathPtr, &pathLen); |
| 4008 |
Tcl_PathType type; |
| 4009 |
|
| 4010 |
type = TclFSNonnativePathType(path, pathLen, filesystemPtrPtr, |
| 4011 |
driveNameLengthPtr, driveNameRef); |
| 4012 |
|
| 4013 |
if (type != TCL_PATH_ABSOLUTE) { |
| 4014 |
type = TclpGetNativePathType(pathPtr, driveNameLengthPtr, |
| 4015 |
driveNameRef); |
| 4016 |
if ((type == TCL_PATH_ABSOLUTE) && (filesystemPtrPtr != NULL)) { |
| 4017 |
*filesystemPtrPtr = &tclNativeFilesystem; |
| 4018 |
} |
| 4019 |
} |
| 4020 |
return type; |
| 4021 |
} |
| 4022 |
|
| 4023 |
/* |
| 4024 |
*---------------------------------------------------------------------- |
| 4025 |
* |
| 4026 |
* TclFSNonnativePathType -- |
| 4027 |
* |
| 4028 |
* Helper function used by TclGetPathType. Its purpose is to check |
| 4029 |
* whether the given path starts with a string which corresponds to a |
| 4030 |
* file volume in any registered filesystem except the native one. For |
| 4031 |
* speed and historical reasons the native filesystem has special |
| 4032 |
* hard-coded checks dotted here and there in the filesystem code. |
| 4033 |
* |
| 4034 |
* Results: |
| 4035 |
* Returns one of TCL_PATH_ABSOLUTE or TCL_PATH_RELATIVE. The filesystem |
| 4036 |
* reference will be set if and only if it is non-NULL and the function's |
| 4037 |
* return value is TCL_PATH_ABSOLUTE. |
| 4038 |
* |
| 4039 |
* Side effects: |
| 4040 |
* None. |
| 4041 |
* |
| 4042 |
*---------------------------------------------------------------------- |
| 4043 |
*/ |
| 4044 |
|
| 4045 |
Tcl_PathType |
| 4046 |
TclFSNonnativePathType( |
| 4047 |
const char *path, /* Path to determine type for. */ |
| 4048 |
int pathLen, /* Length of the path. */ |
| 4049 |
const Tcl_Filesystem **filesystemPtrPtr, |
| 4050 |
/* If absolute path and this is not NULL, then |
| 4051 |
* set to the filesystem which claims this |
| 4052 |
* path. */ |
| 4053 |
int *driveNameLengthPtr, /* If the path is absolute, and this is |
| 4054 |
* non-NULL, then set to the length of the |
| 4055 |
* driveName. */ |
| 4056 |
Tcl_Obj **driveNameRef) /* If the path is absolute, and this is |
| 4057 |
* non-NULL, then set to the name of the |
| 4058 |
* drive, network-volume which contains the |
| 4059 |
* path, already with a refCount for the |
| 4060 |
* caller. */ |
| 4061 |
{ |
| 4062 |
FilesystemRecord *fsRecPtr; |
| 4063 |
Tcl_PathType type = TCL_PATH_RELATIVE; |
| 4064 |
|
| 4065 |
/* |
| 4066 |
* Call each of the "listVolumes" function in succession, checking whether |
| 4067 |
* the given path is an absolute path on any of the volumes returned (this |
| 4068 |
* is done by checking whether the path's prefix matches). |
| 4069 |
*/ |
| 4070 |
|
| 4071 |
fsRecPtr = FsGetFirstFilesystem(); |
| 4072 |
Claim(); |
| 4073 |
while (fsRecPtr != NULL) { |
| 4074 |
/* |
| 4075 |
* We want to skip the native filesystem in this loop because |
| 4076 |
* otherwise we won't necessarily pass all the Tcl testsuite - this is |
| 4077 |
* because some of the tests artificially change the current platform |
| 4078 |
* (between Win, Unix) but the list of volumes we get by calling |
| 4079 |
* fsRecPtr->fsPtr->listVolumesProc will reflect the current (real) |
| 4080 |
* platform only and this may cause some tests to fail. In particular, |
| 4081 |
* on Unix '/' will match the beginning of certain absolute Windows |
| 4082 |
* paths starting '//' and those tests will go wrong. |
| 4083 |
* |
| 4084 |
* Besides these test-suite issues, there is one other reason to skip |
| 4085 |
* the native filesystem - since the tclFilename.c code has nice fast |
| 4086 |
* 'absolute path' checkers, we don't want to waste time repeating |
| 4087 |
* that effort here, and this function is actually called quite often, |
| 4088 |
* so if we can save the overhead of the native filesystem returning |
| 4089 |
* us a list of volumes all the time, it is better. |
| 4090 |
*/ |
| 4091 |
|
| 4092 |
if ((fsRecPtr->fsPtr != &tclNativeFilesystem) |
| 4093 |
&& (fsRecPtr->fsPtr->listVolumesProc != NULL)) { |
| 4094 |
int numVolumes; |
| 4095 |
Tcl_Obj *thisFsVolumes = fsRecPtr->fsPtr->listVolumesProc(); |
| 4096 |
|
| 4097 |
if (thisFsVolumes != NULL) { |
| 4098 |
if (TclListObjLength(NULL, thisFsVolumes, &numVolumes) |
| 4099 |
!= TCL_OK) { |
| 4100 |
/* |
| 4101 |
* This is VERY bad; the listVolumesProc didn't return a |
| 4102 |
* valid list. Set numVolumes to -1 so that we skip the |
| 4103 |
* while loop below and just return with the current value |
| 4104 |
* of 'type'. |
| 4105 |
* |
| 4106 |
* It would be better if we could signal an error here |
| 4107 |
* (but Tcl_Panic seems a bit excessive). |
| 4108 |
*/ |
| 4109 |
|
| 4110 |
numVolumes = -1; |
| 4111 |
} |
| 4112 |
while (numVolumes > 0) { |
| 4113 |
Tcl_Obj *vol; |
| 4114 |
int len; |
| 4115 |
const char *strVol; |
| 4116 |
|
| 4117 |
numVolumes--; |
| 4118 |
Tcl_ListObjIndex(NULL, thisFsVolumes, numVolumes, &vol); |
| 4119 |
strVol = TclGetStringFromObj(vol,&len); |
| 4120 |
if (pathLen < len) { |
| 4121 |
continue; |
| 4122 |
} |
| 4123 |
if (strncmp(strVol, path, len) == 0) { |
| 4124 |
type = TCL_PATH_ABSOLUTE; |
| 4125 |
if (filesystemPtrPtr != NULL) { |
| 4126 |
*filesystemPtrPtr = fsRecPtr->fsPtr; |
| 4127 |
} |
| 4128 |
if (driveNameLengthPtr != NULL) { |
| 4129 |
*driveNameLengthPtr = len; |
| 4130 |
} |
| 4131 |
if (driveNameRef != NULL) { |
| 4132 |
*driveNameRef = vol; |
| 4133 |
Tcl_IncrRefCount(vol); |
| 4134 |
} |
| 4135 |
break; |
| 4136 |
} |
| 4137 |
} |
| 4138 |
Tcl_DecrRefCount(thisFsVolumes); |
| 4139 |
if (type == TCL_PATH_ABSOLUTE) { |
| 4140 |
/* |
| 4141 |
* We don't need to examine any more filesystems. |
| 4142 |
*/ |
| 4143 |
|
| 4144 |
break; |
| 4145 |
} |
| 4146 |
} |
| 4147 |
} |
| 4148 |
fsRecPtr = fsRecPtr->nextPtr; |
| 4149 |
} |
| 4150 |
Disclaim(); |
| 4151 |
return type; |
| 4152 |
} |
| 4153 |
|
| 4154 |
/* |
| 4155 |
*--------------------------------------------------------------------------- |
| 4156 |
* |
| 4157 |
* Tcl_FSRenameFile -- |
| 4158 |
* |
| 4159 |
* If the two paths given belong to the same filesystem, we call that |
| 4160 |
* filesystems rename function. Otherwise we simply return the POSIX |
| 4161 |
* error 'EXDEV', and -1. |
| 4162 |
* |
| 4163 |
* Results: |
| 4164 |
* Standard Tcl error code if a function was called. |
| 4165 |
* |
| 4166 |
* Side effects: |
| 4167 |
* A file may be renamed. |
| 4168 |
* |
| 4169 |
*--------------------------------------------------------------------------- |
| 4170 |
*/ |
| 4171 |
|
| 4172 |
int |
| 4173 |
Tcl_FSRenameFile( |
| 4174 |
Tcl_Obj *srcPathPtr, /* Pathname of file or dir to be renamed |
| 4175 |
* (UTF-8). */ |
| 4176 |
Tcl_Obj *destPathPtr) /* New pathname of file or directory |
| 4177 |
* (UTF-8). */ |
| 4178 |
{ |
| 4179 |
int retVal = -1; |
| 4180 |
const Tcl_Filesystem *fsPtr, *fsPtr2; |
| 4181 |
|
| 4182 |
fsPtr = Tcl_FSGetFileSystemForPath(srcPathPtr); |
| 4183 |
fsPtr2 = Tcl_FSGetFileSystemForPath(destPathPtr); |
| 4184 |
|
| 4185 |
if ((fsPtr == fsPtr2) && (fsPtr != NULL) |
| 4186 |
&& (fsPtr->renameFileProc != NULL)) { |
| 4187 |
retVal = fsPtr->renameFileProc(srcPathPtr, destPathPtr); |
| 4188 |
} |
| 4189 |
if (retVal == -1) { |
| 4190 |
Tcl_SetErrno(EXDEV); |
| 4191 |
} |
| 4192 |
return retVal; |
| 4193 |
} |
| 4194 |
|
| 4195 |
/* |
| 4196 |
*--------------------------------------------------------------------------- |
| 4197 |
* |
| 4198 |
* Tcl_FSCopyFile -- |
| 4199 |
* |
| 4200 |
* If the two paths given belong to the same filesystem, we call that |
| 4201 |
* filesystem's copy function. Otherwise we simply return the POSIX error |
| 4202 |
* 'EXDEV', and -1. |
| 4203 |
* |
| 4204 |
* Note that in the native filesystems, 'copyFileProc' is defined to copy |
| 4205 |
* soft links (i.e. it copies the links themselves, not the things they |
| 4206 |
* point to). |
| 4207 |
* |
| 4208 |
* Results: |
| 4209 |
* Standard Tcl error code if a function was called. |
| 4210 |
* |
| 4211 |
* Side effects: |
| 4212 |
* A file may be copied. |
| 4213 |
* |
| 4214 |
*--------------------------------------------------------------------------- |
| 4215 |
*/ |
| 4216 |
|
| 4217 |
int |
| 4218 |
Tcl_FSCopyFile( |
| 4219 |
Tcl_Obj *srcPathPtr, /* Pathname of file to be copied (UTF-8). */ |
| 4220 |
Tcl_Obj *destPathPtr) /* Pathname of file to copy to (UTF-8). */ |
| 4221 |
{ |
| 4222 |
int retVal = -1; |
| 4223 |
const Tcl_Filesystem *fsPtr, *fsPtr2; |
| 4224 |
|
| 4225 |
fsPtr = Tcl_FSGetFileSystemForPath(srcPathPtr); |
| 4226 |
fsPtr2 = Tcl_FSGetFileSystemForPath(destPathPtr); |
| 4227 |
|
| 4228 |
if (fsPtr == fsPtr2 && fsPtr != NULL && fsPtr->copyFileProc != NULL) { |
| 4229 |
retVal = fsPtr->copyFileProc(srcPathPtr, destPathPtr); |
| 4230 |
} |
| 4231 |
if (retVal == -1) { |
| 4232 |
Tcl_SetErrno(EXDEV); |
| 4233 |
} |
| 4234 |
return retVal; |
| 4235 |
} |
| 4236 |
|
| 4237 |
/* |
| 4238 |
*--------------------------------------------------------------------------- |
| 4239 |
* |
| 4240 |
* TclCrossFilesystemCopy -- |
| 4241 |
* |
| 4242 |
* Helper for above function, and for Tcl_FSLoadFile, to copy files from |
| 4243 |
* one filesystem to another. This function will overwrite the target |
| 4244 |
* file if it already exists. |
| 4245 |
* |
| 4246 |
* Results: |
| 4247 |
* Standard Tcl error code. |
| 4248 |
* |
| 4249 |
* Side effects: |
| 4250 |
* A file may be created. |
| 4251 |
* |
| 4252 |
*--------------------------------------------------------------------------- |
| 4253 |
*/ |
| 4254 |
|
| 4255 |
int |
| 4256 |
TclCrossFilesystemCopy( |
| 4257 |
Tcl_Interp *interp, /* For error messages. */ |
| 4258 |
Tcl_Obj *source, /* Pathname of file to be copied (UTF-8). */ |
| 4259 |
Tcl_Obj *target) /* Pathname of file to copy to (UTF-8). */ |
| 4260 |
{ |
| 4261 |
int result = TCL_ERROR; |
| 4262 |
int prot = 0666; |
| 4263 |
Tcl_Channel in, out; |
| 4264 |
Tcl_StatBuf sourceStatBuf; |
| 4265 |
struct utimbuf tval; |
| 4266 |
|
| 4267 |
out = Tcl_FSOpenFileChannel(interp, target, "wb", prot); |
| 4268 |
if (out == NULL) { |
| 4269 |
/* |
| 4270 |
* It looks like we cannot copy it over. Bail out... |
| 4271 |
*/ |
| 4272 |
goto done; |
| 4273 |
} |
| 4274 |
|
| 4275 |
in = Tcl_FSOpenFileChannel(interp, source, "rb", prot); |
| 4276 |
if (in == NULL) { |
| 4277 |
/* |
| 4278 |
* This is very strange, caller should have checked this... |
| 4279 |
*/ |
| 4280 |
|
| 4281 |
Tcl_Close(interp, out); |
| 4282 |
goto done; |
| 4283 |
} |
| 4284 |
|
| 4285 |
/* |
| 4286 |
* Copy it synchronously. We might wish to add an asynchronous option to |
| 4287 |
* support vfs's which are slow (e.g. network sockets). |
| 4288 |
*/ |
| 4289 |
|
| 4290 |
if (TclCopyChannel(interp, in, out, -1, NULL) == TCL_OK) { |
| 4291 |
result = TCL_OK; |
| 4292 |
} |
| 4293 |
|
| 4294 |
/* |
| 4295 |
* If the copy failed, assume that copy channel left a good error message. |
| 4296 |
*/ |
| 4297 |
|
| 4298 |
Tcl_Close(interp, in); |
| 4299 |
Tcl_Close(interp, out); |
| 4300 |
|
| 4301 |
/* |
| 4302 |
* Set modification date of copied file. |
| 4303 |
*/ |
| 4304 |
|
| 4305 |
if (Tcl_FSLstat(source, &sourceStatBuf) == 0) { |
| 4306 |
tval.actime = Tcl_GetAccessTimeFromStat(&sourceStatBuf); |
| 4307 |
tval.modtime = Tcl_GetModificationTimeFromStat(&sourceStatBuf); |
| 4308 |
Tcl_FSUtime(target, &tval); |
| 4309 |
} |
| 4310 |
|
| 4311 |
done: |
| 4312 |
return result; |
| 4313 |
} |
| 4314 |
|
| 4315 |
/* |
| 4316 |
*--------------------------------------------------------------------------- |
| 4317 |
* |
| 4318 |
* Tcl_FSDeleteFile -- |
| 4319 |
* |
| 4320 |
* The appropriate function for the filesystem to which pathPtr belongs |
| 4321 |
* will be called. |
| 4322 |
* |
| 4323 |
* Results: |
| 4324 |
* Standard Tcl error code. |
| 4325 |
* |
| 4326 |
* Side effects: |
| 4327 |
* A file may be deleted. |
| 4328 |
* |
| 4329 |
*--------------------------------------------------------------------------- |
| 4330 |
*/ |
| 4331 |
|
| 4332 |
int |
| 4333 |
Tcl_FSDeleteFile( |
| 4334 |
Tcl_Obj *pathPtr) /* Pathname of file to be removed (UTF-8). */ |
| 4335 |
{ |
| 4336 |
const Tcl_Filesystem *fsPtr = Tcl_FSGetFileSystemForPath(pathPtr); |
| 4337 |
|
| 4338 |
if (fsPtr != NULL && fsPtr->deleteFileProc != NULL) { |
| 4339 |
return fsPtr->deleteFileProc(pathPtr); |
| 4340 |
} |
| 4341 |
Tcl_SetErrno(ENOENT); |
| 4342 |
return -1; |
| 4343 |
} |
| 4344 |
|
| 4345 |
/* |
| 4346 |
*--------------------------------------------------------------------------- |
| 4347 |
* |
| 4348 |
* Tcl_FSCreateDirectory -- |
| 4349 |
* |
| 4350 |
* The appropriate function for the filesystem to which pathPtr belongs |
| 4351 |
* will be called. |
| 4352 |
* |
| 4353 |
* Results: |
| 4354 |
* Standard Tcl error code. |
| 4355 |
* |
| 4356 |
* Side effects: |
| 4357 |
* A directory may be created. |
| 4358 |
* |
| 4359 |
*--------------------------------------------------------------------------- |
| 4360 |
*/ |
| 4361 |
|
| 4362 |
int |
| 4363 |
Tcl_FSCreateDirectory( |
| 4364 |
Tcl_Obj *pathPtr) /* Pathname of directory to create (UTF-8). */ |
| 4365 |
{ |
| 4366 |
const Tcl_Filesystem *fsPtr = Tcl_FSGetFileSystemForPath(pathPtr); |
| 4367 |
|
| 4368 |
if (fsPtr != NULL && fsPtr->createDirectoryProc != NULL) { |
| 4369 |
return fsPtr->createDirectoryProc(pathPtr); |
| 4370 |
} |
| 4371 |
Tcl_SetErrno(ENOENT); |
| 4372 |
return -1; |
| 4373 |
} |
| 4374 |
|
| 4375 |
/* |
| 4376 |
*--------------------------------------------------------------------------- |
| 4377 |
* |
| 4378 |
* Tcl_FSCopyDirectory -- |
| 4379 |
* |
| 4380 |
* If the two paths given belong to the same filesystem, we call that |
| 4381 |
* filesystems copy-directory function. Otherwise we simply return the |
| 4382 |
* POSIX error 'EXDEV', and -1. |
| 4383 |
* |
| 4384 |
* Results: |
| 4385 |
* Standard Tcl error code if a function was called. |
| 4386 |
* |
| 4387 |
* Side effects: |
| 4388 |
* A directory may be copied. |
| 4389 |
* |
| 4390 |
*--------------------------------------------------------------------------- |
| 4391 |
*/ |
| 4392 |
|
| 4393 |
int |
| 4394 |
Tcl_FSCopyDirectory( |
| 4395 |
Tcl_Obj *srcPathPtr, /* Pathname of directory to be copied |
| 4396 |
* (UTF-8). */ |
| 4397 |
Tcl_Obj *destPathPtr, /* Pathname of target directory (UTF-8). */ |
| 4398 |
Tcl_Obj **errorPtr) /* If non-NULL, then will be set to a new |
| 4399 |
* object containing name of file causing |
| 4400 |
* error, with refCount 1. */ |
| 4401 |
{ |
| 4402 |
int retVal = -1; |
| 4403 |
const Tcl_Filesystem *fsPtr, *fsPtr2; |
| 4404 |
|
| 4405 |
fsPtr = Tcl_FSGetFileSystemForPath(srcPathPtr); |
| 4406 |
fsPtr2 = Tcl_FSGetFileSystemForPath(destPathPtr); |
| 4407 |
|
| 4408 |
if (fsPtr == fsPtr2 && fsPtr != NULL && fsPtr->copyDirectoryProc != NULL){ |
| 4409 |
retVal = fsPtr->copyDirectoryProc(srcPathPtr, destPathPtr, errorPtr); |
| 4410 |
} |
| 4411 |
if (retVal == -1) { |
| 4412 |
Tcl_SetErrno(EXDEV); |
| 4413 |
} |
| 4414 |
return retVal; |
| 4415 |
} |
| 4416 |
|
| 4417 |
/* |
| 4418 |
*--------------------------------------------------------------------------- |
| 4419 |
* |
| 4420 |
* Tcl_FSRemoveDirectory -- |
| 4421 |
* |
| 4422 |
* The appropriate function for the filesystem to which pathPtr belongs |
| 4423 |
* will be called. |
| 4424 |
* |
| 4425 |
* Results: |
| 4426 |
* Standard Tcl error code. |
| 4427 |
* |
| 4428 |
* Side effects: |
| 4429 |
* A directory may be deleted. |
| 4430 |
* |
| 4431 |
*--------------------------------------------------------------------------- |
| 4432 |
*/ |
| 4433 |
|
| 4434 |
int |
| 4435 |
Tcl_FSRemoveDirectory( |
| 4436 |
Tcl_Obj *pathPtr, /* Pathname of directory to be removed |
| 4437 |
* (UTF-8). */ |
| 4438 |
int recursive, /* If non-zero, removes directories that are |
| 4439 |
* nonempty. Otherwise, will only remove empty |
| 4440 |
* directories. */ |
| 4441 |
Tcl_Obj **errorPtr) /* If non-NULL, then will be set to a new |
| 4442 |
* object containing name of file causing |
| 4443 |
* error, with refCount 1. */ |
| 4444 |
{ |
| 4445 |
const Tcl_Filesystem *fsPtr = Tcl_FSGetFileSystemForPath(pathPtr); |
| 4446 |
|
| 4447 |
if (fsPtr == NULL || fsPtr->removeDirectoryProc == NULL) { |
| 4448 |
Tcl_SetErrno(ENOENT); |
| 4449 |
return -1; |
| 4450 |
} |
| 4451 |
|
| 4452 |
/* |
| 4453 |
* When working recursively, we check whether the cwd lies inside this |
| 4454 |
* directory and move it if it does. |
| 4455 |
*/ |
| 4456 |
|
| 4457 |
if (recursive) { |
| 4458 |
Tcl_Obj *cwdPtr = Tcl_FSGetCwd(NULL); |
| 4459 |
|
| 4460 |
if (cwdPtr != NULL) { |
| 4461 |
const char *cwdStr, *normPathStr; |
| 4462 |
int cwdLen, normLen; |
| 4463 |
Tcl_Obj *normPath = Tcl_FSGetNormalizedPath(NULL, pathPtr); |
| 4464 |
|
| 4465 |
if (normPath != NULL) { |
| 4466 |
normPathStr = TclGetStringFromObj(normPath, &normLen); |
| 4467 |
cwdStr = TclGetStringFromObj(cwdPtr, &cwdLen); |
| 4468 |
if ((cwdLen >= normLen) && (strncmp(normPathStr, cwdStr, |
| 4469 |
(size_t) normLen) == 0)) { |
| 4470 |
/* |
| 4471 |
* The cwd is inside the directory, so we perform a 'cd |
| 4472 |
* [file dirname $path]'. |
| 4473 |
*/ |
| 4474 |
|
| 4475 |
Tcl_Obj *dirPtr = TclPathPart(NULL, pathPtr, |
| 4476 |
TCL_PATH_DIRNAME); |
| 4477 |
|
| 4478 |
Tcl_FSChdir(dirPtr); |
| 4479 |
Tcl_DecrRefCount(dirPtr); |
| 4480 |
} |
| 4481 |
} |
| 4482 |
Tcl_DecrRefCount(cwdPtr); |
| 4483 |
} |
| 4484 |
} |
| 4485 |
return fsPtr->removeDirectoryProc(pathPtr, recursive, errorPtr); |
| 4486 |
} |
| 4487 |
|
| 4488 |
/* |
| 4489 |
*--------------------------------------------------------------------------- |
| 4490 |
* |
| 4491 |
* Tcl_FSGetFileSystemForPath -- |
| 4492 |
* |
| 4493 |
* This function determines which filesystem to use for a particular path |
| 4494 |
* object, and returns the filesystem which accepts this file. If no |
| 4495 |
* filesystem will accept this object as a valid file path, then NULL is |
| 4496 |
* returned. |
| 4497 |
* |
| 4498 |
* Results: |
| 4499 |
* NULL or a filesystem which will accept this path. |
| 4500 |
* |
| 4501 |
* Side effects: |
| 4502 |
* The object may be converted to a path type. |
| 4503 |
* |
| 4504 |
*--------------------------------------------------------------------------- |
| 4505 |
*/ |
| 4506 |
|
| 4507 |
const Tcl_Filesystem * |
| 4508 |
Tcl_FSGetFileSystemForPath( |
| 4509 |
Tcl_Obj *pathPtr) |
| 4510 |
{ |
| 4511 |
FilesystemRecord *fsRecPtr; |
| 4512 |
const Tcl_Filesystem *retVal = NULL; |
| 4513 |
|
| 4514 |
if (pathPtr == NULL) { |
| 4515 |
Tcl_Panic("Tcl_FSGetFileSystemForPath called with NULL object"); |
| 4516 |
return NULL; |
| 4517 |
} |
| 4518 |
|
| 4519 |
/* |
| 4520 |
* If the object has a refCount of zero, we reject it. This is to avoid |
| 4521 |
* possible segfaults or nondeterministic memory leaks (i.e. the user |
| 4522 |
* doesn't know if they should decrement the ref count on return or not). |
| 4523 |
*/ |
| 4524 |
|
| 4525 |
if (pathPtr->refCount == 0) { |
| 4526 |
Tcl_Panic("Tcl_FSGetFileSystemForPath called with object with refCount == 0"); |
| 4527 |
return NULL; |
| 4528 |
} |
| 4529 |
|
| 4530 |
/* |
| 4531 |
* Check if the filesystem has changed in some way since this object's |
| 4532 |
* internal representation was calculated. Before doing that, assure we |
| 4533 |
* have the most up-to-date copy of the first filesystem. This is |
| 4534 |
* accomplished by the FsGetFirstFilesystem() call. |
| 4535 |
*/ |
| 4536 |
|
| 4537 |
fsRecPtr = FsGetFirstFilesystem(); |
| 4538 |
Claim(); |
| 4539 |
|
| 4540 |
if (TclFSEnsureEpochOk(pathPtr, &retVal) != TCL_OK) { |
| 4541 |
Disclaim(); |
| 4542 |
return NULL; |
| 4543 |
} else if (retVal != NULL) { |
| 4544 |
/* TODO: Can this happen? */ |
| 4545 |
Disclaim(); |
| 4546 |
return retVal; |
| 4547 |
} |
| 4548 |
|
| 4549 |
/* |
| 4550 |
* Call each of the "pathInFilesystem" functions in succession. A |
| 4551 |
* non-return value of -1 indicates the particular function has succeeded. |
| 4552 |
*/ |
| 4553 |
|
| 4554 |
for (; fsRecPtr!=NULL ; fsRecPtr=fsRecPtr->nextPtr) { |
| 4555 |
ClientData clientData = NULL; |
| 4556 |
|
| 4557 |
if (fsRecPtr->fsPtr->pathInFilesystemProc == NULL) { |
| 4558 |
continue; |
| 4559 |
} |
| 4560 |
|
| 4561 |
if (fsRecPtr->fsPtr->pathInFilesystemProc(pathPtr, &clientData)!=-1) { |
| 4562 |
/* |
| 4563 |
* We assume the type of pathPtr hasn't been changed by the above |
| 4564 |
* call to the pathInFilesystemProc. |
| 4565 |
*/ |
| 4566 |
|
| 4567 |
TclFSSetPathDetails(pathPtr, fsRecPtr->fsPtr, clientData); |
| 4568 |
Disclaim(); |
| 4569 |
return fsRecPtr->fsPtr; |
| 4570 |
} |
| 4571 |
} |
| 4572 |
Disclaim(); |
| 4573 |
|
| 4574 |
return NULL; |
| 4575 |
} |
| 4576 |
|
| 4577 |
/* |
| 4578 |
*--------------------------------------------------------------------------- |
| 4579 |
* |
| 4580 |
* Tcl_FSGetNativePath -- |
| 4581 |
* |
| 4582 |
* This function is for use by the Win/Unix native filesystems, so that |
| 4583 |
* they can easily retrieve the native (char* or WCHAR*) representation |
| 4584 |
* of a path. Other filesystems will probably want to implement similar |
| 4585 |
* functions. They basically act as a safety net around |
| 4586 |
* Tcl_FSGetInternalRep. Normally your file-system functions will always |
| 4587 |
* be called with path objects already converted to the correct |
| 4588 |
* filesystem, but if for some reason they are called directly (i.e. by |
| 4589 |
* functions not in this file), then one cannot necessarily guarantee |
| 4590 |
* that the path object pointer is from the correct filesystem. |
| 4591 |
* |
| 4592 |
* Note: in the future it might be desirable to have separate versions |
| 4593 |
* of this function with different signatures, for example |
| 4594 |
* Tcl_FSGetNativeWinPath, Tcl_FSGetNativeUnixPath etc. Right now, since |
| 4595 |
* native paths are all string based, we use just one function. |
| 4596 |
* |
| 4597 |
* Results: |
| 4598 |
* NULL or a valid native path. |
| 4599 |
* |
| 4600 |
* Side effects: |
| 4601 |
* See Tcl_FSGetInternalRep. |
| 4602 |
* |
| 4603 |
*--------------------------------------------------------------------------- |
| 4604 |
*/ |
| 4605 |
|
| 4606 |
const void * |
| 4607 |
Tcl_FSGetNativePath( |
| 4608 |
Tcl_Obj *pathPtr) |
| 4609 |
{ |
| 4610 |
return Tcl_FSGetInternalRep(pathPtr, &tclNativeFilesystem); |
| 4611 |
} |
| 4612 |
|
| 4613 |
/* |
| 4614 |
*--------------------------------------------------------------------------- |
| 4615 |
* |
| 4616 |
* NativeFreeInternalRep -- |
| 4617 |
* |
| 4618 |
* Free a native internal representation, which will be non-NULL. |
| 4619 |
* |
| 4620 |
* Results: |
| 4621 |
* None. |
| 4622 |
* |
| 4623 |
* Side effects: |
| 4624 |
* Memory is released. |
| 4625 |
* |
| 4626 |
*--------------------------------------------------------------------------- |
| 4627 |
*/ |
| 4628 |
|
| 4629 |
static void |
| 4630 |
NativeFreeInternalRep( |
| 4631 |
ClientData clientData) |
| 4632 |
{ |
| 4633 |
ckfree(clientData); |
| 4634 |
} |
| 4635 |
|
| 4636 |
/* |
| 4637 |
*--------------------------------------------------------------------------- |
| 4638 |
* |
| 4639 |
* Tcl_FSFileSystemInfo -- |
| 4640 |
* |
| 4641 |
* This function returns a list of two elements. The first element is the |
| 4642 |
* name of the filesystem (e.g. "native" or "vfs"), and the second is the |
| 4643 |
* particular type of the given path within that filesystem. |
| 4644 |
* |
| 4645 |
* Results: |
| 4646 |
* A list of two elements. |
| 4647 |
* |
| 4648 |
* Side effects: |
| 4649 |
* The object may be converted to a path type. |
| 4650 |
* |
| 4651 |
*--------------------------------------------------------------------------- |
| 4652 |
*/ |
| 4653 |
|
| 4654 |
Tcl_Obj * |
| 4655 |
Tcl_FSFileSystemInfo( |
| 4656 |
Tcl_Obj *pathPtr) |
| 4657 |
{ |
| 4658 |
Tcl_Obj *resPtr; |
| 4659 |
const Tcl_Filesystem *fsPtr = Tcl_FSGetFileSystemForPath(pathPtr); |
| 4660 |
|
| 4661 |
if (fsPtr == NULL) { |
| 4662 |
return NULL; |
| 4663 |
} |
| 4664 |
|
| 4665 |
resPtr = Tcl_NewListObj(0, NULL); |
| 4666 |
Tcl_ListObjAppendElement(NULL, resPtr, |
| 4667 |
Tcl_NewStringObj(fsPtr->typeName, -1)); |
| 4668 |
|
| 4669 |
if (fsPtr->filesystemPathTypeProc != NULL) { |
| 4670 |
Tcl_Obj *typePtr = fsPtr->filesystemPathTypeProc(pathPtr); |
| 4671 |
|
| 4672 |
if (typePtr != NULL) { |
| 4673 |
Tcl_ListObjAppendElement(NULL, resPtr, typePtr); |
| 4674 |
} |
| 4675 |
} |
| 4676 |
|
| 4677 |
return resPtr; |
| 4678 |
} |
| 4679 |
|
| 4680 |
/* |
| 4681 |
*--------------------------------------------------------------------------- |
| 4682 |
* |
| 4683 |
* Tcl_FSPathSeparator -- |
| 4684 |
* |
| 4685 |
* This function returns the separator to be used for a given path. The |
| 4686 |
* object returned should have a refCount of zero |
| 4687 |
* |
| 4688 |
* Results: |
| 4689 |
* A Tcl object, with a refCount of zero. If the caller needs to retain a |
| 4690 |
* reference to the object, it should call Tcl_IncrRefCount, and should |
| 4691 |
* otherwise free the object. |
| 4692 |
* |
| 4693 |
* Side effects: |
| 4694 |
* The path object may be converted to a path type. |
| 4695 |
* |
| 4696 |
*--------------------------------------------------------------------------- |
| 4697 |
*/ |
| 4698 |
|
| 4699 |
Tcl_Obj * |
| 4700 |
Tcl_FSPathSeparator( |
| 4701 |
Tcl_Obj *pathPtr) |
| 4702 |
{ |
| 4703 |
const Tcl_Filesystem *fsPtr = Tcl_FSGetFileSystemForPath(pathPtr); |
| 4704 |
Tcl_Obj *resultObj; |
| 4705 |
|
| 4706 |
if (fsPtr == NULL) { |
| 4707 |
return NULL; |
| 4708 |
} |
| 4709 |
|
| 4710 |
if (fsPtr->filesystemSeparatorProc != NULL) { |
| 4711 |
return fsPtr->filesystemSeparatorProc(pathPtr); |
| 4712 |
} |
| 4713 |
|
| 4714 |
/* |
| 4715 |
* Allow filesystems not to provide a filesystemSeparatorProc if they wish |
| 4716 |
* to use the standard forward slash. |
| 4717 |
*/ |
| 4718 |
|
| 4719 |
TclNewLiteralStringObj(resultObj, "/"); |
| 4720 |
return resultObj; |
| 4721 |
} |
| 4722 |
|
| 4723 |
/* |
| 4724 |
*--------------------------------------------------------------------------- |
| 4725 |
* |
| 4726 |
* NativeFilesystemSeparator -- |
| 4727 |
* |
| 4728 |
* This function is part of the native filesystem support, and returns |
| 4729 |
* the separator for the given path. |
| 4730 |
* |
| 4731 |
* Results: |
| 4732 |
* String object containing the separator character. |
| 4733 |
* |
| 4734 |
* Side effects: |
| 4735 |
* None. |
| 4736 |
* |
| 4737 |
*--------------------------------------------------------------------------- |
| 4738 |
*/ |
| 4739 |
|
| 4740 |
static Tcl_Obj * |
| 4741 |
NativeFilesystemSeparator( |
| 4742 |
Tcl_Obj *pathPtr) |
| 4743 |
{ |
| 4744 |
const char *separator = NULL; /* lint */ |
| 4745 |
|
| 4746 |
switch (tclPlatform) { |
| 4747 |
case TCL_PLATFORM_UNIX: |
| 4748 |
separator = "/"; |
| 4749 |
break; |
| 4750 |
case TCL_PLATFORM_WINDOWS: |
| 4751 |
separator = "\\"; |
| 4752 |
break; |
| 4753 |
} |
| 4754 |
return Tcl_NewStringObj(separator,1); |
| 4755 |
} |
| 4756 |
|
| 4757 |
/* |
| 4758 |
* Local Variables: |
| 4759 |
* mode: c |
| 4760 |
* c-basic-offset: 4 |
| 4761 |
* fill-column: 78 |
| 4762 |
* End: |
| 4763 |
*/ |