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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/port.h"
31 #define NONAMELESSUNION
32 #define NONAMELESSSTRUCT
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 inline static 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 ));
61 /***********************************************************************
62 * add_boot_rename_entry
64 * Adds an entry to the registry that is loaded when windows boots and
65 * checks if there are some files to be removed or renamed/moved.
66 * <fn1> has to be valid and <fn2> may be NULL. If both pointers are
67 * non-NULL then the file is moved, otherwise it is deleted. The
68 * entry of the registrykey is always appended with two zero
69 * terminated strings. If <fn2> is NULL then the second entry is
70 * simply a single 0-byte. Otherwise the second filename goes
71 * there. The entries are prepended with \??\ before the path and the
72 * second filename gets also a '!' as the first character if
73 * MOVEFILE_REPLACE_EXISTING is set. After the final string another
74 * 0-byte follows to indicate the end of the strings.
76 * \??\D:\test\file1[0]
77 * !\??\D:\test\file1_renamed[0]
78 * \??\D:\Test|delete[0]
79 * [0] <- file is to be deleted, second string empty
80 * \??\D:\test\file2[0]
81 * !\??\D:\test\file2_renamed[0]
82 * [0] <- indicates end of strings
85 * \??\D:\test\file1[0]
86 * !\??\D:\test\file1_renamed[0]
87 * \??\D:\Test|delete[0]
88 * [0] <- file is to be deleted, second string empty
89 * [0] <- indicates end of strings
92 static BOOL add_boot_rename_entry( LPCWSTR source, LPCWSTR dest, DWORD flags )
94 static const WCHAR ValueName[] = {'P','e','n','d','i','n','g',
95 'F','i','l','e','R','e','n','a','m','e',
96 'O','p','e','r','a','t','i','o','n','s',0};
97 static const WCHAR SessionW[] = {'M','a','c','h','i','n','e','\\',
98 'S','y','s','t','e','m','\\',
99 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
100 'C','o','n','t','r','o','l','\\',
101 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
102 static const int info_size = FIELD_OFFSET( KEY_VALUE_PARTIAL_INFORMATION, Data );
104 OBJECT_ATTRIBUTES attr;
105 UNICODE_STRING nameW, source_name, dest_name;
106 KEY_VALUE_PARTIAL_INFORMATION *info;
114 if (!RtlDosPathNameToNtPathName_U( source, &source_name, NULL, NULL ))
116 SetLastError( ERROR_PATH_NOT_FOUND );
119 dest_name.Buffer = NULL;
120 if (dest && !RtlDosPathNameToNtPathName_U( dest, &dest_name, NULL, NULL ))
122 RtlFreeUnicodeString( &source_name );
123 SetLastError( ERROR_PATH_NOT_FOUND );
127 attr.Length = sizeof(attr);
128 attr.RootDirectory = 0;
129 attr.ObjectName = &nameW;
131 attr.SecurityDescriptor = NULL;
132 attr.SecurityQualityOfService = NULL;
133 RtlInitUnicodeString( &nameW, SessionW );
135 if (NtCreateKey( &Reboot, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS)
137 WARN("Error creating key for reboot managment [%s]\n",
138 "SYSTEM\\CurrentControlSet\\Control\\Session Manager");
139 RtlFreeUnicodeString( &source_name );
140 RtlFreeUnicodeString( &dest_name );
144 len1 = source_name.Length + sizeof(WCHAR);
147 len2 = dest_name.Length + sizeof(WCHAR);
148 if (flags & MOVEFILE_REPLACE_EXISTING)
149 len2 += sizeof(WCHAR); /* Plus 1 because of the leading '!' */
151 else len2 = sizeof(WCHAR); /* minimum is the 0 characters for the empty second string */
153 RtlInitUnicodeString( &nameW, ValueName );
155 /* First we check if the key exists and if so how many bytes it already contains. */
156 if (NtQueryValueKey( Reboot, &nameW, KeyValuePartialInformation,
157 NULL, 0, &DataSize ) == STATUS_BUFFER_OVERFLOW)
159 if (!(Buffer = HeapAlloc( GetProcessHeap(), 0, DataSize + len1 + len2 + sizeof(WCHAR) )))
161 if (NtQueryValueKey( Reboot, &nameW, KeyValuePartialInformation,
162 Buffer, DataSize, &DataSize )) goto Quit;
163 info = (KEY_VALUE_PARTIAL_INFORMATION *)Buffer;
164 if (info->Type != REG_MULTI_SZ) goto Quit;
165 if (DataSize > sizeof(info)) DataSize -= sizeof(WCHAR); /* remove terminating null (will be added back later) */
169 DataSize = info_size;
170 if (!(Buffer = HeapAlloc( GetProcessHeap(), 0, DataSize + len1 + len2 + sizeof(WCHAR) )))
174 memcpy( Buffer + DataSize, source_name.Buffer, len1 );
176 p = (WCHAR *)(Buffer + DataSize);
179 if (flags & MOVEFILE_REPLACE_EXISTING)
181 memcpy( p, dest_name.Buffer, len2 );
187 DataSize += sizeof(WCHAR);
191 p = (WCHAR *)(Buffer + DataSize);
193 DataSize += sizeof(WCHAR);
195 rc = !NtSetValueKey(Reboot, &nameW, 0, REG_MULTI_SZ, Buffer + info_size, DataSize - info_size);
198 RtlFreeUnicodeString( &source_name );
199 RtlFreeUnicodeString( &dest_name );
200 if (Reboot) NtClose(Reboot);
201 if (Buffer) HeapFree( GetProcessHeap(), 0, Buffer );
206 /***********************************************************************
207 * GetFullPathNameW (KERNEL32.@)
209 * if the path closed with '\', *lastpart is 0
211 DWORD WINAPI GetFullPathNameW( LPCWSTR name, DWORD len, LPWSTR buffer,
214 return RtlGetFullPathName_U(name, len * sizeof(WCHAR), buffer, lastpart) / sizeof(WCHAR);
217 /***********************************************************************
218 * GetFullPathNameA (KERNEL32.@)
220 * if the path closed with '\', *lastpart is 0
222 DWORD WINAPI GetFullPathNameA( LPCSTR name, DWORD len, LPSTR buffer,
225 UNICODE_STRING nameW;
226 WCHAR bufferW[MAX_PATH];
231 SetLastError(ERROR_INVALID_PARAMETER);
235 if (!RtlCreateUnicodeStringFromAsciiz(&nameW, name))
237 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
241 retW = GetFullPathNameW( nameW.Buffer, MAX_PATH, bufferW, NULL);
245 else if (retW > MAX_PATH)
247 SetLastError(ERROR_FILENAME_EXCED_RANGE);
252 ret = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
253 if (ret && ret <= len)
255 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, len, NULL, NULL);
256 ret--; /* length without 0 */
260 LPSTR p = buffer + strlen(buffer) - 1;
264 while ((p > buffer + 2) && (*p != '\\')) p--;
267 else *lastpart = NULL;
272 RtlFreeUnicodeString(&nameW);
277 /***********************************************************************
278 * GetLongPathNameW (KERNEL32.@)
281 * observed (Win2000):
282 * shortpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
283 * shortpath="": LastError=ERROR_PATH_NOT_FOUND, ret=0
285 DWORD WINAPI GetLongPathNameW( LPCWSTR shortpath, LPWSTR longpath, DWORD longlen )
287 WCHAR tmplongpath[MAX_PATHNAME_LEN];
289 DWORD sp = 0, lp = 0;
291 BOOL unixabsolute = (shortpath[0] == '/');
292 WIN32_FIND_DATAW wfd;
297 SetLastError(ERROR_INVALID_PARAMETER);
302 SetLastError(ERROR_PATH_NOT_FOUND);
306 TRACE("%s,%p,%ld\n", debugstr_w(shortpath), longpath, longlen);
308 if (shortpath[0] == '\\' && shortpath[1] == '\\')
310 ERR("UNC pathname %s\n", debugstr_w(shortpath));
311 lstrcpynW( longpath, shortpath, longlen );
312 return strlenW(longpath);
315 /* check for drive letter */
316 if (!unixabsolute && shortpath[1] == ':' )
318 tmplongpath[0] = shortpath[0];
319 tmplongpath[1] = ':';
323 while (shortpath[sp])
325 /* check for path delimiters and reproduce them */
326 if (shortpath[sp] == '\\' || shortpath[sp] == '/')
328 if (!lp || tmplongpath[lp-1] != '\\')
330 /* strip double "\\" */
331 tmplongpath[lp++] = '\\';
333 tmplongpath[lp] = 0; /* terminate string */
339 if (sp == 0 && p[0] == '.' && (p[1] == '/' || p[1] == '\\'))
341 tmplongpath[lp++] = *p++;
342 tmplongpath[lp++] = *p++;
344 for (; *p && *p != '/' && *p != '\\'; p++);
345 tmplen = p - (shortpath + sp);
346 lstrcpynW(tmplongpath + lp, shortpath + sp, tmplen + 1);
347 /* Check if the file exists and use the existing file name */
348 goit = FindFirstFileW(tmplongpath, &wfd);
349 if (goit == INVALID_HANDLE_VALUE)
351 TRACE("not found %s!\n", debugstr_w(tmplongpath));
352 SetLastError ( ERROR_FILE_NOT_FOUND );
356 strcpyW(tmplongpath + lp, wfd.cFileName);
357 lp += strlenW(tmplongpath + lp);
360 tmplen = strlenW(shortpath) - 1;
361 if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
362 (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
363 tmplongpath[lp++] = shortpath[tmplen];
366 tmplen = strlenW(tmplongpath) + 1;
367 if (tmplen <= longlen)
369 strcpyW(longpath, tmplongpath);
370 TRACE("returning %s\n", debugstr_w(longpath));
371 tmplen--; /* length without 0 */
377 /***********************************************************************
378 * GetLongPathNameA (KERNEL32.@)
380 DWORD WINAPI GetLongPathNameA( LPCSTR shortpath, LPSTR longpath, DWORD longlen )
382 UNICODE_STRING shortpathW;
383 WCHAR longpathW[MAX_PATH];
388 SetLastError(ERROR_INVALID_PARAMETER);
392 TRACE("%s\n", debugstr_a(shortpath));
394 if (!RtlCreateUnicodeStringFromAsciiz(&shortpathW, shortpath))
396 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
400 retW = GetLongPathNameW(shortpathW.Buffer, longpathW, MAX_PATH);
404 else if (retW > MAX_PATH)
406 SetLastError(ERROR_FILENAME_EXCED_RANGE);
411 ret = WideCharToMultiByte(CP_ACP, 0, longpathW, -1, NULL, 0, NULL, NULL);
414 WideCharToMultiByte(CP_ACP, 0, longpathW, -1, longpath, longlen, NULL, NULL);
415 ret--; /* length without 0 */
419 RtlFreeUnicodeString(&shortpathW);
424 /***********************************************************************
425 * GetShortPathNameW (KERNEL32.@)
429 * longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
430 * longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
432 * more observations ( with NT 3.51 (WinDD) ):
433 * longpath <= 8.3 -> just copy longpath to shortpath
435 * a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
436 * b) file does exist -> set the short filename.
437 * - trailing slashes are reproduced in the short name, even if the
438 * file is not a directory
439 * - the absolute/relative path of the short name is reproduced like found
441 * - longpath and shortpath may have the same address
444 DWORD WINAPI GetShortPathNameW( LPCWSTR longpath, LPWSTR shortpath, DWORD shortlen )
446 WCHAR tmpshortpath[MAX_PATHNAME_LEN];
448 DWORD sp = 0, lp = 0;
450 BOOL unixabsolute = (longpath[0] == '/');
451 WIN32_FIND_DATAW wfd;
454 WCHAR ustr_buf[8+1+3+1];
456 TRACE("%s\n", debugstr_w(longpath));
460 SetLastError(ERROR_INVALID_PARAMETER);
465 SetLastError(ERROR_BAD_PATHNAME);
469 /* check for drive letter */
470 if (!unixabsolute && longpath[1] == ':' )
472 tmpshortpath[0] = longpath[0];
473 tmpshortpath[1] = ':';
477 ustr.Buffer = ustr_buf;
479 ustr.MaximumLength = sizeof(ustr_buf);
483 /* check for path delimiters and reproduce them */
484 if (longpath[lp] == '\\' || longpath[lp] == '/')
486 if (!sp || tmpshortpath[sp-1] != '\\')
488 /* strip double "\\" */
489 tmpshortpath[sp] = '\\';
492 tmpshortpath[sp] = 0; /* terminate string */
497 for (p = longpath + lp; *p && *p != '/' && *p != '\\'; p++);
498 tmplen = p - (longpath + lp);
499 lstrcpynW(tmpshortpath + sp, longpath + lp, tmplen + 1);
500 /* Check, if the current element is a valid dos name */
501 if (tmplen <= 8+1+3+1)
504 memcpy(ustr_buf, longpath + lp, tmplen * sizeof(WCHAR));
505 ustr_buf[tmplen] = '\0';
506 ustr.Length = tmplen * sizeof(WCHAR);
507 if (RtlIsNameLegalDOS8Dot3(&ustr, NULL, &spaces) && !spaces)
515 /* Check if the file exists and use the existing short file name */
516 goit = FindFirstFileW(tmpshortpath, &wfd);
517 if (goit == INVALID_HANDLE_VALUE) goto notfound;
519 strcpyW(tmpshortpath + sp, wfd.cAlternateFileName);
520 sp += strlenW(tmpshortpath + sp);
523 tmpshortpath[sp] = 0;
525 tmplen = strlenW(tmpshortpath) + 1;
526 if (tmplen <= shortlen)
528 strcpyW(shortpath, tmpshortpath);
529 TRACE("returning %s\n", debugstr_w(shortpath));
530 tmplen--; /* length without 0 */
536 TRACE("not found!\n" );
537 SetLastError ( ERROR_FILE_NOT_FOUND );
541 /***********************************************************************
542 * GetShortPathNameA (KERNEL32.@)
544 DWORD WINAPI GetShortPathNameA( LPCSTR longpath, LPSTR shortpath, DWORD shortlen )
546 UNICODE_STRING longpathW;
547 WCHAR shortpathW[MAX_PATH];
552 SetLastError(ERROR_INVALID_PARAMETER);
556 TRACE("%s\n", debugstr_a(longpath));
558 if (!RtlCreateUnicodeStringFromAsciiz(&longpathW, longpath))
560 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
564 retW = GetShortPathNameW(longpathW.Buffer, shortpathW, MAX_PATH);
568 else if (retW > MAX_PATH)
570 SetLastError(ERROR_FILENAME_EXCED_RANGE);
575 ret = WideCharToMultiByte(CP_ACP, 0, shortpathW, -1, NULL, 0, NULL, NULL);
578 WideCharToMultiByte(CP_ACP, 0, shortpathW, -1, shortpath, shortlen, NULL, NULL);
579 ret--; /* length without 0 */
583 RtlFreeUnicodeString(&longpathW);
588 /***********************************************************************
589 * GetTempPathA (KERNEL32.@)
591 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
593 WCHAR pathW[MAX_PATH];
596 ret = GetTempPathW(MAX_PATH, pathW);
603 SetLastError(ERROR_FILENAME_EXCED_RANGE);
607 ret = WideCharToMultiByte(CP_ACP, 0, pathW, -1, NULL, 0, NULL, NULL);
610 WideCharToMultiByte(CP_ACP, 0, pathW, -1, path, count, NULL, NULL);
611 ret--; /* length without 0 */
617 /***********************************************************************
618 * GetTempPathW (KERNEL32.@)
620 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
622 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
623 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
624 WCHAR tmp_path[MAX_PATH];
627 TRACE("%u,%p\n", count, path);
629 if (!(ret = GetEnvironmentVariableW( tmp, tmp_path, MAX_PATH )))
630 if (!(ret = GetEnvironmentVariableW( temp, tmp_path, MAX_PATH )))
631 if (!(ret = GetCurrentDirectoryW( MAX_PATH, tmp_path )))
636 SetLastError(ERROR_FILENAME_EXCED_RANGE);
640 ret = GetFullPathNameW(tmp_path, MAX_PATH, tmp_path, NULL);
643 if (ret > MAX_PATH - 2)
645 SetLastError(ERROR_FILENAME_EXCED_RANGE);
649 if (tmp_path[ret-1] != '\\')
651 tmp_path[ret++] = '\\';
652 tmp_path[ret] = '\0';
655 ret++; /* add space for terminating 0 */
659 lstrcpynW(path, tmp_path, count);
661 ret--; /* return length without 0 */
663 path[0] = 0; /* avoid returning ambiguous "X:" */
666 TRACE("returning %u, %s\n", ret, debugstr_w(path));
671 /***********************************************************************
672 * GetTempFileNameA (KERNEL32.@)
674 UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique, LPSTR buffer)
676 UNICODE_STRING pathW, prefixW;
677 WCHAR bufferW[MAX_PATH];
680 if ( !path || !prefix || !buffer )
682 SetLastError( ERROR_INVALID_PARAMETER );
686 RtlCreateUnicodeStringFromAsciiz(&pathW, path);
687 RtlCreateUnicodeStringFromAsciiz(&prefixW, prefix);
689 ret = GetTempFileNameW(pathW.Buffer, prefixW.Buffer, unique, bufferW);
691 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);
693 RtlFreeUnicodeString(&pathW);
694 RtlFreeUnicodeString(&prefixW);
698 /***********************************************************************
699 * GetTempFileNameW (KERNEL32.@)
701 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique, LPWSTR buffer )
703 static const WCHAR formatW[] = {'%','x','.','t','m','p',0};
708 if ( !path || !prefix || !buffer )
710 SetLastError( ERROR_INVALID_PARAMETER );
714 strcpyW( buffer, path );
715 p = buffer + strlenW(buffer);
717 /* add a \, if there isn't one */
718 if ((p == buffer) || (p[-1] != '\\')) *p++ = '\\';
720 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
724 if (unique) sprintfW( p, formatW, unique );
727 /* get a "random" unique number and try to create the file */
729 UINT num = GetTickCount() & 0xffff;
735 sprintfW( p, formatW, unique );
736 handle = CreateFileW( buffer, GENERIC_WRITE, 0, NULL,
737 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
738 if (handle != INVALID_HANDLE_VALUE)
739 { /* We created it */
740 TRACE("created %s\n", debugstr_w(buffer) );
741 CloseHandle( handle );
744 if (GetLastError() != ERROR_FILE_EXISTS &&
745 GetLastError() != ERROR_SHARING_VIOLATION)
746 break; /* No need to go on */
747 if (!(++unique & 0xffff)) unique = 1;
748 } while (unique != num);
751 TRACE("returning %s\n", debugstr_w(buffer) );
756 /***********************************************************************
759 * Check if the file name contains a path; helper for SearchPathW.
760 * A relative path is not considered a path unless it starts with ./ or ../
762 inline static BOOL contains_pathW (LPCWSTR name)
764 if (RtlDetermineDosPathNameType_U( name ) != RELATIVE_PATH) return TRUE;
765 if (name[0] != '.') return FALSE;
766 if (name[1] == '/' || name[1] == '\\') return TRUE;
767 return (name[1] == '.' && (name[2] == '/' || name[2] == '\\'));
771 /***********************************************************************
772 * SearchPathW [KERNEL32.@]
774 * Searches for a specified file in the search path.
777 * path [I] Path to search
778 * name [I] Filename to search for.
779 * ext [I] File extension to append to file name. The first
780 * character must be a period. This parameter is
781 * specified only if the filename given does not
782 * contain an extension.
783 * buflen [I] size of buffer, in characters
784 * buffer [O] buffer for found filename
785 * lastpart [O] address of pointer to last used character in
786 * buffer (the final '\')
789 * Success: length of string copied into buffer, not including
790 * terminating null character. If the filename found is
791 * longer than the length of the buffer, the length of the
792 * filename is returned.
796 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
799 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext, DWORD buflen,
800 LPWSTR buffer, LPWSTR *lastpart )
804 /* If the name contains an explicit path, ignore the path */
806 if (contains_pathW(name))
808 /* try first without extension */
809 if (RtlDoesFileExists_U( name ))
810 return GetFullPathNameW( name, buflen, buffer, lastpart );
814 LPCWSTR p = strrchrW( name, '.' );
815 if (p && !strchrW( p, '/' ) && !strchrW( p, '\\' ))
816 ext = NULL; /* Ignore the specified extension */
819 /* Allocate a buffer for the file name and extension */
823 DWORD len = strlenW(name) + strlenW(ext);
825 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
827 SetLastError( ERROR_OUTOFMEMORY );
830 strcpyW( tmp, name );
832 if (RtlDoesFileExists_U( tmp ))
833 ret = GetFullPathNameW( tmp, buflen, buffer, lastpart );
834 HeapFree( GetProcessHeap(), 0, tmp );
837 else if (path && path[0]) /* search in the specified path */
839 ret = RtlDosSearchPath_U( path, name, ext, buflen * sizeof(WCHAR),
840 buffer, lastpart ) / sizeof(WCHAR);
842 else /* search in the default path */
844 WCHAR *dll_path = MODULE_get_dll_load_path( NULL );
848 ret = RtlDosSearchPath_U( dll_path, name, ext, buflen * sizeof(WCHAR),
849 buffer, lastpart ) / sizeof(WCHAR);
850 HeapFree( GetProcessHeap(), 0, dll_path );
854 SetLastError( ERROR_OUTOFMEMORY );
859 if (!ret) SetLastError( ERROR_FILE_NOT_FOUND );
860 else TRACE( "found %s\n", debugstr_w(buffer) );
865 /***********************************************************************
866 * SearchPathA (KERNEL32.@)
868 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext,
869 DWORD buflen, LPSTR buffer, LPSTR *lastpart )
871 UNICODE_STRING pathW, nameW, extW;
872 WCHAR bufferW[MAX_PATH];
875 if (path) RtlCreateUnicodeStringFromAsciiz(&pathW, path);
876 else pathW.Buffer = NULL;
877 if (name) RtlCreateUnicodeStringFromAsciiz(&nameW, name);
878 else nameW.Buffer = NULL;
879 if (ext) RtlCreateUnicodeStringFromAsciiz(&extW, ext);
880 else extW.Buffer = NULL;
882 retW = SearchPathW(pathW.Buffer, nameW.Buffer, extW.Buffer, MAX_PATH, bufferW, NULL);
886 else if (retW > MAX_PATH)
888 SetLastError(ERROR_FILENAME_EXCED_RANGE);
893 ret = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
896 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, buflen, NULL, NULL);
897 ret--; /* length without 0 */
898 if (lastpart) *lastpart = strrchr(buffer, '\\') + 1;
902 RtlFreeUnicodeString(&pathW);
903 RtlFreeUnicodeString(&nameW);
904 RtlFreeUnicodeString(&extW);
909 /**************************************************************************
910 * CopyFileW (KERNEL32.@)
912 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists )
915 BY_HANDLE_FILE_INFORMATION info;
920 if (!source || !dest)
922 SetLastError(ERROR_INVALID_PARAMETER);
926 TRACE("%s -> %s\n", debugstr_w(source), debugstr_w(dest));
928 if ((h1 = CreateFileW(source, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
929 NULL, OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE)
931 WARN("Unable to open source %s\n", debugstr_w(source));
935 if (!GetFileInformationByHandle( h1, &info ))
937 WARN("GetFileInformationByHandle returned error for %s\n", debugstr_w(source));
942 if ((h2 = CreateFileW( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
943 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
944 info.dwFileAttributes, h1 )) == INVALID_HANDLE_VALUE)
946 WARN("Unable to open dest %s\n", debugstr_w(dest));
951 while (ReadFile( h1, buffer, sizeof(buffer), &count, NULL ) && count)
957 if (!WriteFile( h2, p, count, &res, NULL ) || !res) goto done;
970 /**************************************************************************
971 * CopyFileA (KERNEL32.@)
973 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists)
975 UNICODE_STRING sourceW, destW;
978 if (!source || !dest)
980 SetLastError(ERROR_INVALID_PARAMETER);
984 RtlCreateUnicodeStringFromAsciiz(&sourceW, source);
985 RtlCreateUnicodeStringFromAsciiz(&destW, dest);
987 ret = CopyFileW(sourceW.Buffer, destW.Buffer, fail_if_exists);
989 RtlFreeUnicodeString(&sourceW);
990 RtlFreeUnicodeString(&destW);
995 /**************************************************************************
996 * CopyFileExW (KERNEL32.@)
998 * This implementation ignores most of the extra parameters passed-in into
999 * the "ex" version of the method and calls the CopyFile method.
1000 * It will have to be fixed eventually.
1002 BOOL WINAPI CopyFileExW(LPCWSTR sourceFilename, LPCWSTR destFilename,
1003 LPPROGRESS_ROUTINE progressRoutine, LPVOID appData,
1004 LPBOOL cancelFlagPointer, DWORD copyFlags)
1007 * Interpret the only flag that CopyFile can interpret.
1009 return CopyFileW(sourceFilename, destFilename, (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0);
1013 /**************************************************************************
1014 * CopyFileExA (KERNEL32.@)
1016 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename, LPCSTR destFilename,
1017 LPPROGRESS_ROUTINE progressRoutine, LPVOID appData,
1018 LPBOOL cancelFlagPointer, DWORD copyFlags)
1020 UNICODE_STRING sourceW, destW;
1023 if (!sourceFilename || !destFilename)
1025 SetLastError(ERROR_INVALID_PARAMETER);
1029 RtlCreateUnicodeStringFromAsciiz(&sourceW, sourceFilename);
1030 RtlCreateUnicodeStringFromAsciiz(&destW, destFilename);
1032 ret = CopyFileExW(sourceW.Buffer, destW.Buffer, progressRoutine, appData,
1033 cancelFlagPointer, copyFlags);
1035 RtlFreeUnicodeString(&sourceW);
1036 RtlFreeUnicodeString(&destW);
1041 /**************************************************************************
1042 * MoveFileExW (KERNEL32.@)
1044 BOOL WINAPI MoveFileExW( LPCWSTR source, LPCWSTR dest, DWORD flag )
1046 FILE_BASIC_INFORMATION info;
1047 UNICODE_STRING nt_name;
1048 OBJECT_ATTRIBUTES attr;
1051 HANDLE source_handle = 0, dest_handle;
1052 ANSI_STRING source_unix, dest_unix;
1054 TRACE("(%s,%s,%04lx)\n", debugstr_w(source), debugstr_w(dest), flag);
1056 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
1057 return add_boot_rename_entry( source, dest, flag );
1060 return DeleteFileW( source );
1062 /* check if we are allowed to rename the source */
1064 if (!RtlDosPathNameToNtPathName_U( source, &nt_name, NULL, NULL ))
1066 SetLastError( ERROR_PATH_NOT_FOUND );
1069 source_unix.Buffer = NULL;
1070 dest_unix.Buffer = NULL;
1071 attr.Length = sizeof(attr);
1072 attr.RootDirectory = 0;
1073 attr.Attributes = OBJ_CASE_INSENSITIVE;
1074 attr.ObjectName = &nt_name;
1075 attr.SecurityDescriptor = NULL;
1076 attr.SecurityQualityOfService = NULL;
1078 status = NtOpenFile( &source_handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
1079 if (status == STATUS_SUCCESS)
1080 status = wine_nt_to_unix_file_name( &nt_name, &source_unix, FILE_OPEN, FALSE );
1081 RtlFreeUnicodeString( &nt_name );
1082 if (status != STATUS_SUCCESS)
1084 SetLastError( RtlNtStatusToDosError(status) );
1087 status = NtQueryInformationFile( source_handle, &io, &info, sizeof(info), FileBasicInformation );
1088 if (status != STATUS_SUCCESS)
1090 SetLastError( RtlNtStatusToDosError(status) );
1094 if (info.FileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1096 if (flag & MOVEFILE_REPLACE_EXISTING) /* cannot replace directory */
1098 SetLastError( ERROR_INVALID_PARAMETER );
1103 /* we must have write access to the destination, and it must */
1104 /* not exist except if MOVEFILE_REPLACE_EXISTING is set */
1106 if (!RtlDosPathNameToNtPathName_U( dest, &nt_name, NULL, NULL ))
1108 SetLastError( ERROR_PATH_NOT_FOUND );
1111 status = NtOpenFile( &dest_handle, GENERIC_READ | GENERIC_WRITE, &attr, &io, 0,
1112 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1113 if (status == STATUS_SUCCESS)
1115 NtClose( dest_handle );
1116 if (!(flag & MOVEFILE_REPLACE_EXISTING))
1118 SetLastError( ERROR_ALREADY_EXISTS );
1119 RtlFreeUnicodeString( &nt_name );
1123 else if (status != STATUS_OBJECT_NAME_NOT_FOUND)
1125 SetLastError( RtlNtStatusToDosError(status) );
1126 RtlFreeUnicodeString( &nt_name );
1130 status = wine_nt_to_unix_file_name( &nt_name, &dest_unix, FILE_OPEN_IF, FALSE );
1131 RtlFreeUnicodeString( &nt_name );
1132 if (status != STATUS_SUCCESS && status != STATUS_NO_SUCH_FILE)
1134 SetLastError( RtlNtStatusToDosError(status) );
1138 /* now perform the rename */
1140 if (rename( source_unix.Buffer, dest_unix.Buffer ) == -1)
1142 if (errno == EXDEV && (flag & MOVEFILE_COPY_ALLOWED))
1144 NtClose( source_handle );
1145 RtlFreeAnsiString( &source_unix );
1146 RtlFreeAnsiString( &dest_unix );
1147 return (CopyFileW( source, dest, TRUE ) && DeleteFileW( source ));
1150 /* if we created the destination, remove it */
1151 if (io.Information == FILE_CREATED) unlink( dest_unix.Buffer );
1155 /* fixup executable permissions */
1157 if (is_executable( source ) != is_executable( dest ))
1160 if (stat( dest_unix.Buffer, &fstat ) != -1)
1162 if (is_executable( dest ))
1163 /* set executable bit where read bit is set */
1164 fstat.st_mode |= (fstat.st_mode & 0444) >> 2;
1166 fstat.st_mode &= ~0111;
1167 chmod( dest_unix.Buffer, fstat.st_mode );
1171 NtClose( source_handle );
1172 RtlFreeAnsiString( &source_unix );
1173 RtlFreeAnsiString( &dest_unix );
1177 if (source_handle) NtClose( source_handle );
1178 RtlFreeAnsiString( &source_unix );
1179 RtlFreeAnsiString( &dest_unix );
1183 /**************************************************************************
1184 * MoveFileExA (KERNEL32.@)
1186 BOOL WINAPI MoveFileExA( LPCSTR source, LPCSTR dest, DWORD flag )
1188 UNICODE_STRING sourceW, destW;
1193 SetLastError(ERROR_INVALID_PARAMETER);
1197 RtlCreateUnicodeStringFromAsciiz(&sourceW, source);
1198 if (dest) RtlCreateUnicodeStringFromAsciiz(&destW, dest);
1199 else destW.Buffer = NULL;
1201 ret = MoveFileExW( sourceW.Buffer, destW.Buffer, flag );
1203 RtlFreeUnicodeString(&sourceW);
1204 RtlFreeUnicodeString(&destW);
1209 /**************************************************************************
1210 * MoveFileW (KERNEL32.@)
1212 * Move file or directory
1214 BOOL WINAPI MoveFileW( LPCWSTR source, LPCWSTR dest )
1216 return MoveFileExW( source, dest, MOVEFILE_COPY_ALLOWED );
1220 /**************************************************************************
1221 * MoveFileA (KERNEL32.@)
1223 BOOL WINAPI MoveFileA( LPCSTR source, LPCSTR dest )
1225 return MoveFileExA( source, dest, MOVEFILE_COPY_ALLOWED );
1229 /***********************************************************************
1230 * CreateDirectoryW (KERNEL32.@)
1234 * ERROR_DISK_FULL: on full disk
1235 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
1236 * ERROR_ACCESS_DENIED: on permission problems
1237 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
1239 BOOL WINAPI CreateDirectoryW( LPCWSTR path, LPSECURITY_ATTRIBUTES sa )
1241 OBJECT_ATTRIBUTES attr;
1242 UNICODE_STRING nt_name;
1248 TRACE( "%s\n", debugstr_w(path) );
1250 if (!RtlDosPathNameToNtPathName_U( path, &nt_name, NULL, NULL ))
1252 SetLastError( ERROR_PATH_NOT_FOUND );
1255 attr.Length = sizeof(attr);
1256 attr.RootDirectory = 0;
1257 attr.Attributes = OBJ_CASE_INSENSITIVE;
1258 attr.ObjectName = &nt_name;
1259 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1260 attr.SecurityQualityOfService = NULL;
1262 status = NtCreateFile( &handle, GENERIC_READ, &attr, &io, NULL,
1263 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_CREATE,
1264 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
1266 if (status == STATUS_SUCCESS)
1271 else SetLastError( RtlNtStatusToDosError(status) );
1273 RtlFreeUnicodeString( &nt_name );
1278 /***********************************************************************
1279 * CreateDirectoryA (KERNEL32.@)
1281 BOOL WINAPI CreateDirectoryA( LPCSTR path, LPSECURITY_ATTRIBUTES sa )
1283 UNICODE_STRING pathW;
1288 if (!RtlCreateUnicodeStringFromAsciiz(&pathW, path))
1290 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1294 else pathW.Buffer = NULL;
1295 ret = CreateDirectoryW( pathW.Buffer, sa );
1296 RtlFreeUnicodeString( &pathW );
1301 /***********************************************************************
1302 * CreateDirectoryExA (KERNEL32.@)
1304 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path, LPSECURITY_ATTRIBUTES sa )
1306 UNICODE_STRING pathW, templateW;
1311 if (!RtlCreateUnicodeStringFromAsciiz( &pathW, path ))
1313 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1317 else pathW.Buffer = NULL;
1321 if (!RtlCreateUnicodeStringFromAsciiz( &templateW, template ))
1323 RtlFreeUnicodeString( &pathW );
1324 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1328 else templateW.Buffer = NULL;
1330 ret = CreateDirectoryExW( templateW.Buffer, pathW.Buffer, sa );
1331 RtlFreeUnicodeString( &pathW );
1332 RtlFreeUnicodeString( &templateW );
1337 /***********************************************************************
1338 * CreateDirectoryExW (KERNEL32.@)
1340 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path, LPSECURITY_ATTRIBUTES sa )
1342 return CreateDirectoryW( path, sa );
1346 /***********************************************************************
1347 * RemoveDirectoryW (KERNEL32.@)
1349 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
1351 OBJECT_ATTRIBUTES attr;
1352 UNICODE_STRING nt_name;
1353 ANSI_STRING unix_name;
1359 TRACE( "%s\n", debugstr_w(path) );
1361 if (!RtlDosPathNameToNtPathName_U( path, &nt_name, NULL, NULL ))
1363 SetLastError( ERROR_PATH_NOT_FOUND );
1366 attr.Length = sizeof(attr);
1367 attr.RootDirectory = 0;
1368 attr.Attributes = OBJ_CASE_INSENSITIVE;
1369 attr.ObjectName = &nt_name;
1370 attr.SecurityDescriptor = NULL;
1371 attr.SecurityQualityOfService = NULL;
1373 status = NtOpenFile( &handle, GENERIC_READ, &attr, &io,
1374 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1375 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1376 if (status == STATUS_SUCCESS)
1377 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, FALSE );
1378 RtlFreeUnicodeString( &nt_name );
1380 if (status != STATUS_SUCCESS)
1382 SetLastError( RtlNtStatusToDosError(status) );
1386 if (!(ret = (rmdir( unix_name.Buffer ) != -1))) FILE_SetDosError();
1387 RtlFreeAnsiString( &unix_name );
1393 /***********************************************************************
1394 * RemoveDirectoryA (KERNEL32.@)
1396 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
1398 UNICODE_STRING pathW;
1403 SetLastError(ERROR_INVALID_PARAMETER);
1407 if (RtlCreateUnicodeStringFromAsciiz(&pathW, path))
1409 ret = RemoveDirectoryW(pathW.Buffer);
1410 RtlFreeUnicodeString(&pathW);
1413 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1418 /***********************************************************************
1419 * GetCurrentDirectoryW (KERNEL32.@)
1421 UINT WINAPI GetCurrentDirectoryW( UINT buflen, LPWSTR buf )
1423 return RtlGetCurrentDirectory_U( buflen * sizeof(WCHAR), buf ) / sizeof(WCHAR);
1427 /***********************************************************************
1428 * GetCurrentDirectoryA (KERNEL32.@)
1430 UINT WINAPI GetCurrentDirectoryA( UINT buflen, LPSTR buf )
1432 WCHAR bufferW[MAX_PATH];
1435 retW = GetCurrentDirectoryW(MAX_PATH, bufferW);
1439 else if (retW > MAX_PATH)
1441 SetLastError(ERROR_FILENAME_EXCED_RANGE);
1446 ret = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
1449 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buf, buflen, NULL, NULL);
1450 ret--; /* length without 0 */
1457 /***********************************************************************
1458 * SetCurrentDirectoryW (KERNEL32.@)
1460 BOOL WINAPI SetCurrentDirectoryW( LPCWSTR dir )
1462 UNICODE_STRING dirW;
1465 RtlInitUnicodeString( &dirW, dir );
1466 status = RtlSetCurrentDirectory_U( &dirW );
1467 if (status != STATUS_SUCCESS)
1469 SetLastError( RtlNtStatusToDosError(status) );
1476 /***********************************************************************
1477 * SetCurrentDirectoryA (KERNEL32.@)
1479 BOOL WINAPI SetCurrentDirectoryA( LPCSTR dir )
1481 UNICODE_STRING dirW;
1484 if (!RtlCreateUnicodeStringFromAsciiz( &dirW, dir ))
1486 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1489 status = RtlSetCurrentDirectory_U( &dirW );
1490 if (status != STATUS_SUCCESS)
1492 SetLastError( RtlNtStatusToDosError(status) );
1499 /***********************************************************************
1500 * wine_get_unix_file_name (KERNEL32.@) Not a Windows API
1502 * Return the full Unix file name for a given path.
1503 * Returned buffer must be freed by caller.
1505 char *wine_get_unix_file_name( LPCWSTR dosW )
1507 UNICODE_STRING nt_name;
1508 ANSI_STRING unix_name;
1511 if (!RtlDosPathNameToNtPathName_U( dosW, &nt_name, NULL, NULL )) return NULL;
1512 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN_IF, FALSE );
1513 RtlFreeUnicodeString( &nt_name );
1514 if (status && status != STATUS_NO_SUCH_FILE) return NULL;
1515 return unix_name.Buffer;