os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/win/tclWinFCmd.c
Update contrib.
4 * This file implements the Windows specific portion of file manipulation
5 * subcommands of the "file" command.
7 * Copyright (c) 1996-1998 Sun Microsystems, Inc.
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 * RCS: @(#) $Id: tclWinFCmd.c,v 1.35.2.5 2006/08/30 17:48:48 hobbs Exp $
15 #include "tclWinInt.h"
18 * The following constants specify the type of callback when
19 * TraverseWinTree() calls the traverseProc()
22 #define DOTREE_PRED 1 /* pre-order directory */
23 #define DOTREE_POSTD 2 /* post-order directory */
24 #define DOTREE_F 3 /* regular file */
27 * Callbacks for file attributes code.
30 static int GetWinFileAttributes _ANSI_ARGS_((Tcl_Interp *interp,
31 int objIndex, Tcl_Obj *fileName,
32 Tcl_Obj **attributePtrPtr));
33 static int GetWinFileLongName _ANSI_ARGS_((Tcl_Interp *interp,
34 int objIndex, Tcl_Obj *fileName,
35 Tcl_Obj **attributePtrPtr));
36 static int GetWinFileShortName _ANSI_ARGS_((Tcl_Interp *interp,
37 int objIndex, Tcl_Obj *fileName,
38 Tcl_Obj **attributePtrPtr));
39 static int SetWinFileAttributes _ANSI_ARGS_((Tcl_Interp *interp,
40 int objIndex, Tcl_Obj *fileName,
41 Tcl_Obj *attributePtr));
42 static int CannotSetAttribute _ANSI_ARGS_((Tcl_Interp *interp,
43 int objIndex, Tcl_Obj *fileName,
44 Tcl_Obj *attributePtr));
47 * Constants and variables necessary for file attributes subcommand.
51 WIN_ARCHIVE_ATTRIBUTE,
53 WIN_LONGNAME_ATTRIBUTE,
54 WIN_READONLY_ATTRIBUTE,
55 WIN_SHORTNAME_ATTRIBUTE,
59 static int attributeArray[] = {FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_HIDDEN,
60 0, FILE_ATTRIBUTE_READONLY, 0, FILE_ATTRIBUTE_SYSTEM};
63 CONST char *tclpFileAttrStrings[] = {
64 "-archive", "-hidden", "-longname", "-readonly",
65 "-shortname", "-system", (char *) NULL
68 CONST TclFileAttrProcs tclpFileAttrProcs[] = {
69 {GetWinFileAttributes, SetWinFileAttributes},
70 {GetWinFileAttributes, SetWinFileAttributes},
71 {GetWinFileLongName, CannotSetAttribute},
72 {GetWinFileAttributes, SetWinFileAttributes},
73 {GetWinFileShortName, CannotSetAttribute},
74 {GetWinFileAttributes, SetWinFileAttributes}};
79 * Unlike Borland and Microsoft, we don't register exception handlers
80 * by pushing registration records onto the runtime stack. Instead, we
81 * register them by creating an EXCEPTION_REGISTRATION within the activation
85 typedef struct EXCEPTION_REGISTRATION {
86 struct EXCEPTION_REGISTRATION* link;
87 EXCEPTION_DISPOSITION (*handler)( struct _EXCEPTION_RECORD*, void*,
88 struct _CONTEXT*, void* );
92 } EXCEPTION_REGISTRATION;
97 * Prototype for the TraverseWinTree callback function.
100 typedef int (TraversalProc)(CONST TCHAR *srcPtr, CONST TCHAR *dstPtr,
101 int type, Tcl_DString *errorPtr);
104 * Declarations for local procedures defined in this file:
107 static void StatError(Tcl_Interp *interp, Tcl_Obj *fileName);
108 static int ConvertFileNameFormat(Tcl_Interp *interp,
109 int objIndex, Tcl_Obj *fileName, int longShort,
110 Tcl_Obj **attributePtrPtr);
111 static int DoCopyFile(CONST TCHAR *srcPtr, CONST TCHAR *dstPtr);
112 static int DoCreateDirectory(CONST TCHAR *pathPtr);
113 static int DoRemoveJustDirectory(CONST TCHAR *nativeSrc,
114 int ignoreError, Tcl_DString *errorPtr);
115 static int DoRemoveDirectory(Tcl_DString *pathPtr, int recursive,
116 Tcl_DString *errorPtr);
117 static int DoRenameFile(CONST TCHAR *nativeSrc, CONST TCHAR *dstPtr);
118 static int TraversalCopy(CONST TCHAR *srcPtr, CONST TCHAR *dstPtr,
119 int type, Tcl_DString *errorPtr);
120 static int TraversalDelete(CONST TCHAR *srcPtr, CONST TCHAR *dstPtr,
121 int type, Tcl_DString *errorPtr);
122 static int TraverseWinTree(TraversalProc *traverseProc,
123 Tcl_DString *sourcePtr, Tcl_DString *dstPtr,
124 Tcl_DString *errorPtr);
128 *---------------------------------------------------------------------------
130 * TclpObjRenameFile, DoRenameFile --
132 * Changes the name of an existing file or directory, from src to dst.
133 * If src and dst refer to the same file or directory, does nothing
134 * and returns success. Otherwise if dst already exists, it will be
135 * deleted and replaced by src subject to the following conditions:
136 * If src is a directory, dst may be an empty directory.
137 * If src is a file, dst may be a file.
138 * In any other situation where dst already exists, the rename will
142 * If the file or directory was successfully renamed, returns TCL_OK.
143 * Otherwise the return value is TCL_ERROR and errno is set to
144 * indicate the error. Some possible values for errno are:
146 * ENAMETOOLONG: src or dst names are too long.
147 * EACCES: src or dst parent directory can't be read and/or written.
148 * EEXIST: dst is a non-empty directory.
149 * EINVAL: src is a root directory or dst is a subdirectory of src.
150 * EISDIR: dst is a directory, but src is not.
151 * ENOENT: src doesn't exist. src or dst is "".
152 * ENOTDIR: src is a directory, but dst is not.
153 * EXDEV: src and dst are on different filesystems.
155 * EACCES: exists an open file already referring to src or dst.
156 * EACCES: src or dst specify the current working directory (NT).
157 * EACCES: src specifies a char device (nul:, com1:, etc.)
158 * EEXIST: dst specifies a char device (nul:, com1:, etc.) (NT)
159 * EACCES: dst specifies a char device (nul:, com1:, etc.) (95)
162 * The implementation supports cross-filesystem renames of files,
163 * but the caller should be prepared to emulate cross-filesystem
164 * renames of directories if errno is EXDEV.
166 *---------------------------------------------------------------------------
170 TclpObjRenameFile(srcPathPtr, destPathPtr)
172 Tcl_Obj *destPathPtr;
174 return DoRenameFile(Tcl_FSGetNativePath(srcPathPtr),
175 Tcl_FSGetNativePath(destPathPtr));
180 CONST TCHAR *nativeSrc, /* Pathname of file or dir to be renamed
182 CONST TCHAR *nativeDst) /* New pathname for file or directory
186 EXCEPTION_REGISTRATION registration;
188 DWORD srcAttr, dstAttr;
192 * The MoveFile API acts differently under Win95/98 and NT
193 * WRT NULL and "". Avoid passing these values.
196 if (nativeSrc == NULL || nativeSrc[0] == '\0' ||
197 nativeDst == NULL || nativeDst[0] == '\0') {
198 Tcl_SetErrno(ENOENT);
203 * The MoveFile API would throw an exception under NT
204 * if one of the arguments is a char block device.
209 if ((*tclWinProcs->moveFileProc)(nativeSrc, nativeDst) != FALSE) {
212 } __except (EXCEPTION_EXECUTE_HANDLER) {}
216 * Don't have SEH available, do things the hard way.
217 * Note that this needs to be one block of asm, to avoid stack
218 * imbalance; also, it is illegal for one asm block to contain
222 __asm__ __volatile__ (
224 * Pick up params before messing with the stack */
226 "movl %[nativeDst], %%ebx" "\n\t"
227 "movl %[nativeSrc], %%ecx" "\n\t"
230 * Construct an EXCEPTION_REGISTRATION to protect the
233 "leal %[registration], %%edx" "\n\t"
234 "movl %%fs:0, %%eax" "\n\t"
235 "movl %%eax, 0x0(%%edx)" "\n\t" /* link */
236 "leal 1f, %%eax" "\n\t"
237 "movl %%eax, 0x4(%%edx)" "\n\t" /* handler */
238 "movl %%ebp, 0x8(%%edx)" "\n\t" /* ebp */
239 "movl %%esp, 0xc(%%edx)" "\n\t" /* esp */
240 "movl $0, 0x10(%%edx)" "\n\t" /* status */
242 /* Link the EXCEPTION_REGISTRATION on the chain */
244 "movl %%edx, %%fs:0" "\n\t"
246 /* Call MoveFile( nativeSrc, nativeDst ) */
250 "movl %[moveFile], %%eax" "\n\t"
254 * Come here on normal exit. Recover the EXCEPTION_REGISTRATION
255 * and put the status return from MoveFile into it.
258 "movl %%fs:0, %%edx" "\n\t"
259 "movl %%eax, 0x10(%%edx)" "\n\t"
263 * Come here on an exception. Recover the EXCEPTION_REGISTRATION
267 "movl %%fs:0, %%edx" "\n\t"
268 "movl 0x8(%%edx), %%edx" "\n\t"
271 * Come here however we exited. Restore context from the
272 * EXCEPTION_REGISTRATION in case the stack is unbalanced.
276 "movl 0xc(%%edx), %%esp" "\n\t"
277 "movl 0x8(%%edx), %%ebp" "\n\t"
278 "movl 0x0(%%edx), %%eax" "\n\t"
279 "movl %%eax, %%fs:0" "\n\t"
284 [registration] "m" (registration),
285 [nativeDst] "m" (nativeDst),
286 [nativeSrc] "m" (nativeSrc),
287 [moveFile] "r" (tclWinProcs->moveFileProc)
289 "%eax", "%ebx", "%ecx", "%edx", "memory"
291 if (registration.status != FALSE) {
299 TclWinConvertError(GetLastError());
301 srcAttr = (*tclWinProcs->getFileAttributesProc)(nativeSrc);
302 dstAttr = (*tclWinProcs->getFileAttributesProc)(nativeDst);
303 if (srcAttr == 0xffffffff) {
304 if ((*tclWinProcs->getFullPathNameProc)(nativeSrc, 0, NULL, NULL) >= MAX_PATH) {
305 errno = ENAMETOOLONG;
310 if (dstAttr == 0xffffffff) {
311 if ((*tclWinProcs->getFullPathNameProc)(nativeDst, 0, NULL, NULL) >= MAX_PATH) {
312 errno = ENAMETOOLONG;
318 if (errno == EBADF) {
322 if (errno == EACCES) {
324 if (srcAttr & FILE_ATTRIBUTE_DIRECTORY) {
325 TCHAR *nativeSrcRest, *nativeDstRest;
326 CONST char **srcArgv, **dstArgv;
327 int size, srcArgc, dstArgc;
328 WCHAR nativeSrcPath[MAX_PATH];
329 WCHAR nativeDstPath[MAX_PATH];
330 Tcl_DString srcString, dstString;
331 CONST char *src, *dst;
333 size = (*tclWinProcs->getFullPathNameProc)(nativeSrc, MAX_PATH,
334 nativeSrcPath, &nativeSrcRest);
335 if ((size == 0) || (size > MAX_PATH)) {
338 size = (*tclWinProcs->getFullPathNameProc)(nativeDst, MAX_PATH,
339 nativeDstPath, &nativeDstRest);
340 if ((size == 0) || (size > MAX_PATH)) {
343 (*tclWinProcs->charLowerProc)((TCHAR *) nativeSrcPath);
344 (*tclWinProcs->charLowerProc)((TCHAR *) nativeDstPath);
346 src = Tcl_WinTCharToUtf((TCHAR *) nativeSrcPath, -1, &srcString);
347 dst = Tcl_WinTCharToUtf((TCHAR *) nativeDstPath, -1, &dstString);
348 if (strncmp(src, dst, (size_t) Tcl_DStringLength(&srcString)) == 0) {
350 * Trying to move a directory into itself.
354 Tcl_DStringFree(&srcString);
355 Tcl_DStringFree(&dstString);
358 Tcl_SplitPath(src, &srcArgc, &srcArgv);
359 Tcl_SplitPath(dst, &dstArgc, &dstArgv);
360 Tcl_DStringFree(&srcString);
361 Tcl_DStringFree(&dstString);
365 * They are trying to move a root directory. Whether
366 * or not it is across filesystems, this cannot be
370 Tcl_SetErrno(EINVAL);
371 } else if ((srcArgc > 0) && (dstArgc > 0) &&
372 (strcmp(srcArgv[0], dstArgv[0]) != 0)) {
374 * If src is a directory and dst filesystem != src
375 * filesystem, errno should be EXDEV. It is very
376 * important to get this behavior, so that the caller
377 * can respond to a cross filesystem rename by
378 * simulating it with copy and delete. The MoveFile
379 * system call already handles the case of moving a
380 * file between filesystems.
386 ckfree((char *) srcArgv);
387 ckfree((char *) dstArgv);
391 * Other types of access failure is that dst is a read-only
392 * filesystem, that an open file referred to src or dest, or that
393 * src or dest specified the current working directory on the
394 * current filesystem. EACCES is returned for those cases.
397 } else if (Tcl_GetErrno() == EEXIST) {
399 * Reports EEXIST any time the target already exists. If it makes
400 * sense, remove the old file and try renaming again.
403 if (srcAttr & FILE_ATTRIBUTE_DIRECTORY) {
404 if (dstAttr & FILE_ATTRIBUTE_DIRECTORY) {
406 * Overwrite empty dst directory with src directory. The
407 * following call will remove an empty directory. If it
408 * fails, it's because it wasn't empty.
411 if (DoRemoveJustDirectory(nativeDst, 0, NULL) == TCL_OK) {
413 * Now that that empty directory is gone, we can try
414 * renaming again. If that fails, we'll put this empty
415 * directory back, for completeness.
418 if ((*tclWinProcs->moveFileProc)(nativeSrc, nativeDst) != FALSE) {
423 * Some new error has occurred. Don't know what it
424 * could be, but report this one.
427 TclWinConvertError(GetLastError());
428 (*tclWinProcs->createDirectoryProc)(nativeDst, NULL);
429 (*tclWinProcs->setFileAttributesProc)(nativeDst, dstAttr);
430 if (Tcl_GetErrno() == EACCES) {
432 * Decode the EACCES to a more meaningful error.
438 } else { /* (dstAttr & FILE_ATTRIBUTE_DIRECTORY) == 0 */
439 Tcl_SetErrno(ENOTDIR);
441 } else { /* (srcAttr & FILE_ATTRIBUTE_DIRECTORY) == 0 */
442 if (dstAttr & FILE_ATTRIBUTE_DIRECTORY) {
443 Tcl_SetErrno(EISDIR);
446 * Overwrite existing file by:
448 * 1. Rename existing file to temp name.
449 * 2. Rename old file to new name.
450 * 3. If success, delete temp file. If failure,
451 * put temp file back to old name.
454 TCHAR *nativeRest, *nativeTmp, *nativePrefix;
456 WCHAR tempBuf[MAX_PATH];
458 size = (*tclWinProcs->getFullPathNameProc)(nativeDst, MAX_PATH,
459 tempBuf, &nativeRest);
460 if ((size == 0) || (size > MAX_PATH) || (nativeRest == NULL)) {
463 nativeTmp = (TCHAR *) tempBuf;
464 ((char *) nativeRest)[0] = '\0';
465 ((char *) nativeRest)[1] = '\0'; /* In case it's Unicode. */
468 nativePrefix = (tclWinProcs->useWide)
469 ? (TCHAR *) L"tclr" : (TCHAR *) "tclr";
470 if ((*tclWinProcs->getTempFileNameProc)(nativeTmp,
471 nativePrefix, 0, tempBuf) != 0) {
473 * Strictly speaking, need the following DeleteFile and
474 * MoveFile to be joined as an atomic operation so no
475 * other app comes along in the meantime and creates the
479 nativeTmp = (TCHAR *) tempBuf;
480 (*tclWinProcs->deleteFileProc)(nativeTmp);
481 if ((*tclWinProcs->moveFileProc)(nativeDst, nativeTmp) != FALSE) {
482 if ((*tclWinProcs->moveFileProc)(nativeSrc, nativeDst) != FALSE) {
483 (*tclWinProcs->setFileAttributesProc)(nativeTmp,
484 FILE_ATTRIBUTE_NORMAL);
485 (*tclWinProcs->deleteFileProc)(nativeTmp);
488 (*tclWinProcs->deleteFileProc)(nativeDst);
489 (*tclWinProcs->moveFileProc)(nativeTmp, nativeDst);
494 * Can't backup dst file or move src file. Return that
495 * error. Could happen if an open file refers to dst.
498 TclWinConvertError(GetLastError());
499 if (Tcl_GetErrno() == EACCES) {
501 * Decode the EACCES to a more meaningful error.
515 *---------------------------------------------------------------------------
517 * TclpObjCopyFile, DoCopyFile --
519 * Copy a single file (not a directory). If dst already exists and
520 * is not a directory, it is removed.
523 * If the file was successfully copied, returns TCL_OK. Otherwise
524 * the return value is TCL_ERROR and errno is set to indicate the
525 * error. Some possible values for errno are:
527 * EACCES: src or dst parent directory can't be read and/or written.
528 * EISDIR: src or dst is a directory.
529 * ENOENT: src doesn't exist. src or dst is "".
531 * EACCES: exists an open file already referring to dst (95).
532 * EACCES: src specifies a char device (nul:, com1:, etc.) (NT)
533 * ENOENT: src specifies a char device (nul:, com1:, etc.) (95)
536 * It is not an error to copy to a char device.
538 *---------------------------------------------------------------------------
542 TclpObjCopyFile(srcPathPtr, destPathPtr)
544 Tcl_Obj *destPathPtr;
546 return DoCopyFile(Tcl_FSGetNativePath(srcPathPtr),
547 Tcl_FSGetNativePath(destPathPtr));
552 CONST TCHAR *nativeSrc, /* Pathname of file to be copied (native). */
553 CONST TCHAR *nativeDst) /* Pathname of file to copy to (native). */
556 EXCEPTION_REGISTRATION registration;
561 * The CopyFile API acts differently under Win95/98 and NT
562 * WRT NULL and "". Avoid passing these values.
565 if (nativeSrc == NULL || nativeSrc[0] == '\0' ||
566 nativeDst == NULL || nativeDst[0] == '\0') {
567 Tcl_SetErrno(ENOENT);
572 * The CopyFile API would throw an exception under NT if one
573 * of the arguments is a char block device.
578 if ((*tclWinProcs->copyFileProc)(nativeSrc, nativeDst, 0) != FALSE) {
581 } __except (EXCEPTION_EXECUTE_HANDLER) {}
585 * Don't have SEH available, do things the hard way.
586 * Note that this needs to be one block of asm, to avoid stack
587 * imbalance; also, it is illegal for one asm block to contain
591 __asm__ __volatile__ (
594 * Pick up parameters before messing with the stack
597 "movl %[nativeDst], %%ebx" "\n\t"
598 "movl %[nativeSrc], %%ecx" "\n\t"
600 * Construct an EXCEPTION_REGISTRATION to protect the
603 "leal %[registration], %%edx" "\n\t"
604 "movl %%fs:0, %%eax" "\n\t"
605 "movl %%eax, 0x0(%%edx)" "\n\t" /* link */
606 "leal 1f, %%eax" "\n\t"
607 "movl %%eax, 0x4(%%edx)" "\n\t" /* handler */
608 "movl %%ebp, 0x8(%%edx)" "\n\t" /* ebp */
609 "movl %%esp, 0xc(%%edx)" "\n\t" /* esp */
610 "movl $0, 0x10(%%edx)" "\n\t" /* status */
612 /* Link the EXCEPTION_REGISTRATION on the chain */
614 "movl %%edx, %%fs:0" "\n\t"
616 /* Call CopyFile( nativeSrc, nativeDst, 0 ) */
618 "movl %[copyFile], %%eax" "\n\t"
625 * Come here on normal exit. Recover the EXCEPTION_REGISTRATION
626 * and put the status return from CopyFile into it.
629 "movl %%fs:0, %%edx" "\n\t"
630 "movl %%eax, 0x10(%%edx)" "\n\t"
634 * Come here on an exception. Recover the EXCEPTION_REGISTRATION
638 "movl %%fs:0, %%edx" "\n\t"
639 "movl 0x8(%%edx), %%edx" "\n\t"
642 * Come here however we exited. Restore context from the
643 * EXCEPTION_REGISTRATION in case the stack is unbalanced.
647 "movl 0xc(%%edx), %%esp" "\n\t"
648 "movl 0x8(%%edx), %%ebp" "\n\t"
649 "movl 0x0(%%edx), %%eax" "\n\t"
650 "movl %%eax, %%fs:0" "\n\t"
655 [registration] "m" (registration),
656 [nativeDst] "m" (nativeDst),
657 [nativeSrc] "m" (nativeSrc),
658 [copyFile] "r" (tclWinProcs->copyFileProc)
660 "%eax", "%ebx", "%ecx", "%edx", "memory"
662 if (registration.status != FALSE) {
670 TclWinConvertError(GetLastError());
671 if (Tcl_GetErrno() == EBADF) {
672 Tcl_SetErrno(EACCES);
675 if (Tcl_GetErrno() == EACCES) {
676 DWORD srcAttr, dstAttr;
678 srcAttr = (*tclWinProcs->getFileAttributesProc)(nativeSrc);
679 dstAttr = (*tclWinProcs->getFileAttributesProc)(nativeDst);
680 if (srcAttr != 0xffffffff) {
681 if (dstAttr == 0xffffffff) {
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) {
692 Tcl_SetErrno(EISDIR);
694 if (dstAttr & FILE_ATTRIBUTE_READONLY) {
695 (*tclWinProcs->setFileAttributesProc)(nativeDst,
696 dstAttr & ~((DWORD)FILE_ATTRIBUTE_READONLY));
697 if ((*tclWinProcs->copyFileProc)(nativeSrc, nativeDst, 0) != FALSE) {
701 * Still can't copy onto dst. Return that error, and
702 * restore attributes of dst.
705 TclWinConvertError(GetLastError());
706 (*tclWinProcs->setFileAttributesProc)(nativeDst, dstAttr);
714 *---------------------------------------------------------------------------
716 * TclpObjDeleteFile, TclpDeleteFile --
718 * Removes a single file (not a directory).
721 * If the file was successfully deleted, returns TCL_OK. Otherwise
722 * the return value is TCL_ERROR and errno is set to indicate the
723 * error. Some possible values for errno are:
725 * EACCES: a parent directory can't be read and/or written.
726 * EISDIR: path is a directory.
727 * ENOENT: path doesn't exist or is "".
729 * EACCES: exists an open file already referring to path.
730 * EACCES: path is a char device (nul:, com1:, etc.)
733 * The file is deleted, even if it is read-only.
735 *---------------------------------------------------------------------------
739 TclpObjDeleteFile(pathPtr)
742 return TclpDeleteFile(Tcl_FSGetNativePath(pathPtr));
747 CONST TCHAR *nativePath) /* Pathname of file to be removed (native). */
752 * The DeleteFile API acts differently under Win95/98 and NT
753 * WRT NULL and "". Avoid passing these values.
756 if (nativePath == NULL || nativePath[0] == '\0') {
757 Tcl_SetErrno(ENOENT);
761 if ((*tclWinProcs->deleteFileProc)(nativePath) != FALSE) {
764 TclWinConvertError(GetLastError());
766 if (Tcl_GetErrno() == EACCES) {
767 attr = (*tclWinProcs->getFileAttributesProc)(nativePath);
768 if (attr != 0xffffffff) {
769 if (attr & FILE_ATTRIBUTE_DIRECTORY) {
770 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
771 /* It is a symbolic link -- remove it */
772 if (TclWinSymLinkDelete(nativePath, 0) == 0) {
778 * If we fall through here, it is a directory.
780 * Windows NT reports removing a directory as EACCES instead
784 Tcl_SetErrno(EISDIR);
785 } else if (attr & FILE_ATTRIBUTE_READONLY) {
786 int res = (*tclWinProcs->setFileAttributesProc)(nativePath,
787 attr & ~((DWORD)FILE_ATTRIBUTE_READONLY));
788 if ((res != 0) && ((*tclWinProcs->deleteFileProc)(nativePath)
792 TclWinConvertError(GetLastError());
794 (*tclWinProcs->setFileAttributesProc)(nativePath, attr);
798 } else if (Tcl_GetErrno() == ENOENT) {
799 attr = (*tclWinProcs->getFileAttributesProc)(nativePath);
800 if (attr != 0xffffffff) {
801 if (attr & FILE_ATTRIBUTE_DIRECTORY) {
803 * Windows 95 reports removing a directory as ENOENT instead
807 Tcl_SetErrno(EISDIR);
810 } else if (Tcl_GetErrno() == EINVAL) {
812 * Windows NT reports removing a char device as EINVAL instead of
816 Tcl_SetErrno(EACCES);
823 *---------------------------------------------------------------------------
825 * TclpObjCreateDirectory --
827 * Creates the specified directory. All parent directories of the
828 * specified directory must already exist. The directory is
829 * automatically created with permissions so that user can access
830 * the new directory and create new files or subdirectories in it.
833 * If the directory was successfully created, returns TCL_OK.
834 * Otherwise the return value is TCL_ERROR and errno is set to
835 * indicate the error. Some possible values for errno are:
837 * EACCES: a parent directory can't be read and/or written.
838 * EEXIST: path already exists.
839 * ENOENT: a parent directory doesn't exist.
842 * A directory is created.
844 *---------------------------------------------------------------------------
848 TclpObjCreateDirectory(pathPtr)
851 return DoCreateDirectory(Tcl_FSGetNativePath(pathPtr));
856 CONST TCHAR *nativePath) /* Pathname of directory to create (native). */
859 if ((*tclWinProcs->createDirectoryProc)(nativePath, NULL) == 0) {
860 error = GetLastError();
861 TclWinConvertError(error);
868 *---------------------------------------------------------------------------
870 * TclpObjCopyDirectory --
872 * Recursively copies a directory. The target directory dst must
873 * not already exist. Note that this function does not merge two
874 * directory hierarchies, even if the target directory is an an
878 * If the directory was successfully copied, returns TCL_OK.
879 * Otherwise the return value is TCL_ERROR, errno is set to indicate
880 * the error, and the pathname of the file that caused the error
881 * is stored in errorPtr. See TclpCreateDirectory and TclpCopyFile
882 * for a description of possible values for errno.
885 * An exact copy of the directory hierarchy src will be created
886 * with the name dst. If an error occurs, the error will
887 * be returned immediately, and remaining files will not be
890 *---------------------------------------------------------------------------
894 TclpObjCopyDirectory(srcPathPtr, destPathPtr, errorPtr)
896 Tcl_Obj *destPathPtr;
900 Tcl_DString srcString, dstString;
901 Tcl_Obj *normSrcPtr, *normDestPtr;
904 normSrcPtr = Tcl_FSGetNormalizedPath(NULL,srcPathPtr);
905 if (normSrcPtr == NULL) {
908 Tcl_WinUtfToTChar(Tcl_GetString(normSrcPtr), -1, &srcString);
909 normDestPtr = Tcl_FSGetNormalizedPath(NULL,destPathPtr);
910 if (normDestPtr == NULL) {
913 Tcl_WinUtfToTChar(Tcl_GetString(normDestPtr), -1, &dstString);
915 ret = TraverseWinTree(TraversalCopy, &srcString, &dstString, &ds);
917 Tcl_DStringFree(&srcString);
918 Tcl_DStringFree(&dstString);
921 if (!strcmp(Tcl_DStringValue(&ds), Tcl_GetString(normSrcPtr))) {
922 *errorPtr = srcPathPtr;
923 } else if (!strcmp(Tcl_DStringValue(&ds), Tcl_GetString(normDestPtr))) {
924 *errorPtr = destPathPtr;
926 *errorPtr = Tcl_NewStringObj(Tcl_DStringValue(&ds), -1);
928 Tcl_DStringFree(&ds);
929 Tcl_IncrRefCount(*errorPtr);
935 *----------------------------------------------------------------------
937 * TclpObjRemoveDirectory, DoRemoveDirectory --
939 * Removes directory (and its contents, if the recursive flag is set).
942 * If the directory was successfully removed, returns TCL_OK.
943 * Otherwise the return value is TCL_ERROR, errno is set to indicate
944 * the error, and the pathname of the file that caused the error
945 * is stored in errorPtr. Some possible values for errno are:
947 * EACCES: path directory can't be read and/or written.
948 * EEXIST: path is a non-empty directory.
949 * EINVAL: path is root directory or current directory.
950 * ENOENT: path doesn't exist or is "".
951 * ENOTDIR: path is not a directory.
953 * EACCES: path is a char device (nul:, com1:, etc.) (95)
954 * EINVAL: path is a char device (nul:, com1:, etc.) (NT)
957 * Directory removed. If an error occurs, the error will be returned
958 * immediately, and remaining files will not be deleted.
960 *----------------------------------------------------------------------
964 TclpObjRemoveDirectory(pathPtr, recursive, errorPtr)
970 Tcl_Obj *normPtr = NULL;
974 * In the recursive case, the string rep is used to construct a
975 * Tcl_DString which may be used extensively, so we can't
976 * optimize this case easily.
979 normPtr = Tcl_FSGetNormalizedPath(NULL, pathPtr);
980 if (normPtr == NULL) {
983 Tcl_WinUtfToTChar(Tcl_GetString(normPtr), -1, &native);
984 ret = DoRemoveDirectory(&native, recursive, &ds);
985 Tcl_DStringFree(&native);
987 ret = DoRemoveJustDirectory(Tcl_FSGetNativePath(pathPtr),
991 int len = Tcl_DStringLength(&ds);
994 && !strcmp(Tcl_DStringValue(&ds), Tcl_GetString(normPtr))) {
997 *errorPtr = Tcl_NewStringObj(Tcl_DStringValue(&ds), -1);
999 Tcl_IncrRefCount(*errorPtr);
1001 Tcl_DStringFree(&ds);
1007 DoRemoveJustDirectory(
1008 CONST TCHAR *nativePath, /* Pathname of directory to be removed
1010 int ignoreError, /* If non-zero, don't initialize the
1011 * errorPtr under some circumstances
1013 Tcl_DString *errorPtr) /* If non-NULL, uninitialized or free
1014 * DString filled with UTF-8 name of file
1018 * The RemoveDirectory API acts differently under Win95/98 and NT
1019 * WRT NULL and "". Avoid passing these values.
1022 if (nativePath == NULL || nativePath[0] == '\0') {
1023 Tcl_SetErrno(ENOENT);
1027 if ((*tclWinProcs->removeDirectoryProc)(nativePath) != FALSE) {
1030 TclWinConvertError(GetLastError());
1032 if (Tcl_GetErrno() == EACCES) {
1033 DWORD attr = (*tclWinProcs->getFileAttributesProc)(nativePath);
1034 if (attr != 0xffffffff) {
1035 if ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0) {
1037 * Windows 95 reports calling RemoveDirectory on a file as an
1038 * EACCES, not an ENOTDIR.
1041 Tcl_SetErrno(ENOTDIR);
1045 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
1046 /* It is a symbolic link -- remove it */
1047 if (TclWinSymLinkDelete(nativePath, 1) != 0) {
1052 if (attr & FILE_ATTRIBUTE_READONLY) {
1053 attr &= ~FILE_ATTRIBUTE_READONLY;
1054 if ((*tclWinProcs->setFileAttributesProc)(nativePath, attr) == FALSE) {
1057 if ((*tclWinProcs->removeDirectoryProc)(nativePath) != FALSE) {
1060 TclWinConvertError(GetLastError());
1061 (*tclWinProcs->setFileAttributesProc)(nativePath,
1062 attr | FILE_ATTRIBUTE_READONLY);
1066 * Windows 95 and Win32s report removing a non-empty directory
1067 * as EACCES, not EEXIST. If the directory is not empty,
1068 * change errno so caller knows what's going on.
1071 if (TclWinGetPlatformId() != VER_PLATFORM_WIN32_NT) {
1072 CONST char *path, *find;
1074 WIN32_FIND_DATAA data;
1078 path = (CONST char *) nativePath;
1080 Tcl_DStringInit(&buffer);
1082 find = Tcl_DStringAppend(&buffer, path, len);
1083 if ((len > 0) && (find[len - 1] != '\\')) {
1084 Tcl_DStringAppend(&buffer, "\\", 1);
1086 find = Tcl_DStringAppend(&buffer, "*.*", 3);
1087 handle = FindFirstFileA(find, &data);
1088 if (handle != INVALID_HANDLE_VALUE) {
1090 if ((strcmp(data.cFileName, ".") != 0)
1091 && (strcmp(data.cFileName, "..") != 0)) {
1093 * Found something in this directory.
1096 Tcl_SetErrno(EEXIST);
1099 if (FindNextFileA(handle, &data) == FALSE) {
1105 Tcl_DStringFree(&buffer);
1109 if (Tcl_GetErrno() == ENOTEMPTY) {
1111 * The caller depends on EEXIST to signify that the directory is
1112 * not empty, not ENOTEMPTY.
1115 Tcl_SetErrno(EEXIST);
1117 if ((ignoreError != 0) && (Tcl_GetErrno() == EEXIST)) {
1119 * If we're being recursive, this error may actually
1120 * be ok, so we don't want to initialise the errorPtr
1127 if (errorPtr != NULL) {
1128 Tcl_WinTCharToUtf(nativePath, -1, errorPtr);
1136 Tcl_DString *pathPtr, /* Pathname of directory to be removed
1138 int recursive, /* If non-zero, removes directories that
1139 * are nonempty. Otherwise, will only remove
1140 * empty directories. */
1141 Tcl_DString *errorPtr) /* If non-NULL, uninitialized or free
1142 * DString filled with UTF-8 name of file
1145 int res = DoRemoveJustDirectory(Tcl_DStringValue(pathPtr), recursive,
1148 if ((res == TCL_ERROR) && (recursive != 0) && (Tcl_GetErrno() == EEXIST)) {
1150 * The directory is nonempty, but the recursive flag has been
1151 * specified, so we recursively remove all the files in the directory.
1153 return TraverseWinTree(TraversalDelete, pathPtr, NULL, errorPtr);
1160 *---------------------------------------------------------------------------
1162 * TraverseWinTree --
1164 * Traverse directory tree specified by sourcePtr, calling the function
1165 * traverseProc for each file and directory encountered. If destPtr
1166 * is non-null, each of name in the sourcePtr directory is appended to
1167 * the directory specified by destPtr and passed as the second argument
1168 * to traverseProc() .
1171 * Standard Tcl result.
1174 * None caused by TraverseWinTree, however the user specified
1175 * traverseProc() may change state. If an error occurs, the error will
1176 * be returned immediately, and remaining files will not be processed.
1178 *---------------------------------------------------------------------------
1183 TraversalProc *traverseProc,/* Function to call for every file and
1184 * directory in source hierarchy. */
1185 Tcl_DString *sourcePtr, /* Pathname of source directory to be
1186 * traversed (native). */
1187 Tcl_DString *targetPtr, /* Pathname of directory to traverse in
1188 * parallel with source directory (native),
1190 Tcl_DString *errorPtr) /* If non-NULL, uninitialized or free
1191 * DString filled with UTF-8 name of file
1195 TCHAR *nativeSource, *nativeTarget, *nativeErrfile;
1196 int result, found, sourceLen, targetLen, oldSourceLen, oldTargetLen;
1198 WIN32_FIND_DATAT data;
1200 nativeErrfile = NULL;
1202 oldTargetLen = 0; /* lint. */
1204 nativeSource = (TCHAR *) Tcl_DStringValue(sourcePtr);
1205 nativeTarget = (TCHAR *) (targetPtr == NULL
1206 ? NULL : Tcl_DStringValue(targetPtr));
1208 oldSourceLen = Tcl_DStringLength(sourcePtr);
1209 sourceAttr = (*tclWinProcs->getFileAttributesProc)(nativeSource);
1210 if (sourceAttr == 0xffffffff) {
1211 nativeErrfile = nativeSource;
1214 if ((sourceAttr & FILE_ATTRIBUTE_DIRECTORY) == 0) {
1216 * Process the regular file
1219 return (*traverseProc)(nativeSource, nativeTarget, DOTREE_F, errorPtr);
1222 if (tclWinProcs->useWide) {
1223 Tcl_DStringAppend(sourcePtr, (char *) L"\\*.*", 4 * sizeof(WCHAR) + 1);
1224 Tcl_DStringSetLength(sourcePtr, Tcl_DStringLength(sourcePtr) - 1);
1226 Tcl_DStringAppend(sourcePtr, "\\*.*", 4);
1228 nativeSource = (TCHAR *) Tcl_DStringValue(sourcePtr);
1229 handle = (*tclWinProcs->findFirstFileProc)(nativeSource, &data);
1230 if (handle == INVALID_HANDLE_VALUE) {
1232 * Can't read directory
1235 TclWinConvertError(GetLastError());
1236 nativeErrfile = nativeSource;
1240 nativeSource[oldSourceLen + 1] = '\0';
1241 Tcl_DStringSetLength(sourcePtr, oldSourceLen);
1242 result = (*traverseProc)(nativeSource, nativeTarget, DOTREE_PRED, errorPtr);
1243 if (result != TCL_OK) {
1248 sourceLen = oldSourceLen;
1250 if (tclWinProcs->useWide) {
1251 sourceLen += sizeof(WCHAR);
1252 Tcl_DStringAppend(sourcePtr, (char *) L"\\", sizeof(WCHAR) + 1);
1253 Tcl_DStringSetLength(sourcePtr, sourceLen);
1256 Tcl_DStringAppend(sourcePtr, "\\", 1);
1258 if (targetPtr != NULL) {
1259 oldTargetLen = Tcl_DStringLength(targetPtr);
1261 targetLen = oldTargetLen;
1262 if (tclWinProcs->useWide) {
1263 targetLen += sizeof(WCHAR);
1264 Tcl_DStringAppend(targetPtr, (char *) L"\\", sizeof(WCHAR) + 1);
1265 Tcl_DStringSetLength(targetPtr, targetLen);
1268 Tcl_DStringAppend(targetPtr, "\\", 1);
1273 for ( ; found; found = (*tclWinProcs->findNextFileProc)(handle, &data)) {
1277 if (tclWinProcs->useWide) {
1280 wp = data.w.cFileName;
1290 nativeName = (TCHAR *) data.w.cFileName;
1291 len = wcslen(data.w.cFileName) * sizeof(WCHAR);
1293 if ((strcmp(data.a.cFileName, ".") == 0)
1294 || (strcmp(data.a.cFileName, "..") == 0)) {
1297 nativeName = (TCHAR *) data.a.cFileName;
1298 len = strlen(data.a.cFileName);
1302 * Append name after slash, and recurse on the file.
1305 Tcl_DStringAppend(sourcePtr, (char *) nativeName, len + 1);
1306 Tcl_DStringSetLength(sourcePtr, Tcl_DStringLength(sourcePtr) - 1);
1307 if (targetPtr != NULL) {
1308 Tcl_DStringAppend(targetPtr, (char *) nativeName, len + 1);
1309 Tcl_DStringSetLength(targetPtr, Tcl_DStringLength(targetPtr) - 1);
1311 result = TraverseWinTree(traverseProc, sourcePtr, targetPtr,
1313 if (result != TCL_OK) {
1318 * Remove name after slash.
1321 Tcl_DStringSetLength(sourcePtr, sourceLen);
1322 if (targetPtr != NULL) {
1323 Tcl_DStringSetLength(targetPtr, targetLen);
1329 * Strip off the trailing slash we added
1332 Tcl_DStringSetLength(sourcePtr, oldSourceLen + 1);
1333 Tcl_DStringSetLength(sourcePtr, oldSourceLen);
1334 if (targetPtr != NULL) {
1335 Tcl_DStringSetLength(targetPtr, oldTargetLen + 1);
1336 Tcl_DStringSetLength(targetPtr, oldTargetLen);
1338 if (result == TCL_OK) {
1340 * Call traverseProc() on a directory after visiting all the
1341 * files in that directory.
1344 result = (*traverseProc)(Tcl_DStringValue(sourcePtr),
1345 (targetPtr == NULL ? NULL : Tcl_DStringValue(targetPtr)),
1346 DOTREE_POSTD, errorPtr);
1349 if (nativeErrfile != NULL) {
1350 TclWinConvertError(GetLastError());
1351 if (errorPtr != NULL) {
1352 Tcl_WinTCharToUtf(nativeErrfile, -1, errorPtr);
1361 *----------------------------------------------------------------------
1365 * Called from TraverseUnixTree in order to execute a recursive
1366 * copy of a directory.
1369 * Standard Tcl result.
1372 * Depending on the value of type, src may be copied to dst.
1374 *----------------------------------------------------------------------
1379 CONST TCHAR *nativeSrc, /* Source pathname to copy. */
1380 CONST TCHAR *nativeDst, /* Destination pathname of copy. */
1381 int type, /* Reason for call - see TraverseWinTree() */
1382 Tcl_DString *errorPtr) /* If non-NULL, initialized DString filled
1383 * with UTF-8 name of file causing error. */
1387 if (DoCopyFile(nativeSrc, nativeDst) == TCL_OK) {
1393 if (DoCreateDirectory(nativeDst) == TCL_OK) {
1394 DWORD attr = (*tclWinProcs->getFileAttributesProc)(nativeSrc);
1395 if ((*tclWinProcs->setFileAttributesProc)(nativeDst, attr) != FALSE) {
1398 TclWinConvertError(GetLastError());
1402 case DOTREE_POSTD: {
1408 * There shouldn't be a problem with src, because we already
1409 * checked it to get here.
1412 if (errorPtr != NULL) {
1413 Tcl_WinTCharToUtf(nativeDst, -1, errorPtr);
1419 *----------------------------------------------------------------------
1421 * TraversalDelete --
1423 * Called by procedure TraverseWinTree for every file and
1424 * directory that it encounters in a directory hierarchy. This
1425 * procedure unlinks files, and removes directories after all the
1426 * containing files have been processed.
1429 * Standard Tcl result.
1432 * Files or directory specified by src will be deleted. If an
1433 * error occurs, the windows error is converted to a Posix error
1434 * and errno is set accordingly.
1436 *----------------------------------------------------------------------
1441 CONST TCHAR *nativeSrc, /* Source pathname to delete. */
1442 CONST TCHAR *dstPtr, /* Not used. */
1443 int type, /* Reason for call - see TraverseWinTree() */
1444 Tcl_DString *errorPtr) /* If non-NULL, initialized DString filled
1445 * with UTF-8 name of file causing error. */
1449 if (TclpDeleteFile(nativeSrc) == TCL_OK) {
1457 case DOTREE_POSTD: {
1458 if (DoRemoveJustDirectory(nativeSrc, 0, NULL) == TCL_OK) {
1465 if (errorPtr != NULL) {
1466 Tcl_WinTCharToUtf(nativeSrc, -1, errorPtr);
1472 *----------------------------------------------------------------------
1476 * Sets the object result with the appropriate error.
1482 * The interp's object result is set with an error message
1483 * based on the objIndex, fileName and errno.
1485 *----------------------------------------------------------------------
1490 Tcl_Interp *interp, /* The interp that has the error */
1491 Tcl_Obj *fileName) /* The name of the file which caused the
1494 TclWinConvertError(GetLastError());
1495 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
1496 "could not read \"", Tcl_GetString(fileName),
1497 "\": ", Tcl_PosixError(interp),
1502 *----------------------------------------------------------------------
1504 * GetWinFileAttributes --
1506 * Returns a Tcl_Obj containing the value of a file attribute.
1507 * This routine gets the -hidden, -readonly or -system attribute.
1510 * Standard Tcl result and a Tcl_Obj in attributePtrPtr. The object
1511 * will have ref count 0. If the return value is not TCL_OK,
1512 * attributePtrPtr is not touched.
1515 * A new object is allocated if the file is valid.
1517 *----------------------------------------------------------------------
1521 GetWinFileAttributes(
1522 Tcl_Interp *interp, /* The interp we are using for errors. */
1523 int objIndex, /* The index of the attribute. */
1524 Tcl_Obj *fileName, /* The name of the file. */
1525 Tcl_Obj **attributePtrPtr) /* A pointer to return the object with. */
1528 CONST TCHAR *nativeName;
1531 nativeName = Tcl_FSGetNativePath(fileName);
1532 result = (*tclWinProcs->getFileAttributesProc)(nativeName);
1534 if (result == 0xffffffff) {
1535 StatError(interp, fileName);
1539 attr = (int)(result & attributeArray[objIndex]);
1540 if ((objIndex == WIN_HIDDEN_ATTRIBUTE) && (attr != 0)) {
1542 * It is hidden. However there is a bug on some Windows
1543 * OSes in which root volumes (drives) formatted as NTFS
1544 * are declared hidden when they are not (and cannot be).
1546 * We test for, and fix that case, here.
1549 char *str = Tcl_GetStringFromObj(fileName,&len);
1553 * Not sure if this is possible, but we pass it on
1556 } else if (len == 1 && (str[0] == '/' || str[0] == '\\')) {
1557 /* Path is pointing to the root volume */
1559 } else if ((str[1] == ':')
1560 && (len == 2 || (str[2] == '/' || str[2] == '\\'))) {
1561 /* Path is of the form 'x:' or 'x:/' or 'x:\' */
1566 *attributePtrPtr = Tcl_NewBooleanObj(attr);
1571 *----------------------------------------------------------------------
1573 * ConvertFileNameFormat --
1575 * Returns a Tcl_Obj containing either the long or short version of the
1579 * Standard Tcl result and a Tcl_Obj in attributePtrPtr. The object
1580 * will have ref count 0. If the return value is not TCL_OK,
1581 * attributePtrPtr is not touched.
1583 * Warning: if you pass this function a drive name like 'c:' it
1584 * will actually return the current working directory on that
1585 * drive. To avoid this, make sure the drive name ends in a
1586 * slash, like this 'c:/'.
1589 * A new object is allocated if the file is valid.
1591 *----------------------------------------------------------------------
1595 ConvertFileNameFormat(
1596 Tcl_Interp *interp, /* The interp we are using for errors. */
1597 int objIndex, /* The index of the attribute. */
1598 Tcl_Obj *fileName, /* The name of the file. */
1599 int longShort, /* 0 to short name, 1 to long name. */
1600 Tcl_Obj **attributePtrPtr) /* A pointer to return the object with. */
1604 int result = TCL_OK;
1606 splitPath = Tcl_FSSplitPath(fileName, &pathc);
1608 if (splitPath == NULL || pathc == 0) {
1609 if (interp != NULL) {
1610 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
1611 "could not read \"", Tcl_GetString(fileName),
1612 "\": no such file or directory",
1619 for (i = 0; i < pathc; i++) {
1623 Tcl_ListObjIndex(NULL, splitPath, i, &elt);
1625 pathv = Tcl_GetStringFromObj(elt, &pathLen);
1626 if ((pathv[0] == '/')
1627 || ((pathLen == 3) && (pathv[1] == ':'))
1628 || (strcmp(pathv, ".") == 0)
1629 || (strcmp(pathv, "..") == 0)) {
1631 * Handle "/", "//machine/export", "c:/", "." or ".." by just
1632 * copying the string literally. Uppercase the drive letter,
1633 * just because it looks better under Windows to do so.
1637 /* Here we are modifying the string representation in place */
1638 /* I believe this is legal, since this won't affect any
1639 * file representation this thing may have. */
1640 pathv[0] = (char) Tcl_UniCharToUpper(UCHAR(pathv[0]));
1648 WIN32_FIND_DATAT data;
1652 tempPath = Tcl_FSJoinPath(splitPath, i+1);
1653 Tcl_IncrRefCount(tempPath);
1655 * We'd like to call Tcl_FSGetNativePath(tempPath)
1656 * but that is likely to lead to infinite loops
1658 Tcl_DStringInit(&ds);
1659 tempString = Tcl_GetStringFromObj(tempPath,&tempLen);
1660 nativeName = Tcl_WinUtfToTChar(tempString, tempLen, &ds);
1661 Tcl_DecrRefCount(tempPath);
1662 handle = (*tclWinProcs->findFirstFileProc)(nativeName, &data);
1663 if (handle == INVALID_HANDLE_VALUE) {
1665 * FindFirstFile() doesn't like root directories. We
1666 * would only get a root directory here if the caller
1667 * specified "c:" or "c:." and the current directory on the
1668 * drive was the root directory
1671 attr = (*tclWinProcs->getFileAttributesProc)(nativeName);
1672 if ((attr != 0xFFFFFFFF) && (attr & FILE_ATTRIBUTE_DIRECTORY)) {
1673 Tcl_DStringFree(&ds);
1678 if (handle == INVALID_HANDLE_VALUE) {
1679 Tcl_DStringFree(&ds);
1680 if (interp != NULL) {
1681 StatError(interp, fileName);
1686 if (tclWinProcs->useWide) {
1687 nativeName = (TCHAR *) data.w.cAlternateFileName;
1689 if (data.w.cFileName[0] != '\0') {
1690 nativeName = (TCHAR *) data.w.cFileName;
1693 if (data.w.cAlternateFileName[0] == '\0') {
1694 nativeName = (TCHAR *) data.w.cFileName;
1698 nativeName = (TCHAR *) data.a.cAlternateFileName;
1700 if (data.a.cFileName[0] != '\0') {
1701 nativeName = (TCHAR *) data.a.cFileName;
1704 if (data.a.cAlternateFileName[0] == '\0') {
1705 nativeName = (TCHAR *) data.a.cFileName;
1711 * Purify reports a extraneous UMR in Tcl_WinTCharToUtf() trying
1712 * to dereference nativeName as a Unicode string. I have proven
1713 * to myself that purify is wrong by running the following
1714 * example when nativeName == data.w.cAlternateFileName and
1715 * noting that purify doesn't complain about the first line,
1716 * but does complain about the second.
1718 * fprintf(stderr, "%d\n", data.w.cAlternateFileName[0]);
1719 * fprintf(stderr, "%d\n", ((WCHAR *) nativeName)[0]);
1722 Tcl_DStringInit(&dsTemp);
1723 Tcl_WinTCharToUtf(nativeName, -1, &dsTemp);
1724 /* Deal with issues of tildes being absolute */
1725 if (Tcl_DStringValue(&dsTemp)[0] == '~') {
1726 tempPath = Tcl_NewStringObj("./",2);
1727 Tcl_AppendToObj(tempPath, Tcl_DStringValue(&dsTemp),
1728 Tcl_DStringLength(&dsTemp));
1730 tempPath = Tcl_NewStringObj(Tcl_DStringValue(&dsTemp),
1731 Tcl_DStringLength(&dsTemp));
1733 Tcl_ListObjReplace(NULL, splitPath, i, 1, 1, &tempPath);
1734 Tcl_DStringFree(&ds);
1735 Tcl_DStringFree(&dsTemp);
1740 *attributePtrPtr = Tcl_FSJoinPath(splitPath, -1);
1743 if (splitPath != NULL) {
1744 Tcl_DecrRefCount(splitPath);
1751 *----------------------------------------------------------------------
1753 * GetWinFileLongName --
1755 * Returns a Tcl_Obj containing the long version of the file
1759 * Standard Tcl result and a Tcl_Obj in attributePtrPtr. The object
1760 * will have ref count 0. If the return value is not TCL_OK,
1761 * attributePtrPtr is not touched.
1764 * A new object is allocated if the file is valid.
1766 *----------------------------------------------------------------------
1771 Tcl_Interp *interp, /* The interp we are using for errors. */
1772 int objIndex, /* The index of the attribute. */
1773 Tcl_Obj *fileName, /* The name of the file. */
1774 Tcl_Obj **attributePtrPtr) /* A pointer to return the object with. */
1776 return ConvertFileNameFormat(interp, objIndex, fileName, 1, attributePtrPtr);
1780 *----------------------------------------------------------------------
1782 * GetWinFileShortName --
1784 * Returns a Tcl_Obj containing the short version of the file
1788 * Standard Tcl result and a Tcl_Obj in attributePtrPtr. The object
1789 * will have ref count 0. If the return value is not TCL_OK,
1790 * attributePtrPtr is not touched.
1793 * A new object is allocated if the file is valid.
1795 *----------------------------------------------------------------------
1799 GetWinFileShortName(
1800 Tcl_Interp *interp, /* The interp we are using for errors. */
1801 int objIndex, /* The index of the attribute. */
1802 Tcl_Obj *fileName, /* The name of the file. */
1803 Tcl_Obj **attributePtrPtr) /* A pointer to return the object with. */
1805 return ConvertFileNameFormat(interp, objIndex, fileName, 0, attributePtrPtr);
1809 *----------------------------------------------------------------------
1811 * SetWinFileAttributes --
1813 * Set the file attributes to the value given by attributePtr.
1814 * This routine sets the -hidden, -readonly, or -system attributes.
1817 * Standard TCL error.
1820 * The file's attribute is set.
1822 *----------------------------------------------------------------------
1826 SetWinFileAttributes(
1827 Tcl_Interp *interp, /* The interp we are using for errors. */
1828 int objIndex, /* The index of the attribute. */
1829 Tcl_Obj *fileName, /* The name of the file. */
1830 Tcl_Obj *attributePtr) /* The new value of the attribute. */
1832 DWORD fileAttributes;
1835 CONST TCHAR *nativeName;
1837 nativeName = Tcl_FSGetNativePath(fileName);
1838 fileAttributes = (*tclWinProcs->getFileAttributesProc)(nativeName);
1840 if (fileAttributes == 0xffffffff) {
1841 StatError(interp, fileName);
1845 result = Tcl_GetBooleanFromObj(interp, attributePtr, &yesNo);
1846 if (result != TCL_OK) {
1851 fileAttributes |= (attributeArray[objIndex]);
1853 fileAttributes &= ~(attributeArray[objIndex]);
1856 if (!(*tclWinProcs->setFileAttributesProc)(nativeName, fileAttributes)) {
1857 StatError(interp, fileName);
1865 *----------------------------------------------------------------------
1867 * SetWinFileLongName --
1869 * The attribute in question is a readonly attribute and cannot
1876 * The object result is set to a pertinent error message.
1878 *----------------------------------------------------------------------
1883 Tcl_Interp *interp, /* The interp we are using for errors. */
1884 int objIndex, /* The index of the attribute. */
1885 Tcl_Obj *fileName, /* The name of the file. */
1886 Tcl_Obj *attributePtr) /* The new value of the attribute. */
1888 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
1889 "cannot set attribute \"", tclpFileAttrStrings[objIndex],
1890 "\" for file \"", Tcl_GetString(fileName),
1891 "\": attribute is readonly",
1898 *---------------------------------------------------------------------------
1900 * TclpObjListVolumes --
1902 * Lists the currently mounted volumes
1905 * The list of volumes.
1910 *---------------------------------------------------------------------------
1914 TclpObjListVolumes(void)
1916 Tcl_Obj *resultPtr, *elemPtr;
1917 char buf[40 * 4]; /* There couldn't be more than 30 drives??? */
1921 resultPtr = Tcl_NewObj();
1925 * GetLogicalDriveStrings() isn't implemented.
1926 * GetLogicalDrives() returns incorrect information.
1929 if (GetLogicalDriveStringsA(sizeof(buf), buf) == 0) {
1931 * GetVolumeInformation() will detects all drives, but causes
1932 * chattering on empty floppy drives. We only do this if
1933 * GetLogicalDriveStrings() didn't work. It has also been reported
1934 * that on some laptops it takes a while for GetVolumeInformation()
1935 * to return when pinging an empty floppy drive, another reason to
1936 * try to avoid calling it.
1943 for (i = 0; i < 26; i++) {
1944 buf[0] = (char) ('a' + i);
1945 if (GetVolumeInformationA(buf, NULL, 0, NULL, NULL, NULL, NULL, 0)
1946 || (GetLastError() == ERROR_NOT_READY)) {
1947 elemPtr = Tcl_NewStringObj(buf, -1);
1948 Tcl_ListObjAppendElement(NULL, resultPtr, elemPtr);
1952 for (p = buf; *p != '\0'; p += 4) {
1954 elemPtr = Tcl_NewStringObj(p, -1);
1955 Tcl_ListObjAppendElement(NULL, resultPtr, elemPtr);
1959 Tcl_IncrRefCount(resultPtr);