| 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 of |
| 12 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "tclWinInt.h" |
| 16 |
#include "tclFileSystem.h" |
| 17 |
#include <winioctl.h> |
| 18 |
#include <shlobj.h> |
| 19 |
#include <lm.h> /* For TclpGetUserHome(). */ |
| 20 |
#include <userenv.h> /* For TclpGetUserHome(). */ |
| 21 |
#include <aclapi.h> /* For GetNamedSecurityInfo */ |
| 22 |
|
| 23 |
#ifdef _MSC_VER |
| 24 |
# pragma comment(lib, "userenv.lib") |
| 25 |
#endif |
| 26 |
/* |
| 27 |
* The number of 100-ns intervals between the Windows system epoch (1601-01-01 |
| 28 |
* on the proleptic Gregorian calendar) and the Posix epoch (1970-01-01). |
| 29 |
*/ |
| 30 |
|
| 31 |
#define POSIX_EPOCH_AS_FILETIME \ |
| 32 |
((Tcl_WideInt)116444736 * (Tcl_WideInt)1000000000) |
| 33 |
|
| 34 |
/* |
| 35 |
* Declarations for 'link' related information. This information should come |
| 36 |
* with VC++ 6.0, but is not in some older SDKs. In any case it is not well |
| 37 |
* documented. |
| 38 |
*/ |
| 39 |
|
| 40 |
#ifndef IO_REPARSE_TAG_RESERVED_ONE |
| 41 |
# define IO_REPARSE_TAG_RESERVED_ONE 0x000000001 |
| 42 |
#endif |
| 43 |
#ifndef IO_REPARSE_TAG_RESERVED_RANGE |
| 44 |
# define IO_REPARSE_TAG_RESERVED_RANGE 0x000000001 |
| 45 |
#endif |
| 46 |
#ifndef IO_REPARSE_TAG_VALID_VALUES |
| 47 |
# define IO_REPARSE_TAG_VALID_VALUES 0x0E000FFFF |
| 48 |
#endif |
| 49 |
#ifndef IO_REPARSE_TAG_HSM |
| 50 |
# define IO_REPARSE_TAG_HSM 0x0C0000004 |
| 51 |
#endif |
| 52 |
#ifndef IO_REPARSE_TAG_NSS |
| 53 |
# define IO_REPARSE_TAG_NSS 0x080000005 |
| 54 |
#endif |
| 55 |
#ifndef IO_REPARSE_TAG_NSSRECOVER |
| 56 |
# define IO_REPARSE_TAG_NSSRECOVER 0x080000006 |
| 57 |
#endif |
| 58 |
#ifndef IO_REPARSE_TAG_SIS |
| 59 |
# define IO_REPARSE_TAG_SIS 0x080000007 |
| 60 |
#endif |
| 61 |
#ifndef IO_REPARSE_TAG_DFS |
| 62 |
# define IO_REPARSE_TAG_DFS 0x080000008 |
| 63 |
#endif |
| 64 |
|
| 65 |
#ifndef IO_REPARSE_TAG_RESERVED_ZERO |
| 66 |
# define IO_REPARSE_TAG_RESERVED_ZERO 0x00000000 |
| 67 |
#endif |
| 68 |
#ifndef FILE_FLAG_OPEN_REPARSE_POINT |
| 69 |
# define FILE_FLAG_OPEN_REPARSE_POINT 0x00200000 |
| 70 |
#endif |
| 71 |
#ifndef IO_REPARSE_TAG_MOUNT_POINT |
| 72 |
# define IO_REPARSE_TAG_MOUNT_POINT 0xA0000003 |
| 73 |
#endif |
| 74 |
#ifndef IsReparseTagValid |
| 75 |
# define IsReparseTagValid(x) \ |
| 76 |
(!((x)&~IO_REPARSE_TAG_VALID_VALUES)&&((x)>IO_REPARSE_TAG_RESERVED_RANGE)) |
| 77 |
#endif |
| 78 |
#ifndef IO_REPARSE_TAG_SYMBOLIC_LINK |
| 79 |
# define IO_REPARSE_TAG_SYMBOLIC_LINK IO_REPARSE_TAG_RESERVED_ZERO |
| 80 |
#endif |
| 81 |
#ifndef FILE_SPECIAL_ACCESS |
| 82 |
# define FILE_SPECIAL_ACCESS (FILE_ANY_ACCESS) |
| 83 |
#endif |
| 84 |
#ifndef FSCTL_SET_REPARSE_POINT |
| 85 |
# define FSCTL_SET_REPARSE_POINT \ |
| 86 |
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 41, METHOD_BUFFERED, FILE_SPECIAL_ACCESS) |
| 87 |
# define FSCTL_GET_REPARSE_POINT \ |
| 88 |
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 42, METHOD_BUFFERED, FILE_ANY_ACCESS) |
| 89 |
# define FSCTL_DELETE_REPARSE_POINT \ |
| 90 |
CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 43, METHOD_BUFFERED, FILE_SPECIAL_ACCESS) |
| 91 |
#endif |
| 92 |
#ifndef INVALID_FILE_ATTRIBUTES |
| 93 |
#define INVALID_FILE_ATTRIBUTES ((DWORD)-1) |
| 94 |
#endif |
| 95 |
|
| 96 |
/* |
| 97 |
* Maximum reparse buffer info size. The max user defined reparse data is |
| 98 |
* 16KB, plus there's a header. |
| 99 |
*/ |
| 100 |
|
| 101 |
#define MAX_REPARSE_SIZE 17000 |
| 102 |
|
| 103 |
/* |
| 104 |
* Undocumented REPARSE_MOUNTPOINT_HEADER_SIZE structure definition. This is |
| 105 |
* found in winnt.h. |
| 106 |
* |
| 107 |
* IMPORTANT: caution when using this structure, since the actual structures |
| 108 |
* used will want to store a full path in the 'PathBuffer' field, but there |
| 109 |
* isn't room (there's only a single WCHAR!). Therefore one must artificially |
| 110 |
* create a larger space of memory and then cast it to this type. We use the |
| 111 |
* 'DUMMY_REPARSE_BUFFER' struct just below to deal with this problem. |
| 112 |
*/ |
| 113 |
|
| 114 |
#define REPARSE_MOUNTPOINT_HEADER_SIZE 8 |
| 115 |
#ifndef REPARSE_DATA_BUFFER_HEADER_SIZE |
| 116 |
typedef struct _REPARSE_DATA_BUFFER { |
| 117 |
DWORD ReparseTag; |
| 118 |
WORD ReparseDataLength; |
| 119 |
WORD Reserved; |
| 120 |
union { |
| 121 |
struct { |
| 122 |
WORD SubstituteNameOffset; |
| 123 |
WORD SubstituteNameLength; |
| 124 |
WORD PrintNameOffset; |
| 125 |
WORD PrintNameLength; |
| 126 |
ULONG Flags; |
| 127 |
WCHAR PathBuffer[1]; |
| 128 |
} SymbolicLinkReparseBuffer; |
| 129 |
struct { |
| 130 |
WORD SubstituteNameOffset; |
| 131 |
WORD SubstituteNameLength; |
| 132 |
WORD PrintNameOffset; |
| 133 |
WORD PrintNameLength; |
| 134 |
WCHAR PathBuffer[1]; |
| 135 |
} MountPointReparseBuffer; |
| 136 |
struct { |
| 137 |
BYTE DataBuffer[1]; |
| 138 |
} GenericReparseBuffer; |
| 139 |
}; |
| 140 |
} REPARSE_DATA_BUFFER; |
| 141 |
#endif |
| 142 |
|
| 143 |
typedef struct { |
| 144 |
REPARSE_DATA_BUFFER dummy; |
| 145 |
WCHAR dummyBuf[MAX_PATH * 3]; |
| 146 |
} DUMMY_REPARSE_BUFFER; |
| 147 |
|
| 148 |
/* |
| 149 |
* Other typedefs required by this code. |
| 150 |
*/ |
| 151 |
|
| 152 |
static time_t ToCTime(FILETIME fileTime); |
| 153 |
static void FromCTime(time_t posixTime, FILETIME *fileTime); |
| 154 |
|
| 155 |
/* |
| 156 |
* Declarations for local functions defined in this file: |
| 157 |
*/ |
| 158 |
|
| 159 |
static int NativeAccess(const WCHAR *path, int mode); |
| 160 |
static int NativeDev(const WCHAR *path); |
| 161 |
static int NativeStat(const WCHAR *path, Tcl_StatBuf *statPtr, |
| 162 |
int checkLinks); |
| 163 |
static unsigned short NativeStatMode(DWORD attr, int checkLinks, |
| 164 |
int isExec); |
| 165 |
static int NativeIsExec(const WCHAR *path); |
| 166 |
static int NativeReadReparse(const WCHAR *LinkDirectory, |
| 167 |
REPARSE_DATA_BUFFER *buffer, DWORD desiredAccess); |
| 168 |
static int NativeWriteReparse(const WCHAR *LinkDirectory, |
| 169 |
REPARSE_DATA_BUFFER *buffer); |
| 170 |
static int NativeMatchType(int isDrive, DWORD attr, |
| 171 |
const WCHAR *nativeName, Tcl_GlobTypeData *types); |
| 172 |
static int WinIsDrive(const char *name, size_t nameLen); |
| 173 |
static int WinIsReserved(const char *path); |
| 174 |
static Tcl_Obj * WinReadLink(const WCHAR *LinkSource); |
| 175 |
static Tcl_Obj * WinReadLinkDirectory(const WCHAR *LinkDirectory); |
| 176 |
static int WinLink(const WCHAR *LinkSource, |
| 177 |
const WCHAR *LinkTarget, int linkAction); |
| 178 |
static int WinSymLinkDirectory(const WCHAR *LinkDirectory, |
| 179 |
const WCHAR *LinkTarget); |
| 180 |
|
| 181 |
/* |
| 182 |
*-------------------------------------------------------------------- |
| 183 |
* |
| 184 |
* WinLink -- |
| 185 |
* |
| 186 |
* Make a link from source to target. |
| 187 |
* |
| 188 |
*-------------------------------------------------------------------- |
| 189 |
*/ |
| 190 |
|
| 191 |
static int |
| 192 |
WinLink( |
| 193 |
const WCHAR *linkSourcePath, |
| 194 |
const WCHAR *linkTargetPath, |
| 195 |
int linkAction) |
| 196 |
{ |
| 197 |
WCHAR tempFileName[MAX_PATH]; |
| 198 |
WCHAR *tempFilePart; |
| 199 |
DWORD attr; |
| 200 |
|
| 201 |
/* |
| 202 |
* Get the full path referenced by the target. |
| 203 |
*/ |
| 204 |
|
| 205 |
if (!GetFullPathNameW(linkTargetPath, MAX_PATH, tempFileName, |
| 206 |
&tempFilePart)) { |
| 207 |
/* |
| 208 |
* Invalid file. |
| 209 |
*/ |
| 210 |
|
| 211 |
TclWinConvertError(GetLastError()); |
| 212 |
return -1; |
| 213 |
} |
| 214 |
|
| 215 |
/* |
| 216 |
* Make sure source file doesn't exist. |
| 217 |
*/ |
| 218 |
|
| 219 |
attr = GetFileAttributesW(linkSourcePath); |
| 220 |
if (attr != INVALID_FILE_ATTRIBUTES) { |
| 221 |
Tcl_SetErrno(EEXIST); |
| 222 |
return -1; |
| 223 |
} |
| 224 |
|
| 225 |
/* |
| 226 |
* Get the full path referenced by the source file/directory. |
| 227 |
*/ |
| 228 |
|
| 229 |
if (!GetFullPathNameW(linkSourcePath, MAX_PATH, tempFileName, |
| 230 |
&tempFilePart)) { |
| 231 |
/* |
| 232 |
* Invalid file. |
| 233 |
*/ |
| 234 |
|
| 235 |
TclWinConvertError(GetLastError()); |
| 236 |
return -1; |
| 237 |
} |
| 238 |
|
| 239 |
/* |
| 240 |
* Check the target. |
| 241 |
*/ |
| 242 |
|
| 243 |
attr = GetFileAttributesW(linkTargetPath); |
| 244 |
if (attr == INVALID_FILE_ATTRIBUTES) { |
| 245 |
/* |
| 246 |
* The target doesn't exist. |
| 247 |
*/ |
| 248 |
|
| 249 |
TclWinConvertError(GetLastError()); |
| 250 |
} else if ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0) { |
| 251 |
/* |
| 252 |
* It is a file. |
| 253 |
*/ |
| 254 |
|
| 255 |
if (linkAction & TCL_CREATE_HARD_LINK) { |
| 256 |
if (CreateHardLinkW(linkSourcePath, linkTargetPath, NULL)) { |
| 257 |
/* |
| 258 |
* Success! |
| 259 |
*/ |
| 260 |
|
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
TclWinConvertError(GetLastError()); |
| 265 |
} else if (linkAction & TCL_CREATE_SYMBOLIC_LINK) { |
| 266 |
if (!tclWinProcs.createSymbolicLink) { |
| 267 |
/* |
| 268 |
* Can't symlink files. |
| 269 |
*/ |
| 270 |
Tcl_SetErrno(EINVAL); |
| 271 |
} else if (tclWinProcs.createSymbolicLink(linkSourcePath, linkTargetPath, |
| 272 |
0x2 /* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE */)) { |
| 273 |
/* |
| 274 |
* Success! |
| 275 |
*/ |
| 276 |
|
| 277 |
return 0; |
| 278 |
} else { |
| 279 |
TclWinConvertError(GetLastError()); |
| 280 |
} |
| 281 |
} else { |
| 282 |
Tcl_SetErrno(ENODEV); |
| 283 |
} |
| 284 |
} else { |
| 285 |
/* |
| 286 |
* We've got a directory. Now check whether what we're trying to do is |
| 287 |
* reasonable. |
| 288 |
*/ |
| 289 |
|
| 290 |
if (linkAction & TCL_CREATE_SYMBOLIC_LINK) { |
| 291 |
return WinSymLinkDirectory(linkSourcePath, linkTargetPath); |
| 292 |
|
| 293 |
} else if (linkAction & TCL_CREATE_HARD_LINK) { |
| 294 |
/* |
| 295 |
* Can't hard link directories. |
| 296 |
*/ |
| 297 |
|
| 298 |
Tcl_SetErrno(EISDIR); |
| 299 |
} else { |
| 300 |
Tcl_SetErrno(ENODEV); |
| 301 |
} |
| 302 |
} |
| 303 |
return -1; |
| 304 |
} |
| 305 |
|
| 306 |
/* |
| 307 |
*-------------------------------------------------------------------- |
| 308 |
* |
| 309 |
* WinReadLink -- |
| 310 |
* |
| 311 |
* What does 'LinkSource' point to? |
| 312 |
* |
| 313 |
*-------------------------------------------------------------------- |
| 314 |
*/ |
| 315 |
|
| 316 |
static Tcl_Obj * |
| 317 |
WinReadLink( |
| 318 |
const WCHAR *linkSourcePath) |
| 319 |
{ |
| 320 |
WCHAR tempFileName[MAX_PATH]; |
| 321 |
WCHAR *tempFilePart; |
| 322 |
DWORD attr; |
| 323 |
|
| 324 |
/* |
| 325 |
* Get the full path referenced by the target. |
| 326 |
*/ |
| 327 |
|
| 328 |
if (!GetFullPathNameW(linkSourcePath, MAX_PATH, tempFileName, |
| 329 |
&tempFilePart)) { |
| 330 |
/* |
| 331 |
* Invalid file. |
| 332 |
*/ |
| 333 |
|
| 334 |
TclWinConvertError(GetLastError()); |
| 335 |
return NULL; |
| 336 |
} |
| 337 |
|
| 338 |
/* |
| 339 |
* Make sure source file does exist. |
| 340 |
*/ |
| 341 |
|
| 342 |
attr = GetFileAttributesW(linkSourcePath); |
| 343 |
if (attr == INVALID_FILE_ATTRIBUTES) { |
| 344 |
/* |
| 345 |
* The source doesn't exist. |
| 346 |
*/ |
| 347 |
|
| 348 |
TclWinConvertError(GetLastError()); |
| 349 |
return NULL; |
| 350 |
|
| 351 |
} else if ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0) { |
| 352 |
/* |
| 353 |
* It is a file - this is not yet supported. |
| 354 |
*/ |
| 355 |
|
| 356 |
Tcl_SetErrno(ENOTDIR); |
| 357 |
return NULL; |
| 358 |
} |
| 359 |
|
| 360 |
return WinReadLinkDirectory(linkSourcePath); |
| 361 |
} |
| 362 |
|
| 363 |
/* |
| 364 |
*-------------------------------------------------------------------- |
| 365 |
* |
| 366 |
* WinSymLinkDirectory -- |
| 367 |
* |
| 368 |
* This routine creates a NTFS junction, using the undocumented |
| 369 |
* FSCTL_SET_REPARSE_POINT structure Win2K uses for mount points and |
| 370 |
* junctions. |
| 371 |
* |
| 372 |
* Assumption that linkTargetPath is a valid, existing directory. |
| 373 |
* |
| 374 |
* Returns: |
| 375 |
* Zero on success. |
| 376 |
* |
| 377 |
*-------------------------------------------------------------------- |
| 378 |
*/ |
| 379 |
|
| 380 |
static int |
| 381 |
WinSymLinkDirectory( |
| 382 |
const WCHAR *linkDirPath, |
| 383 |
const WCHAR *linkTargetPath) |
| 384 |
{ |
| 385 |
DUMMY_REPARSE_BUFFER dummy; |
| 386 |
REPARSE_DATA_BUFFER *reparseBuffer = (REPARSE_DATA_BUFFER *) &dummy; |
| 387 |
int len; |
| 388 |
WCHAR nativeTarget[MAX_PATH]; |
| 389 |
WCHAR *loop; |
| 390 |
|
| 391 |
/* |
| 392 |
* Make the native target name. |
| 393 |
*/ |
| 394 |
|
| 395 |
memcpy(nativeTarget, L"\\??\\", 4 * sizeof(WCHAR)); |
| 396 |
memcpy(nativeTarget + 4, linkTargetPath, |
| 397 |
sizeof(WCHAR) * (1+wcslen((WCHAR *) linkTargetPath))); |
| 398 |
len = wcslen(nativeTarget); |
| 399 |
|
| 400 |
/* |
| 401 |
* We must have backslashes only. This is VERY IMPORTANT. If we have any |
| 402 |
* forward slashes everything appears to work, but the resulting symlink |
| 403 |
* is useless! |
| 404 |
*/ |
| 405 |
|
| 406 |
for (loop = nativeTarget; *loop != 0; loop++) { |
| 407 |
if (*loop == '/') { |
| 408 |
*loop = '\\'; |
| 409 |
} |
| 410 |
} |
| 411 |
if ((nativeTarget[len-1] == '\\') && (nativeTarget[len-2] != ':')) { |
| 412 |
nativeTarget[len-1] = 0; |
| 413 |
} |
| 414 |
|
| 415 |
/* |
| 416 |
* Build the reparse info. |
| 417 |
*/ |
| 418 |
|
| 419 |
memset(reparseBuffer, 0, sizeof(DUMMY_REPARSE_BUFFER)); |
| 420 |
reparseBuffer->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT; |
| 421 |
reparseBuffer->MountPointReparseBuffer.SubstituteNameLength = |
| 422 |
wcslen(nativeTarget) * sizeof(WCHAR); |
| 423 |
reparseBuffer->Reserved = 0; |
| 424 |
reparseBuffer->MountPointReparseBuffer.PrintNameLength = 0; |
| 425 |
reparseBuffer->MountPointReparseBuffer.PrintNameOffset = |
| 426 |
reparseBuffer->MountPointReparseBuffer.SubstituteNameLength |
| 427 |
+ sizeof(WCHAR); |
| 428 |
memcpy(reparseBuffer->MountPointReparseBuffer.PathBuffer, nativeTarget, |
| 429 |
sizeof(WCHAR) |
| 430 |
+ reparseBuffer->MountPointReparseBuffer.SubstituteNameLength); |
| 431 |
reparseBuffer->ReparseDataLength = |
| 432 |
reparseBuffer->MountPointReparseBuffer.SubstituteNameLength+12; |
| 433 |
|
| 434 |
return NativeWriteReparse(linkDirPath, reparseBuffer); |
| 435 |
} |
| 436 |
|
| 437 |
/* |
| 438 |
*-------------------------------------------------------------------- |
| 439 |
* |
| 440 |
* TclWinSymLinkCopyDirectory -- |
| 441 |
* |
| 442 |
* Copy a Windows NTFS junction. This function assumes that LinkOriginal |
| 443 |
* exists and is a valid junction point, and that LinkCopy does not |
| 444 |
* exist. |
| 445 |
* |
| 446 |
* Returns: |
| 447 |
* Zero on success. |
| 448 |
* |
| 449 |
*-------------------------------------------------------------------- |
| 450 |
*/ |
| 451 |
|
| 452 |
int |
| 453 |
TclWinSymLinkCopyDirectory( |
| 454 |
const WCHAR *linkOrigPath, /* Existing junction - reparse point */ |
| 455 |
const WCHAR *linkCopyPath) /* Will become a duplicate junction */ |
| 456 |
{ |
| 457 |
DUMMY_REPARSE_BUFFER dummy; |
| 458 |
REPARSE_DATA_BUFFER *reparseBuffer = (REPARSE_DATA_BUFFER *) &dummy; |
| 459 |
|
| 460 |
if (NativeReadReparse(linkOrigPath, reparseBuffer, GENERIC_READ)) { |
| 461 |
return -1; |
| 462 |
} |
| 463 |
return NativeWriteReparse(linkCopyPath, reparseBuffer); |
| 464 |
} |
| 465 |
|
| 466 |
/* |
| 467 |
*-------------------------------------------------------------------- |
| 468 |
* |
| 469 |
* TclWinSymLinkDelete -- |
| 470 |
* |
| 471 |
* Delete a Windows NTFS junction. Once the junction information is |
| 472 |
* deleted, the filesystem object becomes an ordinary directory. Unless |
| 473 |
* 'linkOnly' is given, that directory is also removed. |
| 474 |
* |
| 475 |
* Assumption that LinkOriginal is a valid, existing junction. |
| 476 |
* |
| 477 |
* Returns: |
| 478 |
* Zero on success. |
| 479 |
* |
| 480 |
*-------------------------------------------------------------------- |
| 481 |
*/ |
| 482 |
|
| 483 |
int |
| 484 |
TclWinSymLinkDelete( |
| 485 |
const WCHAR *linkOrigPath, |
| 486 |
int linkOnly) |
| 487 |
{ |
| 488 |
/* |
| 489 |
* It is a symbolic link - remove it. |
| 490 |
*/ |
| 491 |
|
| 492 |
DUMMY_REPARSE_BUFFER dummy; |
| 493 |
REPARSE_DATA_BUFFER *reparseBuffer = (REPARSE_DATA_BUFFER *) &dummy; |
| 494 |
HANDLE hFile; |
| 495 |
DWORD returnedLength; |
| 496 |
|
| 497 |
memset(reparseBuffer, 0, sizeof(DUMMY_REPARSE_BUFFER)); |
| 498 |
reparseBuffer->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT; |
| 499 |
hFile = CreateFileW(linkOrigPath, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, |
| 500 |
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 501 |
|
| 502 |
if (hFile != INVALID_HANDLE_VALUE) { |
| 503 |
if (!DeviceIoControl(hFile, FSCTL_DELETE_REPARSE_POINT, reparseBuffer, |
| 504 |
REPARSE_MOUNTPOINT_HEADER_SIZE,NULL,0,&returnedLength,NULL)) { |
| 505 |
/* |
| 506 |
* Error setting junction. |
| 507 |
*/ |
| 508 |
|
| 509 |
TclWinConvertError(GetLastError()); |
| 510 |
CloseHandle(hFile); |
| 511 |
} else { |
| 512 |
CloseHandle(hFile); |
| 513 |
if (!linkOnly) { |
| 514 |
RemoveDirectoryW(linkOrigPath); |
| 515 |
} |
| 516 |
return 0; |
| 517 |
} |
| 518 |
} |
| 519 |
return -1; |
| 520 |
} |
| 521 |
|
| 522 |
/* |
| 523 |
*-------------------------------------------------------------------- |
| 524 |
* |
| 525 |
* WinReadLinkDirectory -- |
| 526 |
* |
| 527 |
* This routine reads a NTFS junction, using the undocumented |
| 528 |
* FSCTL_GET_REPARSE_POINT structure Win2K uses for mount points and |
| 529 |
* junctions. |
| 530 |
* |
| 531 |
* Assumption that LinkDirectory is a valid, existing directory. |
| 532 |
* |
| 533 |
* Returns: |
| 534 |
* A Tcl_Obj with refCount of 1 (i.e. owned by the caller), or NULL if |
| 535 |
* anything went wrong. |
| 536 |
* |
| 537 |
* In the future we should enhance this to return a path object rather |
| 538 |
* than a string. |
| 539 |
* |
| 540 |
*-------------------------------------------------------------------- |
| 541 |
*/ |
| 542 |
|
| 543 |
#if defined (__clang__) || ((__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) |
| 544 |
#pragma GCC diagnostic push |
| 545 |
#pragma GCC diagnostic ignored "-Warray-bounds" |
| 546 |
#endif |
| 547 |
|
| 548 |
static Tcl_Obj * |
| 549 |
WinReadLinkDirectory( |
| 550 |
const WCHAR *linkDirPath) |
| 551 |
{ |
| 552 |
int attr, len, offset; |
| 553 |
DUMMY_REPARSE_BUFFER dummy; |
| 554 |
REPARSE_DATA_BUFFER *reparseBuffer = (REPARSE_DATA_BUFFER *) &dummy; |
| 555 |
Tcl_Obj *retVal; |
| 556 |
Tcl_DString ds; |
| 557 |
const char *copy; |
| 558 |
|
| 559 |
attr = GetFileAttributesW(linkDirPath); |
| 560 |
if (!(attr & FILE_ATTRIBUTE_REPARSE_POINT)) { |
| 561 |
goto invalidError; |
| 562 |
} |
| 563 |
if (NativeReadReparse(linkDirPath, reparseBuffer, 0)) { |
| 564 |
return NULL; |
| 565 |
} |
| 566 |
|
| 567 |
switch (reparseBuffer->ReparseTag) { |
| 568 |
case 0x80000000|IO_REPARSE_TAG_SYMBOLIC_LINK: |
| 569 |
case IO_REPARSE_TAG_SYMBOLIC_LINK: |
| 570 |
case IO_REPARSE_TAG_MOUNT_POINT: |
| 571 |
/* |
| 572 |
* Certain native path representations on Windows have a special |
| 573 |
* prefix to indicate that they are to be treated specially. For |
| 574 |
* example extremely long paths, or symlinks, or volumes mounted |
| 575 |
* inside directories. |
| 576 |
* |
| 577 |
* There is an assumption in this code that 'wide' interfaces are |
| 578 |
* being used (see tclWin32Dll.c), which is true for the only systems |
| 579 |
* which support reparse tags at present. If that changes in the |
| 580 |
* future, this code will have to be generalised. |
| 581 |
*/ |
| 582 |
|
| 583 |
offset = 0; |
| 584 |
if (reparseBuffer->MountPointReparseBuffer.PathBuffer[0] == '\\') { |
| 585 |
/* |
| 586 |
* Check whether this is a mounted volume. |
| 587 |
*/ |
| 588 |
|
| 589 |
if (wcsncmp(reparseBuffer->MountPointReparseBuffer.PathBuffer, |
| 590 |
L"\\??\\Volume{",11) == 0) { |
| 591 |
char drive; |
| 592 |
|
| 593 |
/* |
| 594 |
* There is some confusion between \??\ and \\?\ which we have |
| 595 |
* to fix here. It doesn't seem very well documented. |
| 596 |
*/ |
| 597 |
|
| 598 |
reparseBuffer->MountPointReparseBuffer.PathBuffer[1] = '\\'; |
| 599 |
|
| 600 |
/* |
| 601 |
* Check if a corresponding drive letter exists, and use that |
| 602 |
* if it is found |
| 603 |
*/ |
| 604 |
|
| 605 |
drive = TclWinDriveLetterForVolMountPoint( |
| 606 |
reparseBuffer->MountPointReparseBuffer.PathBuffer); |
| 607 |
if (drive != -1) { |
| 608 |
char driveSpec[3] = { |
| 609 |
'\0', ':', '\0' |
| 610 |
}; |
| 611 |
|
| 612 |
driveSpec[0] = drive; |
| 613 |
retVal = Tcl_NewStringObj(driveSpec,2); |
| 614 |
Tcl_IncrRefCount(retVal); |
| 615 |
return retVal; |
| 616 |
} |
| 617 |
|
| 618 |
/* |
| 619 |
* This is actually a mounted drive, which doesn't exists as a |
| 620 |
* DOS drive letter. This means the path isn't actually a |
| 621 |
* link, although we partially treat it like one ('file type' |
| 622 |
* will return 'link'), but then the link will actually just |
| 623 |
* be treated like an ordinary directory. I don't believe any |
| 624 |
* serious inconsistency will arise from this, but it is |
| 625 |
* something to be aware of. |
| 626 |
*/ |
| 627 |
|
| 628 |
goto invalidError; |
| 629 |
} else if (wcsncmp(reparseBuffer->MountPointReparseBuffer |
| 630 |
.PathBuffer, L"\\\\?\\",4) == 0) { |
| 631 |
/* |
| 632 |
* Strip off the prefix. |
| 633 |
*/ |
| 634 |
|
| 635 |
offset = 4; |
| 636 |
} else if (wcsncmp(reparseBuffer->MountPointReparseBuffer |
| 637 |
.PathBuffer, L"\\??\\",4) == 0) { |
| 638 |
/* |
| 639 |
* Strip off the prefix. |
| 640 |
*/ |
| 641 |
|
| 642 |
offset = 4; |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
Tcl_WinTCharToUtf((TCHAR *) |
| 647 |
reparseBuffer->MountPointReparseBuffer.PathBuffer, |
| 648 |
reparseBuffer->MountPointReparseBuffer |
| 649 |
.SubstituteNameLength, &ds); |
| 650 |
|
| 651 |
copy = Tcl_DStringValue(&ds)+offset; |
| 652 |
len = Tcl_DStringLength(&ds)-offset; |
| 653 |
retVal = Tcl_NewStringObj(copy,len); |
| 654 |
Tcl_IncrRefCount(retVal); |
| 655 |
Tcl_DStringFree(&ds); |
| 656 |
return retVal; |
| 657 |
} |
| 658 |
|
| 659 |
invalidError: |
| 660 |
Tcl_SetErrno(EINVAL); |
| 661 |
return NULL; |
| 662 |
} |
| 663 |
|
| 664 |
#if defined (__clang__) || ((__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) |
| 665 |
#pragma GCC diagnostic pop |
| 666 |
#endif |
| 667 |
|
| 668 |
/* |
| 669 |
*-------------------------------------------------------------------- |
| 670 |
* |
| 671 |
* NativeReadReparse -- |
| 672 |
* |
| 673 |
* Read the junction/reparse information from a given NTFS directory. |
| 674 |
* |
| 675 |
* Assumption that linkDirPath is a valid, existing directory. |
| 676 |
* |
| 677 |
* Returns: |
| 678 |
* Zero on success. |
| 679 |
* |
| 680 |
*-------------------------------------------------------------------- |
| 681 |
*/ |
| 682 |
|
| 683 |
static int |
| 684 |
NativeReadReparse( |
| 685 |
const WCHAR *linkDirPath, /* The junction to read */ |
| 686 |
REPARSE_DATA_BUFFER *buffer,/* Pointer to buffer. Cannot be NULL */ |
| 687 |
DWORD desiredAccess) |
| 688 |
{ |
| 689 |
HANDLE hFile; |
| 690 |
DWORD returnedLength; |
| 691 |
|
| 692 |
hFile = CreateFileW(linkDirPath, desiredAccess, FILE_SHARE_READ, NULL, |
| 693 |
OPEN_EXISTING, |
| 694 |
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 695 |
|
| 696 |
if (hFile == INVALID_HANDLE_VALUE) { |
| 697 |
/* |
| 698 |
* Error creating directory. |
| 699 |
*/ |
| 700 |
|
| 701 |
TclWinConvertError(GetLastError()); |
| 702 |
return -1; |
| 703 |
} |
| 704 |
|
| 705 |
/* |
| 706 |
* Get the link. |
| 707 |
*/ |
| 708 |
|
| 709 |
if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT, NULL, 0, buffer, |
| 710 |
sizeof(DUMMY_REPARSE_BUFFER), &returnedLength, NULL)) { |
| 711 |
/* |
| 712 |
* Error setting junction. |
| 713 |
*/ |
| 714 |
|
| 715 |
TclWinConvertError(GetLastError()); |
| 716 |
CloseHandle(hFile); |
| 717 |
return -1; |
| 718 |
} |
| 719 |
CloseHandle(hFile); |
| 720 |
|
| 721 |
if (!IsReparseTagValid(buffer->ReparseTag)) { |
| 722 |
Tcl_SetErrno(EINVAL); |
| 723 |
return -1; |
| 724 |
} |
| 725 |
return 0; |
| 726 |
} |
| 727 |
|
| 728 |
/* |
| 729 |
*-------------------------------------------------------------------- |
| 730 |
* |
| 731 |
* NativeWriteReparse -- |
| 732 |
* |
| 733 |
* Write the reparse information for a given directory. |
| 734 |
* |
| 735 |
* Assumption that LinkDirectory does not exist. |
| 736 |
* |
| 737 |
*-------------------------------------------------------------------- |
| 738 |
*/ |
| 739 |
|
| 740 |
static int |
| 741 |
NativeWriteReparse( |
| 742 |
const WCHAR *linkDirPath, |
| 743 |
REPARSE_DATA_BUFFER *buffer) |
| 744 |
{ |
| 745 |
HANDLE hFile; |
| 746 |
DWORD returnedLength; |
| 747 |
|
| 748 |
/* |
| 749 |
* Create the directory - it must not already exist. |
| 750 |
*/ |
| 751 |
|
| 752 |
if (CreateDirectoryW(linkDirPath, NULL) == 0) { |
| 753 |
/* |
| 754 |
* Error creating directory. |
| 755 |
*/ |
| 756 |
|
| 757 |
TclWinConvertError(GetLastError()); |
| 758 |
return -1; |
| 759 |
} |
| 760 |
hFile = CreateFileW(linkDirPath, GENERIC_WRITE, 0, NULL, |
| 761 |
OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT |
| 762 |
| FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 763 |
if (hFile == INVALID_HANDLE_VALUE) { |
| 764 |
/* |
| 765 |
* Error creating directory. |
| 766 |
*/ |
| 767 |
|
| 768 |
TclWinConvertError(GetLastError()); |
| 769 |
return -1; |
| 770 |
} |
| 771 |
|
| 772 |
/* |
| 773 |
* Set the link. |
| 774 |
*/ |
| 775 |
|
| 776 |
if (!DeviceIoControl(hFile, FSCTL_SET_REPARSE_POINT, buffer, |
| 777 |
(DWORD) buffer->ReparseDataLength + REPARSE_MOUNTPOINT_HEADER_SIZE, |
| 778 |
NULL, 0, &returnedLength, NULL)) { |
| 779 |
/* |
| 780 |
* Error setting junction. |
| 781 |
*/ |
| 782 |
|
| 783 |
TclWinConvertError(GetLastError()); |
| 784 |
CloseHandle(hFile); |
| 785 |
RemoveDirectoryW(linkDirPath); |
| 786 |
return -1; |
| 787 |
} |
| 788 |
CloseHandle(hFile); |
| 789 |
|
| 790 |
/* |
| 791 |
* We succeeded. |
| 792 |
*/ |
| 793 |
|
| 794 |
return 0; |
| 795 |
} |
| 796 |
|
| 797 |
/* |
| 798 |
*---------------------------------------------------------------------- |
| 799 |
* |
| 800 |
* tclWinDebugPanic -- |
| 801 |
* |
| 802 |
* Display a message. If a debugger is present, present it directly to |
| 803 |
* the debugger, otherwise use a MessageBox. |
| 804 |
* |
| 805 |
* Results: |
| 806 |
* None. |
| 807 |
* |
| 808 |
* Side effects: |
| 809 |
* None. |
| 810 |
* |
| 811 |
*---------------------------------------------------------------------- |
| 812 |
*/ |
| 813 |
|
| 814 |
MODULE_SCOPE TCL_NORETURN void |
| 815 |
tclWinDebugPanic( |
| 816 |
const char *format, ...) |
| 817 |
{ |
| 818 |
#define TCL_MAX_WARN_LEN 1024 |
| 819 |
va_list argList; |
| 820 |
char buf[TCL_MAX_WARN_LEN * 3]; |
| 821 |
WCHAR msgString[TCL_MAX_WARN_LEN]; |
| 822 |
|
| 823 |
va_start(argList, format); |
| 824 |
vsnprintf(buf, sizeof(buf), format, argList); |
| 825 |
|
| 826 |
msgString[TCL_MAX_WARN_LEN-1] = '\0'; |
| 827 |
MultiByteToWideChar(CP_UTF8, 0, buf, -1, msgString, TCL_MAX_WARN_LEN); |
| 828 |
|
| 829 |
/* |
| 830 |
* Truncate MessageBox string if it is too long to not overflow the screen |
| 831 |
* and cause possible oversized window error. |
| 832 |
*/ |
| 833 |
|
| 834 |
if (msgString[TCL_MAX_WARN_LEN-1] != '\0') { |
| 835 |
memcpy(msgString + (TCL_MAX_WARN_LEN - 5), L" ...", 5 * sizeof(WCHAR)); |
| 836 |
} |
| 837 |
if (IsDebuggerPresent()) { |
| 838 |
OutputDebugStringW(msgString); |
| 839 |
} else { |
| 840 |
MessageBeep(MB_ICONEXCLAMATION); |
| 841 |
MessageBoxW(NULL, msgString, L"Fatal Error", |
| 842 |
MB_ICONSTOP | MB_OK | MB_TASKMODAL | MB_SETFOREGROUND); |
| 843 |
} |
| 844 |
#if defined(__GNUC__) |
| 845 |
__builtin_trap(); |
| 846 |
#elif defined(_WIN64) |
| 847 |
__debugbreak(); |
| 848 |
#elif defined(_MSC_VER) && defined (_M_IX86) |
| 849 |
_asm {int 3} |
| 850 |
#else |
| 851 |
DebugBreak(); |
| 852 |
#endif |
| 853 |
abort(); |
| 854 |
} |
| 855 |
|
| 856 |
/* |
| 857 |
*--------------------------------------------------------------------------- |
| 858 |
* |
| 859 |
* TclpFindExecutable -- |
| 860 |
* |
| 861 |
* This function computes the absolute path name of the current |
| 862 |
* application. |
| 863 |
* |
| 864 |
* Results: |
| 865 |
* None. |
| 866 |
* |
| 867 |
* Side effects: |
| 868 |
* The computed path is stored. |
| 869 |
* |
| 870 |
*--------------------------------------------------------------------------- |
| 871 |
*/ |
| 872 |
|
| 873 |
void |
| 874 |
TclpFindExecutable( |
| 875 |
const char *argv0) /* If NULL, install PanicMessageBox, otherwise |
| 876 |
* ignore. */ |
| 877 |
{ |
| 878 |
WCHAR wName[MAX_PATH]; |
| 879 |
char name[MAX_PATH * 3]; |
| 880 |
|
| 881 |
/* |
| 882 |
* Under Windows we ignore argv0, and return the path for the file used to |
| 883 |
* create this process. Only if it is NULL, install a new panic handler. |
| 884 |
*/ |
| 885 |
|
| 886 |
if (argv0 == NULL) { |
| 887 |
Tcl_SetPanicProc(tclWinDebugPanic); |
| 888 |
} |
| 889 |
|
| 890 |
GetModuleFileNameW(NULL, wName, sizeof(wName)/sizeof(WCHAR)); |
| 891 |
WideCharToMultiByte(CP_UTF8, 0, wName, -1, name, sizeof(name), NULL, NULL); |
| 892 |
TclWinNoBackslash(name); |
| 893 |
TclSetObjNameOfExecutable(Tcl_NewStringObj(name, -1), NULL); |
| 894 |
} |
| 895 |
|
| 896 |
/* |
| 897 |
*---------------------------------------------------------------------- |
| 898 |
* |
| 899 |
* TclpMatchInDirectory -- |
| 900 |
* |
| 901 |
* This routine is used by the globbing code to search a directory for |
| 902 |
* all files which match a given pattern. |
| 903 |
* |
| 904 |
* Results: |
| 905 |
* The return value is a standard Tcl result indicating whether an error |
| 906 |
* occurred in globbing. Errors are left in interp, good results are |
| 907 |
* lappended to resultPtr (which must be a valid object). |
| 908 |
* |
| 909 |
* Side effects: |
| 910 |
* None. |
| 911 |
* |
| 912 |
*---------------------------------------------------------------------- |
| 913 |
*/ |
| 914 |
|
| 915 |
int |
| 916 |
TclpMatchInDirectory( |
| 917 |
Tcl_Interp *interp, /* Interpreter to receive errors. */ |
| 918 |
Tcl_Obj *resultPtr, /* List object to lappend results. */ |
| 919 |
Tcl_Obj *pathPtr, /* Contains path to directory to search. */ |
| 920 |
const char *pattern, /* Pattern to match against. */ |
| 921 |
Tcl_GlobTypeData *types) /* Object containing list of acceptable types. |
| 922 |
* May be NULL. In particular the directory |
| 923 |
* flag is very important. */ |
| 924 |
{ |
| 925 |
const WCHAR *native; |
| 926 |
|
| 927 |
if (types != NULL && types->type == TCL_GLOB_TYPE_MOUNT) { |
| 928 |
/* |
| 929 |
* The native filesystem never adds mounts. |
| 930 |
*/ |
| 931 |
|
| 932 |
return TCL_OK; |
| 933 |
} |
| 934 |
|
| 935 |
if (pattern == NULL || (*pattern == '\0')) { |
| 936 |
Tcl_Obj *norm = Tcl_FSGetNormalizedPath(NULL, pathPtr); |
| 937 |
|
| 938 |
if (norm != NULL) { |
| 939 |
/* |
| 940 |
* Match a single file directly. |
| 941 |
*/ |
| 942 |
|
| 943 |
int len; |
| 944 |
DWORD attr; |
| 945 |
WIN32_FILE_ATTRIBUTE_DATA data; |
| 946 |
const char *str; |
| 947 |
|
| 948 |
if (norm != pathPtr) { Tcl_IncrRefCount(norm); } |
| 949 |
str = TclGetStringFromObj(norm, &len); |
| 950 |
native = (WCHAR *)Tcl_FSGetNativePath(pathPtr); |
| 951 |
|
| 952 |
if (GetFileAttributesExW(native, |
| 953 |
GetFileExInfoStandard, &data) != TRUE) { |
| 954 |
if (norm != pathPtr) { Tcl_DecrRefCount(norm); } |
| 955 |
return TCL_OK; |
| 956 |
} |
| 957 |
attr = data.dwFileAttributes; |
| 958 |
|
| 959 |
if (NativeMatchType(WinIsDrive(str,len), attr, native, types)) { |
| 960 |
Tcl_ListObjAppendElement(interp, resultPtr, pathPtr); |
| 961 |
} |
| 962 |
if (norm != pathPtr) { Tcl_DecrRefCount(norm); } |
| 963 |
} |
| 964 |
return TCL_OK; |
| 965 |
} else { |
| 966 |
DWORD attr; |
| 967 |
HANDLE handle; |
| 968 |
WIN32_FIND_DATAW data; |
| 969 |
const char *dirName; /* UTF-8 dir name, later with pattern |
| 970 |
* appended. */ |
| 971 |
int dirLength; |
| 972 |
int matchSpecialDots; |
| 973 |
Tcl_DString ds; /* Native encoding of dir, also used |
| 974 |
* temporarily for other things. */ |
| 975 |
Tcl_DString dsOrig; /* UTF-8 encoding of dir. */ |
| 976 |
Tcl_Obj *fileNamePtr; |
| 977 |
char lastChar; |
| 978 |
|
| 979 |
/* |
| 980 |
* Get the normalized path representation (the main thing is we dont |
| 981 |
* want any '~' sequences). |
| 982 |
*/ |
| 983 |
|
| 984 |
fileNamePtr = Tcl_FSGetNormalizedPath(interp, pathPtr); |
| 985 |
if (fileNamePtr == NULL) { |
| 986 |
return TCL_ERROR; |
| 987 |
} |
| 988 |
/* Ensure it'd be alive, while used. */ |
| 989 |
if (fileNamePtr != pathPtr) { Tcl_IncrRefCount(fileNamePtr); } |
| 990 |
|
| 991 |
/* |
| 992 |
* Verify that the specified path exists and is actually a directory. |
| 993 |
*/ |
| 994 |
|
| 995 |
native = (const WCHAR *)Tcl_FSGetNativePath(pathPtr); |
| 996 |
if (native == NULL) { |
| 997 |
if (fileNamePtr != pathPtr) { Tcl_DecrRefCount(fileNamePtr); } |
| 998 |
return TCL_OK; |
| 999 |
} |
| 1000 |
attr = GetFileAttributesW(native); |
| 1001 |
|
| 1002 |
if ((attr == INVALID_FILE_ATTRIBUTES) |
| 1003 |
|| ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0)) { |
| 1004 |
if (fileNamePtr != pathPtr) { Tcl_DecrRefCount(fileNamePtr); } |
| 1005 |
return TCL_OK; |
| 1006 |
} |
| 1007 |
|
| 1008 |
/* |
| 1009 |
* Build up the directory name for searching, including a trailing |
| 1010 |
* directory separator. |
| 1011 |
*/ |
| 1012 |
|
| 1013 |
Tcl_DStringInit(&dsOrig); |
| 1014 |
dirName = Tcl_GetStringFromObj(fileNamePtr, &dirLength); |
| 1015 |
Tcl_DStringAppend(&dsOrig, dirName, dirLength); |
| 1016 |
|
| 1017 |
lastChar = dirName[dirLength -1]; |
| 1018 |
if ((lastChar != '\\') && (lastChar != '/') && (lastChar != ':')) { |
| 1019 |
TclDStringAppendLiteral(&dsOrig, "/"); |
| 1020 |
dirLength++; |
| 1021 |
} |
| 1022 |
if (fileNamePtr != pathPtr) { Tcl_DecrRefCount(fileNamePtr); } |
| 1023 |
dirName = Tcl_DStringValue(&dsOrig); |
| 1024 |
|
| 1025 |
/* |
| 1026 |
* We need to check all files in the directory, so we append '*.*' to |
| 1027 |
* the path, unless the pattern we've been given is rather simple, |
| 1028 |
* when we can use that instead. |
| 1029 |
*/ |
| 1030 |
|
| 1031 |
if (strpbrk(pattern, "[]\\") == NULL) { |
| 1032 |
/* |
| 1033 |
* The pattern is a simple one containing just '*' and/or '?'. |
| 1034 |
* This means we can get the OS to help us, by passing it the |
| 1035 |
* pattern. |
| 1036 |
*/ |
| 1037 |
|
| 1038 |
dirName = Tcl_DStringAppend(&dsOrig, pattern, -1); |
| 1039 |
} else { |
| 1040 |
dirName = TclDStringAppendLiteral(&dsOrig, "*.*"); |
| 1041 |
} |
| 1042 |
|
| 1043 |
native = (WCHAR *)Tcl_WinUtfToTChar(dirName, -1, &ds); |
| 1044 |
if ((types == NULL) || (types->type != TCL_GLOB_TYPE_DIR)) { |
| 1045 |
handle = FindFirstFileW(native, &data); |
| 1046 |
} else { |
| 1047 |
/* |
| 1048 |
* We can be more efficient, for pure directory requests. |
| 1049 |
*/ |
| 1050 |
|
| 1051 |
handle = FindFirstFileExW(native, |
| 1052 |
FindExInfoStandard, &data, |
| 1053 |
FindExSearchLimitToDirectories, NULL, 0); |
| 1054 |
} |
| 1055 |
|
| 1056 |
if (handle == INVALID_HANDLE_VALUE) { |
| 1057 |
DWORD err = GetLastError(); |
| 1058 |
|
| 1059 |
Tcl_DStringFree(&ds); |
| 1060 |
if (err == ERROR_FILE_NOT_FOUND) { |
| 1061 |
/* |
| 1062 |
* We used our 'pattern' above, and matched nothing. This |
| 1063 |
* means we just return TCL_OK, indicating no results found. |
| 1064 |
*/ |
| 1065 |
|
| 1066 |
Tcl_DStringFree(&dsOrig); |
| 1067 |
return TCL_OK; |
| 1068 |
} |
| 1069 |
|
| 1070 |
TclWinConvertError(err); |
| 1071 |
if (interp != NULL) { |
| 1072 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1073 |
"couldn't read directory \"%s\": %s", |
| 1074 |
Tcl_DStringValue(&dsOrig), Tcl_PosixError(interp))); |
| 1075 |
} |
| 1076 |
Tcl_DStringFree(&dsOrig); |
| 1077 |
return TCL_ERROR; |
| 1078 |
} |
| 1079 |
Tcl_DStringFree(&ds); |
| 1080 |
|
| 1081 |
/* |
| 1082 |
* We may use this later, so we must restore it to its length |
| 1083 |
* including the directory delimiter. |
| 1084 |
*/ |
| 1085 |
|
| 1086 |
Tcl_DStringSetLength(&dsOrig, dirLength); |
| 1087 |
|
| 1088 |
/* |
| 1089 |
* Check to see if the pattern should match the special . and |
| 1090 |
* .. names, referring to the current directory, or the directory |
| 1091 |
* above. We need a special check for this because paths beginning |
| 1092 |
* with a dot are not considered hidden on Windows, and so otherwise a |
| 1093 |
* relative glob like 'glob -join * *' will actually return |
| 1094 |
* './. ../..' etc. |
| 1095 |
*/ |
| 1096 |
|
| 1097 |
if ((pattern[0] == '.') |
| 1098 |
|| ((pattern[0] == '\\') && (pattern[1] == '.'))) { |
| 1099 |
matchSpecialDots = 1; |
| 1100 |
} else { |
| 1101 |
matchSpecialDots = 0; |
| 1102 |
} |
| 1103 |
|
| 1104 |
/* |
| 1105 |
* Now iterate over all of the files in the directory, starting with |
| 1106 |
* the first one we found. |
| 1107 |
*/ |
| 1108 |
|
| 1109 |
do { |
| 1110 |
const char *utfname; |
| 1111 |
int checkDrive = 0, isDrive; |
| 1112 |
|
| 1113 |
native = data.cFileName; |
| 1114 |
attr = data.dwFileAttributes; |
| 1115 |
utfname = Tcl_WinTCharToUtf((TCHAR *)native, -1, &ds); |
| 1116 |
|
| 1117 |
if (!matchSpecialDots) { |
| 1118 |
/* |
| 1119 |
* If it is exactly '.' or '..' then we ignore it. |
| 1120 |
*/ |
| 1121 |
|
| 1122 |
if ((utfname[0] == '.') && (utfname[1] == '\0' |
| 1123 |
|| (utfname[1] == '.' && utfname[2] == '\0'))) { |
| 1124 |
Tcl_DStringFree(&ds); |
| 1125 |
continue; |
| 1126 |
} |
| 1127 |
} else if (utfname[0] == '.' && utfname[1] == '.' |
| 1128 |
&& utfname[2] == '\0') { |
| 1129 |
/* |
| 1130 |
* Have to check if this is a drive below, so we can correctly |
| 1131 |
* match 'hidden' and not hidden files. |
| 1132 |
*/ |
| 1133 |
|
| 1134 |
checkDrive = 1; |
| 1135 |
} |
| 1136 |
|
| 1137 |
/* |
| 1138 |
* Check to see if the file matches the pattern. Note that we are |
| 1139 |
* ignoring the case sensitivity flag because Windows doesn't |
| 1140 |
* honor case even if the volume is case sensitive. If the volume |
| 1141 |
* also doesn't preserve case, then we previously returned the |
| 1142 |
* lower case form of the name. This didn't seem quite right since |
| 1143 |
* there are non-case-preserving volumes that actually return |
| 1144 |
* mixed case. So now we are returning exactly what we get from |
| 1145 |
* the system. |
| 1146 |
*/ |
| 1147 |
|
| 1148 |
if (Tcl_StringCaseMatch(utfname, pattern, 1)) { |
| 1149 |
/* |
| 1150 |
* If the file matches, then we need to process the remainder |
| 1151 |
* of the path. |
| 1152 |
*/ |
| 1153 |
|
| 1154 |
if (checkDrive) { |
| 1155 |
const char *fullname = Tcl_DStringAppend(&dsOrig, utfname, |
| 1156 |
Tcl_DStringLength(&ds)); |
| 1157 |
|
| 1158 |
isDrive = WinIsDrive(fullname, Tcl_DStringLength(&dsOrig)); |
| 1159 |
Tcl_DStringSetLength(&dsOrig, dirLength); |
| 1160 |
} else { |
| 1161 |
isDrive = 0; |
| 1162 |
} |
| 1163 |
if (NativeMatchType(isDrive, attr, native, types)) { |
| 1164 |
Tcl_ListObjAppendElement(interp, resultPtr, |
| 1165 |
TclNewFSPathObj(pathPtr, utfname, |
| 1166 |
Tcl_DStringLength(&ds))); |
| 1167 |
} |
| 1168 |
} |
| 1169 |
|
| 1170 |
/* |
| 1171 |
* Free ds here to ensure that native is valid above. |
| 1172 |
*/ |
| 1173 |
|
| 1174 |
Tcl_DStringFree(&ds); |
| 1175 |
} while (FindNextFileW(handle, &data) == TRUE); |
| 1176 |
|
| 1177 |
FindClose(handle); |
| 1178 |
Tcl_DStringFree(&dsOrig); |
| 1179 |
return TCL_OK; |
| 1180 |
} |
| 1181 |
} |
| 1182 |
|
| 1183 |
/* |
| 1184 |
* Does the given path represent a root volume? We need this special case |
| 1185 |
* because for NTFS root volumes, the getFileAttributesProc returns a 'hidden' |
| 1186 |
* attribute when it should not. |
| 1187 |
*/ |
| 1188 |
|
| 1189 |
static int |
| 1190 |
WinIsDrive( |
| 1191 |
const char *name, /* Name (UTF-8) */ |
| 1192 |
size_t len) /* Length of name */ |
| 1193 |
{ |
| 1194 |
int remove = 0; |
| 1195 |
|
| 1196 |
while (len > 4) { |
| 1197 |
if ((name[len-1] != '.' || name[len-2] != '.') |
| 1198 |
|| (name[len-3] != '/' && name[len-3] != '\\')) { |
| 1199 |
/* |
| 1200 |
* We don't have '/..' at the end. |
| 1201 |
*/ |
| 1202 |
|
| 1203 |
if (remove == 0) { |
| 1204 |
break; |
| 1205 |
} |
| 1206 |
remove--; |
| 1207 |
while (len > 0) { |
| 1208 |
len--; |
| 1209 |
if (name[len] == '/' || name[len] == '\\') { |
| 1210 |
break; |
| 1211 |
} |
| 1212 |
} |
| 1213 |
if (len < 4) { |
| 1214 |
len++; |
| 1215 |
break; |
| 1216 |
} |
| 1217 |
} else { |
| 1218 |
/* |
| 1219 |
* We do have '/..' |
| 1220 |
*/ |
| 1221 |
|
| 1222 |
len -= 3; |
| 1223 |
remove++; |
| 1224 |
} |
| 1225 |
} |
| 1226 |
|
| 1227 |
if (len < 4) { |
| 1228 |
if (len == 0) { |
| 1229 |
/* |
| 1230 |
* Not sure if this is possible, but we pass it on anyway. |
| 1231 |
*/ |
| 1232 |
} else if (len == 1 && (name[0] == '/' || name[0] == '\\')) { |
| 1233 |
/* |
| 1234 |
* Path is pointing to the root volume. |
| 1235 |
*/ |
| 1236 |
|
| 1237 |
return 1; |
| 1238 |
} else if ((name[1] == ':') |
| 1239 |
&& (len == 2 || (name[2] == '/' || name[2] == '\\'))) { |
| 1240 |
/* |
| 1241 |
* Path is of the form 'x:' or 'x:/' or 'x:\' |
| 1242 |
*/ |
| 1243 |
|
| 1244 |
return 1; |
| 1245 |
} |
| 1246 |
} |
| 1247 |
|
| 1248 |
return 0; |
| 1249 |
} |
| 1250 |
|
| 1251 |
/* |
| 1252 |
* Does the given path represent a reserved window path name? If not return 0, |
| 1253 |
* if true, return the number of characters of the path that we actually want |
| 1254 |
* (not any trailing :). |
| 1255 |
*/ |
| 1256 |
|
| 1257 |
static int |
| 1258 |
WinIsReserved( |
| 1259 |
const char *path) /* Path in UTF-8 */ |
| 1260 |
{ |
| 1261 |
if ((path[0] == 'c' || path[0] == 'C') |
| 1262 |
&& (path[1] == 'o' || path[1] == 'O')) { |
| 1263 |
if ((path[2] == 'm' || path[2] == 'M') |
| 1264 |
&& path[3] >= '1' && path[3] <= '9') { |
| 1265 |
/* |
| 1266 |
* May have match for 'com[1-9]:?', which is a serial port. |
| 1267 |
*/ |
| 1268 |
|
| 1269 |
if (path[4] == '\0') { |
| 1270 |
return 4; |
| 1271 |
} else if (path[4] == ':' && path[5] == '\0') { |
| 1272 |
return 4; |
| 1273 |
} |
| 1274 |
} else if ((path[2] == 'n' || path[2] == 'N') && path[3] == '\0') { |
| 1275 |
/* |
| 1276 |
* Have match for 'con' |
| 1277 |
*/ |
| 1278 |
|
| 1279 |
return 3; |
| 1280 |
} |
| 1281 |
|
| 1282 |
} else if ((path[0] == 'l' || path[0] == 'L') |
| 1283 |
&& (path[1] == 'p' || path[1] == 'P') |
| 1284 |
&& (path[2] == 't' || path[2] == 'T')) { |
| 1285 |
if (path[3] >= '1' && path[3] <= '9') { |
| 1286 |
/* |
| 1287 |
* May have match for 'lpt[1-9]:?' |
| 1288 |
*/ |
| 1289 |
|
| 1290 |
if (path[4] == '\0') { |
| 1291 |
return 4; |
| 1292 |
} else if (path[4] == ':' && path[5] == '\0') { |
| 1293 |
return 4; |
| 1294 |
} |
| 1295 |
} |
| 1296 |
|
| 1297 |
} else if (!strcasecmp(path, "prn") || !strcasecmp(path, "nul") |
| 1298 |
|| !strcasecmp(path, "aux")) { |
| 1299 |
/* |
| 1300 |
* Have match for 'prn', 'nul' or 'aux'. |
| 1301 |
*/ |
| 1302 |
|
| 1303 |
return 3; |
| 1304 |
} |
| 1305 |
return 0; |
| 1306 |
} |
| 1307 |
|
| 1308 |
/* |
| 1309 |
*---------------------------------------------------------------------- |
| 1310 |
* |
| 1311 |
* NativeMatchType -- |
| 1312 |
* |
| 1313 |
* This function needs a special case for a path which is a root volume, |
| 1314 |
* because for NTFS root volumes, the getFileAttributesProc returns a |
| 1315 |
* 'hidden' attribute when it should not. |
| 1316 |
* |
| 1317 |
* We never make any calls to a 'get attributes' routine here, since we |
| 1318 |
* have arranged things so that our caller already knows such |
| 1319 |
* information. |
| 1320 |
* |
| 1321 |
* Results: |
| 1322 |
* 0 = file doesn't match |
| 1323 |
* 1 = file matches |
| 1324 |
* |
| 1325 |
*---------------------------------------------------------------------- |
| 1326 |
*/ |
| 1327 |
|
| 1328 |
static int |
| 1329 |
NativeMatchType( |
| 1330 |
int isDrive, /* Is this a drive. */ |
| 1331 |
DWORD attr, /* We already know the attributes for the |
| 1332 |
* file. */ |
| 1333 |
const WCHAR *nativeName, /* Native path to check. */ |
| 1334 |
Tcl_GlobTypeData *types) /* Type description to match against. */ |
| 1335 |
{ |
| 1336 |
/* |
| 1337 |
* 'attr' represents the attributes of the file, but we only want to |
| 1338 |
* retrieve this info if it is absolutely necessary because it is an |
| 1339 |
* expensive call. Unfortunately, to deal with hidden files properly, we |
| 1340 |
* must always retrieve it. |
| 1341 |
*/ |
| 1342 |
|
| 1343 |
if (types == NULL) { |
| 1344 |
/* |
| 1345 |
* If invisible, don't return the file. |
| 1346 |
*/ |
| 1347 |
|
| 1348 |
return !(attr & FILE_ATTRIBUTE_HIDDEN && !isDrive); |
| 1349 |
} |
| 1350 |
|
| 1351 |
if (attr & FILE_ATTRIBUTE_HIDDEN && !isDrive) { |
| 1352 |
/* |
| 1353 |
* If invisible. |
| 1354 |
*/ |
| 1355 |
|
| 1356 |
if ((types->perm == 0) || !(types->perm & TCL_GLOB_PERM_HIDDEN)) { |
| 1357 |
return 0; |
| 1358 |
} |
| 1359 |
} else { |
| 1360 |
/* |
| 1361 |
* Visible. |
| 1362 |
*/ |
| 1363 |
|
| 1364 |
if (types->perm & TCL_GLOB_PERM_HIDDEN) { |
| 1365 |
return 0; |
| 1366 |
} |
| 1367 |
} |
| 1368 |
|
| 1369 |
if (types->perm != 0) { |
| 1370 |
if (((types->perm & TCL_GLOB_PERM_RONLY) && |
| 1371 |
!(attr & FILE_ATTRIBUTE_READONLY)) || |
| 1372 |
((types->perm & TCL_GLOB_PERM_R) && |
| 1373 |
(0 /* File exists => R_OK on Windows */)) || |
| 1374 |
((types->perm & TCL_GLOB_PERM_W) && |
| 1375 |
(attr & FILE_ATTRIBUTE_READONLY)) || |
| 1376 |
((types->perm & TCL_GLOB_PERM_X) && |
| 1377 |
(!(attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1378 |
&& !NativeIsExec(nativeName)))) { |
| 1379 |
return 0; |
| 1380 |
} |
| 1381 |
} |
| 1382 |
|
| 1383 |
if ((types->type & TCL_GLOB_TYPE_DIR) |
| 1384 |
&& (attr & FILE_ATTRIBUTE_DIRECTORY)) { |
| 1385 |
/* |
| 1386 |
* Quicker test for directory, which is a common case. |
| 1387 |
*/ |
| 1388 |
|
| 1389 |
return 1; |
| 1390 |
|
| 1391 |
} else if (types->type != 0) { |
| 1392 |
unsigned short st_mode; |
| 1393 |
int isExec = NativeIsExec(nativeName); |
| 1394 |
|
| 1395 |
st_mode = NativeStatMode(attr, 0, isExec); |
| 1396 |
|
| 1397 |
/* |
| 1398 |
* In order bcdpfls as in 'find -t' |
| 1399 |
*/ |
| 1400 |
|
| 1401 |
if (((types->type&TCL_GLOB_TYPE_BLOCK) && S_ISBLK(st_mode)) || |
| 1402 |
((types->type&TCL_GLOB_TYPE_CHAR) && S_ISCHR(st_mode)) || |
| 1403 |
((types->type&TCL_GLOB_TYPE_DIR) && S_ISDIR(st_mode)) || |
| 1404 |
((types->type&TCL_GLOB_TYPE_PIPE) && S_ISFIFO(st_mode)) || |
| 1405 |
#ifdef S_ISSOCK |
| 1406 |
((types->type&TCL_GLOB_TYPE_SOCK) && S_ISSOCK(st_mode)) || |
| 1407 |
#endif |
| 1408 |
((types->type&TCL_GLOB_TYPE_FILE) && S_ISREG(st_mode))) { |
| 1409 |
/* |
| 1410 |
* Do nothing - this file is ok. |
| 1411 |
*/ |
| 1412 |
} else { |
| 1413 |
#ifdef S_ISLNK |
| 1414 |
if (types->type & TCL_GLOB_TYPE_LINK) { |
| 1415 |
st_mode = NativeStatMode(attr, 1, isExec); |
| 1416 |
if (S_ISLNK(st_mode)) { |
| 1417 |
return 1; |
| 1418 |
} |
| 1419 |
} |
| 1420 |
#endif /* S_ISLNK */ |
| 1421 |
return 0; |
| 1422 |
} |
| 1423 |
} |
| 1424 |
return 1; |
| 1425 |
} |
| 1426 |
|
| 1427 |
/* |
| 1428 |
*---------------------------------------------------------------------- |
| 1429 |
* |
| 1430 |
* TclpGetUserHome -- |
| 1431 |
* |
| 1432 |
* This function takes the passed in user name and finds the |
| 1433 |
* corresponding home directory specified in the password file. |
| 1434 |
* |
| 1435 |
* Results: |
| 1436 |
* The result is a pointer to a string specifying the user's home |
| 1437 |
* directory, or NULL if the user's home directory could not be |
| 1438 |
* determined. Storage for the result string is allocated in bufferPtr; |
| 1439 |
* the caller must call Tcl_DStringFree() when the result is no longer |
| 1440 |
* needed. |
| 1441 |
* |
| 1442 |
* Side effects: |
| 1443 |
* None. |
| 1444 |
* |
| 1445 |
*---------------------------------------------------------------------- |
| 1446 |
*/ |
| 1447 |
|
| 1448 |
const char * |
| 1449 |
TclpGetUserHome( |
| 1450 |
const char *name, /* User name for desired home directory. */ |
| 1451 |
Tcl_DString *bufferPtr) /* Uninitialized or free DString filled with |
| 1452 |
* name of user's home directory. */ |
| 1453 |
{ |
| 1454 |
char *result = NULL; |
| 1455 |
USER_INFO_1 *uiPtr; |
| 1456 |
Tcl_DString ds; |
| 1457 |
int nameLen = -1; |
| 1458 |
int rc = 0; |
| 1459 |
const char *domain; |
| 1460 |
WCHAR *wName, *wHomeDir, *wDomain; |
| 1461 |
|
| 1462 |
Tcl_DStringInit(bufferPtr); |
| 1463 |
|
| 1464 |
wDomain = NULL; |
| 1465 |
domain = Tcl_UtfFindFirst(name, '@'); |
| 1466 |
if (domain == NULL) { |
| 1467 |
const char *ptr; |
| 1468 |
|
| 1469 |
/* |
| 1470 |
* Treat the current user as a special case because the general case |
| 1471 |
* below does not properly retrieve the path. The NetUserGetInfo |
| 1472 |
* call returns an empty path and the code defaults to the user's |
| 1473 |
* name in the profiles directory. On modern Windows systems, this |
| 1474 |
* is generally wrong as when the account is a Microsoft account, |
| 1475 |
* for example [email protected], the directory name is |
| 1476 |
* abcde and not abcdefghi. |
| 1477 |
* |
| 1478 |
* Note we could have just used env(USERPROFILE) here but |
| 1479 |
* the intent is to retrieve (as on Unix) the system's view |
| 1480 |
* of the home irrespective of environment settings of HOME |
| 1481 |
* and USERPROFILE. |
| 1482 |
* |
| 1483 |
* Fixing this for the general user needs more investigating but |
| 1484 |
* at least for the current user we can use a direct call. |
| 1485 |
*/ |
| 1486 |
ptr = TclpGetUserName(&ds); |
| 1487 |
if (ptr != NULL && strcasecmp(name, ptr) == 0) { |
| 1488 |
HANDLE hProcess; |
| 1489 |
WCHAR buf[MAX_PATH]; |
| 1490 |
DWORD nChars = sizeof(buf) / sizeof(buf[0]); |
| 1491 |
/* Sadly GetCurrentProcessToken not in Win 7 so slightly longer */ |
| 1492 |
hProcess = GetCurrentProcess(); /* Need not be closed */ |
| 1493 |
if (hProcess) { |
| 1494 |
HANDLE hToken; |
| 1495 |
if (OpenProcessToken(hProcess, TOKEN_QUERY, &hToken)) { |
| 1496 |
if (GetUserProfileDirectoryW(hToken, buf, &nChars)) { |
| 1497 |
Tcl_WinTCharToUtf((TCHAR *)buf, |
| 1498 |
(nChars-1)*sizeof(WCHAR), bufferPtr); |
| 1499 |
result = Tcl_DStringValue(bufferPtr); |
| 1500 |
rc = 1; |
| 1501 |
} |
| 1502 |
CloseHandle(hToken); |
| 1503 |
} |
| 1504 |
} |
| 1505 |
} |
| 1506 |
Tcl_DStringFree(&ds); |
| 1507 |
} else { |
| 1508 |
wName = (WCHAR *)Tcl_WinUtfToTChar(domain + 1, -1, &ds); |
| 1509 |
rc = NetGetDCName(NULL, wName, (LPBYTE *) &wDomain); |
| 1510 |
Tcl_DStringFree(&ds); |
| 1511 |
nameLen = domain - name; |
| 1512 |
} |
| 1513 |
if (rc == 0) { |
| 1514 |
wName = (WCHAR *)Tcl_WinUtfToTChar(name, nameLen, &ds); |
| 1515 |
while (NetUserGetInfo(wDomain, wName, 1, (LPBYTE *) &uiPtr) != 0) { |
| 1516 |
/* |
| 1517 |
* User does not exist; if domain was not specified, try again |
| 1518 |
* using current domain. |
| 1519 |
*/ |
| 1520 |
|
| 1521 |
rc = 1; |
| 1522 |
if (domain != NULL) { |
| 1523 |
break; |
| 1524 |
} |
| 1525 |
|
| 1526 |
/* |
| 1527 |
* Get current domain |
| 1528 |
*/ |
| 1529 |
|
| 1530 |
rc = NetGetDCName(NULL, NULL, (LPBYTE *) &wDomain); |
| 1531 |
if (rc != 0) { |
| 1532 |
break; |
| 1533 |
} |
| 1534 |
domain = (const char *)INT2PTR(-1); /* repeat once */ |
| 1535 |
} |
| 1536 |
if (rc == 0) { |
| 1537 |
DWORD i, size = MAX_PATH; |
| 1538 |
|
| 1539 |
wHomeDir = uiPtr->usri1_home_dir; |
| 1540 |
if ((wHomeDir != NULL) && (wHomeDir[0] != '\0')) { |
| 1541 |
size = lstrlenW(wHomeDir); |
| 1542 |
Tcl_WinTCharToUtf((TCHAR *)wHomeDir, size*sizeof(WCHAR), bufferPtr); |
| 1543 |
} else { |
| 1544 |
WCHAR buf[MAX_PATH]; |
| 1545 |
/* |
| 1546 |
* User exists but has no home dir. Return |
| 1547 |
* "{GetProfilesDirectory}/<user>". |
| 1548 |
*/ |
| 1549 |
|
| 1550 |
GetProfilesDirectoryW(buf, &size); |
| 1551 |
Tcl_WinTCharToUtf((TCHAR *)buf, (size-1)*sizeof(WCHAR), bufferPtr); |
| 1552 |
Tcl_DStringAppend(bufferPtr, "/", 1); |
| 1553 |
Tcl_DStringAppend(bufferPtr, name, nameLen); |
| 1554 |
} |
| 1555 |
result = Tcl_DStringValue(bufferPtr); |
| 1556 |
|
| 1557 |
/* |
| 1558 |
* Be sure we return normalized path |
| 1559 |
*/ |
| 1560 |
|
| 1561 |
for (i = 0; i < size; ++i) { |
| 1562 |
if (result[i] == '\\') { |
| 1563 |
result[i] = '/'; |
| 1564 |
} |
| 1565 |
} |
| 1566 |
NetApiBufferFree((void *) uiPtr); |
| 1567 |
} |
| 1568 |
Tcl_DStringFree(&ds); |
| 1569 |
} |
| 1570 |
if (wDomain != NULL) { |
| 1571 |
NetApiBufferFree((void *) wDomain); |
| 1572 |
} |
| 1573 |
|
| 1574 |
return result; |
| 1575 |
} |
| 1576 |
|
| 1577 |
/* |
| 1578 |
*--------------------------------------------------------------------------- |
| 1579 |
* |
| 1580 |
* NativeAccess -- |
| 1581 |
* |
| 1582 |
* This function replaces the library version of access(), fixing the |
| 1583 |
* following bugs: |
| 1584 |
* |
| 1585 |
* 1. access() returns that all files have execute permission. |
| 1586 |
* |
| 1587 |
* Results: |
| 1588 |
* See access documentation. |
| 1589 |
* |
| 1590 |
* Side effects: |
| 1591 |
* See access documentation. |
| 1592 |
* |
| 1593 |
*--------------------------------------------------------------------------- |
| 1594 |
*/ |
| 1595 |
|
| 1596 |
static int |
| 1597 |
NativeAccess( |
| 1598 |
const WCHAR *nativePath, /* Path of file to access, native encoding. */ |
| 1599 |
int mode) /* Permission setting. */ |
| 1600 |
{ |
| 1601 |
DWORD attr; |
| 1602 |
|
| 1603 |
attr = GetFileAttributesW(nativePath); |
| 1604 |
|
| 1605 |
if (attr == INVALID_FILE_ATTRIBUTES) { |
| 1606 |
/* |
| 1607 |
* File might not exist. |
| 1608 |
*/ |
| 1609 |
|
| 1610 |
DWORD lasterror = GetLastError(); |
| 1611 |
if (lasterror != ERROR_SHARING_VIOLATION) { |
| 1612 |
TclWinConvertError(lasterror); |
| 1613 |
return -1; |
| 1614 |
} |
| 1615 |
} |
| 1616 |
|
| 1617 |
if (mode == F_OK) { |
| 1618 |
/* |
| 1619 |
* File exists, nothing else to check. |
| 1620 |
*/ |
| 1621 |
|
| 1622 |
return 0; |
| 1623 |
} |
| 1624 |
|
| 1625 |
/* |
| 1626 |
* If it's not a directory (assume file), do several fast checks: |
| 1627 |
*/ |
| 1628 |
|
| 1629 |
if (!(attr & FILE_ATTRIBUTE_DIRECTORY)) { |
| 1630 |
/* |
| 1631 |
* If the attributes say this is not writable at all. The file is a |
| 1632 |
* regular file (i.e., not a directory), then the file is not |
| 1633 |
* writable, full stop. For directories, the read-only bit is |
| 1634 |
* (mostly) ignored by Windows, so we can't ascertain anything about |
| 1635 |
* directory access from the attrib data. However, if we have the |
| 1636 |
* advanced 'getFileSecurityProc', then more robust ACL checks will be |
| 1637 |
* done below. |
| 1638 |
*/ |
| 1639 |
|
| 1640 |
if ((mode & W_OK) && (attr & FILE_ATTRIBUTE_READONLY)) { |
| 1641 |
Tcl_SetErrno(EACCES); |
| 1642 |
return -1; |
| 1643 |
} |
| 1644 |
|
| 1645 |
/* |
| 1646 |
* If doesn't have the correct extension, it can't be executable |
| 1647 |
*/ |
| 1648 |
|
| 1649 |
if ((mode & X_OK) && !NativeIsExec(nativePath)) { |
| 1650 |
Tcl_SetErrno(EACCES); |
| 1651 |
return -1; |
| 1652 |
} |
| 1653 |
|
| 1654 |
/* |
| 1655 |
* Special case for read/write/executable check on file |
| 1656 |
*/ |
| 1657 |
|
| 1658 |
if ((mode & (R_OK|W_OK|X_OK)) && !(mode & ~(R_OK|W_OK|X_OK))) { |
| 1659 |
DWORD mask = 0; |
| 1660 |
HANDLE hFile; |
| 1661 |
|
| 1662 |
if (mode & R_OK) { |
| 1663 |
mask |= GENERIC_READ; |
| 1664 |
} |
| 1665 |
if (mode & W_OK) { |
| 1666 |
mask |= GENERIC_WRITE; |
| 1667 |
} |
| 1668 |
if (mode & X_OK) { |
| 1669 |
mask |= GENERIC_EXECUTE; |
| 1670 |
} |
| 1671 |
|
| 1672 |
hFile = CreateFileW(nativePath, mask, |
| 1673 |
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 1674 |
NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL); |
| 1675 |
if (hFile != INVALID_HANDLE_VALUE) { |
| 1676 |
CloseHandle(hFile); |
| 1677 |
return 0; |
| 1678 |
} |
| 1679 |
|
| 1680 |
/* |
| 1681 |
* Fast exit if access was denied |
| 1682 |
*/ |
| 1683 |
|
| 1684 |
if (GetLastError() == ERROR_ACCESS_DENIED) { |
| 1685 |
Tcl_SetErrno(EACCES); |
| 1686 |
return -1; |
| 1687 |
} |
| 1688 |
} |
| 1689 |
|
| 1690 |
/* |
| 1691 |
* We cannot verify the access fast, check it below using security |
| 1692 |
* info. |
| 1693 |
*/ |
| 1694 |
} |
| 1695 |
|
| 1696 |
/* |
| 1697 |
* It looks as if the permissions are ok, but if we are on NT, 2000 or XP, |
| 1698 |
* we have a more complex permissions structure so we try to check that. |
| 1699 |
* The code below is remarkably complex for such a simple thing as finding |
| 1700 |
* what permissions the OS has set for a file. |
| 1701 |
*/ |
| 1702 |
|
| 1703 |
{ |
| 1704 |
SECURITY_DESCRIPTOR *sdPtr = NULL; |
| 1705 |
unsigned long size; |
| 1706 |
PSID pSid = 0; |
| 1707 |
BOOL SidDefaulted; |
| 1708 |
SID_IDENTIFIER_AUTHORITY samba_unmapped = {{0, 0, 0, 0, 0, 22}}; |
| 1709 |
GENERIC_MAPPING genMap; |
| 1710 |
HANDLE hToken = NULL; |
| 1711 |
DWORD desiredAccess = 0, grantedAccess = 0; |
| 1712 |
BOOL accessYesNo = FALSE; |
| 1713 |
PRIVILEGE_SET privSet; |
| 1714 |
DWORD privSetSize = sizeof(PRIVILEGE_SET); |
| 1715 |
int error; |
| 1716 |
|
| 1717 |
/* |
| 1718 |
* First find out how big the buffer needs to be. |
| 1719 |
*/ |
| 1720 |
|
| 1721 |
size = 0; |
| 1722 |
GetFileSecurityW(nativePath, |
| 1723 |
OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
| 1724 |
| DACL_SECURITY_INFORMATION | LABEL_SECURITY_INFORMATION, |
| 1725 |
0, 0, &size); |
| 1726 |
|
| 1727 |
/* |
| 1728 |
* Should have failed with ERROR_INSUFFICIENT_BUFFER |
| 1729 |
*/ |
| 1730 |
|
| 1731 |
error = GetLastError(); |
| 1732 |
if (error != ERROR_INSUFFICIENT_BUFFER) { |
| 1733 |
/* |
| 1734 |
* Most likely case is ERROR_ACCESS_DENIED, which we will convert |
| 1735 |
* to EACCES - just what we want! |
| 1736 |
*/ |
| 1737 |
|
| 1738 |
TclWinConvertError((DWORD) error); |
| 1739 |
return -1; |
| 1740 |
} |
| 1741 |
|
| 1742 |
/* |
| 1743 |
* Now size contains the size of buffer needed. |
| 1744 |
*/ |
| 1745 |
|
| 1746 |
sdPtr = (SECURITY_DESCRIPTOR *) HeapAlloc(GetProcessHeap(), 0, size); |
| 1747 |
|
| 1748 |
if (sdPtr == NULL) { |
| 1749 |
goto accessError; |
| 1750 |
} |
| 1751 |
|
| 1752 |
/* |
| 1753 |
* Call GetFileSecurityW() for real. |
| 1754 |
*/ |
| 1755 |
|
| 1756 |
if (!GetFileSecurityW(nativePath, |
| 1757 |
OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
| 1758 |
| DACL_SECURITY_INFORMATION | LABEL_SECURITY_INFORMATION, |
| 1759 |
sdPtr, size, &size)) { |
| 1760 |
/* |
| 1761 |
* Error getting owner SD |
| 1762 |
*/ |
| 1763 |
|
| 1764 |
goto accessError; |
| 1765 |
} |
| 1766 |
|
| 1767 |
/* |
| 1768 |
* As of Samba 3.0.23 (10-Jul-2006), unmapped users and groups are |
| 1769 |
* assigned to SID domains S-1-22-1 and S-1-22-2, where "22" is the |
| 1770 |
* top-level authority. If the file owner and group is unmapped then |
| 1771 |
* the ACL access check below will only test against world access, |
| 1772 |
* which is likely to be more restrictive than the actual access |
| 1773 |
* restrictions. Since the ACL tests are more likely wrong than |
| 1774 |
* right, skip them. Moreover, the unix owner access permissions are |
| 1775 |
* usually mapped to the Windows attributes, so if the user is the |
| 1776 |
* file owner then the attrib checks above are correct (as far as they |
| 1777 |
* go). |
| 1778 |
*/ |
| 1779 |
|
| 1780 |
if(!GetSecurityDescriptorOwner(sdPtr,&pSid,&SidDefaulted) || |
| 1781 |
memcmp(GetSidIdentifierAuthority(pSid),&samba_unmapped, |
| 1782 |
sizeof(SID_IDENTIFIER_AUTHORITY))==0) { |
| 1783 |
HeapFree(GetProcessHeap(), 0, sdPtr); |
| 1784 |
return 0; /* Attrib tests say access allowed. */ |
| 1785 |
} |
| 1786 |
|
| 1787 |
/* |
| 1788 |
* Perform security impersonation of the user and open the resulting |
| 1789 |
* thread token. |
| 1790 |
*/ |
| 1791 |
|
| 1792 |
if (!ImpersonateSelf(SecurityImpersonation)) { |
| 1793 |
/* |
| 1794 |
* Unable to perform security impersonation. |
| 1795 |
*/ |
| 1796 |
|
| 1797 |
goto accessError; |
| 1798 |
} |
| 1799 |
if (!OpenThreadToken(GetCurrentThread(), |
| 1800 |
TOKEN_DUPLICATE | TOKEN_QUERY, FALSE, &hToken)) { |
| 1801 |
/* |
| 1802 |
* Unable to get current thread's token. |
| 1803 |
*/ |
| 1804 |
|
| 1805 |
goto accessError; |
| 1806 |
} |
| 1807 |
|
| 1808 |
RevertToSelf(); |
| 1809 |
|
| 1810 |
/* |
| 1811 |
* Setup desiredAccess according to the access privileges we are |
| 1812 |
* checking. |
| 1813 |
*/ |
| 1814 |
|
| 1815 |
if (mode & R_OK) { |
| 1816 |
desiredAccess |= FILE_GENERIC_READ; |
| 1817 |
} |
| 1818 |
if (mode & W_OK) { |
| 1819 |
desiredAccess |= FILE_GENERIC_WRITE; |
| 1820 |
} |
| 1821 |
if (mode & X_OK) { |
| 1822 |
desiredAccess |= FILE_GENERIC_EXECUTE; |
| 1823 |
} |
| 1824 |
|
| 1825 |
memset(&genMap, 0x0, sizeof(GENERIC_MAPPING)); |
| 1826 |
genMap.GenericRead = FILE_GENERIC_READ; |
| 1827 |
genMap.GenericWrite = FILE_GENERIC_WRITE; |
| 1828 |
genMap.GenericExecute = FILE_GENERIC_EXECUTE; |
| 1829 |
genMap.GenericAll = FILE_ALL_ACCESS; |
| 1830 |
|
| 1831 |
/* |
| 1832 |
* Perform access check using the token. |
| 1833 |
*/ |
| 1834 |
|
| 1835 |
if (!AccessCheck(sdPtr, hToken, desiredAccess, |
| 1836 |
&genMap, &privSet, &privSetSize, &grantedAccess, |
| 1837 |
&accessYesNo)) { |
| 1838 |
/* |
| 1839 |
* Unable to perform access check. |
| 1840 |
*/ |
| 1841 |
|
| 1842 |
accessError: |
| 1843 |
TclWinConvertError(GetLastError()); |
| 1844 |
if (sdPtr != NULL) { |
| 1845 |
HeapFree(GetProcessHeap(), 0, sdPtr); |
| 1846 |
} |
| 1847 |
if (hToken != NULL) { |
| 1848 |
CloseHandle(hToken); |
| 1849 |
} |
| 1850 |
return -1; |
| 1851 |
} |
| 1852 |
|
| 1853 |
/* |
| 1854 |
* Clean up. |
| 1855 |
*/ |
| 1856 |
|
| 1857 |
HeapFree(GetProcessHeap(), 0, sdPtr); |
| 1858 |
CloseHandle(hToken); |
| 1859 |
if (!accessYesNo) { |
| 1860 |
Tcl_SetErrno(EACCES); |
| 1861 |
return -1; |
| 1862 |
} |
| 1863 |
|
| 1864 |
} |
| 1865 |
return 0; |
| 1866 |
} |
| 1867 |
|
| 1868 |
/* |
| 1869 |
*---------------------------------------------------------------------- |
| 1870 |
* |
| 1871 |
* NativeIsExec -- |
| 1872 |
* |
| 1873 |
* Determines if a path is executable. On windows this is simply defined |
| 1874 |
* by whether the path ends in a standard executable extension. |
| 1875 |
* |
| 1876 |
* Results: |
| 1877 |
* 1 = executable, 0 = not. |
| 1878 |
* |
| 1879 |
*---------------------------------------------------------------------- |
| 1880 |
*/ |
| 1881 |
|
| 1882 |
static int |
| 1883 |
NativeIsExec( |
| 1884 |
const WCHAR *path) |
| 1885 |
{ |
| 1886 |
size_t len = wcslen(path); |
| 1887 |
|
| 1888 |
if (len < 5) { |
| 1889 |
return 0; |
| 1890 |
} |
| 1891 |
|
| 1892 |
if (path[len-4] != '.') { |
| 1893 |
return 0; |
| 1894 |
} |
| 1895 |
|
| 1896 |
path += len-3; |
| 1897 |
if ((_wcsicmp(path, L"exe") == 0) |
| 1898 |
|| (_wcsicmp(path, L"com") == 0) |
| 1899 |
|| (_wcsicmp(path, L"cmd") == 0) |
| 1900 |
|| (_wcsicmp(path, L"bat") == 0)) { |
| 1901 |
return 1; |
| 1902 |
} |
| 1903 |
return 0; |
| 1904 |
} |
| 1905 |
|
| 1906 |
/* |
| 1907 |
*---------------------------------------------------------------------- |
| 1908 |
* |
| 1909 |
* TclpObjChdir -- |
| 1910 |
* |
| 1911 |
* This function replaces the library version of chdir(). |
| 1912 |
* |
| 1913 |
* Results: |
| 1914 |
* See chdir() documentation. |
| 1915 |
* |
| 1916 |
* Side effects: |
| 1917 |
* See chdir() documentation. |
| 1918 |
* |
| 1919 |
*---------------------------------------------------------------------- |
| 1920 |
*/ |
| 1921 |
|
| 1922 |
int |
| 1923 |
TclpObjChdir( |
| 1924 |
Tcl_Obj *pathPtr) /* Path to new working directory. */ |
| 1925 |
{ |
| 1926 |
int result; |
| 1927 |
const WCHAR *nativePath; |
| 1928 |
|
| 1929 |
nativePath = (const WCHAR *)Tcl_FSGetNativePath(pathPtr); |
| 1930 |
|
| 1931 |
if (!nativePath) { |
| 1932 |
return -1; |
| 1933 |
} |
| 1934 |
result = SetCurrentDirectoryW(nativePath); |
| 1935 |
|
| 1936 |
if (result == 0) { |
| 1937 |
TclWinConvertError(GetLastError()); |
| 1938 |
return -1; |
| 1939 |
} |
| 1940 |
return 0; |
| 1941 |
} |
| 1942 |
|
| 1943 |
/* |
| 1944 |
*---------------------------------------------------------------------- |
| 1945 |
* |
| 1946 |
* TclpGetCwd -- |
| 1947 |
* |
| 1948 |
* This function replaces the library version of getcwd(). (Obsolete |
| 1949 |
* function, only retained for old extensions which may call it |
| 1950 |
* directly). |
| 1951 |
* |
| 1952 |
* Results: |
| 1953 |
* The result is a pointer to a string specifying the current directory, |
| 1954 |
* or NULL if the current directory could not be determined. If NULL is |
| 1955 |
* returned, an error message is left in the interp's result. Storage for |
| 1956 |
* the result string is allocated in bufferPtr; the caller must call |
| 1957 |
* Tcl_DStringFree() when the result is no longer needed. |
| 1958 |
* |
| 1959 |
* Side effects: |
| 1960 |
* None. |
| 1961 |
* |
| 1962 |
*---------------------------------------------------------------------- |
| 1963 |
*/ |
| 1964 |
|
| 1965 |
const char * |
| 1966 |
TclpGetCwd( |
| 1967 |
Tcl_Interp *interp, /* If non-NULL, used for error reporting. */ |
| 1968 |
Tcl_DString *bufferPtr) /* Uninitialized or free DString filled with |
| 1969 |
* name of current directory. */ |
| 1970 |
{ |
| 1971 |
WCHAR buffer[MAX_PATH]; |
| 1972 |
char *p; |
| 1973 |
WCHAR *native; |
| 1974 |
|
| 1975 |
if (GetCurrentDirectoryW(MAX_PATH, buffer) == 0) { |
| 1976 |
TclWinConvertError(GetLastError()); |
| 1977 |
if (interp != NULL) { |
| 1978 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1979 |
"error getting working directory name: %s", |
| 1980 |
Tcl_PosixError(interp))); |
| 1981 |
} |
| 1982 |
return NULL; |
| 1983 |
} |
| 1984 |
|
| 1985 |
/* |
| 1986 |
* Watch for the weird Windows c:\\UNC syntax. |
| 1987 |
*/ |
| 1988 |
|
| 1989 |
native = (WCHAR *) buffer; |
| 1990 |
if ((native[0] != '\0') && (native[1] == ':') |
| 1991 |
&& (native[2] == '\\') && (native[3] == '\\')) { |
| 1992 |
native += 2; |
| 1993 |
} |
| 1994 |
Tcl_WinTCharToUtf((TCHAR *) native, -1, bufferPtr); |
| 1995 |
|
| 1996 |
/* |
| 1997 |
* Convert to forward slashes for easier use in scripts. |
| 1998 |
*/ |
| 1999 |
|
| 2000 |
for (p = Tcl_DStringValue(bufferPtr); *p != '\0'; p++) { |
| 2001 |
if (*p == '\\') { |
| 2002 |
*p = '/'; |
| 2003 |
} |
| 2004 |
} |
| 2005 |
return Tcl_DStringValue(bufferPtr); |
| 2006 |
} |
| 2007 |
|
| 2008 |
int |
| 2009 |
TclpObjStat( |
| 2010 |
Tcl_Obj *pathPtr, /* Path of file to stat. */ |
| 2011 |
Tcl_StatBuf *statPtr) /* Filled with results of stat call. */ |
| 2012 |
{ |
| 2013 |
/* |
| 2014 |
* Ensure correct file sizes by forcing the OS to write any pending data |
| 2015 |
* to disk. This is done only for channels which are dirty, i.e. have been |
| 2016 |
* written to since the last flush here. |
| 2017 |
*/ |
| 2018 |
|
| 2019 |
TclWinFlushDirtyChannels(); |
| 2020 |
|
| 2021 |
return NativeStat((const WCHAR *)Tcl_FSGetNativePath(pathPtr), statPtr, 0); |
| 2022 |
} |
| 2023 |
|
| 2024 |
/* |
| 2025 |
*---------------------------------------------------------------------- |
| 2026 |
* |
| 2027 |
* NativeStat -- |
| 2028 |
* |
| 2029 |
* This function replaces the library version of stat(), fixing the |
| 2030 |
* following bugs: |
| 2031 |
* |
| 2032 |
* 1. stat("c:") returns an error. |
| 2033 |
* 2. Borland stat() return time in GMT instead of localtime. |
| 2034 |
* 3. stat("\\server\mount") would return error. |
| 2035 |
* 4. Accepts slashes or backslashes. |
| 2036 |
* 5. st_dev and st_rdev were wrong for UNC paths. |
| 2037 |
* |
| 2038 |
* Results: |
| 2039 |
* See stat documentation. |
| 2040 |
* |
| 2041 |
* Side effects: |
| 2042 |
* See stat documentation. |
| 2043 |
* |
| 2044 |
*---------------------------------------------------------------------- |
| 2045 |
*/ |
| 2046 |
|
| 2047 |
static int |
| 2048 |
NativeStat( |
| 2049 |
const WCHAR *nativePath, /* Path of file to stat */ |
| 2050 |
Tcl_StatBuf *statPtr, /* Filled with results of stat call. */ |
| 2051 |
int checkLinks) /* If non-zero, behave like 'lstat' */ |
| 2052 |
{ |
| 2053 |
DWORD attr; |
| 2054 |
int dev, nlink = 1; |
| 2055 |
unsigned short mode; |
| 2056 |
unsigned int inode = 0; |
| 2057 |
HANDLE fileHandle; |
| 2058 |
DWORD fileType = FILE_TYPE_UNKNOWN; |
| 2059 |
|
| 2060 |
/* |
| 2061 |
* If we can use 'createFile' on this, then we can use the resulting |
| 2062 |
* fileHandle to read more information (nlink, ino) than we can get from |
| 2063 |
* other attributes reading APIs. If not, then we try to fall back on the |
| 2064 |
* 'getFileAttributesExProc', and if that isn't available, then on even |
| 2065 |
* simpler routines. |
| 2066 |
* |
| 2067 |
* Special consideration must be given to Windows hard-coded names like |
| 2068 |
* CON, NULL, COM1, LPT1 etc. For these, we still need to do the |
| 2069 |
* CreateFile as some may not exist (e.g. there is no CON in wish by |
| 2070 |
* default). However the subsequent GetFileInformationByHandle will |
| 2071 |
* fail. We do a WinIsReserved to see if it is one of the special names, |
| 2072 |
* and if successful, mock up a BY_HANDLE_FILE_INFORMATION structure. |
| 2073 |
*/ |
| 2074 |
|
| 2075 |
fileHandle = CreateFileW(nativePath, GENERIC_READ, |
| 2076 |
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 2077 |
NULL, OPEN_EXISTING, |
| 2078 |
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 2079 |
|
| 2080 |
if (fileHandle != INVALID_HANDLE_VALUE) { |
| 2081 |
BY_HANDLE_FILE_INFORMATION data; |
| 2082 |
|
| 2083 |
if (GetFileInformationByHandle(fileHandle,&data) != TRUE) { |
| 2084 |
fileType = GetFileType(fileHandle); |
| 2085 |
CloseHandle(fileHandle); |
| 2086 |
if (fileType != FILE_TYPE_CHAR && fileType != FILE_TYPE_DISK) { |
| 2087 |
Tcl_SetErrno(ENOENT); |
| 2088 |
return -1; |
| 2089 |
} |
| 2090 |
|
| 2091 |
/* |
| 2092 |
* Mock up the expected structure |
| 2093 |
*/ |
| 2094 |
|
| 2095 |
memset(&data, 0, sizeof(data)); |
| 2096 |
statPtr->st_atime = 0; |
| 2097 |
statPtr->st_mtime = 0; |
| 2098 |
statPtr->st_ctime = 0; |
| 2099 |
} else { |
| 2100 |
CloseHandle(fileHandle); |
| 2101 |
statPtr->st_atime = ToCTime(data.ftLastAccessTime); |
| 2102 |
statPtr->st_mtime = ToCTime(data.ftLastWriteTime); |
| 2103 |
statPtr->st_ctime = ToCTime(data.ftCreationTime); |
| 2104 |
} |
| 2105 |
attr = data.dwFileAttributes; |
| 2106 |
statPtr->st_size = ((Tcl_WideInt)data.nFileSizeLow) | |
| 2107 |
(((Tcl_WideInt)data.nFileSizeHigh) << 32); |
| 2108 |
|
| 2109 |
/* |
| 2110 |
* On Unix, for directories, nlink apparently depends on the number of |
| 2111 |
* files in the directory. We could calculate that, but it would be a |
| 2112 |
* bit of a performance penalty, I think. Hence we just use what |
| 2113 |
* Windows gives us, which is the same as Unix for files, at least. |
| 2114 |
*/ |
| 2115 |
|
| 2116 |
nlink = data.nNumberOfLinks; |
| 2117 |
|
| 2118 |
/* |
| 2119 |
* Unfortunately our stat definition's inode field (unsigned short) |
| 2120 |
* will throw away most of the precision we have here, which means we |
| 2121 |
* can't rely on inode as a unique identifier of a file. We'd really |
| 2122 |
* like to do something like how we handle 'st_size'. |
| 2123 |
*/ |
| 2124 |
|
| 2125 |
inode = data.nFileIndexHigh | data.nFileIndexLow; |
| 2126 |
} else { |
| 2127 |
/* |
| 2128 |
* Fall back on the less capable routines. This means no nlink or ino. |
| 2129 |
*/ |
| 2130 |
|
| 2131 |
WIN32_FILE_ATTRIBUTE_DATA data; |
| 2132 |
|
| 2133 |
if (GetFileAttributesExW(nativePath, |
| 2134 |
GetFileExInfoStandard, &data) != TRUE) { |
| 2135 |
HANDLE hFind; |
| 2136 |
WIN32_FIND_DATAW ffd; |
| 2137 |
DWORD lasterror = GetLastError(); |
| 2138 |
|
| 2139 |
if (lasterror != ERROR_SHARING_VIOLATION) { |
| 2140 |
TclWinConvertError(lasterror); |
| 2141 |
return -1; |
| 2142 |
} |
| 2143 |
hFind = FindFirstFileW(nativePath, &ffd); |
| 2144 |
if (hFind == INVALID_HANDLE_VALUE) { |
| 2145 |
TclWinConvertError(GetLastError()); |
| 2146 |
return -1; |
| 2147 |
} |
| 2148 |
memcpy(&data, &ffd, sizeof(data)); |
| 2149 |
FindClose(hFind); |
| 2150 |
} |
| 2151 |
|
| 2152 |
attr = data.dwFileAttributes; |
| 2153 |
|
| 2154 |
statPtr->st_size = ((Tcl_WideInt)data.nFileSizeLow) | |
| 2155 |
(((Tcl_WideInt)data.nFileSizeHigh) << 32); |
| 2156 |
statPtr->st_atime = ToCTime(data.ftLastAccessTime); |
| 2157 |
statPtr->st_mtime = ToCTime(data.ftLastWriteTime); |
| 2158 |
statPtr->st_ctime = ToCTime(data.ftCreationTime); |
| 2159 |
} |
| 2160 |
|
| 2161 |
dev = NativeDev(nativePath); |
| 2162 |
mode = NativeStatMode(attr, checkLinks, NativeIsExec(nativePath)); |
| 2163 |
if (fileType == FILE_TYPE_CHAR) { |
| 2164 |
mode &= ~S_IFMT; |
| 2165 |
mode |= S_IFCHR; |
| 2166 |
} else if (fileType == FILE_TYPE_DISK) { |
| 2167 |
mode &= ~S_IFMT; |
| 2168 |
mode |= S_IFBLK; |
| 2169 |
} |
| 2170 |
|
| 2171 |
statPtr->st_dev = (dev_t) dev; |
| 2172 |
statPtr->st_ino = inode; |
| 2173 |
statPtr->st_mode = mode; |
| 2174 |
statPtr->st_nlink = nlink; |
| 2175 |
statPtr->st_uid = 0; |
| 2176 |
statPtr->st_gid = 0; |
| 2177 |
statPtr->st_rdev = (dev_t) dev; |
| 2178 |
return 0; |
| 2179 |
} |
| 2180 |
|
| 2181 |
/* |
| 2182 |
*---------------------------------------------------------------------- |
| 2183 |
* |
| 2184 |
* NativeDev -- |
| 2185 |
* |
| 2186 |
* Calculate just the 'st_dev' field of a 'stat' structure. |
| 2187 |
* |
| 2188 |
*---------------------------------------------------------------------- |
| 2189 |
*/ |
| 2190 |
|
| 2191 |
static int |
| 2192 |
NativeDev( |
| 2193 |
const WCHAR *nativePath) /* Full path of file to stat */ |
| 2194 |
{ |
| 2195 |
int dev; |
| 2196 |
Tcl_DString ds; |
| 2197 |
WCHAR nativeFullPath[MAX_PATH]; |
| 2198 |
WCHAR *nativePart; |
| 2199 |
const char *fullPath; |
| 2200 |
|
| 2201 |
GetFullPathNameW(nativePath, MAX_PATH, nativeFullPath, &nativePart); |
| 2202 |
fullPath = Tcl_WinTCharToUtf((TCHAR *)nativeFullPath, -1, &ds); |
| 2203 |
|
| 2204 |
if ((fullPath[0] == '\\') && (fullPath[1] == '\\')) { |
| 2205 |
const char *p; |
| 2206 |
DWORD dw; |
| 2207 |
const WCHAR *nativeVol; |
| 2208 |
Tcl_DString volString; |
| 2209 |
|
| 2210 |
p = strchr(fullPath + 2, '\\'); |
| 2211 |
p = strchr(p + 1, '\\'); |
| 2212 |
if (p == NULL) { |
| 2213 |
/* |
| 2214 |
* Add terminating backslash to fullpath or GetVolumeInformationW() |
| 2215 |
* won't work. |
| 2216 |
*/ |
| 2217 |
|
| 2218 |
fullPath = TclDStringAppendLiteral(&ds, "\\"); |
| 2219 |
p = fullPath + Tcl_DStringLength(&ds); |
| 2220 |
} else { |
| 2221 |
p++; |
| 2222 |
} |
| 2223 |
nativeVol = (WCHAR *)Tcl_WinUtfToTChar(fullPath, p - fullPath, &volString); |
| 2224 |
dw = (DWORD) -1; |
| 2225 |
GetVolumeInformationW(nativeVol, NULL, 0, &dw, NULL, NULL, NULL, 0); |
| 2226 |
|
| 2227 |
/* |
| 2228 |
* GetFullPathNameW() turns special devices like "NUL" into "\\.\NUL", |
| 2229 |
* but GetVolumeInformationW() returns failure for "\\.\NUL". This will |
| 2230 |
* cause "NUL" to get a drive number of -1, which makes about as much |
| 2231 |
* sense as anything since the special devices don't live on any |
| 2232 |
* drive. |
| 2233 |
*/ |
| 2234 |
|
| 2235 |
dev = dw; |
| 2236 |
Tcl_DStringFree(&volString); |
| 2237 |
} else if ((fullPath[0] != '\0') && (fullPath[1] == ':')) { |
| 2238 |
dev = Tcl_UniCharToLower(fullPath[0]) - 'a'; |
| 2239 |
} else { |
| 2240 |
dev = -1; |
| 2241 |
} |
| 2242 |
Tcl_DStringFree(&ds); |
| 2243 |
|
| 2244 |
return dev; |
| 2245 |
} |
| 2246 |
|
| 2247 |
/* |
| 2248 |
*---------------------------------------------------------------------- |
| 2249 |
* |
| 2250 |
* NativeStatMode -- |
| 2251 |
* |
| 2252 |
* Calculate just the 'st_mode' field of a 'stat' structure. |
| 2253 |
* |
| 2254 |
* In many places we don't need the full stat structure, and it's much |
| 2255 |
* faster just to calculate these pieces, if that's all we need. |
| 2256 |
* |
| 2257 |
*---------------------------------------------------------------------- |
| 2258 |
*/ |
| 2259 |
|
| 2260 |
static unsigned short |
| 2261 |
NativeStatMode( |
| 2262 |
DWORD attr, |
| 2263 |
int checkLinks, |
| 2264 |
int isExec) |
| 2265 |
{ |
| 2266 |
int mode; |
| 2267 |
|
| 2268 |
if (checkLinks && (attr & FILE_ATTRIBUTE_REPARSE_POINT)) { |
| 2269 |
/* |
| 2270 |
* It is a link. |
| 2271 |
*/ |
| 2272 |
|
| 2273 |
mode = S_IFLNK; |
| 2274 |
} else { |
| 2275 |
mode = (attr & FILE_ATTRIBUTE_DIRECTORY) ? S_IFDIR|S_IEXEC : S_IFREG; |
| 2276 |
} |
| 2277 |
mode |= (attr & FILE_ATTRIBUTE_READONLY) ? S_IREAD : S_IREAD|S_IWRITE; |
| 2278 |
if (isExec) { |
| 2279 |
mode |= S_IEXEC; |
| 2280 |
} |
| 2281 |
|
| 2282 |
/* |
| 2283 |
* Propagate the S_IREAD, S_IWRITE, S_IEXEC bits to the group and other |
| 2284 |
* positions. |
| 2285 |
*/ |
| 2286 |
|
| 2287 |
mode |= (mode & (S_IREAD|S_IWRITE|S_IEXEC)) >> 3; |
| 2288 |
mode |= (mode & (S_IREAD|S_IWRITE|S_IEXEC)) >> 6; |
| 2289 |
return (unsigned short) mode; |
| 2290 |
} |
| 2291 |
|
| 2292 |
/* |
| 2293 |
*------------------------------------------------------------------------ |
| 2294 |
* |
| 2295 |
* ToCTime -- |
| 2296 |
* |
| 2297 |
* Converts a Windows FILETIME to a time_t in UTC. |
| 2298 |
* |
| 2299 |
* Results: |
| 2300 |
* Returns the count of seconds from the Posix epoch. |
| 2301 |
* |
| 2302 |
*------------------------------------------------------------------------ |
| 2303 |
*/ |
| 2304 |
|
| 2305 |
static time_t |
| 2306 |
ToCTime( |
| 2307 |
FILETIME fileTime) /* UTC time */ |
| 2308 |
{ |
| 2309 |
LARGE_INTEGER convertedTime; |
| 2310 |
|
| 2311 |
convertedTime.LowPart = fileTime.dwLowDateTime; |
| 2312 |
convertedTime.HighPart = (LONG) fileTime.dwHighDateTime; |
| 2313 |
|
| 2314 |
return (time_t) ((convertedTime.QuadPart - |
| 2315 |
(Tcl_WideInt)POSIX_EPOCH_AS_FILETIME) / (Tcl_WideInt)10000000); |
| 2316 |
} |
| 2317 |
|
| 2318 |
/* |
| 2319 |
*------------------------------------------------------------------------ |
| 2320 |
* |
| 2321 |
* FromCTime -- |
| 2322 |
* |
| 2323 |
* Converts a time_t to a Windows FILETIME |
| 2324 |
* |
| 2325 |
* Results: |
| 2326 |
* Returns the count of 100-ns ticks seconds from the Windows epoch. |
| 2327 |
* |
| 2328 |
*------------------------------------------------------------------------ |
| 2329 |
*/ |
| 2330 |
|
| 2331 |
static void |
| 2332 |
FromCTime( |
| 2333 |
time_t posixTime, |
| 2334 |
FILETIME *fileTime) /* UTC Time */ |
| 2335 |
{ |
| 2336 |
LARGE_INTEGER convertedTime; |
| 2337 |
|
| 2338 |
convertedTime.QuadPart = ((LONGLONG) posixTime) * 10000000 |
| 2339 |
+ POSIX_EPOCH_AS_FILETIME; |
| 2340 |
fileTime->dwLowDateTime = convertedTime.LowPart; |
| 2341 |
fileTime->dwHighDateTime = convertedTime.HighPart; |
| 2342 |
} |
| 2343 |
|
| 2344 |
/* |
| 2345 |
*--------------------------------------------------------------------------- |
| 2346 |
* |
| 2347 |
* TclpGetNativeCwd -- |
| 2348 |
* |
| 2349 |
* This function replaces the library version of getcwd(). |
| 2350 |
* |
| 2351 |
* Results: |
| 2352 |
* The input and output are filesystem paths in native form. The result |
| 2353 |
* is either the given clientData, if the working directory hasn't |
| 2354 |
* changed, or a new clientData (owned by our caller), giving the new |
| 2355 |
* native path, or NULL if the current directory could not be determined. |
| 2356 |
* If NULL is returned, the caller can examine the standard Posix error |
| 2357 |
* codes to determine the cause of the problem. |
| 2358 |
* |
| 2359 |
* Side effects: |
| 2360 |
* None. |
| 2361 |
* |
| 2362 |
*---------------------------------------------------------------------- |
| 2363 |
*/ |
| 2364 |
|
| 2365 |
ClientData |
| 2366 |
TclpGetNativeCwd( |
| 2367 |
ClientData clientData) |
| 2368 |
{ |
| 2369 |
WCHAR buffer[MAX_PATH]; |
| 2370 |
|
| 2371 |
if (GetCurrentDirectoryW(MAX_PATH, buffer) == 0) { |
| 2372 |
TclWinConvertError(GetLastError()); |
| 2373 |
return NULL; |
| 2374 |
} |
| 2375 |
|
| 2376 |
if (clientData != NULL) { |
| 2377 |
if (wcscmp((const WCHAR *) clientData, buffer) == 0) { |
| 2378 |
return clientData; |
| 2379 |
} |
| 2380 |
} |
| 2381 |
|
| 2382 |
return TclNativeDupInternalRep(buffer); |
| 2383 |
} |
| 2384 |
|
| 2385 |
int |
| 2386 |
TclpObjAccess( |
| 2387 |
Tcl_Obj *pathPtr, |
| 2388 |
int mode) |
| 2389 |
{ |
| 2390 |
return NativeAccess((const WCHAR *)Tcl_FSGetNativePath(pathPtr), mode); |
| 2391 |
} |
| 2392 |
|
| 2393 |
int |
| 2394 |
TclpObjLstat( |
| 2395 |
Tcl_Obj *pathPtr, |
| 2396 |
Tcl_StatBuf *statPtr) |
| 2397 |
{ |
| 2398 |
/* |
| 2399 |
* Ensure correct file sizes by forcing the OS to write any pending data |
| 2400 |
* to disk. This is done only for channels which are dirty, i.e. have been |
| 2401 |
* written to since the last flush here. |
| 2402 |
*/ |
| 2403 |
|
| 2404 |
TclWinFlushDirtyChannels(); |
| 2405 |
|
| 2406 |
return NativeStat((const WCHAR *)Tcl_FSGetNativePath(pathPtr), statPtr, 1); |
| 2407 |
} |
| 2408 |
|
| 2409 |
#ifdef S_IFLNK |
| 2410 |
Tcl_Obj * |
| 2411 |
TclpObjLink( |
| 2412 |
Tcl_Obj *pathPtr, |
| 2413 |
Tcl_Obj *toPtr, |
| 2414 |
int linkAction) |
| 2415 |
{ |
| 2416 |
if (toPtr != NULL) { |
| 2417 |
int res; |
| 2418 |
const WCHAR *LinkTarget; |
| 2419 |
const WCHAR *LinkSource = (const WCHAR *)Tcl_FSGetNativePath(pathPtr); |
| 2420 |
Tcl_Obj *normToPtr = Tcl_FSGetNormalizedPath(NULL, toPtr); |
| 2421 |
|
| 2422 |
if (normToPtr == NULL) { |
| 2423 |
return NULL; |
| 2424 |
} |
| 2425 |
if (normToPtr != toPtr) { Tcl_IncrRefCount(normToPtr); } |
| 2426 |
|
| 2427 |
LinkTarget = (const WCHAR *)Tcl_FSGetNativePath(normToPtr); |
| 2428 |
|
| 2429 |
if (LinkSource == NULL || LinkTarget == NULL) { |
| 2430 |
if (normToPtr != toPtr) { Tcl_DecrRefCount(normToPtr); } |
| 2431 |
return NULL; |
| 2432 |
} |
| 2433 |
res = WinLink(LinkSource, LinkTarget, linkAction); |
| 2434 |
if (normToPtr != toPtr) { Tcl_DecrRefCount(normToPtr); } |
| 2435 |
if (res == 0) { |
| 2436 |
return toPtr; |
| 2437 |
} else { |
| 2438 |
return NULL; |
| 2439 |
} |
| 2440 |
} else { |
| 2441 |
const WCHAR *LinkSource = (const WCHAR *)Tcl_FSGetNativePath(pathPtr); |
| 2442 |
|
| 2443 |
if (LinkSource == NULL) { |
| 2444 |
return NULL; |
| 2445 |
} |
| 2446 |
return WinReadLink(LinkSource); |
| 2447 |
} |
| 2448 |
} |
| 2449 |
#endif /* S_IFLNK */ |
| 2450 |
|
| 2451 |
/* |
| 2452 |
*--------------------------------------------------------------------------- |
| 2453 |
* |
| 2454 |
* TclpFilesystemPathType -- |
| 2455 |
* |
| 2456 |
* This function is part of the native filesystem support, and returns |
| 2457 |
* the path type of the given path. Returns NTFS or FAT or whatever is |
| 2458 |
* returned by the 'volume information' proc. |
| 2459 |
* |
| 2460 |
* Results: |
| 2461 |
* NULL at present. |
| 2462 |
* |
| 2463 |
* Side effects: |
| 2464 |
* None. |
| 2465 |
* |
| 2466 |
*--------------------------------------------------------------------------- |
| 2467 |
*/ |
| 2468 |
|
| 2469 |
Tcl_Obj * |
| 2470 |
TclpFilesystemPathType( |
| 2471 |
Tcl_Obj *pathPtr) |
| 2472 |
{ |
| 2473 |
#define VOL_BUF_SIZE 32 |
| 2474 |
int found; |
| 2475 |
WCHAR volType[VOL_BUF_SIZE]; |
| 2476 |
char *firstSeparator; |
| 2477 |
const char *path; |
| 2478 |
Tcl_Obj *normPath = Tcl_FSGetNormalizedPath(NULL, pathPtr); |
| 2479 |
|
| 2480 |
if (normPath == NULL) { |
| 2481 |
return NULL; |
| 2482 |
} |
| 2483 |
path = Tcl_GetString(normPath); |
| 2484 |
if (path == NULL) { |
| 2485 |
return NULL; |
| 2486 |
} |
| 2487 |
|
| 2488 |
firstSeparator = strchr(path, '/'); |
| 2489 |
if (firstSeparator == NULL) { |
| 2490 |
found = GetVolumeInformationW((const WCHAR *)Tcl_FSGetNativePath(pathPtr), |
| 2491 |
NULL, 0, NULL, NULL, NULL, volType, VOL_BUF_SIZE); |
| 2492 |
} else { |
| 2493 |
Tcl_Obj *driveName = Tcl_NewStringObj(path, firstSeparator - path+1); |
| 2494 |
|
| 2495 |
Tcl_IncrRefCount(driveName); |
| 2496 |
found = GetVolumeInformationW((const WCHAR *)Tcl_FSGetNativePath(driveName), |
| 2497 |
NULL, 0, NULL, NULL, NULL, volType, VOL_BUF_SIZE); |
| 2498 |
Tcl_DecrRefCount(driveName); |
| 2499 |
} |
| 2500 |
|
| 2501 |
if (found == 0) { |
| 2502 |
return NULL; |
| 2503 |
} else { |
| 2504 |
Tcl_DString ds; |
| 2505 |
|
| 2506 |
Tcl_WinTCharToUtf((TCHAR *)volType, -1, &ds); |
| 2507 |
return TclDStringToObj(&ds); |
| 2508 |
} |
| 2509 |
#undef VOL_BUF_SIZE |
| 2510 |
} |
| 2511 |
|
| 2512 |
/* |
| 2513 |
* This define can be turned on to experiment with a different way of |
| 2514 |
* normalizing paths (using a different Windows API). Unfortunately the new |
| 2515 |
* path seems to take almost exactly the same amount of time as the old path! |
| 2516 |
* The primary time taken by normalization is in |
| 2517 |
* GetFileAttributesEx/FindFirstFile or GetFileAttributesEx/GetLongPathName. |
| 2518 |
* Conversion to/from native is not a significant factor at all. |
| 2519 |
* |
| 2520 |
* Also, since we have to check for symbolic links (reparse points) then we |
| 2521 |
* have to call GetFileAttributes on each path segment anyway, so there's no |
| 2522 |
* benefit to doing anything clever there. |
| 2523 |
*/ |
| 2524 |
|
| 2525 |
/* #define TclNORM_LONG_PATH */ |
| 2526 |
|
| 2527 |
/* |
| 2528 |
*--------------------------------------------------------------------------- |
| 2529 |
* |
| 2530 |
* TclpObjNormalizePath -- |
| 2531 |
* |
| 2532 |
* This function scans through a path specification and replaces it, in |
| 2533 |
* place, with a normalized version. This means using the 'longname', and |
| 2534 |
* expanding any symbolic links contained within the path. |
| 2535 |
* |
| 2536 |
* Results: |
| 2537 |
* The new 'nextCheckpoint' value, giving as far as we could understand |
| 2538 |
* in the path. |
| 2539 |
* |
| 2540 |
* Side effects: |
| 2541 |
* The pathPtr string, which must contain a valid path, is possibly |
| 2542 |
* modified in place. |
| 2543 |
* |
| 2544 |
*--------------------------------------------------------------------------- |
| 2545 |
*/ |
| 2546 |
|
| 2547 |
int |
| 2548 |
TclpObjNormalizePath( |
| 2549 |
Tcl_Interp *interp, /* not used */ |
| 2550 |
Tcl_Obj *pathPtr, /* An unshared object containing the path to |
| 2551 |
* normalize */ |
| 2552 |
int nextCheckpoint) /* offset to start at in pathPtr */ |
| 2553 |
{ |
| 2554 |
char *lastValidPathEnd = NULL; |
| 2555 |
Tcl_DString dsNorm; /* This will hold the normalized string. */ |
| 2556 |
char *path, *currentPathEndPosition; |
| 2557 |
Tcl_Obj *temp = NULL; |
| 2558 |
int isDrive = 1; |
| 2559 |
Tcl_DString ds; /* Some workspace. */ |
| 2560 |
(void)interp; |
| 2561 |
|
| 2562 |
Tcl_DStringInit(&dsNorm); |
| 2563 |
path = Tcl_GetString(pathPtr); |
| 2564 |
|
| 2565 |
currentPathEndPosition = path + nextCheckpoint; |
| 2566 |
if (*currentPathEndPosition == '/') { |
| 2567 |
currentPathEndPosition++; |
| 2568 |
} |
| 2569 |
while (1) { |
| 2570 |
char cur = *currentPathEndPosition; |
| 2571 |
|
| 2572 |
if ((cur=='/' || cur==0) && (path != currentPathEndPosition)) { |
| 2573 |
/* |
| 2574 |
* Reached directory separator, or end of string. |
| 2575 |
*/ |
| 2576 |
|
| 2577 |
WIN32_FILE_ATTRIBUTE_DATA data; |
| 2578 |
const WCHAR *nativePath = (WCHAR *)Tcl_WinUtfToTChar(path, |
| 2579 |
currentPathEndPosition - path, &ds); |
| 2580 |
|
| 2581 |
if (GetFileAttributesExW(nativePath, |
| 2582 |
GetFileExInfoStandard, &data) != TRUE) { |
| 2583 |
/* |
| 2584 |
* File doesn't exist. |
| 2585 |
*/ |
| 2586 |
|
| 2587 |
if (isDrive) { |
| 2588 |
int len = WinIsReserved(path); |
| 2589 |
|
| 2590 |
if (len > 0) { |
| 2591 |
/* |
| 2592 |
* Actually it does exist - COM1, etc. |
| 2593 |
*/ |
| 2594 |
|
| 2595 |
int i; |
| 2596 |
|
| 2597 |
for (i=0 ; i<len ; i++) { |
| 2598 |
WCHAR wc = ((WCHAR *)nativePath)[i]; |
| 2599 |
|
| 2600 |
if (wc >= 'a') { |
| 2601 |
wc -= ('a' - 'A'); |
| 2602 |
((WCHAR *) nativePath)[i] = wc; |
| 2603 |
} |
| 2604 |
} |
| 2605 |
Tcl_DStringAppend(&dsNorm, |
| 2606 |
(const char *)nativePath, |
| 2607 |
(int)(sizeof(WCHAR) * len)); |
| 2608 |
lastValidPathEnd = currentPathEndPosition; |
| 2609 |
} else if (nextCheckpoint == 0) { |
| 2610 |
/* |
| 2611 |
* Path starts with a drive designation that's not |
| 2612 |
* actually on the system. We still must normalize up |
| 2613 |
* past the first separator. [Bug 3603434] |
| 2614 |
*/ |
| 2615 |
|
| 2616 |
currentPathEndPosition++; |
| 2617 |
} |
| 2618 |
} |
| 2619 |
Tcl_DStringFree(&ds); |
| 2620 |
break; |
| 2621 |
} |
| 2622 |
|
| 2623 |
/* |
| 2624 |
* File 'nativePath' does exist if we get here. We now want to |
| 2625 |
* check if it is a symlink and otherwise continue with the |
| 2626 |
* rest of the path. |
| 2627 |
*/ |
| 2628 |
|
| 2629 |
/* |
| 2630 |
* Check for symlinks, except at last component of path (we don't |
| 2631 |
* follow final symlinks). Also a drive (C:/) for example, may |
| 2632 |
* sometimes have the reparse flag set for some reason I don't |
| 2633 |
* understand. We therefore don't perform this check for drives. |
| 2634 |
*/ |
| 2635 |
|
| 2636 |
if (cur != 0 && !isDrive && |
| 2637 |
data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT){ |
| 2638 |
Tcl_Obj *to = WinReadLinkDirectory(nativePath); |
| 2639 |
|
| 2640 |
if (to != NULL) { |
| 2641 |
/* |
| 2642 |
* Read the reparse point ok. Now, reparse points need not |
| 2643 |
* be normalized, otherwise we could use: |
| 2644 |
* |
| 2645 |
* Tcl_GetStringFromObj(to, &pathLen); |
| 2646 |
* nextCheckpoint = pathLen; |
| 2647 |
* |
| 2648 |
* So, instead we have to start from the beginning. |
| 2649 |
*/ |
| 2650 |
|
| 2651 |
nextCheckpoint = 0; |
| 2652 |
Tcl_AppendToObj(to, currentPathEndPosition, -1); |
| 2653 |
|
| 2654 |
/* |
| 2655 |
* Convert link to forward slashes. |
| 2656 |
*/ |
| 2657 |
|
| 2658 |
for (path = Tcl_GetString(to); *path != 0; path++) { |
| 2659 |
if (*path == '\\') { |
| 2660 |
*path = '/'; |
| 2661 |
} |
| 2662 |
} |
| 2663 |
path = Tcl_GetString(to); |
| 2664 |
currentPathEndPosition = path + nextCheckpoint; |
| 2665 |
if (temp != NULL) { |
| 2666 |
Tcl_DecrRefCount(temp); |
| 2667 |
} |
| 2668 |
temp = to; |
| 2669 |
|
| 2670 |
/* |
| 2671 |
* Reset variables so we can restart normalization. |
| 2672 |
*/ |
| 2673 |
|
| 2674 |
isDrive = 1; |
| 2675 |
Tcl_DStringFree(&dsNorm); |
| 2676 |
Tcl_DStringFree(&ds); |
| 2677 |
continue; |
| 2678 |
} |
| 2679 |
} |
| 2680 |
|
| 2681 |
#ifndef TclNORM_LONG_PATH |
| 2682 |
/* |
| 2683 |
* Now we convert the tail of the current path to its 'long form', |
| 2684 |
* and append it to 'dsNorm' which holds the current normalized |
| 2685 |
* path |
| 2686 |
*/ |
| 2687 |
|
| 2688 |
if (isDrive) { |
| 2689 |
WCHAR drive = ((WCHAR *) nativePath)[0]; |
| 2690 |
|
| 2691 |
if (drive >= 'a') { |
| 2692 |
drive -= ('a' - 'A'); |
| 2693 |
((WCHAR *) nativePath)[0] = drive; |
| 2694 |
} |
| 2695 |
Tcl_DStringAppend(&dsNorm, (const char *)nativePath, |
| 2696 |
Tcl_DStringLength(&ds)); |
| 2697 |
} else { |
| 2698 |
char *checkDots = NULL; |
| 2699 |
|
| 2700 |
if (lastValidPathEnd[1] == '.') { |
| 2701 |
checkDots = lastValidPathEnd + 1; |
| 2702 |
while (checkDots < currentPathEndPosition) { |
| 2703 |
if (*checkDots != '.') { |
| 2704 |
checkDots = NULL; |
| 2705 |
break; |
| 2706 |
} |
| 2707 |
checkDots++; |
| 2708 |
} |
| 2709 |
} |
| 2710 |
if (checkDots != NULL) { |
| 2711 |
int dotLen = currentPathEndPosition-lastValidPathEnd; |
| 2712 |
|
| 2713 |
/* |
| 2714 |
* Path is just dots. We shouldn't really ever see a path |
| 2715 |
* like that. However, to be nice we at least don't mangle |
| 2716 |
* the path - we just add the dots as a path segment and |
| 2717 |
* continue. |
| 2718 |
*/ |
| 2719 |
|
| 2720 |
Tcl_DStringAppend(&dsNorm, ((const char *)nativePath) |
| 2721 |
+ Tcl_DStringLength(&ds) |
| 2722 |
- (dotLen * sizeof(WCHAR)), |
| 2723 |
dotLen * sizeof(WCHAR)); |
| 2724 |
} else { |
| 2725 |
/* |
| 2726 |
* Normal path. |
| 2727 |
*/ |
| 2728 |
|
| 2729 |
WIN32_FIND_DATAW fData; |
| 2730 |
HANDLE handle; |
| 2731 |
|
| 2732 |
handle = FindFirstFileW((WCHAR *) nativePath, &fData); |
| 2733 |
if (handle == INVALID_HANDLE_VALUE) { |
| 2734 |
/* |
| 2735 |
* This is usually the '/' in 'c:/' at end of string. |
| 2736 |
*/ |
| 2737 |
|
| 2738 |
Tcl_DStringAppend(&dsNorm, (const char *) L"/", |
| 2739 |
sizeof(WCHAR)); |
| 2740 |
} else { |
| 2741 |
WCHAR *nativeName; |
| 2742 |
|
| 2743 |
if (fData.cFileName[0] != '\0') { |
| 2744 |
nativeName = fData.cFileName; |
| 2745 |
} else { |
| 2746 |
nativeName = fData.cAlternateFileName; |
| 2747 |
} |
| 2748 |
FindClose(handle); |
| 2749 |
Tcl_DStringAppend(&dsNorm, (const char *) L"/", |
| 2750 |
sizeof(WCHAR)); |
| 2751 |
Tcl_DStringAppend(&dsNorm, |
| 2752 |
(const char *) nativeName, |
| 2753 |
(int) (wcslen(nativeName)*sizeof(WCHAR))); |
| 2754 |
} |
| 2755 |
} |
| 2756 |
} |
| 2757 |
#endif /* !TclNORM_LONG_PATH */ |
| 2758 |
Tcl_DStringFree(&ds); |
| 2759 |
lastValidPathEnd = currentPathEndPosition; |
| 2760 |
if (cur == 0) { |
| 2761 |
break; |
| 2762 |
} |
| 2763 |
|
| 2764 |
/* |
| 2765 |
* If we get here, we've got past one directory delimiter, so we |
| 2766 |
* know it is no longer a drive. |
| 2767 |
*/ |
| 2768 |
|
| 2769 |
isDrive = 0; |
| 2770 |
} |
| 2771 |
currentPathEndPosition++; |
| 2772 |
|
| 2773 |
#ifdef TclNORM_LONG_PATH |
| 2774 |
/* |
| 2775 |
* Convert the entire known path to long form. |
| 2776 |
*/ |
| 2777 |
|
| 2778 |
if (1) { |
| 2779 |
WCHAR wpath[MAX_PATH]; |
| 2780 |
const WCHAR *nativePath = |
| 2781 |
Tcl_WinUtfToTChar(path, lastValidPathEnd - path, &ds); |
| 2782 |
DWORD wpathlen = GetLongPathNameProc(nativePath, |
| 2783 |
(WCHAR *) wpath, MAX_PATH); |
| 2784 |
|
| 2785 |
/* |
| 2786 |
* We have to make the drive letter uppercase. |
| 2787 |
*/ |
| 2788 |
|
| 2789 |
if (wpath[0] >= 'a') { |
| 2790 |
wpath[0] -= ('a' - 'A'); |
| 2791 |
} |
| 2792 |
Tcl_DStringAppend(&dsNorm, (const char *) wpath, |
| 2793 |
wpathlen * sizeof(WCHAR)); |
| 2794 |
Tcl_DStringFree(&ds); |
| 2795 |
} |
| 2796 |
#endif /* TclNORM_LONG_PATH */ |
| 2797 |
} |
| 2798 |
|
| 2799 |
/* |
| 2800 |
* Common code path for all Windows platforms. |
| 2801 |
*/ |
| 2802 |
|
| 2803 |
nextCheckpoint = currentPathEndPosition - path; |
| 2804 |
if (lastValidPathEnd != NULL) { |
| 2805 |
/* |
| 2806 |
* Concatenate the normalized string in dsNorm with the tail of the |
| 2807 |
* path which we didn't recognise. The string in dsNorm is in the |
| 2808 |
* native encoding, so we have to convert it to Utf. |
| 2809 |
*/ |
| 2810 |
|
| 2811 |
Tcl_WinTCharToUtf((TCHAR *) Tcl_DStringValue(&dsNorm), |
| 2812 |
Tcl_DStringLength(&dsNorm), &ds); |
| 2813 |
nextCheckpoint = Tcl_DStringLength(&ds); |
| 2814 |
if (*lastValidPathEnd != 0) { |
| 2815 |
/* |
| 2816 |
* Not the end of the string. |
| 2817 |
*/ |
| 2818 |
|
| 2819 |
int len; |
| 2820 |
Tcl_Obj *tmpPathPtr; |
| 2821 |
|
| 2822 |
tmpPathPtr = Tcl_NewStringObj(Tcl_DStringValue(&ds), |
| 2823 |
nextCheckpoint); |
| 2824 |
Tcl_AppendToObj(tmpPathPtr, lastValidPathEnd, -1); |
| 2825 |
path = Tcl_GetStringFromObj(tmpPathPtr, &len); |
| 2826 |
Tcl_SetStringObj(pathPtr, path, len); |
| 2827 |
Tcl_DecrRefCount(tmpPathPtr); |
| 2828 |
} else { |
| 2829 |
/* |
| 2830 |
* End of string was reached above. |
| 2831 |
*/ |
| 2832 |
|
| 2833 |
Tcl_SetStringObj(pathPtr, Tcl_DStringValue(&ds), nextCheckpoint); |
| 2834 |
} |
| 2835 |
Tcl_DStringFree(&ds); |
| 2836 |
} |
| 2837 |
Tcl_DStringFree(&dsNorm); |
| 2838 |
|
| 2839 |
/* |
| 2840 |
* This must be done after we are totally finished with 'path' as we are |
| 2841 |
* sharing the same underlying string. |
| 2842 |
*/ |
| 2843 |
|
| 2844 |
if (temp != NULL) { |
| 2845 |
Tcl_DecrRefCount(temp); |
| 2846 |
} |
| 2847 |
|
| 2848 |
return nextCheckpoint; |
| 2849 |
} |
| 2850 |
|
| 2851 |
/* |
| 2852 |
*--------------------------------------------------------------------------- |
| 2853 |
* |
| 2854 |
* TclWinVolumeRelativeNormalize -- |
| 2855 |
* |
| 2856 |
* Only Windows has volume-relative paths. These paths are rather rare, |
| 2857 |
* but it is nice if Tcl can handle them. It is much better if we can |
| 2858 |
* handle them here, rather than in the native fs code, because we really |
| 2859 |
* need to have a real absolute path just below. |
| 2860 |
* |
| 2861 |
* We do not let this block compile on non-Windows platforms because the |
| 2862 |
* test suite's manual forcing of tclPlatform can otherwise cause this |
| 2863 |
* code path to be executed, causing various errors because |
| 2864 |
* volume-relative paths really do not exist. |
| 2865 |
* |
| 2866 |
* Results: |
| 2867 |
* A valid normalized path. |
| 2868 |
* |
| 2869 |
* Side effects: |
| 2870 |
* None. |
| 2871 |
* |
| 2872 |
*--------------------------------------------------------------------------- |
| 2873 |
*/ |
| 2874 |
|
| 2875 |
Tcl_Obj * |
| 2876 |
TclWinVolumeRelativeNormalize( |
| 2877 |
Tcl_Interp *interp, |
| 2878 |
const char *path, |
| 2879 |
Tcl_Obj **useThisCwdPtr) |
| 2880 |
{ |
| 2881 |
Tcl_Obj *absolutePath, *useThisCwd; |
| 2882 |
|
| 2883 |
useThisCwd = Tcl_FSGetCwd(interp); |
| 2884 |
if (useThisCwd == NULL) { |
| 2885 |
return NULL; |
| 2886 |
} |
| 2887 |
|
| 2888 |
if (path[0] == '/') { |
| 2889 |
/* |
| 2890 |
* Path of form /foo/bar which is a path in the root directory of the |
| 2891 |
* current volume. |
| 2892 |
*/ |
| 2893 |
|
| 2894 |
const char *drive = Tcl_GetString(useThisCwd); |
| 2895 |
|
| 2896 |
absolutePath = Tcl_NewStringObj(drive,2); |
| 2897 |
Tcl_AppendToObj(absolutePath, path, -1); |
| 2898 |
Tcl_IncrRefCount(absolutePath); |
| 2899 |
|
| 2900 |
/* |
| 2901 |
* We have a refCount on the cwd. |
| 2902 |
*/ |
| 2903 |
} else { |
| 2904 |
/* |
| 2905 |
* Path of form C:foo/bar, but this only makes sense if the cwd is |
| 2906 |
* also on drive C. |
| 2907 |
*/ |
| 2908 |
|
| 2909 |
int cwdLen; |
| 2910 |
const char *drive = |
| 2911 |
Tcl_GetStringFromObj(useThisCwd, &cwdLen); |
| 2912 |
char drive_cur = path[0]; |
| 2913 |
|
| 2914 |
if (drive_cur >= 'a') { |
| 2915 |
drive_cur -= ('a' - 'A'); |
| 2916 |
} |
| 2917 |
if (drive[0] == drive_cur) { |
| 2918 |
absolutePath = Tcl_DuplicateObj(useThisCwd); |
| 2919 |
|
| 2920 |
/* |
| 2921 |
* We have a refCount on the cwd, which we will release later. |
| 2922 |
*/ |
| 2923 |
|
| 2924 |
if (drive[cwdLen-1] != '/' && (path[2] != '\0')) { |
| 2925 |
/* |
| 2926 |
* Only add a trailing '/' if needed, which is if there isn't |
| 2927 |
* one already, and if we are going to be adding some more |
| 2928 |
* characters. |
| 2929 |
*/ |
| 2930 |
|
| 2931 |
Tcl_AppendToObj(absolutePath, "/", 1); |
| 2932 |
} |
| 2933 |
} else { |
| 2934 |
Tcl_DecrRefCount(useThisCwd); |
| 2935 |
useThisCwd = NULL; |
| 2936 |
|
| 2937 |
/* |
| 2938 |
* The path is not in the current drive, but is volume-relative. |
| 2939 |
* The way Tcl 8.3 handles this is that it treats such a path as |
| 2940 |
* relative to the root of the drive. We therefore behave the same |
| 2941 |
* here. This behaviour is, however, different to that of the |
| 2942 |
* windows command-line. If we want to fix this at some point in |
| 2943 |
* the future (at the expense of a behaviour change to Tcl), we |
| 2944 |
* could use the '_dgetdcwd' Win32 API to get the drive's cwd. |
| 2945 |
*/ |
| 2946 |
|
| 2947 |
absolutePath = Tcl_NewStringObj(path, 2); |
| 2948 |
Tcl_AppendToObj(absolutePath, "/", 1); |
| 2949 |
} |
| 2950 |
Tcl_IncrRefCount(absolutePath); |
| 2951 |
Tcl_AppendToObj(absolutePath, path+2, -1); |
| 2952 |
} |
| 2953 |
*useThisCwdPtr = useThisCwd; |
| 2954 |
return absolutePath; |
| 2955 |
} |
| 2956 |
|
| 2957 |
/* |
| 2958 |
*--------------------------------------------------------------------------- |
| 2959 |
* |
| 2960 |
* TclpNativeToNormalized -- |
| 2961 |
* |
| 2962 |
* Convert native format to a normalized path object, with refCount of |
| 2963 |
* zero. |
| 2964 |
* |
| 2965 |
* Currently assumes all native paths are actually normalized already, so |
| 2966 |
* if the path given is not normalized this will actually just convert to |
| 2967 |
* a valid string path, but not necessarily a normalized one. |
| 2968 |
* |
| 2969 |
* Results: |
| 2970 |
* A valid normalized path. |
| 2971 |
* |
| 2972 |
* Side effects: |
| 2973 |
* None. |
| 2974 |
* |
| 2975 |
*--------------------------------------------------------------------------- |
| 2976 |
*/ |
| 2977 |
|
| 2978 |
Tcl_Obj * |
| 2979 |
TclpNativeToNormalized( |
| 2980 |
ClientData clientData) |
| 2981 |
{ |
| 2982 |
Tcl_DString ds; |
| 2983 |
Tcl_Obj *objPtr; |
| 2984 |
int len; |
| 2985 |
char *copy, *p; |
| 2986 |
|
| 2987 |
Tcl_WinTCharToUtf((TCHAR *) clientData, -1, &ds); |
| 2988 |
copy = Tcl_DStringValue(&ds); |
| 2989 |
len = Tcl_DStringLength(&ds); |
| 2990 |
|
| 2991 |
/* |
| 2992 |
* Certain native path representations on Windows have this special prefix |
| 2993 |
* to indicate that they are to be treated specially. For example |
| 2994 |
* extremely long paths, or symlinks. |
| 2995 |
*/ |
| 2996 |
|
| 2997 |
if (*copy == '\\') { |
| 2998 |
if (0 == strncmp(copy,"\\??\\",4)) { |
| 2999 |
copy += 4; |
| 3000 |
len -= 4; |
| 3001 |
} else if (0 == strncmp(copy,"\\\\?\\",4)) { |
| 3002 |
copy += 4; |
| 3003 |
len -= 4; |
| 3004 |
} |
| 3005 |
} |
| 3006 |
|
| 3007 |
/* |
| 3008 |
* Ensure we are using forward slashes only. |
| 3009 |
*/ |
| 3010 |
|
| 3011 |
for (p = copy; *p != '\0'; p++) { |
| 3012 |
if (*p == '\\') { |
| 3013 |
*p = '/'; |
| 3014 |
} |
| 3015 |
} |
| 3016 |
|
| 3017 |
objPtr = Tcl_NewStringObj(copy,len); |
| 3018 |
Tcl_DStringFree(&ds); |
| 3019 |
|
| 3020 |
return objPtr; |
| 3021 |
} |
| 3022 |
|
| 3023 |
/* |
| 3024 |
*--------------------------------------------------------------------------- |
| 3025 |
* |
| 3026 |
* TclNativeCreateNativeRep -- |
| 3027 |
* |
| 3028 |
* Create a native representation for the given path. |
| 3029 |
* |
| 3030 |
* Results: |
| 3031 |
* The nativePath representation. |
| 3032 |
* |
| 3033 |
* Side effects: |
| 3034 |
* Memory will be allocated. The path might be normalized. |
| 3035 |
* |
| 3036 |
*--------------------------------------------------------------------------- |
| 3037 |
*/ |
| 3038 |
|
| 3039 |
ClientData |
| 3040 |
TclNativeCreateNativeRep( |
| 3041 |
Tcl_Obj *pathPtr) |
| 3042 |
{ |
| 3043 |
WCHAR *nativePathPtr = NULL; |
| 3044 |
const char *str; |
| 3045 |
Tcl_Obj *validPathPtr; |
| 3046 |
int len; |
| 3047 |
WCHAR *wp; |
| 3048 |
Tcl_DString ds; |
| 3049 |
Tcl_Encoding utf8; |
| 3050 |
|
| 3051 |
if (TclFSCwdIsNative() || Tcl_FSGetPathType(pathPtr) == TCL_PATH_ABSOLUTE) { |
| 3052 |
/* |
| 3053 |
* The cwd is native (or path is absolute), use the translated path |
| 3054 |
* without worrying about normalization (this will also usually be |
| 3055 |
* shorter so the utf-to-external conversion will be somewhat faster). |
| 3056 |
*/ |
| 3057 |
|
| 3058 |
validPathPtr = Tcl_FSGetTranslatedPath(NULL, pathPtr); |
| 3059 |
if (validPathPtr == NULL) { |
| 3060 |
return NULL; |
| 3061 |
} |
| 3062 |
|
| 3063 |
/* |
| 3064 |
* refCount of validPathPtr was already incremented in |
| 3065 |
* Tcl_FSGetTranslatedPath |
| 3066 |
*/ |
| 3067 |
} else { |
| 3068 |
/* |
| 3069 |
* Make sure the normalized path is set. |
| 3070 |
*/ |
| 3071 |
|
| 3072 |
validPathPtr = Tcl_FSGetNormalizedPath(NULL, pathPtr); |
| 3073 |
if (validPathPtr == NULL) { |
| 3074 |
return NULL; |
| 3075 |
} |
| 3076 |
|
| 3077 |
/* |
| 3078 |
* validPathPtr returned from Tcl_FSGetNormalizedPath is owned by Tcl, |
| 3079 |
* so incr refCount here |
| 3080 |
*/ |
| 3081 |
|
| 3082 |
Tcl_IncrRefCount(validPathPtr); |
| 3083 |
} |
| 3084 |
|
| 3085 |
utf8 = Tcl_GetEncoding(NULL, "utf-8"); |
| 3086 |
str = Tcl_GetStringFromObj(validPathPtr, &len); |
| 3087 |
str = Tcl_UtfToExternalDString(utf8, str, len, &ds); |
| 3088 |
len = Tcl_DStringLength(&ds); |
| 3089 |
Tcl_FreeEncoding(utf8); |
| 3090 |
|
| 3091 |
if (strlen(str) != (size_t)len) { |
| 3092 |
/* |
| 3093 |
* String contains NUL-bytes. This is invalid. |
| 3094 |
*/ |
| 3095 |
|
| 3096 |
goto done; |
| 3097 |
} |
| 3098 |
|
| 3099 |
/* |
| 3100 |
* For a reserved device, strip a possible postfix ':' |
| 3101 |
*/ |
| 3102 |
|
| 3103 |
len = WinIsReserved(str); |
| 3104 |
if (len == 0) { |
| 3105 |
/* |
| 3106 |
* Let MultiByteToWideChar check for other invalid sequences, like |
| 3107 |
* 0xC0 0x80 (== overlong NUL). See bug [3118489]: NUL in filenames |
| 3108 |
*/ |
| 3109 |
|
| 3110 |
len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str, -1, 0, 0); |
| 3111 |
if (len==0) { |
| 3112 |
goto done; |
| 3113 |
} |
| 3114 |
} |
| 3115 |
|
| 3116 |
/* |
| 3117 |
* Overallocate 6 chars, making some room for extended paths |
| 3118 |
*/ |
| 3119 |
|
| 3120 |
wp = nativePathPtr = (WCHAR *)ckalloc((len + 6) * sizeof(WCHAR)); |
| 3121 |
if (nativePathPtr==0) { |
| 3122 |
goto done; |
| 3123 |
} |
| 3124 |
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str, -1, nativePathPtr, |
| 3125 |
len + 2); |
| 3126 |
nativePathPtr[len] = 0; |
| 3127 |
|
| 3128 |
/* |
| 3129 |
* If path starts with "//?/" or "\\?\" (extended path), translate any |
| 3130 |
* slashes to backslashes but leave the '?' intact |
| 3131 |
*/ |
| 3132 |
|
| 3133 |
if ((str[0] == '\\' || str[0] == '/') && (str[1] == '\\' || str[1] == '/') |
| 3134 |
&& str[2] == '?' && (str[3] == '\\' || str[3] == '/')) { |
| 3135 |
wp[0] = wp[1] = wp[3] = '\\'; |
| 3136 |
str += 4; |
| 3137 |
wp += 4; |
| 3138 |
} |
| 3139 |
|
| 3140 |
/* |
| 3141 |
* If there is no "\\?\" prefix but there is a drive or UNC path prefix |
| 3142 |
* and the path is larger than MAX_PATH chars, no Win32 API function can |
| 3143 |
* handle that unless it is prefixed with the extended path prefix. See: |
| 3144 |
* <https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maxpath> |
| 3145 |
*/ |
| 3146 |
|
| 3147 |
if (((str[0] >= 'A' && str[0] <= 'Z') || (str[0] >= 'a' && str[0] <= 'z')) |
| 3148 |
&& str[1] == ':') { |
| 3149 |
if (wp == nativePathPtr && len > MAX_PATH |
| 3150 |
&& (str[2] == '\\' || str[2] == '/')) { |
| 3151 |
memmove(wp + 4, wp, len * sizeof(WCHAR)); |
| 3152 |
memcpy(wp, L"\\\\?\\", 4 * sizeof(WCHAR)); |
| 3153 |
wp += 4; |
| 3154 |
} |
| 3155 |
|
| 3156 |
/* |
| 3157 |
* If (remainder of) path starts with "<drive>:", leave the ':' |
| 3158 |
* intact. |
| 3159 |
*/ |
| 3160 |
|
| 3161 |
wp += 2; |
| 3162 |
} else if (wp == nativePathPtr && len > MAX_PATH |
| 3163 |
&& (str[0] == '\\' || str[0] == '/') |
| 3164 |
&& (str[1] == '\\' || str[1] == '/') && str[2] != '?') { |
| 3165 |
memmove(wp + 6, wp, len * sizeof(WCHAR)); |
| 3166 |
memcpy(wp, L"\\\\?\\UNC", 7 * sizeof(WCHAR)); |
| 3167 |
wp += 7; |
| 3168 |
} |
| 3169 |
|
| 3170 |
/* |
| 3171 |
* In the remainder of the path, translate invalid characters to |
| 3172 |
* characters in the Unicode private use area. |
| 3173 |
*/ |
| 3174 |
|
| 3175 |
while (*wp != '\0') { |
| 3176 |
if ((*wp < ' ') || wcschr(L"\"*<>?|", *wp)) { |
| 3177 |
*wp |= 0xF000; |
| 3178 |
} else if (*wp == '/') { |
| 3179 |
*wp = '\\'; |
| 3180 |
} |
| 3181 |
++wp; |
| 3182 |
} |
| 3183 |
|
| 3184 |
done: |
| 3185 |
Tcl_DStringFree(&ds); |
| 3186 |
TclDecrRefCount(validPathPtr); |
| 3187 |
return nativePathPtr; |
| 3188 |
} |
| 3189 |
|
| 3190 |
/* |
| 3191 |
*--------------------------------------------------------------------------- |
| 3192 |
* |
| 3193 |
* TclNativeDupInternalRep -- |
| 3194 |
* |
| 3195 |
* Duplicate the native representation. |
| 3196 |
* |
| 3197 |
* Results: |
| 3198 |
* The copied native representation, or NULL if it is not possible to |
| 3199 |
* copy the representation. |
| 3200 |
* |
| 3201 |
* Side effects: |
| 3202 |
* Memory allocation for the copy. |
| 3203 |
* |
| 3204 |
*--------------------------------------------------------------------------- |
| 3205 |
*/ |
| 3206 |
|
| 3207 |
ClientData |
| 3208 |
TclNativeDupInternalRep( |
| 3209 |
ClientData clientData) |
| 3210 |
{ |
| 3211 |
char *copy; |
| 3212 |
size_t len; |
| 3213 |
|
| 3214 |
if (clientData == NULL) { |
| 3215 |
return NULL; |
| 3216 |
} |
| 3217 |
|
| 3218 |
len = sizeof(WCHAR) * (wcslen((const WCHAR *) clientData) + 1); |
| 3219 |
|
| 3220 |
copy = (char *)ckalloc(len); |
| 3221 |
memcpy(copy, clientData, len); |
| 3222 |
return copy; |
| 3223 |
} |
| 3224 |
|
| 3225 |
/* |
| 3226 |
*--------------------------------------------------------------------------- |
| 3227 |
* |
| 3228 |
* TclpUtime -- |
| 3229 |
* |
| 3230 |
* Set the modification date for a file. |
| 3231 |
* |
| 3232 |
* Results: |
| 3233 |
* 0 on success, -1 on error. |
| 3234 |
* |
| 3235 |
* Side effects: |
| 3236 |
* Sets errno to a representation of any Windows problem that's observed |
| 3237 |
* in the process. |
| 3238 |
* |
| 3239 |
*--------------------------------------------------------------------------- |
| 3240 |
*/ |
| 3241 |
|
| 3242 |
int |
| 3243 |
TclpUtime( |
| 3244 |
Tcl_Obj *pathPtr, /* File to modify */ |
| 3245 |
struct utimbuf *tval) /* New modification date structure */ |
| 3246 |
{ |
| 3247 |
int res = 0; |
| 3248 |
HANDLE fileHandle; |
| 3249 |
const WCHAR *native; |
| 3250 |
DWORD attr = 0; |
| 3251 |
DWORD flags = FILE_ATTRIBUTE_NORMAL; |
| 3252 |
FILETIME lastAccessTime, lastModTime; |
| 3253 |
|
| 3254 |
FromCTime(tval->actime, &lastAccessTime); |
| 3255 |
FromCTime(tval->modtime, &lastModTime); |
| 3256 |
|
| 3257 |
native = (const WCHAR *)Tcl_FSGetNativePath(pathPtr); |
| 3258 |
|
| 3259 |
attr = GetFileAttributesW(native); |
| 3260 |
|
| 3261 |
if (attr != INVALID_FILE_ATTRIBUTES && attr & FILE_ATTRIBUTE_DIRECTORY) { |
| 3262 |
flags = FILE_FLAG_BACKUP_SEMANTICS; |
| 3263 |
} |
| 3264 |
|
| 3265 |
/* |
| 3266 |
* We use the native APIs (not 'utime') because there are some daylight |
| 3267 |
* savings complications that utime gets wrong. |
| 3268 |
*/ |
| 3269 |
|
| 3270 |
fileHandle = CreateFileW(native, FILE_WRITE_ATTRIBUTES, 0, NULL, |
| 3271 |
OPEN_EXISTING, flags, NULL); |
| 3272 |
|
| 3273 |
if (fileHandle == INVALID_HANDLE_VALUE || |
| 3274 |
!SetFileTime(fileHandle, NULL, &lastAccessTime, &lastModTime)) { |
| 3275 |
TclWinConvertError(GetLastError()); |
| 3276 |
res = -1; |
| 3277 |
} |
| 3278 |
if (fileHandle != INVALID_HANDLE_VALUE) { |
| 3279 |
CloseHandle(fileHandle); |
| 3280 |
} |
| 3281 |
return res; |
| 3282 |
} |
| 3283 |
|
| 3284 |
/* |
| 3285 |
*--------------------------------------------------------------------------- |
| 3286 |
* |
| 3287 |
* TclWinFileOwned -- |
| 3288 |
* |
| 3289 |
* Returns 1 if the specified file exists and is owned by the current |
| 3290 |
* user and 0 otherwise. Like the Unix case, the check is made using |
| 3291 |
* the real process SID, not the effective (impersonation) one. |
| 3292 |
* |
| 3293 |
*--------------------------------------------------------------------------- |
| 3294 |
*/ |
| 3295 |
|
| 3296 |
int |
| 3297 |
TclWinFileOwned( |
| 3298 |
Tcl_Obj *pathPtr) /* File whose ownership is to be checked */ |
| 3299 |
{ |
| 3300 |
const WCHAR *native; |
| 3301 |
PSID ownerSid = NULL; |
| 3302 |
PSECURITY_DESCRIPTOR secd = NULL; |
| 3303 |
HANDLE token; |
| 3304 |
LPBYTE buf = NULL; |
| 3305 |
DWORD bufsz; |
| 3306 |
int owned = 0; |
| 3307 |
|
| 3308 |
native = (const WCHAR *)Tcl_FSGetNativePath(pathPtr); |
| 3309 |
|
| 3310 |
if (GetNamedSecurityInfoW((LPWSTR) native, SE_FILE_OBJECT, |
| 3311 |
OWNER_SECURITY_INFORMATION, &ownerSid, NULL, NULL, NULL, |
| 3312 |
&secd) != ERROR_SUCCESS) { |
| 3313 |
/* |
| 3314 |
* Either not a file, or we do not have access to it in which case we |
| 3315 |
* are in all likelihood not the owner. |
| 3316 |
*/ |
| 3317 |
|
| 3318 |
return 0; |
| 3319 |
} |
| 3320 |
|
| 3321 |
/* |
| 3322 |
* Getting the current process SID is a multi-step process. We make the |
| 3323 |
* assumption that if a call fails, this process is so underprivileged it |
| 3324 |
* could not possibly own anything. Normally a process can *always* look |
| 3325 |
* up its own token. |
| 3326 |
*/ |
| 3327 |
|
| 3328 |
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) { |
| 3329 |
/* |
| 3330 |
* Find out how big the buffer needs to be. |
| 3331 |
*/ |
| 3332 |
|
| 3333 |
bufsz = 0; |
| 3334 |
GetTokenInformation(token, TokenUser, NULL, 0, &bufsz); |
| 3335 |
if (bufsz) { |
| 3336 |
buf = (LPBYTE)ckalloc(bufsz); |
| 3337 |
if (GetTokenInformation(token, TokenUser, buf, bufsz, &bufsz)) { |
| 3338 |
owned = EqualSid(ownerSid, ((PTOKEN_USER) buf)->User.Sid); |
| 3339 |
} |
| 3340 |
} |
| 3341 |
CloseHandle(token); |
| 3342 |
} |
| 3343 |
|
| 3344 |
/* |
| 3345 |
* Free allocations and be done. |
| 3346 |
*/ |
| 3347 |
|
| 3348 |
if (secd) { |
| 3349 |
LocalFree(secd); /* Also frees ownerSid */ |
| 3350 |
} |
| 3351 |
if (buf) { |
| 3352 |
ckfree(buf); |
| 3353 |
} |
| 3354 |
|
| 3355 |
return (owned != 0); /* Convert non-0 to 1 */ |
| 3356 |
} |
| 3357 |
|
| 3358 |
/* |
| 3359 |
* Local Variables: |
| 3360 |
* mode: c |
| 3361 |
* c-basic-offset: 4 |
| 3362 |
* fill-column: 78 |
| 3363 |
* End: |
| 3364 |
*/ |