| 1 |
/* |
| 2 |
* tclWinFCmd.c |
| 3 |
* |
| 4 |
* This file implements the Windows specific portion of file manipulation |
| 5 |
* subcommands of the "file" command. |
| 6 |
* |
| 7 |
* Copyright (c) 1996-1998 Sun Microsystems, Inc. |
| 8 |
* |
| 9 |
* See the file "license.terms" for information on usage and redistribution of |
| 10 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "tclWinInt.h" |
| 14 |
|
| 15 |
/* |
| 16 |
* The following constants specify the type of callback when |
| 17 |
* TraverseWinTree() calls the traverseProc() |
| 18 |
*/ |
| 19 |
|
| 20 |
#define DOTREE_PRED 1 /* pre-order directory */ |
| 21 |
#define DOTREE_POSTD 2 /* post-order directory */ |
| 22 |
#define DOTREE_F 3 /* regular file */ |
| 23 |
#define DOTREE_LINK 4 /* symbolic link */ |
| 24 |
|
| 25 |
/* |
| 26 |
* Callbacks for file attributes code. |
| 27 |
*/ |
| 28 |
|
| 29 |
static int GetWinFileAttributes(Tcl_Interp *interp, int objIndex, |
| 30 |
Tcl_Obj *fileName, Tcl_Obj **attributePtrPtr); |
| 31 |
static int GetWinFileLongName(Tcl_Interp *interp, int objIndex, |
| 32 |
Tcl_Obj *fileName, Tcl_Obj **attributePtrPtr); |
| 33 |
static int GetWinFileShortName(Tcl_Interp *interp, int objIndex, |
| 34 |
Tcl_Obj *fileName, Tcl_Obj **attributePtrPtr); |
| 35 |
static int SetWinFileAttributes(Tcl_Interp *interp, int objIndex, |
| 36 |
Tcl_Obj *fileName, Tcl_Obj *attributePtr); |
| 37 |
static int CannotSetAttribute(Tcl_Interp *interp, int objIndex, |
| 38 |
Tcl_Obj *fileName, Tcl_Obj *attributePtr); |
| 39 |
|
| 40 |
/* |
| 41 |
* Constants and variables necessary for file attributes subcommand. |
| 42 |
*/ |
| 43 |
|
| 44 |
enum { |
| 45 |
WIN_ARCHIVE_ATTRIBUTE, |
| 46 |
WIN_HIDDEN_ATTRIBUTE, |
| 47 |
WIN_LONGNAME_ATTRIBUTE, |
| 48 |
WIN_READONLY_ATTRIBUTE, |
| 49 |
WIN_SHORTNAME_ATTRIBUTE, |
| 50 |
WIN_SYSTEM_ATTRIBUTE |
| 51 |
}; |
| 52 |
|
| 53 |
static const int attributeArray[] = {FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_HIDDEN, |
| 54 |
0, FILE_ATTRIBUTE_READONLY, 0, FILE_ATTRIBUTE_SYSTEM}; |
| 55 |
|
| 56 |
|
| 57 |
const char *const tclpFileAttrStrings[] = { |
| 58 |
"-archive", "-hidden", "-longname", "-readonly", |
| 59 |
"-shortname", "-system", NULL |
| 60 |
}; |
| 61 |
|
| 62 |
const TclFileAttrProcs tclpFileAttrProcs[] = { |
| 63 |
{GetWinFileAttributes, SetWinFileAttributes}, |
| 64 |
{GetWinFileAttributes, SetWinFileAttributes}, |
| 65 |
{GetWinFileLongName, CannotSetAttribute}, |
| 66 |
{GetWinFileAttributes, SetWinFileAttributes}, |
| 67 |
{GetWinFileShortName, CannotSetAttribute}, |
| 68 |
{GetWinFileAttributes, SetWinFileAttributes}}; |
| 69 |
|
| 70 |
/* |
| 71 |
* Prototype for the TraverseWinTree callback function. |
| 72 |
*/ |
| 73 |
|
| 74 |
typedef int (TraversalProc)(const WCHAR *srcPtr, const WCHAR *dstPtr, |
| 75 |
int type, Tcl_DString *errorPtr); |
| 76 |
|
| 77 |
/* |
| 78 |
* Declarations for local functions defined in this file: |
| 79 |
*/ |
| 80 |
|
| 81 |
static void StatError(Tcl_Interp *interp, Tcl_Obj *fileName); |
| 82 |
static int ConvertFileNameFormat(Tcl_Interp *interp, |
| 83 |
int objIndex, Tcl_Obj *fileName, int longShort, |
| 84 |
Tcl_Obj **attributePtrPtr); |
| 85 |
static int DoCopyFile(const WCHAR *srcPtr, const WCHAR *dstPtr); |
| 86 |
static int DoCreateDirectory(const WCHAR *pathPtr); |
| 87 |
static int DoRemoveJustDirectory(const WCHAR *nativeSrc, |
| 88 |
int ignoreError, Tcl_DString *errorPtr); |
| 89 |
static int DoRemoveDirectory(Tcl_DString *pathPtr, int recursive, |
| 90 |
Tcl_DString *errorPtr); |
| 91 |
static int DoRenameFile(const WCHAR *nativeSrc, |
| 92 |
const WCHAR *dstPtr); |
| 93 |
static int TraversalCopy(const WCHAR *srcPtr, const WCHAR *dstPtr, |
| 94 |
int type, Tcl_DString *errorPtr); |
| 95 |
static int TraversalDelete(const WCHAR *srcPtr, |
| 96 |
const WCHAR *dstPtr, int type, |
| 97 |
Tcl_DString *errorPtr); |
| 98 |
static int TraverseWinTree(TraversalProc *traverseProc, |
| 99 |
Tcl_DString *sourcePtr, Tcl_DString *dstPtr, |
| 100 |
Tcl_DString *errorPtr); |
| 101 |
|
| 102 |
/* |
| 103 |
*--------------------------------------------------------------------------- |
| 104 |
* |
| 105 |
* TclpObjRenameFile, DoRenameFile -- |
| 106 |
* |
| 107 |
* Changes the name of an existing file or directory, from src to dst. |
| 108 |
* If src and dst refer to the same file or directory, does nothing and |
| 109 |
* returns success. Otherwise if dst already exists, it will be deleted |
| 110 |
* and replaced by src subject to the following conditions: |
| 111 |
* If src is a directory, dst may be an empty directory. |
| 112 |
* If src is a file, dst may be a file. |
| 113 |
* In any other situation where dst already exists, the rename will fail. |
| 114 |
* |
| 115 |
* Results: |
| 116 |
* If the file or directory was successfully renamed, returns TCL_OK. |
| 117 |
* Otherwise the return value is TCL_ERROR and errno is set to indicate |
| 118 |
* the error. Some possible values for errno are: |
| 119 |
* |
| 120 |
* ENAMETOOLONG: src or dst names are too long. |
| 121 |
* EACCES: src or dst parent directory can't be read and/or written. |
| 122 |
* EEXIST: dst is a non-empty directory. |
| 123 |
* EINVAL: src is a root directory or dst is a subdirectory of src. |
| 124 |
* EISDIR: dst is a directory, but src is not. |
| 125 |
* ENOENT: src doesn't exist. src or dst is "". |
| 126 |
* ENOTDIR: src is a directory, but dst is not. |
| 127 |
* EXDEV: src and dst are on different filesystems. |
| 128 |
* |
| 129 |
* EACCES: exists an open file already referring to src or dst. |
| 130 |
* EACCES: src or dst specify the current working directory (NT). |
| 131 |
* EACCES: src specifies a char device (nul:, com1:, etc.) |
| 132 |
* EEXIST: dst specifies a char device (nul:, com1:, etc.) (NT) |
| 133 |
* EACCES: dst specifies a char device (nul:, com1:, etc.) (95) |
| 134 |
* |
| 135 |
* Side effects: |
| 136 |
* The implementation supports cross-filesystem renames of files, but the |
| 137 |
* caller should be prepared to emulate cross-filesystem renames of |
| 138 |
* directories if errno is EXDEV. |
| 139 |
* |
| 140 |
*--------------------------------------------------------------------------- |
| 141 |
*/ |
| 142 |
|
| 143 |
int |
| 144 |
TclpObjRenameFile( |
| 145 |
Tcl_Obj *srcPathPtr, |
| 146 |
Tcl_Obj *destPathPtr) |
| 147 |
{ |
| 148 |
return DoRenameFile((WCHAR *)Tcl_FSGetNativePath(srcPathPtr), |
| 149 |
(WCHAR *)Tcl_FSGetNativePath(destPathPtr)); |
| 150 |
} |
| 151 |
|
| 152 |
static int |
| 153 |
DoRenameFile( |
| 154 |
const WCHAR *nativeSrc, /* Pathname of file or dir to be renamed |
| 155 |
* (native). */ |
| 156 |
const WCHAR *nativeDst) /* New pathname for file or directory |
| 157 |
* (native). */ |
| 158 |
{ |
| 159 |
#if defined(HAVE_NO_SEH) && !defined(_WIN64) |
| 160 |
TCLEXCEPTION_REGISTRATION registration; |
| 161 |
#endif |
| 162 |
DWORD srcAttr, dstAttr; |
| 163 |
int retval = -1; |
| 164 |
|
| 165 |
/* |
| 166 |
* The MoveFileW API acts differently under Win95/98 and NT WRT NULL and |
| 167 |
* "". Avoid passing these values. |
| 168 |
*/ |
| 169 |
|
| 170 |
if (nativeSrc == NULL || nativeSrc[0] == '\0' || |
| 171 |
nativeDst == NULL || nativeDst[0] == '\0') { |
| 172 |
Tcl_SetErrno(ENOENT); |
| 173 |
return TCL_ERROR; |
| 174 |
} |
| 175 |
|
| 176 |
/* |
| 177 |
* The MoveFileW API would throw an exception under NT if one of the |
| 178 |
* arguments is a char block device. |
| 179 |
*/ |
| 180 |
|
| 181 |
#if defined(HAVE_NO_SEH) && !defined(_WIN64) |
| 182 |
/* |
| 183 |
* Don't have SEH available, do things the hard way. Note that this needs |
| 184 |
* to be one block of asm, to avoid stack imbalance; also, it is illegal |
| 185 |
* for one asm block to contain a jump to another. |
| 186 |
*/ |
| 187 |
|
| 188 |
__asm__ __volatile__ ( |
| 189 |
/* |
| 190 |
* Pick up params before messing with the stack. |
| 191 |
*/ |
| 192 |
|
| 193 |
"movl %[nativeDst], %%ebx" "\n\t" |
| 194 |
"movl %[nativeSrc], %%ecx" "\n\t" |
| 195 |
|
| 196 |
/* |
| 197 |
* Construct an TCLEXCEPTION_REGISTRATION to protect the call to |
| 198 |
* MoveFileW. |
| 199 |
*/ |
| 200 |
|
| 201 |
"leal %[registration], %%edx" "\n\t" |
| 202 |
"movl %%fs:0, %%eax" "\n\t" |
| 203 |
"movl %%eax, 0x0(%%edx)" "\n\t" /* link */ |
| 204 |
"leal 1f, %%eax" "\n\t" |
| 205 |
"movl %%eax, 0x4(%%edx)" "\n\t" /* handler */ |
| 206 |
"movl %%ebp, 0x8(%%edx)" "\n\t" /* ebp */ |
| 207 |
"movl %%esp, 0xC(%%edx)" "\n\t" /* esp */ |
| 208 |
"movl $0, 0x10(%%edx)" "\n\t" /* status */ |
| 209 |
|
| 210 |
/* |
| 211 |
* Link the TCLEXCEPTION_REGISTRATION on the chain. |
| 212 |
*/ |
| 213 |
|
| 214 |
"movl %%edx, %%fs:0" "\n\t" |
| 215 |
|
| 216 |
/* |
| 217 |
* Call MoveFileW(nativeSrc, nativeDst) |
| 218 |
*/ |
| 219 |
|
| 220 |
"pushl %%ebx" "\n\t" |
| 221 |
"pushl %%ecx" "\n\t" |
| 222 |
"movl %[moveFileW], %%eax" "\n\t" |
| 223 |
"call *%%eax" "\n\t" |
| 224 |
|
| 225 |
/* |
| 226 |
* Come here on normal exit. Recover the TCLEXCEPTION_REGISTRATION and |
| 227 |
* put the status return from MoveFileW into it. |
| 228 |
*/ |
| 229 |
|
| 230 |
"movl %%fs:0, %%edx" "\n\t" |
| 231 |
"movl %%eax, 0x10(%%edx)" "\n\t" |
| 232 |
"jmp 2f" "\n" |
| 233 |
|
| 234 |
/* |
| 235 |
* Come here on an exception. Recover the TCLEXCEPTION_REGISTRATION |
| 236 |
*/ |
| 237 |
|
| 238 |
"1:" "\t" |
| 239 |
"movl %%fs:0, %%edx" "\n\t" |
| 240 |
"movl 0x8(%%edx), %%edx" "\n\t" |
| 241 |
|
| 242 |
/* |
| 243 |
* Come here however we exited. Restore context from the |
| 244 |
* TCLEXCEPTION_REGISTRATION in case the stack is unbalanced. |
| 245 |
*/ |
| 246 |
|
| 247 |
"2:" "\t" |
| 248 |
"movl 0xC(%%edx), %%esp" "\n\t" |
| 249 |
"movl 0x8(%%edx), %%ebp" "\n\t" |
| 250 |
"movl 0x0(%%edx), %%eax" "\n\t" |
| 251 |
"movl %%eax, %%fs:0" "\n\t" |
| 252 |
|
| 253 |
: |
| 254 |
/* No outputs */ |
| 255 |
: |
| 256 |
[registration] "m" (registration), |
| 257 |
[nativeDst] "m" (nativeDst), |
| 258 |
[nativeSrc] "m" (nativeSrc), |
| 259 |
[moveFileW] "r" (MoveFileW) |
| 260 |
: |
| 261 |
"%eax", "%ebx", "%ecx", "%edx", "memory" |
| 262 |
); |
| 263 |
if (registration.status != FALSE) { |
| 264 |
retval = TCL_OK; |
| 265 |
} |
| 266 |
#else |
| 267 |
#ifndef HAVE_NO_SEH |
| 268 |
__try { |
| 269 |
#endif |
| 270 |
if (MoveFileW(nativeSrc, nativeDst) != FALSE) { |
| 271 |
retval = TCL_OK; |
| 272 |
} |
| 273 |
#ifndef HAVE_NO_SEH |
| 274 |
} __except (EXCEPTION_EXECUTE_HANDLER) {} |
| 275 |
#endif |
| 276 |
#endif |
| 277 |
|
| 278 |
if (retval != -1) { |
| 279 |
return retval; |
| 280 |
} |
| 281 |
|
| 282 |
TclWinConvertError(GetLastError()); |
| 283 |
|
| 284 |
srcAttr = GetFileAttributesW(nativeSrc); |
| 285 |
dstAttr = GetFileAttributesW(nativeDst); |
| 286 |
if (srcAttr == 0xFFFFFFFF) { |
| 287 |
if (GetFullPathNameW(nativeSrc, 0, NULL, |
| 288 |
NULL) >= MAX_PATH) { |
| 289 |
errno = ENAMETOOLONG; |
| 290 |
return TCL_ERROR; |
| 291 |
} |
| 292 |
srcAttr = 0; |
| 293 |
} |
| 294 |
if (dstAttr == 0xFFFFFFFF) { |
| 295 |
if (GetFullPathNameW(nativeDst, 0, NULL, |
| 296 |
NULL) >= MAX_PATH) { |
| 297 |
errno = ENAMETOOLONG; |
| 298 |
return TCL_ERROR; |
| 299 |
} |
| 300 |
dstAttr = 0; |
| 301 |
} |
| 302 |
|
| 303 |
if (errno == EBADF) { |
| 304 |
errno = EACCES; |
| 305 |
return TCL_ERROR; |
| 306 |
} |
| 307 |
if (errno == EACCES) { |
| 308 |
decode: |
| 309 |
if (srcAttr & FILE_ATTRIBUTE_DIRECTORY) { |
| 310 |
WCHAR *nativeSrcRest, *nativeDstRest; |
| 311 |
const char **srcArgv, **dstArgv; |
| 312 |
int size, srcArgc, dstArgc; |
| 313 |
WCHAR nativeSrcPath[MAX_PATH]; |
| 314 |
WCHAR nativeDstPath[MAX_PATH]; |
| 315 |
Tcl_DString srcString, dstString; |
| 316 |
const char *src, *dst; |
| 317 |
|
| 318 |
size = GetFullPathNameW(nativeSrc, MAX_PATH, |
| 319 |
nativeSrcPath, &nativeSrcRest); |
| 320 |
if ((size == 0) || (size > MAX_PATH)) { |
| 321 |
return TCL_ERROR; |
| 322 |
} |
| 323 |
size = GetFullPathNameW(nativeDst, MAX_PATH, |
| 324 |
nativeDstPath, &nativeDstRest); |
| 325 |
if ((size == 0) || (size > MAX_PATH)) { |
| 326 |
return TCL_ERROR; |
| 327 |
} |
| 328 |
CharLowerW(nativeSrcPath); |
| 329 |
CharLowerW(nativeDstPath); |
| 330 |
|
| 331 |
src = Tcl_WinTCharToUtf((TCHAR *)nativeSrcPath, -1, &srcString); |
| 332 |
dst = Tcl_WinTCharToUtf((TCHAR *)nativeDstPath, -1, &dstString); |
| 333 |
|
| 334 |
/* |
| 335 |
* Check whether the destination path is actually inside the |
| 336 |
* source path. This is true if the prefix matches, and the next |
| 337 |
* character is either end-of-string or a directory separator |
| 338 |
*/ |
| 339 |
|
| 340 |
if ((strncmp(src, dst, Tcl_DStringLength(&srcString))==0) |
| 341 |
&& (dst[Tcl_DStringLength(&srcString)] == '\\' |
| 342 |
|| dst[Tcl_DStringLength(&srcString)] == '/' |
| 343 |
|| dst[Tcl_DStringLength(&srcString)] == '\0')) { |
| 344 |
/* |
| 345 |
* Trying to move a directory into itself. |
| 346 |
*/ |
| 347 |
|
| 348 |
errno = EINVAL; |
| 349 |
Tcl_DStringFree(&srcString); |
| 350 |
Tcl_DStringFree(&dstString); |
| 351 |
return TCL_ERROR; |
| 352 |
} |
| 353 |
Tcl_SplitPath(src, &srcArgc, &srcArgv); |
| 354 |
Tcl_SplitPath(dst, &dstArgc, &dstArgv); |
| 355 |
Tcl_DStringFree(&srcString); |
| 356 |
Tcl_DStringFree(&dstString); |
| 357 |
|
| 358 |
if (srcArgc == 1) { |
| 359 |
/* |
| 360 |
* They are trying to move a root directory. Whether or not it |
| 361 |
* is across filesystems, this cannot be done. |
| 362 |
*/ |
| 363 |
|
| 364 |
Tcl_SetErrno(EINVAL); |
| 365 |
} else if ((srcArgc > 0) && (dstArgc > 0) && |
| 366 |
(strcmp(srcArgv[0], dstArgv[0]) != 0)) { |
| 367 |
/* |
| 368 |
* If src is a directory and dst filesystem != src filesystem, |
| 369 |
* errno should be EXDEV. It is very important to get this |
| 370 |
* behavior, so that the caller can respond to a cross |
| 371 |
* filesystem rename by simulating it with copy and delete. |
| 372 |
* The MoveFileW system call already handles the case of moving |
| 373 |
* a file between filesystems. |
| 374 |
*/ |
| 375 |
|
| 376 |
Tcl_SetErrno(EXDEV); |
| 377 |
} |
| 378 |
|
| 379 |
ckfree(srcArgv); |
| 380 |
ckfree(dstArgv); |
| 381 |
} |
| 382 |
|
| 383 |
/* |
| 384 |
* Other types of access failure is that dst is a read-only |
| 385 |
* filesystem, that an open file referred to src or dest, or that src |
| 386 |
* or dest specified the current working directory on the current |
| 387 |
* filesystem. EACCES is returned for those cases. |
| 388 |
*/ |
| 389 |
|
| 390 |
} else if (Tcl_GetErrno() == EEXIST) { |
| 391 |
/* |
| 392 |
* Reports EEXIST any time the target already exists. If it makes |
| 393 |
* sense, remove the old file and try renaming again. |
| 394 |
*/ |
| 395 |
|
| 396 |
if (srcAttr & FILE_ATTRIBUTE_DIRECTORY) { |
| 397 |
if (dstAttr & FILE_ATTRIBUTE_DIRECTORY) { |
| 398 |
/* |
| 399 |
* Overwrite empty dst directory with src directory. The |
| 400 |
* following call will remove an empty directory. If it fails, |
| 401 |
* it's because it wasn't empty. |
| 402 |
*/ |
| 403 |
|
| 404 |
if (DoRemoveJustDirectory(nativeDst, 0, NULL) == TCL_OK) { |
| 405 |
/* |
| 406 |
* Now that that empty directory is gone, we can try |
| 407 |
* renaming again. If that fails, we'll put this empty |
| 408 |
* directory back, for completeness. |
| 409 |
*/ |
| 410 |
|
| 411 |
if (MoveFileW(nativeSrc, |
| 412 |
nativeDst) != FALSE) { |
| 413 |
return TCL_OK; |
| 414 |
} |
| 415 |
|
| 416 |
/* |
| 417 |
* Some new error has occurred. Don't know what it could |
| 418 |
* be, but report this one. |
| 419 |
*/ |
| 420 |
|
| 421 |
TclWinConvertError(GetLastError()); |
| 422 |
CreateDirectoryW(nativeDst, NULL); |
| 423 |
SetFileAttributesW(nativeDst, dstAttr); |
| 424 |
if (Tcl_GetErrno() == EACCES) { |
| 425 |
/* |
| 426 |
* Decode the EACCES to a more meaningful error. |
| 427 |
*/ |
| 428 |
|
| 429 |
goto decode; |
| 430 |
} |
| 431 |
} |
| 432 |
} else { /* (dstAttr & FILE_ATTRIBUTE_DIRECTORY) == 0 */ |
| 433 |
Tcl_SetErrno(ENOTDIR); |
| 434 |
} |
| 435 |
} else { /* (srcAttr & FILE_ATTRIBUTE_DIRECTORY) == 0 */ |
| 436 |
if (dstAttr & FILE_ATTRIBUTE_DIRECTORY) { |
| 437 |
Tcl_SetErrno(EISDIR); |
| 438 |
} else { |
| 439 |
/* |
| 440 |
* Overwrite existing file by: |
| 441 |
* |
| 442 |
* 1. Rename existing file to temp name. |
| 443 |
* 2. Rename old file to new name. |
| 444 |
* 3. If success, delete temp file. If failure, put temp file |
| 445 |
* back to old name. |
| 446 |
*/ |
| 447 |
|
| 448 |
WCHAR *nativeRest, *nativeTmp, *nativePrefix; |
| 449 |
int result, size; |
| 450 |
WCHAR tempBuf[MAX_PATH]; |
| 451 |
|
| 452 |
size = GetFullPathNameW(nativeDst, MAX_PATH, |
| 453 |
tempBuf, &nativeRest); |
| 454 |
if ((size == 0) || (size > MAX_PATH) || (nativeRest == NULL)) { |
| 455 |
return TCL_ERROR; |
| 456 |
} |
| 457 |
nativeTmp = (WCHAR *) tempBuf; |
| 458 |
nativeRest[0] = L'\0'; |
| 459 |
|
| 460 |
result = TCL_ERROR; |
| 461 |
nativePrefix = (WCHAR *) L"tclr"; |
| 462 |
if (GetTempFileNameW(nativeTmp, nativePrefix, |
| 463 |
0, tempBuf) != 0) { |
| 464 |
/* |
| 465 |
* Strictly speaking, need the following DeleteFile and |
| 466 |
* MoveFileW to be joined as an atomic operation so no |
| 467 |
* other app comes along in the meantime and creates the |
| 468 |
* same temp file. |
| 469 |
*/ |
| 470 |
|
| 471 |
nativeTmp = tempBuf; |
| 472 |
DeleteFileW(nativeTmp); |
| 473 |
if (MoveFileW(nativeDst, nativeTmp) != FALSE) { |
| 474 |
if (MoveFileW(nativeSrc, nativeDst) != FALSE) { |
| 475 |
SetFileAttributesW(nativeTmp, FILE_ATTRIBUTE_NORMAL); |
| 476 |
DeleteFileW(nativeTmp); |
| 477 |
return TCL_OK; |
| 478 |
} else { |
| 479 |
DeleteFileW(nativeDst); |
| 480 |
MoveFileW(nativeTmp, nativeDst); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
/* |
| 485 |
* Can't backup dst file or move src file. Return that |
| 486 |
* error. Could happen if an open file refers to dst. |
| 487 |
*/ |
| 488 |
|
| 489 |
TclWinConvertError(GetLastError()); |
| 490 |
if (Tcl_GetErrno() == EACCES) { |
| 491 |
/* |
| 492 |
* Decode the EACCES to a more meaningful error. |
| 493 |
*/ |
| 494 |
|
| 495 |
goto decode; |
| 496 |
} |
| 497 |
} |
| 498 |
return result; |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
return TCL_ERROR; |
| 503 |
} |
| 504 |
|
| 505 |
/* |
| 506 |
*--------------------------------------------------------------------------- |
| 507 |
* |
| 508 |
* TclpObjCopyFile, DoCopyFile -- |
| 509 |
* |
| 510 |
* Copy a single file (not a directory). If dst already exists and is not |
| 511 |
* a directory, it is removed. |
| 512 |
* |
| 513 |
* Results: |
| 514 |
* If the file was successfully copied, returns TCL_OK. Otherwise the |
| 515 |
* return value is TCL_ERROR and errno is set to indicate the error. |
| 516 |
* Some possible values for errno are: |
| 517 |
* |
| 518 |
* EACCES: src or dst parent directory can't be read and/or written. |
| 519 |
* EISDIR: src or dst is a directory. |
| 520 |
* ENOENT: src doesn't exist. src or dst is "". |
| 521 |
* |
| 522 |
* EACCES: exists an open file already referring to dst (95). |
| 523 |
* EACCES: src specifies a char device (nul:, com1:, etc.) (NT) |
| 524 |
* ENOENT: src specifies a char device (nul:, com1:, etc.) (95) |
| 525 |
* |
| 526 |
* Side effects: |
| 527 |
* It is not an error to copy to a char device. |
| 528 |
* |
| 529 |
*--------------------------------------------------------------------------- |
| 530 |
*/ |
| 531 |
|
| 532 |
int |
| 533 |
TclpObjCopyFile( |
| 534 |
Tcl_Obj *srcPathPtr, |
| 535 |
Tcl_Obj *destPathPtr) |
| 536 |
{ |
| 537 |
return DoCopyFile((WCHAR *)Tcl_FSGetNativePath(srcPathPtr), |
| 538 |
(WCHAR *)Tcl_FSGetNativePath(destPathPtr)); |
| 539 |
} |
| 540 |
|
| 541 |
static int |
| 542 |
DoCopyFile( |
| 543 |
const WCHAR *nativeSrc, /* Pathname of file to be copied (native). */ |
| 544 |
const WCHAR *nativeDst) /* Pathname of file to copy to (native). */ |
| 545 |
{ |
| 546 |
#if defined(HAVE_NO_SEH) && !defined(_WIN64) |
| 547 |
TCLEXCEPTION_REGISTRATION registration; |
| 548 |
#endif |
| 549 |
int retval = -1; |
| 550 |
|
| 551 |
/* |
| 552 |
* The CopyFile API acts differently under Win95/98 and NT WRT NULL and |
| 553 |
* "". Avoid passing these values. |
| 554 |
*/ |
| 555 |
|
| 556 |
if (nativeSrc == NULL || nativeSrc[0] == '\0' || |
| 557 |
nativeDst == NULL || nativeDst[0] == '\0') { |
| 558 |
Tcl_SetErrno(ENOENT); |
| 559 |
return TCL_ERROR; |
| 560 |
} |
| 561 |
|
| 562 |
/* |
| 563 |
* The CopyFile API would throw an exception under NT if one of the |
| 564 |
* arguments is a char block device. |
| 565 |
*/ |
| 566 |
|
| 567 |
#if defined(HAVE_NO_SEH) && !defined(_WIN64) |
| 568 |
/* |
| 569 |
* Don't have SEH available, do things the hard way. Note that this needs |
| 570 |
* to be one block of asm, to avoid stack imbalance; also, it is illegal |
| 571 |
* for one asm block to contain a jump to another. |
| 572 |
*/ |
| 573 |
|
| 574 |
__asm__ __volatile__ ( |
| 575 |
|
| 576 |
/* |
| 577 |
* Pick up parameters before messing with the stack |
| 578 |
*/ |
| 579 |
|
| 580 |
"movl %[nativeDst], %%ebx" "\n\t" |
| 581 |
"movl %[nativeSrc], %%ecx" "\n\t" |
| 582 |
|
| 583 |
/* |
| 584 |
* Construct an TCLEXCEPTION_REGISTRATION to protect the call to |
| 585 |
* CopyFile. |
| 586 |
*/ |
| 587 |
|
| 588 |
"leal %[registration], %%edx" "\n\t" |
| 589 |
"movl %%fs:0, %%eax" "\n\t" |
| 590 |
"movl %%eax, 0x0(%%edx)" "\n\t" /* link */ |
| 591 |
"leal 1f, %%eax" "\n\t" |
| 592 |
"movl %%eax, 0x4(%%edx)" "\n\t" /* handler */ |
| 593 |
"movl %%ebp, 0x8(%%edx)" "\n\t" /* ebp */ |
| 594 |
"movl %%esp, 0xC(%%edx)" "\n\t" /* esp */ |
| 595 |
"movl $0, 0x10(%%edx)" "\n\t" /* status */ |
| 596 |
|
| 597 |
/* |
| 598 |
* Link the TCLEXCEPTION_REGISTRATION on the chain. |
| 599 |
*/ |
| 600 |
|
| 601 |
"movl %%edx, %%fs:0" "\n\t" |
| 602 |
|
| 603 |
/* |
| 604 |
* Call CopyFileW(nativeSrc, nativeDst, 0) |
| 605 |
*/ |
| 606 |
|
| 607 |
"movl %[copyFileW], %%eax" "\n\t" |
| 608 |
"pushl $0" "\n\t" |
| 609 |
"pushl %%ebx" "\n\t" |
| 610 |
"pushl %%ecx" "\n\t" |
| 611 |
"call *%%eax" "\n\t" |
| 612 |
|
| 613 |
/* |
| 614 |
* Come here on normal exit. Recover the TCLEXCEPTION_REGISTRATION and |
| 615 |
* put the status return from CopyFile into it. |
| 616 |
*/ |
| 617 |
|
| 618 |
"movl %%fs:0, %%edx" "\n\t" |
| 619 |
"movl %%eax, 0x10(%%edx)" "\n\t" |
| 620 |
"jmp 2f" "\n" |
| 621 |
|
| 622 |
/* |
| 623 |
* Come here on an exception. Recover the TCLEXCEPTION_REGISTRATION |
| 624 |
*/ |
| 625 |
|
| 626 |
"1:" "\t" |
| 627 |
"movl %%fs:0, %%edx" "\n\t" |
| 628 |
"movl 0x8(%%edx), %%edx" "\n\t" |
| 629 |
|
| 630 |
/* |
| 631 |
* Come here however we exited. Restore context from the |
| 632 |
* TCLEXCEPTION_REGISTRATION in case the stack is unbalanced. |
| 633 |
*/ |
| 634 |
|
| 635 |
"2:" "\t" |
| 636 |
"movl 0xC(%%edx), %%esp" "\n\t" |
| 637 |
"movl 0x8(%%edx), %%ebp" "\n\t" |
| 638 |
"movl 0x0(%%edx), %%eax" "\n\t" |
| 639 |
"movl %%eax, %%fs:0" "\n\t" |
| 640 |
|
| 641 |
: |
| 642 |
/* No outputs */ |
| 643 |
: |
| 644 |
[registration] "m" (registration), |
| 645 |
[nativeDst] "m" (nativeDst), |
| 646 |
[nativeSrc] "m" (nativeSrc), |
| 647 |
[copyFileW] "r" (CopyFileW) |
| 648 |
: |
| 649 |
"%eax", "%ebx", "%ecx", "%edx", "memory" |
| 650 |
); |
| 651 |
if (registration.status != FALSE) { |
| 652 |
retval = TCL_OK; |
| 653 |
} |
| 654 |
#else |
| 655 |
#ifndef HAVE_NO_SEH |
| 656 |
__try { |
| 657 |
#endif |
| 658 |
if (CopyFileW(nativeSrc, nativeDst, 0) != FALSE) { |
| 659 |
retval = TCL_OK; |
| 660 |
} |
| 661 |
#ifndef HAVE_NO_SEH |
| 662 |
} __except (EXCEPTION_EXECUTE_HANDLER) {} |
| 663 |
#endif |
| 664 |
#endif |
| 665 |
|
| 666 |
if (retval != -1) { |
| 667 |
return retval; |
| 668 |
} |
| 669 |
|
| 670 |
TclWinConvertError(GetLastError()); |
| 671 |
if (Tcl_GetErrno() == EBADF) { |
| 672 |
Tcl_SetErrno(EACCES); |
| 673 |
return TCL_ERROR; |
| 674 |
} |
| 675 |
if (Tcl_GetErrno() == EACCES) { |
| 676 |
DWORD srcAttr, dstAttr; |
| 677 |
|
| 678 |
srcAttr = GetFileAttributesW(nativeSrc); |
| 679 |
dstAttr = GetFileAttributesW(nativeDst); |
| 680 |
if (srcAttr != 0xFFFFFFFF) { |
| 681 |
if (dstAttr == 0xFFFFFFFF) { |
| 682 |
dstAttr = 0; |
| 683 |
} |
| 684 |
if ((srcAttr & FILE_ATTRIBUTE_DIRECTORY) || |
| 685 |
(dstAttr & FILE_ATTRIBUTE_DIRECTORY)) { |
| 686 |
if (srcAttr & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 687 |
/* Source is a symbolic link -- copy it */ |
| 688 |
if (TclWinSymLinkCopyDirectory(nativeSrc, nativeDst)==0) { |
| 689 |
return TCL_OK; |
| 690 |
} |
| 691 |
} |
| 692 |
Tcl_SetErrno(EISDIR); |
| 693 |
} |
| 694 |
if (dstAttr & FILE_ATTRIBUTE_READONLY) { |
| 695 |
SetFileAttributesW(nativeDst, |
| 696 |
dstAttr & ~((DWORD)FILE_ATTRIBUTE_READONLY)); |
| 697 |
if (CopyFileW(nativeSrc, nativeDst, |
| 698 |
0) != FALSE) { |
| 699 |
return TCL_OK; |
| 700 |
} |
| 701 |
|
| 702 |
/* |
| 703 |
* Still can't copy onto dst. Return that error, and restore |
| 704 |
* attributes of dst. |
| 705 |
*/ |
| 706 |
|
| 707 |
TclWinConvertError(GetLastError()); |
| 708 |
SetFileAttributesW(nativeDst, dstAttr); |
| 709 |
} |
| 710 |
} |
| 711 |
} |
| 712 |
return TCL_ERROR; |
| 713 |
} |
| 714 |
|
| 715 |
/* |
| 716 |
*--------------------------------------------------------------------------- |
| 717 |
* |
| 718 |
* TclpObjDeleteFile, TclpDeleteFile -- |
| 719 |
* |
| 720 |
* Removes a single file (not a directory). |
| 721 |
* |
| 722 |
* Results: |
| 723 |
* If the file was successfully deleted, returns TCL_OK. Otherwise the |
| 724 |
* return value is TCL_ERROR and errno is set to indicate the error. |
| 725 |
* Some possible values for errno are: |
| 726 |
* |
| 727 |
* EACCES: a parent directory can't be read and/or written. |
| 728 |
* EISDIR: path is a directory. |
| 729 |
* ENOENT: path doesn't exist or is "". |
| 730 |
* |
| 731 |
* EACCES: exists an open file already referring to path. |
| 732 |
* EACCES: path is a char device (nul:, com1:, etc.) |
| 733 |
* |
| 734 |
* Side effects: |
| 735 |
* The file is deleted, even if it is read-only. |
| 736 |
* |
| 737 |
*--------------------------------------------------------------------------- |
| 738 |
*/ |
| 739 |
|
| 740 |
int |
| 741 |
TclpObjDeleteFile( |
| 742 |
Tcl_Obj *pathPtr) |
| 743 |
{ |
| 744 |
return TclpDeleteFile(Tcl_FSGetNativePath(pathPtr)); |
| 745 |
} |
| 746 |
|
| 747 |
int |
| 748 |
TclpDeleteFile( |
| 749 |
const void *nativePath) /* Pathname of file to be removed (native). */ |
| 750 |
{ |
| 751 |
DWORD attr; |
| 752 |
const WCHAR *path = (const WCHAR *)nativePath; |
| 753 |
|
| 754 |
/* |
| 755 |
* The DeleteFile API acts differently under Win95/98 and NT WRT NULL and |
| 756 |
* "". Avoid passing these values. |
| 757 |
*/ |
| 758 |
|
| 759 |
if (path == NULL || path[0] == '\0') { |
| 760 |
Tcl_SetErrno(ENOENT); |
| 761 |
return TCL_ERROR; |
| 762 |
} |
| 763 |
|
| 764 |
if (DeleteFileW(path) != FALSE) { |
| 765 |
return TCL_OK; |
| 766 |
} |
| 767 |
TclWinConvertError(GetLastError()); |
| 768 |
|
| 769 |
if (Tcl_GetErrno() == EACCES) { |
| 770 |
attr = GetFileAttributesW(path); |
| 771 |
if (attr != 0xFFFFFFFF) { |
| 772 |
if (attr & FILE_ATTRIBUTE_DIRECTORY) { |
| 773 |
if (attr & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 774 |
/* |
| 775 |
* It is a symbolic link - remove it. |
| 776 |
*/ |
| 777 |
if (TclWinSymLinkDelete(path, 0) == 0) { |
| 778 |
return TCL_OK; |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
/* |
| 783 |
* If we fall through here, it is a directory. |
| 784 |
* |
| 785 |
* Windows NT reports removing a directory as EACCES instead |
| 786 |
* of EISDIR. |
| 787 |
*/ |
| 788 |
|
| 789 |
Tcl_SetErrno(EISDIR); |
| 790 |
} else if (attr & FILE_ATTRIBUTE_READONLY) { |
| 791 |
int res = SetFileAttributesW(path, |
| 792 |
attr & ~((DWORD) FILE_ATTRIBUTE_READONLY)); |
| 793 |
|
| 794 |
if ((res != 0) && |
| 795 |
(DeleteFileW(path) != FALSE)) { |
| 796 |
return TCL_OK; |
| 797 |
} |
| 798 |
TclWinConvertError(GetLastError()); |
| 799 |
if (res != 0) { |
| 800 |
SetFileAttributesW(path, attr); |
| 801 |
} |
| 802 |
} |
| 803 |
} |
| 804 |
} else if (Tcl_GetErrno() == ENOENT) { |
| 805 |
attr = GetFileAttributesW(path); |
| 806 |
if (attr != 0xFFFFFFFF) { |
| 807 |
if (attr & FILE_ATTRIBUTE_DIRECTORY) { |
| 808 |
/* |
| 809 |
* Windows 95 reports removing a directory as ENOENT instead |
| 810 |
* of EISDIR. |
| 811 |
*/ |
| 812 |
|
| 813 |
Tcl_SetErrno(EISDIR); |
| 814 |
} |
| 815 |
} |
| 816 |
} else if (Tcl_GetErrno() == EINVAL) { |
| 817 |
/* |
| 818 |
* Windows NT reports removing a char device as EINVAL instead of |
| 819 |
* EACCES. |
| 820 |
*/ |
| 821 |
|
| 822 |
Tcl_SetErrno(EACCES); |
| 823 |
} |
| 824 |
|
| 825 |
return TCL_ERROR; |
| 826 |
} |
| 827 |
|
| 828 |
/* |
| 829 |
*--------------------------------------------------------------------------- |
| 830 |
* |
| 831 |
* TclpObjCreateDirectory -- |
| 832 |
* |
| 833 |
* Creates the specified directory. All parent directories of the |
| 834 |
* specified directory must already exist. The directory is automatically |
| 835 |
* created with permissions so that user can access the new directory and |
| 836 |
* create new files or subdirectories in it. |
| 837 |
* |
| 838 |
* Results: |
| 839 |
* If the directory was successfully created, returns TCL_OK. Otherwise |
| 840 |
* the return value is TCL_ERROR and errno is set to indicate the error. |
| 841 |
* Some possible values for errno are: |
| 842 |
* |
| 843 |
* EACCES: a parent directory can't be read and/or written. |
| 844 |
* EEXIST: path already exists. |
| 845 |
* ENOENT: a parent directory doesn't exist. |
| 846 |
* |
| 847 |
* Side effects: |
| 848 |
* A directory is created. |
| 849 |
* |
| 850 |
*--------------------------------------------------------------------------- |
| 851 |
*/ |
| 852 |
|
| 853 |
int |
| 854 |
TclpObjCreateDirectory( |
| 855 |
Tcl_Obj *pathPtr) |
| 856 |
{ |
| 857 |
return DoCreateDirectory((WCHAR *)Tcl_FSGetNativePath(pathPtr)); |
| 858 |
} |
| 859 |
|
| 860 |
static int |
| 861 |
DoCreateDirectory( |
| 862 |
const WCHAR *nativePath) /* Pathname of directory to create (native). */ |
| 863 |
{ |
| 864 |
if (CreateDirectoryW(nativePath, NULL) == 0) { |
| 865 |
DWORD error = GetLastError(); |
| 866 |
|
| 867 |
TclWinConvertError(error); |
| 868 |
return TCL_ERROR; |
| 869 |
} |
| 870 |
return TCL_OK; |
| 871 |
} |
| 872 |
|
| 873 |
/* |
| 874 |
*--------------------------------------------------------------------------- |
| 875 |
* |
| 876 |
* TclpObjCopyDirectory -- |
| 877 |
* |
| 878 |
* Recursively copies a directory. The target directory dst must not |
| 879 |
* already exist. Note that this function does not merge two directory |
| 880 |
* hierarchies, even if the target directory is an empty directory. |
| 881 |
* |
| 882 |
* Results: |
| 883 |
* If the directory was successfully copied, returns TCL_OK. Otherwise |
| 884 |
* the return value is TCL_ERROR, errno is set to indicate the error, and |
| 885 |
* the pathname of the file that caused the error is stored in errorPtr. |
| 886 |
* See TclpCreateDirectory and TclpCopyFile for a description of possible |
| 887 |
* values for errno. |
| 888 |
* |
| 889 |
* Side effects: |
| 890 |
* An exact copy of the directory hierarchy src will be created with the |
| 891 |
* name dst. If an error occurs, the error will be returned immediately, |
| 892 |
* and remaining files will not be processed. |
| 893 |
* |
| 894 |
*--------------------------------------------------------------------------- |
| 895 |
*/ |
| 896 |
|
| 897 |
int |
| 898 |
TclpObjCopyDirectory( |
| 899 |
Tcl_Obj *srcPathPtr, |
| 900 |
Tcl_Obj *destPathPtr, |
| 901 |
Tcl_Obj **errorPtr) |
| 902 |
{ |
| 903 |
Tcl_DString ds; |
| 904 |
Tcl_DString srcString, dstString; |
| 905 |
Tcl_Obj *normSrcPtr, *normDestPtr; |
| 906 |
int ret; |
| 907 |
|
| 908 |
normSrcPtr = Tcl_FSGetNormalizedPath(NULL,srcPathPtr); |
| 909 |
if (normSrcPtr == NULL) { |
| 910 |
return TCL_ERROR; |
| 911 |
} |
| 912 |
if (normSrcPtr != srcPathPtr) { Tcl_IncrRefCount(normSrcPtr); } |
| 913 |
normDestPtr = Tcl_FSGetNormalizedPath(NULL,destPathPtr); |
| 914 |
if (normDestPtr == NULL) { |
| 915 |
if (normSrcPtr != srcPathPtr) { Tcl_DecrRefCount(normSrcPtr); } |
| 916 |
return TCL_ERROR; |
| 917 |
} |
| 918 |
if (normDestPtr != destPathPtr) { Tcl_IncrRefCount(normDestPtr); } |
| 919 |
|
| 920 |
Tcl_WinUtfToTChar(Tcl_GetString(normSrcPtr), -1, &srcString); |
| 921 |
Tcl_WinUtfToTChar(Tcl_GetString(normDestPtr), -1, &dstString); |
| 922 |
|
| 923 |
ret = TraverseWinTree(TraversalCopy, &srcString, &dstString, &ds); |
| 924 |
|
| 925 |
Tcl_DStringFree(&srcString); |
| 926 |
Tcl_DStringFree(&dstString); |
| 927 |
|
| 928 |
if (ret != TCL_OK) { |
| 929 |
if (!strcmp(Tcl_DStringValue(&ds), TclGetString(normSrcPtr))) { |
| 930 |
*errorPtr = srcPathPtr; |
| 931 |
} else if (!strcmp(Tcl_DStringValue(&ds), TclGetString(normDestPtr))) { |
| 932 |
*errorPtr = destPathPtr; |
| 933 |
} else { |
| 934 |
*errorPtr = TclDStringToObj(&ds); |
| 935 |
} |
| 936 |
Tcl_DStringFree(&ds); |
| 937 |
Tcl_IncrRefCount(*errorPtr); |
| 938 |
} |
| 939 |
|
| 940 |
if (normSrcPtr != srcPathPtr) { Tcl_DecrRefCount(normSrcPtr); } |
| 941 |
if (normDestPtr != destPathPtr) { Tcl_DecrRefCount(normDestPtr); } |
| 942 |
return ret; |
| 943 |
} |
| 944 |
|
| 945 |
/* |
| 946 |
*---------------------------------------------------------------------- |
| 947 |
* |
| 948 |
* TclpObjRemoveDirectory, DoRemoveDirectory -- |
| 949 |
* |
| 950 |
* Removes directory (and its contents, if the recursive flag is set). |
| 951 |
* |
| 952 |
* Results: |
| 953 |
* If the directory was successfully removed, returns TCL_OK. Otherwise |
| 954 |
* the return value is TCL_ERROR, errno is set to indicate the error, and |
| 955 |
* the pathname of the file that caused the error is stored in errorPtr. |
| 956 |
* Some possible values for errno are: |
| 957 |
* |
| 958 |
* EACCES: path directory can't be read and/or written. |
| 959 |
* EEXIST: path is a non-empty directory. |
| 960 |
* EINVAL: path is root directory or current directory. |
| 961 |
* ENOENT: path doesn't exist or is "". |
| 962 |
* ENOTDIR: path is not a directory. |
| 963 |
* |
| 964 |
* EACCES: path is a char device (nul:, com1:, etc.) (95) |
| 965 |
* EINVAL: path is a char device (nul:, com1:, etc.) (NT) |
| 966 |
* |
| 967 |
* Side effects: |
| 968 |
* Directory removed. If an error occurs, the error will be returned |
| 969 |
* immediately, and remaining files will not be deleted. |
| 970 |
* |
| 971 |
*---------------------------------------------------------------------- |
| 972 |
*/ |
| 973 |
|
| 974 |
int |
| 975 |
TclpObjRemoveDirectory( |
| 976 |
Tcl_Obj *pathPtr, |
| 977 |
int recursive, |
| 978 |
Tcl_Obj **errorPtr) |
| 979 |
{ |
| 980 |
Tcl_DString ds; |
| 981 |
Tcl_Obj *normPtr = NULL; |
| 982 |
int ret; |
| 983 |
|
| 984 |
if (recursive) { |
| 985 |
/* |
| 986 |
* In the recursive case, the string rep is used to construct a |
| 987 |
* Tcl_DString which may be used extensively, so we can't optimize |
| 988 |
* this case easily. |
| 989 |
*/ |
| 990 |
|
| 991 |
Tcl_DString native; |
| 992 |
normPtr = Tcl_FSGetNormalizedPath(NULL, pathPtr); |
| 993 |
if (normPtr == NULL) { |
| 994 |
return TCL_ERROR; |
| 995 |
} |
| 996 |
if (normPtr != pathPtr) { Tcl_IncrRefCount(normPtr); } |
| 997 |
Tcl_WinUtfToTChar(Tcl_GetString(normPtr), -1, &native); |
| 998 |
ret = DoRemoveDirectory(&native, recursive, &ds); |
| 999 |
Tcl_DStringFree(&native); |
| 1000 |
} else { |
| 1001 |
ret = DoRemoveJustDirectory((WCHAR *)Tcl_FSGetNativePath(pathPtr), 0, &ds); |
| 1002 |
} |
| 1003 |
|
| 1004 |
if (ret != TCL_OK) { |
| 1005 |
if (Tcl_DStringLength(&ds) > 0) { |
| 1006 |
if (normPtr != NULL && |
| 1007 |
!strcmp(Tcl_DStringValue(&ds), TclGetString(normPtr))) { |
| 1008 |
*errorPtr = pathPtr; |
| 1009 |
} else { |
| 1010 |
*errorPtr = TclDStringToObj(&ds); |
| 1011 |
} |
| 1012 |
Tcl_IncrRefCount(*errorPtr); |
| 1013 |
} |
| 1014 |
Tcl_DStringFree(&ds); |
| 1015 |
} |
| 1016 |
if (normPtr && normPtr != pathPtr) { Tcl_DecrRefCount(normPtr); } |
| 1017 |
|
| 1018 |
return ret; |
| 1019 |
} |
| 1020 |
|
| 1021 |
static int |
| 1022 |
DoRemoveJustDirectory( |
| 1023 |
const WCHAR *nativePath, /* Pathname of directory to be removed |
| 1024 |
* (native). */ |
| 1025 |
int ignoreError, /* If non-zero, don't initialize the errorPtr |
| 1026 |
* under some circumstances on return. */ |
| 1027 |
Tcl_DString *errorPtr) /* If non-NULL, uninitialized or free DString |
| 1028 |
* filled with UTF-8 name of file causing |
| 1029 |
* error. */ |
| 1030 |
{ |
| 1031 |
DWORD attr; |
| 1032 |
|
| 1033 |
/* |
| 1034 |
* The RemoveDirectory API acts differently under Win95/98 and NT WRT NULL |
| 1035 |
* and "". Avoid passing these values. |
| 1036 |
*/ |
| 1037 |
|
| 1038 |
if (nativePath == NULL || nativePath[0] == '\0') { |
| 1039 |
Tcl_SetErrno(ENOENT); |
| 1040 |
Tcl_DStringInit(errorPtr); |
| 1041 |
return TCL_ERROR; |
| 1042 |
} |
| 1043 |
|
| 1044 |
attr = GetFileAttributesW(nativePath); |
| 1045 |
|
| 1046 |
if (attr & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1047 |
/* |
| 1048 |
* It is a symbolic link - remove it. |
| 1049 |
*/ |
| 1050 |
if (TclWinSymLinkDelete(nativePath, 0) == 0) { |
| 1051 |
return TCL_OK; |
| 1052 |
} |
| 1053 |
} else { |
| 1054 |
/* |
| 1055 |
* Ordinary directory. |
| 1056 |
*/ |
| 1057 |
|
| 1058 |
if (RemoveDirectoryW(nativePath) != FALSE) { |
| 1059 |
return TCL_OK; |
| 1060 |
} |
| 1061 |
} |
| 1062 |
|
| 1063 |
TclWinConvertError(GetLastError()); |
| 1064 |
|
| 1065 |
if (Tcl_GetErrno() == EACCES) { |
| 1066 |
attr = GetFileAttributesW(nativePath); |
| 1067 |
if (attr != 0xFFFFFFFF) { |
| 1068 |
if ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0) { |
| 1069 |
/* |
| 1070 |
* Windows 95 reports calling RemoveDirectory on a file as an |
| 1071 |
* EACCES, not an ENOTDIR. |
| 1072 |
*/ |
| 1073 |
|
| 1074 |
Tcl_SetErrno(ENOTDIR); |
| 1075 |
goto end; |
| 1076 |
} |
| 1077 |
|
| 1078 |
if (attr & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1079 |
/* |
| 1080 |
* It is a symbolic link - remove it. |
| 1081 |
*/ |
| 1082 |
|
| 1083 |
if (TclWinSymLinkDelete(nativePath, 1) != 0) { |
| 1084 |
goto end; |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
if (attr & FILE_ATTRIBUTE_READONLY) { |
| 1089 |
attr &= ~FILE_ATTRIBUTE_READONLY; |
| 1090 |
if (SetFileAttributesW(nativePath, |
| 1091 |
attr) == FALSE) { |
| 1092 |
goto end; |
| 1093 |
} |
| 1094 |
if (RemoveDirectoryW(nativePath) != FALSE) { |
| 1095 |
return TCL_OK; |
| 1096 |
} |
| 1097 |
TclWinConvertError(GetLastError()); |
| 1098 |
SetFileAttributesW(nativePath, |
| 1099 |
attr | FILE_ATTRIBUTE_READONLY); |
| 1100 |
} |
| 1101 |
} |
| 1102 |
} |
| 1103 |
|
| 1104 |
if (Tcl_GetErrno() == ENOTEMPTY) { |
| 1105 |
/* |
| 1106 |
* The caller depends on EEXIST to signify that the directory is not |
| 1107 |
* empty, not ENOTEMPTY. |
| 1108 |
*/ |
| 1109 |
|
| 1110 |
Tcl_SetErrno(EEXIST); |
| 1111 |
} |
| 1112 |
|
| 1113 |
if ((ignoreError != 0) && (Tcl_GetErrno() == EEXIST)) { |
| 1114 |
/* |
| 1115 |
* If we're being recursive, this error may actually be ok, so we |
| 1116 |
* don't want to initialise the errorPtr yet. |
| 1117 |
*/ |
| 1118 |
return TCL_ERROR; |
| 1119 |
} |
| 1120 |
|
| 1121 |
end: |
| 1122 |
if (errorPtr != NULL) { |
| 1123 |
char *p = Tcl_WinTCharToUtf((TCHAR *)nativePath, -1, errorPtr); |
| 1124 |
for (; *p; ++p) { |
| 1125 |
if (*p == '\\') *p = '/'; |
| 1126 |
} |
| 1127 |
} |
| 1128 |
return TCL_ERROR; |
| 1129 |
|
| 1130 |
} |
| 1131 |
|
| 1132 |
static int |
| 1133 |
DoRemoveDirectory( |
| 1134 |
Tcl_DString *pathPtr, /* Pathname of directory to be removed |
| 1135 |
* (native). */ |
| 1136 |
int recursive, /* If non-zero, removes directories that are |
| 1137 |
* nonempty. Otherwise, will only remove empty |
| 1138 |
* directories. */ |
| 1139 |
Tcl_DString *errorPtr) /* If non-NULL, uninitialized or free DString |
| 1140 |
* filled with UTF-8 name of file causing |
| 1141 |
* error. */ |
| 1142 |
{ |
| 1143 |
int res = DoRemoveJustDirectory((const WCHAR *)Tcl_DStringValue(pathPtr), recursive, |
| 1144 |
errorPtr); |
| 1145 |
|
| 1146 |
if ((res == TCL_ERROR) && (recursive != 0) && (Tcl_GetErrno() == EEXIST)) { |
| 1147 |
/* |
| 1148 |
* The directory is nonempty, but the recursive flag has been |
| 1149 |
* specified, so we recursively remove all the files in the directory. |
| 1150 |
*/ |
| 1151 |
|
| 1152 |
return TraverseWinTree(TraversalDelete, pathPtr, NULL, errorPtr); |
| 1153 |
} else { |
| 1154 |
return res; |
| 1155 |
} |
| 1156 |
} |
| 1157 |
|
| 1158 |
/* |
| 1159 |
*--------------------------------------------------------------------------- |
| 1160 |
* |
| 1161 |
* TraverseWinTree -- |
| 1162 |
* |
| 1163 |
* Traverse directory tree specified by sourcePtr, calling the function |
| 1164 |
* traverseProc for each file and directory encountered. If destPtr is |
| 1165 |
* non-null, each of name in the sourcePtr directory is appended to the |
| 1166 |
* directory specified by destPtr and passed as the second argument to |
| 1167 |
* traverseProc(). |
| 1168 |
* |
| 1169 |
* Results: |
| 1170 |
* Standard Tcl result. |
| 1171 |
* |
| 1172 |
* Side effects: |
| 1173 |
* None caused by TraverseWinTree, however the user specified |
| 1174 |
* traverseProc() may change state. If an error occurs, the error will be |
| 1175 |
* returned immediately, and remaining files will not be processed. |
| 1176 |
* |
| 1177 |
*--------------------------------------------------------------------------- |
| 1178 |
*/ |
| 1179 |
|
| 1180 |
static int |
| 1181 |
TraverseWinTree( |
| 1182 |
TraversalProc *traverseProc,/* Function to call for every file and |
| 1183 |
* directory in source hierarchy. */ |
| 1184 |
Tcl_DString *sourcePtr, /* Pathname of source directory to be |
| 1185 |
* traversed (native). */ |
| 1186 |
Tcl_DString *targetPtr, /* Pathname of directory to traverse in |
| 1187 |
* parallel with source directory (native), |
| 1188 |
* may be NULL. */ |
| 1189 |
Tcl_DString *errorPtr) /* If non-NULL, uninitialized or free DString |
| 1190 |
* filled with UTF-8 name of file causing |
| 1191 |
* error. */ |
| 1192 |
{ |
| 1193 |
DWORD sourceAttr; |
| 1194 |
WCHAR *nativeSource, *nativeTarget, *nativeErrfile; |
| 1195 |
int result, found, sourceLen, targetLen = 0, oldSourceLen, oldTargetLen; |
| 1196 |
HANDLE handle; |
| 1197 |
WIN32_FIND_DATAW data; |
| 1198 |
|
| 1199 |
nativeErrfile = NULL; |
| 1200 |
result = TCL_OK; |
| 1201 |
oldTargetLen = 0; /* lint. */ |
| 1202 |
|
| 1203 |
nativeSource = (WCHAR *) Tcl_DStringValue(sourcePtr); |
| 1204 |
nativeTarget = (WCHAR *) |
| 1205 |
(targetPtr == NULL ? NULL : Tcl_DStringValue(targetPtr)); |
| 1206 |
|
| 1207 |
oldSourceLen = Tcl_DStringLength(sourcePtr); |
| 1208 |
sourceAttr = GetFileAttributesW(nativeSource); |
| 1209 |
if (sourceAttr == 0xFFFFFFFF) { |
| 1210 |
nativeErrfile = nativeSource; |
| 1211 |
goto end; |
| 1212 |
} |
| 1213 |
|
| 1214 |
if (sourceAttr & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1215 |
/* |
| 1216 |
* Process the symbolic link |
| 1217 |
*/ |
| 1218 |
|
| 1219 |
return traverseProc(nativeSource, nativeTarget, DOTREE_LINK, |
| 1220 |
errorPtr); |
| 1221 |
} |
| 1222 |
|
| 1223 |
if ((sourceAttr & FILE_ATTRIBUTE_DIRECTORY) == 0) { |
| 1224 |
/* |
| 1225 |
* Process the regular file |
| 1226 |
*/ |
| 1227 |
|
| 1228 |
return traverseProc(nativeSource, nativeTarget, DOTREE_F, errorPtr); |
| 1229 |
} |
| 1230 |
|
| 1231 |
Tcl_DStringAppend(sourcePtr, (char *) L"\\*.*", 4 * sizeof(WCHAR) + 1); |
| 1232 |
Tcl_DStringSetLength(sourcePtr, Tcl_DStringLength(sourcePtr) - 1); |
| 1233 |
|
| 1234 |
nativeSource = (WCHAR *) Tcl_DStringValue(sourcePtr); |
| 1235 |
handle = FindFirstFileW(nativeSource, &data); |
| 1236 |
if (handle == INVALID_HANDLE_VALUE) { |
| 1237 |
/* |
| 1238 |
* Can't read directory. |
| 1239 |
*/ |
| 1240 |
|
| 1241 |
TclWinConvertError(GetLastError()); |
| 1242 |
nativeErrfile = nativeSource; |
| 1243 |
goto end; |
| 1244 |
} |
| 1245 |
|
| 1246 |
Tcl_DStringSetLength(sourcePtr, oldSourceLen + 1); |
| 1247 |
Tcl_DStringSetLength(sourcePtr, oldSourceLen); |
| 1248 |
result = traverseProc(nativeSource, nativeTarget, DOTREE_PRED, |
| 1249 |
errorPtr); |
| 1250 |
if (result != TCL_OK) { |
| 1251 |
FindClose(handle); |
| 1252 |
return result; |
| 1253 |
} |
| 1254 |
|
| 1255 |
sourceLen = oldSourceLen + sizeof(WCHAR); |
| 1256 |
Tcl_DStringAppend(sourcePtr, (char *) L"\\", sizeof(WCHAR) + 1); |
| 1257 |
Tcl_DStringSetLength(sourcePtr, sourceLen); |
| 1258 |
if (targetPtr != NULL) { |
| 1259 |
oldTargetLen = Tcl_DStringLength(targetPtr); |
| 1260 |
|
| 1261 |
targetLen = oldTargetLen; |
| 1262 |
targetLen += sizeof(WCHAR); |
| 1263 |
Tcl_DStringAppend(targetPtr, (char *) L"\\", sizeof(WCHAR) + 1); |
| 1264 |
Tcl_DStringSetLength(targetPtr, targetLen); |
| 1265 |
} |
| 1266 |
|
| 1267 |
found = 1; |
| 1268 |
for (; found; found = FindNextFileW(handle, &data)) { |
| 1269 |
WCHAR *nativeName; |
| 1270 |
size_t len; |
| 1271 |
|
| 1272 |
WCHAR *wp = data.cFileName; |
| 1273 |
if (*wp == '.') { |
| 1274 |
wp++; |
| 1275 |
if (*wp == '.') { |
| 1276 |
wp++; |
| 1277 |
} |
| 1278 |
if (*wp == '\0') { |
| 1279 |
continue; |
| 1280 |
} |
| 1281 |
} |
| 1282 |
nativeName = (WCHAR *) data.cFileName; |
| 1283 |
len = wcslen(data.cFileName) * sizeof(WCHAR); |
| 1284 |
|
| 1285 |
/* |
| 1286 |
* Append name after slash, and recurse on the file. |
| 1287 |
*/ |
| 1288 |
|
| 1289 |
Tcl_DStringAppend(sourcePtr, (char *) nativeName, len + 1); |
| 1290 |
Tcl_DStringSetLength(sourcePtr, Tcl_DStringLength(sourcePtr) - 1); |
| 1291 |
if (targetPtr != NULL) { |
| 1292 |
Tcl_DStringAppend(targetPtr, (char *) nativeName, len + 1); |
| 1293 |
Tcl_DStringSetLength(targetPtr, Tcl_DStringLength(targetPtr) - 1); |
| 1294 |
} |
| 1295 |
result = TraverseWinTree(traverseProc, sourcePtr, targetPtr, |
| 1296 |
errorPtr); |
| 1297 |
if (result != TCL_OK) { |
| 1298 |
break; |
| 1299 |
} |
| 1300 |
|
| 1301 |
/* |
| 1302 |
* Remove name after slash. |
| 1303 |
*/ |
| 1304 |
|
| 1305 |
Tcl_DStringSetLength(sourcePtr, sourceLen); |
| 1306 |
if (targetPtr != NULL) { |
| 1307 |
Tcl_DStringSetLength(targetPtr, targetLen); |
| 1308 |
} |
| 1309 |
} |
| 1310 |
FindClose(handle); |
| 1311 |
|
| 1312 |
/* |
| 1313 |
* Strip off the trailing slash we added. |
| 1314 |
*/ |
| 1315 |
|
| 1316 |
Tcl_DStringSetLength(sourcePtr, oldSourceLen + 1); |
| 1317 |
Tcl_DStringSetLength(sourcePtr, oldSourceLen); |
| 1318 |
if (targetPtr != NULL) { |
| 1319 |
Tcl_DStringSetLength(targetPtr, oldTargetLen + 1); |
| 1320 |
Tcl_DStringSetLength(targetPtr, oldTargetLen); |
| 1321 |
} |
| 1322 |
if (result == TCL_OK) { |
| 1323 |
/* |
| 1324 |
* Call traverseProc() on a directory after visiting all the |
| 1325 |
* files in that directory. |
| 1326 |
*/ |
| 1327 |
|
| 1328 |
result = traverseProc((const WCHAR *)Tcl_DStringValue(sourcePtr), |
| 1329 |
(const WCHAR *)(targetPtr == NULL ? NULL : Tcl_DStringValue(targetPtr)), |
| 1330 |
DOTREE_POSTD, errorPtr); |
| 1331 |
} |
| 1332 |
|
| 1333 |
end: |
| 1334 |
if (nativeErrfile != NULL) { |
| 1335 |
TclWinConvertError(GetLastError()); |
| 1336 |
if (errorPtr != NULL) { |
| 1337 |
Tcl_WinTCharToUtf((TCHAR *)nativeErrfile, -1, errorPtr); |
| 1338 |
} |
| 1339 |
result = TCL_ERROR; |
| 1340 |
} |
| 1341 |
|
| 1342 |
return result; |
| 1343 |
} |
| 1344 |
|
| 1345 |
/* |
| 1346 |
*---------------------------------------------------------------------- |
| 1347 |
* |
| 1348 |
* TraversalCopy |
| 1349 |
* |
| 1350 |
* Called from TraverseUnixTree in order to execute a recursive copy of a |
| 1351 |
* directory. |
| 1352 |
* |
| 1353 |
* Results: |
| 1354 |
* Standard Tcl result. |
| 1355 |
* |
| 1356 |
* Side effects: |
| 1357 |
* Depending on the value of type, src may be copied to dst. |
| 1358 |
* |
| 1359 |
*---------------------------------------------------------------------- |
| 1360 |
*/ |
| 1361 |
|
| 1362 |
static int |
| 1363 |
TraversalCopy( |
| 1364 |
const WCHAR *nativeSrc, /* Source pathname to copy. */ |
| 1365 |
const WCHAR *nativeDst, /* Destination pathname of copy. */ |
| 1366 |
int type, /* Reason for call - see TraverseWinTree() */ |
| 1367 |
Tcl_DString *errorPtr) /* If non-NULL, initialized DString filled |
| 1368 |
* with UTF-8 name of file causing error. */ |
| 1369 |
{ |
| 1370 |
switch (type) { |
| 1371 |
case DOTREE_F: |
| 1372 |
if (DoCopyFile(nativeSrc, nativeDst) == TCL_OK) { |
| 1373 |
return TCL_OK; |
| 1374 |
} |
| 1375 |
break; |
| 1376 |
case DOTREE_LINK: |
| 1377 |
if (TclWinSymLinkCopyDirectory(nativeSrc, nativeDst) == TCL_OK) { |
| 1378 |
return TCL_OK; |
| 1379 |
} |
| 1380 |
break; |
| 1381 |
case DOTREE_PRED: |
| 1382 |
if (DoCreateDirectory(nativeDst) == TCL_OK) { |
| 1383 |
DWORD attr = GetFileAttributesW(nativeSrc); |
| 1384 |
|
| 1385 |
if (SetFileAttributesW(nativeDst, |
| 1386 |
attr) != FALSE) { |
| 1387 |
return TCL_OK; |
| 1388 |
} |
| 1389 |
TclWinConvertError(GetLastError()); |
| 1390 |
} |
| 1391 |
break; |
| 1392 |
case DOTREE_POSTD: |
| 1393 |
return TCL_OK; |
| 1394 |
} |
| 1395 |
|
| 1396 |
/* |
| 1397 |
* There shouldn't be a problem with src, because we already checked it to |
| 1398 |
* get here. |
| 1399 |
*/ |
| 1400 |
|
| 1401 |
if (errorPtr != NULL) { |
| 1402 |
Tcl_WinTCharToUtf((TCHAR *)nativeDst, -1, errorPtr); |
| 1403 |
} |
| 1404 |
return TCL_ERROR; |
| 1405 |
} |
| 1406 |
|
| 1407 |
/* |
| 1408 |
*---------------------------------------------------------------------- |
| 1409 |
* |
| 1410 |
* TraversalDelete -- |
| 1411 |
* |
| 1412 |
* Called by function TraverseWinTree for every file and directory that |
| 1413 |
* it encounters in a directory hierarchy. This function unlinks files, |
| 1414 |
* and removes directories after all the containing files have been |
| 1415 |
* processed. |
| 1416 |
* |
| 1417 |
* Results: |
| 1418 |
* Standard Tcl result. |
| 1419 |
* |
| 1420 |
* Side effects: |
| 1421 |
* Files or directory specified by src will be deleted. If an error |
| 1422 |
* occurs, the windows error is converted to a Posix error and errno is |
| 1423 |
* set accordingly. |
| 1424 |
* |
| 1425 |
*---------------------------------------------------------------------- |
| 1426 |
*/ |
| 1427 |
|
| 1428 |
static int |
| 1429 |
TraversalDelete( |
| 1430 |
const WCHAR *nativeSrc, /* Source pathname to delete. */ |
| 1431 |
const WCHAR *dstPtr, /* Not used. */ |
| 1432 |
int type, /* Reason for call - see TraverseWinTree() */ |
| 1433 |
Tcl_DString *errorPtr) /* If non-NULL, initialized DString filled |
| 1434 |
* with UTF-8 name of file causing error. */ |
| 1435 |
{ |
| 1436 |
switch (type) { |
| 1437 |
case DOTREE_F: |
| 1438 |
if (TclpDeleteFile(nativeSrc) == TCL_OK) { |
| 1439 |
return TCL_OK; |
| 1440 |
} |
| 1441 |
break; |
| 1442 |
case DOTREE_LINK: |
| 1443 |
if (DoRemoveJustDirectory(nativeSrc, 0, NULL) == TCL_OK) { |
| 1444 |
return TCL_OK; |
| 1445 |
} |
| 1446 |
break; |
| 1447 |
case DOTREE_PRED: |
| 1448 |
return TCL_OK; |
| 1449 |
case DOTREE_POSTD: |
| 1450 |
if (DoRemoveJustDirectory(nativeSrc, 0, NULL) == TCL_OK) { |
| 1451 |
return TCL_OK; |
| 1452 |
} |
| 1453 |
break; |
| 1454 |
} |
| 1455 |
|
| 1456 |
if (errorPtr != NULL) { |
| 1457 |
Tcl_WinTCharToUtf((TCHAR *)nativeSrc, -1, errorPtr); |
| 1458 |
} |
| 1459 |
return TCL_ERROR; |
| 1460 |
} |
| 1461 |
|
| 1462 |
/* |
| 1463 |
*---------------------------------------------------------------------- |
| 1464 |
* |
| 1465 |
* StatError -- |
| 1466 |
* |
| 1467 |
* Sets the object result with the appropriate error. |
| 1468 |
* |
| 1469 |
* Results: |
| 1470 |
* None. |
| 1471 |
* |
| 1472 |
* Side effects: |
| 1473 |
* The interp's object result is set with an error message based on the |
| 1474 |
* objIndex, fileName and errno. |
| 1475 |
* |
| 1476 |
*---------------------------------------------------------------------- |
| 1477 |
*/ |
| 1478 |
|
| 1479 |
static void |
| 1480 |
StatError( |
| 1481 |
Tcl_Interp *interp, /* The interp that has the error */ |
| 1482 |
Tcl_Obj *fileName) /* The name of the file which caused the |
| 1483 |
* error. */ |
| 1484 |
{ |
| 1485 |
TclWinConvertError(GetLastError()); |
| 1486 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf("could not read \"%s\": %s", |
| 1487 |
TclGetString(fileName), Tcl_PosixError(interp))); |
| 1488 |
} |
| 1489 |
|
| 1490 |
/* |
| 1491 |
*---------------------------------------------------------------------- |
| 1492 |
* |
| 1493 |
* GetWinFileAttributes -- |
| 1494 |
* |
| 1495 |
* Returns a Tcl_Obj containing the value of a file attribute. This |
| 1496 |
* routine gets the -hidden, -readonly or -system attribute. |
| 1497 |
* |
| 1498 |
* Results: |
| 1499 |
* Standard Tcl result and a Tcl_Obj in attributePtrPtr. The object will |
| 1500 |
* have ref count 0. If the return value is not TCL_OK, attributePtrPtr |
| 1501 |
* is not touched. |
| 1502 |
* |
| 1503 |
* Side effects: |
| 1504 |
* A new object is allocated if the file is valid. |
| 1505 |
* |
| 1506 |
*---------------------------------------------------------------------- |
| 1507 |
*/ |
| 1508 |
|
| 1509 |
static int |
| 1510 |
GetWinFileAttributes( |
| 1511 |
Tcl_Interp *interp, /* The interp we are using for errors. */ |
| 1512 |
int objIndex, /* The index of the attribute. */ |
| 1513 |
Tcl_Obj *fileName, /* The name of the file. */ |
| 1514 |
Tcl_Obj **attributePtrPtr) /* A pointer to return the object with. */ |
| 1515 |
{ |
| 1516 |
DWORD result; |
| 1517 |
const WCHAR *nativeName; |
| 1518 |
int attr; |
| 1519 |
|
| 1520 |
nativeName = (WCHAR *)Tcl_FSGetNativePath(fileName); |
| 1521 |
result = GetFileAttributesW(nativeName); |
| 1522 |
|
| 1523 |
if (result == 0xFFFFFFFF) { |
| 1524 |
StatError(interp, fileName); |
| 1525 |
return TCL_ERROR; |
| 1526 |
} |
| 1527 |
|
| 1528 |
attr = (int)(result & attributeArray[objIndex]); |
| 1529 |
if ((objIndex == WIN_HIDDEN_ATTRIBUTE) && (attr != 0)) { |
| 1530 |
/* |
| 1531 |
* It is hidden. However there is a bug on some Windows OSes in which |
| 1532 |
* root volumes (drives) formatted as NTFS are declared hidden when |
| 1533 |
* they are not (and cannot be). |
| 1534 |
* |
| 1535 |
* We test for, and fix that case, here. |
| 1536 |
*/ |
| 1537 |
|
| 1538 |
int len; |
| 1539 |
const char *str = Tcl_GetStringFromObj(fileName,&len); |
| 1540 |
|
| 1541 |
if (len < 4) { |
| 1542 |
if (len == 0) { |
| 1543 |
/* |
| 1544 |
* Not sure if this is possible, but we pass it on anyway. |
| 1545 |
*/ |
| 1546 |
} else if (len == 1 && (str[0] == '/' || str[0] == '\\')) { |
| 1547 |
/* |
| 1548 |
* Path is pointing to the root volume. |
| 1549 |
*/ |
| 1550 |
|
| 1551 |
attr = 0; |
| 1552 |
} else if ((str[1] == ':') |
| 1553 |
&& (len == 2 || (str[2] == '/' || str[2] == '\\'))) { |
| 1554 |
/* |
| 1555 |
* Path is of the form 'x:' or 'x:/' or 'x:\' |
| 1556 |
*/ |
| 1557 |
|
| 1558 |
attr = 0; |
| 1559 |
} |
| 1560 |
} |
| 1561 |
} |
| 1562 |
|
| 1563 |
*attributePtrPtr = Tcl_NewBooleanObj(attr); |
| 1564 |
return TCL_OK; |
| 1565 |
} |
| 1566 |
|
| 1567 |
/* |
| 1568 |
*---------------------------------------------------------------------- |
| 1569 |
* |
| 1570 |
* ConvertFileNameFormat -- |
| 1571 |
* |
| 1572 |
* Returns a Tcl_Obj containing either the long or short version of the |
| 1573 |
* file name. |
| 1574 |
* |
| 1575 |
* Results: |
| 1576 |
* Standard Tcl result and a Tcl_Obj in attributePtrPtr. The object will |
| 1577 |
* have ref count 0. If the return value is not TCL_OK, attributePtrPtr |
| 1578 |
* is not touched. |
| 1579 |
* |
| 1580 |
* Warning: if you pass this function a drive name like 'c:' it will |
| 1581 |
* actually return the current working directory on that drive. To avoid |
| 1582 |
* this, make sure the drive name ends in a slash, like this 'c:/'. |
| 1583 |
* |
| 1584 |
* Side effects: |
| 1585 |
* A new object is allocated if the file is valid. |
| 1586 |
* |
| 1587 |
*---------------------------------------------------------------------- |
| 1588 |
*/ |
| 1589 |
|
| 1590 |
static int |
| 1591 |
ConvertFileNameFormat( |
| 1592 |
Tcl_Interp *interp, /* The interp we are using for errors. */ |
| 1593 |
int objIndex, /* The index of the attribute. */ |
| 1594 |
Tcl_Obj *fileName, /* The name of the file. */ |
| 1595 |
int longShort, /* 0 to short name, 1 to long name. */ |
| 1596 |
Tcl_Obj **attributePtrPtr) /* A pointer to return the object with. */ |
| 1597 |
{ |
| 1598 |
int pathc, i; |
| 1599 |
Tcl_Obj *splitPath; |
| 1600 |
|
| 1601 |
splitPath = Tcl_FSSplitPath(fileName, &pathc); |
| 1602 |
|
| 1603 |
if (splitPath == NULL || pathc == 0) { |
| 1604 |
if (interp != NULL) { |
| 1605 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1606 |
"could not read \"%s\": no such file or directory", |
| 1607 |
Tcl_GetString(fileName))); |
| 1608 |
errno = ENOENT; |
| 1609 |
Tcl_PosixError(interp); |
| 1610 |
} |
| 1611 |
goto cleanup; |
| 1612 |
} |
| 1613 |
|
| 1614 |
/* |
| 1615 |
* We will decrement this again at the end. It is safer to do this in |
| 1616 |
* case any of the calls below retain a reference to splitPath. |
| 1617 |
*/ |
| 1618 |
|
| 1619 |
Tcl_IncrRefCount(splitPath); |
| 1620 |
|
| 1621 |
for (i = 0; i < pathc; i++) { |
| 1622 |
Tcl_Obj *elt; |
| 1623 |
char *pathv; |
| 1624 |
int pathLen; |
| 1625 |
|
| 1626 |
Tcl_ListObjIndex(NULL, splitPath, i, &elt); |
| 1627 |
|
| 1628 |
pathv = Tcl_GetStringFromObj(elt, &pathLen); |
| 1629 |
if ((pathv[0] == '/') || ((pathLen == 3) && (pathv[1] == ':')) |
| 1630 |
|| (strcmp(pathv, ".") == 0) || (strcmp(pathv, "..") == 0)) { |
| 1631 |
/* |
| 1632 |
* Handle "/", "//machine/export", "c:/", "." or ".." by just |
| 1633 |
* copying the string literally. Uppercase the drive letter, just |
| 1634 |
* because it looks better under Windows to do so. |
| 1635 |
*/ |
| 1636 |
|
| 1637 |
simple: |
| 1638 |
/* |
| 1639 |
* Here we are modifying the string representation in place. |
| 1640 |
* |
| 1641 |
* I believe this is legal, since this won't affect any file |
| 1642 |
* representation this thing may have. |
| 1643 |
*/ |
| 1644 |
|
| 1645 |
pathv[0] = (char) Tcl_UniCharToUpper(UCHAR(pathv[0])); |
| 1646 |
} else { |
| 1647 |
Tcl_Obj *tempPath; |
| 1648 |
Tcl_DString ds; |
| 1649 |
Tcl_DString dsTemp; |
| 1650 |
const WCHAR *nativeName; |
| 1651 |
const char *tempString; |
| 1652 |
int tempLen; |
| 1653 |
WIN32_FIND_DATAW data; |
| 1654 |
HANDLE handle; |
| 1655 |
DWORD attr; |
| 1656 |
|
| 1657 |
tempPath = Tcl_FSJoinPath(splitPath, i+1); |
| 1658 |
Tcl_IncrRefCount(tempPath); |
| 1659 |
|
| 1660 |
/* |
| 1661 |
* We'd like to call Tcl_FSGetNativePath(tempPath) but that is |
| 1662 |
* likely to lead to infinite loops. |
| 1663 |
*/ |
| 1664 |
|
| 1665 |
Tcl_DStringInit(&ds); |
| 1666 |
tempString = Tcl_GetStringFromObj(tempPath,&tempLen); |
| 1667 |
nativeName = (WCHAR *)Tcl_WinUtfToTChar(tempString, tempLen, &ds); |
| 1668 |
Tcl_DecrRefCount(tempPath); |
| 1669 |
handle = FindFirstFileW(nativeName, &data); |
| 1670 |
if (handle == INVALID_HANDLE_VALUE) { |
| 1671 |
/* |
| 1672 |
* FindFirstFileW() doesn't like root directories. We would |
| 1673 |
* only get a root directory here if the caller specified "c:" |
| 1674 |
* or "c:." and the current directory on the drive was the |
| 1675 |
* root directory |
| 1676 |
*/ |
| 1677 |
|
| 1678 |
attr = GetFileAttributesW(nativeName); |
| 1679 |
if ((attr!=0xFFFFFFFF) && (attr & FILE_ATTRIBUTE_DIRECTORY)) { |
| 1680 |
Tcl_DStringFree(&ds); |
| 1681 |
goto simple; |
| 1682 |
} |
| 1683 |
} |
| 1684 |
|
| 1685 |
if (handle == INVALID_HANDLE_VALUE) { |
| 1686 |
Tcl_DStringFree(&ds); |
| 1687 |
if (interp != NULL) { |
| 1688 |
StatError(interp, fileName); |
| 1689 |
} |
| 1690 |
goto cleanup; |
| 1691 |
} |
| 1692 |
nativeName = data.cAlternateFileName; |
| 1693 |
if (longShort) { |
| 1694 |
if (data.cFileName[0] != '\0') { |
| 1695 |
nativeName = data.cFileName; |
| 1696 |
} |
| 1697 |
} else { |
| 1698 |
if (data.cAlternateFileName[0] == '\0') { |
| 1699 |
nativeName = (WCHAR *) data.cFileName; |
| 1700 |
} |
| 1701 |
} |
| 1702 |
|
| 1703 |
/* |
| 1704 |
* Purify reports a extraneous UMR in Tcl_WinTCharToUtf() trying |
| 1705 |
* to dereference nativeName as a Unicode string. I have proven to |
| 1706 |
* myself that purify is wrong by running the following example |
| 1707 |
* when nativeName == data.w.cAlternateFileName and noting that |
| 1708 |
* purify doesn't complain about the first line, but does complain |
| 1709 |
* about the second. |
| 1710 |
* |
| 1711 |
* fprintf(stderr, "%d\n", data.w.cAlternateFileName[0]); |
| 1712 |
* fprintf(stderr, "%d\n", ((WCHAR *) nativeName)[0]); |
| 1713 |
*/ |
| 1714 |
|
| 1715 |
Tcl_DStringInit(&dsTemp); |
| 1716 |
Tcl_WinTCharToUtf((TCHAR *)nativeName, -1, &dsTemp); |
| 1717 |
Tcl_DStringFree(&ds); |
| 1718 |
|
| 1719 |
/* |
| 1720 |
* Deal with issues of tildes being absolute. |
| 1721 |
*/ |
| 1722 |
|
| 1723 |
if (Tcl_DStringValue(&dsTemp)[0] == '~') { |
| 1724 |
TclNewLiteralStringObj(tempPath, "./"); |
| 1725 |
Tcl_AppendToObj(tempPath, Tcl_DStringValue(&dsTemp), |
| 1726 |
Tcl_DStringLength(&dsTemp)); |
| 1727 |
Tcl_DStringFree(&dsTemp); |
| 1728 |
} else { |
| 1729 |
tempPath = TclDStringToObj(&dsTemp); |
| 1730 |
} |
| 1731 |
Tcl_ListObjReplace(NULL, splitPath, i, 1, 1, &tempPath); |
| 1732 |
FindClose(handle); |
| 1733 |
} |
| 1734 |
} |
| 1735 |
|
| 1736 |
*attributePtrPtr = Tcl_FSJoinPath(splitPath, -1); |
| 1737 |
|
| 1738 |
if (splitPath != NULL) { |
| 1739 |
/* |
| 1740 |
* Unfortunately, the object we will return may have its only refCount |
| 1741 |
* as part of the list splitPath. This means if we free splitPath, the |
| 1742 |
* object will disappear. So, we have to be very careful here. |
| 1743 |
* Unfortunately this means we must manipulate the object's refCount |
| 1744 |
* directly. |
| 1745 |
*/ |
| 1746 |
|
| 1747 |
Tcl_IncrRefCount(*attributePtrPtr); |
| 1748 |
Tcl_DecrRefCount(splitPath); |
| 1749 |
--(*attributePtrPtr)->refCount; |
| 1750 |
} |
| 1751 |
return TCL_OK; |
| 1752 |
|
| 1753 |
cleanup: |
| 1754 |
if (splitPath != NULL) { |
| 1755 |
Tcl_DecrRefCount(splitPath); |
| 1756 |
} |
| 1757 |
|
| 1758 |
return TCL_ERROR; |
| 1759 |
} |
| 1760 |
|
| 1761 |
/* |
| 1762 |
*---------------------------------------------------------------------- |
| 1763 |
* |
| 1764 |
* GetWinFileLongName -- |
| 1765 |
* |
| 1766 |
* Returns a Tcl_Obj containing the long version of the file name. |
| 1767 |
* |
| 1768 |
* Results: |
| 1769 |
* Standard Tcl result and a Tcl_Obj in attributePtrPtr. The object will |
| 1770 |
* have ref count 0. If the return value is not TCL_OK, attributePtrPtr |
| 1771 |
* is not touched. |
| 1772 |
* |
| 1773 |
* Side effects: |
| 1774 |
* A new object is allocated if the file is valid. |
| 1775 |
* |
| 1776 |
*---------------------------------------------------------------------- |
| 1777 |
*/ |
| 1778 |
|
| 1779 |
static int |
| 1780 |
GetWinFileLongName( |
| 1781 |
Tcl_Interp *interp, /* The interp we are using for errors. */ |
| 1782 |
int objIndex, /* The index of the attribute. */ |
| 1783 |
Tcl_Obj *fileName, /* The name of the file. */ |
| 1784 |
Tcl_Obj **attributePtrPtr) /* A pointer to return the object with. */ |
| 1785 |
{ |
| 1786 |
return ConvertFileNameFormat(interp, objIndex, fileName, 1, |
| 1787 |
attributePtrPtr); |
| 1788 |
} |
| 1789 |
|
| 1790 |
/* |
| 1791 |
*---------------------------------------------------------------------- |
| 1792 |
* |
| 1793 |
* GetWinFileShortName -- |
| 1794 |
* |
| 1795 |
* Returns a Tcl_Obj containing the short version of the file name. |
| 1796 |
* |
| 1797 |
* Results: |
| 1798 |
* Standard Tcl result and a Tcl_Obj in attributePtrPtr. The object will |
| 1799 |
* have ref count 0. If the return value is not TCL_OK, attributePtrPtr |
| 1800 |
* is not touched. |
| 1801 |
* |
| 1802 |
* Side effects: |
| 1803 |
* A new object is allocated if the file is valid. |
| 1804 |
* |
| 1805 |
*---------------------------------------------------------------------- |
| 1806 |
*/ |
| 1807 |
|
| 1808 |
static int |
| 1809 |
GetWinFileShortName( |
| 1810 |
Tcl_Interp *interp, /* The interp we are using for errors. */ |
| 1811 |
int objIndex, /* The index of the attribute. */ |
| 1812 |
Tcl_Obj *fileName, /* The name of the file. */ |
| 1813 |
Tcl_Obj **attributePtrPtr) /* A pointer to return the object with. */ |
| 1814 |
{ |
| 1815 |
return ConvertFileNameFormat(interp, objIndex, fileName, 0, |
| 1816 |
attributePtrPtr); |
| 1817 |
} |
| 1818 |
|
| 1819 |
/* |
| 1820 |
*---------------------------------------------------------------------- |
| 1821 |
* |
| 1822 |
* SetWinFileAttributes -- |
| 1823 |
* |
| 1824 |
* Set the file attributes to the value given by attributePtr. This |
| 1825 |
* routine sets the -hidden, -readonly, or -system attributes. |
| 1826 |
* |
| 1827 |
* Results: |
| 1828 |
* Standard TCL error. |
| 1829 |
* |
| 1830 |
* Side effects: |
| 1831 |
* The file's attribute is set. |
| 1832 |
* |
| 1833 |
*---------------------------------------------------------------------- |
| 1834 |
*/ |
| 1835 |
|
| 1836 |
static int |
| 1837 |
SetWinFileAttributes( |
| 1838 |
Tcl_Interp *interp, /* The interp we are using for errors. */ |
| 1839 |
int objIndex, /* The index of the attribute. */ |
| 1840 |
Tcl_Obj *fileName, /* The name of the file. */ |
| 1841 |
Tcl_Obj *attributePtr) /* The new value of the attribute. */ |
| 1842 |
{ |
| 1843 |
DWORD fileAttributes, old; |
| 1844 |
int yesNo, result; |
| 1845 |
const WCHAR *nativeName; |
| 1846 |
|
| 1847 |
nativeName = (WCHAR *)Tcl_FSGetNativePath(fileName); |
| 1848 |
fileAttributes = old = GetFileAttributesW(nativeName); |
| 1849 |
|
| 1850 |
if (fileAttributes == 0xFFFFFFFF) { |
| 1851 |
StatError(interp, fileName); |
| 1852 |
return TCL_ERROR; |
| 1853 |
} |
| 1854 |
|
| 1855 |
result = Tcl_GetBooleanFromObj(interp, attributePtr, &yesNo); |
| 1856 |
if (result != TCL_OK) { |
| 1857 |
return result; |
| 1858 |
} |
| 1859 |
|
| 1860 |
if (yesNo) { |
| 1861 |
fileAttributes |= (attributeArray[objIndex]); |
| 1862 |
} else { |
| 1863 |
fileAttributes &= ~(attributeArray[objIndex]); |
| 1864 |
} |
| 1865 |
|
| 1866 |
if ((fileAttributes != old) |
| 1867 |
&& !SetFileAttributesW(nativeName, fileAttributes)) { |
| 1868 |
StatError(interp, fileName); |
| 1869 |
return TCL_ERROR; |
| 1870 |
} |
| 1871 |
|
| 1872 |
return result; |
| 1873 |
} |
| 1874 |
|
| 1875 |
/* |
| 1876 |
*---------------------------------------------------------------------- |
| 1877 |
* |
| 1878 |
* SetWinFileLongName -- |
| 1879 |
* |
| 1880 |
* The attribute in question is a readonly attribute and cannot be set. |
| 1881 |
* |
| 1882 |
* Results: |
| 1883 |
* TCL_ERROR |
| 1884 |
* |
| 1885 |
* Side effects: |
| 1886 |
* The object result is set to a pertinent error message. |
| 1887 |
* |
| 1888 |
*---------------------------------------------------------------------- |
| 1889 |
*/ |
| 1890 |
|
| 1891 |
static int |
| 1892 |
CannotSetAttribute( |
| 1893 |
Tcl_Interp *interp, /* The interp we are using for errors. */ |
| 1894 |
int objIndex, /* The index of the attribute. */ |
| 1895 |
Tcl_Obj *fileName, /* The name of the file. */ |
| 1896 |
Tcl_Obj *attributePtr) /* The new value of the attribute. */ |
| 1897 |
{ |
| 1898 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1899 |
"cannot set attribute \"%s\" for file \"%s\": attribute is readonly", |
| 1900 |
tclpFileAttrStrings[objIndex], Tcl_GetString(fileName))); |
| 1901 |
errno = EINVAL; |
| 1902 |
Tcl_PosixError(interp); |
| 1903 |
return TCL_ERROR; |
| 1904 |
} |
| 1905 |
|
| 1906 |
/* |
| 1907 |
*--------------------------------------------------------------------------- |
| 1908 |
* |
| 1909 |
* TclpObjListVolumes -- |
| 1910 |
* |
| 1911 |
* Lists the currently mounted volumes |
| 1912 |
* |
| 1913 |
* Results: |
| 1914 |
* The list of volumes. |
| 1915 |
* |
| 1916 |
* Side effects: |
| 1917 |
* None |
| 1918 |
* |
| 1919 |
*--------------------------------------------------------------------------- |
| 1920 |
*/ |
| 1921 |
|
| 1922 |
Tcl_Obj * |
| 1923 |
TclpObjListVolumes(void) |
| 1924 |
{ |
| 1925 |
Tcl_Obj *resultPtr, *elemPtr; |
| 1926 |
char buf[40 * 4]; /* There couldn't be more than 30 drives??? */ |
| 1927 |
int i; |
| 1928 |
char *p; |
| 1929 |
|
| 1930 |
TclNewObj(resultPtr); |
| 1931 |
|
| 1932 |
/* |
| 1933 |
* On Win32s: |
| 1934 |
* GetLogicalDriveStrings() isn't implemented. |
| 1935 |
* GetLogicalDrives() returns incorrect information. |
| 1936 |
*/ |
| 1937 |
|
| 1938 |
if (GetLogicalDriveStringsA(sizeof(buf), buf) == 0) { |
| 1939 |
/* |
| 1940 |
* GetVolumeInformationW() will detect all drives, but causes |
| 1941 |
* chattering on empty floppy drives. We only do this if |
| 1942 |
* GetLogicalDriveStrings() didn't work. It has also been reported |
| 1943 |
* that on some laptops it takes a while for GetVolumeInformationW() to |
| 1944 |
* return when pinging an empty floppy drive, another reason to try to |
| 1945 |
* avoid calling it. |
| 1946 |
*/ |
| 1947 |
|
| 1948 |
buf[1] = ':'; |
| 1949 |
buf[2] = '/'; |
| 1950 |
buf[3] = '\0'; |
| 1951 |
|
| 1952 |
for (i = 0; i < 26; i++) { |
| 1953 |
buf[0] = (char) ('a' + i); |
| 1954 |
if (GetVolumeInformationA(buf, NULL, 0, NULL, NULL, NULL, NULL, 0) |
| 1955 |
|| (GetLastError() == ERROR_NOT_READY)) { |
| 1956 |
elemPtr = Tcl_NewStringObj(buf, -1); |
| 1957 |
Tcl_ListObjAppendElement(NULL, resultPtr, elemPtr); |
| 1958 |
} |
| 1959 |
} |
| 1960 |
} else { |
| 1961 |
for (p = buf; *p != '\0'; p += 4) { |
| 1962 |
p[2] = '/'; |
| 1963 |
elemPtr = Tcl_NewStringObj(p, -1); |
| 1964 |
Tcl_ListObjAppendElement(NULL, resultPtr, elemPtr); |
| 1965 |
} |
| 1966 |
} |
| 1967 |
|
| 1968 |
Tcl_IncrRefCount(resultPtr); |
| 1969 |
return resultPtr; |
| 1970 |
} |
| 1971 |
|
| 1972 |
/* |
| 1973 |
* Local Variables: |
| 1974 |
* mode: c |
| 1975 |
* c-basic-offset: 4 |
| 1976 |
* fill-column: 78 |
| 1977 |
* End: |
| 1978 |
*/ |