setupapi: Add pSetupFree|Malloc|Realloc to spec file and forward them.
[wine.git] / dlls / kernel32 / path.c
blobe376def36a15b97c28afd5bdea40da4aa8da69b5
1 /*
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
24 #include "config.h"
25 #include "wine/port.h"
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdarg.h>
31 #define NONAMELESSUNION
32 #define NONAMELESSSTRUCT
33 #include "winerror.h"
34 #include "ntstatus.h"
35 #define WIN32_NO_STATUS
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winternl.h"
40 #include "kernel_private.h"
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(file);
46 #define MAX_PATHNAME_LEN 1024
49 /* check if a file name is for an executable file (.exe or .com) */
50 static inline BOOL is_executable( const WCHAR *name )
52 static const WCHAR exeW[] = {'.','e','x','e',0};
53 static const WCHAR comW[] = {'.','c','o','m',0};
54 int len = strlenW(name);
56 if (len < 4) return FALSE;
57 return (!strcmpiW( name + len - 4, exeW ) || !strcmpiW( name + len - 4, comW ));
60 /***********************************************************************
61 * copy_filename_WtoA
63 * copy a file name back to OEM/Ansi, but only if the buffer is large enough
65 static DWORD copy_filename_WtoA( LPCWSTR nameW, LPSTR buffer, DWORD len )
67 UNICODE_STRING strW;
68 DWORD ret;
69 BOOL is_ansi = AreFileApisANSI();
71 RtlInitUnicodeString( &strW, nameW );
73 ret = is_ansi ? RtlUnicodeStringToAnsiSize(&strW) : RtlUnicodeStringToOemSize(&strW);
74 if (buffer && ret <= len)
76 ANSI_STRING str;
78 str.Buffer = buffer;
79 str.MaximumLength = min( len, UNICODE_STRING_MAX_CHARS );
80 if (is_ansi)
81 RtlUnicodeStringToAnsiString( &str, &strW, FALSE );
82 else
83 RtlUnicodeStringToOemString( &str, &strW, FALSE );
84 ret = str.Length; /* length without terminating 0 */
86 return ret;
89 /***********************************************************************
90 * add_boot_rename_entry
92 * Adds an entry to the registry that is loaded when windows boots and
93 * checks if there are some files to be removed or renamed/moved.
94 * <fn1> has to be valid and <fn2> may be NULL. If both pointers are
95 * non-NULL then the file is moved, otherwise it is deleted. The
96 * entry of the registry key is always appended with two zero
97 * terminated strings. If <fn2> is NULL then the second entry is
98 * simply a single 0-byte. Otherwise the second filename goes
99 * there. The entries are prepended with \??\ before the path and the
100 * second filename gets also a '!' as the first character if
101 * MOVEFILE_REPLACE_EXISTING is set. After the final string another
102 * 0-byte follows to indicate the end of the strings.
103 * i.e.:
104 * \??\D:\test\file1[0]
105 * !\??\D:\test\file1_renamed[0]
106 * \??\D:\Test|delete[0]
107 * [0] <- file is to be deleted, second string empty
108 * \??\D:\test\file2[0]
109 * !\??\D:\test\file2_renamed[0]
110 * [0] <- indicates end of strings
112 * or:
113 * \??\D:\test\file1[0]
114 * !\??\D:\test\file1_renamed[0]
115 * \??\D:\Test|delete[0]
116 * [0] <- file is to be deleted, second string empty
117 * [0] <- indicates end of strings
120 static BOOL add_boot_rename_entry( LPCWSTR source, LPCWSTR dest, DWORD flags )
122 static const WCHAR ValueName[] = {'P','e','n','d','i','n','g',
123 'F','i','l','e','R','e','n','a','m','e',
124 'O','p','e','r','a','t','i','o','n','s',0};
125 static const WCHAR SessionW[] = {'M','a','c','h','i','n','e','\\',
126 'S','y','s','t','e','m','\\',
127 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
128 'C','o','n','t','r','o','l','\\',
129 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
130 static const int info_size = FIELD_OFFSET( KEY_VALUE_PARTIAL_INFORMATION, Data );
132 OBJECT_ATTRIBUTES attr;
133 UNICODE_STRING nameW, source_name, dest_name;
134 KEY_VALUE_PARTIAL_INFORMATION *info;
135 BOOL rc = FALSE;
136 HANDLE Reboot = 0;
137 DWORD len1, len2;
138 DWORD DataSize = 0;
139 BYTE *Buffer = NULL;
140 WCHAR *p;
142 if (!RtlDosPathNameToNtPathName_U( source, &source_name, NULL, NULL ))
144 SetLastError( ERROR_PATH_NOT_FOUND );
145 return FALSE;
147 dest_name.Buffer = NULL;
148 if (dest && !RtlDosPathNameToNtPathName_U( dest, &dest_name, NULL, NULL ))
150 RtlFreeUnicodeString( &source_name );
151 SetLastError( ERROR_PATH_NOT_FOUND );
152 return FALSE;
155 attr.Length = sizeof(attr);
156 attr.RootDirectory = 0;
157 attr.ObjectName = &nameW;
158 attr.Attributes = 0;
159 attr.SecurityDescriptor = NULL;
160 attr.SecurityQualityOfService = NULL;
161 RtlInitUnicodeString( &nameW, SessionW );
163 if (NtCreateKey( &Reboot, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS)
165 WARN("Error creating key for reboot management [%s]\n",
166 "SYSTEM\\CurrentControlSet\\Control\\Session Manager");
167 RtlFreeUnicodeString( &source_name );
168 RtlFreeUnicodeString( &dest_name );
169 return FALSE;
172 len1 = source_name.Length + sizeof(WCHAR);
173 if (dest)
175 len2 = dest_name.Length + sizeof(WCHAR);
176 if (flags & MOVEFILE_REPLACE_EXISTING)
177 len2 += sizeof(WCHAR); /* Plus 1 because of the leading '!' */
179 else len2 = sizeof(WCHAR); /* minimum is the 0 characters for the empty second string */
181 RtlInitUnicodeString( &nameW, ValueName );
183 /* First we check if the key exists and if so how many bytes it already contains. */
184 if (NtQueryValueKey( Reboot, &nameW, KeyValuePartialInformation,
185 NULL, 0, &DataSize ) == STATUS_BUFFER_TOO_SMALL)
187 if (!(Buffer = HeapAlloc( GetProcessHeap(), 0, DataSize + len1 + len2 + sizeof(WCHAR) )))
188 goto Quit;
189 if (NtQueryValueKey( Reboot, &nameW, KeyValuePartialInformation,
190 Buffer, DataSize, &DataSize )) goto Quit;
191 info = (KEY_VALUE_PARTIAL_INFORMATION *)Buffer;
192 if (info->Type != REG_MULTI_SZ) goto Quit;
193 if (DataSize > sizeof(info)) DataSize -= sizeof(WCHAR); /* remove terminating null (will be added back later) */
195 else
197 DataSize = info_size;
198 if (!(Buffer = HeapAlloc( GetProcessHeap(), 0, DataSize + len1 + len2 + sizeof(WCHAR) )))
199 goto Quit;
202 memcpy( Buffer + DataSize, source_name.Buffer, len1 );
203 DataSize += len1;
204 p = (WCHAR *)(Buffer + DataSize);
205 if (dest)
207 if (flags & MOVEFILE_REPLACE_EXISTING)
208 *p++ = '!';
209 memcpy( p, dest_name.Buffer, len2 );
210 DataSize += len2;
212 else
214 *p = 0;
215 DataSize += sizeof(WCHAR);
218 /* add final null */
219 p = (WCHAR *)(Buffer + DataSize);
220 *p = 0;
221 DataSize += sizeof(WCHAR);
223 rc = !NtSetValueKey(Reboot, &nameW, 0, REG_MULTI_SZ, Buffer + info_size, DataSize - info_size);
225 Quit:
226 RtlFreeUnicodeString( &source_name );
227 RtlFreeUnicodeString( &dest_name );
228 if (Reboot) NtClose(Reboot);
229 HeapFree( GetProcessHeap(), 0, Buffer );
230 return(rc);
234 /***********************************************************************
235 * GetFullPathNameW (KERNEL32.@)
236 * NOTES
237 * if the path closed with '\', *lastpart is 0
239 DWORD WINAPI GetFullPathNameW( LPCWSTR name, DWORD len, LPWSTR buffer,
240 LPWSTR *lastpart )
242 return RtlGetFullPathName_U(name, len * sizeof(WCHAR), buffer, lastpart) / sizeof(WCHAR);
245 /***********************************************************************
246 * GetFullPathNameA (KERNEL32.@)
247 * NOTES
248 * if the path closed with '\', *lastpart is 0
250 DWORD WINAPI GetFullPathNameA( LPCSTR name, DWORD len, LPSTR buffer,
251 LPSTR *lastpart )
253 WCHAR *nameW;
254 WCHAR bufferW[MAX_PATH];
255 DWORD ret;
257 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return 0;
259 ret = GetFullPathNameW( nameW, MAX_PATH, bufferW, NULL);
261 if (!ret) return 0;
262 if (ret > MAX_PATH)
264 SetLastError(ERROR_FILENAME_EXCED_RANGE);
265 return 0;
267 ret = copy_filename_WtoA( bufferW, buffer, len );
268 if (ret < len && lastpart)
270 LPSTR p = buffer + strlen(buffer) - 1;
272 if (*p != '\\')
274 while ((p > buffer + 2) && (*p != '\\')) p--;
275 *lastpart = p + 1;
277 else *lastpart = NULL;
279 return ret;
283 /***********************************************************************
284 * GetLongPathNameW (KERNEL32.@)
286 * NOTES
287 * observed (Win2000):
288 * shortpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
289 * shortpath="": LastError=ERROR_PATH_NOT_FOUND, ret=0
291 DWORD WINAPI GetLongPathNameW( LPCWSTR shortpath, LPWSTR longpath, DWORD longlen )
293 WCHAR tmplongpath[MAX_PATHNAME_LEN];
294 LPCWSTR p;
295 DWORD sp = 0, lp = 0;
296 DWORD tmplen;
297 BOOL unixabsolute;
298 WIN32_FIND_DATAW wfd;
299 HANDLE goit;
301 if (!shortpath)
303 SetLastError(ERROR_INVALID_PARAMETER);
304 return 0;
306 if (!shortpath[0])
308 SetLastError(ERROR_PATH_NOT_FOUND);
309 return 0;
312 TRACE("%s,%p,%d\n", debugstr_w(shortpath), longpath, longlen);
314 if (shortpath[0] == '\\' && shortpath[1] == '\\')
316 FIXME("UNC pathname %s\n", debugstr_w(shortpath));
318 tmplen = strlenW(shortpath);
319 if (tmplen < longlen)
321 if (longpath != shortpath) strcpyW( longpath, shortpath );
322 return tmplen;
324 return tmplen + 1;
327 unixabsolute = (shortpath[0] == '/');
329 /* check for drive letter */
330 if (!unixabsolute && shortpath[1] == ':' )
332 tmplongpath[0] = shortpath[0];
333 tmplongpath[1] = ':';
334 lp = sp = 2;
337 while (shortpath[sp])
339 /* check for path delimiters and reproduce them */
340 if (shortpath[sp] == '\\' || shortpath[sp] == '/')
342 if (!lp || tmplongpath[lp-1] != '\\')
344 /* strip double "\\" */
345 tmplongpath[lp++] = '\\';
347 tmplongpath[lp] = 0; /* terminate string */
348 sp++;
349 continue;
352 p = shortpath + sp;
353 if (sp == 0 && p[0] == '.' && (p[1] == '/' || p[1] == '\\'))
355 tmplongpath[lp++] = *p++;
356 tmplongpath[lp++] = *p++;
358 for (; *p && *p != '/' && *p != '\\'; p++);
359 tmplen = p - (shortpath + sp);
360 lstrcpynW(tmplongpath + lp, shortpath + sp, tmplen + 1);
361 /* Check if the file exists and use the existing file name */
362 goit = FindFirstFileW(tmplongpath, &wfd);
363 if (goit == INVALID_HANDLE_VALUE)
365 TRACE("not found %s!\n", debugstr_w(tmplongpath));
366 SetLastError ( ERROR_FILE_NOT_FOUND );
367 return 0;
369 FindClose(goit);
370 strcpyW(tmplongpath + lp, wfd.cFileName);
371 lp += strlenW(tmplongpath + lp);
372 sp += tmplen;
374 tmplen = strlenW(shortpath) - 1;
375 if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
376 (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
377 tmplongpath[lp++] = shortpath[tmplen];
378 tmplongpath[lp] = 0;
380 tmplen = strlenW(tmplongpath) + 1;
381 if (tmplen <= longlen)
383 strcpyW(longpath, tmplongpath);
384 TRACE("returning %s\n", debugstr_w(longpath));
385 tmplen--; /* length without 0 */
388 return tmplen;
391 /***********************************************************************
392 * GetLongPathNameA (KERNEL32.@)
394 DWORD WINAPI GetLongPathNameA( LPCSTR shortpath, LPSTR longpath, DWORD longlen )
396 WCHAR *shortpathW;
397 WCHAR longpathW[MAX_PATH];
398 DWORD ret;
400 TRACE("%s\n", debugstr_a(shortpath));
402 if (!(shortpathW = FILE_name_AtoW( shortpath, FALSE ))) return 0;
404 ret = GetLongPathNameW(shortpathW, longpathW, MAX_PATH);
406 if (!ret) return 0;
407 if (ret > MAX_PATH)
409 SetLastError(ERROR_FILENAME_EXCED_RANGE);
410 return 0;
412 return copy_filename_WtoA( longpathW, longpath, longlen );
416 /***********************************************************************
417 * GetShortPathNameW (KERNEL32.@)
419 * NOTES
420 * observed:
421 * longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
422 * longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
424 * more observations ( with NT 3.51 (WinDD) ):
425 * longpath <= 8.3 -> just copy longpath to shortpath
426 * longpath > 8.3 ->
427 * a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
428 * b) file does exist -> set the short filename.
429 * - trailing slashes are reproduced in the short name, even if the
430 * file is not a directory
431 * - the absolute/relative path of the short name is reproduced like found
432 * in the long name
433 * - longpath and shortpath may have the same address
434 * Peter Ganten, 1999
436 DWORD WINAPI GetShortPathNameW( LPCWSTR longpath, LPWSTR shortpath, DWORD shortlen )
438 WCHAR tmpshortpath[MAX_PATHNAME_LEN];
439 LPCWSTR p;
440 DWORD sp = 0, lp = 0;
441 DWORD tmplen;
442 WIN32_FIND_DATAW wfd;
443 HANDLE goit;
444 UNICODE_STRING ustr;
445 WCHAR ustr_buf[8+1+3+1];
447 TRACE("%s\n", debugstr_w(longpath));
449 if (!longpath)
451 SetLastError(ERROR_INVALID_PARAMETER);
452 return 0;
454 if (!longpath[0])
456 SetLastError(ERROR_BAD_PATHNAME);
457 return 0;
460 /* check for drive letter */
461 if (longpath[0] != '/' && longpath[1] == ':' )
463 tmpshortpath[0] = longpath[0];
464 tmpshortpath[1] = ':';
465 sp = lp = 2;
468 ustr.Buffer = ustr_buf;
469 ustr.Length = 0;
470 ustr.MaximumLength = sizeof(ustr_buf);
472 while (longpath[lp])
474 /* check for path delimiters and reproduce them */
475 if (longpath[lp] == '\\' || longpath[lp] == '/')
477 if (!sp || tmpshortpath[sp-1] != '\\')
479 /* strip double "\\" */
480 tmpshortpath[sp] = '\\';
481 sp++;
483 tmpshortpath[sp] = 0; /* terminate string */
484 lp++;
485 continue;
488 for (p = longpath + lp; *p && *p != '/' && *p != '\\'; p++);
489 tmplen = p - (longpath + lp);
490 lstrcpynW(tmpshortpath + sp, longpath + lp, tmplen + 1);
491 /* Check, if the current element is a valid dos name */
492 if (tmplen <= 8+1+3)
494 BOOLEAN spaces;
495 memcpy(ustr_buf, longpath + lp, tmplen * sizeof(WCHAR));
496 ustr_buf[tmplen] = '\0';
497 ustr.Length = tmplen * sizeof(WCHAR);
498 if (RtlIsNameLegalDOS8Dot3(&ustr, NULL, &spaces) && !spaces)
500 sp += tmplen;
501 lp += tmplen;
502 continue;
506 /* Check if the file exists and use the existing short file name */
507 goit = FindFirstFileW(tmpshortpath, &wfd);
508 if (goit == INVALID_HANDLE_VALUE) goto notfound;
509 FindClose(goit);
510 strcpyW(tmpshortpath + sp, wfd.cAlternateFileName);
511 sp += strlenW(tmpshortpath + sp);
512 lp += tmplen;
514 tmpshortpath[sp] = 0;
516 tmplen = strlenW(tmpshortpath) + 1;
517 if (tmplen <= shortlen)
519 strcpyW(shortpath, tmpshortpath);
520 TRACE("returning %s\n", debugstr_w(shortpath));
521 tmplen--; /* length without 0 */
524 return tmplen;
526 notfound:
527 TRACE("not found!\n" );
528 SetLastError ( ERROR_FILE_NOT_FOUND );
529 return 0;
532 /***********************************************************************
533 * GetShortPathNameA (KERNEL32.@)
535 DWORD WINAPI GetShortPathNameA( LPCSTR longpath, LPSTR shortpath, DWORD shortlen )
537 WCHAR *longpathW;
538 WCHAR shortpathW[MAX_PATH];
539 DWORD ret;
541 TRACE("%s\n", debugstr_a(longpath));
543 if (!(longpathW = FILE_name_AtoW( longpath, FALSE ))) return 0;
545 ret = GetShortPathNameW(longpathW, shortpathW, MAX_PATH);
547 if (!ret) return 0;
548 if (ret > MAX_PATH)
550 SetLastError(ERROR_FILENAME_EXCED_RANGE);
551 return 0;
553 return copy_filename_WtoA( shortpathW, shortpath, shortlen );
557 /***********************************************************************
558 * GetTempPathA (KERNEL32.@)
560 DWORD WINAPI GetTempPathA( DWORD count, LPSTR path )
562 WCHAR pathW[MAX_PATH];
563 UINT ret;
565 ret = GetTempPathW(MAX_PATH, pathW);
567 if (!ret)
568 return 0;
570 if (ret > MAX_PATH)
572 SetLastError(ERROR_FILENAME_EXCED_RANGE);
573 return 0;
575 return copy_filename_WtoA( pathW, path, count );
579 /***********************************************************************
580 * GetTempPathW (KERNEL32.@)
582 DWORD WINAPI GetTempPathW( DWORD count, LPWSTR path )
584 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
585 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
586 static const WCHAR userprofile[] = { 'U','S','E','R','P','R','O','F','I','L','E',0 };
587 WCHAR tmp_path[MAX_PATH];
588 UINT ret;
590 TRACE("%u,%p\n", count, path);
592 if (!(ret = GetEnvironmentVariableW( tmp, tmp_path, MAX_PATH )) &&
593 !(ret = GetEnvironmentVariableW( temp, tmp_path, MAX_PATH )) &&
594 !(ret = GetEnvironmentVariableW( userprofile, tmp_path, MAX_PATH )) &&
595 !(ret = GetWindowsDirectoryW( tmp_path, MAX_PATH )))
596 return 0;
598 if (ret > MAX_PATH)
600 SetLastError(ERROR_FILENAME_EXCED_RANGE);
601 return 0;
604 ret = GetFullPathNameW(tmp_path, MAX_PATH, tmp_path, NULL);
605 if (!ret) return 0;
607 if (ret > MAX_PATH - 2)
609 SetLastError(ERROR_FILENAME_EXCED_RANGE);
610 return 0;
613 if (tmp_path[ret-1] != '\\')
615 tmp_path[ret++] = '\\';
616 tmp_path[ret] = '\0';
619 ret++; /* add space for terminating 0 */
621 if (count)
623 lstrcpynW(path, tmp_path, count);
624 if (count >= ret)
625 ret--; /* return length without 0 */
626 else if (count < 4)
627 path[0] = 0; /* avoid returning ambiguous "X:" */
630 TRACE("returning %u, %s\n", ret, debugstr_w(path));
631 return ret;
635 /***********************************************************************
636 * GetTempFileNameA (KERNEL32.@)
638 UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique, LPSTR buffer)
640 WCHAR *pathW, *prefixW = NULL;
641 WCHAR bufferW[MAX_PATH];
642 UINT ret;
644 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return 0;
645 if (prefix && !(prefixW = FILE_name_AtoW( prefix, TRUE ))) return 0;
647 ret = GetTempFileNameW(pathW, prefixW, unique, bufferW);
648 if (ret) FILE_name_WtoA( bufferW, -1, buffer, MAX_PATH );
650 HeapFree( GetProcessHeap(), 0, prefixW );
651 return ret;
654 /***********************************************************************
655 * GetTempFileNameW (KERNEL32.@)
657 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique, LPWSTR buffer )
659 static const WCHAR formatW[] = {'%','x','.','t','m','p',0};
661 int i;
662 LPWSTR p;
663 DWORD attr;
665 if ( !path || !buffer )
667 SetLastError( ERROR_INVALID_PARAMETER );
668 return 0;
671 /* ensure that the provided directory exists */
672 attr = GetFileAttributesW(path);
673 if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_DIRECTORY))
675 TRACE("path not found %s\n", debugstr_w(path));
676 SetLastError( ERROR_DIRECTORY );
677 return 0;
680 strcpyW( buffer, path );
681 p = buffer + strlenW(buffer);
683 /* add a \, if there isn't one */
684 if ((p == buffer) || (p[-1] != '\\')) *p++ = '\\';
686 if (prefix)
687 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
689 unique &= 0xffff;
691 if (unique) sprintfW( p, formatW, unique );
692 else
694 /* get a "random" unique number and try to create the file */
695 HANDLE handle;
696 UINT num = GetTickCount() & 0xffff;
697 static UINT last;
699 /* avoid using the same name twice in a short interval */
700 if (last - num < 10) num = last + 1;
701 if (!num) num = 1;
702 unique = num;
705 sprintfW( p, formatW, unique );
706 handle = CreateFileW( buffer, GENERIC_WRITE, 0, NULL,
707 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
708 if (handle != INVALID_HANDLE_VALUE)
709 { /* We created it */
710 TRACE("created %s\n", debugstr_w(buffer) );
711 CloseHandle( handle );
712 last = unique;
713 break;
715 if (GetLastError() != ERROR_FILE_EXISTS &&
716 GetLastError() != ERROR_SHARING_VIOLATION)
717 break; /* No need to go on */
718 if (!(++unique & 0xffff)) unique = 1;
719 } while (unique != num);
722 TRACE("returning %s\n", debugstr_w(buffer) );
723 return unique;
727 /***********************************************************************
728 * contains_pathW
730 * Check if the file name contains a path; helper for SearchPathW.
731 * A relative path is not considered a path unless it starts with ./ or ../
733 static inline BOOL contains_pathW (LPCWSTR name)
735 if (RtlDetermineDosPathNameType_U( name ) != RELATIVE_PATH) return TRUE;
736 if (name[0] != '.') return FALSE;
737 if (name[1] == '/' || name[1] == '\\') return TRUE;
738 return (name[1] == '.' && (name[2] == '/' || name[2] == '\\'));
742 /***********************************************************************
743 * SearchPathW [KERNEL32.@]
745 * Searches for a specified file in the search path.
747 * PARAMS
748 * path [I] Path to search (NULL means default)
749 * name [I] Filename to search for.
750 * ext [I] File extension to append to file name. The first
751 * character must be a period. This parameter is
752 * specified only if the filename given does not
753 * contain an extension.
754 * buflen [I] size of buffer, in characters
755 * buffer [O] buffer for found filename
756 * lastpart [O] address of pointer to last used character in
757 * buffer (the final '\')
759 * RETURNS
760 * Success: length of string copied into buffer, not including
761 * terminating null character. If the filename found is
762 * longer than the length of the buffer, the length of the
763 * filename is returned.
764 * Failure: Zero
766 * NOTES
767 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
768 * (tested on NT 4.0)
770 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext, DWORD buflen,
771 LPWSTR buffer, LPWSTR *lastpart )
773 DWORD ret = 0;
775 if (!name || !name[0])
777 SetLastError(ERROR_INVALID_PARAMETER);
778 return 0;
781 /* If the name contains an explicit path, ignore the path */
783 if (contains_pathW(name))
785 /* try first without extension */
786 if (RtlDoesFileExists_U( name ))
787 return GetFullPathNameW( name, buflen, buffer, lastpart );
789 if (ext)
791 LPCWSTR p = strrchrW( name, '.' );
792 if (p && !strchrW( p, '/' ) && !strchrW( p, '\\' ))
793 ext = NULL; /* Ignore the specified extension */
796 /* Allocate a buffer for the file name and extension */
797 if (ext)
799 LPWSTR tmp;
800 DWORD len = strlenW(name) + strlenW(ext);
802 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
804 SetLastError( ERROR_OUTOFMEMORY );
805 return 0;
807 strcpyW( tmp, name );
808 strcatW( tmp, ext );
809 if (RtlDoesFileExists_U( tmp ))
810 ret = GetFullPathNameW( tmp, buflen, buffer, lastpart );
811 HeapFree( GetProcessHeap(), 0, tmp );
814 else if (path && path[0]) /* search in the specified path */
816 ret = RtlDosSearchPath_U( path, name, ext, buflen * sizeof(WCHAR),
817 buffer, lastpart ) / sizeof(WCHAR);
819 else /* search in the default path */
821 WCHAR *dll_path = MODULE_get_dll_load_path( NULL );
823 if (dll_path)
825 ret = RtlDosSearchPath_U( dll_path, name, ext, buflen * sizeof(WCHAR),
826 buffer, lastpart ) / sizeof(WCHAR);
827 HeapFree( GetProcessHeap(), 0, dll_path );
829 else
831 SetLastError( ERROR_OUTOFMEMORY );
832 return 0;
836 if (!ret) SetLastError( ERROR_FILE_NOT_FOUND );
837 else TRACE( "found %s\n", debugstr_w(buffer) );
838 return ret;
842 /***********************************************************************
843 * SearchPathA (KERNEL32.@)
845 * See SearchPathW.
847 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext,
848 DWORD buflen, LPSTR buffer, LPSTR *lastpart )
850 WCHAR *pathW = NULL, *nameW, *extW = NULL;
851 WCHAR bufferW[MAX_PATH];
852 DWORD ret;
854 if (!name)
856 SetLastError(ERROR_INVALID_PARAMETER);
857 return 0;
860 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return 0;
861 if (path && !(pathW = FILE_name_AtoW( path, TRUE ))) return 0;
863 if (ext && !(extW = FILE_name_AtoW( ext, TRUE )))
865 HeapFree( GetProcessHeap(), 0, pathW );
866 return 0;
869 ret = SearchPathW(pathW, nameW, extW, MAX_PATH, bufferW, NULL);
871 HeapFree( GetProcessHeap(), 0, pathW );
872 HeapFree( GetProcessHeap(), 0, extW );
874 if (!ret) return 0;
875 if (ret > MAX_PATH)
877 SetLastError(ERROR_FILENAME_EXCED_RANGE);
878 return 0;
880 ret = copy_filename_WtoA( bufferW, buffer, buflen );
881 if (buflen > ret && lastpart)
882 *lastpart = strrchr(buffer, '\\') + 1;
883 return ret;
886 static BOOL is_same_file(HANDLE h1, HANDLE h2)
888 int fd1;
889 BOOL ret = FALSE;
890 if (wine_server_handle_to_fd(h1, 0, &fd1, NULL) == STATUS_SUCCESS)
892 int fd2;
893 if (wine_server_handle_to_fd(h2, 0, &fd2, NULL) == STATUS_SUCCESS)
895 struct stat stat1, stat2;
896 if (fstat(fd1, &stat1) == 0 && fstat(fd2, &stat2) == 0)
897 ret = (stat1.st_dev == stat2.st_dev && stat1.st_ino == stat2.st_ino);
898 wine_server_release_fd(h2, fd2);
900 wine_server_release_fd(h1, fd1);
902 return ret;
905 /**************************************************************************
906 * CopyFileW (KERNEL32.@)
908 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists )
910 static const int buffer_size = 65536;
911 HANDLE h1, h2;
912 BY_HANDLE_FILE_INFORMATION info;
913 DWORD count;
914 BOOL ret = FALSE;
915 char *buffer;
917 if (!source || !dest)
919 SetLastError(ERROR_INVALID_PARAMETER);
920 return FALSE;
922 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
924 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
925 return FALSE;
928 TRACE("%s -> %s\n", debugstr_w(source), debugstr_w(dest));
930 if ((h1 = CreateFileW(source, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
931 NULL, OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE)
933 WARN("Unable to open source %s\n", debugstr_w(source));
934 HeapFree( GetProcessHeap(), 0, buffer );
935 return FALSE;
938 if (!GetFileInformationByHandle( h1, &info ))
940 WARN("GetFileInformationByHandle returned error for %s\n", debugstr_w(source));
941 HeapFree( GetProcessHeap(), 0, buffer );
942 CloseHandle( h1 );
943 return FALSE;
946 if (!fail_if_exists)
948 BOOL same_file = FALSE;
949 h2 = CreateFileW( dest, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
950 OPEN_EXISTING, 0, 0);
951 if (h2 != INVALID_HANDLE_VALUE)
953 same_file = is_same_file( h1, h2 );
954 CloseHandle( h2 );
956 if (same_file)
958 HeapFree( GetProcessHeap(), 0, buffer );
959 CloseHandle( h1 );
960 SetLastError( ERROR_SHARING_VIOLATION );
961 return FALSE;
965 if ((h2 = CreateFileW( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
966 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
967 info.dwFileAttributes, h1 )) == INVALID_HANDLE_VALUE)
969 WARN("Unable to open dest %s\n", debugstr_w(dest));
970 HeapFree( GetProcessHeap(), 0, buffer );
971 CloseHandle( h1 );
972 return FALSE;
975 while (ReadFile( h1, buffer, buffer_size, &count, NULL ) && count)
977 char *p = buffer;
978 while (count != 0)
980 DWORD res;
981 if (!WriteFile( h2, p, count, &res, NULL ) || !res) goto done;
982 p += res;
983 count -= res;
986 ret = TRUE;
987 done:
988 /* Maintain the timestamp of source file to destination file */
989 SetFileTime(h2, NULL, NULL, &info.ftLastWriteTime);
990 HeapFree( GetProcessHeap(), 0, buffer );
991 CloseHandle( h1 );
992 CloseHandle( h2 );
993 return ret;
997 /**************************************************************************
998 * CopyFileA (KERNEL32.@)
1000 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists)
1002 WCHAR *sourceW, *destW;
1003 BOOL ret;
1005 if (!(sourceW = FILE_name_AtoW( source, FALSE ))) return FALSE;
1006 if (!(destW = FILE_name_AtoW( dest, TRUE ))) return FALSE;
1008 ret = CopyFileW( sourceW, destW, fail_if_exists );
1010 HeapFree( GetProcessHeap(), 0, destW );
1011 return ret;
1015 /**************************************************************************
1016 * CopyFileExW (KERNEL32.@)
1018 * This implementation ignores most of the extra parameters passed-in into
1019 * the "ex" version of the method and calls the CopyFile method.
1020 * It will have to be fixed eventually.
1022 BOOL WINAPI CopyFileExW(LPCWSTR sourceFilename, LPCWSTR destFilename,
1023 LPPROGRESS_ROUTINE progressRoutine, LPVOID appData,
1024 LPBOOL cancelFlagPointer, DWORD copyFlags)
1027 * Interpret the only flag that CopyFile can interpret.
1029 return CopyFileW(sourceFilename, destFilename, (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0);
1033 /**************************************************************************
1034 * CopyFileExA (KERNEL32.@)
1036 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename, LPCSTR destFilename,
1037 LPPROGRESS_ROUTINE progressRoutine, LPVOID appData,
1038 LPBOOL cancelFlagPointer, DWORD copyFlags)
1040 WCHAR *sourceW, *destW;
1041 BOOL ret;
1043 /* can't use the TEB buffer since we may have a callback routine */
1044 if (!(sourceW = FILE_name_AtoW( sourceFilename, TRUE ))) return FALSE;
1045 if (!(destW = FILE_name_AtoW( destFilename, TRUE )))
1047 HeapFree( GetProcessHeap(), 0, sourceW );
1048 return FALSE;
1050 ret = CopyFileExW(sourceW, destW, progressRoutine, appData,
1051 cancelFlagPointer, copyFlags);
1052 HeapFree( GetProcessHeap(), 0, sourceW );
1053 HeapFree( GetProcessHeap(), 0, destW );
1054 return ret;
1058 /**************************************************************************
1059 * MoveFileWithProgressW (KERNEL32.@)
1061 BOOL WINAPI MoveFileWithProgressW( LPCWSTR source, LPCWSTR dest,
1062 LPPROGRESS_ROUTINE fnProgress,
1063 LPVOID param, DWORD flag )
1065 FILE_BASIC_INFORMATION info;
1066 UNICODE_STRING nt_name;
1067 OBJECT_ATTRIBUTES attr;
1068 IO_STATUS_BLOCK io;
1069 NTSTATUS status;
1070 HANDLE source_handle = 0, dest_handle;
1071 ANSI_STRING source_unix, dest_unix;
1073 TRACE("(%s,%s,%p,%p,%04x)\n",
1074 debugstr_w(source), debugstr_w(dest), fnProgress, param, flag );
1076 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
1077 return add_boot_rename_entry( source, dest, flag );
1079 if (!dest)
1080 return DeleteFileW( source );
1082 if (flag & MOVEFILE_WRITE_THROUGH)
1083 FIXME("MOVEFILE_WRITE_THROUGH unimplemented\n");
1085 /* check if we are allowed to rename the source */
1087 if (!RtlDosPathNameToNtPathName_U( source, &nt_name, NULL, NULL ))
1089 SetLastError( ERROR_PATH_NOT_FOUND );
1090 return FALSE;
1092 source_unix.Buffer = NULL;
1093 dest_unix.Buffer = NULL;
1094 attr.Length = sizeof(attr);
1095 attr.RootDirectory = 0;
1096 attr.Attributes = OBJ_CASE_INSENSITIVE;
1097 attr.ObjectName = &nt_name;
1098 attr.SecurityDescriptor = NULL;
1099 attr.SecurityQualityOfService = NULL;
1101 status = NtOpenFile( &source_handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
1102 if (status == STATUS_SUCCESS)
1103 status = wine_nt_to_unix_file_name( &nt_name, &source_unix, FILE_OPEN, FALSE );
1104 RtlFreeUnicodeString( &nt_name );
1105 if (status != STATUS_SUCCESS)
1107 SetLastError( RtlNtStatusToDosError(status) );
1108 goto error;
1110 status = NtQueryInformationFile( source_handle, &io, &info, sizeof(info), FileBasicInformation );
1111 if (status != STATUS_SUCCESS)
1113 SetLastError( RtlNtStatusToDosError(status) );
1114 goto error;
1117 /* we must have write access to the destination, and it must */
1118 /* not exist except if MOVEFILE_REPLACE_EXISTING is set */
1120 if (!RtlDosPathNameToNtPathName_U( dest, &nt_name, NULL, NULL ))
1122 SetLastError( ERROR_PATH_NOT_FOUND );
1123 goto error;
1125 status = NtOpenFile( &dest_handle, GENERIC_READ | GENERIC_WRITE, &attr, &io, 0,
1126 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1127 if (status == STATUS_SUCCESS) /* destination exists */
1129 NtClose( dest_handle );
1130 if (!(flag & MOVEFILE_REPLACE_EXISTING))
1132 SetLastError( ERROR_ALREADY_EXISTS );
1133 RtlFreeUnicodeString( &nt_name );
1134 goto error;
1136 else if (info.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) /* cannot replace directory */
1138 SetLastError( ERROR_ACCESS_DENIED );
1139 goto error;
1142 else if (status != STATUS_OBJECT_NAME_NOT_FOUND)
1144 SetLastError( RtlNtStatusToDosError(status) );
1145 RtlFreeUnicodeString( &nt_name );
1146 goto error;
1149 status = wine_nt_to_unix_file_name( &nt_name, &dest_unix, FILE_OPEN_IF, FALSE );
1150 RtlFreeUnicodeString( &nt_name );
1151 if (status != STATUS_SUCCESS && status != STATUS_NO_SUCH_FILE)
1153 SetLastError( RtlNtStatusToDosError(status) );
1154 goto error;
1157 /* now perform the rename */
1159 if (rename( source_unix.Buffer, dest_unix.Buffer ) == -1)
1161 if (errno == EXDEV && (flag & MOVEFILE_COPY_ALLOWED))
1163 NtClose( source_handle );
1164 RtlFreeAnsiString( &source_unix );
1165 RtlFreeAnsiString( &dest_unix );
1166 if (!CopyFileExW( source, dest, fnProgress,
1167 param, NULL, COPY_FILE_FAIL_IF_EXISTS ))
1168 return FALSE;
1169 return DeleteFileW( source );
1171 FILE_SetDosError();
1172 /* if we created the destination, remove it */
1173 if (io.Information == FILE_CREATED) unlink( dest_unix.Buffer );
1174 goto error;
1177 /* fixup executable permissions */
1179 if (is_executable( source ) != is_executable( dest ))
1181 struct stat fstat;
1182 if (stat( dest_unix.Buffer, &fstat ) != -1)
1184 if (is_executable( dest ))
1185 /* set executable bit where read bit is set */
1186 fstat.st_mode |= (fstat.st_mode & 0444) >> 2;
1187 else
1188 fstat.st_mode &= ~0111;
1189 chmod( dest_unix.Buffer, fstat.st_mode );
1193 NtClose( source_handle );
1194 RtlFreeAnsiString( &source_unix );
1195 RtlFreeAnsiString( &dest_unix );
1196 return TRUE;
1198 error:
1199 if (source_handle) NtClose( source_handle );
1200 RtlFreeAnsiString( &source_unix );
1201 RtlFreeAnsiString( &dest_unix );
1202 return FALSE;
1205 /**************************************************************************
1206 * MoveFileWithProgressA (KERNEL32.@)
1208 BOOL WINAPI MoveFileWithProgressA( LPCSTR source, LPCSTR dest,
1209 LPPROGRESS_ROUTINE fnProgress,
1210 LPVOID param, DWORD flag )
1212 WCHAR *sourceW, *destW;
1213 BOOL ret;
1215 if (!(sourceW = FILE_name_AtoW( source, FALSE ))) return FALSE;
1216 if (dest)
1218 if (!(destW = FILE_name_AtoW( dest, TRUE ))) return FALSE;
1220 else
1221 destW = NULL;
1223 ret = MoveFileWithProgressW( sourceW, destW, fnProgress, param, flag );
1224 HeapFree( GetProcessHeap(), 0, destW );
1225 return ret;
1228 /**************************************************************************
1229 * MoveFileExW (KERNEL32.@)
1231 BOOL WINAPI MoveFileExW( LPCWSTR source, LPCWSTR dest, DWORD flag )
1233 return MoveFileWithProgressW( source, dest, NULL, NULL, flag );
1236 /**************************************************************************
1237 * MoveFileExA (KERNEL32.@)
1239 BOOL WINAPI MoveFileExA( LPCSTR source, LPCSTR dest, DWORD flag )
1241 return MoveFileWithProgressA( source, dest, NULL, NULL, flag );
1245 /**************************************************************************
1246 * MoveFileW (KERNEL32.@)
1248 * Move file or directory
1250 BOOL WINAPI MoveFileW( LPCWSTR source, LPCWSTR dest )
1252 return MoveFileExW( source, dest, MOVEFILE_COPY_ALLOWED );
1256 /**************************************************************************
1257 * MoveFileA (KERNEL32.@)
1259 BOOL WINAPI MoveFileA( LPCSTR source, LPCSTR dest )
1261 return MoveFileExA( source, dest, MOVEFILE_COPY_ALLOWED );
1265 /*************************************************************************
1266 * CreateHardLinkW (KERNEL32.@)
1268 BOOL WINAPI CreateHardLinkW(LPCWSTR lpFileName, LPCWSTR lpExistingFileName,
1269 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
1271 NTSTATUS status;
1272 UNICODE_STRING ntDest, ntSource;
1273 ANSI_STRING unixDest, unixSource;
1274 BOOL ret = FALSE;
1276 TRACE("(%s, %s, %p)\n", debugstr_w(lpFileName),
1277 debugstr_w(lpExistingFileName), lpSecurityAttributes);
1279 ntDest.Buffer = ntSource.Buffer = NULL;
1280 if (!RtlDosPathNameToNtPathName_U( lpFileName, &ntDest, NULL, NULL ) ||
1281 !RtlDosPathNameToNtPathName_U( lpExistingFileName, &ntSource, NULL, NULL ))
1283 SetLastError( ERROR_PATH_NOT_FOUND );
1284 goto err;
1287 unixSource.Buffer = unixDest.Buffer = NULL;
1288 status = wine_nt_to_unix_file_name( &ntSource, &unixSource, FILE_OPEN, FALSE );
1289 if (!status)
1291 status = wine_nt_to_unix_file_name( &ntDest, &unixDest, FILE_CREATE, FALSE );
1292 if (!status) /* destination must not exist */
1294 status = STATUS_OBJECT_NAME_EXISTS;
1295 } else if (status == STATUS_NO_SUCH_FILE)
1297 status = STATUS_SUCCESS;
1301 if (status)
1302 SetLastError( RtlNtStatusToDosError(status) );
1303 else if (!link( unixSource.Buffer, unixDest.Buffer ))
1305 TRACE("Hardlinked '%s' to '%s'\n", debugstr_a( unixDest.Buffer ),
1306 debugstr_a( unixSource.Buffer ));
1307 ret = TRUE;
1309 else
1310 FILE_SetDosError();
1312 RtlFreeAnsiString( &unixSource );
1313 RtlFreeAnsiString( &unixDest );
1315 err:
1316 RtlFreeUnicodeString( &ntSource );
1317 RtlFreeUnicodeString( &ntDest );
1318 return ret;
1322 /*************************************************************************
1323 * CreateHardLinkA (KERNEL32.@)
1325 BOOL WINAPI CreateHardLinkA(LPCSTR lpFileName, LPCSTR lpExistingFileName,
1326 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
1328 WCHAR *sourceW, *destW;
1329 BOOL res;
1331 if (!(sourceW = FILE_name_AtoW( lpExistingFileName, TRUE )))
1333 return FALSE;
1335 if (!(destW = FILE_name_AtoW( lpFileName, TRUE )))
1337 HeapFree( GetProcessHeap(), 0, sourceW );
1338 return FALSE;
1341 res = CreateHardLinkW( destW, sourceW, lpSecurityAttributes );
1343 HeapFree( GetProcessHeap(), 0, sourceW );
1344 HeapFree( GetProcessHeap(), 0, destW );
1346 return res;
1350 /***********************************************************************
1351 * CreateDirectoryW (KERNEL32.@)
1352 * RETURNS:
1353 * TRUE : success
1354 * FALSE : failure
1355 * ERROR_DISK_FULL: on full disk
1356 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
1357 * ERROR_ACCESS_DENIED: on permission problems
1358 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
1360 BOOL WINAPI CreateDirectoryW( LPCWSTR path, LPSECURITY_ATTRIBUTES sa )
1362 OBJECT_ATTRIBUTES attr;
1363 UNICODE_STRING nt_name;
1364 IO_STATUS_BLOCK io;
1365 NTSTATUS status;
1366 HANDLE handle;
1367 BOOL ret = FALSE;
1369 TRACE( "%s\n", debugstr_w(path) );
1371 if (!RtlDosPathNameToNtPathName_U( path, &nt_name, NULL, NULL ))
1373 SetLastError( ERROR_PATH_NOT_FOUND );
1374 return FALSE;
1376 attr.Length = sizeof(attr);
1377 attr.RootDirectory = 0;
1378 attr.Attributes = OBJ_CASE_INSENSITIVE;
1379 attr.ObjectName = &nt_name;
1380 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1381 attr.SecurityQualityOfService = NULL;
1383 status = NtCreateFile( &handle, GENERIC_READ, &attr, &io, NULL,
1384 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_CREATE,
1385 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
1387 if (status == STATUS_SUCCESS)
1389 NtClose( handle );
1390 ret = TRUE;
1392 else SetLastError( RtlNtStatusToDosError(status) );
1394 RtlFreeUnicodeString( &nt_name );
1395 return ret;
1399 /***********************************************************************
1400 * CreateDirectoryA (KERNEL32.@)
1402 BOOL WINAPI CreateDirectoryA( LPCSTR path, LPSECURITY_ATTRIBUTES sa )
1404 WCHAR *pathW;
1406 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1407 return CreateDirectoryW( pathW, sa );
1411 /***********************************************************************
1412 * CreateDirectoryExA (KERNEL32.@)
1414 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path, LPSECURITY_ATTRIBUTES sa )
1416 WCHAR *pathW, *templateW = NULL;
1417 BOOL ret;
1419 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1420 if (template && !(templateW = FILE_name_AtoW( template, TRUE ))) return FALSE;
1422 ret = CreateDirectoryExW( templateW, pathW, sa );
1423 HeapFree( GetProcessHeap(), 0, templateW );
1424 return ret;
1428 /***********************************************************************
1429 * CreateDirectoryExW (KERNEL32.@)
1431 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path, LPSECURITY_ATTRIBUTES sa )
1433 return CreateDirectoryW( path, sa );
1437 /***********************************************************************
1438 * RemoveDirectoryW (KERNEL32.@)
1440 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
1442 OBJECT_ATTRIBUTES attr;
1443 UNICODE_STRING nt_name;
1444 ANSI_STRING unix_name;
1445 IO_STATUS_BLOCK io;
1446 NTSTATUS status;
1447 HANDLE handle;
1448 BOOL ret = FALSE;
1450 TRACE( "%s\n", debugstr_w(path) );
1452 if (!RtlDosPathNameToNtPathName_U( path, &nt_name, NULL, NULL ))
1454 SetLastError( ERROR_PATH_NOT_FOUND );
1455 return FALSE;
1457 attr.Length = sizeof(attr);
1458 attr.RootDirectory = 0;
1459 attr.Attributes = OBJ_CASE_INSENSITIVE;
1460 attr.ObjectName = &nt_name;
1461 attr.SecurityDescriptor = NULL;
1462 attr.SecurityQualityOfService = NULL;
1464 status = NtOpenFile( &handle, DELETE, &attr, &io,
1465 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1466 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1467 if (status == STATUS_SUCCESS)
1468 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, FALSE );
1469 RtlFreeUnicodeString( &nt_name );
1471 if (status != STATUS_SUCCESS)
1473 SetLastError( RtlNtStatusToDosError(status) );
1474 return FALSE;
1477 if (!(ret = (rmdir( unix_name.Buffer ) != -1))) FILE_SetDosError();
1478 RtlFreeAnsiString( &unix_name );
1479 NtClose( handle );
1480 return ret;
1484 /***********************************************************************
1485 * RemoveDirectoryA (KERNEL32.@)
1487 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
1489 WCHAR *pathW;
1491 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1492 return RemoveDirectoryW( pathW );
1496 /***********************************************************************
1497 * GetCurrentDirectoryW (KERNEL32.@)
1499 UINT WINAPI GetCurrentDirectoryW( UINT buflen, LPWSTR buf )
1501 return RtlGetCurrentDirectory_U( buflen * sizeof(WCHAR), buf ) / sizeof(WCHAR);
1505 /***********************************************************************
1506 * GetCurrentDirectoryA (KERNEL32.@)
1508 UINT WINAPI GetCurrentDirectoryA( UINT buflen, LPSTR buf )
1510 WCHAR bufferW[MAX_PATH];
1511 DWORD ret;
1513 if (buflen && buf && ((ULONG_PTR)buf >> 16) == 0)
1515 /* Win9x catches access violations here, returning zero.
1516 * This behaviour resulted in some people not noticing
1517 * that they got the argument order wrong. So let's be
1518 * nice and fail gracefully if buf is invalid and looks
1519 * more like a buflen. */
1520 SetLastError(ERROR_INVALID_PARAMETER);
1521 return 0;
1524 ret = GetCurrentDirectoryW(MAX_PATH, bufferW);
1526 if (!ret) return 0;
1527 if (ret > MAX_PATH)
1529 SetLastError(ERROR_FILENAME_EXCED_RANGE);
1530 return 0;
1532 return copy_filename_WtoA( bufferW, buf, buflen );
1536 /***********************************************************************
1537 * SetCurrentDirectoryW (KERNEL32.@)
1539 BOOL WINAPI SetCurrentDirectoryW( LPCWSTR dir )
1541 UNICODE_STRING dirW;
1542 NTSTATUS status;
1544 RtlInitUnicodeString( &dirW, dir );
1545 status = RtlSetCurrentDirectory_U( &dirW );
1546 if (status != STATUS_SUCCESS)
1548 SetLastError( RtlNtStatusToDosError(status) );
1549 return FALSE;
1551 return TRUE;
1555 /***********************************************************************
1556 * SetCurrentDirectoryA (KERNEL32.@)
1558 BOOL WINAPI SetCurrentDirectoryA( LPCSTR dir )
1560 WCHAR *dirW;
1562 if (!(dirW = FILE_name_AtoW( dir, FALSE ))) return FALSE;
1563 return SetCurrentDirectoryW( dirW );
1567 /***********************************************************************
1568 * GetWindowsDirectoryW (KERNEL32.@)
1570 * See comment for GetWindowsDirectoryA.
1572 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
1574 UINT len = strlenW( DIR_Windows ) + 1;
1575 if (path && count >= len)
1577 strcpyW( path, DIR_Windows );
1578 len--;
1580 return len;
1584 /***********************************************************************
1585 * GetWindowsDirectoryA (KERNEL32.@)
1587 * Return value:
1588 * If buffer is large enough to hold full path and terminating '\0' character
1589 * function copies path to buffer and returns length of the path without '\0'.
1590 * Otherwise function returns required size including '\0' character and
1591 * does not touch the buffer.
1593 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
1595 return copy_filename_WtoA( DIR_Windows, path, count );
1599 /***********************************************************************
1600 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
1602 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
1604 return GetWindowsDirectoryA( path, count );
1608 /***********************************************************************
1609 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
1611 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
1613 return GetWindowsDirectoryW( path, count );
1617 /***********************************************************************
1618 * GetSystemDirectoryW (KERNEL32.@)
1620 * See comment for GetWindowsDirectoryA.
1622 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
1624 UINT len = strlenW( DIR_System ) + 1;
1625 if (path && count >= len)
1627 strcpyW( path, DIR_System );
1628 len--;
1630 return len;
1634 /***********************************************************************
1635 * GetSystemDirectoryA (KERNEL32.@)
1637 * See comment for GetWindowsDirectoryA.
1639 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
1641 return copy_filename_WtoA( DIR_System, path, count );
1645 /***********************************************************************
1646 * GetSystemWow64DirectoryW (KERNEL32.@)
1648 * As seen on MSDN
1649 * - On Win32 we should returns ERROR_CALL_NOT_IMPLEMENTED
1650 * - On Win64 we should returns the SysWow64 (system64) directory
1652 UINT WINAPI GetSystemWow64DirectoryW( LPWSTR path, UINT count )
1654 UINT len;
1656 if (!DIR_SysWow64)
1658 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1659 return 0;
1661 len = strlenW( DIR_SysWow64 ) + 1;
1662 if (path && count >= len)
1664 strcpyW( path, DIR_SysWow64 );
1665 len--;
1667 return len;
1671 /***********************************************************************
1672 * GetSystemWow64DirectoryA (KERNEL32.@)
1674 * See comment for GetWindowsWow64DirectoryW.
1676 UINT WINAPI GetSystemWow64DirectoryA( LPSTR path, UINT count )
1678 if (!DIR_SysWow64)
1680 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1681 return 0;
1683 return copy_filename_WtoA( DIR_SysWow64, path, count );
1687 /***********************************************************************
1688 * Wow64EnableWow64FsRedirection (KERNEL32.@)
1690 BOOLEAN WINAPI Wow64EnableWow64FsRedirection( BOOLEAN enable )
1692 NTSTATUS status = RtlWow64EnableFsRedirection( enable );
1693 if (status) SetLastError( RtlNtStatusToDosError(status) );
1694 return !status;
1698 /***********************************************************************
1699 * Wow64DisableWow64FsRedirection (KERNEL32.@)
1701 BOOL WINAPI Wow64DisableWow64FsRedirection( PVOID *old_value )
1703 NTSTATUS status = RtlWow64EnableFsRedirectionEx( TRUE, (ULONG *)old_value );
1704 if (status) SetLastError( RtlNtStatusToDosError(status) );
1705 return !status;
1709 /***********************************************************************
1710 * Wow64RevertWow64FsRedirection (KERNEL32.@)
1712 BOOL WINAPI Wow64RevertWow64FsRedirection( PVOID old_value )
1714 NTSTATUS status = RtlWow64EnableFsRedirection( !old_value );
1715 if (status) SetLastError( RtlNtStatusToDosError(status) );
1716 return !status;
1720 /***********************************************************************
1721 * NeedCurrentDirectoryForExePathW (KERNEL32.@)
1723 BOOL WINAPI NeedCurrentDirectoryForExePathW( LPCWSTR name )
1725 static const WCHAR env_name[] = {'N','o','D','e','f','a','u','l','t',
1726 'C','u','r','r','e','n','t',
1727 'D','i','r','e','c','t','o','r','y',
1728 'I','n','E','x','e','P','a','t','h',0};
1729 WCHAR env_val;
1731 /* MSDN mentions some 'registry location'. We do not use registry. */
1732 FIXME("(%s): partial stub\n", debugstr_w(name));
1734 if (strchrW(name, '\\'))
1735 return TRUE;
1737 /* Check the existence of the variable, not value */
1738 if (!GetEnvironmentVariableW( env_name, &env_val, 1 ))
1739 return TRUE;
1741 return FALSE;
1745 /***********************************************************************
1746 * NeedCurrentDirectoryForExePathA (KERNEL32.@)
1748 BOOL WINAPI NeedCurrentDirectoryForExePathA( LPCSTR name )
1750 WCHAR *nameW;
1752 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return TRUE;
1753 return NeedCurrentDirectoryForExePathW( nameW );
1757 /***********************************************************************
1758 * wine_get_unix_file_name (KERNEL32.@) Not a Windows API
1760 * Return the full Unix file name for a given path.
1761 * Returned buffer must be freed by caller.
1763 char * CDECL wine_get_unix_file_name( LPCWSTR dosW )
1765 UNICODE_STRING nt_name;
1766 ANSI_STRING unix_name;
1767 NTSTATUS status;
1769 if (!RtlDosPathNameToNtPathName_U( dosW, &nt_name, NULL, NULL )) return NULL;
1770 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN_IF, FALSE );
1771 RtlFreeUnicodeString( &nt_name );
1772 if (status && status != STATUS_NO_SUCH_FILE)
1774 SetLastError( RtlNtStatusToDosError( status ) );
1775 return NULL;
1777 return unix_name.Buffer;
1781 /***********************************************************************
1782 * wine_get_dos_file_name (KERNEL32.@) Not a Windows API
1784 * Return the full DOS file name for a given Unix path.
1785 * Returned buffer must be freed by caller.
1787 WCHAR * CDECL wine_get_dos_file_name( LPCSTR str )
1789 UNICODE_STRING nt_name;
1790 ANSI_STRING unix_name;
1791 NTSTATUS status;
1792 DWORD len;
1794 RtlInitAnsiString( &unix_name, str );
1795 status = wine_unix_to_nt_file_name( &unix_name, &nt_name );
1796 if (status)
1798 SetLastError( RtlNtStatusToDosError( status ) );
1799 return NULL;
1801 if (nt_name.Buffer[5] == ':')
1803 /* get rid of the \??\ prefix */
1804 /* FIXME: should implement RtlNtPathNameToDosPathName and use that instead */
1805 len = nt_name.Length - 4 * sizeof(WCHAR);
1806 memmove( nt_name.Buffer, nt_name.Buffer + 4, len );
1807 nt_name.Buffer[len / sizeof(WCHAR)] = 0;
1809 else
1810 nt_name.Buffer[1] = '\\';
1811 return nt_name.Buffer;