libwine: Remove __wine_main_arg* from the public header.
[wine/zf.git] / dlls / kernel32 / path.c
blob334fc25e873c9dfa4be9f4c988fdd9839770b460
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 #include "winerror.h"
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winternl.h"
38 #include "kernel_private.h"
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(file);
44 #define MAX_PATHNAME_LEN 1024
46 /* check if a file name is for an executable file (.exe or .com) */
47 static inline BOOL is_executable( const WCHAR *name )
49 static const WCHAR exeW[] = {'.','e','x','e',0};
50 static const WCHAR comW[] = {'.','c','o','m',0};
51 int len = strlenW(name);
53 if (len < 4) return FALSE;
54 return (!strcmpiW( name + len - 4, exeW ) || !strcmpiW( name + len - 4, comW ));
57 /***********************************************************************
58 * copy_filename_WtoA
60 * copy a file name back to OEM/Ansi, but only if the buffer is large enough
62 static DWORD copy_filename_WtoA( LPCWSTR nameW, LPSTR buffer, DWORD len )
64 UNICODE_STRING strW;
65 DWORD ret;
66 BOOL is_ansi = AreFileApisANSI();
68 RtlInitUnicodeString( &strW, nameW );
70 ret = is_ansi ? RtlUnicodeStringToAnsiSize(&strW) : RtlUnicodeStringToOemSize(&strW);
71 if (buffer && ret <= len)
73 ANSI_STRING str;
75 str.Buffer = buffer;
76 str.MaximumLength = min( len, UNICODE_STRING_MAX_CHARS );
77 if (is_ansi)
78 RtlUnicodeStringToAnsiString( &str, &strW, FALSE );
79 else
80 RtlUnicodeStringToOemString( &str, &strW, FALSE );
81 ret = str.Length; /* length without terminating 0 */
83 return ret;
86 /***********************************************************************
87 * add_boot_rename_entry
89 * Adds an entry to the registry that is loaded when windows boots and
90 * checks if there are some files to be removed or renamed/moved.
91 * <fn1> has to be valid and <fn2> may be NULL. If both pointers are
92 * non-NULL then the file is moved, otherwise it is deleted. The
93 * entry of the registry key is always appended with two zero
94 * terminated strings. If <fn2> is NULL then the second entry is
95 * simply a single 0-byte. Otherwise the second filename goes
96 * there. The entries are prepended with \??\ before the path and the
97 * second filename gets also a '!' as the first character if
98 * MOVEFILE_REPLACE_EXISTING is set. After the final string another
99 * 0-byte follows to indicate the end of the strings.
100 * i.e.:
101 * \??\D:\test\file1[0]
102 * !\??\D:\test\file1_renamed[0]
103 * \??\D:\Test|delete[0]
104 * [0] <- file is to be deleted, second string empty
105 * \??\D:\test\file2[0]
106 * !\??\D:\test\file2_renamed[0]
107 * [0] <- indicates end of strings
109 * or:
110 * \??\D:\test\file1[0]
111 * !\??\D:\test\file1_renamed[0]
112 * \??\D:\Test|delete[0]
113 * [0] <- file is to be deleted, second string empty
114 * [0] <- indicates end of strings
117 static BOOL add_boot_rename_entry( LPCWSTR source, LPCWSTR dest, DWORD flags )
119 static const WCHAR ValueName[] = {'P','e','n','d','i','n','g',
120 'F','i','l','e','R','e','n','a','m','e',
121 'O','p','e','r','a','t','i','o','n','s',0};
122 static const WCHAR SessionW[] = {'\\','R','e','g','i','s','t','r','y','\\',
123 'M','a','c','h','i','n','e','\\',
124 'S','y','s','t','e','m','\\',
125 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
126 'C','o','n','t','r','o','l','\\',
127 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
128 static const int info_size = FIELD_OFFSET( KEY_VALUE_PARTIAL_INFORMATION, Data );
130 OBJECT_ATTRIBUTES attr;
131 UNICODE_STRING nameW, source_name, dest_name;
132 KEY_VALUE_PARTIAL_INFORMATION *info;
133 BOOL rc = FALSE;
134 HANDLE Reboot = 0;
135 DWORD len1, len2;
136 DWORD DataSize = 0;
137 BYTE *Buffer = NULL;
138 WCHAR *p;
140 if (!RtlDosPathNameToNtPathName_U( source, &source_name, NULL, NULL ))
142 SetLastError( ERROR_PATH_NOT_FOUND );
143 return FALSE;
145 dest_name.Buffer = NULL;
146 if (dest && !RtlDosPathNameToNtPathName_U( dest, &dest_name, NULL, NULL ))
148 RtlFreeUnicodeString( &source_name );
149 SetLastError( ERROR_PATH_NOT_FOUND );
150 return FALSE;
153 attr.Length = sizeof(attr);
154 attr.RootDirectory = 0;
155 attr.ObjectName = &nameW;
156 attr.Attributes = 0;
157 attr.SecurityDescriptor = NULL;
158 attr.SecurityQualityOfService = NULL;
159 RtlInitUnicodeString( &nameW, SessionW );
161 if (NtCreateKey( &Reboot, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS)
163 WARN("Error creating key for reboot management [%s]\n",
164 "SYSTEM\\CurrentControlSet\\Control\\Session Manager");
165 RtlFreeUnicodeString( &source_name );
166 RtlFreeUnicodeString( &dest_name );
167 return FALSE;
170 len1 = source_name.Length + sizeof(WCHAR);
171 if (dest)
173 len2 = dest_name.Length + sizeof(WCHAR);
174 if (flags & MOVEFILE_REPLACE_EXISTING)
175 len2 += sizeof(WCHAR); /* Plus 1 because of the leading '!' */
177 else len2 = sizeof(WCHAR); /* minimum is the 0 characters for the empty second string */
179 RtlInitUnicodeString( &nameW, ValueName );
181 /* First we check if the key exists and if so how many bytes it already contains. */
182 if (NtQueryValueKey( Reboot, &nameW, KeyValuePartialInformation,
183 NULL, 0, &DataSize ) == STATUS_BUFFER_TOO_SMALL)
185 if (!(Buffer = HeapAlloc( GetProcessHeap(), 0, DataSize + len1 + len2 + sizeof(WCHAR) )))
186 goto Quit;
187 if (NtQueryValueKey( Reboot, &nameW, KeyValuePartialInformation,
188 Buffer, DataSize, &DataSize )) goto Quit;
189 info = (KEY_VALUE_PARTIAL_INFORMATION *)Buffer;
190 if (info->Type != REG_MULTI_SZ) goto Quit;
191 if (DataSize > sizeof(info)) DataSize -= sizeof(WCHAR); /* remove terminating null (will be added back later) */
193 else
195 DataSize = info_size;
196 if (!(Buffer = HeapAlloc( GetProcessHeap(), 0, DataSize + len1 + len2 + sizeof(WCHAR) )))
197 goto Quit;
200 memcpy( Buffer + DataSize, source_name.Buffer, len1 );
201 DataSize += len1;
202 p = (WCHAR *)(Buffer + DataSize);
203 if (dest)
205 if (flags & MOVEFILE_REPLACE_EXISTING)
206 *p++ = '!';
207 memcpy( p, dest_name.Buffer, len2 );
208 DataSize += len2;
210 else
212 *p = 0;
213 DataSize += sizeof(WCHAR);
216 /* add final null */
217 p = (WCHAR *)(Buffer + DataSize);
218 *p = 0;
219 DataSize += sizeof(WCHAR);
221 rc = !NtSetValueKey(Reboot, &nameW, 0, REG_MULTI_SZ, Buffer + info_size, DataSize - info_size);
223 Quit:
224 RtlFreeUnicodeString( &source_name );
225 RtlFreeUnicodeString( &dest_name );
226 if (Reboot) NtClose(Reboot);
227 HeapFree( GetProcessHeap(), 0, Buffer );
228 return(rc);
233 /***********************************************************************
234 * GetShortPathNameA (KERNEL32.@)
236 DWORD WINAPI GetShortPathNameA( LPCSTR longpath, LPSTR shortpath, DWORD shortlen )
238 WCHAR *longpathW;
239 WCHAR shortpathW[MAX_PATH];
240 DWORD ret;
242 TRACE("%s\n", debugstr_a(longpath));
244 if (!(longpathW = FILE_name_AtoW( longpath, FALSE ))) return 0;
246 ret = GetShortPathNameW(longpathW, shortpathW, MAX_PATH);
248 if (!ret) return 0;
249 if (ret > MAX_PATH)
251 SetLastError(ERROR_FILENAME_EXCED_RANGE);
252 return 0;
254 return copy_filename_WtoA( shortpathW, shortpath, shortlen );
258 static BOOL is_same_file(HANDLE h1, HANDLE h2)
260 FILE_ID_INFORMATION id1, id2;
261 IO_STATUS_BLOCK io;
263 return !NtQueryInformationFile( h1, &io, &id1, sizeof(id1), FileIdInformation )
264 && !NtQueryInformationFile( h2, &io, &id2, sizeof(id2), FileIdInformation )
265 && !memcmp( &id1, &id2, sizeof(FILE_ID_INFORMATION) );
268 /**************************************************************************
269 * CopyFileW (KERNEL32.@)
271 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists )
273 return CopyFileExW( source, dest, NULL, NULL, NULL,
274 fail_if_exists ? COPY_FILE_FAIL_IF_EXISTS : 0 );
278 /**************************************************************************
279 * CopyFileA (KERNEL32.@)
281 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists)
283 WCHAR *sourceW, *destW;
284 BOOL ret;
286 if (!(sourceW = FILE_name_AtoW( source, FALSE ))) return FALSE;
287 if (!(destW = FILE_name_AtoW( dest, TRUE ))) return FALSE;
289 ret = CopyFileW( sourceW, destW, fail_if_exists );
291 HeapFree( GetProcessHeap(), 0, destW );
292 return ret;
296 /**************************************************************************
297 * CopyFileExW (KERNEL32.@)
299 BOOL WINAPI CopyFileExW(LPCWSTR source, LPCWSTR dest,
300 LPPROGRESS_ROUTINE progress, LPVOID param,
301 LPBOOL cancel_ptr, DWORD flags)
303 static const int buffer_size = 65536;
304 HANDLE h1, h2;
305 BY_HANDLE_FILE_INFORMATION info;
306 DWORD count;
307 BOOL ret = FALSE;
308 char *buffer;
310 if (!source || !dest)
312 SetLastError(ERROR_INVALID_PARAMETER);
313 return FALSE;
315 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
317 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
318 return FALSE;
321 TRACE("%s -> %s, %x\n", debugstr_w(source), debugstr_w(dest), flags);
323 if ((h1 = CreateFileW(source, GENERIC_READ,
324 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
325 NULL, OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE)
327 WARN("Unable to open source %s\n", debugstr_w(source));
328 HeapFree( GetProcessHeap(), 0, buffer );
329 return FALSE;
332 if (!GetFileInformationByHandle( h1, &info ))
334 WARN("GetFileInformationByHandle returned error for %s\n", debugstr_w(source));
335 HeapFree( GetProcessHeap(), 0, buffer );
336 CloseHandle( h1 );
337 return FALSE;
340 if (!(flags & COPY_FILE_FAIL_IF_EXISTS))
342 BOOL same_file = FALSE;
343 h2 = CreateFileW( dest, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
344 OPEN_EXISTING, 0, 0);
345 if (h2 != INVALID_HANDLE_VALUE)
347 same_file = is_same_file( h1, h2 );
348 CloseHandle( h2 );
350 if (same_file)
352 HeapFree( GetProcessHeap(), 0, buffer );
353 CloseHandle( h1 );
354 SetLastError( ERROR_SHARING_VIOLATION );
355 return FALSE;
359 if ((h2 = CreateFileW( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
360 (flags & COPY_FILE_FAIL_IF_EXISTS) ? CREATE_NEW : CREATE_ALWAYS,
361 info.dwFileAttributes, h1 )) == INVALID_HANDLE_VALUE)
363 WARN("Unable to open dest %s\n", debugstr_w(dest));
364 HeapFree( GetProcessHeap(), 0, buffer );
365 CloseHandle( h1 );
366 return FALSE;
369 while (ReadFile( h1, buffer, buffer_size, &count, NULL ) && count)
371 char *p = buffer;
372 while (count != 0)
374 DWORD res;
375 if (!WriteFile( h2, p, count, &res, NULL ) || !res) goto done;
376 p += res;
377 count -= res;
380 ret = TRUE;
381 done:
382 /* Maintain the timestamp of source file to destination file */
383 SetFileTime(h2, NULL, NULL, &info.ftLastWriteTime);
384 HeapFree( GetProcessHeap(), 0, buffer );
385 CloseHandle( h1 );
386 CloseHandle( h2 );
387 return ret;
391 /**************************************************************************
392 * CopyFileExA (KERNEL32.@)
394 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename, LPCSTR destFilename,
395 LPPROGRESS_ROUTINE progressRoutine, LPVOID appData,
396 LPBOOL cancelFlagPointer, DWORD copyFlags)
398 WCHAR *sourceW, *destW;
399 BOOL ret;
401 /* can't use the TEB buffer since we may have a callback routine */
402 if (!(sourceW = FILE_name_AtoW( sourceFilename, TRUE ))) return FALSE;
403 if (!(destW = FILE_name_AtoW( destFilename, TRUE )))
405 HeapFree( GetProcessHeap(), 0, sourceW );
406 return FALSE;
408 ret = CopyFileExW(sourceW, destW, progressRoutine, appData,
409 cancelFlagPointer, copyFlags);
410 HeapFree( GetProcessHeap(), 0, sourceW );
411 HeapFree( GetProcessHeap(), 0, destW );
412 return ret;
415 /**************************************************************************
416 * MoveFileTransactedA (KERNEL32.@)
418 BOOL WINAPI MoveFileTransactedA(const char *source, const char *dest, LPPROGRESS_ROUTINE progress, void *data, DWORD flags, HANDLE handle)
420 FIXME("(%s, %s, %p, %p, %d, %p)\n", debugstr_a(source), debugstr_a(dest), progress, data, flags, handle);
421 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
422 return FALSE;
425 /**************************************************************************
426 * MoveFileTransactedW (KERNEL32.@)
428 BOOL WINAPI MoveFileTransactedW(const WCHAR *source, const WCHAR *dest, LPPROGRESS_ROUTINE progress, void *data, DWORD flags, HANDLE handle)
430 FIXME("(%s, %s, %p, %p, %d, %p)\n", debugstr_w(source), debugstr_w(dest), progress, data, flags, handle);
431 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
432 return FALSE;
435 /**************************************************************************
436 * MoveFileWithProgressW (KERNEL32.@)
438 BOOL WINAPI MoveFileWithProgressW( LPCWSTR source, LPCWSTR dest,
439 LPPROGRESS_ROUTINE fnProgress,
440 LPVOID param, DWORD flag )
442 FILE_RENAME_INFORMATION *rename_info;
443 FILE_BASIC_INFORMATION info;
444 UNICODE_STRING nt_name;
445 OBJECT_ATTRIBUTES attr;
446 IO_STATUS_BLOCK io;
447 NTSTATUS status;
448 HANDLE source_handle = 0;
449 ULONG size;
451 TRACE("(%s,%s,%p,%p,%04x)\n",
452 debugstr_w(source), debugstr_w(dest), fnProgress, param, flag );
454 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
455 return add_boot_rename_entry( source, dest, flag );
457 if (!dest)
458 return DeleteFileW( source );
460 /* check if we are allowed to rename the source */
462 if (!RtlDosPathNameToNtPathName_U( source, &nt_name, NULL, NULL ))
464 SetLastError( ERROR_PATH_NOT_FOUND );
465 return FALSE;
467 attr.Length = sizeof(attr);
468 attr.RootDirectory = 0;
469 attr.Attributes = OBJ_CASE_INSENSITIVE;
470 attr.ObjectName = &nt_name;
471 attr.SecurityDescriptor = NULL;
472 attr.SecurityQualityOfService = NULL;
474 status = NtOpenFile( &source_handle, DELETE | SYNCHRONIZE, &attr, &io,
475 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_SYNCHRONOUS_IO_NONALERT );
476 RtlFreeUnicodeString( &nt_name );
477 if (status != STATUS_SUCCESS)
479 SetLastError( RtlNtStatusToDosError(status) );
480 goto error;
482 status = NtQueryInformationFile( source_handle, &io, &info, sizeof(info), FileBasicInformation );
483 if (status != STATUS_SUCCESS)
485 SetLastError( RtlNtStatusToDosError(status) );
486 goto error;
489 if (!RtlDosPathNameToNtPathName_U( dest, &nt_name, NULL, NULL ))
491 SetLastError( ERROR_PATH_NOT_FOUND );
492 goto error;
495 size = offsetof( FILE_RENAME_INFORMATION, FileName ) + nt_name.Length;
496 if (!(rename_info = HeapAlloc( GetProcessHeap(), 0, size )))
497 goto error;
499 rename_info->ReplaceIfExists = !!(flag & MOVEFILE_REPLACE_EXISTING);
500 rename_info->RootDirectory = NULL;
501 rename_info->FileNameLength = nt_name.Length;
502 memcpy( rename_info->FileName, nt_name.Buffer, nt_name.Length );
503 RtlFreeUnicodeString( &nt_name );
504 status = NtSetInformationFile( source_handle, &io, rename_info, size, FileRenameInformation );
505 if (status == STATUS_NOT_SAME_DEVICE && (flag & MOVEFILE_COPY_ALLOWED))
507 NtClose( source_handle );
508 if (!CopyFileExW( source, dest, fnProgress, param, NULL,
509 flag & MOVEFILE_REPLACE_EXISTING ?
510 0 : COPY_FILE_FAIL_IF_EXISTS ))
511 return FALSE;
512 return DeleteFileW( source );
515 NtClose( source_handle );
516 return set_ntstatus( status );
518 error:
519 if (source_handle) NtClose( source_handle );
520 return FALSE;
523 /**************************************************************************
524 * MoveFileWithProgressA (KERNEL32.@)
526 BOOL WINAPI MoveFileWithProgressA( LPCSTR source, LPCSTR dest,
527 LPPROGRESS_ROUTINE fnProgress,
528 LPVOID param, DWORD flag )
530 WCHAR *sourceW, *destW;
531 BOOL ret;
533 if (!(sourceW = FILE_name_AtoW( source, FALSE ))) return FALSE;
534 if (dest)
536 if (!(destW = FILE_name_AtoW( dest, TRUE ))) return FALSE;
538 else
539 destW = NULL;
541 ret = MoveFileWithProgressW( sourceW, destW, fnProgress, param, flag );
542 HeapFree( GetProcessHeap(), 0, destW );
543 return ret;
546 /**************************************************************************
547 * MoveFileExW (KERNEL32.@)
549 BOOL WINAPI MoveFileExW( LPCWSTR source, LPCWSTR dest, DWORD flag )
551 return MoveFileWithProgressW( source, dest, NULL, NULL, flag );
554 /**************************************************************************
555 * MoveFileExA (KERNEL32.@)
557 BOOL WINAPI MoveFileExA( LPCSTR source, LPCSTR dest, DWORD flag )
559 return MoveFileWithProgressA( source, dest, NULL, NULL, flag );
563 /**************************************************************************
564 * MoveFileW (KERNEL32.@)
566 * Move file or directory
568 BOOL WINAPI MoveFileW( LPCWSTR source, LPCWSTR dest )
570 return MoveFileExW( source, dest, MOVEFILE_COPY_ALLOWED );
574 /**************************************************************************
575 * MoveFileA (KERNEL32.@)
577 BOOL WINAPI MoveFileA( LPCSTR source, LPCSTR dest )
579 return MoveFileExA( source, dest, MOVEFILE_COPY_ALLOWED );
583 /*************************************************************************
584 * CreateHardLinkW (KERNEL32.@)
586 BOOL WINAPI CreateHardLinkW(LPCWSTR lpFileName, LPCWSTR lpExistingFileName,
587 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
589 UNICODE_STRING ntDest, ntSource;
590 FILE_LINK_INFORMATION *info = NULL;
591 OBJECT_ATTRIBUTES attr;
592 IO_STATUS_BLOCK io;
593 BOOL ret = FALSE;
594 HANDLE file;
595 ULONG size;
597 TRACE("(%s, %s, %p)\n", debugstr_w(lpFileName),
598 debugstr_w(lpExistingFileName), lpSecurityAttributes);
600 ntDest.Buffer = ntSource.Buffer = NULL;
601 if (!RtlDosPathNameToNtPathName_U( lpFileName, &ntDest, NULL, NULL ) ||
602 !RtlDosPathNameToNtPathName_U( lpExistingFileName, &ntSource, NULL, NULL ))
604 SetLastError( ERROR_PATH_NOT_FOUND );
605 goto err;
608 size = offsetof( FILE_LINK_INFORMATION, FileName ) + ntDest.Length;
609 if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
611 SetLastError( ERROR_OUTOFMEMORY );
612 goto err;
615 InitializeObjectAttributes( &attr, &ntSource, OBJ_CASE_INSENSITIVE, 0, NULL );
616 if (!(ret = set_ntstatus( NtOpenFile( &file, SYNCHRONIZE, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
617 FILE_SYNCHRONOUS_IO_NONALERT ) )))
618 goto err;
620 info->ReplaceIfExists = FALSE;
621 info->RootDirectory = NULL;
622 info->FileNameLength = ntDest.Length;
623 memcpy( info->FileName, ntDest.Buffer, ntDest.Length );
624 ret = set_ntstatus( NtSetInformationFile( file, &io, info, size, FileLinkInformation ) );
626 NtClose( file );
628 err:
629 RtlFreeUnicodeString( &ntSource );
630 RtlFreeUnicodeString( &ntDest );
631 HeapFree( GetProcessHeap(), 0, info );
632 return ret;
636 /*************************************************************************
637 * CreateHardLinkA (KERNEL32.@)
639 BOOL WINAPI CreateHardLinkA(LPCSTR lpFileName, LPCSTR lpExistingFileName,
640 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
642 WCHAR *sourceW, *destW;
643 BOOL res;
645 if (!(sourceW = FILE_name_AtoW( lpExistingFileName, TRUE )))
647 return FALSE;
649 if (!(destW = FILE_name_AtoW( lpFileName, TRUE )))
651 HeapFree( GetProcessHeap(), 0, sourceW );
652 return FALSE;
655 res = CreateHardLinkW( destW, sourceW, lpSecurityAttributes );
657 HeapFree( GetProcessHeap(), 0, sourceW );
658 HeapFree( GetProcessHeap(), 0, destW );
660 return res;
664 /***********************************************************************
665 * CreateDirectoryExA (KERNEL32.@)
667 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path, LPSECURITY_ATTRIBUTES sa )
669 WCHAR *pathW, *templateW = NULL;
670 BOOL ret;
672 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
673 if (template && !(templateW = FILE_name_AtoW( template, TRUE ))) return FALSE;
675 ret = CreateDirectoryExW( templateW, pathW, sa );
676 HeapFree( GetProcessHeap(), 0, templateW );
677 return ret;
681 /***********************************************************************
682 * RemoveDirectoryW (KERNEL32.@)
684 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
686 OBJECT_ATTRIBUTES attr;
687 UNICODE_STRING nt_name;
688 ANSI_STRING unix_name;
689 IO_STATUS_BLOCK io;
690 NTSTATUS status;
691 HANDLE handle;
692 BOOL ret = FALSE;
694 TRACE( "%s\n", debugstr_w(path) );
696 if (!RtlDosPathNameToNtPathName_U( path, &nt_name, NULL, NULL ))
698 SetLastError( ERROR_PATH_NOT_FOUND );
699 return FALSE;
701 attr.Length = sizeof(attr);
702 attr.RootDirectory = 0;
703 attr.Attributes = OBJ_CASE_INSENSITIVE;
704 attr.ObjectName = &nt_name;
705 attr.SecurityDescriptor = NULL;
706 attr.SecurityQualityOfService = NULL;
708 if (!set_ntstatus( NtOpenFile( &handle, DELETE | SYNCHRONIZE, &attr, &io,
709 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
710 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT )))
712 RtlFreeUnicodeString( &nt_name );
713 return FALSE;
716 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, FALSE );
717 RtlFreeUnicodeString( &nt_name );
718 if (!set_ntstatus( status ))
720 NtClose( handle );
721 return FALSE;
724 if (!(ret = (rmdir( unix_name.Buffer ) != -1))) FILE_SetDosError();
725 RtlFreeAnsiString( &unix_name );
726 NtClose( handle );
727 return ret;
731 /***********************************************************************
732 * RemoveDirectoryA (KERNEL32.@)
734 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
736 WCHAR *pathW;
738 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
739 return RemoveDirectoryW( pathW );
743 /***********************************************************************
744 * GetSystemDirectoryW (KERNEL32.@)
746 * See comment for GetWindowsDirectoryA.
748 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
750 UINT len = strlenW( DIR_System ) + 1;
751 if (path && count >= len)
753 strcpyW( path, DIR_System );
754 len--;
756 return len;
760 /***********************************************************************
761 * GetSystemDirectoryA (KERNEL32.@)
763 * See comment for GetWindowsDirectoryA.
765 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
767 return copy_filename_WtoA( DIR_System, path, count );
771 /***********************************************************************
772 * Wow64EnableWow64FsRedirection (KERNEL32.@)
774 * Microsoft C++ Redistributable installers are depending on all %eax bits being set.
776 DWORD /*BOOLEAN*/ WINAPI KERNEL32_Wow64EnableWow64FsRedirection( BOOLEAN enable )
778 return set_ntstatus( RtlWow64EnableFsRedirection( enable ));
782 /***********************************************************************
783 * wine_get_unix_file_name (KERNEL32.@) Not a Windows API
785 * Return the full Unix file name for a given path.
786 * Returned buffer must be freed by caller.
788 char * CDECL wine_get_unix_file_name( LPCWSTR dosW )
790 UNICODE_STRING nt_name;
791 ANSI_STRING unix_name;
792 NTSTATUS status;
794 if (!RtlDosPathNameToNtPathName_U( dosW, &nt_name, NULL, NULL )) return NULL;
795 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN_IF, FALSE );
796 RtlFreeUnicodeString( &nt_name );
797 if (status && status != STATUS_NO_SUCH_FILE)
799 SetLastError( RtlNtStatusToDosError( status ) );
800 return NULL;
802 return unix_name.Buffer;
806 /***********************************************************************
807 * wine_get_dos_file_name (KERNEL32.@) Not a Windows API
809 * Return the full DOS file name for a given Unix path.
810 * Returned buffer must be freed by caller.
812 WCHAR * CDECL wine_get_dos_file_name( LPCSTR str )
814 UNICODE_STRING nt_name;
815 ANSI_STRING unix_name;
816 DWORD len;
818 RtlInitAnsiString( &unix_name, str );
819 if (!set_ntstatus( wine_unix_to_nt_file_name( &unix_name, &nt_name ))) return NULL;
820 if (nt_name.Buffer[5] == ':')
822 /* get rid of the \??\ prefix */
823 /* FIXME: should implement RtlNtPathNameToDosPathName and use that instead */
824 len = nt_name.Length - 4 * sizeof(WCHAR);
825 memmove( nt_name.Buffer, nt_name.Buffer + 4, len );
826 nt_name.Buffer[len / sizeof(WCHAR)] = 0;
828 else
829 nt_name.Buffer[1] = '\\';
830 return nt_name.Buffer;
833 /*************************************************************************
834 * CreateSymbolicLinkW (KERNEL32.@)
836 BOOLEAN WINAPI CreateSymbolicLinkW(LPCWSTR link, LPCWSTR target, DWORD flags)
838 FIXME("(%s %s %d): stub\n", debugstr_w(link), debugstr_w(target), flags);
839 return TRUE;
842 /*************************************************************************
843 * CreateSymbolicLinkA (KERNEL32.@)
845 BOOLEAN WINAPI CreateSymbolicLinkA(LPCSTR link, LPCSTR target, DWORD flags)
847 FIXME("(%s %s %d): stub\n", debugstr_a(link), debugstr_a(target), flags);
848 return TRUE;
851 /*************************************************************************
852 * CreateHardLinkTransactedA (KERNEL32.@)
854 BOOL WINAPI CreateHardLinkTransactedA(LPCSTR link, LPCSTR target, LPSECURITY_ATTRIBUTES sa, HANDLE transaction)
856 FIXME("(%s %s %p %p): stub\n", debugstr_a(link), debugstr_a(target), sa, transaction);
857 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
858 return FALSE;
861 /*************************************************************************
862 * CreateHardLinkTransactedW (KERNEL32.@)
864 BOOL WINAPI CreateHardLinkTransactedW(LPCWSTR link, LPCWSTR target, LPSECURITY_ATTRIBUTES sa, HANDLE transaction)
866 FIXME("(%s %s %p %p): stub\n", debugstr_w(link), debugstr_w(target), sa, transaction);
867 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
868 return FALSE;
871 /*************************************************************************
872 * CheckNameLegalDOS8Dot3A (KERNEL32.@)
874 BOOL WINAPI CheckNameLegalDOS8Dot3A(const char *name, char *oemname, DWORD oemname_len,
875 BOOL *contains_spaces, BOOL *is_legal)
877 WCHAR *nameW;
879 TRACE("(%s %p %u %p %p)\n", name, oemname,
880 oemname_len, contains_spaces, is_legal);
882 if (!name || !is_legal)
883 return FALSE;
885 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
887 return CheckNameLegalDOS8Dot3W( nameW, oemname, oemname_len, contains_spaces, is_legal );
890 /*************************************************************************
891 * CheckNameLegalDOS8Dot3W (KERNEL32.@)
893 BOOL WINAPI CheckNameLegalDOS8Dot3W(const WCHAR *name, char *oemname, DWORD oemname_len,
894 BOOL *contains_spaces_ret, BOOL *is_legal)
896 OEM_STRING oem_str;
897 UNICODE_STRING nameW;
898 BOOLEAN contains_spaces;
900 TRACE("(%s %p %u %p %p)\n", wine_dbgstr_w(name), oemname,
901 oemname_len, contains_spaces_ret, is_legal);
903 if (!name || !is_legal)
904 return FALSE;
906 RtlInitUnicodeString( &nameW, name );
908 if (oemname) {
909 oem_str.Length = oemname_len;
910 oem_str.MaximumLength = oemname_len;
911 oem_str.Buffer = oemname;
914 *is_legal = RtlIsNameLegalDOS8Dot3( &nameW, oemname ? &oem_str : NULL, &contains_spaces );
915 if (contains_spaces_ret) *contains_spaces_ret = contains_spaces;
917 return TRUE;
920 /*************************************************************************
921 * SetSearchPathMode (KERNEL32.@)
923 BOOL WINAPI SetSearchPathMode( DWORD flags )
925 return set_ntstatus( RtlSetSearchPathMode( flags ));