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
47 /* check if a file name is for an executable file (.exe or .com) */
48 static inline BOOL
is_executable( const WCHAR
*name
)
50 static const WCHAR exeW
[] = {'.','e','x','e',0};
51 static const WCHAR comW
[] = {'.','c','o','m',0};
52 int len
= strlenW(name
);
54 if (len
< 4) return FALSE
;
55 return (!strcmpiW( name
+ len
- 4, exeW
) || !strcmpiW( name
+ len
- 4, comW
));
58 /***********************************************************************
61 * copy a file name back to OEM/Ansi, but only if the buffer is large enough
63 static DWORD
copy_filename_WtoA( LPCWSTR nameW
, LPSTR buffer
, DWORD len
)
67 BOOL is_ansi
= AreFileApisANSI();
69 RtlInitUnicodeString( &strW
, nameW
);
71 ret
= is_ansi
? RtlUnicodeStringToAnsiSize(&strW
) : RtlUnicodeStringToOemSize(&strW
);
72 if (buffer
&& ret
<= len
)
77 str
.MaximumLength
= min( len
, UNICODE_STRING_MAX_CHARS
);
79 RtlUnicodeStringToAnsiString( &str
, &strW
, FALSE
);
81 RtlUnicodeStringToOemString( &str
, &strW
, FALSE
);
82 ret
= str
.Length
; /* length without terminating 0 */
87 /***********************************************************************
88 * add_boot_rename_entry
90 * Adds an entry to the registry that is loaded when windows boots and
91 * checks if there are some files to be removed or renamed/moved.
92 * <fn1> has to be valid and <fn2> may be NULL. If both pointers are
93 * non-NULL then the file is moved, otherwise it is deleted. The
94 * entry of the registry key is always appended with two zero
95 * terminated strings. If <fn2> is NULL then the second entry is
96 * simply a single 0-byte. Otherwise the second filename goes
97 * there. The entries are prepended with \??\ before the path and the
98 * second filename gets also a '!' as the first character if
99 * MOVEFILE_REPLACE_EXISTING is set. After the final string another
100 * 0-byte follows to indicate the end of the strings.
102 * \??\D:\test\file1[0]
103 * !\??\D:\test\file1_renamed[0]
104 * \??\D:\Test|delete[0]
105 * [0] <- file is to be deleted, second string empty
106 * \??\D:\test\file2[0]
107 * !\??\D:\test\file2_renamed[0]
108 * [0] <- indicates end of strings
111 * \??\D:\test\file1[0]
112 * !\??\D:\test\file1_renamed[0]
113 * \??\D:\Test|delete[0]
114 * [0] <- file is to be deleted, second string empty
115 * [0] <- indicates end of strings
118 static BOOL
add_boot_rename_entry( LPCWSTR source
, LPCWSTR dest
, DWORD flags
)
120 static const WCHAR ValueName
[] = {'P','e','n','d','i','n','g',
121 'F','i','l','e','R','e','n','a','m','e',
122 'O','p','e','r','a','t','i','o','n','s',0};
123 static const WCHAR SessionW
[] = {'\\','R','e','g','i','s','t','r','y','\\',
124 'M','a','c','h','i','n','e','\\',
125 'S','y','s','t','e','m','\\',
126 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
127 'C','o','n','t','r','o','l','\\',
128 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
129 static const int info_size
= FIELD_OFFSET( KEY_VALUE_PARTIAL_INFORMATION
, Data
);
131 OBJECT_ATTRIBUTES attr
;
132 UNICODE_STRING nameW
, source_name
, dest_name
;
133 KEY_VALUE_PARTIAL_INFORMATION
*info
;
141 if (!RtlDosPathNameToNtPathName_U( source
, &source_name
, NULL
, NULL
))
143 SetLastError( ERROR_PATH_NOT_FOUND
);
146 dest_name
.Buffer
= NULL
;
147 if (dest
&& !RtlDosPathNameToNtPathName_U( dest
, &dest_name
, NULL
, NULL
))
149 RtlFreeUnicodeString( &source_name
);
150 SetLastError( ERROR_PATH_NOT_FOUND
);
154 attr
.Length
= sizeof(attr
);
155 attr
.RootDirectory
= 0;
156 attr
.ObjectName
= &nameW
;
158 attr
.SecurityDescriptor
= NULL
;
159 attr
.SecurityQualityOfService
= NULL
;
160 RtlInitUnicodeString( &nameW
, SessionW
);
162 if (NtCreateKey( &Reboot
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, NULL
) != STATUS_SUCCESS
)
164 WARN("Error creating key for reboot management [%s]\n",
165 "SYSTEM\\CurrentControlSet\\Control\\Session Manager");
166 RtlFreeUnicodeString( &source_name
);
167 RtlFreeUnicodeString( &dest_name
);
171 len1
= source_name
.Length
+ sizeof(WCHAR
);
174 len2
= dest_name
.Length
+ sizeof(WCHAR
);
175 if (flags
& MOVEFILE_REPLACE_EXISTING
)
176 len2
+= sizeof(WCHAR
); /* Plus 1 because of the leading '!' */
178 else len2
= sizeof(WCHAR
); /* minimum is the 0 characters for the empty second string */
180 RtlInitUnicodeString( &nameW
, ValueName
);
182 /* First we check if the key exists and if so how many bytes it already contains. */
183 if (NtQueryValueKey( Reboot
, &nameW
, KeyValuePartialInformation
,
184 NULL
, 0, &DataSize
) == STATUS_BUFFER_TOO_SMALL
)
186 if (!(Buffer
= HeapAlloc( GetProcessHeap(), 0, DataSize
+ len1
+ len2
+ sizeof(WCHAR
) )))
188 if (NtQueryValueKey( Reboot
, &nameW
, KeyValuePartialInformation
,
189 Buffer
, DataSize
, &DataSize
)) goto Quit
;
190 info
= (KEY_VALUE_PARTIAL_INFORMATION
*)Buffer
;
191 if (info
->Type
!= REG_MULTI_SZ
) goto Quit
;
192 if (DataSize
> sizeof(info
)) DataSize
-= sizeof(WCHAR
); /* remove terminating null (will be added back later) */
196 DataSize
= info_size
;
197 if (!(Buffer
= HeapAlloc( GetProcessHeap(), 0, DataSize
+ len1
+ len2
+ sizeof(WCHAR
) )))
201 memcpy( Buffer
+ DataSize
, source_name
.Buffer
, len1
);
203 p
= (WCHAR
*)(Buffer
+ DataSize
);
206 if (flags
& MOVEFILE_REPLACE_EXISTING
)
208 memcpy( p
, dest_name
.Buffer
, len2
);
214 DataSize
+= sizeof(WCHAR
);
218 p
= (WCHAR
*)(Buffer
+ DataSize
);
220 DataSize
+= sizeof(WCHAR
);
222 rc
= !NtSetValueKey(Reboot
, &nameW
, 0, REG_MULTI_SZ
, Buffer
+ info_size
, DataSize
- info_size
);
225 RtlFreeUnicodeString( &source_name
);
226 RtlFreeUnicodeString( &dest_name
);
227 if (Reboot
) NtClose(Reboot
);
228 HeapFree( GetProcessHeap(), 0, Buffer
);
233 /***********************************************************************
234 * GetFullPathNameW (KERNEL32.@)
236 * if the path closed with '\', *lastpart is 0
238 DWORD WINAPI
GetFullPathNameW( LPCWSTR name
, DWORD len
, LPWSTR buffer
,
241 return RtlGetFullPathName_U(name
, len
* sizeof(WCHAR
), buffer
, lastpart
) / sizeof(WCHAR
);
244 /***********************************************************************
245 * GetFullPathNameA (KERNEL32.@)
247 * if the path closed with '\', *lastpart is 0
249 DWORD WINAPI
GetFullPathNameA( LPCSTR name
, DWORD len
, LPSTR buffer
,
253 WCHAR bufferW
[MAX_PATH
], *lastpartW
= NULL
;
256 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return 0;
258 ret
= GetFullPathNameW( nameW
, MAX_PATH
, bufferW
, &lastpartW
);
263 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
266 ret
= copy_filename_WtoA( bufferW
, buffer
, len
);
267 if (ret
< len
&& lastpart
)
270 *lastpart
= buffer
+ FILE_name_WtoA( bufferW
, lastpartW
- bufferW
, NULL
, 0 );
278 /***********************************************************************
279 * GetLongPathNameW (KERNEL32.@)
282 * observed (Win2000):
283 * shortpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
284 * shortpath="": LastError=ERROR_PATH_NOT_FOUND, ret=0
286 DWORD WINAPI
GetLongPathNameW( LPCWSTR shortpath
, LPWSTR longpath
, DWORD longlen
)
288 WCHAR tmplongpath
[MAX_PATHNAME_LEN
];
290 DWORD sp
= 0, lp
= 0;
293 WIN32_FIND_DATAW wfd
;
299 SetLastError(ERROR_INVALID_PARAMETER
);
304 SetLastError(ERROR_PATH_NOT_FOUND
);
308 TRACE("%s,%p,%d\n", debugstr_w(shortpath
), longpath
, longlen
);
310 if (shortpath
[0] == '\\' && shortpath
[1] == '\\')
312 FIXME("UNC pathname %s\n", debugstr_w(shortpath
));
314 tmplen
= strlenW(shortpath
);
315 if (tmplen
< longlen
)
317 if (longpath
!= shortpath
) strcpyW( longpath
, shortpath
);
323 unixabsolute
= (shortpath
[0] == '/');
325 /* check for drive letter */
326 if (!unixabsolute
&& shortpath
[1] == ':' )
328 tmplongpath
[0] = shortpath
[0];
329 tmplongpath
[1] = ':';
333 while (shortpath
[sp
])
335 /* check for path delimiters and reproduce them */
336 if (shortpath
[sp
] == '\\' || shortpath
[sp
] == '/')
338 if (!lp
|| (tmplongpath
[lp
-1] != '\\' && tmplongpath
[lp
-1] != '/'))
340 /* strip double delimiters */
341 tmplongpath
[lp
++] = shortpath
[sp
];
343 tmplongpath
[lp
] = 0; /* terminate string */
349 if (sp
== 0 && p
[0] == '.' && (p
[1] == '/' || p
[1] == '\\'))
351 tmplongpath
[lp
++] = *p
++;
352 tmplongpath
[lp
++] = *p
++;
355 for (; *p
&& *p
!= '/' && *p
!= '\\'; p
++);
356 tmplen
= p
- (shortpath
+ sp
);
357 lstrcpynW(tmplongpath
+ lp
, shortpath
+ sp
, tmplen
+ 1);
359 if (tmplongpath
[lp
] == '.')
361 if (tmplen
== 1 || (tmplen
== 2 && tmplongpath
[lp
+ 1] == '.'))
369 /* Check if the file exists */
370 goit
= FindFirstFileW(tmplongpath
, &wfd
);
371 if (goit
== INVALID_HANDLE_VALUE
)
373 TRACE("not found %s!\n", debugstr_w(tmplongpath
));
374 SetLastError ( ERROR_FILE_NOT_FOUND
);
379 is_legal_8dot3
= FALSE
;
380 CheckNameLegalDOS8Dot3W(tmplongpath
+ lp
, NULL
, 0, NULL
, &is_legal_8dot3
);
381 /* Use the existing file name if it's a short name */
383 strcpyW(tmplongpath
+ lp
, wfd
.cFileName
);
384 lp
+= strlenW(tmplongpath
+ lp
);
387 tmplen
= strlenW(shortpath
) - 1;
388 if ((shortpath
[tmplen
] == '/' || shortpath
[tmplen
] == '\\') &&
389 (tmplongpath
[lp
- 1] != '/' && tmplongpath
[lp
- 1] != '\\'))
390 tmplongpath
[lp
++] = shortpath
[tmplen
];
393 tmplen
= strlenW(tmplongpath
) + 1;
394 if (tmplen
<= longlen
)
396 strcpyW(longpath
, tmplongpath
);
397 TRACE("returning %s\n", debugstr_w(longpath
));
398 tmplen
--; /* length without 0 */
404 /***********************************************************************
405 * GetLongPathNameA (KERNEL32.@)
407 DWORD WINAPI
GetLongPathNameA( LPCSTR shortpath
, LPSTR longpath
, DWORD longlen
)
410 WCHAR longpathW
[MAX_PATH
];
413 TRACE("%s\n", debugstr_a(shortpath
));
415 if (!(shortpathW
= FILE_name_AtoW( shortpath
, FALSE
))) return 0;
417 ret
= GetLongPathNameW(shortpathW
, longpathW
, MAX_PATH
);
422 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
425 return copy_filename_WtoA( longpathW
, longpath
, longlen
);
429 /***********************************************************************
430 * GetShortPathNameW (KERNEL32.@)
434 * longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
435 * longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
437 * more observations ( with NT 3.51 (WinDD) ):
438 * longpath <= 8.3 -> just copy longpath to shortpath
440 * a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
441 * b) file does exist -> set the short filename.
442 * - trailing slashes are reproduced in the short name, even if the
443 * file is not a directory
444 * - the absolute/relative path of the short name is reproduced like found
446 * - longpath and shortpath may have the same address
449 DWORD WINAPI
GetShortPathNameW( LPCWSTR longpath
, LPWSTR shortpath
, DWORD shortlen
)
453 DWORD sp
= 0, lp
= 0;
454 DWORD tmplen
, buf_len
;
455 WIN32_FIND_DATAW wfd
;
458 TRACE("%s\n", debugstr_w(longpath
));
462 SetLastError(ERROR_INVALID_PARAMETER
);
467 SetLastError(ERROR_BAD_PATHNAME
);
471 /* code below only removes characters from string, never adds, so this is
472 * the largest buffer that tmpshortpath will need to have */
473 buf_len
= strlenW(longpath
) + 1;
474 tmpshortpath
= HeapAlloc(GetProcessHeap(), 0, buf_len
* sizeof(WCHAR
));
477 SetLastError(ERROR_OUTOFMEMORY
);
481 if (longpath
[0] == '\\' && longpath
[1] == '\\' && longpath
[2] == '?' && longpath
[3] == '\\')
483 memcpy(tmpshortpath
, longpath
, 4 * sizeof(WCHAR
));
487 /* check for drive letter */
488 if (longpath
[lp
] != '/' && longpath
[lp
+ 1] == ':' )
490 tmpshortpath
[sp
] = longpath
[lp
];
491 tmpshortpath
[sp
+ 1] = ':';
498 /* check for path delimiters and reproduce them */
499 if (longpath
[lp
] == '\\' || longpath
[lp
] == '/')
501 if (!sp
|| (tmpshortpath
[sp
-1] != '\\' && tmpshortpath
[sp
-1] != '/'))
503 /* strip double delimiters */
504 tmpshortpath
[sp
] = longpath
[lp
];
507 tmpshortpath
[sp
] = 0; /* terminate string */
513 if (lp
== 0 && p
[0] == '.' && (p
[1] == '/' || p
[1] == '\\'))
515 tmpshortpath
[sp
++] = *p
++;
516 tmpshortpath
[sp
++] = *p
++;
519 for (; *p
&& *p
!= '/' && *p
!= '\\'; p
++);
520 tmplen
= p
- (longpath
+ lp
);
521 lstrcpynW(tmpshortpath
+ sp
, longpath
+ lp
, tmplen
+ 1);
523 if (tmpshortpath
[sp
] == '.')
525 if (tmplen
== 1 || (tmplen
== 2 && tmpshortpath
[sp
+ 1] == '.'))
533 /* Check if the file exists and use the existing short file name */
534 goit
= FindFirstFileW(tmpshortpath
, &wfd
);
535 if (goit
== INVALID_HANDLE_VALUE
) goto notfound
;
538 /* In rare cases (like "a.abcd") short path may be longer than original path.
539 * Make sure we have enough space in temp buffer. */
540 if (wfd
.cAlternateFileName
[0] && tmplen
< strlenW(wfd
.cAlternateFileName
))
543 buf_len
+= strlenW(wfd
.cAlternateFileName
) - tmplen
;
544 new_buf
= HeapReAlloc(GetProcessHeap(), 0, tmpshortpath
, buf_len
* sizeof(WCHAR
));
547 HeapFree(GetProcessHeap(), 0, tmpshortpath
);
548 SetLastError(ERROR_OUTOFMEMORY
);
551 tmpshortpath
= new_buf
;
554 strcpyW(tmpshortpath
+ sp
, wfd
.cAlternateFileName
[0] ? wfd
.cAlternateFileName
: wfd
.cFileName
);
555 sp
+= strlenW(tmpshortpath
+ sp
);
558 tmpshortpath
[sp
] = 0;
560 tmplen
= strlenW(tmpshortpath
) + 1;
561 if (tmplen
<= shortlen
)
563 strcpyW(shortpath
, tmpshortpath
);
564 TRACE("returning %s\n", debugstr_w(shortpath
));
565 tmplen
--; /* length without 0 */
568 HeapFree(GetProcessHeap(), 0, tmpshortpath
);
572 HeapFree(GetProcessHeap(), 0, tmpshortpath
);
573 TRACE("not found!\n" );
574 SetLastError ( ERROR_FILE_NOT_FOUND
);
578 /***********************************************************************
579 * GetShortPathNameA (KERNEL32.@)
581 DWORD WINAPI
GetShortPathNameA( LPCSTR longpath
, LPSTR shortpath
, DWORD shortlen
)
584 WCHAR shortpathW
[MAX_PATH
];
587 TRACE("%s\n", debugstr_a(longpath
));
589 if (!(longpathW
= FILE_name_AtoW( longpath
, FALSE
))) return 0;
591 ret
= GetShortPathNameW(longpathW
, shortpathW
, MAX_PATH
);
596 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
599 return copy_filename_WtoA( shortpathW
, shortpath
, shortlen
);
603 /***********************************************************************
604 * GetTempPathA (KERNEL32.@)
606 DWORD WINAPI
GetTempPathA( DWORD count
, LPSTR path
)
608 WCHAR pathW
[MAX_PATH
];
611 ret
= GetTempPathW(MAX_PATH
, pathW
);
618 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
621 return copy_filename_WtoA( pathW
, path
, count
);
625 /***********************************************************************
626 * GetTempPathW (KERNEL32.@)
628 DWORD WINAPI
GetTempPathW( DWORD count
, LPWSTR path
)
630 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
631 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
632 static const WCHAR userprofile
[] = { 'U','S','E','R','P','R','O','F','I','L','E',0 };
633 WCHAR tmp_path
[MAX_PATH
];
636 TRACE("%u,%p\n", count
, path
);
638 if (!(ret
= GetEnvironmentVariableW( tmp
, tmp_path
, MAX_PATH
)) &&
639 !(ret
= GetEnvironmentVariableW( temp
, tmp_path
, MAX_PATH
)) &&
640 !(ret
= GetEnvironmentVariableW( userprofile
, tmp_path
, MAX_PATH
)) &&
641 !(ret
= GetWindowsDirectoryW( tmp_path
, MAX_PATH
)))
646 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
650 ret
= GetFullPathNameW(tmp_path
, MAX_PATH
, tmp_path
, NULL
);
653 if (ret
> MAX_PATH
- 2)
655 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
659 if (tmp_path
[ret
-1] != '\\')
661 tmp_path
[ret
++] = '\\';
662 tmp_path
[ret
] = '\0';
665 ret
++; /* add space for terminating 0 */
669 lstrcpynW(path
, tmp_path
, count
);
670 /* the remaining buffer must be zeroed up to 32766 bytes in XP or 32767
671 * bytes after it, we will assume the > XP behavior for now */
672 memset(path
+ ret
, 0, (min(count
, 32767) - ret
) * sizeof(WCHAR
));
673 ret
--; /* return length without 0 */
677 /* the buffer must be cleared if contents will not fit */
678 memset(path
, 0, count
* sizeof(WCHAR
));
681 TRACE("returning %u, %s\n", ret
, debugstr_w(path
));
686 /***********************************************************************
687 * GetTempFileNameA (KERNEL32.@)
689 UINT WINAPI
GetTempFileNameA( LPCSTR path
, LPCSTR prefix
, UINT unique
, LPSTR buffer
)
691 WCHAR
*pathW
, *prefixW
= NULL
;
692 WCHAR bufferW
[MAX_PATH
];
695 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return 0;
696 if (prefix
&& !(prefixW
= FILE_name_AtoW( prefix
, TRUE
))) return 0;
698 ret
= GetTempFileNameW(pathW
, prefixW
, unique
, bufferW
);
699 if (ret
) FILE_name_WtoA( bufferW
, -1, buffer
, MAX_PATH
);
701 HeapFree( GetProcessHeap(), 0, prefixW
);
705 /***********************************************************************
706 * GetTempFileNameW (KERNEL32.@)
708 UINT WINAPI
GetTempFileNameW( LPCWSTR path
, LPCWSTR prefix
, UINT unique
, LPWSTR buffer
)
710 static const WCHAR formatW
[] = {'%','x','.','t','m','p',0};
716 if ( !path
|| !buffer
)
718 SetLastError( ERROR_INVALID_PARAMETER
);
722 /* ensure that the provided directory exists */
723 attr
= GetFileAttributesW(path
);
724 if (attr
== INVALID_FILE_ATTRIBUTES
|| !(attr
& FILE_ATTRIBUTE_DIRECTORY
))
726 TRACE("path not found %s\n", debugstr_w(path
));
727 SetLastError( ERROR_DIRECTORY
);
731 strcpyW( buffer
, path
);
732 p
= buffer
+ strlenW(buffer
);
734 /* add a \, if there isn't one */
735 if ((p
== buffer
) || (p
[-1] != '\\')) *p
++ = '\\';
738 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
742 if (unique
) sprintfW( p
, formatW
, unique
);
745 /* get a "random" unique number and try to create the file */
747 UINT num
= GetTickCount() & 0xffff;
750 /* avoid using the same name twice in a short interval */
751 if (last
- num
< 10) num
= last
+ 1;
756 sprintfW( p
, formatW
, unique
);
757 handle
= CreateFileW( buffer
, GENERIC_WRITE
, 0, NULL
,
758 CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, 0 );
759 if (handle
!= INVALID_HANDLE_VALUE
)
760 { /* We created it */
761 TRACE("created %s\n", debugstr_w(buffer
) );
762 CloseHandle( handle
);
766 if (GetLastError() != ERROR_FILE_EXISTS
&&
767 GetLastError() != ERROR_SHARING_VIOLATION
)
768 break; /* No need to go on */
769 if (!(++unique
& 0xffff)) unique
= 1;
770 } while (unique
!= num
);
773 TRACE("returning %s\n", debugstr_w(buffer
) );
778 /***********************************************************************
781 * Check if the file name contains a path; helper for SearchPathW.
782 * A relative path is not considered a path unless it starts with ./ or ../
784 static inline BOOL
contains_pathW (LPCWSTR name
)
786 if (RtlDetermineDosPathNameType_U( name
) != RELATIVE_PATH
) return TRUE
;
787 if (name
[0] != '.') return FALSE
;
788 if (name
[1] == '/' || name
[1] == '\\') return TRUE
;
789 return (name
[1] == '.' && (name
[2] == '/' || name
[2] == '\\'));
792 /***********************************************************************
793 * find_actctx_dllpath
795 * Find the path (if any) of the dll from the activation context.
796 * Returned path doesn't include a name.
798 static NTSTATUS
find_actctx_dllpath(const WCHAR
*libname
, WCHAR
**path
)
800 static const WCHAR winsxsW
[] = {'\\','w','i','n','s','x','s','\\'};
801 static const WCHAR dotManifestW
[] = {'.','m','a','n','i','f','e','s','t',0};
803 ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION
*info
;
804 ACTCTX_SECTION_KEYED_DATA data
;
805 UNICODE_STRING nameW
;
807 SIZE_T needed
, size
= 1024;
810 RtlInitUnicodeString( &nameW
, libname
);
811 data
.cbSize
= sizeof(data
);
812 status
= RtlFindActivationContextSectionString( FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX
, NULL
,
813 ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION
,
815 if (status
!= STATUS_SUCCESS
) return status
;
819 if (!(info
= HeapAlloc( GetProcessHeap(), 0, size
)))
821 status
= STATUS_NO_MEMORY
;
824 status
= RtlQueryInformationActivationContext( 0, data
.hActCtx
, &data
.ulAssemblyRosterIndex
,
825 AssemblyDetailedInformationInActivationContext
,
826 info
, size
, &needed
);
827 if (status
== STATUS_SUCCESS
) break;
828 if (status
!= STATUS_BUFFER_TOO_SMALL
) goto done
;
829 HeapFree( GetProcessHeap(), 0, info
);
831 /* restart with larger buffer */
834 if (!info
->lpAssemblyManifestPath
|| !info
->lpAssemblyDirectoryName
)
836 status
= STATUS_SXS_KEY_NOT_FOUND
;
840 if ((p
= strrchrW( info
->lpAssemblyManifestPath
, '\\' )))
842 DWORD dirlen
= info
->ulAssemblyDirectoryNameLength
/ sizeof(WCHAR
);
845 if (strncmpiW( p
, info
->lpAssemblyDirectoryName
, dirlen
) || strcmpiW( p
+ dirlen
, dotManifestW
))
847 /* manifest name does not match directory name, so it's not a global
848 * windows/winsxs manifest; use the manifest directory name instead */
849 dirlen
= p
- info
->lpAssemblyManifestPath
;
850 needed
= (dirlen
+ 1) * sizeof(WCHAR
);
851 if (!(*path
= p
= HeapAlloc( GetProcessHeap(), 0, needed
)))
853 status
= STATUS_NO_MEMORY
;
856 memcpy( p
, info
->lpAssemblyManifestPath
, dirlen
* sizeof(WCHAR
) );
862 needed
= (strlenW( DIR_Windows
) * sizeof(WCHAR
) +
863 sizeof(winsxsW
) + info
->ulAssemblyDirectoryNameLength
+ 2*sizeof(WCHAR
));
865 if (!(*path
= p
= HeapAlloc( GetProcessHeap(), 0, needed
)))
867 status
= STATUS_NO_MEMORY
;
870 strcpyW( p
, DIR_Windows
);
872 memcpy( p
, winsxsW
, sizeof(winsxsW
) );
873 p
+= sizeof(winsxsW
) / sizeof(WCHAR
);
874 memcpy( p
, info
->lpAssemblyDirectoryName
, info
->ulAssemblyDirectoryNameLength
);
875 p
+= info
->ulAssemblyDirectoryNameLength
/ sizeof(WCHAR
);
879 HeapFree( GetProcessHeap(), 0, info
);
880 RtlReleaseActivationContext( data
.hActCtx
);
884 /***********************************************************************
885 * SearchPathW [KERNEL32.@]
887 * Searches for a specified file in the search path.
890 * path [I] Path to search (NULL means default)
891 * name [I] Filename to search for.
892 * ext [I] File extension to append to file name. The first
893 * character must be a period. This parameter is
894 * specified only if the filename given does not
895 * contain an extension.
896 * buflen [I] size of buffer, in characters
897 * buffer [O] buffer for found filename
898 * lastpart [O] address of pointer to last used character in
899 * buffer (the final '\')
902 * Success: length of string copied into buffer, not including
903 * terminating null character. If the filename found is
904 * longer than the length of the buffer, the length of the
905 * filename is returned.
909 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
912 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
, DWORD buflen
,
913 LPWSTR buffer
, LPWSTR
*lastpart
)
917 if (!name
|| !name
[0])
919 SetLastError(ERROR_INVALID_PARAMETER
);
923 /* If the name contains an explicit path, ignore the path */
925 if (contains_pathW(name
))
927 /* try first without extension */
928 if (RtlDoesFileExists_U( name
))
929 return GetFullPathNameW( name
, buflen
, buffer
, lastpart
);
933 LPCWSTR p
= strrchrW( name
, '.' );
934 if (p
&& !strchrW( p
, '/' ) && !strchrW( p
, '\\' ))
935 ext
= NULL
; /* Ignore the specified extension */
938 /* Allocate a buffer for the file name and extension */
942 DWORD len
= strlenW(name
) + strlenW(ext
);
944 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) )))
946 SetLastError( ERROR_OUTOFMEMORY
);
949 strcpyW( tmp
, name
);
951 if (RtlDoesFileExists_U( tmp
))
952 ret
= GetFullPathNameW( tmp
, buflen
, buffer
, lastpart
);
953 HeapFree( GetProcessHeap(), 0, tmp
);
956 else if (path
&& path
[0]) /* search in the specified path */
958 ret
= RtlDosSearchPath_U( path
, name
, ext
, buflen
* sizeof(WCHAR
),
959 buffer
, lastpart
) / sizeof(WCHAR
);
961 else /* search in active context and default path */
963 WCHAR
*dll_path
= NULL
, *search
= NULL
;
964 DWORD req_len
, name_len
;
966 req_len
= name_len
= strlenW(name
);
968 if (strchrW( name
, '.' )) ext
= NULL
;
971 DWORD ext_len
= strlenW(ext
);
976 search
= HeapAlloc( GetProcessHeap(), 0, (name_len
+ ext_len
+ 1) * sizeof(WCHAR
) );
979 SetLastError( ERROR_OUTOFMEMORY
);
982 strcpyW( search
, name
);
983 strcatW( search
, ext
);
986 /* now that we have combined name we don't need extension any more */
989 /* When file is found with activation context no attempt is made
990 to check if it's really exist, path is returned only basing on context info. */
991 if (find_actctx_dllpath( name
, &dll_path
) == STATUS_SUCCESS
)
995 path_len
= strlenW(dll_path
);
998 if (lastpart
) *lastpart
= NULL
;
1000 /* count null termination char too */
1001 if (req_len
+ 1 <= buflen
)
1003 memcpy( buffer
, dll_path
, path_len
* sizeof(WCHAR
) );
1004 memcpy( &buffer
[path_len
], name
, name_len
* sizeof(WCHAR
) );
1005 buffer
[req_len
] = 0;
1006 if (lastpart
) *lastpart
= buffer
+ path_len
;
1012 HeapFree( GetProcessHeap(), 0, dll_path
);
1013 HeapFree( GetProcessHeap(), 0, search
);
1017 if ((dll_path
= MODULE_get_dll_load_path( NULL
)))
1019 ret
= RtlDosSearchPath_U( dll_path
, name
, NULL
, buflen
* sizeof(WCHAR
),
1020 buffer
, lastpart
) / sizeof(WCHAR
);
1021 HeapFree( GetProcessHeap(), 0, dll_path
);
1022 HeapFree( GetProcessHeap(), 0, search
);
1026 SetLastError( ERROR_OUTOFMEMORY
);
1032 if (!ret
) SetLastError( ERROR_FILE_NOT_FOUND
);
1033 else TRACE( "found %s\n", debugstr_w(buffer
) );
1038 /***********************************************************************
1039 * SearchPathA (KERNEL32.@)
1043 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
1044 DWORD buflen
, LPSTR buffer
, LPSTR
*lastpart
)
1046 WCHAR
*pathW
= NULL
, *nameW
, *extW
= NULL
;
1047 WCHAR bufferW
[MAX_PATH
];
1052 SetLastError(ERROR_INVALID_PARAMETER
);
1056 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return 0;
1057 if (path
&& !(pathW
= FILE_name_AtoW( path
, TRUE
))) return 0;
1059 if (ext
&& !(extW
= FILE_name_AtoW( ext
, TRUE
)))
1061 HeapFree( GetProcessHeap(), 0, pathW
);
1065 ret
= SearchPathW(pathW
, nameW
, extW
, MAX_PATH
, bufferW
, NULL
);
1067 HeapFree( GetProcessHeap(), 0, pathW
);
1068 HeapFree( GetProcessHeap(), 0, extW
);
1073 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
1076 ret
= copy_filename_WtoA( bufferW
, buffer
, buflen
);
1077 if (buflen
> ret
&& lastpart
)
1078 *lastpart
= strrchr(buffer
, '\\') + 1;
1082 static BOOL
is_same_file(HANDLE h1
, HANDLE h2
)
1086 if (wine_server_handle_to_fd(h1
, 0, &fd1
, NULL
) == STATUS_SUCCESS
)
1089 if (wine_server_handle_to_fd(h2
, 0, &fd2
, NULL
) == STATUS_SUCCESS
)
1091 struct stat stat1
, stat2
;
1092 if (fstat(fd1
, &stat1
) == 0 && fstat(fd2
, &stat2
) == 0)
1093 ret
= (stat1
.st_dev
== stat2
.st_dev
&& stat1
.st_ino
== stat2
.st_ino
);
1094 wine_server_release_fd(h2
, fd2
);
1096 wine_server_release_fd(h1
, fd1
);
1101 /**************************************************************************
1102 * CopyFileW (KERNEL32.@)
1104 BOOL WINAPI
CopyFileW( LPCWSTR source
, LPCWSTR dest
, BOOL fail_if_exists
)
1106 return CopyFileExW( source
, dest
, NULL
, NULL
, NULL
,
1107 fail_if_exists
? COPY_FILE_FAIL_IF_EXISTS
: 0 );
1111 /**************************************************************************
1112 * CopyFileA (KERNEL32.@)
1114 BOOL WINAPI
CopyFileA( LPCSTR source
, LPCSTR dest
, BOOL fail_if_exists
)
1116 WCHAR
*sourceW
, *destW
;
1119 if (!(sourceW
= FILE_name_AtoW( source
, FALSE
))) return FALSE
;
1120 if (!(destW
= FILE_name_AtoW( dest
, TRUE
))) return FALSE
;
1122 ret
= CopyFileW( sourceW
, destW
, fail_if_exists
);
1124 HeapFree( GetProcessHeap(), 0, destW
);
1129 /**************************************************************************
1130 * CopyFileExW (KERNEL32.@)
1132 BOOL WINAPI
CopyFileExW(LPCWSTR source
, LPCWSTR dest
,
1133 LPPROGRESS_ROUTINE progress
, LPVOID param
,
1134 LPBOOL cancel_ptr
, DWORD flags
)
1136 static const int buffer_size
= 65536;
1138 BY_HANDLE_FILE_INFORMATION info
;
1143 if (!source
|| !dest
)
1145 SetLastError(ERROR_INVALID_PARAMETER
);
1148 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, buffer_size
)))
1150 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1154 TRACE("%s -> %s, %x\n", debugstr_w(source
), debugstr_w(dest
), flags
);
1156 if ((h1
= CreateFileW(source
, GENERIC_READ
,
1157 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1158 NULL
, OPEN_EXISTING
, 0, 0)) == INVALID_HANDLE_VALUE
)
1160 WARN("Unable to open source %s\n", debugstr_w(source
));
1161 HeapFree( GetProcessHeap(), 0, buffer
);
1165 if (!GetFileInformationByHandle( h1
, &info
))
1167 WARN("GetFileInformationByHandle returned error for %s\n", debugstr_w(source
));
1168 HeapFree( GetProcessHeap(), 0, buffer
);
1173 if (!(flags
& COPY_FILE_FAIL_IF_EXISTS
))
1175 BOOL same_file
= FALSE
;
1176 h2
= CreateFileW( dest
, 0, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1177 OPEN_EXISTING
, 0, 0);
1178 if (h2
!= INVALID_HANDLE_VALUE
)
1180 same_file
= is_same_file( h1
, h2
);
1185 HeapFree( GetProcessHeap(), 0, buffer
);
1187 SetLastError( ERROR_SHARING_VIOLATION
);
1192 if ((h2
= CreateFileW( dest
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1193 (flags
& COPY_FILE_FAIL_IF_EXISTS
) ? CREATE_NEW
: CREATE_ALWAYS
,
1194 info
.dwFileAttributes
, h1
)) == INVALID_HANDLE_VALUE
)
1196 WARN("Unable to open dest %s\n", debugstr_w(dest
));
1197 HeapFree( GetProcessHeap(), 0, buffer
);
1202 while (ReadFile( h1
, buffer
, buffer_size
, &count
, NULL
) && count
)
1208 if (!WriteFile( h2
, p
, count
, &res
, NULL
) || !res
) goto done
;
1215 /* Maintain the timestamp of source file to destination file */
1216 SetFileTime(h2
, NULL
, NULL
, &info
.ftLastWriteTime
);
1217 HeapFree( GetProcessHeap(), 0, buffer
);
1224 /**************************************************************************
1225 * CopyFileExA (KERNEL32.@)
1227 BOOL WINAPI
CopyFileExA(LPCSTR sourceFilename
, LPCSTR destFilename
,
1228 LPPROGRESS_ROUTINE progressRoutine
, LPVOID appData
,
1229 LPBOOL cancelFlagPointer
, DWORD copyFlags
)
1231 WCHAR
*sourceW
, *destW
;
1234 /* can't use the TEB buffer since we may have a callback routine */
1235 if (!(sourceW
= FILE_name_AtoW( sourceFilename
, TRUE
))) return FALSE
;
1236 if (!(destW
= FILE_name_AtoW( destFilename
, TRUE
)))
1238 HeapFree( GetProcessHeap(), 0, sourceW
);
1241 ret
= CopyFileExW(sourceW
, destW
, progressRoutine
, appData
,
1242 cancelFlagPointer
, copyFlags
);
1243 HeapFree( GetProcessHeap(), 0, sourceW
);
1244 HeapFree( GetProcessHeap(), 0, destW
);
1249 /**************************************************************************
1250 * MoveFileWithProgressW (KERNEL32.@)
1252 BOOL WINAPI
MoveFileWithProgressW( LPCWSTR source
, LPCWSTR dest
,
1253 LPPROGRESS_ROUTINE fnProgress
,
1254 LPVOID param
, DWORD flag
)
1256 FILE_BASIC_INFORMATION info
;
1257 UNICODE_STRING nt_name
;
1258 OBJECT_ATTRIBUTES attr
;
1261 HANDLE source_handle
= 0, dest_handle
;
1262 ANSI_STRING source_unix
, dest_unix
;
1264 TRACE("(%s,%s,%p,%p,%04x)\n",
1265 debugstr_w(source
), debugstr_w(dest
), fnProgress
, param
, flag
);
1267 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
)
1268 return add_boot_rename_entry( source
, dest
, flag
);
1271 return DeleteFileW( source
);
1273 if (flag
& MOVEFILE_WRITE_THROUGH
)
1274 FIXME("MOVEFILE_WRITE_THROUGH unimplemented\n");
1276 /* check if we are allowed to rename the source */
1278 if (!RtlDosPathNameToNtPathName_U( source
, &nt_name
, NULL
, NULL
))
1280 SetLastError( ERROR_PATH_NOT_FOUND
);
1283 source_unix
.Buffer
= NULL
;
1284 dest_unix
.Buffer
= NULL
;
1285 attr
.Length
= sizeof(attr
);
1286 attr
.RootDirectory
= 0;
1287 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1288 attr
.ObjectName
= &nt_name
;
1289 attr
.SecurityDescriptor
= NULL
;
1290 attr
.SecurityQualityOfService
= NULL
;
1292 status
= NtOpenFile( &source_handle
, SYNCHRONIZE
, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
1293 if (status
== STATUS_SUCCESS
)
1294 status
= wine_nt_to_unix_file_name( &nt_name
, &source_unix
, FILE_OPEN
, FALSE
);
1295 RtlFreeUnicodeString( &nt_name
);
1296 if (status
!= STATUS_SUCCESS
)
1298 SetLastError( RtlNtStatusToDosError(status
) );
1301 status
= NtQueryInformationFile( source_handle
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1302 if (status
!= STATUS_SUCCESS
)
1304 SetLastError( RtlNtStatusToDosError(status
) );
1308 /* we must have write access to the destination, and it must */
1309 /* not exist except if MOVEFILE_REPLACE_EXISTING is set */
1311 if (!RtlDosPathNameToNtPathName_U( dest
, &nt_name
, NULL
, NULL
))
1313 SetLastError( ERROR_PATH_NOT_FOUND
);
1316 status
= NtOpenFile( &dest_handle
, GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
, &attr
, &io
, 0,
1317 FILE_NON_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
1318 if (status
== STATUS_SUCCESS
) /* destination exists */
1320 NtClose( dest_handle
);
1321 if (!(flag
& MOVEFILE_REPLACE_EXISTING
))
1323 SetLastError( ERROR_ALREADY_EXISTS
);
1324 RtlFreeUnicodeString( &nt_name
);
1327 else if (info
.FileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) /* cannot replace directory */
1329 SetLastError( ERROR_ACCESS_DENIED
);
1333 else if (status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
1335 SetLastError( RtlNtStatusToDosError(status
) );
1336 RtlFreeUnicodeString( &nt_name
);
1340 status
= wine_nt_to_unix_file_name( &nt_name
, &dest_unix
, FILE_OPEN_IF
, FALSE
);
1341 RtlFreeUnicodeString( &nt_name
);
1342 if (status
!= STATUS_SUCCESS
&& status
!= STATUS_NO_SUCH_FILE
)
1344 SetLastError( RtlNtStatusToDosError(status
) );
1348 /* now perform the rename */
1350 if (rename( source_unix
.Buffer
, dest_unix
.Buffer
) == -1)
1352 if (errno
== EXDEV
&& (flag
& MOVEFILE_COPY_ALLOWED
))
1354 NtClose( source_handle
);
1355 RtlFreeAnsiString( &source_unix
);
1356 RtlFreeAnsiString( &dest_unix
);
1357 if (!CopyFileExW( source
, dest
, fnProgress
,
1358 param
, NULL
, COPY_FILE_FAIL_IF_EXISTS
))
1360 return DeleteFileW( source
);
1363 /* if we created the destination, remove it */
1364 if (io
.Information
== FILE_CREATED
) unlink( dest_unix
.Buffer
);
1368 /* fixup executable permissions */
1370 if (is_executable( source
) != is_executable( dest
))
1373 if (stat( dest_unix
.Buffer
, &fstat
) != -1)
1375 if (is_executable( dest
))
1376 /* set executable bit where read bit is set */
1377 fstat
.st_mode
|= (fstat
.st_mode
& 0444) >> 2;
1379 fstat
.st_mode
&= ~0111;
1380 chmod( dest_unix
.Buffer
, fstat
.st_mode
);
1384 NtClose( source_handle
);
1385 RtlFreeAnsiString( &source_unix
);
1386 RtlFreeAnsiString( &dest_unix
);
1390 if (source_handle
) NtClose( source_handle
);
1391 RtlFreeAnsiString( &source_unix
);
1392 RtlFreeAnsiString( &dest_unix
);
1396 /**************************************************************************
1397 * MoveFileWithProgressA (KERNEL32.@)
1399 BOOL WINAPI
MoveFileWithProgressA( LPCSTR source
, LPCSTR dest
,
1400 LPPROGRESS_ROUTINE fnProgress
,
1401 LPVOID param
, DWORD flag
)
1403 WCHAR
*sourceW
, *destW
;
1406 if (!(sourceW
= FILE_name_AtoW( source
, FALSE
))) return FALSE
;
1409 if (!(destW
= FILE_name_AtoW( dest
, TRUE
))) return FALSE
;
1414 ret
= MoveFileWithProgressW( sourceW
, destW
, fnProgress
, param
, flag
);
1415 HeapFree( GetProcessHeap(), 0, destW
);
1419 /**************************************************************************
1420 * MoveFileExW (KERNEL32.@)
1422 BOOL WINAPI
MoveFileExW( LPCWSTR source
, LPCWSTR dest
, DWORD flag
)
1424 return MoveFileWithProgressW( source
, dest
, NULL
, NULL
, flag
);
1427 /**************************************************************************
1428 * MoveFileExA (KERNEL32.@)
1430 BOOL WINAPI
MoveFileExA( LPCSTR source
, LPCSTR dest
, DWORD flag
)
1432 return MoveFileWithProgressA( source
, dest
, NULL
, NULL
, flag
);
1436 /**************************************************************************
1437 * MoveFileW (KERNEL32.@)
1439 * Move file or directory
1441 BOOL WINAPI
MoveFileW( LPCWSTR source
, LPCWSTR dest
)
1443 return MoveFileExW( source
, dest
, MOVEFILE_COPY_ALLOWED
);
1447 /**************************************************************************
1448 * MoveFileA (KERNEL32.@)
1450 BOOL WINAPI
MoveFileA( LPCSTR source
, LPCSTR dest
)
1452 return MoveFileExA( source
, dest
, MOVEFILE_COPY_ALLOWED
);
1456 /*************************************************************************
1457 * CreateHardLinkW (KERNEL32.@)
1459 BOOL WINAPI
CreateHardLinkW(LPCWSTR lpFileName
, LPCWSTR lpExistingFileName
,
1460 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
1463 UNICODE_STRING ntDest
, ntSource
;
1464 ANSI_STRING unixDest
, unixSource
;
1467 TRACE("(%s, %s, %p)\n", debugstr_w(lpFileName
),
1468 debugstr_w(lpExistingFileName
), lpSecurityAttributes
);
1470 ntDest
.Buffer
= ntSource
.Buffer
= NULL
;
1471 if (!RtlDosPathNameToNtPathName_U( lpFileName
, &ntDest
, NULL
, NULL
) ||
1472 !RtlDosPathNameToNtPathName_U( lpExistingFileName
, &ntSource
, NULL
, NULL
))
1474 SetLastError( ERROR_PATH_NOT_FOUND
);
1478 unixSource
.Buffer
= unixDest
.Buffer
= NULL
;
1479 status
= wine_nt_to_unix_file_name( &ntSource
, &unixSource
, FILE_OPEN
, FALSE
);
1482 status
= wine_nt_to_unix_file_name( &ntDest
, &unixDest
, FILE_CREATE
, FALSE
);
1483 if (!status
) /* destination must not exist */
1485 status
= STATUS_OBJECT_NAME_EXISTS
;
1486 } else if (status
== STATUS_NO_SUCH_FILE
)
1488 status
= STATUS_SUCCESS
;
1493 SetLastError( RtlNtStatusToDosError(status
) );
1494 else if (!link( unixSource
.Buffer
, unixDest
.Buffer
))
1496 TRACE("Hardlinked '%s' to '%s'\n", debugstr_a( unixDest
.Buffer
),
1497 debugstr_a( unixSource
.Buffer
));
1503 RtlFreeAnsiString( &unixSource
);
1504 RtlFreeAnsiString( &unixDest
);
1507 RtlFreeUnicodeString( &ntSource
);
1508 RtlFreeUnicodeString( &ntDest
);
1513 /*************************************************************************
1514 * CreateHardLinkA (KERNEL32.@)
1516 BOOL WINAPI
CreateHardLinkA(LPCSTR lpFileName
, LPCSTR lpExistingFileName
,
1517 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
1519 WCHAR
*sourceW
, *destW
;
1522 if (!(sourceW
= FILE_name_AtoW( lpExistingFileName
, TRUE
)))
1526 if (!(destW
= FILE_name_AtoW( lpFileName
, TRUE
)))
1528 HeapFree( GetProcessHeap(), 0, sourceW
);
1532 res
= CreateHardLinkW( destW
, sourceW
, lpSecurityAttributes
);
1534 HeapFree( GetProcessHeap(), 0, sourceW
);
1535 HeapFree( GetProcessHeap(), 0, destW
);
1541 /***********************************************************************
1542 * CreateDirectoryW (KERNEL32.@)
1546 * ERROR_DISK_FULL: on full disk
1547 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
1548 * ERROR_ACCESS_DENIED: on permission problems
1549 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
1551 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
, LPSECURITY_ATTRIBUTES sa
)
1553 OBJECT_ATTRIBUTES attr
;
1554 UNICODE_STRING nt_name
;
1560 TRACE( "%s\n", debugstr_w(path
) );
1562 if (!RtlDosPathNameToNtPathName_U( path
, &nt_name
, NULL
, NULL
))
1564 SetLastError( ERROR_PATH_NOT_FOUND
);
1567 attr
.Length
= sizeof(attr
);
1568 attr
.RootDirectory
= 0;
1569 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1570 attr
.ObjectName
= &nt_name
;
1571 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1572 attr
.SecurityQualityOfService
= NULL
;
1574 status
= NtCreateFile( &handle
, GENERIC_READ
| SYNCHRONIZE
, &attr
, &io
, NULL
,
1575 FILE_ATTRIBUTE_NORMAL
, FILE_SHARE_READ
, FILE_CREATE
,
1576 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
, NULL
, 0 );
1578 if (status
== STATUS_SUCCESS
)
1583 else SetLastError( RtlNtStatusToDosError(status
) );
1585 RtlFreeUnicodeString( &nt_name
);
1590 /***********************************************************************
1591 * CreateDirectoryA (KERNEL32.@)
1593 BOOL WINAPI
CreateDirectoryA( LPCSTR path
, LPSECURITY_ATTRIBUTES sa
)
1597 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1598 return CreateDirectoryW( pathW
, sa
);
1602 /***********************************************************************
1603 * CreateDirectoryExA (KERNEL32.@)
1605 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
, LPSECURITY_ATTRIBUTES sa
)
1607 WCHAR
*pathW
, *templateW
= NULL
;
1610 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1611 if (template && !(templateW
= FILE_name_AtoW( template, TRUE
))) return FALSE
;
1613 ret
= CreateDirectoryExW( templateW
, pathW
, sa
);
1614 HeapFree( GetProcessHeap(), 0, templateW
);
1619 /***********************************************************************
1620 * CreateDirectoryExW (KERNEL32.@)
1622 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
, LPSECURITY_ATTRIBUTES sa
)
1624 return CreateDirectoryW( path
, sa
);
1628 /***********************************************************************
1629 * RemoveDirectoryW (KERNEL32.@)
1631 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
1633 OBJECT_ATTRIBUTES attr
;
1634 UNICODE_STRING nt_name
;
1635 ANSI_STRING unix_name
;
1641 TRACE( "%s\n", debugstr_w(path
) );
1643 if (!RtlDosPathNameToNtPathName_U( path
, &nt_name
, NULL
, NULL
))
1645 SetLastError( ERROR_PATH_NOT_FOUND
);
1648 attr
.Length
= sizeof(attr
);
1649 attr
.RootDirectory
= 0;
1650 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1651 attr
.ObjectName
= &nt_name
;
1652 attr
.SecurityDescriptor
= NULL
;
1653 attr
.SecurityQualityOfService
= NULL
;
1655 status
= NtOpenFile( &handle
, DELETE
| SYNCHRONIZE
, &attr
, &io
,
1656 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1657 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
1658 if (status
!= STATUS_SUCCESS
)
1660 SetLastError( RtlNtStatusToDosError(status
) );
1661 RtlFreeUnicodeString( &nt_name
);
1665 status
= wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN
, FALSE
);
1666 RtlFreeUnicodeString( &nt_name
);
1667 if (status
!= STATUS_SUCCESS
)
1669 SetLastError( RtlNtStatusToDosError(status
) );
1674 if (!(ret
= (rmdir( unix_name
.Buffer
) != -1))) FILE_SetDosError();
1675 RtlFreeAnsiString( &unix_name
);
1681 /***********************************************************************
1682 * RemoveDirectoryA (KERNEL32.@)
1684 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
1688 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1689 return RemoveDirectoryW( pathW
);
1693 /***********************************************************************
1694 * GetCurrentDirectoryW (KERNEL32.@)
1696 UINT WINAPI
GetCurrentDirectoryW( UINT buflen
, LPWSTR buf
)
1698 return RtlGetCurrentDirectory_U( buflen
* sizeof(WCHAR
), buf
) / sizeof(WCHAR
);
1702 /***********************************************************************
1703 * GetCurrentDirectoryA (KERNEL32.@)
1705 UINT WINAPI
GetCurrentDirectoryA( UINT buflen
, LPSTR buf
)
1707 WCHAR bufferW
[MAX_PATH
];
1710 if (buflen
&& buf
&& ((ULONG_PTR
)buf
>> 16) == 0)
1712 /* Win9x catches access violations here, returning zero.
1713 * This behaviour resulted in some people not noticing
1714 * that they got the argument order wrong. So let's be
1715 * nice and fail gracefully if buf is invalid and looks
1716 * more like a buflen. */
1717 SetLastError(ERROR_INVALID_PARAMETER
);
1721 ret
= RtlGetCurrentDirectory_U( sizeof(bufferW
), bufferW
);
1723 if (ret
> sizeof(bufferW
))
1725 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
1728 return copy_filename_WtoA( bufferW
, buf
, buflen
);
1732 /***********************************************************************
1733 * SetCurrentDirectoryW (KERNEL32.@)
1735 BOOL WINAPI
SetCurrentDirectoryW( LPCWSTR dir
)
1737 UNICODE_STRING dirW
;
1740 RtlInitUnicodeString( &dirW
, dir
);
1741 status
= RtlSetCurrentDirectory_U( &dirW
);
1742 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
1747 /***********************************************************************
1748 * SetCurrentDirectoryA (KERNEL32.@)
1750 BOOL WINAPI
SetCurrentDirectoryA( LPCSTR dir
)
1753 UNICODE_STRING strW
;
1756 if (!(dirW
= FILE_name_AtoW( dir
, FALSE
))) return FALSE
;
1757 RtlInitUnicodeString( &strW
, dirW
);
1758 status
= RtlSetCurrentDirectory_U( &strW
);
1759 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
1764 /***********************************************************************
1765 * GetWindowsDirectoryW (KERNEL32.@)
1767 * See comment for GetWindowsDirectoryA.
1769 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
1771 UINT len
= strlenW( DIR_Windows
) + 1;
1772 if (path
&& count
>= len
)
1774 strcpyW( path
, DIR_Windows
);
1781 /***********************************************************************
1782 * GetWindowsDirectoryA (KERNEL32.@)
1785 * If buffer is large enough to hold full path and terminating '\0' character
1786 * function copies path to buffer and returns length of the path without '\0'.
1787 * Otherwise function returns required size including '\0' character and
1788 * does not touch the buffer.
1790 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
1792 return copy_filename_WtoA( DIR_Windows
, path
, count
);
1796 /***********************************************************************
1797 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
1799 UINT WINAPI
GetSystemWindowsDirectoryA( LPSTR path
, UINT count
)
1801 return GetWindowsDirectoryA( path
, count
);
1805 /***********************************************************************
1806 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
1808 UINT WINAPI
GetSystemWindowsDirectoryW( LPWSTR path
, UINT count
)
1810 return GetWindowsDirectoryW( path
, count
);
1814 /***********************************************************************
1815 * GetSystemDirectoryW (KERNEL32.@)
1817 * See comment for GetWindowsDirectoryA.
1819 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
1821 UINT len
= strlenW( DIR_System
) + 1;
1822 if (path
&& count
>= len
)
1824 strcpyW( path
, DIR_System
);
1831 /***********************************************************************
1832 * GetSystemDirectoryA (KERNEL32.@)
1834 * See comment for GetWindowsDirectoryA.
1836 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
1838 return copy_filename_WtoA( DIR_System
, path
, count
);
1842 /***********************************************************************
1843 * GetSystemWow64DirectoryW (KERNEL32.@)
1846 * - On Win32 we should return ERROR_CALL_NOT_IMPLEMENTED
1847 * - On Win64 we should return the SysWow64 (system64) directory
1849 UINT WINAPI
GetSystemWow64DirectoryW( LPWSTR path
, UINT count
)
1855 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
1858 len
= strlenW( DIR_SysWow64
) + 1;
1859 if (path
&& count
>= len
)
1861 strcpyW( path
, DIR_SysWow64
);
1868 /***********************************************************************
1869 * GetSystemWow64DirectoryA (KERNEL32.@)
1871 * See comment for GetWindowsWow64DirectoryW.
1873 UINT WINAPI
GetSystemWow64DirectoryA( LPSTR path
, UINT count
)
1877 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
1880 return copy_filename_WtoA( DIR_SysWow64
, path
, count
);
1884 /***********************************************************************
1885 * Wow64EnableWow64FsRedirection (KERNEL32.@)
1887 BOOLEAN WINAPI
Wow64EnableWow64FsRedirection( BOOLEAN enable
)
1889 NTSTATUS status
= RtlWow64EnableFsRedirection( enable
);
1890 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1895 /***********************************************************************
1896 * Wow64DisableWow64FsRedirection (KERNEL32.@)
1898 BOOL WINAPI
Wow64DisableWow64FsRedirection( PVOID
*old_value
)
1900 NTSTATUS status
= RtlWow64EnableFsRedirectionEx( TRUE
, (ULONG
*)old_value
);
1901 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1906 /***********************************************************************
1907 * Wow64RevertWow64FsRedirection (KERNEL32.@)
1909 BOOL WINAPI
Wow64RevertWow64FsRedirection( PVOID old_value
)
1911 NTSTATUS status
= RtlWow64EnableFsRedirection( !old_value
);
1912 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1917 /***********************************************************************
1918 * NeedCurrentDirectoryForExePathW (KERNEL32.@)
1920 BOOL WINAPI
NeedCurrentDirectoryForExePathW( LPCWSTR name
)
1922 static const WCHAR env_name
[] = {'N','o','D','e','f','a','u','l','t',
1923 'C','u','r','r','e','n','t',
1924 'D','i','r','e','c','t','o','r','y',
1925 'I','n','E','x','e','P','a','t','h',0};
1928 /* MSDN mentions some 'registry location'. We do not use registry. */
1929 FIXME("(%s): partial stub\n", debugstr_w(name
));
1931 if (strchrW(name
, '\\'))
1934 /* Check the existence of the variable, not value */
1935 if (!GetEnvironmentVariableW( env_name
, &env_val
, 1 ))
1942 /***********************************************************************
1943 * NeedCurrentDirectoryForExePathA (KERNEL32.@)
1945 BOOL WINAPI
NeedCurrentDirectoryForExePathA( LPCSTR name
)
1949 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return TRUE
;
1950 return NeedCurrentDirectoryForExePathW( nameW
);
1954 /***********************************************************************
1955 * wine_get_unix_file_name (KERNEL32.@) Not a Windows API
1957 * Return the full Unix file name for a given path.
1958 * Returned buffer must be freed by caller.
1960 char * CDECL
wine_get_unix_file_name( LPCWSTR dosW
)
1962 UNICODE_STRING nt_name
;
1963 ANSI_STRING unix_name
;
1966 if (!RtlDosPathNameToNtPathName_U( dosW
, &nt_name
, NULL
, NULL
)) return NULL
;
1967 status
= wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN_IF
, FALSE
);
1968 RtlFreeUnicodeString( &nt_name
);
1969 if (status
&& status
!= STATUS_NO_SUCH_FILE
)
1971 SetLastError( RtlNtStatusToDosError( status
) );
1974 return unix_name
.Buffer
;
1978 /***********************************************************************
1979 * wine_get_dos_file_name (KERNEL32.@) Not a Windows API
1981 * Return the full DOS file name for a given Unix path.
1982 * Returned buffer must be freed by caller.
1984 WCHAR
* CDECL
wine_get_dos_file_name( LPCSTR str
)
1986 UNICODE_STRING nt_name
;
1987 ANSI_STRING unix_name
;
1991 RtlInitAnsiString( &unix_name
, str
);
1992 status
= wine_unix_to_nt_file_name( &unix_name
, &nt_name
);
1995 SetLastError( RtlNtStatusToDosError( status
) );
1998 if (nt_name
.Buffer
[5] == ':')
2000 /* get rid of the \??\ prefix */
2001 /* FIXME: should implement RtlNtPathNameToDosPathName and use that instead */
2002 len
= nt_name
.Length
- 4 * sizeof(WCHAR
);
2003 memmove( nt_name
.Buffer
, nt_name
.Buffer
+ 4, len
);
2004 nt_name
.Buffer
[len
/ sizeof(WCHAR
)] = 0;
2007 nt_name
.Buffer
[1] = '\\';
2008 return nt_name
.Buffer
;
2011 /*************************************************************************
2012 * CreateSymbolicLinkW (KERNEL32.@)
2014 BOOLEAN WINAPI
CreateSymbolicLinkW(LPCWSTR link
, LPCWSTR target
, DWORD flags
)
2016 FIXME("(%s %s %d): stub\n", debugstr_w(link
), debugstr_w(target
), flags
);
2020 /*************************************************************************
2021 * CreateSymbolicLinkA (KERNEL32.@)
2023 BOOLEAN WINAPI
CreateSymbolicLinkA(LPCSTR link
, LPCSTR target
, DWORD flags
)
2025 FIXME("(%s %s %d): stub\n", debugstr_a(link
), debugstr_a(target
), flags
);
2029 /*************************************************************************
2030 * CreateHardLinkTransactedA (KERNEL32.@)
2032 BOOL WINAPI
CreateHardLinkTransactedA(LPCSTR link
, LPCSTR target
, LPSECURITY_ATTRIBUTES sa
, HANDLE transaction
)
2034 FIXME("(%s %s %p %p): stub\n", debugstr_a(link
), debugstr_a(target
), sa
, transaction
);
2035 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
2039 /*************************************************************************
2040 * CreateHardLinkTransactedW (KERNEL32.@)
2042 BOOL WINAPI
CreateHardLinkTransactedW(LPCWSTR link
, LPCWSTR target
, LPSECURITY_ATTRIBUTES sa
, HANDLE transaction
)
2044 FIXME("(%s %s %p %p): stub\n", debugstr_w(link
), debugstr_w(target
), sa
, transaction
);
2045 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
2049 /*************************************************************************
2050 * CheckNameLegalDOS8Dot3A (KERNEL32.@)
2052 BOOL WINAPI
CheckNameLegalDOS8Dot3A(const char *name
, char *oemname
, DWORD oemname_len
,
2053 BOOL
*contains_spaces
, BOOL
*is_legal
)
2057 TRACE("(%s %p %u %p %p)\n", name
, oemname
,
2058 oemname_len
, contains_spaces
, is_legal
);
2060 if (!name
|| !is_legal
)
2063 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
2065 return CheckNameLegalDOS8Dot3W( nameW
, oemname
, oemname_len
, contains_spaces
, is_legal
);
2068 /*************************************************************************
2069 * CheckNameLegalDOS8Dot3W (KERNEL32.@)
2071 BOOL WINAPI
CheckNameLegalDOS8Dot3W(const WCHAR
*name
, char *oemname
, DWORD oemname_len
,
2072 BOOL
*contains_spaces_ret
, BOOL
*is_legal
)
2075 UNICODE_STRING nameW
;
2076 BOOLEAN contains_spaces
;
2078 TRACE("(%s %p %u %p %p)\n", wine_dbgstr_w(name
), oemname
,
2079 oemname_len
, contains_spaces_ret
, is_legal
);
2081 if (!name
|| !is_legal
)
2084 RtlInitUnicodeString( &nameW
, name
);
2087 oem_str
.Length
= oemname_len
;
2088 oem_str
.MaximumLength
= oemname_len
;
2089 oem_str
.Buffer
= oemname
;
2092 *is_legal
= RtlIsNameLegalDOS8Dot3( &nameW
, oemname
? &oem_str
: NULL
, &contains_spaces
);
2093 if (contains_spaces_ret
) *contains_spaces_ret
= contains_spaces
;
2098 BOOL WINAPI
SetSearchPathMode(DWORD flags
)
2100 FIXME("(%x): stub\n", flags
);
2101 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);