xinput1_3: Move HID data into struct xinput_controller.
[wine.git] / dlls / kernelbase / file.c
blobe0a75c2ad08a96f91951202ef759987d0712f136
1 /*
2 * File handling functions
4 * Copyright 1993 John Burton
5 * Copyright 1996, 2004 Alexandre Julliard
6 * Copyright 2008 Jeff Zaroyko
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
23 #include <stdarg.h>
24 #include <stdio.h>
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28 #include "winerror.h"
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winnls.h"
34 #include "winternl.h"
35 #include "winioctl.h"
36 #include "wincon.h"
37 #include "fileapi.h"
38 #include "shlwapi.h"
39 #include "ddk/ntddk.h"
40 #include "ddk/ntddser.h"
42 #include "kernelbase.h"
43 #include "wine/exception.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(file);
48 /* info structure for FindFirstFile handle */
49 typedef struct
51 DWORD magic; /* magic number */
52 HANDLE handle; /* handle to directory */
53 CRITICAL_SECTION cs; /* crit section protecting this structure */
54 FINDEX_SEARCH_OPS search_op; /* Flags passed to FindFirst. */
55 FINDEX_INFO_LEVELS level; /* Level passed to FindFirst */
56 UNICODE_STRING path; /* NT path used to open the directory */
57 BOOL is_root; /* is directory the root of the drive? */
58 BOOL wildcard; /* did the mask contain wildcard characters? */
59 UINT data_pos; /* current position in dir data */
60 UINT data_len; /* length of dir data */
61 UINT data_size; /* size of data buffer, or 0 when everything has been read */
62 BYTE data[1]; /* directory data */
63 } FIND_FIRST_INFO;
65 #define FIND_FIRST_MAGIC 0xc0ffee11
67 static const UINT max_entry_size = offsetof( FILE_BOTH_DIRECTORY_INFORMATION, FileName[256] );
69 const WCHAR windows_dir[] = L"C:\\windows";
70 const WCHAR system_dir[] = L"C:\\windows\\system32";
72 static BOOL oem_file_apis;
75 static void WINAPI read_write_apc( void *apc_user, PIO_STATUS_BLOCK io, ULONG reserved )
77 LPOVERLAPPED_COMPLETION_ROUTINE func = apc_user;
78 func( RtlNtStatusToDosError( io->u.Status ), io->Information, (LPOVERLAPPED)io );
81 static const WCHAR *get_machine_wow64_dir( WORD machine )
83 switch (machine)
85 case IMAGE_FILE_MACHINE_TARGET_HOST: return system_dir;
86 case IMAGE_FILE_MACHINE_I386: return L"C:\\windows\\syswow64";
87 case IMAGE_FILE_MACHINE_ARMNT: return L"C:\\windows\\sysarm32";
88 case IMAGE_FILE_MACHINE_AMD64: return L"C:\\windows\\sysx8664";
89 case IMAGE_FILE_MACHINE_ARM64: return L"C:\\windows\\sysarm64";
90 default: return NULL;
95 /***********************************************************************
96 * Operations on file names
97 ***********************************************************************/
100 /***********************************************************************
101 * contains_path
103 * Check if the file name contains a path; helper for SearchPathW.
104 * A relative path is not considered a path unless it starts with ./ or ../
106 static inline BOOL contains_path( const WCHAR *name )
108 if (RtlDetermineDosPathNameType_U( name ) != RELATIVE_PATH) return TRUE;
109 if (name[0] != '.') return FALSE;
110 if (name[1] == '/' || name[1] == '\\') return TRUE;
111 return (name[1] == '.' && (name[2] == '/' || name[2] == '\\'));
115 /***********************************************************************
116 * add_boot_rename_entry
118 * Adds an entry to the registry that is loaded when windows boots and
119 * checks if there are some files to be removed or renamed/moved.
120 * <fn1> has to be valid and <fn2> may be NULL. If both pointers are
121 * non-NULL then the file is moved, otherwise it is deleted. The
122 * entry of the registry key is always appended with two zero
123 * terminated strings. If <fn2> is NULL then the second entry is
124 * simply a single 0-byte. Otherwise the second filename goes
125 * there. The entries are prepended with \??\ before the path and the
126 * second filename gets also a '!' as the first character if
127 * MOVEFILE_REPLACE_EXISTING is set. After the final string another
128 * 0-byte follows to indicate the end of the strings.
129 * i.e.:
130 * \??\D:\test\file1[0]
131 * !\??\D:\test\file1_renamed[0]
132 * \??\D:\Test|delete[0]
133 * [0] <- file is to be deleted, second string empty
134 * \??\D:\test\file2[0]
135 * !\??\D:\test\file2_renamed[0]
136 * [0] <- indicates end of strings
138 * or:
139 * \??\D:\test\file1[0]
140 * !\??\D:\test\file1_renamed[0]
141 * \??\D:\Test|delete[0]
142 * [0] <- file is to be deleted, second string empty
143 * [0] <- indicates end of strings
146 static BOOL add_boot_rename_entry( LPCWSTR source, LPCWSTR dest, DWORD flags )
148 static const int info_size = FIELD_OFFSET( KEY_VALUE_PARTIAL_INFORMATION, Data );
150 OBJECT_ATTRIBUTES attr;
151 UNICODE_STRING nameW, source_name, dest_name;
152 KEY_VALUE_PARTIAL_INFORMATION *info;
153 BOOL rc = FALSE;
154 HANDLE key = 0;
155 DWORD len1, len2;
156 DWORD size = 0;
157 BYTE *buffer = NULL;
158 WCHAR *p;
160 if (!RtlDosPathNameToNtPathName_U( source, &source_name, NULL, NULL ))
162 SetLastError( ERROR_PATH_NOT_FOUND );
163 return FALSE;
165 dest_name.Buffer = NULL;
166 if (dest && !RtlDosPathNameToNtPathName_U( dest, &dest_name, NULL, NULL ))
168 RtlFreeUnicodeString( &source_name );
169 SetLastError( ERROR_PATH_NOT_FOUND );
170 return FALSE;
173 attr.Length = sizeof(attr);
174 attr.RootDirectory = 0;
175 attr.ObjectName = &nameW;
176 attr.Attributes = 0;
177 attr.SecurityDescriptor = NULL;
178 attr.SecurityQualityOfService = NULL;
179 RtlInitUnicodeString( &nameW, L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\Session Manager" );
181 if (NtCreateKey( &key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS)
183 RtlFreeUnicodeString( &source_name );
184 RtlFreeUnicodeString( &dest_name );
185 return FALSE;
188 len1 = source_name.Length + sizeof(WCHAR);
189 if (dest)
191 len2 = dest_name.Length + sizeof(WCHAR);
192 if (flags & MOVEFILE_REPLACE_EXISTING)
193 len2 += sizeof(WCHAR); /* Plus 1 because of the leading '!' */
195 else len2 = sizeof(WCHAR); /* minimum is the 0 characters for the empty second string */
197 RtlInitUnicodeString( &nameW, L"PendingFileRenameOperations" );
199 /* First we check if the key exists and if so how many bytes it already contains. */
200 if (NtQueryValueKey( key, &nameW, KeyValuePartialInformation,
201 NULL, 0, &size ) == STATUS_BUFFER_TOO_SMALL)
203 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size + len1 + len2 + sizeof(WCHAR) ))) goto done;
204 if (NtQueryValueKey( key, &nameW, KeyValuePartialInformation, buffer, size, &size )) goto done;
205 info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
206 if (info->Type != REG_MULTI_SZ) goto done;
207 if (size > sizeof(info)) size -= sizeof(WCHAR); /* remove terminating null (will be added back later) */
209 else
211 size = info_size;
212 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size + len1 + len2 + sizeof(WCHAR) ))) goto done;
215 memcpy( buffer + size, source_name.Buffer, len1 );
216 size += len1;
217 p = (WCHAR *)(buffer + size);
218 if (dest)
220 if (flags & MOVEFILE_REPLACE_EXISTING) *p++ = '!';
221 memcpy( p, dest_name.Buffer, len2 );
222 size += len2;
224 else
226 *p = 0;
227 size += sizeof(WCHAR);
230 /* add final null */
231 p = (WCHAR *)(buffer + size);
232 *p = 0;
233 size += sizeof(WCHAR);
234 rc = !NtSetValueKey( key, &nameW, 0, REG_MULTI_SZ, buffer + info_size, size - info_size );
236 done:
237 RtlFreeUnicodeString( &source_name );
238 RtlFreeUnicodeString( &dest_name );
239 if (key) NtClose(key);
240 HeapFree( GetProcessHeap(), 0, buffer );
241 return rc;
245 /***********************************************************************
246 * append_ext
248 static WCHAR *append_ext( const WCHAR *name, const WCHAR *ext )
250 const WCHAR *p;
251 WCHAR *ret;
252 DWORD len;
254 if (!ext) return NULL;
255 p = wcsrchr( name, '.' );
256 if (p && !wcschr( p, '/' ) && !wcschr( p, '\\' )) return NULL;
258 len = lstrlenW( name ) + lstrlenW( ext );
259 if ((ret = RtlAllocateHeap( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
261 lstrcpyW( ret, name );
262 lstrcatW( ret, ext );
264 return ret;
268 /***********************************************************************
269 * find_actctx_dllpath
271 * Find the path (if any) of the dll from the activation context.
272 * Returned path doesn't include a name.
274 static NTSTATUS find_actctx_dllpath( const WCHAR *name, WCHAR **path )
276 ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION *info;
277 ACTCTX_SECTION_KEYED_DATA data;
278 UNICODE_STRING nameW;
279 NTSTATUS status;
280 SIZE_T needed, size = 1024;
281 WCHAR *p;
283 RtlInitUnicodeString( &nameW, name );
284 data.cbSize = sizeof(data);
285 status = RtlFindActivationContextSectionString( FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX, NULL,
286 ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION,
287 &nameW, &data );
288 if (status != STATUS_SUCCESS) return status;
290 for (;;)
292 if (!(info = RtlAllocateHeap( GetProcessHeap(), 0, size )))
294 status = STATUS_NO_MEMORY;
295 goto done;
297 status = RtlQueryInformationActivationContext( 0, data.hActCtx, &data.ulAssemblyRosterIndex,
298 AssemblyDetailedInformationInActivationContext,
299 info, size, &needed );
300 if (status == STATUS_SUCCESS) break;
301 if (status != STATUS_BUFFER_TOO_SMALL) goto done;
302 RtlFreeHeap( GetProcessHeap(), 0, info );
303 size = needed;
304 /* restart with larger buffer */
307 if (!info->lpAssemblyManifestPath)
309 status = STATUS_SXS_KEY_NOT_FOUND;
310 goto done;
313 if ((p = wcsrchr( info->lpAssemblyManifestPath, '\\' )))
315 DWORD dirlen = info->ulAssemblyDirectoryNameLength / sizeof(WCHAR);
317 p++;
318 if (!dirlen ||
319 CompareStringOrdinal( p, dirlen, info->lpAssemblyDirectoryName, dirlen, TRUE ) != CSTR_EQUAL ||
320 wcsicmp( p + dirlen, L".manifest" ))
322 /* manifest name does not match directory name, so it's not a global
323 * windows/winsxs manifest; use the manifest directory name instead */
324 dirlen = p - info->lpAssemblyManifestPath;
325 needed = (dirlen + 1) * sizeof(WCHAR);
326 if (!(*path = p = HeapAlloc( GetProcessHeap(), 0, needed )))
328 status = STATUS_NO_MEMORY;
329 goto done;
331 memcpy( p, info->lpAssemblyManifestPath, dirlen * sizeof(WCHAR) );
332 *(p + dirlen) = 0;
333 goto done;
337 if (!info->lpAssemblyDirectoryName)
339 status = STATUS_SXS_KEY_NOT_FOUND;
340 goto done;
343 needed = sizeof(L"C:\\windows\\winsxs\\") + info->ulAssemblyDirectoryNameLength + sizeof(WCHAR);
345 if (!(*path = p = RtlAllocateHeap( GetProcessHeap(), 0, needed )))
347 status = STATUS_NO_MEMORY;
348 goto done;
350 lstrcpyW( p, L"C:\\windows\\winsxs\\" );
351 p += lstrlenW(p);
352 memcpy( p, info->lpAssemblyDirectoryName, info->ulAssemblyDirectoryNameLength );
353 p += info->ulAssemblyDirectoryNameLength / sizeof(WCHAR);
354 *p++ = '\\';
355 *p = 0;
356 done:
357 RtlFreeHeap( GetProcessHeap(), 0, info );
358 RtlReleaseActivationContext( data.hActCtx );
359 return status;
363 /***********************************************************************
364 * copy_filename
366 static DWORD copy_filename( const WCHAR *name, WCHAR *buffer, DWORD len )
368 UINT ret = lstrlenW( name ) + 1;
369 if (buffer && len >= ret)
371 lstrcpyW( buffer, name );
372 ret--;
374 return ret;
378 /***********************************************************************
379 * copy_filename_WtoA
381 * copy a file name back to OEM/Ansi, but only if the buffer is large enough
383 static DWORD copy_filename_WtoA( LPCWSTR nameW, LPSTR buffer, DWORD len )
385 UNICODE_STRING strW;
386 DWORD ret;
388 RtlInitUnicodeString( &strW, nameW );
390 ret = oem_file_apis ? RtlUnicodeStringToOemSize( &strW ) : RtlUnicodeStringToAnsiSize( &strW );
391 if (buffer && ret <= len)
393 ANSI_STRING str;
395 str.Buffer = buffer;
396 str.MaximumLength = min( len, UNICODE_STRING_MAX_CHARS );
397 if (oem_file_apis)
398 RtlUnicodeStringToOemString( &str, &strW, FALSE );
399 else
400 RtlUnicodeStringToAnsiString( &str, &strW, FALSE );
401 ret = str.Length; /* length without terminating 0 */
403 return ret;
407 /***********************************************************************
408 * file_name_AtoW
410 * Convert a file name to Unicode, taking into account the OEM/Ansi API mode.
412 * If alloc is FALSE uses the TEB static buffer, so it can only be used when
413 * there is no possibility for the function to do that twice, taking into
414 * account any called function.
416 WCHAR *file_name_AtoW( LPCSTR name, BOOL alloc )
418 ANSI_STRING str;
419 UNICODE_STRING strW, *pstrW;
420 NTSTATUS status;
422 RtlInitAnsiString( &str, name );
423 pstrW = alloc ? &strW : &NtCurrentTeb()->StaticUnicodeString;
424 if (oem_file_apis)
425 status = RtlOemStringToUnicodeString( pstrW, &str, alloc );
426 else
427 status = RtlAnsiStringToUnicodeString( pstrW, &str, alloc );
428 if (status == STATUS_SUCCESS) return pstrW->Buffer;
430 if (status == STATUS_BUFFER_OVERFLOW)
431 SetLastError( ERROR_FILENAME_EXCED_RANGE );
432 else
433 SetLastError( RtlNtStatusToDosError(status) );
434 return NULL;
438 /***********************************************************************
439 * file_name_WtoA
441 * Convert a file name back to OEM/Ansi. Returns number of bytes copied.
443 DWORD file_name_WtoA( LPCWSTR src, INT srclen, LPSTR dest, INT destlen )
445 DWORD ret;
447 if (srclen < 0) srclen = lstrlenW( src ) + 1;
448 if (!destlen)
450 if (oem_file_apis)
452 UNICODE_STRING strW;
453 strW.Buffer = (WCHAR *)src;
454 strW.Length = srclen * sizeof(WCHAR);
455 ret = RtlUnicodeStringToOemSize( &strW ) - 1;
457 else
458 RtlUnicodeToMultiByteSize( &ret, src, srclen * sizeof(WCHAR) );
460 else
462 if (oem_file_apis)
463 RtlUnicodeToOemN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
464 else
465 RtlUnicodeToMultiByteN( dest, destlen, &ret, src, srclen * sizeof(WCHAR) );
467 return ret;
471 /***********************************************************************
472 * is_same_file
474 static BOOL is_same_file( HANDLE h1, HANDLE h2 )
476 FILE_OBJECTID_BUFFER id1, id2;
477 IO_STATUS_BLOCK io;
479 return (!NtFsControlFile( h1, 0, NULL, NULL, &io, FSCTL_GET_OBJECT_ID, NULL, 0, &id1, sizeof(id1) ) &&
480 !NtFsControlFile( h2, 0, NULL, NULL, &io, FSCTL_GET_OBJECT_ID, NULL, 0, &id2, sizeof(id2) ) &&
481 !memcmp( &id1.ObjectId, &id2.ObjectId, sizeof(id1.ObjectId) ));
485 /******************************************************************************
486 * AreFileApisANSI (kernelbase.@)
488 BOOL WINAPI DECLSPEC_HOTPATCH AreFileApisANSI(void)
490 return !oem_file_apis;
494 /***********************************************************************
495 * CopyFileExW (kernelbase.@)
497 BOOL WINAPI CopyFileExW( const WCHAR *source, const WCHAR *dest, LPPROGRESS_ROUTINE progress,
498 void *param, BOOL *cancel_ptr, DWORD flags )
500 static const int buffer_size = 65536;
501 HANDLE h1, h2;
502 FILE_BASIC_INFORMATION info;
503 IO_STATUS_BLOCK io;
504 DWORD count;
505 BOOL ret = FALSE;
506 char *buffer;
508 if (!source || !dest)
510 SetLastError( ERROR_INVALID_PARAMETER );
511 return FALSE;
513 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
515 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
516 return FALSE;
519 TRACE("%s -> %s, %x\n", debugstr_w(source), debugstr_w(dest), flags);
521 if ((h1 = CreateFileW( source, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
522 NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
524 WARN("Unable to open source %s\n", debugstr_w(source));
525 HeapFree( GetProcessHeap(), 0, buffer );
526 return FALSE;
529 if (!set_ntstatus( NtQueryInformationFile( h1, &io, &info, sizeof(info), FileBasicInformation )))
531 WARN("GetFileInformationByHandle returned error for %s\n", debugstr_w(source));
532 HeapFree( GetProcessHeap(), 0, buffer );
533 CloseHandle( h1 );
534 return FALSE;
537 if (!(flags & COPY_FILE_FAIL_IF_EXISTS))
539 BOOL same_file = FALSE;
540 h2 = CreateFileW( dest, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
541 if (h2 != INVALID_HANDLE_VALUE)
543 same_file = is_same_file( h1, h2 );
544 CloseHandle( h2 );
546 if (same_file)
548 HeapFree( GetProcessHeap(), 0, buffer );
549 CloseHandle( h1 );
550 SetLastError( ERROR_SHARING_VIOLATION );
551 return FALSE;
555 if ((h2 = CreateFileW( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
556 (flags & COPY_FILE_FAIL_IF_EXISTS) ? CREATE_NEW : CREATE_ALWAYS,
557 info.FileAttributes, h1 )) == INVALID_HANDLE_VALUE)
559 WARN("Unable to open dest %s\n", debugstr_w(dest));
560 HeapFree( GetProcessHeap(), 0, buffer );
561 CloseHandle( h1 );
562 return FALSE;
565 while (ReadFile( h1, buffer, buffer_size, &count, NULL ) && count)
567 char *p = buffer;
568 while (count != 0)
570 DWORD res;
571 if (!WriteFile( h2, p, count, &res, NULL ) || !res) goto done;
572 p += res;
573 count -= res;
576 ret = TRUE;
577 done:
578 /* Maintain the timestamp of source file to destination file */
579 info.FileAttributes = 0;
580 NtSetInformationFile( h2, &io, &info, sizeof(info), FileBasicInformation );
581 HeapFree( GetProcessHeap(), 0, buffer );
582 CloseHandle( h1 );
583 CloseHandle( h2 );
584 return ret;
588 /**************************************************************************
589 * CopyFileW (kernelbase.@)
591 BOOL WINAPI DECLSPEC_HOTPATCH CopyFileW( const WCHAR *source, const WCHAR *dest, BOOL fail_if_exists )
593 return CopyFileExW( source, dest, NULL, NULL, NULL, fail_if_exists ? COPY_FILE_FAIL_IF_EXISTS : 0 );
597 /***********************************************************************
598 * CreateDirectoryA (kernelbase.@)
600 BOOL WINAPI DECLSPEC_HOTPATCH CreateDirectoryA( LPCSTR path, LPSECURITY_ATTRIBUTES sa )
602 WCHAR *pathW;
604 if (!(pathW = file_name_AtoW( path, FALSE ))) return FALSE;
605 return CreateDirectoryW( pathW, sa );
609 /***********************************************************************
610 * CreateDirectoryW (kernelbase.@)
612 BOOL WINAPI DECLSPEC_HOTPATCH CreateDirectoryW( LPCWSTR path, LPSECURITY_ATTRIBUTES sa )
614 OBJECT_ATTRIBUTES attr;
615 UNICODE_STRING nt_name;
616 IO_STATUS_BLOCK io;
617 NTSTATUS status;
618 HANDLE handle;
620 TRACE( "%s\n", debugstr_w(path) );
622 if (!RtlDosPathNameToNtPathName_U( path, &nt_name, NULL, NULL ))
624 SetLastError( ERROR_PATH_NOT_FOUND );
625 return FALSE;
627 attr.Length = sizeof(attr);
628 attr.RootDirectory = 0;
629 attr.Attributes = OBJ_CASE_INSENSITIVE;
630 attr.ObjectName = &nt_name;
631 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
632 attr.SecurityQualityOfService = NULL;
634 status = NtCreateFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr, &io, NULL,
635 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_CREATE,
636 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
637 if (status == STATUS_SUCCESS) NtClose( handle );
639 RtlFreeUnicodeString( &nt_name );
640 return set_ntstatus( status );
644 /***********************************************************************
645 * CreateDirectoryEx (kernelbase.@)
647 BOOL WINAPI DECLSPEC_HOTPATCH CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
648 LPSECURITY_ATTRIBUTES sa )
650 return CreateDirectoryW( path, sa );
654 /*************************************************************************
655 * CreateFile2 (kernelbase.@)
657 HANDLE WINAPI DECLSPEC_HOTPATCH CreateFile2( LPCWSTR name, DWORD access, DWORD sharing, DWORD creation,
658 CREATEFILE2_EXTENDED_PARAMETERS *params )
660 static const DWORD attributes_mask = FILE_ATTRIBUTE_READONLY |
661 FILE_ATTRIBUTE_HIDDEN |
662 FILE_ATTRIBUTE_SYSTEM |
663 FILE_ATTRIBUTE_ARCHIVE |
664 FILE_ATTRIBUTE_NORMAL |
665 FILE_ATTRIBUTE_TEMPORARY |
666 FILE_ATTRIBUTE_OFFLINE |
667 FILE_ATTRIBUTE_ENCRYPTED |
668 FILE_ATTRIBUTE_INTEGRITY_STREAM;
669 static const DWORD flags_mask = FILE_FLAG_BACKUP_SEMANTICS |
670 FILE_FLAG_DELETE_ON_CLOSE |
671 FILE_FLAG_NO_BUFFERING |
672 FILE_FLAG_OPEN_NO_RECALL |
673 FILE_FLAG_OPEN_REPARSE_POINT |
674 FILE_FLAG_OVERLAPPED |
675 FILE_FLAG_POSIX_SEMANTICS |
676 FILE_FLAG_RANDOM_ACCESS |
677 FILE_FLAG_SEQUENTIAL_SCAN |
678 FILE_FLAG_WRITE_THROUGH;
680 LPSECURITY_ATTRIBUTES sa = params ? params->lpSecurityAttributes : NULL;
681 HANDLE template = params ? params->hTemplateFile : NULL;
682 DWORD attributes = params ? params->dwFileAttributes : 0;
683 DWORD flags = params ? params->dwFileFlags : 0;
685 FIXME( "(%s %x %x %x %p), partial stub\n", debugstr_w(name), access, sharing, creation, params );
687 if (attributes & ~attributes_mask) FIXME( "unsupported attributes %#x\n", attributes );
688 if (flags & ~flags_mask) FIXME( "unsupported flags %#x\n", flags );
689 attributes &= attributes_mask;
690 flags &= flags_mask;
692 return CreateFileW( name, access, sharing, sa, creation, flags | attributes, template );
696 /*************************************************************************
697 * CreateFileA (kernelbase.@)
699 HANDLE WINAPI DECLSPEC_HOTPATCH CreateFileA( LPCSTR name, DWORD access, DWORD sharing,
700 LPSECURITY_ATTRIBUTES sa, DWORD creation,
701 DWORD attributes, HANDLE template)
703 WCHAR *nameW;
705 if ((GetVersion() & 0x80000000) && IsBadStringPtrA( name, -1 )) return INVALID_HANDLE_VALUE;
706 if (!(nameW = file_name_AtoW( name, FALSE ))) return INVALID_HANDLE_VALUE;
707 return CreateFileW( nameW, access, sharing, sa, creation, attributes, template );
710 static UINT get_nt_file_options( DWORD attributes )
712 UINT options = 0;
714 if (attributes & FILE_FLAG_BACKUP_SEMANTICS)
715 options |= FILE_OPEN_FOR_BACKUP_INTENT;
716 else
717 options |= FILE_NON_DIRECTORY_FILE;
718 if (attributes & FILE_FLAG_DELETE_ON_CLOSE)
719 options |= FILE_DELETE_ON_CLOSE;
720 if (attributes & FILE_FLAG_NO_BUFFERING)
721 options |= FILE_NO_INTERMEDIATE_BUFFERING;
722 if (!(attributes & FILE_FLAG_OVERLAPPED))
723 options |= FILE_SYNCHRONOUS_IO_NONALERT;
724 if (attributes & FILE_FLAG_RANDOM_ACCESS)
725 options |= FILE_RANDOM_ACCESS;
726 if (attributes & FILE_FLAG_WRITE_THROUGH)
727 options |= FILE_WRITE_THROUGH;
728 return options;
731 /*************************************************************************
732 * CreateFileW (kernelbase.@)
734 HANDLE WINAPI DECLSPEC_HOTPATCH CreateFileW( LPCWSTR filename, DWORD access, DWORD sharing,
735 LPSECURITY_ATTRIBUTES sa, DWORD creation,
736 DWORD attributes, HANDLE template )
738 NTSTATUS status;
739 OBJECT_ATTRIBUTES attr;
740 UNICODE_STRING nameW;
741 IO_STATUS_BLOCK io;
742 HANDLE ret;
743 const WCHAR *vxd_name = NULL;
744 SECURITY_QUALITY_OF_SERVICE qos;
746 static const UINT nt_disposition[5] =
748 FILE_CREATE, /* CREATE_NEW */
749 FILE_OVERWRITE_IF, /* CREATE_ALWAYS */
750 FILE_OPEN, /* OPEN_EXISTING */
751 FILE_OPEN_IF, /* OPEN_ALWAYS */
752 FILE_OVERWRITE /* TRUNCATE_EXISTING */
756 /* sanity checks */
758 if (!filename || !filename[0])
760 SetLastError( ERROR_PATH_NOT_FOUND );
761 return INVALID_HANDLE_VALUE;
764 TRACE( "%s %s%s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename),
765 (access & GENERIC_READ) ? "GENERIC_READ " : "",
766 (access & GENERIC_WRITE) ? "GENERIC_WRITE " : "",
767 (access & GENERIC_EXECUTE) ? "GENERIC_EXECUTE " : "",
768 !access ? "QUERY_ACCESS " : "",
769 (sharing & FILE_SHARE_READ) ? "FILE_SHARE_READ " : "",
770 (sharing & FILE_SHARE_WRITE) ? "FILE_SHARE_WRITE " : "",
771 (sharing & FILE_SHARE_DELETE) ? "FILE_SHARE_DELETE " : "",
772 creation, attributes);
774 if ((GetVersion() & 0x80000000) && !wcsncmp( filename, L"\\\\.\\", 4 ) &&
775 !RtlIsDosDeviceName_U( filename + 4 ) &&
776 wcsnicmp( filename + 4, L"PIPE\\", 5 ) &&
777 wcsnicmp( filename + 4, L"MAILSLOT\\", 9 ))
779 vxd_name = filename + 4;
780 if (!creation) creation = OPEN_EXISTING;
783 if (creation < CREATE_NEW || creation > TRUNCATE_EXISTING)
785 SetLastError( ERROR_INVALID_PARAMETER );
786 return INVALID_HANDLE_VALUE;
789 if (!RtlDosPathNameToNtPathName_U( filename, &nameW, NULL, NULL ))
791 SetLastError( ERROR_PATH_NOT_FOUND );
792 return INVALID_HANDLE_VALUE;
795 /* now call NtCreateFile */
797 if (attributes & FILE_FLAG_DELETE_ON_CLOSE)
798 access |= DELETE;
800 attr.Length = sizeof(attr);
801 attr.RootDirectory = 0;
802 attr.Attributes = OBJ_CASE_INSENSITIVE;
803 attr.ObjectName = &nameW;
804 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
805 if (attributes & SECURITY_SQOS_PRESENT)
807 qos.Length = sizeof(qos);
808 qos.ImpersonationLevel = (attributes >> 16) & 0x3;
809 qos.ContextTrackingMode = attributes & SECURITY_CONTEXT_TRACKING ? SECURITY_DYNAMIC_TRACKING : SECURITY_STATIC_TRACKING;
810 qos.EffectiveOnly = (attributes & SECURITY_EFFECTIVE_ONLY) != 0;
811 attr.SecurityQualityOfService = &qos;
813 else
814 attr.SecurityQualityOfService = NULL;
816 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
818 status = NtCreateFile( &ret, access | SYNCHRONIZE | FILE_READ_ATTRIBUTES, &attr, &io,
819 NULL, attributes & FILE_ATTRIBUTE_VALID_FLAGS, sharing,
820 nt_disposition[creation - CREATE_NEW],
821 get_nt_file_options( attributes ), NULL, 0 );
822 if (status)
824 if (vxd_name && vxd_name[0])
826 static HANDLE (*vxd_open)(LPCWSTR,DWORD,SECURITY_ATTRIBUTES*);
827 if (!vxd_open) vxd_open = (void *)GetProcAddress( GetModuleHandleW(L"krnl386.exe16"),
828 "__wine_vxd_open" );
829 if (vxd_open && (ret = vxd_open( vxd_name, access, sa ))) goto done;
832 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename), status);
833 ret = INVALID_HANDLE_VALUE;
835 /* In the case file creation was rejected due to CREATE_NEW flag
836 * was specified and file with that name already exists, correct
837 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
838 * Note: RtlNtStatusToDosError is not the subject to blame here.
840 if (status == STATUS_OBJECT_NAME_COLLISION)
841 SetLastError( ERROR_FILE_EXISTS );
842 else
843 SetLastError( RtlNtStatusToDosError(status) );
845 else
847 if ((creation == CREATE_ALWAYS && io.Information == FILE_OVERWRITTEN) ||
848 (creation == OPEN_ALWAYS && io.Information == FILE_OPENED))
849 SetLastError( ERROR_ALREADY_EXISTS );
850 else
851 SetLastError( 0 );
853 RtlFreeUnicodeString( &nameW );
855 done:
856 if (!ret) ret = INVALID_HANDLE_VALUE;
857 TRACE("returning %p\n", ret);
858 return ret;
862 /*************************************************************************
863 * CreateHardLinkA (kernelbase.@)
865 BOOL WINAPI DECLSPEC_HOTPATCH CreateHardLinkA( const char *dest, const char *source,
866 SECURITY_ATTRIBUTES *attr )
868 WCHAR *sourceW, *destW;
869 BOOL res;
871 if (!(sourceW = file_name_AtoW( source, TRUE ))) return FALSE;
872 if (!(destW = file_name_AtoW( dest, TRUE )))
874 HeapFree( GetProcessHeap(), 0, sourceW );
875 return FALSE;
877 res = CreateHardLinkW( destW, sourceW, attr );
878 HeapFree( GetProcessHeap(), 0, sourceW );
879 HeapFree( GetProcessHeap(), 0, destW );
880 return res;
884 /*************************************************************************
885 * CreateHardLinkW (kernelbase.@)
887 BOOL WINAPI CreateHardLinkW( LPCWSTR dest, LPCWSTR source, SECURITY_ATTRIBUTES *sec_attr )
889 UNICODE_STRING ntDest, ntSource;
890 FILE_LINK_INFORMATION *info = NULL;
891 OBJECT_ATTRIBUTES attr;
892 IO_STATUS_BLOCK io;
893 BOOL ret = FALSE;
894 HANDLE file;
895 ULONG size;
897 TRACE( "(%s, %s, %p)\n", debugstr_w(dest), debugstr_w(source), sec_attr );
899 ntDest.Buffer = ntSource.Buffer = NULL;
900 if (!RtlDosPathNameToNtPathName_U( dest, &ntDest, NULL, NULL ) ||
901 !RtlDosPathNameToNtPathName_U( source, &ntSource, NULL, NULL ))
903 SetLastError( ERROR_PATH_NOT_FOUND );
904 goto done;
907 size = offsetof( FILE_LINK_INFORMATION, FileName ) + ntDest.Length;
908 if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
910 SetLastError( ERROR_OUTOFMEMORY );
911 goto done;
914 InitializeObjectAttributes( &attr, &ntSource, OBJ_CASE_INSENSITIVE, 0, NULL );
915 if (!(ret = set_ntstatus( NtOpenFile( &file, SYNCHRONIZE, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
916 FILE_SYNCHRONOUS_IO_NONALERT ) )))
917 goto done;
919 info->ReplaceIfExists = FALSE;
920 info->RootDirectory = NULL;
921 info->FileNameLength = ntDest.Length;
922 memcpy( info->FileName, ntDest.Buffer, ntDest.Length );
923 ret = set_ntstatus( NtSetInformationFile( file, &io, info, size, FileLinkInformation ) );
924 NtClose( file );
926 done:
927 RtlFreeUnicodeString( &ntSource );
928 RtlFreeUnicodeString( &ntDest );
929 HeapFree( GetProcessHeap(), 0, info );
930 return ret;
934 /*************************************************************************
935 * CreateSymbolicLinkW (kernelbase.@)
937 BOOLEAN WINAPI /* DECLSPEC_HOTPATCH */ CreateSymbolicLinkW( LPCWSTR link, LPCWSTR target, DWORD flags )
939 FIXME( "(%s %s %d): stub\n", debugstr_w(link), debugstr_w(target), flags );
940 return TRUE;
944 /***********************************************************************
945 * DeleteFileA (kernelbase.@)
947 BOOL WINAPI DECLSPEC_HOTPATCH DeleteFileA( LPCSTR path )
949 WCHAR *pathW;
951 if (!(pathW = file_name_AtoW( path, FALSE ))) return FALSE;
952 return DeleteFileW( pathW );
956 /***********************************************************************
957 * DeleteFileW (kernelbase.@)
959 BOOL WINAPI DECLSPEC_HOTPATCH DeleteFileW( LPCWSTR path )
961 UNICODE_STRING nameW;
962 OBJECT_ATTRIBUTES attr;
963 NTSTATUS status;
964 HANDLE hFile;
965 IO_STATUS_BLOCK io;
967 TRACE( "%s\n", debugstr_w(path) );
969 if (!RtlDosPathNameToNtPathName_U( path, &nameW, NULL, NULL ))
971 SetLastError( ERROR_PATH_NOT_FOUND );
972 return FALSE;
975 attr.Length = sizeof(attr);
976 attr.RootDirectory = 0;
977 attr.Attributes = OBJ_CASE_INSENSITIVE;
978 attr.ObjectName = &nameW;
979 attr.SecurityDescriptor = NULL;
980 attr.SecurityQualityOfService = NULL;
982 status = NtCreateFile(&hFile, SYNCHRONIZE | DELETE, &attr, &io, NULL, 0,
983 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
984 FILE_OPEN, FILE_DELETE_ON_CLOSE | FILE_NON_DIRECTORY_FILE, NULL, 0);
985 if (status == STATUS_SUCCESS) status = NtClose(hFile);
987 RtlFreeUnicodeString( &nameW );
988 return set_ntstatus( status );
992 /****************************************************************************
993 * FindCloseChangeNotification (kernelbase.@)
995 BOOL WINAPI DECLSPEC_HOTPATCH FindCloseChangeNotification( HANDLE handle )
997 return CloseHandle( handle );
1001 /****************************************************************************
1002 * FindFirstChangeNotificationA (kernelbase.@)
1004 HANDLE WINAPI DECLSPEC_HOTPATCH FindFirstChangeNotificationA( LPCSTR path, BOOL subtree, DWORD filter )
1006 WCHAR *pathW;
1008 if (!(pathW = file_name_AtoW( path, FALSE ))) return INVALID_HANDLE_VALUE;
1009 return FindFirstChangeNotificationW( pathW, subtree, filter );
1014 * NtNotifyChangeDirectoryFile may write back to the IO_STATUS_BLOCK
1015 * asynchronously. We don't care about the contents, but it can't
1016 * be placed on the stack since it will go out of scope when we return.
1018 static IO_STATUS_BLOCK dummy_iosb;
1020 /****************************************************************************
1021 * FindFirstChangeNotificationW (kernelbase.@)
1023 HANDLE WINAPI DECLSPEC_HOTPATCH FindFirstChangeNotificationW( LPCWSTR path, BOOL subtree, DWORD filter )
1025 UNICODE_STRING nt_name;
1026 OBJECT_ATTRIBUTES attr;
1027 NTSTATUS status;
1028 HANDLE handle = INVALID_HANDLE_VALUE;
1030 TRACE( "%s %d %x\n", debugstr_w(path), subtree, filter );
1032 if (!RtlDosPathNameToNtPathName_U( path, &nt_name, NULL, NULL ))
1034 SetLastError( ERROR_PATH_NOT_FOUND );
1035 return handle;
1038 attr.Length = sizeof(attr);
1039 attr.RootDirectory = 0;
1040 attr.Attributes = OBJ_CASE_INSENSITIVE;
1041 attr.ObjectName = &nt_name;
1042 attr.SecurityDescriptor = NULL;
1043 attr.SecurityQualityOfService = NULL;
1045 status = NtOpenFile( &handle, FILE_LIST_DIRECTORY | SYNCHRONIZE, &attr, &dummy_iosb,
1046 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1047 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1048 RtlFreeUnicodeString( &nt_name );
1050 if (!set_ntstatus( status )) return INVALID_HANDLE_VALUE;
1052 status = NtNotifyChangeDirectoryFile( handle, NULL, NULL, NULL, &dummy_iosb, NULL, 0, filter, subtree );
1053 if (status != STATUS_PENDING)
1055 NtClose( handle );
1056 SetLastError( RtlNtStatusToDosError(status) );
1057 return INVALID_HANDLE_VALUE;
1059 return handle;
1063 /****************************************************************************
1064 * FindNextChangeNotification (kernelbase.@)
1066 BOOL WINAPI DECLSPEC_HOTPATCH FindNextChangeNotification( HANDLE handle )
1068 NTSTATUS status = NtNotifyChangeDirectoryFile( handle, NULL, NULL, NULL, &dummy_iosb,
1069 NULL, 0, FILE_NOTIFY_CHANGE_SIZE, 0 );
1070 if (status == STATUS_PENDING) return TRUE;
1071 return set_ntstatus( status );
1075 /******************************************************************************
1076 * FindFirstFileExA (kernelbase.@)
1078 HANDLE WINAPI DECLSPEC_HOTPATCH FindFirstFileExA( const char *filename, FINDEX_INFO_LEVELS level,
1079 void *data, FINDEX_SEARCH_OPS search_op,
1080 void *filter, DWORD flags )
1082 HANDLE handle;
1083 WIN32_FIND_DATAA *dataA = data;
1084 WIN32_FIND_DATAW dataW;
1085 WCHAR *nameW;
1087 if (!(nameW = file_name_AtoW( filename, FALSE ))) return INVALID_HANDLE_VALUE;
1089 handle = FindFirstFileExW( nameW, level, &dataW, search_op, filter, flags );
1090 if (handle == INVALID_HANDLE_VALUE) return handle;
1092 dataA->dwFileAttributes = dataW.dwFileAttributes;
1093 dataA->ftCreationTime = dataW.ftCreationTime;
1094 dataA->ftLastAccessTime = dataW.ftLastAccessTime;
1095 dataA->ftLastWriteTime = dataW.ftLastWriteTime;
1096 dataA->nFileSizeHigh = dataW.nFileSizeHigh;
1097 dataA->nFileSizeLow = dataW.nFileSizeLow;
1098 file_name_WtoA( dataW.cFileName, -1, dataA->cFileName, sizeof(dataA->cFileName) );
1099 file_name_WtoA( dataW.cAlternateFileName, -1, dataA->cAlternateFileName,
1100 sizeof(dataA->cAlternateFileName) );
1101 return handle;
1105 /******************************************************************************
1106 * FindFirstFileExW (kernelbase.@)
1108 HANDLE WINAPI DECLSPEC_HOTPATCH FindFirstFileExW( LPCWSTR filename, FINDEX_INFO_LEVELS level,
1109 LPVOID data, FINDEX_SEARCH_OPS search_op,
1110 LPVOID filter, DWORD flags )
1112 WCHAR *mask;
1113 BOOL has_wildcard = FALSE;
1114 FIND_FIRST_INFO *info = NULL;
1115 UNICODE_STRING nt_name;
1116 OBJECT_ATTRIBUTES attr;
1117 IO_STATUS_BLOCK io;
1118 NTSTATUS status;
1119 DWORD size, device = 0;
1121 TRACE( "%s %d %p %d %p %x\n", debugstr_w(filename), level, data, search_op, filter, flags );
1123 if (flags & ~FIND_FIRST_EX_LARGE_FETCH)
1125 FIXME("flags not implemented 0x%08x\n", flags );
1127 if (search_op != FindExSearchNameMatch && search_op != FindExSearchLimitToDirectories)
1129 FIXME( "search_op not implemented 0x%08x\n", search_op );
1130 SetLastError( ERROR_INVALID_PARAMETER );
1131 return INVALID_HANDLE_VALUE;
1133 if (level != FindExInfoStandard && level != FindExInfoBasic)
1135 FIXME("info level %d not implemented\n", level );
1136 SetLastError( ERROR_INVALID_PARAMETER );
1137 return INVALID_HANDLE_VALUE;
1140 if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, &mask, NULL ))
1142 SetLastError( ERROR_PATH_NOT_FOUND );
1143 return INVALID_HANDLE_VALUE;
1146 if (!mask && (device = RtlIsDosDeviceName_U( filename )))
1148 WCHAR *dir = NULL;
1150 /* we still need to check that the directory can be opened */
1152 if (HIWORD(device))
1154 if (!(dir = HeapAlloc( GetProcessHeap(), 0, HIWORD(device) + sizeof(WCHAR) )))
1156 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1157 goto error;
1159 memcpy( dir, filename, HIWORD(device) );
1160 dir[HIWORD(device)/sizeof(WCHAR)] = 0;
1162 RtlFreeUnicodeString( &nt_name );
1163 if (!RtlDosPathNameToNtPathName_U( dir ? dir : L".", &nt_name, &mask, NULL ))
1165 HeapFree( GetProcessHeap(), 0, dir );
1166 SetLastError( ERROR_PATH_NOT_FOUND );
1167 goto error;
1169 HeapFree( GetProcessHeap(), 0, dir );
1170 size = 0;
1172 else if (!mask || !*mask)
1174 SetLastError( ERROR_FILE_NOT_FOUND );
1175 goto error;
1177 else
1179 nt_name.Length = (mask - nt_name.Buffer) * sizeof(WCHAR);
1180 has_wildcard = wcspbrk( mask, L"*?" ) != NULL;
1181 size = has_wildcard ? 8192 : max_entry_size;
1184 if (!(info = HeapAlloc( GetProcessHeap(), 0, offsetof( FIND_FIRST_INFO, data[size] ))))
1186 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1187 goto error;
1190 /* check if path is the root of the drive, skipping the \??\ prefix */
1191 info->is_root = FALSE;
1192 if (nt_name.Length >= 6 * sizeof(WCHAR) && nt_name.Buffer[5] == ':')
1194 DWORD pos = 6;
1195 while (pos * sizeof(WCHAR) < nt_name.Length && nt_name.Buffer[pos] == '\\') pos++;
1196 info->is_root = (pos * sizeof(WCHAR) >= nt_name.Length);
1199 attr.Length = sizeof(attr);
1200 attr.RootDirectory = 0;
1201 attr.Attributes = OBJ_CASE_INSENSITIVE;
1202 attr.ObjectName = &nt_name;
1203 attr.SecurityDescriptor = NULL;
1204 attr.SecurityQualityOfService = NULL;
1206 status = NtOpenFile( &info->handle, GENERIC_READ | SYNCHRONIZE, &attr, &io,
1207 FILE_SHARE_READ | FILE_SHARE_WRITE,
1208 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT );
1209 if (status != STATUS_SUCCESS)
1211 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
1212 SetLastError( ERROR_PATH_NOT_FOUND );
1213 else
1214 SetLastError( RtlNtStatusToDosError(status) );
1215 goto error;
1218 RtlInitializeCriticalSection( &info->cs );
1219 info->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FIND_FIRST_INFO.cs");
1220 info->path = nt_name;
1221 info->magic = FIND_FIRST_MAGIC;
1222 info->wildcard = has_wildcard;
1223 info->data_pos = 0;
1224 info->data_len = 0;
1225 info->data_size = size;
1226 info->search_op = search_op;
1227 info->level = level;
1229 if (device)
1231 WIN32_FIND_DATAW *wfd = data;
1233 memset( wfd, 0, sizeof(*wfd) );
1234 memcpy( wfd->cFileName, filename + HIWORD(device)/sizeof(WCHAR), LOWORD(device) );
1235 wfd->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1236 CloseHandle( info->handle );
1237 info->handle = 0;
1239 else
1241 UNICODE_STRING mask_str;
1243 RtlInitUnicodeString( &mask_str, mask );
1244 status = NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, info->data_size,
1245 FileBothDirectoryInformation, FALSE, &mask_str, TRUE );
1246 if (status)
1248 FindClose( info );
1249 SetLastError( RtlNtStatusToDosError( status ) );
1250 return INVALID_HANDLE_VALUE;
1253 info->data_len = io.Information;
1254 if (!has_wildcard) info->data_size = 0; /* we read everything */
1256 if (!FindNextFileW( info, data ))
1258 TRACE( "%s not found\n", debugstr_w(filename) );
1259 FindClose( info );
1260 SetLastError( ERROR_FILE_NOT_FOUND );
1261 return INVALID_HANDLE_VALUE;
1263 if (!has_wildcard) /* we can't find two files with the same name */
1265 CloseHandle( info->handle );
1266 info->handle = 0;
1269 return info;
1271 error:
1272 HeapFree( GetProcessHeap(), 0, info );
1273 RtlFreeUnicodeString( &nt_name );
1274 return INVALID_HANDLE_VALUE;
1278 /******************************************************************************
1279 * FindFirstFileA (kernelbase.@)
1281 HANDLE WINAPI DECLSPEC_HOTPATCH FindFirstFileA( const char *filename, WIN32_FIND_DATAA *data )
1283 return FindFirstFileExA( filename, FindExInfoStandard, data, FindExSearchNameMatch, NULL, 0 );
1287 /******************************************************************************
1288 * FindFirstFileW (kernelbase.@)
1290 HANDLE WINAPI DECLSPEC_HOTPATCH FindFirstFileW( const WCHAR *filename, WIN32_FIND_DATAW *data )
1292 return FindFirstFileExW( filename, FindExInfoStandard, data, FindExSearchNameMatch, NULL, 0 );
1296 /**************************************************************************
1297 * FindFirstStreamW (kernelbase.@)
1299 HANDLE WINAPI FindFirstStreamW( const WCHAR *filename, STREAM_INFO_LEVELS level, void *data, DWORD flags )
1301 FIXME("(%s, %d, %p, %x): stub!\n", debugstr_w(filename), level, data, flags);
1302 SetLastError( ERROR_HANDLE_EOF );
1303 return INVALID_HANDLE_VALUE;
1307 /******************************************************************************
1308 * FindNextFileA (kernelbase.@)
1310 BOOL WINAPI DECLSPEC_HOTPATCH FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
1312 WIN32_FIND_DATAW dataW;
1314 if (!FindNextFileW( handle, &dataW )) return FALSE;
1315 data->dwFileAttributes = dataW.dwFileAttributes;
1316 data->ftCreationTime = dataW.ftCreationTime;
1317 data->ftLastAccessTime = dataW.ftLastAccessTime;
1318 data->ftLastWriteTime = dataW.ftLastWriteTime;
1319 data->nFileSizeHigh = dataW.nFileSizeHigh;
1320 data->nFileSizeLow = dataW.nFileSizeLow;
1321 file_name_WtoA( dataW.cFileName, -1, data->cFileName, sizeof(data->cFileName) );
1322 file_name_WtoA( dataW.cAlternateFileName, -1, data->cAlternateFileName,
1323 sizeof(data->cAlternateFileName) );
1324 return TRUE;
1328 /******************************************************************************
1329 * FindNextFileW (kernelbase.@)
1331 BOOL WINAPI DECLSPEC_HOTPATCH FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
1333 FIND_FIRST_INFO *info = handle;
1334 FILE_BOTH_DIR_INFORMATION *dir_info;
1335 BOOL ret = FALSE;
1336 NTSTATUS status;
1338 TRACE( "%p %p\n", handle, data );
1340 if (!handle || handle == INVALID_HANDLE_VALUE || info->magic != FIND_FIRST_MAGIC)
1342 SetLastError( ERROR_INVALID_HANDLE );
1343 return ret;
1346 RtlEnterCriticalSection( &info->cs );
1348 if (!info->handle) SetLastError( ERROR_NO_MORE_FILES );
1349 else for (;;)
1351 if (info->data_pos >= info->data_len) /* need to read some more data */
1353 IO_STATUS_BLOCK io;
1355 if (info->data_size)
1356 status = NtQueryDirectoryFile( info->handle, 0, NULL, NULL, &io, info->data, info->data_size,
1357 FileBothDirectoryInformation, FALSE, NULL, FALSE );
1358 else
1359 status = STATUS_NO_MORE_FILES;
1361 if (!set_ntstatus( status ))
1363 if (status == STATUS_NO_MORE_FILES)
1365 CloseHandle( info->handle );
1366 info->handle = 0;
1368 break;
1370 info->data_len = io.Information;
1371 info->data_pos = 0;
1374 dir_info = (FILE_BOTH_DIR_INFORMATION *)(info->data + info->data_pos);
1376 if (dir_info->NextEntryOffset) info->data_pos += dir_info->NextEntryOffset;
1377 else info->data_pos = info->data_len;
1379 /* don't return '.' and '..' in the root of the drive */
1380 if (info->is_root)
1382 if (dir_info->FileNameLength == sizeof(WCHAR) && dir_info->FileName[0] == '.') continue;
1383 if (dir_info->FileNameLength == 2 * sizeof(WCHAR) &&
1384 dir_info->FileName[0] == '.' && dir_info->FileName[1] == '.') continue;
1387 data->dwFileAttributes = dir_info->FileAttributes;
1388 data->ftCreationTime = *(FILETIME *)&dir_info->CreationTime;
1389 data->ftLastAccessTime = *(FILETIME *)&dir_info->LastAccessTime;
1390 data->ftLastWriteTime = *(FILETIME *)&dir_info->LastWriteTime;
1391 data->nFileSizeHigh = dir_info->EndOfFile.QuadPart >> 32;
1392 data->nFileSizeLow = (DWORD)dir_info->EndOfFile.QuadPart;
1393 data->dwReserved0 = 0;
1394 data->dwReserved1 = 0;
1396 memcpy( data->cFileName, dir_info->FileName, dir_info->FileNameLength );
1397 data->cFileName[dir_info->FileNameLength/sizeof(WCHAR)] = 0;
1399 if (info->level != FindExInfoBasic)
1401 memcpy( data->cAlternateFileName, dir_info->ShortName, dir_info->ShortNameLength );
1402 data->cAlternateFileName[dir_info->ShortNameLength/sizeof(WCHAR)] = 0;
1404 else
1405 data->cAlternateFileName[0] = 0;
1407 TRACE( "returning %s (%s)\n",
1408 debugstr_w(data->cFileName), debugstr_w(data->cAlternateFileName) );
1410 ret = TRUE;
1411 break;
1414 RtlLeaveCriticalSection( &info->cs );
1415 return ret;
1419 /**************************************************************************
1420 * FindNextStreamW (kernelbase.@)
1422 BOOL WINAPI FindNextStreamW( HANDLE handle, void *data )
1424 FIXME( "(%p, %p): stub!\n", handle, data );
1425 SetLastError( ERROR_HANDLE_EOF );
1426 return FALSE;
1430 /******************************************************************************
1431 * FindClose (kernelbase.@)
1433 BOOL WINAPI DECLSPEC_HOTPATCH FindClose( HANDLE handle )
1435 FIND_FIRST_INFO *info = handle;
1437 if (!handle || handle == INVALID_HANDLE_VALUE)
1439 SetLastError( ERROR_INVALID_HANDLE );
1440 return FALSE;
1443 __TRY
1445 if (info->magic == FIND_FIRST_MAGIC)
1447 RtlEnterCriticalSection( &info->cs );
1448 if (info->magic == FIND_FIRST_MAGIC) /* in case someone else freed it in the meantime */
1450 info->magic = 0;
1451 if (info->handle) CloseHandle( info->handle );
1452 info->handle = 0;
1453 RtlFreeUnicodeString( &info->path );
1454 info->data_pos = 0;
1455 info->data_len = 0;
1456 RtlLeaveCriticalSection( &info->cs );
1457 info->cs.DebugInfo->Spare[0] = 0;
1458 RtlDeleteCriticalSection( &info->cs );
1459 HeapFree( GetProcessHeap(), 0, info );
1463 __EXCEPT_PAGE_FAULT
1465 WARN( "illegal handle %p\n", handle );
1466 SetLastError( ERROR_INVALID_HANDLE );
1467 return FALSE;
1469 __ENDTRY
1471 return TRUE;
1475 /******************************************************************************
1476 * GetCompressedFileSizeA (kernelbase.@)
1478 DWORD WINAPI DECLSPEC_HOTPATCH GetCompressedFileSizeA( LPCSTR name, LPDWORD size_high )
1480 WCHAR *nameW;
1482 if (!(nameW = file_name_AtoW( name, FALSE ))) return INVALID_FILE_SIZE;
1483 return GetCompressedFileSizeW( nameW, size_high );
1487 /******************************************************************************
1488 * GetCompressedFileSizeW (kernelbase.@)
1490 DWORD WINAPI DECLSPEC_HOTPATCH GetCompressedFileSizeW( LPCWSTR name, LPDWORD size_high )
1492 UNICODE_STRING nt_name;
1493 OBJECT_ATTRIBUTES attr;
1494 IO_STATUS_BLOCK io;
1495 NTSTATUS status;
1496 HANDLE handle;
1497 DWORD ret;
1499 TRACE("%s %p\n", debugstr_w(name), size_high);
1501 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1503 SetLastError( ERROR_PATH_NOT_FOUND );
1504 return INVALID_FILE_SIZE;
1507 attr.Length = sizeof(attr);
1508 attr.RootDirectory = 0;
1509 attr.Attributes = OBJ_CASE_INSENSITIVE;
1510 attr.ObjectName = &nt_name;
1511 attr.SecurityDescriptor = NULL;
1512 attr.SecurityQualityOfService = NULL;
1514 status = NtOpenFile( &handle, SYNCHRONIZE, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
1515 RtlFreeUnicodeString( &nt_name );
1516 if (!set_ntstatus( status )) return INVALID_FILE_SIZE;
1518 /* we don't support compressed files, simply return the file size */
1519 ret = GetFileSize( handle, size_high );
1520 NtClose( handle );
1521 return ret;
1525 /***********************************************************************
1526 * GetCurrentDirectoryA (kernelbase.@)
1528 UINT WINAPI DECLSPEC_HOTPATCH GetCurrentDirectoryA( UINT buflen, LPSTR buf )
1530 WCHAR bufferW[MAX_PATH];
1531 DWORD ret;
1533 if (buflen && buf && ((ULONG_PTR)buf >> 16) == 0)
1535 /* Win9x catches access violations here, returning zero.
1536 * This behaviour resulted in some people not noticing
1537 * that they got the argument order wrong. So let's be
1538 * nice and fail gracefully if buf is invalid and looks
1539 * more like a buflen. */
1540 SetLastError( ERROR_INVALID_PARAMETER );
1541 return 0;
1544 ret = RtlGetCurrentDirectory_U( sizeof(bufferW), bufferW );
1545 if (!ret) return 0;
1546 if (ret > sizeof(bufferW))
1548 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1549 return 0;
1551 return copy_filename_WtoA( bufferW, buf, buflen );
1555 /***********************************************************************
1556 * GetCurrentDirectoryW (kernelbase.@)
1558 UINT WINAPI DECLSPEC_HOTPATCH GetCurrentDirectoryW( UINT buflen, LPWSTR buf )
1560 return RtlGetCurrentDirectory_U( buflen * sizeof(WCHAR), buf ) / sizeof(WCHAR);
1564 /**************************************************************************
1565 * GetFileAttributesA (kernelbase.@)
1567 DWORD WINAPI DECLSPEC_HOTPATCH GetFileAttributesA( LPCSTR name )
1569 WCHAR *nameW;
1571 if (!(nameW = file_name_AtoW( name, FALSE ))) return INVALID_FILE_ATTRIBUTES;
1572 return GetFileAttributesW( nameW );
1576 /**************************************************************************
1577 * GetFileAttributesW (kernelbase.@)
1579 DWORD WINAPI DECLSPEC_HOTPATCH GetFileAttributesW( LPCWSTR name )
1581 FILE_BASIC_INFORMATION info;
1582 UNICODE_STRING nt_name;
1583 OBJECT_ATTRIBUTES attr;
1584 NTSTATUS status;
1586 TRACE( "%s\n", debugstr_w(name) );
1588 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1590 SetLastError( ERROR_PATH_NOT_FOUND );
1591 return INVALID_FILE_ATTRIBUTES;
1594 attr.Length = sizeof(attr);
1595 attr.RootDirectory = 0;
1596 attr.Attributes = OBJ_CASE_INSENSITIVE;
1597 attr.ObjectName = &nt_name;
1598 attr.SecurityDescriptor = NULL;
1599 attr.SecurityQualityOfService = NULL;
1601 status = NtQueryAttributesFile( &attr, &info );
1602 RtlFreeUnicodeString( &nt_name );
1604 if (status == STATUS_SUCCESS) return info.FileAttributes;
1606 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
1607 if (RtlIsDosDeviceName_U( name )) return FILE_ATTRIBUTE_ARCHIVE;
1609 SetLastError( RtlNtStatusToDosError(status) );
1610 return INVALID_FILE_ATTRIBUTES;
1614 /**************************************************************************
1615 * GetFileAttributesExA (kernelbase.@)
1617 BOOL WINAPI DECLSPEC_HOTPATCH GetFileAttributesExA( LPCSTR name, GET_FILEEX_INFO_LEVELS level, void *ptr )
1619 WCHAR *nameW;
1621 if (!(nameW = file_name_AtoW( name, FALSE ))) return FALSE;
1622 return GetFileAttributesExW( nameW, level, ptr );
1626 /**************************************************************************
1627 * GetFileAttributesExW (kernelbase.@)
1629 BOOL WINAPI DECLSPEC_HOTPATCH GetFileAttributesExW( LPCWSTR name, GET_FILEEX_INFO_LEVELS level, void *ptr )
1631 FILE_NETWORK_OPEN_INFORMATION info;
1632 WIN32_FILE_ATTRIBUTE_DATA *data = ptr;
1633 UNICODE_STRING nt_name;
1634 OBJECT_ATTRIBUTES attr;
1635 NTSTATUS status;
1637 TRACE("%s %d %p\n", debugstr_w(name), level, ptr);
1639 if (level != GetFileExInfoStandard)
1641 SetLastError( ERROR_INVALID_PARAMETER );
1642 return FALSE;
1645 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1647 SetLastError( ERROR_PATH_NOT_FOUND );
1648 return FALSE;
1651 attr.Length = sizeof(attr);
1652 attr.RootDirectory = 0;
1653 attr.Attributes = OBJ_CASE_INSENSITIVE;
1654 attr.ObjectName = &nt_name;
1655 attr.SecurityDescriptor = NULL;
1656 attr.SecurityQualityOfService = NULL;
1658 status = NtQueryFullAttributesFile( &attr, &info );
1659 RtlFreeUnicodeString( &nt_name );
1660 if (!set_ntstatus( status )) return FALSE;
1662 data->dwFileAttributes = info.FileAttributes;
1663 data->ftCreationTime.dwLowDateTime = info.CreationTime.u.LowPart;
1664 data->ftCreationTime.dwHighDateTime = info.CreationTime.u.HighPart;
1665 data->ftLastAccessTime.dwLowDateTime = info.LastAccessTime.u.LowPart;
1666 data->ftLastAccessTime.dwHighDateTime = info.LastAccessTime.u.HighPart;
1667 data->ftLastWriteTime.dwLowDateTime = info.LastWriteTime.u.LowPart;
1668 data->ftLastWriteTime.dwHighDateTime = info.LastWriteTime.u.HighPart;
1669 data->nFileSizeLow = info.EndOfFile.u.LowPart;
1670 data->nFileSizeHigh = info.EndOfFile.u.HighPart;
1671 return TRUE;
1675 /***********************************************************************
1676 * GetFinalPathNameByHandleA (kernelbase.@)
1678 DWORD WINAPI DECLSPEC_HOTPATCH GetFinalPathNameByHandleA( HANDLE file, LPSTR path,
1679 DWORD count, DWORD flags )
1681 WCHAR *str;
1682 DWORD result, len;
1684 TRACE( "(%p,%p,%d,%x)\n", file, path, count, flags);
1686 len = GetFinalPathNameByHandleW(file, NULL, 0, flags);
1687 if (len == 0) return 0;
1689 str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1690 if (!str)
1692 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1693 return 0;
1696 result = GetFinalPathNameByHandleW(file, str, len, flags);
1697 if (result != len - 1)
1699 HeapFree(GetProcessHeap(), 0, str);
1700 return 0;
1703 len = file_name_WtoA( str, -1, NULL, 0 );
1704 if (count < len)
1706 HeapFree(GetProcessHeap(), 0, str);
1707 return len - 1;
1709 file_name_WtoA( str, -1, path, count );
1710 HeapFree(GetProcessHeap(), 0, str);
1711 return len - 1;
1715 /***********************************************************************
1716 * GetFinalPathNameByHandleW (kernelbase.@)
1718 DWORD WINAPI DECLSPEC_HOTPATCH GetFinalPathNameByHandleW( HANDLE file, LPWSTR path,
1719 DWORD count, DWORD flags )
1721 WCHAR buffer[sizeof(OBJECT_NAME_INFORMATION) + MAX_PATH + 1];
1722 OBJECT_NAME_INFORMATION *info = (OBJECT_NAME_INFORMATION*)&buffer;
1723 WCHAR drive_part[MAX_PATH];
1724 DWORD drive_part_len = 0;
1725 NTSTATUS status;
1726 DWORD result = 0;
1727 ULONG dummy;
1728 WCHAR *ptr;
1730 TRACE( "(%p,%p,%d,%x)\n", file, path, count, flags );
1732 if (flags & ~(FILE_NAME_OPENED | VOLUME_NAME_GUID | VOLUME_NAME_NONE | VOLUME_NAME_NT))
1734 WARN("Unknown flags: %x\n", flags);
1735 SetLastError( ERROR_INVALID_PARAMETER );
1736 return 0;
1739 /* get object name */
1740 status = NtQueryObject( file, ObjectNameInformation, &buffer, sizeof(buffer) - sizeof(WCHAR), &dummy );
1741 if (!set_ntstatus( status )) return 0;
1743 if (!info->Name.Buffer)
1745 SetLastError( ERROR_INVALID_HANDLE );
1746 return 0;
1748 if (info->Name.Length < 4 * sizeof(WCHAR) || info->Name.Buffer[0] != '\\' ||
1749 info->Name.Buffer[1] != '?' || info->Name.Buffer[2] != '?' || info->Name.Buffer[3] != '\\' )
1751 FIXME("Unexpected object name: %s\n", debugstr_wn(info->Name.Buffer, info->Name.Length / sizeof(WCHAR)));
1752 SetLastError( ERROR_GEN_FAILURE );
1753 return 0;
1756 /* add terminating null character, remove "\\??\\" */
1757 info->Name.Buffer[info->Name.Length / sizeof(WCHAR)] = 0;
1758 info->Name.Length -= 4 * sizeof(WCHAR);
1759 info->Name.Buffer += 4;
1761 /* FILE_NAME_OPENED is not supported yet, and would require Wineserver changes */
1762 if (flags & FILE_NAME_OPENED)
1764 FIXME("FILE_NAME_OPENED not supported\n");
1765 flags &= ~FILE_NAME_OPENED;
1768 /* Get information required for VOLUME_NAME_NONE, VOLUME_NAME_GUID and VOLUME_NAME_NT */
1769 if (flags == VOLUME_NAME_NONE || flags == VOLUME_NAME_GUID || flags == VOLUME_NAME_NT)
1771 if (!GetVolumePathNameW( info->Name.Buffer, drive_part, MAX_PATH )) return 0;
1772 drive_part_len = lstrlenW(drive_part);
1773 if (!drive_part_len || drive_part_len > lstrlenW(info->Name.Buffer) ||
1774 drive_part[drive_part_len-1] != '\\' ||
1775 CompareStringOrdinal( info->Name.Buffer, drive_part_len, drive_part, drive_part_len, TRUE ) != CSTR_EQUAL)
1777 FIXME( "Path %s returned by GetVolumePathNameW does not match file path %s\n",
1778 debugstr_w(drive_part), debugstr_w(info->Name.Buffer) );
1779 SetLastError( ERROR_GEN_FAILURE );
1780 return 0;
1784 if (flags == VOLUME_NAME_NONE)
1786 ptr = info->Name.Buffer + drive_part_len - 1;
1787 result = lstrlenW(ptr);
1788 if (result < count) memcpy(path, ptr, (result + 1) * sizeof(WCHAR));
1789 else result++;
1791 else if (flags == VOLUME_NAME_GUID)
1793 WCHAR volume_prefix[51];
1795 /* GetVolumeNameForVolumeMountPointW sets error code on failure */
1796 if (!GetVolumeNameForVolumeMountPointW( drive_part, volume_prefix, 50 )) return 0;
1797 ptr = info->Name.Buffer + drive_part_len;
1798 result = lstrlenW(volume_prefix) + lstrlenW(ptr);
1799 if (result < count)
1801 lstrcpyW(path, volume_prefix);
1802 lstrcatW(path, ptr);
1804 else
1806 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1807 result++;
1810 else if (flags == VOLUME_NAME_NT)
1812 WCHAR nt_prefix[MAX_PATH];
1814 /* QueryDosDeviceW sets error code on failure */
1815 drive_part[drive_part_len - 1] = 0;
1816 if (!QueryDosDeviceW( drive_part, nt_prefix, MAX_PATH )) return 0;
1817 ptr = info->Name.Buffer + drive_part_len - 1;
1818 result = lstrlenW(nt_prefix) + lstrlenW(ptr);
1819 if (result < count)
1821 lstrcpyW(path, nt_prefix);
1822 lstrcatW(path, ptr);
1824 else
1826 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1827 result++;
1830 else if (flags == VOLUME_NAME_DOS)
1832 result = 4 + lstrlenW(info->Name.Buffer);
1833 if (result < count)
1835 lstrcpyW(path, L"\\\\?\\");
1836 lstrcatW(path, info->Name.Buffer);
1838 else
1840 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1841 result++;
1844 else
1846 /* Windows crashes here, but we prefer returning ERROR_INVALID_PARAMETER */
1847 WARN("Invalid combination of flags: %x\n", flags);
1848 SetLastError( ERROR_INVALID_PARAMETER );
1850 return result;
1854 /***********************************************************************
1855 * GetFullPathNameA (kernelbase.@)
1857 DWORD WINAPI DECLSPEC_HOTPATCH GetFullPathNameA( LPCSTR name, DWORD len, LPSTR buffer, LPSTR *lastpart )
1859 WCHAR *nameW;
1860 WCHAR bufferW[MAX_PATH], *lastpartW = NULL;
1861 DWORD ret;
1863 if (!(nameW = file_name_AtoW( name, FALSE ))) return 0;
1865 ret = GetFullPathNameW( nameW, MAX_PATH, bufferW, &lastpartW );
1867 if (!ret) return 0;
1868 if (ret > MAX_PATH)
1870 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1871 return 0;
1873 ret = copy_filename_WtoA( bufferW, buffer, len );
1874 if (ret < len && lastpart)
1876 if (lastpartW)
1877 *lastpart = buffer + file_name_WtoA( bufferW, lastpartW - bufferW, NULL, 0 );
1878 else
1879 *lastpart = NULL;
1881 return ret;
1885 /***********************************************************************
1886 * GetFullPathNameW (kernelbase.@)
1888 DWORD WINAPI DECLSPEC_HOTPATCH GetFullPathNameW( LPCWSTR name, DWORD len, LPWSTR buffer, LPWSTR *lastpart )
1890 return RtlGetFullPathName_U( name, len * sizeof(WCHAR), buffer, lastpart ) / sizeof(WCHAR);
1894 /***********************************************************************
1895 * GetLongPathNameA (kernelbase.@)
1897 DWORD WINAPI DECLSPEC_HOTPATCH GetLongPathNameA( LPCSTR shortpath, LPSTR longpath, DWORD longlen )
1899 WCHAR *shortpathW;
1900 WCHAR longpathW[MAX_PATH];
1901 DWORD ret;
1903 TRACE( "%s\n", debugstr_a( shortpath ));
1905 if (!(shortpathW = file_name_AtoW( shortpath, FALSE ))) return 0;
1907 ret = GetLongPathNameW( shortpathW, longpathW, MAX_PATH );
1909 if (!ret) return 0;
1910 if (ret > MAX_PATH)
1912 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1913 return 0;
1915 return copy_filename_WtoA( longpathW, longpath, longlen );
1919 /***********************************************************************
1920 * GetLongPathNameW (kernelbase.@)
1922 DWORD WINAPI DECLSPEC_HOTPATCH GetLongPathNameW( LPCWSTR shortpath, LPWSTR longpath, DWORD longlen )
1924 WCHAR tmplongpath[1024];
1925 DWORD sp = 0, lp = 0, tmplen;
1926 WIN32_FIND_DATAW wfd;
1927 UNICODE_STRING nameW;
1928 LPCWSTR p;
1929 HANDLE handle;
1931 TRACE("%s,%p,%u\n", debugstr_w(shortpath), longpath, longlen);
1933 if (!shortpath)
1935 SetLastError( ERROR_INVALID_PARAMETER );
1936 return 0;
1938 if (!shortpath[0])
1940 SetLastError( ERROR_PATH_NOT_FOUND );
1941 return 0;
1944 if (shortpath[0] == '\\' && shortpath[1] == '\\')
1946 FIXME( "UNC pathname %s\n", debugstr_w(shortpath) );
1947 tmplen = lstrlenW( shortpath );
1948 if (tmplen < longlen)
1950 if (longpath != shortpath) lstrcpyW( longpath, shortpath );
1951 return tmplen;
1953 return tmplen + 1;
1956 /* check for drive letter */
1957 if (shortpath[0] != '/' && shortpath[1] == ':' )
1959 tmplongpath[0] = shortpath[0];
1960 tmplongpath[1] = ':';
1961 lp = sp = 2;
1964 if (wcspbrk( shortpath + sp, L"*?" ))
1966 SetLastError( ERROR_INVALID_NAME );
1967 return 0;
1970 while (shortpath[sp])
1972 /* check for path delimiters and reproduce them */
1973 if (shortpath[sp] == '\\' || shortpath[sp] == '/')
1975 tmplongpath[lp++] = shortpath[sp++];
1976 tmplongpath[lp] = 0; /* terminate string */
1977 continue;
1980 for (p = shortpath + sp; *p && *p != '/' && *p != '\\'; p++);
1981 tmplen = p - (shortpath + sp);
1982 lstrcpynW( tmplongpath + lp, shortpath + sp, tmplen + 1 );
1984 if (tmplongpath[lp] == '.')
1986 if (tmplen == 1 || (tmplen == 2 && tmplongpath[lp + 1] == '.'))
1988 lp += tmplen;
1989 sp += tmplen;
1990 continue;
1994 /* Check if the file exists */
1995 handle = FindFirstFileW( tmplongpath, &wfd );
1996 if (handle == INVALID_HANDLE_VALUE)
1998 TRACE( "not found %s\n", debugstr_w( tmplongpath ));
1999 SetLastError ( ERROR_FILE_NOT_FOUND );
2000 return 0;
2002 FindClose( handle );
2004 /* Use the existing file name if it's a short name */
2005 RtlInitUnicodeString( &nameW, tmplongpath + lp );
2006 if (RtlIsNameLegalDOS8Dot3( &nameW, NULL, NULL )) lstrcpyW( tmplongpath + lp, wfd.cFileName );
2007 lp += lstrlenW( tmplongpath + lp );
2008 sp += tmplen;
2010 tmplen = lstrlenW( shortpath ) - 1;
2011 if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
2012 (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
2013 tmplongpath[lp++] = shortpath[tmplen];
2014 tmplongpath[lp] = 0;
2016 tmplen = lstrlenW( tmplongpath ) + 1;
2017 if (tmplen <= longlen)
2019 lstrcpyW( longpath, tmplongpath );
2020 TRACE("returning %s\n", debugstr_w( longpath ));
2021 tmplen--; /* length without 0 */
2023 return tmplen;
2027 /***********************************************************************
2028 * GetShortPathNameW (kernelbase.@)
2030 DWORD WINAPI DECLSPEC_HOTPATCH GetShortPathNameW( LPCWSTR longpath, LPWSTR shortpath, DWORD shortlen )
2032 WIN32_FIND_DATAW wfd;
2033 WCHAR *tmpshortpath;
2034 HANDLE handle;
2035 LPCWSTR p;
2036 DWORD sp = 0, lp = 0, tmplen, buf_len;
2038 TRACE( "%s,%p,%u\n", debugstr_w(longpath), shortpath, shortlen );
2040 if (!longpath)
2042 SetLastError( ERROR_INVALID_PARAMETER );
2043 return 0;
2045 if (!longpath[0])
2047 SetLastError( ERROR_BAD_PATHNAME );
2048 return 0;
2051 /* code below only removes characters from string, never adds, so this is
2052 * the largest buffer that tmpshortpath will need to have */
2053 buf_len = lstrlenW(longpath) + 1;
2054 tmpshortpath = HeapAlloc( GetProcessHeap(), 0, buf_len * sizeof(WCHAR) );
2055 if (!tmpshortpath)
2057 SetLastError( ERROR_OUTOFMEMORY );
2058 return 0;
2061 if (longpath[0] == '\\' && longpath[1] == '\\' && longpath[2] == '?' && longpath[3] == '\\')
2063 memcpy( tmpshortpath, longpath, 4 * sizeof(WCHAR) );
2064 sp = lp = 4;
2067 if (wcspbrk( longpath + lp, L"*?" ))
2069 HeapFree( GetProcessHeap(), 0, tmpshortpath );
2070 SetLastError( ERROR_INVALID_NAME );
2071 return 0;
2074 /* check for drive letter */
2075 if (longpath[lp] != '/' && longpath[lp + 1] == ':' )
2077 tmpshortpath[sp] = longpath[lp];
2078 tmpshortpath[sp + 1] = ':';
2079 sp += 2;
2080 lp += 2;
2083 while (longpath[lp])
2085 /* check for path delimiters and reproduce them */
2086 if (longpath[lp] == '\\' || longpath[lp] == '/')
2088 tmpshortpath[sp++] = longpath[lp++];
2089 tmpshortpath[sp] = 0; /* terminate string */
2090 continue;
2093 p = longpath + lp;
2094 for (; *p && *p != '/' && *p != '\\'; p++);
2095 tmplen = p - (longpath + lp);
2096 lstrcpynW( tmpshortpath + sp, longpath + lp, tmplen + 1 );
2098 if (tmpshortpath[sp] == '.')
2100 if (tmplen == 1 || (tmplen == 2 && tmpshortpath[sp + 1] == '.'))
2102 sp += tmplen;
2103 lp += tmplen;
2104 continue;
2108 /* Check if the file exists and use the existing short file name */
2109 handle = FindFirstFileW( tmpshortpath, &wfd );
2110 if (handle == INVALID_HANDLE_VALUE) goto notfound;
2111 FindClose( handle );
2113 /* In rare cases (like "a.abcd") short path may be longer than original path.
2114 * Make sure we have enough space in temp buffer. */
2115 if (wfd.cAlternateFileName[0] && tmplen < lstrlenW(wfd.cAlternateFileName))
2117 WCHAR *new_buf;
2118 buf_len += lstrlenW( wfd.cAlternateFileName ) - tmplen;
2119 new_buf = HeapReAlloc( GetProcessHeap(), 0, tmpshortpath, buf_len * sizeof(WCHAR) );
2120 if(!new_buf)
2122 HeapFree( GetProcessHeap(), 0, tmpshortpath );
2123 SetLastError( ERROR_OUTOFMEMORY );
2124 return 0;
2126 tmpshortpath = new_buf;
2129 lstrcpyW( tmpshortpath + sp, wfd.cAlternateFileName[0] ? wfd.cAlternateFileName : wfd.cFileName );
2130 sp += lstrlenW( tmpshortpath + sp );
2131 lp += tmplen;
2133 tmpshortpath[sp] = 0;
2135 tmplen = lstrlenW( tmpshortpath ) + 1;
2136 if (tmplen <= shortlen)
2138 lstrcpyW( shortpath, tmpshortpath );
2139 TRACE( "returning %s\n", debugstr_w( shortpath ));
2140 tmplen--; /* length without 0 */
2143 HeapFree( GetProcessHeap(), 0, tmpshortpath );
2144 return tmplen;
2146 notfound:
2147 HeapFree( GetProcessHeap(), 0, tmpshortpath );
2148 TRACE( "not found\n" );
2149 SetLastError( ERROR_FILE_NOT_FOUND );
2150 return 0;
2154 /***********************************************************************
2155 * GetSystemDirectoryA (kernelbase.@)
2157 UINT WINAPI DECLSPEC_HOTPATCH GetSystemDirectoryA( LPSTR path, UINT count )
2159 return copy_filename_WtoA( system_dir, path, count );
2163 /***********************************************************************
2164 * GetSystemDirectoryW (kernelbase.@)
2166 UINT WINAPI DECLSPEC_HOTPATCH GetSystemDirectoryW( LPWSTR path, UINT count )
2168 return copy_filename( system_dir, path, count );
2172 /***********************************************************************
2173 * GetSystemWindowsDirectoryA (kernelbase.@)
2175 UINT WINAPI DECLSPEC_HOTPATCH GetSystemWindowsDirectoryA( LPSTR path, UINT count )
2177 return GetWindowsDirectoryA( path, count );
2181 /***********************************************************************
2182 * GetSystemWindowsDirectoryW (kernelbase.@)
2184 UINT WINAPI DECLSPEC_HOTPATCH GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
2186 return GetWindowsDirectoryW( path, count );
2190 /***********************************************************************
2191 * GetSystemWow64DirectoryA (kernelbase.@)
2193 UINT WINAPI /* DECLSPEC_HOTPATCH */ GetSystemWow64DirectoryA( LPSTR path, UINT count )
2195 if (!is_win64 && !is_wow64)
2197 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
2198 return 0;
2200 return copy_filename_WtoA( get_machine_wow64_dir( IMAGE_FILE_MACHINE_I386 ), path, count );
2204 /***********************************************************************
2205 * GetSystemWow64DirectoryW (kernelbase.@)
2207 UINT WINAPI /* DECLSPEC_HOTPATCH */ GetSystemWow64DirectoryW( LPWSTR path, UINT count )
2209 if (!is_win64 && !is_wow64)
2211 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
2212 return 0;
2214 return copy_filename( get_machine_wow64_dir( IMAGE_FILE_MACHINE_I386 ), path, count );
2218 /***********************************************************************
2219 * GetSystemWow64Directory2A (kernelbase.@)
2221 UINT WINAPI DECLSPEC_HOTPATCH GetSystemWow64Directory2A( LPSTR path, UINT count, WORD machine )
2223 const WCHAR *dir = get_machine_wow64_dir( machine );
2225 return dir ? copy_filename_WtoA( dir, path, count ) : 0;
2229 /***********************************************************************
2230 * GetSystemWow64Directory2W (kernelbase.@)
2232 UINT WINAPI DECLSPEC_HOTPATCH GetSystemWow64Directory2W( LPWSTR path, UINT count, WORD machine )
2234 const WCHAR *dir = get_machine_wow64_dir( machine );
2236 return dir ? copy_filename( dir, path, count ) : 0;
2240 /***********************************************************************
2241 * GetTempFileNameA (kernelbase.@)
2243 UINT WINAPI DECLSPEC_HOTPATCH GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique, LPSTR buffer )
2245 WCHAR *pathW, *prefixW = NULL;
2246 WCHAR bufferW[MAX_PATH];
2247 UINT ret;
2249 if (!(pathW = file_name_AtoW( path, FALSE ))) return 0;
2250 if (prefix && !(prefixW = file_name_AtoW( prefix, TRUE ))) return 0;
2252 ret = GetTempFileNameW( pathW, prefixW, unique, bufferW );
2253 if (ret) file_name_WtoA( bufferW, -1, buffer, MAX_PATH );
2255 HeapFree( GetProcessHeap(), 0, prefixW );
2256 return ret;
2260 /***********************************************************************
2261 * GetTempFileNameW (kernelbase.@)
2263 UINT WINAPI DECLSPEC_HOTPATCH GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique, LPWSTR buffer )
2265 int i;
2266 LPWSTR p;
2267 DWORD attr;
2269 if (!path || !buffer)
2271 SetLastError( ERROR_INVALID_PARAMETER );
2272 return 0;
2275 /* ensure that the provided directory exists */
2276 attr = GetFileAttributesW( path );
2277 if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_DIRECTORY))
2279 TRACE( "path not found %s\n", debugstr_w( path ));
2280 SetLastError( ERROR_DIRECTORY );
2281 return 0;
2284 lstrcpyW( buffer, path );
2285 p = buffer + lstrlenW(buffer);
2287 /* add a \, if there isn't one */
2288 if ((p == buffer) || (p[-1] != '\\')) *p++ = '\\';
2290 if (prefix) for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
2292 unique &= 0xffff;
2293 if (unique) swprintf( p, MAX_PATH - (p - buffer), L"%x.tmp", unique );
2294 else
2296 /* get a "random" unique number and try to create the file */
2297 HANDLE handle;
2298 UINT num = NtGetTickCount() & 0xffff;
2299 static UINT last;
2301 /* avoid using the same name twice in a short interval */
2302 if (last - num < 10) num = last + 1;
2303 if (!num) num = 1;
2304 unique = num;
2307 swprintf( p, MAX_PATH - (p - buffer), L"%x.tmp", unique );
2308 handle = CreateFileW( buffer, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
2309 if (handle != INVALID_HANDLE_VALUE)
2310 { /* We created it */
2311 CloseHandle( handle );
2312 last = unique;
2313 break;
2315 if (GetLastError() != ERROR_FILE_EXISTS && GetLastError() != ERROR_SHARING_VIOLATION)
2316 break; /* No need to go on */
2317 if (!(++unique & 0xffff)) unique = 1;
2318 } while (unique != num);
2320 TRACE( "returning %s\n", debugstr_w( buffer ));
2321 return unique;
2325 /***********************************************************************
2326 * GetTempPathA (kernelbase.@)
2328 DWORD WINAPI DECLSPEC_HOTPATCH GetTempPathA( DWORD count, LPSTR path )
2330 WCHAR pathW[MAX_PATH];
2331 UINT ret;
2333 if (!(ret = GetTempPathW( MAX_PATH, pathW ))) return 0;
2334 if (ret > MAX_PATH)
2336 SetLastError( ERROR_FILENAME_EXCED_RANGE );
2337 return 0;
2339 return copy_filename_WtoA( pathW, path, count );
2343 /***********************************************************************
2344 * GetTempPathW (kernelbase.@)
2346 DWORD WINAPI DECLSPEC_HOTPATCH GetTempPathW( DWORD count, LPWSTR path )
2348 WCHAR tmp_path[MAX_PATH];
2349 UINT ret;
2351 if (!(ret = GetEnvironmentVariableW( L"TMP", tmp_path, MAX_PATH )) &&
2352 !(ret = GetEnvironmentVariableW( L"TEMP", tmp_path, MAX_PATH )) &&
2353 !(ret = GetEnvironmentVariableW( L"USERPROFILE", tmp_path, MAX_PATH )) &&
2354 !(ret = GetWindowsDirectoryW( tmp_path, MAX_PATH )))
2355 return 0;
2357 if (ret > MAX_PATH)
2359 SetLastError( ERROR_FILENAME_EXCED_RANGE );
2360 return 0;
2362 ret = GetFullPathNameW( tmp_path, MAX_PATH, tmp_path, NULL );
2363 if (!ret) return 0;
2365 if (ret > MAX_PATH - 2)
2367 SetLastError( ERROR_FILENAME_EXCED_RANGE );
2368 return 0;
2370 if (tmp_path[ret-1] != '\\')
2372 tmp_path[ret++] = '\\';
2373 tmp_path[ret] = '\0';
2376 ret++; /* add space for terminating 0 */
2377 if (count >= ret)
2379 lstrcpynW( path, tmp_path, count );
2380 /* the remaining buffer must be zeroed up to 32766 bytes in XP or 32767
2381 * bytes after it, we will assume the > XP behavior for now */
2382 memset( path + ret, 0, (min(count, 32767) - ret) * sizeof(WCHAR) );
2383 ret--; /* return length without 0 */
2385 else if (count)
2387 /* the buffer must be cleared if contents will not fit */
2388 memset( path, 0, count * sizeof(WCHAR) );
2391 TRACE( "returning %u, %s\n", ret, debugstr_w( path ));
2392 return ret;
2396 /***********************************************************************
2397 * GetWindowsDirectoryA (kernelbase.@)
2399 UINT WINAPI DECLSPEC_HOTPATCH GetWindowsDirectoryA( LPSTR path, UINT count )
2401 return copy_filename_WtoA( windows_dir, path, count );
2405 /***********************************************************************
2406 * GetWindowsDirectoryW (kernelbase.@)
2408 UINT WINAPI DECLSPEC_HOTPATCH GetWindowsDirectoryW( LPWSTR path, UINT count )
2410 return copy_filename( windows_dir, path, count );
2414 /**************************************************************************
2415 * MoveFileExW (kernelbase.@)
2417 BOOL WINAPI MoveFileExW( const WCHAR *source, const WCHAR *dest, DWORD flag )
2419 return MoveFileWithProgressW( source, dest, NULL, NULL, flag );
2423 /**************************************************************************
2424 * MoveFileWithProgressW (kernelbase.@)
2426 BOOL WINAPI DECLSPEC_HOTPATCH MoveFileWithProgressW( const WCHAR *source, const WCHAR *dest,
2427 LPPROGRESS_ROUTINE progress,
2428 void *param, DWORD flag )
2430 FILE_RENAME_INFORMATION *rename_info;
2431 FILE_BASIC_INFORMATION info;
2432 UNICODE_STRING nt_name;
2433 OBJECT_ATTRIBUTES attr;
2434 IO_STATUS_BLOCK io;
2435 NTSTATUS status;
2436 HANDLE source_handle = 0;
2437 ULONG size;
2439 TRACE( "(%s,%s,%p,%p,%04x)\n", debugstr_w(source), debugstr_w(dest), progress, param, flag );
2441 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT) return add_boot_rename_entry( source, dest, flag );
2443 if (!dest) return DeleteFileW( source );
2445 /* check if we are allowed to rename the source */
2447 if (!RtlDosPathNameToNtPathName_U( source, &nt_name, NULL, NULL ))
2449 SetLastError( ERROR_PATH_NOT_FOUND );
2450 return FALSE;
2452 attr.Length = sizeof(attr);
2453 attr.RootDirectory = 0;
2454 attr.Attributes = OBJ_CASE_INSENSITIVE;
2455 attr.ObjectName = &nt_name;
2456 attr.SecurityDescriptor = NULL;
2457 attr.SecurityQualityOfService = NULL;
2459 status = NtOpenFile( &source_handle, DELETE | SYNCHRONIZE, &attr, &io,
2460 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2461 FILE_SYNCHRONOUS_IO_NONALERT );
2462 RtlFreeUnicodeString( &nt_name );
2463 if (!set_ntstatus( status )) goto error;
2465 status = NtQueryInformationFile( source_handle, &io, &info, sizeof(info), FileBasicInformation );
2466 if (!set_ntstatus( status )) goto error;
2468 if (!RtlDosPathNameToNtPathName_U( dest, &nt_name, NULL, NULL ))
2470 SetLastError( ERROR_PATH_NOT_FOUND );
2471 goto error;
2474 size = offsetof( FILE_RENAME_INFORMATION, FileName ) + nt_name.Length;
2475 if (!(rename_info = HeapAlloc( GetProcessHeap(), 0, size ))) goto error;
2477 rename_info->ReplaceIfExists = !!(flag & MOVEFILE_REPLACE_EXISTING);
2478 rename_info->RootDirectory = NULL;
2479 rename_info->FileNameLength = nt_name.Length;
2480 memcpy( rename_info->FileName, nt_name.Buffer, nt_name.Length );
2481 RtlFreeUnicodeString( &nt_name );
2482 status = NtSetInformationFile( source_handle, &io, rename_info, size, FileRenameInformation );
2483 HeapFree( GetProcessHeap(), 0, rename_info );
2484 if (status == STATUS_NOT_SAME_DEVICE && (flag & MOVEFILE_COPY_ALLOWED))
2486 NtClose( source_handle );
2487 if (!CopyFileExW( source, dest, progress, param, NULL,
2488 flag & MOVEFILE_REPLACE_EXISTING ? 0 : COPY_FILE_FAIL_IF_EXISTS ))
2489 return FALSE;
2490 return DeleteFileW( source );
2493 NtClose( source_handle );
2494 return set_ntstatus( status );
2496 error:
2497 if (source_handle) NtClose( source_handle );
2498 return FALSE;
2502 /***********************************************************************
2503 * NeedCurrentDirectoryForExePathA (kernelbase.@)
2505 BOOL WINAPI DECLSPEC_HOTPATCH NeedCurrentDirectoryForExePathA( LPCSTR name )
2507 WCHAR *nameW;
2509 if (!(nameW = file_name_AtoW( name, FALSE ))) return TRUE;
2510 return NeedCurrentDirectoryForExePathW( nameW );
2514 /***********************************************************************
2515 * NeedCurrentDirectoryForExePathW (kernelbase.@)
2517 BOOL WINAPI DECLSPEC_HOTPATCH NeedCurrentDirectoryForExePathW( LPCWSTR name )
2519 WCHAR env_val;
2521 if (wcschr( name, '\\' )) return TRUE;
2522 /* check the existence of the variable, not value */
2523 return !GetEnvironmentVariableW( L"NoDefaultCurrentDirectoryInExePath", &env_val, 1 );
2527 /***********************************************************************
2528 * ReplaceFileW (kernelbase.@)
2530 BOOL WINAPI DECLSPEC_HOTPATCH ReplaceFileW( const WCHAR *replaced, const WCHAR *replacement,
2531 const WCHAR *backup, DWORD flags,
2532 void *exclude, void *reserved )
2534 UNICODE_STRING nt_replaced_name, nt_replacement_name;
2535 HANDLE hReplacement = NULL;
2536 NTSTATUS status;
2537 IO_STATUS_BLOCK io;
2538 OBJECT_ATTRIBUTES attr;
2539 FILE_BASIC_INFORMATION info;
2541 TRACE( "%s %s %s 0x%08x %p %p\n", debugstr_w(replaced), debugstr_w(replacement), debugstr_w(backup),
2542 flags, exclude, reserved );
2544 if (flags) FIXME("Ignoring flags %x\n", flags);
2546 /* First two arguments are mandatory */
2547 if (!replaced || !replacement)
2549 SetLastError(ERROR_INVALID_PARAMETER);
2550 return FALSE;
2553 attr.Length = sizeof(attr);
2554 attr.RootDirectory = 0;
2555 attr.Attributes = OBJ_CASE_INSENSITIVE;
2556 attr.ObjectName = NULL;
2557 attr.SecurityDescriptor = NULL;
2558 attr.SecurityQualityOfService = NULL;
2560 /* Open the "replaced" file for reading */
2561 if (!RtlDosPathNameToNtPathName_U( replaced, &nt_replaced_name, NULL, NULL ))
2563 SetLastError( ERROR_PATH_NOT_FOUND );
2564 return FALSE;
2566 attr.ObjectName = &nt_replaced_name;
2568 /* Replacement should fail if replaced is READ_ONLY */
2569 status = NtQueryAttributesFile(&attr, &info);
2570 RtlFreeUnicodeString(&nt_replaced_name);
2571 if (!set_ntstatus( status )) return FALSE;
2573 if (info.FileAttributes & FILE_ATTRIBUTE_READONLY)
2575 SetLastError( ERROR_ACCESS_DENIED );
2576 return FALSE;
2580 * Open the replacement file for reading, writing, and deleting
2581 * (writing and deleting are needed when finished)
2583 if (!RtlDosPathNameToNtPathName_U( replacement, &nt_replacement_name, NULL, NULL ))
2585 SetLastError( ERROR_PATH_NOT_FOUND );
2586 return FALSE;
2588 attr.ObjectName = &nt_replacement_name;
2589 status = NtOpenFile( &hReplacement, GENERIC_READ | GENERIC_WRITE | DELETE | WRITE_DAC | SYNCHRONIZE,
2590 &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE );
2591 RtlFreeUnicodeString(&nt_replacement_name);
2592 if (!set_ntstatus( status )) return FALSE;
2593 NtClose( hReplacement );
2595 /* If the user wants a backup then that needs to be performed first */
2596 if (backup)
2598 if (!MoveFileExW( replaced, backup, MOVEFILE_REPLACE_EXISTING )) return FALSE;
2600 else
2602 /* ReplaceFile() can replace an open target. To do this, we need to move
2603 * it out of the way first. */
2604 WCHAR temp_path[MAX_PATH], temp_file[MAX_PATH];
2606 lstrcpynW( temp_path, replaced, ARRAY_SIZE( temp_path ) );
2607 PathRemoveFileSpecW( temp_path );
2608 if (!GetTempFileNameW( temp_path, L"rf", 0, temp_file ) ||
2609 !MoveFileExW( replaced, temp_file, MOVEFILE_REPLACE_EXISTING ))
2610 return FALSE;
2612 DeleteFileW( temp_file );
2616 * Now that the backup has been performed (if requested), copy the replacement
2617 * into place
2619 if (!MoveFileExW( replacement, replaced, 0 ))
2621 /* on failure we need to indicate whether a backup was made */
2622 if (!backup)
2623 SetLastError( ERROR_UNABLE_TO_MOVE_REPLACEMENT );
2624 else
2625 SetLastError( ERROR_UNABLE_TO_MOVE_REPLACEMENT_2 );
2626 return FALSE;
2628 return TRUE;
2632 /***********************************************************************
2633 * SearchPathA (kernelbase.@)
2635 DWORD WINAPI DECLSPEC_HOTPATCH SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext,
2636 DWORD buflen, LPSTR buffer, LPSTR *lastpart )
2638 WCHAR *pathW = NULL, *nameW, *extW = NULL;
2639 WCHAR bufferW[MAX_PATH];
2640 DWORD ret;
2642 if (!name)
2644 SetLastError( ERROR_INVALID_PARAMETER );
2645 return 0;
2648 if (!(nameW = file_name_AtoW( name, FALSE ))) return 0;
2649 if (path && !(pathW = file_name_AtoW( path, TRUE ))) return 0;
2650 if (ext && !(extW = file_name_AtoW( ext, TRUE )))
2652 RtlFreeHeap( GetProcessHeap(), 0, pathW );
2653 return 0;
2656 ret = SearchPathW( pathW, nameW, extW, MAX_PATH, bufferW, NULL );
2658 RtlFreeHeap( GetProcessHeap(), 0, pathW );
2659 RtlFreeHeap( GetProcessHeap(), 0, extW );
2661 if (!ret) return 0;
2662 if (ret > MAX_PATH)
2664 SetLastError( ERROR_FILENAME_EXCED_RANGE );
2665 return 0;
2667 ret = copy_filename_WtoA( bufferW, buffer, buflen );
2668 if (buflen > ret && lastpart) *lastpart = strrchr(buffer, '\\') + 1;
2669 return ret;
2673 /***********************************************************************
2674 * SearchPathW (kernelbase.@)
2676 DWORD WINAPI DECLSPEC_HOTPATCH SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext, DWORD buflen,
2677 LPWSTR buffer, LPWSTR *lastpart )
2679 DWORD ret = 0;
2680 WCHAR *name_ext;
2682 if (!name || !name[0])
2684 SetLastError( ERROR_INVALID_PARAMETER );
2685 return 0;
2688 /* If the name contains an explicit path, ignore the path */
2690 if (contains_path( name ))
2692 /* try first without extension */
2693 if (RtlDoesFileExists_U( name )) return GetFullPathNameW( name, buflen, buffer, lastpart );
2695 if ((name_ext = append_ext( name, ext )))
2697 if (RtlDoesFileExists_U( name_ext ))
2698 ret = GetFullPathNameW( name_ext, buflen, buffer, lastpart );
2699 RtlFreeHeap( GetProcessHeap(), 0, name_ext );
2702 else if (path && path[0]) /* search in the specified path */
2704 ret = RtlDosSearchPath_U( path, name, ext, buflen * sizeof(WCHAR),
2705 buffer, lastpart ) / sizeof(WCHAR);
2707 else /* search in active context and default path */
2709 WCHAR *dll_path = NULL, *name_ext = append_ext( name, ext );
2711 if (name_ext) name = name_ext;
2713 /* When file is found with activation context no attempt is made
2714 to check if it's really exist, path is returned only basing on context info. */
2715 if (find_actctx_dllpath( name, &dll_path ) == STATUS_SUCCESS)
2717 ret = lstrlenW( dll_path ) + lstrlenW( name ) + 1;
2719 /* count null termination char too */
2720 if (ret <= buflen)
2722 lstrcpyW( buffer, dll_path );
2723 lstrcatW( buffer, name );
2724 if (lastpart) *lastpart = buffer + lstrlenW( dll_path );
2725 ret--;
2727 else if (lastpart) *lastpart = NULL;
2728 RtlFreeHeap( GetProcessHeap(), 0, dll_path );
2730 else if (!RtlGetSearchPath( &dll_path ))
2732 ret = RtlDosSearchPath_U( dll_path, name, NULL, buflen * sizeof(WCHAR),
2733 buffer, lastpart ) / sizeof(WCHAR);
2734 RtlReleasePath( dll_path );
2736 RtlFreeHeap( GetProcessHeap(), 0, name_ext );
2739 if (!ret) SetLastError( ERROR_FILE_NOT_FOUND );
2740 else TRACE( "found %s\n", debugstr_w(buffer) );
2741 return ret;
2745 /***********************************************************************
2746 * SetCurrentDirectoryA (kernelbase.@)
2748 BOOL WINAPI DECLSPEC_HOTPATCH SetCurrentDirectoryA( LPCSTR dir )
2750 WCHAR *dirW;
2751 UNICODE_STRING strW;
2753 if (!(dirW = file_name_AtoW( dir, FALSE ))) return FALSE;
2754 RtlInitUnicodeString( &strW, dirW );
2755 return set_ntstatus( RtlSetCurrentDirectory_U( &strW ));
2759 /***********************************************************************
2760 * SetCurrentDirectoryW (kernelbase.@)
2762 BOOL WINAPI DECLSPEC_HOTPATCH SetCurrentDirectoryW( LPCWSTR dir )
2764 UNICODE_STRING dirW;
2766 RtlInitUnicodeString( &dirW, dir );
2767 return set_ntstatus( RtlSetCurrentDirectory_U( &dirW ));
2771 /**************************************************************************
2772 * SetFileApisToANSI (kernelbase.@)
2774 void WINAPI DECLSPEC_HOTPATCH SetFileApisToANSI(void)
2776 oem_file_apis = FALSE;
2780 /**************************************************************************
2781 * SetFileApisToOEM (kernelbase.@)
2783 void WINAPI DECLSPEC_HOTPATCH SetFileApisToOEM(void)
2785 oem_file_apis = TRUE;
2789 /**************************************************************************
2790 * SetFileAttributesA (kernelbase.@)
2792 BOOL WINAPI DECLSPEC_HOTPATCH SetFileAttributesA( LPCSTR name, DWORD attributes )
2794 WCHAR *nameW;
2796 if (!(nameW = file_name_AtoW( name, FALSE ))) return FALSE;
2797 return SetFileAttributesW( nameW, attributes );
2801 /**************************************************************************
2802 * SetFileAttributesW (kernelbase.@)
2804 BOOL WINAPI DECLSPEC_HOTPATCH SetFileAttributesW( LPCWSTR name, DWORD attributes )
2806 UNICODE_STRING nt_name;
2807 OBJECT_ATTRIBUTES attr;
2808 IO_STATUS_BLOCK io;
2809 NTSTATUS status;
2810 HANDLE handle;
2812 TRACE( "%s %x\n", debugstr_w(name), attributes );
2814 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
2816 SetLastError( ERROR_PATH_NOT_FOUND );
2817 return FALSE;
2820 attr.Length = sizeof(attr);
2821 attr.RootDirectory = 0;
2822 attr.Attributes = OBJ_CASE_INSENSITIVE;
2823 attr.ObjectName = &nt_name;
2824 attr.SecurityDescriptor = NULL;
2825 attr.SecurityQualityOfService = NULL;
2827 status = NtOpenFile( &handle, SYNCHRONIZE, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
2828 RtlFreeUnicodeString( &nt_name );
2830 if (status == STATUS_SUCCESS)
2832 FILE_BASIC_INFORMATION info;
2834 memset( &info, 0, sizeof(info) );
2835 info.FileAttributes = attributes | FILE_ATTRIBUTE_NORMAL; /* make sure it's not zero */
2836 status = NtSetInformationFile( handle, &io, &info, sizeof(info), FileBasicInformation );
2837 NtClose( handle );
2839 return set_ntstatus( status );
2843 /***********************************************************************
2844 * Wow64DisableWow64FsRedirection (kernelbase.@)
2846 BOOL WINAPI DECLSPEC_HOTPATCH Wow64DisableWow64FsRedirection( PVOID *old_value )
2848 return set_ntstatus( RtlWow64EnableFsRedirectionEx( TRUE, (ULONG *)old_value ));
2852 /***********************************************************************
2853 * Wow64RevertWow64FsRedirection (kernelbase.@)
2855 BOOL WINAPI DECLSPEC_HOTPATCH Wow64RevertWow64FsRedirection( PVOID old_value )
2857 return set_ntstatus( RtlWow64EnableFsRedirection( !old_value ));
2861 /***********************************************************************
2862 * Operations on file handles
2863 ***********************************************************************/
2866 /***********************************************************************
2867 * CancelIo (kernelbase.@)
2869 BOOL WINAPI DECLSPEC_HOTPATCH CancelIo( HANDLE handle )
2871 IO_STATUS_BLOCK io;
2873 NtCancelIoFile( handle, &io );
2874 return set_ntstatus( io.u.Status );
2878 /***********************************************************************
2879 * CancelIoEx (kernelbase.@)
2881 BOOL WINAPI DECLSPEC_HOTPATCH CancelIoEx( HANDLE handle, LPOVERLAPPED overlapped )
2883 IO_STATUS_BLOCK io;
2885 NtCancelIoFileEx( handle, (PIO_STATUS_BLOCK)overlapped, &io );
2886 return set_ntstatus( io.u.Status );
2890 /***********************************************************************
2891 * CancelSynchronousIo (kernelbase.@)
2893 BOOL WINAPI DECLSPEC_HOTPATCH CancelSynchronousIo( HANDLE thread )
2895 FIXME( "(%p): stub\n", thread );
2896 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
2897 return FALSE;
2901 /***********************************************************************
2902 * FlushFileBuffers (kernelbase.@)
2904 BOOL WINAPI DECLSPEC_HOTPATCH FlushFileBuffers( HANDLE file )
2906 IO_STATUS_BLOCK iosb;
2908 return set_ntstatus( NtFlushBuffersFile( file, &iosb ));
2912 /***********************************************************************
2913 * GetFileInformationByHandle (kernelbase.@)
2915 BOOL WINAPI DECLSPEC_HOTPATCH GetFileInformationByHandle( HANDLE file, BY_HANDLE_FILE_INFORMATION *info )
2917 FILE_FS_VOLUME_INFORMATION volume_info;
2918 FILE_ALL_INFORMATION all_info;
2919 IO_STATUS_BLOCK io;
2920 NTSTATUS status;
2922 status = NtQueryInformationFile( file, &io, &all_info, sizeof(all_info), FileAllInformation );
2923 if (status == STATUS_BUFFER_OVERFLOW) status = STATUS_SUCCESS;
2924 if (!set_ntstatus( status )) return FALSE;
2926 info->dwFileAttributes = all_info.BasicInformation.FileAttributes;
2927 info->ftCreationTime.dwHighDateTime = all_info.BasicInformation.CreationTime.u.HighPart;
2928 info->ftCreationTime.dwLowDateTime = all_info.BasicInformation.CreationTime.u.LowPart;
2929 info->ftLastAccessTime.dwHighDateTime = all_info.BasicInformation.LastAccessTime.u.HighPart;
2930 info->ftLastAccessTime.dwLowDateTime = all_info.BasicInformation.LastAccessTime.u.LowPart;
2931 info->ftLastWriteTime.dwHighDateTime = all_info.BasicInformation.LastWriteTime.u.HighPart;
2932 info->ftLastWriteTime.dwLowDateTime = all_info.BasicInformation.LastWriteTime.u.LowPart;
2933 info->dwVolumeSerialNumber = 0;
2934 info->nFileSizeHigh = all_info.StandardInformation.EndOfFile.u.HighPart;
2935 info->nFileSizeLow = all_info.StandardInformation.EndOfFile.u.LowPart;
2936 info->nNumberOfLinks = all_info.StandardInformation.NumberOfLinks;
2937 info->nFileIndexHigh = all_info.InternalInformation.IndexNumber.u.HighPart;
2938 info->nFileIndexLow = all_info.InternalInformation.IndexNumber.u.LowPart;
2940 status = NtQueryVolumeInformationFile( file, &io, &volume_info, sizeof(volume_info), FileFsVolumeInformation );
2941 if (status == STATUS_SUCCESS || status == STATUS_BUFFER_OVERFLOW)
2942 info->dwVolumeSerialNumber = volume_info.VolumeSerialNumber;
2944 return TRUE;
2948 /***********************************************************************
2949 * GetFileInformationByHandleEx (kernelbase.@)
2951 BOOL WINAPI DECLSPEC_HOTPATCH GetFileInformationByHandleEx( HANDLE handle, FILE_INFO_BY_HANDLE_CLASS class,
2952 LPVOID info, DWORD size )
2954 NTSTATUS status;
2955 IO_STATUS_BLOCK io;
2957 switch (class)
2959 case FileStreamInfo:
2960 case FileCompressionInfo:
2961 case FileRemoteProtocolInfo:
2962 case FileFullDirectoryInfo:
2963 case FileFullDirectoryRestartInfo:
2964 case FileStorageInfo:
2965 case FileAlignmentInfo:
2966 case FileIdExtdDirectoryInfo:
2967 case FileIdExtdDirectoryRestartInfo:
2968 FIXME( "%p, %u, %p, %u\n", handle, class, info, size );
2969 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
2970 return FALSE;
2972 case FileAttributeTagInfo:
2973 status = NtQueryInformationFile( handle, &io, info, size, FileAttributeTagInformation );
2974 break;
2976 case FileBasicInfo:
2977 status = NtQueryInformationFile( handle, &io, info, size, FileBasicInformation );
2978 break;
2980 case FileStandardInfo:
2981 status = NtQueryInformationFile( handle, &io, info, size, FileStandardInformation );
2982 break;
2984 case FileNameInfo:
2985 status = NtQueryInformationFile( handle, &io, info, size, FileNameInformation );
2986 break;
2988 case FileIdInfo:
2989 status = NtQueryInformationFile( handle, &io, info, size, FileIdInformation );
2990 break;
2992 case FileIdBothDirectoryRestartInfo:
2993 case FileIdBothDirectoryInfo:
2994 status = NtQueryDirectoryFile( handle, NULL, NULL, NULL, &io, info, size,
2995 FileIdBothDirectoryInformation, FALSE, NULL,
2996 (class == FileIdBothDirectoryRestartInfo) );
2997 break;
2999 case FileRenameInfo:
3000 case FileDispositionInfo:
3001 case FileAllocationInfo:
3002 case FileIoPriorityHintInfo:
3003 case FileEndOfFileInfo:
3004 default:
3005 SetLastError( ERROR_INVALID_PARAMETER );
3006 return FALSE;
3008 return set_ntstatus( status );
3012 /***********************************************************************
3013 * GetFileSize (kernelbase.@)
3015 DWORD WINAPI DECLSPEC_HOTPATCH GetFileSize( HANDLE file, LPDWORD size_high )
3017 LARGE_INTEGER size;
3019 if (!GetFileSizeEx( file, &size )) return INVALID_FILE_SIZE;
3020 if (size_high) *size_high = size.u.HighPart;
3021 if (size.u.LowPart == INVALID_FILE_SIZE) SetLastError( 0 );
3022 return size.u.LowPart;
3026 /***********************************************************************
3027 * GetFileSizeEx (kernelbase.@)
3029 BOOL WINAPI DECLSPEC_HOTPATCH GetFileSizeEx( HANDLE file, PLARGE_INTEGER size )
3031 FILE_STANDARD_INFORMATION info;
3032 IO_STATUS_BLOCK io;
3034 if (!set_ntstatus( NtQueryInformationFile( file, &io, &info, sizeof(info), FileStandardInformation )))
3035 return FALSE;
3037 *size = info.EndOfFile;
3038 return TRUE;
3042 /***********************************************************************
3043 * GetFileTime (kernelbase.@)
3045 BOOL WINAPI DECLSPEC_HOTPATCH GetFileTime( HANDLE file, FILETIME *creation,
3046 FILETIME *access, FILETIME *write )
3048 FILE_BASIC_INFORMATION info;
3049 IO_STATUS_BLOCK io;
3051 if (!set_ntstatus( NtQueryInformationFile( file, &io, &info, sizeof(info), FileBasicInformation )))
3052 return FALSE;
3054 if (creation)
3056 creation->dwHighDateTime = info.CreationTime.u.HighPart;
3057 creation->dwLowDateTime = info.CreationTime.u.LowPart;
3059 if (access)
3061 access->dwHighDateTime = info.LastAccessTime.u.HighPart;
3062 access->dwLowDateTime = info.LastAccessTime.u.LowPart;
3064 if (write)
3066 write->dwHighDateTime = info.LastWriteTime.u.HighPart;
3067 write->dwLowDateTime = info.LastWriteTime.u.LowPart;
3069 return TRUE;
3073 /***********************************************************************
3074 * GetFileType (kernelbase.@)
3076 DWORD WINAPI DECLSPEC_HOTPATCH GetFileType( HANDLE file )
3078 FILE_FS_DEVICE_INFORMATION info;
3079 IO_STATUS_BLOCK io;
3081 if (file == (HANDLE)STD_INPUT_HANDLE ||
3082 file == (HANDLE)STD_OUTPUT_HANDLE ||
3083 file == (HANDLE)STD_ERROR_HANDLE)
3084 file = GetStdHandle( (DWORD_PTR)file );
3086 if (!set_ntstatus( NtQueryVolumeInformationFile( file, &io, &info, sizeof(info),
3087 FileFsDeviceInformation )))
3088 return FILE_TYPE_UNKNOWN;
3090 switch (info.DeviceType)
3092 case FILE_DEVICE_NULL:
3093 case FILE_DEVICE_CONSOLE:
3094 case FILE_DEVICE_SERIAL_PORT:
3095 case FILE_DEVICE_PARALLEL_PORT:
3096 case FILE_DEVICE_TAPE:
3097 case FILE_DEVICE_UNKNOWN:
3098 return FILE_TYPE_CHAR;
3099 case FILE_DEVICE_NAMED_PIPE:
3100 return FILE_TYPE_PIPE;
3101 default:
3102 return FILE_TYPE_DISK;
3107 /***********************************************************************
3108 * GetOverlappedResult (kernelbase.@)
3110 BOOL WINAPI DECLSPEC_HOTPATCH GetOverlappedResult( HANDLE file, LPOVERLAPPED overlapped,
3111 LPDWORD result, BOOL wait )
3113 return GetOverlappedResultEx( file, overlapped, result, wait ? INFINITE : 0, FALSE );
3117 /***********************************************************************
3118 * GetOverlappedResultEx (kernelbase.@)
3120 BOOL WINAPI DECLSPEC_HOTPATCH GetOverlappedResultEx( HANDLE file, OVERLAPPED *overlapped,
3121 DWORD *result, DWORD timeout, BOOL alertable )
3123 NTSTATUS status;
3124 DWORD ret;
3126 TRACE( "(%p %p %p %u %d)\n", file, overlapped, result, timeout, alertable );
3128 status = overlapped->Internal;
3129 if (status == STATUS_PENDING)
3131 if (!timeout)
3133 SetLastError( ERROR_IO_INCOMPLETE );
3134 return FALSE;
3136 ret = WaitForSingleObjectEx( overlapped->hEvent ? overlapped->hEvent : file, timeout, alertable );
3137 if (ret == WAIT_FAILED)
3138 return FALSE;
3139 else if (ret)
3141 SetLastError( ret );
3142 return FALSE;
3145 status = overlapped->Internal;
3146 if (status == STATUS_PENDING) status = STATUS_SUCCESS;
3149 *result = overlapped->InternalHigh;
3150 return set_ntstatus( status );
3154 /**************************************************************************
3155 * LockFile (kernelbase.@)
3157 BOOL WINAPI DECLSPEC_HOTPATCH LockFile( HANDLE file, DWORD offset_low, DWORD offset_high,
3158 DWORD count_low, DWORD count_high )
3160 LARGE_INTEGER count, offset;
3162 TRACE( "%p %x%08x %x%08x\n", file, offset_high, offset_low, count_high, count_low );
3164 count.u.LowPart = count_low;
3165 count.u.HighPart = count_high;
3166 offset.u.LowPart = offset_low;
3167 offset.u.HighPart = offset_high;
3168 return set_ntstatus( NtLockFile( file, 0, NULL, NULL, NULL, &offset, &count, NULL, TRUE, TRUE ));
3172 /**************************************************************************
3173 * LockFileEx (kernelbase.@)
3175 BOOL WINAPI DECLSPEC_HOTPATCH LockFileEx( HANDLE file, DWORD flags, DWORD reserved,
3176 DWORD count_low, DWORD count_high, LPOVERLAPPED overlapped )
3178 LARGE_INTEGER count, offset;
3179 LPVOID cvalue = NULL;
3181 if (reserved)
3183 SetLastError( ERROR_INVALID_PARAMETER );
3184 return FALSE;
3187 TRACE( "%p %x%08x %x%08x flags %x\n",
3188 file, overlapped->u.s.OffsetHigh, overlapped->u.s.Offset, count_high, count_low, flags );
3190 count.u.LowPart = count_low;
3191 count.u.HighPart = count_high;
3192 offset.u.LowPart = overlapped->u.s.Offset;
3193 offset.u.HighPart = overlapped->u.s.OffsetHigh;
3195 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
3197 return set_ntstatus( NtLockFile( file, overlapped->hEvent, NULL, cvalue,
3198 NULL, &offset, &count, NULL,
3199 flags & LOCKFILE_FAIL_IMMEDIATELY,
3200 flags & LOCKFILE_EXCLUSIVE_LOCK ));
3204 /***********************************************************************
3205 * OpenFileById (kernelbase.@)
3207 HANDLE WINAPI DECLSPEC_HOTPATCH OpenFileById( HANDLE handle, LPFILE_ID_DESCRIPTOR id, DWORD access,
3208 DWORD share, LPSECURITY_ATTRIBUTES sec_attr, DWORD flags )
3210 UINT options;
3211 HANDLE result;
3212 OBJECT_ATTRIBUTES attr;
3213 IO_STATUS_BLOCK io;
3214 UNICODE_STRING objectName;
3216 if (!id)
3218 SetLastError( ERROR_INVALID_PARAMETER );
3219 return INVALID_HANDLE_VALUE;
3222 options = FILE_OPEN_BY_FILE_ID;
3223 if (flags & FILE_FLAG_BACKUP_SEMANTICS)
3224 options |= FILE_OPEN_FOR_BACKUP_INTENT;
3225 else
3226 options |= FILE_NON_DIRECTORY_FILE;
3227 if (flags & FILE_FLAG_NO_BUFFERING) options |= FILE_NO_INTERMEDIATE_BUFFERING;
3228 if (!(flags & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_NONALERT;
3229 if (flags & FILE_FLAG_RANDOM_ACCESS) options |= FILE_RANDOM_ACCESS;
3230 flags &= FILE_ATTRIBUTE_VALID_FLAGS;
3232 objectName.Length = sizeof(ULONGLONG);
3233 objectName.Buffer = (WCHAR *)&id->u.FileId;
3234 attr.Length = sizeof(attr);
3235 attr.RootDirectory = handle;
3236 attr.Attributes = 0;
3237 attr.ObjectName = &objectName;
3238 attr.SecurityDescriptor = sec_attr ? sec_attr->lpSecurityDescriptor : NULL;
3239 attr.SecurityQualityOfService = NULL;
3240 if (sec_attr && sec_attr->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
3242 if (!set_ntstatus( NtCreateFile( &result, access | SYNCHRONIZE, &attr, &io, NULL, flags,
3243 share, OPEN_EXISTING, options, NULL, 0 )))
3244 return INVALID_HANDLE_VALUE;
3245 return result;
3249 /***********************************************************************
3250 * ReOpenFile (kernelbase.@)
3252 HANDLE WINAPI DECLSPEC_HOTPATCH ReOpenFile( HANDLE handle, DWORD access, DWORD sharing, DWORD attributes )
3254 SECURITY_QUALITY_OF_SERVICE qos;
3255 OBJECT_ATTRIBUTES attr;
3256 UNICODE_STRING empty = { 0 };
3257 IO_STATUS_BLOCK io;
3258 NTSTATUS status;
3259 HANDLE file;
3261 TRACE("handle %p, access %#x, sharing %#x, attributes %#x.\n", handle, access, sharing, attributes);
3263 if (attributes & 0x7ffff) /* FILE_ATTRIBUTE_* flags are invalid */
3265 SetLastError(ERROR_INVALID_PARAMETER);
3266 return INVALID_HANDLE_VALUE;
3269 if (attributes & FILE_FLAG_DELETE_ON_CLOSE)
3270 access |= DELETE;
3272 InitializeObjectAttributes( &attr, &empty, OBJ_CASE_INSENSITIVE, handle, NULL );
3273 if (attributes & SECURITY_SQOS_PRESENT)
3275 qos.Length = sizeof(qos);
3276 qos.ImpersonationLevel = (attributes >> 16) & 0x3;
3277 qos.ContextTrackingMode = attributes & SECURITY_CONTEXT_TRACKING ? SECURITY_DYNAMIC_TRACKING : SECURITY_STATIC_TRACKING;
3278 qos.EffectiveOnly = (attributes & SECURITY_EFFECTIVE_ONLY) != 0;
3279 attr.SecurityQualityOfService = &qos;
3282 status = NtCreateFile( &file, access | SYNCHRONIZE | FILE_READ_ATTRIBUTES, &attr, &io, NULL,
3283 0, sharing, FILE_OPEN, get_nt_file_options( attributes ), NULL, 0 );
3284 if (!set_ntstatus( status ))
3285 return INVALID_HANDLE_VALUE;
3286 return file;
3290 static void WINAPI invoke_completion( void *context, IO_STATUS_BLOCK *io, ULONG res )
3292 LPOVERLAPPED_COMPLETION_ROUTINE completion = context;
3293 completion( io->u.Status, io->Information, (LPOVERLAPPED)io );
3296 /****************************************************************************
3297 * ReadDirectoryChangesW (kernelbase.@)
3299 BOOL WINAPI DECLSPEC_HOTPATCH ReadDirectoryChangesW( HANDLE handle, LPVOID buffer, DWORD len,
3300 BOOL subtree, DWORD filter, LPDWORD returned,
3301 LPOVERLAPPED overlapped,
3302 LPOVERLAPPED_COMPLETION_ROUTINE completion )
3304 OVERLAPPED ov, *pov;
3305 IO_STATUS_BLOCK *ios;
3306 NTSTATUS status;
3307 LPVOID cvalue = NULL;
3309 TRACE( "%p %p %08x %d %08x %p %p %p\n",
3310 handle, buffer, len, subtree, filter, returned, overlapped, completion );
3312 if (!overlapped)
3314 memset( &ov, 0, sizeof ov );
3315 ov.hEvent = CreateEventW( NULL, 0, 0, NULL );
3316 pov = &ov;
3318 else
3320 pov = overlapped;
3321 if (completion) cvalue = completion;
3322 else if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
3325 ios = (PIO_STATUS_BLOCK)pov;
3326 ios->u.Status = STATUS_PENDING;
3328 status = NtNotifyChangeDirectoryFile( handle, completion && overlapped ? NULL : pov->hEvent,
3329 completion && overlapped ? invoke_completion : NULL,
3330 cvalue, ios, buffer, len, filter, subtree );
3331 if (status == STATUS_PENDING)
3333 if (overlapped) return TRUE;
3334 WaitForSingleObjectEx( ov.hEvent, INFINITE, TRUE );
3335 if (returned) *returned = ios->Information;
3336 status = ios->u.Status;
3338 if (!overlapped) CloseHandle( ov.hEvent );
3339 return set_ntstatus( status );
3343 /***********************************************************************
3344 * ReadFile (kernelbase.@)
3346 BOOL WINAPI DECLSPEC_HOTPATCH ReadFile( HANDLE file, LPVOID buffer, DWORD count,
3347 LPDWORD result, LPOVERLAPPED overlapped )
3349 LARGE_INTEGER offset;
3350 PLARGE_INTEGER poffset = NULL;
3351 IO_STATUS_BLOCK iosb;
3352 PIO_STATUS_BLOCK io_status = &iosb;
3353 HANDLE event = 0;
3354 NTSTATUS status;
3355 LPVOID cvalue = NULL;
3357 TRACE( "%p %p %d %p %p\n", file, buffer, count, result, overlapped );
3359 if (result) *result = 0;
3361 if (overlapped)
3363 offset.u.LowPart = overlapped->u.s.Offset;
3364 offset.u.HighPart = overlapped->u.s.OffsetHigh;
3365 poffset = &offset;
3366 event = overlapped->hEvent;
3367 io_status = (PIO_STATUS_BLOCK)overlapped;
3368 if (((ULONG_PTR)event & 1) == 0) cvalue = overlapped;
3370 else io_status->Information = 0;
3371 io_status->u.Status = STATUS_PENDING;
3373 status = NtReadFile( file, event, NULL, cvalue, io_status, buffer, count, poffset, NULL);
3375 if (status == STATUS_PENDING && !overlapped)
3377 WaitForSingleObject( file, INFINITE );
3378 status = io_status->u.Status;
3381 if (result) *result = overlapped && status ? 0 : io_status->Information;
3383 if (status == STATUS_END_OF_FILE)
3385 if (overlapped != NULL)
3387 SetLastError( RtlNtStatusToDosError(status) );
3388 return FALSE;
3391 else if (status && status != STATUS_TIMEOUT)
3393 SetLastError( RtlNtStatusToDosError(status) );
3394 return FALSE;
3396 return TRUE;
3400 /***********************************************************************
3401 * ReadFileEx (kernelbase.@)
3403 BOOL WINAPI DECLSPEC_HOTPATCH ReadFileEx( HANDLE file, LPVOID buffer, DWORD count, LPOVERLAPPED overlapped,
3404 LPOVERLAPPED_COMPLETION_ROUTINE completion )
3406 PIO_STATUS_BLOCK io;
3407 LARGE_INTEGER offset;
3408 NTSTATUS status;
3410 TRACE( "(file=%p, buffer=%p, bytes=%u, ovl=%p, ovl_fn=%p)\n",
3411 file, buffer, count, overlapped, completion );
3413 if (!overlapped)
3415 SetLastError( ERROR_INVALID_PARAMETER );
3416 return FALSE;
3418 offset.u.LowPart = overlapped->u.s.Offset;
3419 offset.u.HighPart = overlapped->u.s.OffsetHigh;
3420 io = (PIO_STATUS_BLOCK)overlapped;
3421 io->u.Status = STATUS_PENDING;
3422 io->Information = 0;
3424 status = NtReadFile( file, NULL, read_write_apc, completion, io, buffer, count, &offset, NULL);
3425 if (status == STATUS_PENDING) return TRUE;
3426 return set_ntstatus( status );
3430 /***********************************************************************
3431 * ReadFileScatter (kernelbase.@)
3433 BOOL WINAPI DECLSPEC_HOTPATCH ReadFileScatter( HANDLE file, FILE_SEGMENT_ELEMENT *segments, DWORD count,
3434 LPDWORD reserved, LPOVERLAPPED overlapped )
3436 PIO_STATUS_BLOCK io;
3437 LARGE_INTEGER offset;
3438 void *cvalue = NULL;
3440 TRACE( "(%p %p %u %p)\n", file, segments, count, overlapped );
3442 offset.u.LowPart = overlapped->u.s.Offset;
3443 offset.u.HighPart = overlapped->u.s.OffsetHigh;
3444 if (!((ULONG_PTR)overlapped->hEvent & 1)) cvalue = overlapped;
3445 io = (PIO_STATUS_BLOCK)overlapped;
3446 io->u.Status = STATUS_PENDING;
3447 io->Information = 0;
3449 return set_ntstatus( NtReadFileScatter( file, overlapped->hEvent, NULL, cvalue, io,
3450 segments, count, &offset, NULL ));
3454 /***********************************************************************
3455 * RemoveDirectoryA (kernelbase.@)
3457 BOOL WINAPI DECLSPEC_HOTPATCH RemoveDirectoryA( LPCSTR path )
3459 WCHAR *pathW;
3461 if (!(pathW = file_name_AtoW( path, FALSE ))) return FALSE;
3462 return RemoveDirectoryW( pathW );
3466 /***********************************************************************
3467 * RemoveDirectoryW (kernelbase.@)
3469 BOOL WINAPI DECLSPEC_HOTPATCH RemoveDirectoryW( LPCWSTR path )
3471 OBJECT_ATTRIBUTES attr;
3472 UNICODE_STRING nt_name;
3473 IO_STATUS_BLOCK io;
3474 NTSTATUS status;
3475 HANDLE handle;
3477 TRACE( "%s\n", debugstr_w(path) );
3479 status = RtlDosPathNameToNtPathName_U_WithStatus( path, &nt_name, NULL, NULL );
3480 if (!set_ntstatus( status )) return FALSE;
3482 InitializeObjectAttributes( &attr, &nt_name, OBJ_CASE_INSENSITIVE, 0, NULL );
3483 status = NtOpenFile( &handle, DELETE | SYNCHRONIZE, &attr, &io,
3484 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
3485 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
3486 RtlFreeUnicodeString( &nt_name );
3488 if (!status)
3490 FILE_DISPOSITION_INFORMATION info = { TRUE };
3491 status = NtSetInformationFile( handle, &io, &info, sizeof(info), FileDispositionInformation );
3492 NtClose( handle );
3494 return set_ntstatus( status );
3498 /**************************************************************************
3499 * SetEndOfFile (kernelbase.@)
3501 BOOL WINAPI DECLSPEC_HOTPATCH SetEndOfFile( HANDLE file )
3503 FILE_POSITION_INFORMATION pos;
3504 FILE_END_OF_FILE_INFORMATION eof;
3505 IO_STATUS_BLOCK io;
3506 NTSTATUS status;
3508 if (!(status = NtQueryInformationFile( file, &io, &pos, sizeof(pos), FilePositionInformation )))
3510 eof.EndOfFile = pos.CurrentByteOffset;
3511 status = NtSetInformationFile( file, &io, &eof, sizeof(eof), FileEndOfFileInformation );
3513 return set_ntstatus( status );
3517 /***********************************************************************
3518 * SetFileInformationByHandle (kernelbase.@)
3520 BOOL WINAPI DECLSPEC_HOTPATCH SetFileInformationByHandle( HANDLE file, FILE_INFO_BY_HANDLE_CLASS class,
3521 void *info, DWORD size )
3523 NTSTATUS status;
3524 IO_STATUS_BLOCK io;
3526 TRACE( "%p %u %p %u\n", file, class, info, size );
3528 switch (class)
3530 case FileNameInfo:
3531 case FileAllocationInfo:
3532 case FileStreamInfo:
3533 case FileIdBothDirectoryInfo:
3534 case FileIdBothDirectoryRestartInfo:
3535 case FileFullDirectoryInfo:
3536 case FileFullDirectoryRestartInfo:
3537 case FileStorageInfo:
3538 case FileAlignmentInfo:
3539 case FileIdInfo:
3540 case FileIdExtdDirectoryInfo:
3541 case FileIdExtdDirectoryRestartInfo:
3542 FIXME( "%p, %u, %p, %u\n", file, class, info, size );
3543 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
3544 return FALSE;
3546 case FileEndOfFileInfo:
3547 status = NtSetInformationFile( file, &io, info, size, FileEndOfFileInformation );
3548 break;
3549 case FileBasicInfo:
3550 status = NtSetInformationFile( file, &io, info, size, FileBasicInformation );
3551 break;
3552 case FileDispositionInfo:
3553 status = NtSetInformationFile( file, &io, info, size, FileDispositionInformation );
3554 break;
3555 case FileIoPriorityHintInfo:
3556 status = NtSetInformationFile( file, &io, info, size, FileIoPriorityHintInformation );
3557 break;
3558 case FileRenameInfo:
3560 FILE_RENAME_INFORMATION *rename_info;
3561 UNICODE_STRING nt_name;
3562 ULONG size;
3564 if ((status = RtlDosPathNameToNtPathName_U_WithStatus( ((FILE_RENAME_INFORMATION *)info)->FileName,
3565 &nt_name, NULL, NULL )))
3566 break;
3568 size = sizeof(*rename_info) + nt_name.Length;
3569 if ((rename_info = HeapAlloc( GetProcessHeap(), 0, size )))
3571 memcpy( rename_info, info, sizeof(*rename_info) );
3572 memcpy( rename_info->FileName, nt_name.Buffer, nt_name.Length + sizeof(WCHAR) );
3573 rename_info->FileNameLength = nt_name.Length;
3574 status = NtSetInformationFile( file, &io, rename_info, size, FileRenameInformation );
3575 HeapFree( GetProcessHeap(), 0, rename_info );
3577 RtlFreeUnicodeString( &nt_name );
3578 break;
3580 case FileStandardInfo:
3581 case FileCompressionInfo:
3582 case FileAttributeTagInfo:
3583 case FileRemoteProtocolInfo:
3584 default:
3585 SetLastError( ERROR_INVALID_PARAMETER );
3586 return FALSE;
3588 return set_ntstatus( status );
3592 /***********************************************************************
3593 * SetFilePointer (kernelbase.@)
3595 DWORD WINAPI DECLSPEC_HOTPATCH SetFilePointer( HANDLE file, LONG distance, LONG *highword, DWORD method )
3597 LARGE_INTEGER dist, newpos;
3599 if (highword)
3601 dist.u.LowPart = distance;
3602 dist.u.HighPart = *highword;
3604 else dist.QuadPart = distance;
3606 if (!SetFilePointerEx( file, dist, &newpos, method )) return INVALID_SET_FILE_POINTER;
3608 if (highword) *highword = newpos.u.HighPart;
3609 if (newpos.u.LowPart == INVALID_SET_FILE_POINTER) SetLastError( 0 );
3610 return newpos.u.LowPart;
3614 /***********************************************************************
3615 * SetFilePointerEx (kernelbase.@)
3617 BOOL WINAPI DECLSPEC_HOTPATCH SetFilePointerEx( HANDLE file, LARGE_INTEGER distance,
3618 LARGE_INTEGER *newpos, DWORD method )
3620 LONGLONG pos;
3621 IO_STATUS_BLOCK io;
3622 FILE_POSITION_INFORMATION info;
3623 FILE_END_OF_FILE_INFORMATION eof;
3625 switch(method)
3627 case FILE_BEGIN:
3628 pos = distance.QuadPart;
3629 break;
3630 case FILE_CURRENT:
3631 if (NtQueryInformationFile( file, &io, &info, sizeof(info), FilePositionInformation ))
3632 goto error;
3633 pos = info.CurrentByteOffset.QuadPart + distance.QuadPart;
3634 break;
3635 case FILE_END:
3636 if (NtQueryInformationFile( file, &io, &eof, sizeof(eof), FileEndOfFileInformation ))
3637 goto error;
3638 pos = eof.EndOfFile.QuadPart + distance.QuadPart;
3639 break;
3640 default:
3641 SetLastError( ERROR_INVALID_PARAMETER );
3642 return FALSE;
3645 if (pos < 0)
3647 SetLastError( ERROR_NEGATIVE_SEEK );
3648 return FALSE;
3651 info.CurrentByteOffset.QuadPart = pos;
3652 if (!NtSetInformationFile( file, &io, &info, sizeof(info), FilePositionInformation ))
3654 if (newpos) newpos->QuadPart = pos;
3655 return TRUE;
3658 error:
3659 return set_ntstatus( io.u.Status );
3663 /***********************************************************************
3664 * SetFileTime (kernelbase.@)
3666 BOOL WINAPI DECLSPEC_HOTPATCH SetFileTime( HANDLE file, const FILETIME *ctime,
3667 const FILETIME *atime, const FILETIME *mtime )
3669 FILE_BASIC_INFORMATION info;
3670 IO_STATUS_BLOCK io;
3672 memset( &info, 0, sizeof(info) );
3673 if (ctime)
3675 info.CreationTime.u.HighPart = ctime->dwHighDateTime;
3676 info.CreationTime.u.LowPart = ctime->dwLowDateTime;
3678 if (atime)
3680 info.LastAccessTime.u.HighPart = atime->dwHighDateTime;
3681 info.LastAccessTime.u.LowPart = atime->dwLowDateTime;
3683 if (mtime)
3685 info.LastWriteTime.u.HighPart = mtime->dwHighDateTime;
3686 info.LastWriteTime.u.LowPart = mtime->dwLowDateTime;
3689 return set_ntstatus( NtSetInformationFile( file, &io, &info, sizeof(info), FileBasicInformation ));
3693 /***********************************************************************
3694 * SetFileValidData (kernelbase.@)
3696 BOOL WINAPI DECLSPEC_HOTPATCH SetFileValidData( HANDLE file, LONGLONG length )
3698 FILE_VALID_DATA_LENGTH_INFORMATION info;
3699 IO_STATUS_BLOCK io;
3701 info.ValidDataLength.QuadPart = length;
3702 return set_ntstatus( NtSetInformationFile( file, &io, &info, sizeof(info),
3703 FileValidDataLengthInformation ));
3707 /**************************************************************************
3708 * UnlockFile (kernelbase.@)
3710 BOOL WINAPI DECLSPEC_HOTPATCH UnlockFile( HANDLE file, DWORD offset_low, DWORD offset_high,
3711 DWORD count_low, DWORD count_high )
3713 LARGE_INTEGER count, offset;
3715 count.u.LowPart = count_low;
3716 count.u.HighPart = count_high;
3717 offset.u.LowPart = offset_low;
3718 offset.u.HighPart = offset_high;
3719 return set_ntstatus( NtUnlockFile( file, NULL, &offset, &count, NULL ));
3723 /**************************************************************************
3724 * UnlockFileEx (kernelbase.@)
3726 BOOL WINAPI DECLSPEC_HOTPATCH UnlockFileEx( HANDLE file, DWORD reserved,
3727 DWORD count_low, DWORD count_high, LPOVERLAPPED overlapped )
3729 if (reserved)
3731 SetLastError( ERROR_INVALID_PARAMETER );
3732 return FALSE;
3734 if (overlapped->hEvent) FIXME("Unimplemented overlapped operation\n");
3736 return UnlockFile( file, overlapped->u.s.Offset, overlapped->u.s.OffsetHigh, count_low, count_high );
3740 /***********************************************************************
3741 * WriteFile (kernelbase.@)
3743 BOOL WINAPI DECLSPEC_HOTPATCH WriteFile( HANDLE file, LPCVOID buffer, DWORD count,
3744 LPDWORD result, LPOVERLAPPED overlapped )
3746 HANDLE event = NULL;
3747 LARGE_INTEGER offset;
3748 PLARGE_INTEGER poffset = NULL;
3749 NTSTATUS status;
3750 IO_STATUS_BLOCK iosb;
3751 PIO_STATUS_BLOCK piosb = &iosb;
3752 LPVOID cvalue = NULL;
3754 TRACE( "%p %p %d %p %p\n", file, buffer, count, result, overlapped );
3756 if (overlapped)
3758 offset.u.LowPart = overlapped->u.s.Offset;
3759 offset.u.HighPart = overlapped->u.s.OffsetHigh;
3760 poffset = &offset;
3761 event = overlapped->hEvent;
3762 piosb = (PIO_STATUS_BLOCK)overlapped;
3763 if (((ULONG_PTR)event & 1) == 0) cvalue = overlapped;
3765 else piosb->Information = 0;
3766 piosb->u.Status = STATUS_PENDING;
3768 status = NtWriteFile( file, event, NULL, cvalue, piosb, buffer, count, poffset, NULL );
3770 if (status == STATUS_PENDING && !overlapped)
3772 WaitForSingleObject( file, INFINITE );
3773 status = piosb->u.Status;
3776 if (result) *result = overlapped && status ? 0 : piosb->Information;
3778 if (status && status != STATUS_TIMEOUT)
3780 SetLastError( RtlNtStatusToDosError(status) );
3781 return FALSE;
3783 return TRUE;
3787 /***********************************************************************
3788 * WriteFileEx (kernelbase.@)
3790 BOOL WINAPI DECLSPEC_HOTPATCH WriteFileEx( HANDLE file, LPCVOID buffer,
3791 DWORD count, LPOVERLAPPED overlapped,
3792 LPOVERLAPPED_COMPLETION_ROUTINE completion )
3794 LARGE_INTEGER offset;
3795 NTSTATUS status;
3796 PIO_STATUS_BLOCK io;
3798 TRACE( "%p %p %d %p %p\n", file, buffer, count, overlapped, completion );
3800 if (!overlapped)
3802 SetLastError( ERROR_INVALID_PARAMETER );
3803 return FALSE;
3805 offset.u.LowPart = overlapped->u.s.Offset;
3806 offset.u.HighPart = overlapped->u.s.OffsetHigh;
3808 io = (PIO_STATUS_BLOCK)overlapped;
3809 io->u.Status = STATUS_PENDING;
3810 io->Information = 0;
3812 status = NtWriteFile( file, NULL, read_write_apc, completion, io, buffer, count, &offset, NULL );
3813 if (status == STATUS_PENDING) return TRUE;
3814 return set_ntstatus( status );
3818 /***********************************************************************
3819 * WriteFileGather (kernelbase.@)
3821 BOOL WINAPI DECLSPEC_HOTPATCH WriteFileGather( HANDLE file, FILE_SEGMENT_ELEMENT *segments, DWORD count,
3822 LPDWORD reserved, LPOVERLAPPED overlapped )
3824 PIO_STATUS_BLOCK io;
3825 LARGE_INTEGER offset;
3826 void *cvalue = NULL;
3828 TRACE( "%p %p %u %p\n", file, segments, count, overlapped );
3830 offset.u.LowPart = overlapped->u.s.Offset;
3831 offset.u.HighPart = overlapped->u.s.OffsetHigh;
3832 if (!((ULONG_PTR)overlapped->hEvent & 1)) cvalue = overlapped;
3833 io = (PIO_STATUS_BLOCK)overlapped;
3834 io->u.Status = STATUS_PENDING;
3835 io->Information = 0;
3837 return set_ntstatus( NtWriteFileGather( file, overlapped->hEvent, NULL, cvalue,
3838 io, segments, count, &offset, NULL ));
3842 /***********************************************************************
3843 * Operations on file times
3844 ***********************************************************************/
3847 /*********************************************************************
3848 * CompareFileTime (kernelbase.@)
3850 INT WINAPI DECLSPEC_HOTPATCH CompareFileTime( const FILETIME *x, const FILETIME *y )
3852 if (!x || !y) return -1;
3853 if (x->dwHighDateTime > y->dwHighDateTime) return 1;
3854 if (x->dwHighDateTime < y->dwHighDateTime) return -1;
3855 if (x->dwLowDateTime > y->dwLowDateTime) return 1;
3856 if (x->dwLowDateTime < y->dwLowDateTime) return -1;
3857 return 0;
3861 /*********************************************************************
3862 * FileTimeToLocalFileTime (kernelbase.@)
3864 BOOL WINAPI DECLSPEC_HOTPATCH FileTimeToLocalFileTime( const FILETIME *utc, FILETIME *local )
3866 return set_ntstatus( RtlSystemTimeToLocalTime( (const LARGE_INTEGER *)utc, (LARGE_INTEGER *)local ));
3870 /*********************************************************************
3871 * FileTimeToSystemTime (kernelbase.@)
3873 BOOL WINAPI DECLSPEC_HOTPATCH FileTimeToSystemTime( const FILETIME *ft, SYSTEMTIME *systime )
3875 TIME_FIELDS tf;
3877 RtlTimeToTimeFields( (const LARGE_INTEGER *)ft, &tf );
3878 systime->wYear = tf.Year;
3879 systime->wMonth = tf.Month;
3880 systime->wDay = tf.Day;
3881 systime->wHour = tf.Hour;
3882 systime->wMinute = tf.Minute;
3883 systime->wSecond = tf.Second;
3884 systime->wMilliseconds = tf.Milliseconds;
3885 systime->wDayOfWeek = tf.Weekday;
3886 return TRUE;
3890 /*********************************************************************
3891 * GetLocalTime (kernelbase.@)
3893 void WINAPI DECLSPEC_HOTPATCH GetLocalTime( SYSTEMTIME *systime )
3895 LARGE_INTEGER ft, ft2;
3897 NtQuerySystemTime( &ft );
3898 RtlSystemTimeToLocalTime( &ft, &ft2 );
3899 FileTimeToSystemTime( (FILETIME *)&ft2, systime );
3903 /*********************************************************************
3904 * GetSystemTime (kernelbase.@)
3906 void WINAPI DECLSPEC_HOTPATCH GetSystemTime( SYSTEMTIME *systime )
3908 LARGE_INTEGER ft;
3910 NtQuerySystemTime( &ft );
3911 FileTimeToSystemTime( (FILETIME *)&ft, systime );
3915 /***********************************************************************
3916 * GetSystemTimeAdjustment (kernelbase.@)
3918 BOOL WINAPI DECLSPEC_HOTPATCH GetSystemTimeAdjustment( DWORD *adjust, DWORD *increment, BOOL *disabled )
3920 SYSTEM_TIME_ADJUSTMENT_QUERY st;
3921 ULONG len;
3923 if (!set_ntstatus( NtQuerySystemInformation( SystemTimeAdjustmentInformation, &st, sizeof(st), &len )))
3924 return FALSE;
3925 *adjust = st.TimeAdjustment;
3926 *increment = st.TimeIncrement;
3927 *disabled = st.TimeAdjustmentDisabled;
3928 return TRUE;
3932 /***********************************************************************
3933 * GetSystemTimeAsFileTime (kernelbase.@)
3935 void WINAPI DECLSPEC_HOTPATCH GetSystemTimeAsFileTime( FILETIME *time )
3937 NtQuerySystemTime( (LARGE_INTEGER *)time );
3941 /***********************************************************************
3942 * GetSystemTimePreciseAsFileTime (kernelbase.@)
3944 void WINAPI DECLSPEC_HOTPATCH GetSystemTimePreciseAsFileTime( FILETIME *time )
3946 LARGE_INTEGER t;
3948 t.QuadPart = RtlGetSystemTimePrecise();
3949 time->dwLowDateTime = t.u.LowPart;
3950 time->dwHighDateTime = t.u.HighPart;
3954 /*********************************************************************
3955 * LocalFileTimeToFileTime (kernelbase.@)
3957 BOOL WINAPI DECLSPEC_HOTPATCH LocalFileTimeToFileTime( const FILETIME *local, FILETIME *utc )
3959 return set_ntstatus( RtlLocalTimeToSystemTime( (const LARGE_INTEGER *)local, (LARGE_INTEGER *)utc ));
3963 /***********************************************************************
3964 * SetLocalTime (kernelbase.@)
3966 BOOL WINAPI DECLSPEC_HOTPATCH SetLocalTime( const SYSTEMTIME *systime )
3968 FILETIME ft;
3969 LARGE_INTEGER st;
3971 if (!SystemTimeToFileTime( systime, &ft )) return FALSE;
3972 RtlLocalTimeToSystemTime( (LARGE_INTEGER *)&ft, &st );
3973 return set_ntstatus( NtSetSystemTime( &st, NULL ));
3977 /***********************************************************************
3978 * SetSystemTime (kernelbase.@)
3980 BOOL WINAPI DECLSPEC_HOTPATCH SetSystemTime( const SYSTEMTIME *systime )
3982 FILETIME ft;
3984 if (!SystemTimeToFileTime( systime, &ft )) return FALSE;
3985 return set_ntstatus( NtSetSystemTime( (LARGE_INTEGER *)&ft, NULL ));
3989 /***********************************************************************
3990 * SetSystemTimeAdjustment (kernelbase.@)
3992 BOOL WINAPI DECLSPEC_HOTPATCH SetSystemTimeAdjustment( DWORD adjust, BOOL disabled )
3994 SYSTEM_TIME_ADJUSTMENT st;
3996 st.TimeAdjustment = adjust;
3997 st.TimeAdjustmentDisabled = disabled;
3998 return set_ntstatus( NtSetSystemInformation( SystemTimeAdjustmentInformation, &st, sizeof(st) ));
4002 /*********************************************************************
4003 * SystemTimeToFileTime (kernelbase.@)
4005 BOOL WINAPI DECLSPEC_HOTPATCH SystemTimeToFileTime( const SYSTEMTIME *systime, FILETIME *ft )
4007 TIME_FIELDS tf;
4009 tf.Year = systime->wYear;
4010 tf.Month = systime->wMonth;
4011 tf.Day = systime->wDay;
4012 tf.Hour = systime->wHour;
4013 tf.Minute = systime->wMinute;
4014 tf.Second = systime->wSecond;
4015 tf.Milliseconds = systime->wMilliseconds;
4016 if (RtlTimeFieldsToTime( &tf, (LARGE_INTEGER *)ft )) return TRUE;
4017 SetLastError( ERROR_INVALID_PARAMETER );
4018 return FALSE;
4022 /***********************************************************************
4023 * I/O controls
4024 ***********************************************************************/
4027 static void dump_dcb( const DCB *dcb )
4029 TRACE( "size=%d rate=%d fParity=%d Parity=%d stopbits=%d %sIXON %sIXOFF CTS=%d RTS=%d DSR=%d DTR=%d %sCRTSCTS\n",
4030 dcb->ByteSize, dcb->BaudRate, dcb->fParity, dcb->Parity,
4031 (dcb->StopBits == ONESTOPBIT) ? 1 : (dcb->StopBits == TWOSTOPBITS) ? 2 : 0,
4032 dcb->fOutX ? "" : "~", dcb->fInX ? "" : "~",
4033 dcb->fOutxCtsFlow, dcb->fRtsControl, dcb->fOutxDsrFlow, dcb->fDtrControl,
4034 (dcb->fOutxCtsFlow || dcb->fRtsControl == RTS_CONTROL_HANDSHAKE) ? "" : "~" );
4037 /*****************************************************************************
4038 * ClearCommBreak (kernelbase.@)
4040 BOOL WINAPI DECLSPEC_HOTPATCH ClearCommBreak( HANDLE handle )
4042 return EscapeCommFunction( handle, CLRBREAK );
4046 /*****************************************************************************
4047 * ClearCommError (kernelbase.@)
4049 BOOL WINAPI DECLSPEC_HOTPATCH ClearCommError( HANDLE handle, DWORD *errors, COMSTAT *stat )
4051 SERIAL_STATUS ss;
4053 if (!DeviceIoControl( handle, IOCTL_SERIAL_GET_COMMSTATUS, NULL, 0, &ss, sizeof(ss), NULL, NULL ))
4054 return FALSE;
4056 TRACE( "status %#x,%#x, in %u, out %u, eof %d, wait %d\n", ss.Errors, ss.HoldReasons,
4057 ss.AmountInInQueue, ss.AmountInOutQueue, ss.EofReceived, ss.WaitForImmediate );
4059 if (errors)
4061 *errors = 0;
4062 if (ss.Errors & SERIAL_ERROR_BREAK) *errors |= CE_BREAK;
4063 if (ss.Errors & SERIAL_ERROR_FRAMING) *errors |= CE_FRAME;
4064 if (ss.Errors & SERIAL_ERROR_OVERRUN) *errors |= CE_OVERRUN;
4065 if (ss.Errors & SERIAL_ERROR_QUEUEOVERRUN) *errors |= CE_RXOVER;
4066 if (ss.Errors & SERIAL_ERROR_PARITY) *errors |= CE_RXPARITY;
4068 if (stat)
4070 stat->fCtsHold = !!(ss.HoldReasons & SERIAL_TX_WAITING_FOR_CTS);
4071 stat->fDsrHold = !!(ss.HoldReasons & SERIAL_TX_WAITING_FOR_DSR);
4072 stat->fRlsdHold = !!(ss.HoldReasons & SERIAL_TX_WAITING_FOR_DCD);
4073 stat->fXoffHold = !!(ss.HoldReasons & SERIAL_TX_WAITING_FOR_XON);
4074 stat->fXoffSent = !!(ss.HoldReasons & SERIAL_TX_WAITING_XOFF_SENT);
4075 stat->fEof = !!ss.EofReceived;
4076 stat->fTxim = !!ss.WaitForImmediate;
4077 stat->cbInQue = ss.AmountInInQueue;
4078 stat->cbOutQue = ss.AmountInOutQueue;
4080 return TRUE;
4084 /****************************************************************************
4085 * DeviceIoControl (kernelbase.@)
4087 BOOL WINAPI DECLSPEC_HOTPATCH DeviceIoControl( HANDLE handle, DWORD code, void *in_buff, DWORD in_count,
4088 void *out_buff, DWORD out_count, DWORD *returned,
4089 OVERLAPPED *overlapped )
4091 IO_STATUS_BLOCK iosb, *piosb = &iosb;
4092 void *cvalue = NULL;
4093 HANDLE event = 0;
4094 NTSTATUS status;
4096 TRACE( "(%p,%x,%p,%d,%p,%d,%p,%p)\n",
4097 handle, code, in_buff, in_count, out_buff, out_count, returned, overlapped );
4099 if (overlapped)
4101 piosb = (IO_STATUS_BLOCK *)overlapped;
4102 if (!((ULONG_PTR)overlapped->hEvent & 1)) cvalue = overlapped;
4103 event = overlapped->hEvent;
4104 overlapped->Internal = STATUS_PENDING;
4105 overlapped->InternalHigh = 0;
4108 if (HIWORD(code) == FILE_DEVICE_FILE_SYSTEM)
4109 status = NtFsControlFile( handle, event, NULL, cvalue, piosb, code,
4110 in_buff, in_count, out_buff, out_count );
4111 else
4112 status = NtDeviceIoControlFile( handle, event, NULL, cvalue, piosb, code,
4113 in_buff, in_count, out_buff, out_count );
4115 if (returned) *returned = piosb->Information;
4116 return set_ntstatus( status );
4120 /*****************************************************************************
4121 * EscapeCommFunction (kernelbase.@)
4123 BOOL WINAPI DECLSPEC_HOTPATCH EscapeCommFunction( HANDLE handle, DWORD func )
4125 static const DWORD ioctls[] =
4128 IOCTL_SERIAL_SET_XOFF, /* SETXOFF */
4129 IOCTL_SERIAL_SET_XON, /* SETXON */
4130 IOCTL_SERIAL_SET_RTS, /* SETRTS */
4131 IOCTL_SERIAL_CLR_RTS, /* CLRRTS */
4132 IOCTL_SERIAL_SET_DTR, /* SETDTR */
4133 IOCTL_SERIAL_CLR_DTR, /* CLRDTR */
4134 IOCTL_SERIAL_RESET_DEVICE, /* RESETDEV */
4135 IOCTL_SERIAL_SET_BREAK_ON, /* SETBREAK */
4136 IOCTL_SERIAL_SET_BREAK_OFF /* CLRBREAK */
4139 if (func >= ARRAY_SIZE(ioctls) || !ioctls[func])
4141 SetLastError( ERROR_INVALID_PARAMETER );
4142 return FALSE;
4144 return DeviceIoControl( handle, ioctls[func], NULL, 0, NULL, 0, NULL, NULL );
4148 /***********************************************************************
4149 * GetCommConfig (kernelbase.@)
4151 BOOL WINAPI DECLSPEC_HOTPATCH GetCommConfig( HANDLE handle, COMMCONFIG *config, DWORD *size )
4153 if (!config) return FALSE;
4155 TRACE( "(%p, %p, %p %u)\n", handle, config, size, *size );
4157 if (*size < sizeof(COMMCONFIG))
4159 *size = sizeof(COMMCONFIG);
4160 return FALSE;
4162 *size = sizeof(COMMCONFIG);
4163 config->dwSize = sizeof(COMMCONFIG);
4164 config->wVersion = 1;
4165 config->wReserved = 0;
4166 config->dwProviderSubType = PST_RS232;
4167 config->dwProviderOffset = 0;
4168 config->dwProviderSize = 0;
4169 return GetCommState( handle, &config->dcb );
4173 /*****************************************************************************
4174 * GetCommMask (kernelbase.@)
4176 BOOL WINAPI DECLSPEC_HOTPATCH GetCommMask( HANDLE handle, DWORD *mask )
4178 return DeviceIoControl( handle, IOCTL_SERIAL_GET_WAIT_MASK, NULL, 0, mask, sizeof(*mask),
4179 NULL, NULL );
4183 /***********************************************************************
4184 * GetCommModemStatus (kernelbase.@)
4186 BOOL WINAPI DECLSPEC_HOTPATCH GetCommModemStatus( HANDLE handle, DWORD *status )
4188 return DeviceIoControl( handle, IOCTL_SERIAL_GET_MODEMSTATUS, NULL, 0, status, sizeof(*status),
4189 NULL, NULL );
4193 /***********************************************************************
4194 * GetCommProperties (kernelbase.@)
4196 BOOL WINAPI DECLSPEC_HOTPATCH GetCommProperties( HANDLE handle, COMMPROP *prop )
4198 return DeviceIoControl( handle, IOCTL_SERIAL_GET_PROPERTIES, NULL, 0, prop, sizeof(*prop), NULL, NULL );
4202 /*****************************************************************************
4203 * GetCommState (kernelbase.@)
4205 BOOL WINAPI DECLSPEC_HOTPATCH GetCommState( HANDLE handle, DCB *dcb )
4207 SERIAL_BAUD_RATE sbr;
4208 SERIAL_LINE_CONTROL slc;
4209 SERIAL_HANDFLOW shf;
4210 SERIAL_CHARS sc;
4212 if (!dcb)
4214 SetLastError( ERROR_INVALID_PARAMETER );
4215 return FALSE;
4217 if (!DeviceIoControl(handle, IOCTL_SERIAL_GET_BAUD_RATE, NULL, 0, &sbr, sizeof(sbr), NULL, NULL) ||
4218 !DeviceIoControl(handle, IOCTL_SERIAL_GET_LINE_CONTROL, NULL, 0, &slc, sizeof(slc), NULL, NULL) ||
4219 !DeviceIoControl(handle, IOCTL_SERIAL_GET_HANDFLOW, NULL, 0, &shf, sizeof(shf), NULL, NULL) ||
4220 !DeviceIoControl(handle, IOCTL_SERIAL_GET_CHARS, NULL, 0, &sc, sizeof(sc), NULL, NULL))
4221 return FALSE;
4223 dcb->DCBlength = sizeof(*dcb);
4224 dcb->BaudRate = sbr.BaudRate;
4225 /* yes, they seem no never be (re)set on NT */
4226 dcb->fBinary = 1;
4227 dcb->fParity = 0;
4228 dcb->fOutxCtsFlow = !!(shf.ControlHandShake & SERIAL_CTS_HANDSHAKE);
4229 dcb->fOutxDsrFlow = !!(shf.ControlHandShake & SERIAL_DSR_HANDSHAKE);
4230 dcb->fDsrSensitivity = !!(shf.ControlHandShake & SERIAL_DSR_SENSITIVITY);
4231 dcb->fTXContinueOnXoff = !!(shf.FlowReplace & SERIAL_XOFF_CONTINUE);
4232 dcb->fOutX = !!(shf.FlowReplace & SERIAL_AUTO_TRANSMIT);
4233 dcb->fInX = !!(shf.FlowReplace & SERIAL_AUTO_RECEIVE);
4234 dcb->fErrorChar = !!(shf.FlowReplace & SERIAL_ERROR_CHAR);
4235 dcb->fNull = !!(shf.FlowReplace & SERIAL_NULL_STRIPPING);
4236 dcb->fAbortOnError = !!(shf.ControlHandShake & SERIAL_ERROR_ABORT);
4237 dcb->XonLim = shf.XonLimit;
4238 dcb->XoffLim = shf.XoffLimit;
4239 dcb->ByteSize = slc.WordLength;
4240 dcb->Parity = slc.Parity;
4241 dcb->StopBits = slc.StopBits;
4242 dcb->XonChar = sc.XonChar;
4243 dcb->XoffChar = sc.XoffChar;
4244 dcb->ErrorChar = sc.ErrorChar;
4245 dcb->EofChar = sc.EofChar;
4246 dcb->EvtChar = sc.EventChar;
4248 switch (shf.ControlHandShake & (SERIAL_DTR_CONTROL | SERIAL_DTR_HANDSHAKE))
4250 case SERIAL_DTR_CONTROL: dcb->fDtrControl = DTR_CONTROL_ENABLE; break;
4251 case SERIAL_DTR_HANDSHAKE: dcb->fDtrControl = DTR_CONTROL_HANDSHAKE; break;
4252 default: dcb->fDtrControl = DTR_CONTROL_DISABLE; break;
4254 switch (shf.FlowReplace & (SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE))
4256 case SERIAL_RTS_CONTROL: dcb->fRtsControl = RTS_CONTROL_ENABLE; break;
4257 case SERIAL_RTS_HANDSHAKE: dcb->fRtsControl = RTS_CONTROL_HANDSHAKE; break;
4258 case SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE:
4259 dcb->fRtsControl = RTS_CONTROL_TOGGLE; break;
4260 default: dcb->fRtsControl = RTS_CONTROL_DISABLE; break;
4262 dump_dcb( dcb );
4263 return TRUE;
4267 /*****************************************************************************
4268 * GetCommTimeouts (kernelbase.@)
4270 BOOL WINAPI DECLSPEC_HOTPATCH GetCommTimeouts( HANDLE handle, COMMTIMEOUTS *timeouts )
4272 if (!timeouts)
4274 SetLastError( ERROR_INVALID_PARAMETER );
4275 return FALSE;
4277 return DeviceIoControl( handle, IOCTL_SERIAL_GET_TIMEOUTS, NULL, 0, timeouts, sizeof(*timeouts),
4278 NULL, NULL );
4281 /********************************************************************
4282 * PurgeComm (kernelbase.@)
4284 BOOL WINAPI DECLSPEC_HOTPATCH PurgeComm(HANDLE handle, DWORD flags)
4286 return DeviceIoControl( handle, IOCTL_SERIAL_PURGE, &flags, sizeof(flags),
4287 NULL, 0, NULL, NULL );
4291 /*****************************************************************************
4292 * SetCommBreak (kernelbase.@)
4294 BOOL WINAPI DECLSPEC_HOTPATCH SetCommBreak( HANDLE handle )
4296 return EscapeCommFunction( handle, SETBREAK );
4300 /***********************************************************************
4301 * SetCommConfig (kernelbase.@)
4303 BOOL WINAPI DECLSPEC_HOTPATCH SetCommConfig( HANDLE handle, COMMCONFIG *config, DWORD size )
4305 TRACE( "(%p, %p, %u)\n", handle, config, size );
4306 return SetCommState( handle, &config->dcb );
4310 /*****************************************************************************
4311 * SetCommMask (kernelbase.@)
4313 BOOL WINAPI DECLSPEC_HOTPATCH SetCommMask( HANDLE handle, DWORD mask )
4315 return DeviceIoControl( handle, IOCTL_SERIAL_SET_WAIT_MASK, &mask, sizeof(mask),
4316 NULL, 0, NULL, NULL );
4320 /*****************************************************************************
4321 * SetCommState (kernelbase.@)
4323 BOOL WINAPI DECLSPEC_HOTPATCH SetCommState( HANDLE handle, DCB *dcb )
4325 SERIAL_BAUD_RATE sbr;
4326 SERIAL_LINE_CONTROL slc;
4327 SERIAL_HANDFLOW shf;
4328 SERIAL_CHARS sc;
4330 if (!dcb)
4332 SetLastError( ERROR_INVALID_PARAMETER );
4333 return FALSE;
4335 dump_dcb( dcb );
4337 sbr.BaudRate = dcb->BaudRate;
4338 slc.StopBits = dcb->StopBits;
4339 slc.Parity = dcb->Parity;
4340 slc.WordLength = dcb->ByteSize;
4341 shf.ControlHandShake = 0;
4342 shf.FlowReplace = 0;
4343 if (dcb->fOutxCtsFlow) shf.ControlHandShake |= SERIAL_CTS_HANDSHAKE;
4344 if (dcb->fOutxDsrFlow) shf.ControlHandShake |= SERIAL_DSR_HANDSHAKE;
4345 switch (dcb->fDtrControl)
4347 case DTR_CONTROL_DISABLE: break;
4348 case DTR_CONTROL_ENABLE: shf.ControlHandShake |= SERIAL_DTR_CONTROL; break;
4349 case DTR_CONTROL_HANDSHAKE: shf.ControlHandShake |= SERIAL_DTR_HANDSHAKE; break;
4350 default:
4351 SetLastError( ERROR_INVALID_PARAMETER );
4352 return FALSE;
4354 switch (dcb->fRtsControl)
4356 case RTS_CONTROL_DISABLE: break;
4357 case RTS_CONTROL_ENABLE: shf.FlowReplace |= SERIAL_RTS_CONTROL; break;
4358 case RTS_CONTROL_HANDSHAKE: shf.FlowReplace |= SERIAL_RTS_HANDSHAKE; break;
4359 case RTS_CONTROL_TOGGLE: shf.FlowReplace |= SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE; break;
4360 default:
4361 SetLastError( ERROR_INVALID_PARAMETER );
4362 return FALSE;
4364 if (dcb->fDsrSensitivity) shf.ControlHandShake |= SERIAL_DSR_SENSITIVITY;
4365 if (dcb->fAbortOnError) shf.ControlHandShake |= SERIAL_ERROR_ABORT;
4366 if (dcb->fErrorChar) shf.FlowReplace |= SERIAL_ERROR_CHAR;
4367 if (dcb->fNull) shf.FlowReplace |= SERIAL_NULL_STRIPPING;
4368 if (dcb->fTXContinueOnXoff) shf.FlowReplace |= SERIAL_XOFF_CONTINUE;
4369 if (dcb->fOutX) shf.FlowReplace |= SERIAL_AUTO_TRANSMIT;
4370 if (dcb->fInX) shf.FlowReplace |= SERIAL_AUTO_RECEIVE;
4371 shf.XonLimit = dcb->XonLim;
4372 shf.XoffLimit = dcb->XoffLim;
4373 sc.EofChar = dcb->EofChar;
4374 sc.ErrorChar = dcb->ErrorChar;
4375 sc.BreakChar = 0;
4376 sc.EventChar = dcb->EvtChar;
4377 sc.XonChar = dcb->XonChar;
4378 sc.XoffChar = dcb->XoffChar;
4380 /* note: change DTR/RTS lines after setting the comm attributes,
4381 * so flow control does not interfere.
4383 return (DeviceIoControl( handle, IOCTL_SERIAL_SET_BAUD_RATE, &sbr, sizeof(sbr), NULL, 0, NULL, NULL ) &&
4384 DeviceIoControl( handle, IOCTL_SERIAL_SET_LINE_CONTROL, &slc, sizeof(slc), NULL, 0, NULL, NULL ) &&
4385 DeviceIoControl( handle, IOCTL_SERIAL_SET_HANDFLOW, &shf, sizeof(shf), NULL, 0, NULL, NULL ) &&
4386 DeviceIoControl( handle, IOCTL_SERIAL_SET_CHARS, &sc, sizeof(sc), NULL, 0, NULL, NULL ));
4390 /*****************************************************************************
4391 * SetCommTimeouts (kernelbase.@)
4393 BOOL WINAPI DECLSPEC_HOTPATCH SetCommTimeouts( HANDLE handle, COMMTIMEOUTS *timeouts )
4395 if (!timeouts)
4397 SetLastError( ERROR_INVALID_PARAMETER );
4398 return FALSE;
4400 return DeviceIoControl( handle, IOCTL_SERIAL_SET_TIMEOUTS, timeouts, sizeof(*timeouts),
4401 NULL, 0, NULL, NULL );
4405 /*****************************************************************************
4406 * SetupComm (kernelbase.@)
4408 BOOL WINAPI DECLSPEC_HOTPATCH SetupComm( HANDLE handle, DWORD in_size, DWORD out_size )
4410 SERIAL_QUEUE_SIZE sqs;
4412 sqs.InSize = in_size;
4413 sqs.OutSize = out_size;
4414 return DeviceIoControl( handle, IOCTL_SERIAL_SET_QUEUE_SIZE, &sqs, sizeof(sqs), NULL, 0, NULL, NULL );
4418 /*****************************************************************************
4419 * TransmitCommChar (kernelbase.@)
4421 BOOL WINAPI DECLSPEC_HOTPATCH TransmitCommChar( HANDLE handle, CHAR ch )
4423 return DeviceIoControl( handle, IOCTL_SERIAL_IMMEDIATE_CHAR, &ch, sizeof(ch), NULL, 0, NULL, NULL );
4427 /***********************************************************************
4428 * WaitCommEvent (kernelbase.@)
4430 BOOL WINAPI DECLSPEC_HOTPATCH WaitCommEvent( HANDLE handle, DWORD *events, OVERLAPPED *overlapped )
4432 return DeviceIoControl( handle, IOCTL_SERIAL_WAIT_ON_MASK, NULL, 0, events, sizeof(*events),
4433 NULL, overlapped );