| 1 |
/* |
| 2 |
* tclWinFile.c -- |
| 3 |
* |
| 4 |
* This file contains temporary wrappers around UNIX file handling |
| 5 |
* functions. These wrappers map the UNIX functions to Win32 HANDLE-style |
| 6 |
* files, which can be manipulated through the Win32 console redirection |
| 7 |
* interfaces. |
| 8 |
* |
| 9 |
* Copyright (c) 1995-1998 Sun Microsystems, Inc. |
| 10 |
* |
| 11 |
* See the file "license.terms" for information on usage and redistribution |
| 12 |
* of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 13 |
* |
| 14 |
* RCS: @(#) $Id: tclWinFile.c,v 1.44.2.6 2003/10/03 17:45:37 vincentdarley Exp $ |
| 15 |
*/ |
| 16 |
|
| 17 |
//#define _WIN32_WINNT 0x0500 |
| 18 |
|
| 19 |
#include "tclWinInt.h" |
| 20 |
#include <winioctl.h> |
| 21 |
#include <sys/stat.h> |
| 22 |
#include <shlobj.h> |
| 23 |
#include <lmaccess.h> /* For TclpGetUserHome(). */ |
| 24 |
|
| 25 |
/* |
| 26 |
* Declarations for 'link' related information. This information |
| 27 |
* should come with VC++ 6.0, but is not in some older SDKs. |
| 28 |
* In any case it is not well documented. |
| 29 |
*/ |
| 30 |
#ifndef IO_REPARSE_TAG_RESERVED_ONE |
| 31 |
# define IO_REPARSE_TAG_RESERVED_ONE 0x000000001 |
| 32 |
#endif |
| 33 |
#ifndef IO_REPARSE_TAG_RESERVED_RANGE |
| 34 |
# define IO_REPARSE_TAG_RESERVED_RANGE 0x000000001 |
| 35 |
#endif |
| 36 |
#ifndef IO_REPARSE_TAG_VALID_VALUES |
| 37 |
# define IO_REPARSE_TAG_VALID_VALUES 0x0E000FFFF |
| 38 |
#endif |
| 39 |
#ifndef IO_REPARSE_TAG_HSM |
| 40 |
# define IO_REPARSE_TAG_HSM 0x0C0000004 |
| 41 |
#endif |
| 42 |
#ifndef IO_REPARSE_TAG_NSS |
| 43 |
# define IO_REPARSE_TAG_NSS 0x080000005 |
| 44 |
#endif |
| 45 |
#ifndef IO_REPARSE_TAG_NSSRECOVER |
| 46 |
# define IO_REPARSE_TAG_NSSRECOVER 0x080000006 |
| 47 |
#endif |
| 48 |
#ifndef IO_REPARSE_TAG_SIS |
| 49 |
# define IO_REPARSE_TAG_SIS 0x080000007 |
| 50 |
#endif |
| 51 |
#ifndef IO_REPARSE_TAG_DFS |
| 52 |
# define IO_REPARSE_TAG_DFS 0x080000008 |
| 53 |
#endif |
| 54 |
|
| 55 |
#ifndef IO_REPARSE_TAG_RESERVED_ZERO |
| 56 |
# define IO_REPARSE_TAG_RESERVED_ZERO 0x00000000 |
| 57 |
#endif |
| 58 |
#ifndef FILE_FLAG_OPEN_REPARSE_POINT |
| 59 |
# define FILE_FLAG_OPEN_REPARSE_POINT 0x00200000 |
| 60 |
#endif |
| 61 |
#ifndef IO_REPARSE_TAG_MOUNT_POINT |
| 62 |
# define IO_REPARSE_TAG_MOUNT_POINT 0xA0000003 |
| 63 |
#endif |
| 64 |
#ifndef IsReparseTagValid |
| 65 |
# define IsReparseTagValid(x) (!((x)&~IO_REPARSE_TAG_VALID_VALUES)&&((x)>IO_REPARSE_TAG_RESERVED_RANGE)) |
| 66 |
#endif |
| 67 |
#ifndef IO_REPARSE_TAG_SYMBOLIC_LINK |
| 68 |
# define IO_REPARSE_TAG_SYMBOLIC_LINK IO_REPARSE_TAG_RESERVED_ZERO |
| 69 |
#endif |
| 70 |
#ifndef FILE_SPECIAL_ACCESS |
| 71 |
# define FILE_SPECIAL_ACCESS (FILE_ANY_ACCESS) |
| 72 |
#endif |
| 73 |
#ifndef FSCTL_SET_REPARSE_POINT |
| 74 |
# define FSCTL_SET_REPARSE_POINT CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 41, METHOD_BUFFERED, FILE_SPECIAL_ACCESS) |
| 75 |
# define FSCTL_GET_REPARSE_POINT CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 42, METHOD_BUFFERED, FILE_ANY_ACCESS) |
| 76 |
# define FSCTL_DELETE_REPARSE_POINT CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 43, METHOD_BUFFERED, FILE_SPECIAL_ACCESS) |
| 77 |
#endif |
| 78 |
|
| 79 |
/* |
| 80 |
* Maximum reparse buffer info size. The max user defined reparse |
| 81 |
* data is 16KB, plus there's a header. |
| 82 |
*/ |
| 83 |
|
| 84 |
#define MAX_REPARSE_SIZE 17000 |
| 85 |
|
| 86 |
/* |
| 87 |
* Undocumented REPARSE_MOUNTPOINT_HEADER_SIZE structure definition. |
| 88 |
* This is found in winnt.h. |
| 89 |
* |
| 90 |
* IMPORTANT: caution when using this structure, since the actual |
| 91 |
* structures used will want to store a full path in the 'PathBuffer' |
| 92 |
* field, but there isn't room (there's only a single WCHAR!). Therefore |
| 93 |
* one must artificially create a larger space of memory and then cast it |
| 94 |
* to this type. We use the 'DUMMY_REPARSE_BUFFER' struct just below to |
| 95 |
* deal with this problem. |
| 96 |
*/ |
| 97 |
|
| 98 |
#define REPARSE_MOUNTPOINT_HEADER_SIZE 8 |
| 99 |
#ifndef REPARSE_DATA_BUFFER_HEADER_SIZE |
| 100 |
typedef struct _REPARSE_DATA_BUFFER { |
| 101 |
DWORD ReparseTag; |
| 102 |
WORD ReparseDataLength; |
| 103 |
WORD Reserved; |
| 104 |
union { |
| 105 |
struct { |
| 106 |
WORD SubstituteNameOffset; |
| 107 |
WORD SubstituteNameLength; |
| 108 |
WORD PrintNameOffset; |
| 109 |
WORD PrintNameLength; |
| 110 |
WCHAR PathBuffer[1]; |
| 111 |
} SymbolicLinkReparseBuffer; |
| 112 |
struct { |
| 113 |
WORD SubstituteNameOffset; |
| 114 |
WORD SubstituteNameLength; |
| 115 |
WORD PrintNameOffset; |
| 116 |
WORD PrintNameLength; |
| 117 |
WCHAR PathBuffer[1]; |
| 118 |
} MountPointReparseBuffer; |
| 119 |
struct { |
| 120 |
BYTE DataBuffer[1]; |
| 121 |
} GenericReparseBuffer; |
| 122 |
}; |
| 123 |
} REPARSE_DATA_BUFFER; |
| 124 |
#endif |
| 125 |
|
| 126 |
typedef struct { |
| 127 |
REPARSE_DATA_BUFFER dummy; |
| 128 |
WCHAR dummyBuf[MAX_PATH*3]; |
| 129 |
} DUMMY_REPARSE_BUFFER; |
| 130 |
|
| 131 |
#if defined(_MSC_VER) && ( _MSC_VER <= 1100 ) |
| 132 |
#define HAVE_NO_FINDEX_ENUMS |
| 133 |
#elif !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0400) |
| 134 |
#define HAVE_NO_FINDEX_ENUMS |
| 135 |
#endif |
| 136 |
|
| 137 |
#ifdef HAVE_NO_FINDEX_ENUMS |
| 138 |
/* These two aren't in VC++ 5.2 headers */ |
| 139 |
typedef enum _FINDEX_INFO_LEVELS { |
| 140 |
FindExInfoStandard, |
| 141 |
FindExInfoMaxInfoLevel |
| 142 |
} FINDEX_INFO_LEVELS; |
| 143 |
typedef enum _FINDEX_SEARCH_OPS { |
| 144 |
FindExSearchNameMatch, |
| 145 |
FindExSearchLimitToDirectories, |
| 146 |
FindExSearchLimitToDevices, |
| 147 |
FindExSearchMaxSearchOp |
| 148 |
} FINDEX_SEARCH_OPS; |
| 149 |
#endif /* HAVE_NO_FINDEX_ENUMS */ |
| 150 |
|
| 151 |
/* Other typedefs required by this code */ |
| 152 |
|
| 153 |
static time_t ToCTime(FILETIME fileTime); |
| 154 |
|
| 155 |
typedef NET_API_STATUS NET_API_FUNCTION NETUSERGETINFOPROC |
| 156 |
(LPWSTR servername, LPWSTR username, DWORD level, LPBYTE *bufptr); |
| 157 |
|
| 158 |
typedef NET_API_STATUS NET_API_FUNCTION NETAPIBUFFERFREEPROC |
| 159 |
(LPVOID Buffer); |
| 160 |
|
| 161 |
typedef NET_API_STATUS NET_API_FUNCTION NETGETDCNAMEPROC |
| 162 |
(LPWSTR servername, LPWSTR domainname, LPBYTE *bufptr); |
| 163 |
|
| 164 |
extern Tcl_FSDupInternalRepProc NativeDupInternalRep; |
| 165 |
|
| 166 |
/* |
| 167 |
* Declarations for local procedures defined in this file: |
| 168 |
*/ |
| 169 |
|
| 170 |
static int NativeAccess(CONST TCHAR *path, int mode); |
| 171 |
static int NativeStat(CONST TCHAR *path, Tcl_StatBuf *statPtr, int checkLinks); |
| 172 |
static unsigned short NativeStatMode(DWORD attr, int checkLinks, int isExec); |
| 173 |
static int NativeIsExec(CONST TCHAR *path); |
| 174 |
static int NativeReadReparse(CONST TCHAR* LinkDirectory, |
| 175 |
REPARSE_DATA_BUFFER* buffer); |
| 176 |
static int NativeWriteReparse(CONST TCHAR* LinkDirectory, |
| 177 |
REPARSE_DATA_BUFFER* buffer); |
| 178 |
static int NativeMatchType(int isDrive, DWORD attr, CONST TCHAR* nativeName, |
| 179 |
Tcl_GlobTypeData *types); |
| 180 |
static int WinIsDrive(CONST char *name, int nameLen); |
| 181 |
static Tcl_Obj* WinReadLink(CONST TCHAR* LinkSource); |
| 182 |
static Tcl_Obj* WinReadLinkDirectory(CONST TCHAR* LinkDirectory); |
| 183 |
static int WinLink(CONST TCHAR* LinkSource, CONST TCHAR* LinkTarget, |
| 184 |
int linkAction); |
| 185 |
static int WinSymLinkDirectory(CONST TCHAR* LinkDirectory, |
| 186 |
CONST TCHAR* LinkTarget); |
| 187 |
|
| 188 |
/* |
| 189 |
*-------------------------------------------------------------------- |
| 190 |
* |
| 191 |
* WinLink |
| 192 |
* |
| 193 |
* Make a link from source to target. |
| 194 |
*-------------------------------------------------------------------- |
| 195 |
*/ |
| 196 |
static int |
| 197 |
WinLink(LinkSource, LinkTarget, linkAction) |
| 198 |
CONST TCHAR* LinkSource; |
| 199 |
CONST TCHAR* LinkTarget; |
| 200 |
int linkAction; |
| 201 |
{ |
| 202 |
WCHAR tempFileName[MAX_PATH]; |
| 203 |
TCHAR* tempFilePart; |
| 204 |
int attr; |
| 205 |
|
| 206 |
/* Get the full path referenced by the target */ |
| 207 |
if (!(*tclWinProcs->getFullPathNameProc)(LinkTarget, |
| 208 |
MAX_PATH, tempFileName, &tempFilePart)) { |
| 209 |
/* Invalid file */ |
| 210 |
TclWinConvertError(GetLastError()); |
| 211 |
return -1; |
| 212 |
} |
| 213 |
|
| 214 |
/* Make sure source file doesn't exist */ |
| 215 |
attr = (*tclWinProcs->getFileAttributesProc)(LinkSource); |
| 216 |
if (attr != 0xffffffff) { |
| 217 |
Tcl_SetErrno(EEXIST); |
| 218 |
return -1; |
| 219 |
} |
| 220 |
|
| 221 |
/* Get the full path referenced by the directory */ |
| 222 |
if (!(*tclWinProcs->getFullPathNameProc)(LinkSource, |
| 223 |
MAX_PATH, tempFileName, &tempFilePart)) { |
| 224 |
/* Invalid file */ |
| 225 |
TclWinConvertError(GetLastError()); |
| 226 |
return -1; |
| 227 |
} |
| 228 |
/* Check the target */ |
| 229 |
attr = (*tclWinProcs->getFileAttributesProc)(LinkTarget); |
| 230 |
if (attr == 0xffffffff) { |
| 231 |
/* The target doesn't exist */ |
| 232 |
TclWinConvertError(GetLastError()); |
| 233 |
return -1; |
| 234 |
} else if ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0) { |
| 235 |
/* It is a file */ |
| 236 |
if (tclWinProcs->createHardLinkProc == NULL) { |
| 237 |
Tcl_SetErrno(ENOTDIR); |
| 238 |
return -1; |
| 239 |
} |
| 240 |
if (linkAction & TCL_CREATE_HARD_LINK) { |
| 241 |
if (!(*tclWinProcs->createHardLinkProc)(LinkSource, LinkTarget, NULL)) { |
| 242 |
TclWinConvertError(GetLastError()); |
| 243 |
return -1; |
| 244 |
} |
| 245 |
return 0; |
| 246 |
} else if (linkAction & TCL_CREATE_SYMBOLIC_LINK) { |
| 247 |
/* Can't symlink files */ |
| 248 |
Tcl_SetErrno(ENOTDIR); |
| 249 |
return -1; |
| 250 |
} else { |
| 251 |
Tcl_SetErrno(ENODEV); |
| 252 |
return -1; |
| 253 |
} |
| 254 |
} else { |
| 255 |
if (linkAction & TCL_CREATE_SYMBOLIC_LINK) { |
| 256 |
return WinSymLinkDirectory(LinkSource, LinkTarget); |
| 257 |
} else if (linkAction & TCL_CREATE_HARD_LINK) { |
| 258 |
/* Can't hard link directories */ |
| 259 |
Tcl_SetErrno(EISDIR); |
| 260 |
return -1; |
| 261 |
} else { |
| 262 |
Tcl_SetErrno(ENODEV); |
| 263 |
return -1; |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
/* |
| 269 |
*-------------------------------------------------------------------- |
| 270 |
* |
| 271 |
* WinReadLink |
| 272 |
* |
| 273 |
* What does 'LinkSource' point to? |
| 274 |
*-------------------------------------------------------------------- |
| 275 |
*/ |
| 276 |
static Tcl_Obj* |
| 277 |
WinReadLink(LinkSource) |
| 278 |
CONST TCHAR* LinkSource; |
| 279 |
{ |
| 280 |
WCHAR tempFileName[MAX_PATH]; |
| 281 |
TCHAR* tempFilePart; |
| 282 |
int attr; |
| 283 |
|
| 284 |
/* Get the full path referenced by the target */ |
| 285 |
if (!(*tclWinProcs->getFullPathNameProc)(LinkSource, |
| 286 |
MAX_PATH, tempFileName, &tempFilePart)) { |
| 287 |
/* Invalid file */ |
| 288 |
TclWinConvertError(GetLastError()); |
| 289 |
return NULL; |
| 290 |
} |
| 291 |
|
| 292 |
/* Make sure source file does exist */ |
| 293 |
attr = (*tclWinProcs->getFileAttributesProc)(LinkSource); |
| 294 |
if (attr == 0xffffffff) { |
| 295 |
/* The source doesn't exist */ |
| 296 |
TclWinConvertError(GetLastError()); |
| 297 |
return NULL; |
| 298 |
} else if ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0) { |
| 299 |
/* It is a file - this is not yet supported */ |
| 300 |
Tcl_SetErrno(ENOTDIR); |
| 301 |
return NULL; |
| 302 |
} else { |
| 303 |
return WinReadLinkDirectory(LinkSource); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
/* |
| 308 |
*-------------------------------------------------------------------- |
| 309 |
* |
| 310 |
* WinSymLinkDirectory |
| 311 |
* |
| 312 |
* This routine creates a NTFS junction, using the undocumented |
| 313 |
* FSCTL_SET_REPARSE_POINT structure Win2K uses for mount points |
| 314 |
* and junctions. |
| 315 |
* |
| 316 |
* Assumption that LinkTarget is a valid, existing directory. |
| 317 |
* |
| 318 |
* Returns zero on success. |
| 319 |
*-------------------------------------------------------------------- |
| 320 |
*/ |
| 321 |
static int |
| 322 |
WinSymLinkDirectory(LinkDirectory, LinkTarget) |
| 323 |
CONST TCHAR* LinkDirectory; |
| 324 |
CONST TCHAR* LinkTarget; |
| 325 |
{ |
| 326 |
DUMMY_REPARSE_BUFFER dummy; |
| 327 |
REPARSE_DATA_BUFFER *reparseBuffer = (REPARSE_DATA_BUFFER*)&dummy; |
| 328 |
int len; |
| 329 |
WCHAR nativeTarget[MAX_PATH]; |
| 330 |
WCHAR *loop; |
| 331 |
|
| 332 |
/* Make the native target name */ |
| 333 |
memcpy((VOID*)nativeTarget, (VOID*)L"\\??\\", 4*sizeof(WCHAR)); |
| 334 |
memcpy((VOID*)(nativeTarget + 4), (VOID*)LinkTarget, |
| 335 |
sizeof(WCHAR)*(1+wcslen((WCHAR*)LinkTarget))); |
| 336 |
len = wcslen(nativeTarget); |
| 337 |
/* |
| 338 |
* We must have backslashes only. This is VERY IMPORTANT. |
| 339 |
* If we have any forward slashes everything appears to work, |
| 340 |
* but the resulting symlink is useless! |
| 341 |
*/ |
| 342 |
for (loop = nativeTarget; *loop != 0; loop++) { |
| 343 |
if (*loop == L'/') *loop = L'\\'; |
| 344 |
} |
| 345 |
if ((nativeTarget[len-1] == L'\\') && (nativeTarget[len-2] != L':')) { |
| 346 |
nativeTarget[len-1] = 0; |
| 347 |
} |
| 348 |
|
| 349 |
/* Build the reparse info */ |
| 350 |
memset(reparseBuffer, 0, sizeof(DUMMY_REPARSE_BUFFER)); |
| 351 |
reparseBuffer->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT; |
| 352 |
reparseBuffer->SymbolicLinkReparseBuffer.SubstituteNameLength = |
| 353 |
wcslen(nativeTarget) * sizeof(WCHAR); |
| 354 |
reparseBuffer->Reserved = 0; |
| 355 |
reparseBuffer->SymbolicLinkReparseBuffer.PrintNameLength = 0; |
| 356 |
reparseBuffer->SymbolicLinkReparseBuffer.PrintNameOffset = |
| 357 |
reparseBuffer->SymbolicLinkReparseBuffer.SubstituteNameLength |
| 358 |
+ sizeof(WCHAR); |
| 359 |
memcpy(reparseBuffer->SymbolicLinkReparseBuffer.PathBuffer, nativeTarget, |
| 360 |
sizeof(WCHAR) |
| 361 |
+ reparseBuffer->SymbolicLinkReparseBuffer.SubstituteNameLength); |
| 362 |
reparseBuffer->ReparseDataLength = |
| 363 |
reparseBuffer->SymbolicLinkReparseBuffer.SubstituteNameLength + 12; |
| 364 |
|
| 365 |
return NativeWriteReparse(LinkDirectory, reparseBuffer); |
| 366 |
} |
| 367 |
|
| 368 |
/* |
| 369 |
*-------------------------------------------------------------------- |
| 370 |
* |
| 371 |
* TclWinSymLinkCopyDirectory |
| 372 |
* |
| 373 |
* Copy a Windows NTFS junction. This function assumes that |
| 374 |
* LinkOriginal exists and is a valid junction point, and that |
| 375 |
* LinkCopy does not exist. |
| 376 |
* |
| 377 |
* Returns zero on success. |
| 378 |
*-------------------------------------------------------------------- |
| 379 |
*/ |
| 380 |
int |
| 381 |
TclWinSymLinkCopyDirectory(LinkOriginal, LinkCopy) |
| 382 |
CONST TCHAR* LinkOriginal; /* Existing junction - reparse point */ |
| 383 |
CONST TCHAR* LinkCopy; /* Will become a duplicate junction */ |
| 384 |
{ |
| 385 |
DUMMY_REPARSE_BUFFER dummy; |
| 386 |
REPARSE_DATA_BUFFER *reparseBuffer = (REPARSE_DATA_BUFFER*)&dummy; |
| 387 |
|
| 388 |
if (NativeReadReparse(LinkOriginal, reparseBuffer)) { |
| 389 |
return -1; |
| 390 |
} |
| 391 |
return NativeWriteReparse(LinkCopy, reparseBuffer); |
| 392 |
} |
| 393 |
|
| 394 |
/* |
| 395 |
*-------------------------------------------------------------------- |
| 396 |
* |
| 397 |
* TclWinSymLinkDelete |
| 398 |
* |
| 399 |
* Delete a Windows NTFS junction. Once the junction information |
| 400 |
* is deleted, the filesystem object becomes an ordinary directory. |
| 401 |
* Unless 'linkOnly' is given, that directory is also removed. |
| 402 |
* |
| 403 |
* Assumption that LinkOriginal is a valid, existing junction. |
| 404 |
* |
| 405 |
* Returns zero on success. |
| 406 |
*-------------------------------------------------------------------- |
| 407 |
*/ |
| 408 |
int |
| 409 |
TclWinSymLinkDelete(LinkOriginal, linkOnly) |
| 410 |
CONST TCHAR* LinkOriginal; |
| 411 |
int linkOnly; |
| 412 |
{ |
| 413 |
/* It is a symbolic link -- remove it */ |
| 414 |
DUMMY_REPARSE_BUFFER dummy; |
| 415 |
REPARSE_DATA_BUFFER *reparseBuffer = (REPARSE_DATA_BUFFER*)&dummy; |
| 416 |
HANDLE hFile; |
| 417 |
DWORD returnedLength; |
| 418 |
memset(reparseBuffer, 0, sizeof(DUMMY_REPARSE_BUFFER)); |
| 419 |
reparseBuffer->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT; |
| 420 |
hFile = (*tclWinProcs->createFileProc)(LinkOriginal, GENERIC_WRITE, 0, |
| 421 |
NULL, OPEN_EXISTING, |
| 422 |
FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 423 |
if (hFile != INVALID_HANDLE_VALUE) { |
| 424 |
if (!DeviceIoControl(hFile, FSCTL_DELETE_REPARSE_POINT, reparseBuffer, |
| 425 |
REPARSE_MOUNTPOINT_HEADER_SIZE, |
| 426 |
NULL, 0, &returnedLength, NULL)) { |
| 427 |
/* Error setting junction */ |
| 428 |
TclWinConvertError(GetLastError()); |
| 429 |
CloseHandle(hFile); |
| 430 |
} else { |
| 431 |
CloseHandle(hFile); |
| 432 |
if (!linkOnly) { |
| 433 |
(*tclWinProcs->removeDirectoryProc)(LinkOriginal); |
| 434 |
} |
| 435 |
return 0; |
| 436 |
} |
| 437 |
} |
| 438 |
return -1; |
| 439 |
} |
| 440 |
|
| 441 |
/* |
| 442 |
*-------------------------------------------------------------------- |
| 443 |
* |
| 444 |
* WinReadLinkDirectory |
| 445 |
* |
| 446 |
* This routine reads a NTFS junction, using the undocumented |
| 447 |
* FSCTL_GET_REPARSE_POINT structure Win2K uses for mount points |
| 448 |
* and junctions. |
| 449 |
* |
| 450 |
* Assumption that LinkDirectory is a valid, existing directory. |
| 451 |
* |
| 452 |
* Returns a Tcl_Obj with refCount of 1 (i.e. owned by the caller), |
| 453 |
* or NULL if anything went wrong. |
| 454 |
* |
| 455 |
* In the future we should enhance this to return a path object |
| 456 |
* rather than a string. |
| 457 |
*-------------------------------------------------------------------- |
| 458 |
*/ |
| 459 |
static Tcl_Obj* |
| 460 |
WinReadLinkDirectory(LinkDirectory) |
| 461 |
CONST TCHAR* LinkDirectory; |
| 462 |
{ |
| 463 |
int attr; |
| 464 |
DUMMY_REPARSE_BUFFER dummy; |
| 465 |
REPARSE_DATA_BUFFER *reparseBuffer = (REPARSE_DATA_BUFFER*)&dummy; |
| 466 |
|
| 467 |
attr = (*tclWinProcs->getFileAttributesProc)(LinkDirectory); |
| 468 |
if (!(attr & FILE_ATTRIBUTE_REPARSE_POINT)) { |
| 469 |
Tcl_SetErrno(EINVAL); |
| 470 |
return NULL; |
| 471 |
} |
| 472 |
if (NativeReadReparse(LinkDirectory, reparseBuffer)) { |
| 473 |
return NULL; |
| 474 |
} |
| 475 |
|
| 476 |
switch (reparseBuffer->ReparseTag) { |
| 477 |
case 0x80000000|IO_REPARSE_TAG_SYMBOLIC_LINK: |
| 478 |
case IO_REPARSE_TAG_SYMBOLIC_LINK: |
| 479 |
case IO_REPARSE_TAG_MOUNT_POINT: { |
| 480 |
Tcl_Obj *retVal; |
| 481 |
Tcl_DString ds; |
| 482 |
CONST char *copy; |
| 483 |
int len; |
| 484 |
int offset = 0; |
| 485 |
|
| 486 |
/* |
| 487 |
* Certain native path representations on Windows have a |
| 488 |
* special prefix to indicate that they are to be treated |
| 489 |
* specially. For example extremely long paths, or symlinks, |
| 490 |
* or volumes mounted inside directories. |
| 491 |
* |
| 492 |
* There is an assumption in this code that 'wide' interfaces |
| 493 |
* are being used (see tclWin32Dll.c), which is true for the |
| 494 |
* only systems which support reparse tags at present. If |
| 495 |
* that changes in the future, this code will have to be |
| 496 |
* generalised. |
| 497 |
*/ |
| 498 |
if (reparseBuffer->SymbolicLinkReparseBuffer.PathBuffer[0] |
| 499 |
== L'\\') { |
| 500 |
/* Check whether this is a mounted volume */ |
| 501 |
if (wcsncmp(reparseBuffer->SymbolicLinkReparseBuffer.PathBuffer, |
| 502 |
L"\\??\\Volume{",11) == 0) { |
| 503 |
char drive; |
| 504 |
/* |
| 505 |
* There is some confusion between \??\ and \\?\ which |
| 506 |
* we have to fix here. It doesn't seem very well |
| 507 |
* documented. |
| 508 |
*/ |
| 509 |
reparseBuffer->SymbolicLinkReparseBuffer |
| 510 |
.PathBuffer[1] = L'\\'; |
| 511 |
/* |
| 512 |
* Check if a corresponding drive letter exists, and |
| 513 |
* use that if it is found |
| 514 |
*/ |
| 515 |
drive = TclWinDriveLetterForVolMountPoint(reparseBuffer |
| 516 |
->SymbolicLinkReparseBuffer.PathBuffer); |
| 517 |
if (drive != -1) { |
| 518 |
char driveSpec[3] = { |
| 519 |
drive, ':', '\0' |
| 520 |
}; |
| 521 |
retVal = Tcl_NewStringObj(driveSpec,2); |
| 522 |
Tcl_IncrRefCount(retVal); |
| 523 |
return retVal; |
| 524 |
} |
| 525 |
/* |
| 526 |
* This is actually a mounted drive, which doesn't |
| 527 |
* exists as a DOS drive letter. This means the path |
| 528 |
* isn't actually a link, although we partially treat |
| 529 |
* it like one ('file type' will return 'link'), but |
| 530 |
* then the link will actually just be treated like |
| 531 |
* an ordinary directory. I don't believe any |
| 532 |
* serious inconsistency will arise from this, but it |
| 533 |
* is something to be aware of. |
| 534 |
*/ |
| 535 |
Tcl_SetErrno(EINVAL); |
| 536 |
return NULL; |
| 537 |
} else if (wcsncmp(reparseBuffer->SymbolicLinkReparseBuffer |
| 538 |
.PathBuffer, L"\\\\?\\",4) == 0) { |
| 539 |
/* Strip off the prefix */ |
| 540 |
offset = 4; |
| 541 |
} else if (wcsncmp(reparseBuffer->SymbolicLinkReparseBuffer |
| 542 |
.PathBuffer, L"\\??\\",4) == 0) { |
| 543 |
/* Strip off the prefix */ |
| 544 |
offset = 4; |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
Tcl_WinTCharToUtf( |
| 549 |
(CONST char*)reparseBuffer->SymbolicLinkReparseBuffer.PathBuffer, |
| 550 |
(int)reparseBuffer->SymbolicLinkReparseBuffer |
| 551 |
.SubstituteNameLength, &ds); |
| 552 |
|
| 553 |
copy = Tcl_DStringValue(&ds)+offset; |
| 554 |
len = Tcl_DStringLength(&ds)-offset; |
| 555 |
retVal = Tcl_NewStringObj(copy,len); |
| 556 |
Tcl_IncrRefCount(retVal); |
| 557 |
Tcl_DStringFree(&ds); |
| 558 |
return retVal; |
| 559 |
} |
| 560 |
} |
| 561 |
Tcl_SetErrno(EINVAL); |
| 562 |
return NULL; |
| 563 |
} |
| 564 |
|
| 565 |
/* |
| 566 |
*-------------------------------------------------------------------- |
| 567 |
* |
| 568 |
* NativeReadReparse |
| 569 |
* |
| 570 |
* Read the junction/reparse information from a given NTFS directory. |
| 571 |
* |
| 572 |
* Assumption that LinkDirectory is a valid, existing directory. |
| 573 |
* |
| 574 |
* Returns zero on success. |
| 575 |
*-------------------------------------------------------------------- |
| 576 |
*/ |
| 577 |
static int |
| 578 |
NativeReadReparse(LinkDirectory, buffer) |
| 579 |
CONST TCHAR* LinkDirectory; /* The junction to read */ |
| 580 |
REPARSE_DATA_BUFFER* buffer; /* Pointer to buffer. Cannot be NULL */ |
| 581 |
{ |
| 582 |
HANDLE hFile; |
| 583 |
DWORD returnedLength; |
| 584 |
|
| 585 |
hFile = (*tclWinProcs->createFileProc)(LinkDirectory, GENERIC_READ, 0, |
| 586 |
NULL, OPEN_EXISTING, |
| 587 |
FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 588 |
if (hFile == INVALID_HANDLE_VALUE) { |
| 589 |
/* Error creating directory */ |
| 590 |
TclWinConvertError(GetLastError()); |
| 591 |
return -1; |
| 592 |
} |
| 593 |
/* Get the link */ |
| 594 |
if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT, NULL, |
| 595 |
0, buffer, sizeof(DUMMY_REPARSE_BUFFER), |
| 596 |
&returnedLength, NULL)) { |
| 597 |
/* Error setting junction */ |
| 598 |
TclWinConvertError(GetLastError()); |
| 599 |
CloseHandle(hFile); |
| 600 |
return -1; |
| 601 |
} |
| 602 |
CloseHandle(hFile); |
| 603 |
|
| 604 |
if (!IsReparseTagValid(buffer->ReparseTag)) { |
| 605 |
Tcl_SetErrno(EINVAL); |
| 606 |
return -1; |
| 607 |
} |
| 608 |
return 0; |
| 609 |
} |
| 610 |
|
| 611 |
/* |
| 612 |
*-------------------------------------------------------------------- |
| 613 |
* |
| 614 |
* NativeWriteReparse |
| 615 |
* |
| 616 |
* Write the reparse information for a given directory. |
| 617 |
* |
| 618 |
* Assumption that LinkDirectory does not exist. |
| 619 |
*-------------------------------------------------------------------- |
| 620 |
*/ |
| 621 |
static int |
| 622 |
NativeWriteReparse(LinkDirectory, buffer) |
| 623 |
CONST TCHAR* LinkDirectory; |
| 624 |
REPARSE_DATA_BUFFER* buffer; |
| 625 |
{ |
| 626 |
HANDLE hFile; |
| 627 |
DWORD returnedLength; |
| 628 |
|
| 629 |
/* Create the directory - it must not already exist */ |
| 630 |
if ((*tclWinProcs->createDirectoryProc)(LinkDirectory, NULL) == 0) { |
| 631 |
/* Error creating directory */ |
| 632 |
TclWinConvertError(GetLastError()); |
| 633 |
return -1; |
| 634 |
} |
| 635 |
hFile = (*tclWinProcs->createFileProc)(LinkDirectory, GENERIC_WRITE, 0, |
| 636 |
NULL, OPEN_EXISTING, |
| 637 |
FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 638 |
if (hFile == INVALID_HANDLE_VALUE) { |
| 639 |
/* Error creating directory */ |
| 640 |
TclWinConvertError(GetLastError()); |
| 641 |
return -1; |
| 642 |
} |
| 643 |
/* Set the link */ |
| 644 |
if (!DeviceIoControl(hFile, FSCTL_SET_REPARSE_POINT, buffer, |
| 645 |
(DWORD) buffer->ReparseDataLength |
| 646 |
+ REPARSE_MOUNTPOINT_HEADER_SIZE, |
| 647 |
NULL, 0, &returnedLength, NULL)) { |
| 648 |
/* Error setting junction */ |
| 649 |
TclWinConvertError(GetLastError()); |
| 650 |
CloseHandle(hFile); |
| 651 |
(*tclWinProcs->removeDirectoryProc)(LinkDirectory); |
| 652 |
return -1; |
| 653 |
} |
| 654 |
CloseHandle(hFile); |
| 655 |
/* We succeeded */ |
| 656 |
return 0; |
| 657 |
} |
| 658 |
|
| 659 |
/* |
| 660 |
*--------------------------------------------------------------------------- |
| 661 |
* |
| 662 |
* TclpFindExecutable -- |
| 663 |
* |
| 664 |
* This procedure computes the absolute path name of the current |
| 665 |
* application, given its argv[0] value. |
| 666 |
* |
| 667 |
* Results: |
| 668 |
* A dirty UTF string that is the path to the executable. At this |
| 669 |
* point we may not know the system encoding. Convert the native |
| 670 |
* string value to UTF using the default encoding. The assumption |
| 671 |
* is that we will still be able to parse the path given the path |
| 672 |
* name contains ASCII string and '/' chars do not conflict with |
| 673 |
* other UTF chars. |
| 674 |
* |
| 675 |
* Side effects: |
| 676 |
* The variable tclNativeExecutableName gets filled in with the file |
| 677 |
* name for the application, if we figured it out. If we couldn't |
| 678 |
* figure it out, tclNativeExecutableName is set to NULL. |
| 679 |
* |
| 680 |
*--------------------------------------------------------------------------- |
| 681 |
*/ |
| 682 |
|
| 683 |
char * |
| 684 |
TclpFindExecutable(argv0) |
| 685 |
CONST char *argv0; /* The value of the application's argv[0] |
| 686 |
* (native). */ |
| 687 |
{ |
| 688 |
Tcl_DString ds; |
| 689 |
WCHAR wName[MAX_PATH]; |
| 690 |
|
| 691 |
if (argv0 == NULL) { |
| 692 |
return NULL; |
| 693 |
} |
| 694 |
if (tclNativeExecutableName != NULL) { |
| 695 |
return tclNativeExecutableName; |
| 696 |
} |
| 697 |
|
| 698 |
/* |
| 699 |
* Under Windows we ignore argv0, and return the path for the file used to |
| 700 |
* create this process. |
| 701 |
*/ |
| 702 |
|
| 703 |
(*tclWinProcs->getModuleFileNameProc)(NULL, wName, MAX_PATH); |
| 704 |
Tcl_WinTCharToUtf((CONST TCHAR *) wName, -1, &ds); |
| 705 |
|
| 706 |
tclNativeExecutableName = ckalloc((unsigned) (Tcl_DStringLength(&ds) + 1)); |
| 707 |
strcpy(tclNativeExecutableName, Tcl_DStringValue(&ds)); |
| 708 |
Tcl_DStringFree(&ds); |
| 709 |
|
| 710 |
TclWinNoBackslash(tclNativeExecutableName); |
| 711 |
return tclNativeExecutableName; |
| 712 |
} |
| 713 |
|
| 714 |
/* |
| 715 |
*---------------------------------------------------------------------- |
| 716 |
* |
| 717 |
* TclpMatchInDirectory -- |
| 718 |
* |
| 719 |
* This routine is used by the globbing code to search a |
| 720 |
* directory for all files which match a given pattern. |
| 721 |
* |
| 722 |
* Results: |
| 723 |
* |
| 724 |
* The return value is a standard Tcl result indicating whether an |
| 725 |
* error occurred in globbing. Errors are left in interp, good |
| 726 |
* results are lappended to resultPtr (which must be a valid object) |
| 727 |
* |
| 728 |
* Side effects: |
| 729 |
* None. |
| 730 |
* |
| 731 |
*---------------------------------------------------------------------- */ |
| 732 |
|
| 733 |
int |
| 734 |
TclpMatchInDirectory(interp, resultPtr, pathPtr, pattern, types) |
| 735 |
Tcl_Interp *interp; /* Interpreter to receive errors. */ |
| 736 |
Tcl_Obj *resultPtr; /* List object to lappend results. */ |
| 737 |
Tcl_Obj *pathPtr; /* Contains path to directory to search. */ |
| 738 |
CONST char *pattern; /* Pattern to match against. */ |
| 739 |
Tcl_GlobTypeData *types; /* Object containing list of acceptable types. |
| 740 |
* May be NULL. In particular the directory |
| 741 |
* flag is very important. */ |
| 742 |
{ |
| 743 |
CONST TCHAR *native; |
| 744 |
|
| 745 |
if (pattern == NULL || (*pattern == '\0')) { |
| 746 |
Tcl_Obj *norm = Tcl_FSGetNormalizedPath(NULL, pathPtr); |
| 747 |
if (norm != NULL) { |
| 748 |
/* Match a single file directly */ |
| 749 |
int len; |
| 750 |
DWORD attr; |
| 751 |
CONST char *str = Tcl_GetStringFromObj(norm,&len); |
| 752 |
|
| 753 |
native = (CONST TCHAR*) Tcl_FSGetNativePath(pathPtr); |
| 754 |
|
| 755 |
if (tclWinProcs->getFileAttributesExProc == NULL) { |
| 756 |
attr = (*tclWinProcs->getFileAttributesProc)(native); |
| 757 |
if (attr == 0xffffffff) { |
| 758 |
return TCL_OK; |
| 759 |
} |
| 760 |
} else { |
| 761 |
WIN32_FILE_ATTRIBUTE_DATA data; |
| 762 |
if ((*tclWinProcs->getFileAttributesExProc)(native, |
| 763 |
GetFileExInfoStandard, &data) != TRUE) { |
| 764 |
return TCL_OK; |
| 765 |
} |
| 766 |
attr = data.dwFileAttributes; |
| 767 |
} |
| 768 |
if (NativeMatchType(WinIsDrive(str,len), attr, |
| 769 |
native, types)) { |
| 770 |
Tcl_ListObjAppendElement(interp, resultPtr, pathPtr); |
| 771 |
} |
| 772 |
} |
| 773 |
return TCL_OK; |
| 774 |
} else { |
| 775 |
DWORD attr; |
| 776 |
HANDLE handle; |
| 777 |
WIN32_FIND_DATAT data; |
| 778 |
CONST char *dirName; |
| 779 |
int dirLength; |
| 780 |
int matchSpecialDots; |
| 781 |
Tcl_DString ds; /* native encoding of dir */ |
| 782 |
Tcl_DString dsOrig; /* utf-8 encoding of dir */ |
| 783 |
Tcl_DString dirString; /* utf-8 encoding of dir with \'s */ |
| 784 |
Tcl_Obj *fileNamePtr; |
| 785 |
|
| 786 |
/* |
| 787 |
* Convert the path to normalized form since some interfaces only |
| 788 |
* accept backslashes. Also, ensure that the directory ends with a |
| 789 |
* separator character. |
| 790 |
*/ |
| 791 |
|
| 792 |
fileNamePtr = Tcl_FSGetTranslatedPath(interp, pathPtr); |
| 793 |
if (fileNamePtr == NULL) { |
| 794 |
return TCL_ERROR; |
| 795 |
} |
| 796 |
Tcl_DStringInit(&dsOrig); |
| 797 |
dirName = Tcl_GetStringFromObj(fileNamePtr, &dirLength); |
| 798 |
Tcl_DStringAppend(&dsOrig, dirName, dirLength); |
| 799 |
|
| 800 |
Tcl_DStringInit(&dirString); |
| 801 |
if (dirLength == 0) { |
| 802 |
Tcl_DStringAppend(&dirString, ".\\", 2); |
| 803 |
} else { |
| 804 |
char *p; |
| 805 |
|
| 806 |
Tcl_DStringAppend(&dirString, dirName, dirLength); |
| 807 |
for (p = Tcl_DStringValue(&dirString); *p != '\0'; p++) { |
| 808 |
if (*p == '/') { |
| 809 |
*p = '\\'; |
| 810 |
} |
| 811 |
} |
| 812 |
p--; |
| 813 |
/* Make sure we have a trailing directory delimiter */ |
| 814 |
if ((*p != '\\') && (*p != ':')) { |
| 815 |
Tcl_DStringAppend(&dirString, "\\", 1); |
| 816 |
Tcl_DStringAppend(&dsOrig, "/", 1); |
| 817 |
dirLength++; |
| 818 |
} |
| 819 |
} |
| 820 |
dirName = Tcl_DStringValue(&dirString); |
| 821 |
Tcl_DecrRefCount(fileNamePtr); |
| 822 |
|
| 823 |
/* |
| 824 |
* First verify that the specified path is actually a directory. |
| 825 |
*/ |
| 826 |
|
| 827 |
native = Tcl_WinUtfToTChar(dirName, Tcl_DStringLength(&dirString), |
| 828 |
&ds); |
| 829 |
attr = (*tclWinProcs->getFileAttributesProc)(native); |
| 830 |
Tcl_DStringFree(&ds); |
| 831 |
|
| 832 |
if ((attr == 0xffffffff) || ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0)) { |
| 833 |
Tcl_DStringFree(&dirString); |
| 834 |
return TCL_OK; |
| 835 |
} |
| 836 |
|
| 837 |
/* |
| 838 |
* We need to check all files in the directory, so append a *.* |
| 839 |
* to the path. |
| 840 |
*/ |
| 841 |
|
| 842 |
dirName = Tcl_DStringAppend(&dirString, "*.*", 3); |
| 843 |
native = Tcl_WinUtfToTChar(dirName, -1, &ds); |
| 844 |
handle = (*tclWinProcs->findFirstFileProc)(native, &data); |
| 845 |
Tcl_DStringFree(&ds); |
| 846 |
|
| 847 |
if (handle == INVALID_HANDLE_VALUE) { |
| 848 |
Tcl_DStringFree(&dirString); |
| 849 |
TclWinConvertError(GetLastError()); |
| 850 |
Tcl_ResetResult(interp); |
| 851 |
Tcl_AppendResult(interp, "couldn't read directory \"", |
| 852 |
Tcl_DStringValue(&dsOrig), "\": ", |
| 853 |
Tcl_PosixError(interp), (char *) NULL); |
| 854 |
Tcl_DStringFree(&dsOrig); |
| 855 |
return TCL_ERROR; |
| 856 |
} |
| 857 |
|
| 858 |
/* |
| 859 |
* Check to see if the pattern should match the special |
| 860 |
* . and .. names, referring to the current directory, |
| 861 |
* or the directory above. We need a special check for |
| 862 |
* this because paths beginning with a dot are not considered |
| 863 |
* hidden on Windows, and so otherwise a relative glob like |
| 864 |
* 'glob -join * *' will actually return './. ../..' etc. |
| 865 |
*/ |
| 866 |
|
| 867 |
if ((pattern[0] == '.') |
| 868 |
|| ((pattern[0] == '\\') && (pattern[1] == '.'))) { |
| 869 |
matchSpecialDots = 1; |
| 870 |
} else { |
| 871 |
matchSpecialDots = 0; |
| 872 |
} |
| 873 |
|
| 874 |
/* |
| 875 |
* Now iterate over all of the files in the directory, starting |
| 876 |
* with the first one we found. |
| 877 |
*/ |
| 878 |
|
| 879 |
do { |
| 880 |
CONST char *utfname; |
| 881 |
int checkDrive = 0; |
| 882 |
int isDrive; |
| 883 |
DWORD attr; |
| 884 |
|
| 885 |
if (tclWinProcs->useWide) { |
| 886 |
native = (CONST TCHAR *) data.w.cFileName; |
| 887 |
attr = data.w.dwFileAttributes; |
| 888 |
} else { |
| 889 |
native = (CONST TCHAR *) data.a.cFileName; |
| 890 |
attr = data.a.dwFileAttributes; |
| 891 |
} |
| 892 |
|
| 893 |
utfname = Tcl_WinTCharToUtf(native, -1, &ds); |
| 894 |
|
| 895 |
if (!matchSpecialDots) { |
| 896 |
/* If it is exactly '.' or '..' then we ignore it */ |
| 897 |
if ((utfname[0] == '.') && (utfname[1] == '\0' |
| 898 |
|| (utfname[1] == '.' && utfname[2] == '\0'))) { |
| 899 |
Tcl_DStringFree(&ds); |
| 900 |
continue; |
| 901 |
} |
| 902 |
} else if (utfname[0] == '.' && utfname[1] == '.' |
| 903 |
&& utfname[2] == '\0') { |
| 904 |
/* |
| 905 |
* Have to check if this is a drive below, so we can |
| 906 |
* correctly match 'hidden' and not hidden files. |
| 907 |
*/ |
| 908 |
checkDrive = 1; |
| 909 |
} |
| 910 |
|
| 911 |
/* |
| 912 |
* Check to see if the file matches the pattern. Note that |
| 913 |
* we are ignoring the case sensitivity flag because Windows |
| 914 |
* doesn't honor case even if the volume is case sensitive. |
| 915 |
* If the volume also doesn't preserve case, then we |
| 916 |
* previously returned the lower case form of the name. This |
| 917 |
* didn't seem quite right since there are |
| 918 |
* non-case-preserving volumes that actually return mixed |
| 919 |
* case. So now we are returning exactly what we get from |
| 920 |
* the system. |
| 921 |
*/ |
| 922 |
|
| 923 |
if (Tcl_StringCaseMatch(utfname, pattern, 1)) { |
| 924 |
/* |
| 925 |
* If the file matches, then we need to process the remainder |
| 926 |
* of the path. |
| 927 |
*/ |
| 928 |
|
| 929 |
if (checkDrive) { |
| 930 |
CONST char *fullname = Tcl_DStringAppend(&dsOrig, utfname, |
| 931 |
Tcl_DStringLength(&ds)); |
| 932 |
isDrive = WinIsDrive(fullname, Tcl_DStringLength(&dsOrig)); |
| 933 |
Tcl_DStringSetLength(&dsOrig, dirLength); |
| 934 |
} else { |
| 935 |
isDrive = 0; |
| 936 |
} |
| 937 |
if (NativeMatchType(isDrive, attr, native, types)) { |
| 938 |
Tcl_ListObjAppendElement(interp, resultPtr, |
| 939 |
TclNewFSPathObj(pathPtr, utfname, |
| 940 |
Tcl_DStringLength(&ds))); |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
/* |
| 945 |
* Free ds here to ensure that native is valid above. |
| 946 |
*/ |
| 947 |
Tcl_DStringFree(&ds); |
| 948 |
} while ((*tclWinProcs->findNextFileProc)(handle, &data) == TRUE); |
| 949 |
|
| 950 |
FindClose(handle); |
| 951 |
Tcl_DStringFree(&dirString); |
| 952 |
Tcl_DStringFree(&dsOrig); |
| 953 |
return TCL_OK; |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
/* |
| 958 |
* Does the given path represent a root volume? We need this special |
| 959 |
* case because for NTFS root volumes, the getFileAttributesProc returns |
| 960 |
* a 'hidden' attribute when it should not. |
| 961 |
*/ |
| 962 |
static int |
| 963 |
WinIsDrive( |
| 964 |
CONST char *name, /* Name (UTF-8) */ |
| 965 |
int len) /* Length of name */ |
| 966 |
{ |
| 967 |
int remove = 0; |
| 968 |
while (len > 4) { |
| 969 |
if ((name[len-1] != '.' || name[len-2] != '.') |
| 970 |
|| (name[len-3] != '/' && name[len-3] != '\\')) { |
| 971 |
/* We don't have '/..' at the end */ |
| 972 |
if (remove == 0) { |
| 973 |
break; |
| 974 |
} |
| 975 |
remove--; |
| 976 |
while (len > 0) { |
| 977 |
len--; |
| 978 |
if (name[len] == '/' || name[len] == '\\') { |
| 979 |
break; |
| 980 |
} |
| 981 |
} |
| 982 |
if (len < 4) { |
| 983 |
len++; |
| 984 |
break; |
| 985 |
} |
| 986 |
} else { |
| 987 |
/* We do have '/..' */ |
| 988 |
len -= 3; |
| 989 |
remove++; |
| 990 |
} |
| 991 |
} |
| 992 |
if (len < 4) { |
| 993 |
if (len == 0) { |
| 994 |
/* |
| 995 |
* Not sure if this is possible, but we pass it on |
| 996 |
* anyway |
| 997 |
*/ |
| 998 |
} else if (len == 1 && (name[0] == '/' || name[0] == '\\')) { |
| 999 |
/* Path is pointing to the root volume */ |
| 1000 |
return 1; |
| 1001 |
} else if ((name[1] == ':') |
| 1002 |
&& (len == 2 || (name[2] == '/' || name[2] == '\\'))) { |
| 1003 |
/* Path is of the form 'x:' or 'x:/' or 'x:\' */ |
| 1004 |
return 1; |
| 1005 |
} |
| 1006 |
} |
| 1007 |
return 0; |
| 1008 |
} |
| 1009 |
|
| 1010 |
/* |
| 1011 |
*---------------------------------------------------------------------- |
| 1012 |
* |
| 1013 |
* NativeMatchType -- |
| 1014 |
* |
| 1015 |
* This function needs a special case for a path which is a root |
| 1016 |
* volume, because for NTFS root volumes, the getFileAttributesProc |
| 1017 |
* returns a 'hidden' attribute when it should not. |
| 1018 |
* |
| 1019 |
* We never make any calss to a 'get attributes' routine here, |
| 1020 |
* since we have arranged things so that our caller already knows |
| 1021 |
* such information. |
| 1022 |
* |
| 1023 |
* Results: |
| 1024 |
* 0 = file doesn't match |
| 1025 |
* 1 = file matches |
| 1026 |
* |
| 1027 |
*---------------------------------------------------------------------- |
| 1028 |
*/ |
| 1029 |
static int |
| 1030 |
NativeMatchType( |
| 1031 |
int isDrive, /* Is this a drive */ |
| 1032 |
DWORD attr, /* We already know the attributes |
| 1033 |
* for the file */ |
| 1034 |
CONST TCHAR* nativeName, /* Native path to check */ |
| 1035 |
Tcl_GlobTypeData *types) /* Type description to match against */ |
| 1036 |
{ |
| 1037 |
/* |
| 1038 |
* 'attr' represents the attributes of the file, but we only |
| 1039 |
* want to retrieve this info if it is absolutely necessary |
| 1040 |
* because it is an expensive call. Unfortunately, to deal |
| 1041 |
* with hidden files properly, we must always retrieve it. |
| 1042 |
*/ |
| 1043 |
|
| 1044 |
if (types == NULL) { |
| 1045 |
/* If invisible, don't return the file */ |
| 1046 |
if (attr & FILE_ATTRIBUTE_HIDDEN && !isDrive) { |
| 1047 |
return 0; |
| 1048 |
} |
| 1049 |
} else { |
| 1050 |
if (attr & FILE_ATTRIBUTE_HIDDEN && !isDrive) { |
| 1051 |
/* If invisible */ |
| 1052 |
if ((types->perm == 0) || |
| 1053 |
!(types->perm & TCL_GLOB_PERM_HIDDEN)) { |
| 1054 |
return 0; |
| 1055 |
} |
| 1056 |
} else { |
| 1057 |
/* Visible */ |
| 1058 |
if (types->perm & TCL_GLOB_PERM_HIDDEN) { |
| 1059 |
return 0; |
| 1060 |
} |
| 1061 |
} |
| 1062 |
|
| 1063 |
if (types->perm != 0) { |
| 1064 |
if ( |
| 1065 |
((types->perm & TCL_GLOB_PERM_RONLY) && |
| 1066 |
!(attr & FILE_ATTRIBUTE_READONLY)) || |
| 1067 |
((types->perm & TCL_GLOB_PERM_R) && |
| 1068 |
(0 /* File exists => R_OK on Windows */)) || |
| 1069 |
((types->perm & TCL_GLOB_PERM_W) && |
| 1070 |
(attr & FILE_ATTRIBUTE_READONLY)) || |
| 1071 |
((types->perm & TCL_GLOB_PERM_X) && |
| 1072 |
(!(attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1073 |
&& !NativeIsExec(nativeName))) |
| 1074 |
) { |
| 1075 |
return 0; |
| 1076 |
} |
| 1077 |
} |
| 1078 |
if ((types->type & TCL_GLOB_TYPE_DIR) |
| 1079 |
&& (attr & FILE_ATTRIBUTE_DIRECTORY)) { |
| 1080 |
/* Quicker test for directory, which is a common case */ |
| 1081 |
return 1; |
| 1082 |
} else if (types->type != 0) { |
| 1083 |
unsigned short st_mode; |
| 1084 |
int isExec = NativeIsExec(nativeName); |
| 1085 |
|
| 1086 |
st_mode = NativeStatMode(attr, 0, isExec); |
| 1087 |
|
| 1088 |
/* |
| 1089 |
* In order bcdpfls as in 'find -t' |
| 1090 |
*/ |
| 1091 |
if ( |
| 1092 |
((types->type & TCL_GLOB_TYPE_BLOCK) && |
| 1093 |
S_ISBLK(st_mode)) || |
| 1094 |
((types->type & TCL_GLOB_TYPE_CHAR) && |
| 1095 |
S_ISCHR(st_mode)) || |
| 1096 |
((types->type & TCL_GLOB_TYPE_DIR) && |
| 1097 |
S_ISDIR(st_mode)) || |
| 1098 |
((types->type & TCL_GLOB_TYPE_PIPE) && |
| 1099 |
S_ISFIFO(st_mode)) || |
| 1100 |
((types->type & TCL_GLOB_TYPE_FILE) && |
| 1101 |
S_ISREG(st_mode)) |
| 1102 |
#ifdef S_ISSOCK |
| 1103 |
|| ((types->type & TCL_GLOB_TYPE_SOCK) && |
| 1104 |
S_ISSOCK(st_mode)) |
| 1105 |
#endif |
| 1106 |
) { |
| 1107 |
/* Do nothing -- this file is ok */ |
| 1108 |
} else { |
| 1109 |
#ifdef S_ISLNK |
| 1110 |
if (types->type & TCL_GLOB_TYPE_LINK) { |
| 1111 |
st_mode = NativeStatMode(attr, 1, isExec); |
| 1112 |
if (S_ISLNK(st_mode)) { |
| 1113 |
return 1; |
| 1114 |
} |
| 1115 |
} |
| 1116 |
#endif |
| 1117 |
return 0; |
| 1118 |
} |
| 1119 |
} |
| 1120 |
} |
| 1121 |
return 1; |
| 1122 |
} |
| 1123 |
|
| 1124 |
/* |
| 1125 |
*---------------------------------------------------------------------- |
| 1126 |
* |
| 1127 |
* TclpGetUserHome -- |
| 1128 |
* |
| 1129 |
* This function takes the passed in user name and finds the |
| 1130 |
* corresponding home directory specified in the password file. |
| 1131 |
* |
| 1132 |
* Results: |
| 1133 |
* The result is a pointer to a string specifying the user's home |
| 1134 |
* directory, or NULL if the user's home directory could not be |
| 1135 |
* determined. Storage for the result string is allocated in |
| 1136 |
* bufferPtr; the caller must call Tcl_DStringFree() when the result |
| 1137 |
* is no longer needed. |
| 1138 |
* |
| 1139 |
* Side effects: |
| 1140 |
* None. |
| 1141 |
* |
| 1142 |
*---------------------------------------------------------------------- |
| 1143 |
*/ |
| 1144 |
|
| 1145 |
char * |
| 1146 |
TclpGetUserHome(name, bufferPtr) |
| 1147 |
CONST char *name; /* User name for desired home directory. */ |
| 1148 |
Tcl_DString *bufferPtr; /* Uninitialized or free DString filled |
| 1149 |
* with name of user's home directory. */ |
| 1150 |
{ |
| 1151 |
char *result; |
| 1152 |
HINSTANCE netapiInst; |
| 1153 |
|
| 1154 |
result = NULL; |
| 1155 |
|
| 1156 |
Tcl_DStringInit(bufferPtr); |
| 1157 |
|
| 1158 |
netapiInst = LoadLibraryA("netapi32.dll"); |
| 1159 |
if (netapiInst != NULL) { |
| 1160 |
NETAPIBUFFERFREEPROC *netApiBufferFreeProc; |
| 1161 |
NETGETDCNAMEPROC *netGetDCNameProc; |
| 1162 |
NETUSERGETINFOPROC *netUserGetInfoProc; |
| 1163 |
|
| 1164 |
netApiBufferFreeProc = (NETAPIBUFFERFREEPROC *) |
| 1165 |
GetProcAddress(netapiInst, "NetApiBufferFree"); |
| 1166 |
netGetDCNameProc = (NETGETDCNAMEPROC *) |
| 1167 |
GetProcAddress(netapiInst, "NetGetDCName"); |
| 1168 |
netUserGetInfoProc = (NETUSERGETINFOPROC *) |
| 1169 |
GetProcAddress(netapiInst, "NetUserGetInfo"); |
| 1170 |
if ((netUserGetInfoProc != NULL) && (netGetDCNameProc != NULL) |
| 1171 |
&& (netApiBufferFreeProc != NULL)) { |
| 1172 |
USER_INFO_1 *uiPtr; |
| 1173 |
Tcl_DString ds; |
| 1174 |
int nameLen, badDomain; |
| 1175 |
char *domain; |
| 1176 |
WCHAR *wName, *wHomeDir, *wDomain; |
| 1177 |
WCHAR buf[MAX_PATH]; |
| 1178 |
|
| 1179 |
badDomain = 0; |
| 1180 |
nameLen = -1; |
| 1181 |
wDomain = NULL; |
| 1182 |
domain = strchr(name, '@'); |
| 1183 |
if (domain != NULL) { |
| 1184 |
Tcl_DStringInit(&ds); |
| 1185 |
wName = Tcl_UtfToUniCharDString(domain + 1, -1, &ds); |
| 1186 |
badDomain = (*netGetDCNameProc)(NULL, wName, |
| 1187 |
(LPBYTE *) &wDomain); |
| 1188 |
Tcl_DStringFree(&ds); |
| 1189 |
nameLen = domain - name; |
| 1190 |
} |
| 1191 |
if (badDomain == 0) { |
| 1192 |
Tcl_DStringInit(&ds); |
| 1193 |
wName = Tcl_UtfToUniCharDString(name, nameLen, &ds); |
| 1194 |
if ((*netUserGetInfoProc)(wDomain, wName, 1, |
| 1195 |
(LPBYTE *) &uiPtr) == 0) { |
| 1196 |
wHomeDir = uiPtr->usri1_home_dir; |
| 1197 |
if ((wHomeDir != NULL) && (wHomeDir[0] != L'\0')) { |
| 1198 |
Tcl_UniCharToUtfDString(wHomeDir, lstrlenW(wHomeDir), |
| 1199 |
bufferPtr); |
| 1200 |
} else { |
| 1201 |
/* |
| 1202 |
* User exists but has no home dir. Return |
| 1203 |
* "{Windows Drive}:/users/default". |
| 1204 |
*/ |
| 1205 |
|
| 1206 |
GetWindowsDirectoryW(buf, MAX_PATH); |
| 1207 |
Tcl_UniCharToUtfDString(buf, 2, bufferPtr); |
| 1208 |
Tcl_DStringAppend(bufferPtr, "/users/default", -1); |
| 1209 |
} |
| 1210 |
result = Tcl_DStringValue(bufferPtr); |
| 1211 |
(*netApiBufferFreeProc)((void *) uiPtr); |
| 1212 |
} |
| 1213 |
Tcl_DStringFree(&ds); |
| 1214 |
} |
| 1215 |
if (wDomain != NULL) { |
| 1216 |
(*netApiBufferFreeProc)((void *) wDomain); |
| 1217 |
} |
| 1218 |
} |
| 1219 |
FreeLibrary(netapiInst); |
| 1220 |
} |
| 1221 |
if (result == NULL) { |
| 1222 |
/* |
| 1223 |
* Look in the "Password Lists" section of system.ini for the |
| 1224 |
* local user. There are also entries in that section that begin |
| 1225 |
* with a "*" character that are used by Windows for other |
| 1226 |
* purposes; ignore user names beginning with a "*". |
| 1227 |
*/ |
| 1228 |
|
| 1229 |
char buf[MAX_PATH]; |
| 1230 |
|
| 1231 |
if (name[0] != '*') { |
| 1232 |
if (GetPrivateProfileStringA("Password Lists", name, "", buf, |
| 1233 |
MAX_PATH, "system.ini") > 0) { |
| 1234 |
/* |
| 1235 |
* User exists, but there is no such thing as a home |
| 1236 |
* directory in system.ini. Return "{Windows drive}:/". |
| 1237 |
*/ |
| 1238 |
|
| 1239 |
GetWindowsDirectoryA(buf, MAX_PATH); |
| 1240 |
Tcl_DStringAppend(bufferPtr, buf, 3); |
| 1241 |
result = Tcl_DStringValue(bufferPtr); |
| 1242 |
} |
| 1243 |
} |
| 1244 |
} |
| 1245 |
|
| 1246 |
return result; |
| 1247 |
} |
| 1248 |
|
| 1249 |
/* |
| 1250 |
*--------------------------------------------------------------------------- |
| 1251 |
* |
| 1252 |
* NativeAccess -- |
| 1253 |
* |
| 1254 |
* This function replaces the library version of access(), fixing the |
| 1255 |
* following bugs: |
| 1256 |
* |
| 1257 |
* 1. access() returns that all files have execute permission. |
| 1258 |
* |
| 1259 |
* Results: |
| 1260 |
* See access documentation. |
| 1261 |
* |
| 1262 |
* Side effects: |
| 1263 |
* See access documentation. |
| 1264 |
* |
| 1265 |
*--------------------------------------------------------------------------- |
| 1266 |
*/ |
| 1267 |
|
| 1268 |
static int |
| 1269 |
NativeAccess( |
| 1270 |
CONST TCHAR *nativePath, /* Path of file to access (UTF-8). */ |
| 1271 |
int mode) /* Permission setting. */ |
| 1272 |
{ |
| 1273 |
DWORD attr; |
| 1274 |
|
| 1275 |
attr = (*tclWinProcs->getFileAttributesProc)(nativePath); |
| 1276 |
|
| 1277 |
if (attr == 0xffffffff) { |
| 1278 |
/* |
| 1279 |
* File doesn't exist. |
| 1280 |
*/ |
| 1281 |
|
| 1282 |
TclWinConvertError(GetLastError()); |
| 1283 |
return -1; |
| 1284 |
} |
| 1285 |
|
| 1286 |
if ((mode & W_OK) && (attr & FILE_ATTRIBUTE_READONLY)) { |
| 1287 |
/* |
| 1288 |
* File is not writable. |
| 1289 |
*/ |
| 1290 |
|
| 1291 |
Tcl_SetErrno(EACCES); |
| 1292 |
return -1; |
| 1293 |
} |
| 1294 |
|
| 1295 |
if (mode & X_OK) { |
| 1296 |
if (attr & FILE_ATTRIBUTE_DIRECTORY) { |
| 1297 |
/* |
| 1298 |
* Directories are always executable. |
| 1299 |
*/ |
| 1300 |
|
| 1301 |
return 0; |
| 1302 |
} |
| 1303 |
if (NativeIsExec(nativePath)) { |
| 1304 |
return 0; |
| 1305 |
} |
| 1306 |
Tcl_SetErrno(EACCES); |
| 1307 |
return -1; |
| 1308 |
} |
| 1309 |
|
| 1310 |
return 0; |
| 1311 |
} |
| 1312 |
|
| 1313 |
/* |
| 1314 |
*---------------------------------------------------------------------- |
| 1315 |
* |
| 1316 |
* NativeIsExec -- |
| 1317 |
* |
| 1318 |
* Determines if a path is executable. On windows this is |
| 1319 |
* simply defined by whether the path ends in any of ".exe", |
| 1320 |
* ".com", or ".bat" |
| 1321 |
* |
| 1322 |
* Results: |
| 1323 |
* 1 = executable, 0 = not. |
| 1324 |
* |
| 1325 |
*---------------------------------------------------------------------- |
| 1326 |
*/ |
| 1327 |
static int |
| 1328 |
NativeIsExec(nativePath) |
| 1329 |
CONST TCHAR *nativePath; |
| 1330 |
{ |
| 1331 |
if (tclWinProcs->useWide) { |
| 1332 |
CONST WCHAR *path; |
| 1333 |
int len; |
| 1334 |
|
| 1335 |
path = (CONST WCHAR*)nativePath; |
| 1336 |
len = wcslen(path); |
| 1337 |
|
| 1338 |
if (len < 5) { |
| 1339 |
return 0; |
| 1340 |
} |
| 1341 |
|
| 1342 |
if (path[len-4] != L'.') { |
| 1343 |
return 0; |
| 1344 |
} |
| 1345 |
|
| 1346 |
if ((memcmp((char*)(path+len-3),L"exe",3*sizeof(WCHAR)) == 0) |
| 1347 |
|| (memcmp((char*)(path+len-3),L"com",3*sizeof(WCHAR)) == 0) |
| 1348 |
|| (memcmp((char*)(path+len-3),L"bat",3*sizeof(WCHAR)) == 0)) { |
| 1349 |
return 1; |
| 1350 |
} |
| 1351 |
} else { |
| 1352 |
CONST char *p; |
| 1353 |
|
| 1354 |
/* We are only looking for pure ascii */ |
| 1355 |
|
| 1356 |
p = strrchr((CONST char*)nativePath, '.'); |
| 1357 |
if (p != NULL) { |
| 1358 |
p++; |
| 1359 |
/* |
| 1360 |
* Note: in the old code, stat considered '.pif' files as |
| 1361 |
* executable, whereas access did not. |
| 1362 |
*/ |
| 1363 |
if ((stricmp(p, "exe") == 0) |
| 1364 |
|| (stricmp(p, "com") == 0) |
| 1365 |
|| (stricmp(p, "bat") == 0)) { |
| 1366 |
/* |
| 1367 |
* File that ends with .exe, .com, or .bat is executable. |
| 1368 |
*/ |
| 1369 |
|
| 1370 |
return 1; |
| 1371 |
} |
| 1372 |
} |
| 1373 |
} |
| 1374 |
return 0; |
| 1375 |
} |
| 1376 |
|
| 1377 |
/* |
| 1378 |
*---------------------------------------------------------------------- |
| 1379 |
* |
| 1380 |
* TclpObjChdir -- |
| 1381 |
* |
| 1382 |
* This function replaces the library version of chdir(). |
| 1383 |
* |
| 1384 |
* Results: |
| 1385 |
* See chdir() documentation. |
| 1386 |
* |
| 1387 |
* Side effects: |
| 1388 |
* See chdir() documentation. |
| 1389 |
* |
| 1390 |
*---------------------------------------------------------------------- |
| 1391 |
*/ |
| 1392 |
|
| 1393 |
int |
| 1394 |
TclpObjChdir(pathPtr) |
| 1395 |
Tcl_Obj *pathPtr; /* Path to new working directory. */ |
| 1396 |
{ |
| 1397 |
int result; |
| 1398 |
CONST TCHAR *nativePath; |
| 1399 |
#ifdef __CYGWIN__ |
| 1400 |
extern int cygwin_conv_to_posix_path |
| 1401 |
_ANSI_ARGS_((CONST char *, char *)); |
| 1402 |
char posixPath[MAX_PATH+1]; |
| 1403 |
CONST char *path; |
| 1404 |
Tcl_DString ds; |
| 1405 |
#endif /* __CYGWIN__ */ |
| 1406 |
|
| 1407 |
nativePath = (CONST TCHAR *) Tcl_FSGetNativePath(pathPtr); |
| 1408 |
#ifdef __CYGWIN__ |
| 1409 |
/* Cygwin chdir only groks POSIX path. */ |
| 1410 |
path = Tcl_WinTCharToUtf(nativePath, -1, &ds); |
| 1411 |
cygwin_conv_to_posix_path(path, posixPath); |
| 1412 |
result = (chdir(posixPath) == 0 ? 1 : 0); |
| 1413 |
Tcl_DStringFree(&ds); |
| 1414 |
#else /* __CYGWIN__ */ |
| 1415 |
result = (*tclWinProcs->setCurrentDirectoryProc)(nativePath); |
| 1416 |
#endif /* __CYGWIN__ */ |
| 1417 |
|
| 1418 |
if (result == 0) { |
| 1419 |
TclWinConvertError(GetLastError()); |
| 1420 |
return -1; |
| 1421 |
} |
| 1422 |
return 0; |
| 1423 |
} |
| 1424 |
|
| 1425 |
#ifdef __CYGWIN__ |
| 1426 |
/* |
| 1427 |
*--------------------------------------------------------------------------- |
| 1428 |
* |
| 1429 |
* TclpReadlink -- |
| 1430 |
* |
| 1431 |
* This function replaces the library version of readlink(). |
| 1432 |
* |
| 1433 |
* Results: |
| 1434 |
* The result is a pointer to a string specifying the contents |
| 1435 |
* of the symbolic link given by 'path', or NULL if the symbolic |
| 1436 |
* link could not be read. Storage for the result string is |
| 1437 |
* allocated in bufferPtr; the caller must call Tcl_DStringFree() |
| 1438 |
* when the result is no longer needed. |
| 1439 |
* |
| 1440 |
* Side effects: |
| 1441 |
* See readlink() documentation. |
| 1442 |
* |
| 1443 |
*--------------------------------------------------------------------------- |
| 1444 |
*/ |
| 1445 |
|
| 1446 |
char * |
| 1447 |
TclpReadlink(path, linkPtr) |
| 1448 |
CONST char *path; /* Path of file to readlink (UTF-8). */ |
| 1449 |
Tcl_DString *linkPtr; /* Uninitialized or free DString filled |
| 1450 |
* with contents of link (UTF-8). */ |
| 1451 |
{ |
| 1452 |
char link[MAXPATHLEN]; |
| 1453 |
int length; |
| 1454 |
char *native; |
| 1455 |
Tcl_DString ds; |
| 1456 |
|
| 1457 |
native = Tcl_UtfToExternalDString(NULL, path, -1, &ds); |
| 1458 |
length = readlink(native, link, sizeof(link)); /* INTL: Native. */ |
| 1459 |
Tcl_DStringFree(&ds); |
| 1460 |
|
| 1461 |
if (length < 0) { |
| 1462 |
return NULL; |
| 1463 |
} |
| 1464 |
|
| 1465 |
Tcl_ExternalToUtfDString(NULL, link, length, linkPtr); |
| 1466 |
return Tcl_DStringValue(linkPtr); |
| 1467 |
} |
| 1468 |
#endif /* __CYGWIN__ */ |
| 1469 |
|
| 1470 |
/* |
| 1471 |
*---------------------------------------------------------------------- |
| 1472 |
* |
| 1473 |
* TclpGetCwd -- |
| 1474 |
* |
| 1475 |
* This function replaces the library version of getcwd(). |
| 1476 |
* |
| 1477 |
* Results: |
| 1478 |
* The result is a pointer to a string specifying the current |
| 1479 |
* directory, or NULL if the current directory could not be |
| 1480 |
* determined. If NULL is returned, an error message is left in the |
| 1481 |
* interp's result. Storage for the result string is allocated in |
| 1482 |
* bufferPtr; the caller must call Tcl_DStringFree() when the result |
| 1483 |
* is no longer needed. |
| 1484 |
* |
| 1485 |
* Side effects: |
| 1486 |
* None. |
| 1487 |
* |
| 1488 |
*---------------------------------------------------------------------- |
| 1489 |
*/ |
| 1490 |
|
| 1491 |
CONST char * |
| 1492 |
TclpGetCwd(interp, bufferPtr) |
| 1493 |
Tcl_Interp *interp; /* If non-NULL, used for error reporting. */ |
| 1494 |
Tcl_DString *bufferPtr; /* Uninitialized or free DString filled |
| 1495 |
* with name of current directory. */ |
| 1496 |
{ |
| 1497 |
WCHAR buffer[MAX_PATH]; |
| 1498 |
char *p; |
| 1499 |
|
| 1500 |
if ((*tclWinProcs->getCurrentDirectoryProc)(MAX_PATH, buffer) == 0) { |
| 1501 |
TclWinConvertError(GetLastError()); |
| 1502 |
if (interp != NULL) { |
| 1503 |
Tcl_AppendResult(interp, |
| 1504 |
"error getting working directory name: ", |
| 1505 |
Tcl_PosixError(interp), (char *) NULL); |
| 1506 |
} |
| 1507 |
return NULL; |
| 1508 |
} |
| 1509 |
|
| 1510 |
/* |
| 1511 |
* Watch for the weird Windows c:\\UNC syntax. |
| 1512 |
*/ |
| 1513 |
|
| 1514 |
if (tclWinProcs->useWide) { |
| 1515 |
WCHAR *native; |
| 1516 |
|
| 1517 |
native = (WCHAR *) buffer; |
| 1518 |
if ((native[0] != '\0') && (native[1] == ':') |
| 1519 |
&& (native[2] == '\\') && (native[3] == '\\')) { |
| 1520 |
native += 2; |
| 1521 |
} |
| 1522 |
Tcl_WinTCharToUtf((TCHAR *) native, -1, bufferPtr); |
| 1523 |
} else { |
| 1524 |
char *native; |
| 1525 |
|
| 1526 |
native = (char *) buffer; |
| 1527 |
if ((native[0] != '\0') && (native[1] == ':') |
| 1528 |
&& (native[2] == '\\') && (native[3] == '\\')) { |
| 1529 |
native += 2; |
| 1530 |
} |
| 1531 |
Tcl_WinTCharToUtf((TCHAR *) native, -1, bufferPtr); |
| 1532 |
} |
| 1533 |
|
| 1534 |
/* |
| 1535 |
* Convert to forward slashes for easier use in scripts. |
| 1536 |
*/ |
| 1537 |
|
| 1538 |
for (p = Tcl_DStringValue(bufferPtr); *p != '\0'; p++) { |
| 1539 |
if (*p == '\\') { |
| 1540 |
*p = '/'; |
| 1541 |
} |
| 1542 |
} |
| 1543 |
return Tcl_DStringValue(bufferPtr); |
| 1544 |
} |
| 1545 |
|
| 1546 |
int |
| 1547 |
TclpObjStat(pathPtr, statPtr) |
| 1548 |
Tcl_Obj *pathPtr; /* Path of file to stat */ |
| 1549 |
Tcl_StatBuf *statPtr; /* Filled with results of stat call. */ |
| 1550 |
{ |
| 1551 |
#ifdef OLD_API |
| 1552 |
Tcl_Obj *transPtr; |
| 1553 |
/* |
| 1554 |
* Eliminate file names containing wildcard characters, or subsequent |
| 1555 |
* call to FindFirstFile() will expand them, matching some other file. |
| 1556 |
*/ |
| 1557 |
|
| 1558 |
transPtr = Tcl_FSGetTranslatedPath(NULL, pathPtr); |
| 1559 |
if (transPtr == NULL || (strpbrk(Tcl_GetString(transPtr), "?*") != NULL)) { |
| 1560 |
if (transPtr != NULL) { |
| 1561 |
Tcl_DecrRefCount(transPtr); |
| 1562 |
} |
| 1563 |
Tcl_SetErrno(ENOENT); |
| 1564 |
return -1; |
| 1565 |
} |
| 1566 |
Tcl_DecrRefCount(transPtr); |
| 1567 |
#endif |
| 1568 |
|
| 1569 |
/* |
| 1570 |
* Ensure correct file sizes by forcing the OS to write any |
| 1571 |
* pending data to disk. This is done only for channels which are |
| 1572 |
* dirty, i.e. have been written to since the last flush here. |
| 1573 |
*/ |
| 1574 |
|
| 1575 |
TclWinFlushDirtyChannels (); |
| 1576 |
|
| 1577 |
return NativeStat((CONST TCHAR*) Tcl_FSGetNativePath(pathPtr), statPtr, 0); |
| 1578 |
} |
| 1579 |
|
| 1580 |
/* |
| 1581 |
*---------------------------------------------------------------------- |
| 1582 |
* |
| 1583 |
* NativeStat -- |
| 1584 |
* |
| 1585 |
* This function replaces the library version of stat(), fixing |
| 1586 |
* the following bugs: |
| 1587 |
* |
| 1588 |
* 1. stat("c:") returns an error. |
| 1589 |
* 2. Borland stat() return time in GMT instead of localtime. |
| 1590 |
* 3. stat("\\server\mount") would return error. |
| 1591 |
* 4. Accepts slashes or backslashes. |
| 1592 |
* 5. st_dev and st_rdev were wrong for UNC paths. |
| 1593 |
* |
| 1594 |
* Results: |
| 1595 |
* See stat documentation. |
| 1596 |
* |
| 1597 |
* Side effects: |
| 1598 |
* See stat documentation. |
| 1599 |
* |
| 1600 |
*---------------------------------------------------------------------- |
| 1601 |
*/ |
| 1602 |
|
| 1603 |
static int |
| 1604 |
NativeStat(nativePath, statPtr, checkLinks) |
| 1605 |
CONST TCHAR *nativePath; /* Path of file to stat */ |
| 1606 |
Tcl_StatBuf *statPtr; /* Filled with results of stat call. */ |
| 1607 |
int checkLinks; /* If non-zero, behave like 'lstat' */ |
| 1608 |
{ |
| 1609 |
Tcl_DString ds; |
| 1610 |
DWORD attr; |
| 1611 |
WCHAR nativeFullPath[MAX_PATH]; |
| 1612 |
TCHAR *nativePart; |
| 1613 |
CONST char *fullPath; |
| 1614 |
int dev; |
| 1615 |
unsigned short mode; |
| 1616 |
|
| 1617 |
if (tclWinProcs->getFileAttributesExProc == NULL) { |
| 1618 |
/* |
| 1619 |
* We don't have the faster attributes proc, so we're |
| 1620 |
* probably running on Win95 |
| 1621 |
*/ |
| 1622 |
WIN32_FIND_DATAT data; |
| 1623 |
HANDLE handle; |
| 1624 |
|
| 1625 |
handle = (*tclWinProcs->findFirstFileProc)(nativePath, &data); |
| 1626 |
if (handle == INVALID_HANDLE_VALUE) { |
| 1627 |
/* |
| 1628 |
* FindFirstFile() doesn't work on root directories, so call |
| 1629 |
* GetFileAttributes() to see if the specified file exists. |
| 1630 |
*/ |
| 1631 |
|
| 1632 |
attr = (*tclWinProcs->getFileAttributesProc)(nativePath); |
| 1633 |
if (attr == 0xffffffff) { |
| 1634 |
Tcl_SetErrno(ENOENT); |
| 1635 |
return -1; |
| 1636 |
} |
| 1637 |
|
| 1638 |
/* |
| 1639 |
* Make up some fake information for this file. It has the |
| 1640 |
* correct file attributes and a time of 0. |
| 1641 |
*/ |
| 1642 |
|
| 1643 |
memset(&data, 0, sizeof(data)); |
| 1644 |
data.a.dwFileAttributes = attr; |
| 1645 |
} else { |
| 1646 |
FindClose(handle); |
| 1647 |
} |
| 1648 |
|
| 1649 |
|
| 1650 |
(*tclWinProcs->getFullPathNameProc)(nativePath, MAX_PATH, nativeFullPath, |
| 1651 |
&nativePart); |
| 1652 |
|
| 1653 |
fullPath = Tcl_WinTCharToUtf((TCHAR *) nativeFullPath, -1, &ds); |
| 1654 |
|
| 1655 |
dev = -1; |
| 1656 |
if ((fullPath[0] == '\\') && (fullPath[1] == '\\')) { |
| 1657 |
CONST char *p; |
| 1658 |
DWORD dw; |
| 1659 |
CONST TCHAR *nativeVol; |
| 1660 |
Tcl_DString volString; |
| 1661 |
|
| 1662 |
p = strchr(fullPath + 2, '\\'); |
| 1663 |
p = strchr(p + 1, '\\'); |
| 1664 |
if (p == NULL) { |
| 1665 |
/* |
| 1666 |
* Add terminating backslash to fullpath or |
| 1667 |
* GetVolumeInformation() won't work. |
| 1668 |
*/ |
| 1669 |
|
| 1670 |
fullPath = Tcl_DStringAppend(&ds, "\\", 1); |
| 1671 |
p = fullPath + Tcl_DStringLength(&ds); |
| 1672 |
} else { |
| 1673 |
p++; |
| 1674 |
} |
| 1675 |
nativeVol = Tcl_WinUtfToTChar(fullPath, p - fullPath, &volString); |
| 1676 |
dw = (DWORD) -1; |
| 1677 |
(*tclWinProcs->getVolumeInformationProc)(nativeVol, NULL, 0, &dw, |
| 1678 |
NULL, NULL, NULL, 0); |
| 1679 |
/* |
| 1680 |
* GetFullPathName() turns special devices like "NUL" into |
| 1681 |
* "\\.\NUL", but GetVolumeInformation() returns failure for |
| 1682 |
* "\\.\NUL". This will cause "NUL" to get a drive number of |
| 1683 |
* -1, which makes about as much sense as anything since the |
| 1684 |
* special devices don't live on any drive. |
| 1685 |
*/ |
| 1686 |
|
| 1687 |
dev = dw; |
| 1688 |
Tcl_DStringFree(&volString); |
| 1689 |
} else if ((fullPath[0] != '\0') && (fullPath[1] == ':')) { |
| 1690 |
dev = Tcl_UniCharToLower(fullPath[0]) - 'a'; |
| 1691 |
} |
| 1692 |
Tcl_DStringFree(&ds); |
| 1693 |
|
| 1694 |
attr = data.a.dwFileAttributes; |
| 1695 |
|
| 1696 |
statPtr->st_size = ((Tcl_WideInt)data.a.nFileSizeLow) | |
| 1697 |
(((Tcl_WideInt)data.a.nFileSizeHigh) << 32); |
| 1698 |
statPtr->st_atime = ToCTime(data.a.ftLastAccessTime); |
| 1699 |
statPtr->st_mtime = ToCTime(data.a.ftLastWriteTime); |
| 1700 |
statPtr->st_ctime = ToCTime(data.a.ftCreationTime); |
| 1701 |
} else { |
| 1702 |
WIN32_FILE_ATTRIBUTE_DATA data; |
| 1703 |
if((*tclWinProcs->getFileAttributesExProc)(nativePath, |
| 1704 |
GetFileExInfoStandard, |
| 1705 |
&data) != TRUE) { |
| 1706 |
Tcl_SetErrno(ENOENT); |
| 1707 |
return -1; |
| 1708 |
} |
| 1709 |
|
| 1710 |
|
| 1711 |
(*tclWinProcs->getFullPathNameProc)(nativePath, MAX_PATH, |
| 1712 |
nativeFullPath, &nativePart); |
| 1713 |
|
| 1714 |
fullPath = Tcl_WinTCharToUtf((TCHAR *) nativeFullPath, -1, &ds); |
| 1715 |
|
| 1716 |
dev = -1; |
| 1717 |
if ((fullPath[0] == '\\') && (fullPath[1] == '\\')) { |
| 1718 |
CONST char *p; |
| 1719 |
DWORD dw; |
| 1720 |
CONST TCHAR *nativeVol; |
| 1721 |
Tcl_DString volString; |
| 1722 |
|
| 1723 |
p = strchr(fullPath + 2, '\\'); |
| 1724 |
p = strchr(p + 1, '\\'); |
| 1725 |
if (p == NULL) { |
| 1726 |
/* |
| 1727 |
* Add terminating backslash to fullpath or |
| 1728 |
* GetVolumeInformation() won't work. |
| 1729 |
*/ |
| 1730 |
|
| 1731 |
fullPath = Tcl_DStringAppend(&ds, "\\", 1); |
| 1732 |
p = fullPath + Tcl_DStringLength(&ds); |
| 1733 |
} else { |
| 1734 |
p++; |
| 1735 |
} |
| 1736 |
nativeVol = Tcl_WinUtfToTChar(fullPath, p - fullPath, &volString); |
| 1737 |
dw = (DWORD) -1; |
| 1738 |
(*tclWinProcs->getVolumeInformationProc)(nativeVol, NULL, 0, &dw, |
| 1739 |
NULL, NULL, NULL, 0); |
| 1740 |
/* |
| 1741 |
* GetFullPathName() turns special devices like "NUL" into |
| 1742 |
* "\\.\NUL", but GetVolumeInformation() returns failure for |
| 1743 |
* "\\.\NUL". This will cause "NUL" to get a drive number of |
| 1744 |
* -1, which makes about as much sense as anything since the |
| 1745 |
* special devices don't live on any drive. |
| 1746 |
*/ |
| 1747 |
|
| 1748 |
dev = dw; |
| 1749 |
Tcl_DStringFree(&volString); |
| 1750 |
} else if ((fullPath[0] != '\0') && (fullPath[1] == ':')) { |
| 1751 |
dev = Tcl_UniCharToLower(fullPath[0]) - 'a'; |
| 1752 |
} |
| 1753 |
Tcl_DStringFree(&ds); |
| 1754 |
|
| 1755 |
attr = data.dwFileAttributes; |
| 1756 |
|
| 1757 |
statPtr->st_size = ((Tcl_WideInt)data.nFileSizeLow) | |
| 1758 |
(((Tcl_WideInt)data.nFileSizeHigh) << 32); |
| 1759 |
statPtr->st_atime = ToCTime(data.ftLastAccessTime); |
| 1760 |
statPtr->st_mtime = ToCTime(data.ftLastWriteTime); |
| 1761 |
statPtr->st_ctime = ToCTime(data.ftCreationTime); |
| 1762 |
} |
| 1763 |
|
| 1764 |
mode = NativeStatMode(attr, checkLinks, NativeIsExec(nativePath)); |
| 1765 |
|
| 1766 |
statPtr->st_dev = (dev_t) dev; |
| 1767 |
statPtr->st_ino = 0; |
| 1768 |
statPtr->st_mode = mode; |
| 1769 |
statPtr->st_nlink = 1; |
| 1770 |
statPtr->st_uid = 0; |
| 1771 |
statPtr->st_gid = 0; |
| 1772 |
statPtr->st_rdev = (dev_t) dev; |
| 1773 |
return 0; |
| 1774 |
} |
| 1775 |
|
| 1776 |
/* |
| 1777 |
*---------------------------------------------------------------------- |
| 1778 |
* |
| 1779 |
* NativeStatMode -- |
| 1780 |
* |
| 1781 |
* Calculate just the 'st_mode' field of a 'stat' structure. |
| 1782 |
* |
| 1783 |
*---------------------------------------------------------------------- |
| 1784 |
*/ |
| 1785 |
static unsigned short |
| 1786 |
NativeStatMode(DWORD attr, int checkLinks, int isExec) |
| 1787 |
{ |
| 1788 |
int mode; |
| 1789 |
if (checkLinks && (attr & FILE_ATTRIBUTE_REPARSE_POINT)) { |
| 1790 |
/* It is a link */ |
| 1791 |
mode = S_IFLNK; |
| 1792 |
} else { |
| 1793 |
mode = (attr & FILE_ATTRIBUTE_DIRECTORY) ? S_IFDIR | S_IEXEC : S_IFREG; |
| 1794 |
} |
| 1795 |
mode |= (attr & FILE_ATTRIBUTE_READONLY) ? S_IREAD : S_IREAD | S_IWRITE; |
| 1796 |
if (isExec) { |
| 1797 |
mode |= S_IEXEC; |
| 1798 |
} |
| 1799 |
|
| 1800 |
/* |
| 1801 |
* Propagate the S_IREAD, S_IWRITE, S_IEXEC bits to the group and |
| 1802 |
* other positions. |
| 1803 |
*/ |
| 1804 |
|
| 1805 |
mode |= (mode & 0x0700) >> 3; |
| 1806 |
mode |= (mode & 0x0700) >> 6; |
| 1807 |
return (unsigned short)mode; |
| 1808 |
} |
| 1809 |
|
| 1810 |
static time_t |
| 1811 |
ToCTime( |
| 1812 |
FILETIME fileTime) /* UTC Time to convert to local time_t. */ |
| 1813 |
{ |
| 1814 |
FILETIME localFileTime; |
| 1815 |
SYSTEMTIME systemTime; |
| 1816 |
struct tm tm; |
| 1817 |
|
| 1818 |
if (FileTimeToLocalFileTime(&fileTime, &localFileTime) == 0) { |
| 1819 |
return 0; |
| 1820 |
} |
| 1821 |
if (FileTimeToSystemTime(&localFileTime, &systemTime) == 0) { |
| 1822 |
return 0; |
| 1823 |
} |
| 1824 |
tm.tm_sec = systemTime.wSecond; |
| 1825 |
tm.tm_min = systemTime.wMinute; |
| 1826 |
tm.tm_hour = systemTime.wHour; |
| 1827 |
tm.tm_mday = systemTime.wDay; |
| 1828 |
tm.tm_mon = systemTime.wMonth - 1; |
| 1829 |
tm.tm_year = systemTime.wYear - 1900; |
| 1830 |
tm.tm_wday = 0; |
| 1831 |
tm.tm_yday = 0; |
| 1832 |
tm.tm_isdst = -1; |
| 1833 |
|
| 1834 |
return mktime(&tm); |
| 1835 |
} |
| 1836 |
|
| 1837 |
#if 0 |
| 1838 |
|
| 1839 |
/* |
| 1840 |
* Borland's stat doesn't take into account localtime. |
| 1841 |
*/ |
| 1842 |
|
| 1843 |
if ((result == 0) && (buf->st_mtime != 0)) { |
| 1844 |
TIME_ZONE_INFORMATION tz; |
| 1845 |
int time, bias; |
| 1846 |
|
| 1847 |
time = GetTimeZoneInformation(&tz); |
| 1848 |
bias = tz.Bias; |
| 1849 |
if (time == TIME_ZONE_ID_DAYLIGHT) { |
| 1850 |
bias += tz.DaylightBias; |
| 1851 |
} |
| 1852 |
bias *= 60; |
| 1853 |
buf->st_atime -= bias; |
| 1854 |
buf->st_ctime -= bias; |
| 1855 |
buf->st_mtime -= bias; |
| 1856 |
} |
| 1857 |
|
| 1858 |
#endif |
| 1859 |
|
| 1860 |
|
| 1861 |
#if 0 |
| 1862 |
/* |
| 1863 |
*------------------------------------------------------------------------- |
| 1864 |
* |
| 1865 |
* TclWinResolveShortcut -- |
| 1866 |
* |
| 1867 |
* Resolve a potential Windows shortcut to get the actual file or |
| 1868 |
* directory in question. |
| 1869 |
* |
| 1870 |
* Results: |
| 1871 |
* Returns 1 if the shortcut could be resolved, or 0 if there was |
| 1872 |
* an error or if the filename was not a shortcut. |
| 1873 |
* If bufferPtr did hold the name of a shortcut, it is modified to |
| 1874 |
* hold the resolved target of the shortcut instead. |
| 1875 |
* |
| 1876 |
* Side effects: |
| 1877 |
* Loads and unloads OLE package to determine if filename refers to |
| 1878 |
* a shortcut. |
| 1879 |
* |
| 1880 |
*------------------------------------------------------------------------- |
| 1881 |
*/ |
| 1882 |
|
| 1883 |
int |
| 1884 |
TclWinResolveShortcut(bufferPtr) |
| 1885 |
Tcl_DString *bufferPtr; /* Holds name of file to resolve. On |
| 1886 |
* return, holds resolved file name. */ |
| 1887 |
{ |
| 1888 |
HRESULT hres; |
| 1889 |
IShellLink *psl; |
| 1890 |
IPersistFile *ppf; |
| 1891 |
WIN32_FIND_DATA wfd; |
| 1892 |
WCHAR wpath[MAX_PATH]; |
| 1893 |
char *path, *ext; |
| 1894 |
char realFileName[MAX_PATH]; |
| 1895 |
|
| 1896 |
/* |
| 1897 |
* Windows system calls do not automatically resolve |
| 1898 |
* shortcuts like UNIX automatically will with symbolic links. |
| 1899 |
*/ |
| 1900 |
|
| 1901 |
path = Tcl_DStringValue(bufferPtr); |
| 1902 |
ext = strrchr(path, '.'); |
| 1903 |
if ((ext == NULL) || (stricmp(ext, ".lnk") != 0)) { |
| 1904 |
return 0; |
| 1905 |
} |
| 1906 |
|
| 1907 |
CoInitialize(NULL); |
| 1908 |
path = Tcl_DStringValue(bufferPtr); |
| 1909 |
realFileName[0] = '\0'; |
| 1910 |
hres = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, |
| 1911 |
&IID_IShellLink, &psl); |
| 1912 |
if (SUCCEEDED(hres)) { |
| 1913 |
hres = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile, &ppf); |
| 1914 |
if (SUCCEEDED(hres)) { |
| 1915 |
MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, sizeof(wpath)); |
| 1916 |
hres = ppf->lpVtbl->Load(ppf, wpath, STGM_READ); |
| 1917 |
if (SUCCEEDED(hres)) { |
| 1918 |
hres = psl->lpVtbl->Resolve(psl, NULL, |
| 1919 |
SLR_ANY_MATCH | SLR_NO_UI); |
| 1920 |
if (SUCCEEDED(hres)) { |
| 1921 |
hres = psl->lpVtbl->GetPath(psl, realFileName, MAX_PATH, |
| 1922 |
&wfd, 0); |
| 1923 |
} |
| 1924 |
} |
| 1925 |
ppf->lpVtbl->Release(ppf); |
| 1926 |
} |
| 1927 |
psl->lpVtbl->Release(psl); |
| 1928 |
} |
| 1929 |
CoUninitialize(); |
| 1930 |
|
| 1931 |
if (realFileName[0] != '\0') { |
| 1932 |
Tcl_DStringSetLength(bufferPtr, 0); |
| 1933 |
Tcl_DStringAppend(bufferPtr, realFileName, -1); |
| 1934 |
return 1; |
| 1935 |
} |
| 1936 |
return 0; |
| 1937 |
} |
| 1938 |
#endif |
| 1939 |
|
| 1940 |
Tcl_Obj* |
| 1941 |
TclpObjGetCwd(interp) |
| 1942 |
Tcl_Interp *interp; |
| 1943 |
{ |
| 1944 |
Tcl_DString ds; |
| 1945 |
if (TclpGetCwd(interp, &ds) != NULL) { |
| 1946 |
Tcl_Obj *cwdPtr = Tcl_NewStringObj(Tcl_DStringValue(&ds), -1); |
| 1947 |
Tcl_IncrRefCount(cwdPtr); |
| 1948 |
Tcl_DStringFree(&ds); |
| 1949 |
return cwdPtr; |
| 1950 |
} else { |
| 1951 |
return NULL; |
| 1952 |
} |
| 1953 |
} |
| 1954 |
|
| 1955 |
int |
| 1956 |
TclpObjAccess(pathPtr, mode) |
| 1957 |
Tcl_Obj *pathPtr; |
| 1958 |
int mode; |
| 1959 |
{ |
| 1960 |
return NativeAccess((CONST TCHAR*) Tcl_FSGetNativePath(pathPtr), mode); |
| 1961 |
} |
| 1962 |
|
| 1963 |
int |
| 1964 |
TclpObjLstat(pathPtr, statPtr) |
| 1965 |
Tcl_Obj *pathPtr; |
| 1966 |
Tcl_StatBuf *statPtr; |
| 1967 |
{ |
| 1968 |
/* |
| 1969 |
* Ensure correct file sizes by forcing the OS to write any |
| 1970 |
* pending data to disk. This is done only for channels which are |
| 1971 |
* dirty, i.e. have been written to since the last flush here. |
| 1972 |
*/ |
| 1973 |
|
| 1974 |
TclWinFlushDirtyChannels (); |
| 1975 |
|
| 1976 |
return NativeStat((CONST TCHAR*) Tcl_FSGetNativePath(pathPtr), statPtr, 1); |
| 1977 |
} |
| 1978 |
|
| 1979 |
#ifdef S_IFLNK |
| 1980 |
|
| 1981 |
Tcl_Obj* |
| 1982 |
TclpObjLink(pathPtr, toPtr, linkAction) |
| 1983 |
Tcl_Obj *pathPtr; |
| 1984 |
Tcl_Obj *toPtr; |
| 1985 |
int linkAction; |
| 1986 |
{ |
| 1987 |
if (toPtr != NULL) { |
| 1988 |
int res; |
| 1989 |
TCHAR* LinkTarget = (TCHAR*)Tcl_FSGetNativePath(toPtr); |
| 1990 |
TCHAR* LinkSource = (TCHAR*)Tcl_FSGetNativePath(pathPtr); |
| 1991 |
if (LinkSource == NULL || LinkTarget == NULL) { |
| 1992 |
return NULL; |
| 1993 |
} |
| 1994 |
res = WinLink(LinkSource, LinkTarget, linkAction); |
| 1995 |
if (res == 0) { |
| 1996 |
return toPtr; |
| 1997 |
} else { |
| 1998 |
return NULL; |
| 1999 |
} |
| 2000 |
} else { |
| 2001 |
TCHAR* LinkSource = (TCHAR*)Tcl_FSGetNativePath(pathPtr); |
| 2002 |
if (LinkSource == NULL) { |
| 2003 |
return NULL; |
| 2004 |
} |
| 2005 |
return WinReadLink(LinkSource); |
| 2006 |
} |
| 2007 |
} |
| 2008 |
|
| 2009 |
#endif |
| 2010 |
|
| 2011 |
|
| 2012 |
/* |
| 2013 |
*--------------------------------------------------------------------------- |
| 2014 |
* |
| 2015 |
* TclpFilesystemPathType -- |
| 2016 |
* |
| 2017 |
* This function is part of the native filesystem support, and |
| 2018 |
* returns the path type of the given path. Returns NTFS or FAT |
| 2019 |
* or whatever is returned by the 'volume information' proc. |
| 2020 |
* |
| 2021 |
* Results: |
| 2022 |
* NULL at present. |
| 2023 |
* |
| 2024 |
* Side effects: |
| 2025 |
* None. |
| 2026 |
* |
| 2027 |
*--------------------------------------------------------------------------- |
| 2028 |
*/ |
| 2029 |
Tcl_Obj* |
| 2030 |
TclpFilesystemPathType(pathObjPtr) |
| 2031 |
Tcl_Obj* pathObjPtr; |
| 2032 |
{ |
| 2033 |
#define VOL_BUF_SIZE 32 |
| 2034 |
int found; |
| 2035 |
char volType[VOL_BUF_SIZE]; |
| 2036 |
char* firstSeparator; |
| 2037 |
CONST char *path; |
| 2038 |
|
| 2039 |
Tcl_Obj *normPath = Tcl_FSGetNormalizedPath(NULL, pathObjPtr); |
| 2040 |
if (normPath == NULL) return NULL; |
| 2041 |
path = Tcl_GetString(normPath); |
| 2042 |
if (path == NULL) return NULL; |
| 2043 |
|
| 2044 |
firstSeparator = strchr(path, '/'); |
| 2045 |
if (firstSeparator == NULL) { |
| 2046 |
found = tclWinProcs->getVolumeInformationProc( |
| 2047 |
Tcl_FSGetNativePath(pathObjPtr), NULL, 0, NULL, NULL, |
| 2048 |
NULL, (WCHAR *)volType, VOL_BUF_SIZE); |
| 2049 |
} else { |
| 2050 |
Tcl_Obj *driveName = Tcl_NewStringObj(path, firstSeparator - path+1); |
| 2051 |
Tcl_IncrRefCount(driveName); |
| 2052 |
found = tclWinProcs->getVolumeInformationProc( |
| 2053 |
Tcl_FSGetNativePath(driveName), NULL, 0, NULL, NULL, |
| 2054 |
NULL, (WCHAR *)volType, VOL_BUF_SIZE); |
| 2055 |
Tcl_DecrRefCount(driveName); |
| 2056 |
} |
| 2057 |
|
| 2058 |
if (found == 0) { |
| 2059 |
return NULL; |
| 2060 |
} else { |
| 2061 |
Tcl_DString ds; |
| 2062 |
Tcl_Obj *objPtr; |
| 2063 |
|
| 2064 |
Tcl_WinTCharToUtf(volType, -1, &ds); |
| 2065 |
objPtr = Tcl_NewStringObj(Tcl_DStringValue(&ds),Tcl_DStringLength(&ds)); |
| 2066 |
Tcl_DStringFree(&ds); |
| 2067 |
return objPtr; |
| 2068 |
} |
| 2069 |
#undef VOL_BUF_SIZE |
| 2070 |
} |
| 2071 |
|
| 2072 |
|
| 2073 |
/* |
| 2074 |
*--------------------------------------------------------------------------- |
| 2075 |
* |
| 2076 |
* TclpObjNormalizePath -- |
| 2077 |
* |
| 2078 |
* This function scans through a path specification and replaces it, |
| 2079 |
* in place, with a normalized version. This means using the |
| 2080 |
* 'longname', and expanding any symbolic links contained within the |
| 2081 |
* path. |
| 2082 |
* |
| 2083 |
* Results: |
| 2084 |
* The new 'nextCheckpoint' value, giving as far as we could |
| 2085 |
* understand in the path. |
| 2086 |
* |
| 2087 |
* Side effects: |
| 2088 |
* The pathPtr string, which must contain a valid path, is |
| 2089 |
* possibly modified in place. |
| 2090 |
* |
| 2091 |
*--------------------------------------------------------------------------- |
| 2092 |
*/ |
| 2093 |
|
| 2094 |
int |
| 2095 |
TclpObjNormalizePath(interp, pathPtr, nextCheckpoint) |
| 2096 |
Tcl_Interp *interp; |
| 2097 |
Tcl_Obj *pathPtr; |
| 2098 |
int nextCheckpoint; |
| 2099 |
{ |
| 2100 |
char *lastValidPathEnd = NULL; |
| 2101 |
/* This will hold the normalized string */ |
| 2102 |
Tcl_DString dsNorm; |
| 2103 |
char *path; |
| 2104 |
char *currentPathEndPosition; |
| 2105 |
|
| 2106 |
Tcl_DStringInit(&dsNorm); |
| 2107 |
path = Tcl_GetString(pathPtr); |
| 2108 |
|
| 2109 |
if (TclWinGetPlatformId() == VER_PLATFORM_WIN32_WINDOWS) { |
| 2110 |
/* |
| 2111 |
* We're on Win95, 98 or ME. There are two assumptions |
| 2112 |
* in this block of code. First that the native (NULL) |
| 2113 |
* encoding is basically ascii, and second that symbolic |
| 2114 |
* links are not possible. Both of these assumptions |
| 2115 |
* appear to be true of these operating systems. |
| 2116 |
*/ |
| 2117 |
int isDrive = 1; |
| 2118 |
Tcl_DString ds; |
| 2119 |
|
| 2120 |
currentPathEndPosition = path + nextCheckpoint; |
| 2121 |
if (*currentPathEndPosition == '/') { |
| 2122 |
currentPathEndPosition++; |
| 2123 |
} |
| 2124 |
while (1) { |
| 2125 |
char cur = *currentPathEndPosition; |
| 2126 |
if ((cur == '/' || cur == 0) && (path != currentPathEndPosition)) { |
| 2127 |
/* Reached directory separator, or end of string */ |
| 2128 |
CONST char *nativePath = Tcl_UtfToExternalDString(NULL, path, |
| 2129 |
currentPathEndPosition - path, &ds); |
| 2130 |
|
| 2131 |
/* |
| 2132 |
* Now we convert the tail of the current path to its |
| 2133 |
* 'long form', and append it to 'dsNorm' which holds |
| 2134 |
* the current normalized path, if the file exists. |
| 2135 |
*/ |
| 2136 |
if (isDrive) { |
| 2137 |
if (GetFileAttributesA(nativePath) |
| 2138 |
== 0xffffffff) { |
| 2139 |
/* File doesn't exist */ |
| 2140 |
Tcl_DStringFree(&ds); |
| 2141 |
break; |
| 2142 |
} |
| 2143 |
if (nativePath[0] >= 'a') { |
| 2144 |
((char*)nativePath)[0] -= ('a' - 'A'); |
| 2145 |
} |
| 2146 |
Tcl_DStringAppend(&dsNorm,nativePath,Tcl_DStringLength(&ds)); |
| 2147 |
} else { |
| 2148 |
WIN32_FIND_DATA fData; |
| 2149 |
HANDLE handle; |
| 2150 |
|
| 2151 |
handle = FindFirstFileA(nativePath, &fData); |
| 2152 |
if (handle == INVALID_HANDLE_VALUE) { |
| 2153 |
if (GetFileAttributesA(nativePath) |
| 2154 |
== 0xffffffff) { |
| 2155 |
/* File doesn't exist */ |
| 2156 |
Tcl_DStringFree(&ds); |
| 2157 |
break; |
| 2158 |
} |
| 2159 |
/* This is usually the '/' in 'c:/' at end of string */ |
| 2160 |
Tcl_DStringAppend(&dsNorm,"/", 1); |
| 2161 |
} else { |
| 2162 |
char *nativeName; |
| 2163 |
if (fData.cFileName[0] != '\0') { |
| 2164 |
nativeName = fData.cFileName; |
| 2165 |
} else { |
| 2166 |
nativeName = fData.cAlternateFileName; |
| 2167 |
} |
| 2168 |
FindClose(handle); |
| 2169 |
Tcl_DStringAppend(&dsNorm,"/", 1); |
| 2170 |
Tcl_DStringAppend(&dsNorm,nativeName,-1); |
| 2171 |
} |
| 2172 |
} |
| 2173 |
Tcl_DStringFree(&ds); |
| 2174 |
lastValidPathEnd = currentPathEndPosition; |
| 2175 |
if (cur == 0) { |
| 2176 |
break; |
| 2177 |
} |
| 2178 |
/* |
| 2179 |
* If we get here, we've got past one directory |
| 2180 |
* delimiter, so we know it is no longer a drive |
| 2181 |
*/ |
| 2182 |
isDrive = 0; |
| 2183 |
} |
| 2184 |
currentPathEndPosition++; |
| 2185 |
} |
| 2186 |
} else { |
| 2187 |
/* We're on WinNT or 2000 or XP */ |
| 2188 |
Tcl_Obj *temp = NULL; |
| 2189 |
int isDrive = 1; |
| 2190 |
Tcl_DString ds; |
| 2191 |
|
| 2192 |
currentPathEndPosition = path + nextCheckpoint; |
| 2193 |
if (*currentPathEndPosition == '/') { |
| 2194 |
currentPathEndPosition++; |
| 2195 |
} |
| 2196 |
while (1) { |
| 2197 |
char cur = *currentPathEndPosition; |
| 2198 |
if ((cur == '/' || cur == 0) && (path != currentPathEndPosition)) { |
| 2199 |
/* Reached directory separator, or end of string */ |
| 2200 |
WIN32_FILE_ATTRIBUTE_DATA data; |
| 2201 |
CONST char *nativePath = Tcl_WinUtfToTChar(path, |
| 2202 |
currentPathEndPosition - path, &ds); |
| 2203 |
if ((*tclWinProcs->getFileAttributesExProc)(nativePath, |
| 2204 |
GetFileExInfoStandard, &data) != TRUE) { |
| 2205 |
/* File doesn't exist */ |
| 2206 |
Tcl_DStringFree(&ds); |
| 2207 |
break; |
| 2208 |
} |
| 2209 |
|
| 2210 |
/* |
| 2211 |
* File 'nativePath' does exist if we get here. We |
| 2212 |
* now want to check if it is a symlink and otherwise |
| 2213 |
* continue with the rest of the path. |
| 2214 |
*/ |
| 2215 |
|
| 2216 |
/* |
| 2217 |
* Check for symlinks, except at last component |
| 2218 |
* of path (we don't follow final symlinks). Also |
| 2219 |
* a drive (C:/) for example, may sometimes have |
| 2220 |
* the reparse flag set for some reason I don't |
| 2221 |
* understand. We therefore don't perform this |
| 2222 |
* check for drives. |
| 2223 |
*/ |
| 2224 |
if (cur != 0 && !isDrive && (data.dwFileAttributes |
| 2225 |
& FILE_ATTRIBUTE_REPARSE_POINT)) { |
| 2226 |
Tcl_Obj *to = WinReadLinkDirectory(nativePath); |
| 2227 |
if (to != NULL) { |
| 2228 |
/* Read the reparse point ok */ |
| 2229 |
/* Tcl_GetStringFromObj(to, &pathLen); */ |
| 2230 |
nextCheckpoint = 0; /* pathLen */ |
| 2231 |
Tcl_AppendToObj(to, currentPathEndPosition, -1); |
| 2232 |
/* Convert link to forward slashes */ |
| 2233 |
for (path = Tcl_GetString(to); *path != 0; path++) { |
| 2234 |
if (*path == '\\') *path = '/'; |
| 2235 |
} |
| 2236 |
path = Tcl_GetString(to); |
| 2237 |
currentPathEndPosition = path + nextCheckpoint; |
| 2238 |
if (temp != NULL) { |
| 2239 |
Tcl_DecrRefCount(temp); |
| 2240 |
} |
| 2241 |
temp = to; |
| 2242 |
/* Reset variables so we can restart normalization */ |
| 2243 |
isDrive = 1; |
| 2244 |
Tcl_DStringFree(&dsNorm); |
| 2245 |
Tcl_DStringInit(&dsNorm); |
| 2246 |
Tcl_DStringFree(&ds); |
| 2247 |
continue; |
| 2248 |
} |
| 2249 |
} |
| 2250 |
/* |
| 2251 |
* Now we convert the tail of the current path to its |
| 2252 |
* 'long form', and append it to 'dsNorm' which holds |
| 2253 |
* the current normalized path |
| 2254 |
*/ |
| 2255 |
if (isDrive) { |
| 2256 |
WCHAR drive = ((WCHAR*)nativePath)[0]; |
| 2257 |
if (drive >= L'a') { |
| 2258 |
drive -= (L'a' - L'A'); |
| 2259 |
((WCHAR*)nativePath)[0] = drive; |
| 2260 |
} |
| 2261 |
Tcl_DStringAppend(&dsNorm,nativePath,Tcl_DStringLength(&ds)); |
| 2262 |
} else { |
| 2263 |
WIN32_FIND_DATAW fData; |
| 2264 |
HANDLE handle; |
| 2265 |
|
| 2266 |
handle = FindFirstFileW((WCHAR*)nativePath, &fData); |
| 2267 |
if (handle == INVALID_HANDLE_VALUE) { |
| 2268 |
/* This is usually the '/' in 'c:/' at end of string */ |
| 2269 |
Tcl_DStringAppend(&dsNorm,(CONST char*)L"/", |
| 2270 |
sizeof(WCHAR)); |
| 2271 |
} else { |
| 2272 |
WCHAR *nativeName; |
| 2273 |
if (fData.cFileName[0] != '\0') { |
| 2274 |
nativeName = fData.cFileName; |
| 2275 |
} else { |
| 2276 |
nativeName = fData.cAlternateFileName; |
| 2277 |
} |
| 2278 |
FindClose(handle); |
| 2279 |
Tcl_DStringAppend(&dsNorm,(CONST char*)L"/", |
| 2280 |
sizeof(WCHAR)); |
| 2281 |
Tcl_DStringAppend(&dsNorm,(TCHAR*)nativeName, |
| 2282 |
(int) (wcslen(nativeName)*sizeof(WCHAR))); |
| 2283 |
} |
| 2284 |
} |
| 2285 |
Tcl_DStringFree(&ds); |
| 2286 |
lastValidPathEnd = currentPathEndPosition; |
| 2287 |
if (cur == 0) { |
| 2288 |
break; |
| 2289 |
} |
| 2290 |
/* |
| 2291 |
* If we get here, we've got past one directory |
| 2292 |
* delimiter, so we know it is no longer a drive |
| 2293 |
*/ |
| 2294 |
isDrive = 0; |
| 2295 |
} |
| 2296 |
currentPathEndPosition++; |
| 2297 |
} |
| 2298 |
} |
| 2299 |
/* Common code path for all Windows platforms */ |
| 2300 |
nextCheckpoint = currentPathEndPosition - path; |
| 2301 |
if (lastValidPathEnd != NULL) { |
| 2302 |
/* |
| 2303 |
* Concatenate the normalized string in dsNorm with the |
| 2304 |
* tail of the path which we didn't recognise. The |
| 2305 |
* string in dsNorm is in the native encoding, so we |
| 2306 |
* have to convert it to Utf. |
| 2307 |
*/ |
| 2308 |
Tcl_DString dsTemp; |
| 2309 |
Tcl_WinTCharToUtf(Tcl_DStringValue(&dsNorm), |
| 2310 |
Tcl_DStringLength(&dsNorm), &dsTemp); |
| 2311 |
nextCheckpoint = Tcl_DStringLength(&dsTemp); |
| 2312 |
if (*lastValidPathEnd != 0) { |
| 2313 |
/* Not the end of the string */ |
| 2314 |
int len; |
| 2315 |
char *path; |
| 2316 |
Tcl_Obj *tmpPathPtr; |
| 2317 |
tmpPathPtr = Tcl_NewStringObj(Tcl_DStringValue(&dsTemp), |
| 2318 |
nextCheckpoint); |
| 2319 |
Tcl_AppendToObj(tmpPathPtr, lastValidPathEnd, -1); |
| 2320 |
path = Tcl_GetStringFromObj(tmpPathPtr, &len); |
| 2321 |
Tcl_SetStringObj(pathPtr, path, len); |
| 2322 |
Tcl_DecrRefCount(tmpPathPtr); |
| 2323 |
} else { |
| 2324 |
/* End of string was reached above */ |
| 2325 |
Tcl_SetStringObj(pathPtr, Tcl_DStringValue(&dsTemp), |
| 2326 |
nextCheckpoint); |
| 2327 |
} |
| 2328 |
Tcl_DStringFree(&dsTemp); |
| 2329 |
} |
| 2330 |
Tcl_DStringFree(&dsNorm); |
| 2331 |
return nextCheckpoint; |
| 2332 |
} |
| 2333 |
|
| 2334 |
/* |
| 2335 |
*--------------------------------------------------------------------------- |
| 2336 |
* |
| 2337 |
* TclpUtime -- |
| 2338 |
* |
| 2339 |
* Set the modification date for a file. |
| 2340 |
* |
| 2341 |
* Results: |
| 2342 |
* 0 on success, -1 on error. |
| 2343 |
* |
| 2344 |
* Side effects: |
| 2345 |
* None. |
| 2346 |
* |
| 2347 |
*--------------------------------------------------------------------------- |
| 2348 |
*/ |
| 2349 |
int |
| 2350 |
TclpUtime(pathPtr, tval) |
| 2351 |
Tcl_Obj *pathPtr; /* File to modify */ |
| 2352 |
struct utimbuf *tval; /* New modification date structure */ |
| 2353 |
{ |
| 2354 |
int res; |
| 2355 |
/* |
| 2356 |
* Windows uses a slightly different structure name and, possibly, |
| 2357 |
* contents, so we have to copy the information over |
| 2358 |
*/ |
| 2359 |
struct _utimbuf buf; |
| 2360 |
|
| 2361 |
buf.actime = tval->actime; |
| 2362 |
buf.modtime = tval->modtime; |
| 2363 |
|
| 2364 |
res = (*tclWinProcs->utimeProc)(Tcl_FSGetNativePath(pathPtr),&buf); |
| 2365 |
return res; |
| 2366 |
} |