dbghelp: Use local declarations of r_debug and link_map structs.
[wine.git] / dlls / kernel32 / path.c
blob31652d3164bdd6e779de1ed9c7a8387a69168b77
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 int fd1;
261 BOOL ret = FALSE;
262 if (wine_server_handle_to_fd(h1, 0, &fd1, NULL) == STATUS_SUCCESS)
264 int fd2;
265 if (wine_server_handle_to_fd(h2, 0, &fd2, NULL) == STATUS_SUCCESS)
267 struct stat stat1, stat2;
268 if (fstat(fd1, &stat1) == 0 && fstat(fd2, &stat2) == 0)
269 ret = (stat1.st_dev == stat2.st_dev && stat1.st_ino == stat2.st_ino);
270 wine_server_release_fd(h2, fd2);
272 wine_server_release_fd(h1, fd1);
274 return ret;
277 /**************************************************************************
278 * CopyFileW (KERNEL32.@)
280 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists )
282 return CopyFileExW( source, dest, NULL, NULL, NULL,
283 fail_if_exists ? COPY_FILE_FAIL_IF_EXISTS : 0 );
287 /**************************************************************************
288 * CopyFileA (KERNEL32.@)
290 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists)
292 WCHAR *sourceW, *destW;
293 BOOL ret;
295 if (!(sourceW = FILE_name_AtoW( source, FALSE ))) return FALSE;
296 if (!(destW = FILE_name_AtoW( dest, TRUE ))) return FALSE;
298 ret = CopyFileW( sourceW, destW, fail_if_exists );
300 HeapFree( GetProcessHeap(), 0, destW );
301 return ret;
305 /**************************************************************************
306 * CopyFileExW (KERNEL32.@)
308 BOOL WINAPI CopyFileExW(LPCWSTR source, LPCWSTR dest,
309 LPPROGRESS_ROUTINE progress, LPVOID param,
310 LPBOOL cancel_ptr, DWORD flags)
312 static const int buffer_size = 65536;
313 HANDLE h1, h2;
314 BY_HANDLE_FILE_INFORMATION info;
315 DWORD count;
316 BOOL ret = FALSE;
317 char *buffer;
319 if (!source || !dest)
321 SetLastError(ERROR_INVALID_PARAMETER);
322 return FALSE;
324 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
326 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
327 return FALSE;
330 TRACE("%s -> %s, %x\n", debugstr_w(source), debugstr_w(dest), flags);
332 if ((h1 = CreateFileW(source, GENERIC_READ,
333 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
334 NULL, OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE)
336 WARN("Unable to open source %s\n", debugstr_w(source));
337 HeapFree( GetProcessHeap(), 0, buffer );
338 return FALSE;
341 if (!GetFileInformationByHandle( h1, &info ))
343 WARN("GetFileInformationByHandle returned error for %s\n", debugstr_w(source));
344 HeapFree( GetProcessHeap(), 0, buffer );
345 CloseHandle( h1 );
346 return FALSE;
349 if (!(flags & COPY_FILE_FAIL_IF_EXISTS))
351 BOOL same_file = FALSE;
352 h2 = CreateFileW( dest, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
353 OPEN_EXISTING, 0, 0);
354 if (h2 != INVALID_HANDLE_VALUE)
356 same_file = is_same_file( h1, h2 );
357 CloseHandle( h2 );
359 if (same_file)
361 HeapFree( GetProcessHeap(), 0, buffer );
362 CloseHandle( h1 );
363 SetLastError( ERROR_SHARING_VIOLATION );
364 return FALSE;
368 if ((h2 = CreateFileW( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
369 (flags & COPY_FILE_FAIL_IF_EXISTS) ? CREATE_NEW : CREATE_ALWAYS,
370 info.dwFileAttributes, h1 )) == INVALID_HANDLE_VALUE)
372 WARN("Unable to open dest %s\n", debugstr_w(dest));
373 HeapFree( GetProcessHeap(), 0, buffer );
374 CloseHandle( h1 );
375 return FALSE;
378 while (ReadFile( h1, buffer, buffer_size, &count, NULL ) && count)
380 char *p = buffer;
381 while (count != 0)
383 DWORD res;
384 if (!WriteFile( h2, p, count, &res, NULL ) || !res) goto done;
385 p += res;
386 count -= res;
389 ret = TRUE;
390 done:
391 /* Maintain the timestamp of source file to destination file */
392 SetFileTime(h2, NULL, NULL, &info.ftLastWriteTime);
393 HeapFree( GetProcessHeap(), 0, buffer );
394 CloseHandle( h1 );
395 CloseHandle( h2 );
396 return ret;
400 /**************************************************************************
401 * CopyFileExA (KERNEL32.@)
403 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename, LPCSTR destFilename,
404 LPPROGRESS_ROUTINE progressRoutine, LPVOID appData,
405 LPBOOL cancelFlagPointer, DWORD copyFlags)
407 WCHAR *sourceW, *destW;
408 BOOL ret;
410 /* can't use the TEB buffer since we may have a callback routine */
411 if (!(sourceW = FILE_name_AtoW( sourceFilename, TRUE ))) return FALSE;
412 if (!(destW = FILE_name_AtoW( destFilename, TRUE )))
414 HeapFree( GetProcessHeap(), 0, sourceW );
415 return FALSE;
417 ret = CopyFileExW(sourceW, destW, progressRoutine, appData,
418 cancelFlagPointer, copyFlags);
419 HeapFree( GetProcessHeap(), 0, sourceW );
420 HeapFree( GetProcessHeap(), 0, destW );
421 return ret;
424 /**************************************************************************
425 * MoveFileTransactedA (KERNEL32.@)
427 BOOL WINAPI MoveFileTransactedA(const char *source, const char *dest, LPPROGRESS_ROUTINE progress, void *data, DWORD flags, HANDLE handle)
429 FIXME("(%s, %s, %p, %p, %d, %p)\n", debugstr_a(source), debugstr_a(dest), progress, data, flags, handle);
430 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
431 return FALSE;
434 /**************************************************************************
435 * MoveFileTransactedW (KERNEL32.@)
437 BOOL WINAPI MoveFileTransactedW(const WCHAR *source, const WCHAR *dest, LPPROGRESS_ROUTINE progress, void *data, DWORD flags, HANDLE handle)
439 FIXME("(%s, %s, %p, %p, %d, %p)\n", debugstr_w(source), debugstr_w(dest), progress, data, flags, handle);
440 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
441 return FALSE;
444 /**************************************************************************
445 * MoveFileWithProgressW (KERNEL32.@)
447 BOOL WINAPI MoveFileWithProgressW( LPCWSTR source, LPCWSTR dest,
448 LPPROGRESS_ROUTINE fnProgress,
449 LPVOID param, DWORD flag )
451 FILE_RENAME_INFORMATION *rename_info;
452 FILE_BASIC_INFORMATION info;
453 UNICODE_STRING nt_name;
454 OBJECT_ATTRIBUTES attr;
455 IO_STATUS_BLOCK io;
456 NTSTATUS status;
457 HANDLE source_handle = 0;
458 ULONG size;
460 TRACE("(%s,%s,%p,%p,%04x)\n",
461 debugstr_w(source), debugstr_w(dest), fnProgress, param, flag );
463 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
464 return add_boot_rename_entry( source, dest, flag );
466 if (!dest)
467 return DeleteFileW( source );
469 /* check if we are allowed to rename the source */
471 if (!RtlDosPathNameToNtPathName_U( source, &nt_name, NULL, NULL ))
473 SetLastError( ERROR_PATH_NOT_FOUND );
474 return FALSE;
476 attr.Length = sizeof(attr);
477 attr.RootDirectory = 0;
478 attr.Attributes = OBJ_CASE_INSENSITIVE;
479 attr.ObjectName = &nt_name;
480 attr.SecurityDescriptor = NULL;
481 attr.SecurityQualityOfService = NULL;
483 status = NtOpenFile( &source_handle, DELETE | SYNCHRONIZE, &attr, &io,
484 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_SYNCHRONOUS_IO_NONALERT );
485 RtlFreeUnicodeString( &nt_name );
486 if (status != STATUS_SUCCESS)
488 SetLastError( RtlNtStatusToDosError(status) );
489 goto error;
491 status = NtQueryInformationFile( source_handle, &io, &info, sizeof(info), FileBasicInformation );
492 if (status != STATUS_SUCCESS)
494 SetLastError( RtlNtStatusToDosError(status) );
495 goto error;
498 if (!RtlDosPathNameToNtPathName_U( dest, &nt_name, NULL, NULL ))
500 SetLastError( ERROR_PATH_NOT_FOUND );
501 goto error;
504 size = offsetof( FILE_RENAME_INFORMATION, FileName ) + nt_name.Length;
505 if (!(rename_info = HeapAlloc( GetProcessHeap(), 0, size )))
506 goto error;
508 rename_info->ReplaceIfExists = !!(flag & MOVEFILE_REPLACE_EXISTING);
509 rename_info->RootDirectory = NULL;
510 rename_info->FileNameLength = nt_name.Length;
511 memcpy( rename_info->FileName, nt_name.Buffer, nt_name.Length );
512 RtlFreeUnicodeString( &nt_name );
513 status = NtSetInformationFile( source_handle, &io, rename_info, size, FileRenameInformation );
514 if (status == STATUS_NOT_SAME_DEVICE && (flag & MOVEFILE_COPY_ALLOWED))
516 NtClose( source_handle );
517 if (!CopyFileExW( source, dest, fnProgress, param, NULL,
518 flag & MOVEFILE_REPLACE_EXISTING ?
519 0 : COPY_FILE_FAIL_IF_EXISTS ))
520 return FALSE;
521 return DeleteFileW( source );
524 NtClose( source_handle );
525 return set_ntstatus( status );
527 error:
528 if (source_handle) NtClose( source_handle );
529 return FALSE;
532 /**************************************************************************
533 * MoveFileWithProgressA (KERNEL32.@)
535 BOOL WINAPI MoveFileWithProgressA( LPCSTR source, LPCSTR dest,
536 LPPROGRESS_ROUTINE fnProgress,
537 LPVOID param, DWORD flag )
539 WCHAR *sourceW, *destW;
540 BOOL ret;
542 if (!(sourceW = FILE_name_AtoW( source, FALSE ))) return FALSE;
543 if (dest)
545 if (!(destW = FILE_name_AtoW( dest, TRUE ))) return FALSE;
547 else
548 destW = NULL;
550 ret = MoveFileWithProgressW( sourceW, destW, fnProgress, param, flag );
551 HeapFree( GetProcessHeap(), 0, destW );
552 return ret;
555 /**************************************************************************
556 * MoveFileExW (KERNEL32.@)
558 BOOL WINAPI MoveFileExW( LPCWSTR source, LPCWSTR dest, DWORD flag )
560 return MoveFileWithProgressW( source, dest, NULL, NULL, flag );
563 /**************************************************************************
564 * MoveFileExA (KERNEL32.@)
566 BOOL WINAPI MoveFileExA( LPCSTR source, LPCSTR dest, DWORD flag )
568 return MoveFileWithProgressA( source, dest, NULL, NULL, flag );
572 /**************************************************************************
573 * MoveFileW (KERNEL32.@)
575 * Move file or directory
577 BOOL WINAPI MoveFileW( LPCWSTR source, LPCWSTR dest )
579 return MoveFileExW( source, dest, MOVEFILE_COPY_ALLOWED );
583 /**************************************************************************
584 * MoveFileA (KERNEL32.@)
586 BOOL WINAPI MoveFileA( LPCSTR source, LPCSTR dest )
588 return MoveFileExA( source, dest, MOVEFILE_COPY_ALLOWED );
592 /*************************************************************************
593 * CreateHardLinkW (KERNEL32.@)
595 BOOL WINAPI CreateHardLinkW(LPCWSTR lpFileName, LPCWSTR lpExistingFileName,
596 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
598 UNICODE_STRING ntDest, ntSource;
599 FILE_LINK_INFORMATION *info = NULL;
600 OBJECT_ATTRIBUTES attr;
601 IO_STATUS_BLOCK io;
602 BOOL ret = FALSE;
603 HANDLE file;
604 ULONG size;
606 TRACE("(%s, %s, %p)\n", debugstr_w(lpFileName),
607 debugstr_w(lpExistingFileName), lpSecurityAttributes);
609 ntDest.Buffer = ntSource.Buffer = NULL;
610 if (!RtlDosPathNameToNtPathName_U( lpFileName, &ntDest, NULL, NULL ) ||
611 !RtlDosPathNameToNtPathName_U( lpExistingFileName, &ntSource, NULL, NULL ))
613 SetLastError( ERROR_PATH_NOT_FOUND );
614 goto err;
617 size = offsetof( FILE_LINK_INFORMATION, FileName ) + ntDest.Length;
618 if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
620 SetLastError( ERROR_OUTOFMEMORY );
621 goto err;
624 InitializeObjectAttributes( &attr, &ntSource, OBJ_CASE_INSENSITIVE, 0, NULL );
625 if (!(ret = set_ntstatus( NtOpenFile( &file, SYNCHRONIZE, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
626 FILE_SYNCHRONOUS_IO_NONALERT ) )))
627 goto err;
629 info->ReplaceIfExists = FALSE;
630 info->RootDirectory = NULL;
631 info->FileNameLength = ntDest.Length;
632 memcpy( info->FileName, ntDest.Buffer, ntDest.Length );
633 ret = set_ntstatus( NtSetInformationFile( file, &io, info, size, FileLinkInformation ) );
635 NtClose( file );
637 err:
638 RtlFreeUnicodeString( &ntSource );
639 RtlFreeUnicodeString( &ntDest );
640 HeapFree( GetProcessHeap(), 0, info );
641 return ret;
645 /*************************************************************************
646 * CreateHardLinkA (KERNEL32.@)
648 BOOL WINAPI CreateHardLinkA(LPCSTR lpFileName, LPCSTR lpExistingFileName,
649 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
651 WCHAR *sourceW, *destW;
652 BOOL res;
654 if (!(sourceW = FILE_name_AtoW( lpExistingFileName, TRUE )))
656 return FALSE;
658 if (!(destW = FILE_name_AtoW( lpFileName, TRUE )))
660 HeapFree( GetProcessHeap(), 0, sourceW );
661 return FALSE;
664 res = CreateHardLinkW( destW, sourceW, lpSecurityAttributes );
666 HeapFree( GetProcessHeap(), 0, sourceW );
667 HeapFree( GetProcessHeap(), 0, destW );
669 return res;
673 /***********************************************************************
674 * CreateDirectoryExA (KERNEL32.@)
676 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path, LPSECURITY_ATTRIBUTES sa )
678 WCHAR *pathW, *templateW = NULL;
679 BOOL ret;
681 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
682 if (template && !(templateW = FILE_name_AtoW( template, TRUE ))) return FALSE;
684 ret = CreateDirectoryExW( templateW, pathW, sa );
685 HeapFree( GetProcessHeap(), 0, templateW );
686 return ret;
690 /***********************************************************************
691 * RemoveDirectoryW (KERNEL32.@)
693 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
695 OBJECT_ATTRIBUTES attr;
696 UNICODE_STRING nt_name;
697 ANSI_STRING unix_name;
698 IO_STATUS_BLOCK io;
699 NTSTATUS status;
700 HANDLE handle;
701 BOOL ret = FALSE;
703 TRACE( "%s\n", debugstr_w(path) );
705 if (!RtlDosPathNameToNtPathName_U( path, &nt_name, NULL, NULL ))
707 SetLastError( ERROR_PATH_NOT_FOUND );
708 return FALSE;
710 attr.Length = sizeof(attr);
711 attr.RootDirectory = 0;
712 attr.Attributes = OBJ_CASE_INSENSITIVE;
713 attr.ObjectName = &nt_name;
714 attr.SecurityDescriptor = NULL;
715 attr.SecurityQualityOfService = NULL;
717 if (!set_ntstatus( NtOpenFile( &handle, DELETE | SYNCHRONIZE, &attr, &io,
718 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
719 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT )))
721 RtlFreeUnicodeString( &nt_name );
722 return FALSE;
725 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, FALSE );
726 RtlFreeUnicodeString( &nt_name );
727 if (!set_ntstatus( status ))
729 NtClose( handle );
730 return FALSE;
733 if (!(ret = (rmdir( unix_name.Buffer ) != -1))) FILE_SetDosError();
734 RtlFreeAnsiString( &unix_name );
735 NtClose( handle );
736 return ret;
740 /***********************************************************************
741 * RemoveDirectoryA (KERNEL32.@)
743 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
745 WCHAR *pathW;
747 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
748 return RemoveDirectoryW( pathW );
752 /***********************************************************************
753 * GetSystemDirectoryW (KERNEL32.@)
755 * See comment for GetWindowsDirectoryA.
757 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
759 UINT len = strlenW( DIR_System ) + 1;
760 if (path && count >= len)
762 strcpyW( path, DIR_System );
763 len--;
765 return len;
769 /***********************************************************************
770 * GetSystemDirectoryA (KERNEL32.@)
772 * See comment for GetWindowsDirectoryA.
774 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
776 return copy_filename_WtoA( DIR_System, path, count );
780 /***********************************************************************
781 * Wow64EnableWow64FsRedirection (KERNEL32.@)
783 * Microsoft C++ Redistributable installers are depending on all %eax bits being set.
785 DWORD /*BOOLEAN*/ WINAPI KERNEL32_Wow64EnableWow64FsRedirection( BOOLEAN enable )
787 return set_ntstatus( RtlWow64EnableFsRedirection( enable ));
791 /***********************************************************************
792 * wine_get_unix_file_name (KERNEL32.@) Not a Windows API
794 * Return the full Unix file name for a given path.
795 * Returned buffer must be freed by caller.
797 char * CDECL wine_get_unix_file_name( LPCWSTR dosW )
799 UNICODE_STRING nt_name;
800 ANSI_STRING unix_name;
801 NTSTATUS status;
803 if (!RtlDosPathNameToNtPathName_U( dosW, &nt_name, NULL, NULL )) return NULL;
804 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN_IF, FALSE );
805 RtlFreeUnicodeString( &nt_name );
806 if (status && status != STATUS_NO_SUCH_FILE)
808 SetLastError( RtlNtStatusToDosError( status ) );
809 return NULL;
811 return unix_name.Buffer;
815 /***********************************************************************
816 * wine_get_dos_file_name (KERNEL32.@) Not a Windows API
818 * Return the full DOS file name for a given Unix path.
819 * Returned buffer must be freed by caller.
821 WCHAR * CDECL wine_get_dos_file_name( LPCSTR str )
823 UNICODE_STRING nt_name;
824 ANSI_STRING unix_name;
825 DWORD len;
827 RtlInitAnsiString( &unix_name, str );
828 if (!set_ntstatus( wine_unix_to_nt_file_name( &unix_name, &nt_name ))) return NULL;
829 if (nt_name.Buffer[5] == ':')
831 /* get rid of the \??\ prefix */
832 /* FIXME: should implement RtlNtPathNameToDosPathName and use that instead */
833 len = nt_name.Length - 4 * sizeof(WCHAR);
834 memmove( nt_name.Buffer, nt_name.Buffer + 4, len );
835 nt_name.Buffer[len / sizeof(WCHAR)] = 0;
837 else
838 nt_name.Buffer[1] = '\\';
839 return nt_name.Buffer;
842 /*************************************************************************
843 * CreateSymbolicLinkW (KERNEL32.@)
845 BOOLEAN WINAPI CreateSymbolicLinkW(LPCWSTR link, LPCWSTR target, DWORD flags)
847 FIXME("(%s %s %d): stub\n", debugstr_w(link), debugstr_w(target), flags);
848 return TRUE;
851 /*************************************************************************
852 * CreateSymbolicLinkA (KERNEL32.@)
854 BOOLEAN WINAPI CreateSymbolicLinkA(LPCSTR link, LPCSTR target, DWORD flags)
856 FIXME("(%s %s %d): stub\n", debugstr_a(link), debugstr_a(target), flags);
857 return TRUE;
860 /*************************************************************************
861 * CreateHardLinkTransactedA (KERNEL32.@)
863 BOOL WINAPI CreateHardLinkTransactedA(LPCSTR link, LPCSTR target, LPSECURITY_ATTRIBUTES sa, HANDLE transaction)
865 FIXME("(%s %s %p %p): stub\n", debugstr_a(link), debugstr_a(target), sa, transaction);
866 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
867 return FALSE;
870 /*************************************************************************
871 * CreateHardLinkTransactedW (KERNEL32.@)
873 BOOL WINAPI CreateHardLinkTransactedW(LPCWSTR link, LPCWSTR target, LPSECURITY_ATTRIBUTES sa, HANDLE transaction)
875 FIXME("(%s %s %p %p): stub\n", debugstr_w(link), debugstr_w(target), sa, transaction);
876 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
877 return FALSE;
880 /*************************************************************************
881 * CheckNameLegalDOS8Dot3A (KERNEL32.@)
883 BOOL WINAPI CheckNameLegalDOS8Dot3A(const char *name, char *oemname, DWORD oemname_len,
884 BOOL *contains_spaces, BOOL *is_legal)
886 WCHAR *nameW;
888 TRACE("(%s %p %u %p %p)\n", name, oemname,
889 oemname_len, contains_spaces, is_legal);
891 if (!name || !is_legal)
892 return FALSE;
894 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
896 return CheckNameLegalDOS8Dot3W( nameW, oemname, oemname_len, contains_spaces, is_legal );
899 /*************************************************************************
900 * CheckNameLegalDOS8Dot3W (KERNEL32.@)
902 BOOL WINAPI CheckNameLegalDOS8Dot3W(const WCHAR *name, char *oemname, DWORD oemname_len,
903 BOOL *contains_spaces_ret, BOOL *is_legal)
905 OEM_STRING oem_str;
906 UNICODE_STRING nameW;
907 BOOLEAN contains_spaces;
909 TRACE("(%s %p %u %p %p)\n", wine_dbgstr_w(name), oemname,
910 oemname_len, contains_spaces_ret, is_legal);
912 if (!name || !is_legal)
913 return FALSE;
915 RtlInitUnicodeString( &nameW, name );
917 if (oemname) {
918 oem_str.Length = oemname_len;
919 oem_str.MaximumLength = oemname_len;
920 oem_str.Buffer = oemname;
923 *is_legal = RtlIsNameLegalDOS8Dot3( &nameW, oemname ? &oem_str : NULL, &contains_spaces );
924 if (contains_spaces_ret) *contains_spaces_ret = contains_spaces;
926 return TRUE;
929 /*************************************************************************
930 * SetSearchPathMode (KERNEL32.@)
932 BOOL WINAPI SetSearchPathMode( DWORD flags )
934 return set_ntstatus( RtlSetSearchPathMode( flags ));