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
));
60 /***********************************************************************
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
)
69 BOOL is_ansi
= AreFileApisANSI();
71 RtlInitUnicodeString( &strW
, nameW
);
73 ret
= is_ansi
? RtlUnicodeStringToAnsiSize(&strW
) : RtlUnicodeStringToOemSize(&strW
);
74 if (buffer
&& ret
<= len
)
79 str
.MaximumLength
= len
;
81 RtlUnicodeStringToAnsiString( &str
, &strW
, FALSE
);
83 RtlUnicodeStringToOemString( &str
, &strW
, FALSE
);
84 ret
= str
.Length
; /* length without terminating 0 */
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 registrykey 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.
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
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
;
142 if (!RtlDosPathNameToNtPathName_U( source
, &source_name
, NULL
, NULL
))
144 SetLastError( ERROR_PATH_NOT_FOUND
);
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
);
155 attr
.Length
= sizeof(attr
);
156 attr
.RootDirectory
= 0;
157 attr
.ObjectName
= &nameW
;
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 managment [%s]\n",
166 "SYSTEM\\CurrentControlSet\\Control\\Session Manager");
167 RtlFreeUnicodeString( &source_name
);
168 RtlFreeUnicodeString( &dest_name
);
172 len1
= source_name
.Length
+ sizeof(WCHAR
);
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_OVERFLOW
)
187 if (!(Buffer
= HeapAlloc( GetProcessHeap(), 0, DataSize
+ len1
+ len2
+ sizeof(WCHAR
) )))
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) */
197 DataSize
= info_size
;
198 if (!(Buffer
= HeapAlloc( GetProcessHeap(), 0, DataSize
+ len1
+ len2
+ sizeof(WCHAR
) )))
202 memcpy( Buffer
+ DataSize
, source_name
.Buffer
, len1
);
204 p
= (WCHAR
*)(Buffer
+ DataSize
);
207 if (flags
& MOVEFILE_REPLACE_EXISTING
)
209 memcpy( p
, dest_name
.Buffer
, len2
);
215 DataSize
+= sizeof(WCHAR
);
219 p
= (WCHAR
*)(Buffer
+ DataSize
);
221 DataSize
+= sizeof(WCHAR
);
223 rc
= !NtSetValueKey(Reboot
, &nameW
, 0, REG_MULTI_SZ
, Buffer
+ info_size
, DataSize
- info_size
);
226 RtlFreeUnicodeString( &source_name
);
227 RtlFreeUnicodeString( &dest_name
);
228 if (Reboot
) NtClose(Reboot
);
229 HeapFree( GetProcessHeap(), 0, Buffer
);
234 /***********************************************************************
235 * GetFullPathNameW (KERNEL32.@)
237 * if the path closed with '\', *lastpart is 0
239 DWORD WINAPI
GetFullPathNameW( LPCWSTR name
, DWORD len
, LPWSTR buffer
,
242 return RtlGetFullPathName_U(name
, len
* sizeof(WCHAR
), buffer
, lastpart
) / sizeof(WCHAR
);
245 /***********************************************************************
246 * GetFullPathNameA (KERNEL32.@)
248 * if the path closed with '\', *lastpart is 0
250 DWORD WINAPI
GetFullPathNameA( LPCSTR name
, DWORD len
, LPSTR buffer
,
254 WCHAR bufferW
[MAX_PATH
];
257 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return 0;
259 ret
= GetFullPathNameW( nameW
, MAX_PATH
, bufferW
, NULL
);
264 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
267 ret
= copy_filename_WtoA( bufferW
, buffer
, len
);
268 if (ret
< len
&& lastpart
)
270 LPSTR p
= buffer
+ strlen(buffer
) - 1;
274 while ((p
> buffer
+ 2) && (*p
!= '\\')) p
--;
277 else *lastpart
= NULL
;
283 /***********************************************************************
284 * GetLongPathNameW (KERNEL32.@)
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
];
295 DWORD sp
= 0, lp
= 0;
297 BOOL unixabsolute
= (shortpath
[0] == '/');
298 WIN32_FIND_DATAW wfd
;
303 SetLastError(ERROR_INVALID_PARAMETER
);
308 SetLastError(ERROR_PATH_NOT_FOUND
);
312 TRACE("%s,%p,%ld\n", debugstr_w(shortpath
), longpath
, longlen
);
314 if (shortpath
[0] == '\\' && shortpath
[1] == '\\')
316 ERR("UNC pathname %s\n", debugstr_w(shortpath
));
317 lstrcpynW( longpath
, shortpath
, longlen
);
318 return strlenW(longpath
);
321 /* check for drive letter */
322 if (!unixabsolute
&& shortpath
[1] == ':' )
324 tmplongpath
[0] = shortpath
[0];
325 tmplongpath
[1] = ':';
329 while (shortpath
[sp
])
331 /* check for path delimiters and reproduce them */
332 if (shortpath
[sp
] == '\\' || shortpath
[sp
] == '/')
334 if (!lp
|| tmplongpath
[lp
-1] != '\\')
336 /* strip double "\\" */
337 tmplongpath
[lp
++] = '\\';
339 tmplongpath
[lp
] = 0; /* terminate string */
345 if (sp
== 0 && p
[0] == '.' && (p
[1] == '/' || p
[1] == '\\'))
347 tmplongpath
[lp
++] = *p
++;
348 tmplongpath
[lp
++] = *p
++;
350 for (; *p
&& *p
!= '/' && *p
!= '\\'; p
++);
351 tmplen
= p
- (shortpath
+ sp
);
352 lstrcpynW(tmplongpath
+ lp
, shortpath
+ sp
, tmplen
+ 1);
353 /* Check if the file exists and use the existing file name */
354 goit
= FindFirstFileW(tmplongpath
, &wfd
);
355 if (goit
== INVALID_HANDLE_VALUE
)
357 TRACE("not found %s!\n", debugstr_w(tmplongpath
));
358 SetLastError ( ERROR_FILE_NOT_FOUND
);
362 strcpyW(tmplongpath
+ lp
, wfd
.cFileName
);
363 lp
+= strlenW(tmplongpath
+ lp
);
366 tmplen
= strlenW(shortpath
) - 1;
367 if ((shortpath
[tmplen
] == '/' || shortpath
[tmplen
] == '\\') &&
368 (tmplongpath
[lp
- 1] != '/' && tmplongpath
[lp
- 1] != '\\'))
369 tmplongpath
[lp
++] = shortpath
[tmplen
];
372 tmplen
= strlenW(tmplongpath
) + 1;
373 if (tmplen
<= longlen
)
375 strcpyW(longpath
, tmplongpath
);
376 TRACE("returning %s\n", debugstr_w(longpath
));
377 tmplen
--; /* length without 0 */
383 /***********************************************************************
384 * GetLongPathNameA (KERNEL32.@)
386 DWORD WINAPI
GetLongPathNameA( LPCSTR shortpath
, LPSTR longpath
, DWORD longlen
)
389 WCHAR longpathW
[MAX_PATH
];
392 TRACE("%s\n", debugstr_a(shortpath
));
394 if (!(shortpathW
= FILE_name_AtoW( shortpath
, FALSE
))) return 0;
396 ret
= GetLongPathNameW(shortpathW
, longpathW
, MAX_PATH
);
401 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
404 return copy_filename_WtoA( longpathW
, longpath
, longlen
);
408 /***********************************************************************
409 * GetShortPathNameW (KERNEL32.@)
413 * longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
414 * longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
416 * more observations ( with NT 3.51 (WinDD) ):
417 * longpath <= 8.3 -> just copy longpath to shortpath
419 * a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
420 * b) file does exist -> set the short filename.
421 * - trailing slashes are reproduced in the short name, even if the
422 * file is not a directory
423 * - the absolute/relative path of the short name is reproduced like found
425 * - longpath and shortpath may have the same address
428 DWORD WINAPI
GetShortPathNameW( LPCWSTR longpath
, LPWSTR shortpath
, DWORD shortlen
)
430 WCHAR tmpshortpath
[MAX_PATHNAME_LEN
];
432 DWORD sp
= 0, lp
= 0;
434 BOOL unixabsolute
= (longpath
[0] == '/');
435 WIN32_FIND_DATAW wfd
;
438 WCHAR ustr_buf
[8+1+3+1];
440 TRACE("%s\n", debugstr_w(longpath
));
444 SetLastError(ERROR_INVALID_PARAMETER
);
449 SetLastError(ERROR_BAD_PATHNAME
);
453 /* check for drive letter */
454 if (!unixabsolute
&& longpath
[1] == ':' )
456 tmpshortpath
[0] = longpath
[0];
457 tmpshortpath
[1] = ':';
461 ustr
.Buffer
= ustr_buf
;
463 ustr
.MaximumLength
= sizeof(ustr_buf
);
467 /* check for path delimiters and reproduce them */
468 if (longpath
[lp
] == '\\' || longpath
[lp
] == '/')
470 if (!sp
|| tmpshortpath
[sp
-1] != '\\')
472 /* strip double "\\" */
473 tmpshortpath
[sp
] = '\\';
476 tmpshortpath
[sp
] = 0; /* terminate string */
481 for (p
= longpath
+ lp
; *p
&& *p
!= '/' && *p
!= '\\'; p
++);
482 tmplen
= p
- (longpath
+ lp
);
483 lstrcpynW(tmpshortpath
+ sp
, longpath
+ lp
, tmplen
+ 1);
484 /* Check, if the current element is a valid dos name */
485 if (tmplen
<= 8+1+3+1)
488 memcpy(ustr_buf
, longpath
+ lp
, tmplen
* sizeof(WCHAR
));
489 ustr_buf
[tmplen
] = '\0';
490 ustr
.Length
= tmplen
* sizeof(WCHAR
);
491 if (RtlIsNameLegalDOS8Dot3(&ustr
, NULL
, &spaces
) && !spaces
)
499 /* Check if the file exists and use the existing short file name */
500 goit
= FindFirstFileW(tmpshortpath
, &wfd
);
501 if (goit
== INVALID_HANDLE_VALUE
) goto notfound
;
503 strcpyW(tmpshortpath
+ sp
, wfd
.cAlternateFileName
);
504 sp
+= strlenW(tmpshortpath
+ sp
);
507 tmpshortpath
[sp
] = 0;
509 tmplen
= strlenW(tmpshortpath
) + 1;
510 if (tmplen
<= shortlen
)
512 strcpyW(shortpath
, tmpshortpath
);
513 TRACE("returning %s\n", debugstr_w(shortpath
));
514 tmplen
--; /* length without 0 */
520 TRACE("not found!\n" );
521 SetLastError ( ERROR_FILE_NOT_FOUND
);
525 /***********************************************************************
526 * GetShortPathNameA (KERNEL32.@)
528 DWORD WINAPI
GetShortPathNameA( LPCSTR longpath
, LPSTR shortpath
, DWORD shortlen
)
531 WCHAR shortpathW
[MAX_PATH
];
534 TRACE("%s\n", debugstr_a(longpath
));
536 if (!(longpathW
= FILE_name_AtoW( longpath
, FALSE
))) return 0;
538 ret
= GetShortPathNameW(longpathW
, shortpathW
, MAX_PATH
);
543 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
546 return copy_filename_WtoA( shortpathW
, shortpath
, shortlen
);
550 /***********************************************************************
551 * GetTempPathA (KERNEL32.@)
553 DWORD WINAPI
GetTempPathA( DWORD count
, LPSTR path
)
555 WCHAR pathW
[MAX_PATH
];
558 ret
= GetTempPathW(MAX_PATH
, pathW
);
565 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
568 return copy_filename_WtoA( pathW
, path
, count
);
572 /***********************************************************************
573 * GetTempPathW (KERNEL32.@)
575 DWORD WINAPI
GetTempPathW( DWORD count
, LPWSTR path
)
577 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
578 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
579 WCHAR tmp_path
[MAX_PATH
];
582 TRACE("%lu,%p\n", count
, path
);
584 if (!(ret
= GetEnvironmentVariableW( tmp
, tmp_path
, MAX_PATH
)))
585 if (!(ret
= GetEnvironmentVariableW( temp
, tmp_path
, MAX_PATH
)))
586 if (!(ret
= GetCurrentDirectoryW( MAX_PATH
, tmp_path
)))
591 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
595 ret
= GetFullPathNameW(tmp_path
, MAX_PATH
, tmp_path
, NULL
);
598 if (ret
> MAX_PATH
- 2)
600 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
604 if (tmp_path
[ret
-1] != '\\')
606 tmp_path
[ret
++] = '\\';
607 tmp_path
[ret
] = '\0';
610 ret
++; /* add space for terminating 0 */
614 lstrcpynW(path
, tmp_path
, count
);
616 ret
--; /* return length without 0 */
618 path
[0] = 0; /* avoid returning ambiguous "X:" */
621 TRACE("returning %u, %s\n", ret
, debugstr_w(path
));
626 /***********************************************************************
627 * GetTempFileNameA (KERNEL32.@)
629 UINT WINAPI
GetTempFileNameA( LPCSTR path
, LPCSTR prefix
, UINT unique
, LPSTR buffer
)
631 WCHAR
*pathW
, *prefixW
= NULL
;
632 WCHAR bufferW
[MAX_PATH
];
635 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return 0;
636 if (prefix
&& !(prefixW
= FILE_name_AtoW( prefix
, TRUE
))) return 0;
638 ret
= GetTempFileNameW(pathW
, prefixW
, unique
, bufferW
);
639 if (ret
) FILE_name_WtoA( bufferW
, -1, buffer
, MAX_PATH
);
641 HeapFree( GetProcessHeap(), 0, prefixW
);
645 /***********************************************************************
646 * GetTempFileNameW (KERNEL32.@)
648 UINT WINAPI
GetTempFileNameW( LPCWSTR path
, LPCWSTR prefix
, UINT unique
, LPWSTR buffer
)
650 static const WCHAR formatW
[] = {'%','x','.','t','m','p',0};
655 if ( !path
|| !prefix
|| !buffer
)
657 SetLastError( ERROR_INVALID_PARAMETER
);
661 strcpyW( buffer
, path
);
662 p
= buffer
+ strlenW(buffer
);
664 /* add a \, if there isn't one */
665 if ((p
== buffer
) || (p
[-1] != '\\')) *p
++ = '\\';
667 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
671 if (unique
) sprintfW( p
, formatW
, unique
);
674 /* get a "random" unique number and try to create the file */
676 UINT num
= GetTickCount() & 0xffff;
682 sprintfW( p
, formatW
, unique
);
683 handle
= CreateFileW( buffer
, GENERIC_WRITE
, 0, NULL
,
684 CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, 0 );
685 if (handle
!= INVALID_HANDLE_VALUE
)
686 { /* We created it */
687 TRACE("created %s\n", debugstr_w(buffer
) );
688 CloseHandle( handle
);
691 if (GetLastError() != ERROR_FILE_EXISTS
&&
692 GetLastError() != ERROR_SHARING_VIOLATION
)
693 break; /* No need to go on */
694 if (!(++unique
& 0xffff)) unique
= 1;
695 } while (unique
!= num
);
698 TRACE("returning %s\n", debugstr_w(buffer
) );
703 /***********************************************************************
706 * Check if the file name contains a path; helper for SearchPathW.
707 * A relative path is not considered a path unless it starts with ./ or ../
709 inline static BOOL
contains_pathW (LPCWSTR name
)
711 if (RtlDetermineDosPathNameType_U( name
) != RELATIVE_PATH
) return TRUE
;
712 if (name
[0] != '.') return FALSE
;
713 if (name
[1] == '/' || name
[1] == '\\') return TRUE
;
714 return (name
[1] == '.' && (name
[2] == '/' || name
[2] == '\\'));
718 /***********************************************************************
719 * SearchPathW [KERNEL32.@]
721 * Searches for a specified file in the search path.
724 * path [I] Path to search
725 * name [I] Filename to search for.
726 * ext [I] File extension to append to file name. The first
727 * character must be a period. This parameter is
728 * specified only if the filename given does not
729 * contain an extension.
730 * buflen [I] size of buffer, in characters
731 * buffer [O] buffer for found filename
732 * lastpart [O] address of pointer to last used character in
733 * buffer (the final '\')
736 * Success: length of string copied into buffer, not including
737 * terminating null character. If the filename found is
738 * longer than the length of the buffer, the length of the
739 * filename is returned.
743 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
746 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
, DWORD buflen
,
747 LPWSTR buffer
, LPWSTR
*lastpart
)
751 /* If the name contains an explicit path, ignore the path */
753 if (contains_pathW(name
))
755 /* try first without extension */
756 if (RtlDoesFileExists_U( name
))
757 return GetFullPathNameW( name
, buflen
, buffer
, lastpart
);
761 LPCWSTR p
= strrchrW( name
, '.' );
762 if (p
&& !strchrW( p
, '/' ) && !strchrW( p
, '\\' ))
763 ext
= NULL
; /* Ignore the specified extension */
766 /* Allocate a buffer for the file name and extension */
770 DWORD len
= strlenW(name
) + strlenW(ext
);
772 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) )))
774 SetLastError( ERROR_OUTOFMEMORY
);
777 strcpyW( tmp
, name
);
779 if (RtlDoesFileExists_U( tmp
))
780 ret
= GetFullPathNameW( tmp
, buflen
, buffer
, lastpart
);
781 HeapFree( GetProcessHeap(), 0, tmp
);
784 else if (path
&& path
[0]) /* search in the specified path */
786 ret
= RtlDosSearchPath_U( path
, name
, ext
, buflen
* sizeof(WCHAR
),
787 buffer
, lastpart
) / sizeof(WCHAR
);
789 else /* search in the default path */
791 WCHAR
*dll_path
= MODULE_get_dll_load_path( NULL
);
795 ret
= RtlDosSearchPath_U( dll_path
, name
, ext
, buflen
* sizeof(WCHAR
),
796 buffer
, lastpart
) / sizeof(WCHAR
);
797 HeapFree( GetProcessHeap(), 0, dll_path
);
801 SetLastError( ERROR_OUTOFMEMORY
);
806 if (!ret
) SetLastError( ERROR_FILE_NOT_FOUND
);
807 else TRACE( "found %s\n", debugstr_w(buffer
) );
812 /***********************************************************************
813 * SearchPathA (KERNEL32.@)
815 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
816 DWORD buflen
, LPSTR buffer
, LPSTR
*lastpart
)
818 WCHAR
*pathW
, *nameW
= NULL
, *extW
= NULL
;
819 WCHAR bufferW
[MAX_PATH
];
822 if (name
&& !(nameW
= FILE_name_AtoW( name
, FALSE
))) return 0;
823 if (!(pathW
= FILE_name_AtoW( path
, TRUE
))) return 0;
824 if (ext
&& !(extW
= FILE_name_AtoW( ext
, TRUE
)))
826 HeapFree( GetProcessHeap(), 0, pathW
);
830 ret
= SearchPathW(pathW
, nameW
, extW
, MAX_PATH
, bufferW
, NULL
);
832 HeapFree( GetProcessHeap(), 0, pathW
);
833 HeapFree( GetProcessHeap(), 0, extW
);
838 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
841 ret
= copy_filename_WtoA( bufferW
, buffer
, buflen
);
842 if (buflen
> ret
&& lastpart
)
843 *lastpart
= strrchr(buffer
, '\\') + 1;
848 /**************************************************************************
849 * CopyFileW (KERNEL32.@)
851 BOOL WINAPI
CopyFileW( LPCWSTR source
, LPCWSTR dest
, BOOL fail_if_exists
)
854 BY_HANDLE_FILE_INFORMATION info
;
860 if (!source
|| !dest
)
862 SetLastError(ERROR_INVALID_PARAMETER
);
866 TRACE("%s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
868 if ((h1
= CreateFileW(source
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
869 NULL
, OPEN_EXISTING
, 0, 0)) == INVALID_HANDLE_VALUE
)
871 WARN("Unable to open source %s\n", debugstr_w(source
));
875 if (!GetFileInformationByHandle( h1
, &info
))
877 WARN("GetFileInformationByHandle returned error for %s\n", debugstr_w(source
));
882 if ((h2
= CreateFileW( dest
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
883 fail_if_exists
? CREATE_NEW
: CREATE_ALWAYS
,
884 info
.dwFileAttributes
, h1
)) == INVALID_HANDLE_VALUE
)
886 WARN("Unable to open dest %s\n", debugstr_w(dest
));
891 while (ReadFile( h1
, buffer
, sizeof(buffer
), &count
, NULL
) && count
)
897 if (!WriteFile( h2
, p
, count
, &res
, NULL
) || !res
) goto done
;
904 /* Maintain the timestamp of source file to destination file */
905 GetFileTime(h1
, NULL
, NULL
, &filetime
);
906 SetFileTime(h2
, NULL
, NULL
, &filetime
);
914 /**************************************************************************
915 * CopyFileA (KERNEL32.@)
917 BOOL WINAPI
CopyFileA( LPCSTR source
, LPCSTR dest
, BOOL fail_if_exists
)
919 WCHAR
*sourceW
, *destW
;
922 if (!(sourceW
= FILE_name_AtoW( source
, FALSE
))) return FALSE
;
923 if (!(destW
= FILE_name_AtoW( dest
, TRUE
))) return FALSE
;
925 ret
= CopyFileW( sourceW
, destW
, fail_if_exists
);
927 HeapFree( GetProcessHeap(), 0, destW
);
932 /**************************************************************************
933 * CopyFileExW (KERNEL32.@)
935 * This implementation ignores most of the extra parameters passed-in into
936 * the "ex" version of the method and calls the CopyFile method.
937 * It will have to be fixed eventually.
939 BOOL WINAPI
CopyFileExW(LPCWSTR sourceFilename
, LPCWSTR destFilename
,
940 LPPROGRESS_ROUTINE progressRoutine
, LPVOID appData
,
941 LPBOOL cancelFlagPointer
, DWORD copyFlags
)
944 * Interpret the only flag that CopyFile can interpret.
946 return CopyFileW(sourceFilename
, destFilename
, (copyFlags
& COPY_FILE_FAIL_IF_EXISTS
) != 0);
950 /**************************************************************************
951 * CopyFileExA (KERNEL32.@)
953 BOOL WINAPI
CopyFileExA(LPCSTR sourceFilename
, LPCSTR destFilename
,
954 LPPROGRESS_ROUTINE progressRoutine
, LPVOID appData
,
955 LPBOOL cancelFlagPointer
, DWORD copyFlags
)
957 WCHAR
*sourceW
, *destW
;
960 /* can't use the TEB buffer since we may have a callback routine */
961 if (!(sourceW
= FILE_name_AtoW( sourceFilename
, TRUE
))) return FALSE
;
962 if (!(destW
= FILE_name_AtoW( destFilename
, TRUE
)))
964 HeapFree( GetProcessHeap(), 0, sourceW
);
967 ret
= CopyFileExW(sourceW
, destW
, progressRoutine
, appData
,
968 cancelFlagPointer
, copyFlags
);
969 HeapFree( GetProcessHeap(), 0, sourceW
);
970 HeapFree( GetProcessHeap(), 0, destW
);
975 /**************************************************************************
976 * MoveFileExW (KERNEL32.@)
978 BOOL WINAPI
MoveFileExW( LPCWSTR source
, LPCWSTR dest
, DWORD flag
)
980 FILE_BASIC_INFORMATION info
;
981 UNICODE_STRING nt_name
;
982 OBJECT_ATTRIBUTES attr
;
985 HANDLE source_handle
= 0, dest_handle
;
986 ANSI_STRING source_unix
, dest_unix
;
988 TRACE("(%s,%s,%04lx)\n", debugstr_w(source
), debugstr_w(dest
), flag
);
990 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
)
991 return add_boot_rename_entry( source
, dest
, flag
);
994 return DeleteFileW( source
);
996 /* check if we are allowed to rename the source */
998 if (!RtlDosPathNameToNtPathName_U( source
, &nt_name
, NULL
, NULL
))
1000 SetLastError( ERROR_PATH_NOT_FOUND
);
1003 source_unix
.Buffer
= NULL
;
1004 dest_unix
.Buffer
= NULL
;
1005 attr
.Length
= sizeof(attr
);
1006 attr
.RootDirectory
= 0;
1007 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1008 attr
.ObjectName
= &nt_name
;
1009 attr
.SecurityDescriptor
= NULL
;
1010 attr
.SecurityQualityOfService
= NULL
;
1012 status
= NtOpenFile( &source_handle
, 0, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
1013 if (status
== STATUS_SUCCESS
)
1014 status
= wine_nt_to_unix_file_name( &nt_name
, &source_unix
, FILE_OPEN
, FALSE
);
1015 RtlFreeUnicodeString( &nt_name
);
1016 if (status
!= STATUS_SUCCESS
)
1018 SetLastError( RtlNtStatusToDosError(status
) );
1021 status
= NtQueryInformationFile( source_handle
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1022 if (status
!= STATUS_SUCCESS
)
1024 SetLastError( RtlNtStatusToDosError(status
) );
1028 if (info
.FileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
1030 if (flag
& MOVEFILE_REPLACE_EXISTING
) /* cannot replace directory */
1032 SetLastError( ERROR_INVALID_PARAMETER
);
1037 /* we must have write access to the destination, and it must */
1038 /* not exist except if MOVEFILE_REPLACE_EXISTING is set */
1040 if (!RtlDosPathNameToNtPathName_U( dest
, &nt_name
, NULL
, NULL
))
1042 SetLastError( ERROR_PATH_NOT_FOUND
);
1045 status
= NtOpenFile( &dest_handle
, GENERIC_READ
| GENERIC_WRITE
, &attr
, &io
, 0,
1046 FILE_NON_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
1047 if (status
== STATUS_SUCCESS
)
1049 NtClose( dest_handle
);
1050 if (!(flag
& MOVEFILE_REPLACE_EXISTING
))
1052 SetLastError( ERROR_ALREADY_EXISTS
);
1053 RtlFreeUnicodeString( &nt_name
);
1057 else if (status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
1059 SetLastError( RtlNtStatusToDosError(status
) );
1060 RtlFreeUnicodeString( &nt_name
);
1064 status
= wine_nt_to_unix_file_name( &nt_name
, &dest_unix
, FILE_OPEN_IF
, FALSE
);
1065 RtlFreeUnicodeString( &nt_name
);
1066 if (status
!= STATUS_SUCCESS
&& status
!= STATUS_NO_SUCH_FILE
)
1068 SetLastError( RtlNtStatusToDosError(status
) );
1072 /* now perform the rename */
1074 if (rename( source_unix
.Buffer
, dest_unix
.Buffer
) == -1)
1076 if (errno
== EXDEV
&& (flag
& MOVEFILE_COPY_ALLOWED
))
1078 NtClose( source_handle
);
1079 RtlFreeAnsiString( &source_unix
);
1080 RtlFreeAnsiString( &dest_unix
);
1081 return (CopyFileW( source
, dest
, TRUE
) && DeleteFileW( source
));
1084 /* if we created the destination, remove it */
1085 if (io
.Information
== FILE_CREATED
) unlink( dest_unix
.Buffer
);
1089 /* fixup executable permissions */
1091 if (is_executable( source
) != is_executable( dest
))
1094 if (stat( dest_unix
.Buffer
, &fstat
) != -1)
1096 if (is_executable( dest
))
1097 /* set executable bit where read bit is set */
1098 fstat
.st_mode
|= (fstat
.st_mode
& 0444) >> 2;
1100 fstat
.st_mode
&= ~0111;
1101 chmod( dest_unix
.Buffer
, fstat
.st_mode
);
1105 NtClose( source_handle
);
1106 RtlFreeAnsiString( &source_unix
);
1107 RtlFreeAnsiString( &dest_unix
);
1111 if (source_handle
) NtClose( source_handle
);
1112 RtlFreeAnsiString( &source_unix
);
1113 RtlFreeAnsiString( &dest_unix
);
1117 /**************************************************************************
1118 * MoveFileExA (KERNEL32.@)
1120 BOOL WINAPI
MoveFileExA( LPCSTR source
, LPCSTR dest
, DWORD flag
)
1122 WCHAR
*sourceW
, *destW
;
1125 if (!(sourceW
= FILE_name_AtoW( source
, FALSE
))) return FALSE
;
1126 if (!(destW
= FILE_name_AtoW( dest
, TRUE
))) return FALSE
;
1127 ret
= MoveFileExW( sourceW
, destW
, flag
);
1128 HeapFree( GetProcessHeap(), 0, destW
);
1133 /**************************************************************************
1134 * MoveFileW (KERNEL32.@)
1136 * Move file or directory
1138 BOOL WINAPI
MoveFileW( LPCWSTR source
, LPCWSTR dest
)
1140 return MoveFileExW( source
, dest
, MOVEFILE_COPY_ALLOWED
);
1144 /**************************************************************************
1145 * MoveFileA (KERNEL32.@)
1147 BOOL WINAPI
MoveFileA( LPCSTR source
, LPCSTR dest
)
1149 return MoveFileExA( source
, dest
, MOVEFILE_COPY_ALLOWED
);
1153 /***********************************************************************
1154 * CreateDirectoryW (KERNEL32.@)
1158 * ERROR_DISK_FULL: on full disk
1159 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
1160 * ERROR_ACCESS_DENIED: on permission problems
1161 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
1163 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
, LPSECURITY_ATTRIBUTES sa
)
1165 OBJECT_ATTRIBUTES attr
;
1166 UNICODE_STRING nt_name
;
1172 TRACE( "%s\n", debugstr_w(path
) );
1174 if (!RtlDosPathNameToNtPathName_U( path
, &nt_name
, NULL
, NULL
))
1176 SetLastError( ERROR_PATH_NOT_FOUND
);
1179 attr
.Length
= sizeof(attr
);
1180 attr
.RootDirectory
= 0;
1181 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1182 attr
.ObjectName
= &nt_name
;
1183 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1184 attr
.SecurityQualityOfService
= NULL
;
1186 status
= NtCreateFile( &handle
, GENERIC_READ
, &attr
, &io
, NULL
,
1187 FILE_ATTRIBUTE_NORMAL
, FILE_SHARE_READ
, FILE_CREATE
,
1188 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
, NULL
, 0 );
1190 if (status
== STATUS_SUCCESS
)
1195 else SetLastError( RtlNtStatusToDosError(status
) );
1197 RtlFreeUnicodeString( &nt_name
);
1202 /***********************************************************************
1203 * CreateDirectoryA (KERNEL32.@)
1205 BOOL WINAPI
CreateDirectoryA( LPCSTR path
, LPSECURITY_ATTRIBUTES sa
)
1209 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1210 return CreateDirectoryW( pathW
, sa
);
1214 /***********************************************************************
1215 * CreateDirectoryExA (KERNEL32.@)
1217 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
, LPSECURITY_ATTRIBUTES sa
)
1219 WCHAR
*pathW
, *templateW
= NULL
;
1222 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1223 if (template && !(templateW
= FILE_name_AtoW( template, TRUE
))) return FALSE
;
1225 ret
= CreateDirectoryExW( templateW
, pathW
, sa
);
1226 HeapFree( GetProcessHeap(), 0, templateW
);
1231 /***********************************************************************
1232 * CreateDirectoryExW (KERNEL32.@)
1234 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
, LPSECURITY_ATTRIBUTES sa
)
1236 return CreateDirectoryW( path
, sa
);
1240 /***********************************************************************
1241 * RemoveDirectoryW (KERNEL32.@)
1243 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
1245 OBJECT_ATTRIBUTES attr
;
1246 UNICODE_STRING nt_name
;
1247 ANSI_STRING unix_name
;
1253 TRACE( "%s\n", debugstr_w(path
) );
1255 if (!RtlDosPathNameToNtPathName_U( path
, &nt_name
, NULL
, NULL
))
1257 SetLastError( ERROR_PATH_NOT_FOUND
);
1260 attr
.Length
= sizeof(attr
);
1261 attr
.RootDirectory
= 0;
1262 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1263 attr
.ObjectName
= &nt_name
;
1264 attr
.SecurityDescriptor
= NULL
;
1265 attr
.SecurityQualityOfService
= NULL
;
1267 status
= NtOpenFile( &handle
, GENERIC_READ
, &attr
, &io
,
1268 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1269 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
1270 if (status
== STATUS_SUCCESS
)
1271 status
= wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN
, FALSE
);
1272 RtlFreeUnicodeString( &nt_name
);
1274 if (status
!= STATUS_SUCCESS
)
1276 SetLastError( RtlNtStatusToDosError(status
) );
1280 if (!(ret
= (rmdir( unix_name
.Buffer
) != -1))) FILE_SetDosError();
1281 RtlFreeAnsiString( &unix_name
);
1287 /***********************************************************************
1288 * RemoveDirectoryA (KERNEL32.@)
1290 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
1294 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1295 return RemoveDirectoryW( pathW
);
1299 /***********************************************************************
1300 * GetCurrentDirectoryW (KERNEL32.@)
1302 UINT WINAPI
GetCurrentDirectoryW( UINT buflen
, LPWSTR buf
)
1304 return RtlGetCurrentDirectory_U( buflen
* sizeof(WCHAR
), buf
) / sizeof(WCHAR
);
1308 /***********************************************************************
1309 * GetCurrentDirectoryA (KERNEL32.@)
1311 UINT WINAPI
GetCurrentDirectoryA( UINT buflen
, LPSTR buf
)
1313 WCHAR bufferW
[MAX_PATH
];
1316 ret
= GetCurrentDirectoryW(MAX_PATH
, bufferW
);
1321 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
1324 return copy_filename_WtoA( bufferW
, buf
, buflen
);
1328 /***********************************************************************
1329 * SetCurrentDirectoryW (KERNEL32.@)
1331 BOOL WINAPI
SetCurrentDirectoryW( LPCWSTR dir
)
1333 UNICODE_STRING dirW
;
1336 RtlInitUnicodeString( &dirW
, dir
);
1337 status
= RtlSetCurrentDirectory_U( &dirW
);
1338 if (status
!= STATUS_SUCCESS
)
1340 SetLastError( RtlNtStatusToDosError(status
) );
1347 /***********************************************************************
1348 * SetCurrentDirectoryA (KERNEL32.@)
1350 BOOL WINAPI
SetCurrentDirectoryA( LPCSTR dir
)
1354 if (!(dirW
= FILE_name_AtoW( dir
, FALSE
))) return FALSE
;
1355 return SetCurrentDirectoryW( dirW
);
1359 /***********************************************************************
1360 * GetWindowsDirectoryW (KERNEL32.@)
1362 * See comment for GetWindowsDirectoryA.
1364 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
1366 UINT len
= strlenW( DIR_Windows
) + 1;
1367 if (path
&& count
>= len
)
1369 strcpyW( path
, DIR_Windows
);
1376 /***********************************************************************
1377 * GetWindowsDirectoryA (KERNEL32.@)
1380 * If buffer is large enough to hold full path and terminating '\0' character
1381 * function copies path to buffer and returns length of the path without '\0'.
1382 * Otherwise function returns required size including '\0' character and
1383 * does not touch the buffer.
1385 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
1387 return copy_filename_WtoA( DIR_Windows
, path
, count
);
1391 /***********************************************************************
1392 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
1394 UINT WINAPI
GetSystemWindowsDirectoryA( LPSTR path
, UINT count
)
1396 return GetWindowsDirectoryA( path
, count
);
1400 /***********************************************************************
1401 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
1403 UINT WINAPI
GetSystemWindowsDirectoryW( LPWSTR path
, UINT count
)
1405 return GetWindowsDirectoryW( path
, count
);
1409 /***********************************************************************
1410 * GetSystemDirectoryW (KERNEL32.@)
1412 * See comment for GetWindowsDirectoryA.
1414 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
1416 UINT len
= strlenW( DIR_System
) + 1;
1417 if (path
&& count
>= len
)
1419 strcpyW( path
, DIR_System
);
1426 /***********************************************************************
1427 * GetSystemDirectoryA (KERNEL32.@)
1429 * See comment for GetWindowsDirectoryA.
1431 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
1433 return copy_filename_WtoA( DIR_System
, path
, count
);
1437 /***********************************************************************
1438 * GetSystemWow64DirectoryW (KERNEL32.@)
1441 * - On Win32 we should returns ERROR_CALL_NOT_IMPLEMENTED
1442 * - On Win64 we should returns the SysWow64 (system64) directory
1444 UINT WINAPI
GetSystemWow64DirectoryW( LPWSTR lpBuffer
, UINT uSize
)
1446 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
1451 /***********************************************************************
1452 * GetSystemWow64DirectoryA (KERNEL32.@)
1454 * See comment for GetWindowsWow64DirectoryW.
1456 UINT WINAPI
GetSystemWow64DirectoryA( LPSTR lpBuffer
, UINT uSize
)
1458 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
1463 /***********************************************************************
1464 * wine_get_unix_file_name (KERNEL32.@) Not a Windows API
1466 * Return the full Unix file name for a given path.
1467 * Returned buffer must be freed by caller.
1469 char *wine_get_unix_file_name( LPCWSTR dosW
)
1471 UNICODE_STRING nt_name
;
1472 ANSI_STRING unix_name
;
1475 if (!RtlDosPathNameToNtPathName_U( dosW
, &nt_name
, NULL
, NULL
)) return NULL
;
1476 status
= wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN_IF
, FALSE
);
1477 RtlFreeUnicodeString( &nt_name
);
1478 if (status
&& status
!= STATUS_NO_SUCH_FILE
) return NULL
;
1479 return unix_name
.Buffer
;