2 * File handling functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996, 2004 Alexandre Julliard
6 * Copyright 2003 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/port.h"
33 #define WIN32_NO_STATUS
38 #include "kernel_private.h"
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(file
);
44 #define MAX_PATHNAME_LEN 1024
46 static int path_safe_mode
= -1; /* path mode set by SetSearchPathMode */
48 static const WCHAR wildcardsW
[] = {'*','?',0};
50 /* check if a file name is for an executable file (.exe or .com) */
51 static inline BOOL
is_executable( const WCHAR
*name
)
53 static const WCHAR exeW
[] = {'.','e','x','e',0};
54 static const WCHAR comW
[] = {'.','c','o','m',0};
55 int len
= strlenW(name
);
57 if (len
< 4) return FALSE
;
58 return (!strcmpiW( name
+ len
- 4, exeW
) || !strcmpiW( name
+ len
- 4, comW
));
61 /***********************************************************************
64 * copy a file name back to OEM/Ansi, but only if the buffer is large enough
66 static DWORD
copy_filename_WtoA( LPCWSTR nameW
, LPSTR buffer
, DWORD len
)
70 BOOL is_ansi
= AreFileApisANSI();
72 RtlInitUnicodeString( &strW
, nameW
);
74 ret
= is_ansi
? RtlUnicodeStringToAnsiSize(&strW
) : RtlUnicodeStringToOemSize(&strW
);
75 if (buffer
&& ret
<= len
)
80 str
.MaximumLength
= min( len
, UNICODE_STRING_MAX_CHARS
);
82 RtlUnicodeStringToAnsiString( &str
, &strW
, FALSE
);
84 RtlUnicodeStringToOemString( &str
, &strW
, FALSE
);
85 ret
= str
.Length
; /* length without terminating 0 */
90 /***********************************************************************
91 * add_boot_rename_entry
93 * Adds an entry to the registry that is loaded when windows boots and
94 * checks if there are some files to be removed or renamed/moved.
95 * <fn1> has to be valid and <fn2> may be NULL. If both pointers are
96 * non-NULL then the file is moved, otherwise it is deleted. The
97 * entry of the registry key is always appended with two zero
98 * terminated strings. If <fn2> is NULL then the second entry is
99 * simply a single 0-byte. Otherwise the second filename goes
100 * there. The entries are prepended with \??\ before the path and the
101 * second filename gets also a '!' as the first character if
102 * MOVEFILE_REPLACE_EXISTING is set. After the final string another
103 * 0-byte follows to indicate the end of the strings.
105 * \??\D:\test\file1[0]
106 * !\??\D:\test\file1_renamed[0]
107 * \??\D:\Test|delete[0]
108 * [0] <- file is to be deleted, second string empty
109 * \??\D:\test\file2[0]
110 * !\??\D:\test\file2_renamed[0]
111 * [0] <- indicates end of strings
114 * \??\D:\test\file1[0]
115 * !\??\D:\test\file1_renamed[0]
116 * \??\D:\Test|delete[0]
117 * [0] <- file is to be deleted, second string empty
118 * [0] <- indicates end of strings
121 static BOOL
add_boot_rename_entry( LPCWSTR source
, LPCWSTR dest
, DWORD flags
)
123 static const WCHAR ValueName
[] = {'P','e','n','d','i','n','g',
124 'F','i','l','e','R','e','n','a','m','e',
125 'O','p','e','r','a','t','i','o','n','s',0};
126 static const WCHAR SessionW
[] = {'\\','R','e','g','i','s','t','r','y','\\',
127 'M','a','c','h','i','n','e','\\',
128 'S','y','s','t','e','m','\\',
129 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
130 'C','o','n','t','r','o','l','\\',
131 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
132 static const int info_size
= FIELD_OFFSET( KEY_VALUE_PARTIAL_INFORMATION
, Data
);
134 OBJECT_ATTRIBUTES attr
;
135 UNICODE_STRING nameW
, source_name
, dest_name
;
136 KEY_VALUE_PARTIAL_INFORMATION
*info
;
144 if (!RtlDosPathNameToNtPathName_U( source
, &source_name
, NULL
, NULL
))
146 SetLastError( ERROR_PATH_NOT_FOUND
);
149 dest_name
.Buffer
= NULL
;
150 if (dest
&& !RtlDosPathNameToNtPathName_U( dest
, &dest_name
, NULL
, NULL
))
152 RtlFreeUnicodeString( &source_name
);
153 SetLastError( ERROR_PATH_NOT_FOUND
);
157 attr
.Length
= sizeof(attr
);
158 attr
.RootDirectory
= 0;
159 attr
.ObjectName
= &nameW
;
161 attr
.SecurityDescriptor
= NULL
;
162 attr
.SecurityQualityOfService
= NULL
;
163 RtlInitUnicodeString( &nameW
, SessionW
);
165 if (NtCreateKey( &Reboot
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, NULL
) != STATUS_SUCCESS
)
167 WARN("Error creating key for reboot management [%s]\n",
168 "SYSTEM\\CurrentControlSet\\Control\\Session Manager");
169 RtlFreeUnicodeString( &source_name
);
170 RtlFreeUnicodeString( &dest_name
);
174 len1
= source_name
.Length
+ sizeof(WCHAR
);
177 len2
= dest_name
.Length
+ sizeof(WCHAR
);
178 if (flags
& MOVEFILE_REPLACE_EXISTING
)
179 len2
+= sizeof(WCHAR
); /* Plus 1 because of the leading '!' */
181 else len2
= sizeof(WCHAR
); /* minimum is the 0 characters for the empty second string */
183 RtlInitUnicodeString( &nameW
, ValueName
);
185 /* First we check if the key exists and if so how many bytes it already contains. */
186 if (NtQueryValueKey( Reboot
, &nameW
, KeyValuePartialInformation
,
187 NULL
, 0, &DataSize
) == STATUS_BUFFER_TOO_SMALL
)
189 if (!(Buffer
= HeapAlloc( GetProcessHeap(), 0, DataSize
+ len1
+ len2
+ sizeof(WCHAR
) )))
191 if (NtQueryValueKey( Reboot
, &nameW
, KeyValuePartialInformation
,
192 Buffer
, DataSize
, &DataSize
)) goto Quit
;
193 info
= (KEY_VALUE_PARTIAL_INFORMATION
*)Buffer
;
194 if (info
->Type
!= REG_MULTI_SZ
) goto Quit
;
195 if (DataSize
> sizeof(info
)) DataSize
-= sizeof(WCHAR
); /* remove terminating null (will be added back later) */
199 DataSize
= info_size
;
200 if (!(Buffer
= HeapAlloc( GetProcessHeap(), 0, DataSize
+ len1
+ len2
+ sizeof(WCHAR
) )))
204 memcpy( Buffer
+ DataSize
, source_name
.Buffer
, len1
);
206 p
= (WCHAR
*)(Buffer
+ DataSize
);
209 if (flags
& MOVEFILE_REPLACE_EXISTING
)
211 memcpy( p
, dest_name
.Buffer
, len2
);
217 DataSize
+= sizeof(WCHAR
);
221 p
= (WCHAR
*)(Buffer
+ DataSize
);
223 DataSize
+= sizeof(WCHAR
);
225 rc
= !NtSetValueKey(Reboot
, &nameW
, 0, REG_MULTI_SZ
, Buffer
+ info_size
, DataSize
- info_size
);
228 RtlFreeUnicodeString( &source_name
);
229 RtlFreeUnicodeString( &dest_name
);
230 if (Reboot
) NtClose(Reboot
);
231 HeapFree( GetProcessHeap(), 0, Buffer
);
236 /***********************************************************************
237 * GetFullPathNameW (KERNEL32.@)
239 * if the path closed with '\', *lastpart is 0
241 DWORD WINAPI
GetFullPathNameW( LPCWSTR name
, DWORD len
, LPWSTR buffer
,
244 return RtlGetFullPathName_U(name
, len
* sizeof(WCHAR
), buffer
, lastpart
) / sizeof(WCHAR
);
247 /***********************************************************************
248 * GetFullPathNameA (KERNEL32.@)
250 * if the path closed with '\', *lastpart is 0
252 DWORD WINAPI
GetFullPathNameA( LPCSTR name
, DWORD len
, LPSTR buffer
,
256 WCHAR bufferW
[MAX_PATH
], *lastpartW
= NULL
;
259 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return 0;
261 ret
= GetFullPathNameW( nameW
, MAX_PATH
, bufferW
, &lastpartW
);
266 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
269 ret
= copy_filename_WtoA( bufferW
, buffer
, len
);
270 if (ret
< len
&& lastpart
)
273 *lastpart
= buffer
+ FILE_name_WtoA( bufferW
, lastpartW
- bufferW
, NULL
, 0 );
281 /***********************************************************************
282 * GetLongPathNameW (KERNEL32.@)
285 * observed (Win2000):
286 * shortpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
287 * shortpath="": LastError=ERROR_PATH_NOT_FOUND, ret=0
289 DWORD WINAPI
GetLongPathNameW( LPCWSTR shortpath
, LPWSTR longpath
, DWORD longlen
)
291 WCHAR tmplongpath
[MAX_PATHNAME_LEN
];
293 DWORD sp
= 0, lp
= 0;
296 WIN32_FIND_DATAW wfd
;
300 TRACE("%s,%p,%u\n", debugstr_w(shortpath
), longpath
, longlen
);
304 SetLastError(ERROR_INVALID_PARAMETER
);
309 SetLastError(ERROR_PATH_NOT_FOUND
);
313 if (shortpath
[0] == '\\' && shortpath
[1] == '\\')
315 FIXME("UNC pathname %s\n", debugstr_w(shortpath
));
317 tmplen
= strlenW(shortpath
);
318 if (tmplen
< longlen
)
320 if (longpath
!= shortpath
) strcpyW( longpath
, shortpath
);
326 unixabsolute
= (shortpath
[0] == '/');
328 /* check for drive letter */
329 if (!unixabsolute
&& shortpath
[1] == ':' )
331 tmplongpath
[0] = shortpath
[0];
332 tmplongpath
[1] = ':';
336 if (strpbrkW(shortpath
+ sp
, wildcardsW
))
338 SetLastError(ERROR_INVALID_NAME
);
342 while (shortpath
[sp
])
344 /* check for path delimiters and reproduce them */
345 if (shortpath
[sp
] == '\\' || shortpath
[sp
] == '/')
347 tmplongpath
[lp
++] = shortpath
[sp
++];
348 tmplongpath
[lp
] = 0; /* terminate string */
353 for (; *p
&& *p
!= '/' && *p
!= '\\'; p
++);
354 tmplen
= p
- (shortpath
+ sp
);
355 lstrcpynW(tmplongpath
+ lp
, shortpath
+ sp
, tmplen
+ 1);
357 if (tmplongpath
[lp
] == '.')
359 if (tmplen
== 1 || (tmplen
== 2 && tmplongpath
[lp
+ 1] == '.'))
367 /* Check if the file exists */
368 goit
= FindFirstFileW(tmplongpath
, &wfd
);
369 if (goit
== INVALID_HANDLE_VALUE
)
371 TRACE("not found %s!\n", debugstr_w(tmplongpath
));
372 SetLastError ( ERROR_FILE_NOT_FOUND
);
377 is_legal_8dot3
= FALSE
;
378 CheckNameLegalDOS8Dot3W(tmplongpath
+ lp
, NULL
, 0, NULL
, &is_legal_8dot3
);
379 /* Use the existing file name if it's a short name */
381 strcpyW(tmplongpath
+ lp
, wfd
.cFileName
);
382 lp
+= strlenW(tmplongpath
+ lp
);
385 tmplen
= strlenW(shortpath
) - 1;
386 if ((shortpath
[tmplen
] == '/' || shortpath
[tmplen
] == '\\') &&
387 (tmplongpath
[lp
- 1] != '/' && tmplongpath
[lp
- 1] != '\\'))
388 tmplongpath
[lp
++] = shortpath
[tmplen
];
391 tmplen
= strlenW(tmplongpath
) + 1;
392 if (tmplen
<= longlen
)
394 strcpyW(longpath
, tmplongpath
);
395 TRACE("returning %s\n", debugstr_w(longpath
));
396 tmplen
--; /* length without 0 */
402 /***********************************************************************
403 * GetLongPathNameA (KERNEL32.@)
405 DWORD WINAPI
GetLongPathNameA( LPCSTR shortpath
, LPSTR longpath
, DWORD longlen
)
408 WCHAR longpathW
[MAX_PATH
];
411 TRACE("%s\n", debugstr_a(shortpath
));
413 if (!(shortpathW
= FILE_name_AtoW( shortpath
, FALSE
))) return 0;
415 ret
= GetLongPathNameW(shortpathW
, longpathW
, MAX_PATH
);
420 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
423 return copy_filename_WtoA( longpathW
, longpath
, longlen
);
427 /***********************************************************************
428 * GetShortPathNameW (KERNEL32.@)
432 * longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
433 * longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
435 * more observations ( with NT 3.51 (WinDD) ):
436 * longpath <= 8.3 -> just copy longpath to shortpath
438 * a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
439 * b) file does exist -> set the short filename.
440 * - trailing slashes are reproduced in the short name, even if the
441 * file is not a directory
442 * - the absolute/relative path of the short name is reproduced like found
444 * - longpath and shortpath may have the same address
447 DWORD WINAPI
GetShortPathNameW( LPCWSTR longpath
, LPWSTR shortpath
, DWORD shortlen
)
451 DWORD sp
= 0, lp
= 0;
452 DWORD tmplen
, buf_len
;
453 WIN32_FIND_DATAW wfd
;
456 TRACE("%s,%p,%u\n", debugstr_w(longpath
), shortpath
, shortlen
);
460 SetLastError(ERROR_INVALID_PARAMETER
);
465 SetLastError(ERROR_BAD_PATHNAME
);
469 /* code below only removes characters from string, never adds, so this is
470 * the largest buffer that tmpshortpath will need to have */
471 buf_len
= strlenW(longpath
) + 1;
472 tmpshortpath
= HeapAlloc(GetProcessHeap(), 0, buf_len
* sizeof(WCHAR
));
475 SetLastError(ERROR_OUTOFMEMORY
);
479 if (longpath
[0] == '\\' && longpath
[1] == '\\' && longpath
[2] == '?' && longpath
[3] == '\\')
481 memcpy(tmpshortpath
, longpath
, 4 * sizeof(WCHAR
));
485 if (strpbrkW(longpath
+ lp
, wildcardsW
))
487 HeapFree(GetProcessHeap(), 0, tmpshortpath
);
488 SetLastError(ERROR_INVALID_NAME
);
492 /* check for drive letter */
493 if (longpath
[lp
] != '/' && longpath
[lp
+ 1] == ':' )
495 tmpshortpath
[sp
] = longpath
[lp
];
496 tmpshortpath
[sp
+ 1] = ':';
503 /* check for path delimiters and reproduce them */
504 if (longpath
[lp
] == '\\' || longpath
[lp
] == '/')
506 tmpshortpath
[sp
++] = longpath
[lp
++];
507 tmpshortpath
[sp
] = 0; /* terminate string */
512 for (; *p
&& *p
!= '/' && *p
!= '\\'; p
++);
513 tmplen
= p
- (longpath
+ lp
);
514 lstrcpynW(tmpshortpath
+ sp
, longpath
+ lp
, tmplen
+ 1);
516 if (tmpshortpath
[sp
] == '.')
518 if (tmplen
== 1 || (tmplen
== 2 && tmpshortpath
[sp
+ 1] == '.'))
526 /* Check if the file exists and use the existing short file name */
527 goit
= FindFirstFileW(tmpshortpath
, &wfd
);
528 if (goit
== INVALID_HANDLE_VALUE
) goto notfound
;
531 /* In rare cases (like "a.abcd") short path may be longer than original path.
532 * Make sure we have enough space in temp buffer. */
533 if (wfd
.cAlternateFileName
[0] && tmplen
< strlenW(wfd
.cAlternateFileName
))
536 buf_len
+= strlenW(wfd
.cAlternateFileName
) - tmplen
;
537 new_buf
= HeapReAlloc(GetProcessHeap(), 0, tmpshortpath
, buf_len
* sizeof(WCHAR
));
540 HeapFree(GetProcessHeap(), 0, tmpshortpath
);
541 SetLastError(ERROR_OUTOFMEMORY
);
544 tmpshortpath
= new_buf
;
547 strcpyW(tmpshortpath
+ sp
, wfd
.cAlternateFileName
[0] ? wfd
.cAlternateFileName
: wfd
.cFileName
);
548 sp
+= strlenW(tmpshortpath
+ sp
);
551 tmpshortpath
[sp
] = 0;
553 tmplen
= strlenW(tmpshortpath
) + 1;
554 if (tmplen
<= shortlen
)
556 strcpyW(shortpath
, tmpshortpath
);
557 TRACE("returning %s\n", debugstr_w(shortpath
));
558 tmplen
--; /* length without 0 */
561 HeapFree(GetProcessHeap(), 0, tmpshortpath
);
565 HeapFree(GetProcessHeap(), 0, tmpshortpath
);
566 TRACE("not found!\n" );
567 SetLastError ( ERROR_FILE_NOT_FOUND
);
571 /***********************************************************************
572 * GetShortPathNameA (KERNEL32.@)
574 DWORD WINAPI
GetShortPathNameA( LPCSTR longpath
, LPSTR shortpath
, DWORD shortlen
)
577 WCHAR shortpathW
[MAX_PATH
];
580 TRACE("%s\n", debugstr_a(longpath
));
582 if (!(longpathW
= FILE_name_AtoW( longpath
, FALSE
))) return 0;
584 ret
= GetShortPathNameW(longpathW
, shortpathW
, MAX_PATH
);
589 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
592 return copy_filename_WtoA( shortpathW
, shortpath
, shortlen
);
596 /***********************************************************************
597 * GetTempPathA (KERNEL32.@)
599 DWORD WINAPI
GetTempPathA( DWORD count
, LPSTR path
)
601 WCHAR pathW
[MAX_PATH
];
604 ret
= GetTempPathW(MAX_PATH
, pathW
);
611 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
614 return copy_filename_WtoA( pathW
, path
, count
);
618 /***********************************************************************
619 * GetTempPathW (KERNEL32.@)
621 DWORD WINAPI
GetTempPathW( DWORD count
, LPWSTR path
)
623 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
624 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
625 static const WCHAR userprofile
[] = { 'U','S','E','R','P','R','O','F','I','L','E',0 };
626 WCHAR tmp_path
[MAX_PATH
];
629 TRACE("%u,%p\n", count
, path
);
631 if (!(ret
= GetEnvironmentVariableW( tmp
, tmp_path
, MAX_PATH
)) &&
632 !(ret
= GetEnvironmentVariableW( temp
, tmp_path
, MAX_PATH
)) &&
633 !(ret
= GetEnvironmentVariableW( userprofile
, tmp_path
, MAX_PATH
)) &&
634 !(ret
= GetWindowsDirectoryW( tmp_path
, MAX_PATH
)))
639 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
643 ret
= GetFullPathNameW(tmp_path
, MAX_PATH
, tmp_path
, NULL
);
646 if (ret
> MAX_PATH
- 2)
648 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
652 if (tmp_path
[ret
-1] != '\\')
654 tmp_path
[ret
++] = '\\';
655 tmp_path
[ret
] = '\0';
658 ret
++; /* add space for terminating 0 */
662 lstrcpynW(path
, tmp_path
, count
);
663 /* the remaining buffer must be zeroed up to 32766 bytes in XP or 32767
664 * bytes after it, we will assume the > XP behavior for now */
665 memset(path
+ ret
, 0, (min(count
, 32767) - ret
) * sizeof(WCHAR
));
666 ret
--; /* return length without 0 */
670 /* the buffer must be cleared if contents will not fit */
671 memset(path
, 0, count
* sizeof(WCHAR
));
674 TRACE("returning %u, %s\n", ret
, debugstr_w(path
));
679 /***********************************************************************
680 * GetTempFileNameA (KERNEL32.@)
682 UINT WINAPI
GetTempFileNameA( LPCSTR path
, LPCSTR prefix
, UINT unique
, LPSTR buffer
)
684 WCHAR
*pathW
, *prefixW
= NULL
;
685 WCHAR bufferW
[MAX_PATH
];
688 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return 0;
689 if (prefix
&& !(prefixW
= FILE_name_AtoW( prefix
, TRUE
))) return 0;
691 ret
= GetTempFileNameW(pathW
, prefixW
, unique
, bufferW
);
692 if (ret
) FILE_name_WtoA( bufferW
, -1, buffer
, MAX_PATH
);
694 HeapFree( GetProcessHeap(), 0, prefixW
);
698 /***********************************************************************
699 * GetTempFileNameW (KERNEL32.@)
701 UINT WINAPI
GetTempFileNameW( LPCWSTR path
, LPCWSTR prefix
, UINT unique
, LPWSTR buffer
)
703 static const WCHAR formatW
[] = {'%','x','.','t','m','p',0};
709 if ( !path
|| !buffer
)
711 SetLastError( ERROR_INVALID_PARAMETER
);
715 /* ensure that the provided directory exists */
716 attr
= GetFileAttributesW(path
);
717 if (attr
== INVALID_FILE_ATTRIBUTES
|| !(attr
& FILE_ATTRIBUTE_DIRECTORY
))
719 TRACE("path not found %s\n", debugstr_w(path
));
720 SetLastError( ERROR_DIRECTORY
);
724 strcpyW( buffer
, path
);
725 p
= buffer
+ strlenW(buffer
);
727 /* add a \, if there isn't one */
728 if ((p
== buffer
) || (p
[-1] != '\\')) *p
++ = '\\';
731 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
735 if (unique
) sprintfW( p
, formatW
, unique
);
738 /* get a "random" unique number and try to create the file */
740 UINT num
= GetTickCount() & 0xffff;
743 /* avoid using the same name twice in a short interval */
744 if (last
- num
< 10) num
= last
+ 1;
749 sprintfW( p
, formatW
, unique
);
750 handle
= CreateFileW( buffer
, GENERIC_WRITE
, 0, NULL
,
751 CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, 0 );
752 if (handle
!= INVALID_HANDLE_VALUE
)
753 { /* We created it */
754 TRACE("created %s\n", debugstr_w(buffer
) );
755 CloseHandle( handle
);
759 if (GetLastError() != ERROR_FILE_EXISTS
&&
760 GetLastError() != ERROR_SHARING_VIOLATION
)
761 break; /* No need to go on */
762 if (!(++unique
& 0xffff)) unique
= 1;
763 } while (unique
!= num
);
766 TRACE("returning %s\n", debugstr_w(buffer
) );
771 /***********************************************************************
774 static BOOL
get_path_safe_mode(void)
776 static const WCHAR keyW
[] = {'\\','R','e','g','i','s','t','r','y','\\',
777 'M','a','c','h','i','n','e','\\',
778 'S','y','s','t','e','m','\\',
779 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
780 'C','o','n','t','r','o','l','\\',
781 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
782 static const WCHAR valueW
[] = {'S','a','f','e','P','r','o','c','e','s','s','S','e','a','r','c','h','M','o','d','e',0};
784 if (path_safe_mode
== -1)
786 char buffer
[offsetof(KEY_VALUE_PARTIAL_INFORMATION
, Data
[sizeof(DWORD
)])];
787 KEY_VALUE_PARTIAL_INFORMATION
*info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buffer
;
788 OBJECT_ATTRIBUTES attr
;
789 UNICODE_STRING nameW
;
791 DWORD size
= sizeof(buffer
);
794 attr
.Length
= sizeof(attr
);
795 attr
.RootDirectory
= 0;
796 attr
.ObjectName
= &nameW
;
798 attr
.SecurityDescriptor
= NULL
;
799 attr
.SecurityQualityOfService
= NULL
;
801 RtlInitUnicodeString( &nameW
, keyW
);
802 if (!NtOpenKey( &hkey
, KEY_READ
, &attr
))
804 RtlInitUnicodeString( &nameW
, valueW
);
805 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, buffer
, size
, &size
) &&
806 info
->Type
== REG_DWORD
&& info
->DataLength
== sizeof(DWORD
))
807 mode
= !!*(DWORD
*)info
->Data
;
810 InterlockedCompareExchange( &path_safe_mode
, mode
, -1 );
812 return path_safe_mode
!= 0;
816 /***********************************************************************
819 * Check if the file name contains a path; helper for SearchPathW.
820 * A relative path is not considered a path unless it starts with ./ or ../
822 static inline BOOL
contains_pathW (LPCWSTR name
)
824 if (RtlDetermineDosPathNameType_U( name
) != RELATIVE_PATH
) return TRUE
;
825 if (name
[0] != '.') return FALSE
;
826 if (name
[1] == '/' || name
[1] == '\\') return TRUE
;
827 return (name
[1] == '.' && (name
[2] == '/' || name
[2] == '\\'));
830 /***********************************************************************
831 * find_actctx_dllpath
833 * Find the path (if any) of the dll from the activation context.
834 * Returned path doesn't include a name.
836 static NTSTATUS
find_actctx_dllpath(const WCHAR
*libname
, WCHAR
**path
)
838 static const WCHAR winsxsW
[] = {'\\','w','i','n','s','x','s','\\'};
839 static const WCHAR dotManifestW
[] = {'.','m','a','n','i','f','e','s','t',0};
841 ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION
*info
;
842 ACTCTX_SECTION_KEYED_DATA data
;
843 UNICODE_STRING nameW
;
845 SIZE_T needed
, size
= 1024;
848 RtlInitUnicodeString( &nameW
, libname
);
849 data
.cbSize
= sizeof(data
);
850 status
= RtlFindActivationContextSectionString( FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX
, NULL
,
851 ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION
,
853 if (status
!= STATUS_SUCCESS
) return status
;
857 if (!(info
= HeapAlloc( GetProcessHeap(), 0, size
)))
859 status
= STATUS_NO_MEMORY
;
862 status
= RtlQueryInformationActivationContext( 0, data
.hActCtx
, &data
.ulAssemblyRosterIndex
,
863 AssemblyDetailedInformationInActivationContext
,
864 info
, size
, &needed
);
865 if (status
== STATUS_SUCCESS
) break;
866 if (status
!= STATUS_BUFFER_TOO_SMALL
) goto done
;
867 HeapFree( GetProcessHeap(), 0, info
);
869 /* restart with larger buffer */
872 if (!info
->lpAssemblyManifestPath
|| !info
->lpAssemblyDirectoryName
)
874 status
= STATUS_SXS_KEY_NOT_FOUND
;
878 if ((p
= strrchrW( info
->lpAssemblyManifestPath
, '\\' )))
880 DWORD dirlen
= info
->ulAssemblyDirectoryNameLength
/ sizeof(WCHAR
);
883 if (strncmpiW( p
, info
->lpAssemblyDirectoryName
, dirlen
) || strcmpiW( p
+ dirlen
, dotManifestW
))
885 /* manifest name does not match directory name, so it's not a global
886 * windows/winsxs manifest; use the manifest directory name instead */
887 dirlen
= p
- info
->lpAssemblyManifestPath
;
888 needed
= (dirlen
+ 1) * sizeof(WCHAR
);
889 if (!(*path
= p
= HeapAlloc( GetProcessHeap(), 0, needed
)))
891 status
= STATUS_NO_MEMORY
;
894 memcpy( p
, info
->lpAssemblyManifestPath
, dirlen
* sizeof(WCHAR
) );
900 needed
= (strlenW( DIR_Windows
) * sizeof(WCHAR
) +
901 sizeof(winsxsW
) + info
->ulAssemblyDirectoryNameLength
+ 2*sizeof(WCHAR
));
903 if (!(*path
= p
= HeapAlloc( GetProcessHeap(), 0, needed
)))
905 status
= STATUS_NO_MEMORY
;
908 strcpyW( p
, DIR_Windows
);
910 memcpy( p
, winsxsW
, sizeof(winsxsW
) );
911 p
+= ARRAY_SIZE( winsxsW
);
912 memcpy( p
, info
->lpAssemblyDirectoryName
, info
->ulAssemblyDirectoryNameLength
);
913 p
+= info
->ulAssemblyDirectoryNameLength
/ sizeof(WCHAR
);
917 HeapFree( GetProcessHeap(), 0, info
);
918 RtlReleaseActivationContext( data
.hActCtx
);
922 /***********************************************************************
923 * SearchPathW [KERNEL32.@]
925 * Searches for a specified file in the search path.
928 * path [I] Path to search (NULL means default)
929 * name [I] Filename to search for.
930 * ext [I] File extension to append to file name. The first
931 * character must be a period. This parameter is
932 * specified only if the filename given does not
933 * contain an extension.
934 * buflen [I] size of buffer, in characters
935 * buffer [O] buffer for found filename
936 * lastpart [O] address of pointer to last used character in
937 * buffer (the final '\')
940 * Success: length of string copied into buffer, not including
941 * terminating null character. If the filename found is
942 * longer than the length of the buffer, the length of the
943 * filename is returned.
947 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
950 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
, DWORD buflen
,
951 LPWSTR buffer
, LPWSTR
*lastpart
)
955 if (!name
|| !name
[0])
957 SetLastError(ERROR_INVALID_PARAMETER
);
961 /* If the name contains an explicit path, ignore the path */
963 if (contains_pathW(name
))
965 /* try first without extension */
966 if (RtlDoesFileExists_U( name
))
967 return GetFullPathNameW( name
, buflen
, buffer
, lastpart
);
971 LPCWSTR p
= strrchrW( name
, '.' );
972 if (p
&& !strchrW( p
, '/' ) && !strchrW( p
, '\\' ))
973 ext
= NULL
; /* Ignore the specified extension */
976 /* Allocate a buffer for the file name and extension */
980 DWORD len
= strlenW(name
) + strlenW(ext
);
982 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) )))
984 SetLastError( ERROR_OUTOFMEMORY
);
987 strcpyW( tmp
, name
);
989 if (RtlDoesFileExists_U( tmp
))
990 ret
= GetFullPathNameW( tmp
, buflen
, buffer
, lastpart
);
991 HeapFree( GetProcessHeap(), 0, tmp
);
994 else if (path
&& path
[0]) /* search in the specified path */
996 ret
= RtlDosSearchPath_U( path
, name
, ext
, buflen
* sizeof(WCHAR
),
997 buffer
, lastpart
) / sizeof(WCHAR
);
999 else /* search in active context and default path */
1001 WCHAR
*dll_path
= NULL
, *search
= NULL
;
1002 DWORD req_len
, name_len
;
1004 req_len
= name_len
= strlenW(name
);
1006 if (strchrW( name
, '.' )) ext
= NULL
;
1009 DWORD ext_len
= strlenW(ext
);
1012 name_len
+= ext_len
;
1014 search
= HeapAlloc( GetProcessHeap(), 0, (name_len
+ ext_len
+ 1) * sizeof(WCHAR
) );
1017 SetLastError( ERROR_OUTOFMEMORY
);
1020 strcpyW( search
, name
);
1021 strcatW( search
, ext
);
1024 /* now that we have combined name we don't need extension any more */
1027 /* When file is found with activation context no attempt is made
1028 to check if it's really exist, path is returned only basing on context info. */
1029 if (find_actctx_dllpath( name
, &dll_path
) == STATUS_SUCCESS
)
1033 path_len
= strlenW(dll_path
);
1034 req_len
+= path_len
;
1036 if (lastpart
) *lastpart
= NULL
;
1038 /* count null termination char too */
1039 if (req_len
+ 1 <= buflen
)
1041 memcpy( buffer
, dll_path
, path_len
* sizeof(WCHAR
) );
1042 memcpy( &buffer
[path_len
], name
, name_len
* sizeof(WCHAR
) );
1043 buffer
[req_len
] = 0;
1044 if (lastpart
) *lastpart
= buffer
+ path_len
;
1050 HeapFree( GetProcessHeap(), 0, dll_path
);
1051 HeapFree( GetProcessHeap(), 0, search
);
1055 if ((dll_path
= MODULE_get_dll_load_path( NULL
, get_path_safe_mode() )))
1057 ret
= RtlDosSearchPath_U( dll_path
, name
, NULL
, buflen
* sizeof(WCHAR
),
1058 buffer
, lastpart
) / sizeof(WCHAR
);
1059 HeapFree( GetProcessHeap(), 0, dll_path
);
1060 HeapFree( GetProcessHeap(), 0, search
);
1064 SetLastError( ERROR_OUTOFMEMORY
);
1070 if (!ret
) SetLastError( ERROR_FILE_NOT_FOUND
);
1071 else TRACE( "found %s\n", debugstr_w(buffer
) );
1076 /***********************************************************************
1077 * SearchPathA (KERNEL32.@)
1081 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
1082 DWORD buflen
, LPSTR buffer
, LPSTR
*lastpart
)
1084 WCHAR
*pathW
= NULL
, *nameW
, *extW
= NULL
;
1085 WCHAR bufferW
[MAX_PATH
];
1090 SetLastError(ERROR_INVALID_PARAMETER
);
1094 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return 0;
1095 if (path
&& !(pathW
= FILE_name_AtoW( path
, TRUE
))) return 0;
1097 if (ext
&& !(extW
= FILE_name_AtoW( ext
, TRUE
)))
1099 HeapFree( GetProcessHeap(), 0, pathW
);
1103 ret
= SearchPathW(pathW
, nameW
, extW
, MAX_PATH
, bufferW
, NULL
);
1105 HeapFree( GetProcessHeap(), 0, pathW
);
1106 HeapFree( GetProcessHeap(), 0, extW
);
1111 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
1114 ret
= copy_filename_WtoA( bufferW
, buffer
, buflen
);
1115 if (buflen
> ret
&& lastpart
)
1116 *lastpart
= strrchr(buffer
, '\\') + 1;
1120 static BOOL
is_same_file(HANDLE h1
, HANDLE h2
)
1124 if (wine_server_handle_to_fd(h1
, 0, &fd1
, NULL
) == STATUS_SUCCESS
)
1127 if (wine_server_handle_to_fd(h2
, 0, &fd2
, NULL
) == STATUS_SUCCESS
)
1129 struct stat stat1
, stat2
;
1130 if (fstat(fd1
, &stat1
) == 0 && fstat(fd2
, &stat2
) == 0)
1131 ret
= (stat1
.st_dev
== stat2
.st_dev
&& stat1
.st_ino
== stat2
.st_ino
);
1132 wine_server_release_fd(h2
, fd2
);
1134 wine_server_release_fd(h1
, fd1
);
1139 /**************************************************************************
1140 * CopyFileW (KERNEL32.@)
1142 BOOL WINAPI
CopyFileW( LPCWSTR source
, LPCWSTR dest
, BOOL fail_if_exists
)
1144 return CopyFileExW( source
, dest
, NULL
, NULL
, NULL
,
1145 fail_if_exists
? COPY_FILE_FAIL_IF_EXISTS
: 0 );
1149 /**************************************************************************
1150 * CopyFileA (KERNEL32.@)
1152 BOOL WINAPI
CopyFileA( LPCSTR source
, LPCSTR dest
, BOOL fail_if_exists
)
1154 WCHAR
*sourceW
, *destW
;
1157 if (!(sourceW
= FILE_name_AtoW( source
, FALSE
))) return FALSE
;
1158 if (!(destW
= FILE_name_AtoW( dest
, TRUE
))) return FALSE
;
1160 ret
= CopyFileW( sourceW
, destW
, fail_if_exists
);
1162 HeapFree( GetProcessHeap(), 0, destW
);
1167 /**************************************************************************
1168 * CopyFileExW (KERNEL32.@)
1170 BOOL WINAPI
CopyFileExW(LPCWSTR source
, LPCWSTR dest
,
1171 LPPROGRESS_ROUTINE progress
, LPVOID param
,
1172 LPBOOL cancel_ptr
, DWORD flags
)
1174 static const int buffer_size
= 65536;
1176 BY_HANDLE_FILE_INFORMATION info
;
1181 if (!source
|| !dest
)
1183 SetLastError(ERROR_INVALID_PARAMETER
);
1186 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, buffer_size
)))
1188 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1192 TRACE("%s -> %s, %x\n", debugstr_w(source
), debugstr_w(dest
), flags
);
1194 if ((h1
= CreateFileW(source
, GENERIC_READ
,
1195 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1196 NULL
, OPEN_EXISTING
, 0, 0)) == INVALID_HANDLE_VALUE
)
1198 WARN("Unable to open source %s\n", debugstr_w(source
));
1199 HeapFree( GetProcessHeap(), 0, buffer
);
1203 if (!GetFileInformationByHandle( h1
, &info
))
1205 WARN("GetFileInformationByHandle returned error for %s\n", debugstr_w(source
));
1206 HeapFree( GetProcessHeap(), 0, buffer
);
1211 if (!(flags
& COPY_FILE_FAIL_IF_EXISTS
))
1213 BOOL same_file
= FALSE
;
1214 h2
= CreateFileW( dest
, 0, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1215 OPEN_EXISTING
, 0, 0);
1216 if (h2
!= INVALID_HANDLE_VALUE
)
1218 same_file
= is_same_file( h1
, h2
);
1223 HeapFree( GetProcessHeap(), 0, buffer
);
1225 SetLastError( ERROR_SHARING_VIOLATION
);
1230 if ((h2
= CreateFileW( dest
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1231 (flags
& COPY_FILE_FAIL_IF_EXISTS
) ? CREATE_NEW
: CREATE_ALWAYS
,
1232 info
.dwFileAttributes
, h1
)) == INVALID_HANDLE_VALUE
)
1234 WARN("Unable to open dest %s\n", debugstr_w(dest
));
1235 HeapFree( GetProcessHeap(), 0, buffer
);
1240 while (ReadFile( h1
, buffer
, buffer_size
, &count
, NULL
) && count
)
1246 if (!WriteFile( h2
, p
, count
, &res
, NULL
) || !res
) goto done
;
1253 /* Maintain the timestamp of source file to destination file */
1254 SetFileTime(h2
, NULL
, NULL
, &info
.ftLastWriteTime
);
1255 HeapFree( GetProcessHeap(), 0, buffer
);
1262 /**************************************************************************
1263 * CopyFileExA (KERNEL32.@)
1265 BOOL WINAPI
CopyFileExA(LPCSTR sourceFilename
, LPCSTR destFilename
,
1266 LPPROGRESS_ROUTINE progressRoutine
, LPVOID appData
,
1267 LPBOOL cancelFlagPointer
, DWORD copyFlags
)
1269 WCHAR
*sourceW
, *destW
;
1272 /* can't use the TEB buffer since we may have a callback routine */
1273 if (!(sourceW
= FILE_name_AtoW( sourceFilename
, TRUE
))) return FALSE
;
1274 if (!(destW
= FILE_name_AtoW( destFilename
, TRUE
)))
1276 HeapFree( GetProcessHeap(), 0, sourceW
);
1279 ret
= CopyFileExW(sourceW
, destW
, progressRoutine
, appData
,
1280 cancelFlagPointer
, copyFlags
);
1281 HeapFree( GetProcessHeap(), 0, sourceW
);
1282 HeapFree( GetProcessHeap(), 0, destW
);
1286 /**************************************************************************
1287 * MoveFileTransactedA (KERNEL32.@)
1289 BOOL WINAPI
MoveFileTransactedA(const char *source
, const char *dest
, LPPROGRESS_ROUTINE progress
, void *data
, DWORD flags
, HANDLE handle
)
1291 FIXME("(%s, %s, %p, %p, %d, %p)\n", debugstr_a(source
), debugstr_a(dest
), progress
, data
, flags
, handle
);
1292 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1296 /**************************************************************************
1297 * MoveFileTransactedW (KERNEL32.@)
1299 BOOL WINAPI
MoveFileTransactedW(const WCHAR
*source
, const WCHAR
*dest
, LPPROGRESS_ROUTINE progress
, void *data
, DWORD flags
, HANDLE handle
)
1301 FIXME("(%s, %s, %p, %p, %d, %p)\n", debugstr_w(source
), debugstr_w(dest
), progress
, data
, flags
, handle
);
1302 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1306 /**************************************************************************
1307 * MoveFileWithProgressW (KERNEL32.@)
1309 BOOL WINAPI
MoveFileWithProgressW( LPCWSTR source
, LPCWSTR dest
,
1310 LPPROGRESS_ROUTINE fnProgress
,
1311 LPVOID param
, DWORD flag
)
1313 FILE_BASIC_INFORMATION info
;
1314 UNICODE_STRING nt_name
;
1315 OBJECT_ATTRIBUTES attr
;
1318 HANDLE source_handle
= 0, dest_handle
= 0;
1319 ANSI_STRING source_unix
, dest_unix
;
1322 TRACE("(%s,%s,%p,%p,%04x)\n",
1323 debugstr_w(source
), debugstr_w(dest
), fnProgress
, param
, flag
);
1325 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
)
1326 return add_boot_rename_entry( source
, dest
, flag
);
1329 return DeleteFileW( source
);
1331 /* check if we are allowed to rename the source */
1333 if (!RtlDosPathNameToNtPathName_U( source
, &nt_name
, NULL
, NULL
))
1335 SetLastError( ERROR_PATH_NOT_FOUND
);
1338 source_unix
.Buffer
= NULL
;
1339 dest_unix
.Buffer
= NULL
;
1340 attr
.Length
= sizeof(attr
);
1341 attr
.RootDirectory
= 0;
1342 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1343 attr
.ObjectName
= &nt_name
;
1344 attr
.SecurityDescriptor
= NULL
;
1345 attr
.SecurityQualityOfService
= NULL
;
1347 status
= NtOpenFile( &source_handle
, DELETE
| SYNCHRONIZE
, &attr
, &io
,
1348 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
, FILE_SYNCHRONOUS_IO_NONALERT
);
1349 if (status
== STATUS_SUCCESS
)
1350 status
= wine_nt_to_unix_file_name( &nt_name
, &source_unix
, FILE_OPEN
, FALSE
);
1351 RtlFreeUnicodeString( &nt_name
);
1352 if (status
!= STATUS_SUCCESS
)
1354 SetLastError( RtlNtStatusToDosError(status
) );
1357 status
= NtQueryInformationFile( source_handle
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1358 if (status
!= STATUS_SUCCESS
)
1360 SetLastError( RtlNtStatusToDosError(status
) );
1364 /* we must have write access to the destination, and it must */
1365 /* not exist except if MOVEFILE_REPLACE_EXISTING is set */
1367 if (!RtlDosPathNameToNtPathName_U( dest
, &nt_name
, NULL
, NULL
))
1369 SetLastError( ERROR_PATH_NOT_FOUND
);
1372 options
= FILE_NON_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
;
1373 if (flag
& MOVEFILE_WRITE_THROUGH
)
1374 options
|= FILE_WRITE_THROUGH
;
1375 status
= NtOpenFile( &dest_handle
, GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, &attr
, &io
,
1376 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
, options
);
1377 if (status
== STATUS_SUCCESS
) /* destination exists */
1379 if (!(flag
& MOVEFILE_REPLACE_EXISTING
))
1381 if (!is_same_file( source_handle
, dest_handle
))
1383 SetLastError( ERROR_ALREADY_EXISTS
);
1384 RtlFreeUnicodeString( &nt_name
);
1388 else if (info
.FileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) /* cannot replace directory */
1390 SetLastError( ERROR_ACCESS_DENIED
);
1394 NtClose( dest_handle
);
1396 else if (status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
1398 SetLastError( RtlNtStatusToDosError(status
) );
1399 RtlFreeUnicodeString( &nt_name
);
1403 status
= wine_nt_to_unix_file_name( &nt_name
, &dest_unix
, FILE_OPEN_IF
, FALSE
);
1404 RtlFreeUnicodeString( &nt_name
);
1405 if (status
!= STATUS_SUCCESS
&& status
!= STATUS_NO_SUCH_FILE
)
1407 SetLastError( RtlNtStatusToDosError(status
) );
1411 /* now perform the rename */
1413 if (rename( source_unix
.Buffer
, dest_unix
.Buffer
) == -1)
1415 if (errno
== EXDEV
&& (flag
& MOVEFILE_COPY_ALLOWED
))
1417 NtClose( source_handle
);
1418 RtlFreeAnsiString( &source_unix
);
1419 RtlFreeAnsiString( &dest_unix
);
1420 if (!CopyFileExW( source
, dest
, fnProgress
,
1421 param
, NULL
, COPY_FILE_FAIL_IF_EXISTS
))
1423 return DeleteFileW( source
);
1426 /* if we created the destination, remove it */
1427 if (io
.Information
== FILE_CREATED
) unlink( dest_unix
.Buffer
);
1431 /* fixup executable permissions */
1433 if (is_executable( source
) != is_executable( dest
))
1436 if (stat( dest_unix
.Buffer
, &fstat
) != -1)
1438 if (is_executable( dest
))
1439 /* set executable bit where read bit is set */
1440 fstat
.st_mode
|= (fstat
.st_mode
& 0444) >> 2;
1442 fstat
.st_mode
&= ~0111;
1443 chmod( dest_unix
.Buffer
, fstat
.st_mode
);
1447 NtClose( source_handle
);
1448 RtlFreeAnsiString( &source_unix
);
1449 RtlFreeAnsiString( &dest_unix
);
1453 if (source_handle
) NtClose( source_handle
);
1454 if (dest_handle
) NtClose( dest_handle
);
1455 RtlFreeAnsiString( &source_unix
);
1456 RtlFreeAnsiString( &dest_unix
);
1460 /**************************************************************************
1461 * MoveFileWithProgressA (KERNEL32.@)
1463 BOOL WINAPI
MoveFileWithProgressA( LPCSTR source
, LPCSTR dest
,
1464 LPPROGRESS_ROUTINE fnProgress
,
1465 LPVOID param
, DWORD flag
)
1467 WCHAR
*sourceW
, *destW
;
1470 if (!(sourceW
= FILE_name_AtoW( source
, FALSE
))) return FALSE
;
1473 if (!(destW
= FILE_name_AtoW( dest
, TRUE
))) return FALSE
;
1478 ret
= MoveFileWithProgressW( sourceW
, destW
, fnProgress
, param
, flag
);
1479 HeapFree( GetProcessHeap(), 0, destW
);
1483 /**************************************************************************
1484 * MoveFileExW (KERNEL32.@)
1486 BOOL WINAPI
MoveFileExW( LPCWSTR source
, LPCWSTR dest
, DWORD flag
)
1488 return MoveFileWithProgressW( source
, dest
, NULL
, NULL
, flag
);
1491 /**************************************************************************
1492 * MoveFileExA (KERNEL32.@)
1494 BOOL WINAPI
MoveFileExA( LPCSTR source
, LPCSTR dest
, DWORD flag
)
1496 return MoveFileWithProgressA( source
, dest
, NULL
, NULL
, flag
);
1500 /**************************************************************************
1501 * MoveFileW (KERNEL32.@)
1503 * Move file or directory
1505 BOOL WINAPI
MoveFileW( LPCWSTR source
, LPCWSTR dest
)
1507 return MoveFileExW( source
, dest
, MOVEFILE_COPY_ALLOWED
);
1511 /**************************************************************************
1512 * MoveFileA (KERNEL32.@)
1514 BOOL WINAPI
MoveFileA( LPCSTR source
, LPCSTR dest
)
1516 return MoveFileExA( source
, dest
, MOVEFILE_COPY_ALLOWED
);
1520 /*************************************************************************
1521 * CreateHardLinkW (KERNEL32.@)
1523 BOOL WINAPI
CreateHardLinkW(LPCWSTR lpFileName
, LPCWSTR lpExistingFileName
,
1524 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
1527 UNICODE_STRING ntDest
, ntSource
;
1528 ANSI_STRING unixDest
, unixSource
;
1531 TRACE("(%s, %s, %p)\n", debugstr_w(lpFileName
),
1532 debugstr_w(lpExistingFileName
), lpSecurityAttributes
);
1534 ntDest
.Buffer
= ntSource
.Buffer
= NULL
;
1535 if (!RtlDosPathNameToNtPathName_U( lpFileName
, &ntDest
, NULL
, NULL
) ||
1536 !RtlDosPathNameToNtPathName_U( lpExistingFileName
, &ntSource
, NULL
, NULL
))
1538 SetLastError( ERROR_PATH_NOT_FOUND
);
1542 unixSource
.Buffer
= unixDest
.Buffer
= NULL
;
1543 status
= wine_nt_to_unix_file_name( &ntSource
, &unixSource
, FILE_OPEN
, FALSE
);
1546 status
= wine_nt_to_unix_file_name( &ntDest
, &unixDest
, FILE_CREATE
, FALSE
);
1547 if (!status
) /* destination must not exist */
1549 status
= STATUS_OBJECT_NAME_EXISTS
;
1550 } else if (status
== STATUS_NO_SUCH_FILE
)
1552 status
= STATUS_SUCCESS
;
1557 SetLastError( RtlNtStatusToDosError(status
) );
1558 else if (!link( unixSource
.Buffer
, unixDest
.Buffer
))
1560 TRACE("Hardlinked '%s' to '%s'\n", debugstr_a( unixDest
.Buffer
),
1561 debugstr_a( unixSource
.Buffer
));
1567 RtlFreeAnsiString( &unixSource
);
1568 RtlFreeAnsiString( &unixDest
);
1571 RtlFreeUnicodeString( &ntSource
);
1572 RtlFreeUnicodeString( &ntDest
);
1577 /*************************************************************************
1578 * CreateHardLinkA (KERNEL32.@)
1580 BOOL WINAPI
CreateHardLinkA(LPCSTR lpFileName
, LPCSTR lpExistingFileName
,
1581 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
1583 WCHAR
*sourceW
, *destW
;
1586 if (!(sourceW
= FILE_name_AtoW( lpExistingFileName
, TRUE
)))
1590 if (!(destW
= FILE_name_AtoW( lpFileName
, TRUE
)))
1592 HeapFree( GetProcessHeap(), 0, sourceW
);
1596 res
= CreateHardLinkW( destW
, sourceW
, lpSecurityAttributes
);
1598 HeapFree( GetProcessHeap(), 0, sourceW
);
1599 HeapFree( GetProcessHeap(), 0, destW
);
1605 /***********************************************************************
1606 * CreateDirectoryW (KERNEL32.@)
1610 * ERROR_DISK_FULL: on full disk
1611 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
1612 * ERROR_ACCESS_DENIED: on permission problems
1613 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
1615 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
, LPSECURITY_ATTRIBUTES sa
)
1617 OBJECT_ATTRIBUTES attr
;
1618 UNICODE_STRING nt_name
;
1624 TRACE( "%s\n", debugstr_w(path
) );
1626 if (!RtlDosPathNameToNtPathName_U( path
, &nt_name
, NULL
, NULL
))
1628 SetLastError( ERROR_PATH_NOT_FOUND
);
1631 attr
.Length
= sizeof(attr
);
1632 attr
.RootDirectory
= 0;
1633 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1634 attr
.ObjectName
= &nt_name
;
1635 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1636 attr
.SecurityQualityOfService
= NULL
;
1638 status
= NtCreateFile( &handle
, GENERIC_READ
| SYNCHRONIZE
, &attr
, &io
, NULL
,
1639 FILE_ATTRIBUTE_NORMAL
, FILE_SHARE_READ
, FILE_CREATE
,
1640 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
, NULL
, 0 );
1642 if (status
== STATUS_SUCCESS
)
1647 else SetLastError( RtlNtStatusToDosError(status
) );
1649 RtlFreeUnicodeString( &nt_name
);
1654 /***********************************************************************
1655 * CreateDirectoryA (KERNEL32.@)
1657 BOOL WINAPI
CreateDirectoryA( LPCSTR path
, LPSECURITY_ATTRIBUTES sa
)
1661 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1662 return CreateDirectoryW( pathW
, sa
);
1666 /***********************************************************************
1667 * CreateDirectoryExA (KERNEL32.@)
1669 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
, LPSECURITY_ATTRIBUTES sa
)
1671 WCHAR
*pathW
, *templateW
= NULL
;
1674 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1675 if (template && !(templateW
= FILE_name_AtoW( template, TRUE
))) return FALSE
;
1677 ret
= CreateDirectoryExW( templateW
, pathW
, sa
);
1678 HeapFree( GetProcessHeap(), 0, templateW
);
1683 /***********************************************************************
1684 * CreateDirectoryExW (KERNEL32.@)
1686 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
, LPSECURITY_ATTRIBUTES sa
)
1688 return CreateDirectoryW( path
, sa
);
1692 /***********************************************************************
1693 * RemoveDirectoryW (KERNEL32.@)
1695 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
1697 OBJECT_ATTRIBUTES attr
;
1698 UNICODE_STRING nt_name
;
1699 ANSI_STRING unix_name
;
1705 TRACE( "%s\n", debugstr_w(path
) );
1707 if (!RtlDosPathNameToNtPathName_U( path
, &nt_name
, NULL
, NULL
))
1709 SetLastError( ERROR_PATH_NOT_FOUND
);
1712 attr
.Length
= sizeof(attr
);
1713 attr
.RootDirectory
= 0;
1714 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1715 attr
.ObjectName
= &nt_name
;
1716 attr
.SecurityDescriptor
= NULL
;
1717 attr
.SecurityQualityOfService
= NULL
;
1719 status
= NtOpenFile( &handle
, DELETE
| SYNCHRONIZE
, &attr
, &io
,
1720 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1721 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
1722 if (status
!= STATUS_SUCCESS
)
1724 SetLastError( RtlNtStatusToDosError(status
) );
1725 RtlFreeUnicodeString( &nt_name
);
1729 status
= wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN
, FALSE
);
1730 RtlFreeUnicodeString( &nt_name
);
1731 if (status
!= STATUS_SUCCESS
)
1733 SetLastError( RtlNtStatusToDosError(status
) );
1738 if (!(ret
= (rmdir( unix_name
.Buffer
) != -1))) FILE_SetDosError();
1739 RtlFreeAnsiString( &unix_name
);
1745 /***********************************************************************
1746 * RemoveDirectoryA (KERNEL32.@)
1748 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
1752 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1753 return RemoveDirectoryW( pathW
);
1757 /***********************************************************************
1758 * GetCurrentDirectoryW (KERNEL32.@)
1760 UINT WINAPI
GetCurrentDirectoryW( UINT buflen
, LPWSTR buf
)
1762 return RtlGetCurrentDirectory_U( buflen
* sizeof(WCHAR
), buf
) / sizeof(WCHAR
);
1766 /***********************************************************************
1767 * GetCurrentDirectoryA (KERNEL32.@)
1769 UINT WINAPI
GetCurrentDirectoryA( UINT buflen
, LPSTR buf
)
1771 WCHAR bufferW
[MAX_PATH
];
1774 if (buflen
&& buf
&& ((ULONG_PTR
)buf
>> 16) == 0)
1776 /* Win9x catches access violations here, returning zero.
1777 * This behaviour resulted in some people not noticing
1778 * that they got the argument order wrong. So let's be
1779 * nice and fail gracefully if buf is invalid and looks
1780 * more like a buflen. */
1781 SetLastError(ERROR_INVALID_PARAMETER
);
1785 ret
= RtlGetCurrentDirectory_U( sizeof(bufferW
), bufferW
);
1787 if (ret
> sizeof(bufferW
))
1789 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
1792 return copy_filename_WtoA( bufferW
, buf
, buflen
);
1796 /***********************************************************************
1797 * SetCurrentDirectoryW (KERNEL32.@)
1799 BOOL WINAPI
SetCurrentDirectoryW( LPCWSTR dir
)
1801 UNICODE_STRING dirW
;
1804 RtlInitUnicodeString( &dirW
, dir
);
1805 status
= RtlSetCurrentDirectory_U( &dirW
);
1806 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
1811 /***********************************************************************
1812 * SetCurrentDirectoryA (KERNEL32.@)
1814 BOOL WINAPI
SetCurrentDirectoryA( LPCSTR dir
)
1817 UNICODE_STRING strW
;
1820 if (!(dirW
= FILE_name_AtoW( dir
, FALSE
))) return FALSE
;
1821 RtlInitUnicodeString( &strW
, dirW
);
1822 status
= RtlSetCurrentDirectory_U( &strW
);
1823 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
1828 /***********************************************************************
1829 * GetWindowsDirectoryW (KERNEL32.@)
1831 * See comment for GetWindowsDirectoryA.
1833 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
1835 UINT len
= strlenW( DIR_Windows
) + 1;
1836 if (path
&& count
>= len
)
1838 strcpyW( path
, DIR_Windows
);
1845 /***********************************************************************
1846 * GetWindowsDirectoryA (KERNEL32.@)
1849 * If buffer is large enough to hold full path and terminating '\0' character
1850 * function copies path to buffer and returns length of the path without '\0'.
1851 * Otherwise function returns required size including '\0' character and
1852 * does not touch the buffer.
1854 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
1856 return copy_filename_WtoA( DIR_Windows
, path
, count
);
1860 /***********************************************************************
1861 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
1863 UINT WINAPI
GetSystemWindowsDirectoryA( LPSTR path
, UINT count
)
1865 return GetWindowsDirectoryA( path
, count
);
1869 /***********************************************************************
1870 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
1872 UINT WINAPI
GetSystemWindowsDirectoryW( LPWSTR path
, UINT count
)
1874 return GetWindowsDirectoryW( path
, count
);
1878 /***********************************************************************
1879 * GetSystemDirectoryW (KERNEL32.@)
1881 * See comment for GetWindowsDirectoryA.
1883 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
1885 UINT len
= strlenW( DIR_System
) + 1;
1886 if (path
&& count
>= len
)
1888 strcpyW( path
, DIR_System
);
1895 /***********************************************************************
1896 * GetSystemDirectoryA (KERNEL32.@)
1898 * See comment for GetWindowsDirectoryA.
1900 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
1902 return copy_filename_WtoA( DIR_System
, path
, count
);
1906 /***********************************************************************
1907 * GetSystemWow64DirectoryW (KERNEL32.@)
1910 * - On Win32 we should return ERROR_CALL_NOT_IMPLEMENTED
1911 * - On Win64 we should return the SysWow64 (system64) directory
1913 UINT WINAPI
GetSystemWow64DirectoryW( LPWSTR path
, UINT count
)
1919 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
1922 len
= strlenW( DIR_SysWow64
) + 1;
1923 if (path
&& count
>= len
)
1925 strcpyW( path
, DIR_SysWow64
);
1932 /***********************************************************************
1933 * GetSystemWow64DirectoryA (KERNEL32.@)
1935 * See comment for GetWindowsWow64DirectoryW.
1937 UINT WINAPI
GetSystemWow64DirectoryA( LPSTR path
, UINT count
)
1941 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
1944 return copy_filename_WtoA( DIR_SysWow64
, path
, count
);
1948 /***********************************************************************
1949 * Wow64EnableWow64FsRedirection (KERNEL32.@)
1951 BOOLEAN WINAPI
Wow64EnableWow64FsRedirection( BOOLEAN enable
)
1953 NTSTATUS status
= RtlWow64EnableFsRedirection( enable
);
1954 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1959 /***********************************************************************
1960 * Wow64DisableWow64FsRedirection (KERNEL32.@)
1962 BOOL WINAPI
Wow64DisableWow64FsRedirection( PVOID
*old_value
)
1964 NTSTATUS status
= RtlWow64EnableFsRedirectionEx( TRUE
, (ULONG
*)old_value
);
1965 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1970 /***********************************************************************
1971 * Wow64RevertWow64FsRedirection (KERNEL32.@)
1973 BOOL WINAPI
Wow64RevertWow64FsRedirection( PVOID old_value
)
1975 NTSTATUS status
= RtlWow64EnableFsRedirection( !old_value
);
1976 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1981 /***********************************************************************
1982 * NeedCurrentDirectoryForExePathW (KERNEL32.@)
1984 BOOL WINAPI
NeedCurrentDirectoryForExePathW( LPCWSTR name
)
1986 static const WCHAR env_name
[] = {'N','o','D','e','f','a','u','l','t',
1987 'C','u','r','r','e','n','t',
1988 'D','i','r','e','c','t','o','r','y',
1989 'I','n','E','x','e','P','a','t','h',0};
1992 /* MSDN mentions some 'registry location'. We do not use registry. */
1993 FIXME("(%s): partial stub\n", debugstr_w(name
));
1995 if (strchrW(name
, '\\'))
1998 /* Check the existence of the variable, not value */
1999 if (!GetEnvironmentVariableW( env_name
, &env_val
, 1 ))
2006 /***********************************************************************
2007 * NeedCurrentDirectoryForExePathA (KERNEL32.@)
2009 BOOL WINAPI
NeedCurrentDirectoryForExePathA( LPCSTR name
)
2013 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return TRUE
;
2014 return NeedCurrentDirectoryForExePathW( nameW
);
2018 /***********************************************************************
2019 * wine_get_unix_file_name (KERNEL32.@) Not a Windows API
2021 * Return the full Unix file name for a given path.
2022 * Returned buffer must be freed by caller.
2024 char * CDECL
wine_get_unix_file_name( LPCWSTR dosW
)
2026 UNICODE_STRING nt_name
;
2027 ANSI_STRING unix_name
;
2030 if (!RtlDosPathNameToNtPathName_U( dosW
, &nt_name
, NULL
, NULL
)) return NULL
;
2031 status
= wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN_IF
, FALSE
);
2032 RtlFreeUnicodeString( &nt_name
);
2033 if (status
&& status
!= STATUS_NO_SUCH_FILE
)
2035 SetLastError( RtlNtStatusToDosError( status
) );
2038 return unix_name
.Buffer
;
2042 /***********************************************************************
2043 * wine_get_dos_file_name (KERNEL32.@) Not a Windows API
2045 * Return the full DOS file name for a given Unix path.
2046 * Returned buffer must be freed by caller.
2048 WCHAR
* CDECL
wine_get_dos_file_name( LPCSTR str
)
2050 UNICODE_STRING nt_name
;
2051 ANSI_STRING unix_name
;
2055 RtlInitAnsiString( &unix_name
, str
);
2056 status
= wine_unix_to_nt_file_name( &unix_name
, &nt_name
);
2059 SetLastError( RtlNtStatusToDosError( status
) );
2062 if (nt_name
.Buffer
[5] == ':')
2064 /* get rid of the \??\ prefix */
2065 /* FIXME: should implement RtlNtPathNameToDosPathName and use that instead */
2066 len
= nt_name
.Length
- 4 * sizeof(WCHAR
);
2067 memmove( nt_name
.Buffer
, nt_name
.Buffer
+ 4, len
);
2068 nt_name
.Buffer
[len
/ sizeof(WCHAR
)] = 0;
2071 nt_name
.Buffer
[1] = '\\';
2072 return nt_name
.Buffer
;
2075 /*************************************************************************
2076 * CreateSymbolicLinkW (KERNEL32.@)
2078 BOOLEAN WINAPI
CreateSymbolicLinkW(LPCWSTR link
, LPCWSTR target
, DWORD flags
)
2080 FIXME("(%s %s %d): stub\n", debugstr_w(link
), debugstr_w(target
), flags
);
2084 /*************************************************************************
2085 * CreateSymbolicLinkA (KERNEL32.@)
2087 BOOLEAN WINAPI
CreateSymbolicLinkA(LPCSTR link
, LPCSTR target
, DWORD flags
)
2089 FIXME("(%s %s %d): stub\n", debugstr_a(link
), debugstr_a(target
), flags
);
2093 /*************************************************************************
2094 * CreateHardLinkTransactedA (KERNEL32.@)
2096 BOOL WINAPI
CreateHardLinkTransactedA(LPCSTR link
, LPCSTR target
, LPSECURITY_ATTRIBUTES sa
, HANDLE transaction
)
2098 FIXME("(%s %s %p %p): stub\n", debugstr_a(link
), debugstr_a(target
), sa
, transaction
);
2099 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
2103 /*************************************************************************
2104 * CreateHardLinkTransactedW (KERNEL32.@)
2106 BOOL WINAPI
CreateHardLinkTransactedW(LPCWSTR link
, LPCWSTR target
, LPSECURITY_ATTRIBUTES sa
, HANDLE transaction
)
2108 FIXME("(%s %s %p %p): stub\n", debugstr_w(link
), debugstr_w(target
), sa
, transaction
);
2109 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
2113 /*************************************************************************
2114 * CheckNameLegalDOS8Dot3A (KERNEL32.@)
2116 BOOL WINAPI
CheckNameLegalDOS8Dot3A(const char *name
, char *oemname
, DWORD oemname_len
,
2117 BOOL
*contains_spaces
, BOOL
*is_legal
)
2121 TRACE("(%s %p %u %p %p)\n", name
, oemname
,
2122 oemname_len
, contains_spaces
, is_legal
);
2124 if (!name
|| !is_legal
)
2127 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
2129 return CheckNameLegalDOS8Dot3W( nameW
, oemname
, oemname_len
, contains_spaces
, is_legal
);
2132 /*************************************************************************
2133 * CheckNameLegalDOS8Dot3W (KERNEL32.@)
2135 BOOL WINAPI
CheckNameLegalDOS8Dot3W(const WCHAR
*name
, char *oemname
, DWORD oemname_len
,
2136 BOOL
*contains_spaces_ret
, BOOL
*is_legal
)
2139 UNICODE_STRING nameW
;
2140 BOOLEAN contains_spaces
;
2142 TRACE("(%s %p %u %p %p)\n", wine_dbgstr_w(name
), oemname
,
2143 oemname_len
, contains_spaces_ret
, is_legal
);
2145 if (!name
|| !is_legal
)
2148 RtlInitUnicodeString( &nameW
, name
);
2151 oem_str
.Length
= oemname_len
;
2152 oem_str
.MaximumLength
= oemname_len
;
2153 oem_str
.Buffer
= oemname
;
2156 *is_legal
= RtlIsNameLegalDOS8Dot3( &nameW
, oemname
? &oem_str
: NULL
, &contains_spaces
);
2157 if (contains_spaces_ret
) *contains_spaces_ret
= contains_spaces
;
2162 /*************************************************************************
2163 * SetSearchPathMode (KERNEL32.@)
2165 BOOL WINAPI
SetSearchPathMode( DWORD flags
)
2171 case BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE
:
2174 case BASE_SEARCH_PATH_DISABLE_SAFE_SEARCHMODE
:
2177 case BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE
| BASE_SEARCH_PATH_PERMANENT
:
2178 InterlockedExchange( &path_safe_mode
, 2 );
2181 SetLastError( ERROR_INVALID_PARAMETER
);
2187 int prev
= path_safe_mode
;
2188 if (prev
== 2) break; /* permanently set */
2189 if (InterlockedCompareExchange( &path_safe_mode
, val
, prev
) == prev
) return TRUE
;
2191 SetLastError( ERROR_ACCESS_DENIED
);