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
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
30 #define WIN32_NO_STATUS
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 */
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 */
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
)
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";
95 /***********************************************************************
96 * Operations on file names
97 ***********************************************************************/
100 /***********************************************************************
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.
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
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
;
160 if (!RtlDosPathNameToNtPathName_U( source
, &source_name
, NULL
, NULL
))
162 SetLastError( ERROR_PATH_NOT_FOUND
);
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
);
173 attr
.Length
= sizeof(attr
);
174 attr
.RootDirectory
= 0;
175 attr
.ObjectName
= &nameW
;
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
);
188 len1
= source_name
.Length
+ sizeof(WCHAR
);
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) */
212 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, size
+ len1
+ len2
+ sizeof(WCHAR
) ))) goto done
;
215 memcpy( buffer
+ size
, source_name
.Buffer
, len1
);
217 p
= (WCHAR
*)(buffer
+ size
);
220 if (flags
& MOVEFILE_REPLACE_EXISTING
) *p
++ = '!';
221 memcpy( p
, dest_name
.Buffer
, len2
);
227 size
+= sizeof(WCHAR
);
231 p
= (WCHAR
*)(buffer
+ size
);
233 size
+= sizeof(WCHAR
);
234 rc
= !NtSetValueKey( key
, &nameW
, 0, REG_MULTI_SZ
, buffer
+ info_size
, size
- info_size
);
237 RtlFreeUnicodeString( &source_name
);
238 RtlFreeUnicodeString( &dest_name
);
239 if (key
) NtClose(key
);
240 HeapFree( GetProcessHeap(), 0, buffer
);
245 /***********************************************************************
248 static WCHAR
*append_ext( const WCHAR
*name
, const WCHAR
*ext
)
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
);
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
;
280 SIZE_T needed
, size
= 1024;
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
,
288 if (status
!= STATUS_SUCCESS
) return status
;
292 if (!(info
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
294 status
= STATUS_NO_MEMORY
;
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
);
304 /* restart with larger buffer */
307 if (!info
->lpAssemblyManifestPath
)
309 status
= STATUS_SXS_KEY_NOT_FOUND
;
313 if ((p
= wcsrchr( info
->lpAssemblyManifestPath
, '\\' )))
315 DWORD dirlen
= info
->ulAssemblyDirectoryNameLength
/ sizeof(WCHAR
);
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
;
331 memcpy( p
, info
->lpAssemblyManifestPath
, dirlen
* sizeof(WCHAR
) );
337 if (!info
->lpAssemblyDirectoryName
)
339 status
= STATUS_SXS_KEY_NOT_FOUND
;
343 needed
= sizeof(L
"C:\\windows\\winsxs\\") + info
->ulAssemblyDirectoryNameLength
+ sizeof(WCHAR
);
345 if (!(*path
= p
= RtlAllocateHeap( GetProcessHeap(), 0, needed
)))
347 status
= STATUS_NO_MEMORY
;
350 lstrcpyW( p
, L
"C:\\windows\\winsxs\\" );
352 memcpy( p
, info
->lpAssemblyDirectoryName
, info
->ulAssemblyDirectoryNameLength
);
353 p
+= info
->ulAssemblyDirectoryNameLength
/ sizeof(WCHAR
);
357 RtlFreeHeap( GetProcessHeap(), 0, info
);
358 RtlReleaseActivationContext( data
.hActCtx
);
363 /***********************************************************************
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
);
378 /***********************************************************************
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
)
388 RtlInitUnicodeString( &strW
, nameW
);
390 ret
= oem_file_apis
? RtlUnicodeStringToOemSize( &strW
) : RtlUnicodeStringToAnsiSize( &strW
);
391 if (buffer
&& ret
<= len
)
396 str
.MaximumLength
= min( len
, UNICODE_STRING_MAX_CHARS
);
398 RtlUnicodeStringToOemString( &str
, &strW
, FALSE
);
400 RtlUnicodeStringToAnsiString( &str
, &strW
, FALSE
);
401 ret
= str
.Length
; /* length without terminating 0 */
407 /***********************************************************************
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
)
419 UNICODE_STRING strW
, *pstrW
;
422 RtlInitAnsiString( &str
, name
);
423 pstrW
= alloc
? &strW
: &NtCurrentTeb()->StaticUnicodeString
;
425 status
= RtlOemStringToUnicodeString( pstrW
, &str
, alloc
);
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
);
433 SetLastError( RtlNtStatusToDosError(status
) );
438 /***********************************************************************
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
)
447 if (srclen
< 0) srclen
= lstrlenW( src
) + 1;
453 strW
.Buffer
= (WCHAR
*)src
;
454 strW
.Length
= srclen
* sizeof(WCHAR
);
455 ret
= RtlUnicodeStringToOemSize( &strW
) - 1;
458 RtlUnicodeToMultiByteSize( &ret
, src
, srclen
* sizeof(WCHAR
) );
463 RtlUnicodeToOemN( dest
, destlen
, &ret
, src
, srclen
* sizeof(WCHAR
) );
465 RtlUnicodeToMultiByteN( dest
, destlen
, &ret
, src
, srclen
* sizeof(WCHAR
) );
471 /***********************************************************************
474 static BOOL
is_same_file( HANDLE h1
, HANDLE h2
)
476 FILE_OBJECTID_BUFFER id1
, id2
;
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;
502 BY_HANDLE_FILE_INFORMATION info
;
507 if (!source
|| !dest
)
509 SetLastError( ERROR_INVALID_PARAMETER
);
512 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, buffer_size
)))
514 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
518 TRACE("%s -> %s, %x\n", debugstr_w(source
), debugstr_w(dest
), flags
);
520 if ((h1
= CreateFileW( source
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
521 NULL
, OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
523 WARN("Unable to open source %s\n", debugstr_w(source
));
524 HeapFree( GetProcessHeap(), 0, buffer
);
528 if (!GetFileInformationByHandle( h1
, &info
))
530 WARN("GetFileInformationByHandle returned error for %s\n", debugstr_w(source
));
531 HeapFree( GetProcessHeap(), 0, buffer
);
536 if (!(flags
& COPY_FILE_FAIL_IF_EXISTS
))
538 BOOL same_file
= FALSE
;
539 h2
= CreateFileW( dest
, 0, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0 );
540 if (h2
!= INVALID_HANDLE_VALUE
)
542 same_file
= is_same_file( h1
, h2
);
547 HeapFree( GetProcessHeap(), 0, buffer
);
549 SetLastError( ERROR_SHARING_VIOLATION
);
554 if ((h2
= CreateFileW( dest
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
555 (flags
& COPY_FILE_FAIL_IF_EXISTS
) ? CREATE_NEW
: CREATE_ALWAYS
,
556 info
.dwFileAttributes
, h1
)) == INVALID_HANDLE_VALUE
)
558 WARN("Unable to open dest %s\n", debugstr_w(dest
));
559 HeapFree( GetProcessHeap(), 0, buffer
);
564 while (ReadFile( h1
, buffer
, buffer_size
, &count
, NULL
) && count
)
570 if (!WriteFile( h2
, p
, count
, &res
, NULL
) || !res
) goto done
;
577 /* Maintain the timestamp of source file to destination file */
578 SetFileTime( h2
, NULL
, NULL
, &info
.ftLastWriteTime
);
579 HeapFree( GetProcessHeap(), 0, buffer
);
586 /**************************************************************************
587 * CopyFileW (kernelbase.@)
589 BOOL WINAPI DECLSPEC_HOTPATCH
CopyFileW( const WCHAR
*source
, const WCHAR
*dest
, BOOL fail_if_exists
)
591 return CopyFileExW( source
, dest
, NULL
, NULL
, NULL
, fail_if_exists
? COPY_FILE_FAIL_IF_EXISTS
: 0 );
595 /***********************************************************************
596 * CreateDirectoryA (kernelbase.@)
598 BOOL WINAPI DECLSPEC_HOTPATCH
CreateDirectoryA( LPCSTR path
, LPSECURITY_ATTRIBUTES sa
)
602 if (!(pathW
= file_name_AtoW( path
, FALSE
))) return FALSE
;
603 return CreateDirectoryW( pathW
, sa
);
607 /***********************************************************************
608 * CreateDirectoryW (kernelbase.@)
610 BOOL WINAPI DECLSPEC_HOTPATCH
CreateDirectoryW( LPCWSTR path
, LPSECURITY_ATTRIBUTES sa
)
612 OBJECT_ATTRIBUTES attr
;
613 UNICODE_STRING nt_name
;
618 TRACE( "%s\n", debugstr_w(path
) );
620 if (!RtlDosPathNameToNtPathName_U( path
, &nt_name
, NULL
, NULL
))
622 SetLastError( ERROR_PATH_NOT_FOUND
);
625 attr
.Length
= sizeof(attr
);
626 attr
.RootDirectory
= 0;
627 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
628 attr
.ObjectName
= &nt_name
;
629 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
630 attr
.SecurityQualityOfService
= NULL
;
632 status
= NtCreateFile( &handle
, GENERIC_READ
| SYNCHRONIZE
, &attr
, &io
, NULL
,
633 FILE_ATTRIBUTE_NORMAL
, FILE_SHARE_READ
, FILE_CREATE
,
634 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
, NULL
, 0 );
635 if (status
== STATUS_SUCCESS
) NtClose( handle
);
637 RtlFreeUnicodeString( &nt_name
);
638 return set_ntstatus( status
);
642 /***********************************************************************
643 * CreateDirectoryEx (kernelbase.@)
645 BOOL WINAPI DECLSPEC_HOTPATCH
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
646 LPSECURITY_ATTRIBUTES sa
)
648 return CreateDirectoryW( path
, sa
);
652 /*************************************************************************
653 * CreateFile2 (kernelbase.@)
655 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateFile2( LPCWSTR name
, DWORD access
, DWORD sharing
, DWORD creation
,
656 CREATEFILE2_EXTENDED_PARAMETERS
*params
)
658 static const DWORD attributes_mask
= FILE_ATTRIBUTE_READONLY
|
659 FILE_ATTRIBUTE_HIDDEN
|
660 FILE_ATTRIBUTE_SYSTEM
|
661 FILE_ATTRIBUTE_ARCHIVE
|
662 FILE_ATTRIBUTE_NORMAL
|
663 FILE_ATTRIBUTE_TEMPORARY
|
664 FILE_ATTRIBUTE_OFFLINE
|
665 FILE_ATTRIBUTE_ENCRYPTED
|
666 FILE_ATTRIBUTE_INTEGRITY_STREAM
;
667 static const DWORD flags_mask
= FILE_FLAG_BACKUP_SEMANTICS
|
668 FILE_FLAG_DELETE_ON_CLOSE
|
669 FILE_FLAG_NO_BUFFERING
|
670 FILE_FLAG_OPEN_NO_RECALL
|
671 FILE_FLAG_OPEN_REPARSE_POINT
|
672 FILE_FLAG_OVERLAPPED
|
673 FILE_FLAG_POSIX_SEMANTICS
|
674 FILE_FLAG_RANDOM_ACCESS
|
675 FILE_FLAG_SEQUENTIAL_SCAN
|
676 FILE_FLAG_WRITE_THROUGH
;
678 LPSECURITY_ATTRIBUTES sa
= params
? params
->lpSecurityAttributes
: NULL
;
679 HANDLE
template = params
? params
->hTemplateFile
: NULL
;
680 DWORD attributes
= params
? params
->dwFileAttributes
: 0;
681 DWORD flags
= params
? params
->dwFileFlags
: 0;
683 FIXME( "(%s %x %x %x %p), partial stub\n", debugstr_w(name
), access
, sharing
, creation
, params
);
685 if (attributes
& ~attributes_mask
) FIXME( "unsupported attributes %#x\n", attributes
);
686 if (flags
& ~flags_mask
) FIXME( "unsupported flags %#x\n", flags
);
687 attributes
&= attributes_mask
;
690 return CreateFileW( name
, access
, sharing
, sa
, creation
, flags
| attributes
, template );
694 /*************************************************************************
695 * CreateFileA (kernelbase.@)
697 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateFileA( LPCSTR name
, DWORD access
, DWORD sharing
,
698 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
699 DWORD attributes
, HANDLE
template)
703 if ((GetVersion() & 0x80000000) && IsBadStringPtrA( name
, -1 )) return INVALID_HANDLE_VALUE
;
704 if (!(nameW
= file_name_AtoW( name
, FALSE
))) return INVALID_HANDLE_VALUE
;
705 return CreateFileW( nameW
, access
, sharing
, sa
, creation
, attributes
, template );
708 static UINT
get_nt_file_options( DWORD attributes
)
712 if (attributes
& FILE_FLAG_BACKUP_SEMANTICS
)
713 options
|= FILE_OPEN_FOR_BACKUP_INTENT
;
715 options
|= FILE_NON_DIRECTORY_FILE
;
716 if (attributes
& FILE_FLAG_DELETE_ON_CLOSE
)
717 options
|= FILE_DELETE_ON_CLOSE
;
718 if (attributes
& FILE_FLAG_NO_BUFFERING
)
719 options
|= FILE_NO_INTERMEDIATE_BUFFERING
;
720 if (!(attributes
& FILE_FLAG_OVERLAPPED
))
721 options
|= FILE_SYNCHRONOUS_IO_NONALERT
;
722 if (attributes
& FILE_FLAG_RANDOM_ACCESS
)
723 options
|= FILE_RANDOM_ACCESS
;
724 if (attributes
& FILE_FLAG_WRITE_THROUGH
)
725 options
|= FILE_WRITE_THROUGH
;
729 /*************************************************************************
730 * CreateFileW (kernelbase.@)
732 HANDLE WINAPI DECLSPEC_HOTPATCH
CreateFileW( LPCWSTR filename
, DWORD access
, DWORD sharing
,
733 LPSECURITY_ATTRIBUTES sa
, DWORD creation
,
734 DWORD attributes
, HANDLE
template )
737 OBJECT_ATTRIBUTES attr
;
738 UNICODE_STRING nameW
;
742 const WCHAR
*vxd_name
= NULL
;
743 SECURITY_QUALITY_OF_SERVICE qos
;
745 static const UINT nt_disposition
[5] =
747 FILE_CREATE
, /* CREATE_NEW */
748 FILE_OVERWRITE_IF
, /* CREATE_ALWAYS */
749 FILE_OPEN
, /* OPEN_EXISTING */
750 FILE_OPEN_IF
, /* OPEN_ALWAYS */
751 FILE_OVERWRITE
/* TRUNCATE_EXISTING */
757 if (!filename
|| !filename
[0])
759 SetLastError( ERROR_PATH_NOT_FOUND
);
760 return INVALID_HANDLE_VALUE
;
763 TRACE( "%s %s%s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename
),
764 (access
& GENERIC_READ
) ? "GENERIC_READ " : "",
765 (access
& GENERIC_WRITE
) ? "GENERIC_WRITE " : "",
766 (access
& GENERIC_EXECUTE
) ? "GENERIC_EXECUTE " : "",
767 !access
? "QUERY_ACCESS " : "",
768 (sharing
& FILE_SHARE_READ
) ? "FILE_SHARE_READ " : "",
769 (sharing
& FILE_SHARE_WRITE
) ? "FILE_SHARE_WRITE " : "",
770 (sharing
& FILE_SHARE_DELETE
) ? "FILE_SHARE_DELETE " : "",
771 creation
, attributes
);
773 if (!wcsncmp( filename
, L
"\\\\.\\", 4 ))
775 if ((filename
[4] && filename
[5] == ':' && !filename
[6]) ||
776 !wcsnicmp( filename
+ 4, L
"PIPE\\", 5 ) ||
777 !wcsnicmp( filename
+ 4, L
"MAILSLOT\\", 9 ))
781 else if ((dosdev
= RtlIsDosDeviceName_U( filename
+ 4 )))
783 dosdev
+= MAKELONG( 0, 4*sizeof(WCHAR
) ); /* adjust position to start of filename */
785 else if (GetVersion() & 0x80000000)
787 vxd_name
= filename
+ 4;
788 if (!creation
) creation
= OPEN_EXISTING
;
791 else dosdev
= RtlIsDosDeviceName_U( filename
);
793 if (creation
< CREATE_NEW
|| creation
> TRUNCATE_EXISTING
)
795 SetLastError( ERROR_INVALID_PARAMETER
);
796 return INVALID_HANDLE_VALUE
;
799 if (!RtlDosPathNameToNtPathName_U( filename
, &nameW
, NULL
, NULL
))
801 SetLastError( ERROR_PATH_NOT_FOUND
);
802 return INVALID_HANDLE_VALUE
;
805 /* now call NtCreateFile */
807 if (attributes
& FILE_FLAG_DELETE_ON_CLOSE
)
810 attr
.Length
= sizeof(attr
);
811 attr
.RootDirectory
= 0;
812 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
813 attr
.ObjectName
= &nameW
;
814 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
815 if (attributes
& SECURITY_SQOS_PRESENT
)
817 qos
.Length
= sizeof(qos
);
818 qos
.ImpersonationLevel
= (attributes
>> 16) & 0x3;
819 qos
.ContextTrackingMode
= attributes
& SECURITY_CONTEXT_TRACKING
? SECURITY_DYNAMIC_TRACKING
: SECURITY_STATIC_TRACKING
;
820 qos
.EffectiveOnly
= (attributes
& SECURITY_EFFECTIVE_ONLY
) != 0;
821 attr
.SecurityQualityOfService
= &qos
;
824 attr
.SecurityQualityOfService
= NULL
;
826 if (sa
&& sa
->bInheritHandle
) attr
.Attributes
|= OBJ_INHERIT
;
828 status
= NtCreateFile( &ret
, access
| SYNCHRONIZE
| FILE_READ_ATTRIBUTES
, &attr
, &io
,
829 NULL
, attributes
& FILE_ATTRIBUTE_VALID_FLAGS
, sharing
,
830 nt_disposition
[creation
- CREATE_NEW
],
831 get_nt_file_options( attributes
), NULL
, 0 );
834 if (vxd_name
&& vxd_name
[0])
836 static HANDLE (*vxd_open
)(LPCWSTR
,DWORD
,SECURITY_ATTRIBUTES
*);
837 if (!vxd_open
) vxd_open
= (void *)GetProcAddress( GetModuleHandleW(L
"krnl386.exe16"),
839 if (vxd_open
&& (ret
= vxd_open( vxd_name
, access
, sa
))) goto done
;
842 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename
), status
);
843 ret
= INVALID_HANDLE_VALUE
;
845 /* In the case file creation was rejected due to CREATE_NEW flag
846 * was specified and file with that name already exists, correct
847 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
848 * Note: RtlNtStatusToDosError is not the subject to blame here.
850 if (status
== STATUS_OBJECT_NAME_COLLISION
)
851 SetLastError( ERROR_FILE_EXISTS
);
853 SetLastError( RtlNtStatusToDosError(status
) );
858 ((LOWORD(dosdev
) == 3 * sizeof(WCHAR
) && !wcsnicmp( filename
+ HIWORD(dosdev
)/sizeof(WCHAR
), L
"CON", 3 )) ||
859 (LOWORD(dosdev
) == 6 * sizeof(WCHAR
) && !wcsnicmp( filename
+ HIWORD(dosdev
)/sizeof(WCHAR
), L
"CONIN$", 6 )) ||
860 (LOWORD(dosdev
) == 7 * sizeof(WCHAR
) && !wcsnicmp( filename
+ HIWORD(dosdev
)/sizeof(WCHAR
), L
"CONOUT$", 7 ))))
861 ret
= console_handle_map( ret
);
863 if ((creation
== CREATE_ALWAYS
&& io
.Information
== FILE_OVERWRITTEN
) ||
864 (creation
== OPEN_ALWAYS
&& io
.Information
== FILE_OPENED
))
865 SetLastError( ERROR_ALREADY_EXISTS
);
869 RtlFreeUnicodeString( &nameW
);
872 if (!ret
) ret
= INVALID_HANDLE_VALUE
;
873 TRACE("returning %p\n", ret
);
878 /*************************************************************************
879 * CreateHardLinkA (kernelbase.@)
881 BOOL WINAPI DECLSPEC_HOTPATCH
CreateHardLinkA( const char *dest
, const char *source
,
882 SECURITY_ATTRIBUTES
*attr
)
884 WCHAR
*sourceW
, *destW
;
887 if (!(sourceW
= file_name_AtoW( source
, TRUE
))) return FALSE
;
888 if (!(destW
= file_name_AtoW( dest
, TRUE
)))
890 HeapFree( GetProcessHeap(), 0, sourceW
);
893 res
= CreateHardLinkW( destW
, sourceW
, attr
);
894 HeapFree( GetProcessHeap(), 0, sourceW
);
895 HeapFree( GetProcessHeap(), 0, destW
);
900 /*************************************************************************
901 * CreateHardLinkW (kernelbase.@)
903 BOOL WINAPI
CreateHardLinkW( LPCWSTR dest
, LPCWSTR source
, SECURITY_ATTRIBUTES
*sec_attr
)
905 UNICODE_STRING ntDest
, ntSource
;
906 FILE_LINK_INFORMATION
*info
= NULL
;
907 OBJECT_ATTRIBUTES attr
;
913 TRACE( "(%s, %s, %p)\n", debugstr_w(dest
), debugstr_w(source
), sec_attr
);
915 ntDest
.Buffer
= ntSource
.Buffer
= NULL
;
916 if (!RtlDosPathNameToNtPathName_U( dest
, &ntDest
, NULL
, NULL
) ||
917 !RtlDosPathNameToNtPathName_U( source
, &ntSource
, NULL
, NULL
))
919 SetLastError( ERROR_PATH_NOT_FOUND
);
923 size
= offsetof( FILE_LINK_INFORMATION
, FileName
) + ntDest
.Length
;
924 if (!(info
= HeapAlloc( GetProcessHeap(), 0, size
)))
926 SetLastError( ERROR_OUTOFMEMORY
);
930 InitializeObjectAttributes( &attr
, &ntSource
, OBJ_CASE_INSENSITIVE
, 0, NULL
);
931 if (!(ret
= set_ntstatus( NtOpenFile( &file
, SYNCHRONIZE
, &attr
, &io
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
932 FILE_SYNCHRONOUS_IO_NONALERT
) )))
935 info
->ReplaceIfExists
= FALSE
;
936 info
->RootDirectory
= NULL
;
937 info
->FileNameLength
= ntDest
.Length
;
938 memcpy( info
->FileName
, ntDest
.Buffer
, ntDest
.Length
);
939 ret
= set_ntstatus( NtSetInformationFile( file
, &io
, info
, size
, FileLinkInformation
) );
943 RtlFreeUnicodeString( &ntSource
);
944 RtlFreeUnicodeString( &ntDest
);
945 HeapFree( GetProcessHeap(), 0, info
);
950 /*************************************************************************
951 * CreateSymbolicLinkW (kernelbase.@)
953 BOOLEAN WINAPI
/* DECLSPEC_HOTPATCH */ CreateSymbolicLinkW( LPCWSTR link
, LPCWSTR target
, DWORD flags
)
955 FIXME( "(%s %s %d): stub\n", debugstr_w(link
), debugstr_w(target
), flags
);
960 /***********************************************************************
961 * DeleteFileA (kernelbase.@)
963 BOOL WINAPI DECLSPEC_HOTPATCH
DeleteFileA( LPCSTR path
)
967 if (!(pathW
= file_name_AtoW( path
, FALSE
))) return FALSE
;
968 return DeleteFileW( pathW
);
972 /***********************************************************************
973 * DeleteFileW (kernelbase.@)
975 BOOL WINAPI DECLSPEC_HOTPATCH
DeleteFileW( LPCWSTR path
)
977 UNICODE_STRING nameW
;
978 OBJECT_ATTRIBUTES attr
;
983 TRACE( "%s\n", debugstr_w(path
) );
985 if (!RtlDosPathNameToNtPathName_U( path
, &nameW
, NULL
, NULL
))
987 SetLastError( ERROR_PATH_NOT_FOUND
);
991 attr
.Length
= sizeof(attr
);
992 attr
.RootDirectory
= 0;
993 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
994 attr
.ObjectName
= &nameW
;
995 attr
.SecurityDescriptor
= NULL
;
996 attr
.SecurityQualityOfService
= NULL
;
998 status
= NtCreateFile(&hFile
, SYNCHRONIZE
| DELETE
, &attr
, &io
, NULL
, 0,
999 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1000 FILE_OPEN
, FILE_DELETE_ON_CLOSE
| FILE_NON_DIRECTORY_FILE
, NULL
, 0);
1001 if (status
== STATUS_SUCCESS
) status
= NtClose(hFile
);
1003 RtlFreeUnicodeString( &nameW
);
1004 return set_ntstatus( status
);
1008 /****************************************************************************
1009 * FindCloseChangeNotification (kernelbase.@)
1011 BOOL WINAPI DECLSPEC_HOTPATCH
FindCloseChangeNotification( HANDLE handle
)
1013 return CloseHandle( handle
);
1017 /****************************************************************************
1018 * FindFirstChangeNotificationA (kernelbase.@)
1020 HANDLE WINAPI DECLSPEC_HOTPATCH
FindFirstChangeNotificationA( LPCSTR path
, BOOL subtree
, DWORD filter
)
1024 if (!(pathW
= file_name_AtoW( path
, FALSE
))) return INVALID_HANDLE_VALUE
;
1025 return FindFirstChangeNotificationW( pathW
, subtree
, filter
);
1030 * NtNotifyChangeDirectoryFile may write back to the IO_STATUS_BLOCK
1031 * asynchronously. We don't care about the contents, but it can't
1032 * be placed on the stack since it will go out of scope when we return.
1034 static IO_STATUS_BLOCK dummy_iosb
;
1036 /****************************************************************************
1037 * FindFirstChangeNotificationW (kernelbase.@)
1039 HANDLE WINAPI DECLSPEC_HOTPATCH
FindFirstChangeNotificationW( LPCWSTR path
, BOOL subtree
, DWORD filter
)
1041 UNICODE_STRING nt_name
;
1042 OBJECT_ATTRIBUTES attr
;
1044 HANDLE handle
= INVALID_HANDLE_VALUE
;
1046 TRACE( "%s %d %x\n", debugstr_w(path
), subtree
, filter
);
1048 if (!RtlDosPathNameToNtPathName_U( path
, &nt_name
, NULL
, NULL
))
1050 SetLastError( ERROR_PATH_NOT_FOUND
);
1054 attr
.Length
= sizeof(attr
);
1055 attr
.RootDirectory
= 0;
1056 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1057 attr
.ObjectName
= &nt_name
;
1058 attr
.SecurityDescriptor
= NULL
;
1059 attr
.SecurityQualityOfService
= NULL
;
1061 status
= NtOpenFile( &handle
, FILE_LIST_DIRECTORY
| SYNCHRONIZE
, &attr
, &dummy_iosb
,
1062 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1063 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
1064 RtlFreeUnicodeString( &nt_name
);
1066 if (!set_ntstatus( status
)) return INVALID_HANDLE_VALUE
;
1068 status
= NtNotifyChangeDirectoryFile( handle
, NULL
, NULL
, NULL
, &dummy_iosb
, NULL
, 0, filter
, subtree
);
1069 if (status
!= STATUS_PENDING
)
1072 SetLastError( RtlNtStatusToDosError(status
) );
1073 return INVALID_HANDLE_VALUE
;
1079 /****************************************************************************
1080 * FindNextChangeNotification (kernelbase.@)
1082 BOOL WINAPI DECLSPEC_HOTPATCH
FindNextChangeNotification( HANDLE handle
)
1084 NTSTATUS status
= NtNotifyChangeDirectoryFile( handle
, NULL
, NULL
, NULL
, &dummy_iosb
,
1085 NULL
, 0, FILE_NOTIFY_CHANGE_SIZE
, 0 );
1086 if (status
== STATUS_PENDING
) return TRUE
;
1087 return set_ntstatus( status
);
1091 /******************************************************************************
1092 * FindFirstFileExA (kernelbase.@)
1094 HANDLE WINAPI DECLSPEC_HOTPATCH
FindFirstFileExA( const char *filename
, FINDEX_INFO_LEVELS level
,
1095 void *data
, FINDEX_SEARCH_OPS search_op
,
1096 void *filter
, DWORD flags
)
1099 WIN32_FIND_DATAA
*dataA
= data
;
1100 WIN32_FIND_DATAW dataW
;
1103 if (!(nameW
= file_name_AtoW( filename
, FALSE
))) return INVALID_HANDLE_VALUE
;
1105 handle
= FindFirstFileExW( nameW
, level
, &dataW
, search_op
, filter
, flags
);
1106 if (handle
== INVALID_HANDLE_VALUE
) return handle
;
1108 dataA
->dwFileAttributes
= dataW
.dwFileAttributes
;
1109 dataA
->ftCreationTime
= dataW
.ftCreationTime
;
1110 dataA
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
1111 dataA
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
1112 dataA
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
1113 dataA
->nFileSizeLow
= dataW
.nFileSizeLow
;
1114 file_name_WtoA( dataW
.cFileName
, -1, dataA
->cFileName
, sizeof(dataA
->cFileName
) );
1115 file_name_WtoA( dataW
.cAlternateFileName
, -1, dataA
->cAlternateFileName
,
1116 sizeof(dataA
->cAlternateFileName
) );
1121 /******************************************************************************
1122 * FindFirstFileExW (kernelbase.@)
1124 HANDLE WINAPI DECLSPEC_HOTPATCH
FindFirstFileExW( LPCWSTR filename
, FINDEX_INFO_LEVELS level
,
1125 LPVOID data
, FINDEX_SEARCH_OPS search_op
,
1126 LPVOID filter
, DWORD flags
)
1129 BOOL has_wildcard
= FALSE
;
1130 FIND_FIRST_INFO
*info
= NULL
;
1131 UNICODE_STRING nt_name
;
1132 OBJECT_ATTRIBUTES attr
;
1135 DWORD size
, device
= 0;
1137 TRACE( "%s %d %p %d %p %x\n", debugstr_w(filename
), level
, data
, search_op
, filter
, flags
);
1139 if (flags
& ~FIND_FIRST_EX_LARGE_FETCH
)
1141 FIXME("flags not implemented 0x%08x\n", flags
);
1143 if (search_op
!= FindExSearchNameMatch
&& search_op
!= FindExSearchLimitToDirectories
)
1145 FIXME( "search_op not implemented 0x%08x\n", search_op
);
1146 SetLastError( ERROR_INVALID_PARAMETER
);
1147 return INVALID_HANDLE_VALUE
;
1149 if (level
!= FindExInfoStandard
&& level
!= FindExInfoBasic
)
1151 FIXME("info level %d not implemented\n", level
);
1152 SetLastError( ERROR_INVALID_PARAMETER
);
1153 return INVALID_HANDLE_VALUE
;
1156 if (!RtlDosPathNameToNtPathName_U( filename
, &nt_name
, &mask
, NULL
))
1158 SetLastError( ERROR_PATH_NOT_FOUND
);
1159 return INVALID_HANDLE_VALUE
;
1162 if (!mask
&& (device
= RtlIsDosDeviceName_U( filename
)))
1166 /* we still need to check that the directory can be opened */
1170 if (!(dir
= HeapAlloc( GetProcessHeap(), 0, HIWORD(device
) + sizeof(WCHAR
) )))
1172 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1175 memcpy( dir
, filename
, HIWORD(device
) );
1176 dir
[HIWORD(device
)/sizeof(WCHAR
)] = 0;
1178 RtlFreeUnicodeString( &nt_name
);
1179 if (!RtlDosPathNameToNtPathName_U( dir
? dir
: L
".", &nt_name
, &mask
, NULL
))
1181 HeapFree( GetProcessHeap(), 0, dir
);
1182 SetLastError( ERROR_PATH_NOT_FOUND
);
1185 HeapFree( GetProcessHeap(), 0, dir
);
1188 else if (!mask
|| !*mask
)
1190 SetLastError( ERROR_FILE_NOT_FOUND
);
1195 nt_name
.Length
= (mask
- nt_name
.Buffer
) * sizeof(WCHAR
);
1196 has_wildcard
= wcspbrk( mask
, L
"*?" ) != NULL
;
1197 size
= has_wildcard
? 8192 : max_entry_size
;
1200 if (!(info
= HeapAlloc( GetProcessHeap(), 0, offsetof( FIND_FIRST_INFO
, data
[size
] ))))
1202 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1206 /* check if path is the root of the drive, skipping the \??\ prefix */
1207 info
->is_root
= FALSE
;
1208 if (nt_name
.Length
>= 6 * sizeof(WCHAR
) && nt_name
.Buffer
[5] == ':')
1211 while (pos
* sizeof(WCHAR
) < nt_name
.Length
&& nt_name
.Buffer
[pos
] == '\\') pos
++;
1212 info
->is_root
= (pos
* sizeof(WCHAR
) >= nt_name
.Length
);
1215 attr
.Length
= sizeof(attr
);
1216 attr
.RootDirectory
= 0;
1217 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1218 attr
.ObjectName
= &nt_name
;
1219 attr
.SecurityDescriptor
= NULL
;
1220 attr
.SecurityQualityOfService
= NULL
;
1222 status
= NtOpenFile( &info
->handle
, GENERIC_READ
| SYNCHRONIZE
, &attr
, &io
,
1223 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1224 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
| FILE_OPEN_FOR_BACKUP_INTENT
);
1225 if (status
!= STATUS_SUCCESS
)
1227 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1228 SetLastError( ERROR_PATH_NOT_FOUND
);
1230 SetLastError( RtlNtStatusToDosError(status
) );
1234 RtlInitializeCriticalSection( &info
->cs
);
1235 info
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FIND_FIRST_INFO.cs");
1236 info
->path
= nt_name
;
1237 info
->magic
= FIND_FIRST_MAGIC
;
1238 info
->wildcard
= has_wildcard
;
1241 info
->data_size
= size
;
1242 info
->search_op
= search_op
;
1243 info
->level
= level
;
1247 WIN32_FIND_DATAW
*wfd
= data
;
1249 memset( wfd
, 0, sizeof(*wfd
) );
1250 memcpy( wfd
->cFileName
, filename
+ HIWORD(device
)/sizeof(WCHAR
), LOWORD(device
) );
1251 wfd
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
1252 CloseHandle( info
->handle
);
1257 UNICODE_STRING mask_str
;
1259 RtlInitUnicodeString( &mask_str
, mask
);
1260 status
= NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, info
->data_size
,
1261 FileBothDirectoryInformation
, FALSE
, &mask_str
, TRUE
);
1265 SetLastError( RtlNtStatusToDosError( status
) );
1266 return INVALID_HANDLE_VALUE
;
1269 info
->data_len
= io
.Information
;
1270 if (!has_wildcard
) info
->data_size
= 0; /* we read everything */
1272 if (!FindNextFileW( info
, data
))
1274 TRACE( "%s not found\n", debugstr_w(filename
) );
1276 SetLastError( ERROR_FILE_NOT_FOUND
);
1277 return INVALID_HANDLE_VALUE
;
1279 if (!has_wildcard
) /* we can't find two files with the same name */
1281 CloseHandle( info
->handle
);
1288 HeapFree( GetProcessHeap(), 0, info
);
1289 RtlFreeUnicodeString( &nt_name
);
1290 return INVALID_HANDLE_VALUE
;
1294 /******************************************************************************
1295 * FindFirstFileA (kernelbase.@)
1297 HANDLE WINAPI DECLSPEC_HOTPATCH
FindFirstFileA( const char *filename
, WIN32_FIND_DATAA
*data
)
1299 return FindFirstFileExA( filename
, FindExInfoStandard
, data
, FindExSearchNameMatch
, NULL
, 0 );
1303 /******************************************************************************
1304 * FindFirstFileW (kernelbase.@)
1306 HANDLE WINAPI DECLSPEC_HOTPATCH
FindFirstFileW( const WCHAR
*filename
, WIN32_FIND_DATAW
*data
)
1308 return FindFirstFileExW( filename
, FindExInfoStandard
, data
, FindExSearchNameMatch
, NULL
, 0 );
1312 /**************************************************************************
1313 * FindFirstStreamW (kernelbase.@)
1315 HANDLE WINAPI
FindFirstStreamW( const WCHAR
*filename
, STREAM_INFO_LEVELS level
, void *data
, DWORD flags
)
1317 FIXME("(%s, %d, %p, %x): stub!\n", debugstr_w(filename
), level
, data
, flags
);
1318 SetLastError( ERROR_HANDLE_EOF
);
1319 return INVALID_HANDLE_VALUE
;
1323 /******************************************************************************
1324 * FindNextFileA (kernelbase.@)
1326 BOOL WINAPI DECLSPEC_HOTPATCH
FindNextFileA( HANDLE handle
, WIN32_FIND_DATAA
*data
)
1328 WIN32_FIND_DATAW dataW
;
1330 if (!FindNextFileW( handle
, &dataW
)) return FALSE
;
1331 data
->dwFileAttributes
= dataW
.dwFileAttributes
;
1332 data
->ftCreationTime
= dataW
.ftCreationTime
;
1333 data
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
1334 data
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
1335 data
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
1336 data
->nFileSizeLow
= dataW
.nFileSizeLow
;
1337 file_name_WtoA( dataW
.cFileName
, -1, data
->cFileName
, sizeof(data
->cFileName
) );
1338 file_name_WtoA( dataW
.cAlternateFileName
, -1, data
->cAlternateFileName
,
1339 sizeof(data
->cAlternateFileName
) );
1344 /******************************************************************************
1345 * FindNextFileW (kernelbase.@)
1347 BOOL WINAPI DECLSPEC_HOTPATCH
FindNextFileW( HANDLE handle
, WIN32_FIND_DATAW
*data
)
1349 FIND_FIRST_INFO
*info
= handle
;
1350 FILE_BOTH_DIR_INFORMATION
*dir_info
;
1354 TRACE( "%p %p\n", handle
, data
);
1356 if (!handle
|| handle
== INVALID_HANDLE_VALUE
|| info
->magic
!= FIND_FIRST_MAGIC
)
1358 SetLastError( ERROR_INVALID_HANDLE
);
1362 RtlEnterCriticalSection( &info
->cs
);
1364 if (!info
->handle
) SetLastError( ERROR_NO_MORE_FILES
);
1367 if (info
->data_pos
>= info
->data_len
) /* need to read some more data */
1371 if (info
->data_size
)
1372 status
= NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, info
->data_size
,
1373 FileBothDirectoryInformation
, FALSE
, NULL
, FALSE
);
1375 status
= STATUS_NO_MORE_FILES
;
1377 if (!set_ntstatus( status
))
1379 if (status
== STATUS_NO_MORE_FILES
)
1381 CloseHandle( info
->handle
);
1386 info
->data_len
= io
.Information
;
1390 dir_info
= (FILE_BOTH_DIR_INFORMATION
*)(info
->data
+ info
->data_pos
);
1392 if (dir_info
->NextEntryOffset
) info
->data_pos
+= dir_info
->NextEntryOffset
;
1393 else info
->data_pos
= info
->data_len
;
1395 /* don't return '.' and '..' in the root of the drive */
1398 if (dir_info
->FileNameLength
== sizeof(WCHAR
) && dir_info
->FileName
[0] == '.') continue;
1399 if (dir_info
->FileNameLength
== 2 * sizeof(WCHAR
) &&
1400 dir_info
->FileName
[0] == '.' && dir_info
->FileName
[1] == '.') continue;
1403 data
->dwFileAttributes
= dir_info
->FileAttributes
;
1404 data
->ftCreationTime
= *(FILETIME
*)&dir_info
->CreationTime
;
1405 data
->ftLastAccessTime
= *(FILETIME
*)&dir_info
->LastAccessTime
;
1406 data
->ftLastWriteTime
= *(FILETIME
*)&dir_info
->LastWriteTime
;
1407 data
->nFileSizeHigh
= dir_info
->EndOfFile
.QuadPart
>> 32;
1408 data
->nFileSizeLow
= (DWORD
)dir_info
->EndOfFile
.QuadPart
;
1409 data
->dwReserved0
= 0;
1410 data
->dwReserved1
= 0;
1412 memcpy( data
->cFileName
, dir_info
->FileName
, dir_info
->FileNameLength
);
1413 data
->cFileName
[dir_info
->FileNameLength
/sizeof(WCHAR
)] = 0;
1415 if (info
->level
!= FindExInfoBasic
)
1417 memcpy( data
->cAlternateFileName
, dir_info
->ShortName
, dir_info
->ShortNameLength
);
1418 data
->cAlternateFileName
[dir_info
->ShortNameLength
/sizeof(WCHAR
)] = 0;
1421 data
->cAlternateFileName
[0] = 0;
1423 TRACE( "returning %s (%s)\n",
1424 debugstr_w(data
->cFileName
), debugstr_w(data
->cAlternateFileName
) );
1430 RtlLeaveCriticalSection( &info
->cs
);
1435 /**************************************************************************
1436 * FindNextStreamW (kernelbase.@)
1438 BOOL WINAPI
FindNextStreamW( HANDLE handle
, void *data
)
1440 FIXME( "(%p, %p): stub!\n", handle
, data
);
1441 SetLastError( ERROR_HANDLE_EOF
);
1446 /******************************************************************************
1447 * FindClose (kernelbase.@)
1449 BOOL WINAPI DECLSPEC_HOTPATCH
FindClose( HANDLE handle
)
1451 FIND_FIRST_INFO
*info
= handle
;
1453 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
1455 SetLastError( ERROR_INVALID_HANDLE
);
1461 if (info
->magic
== FIND_FIRST_MAGIC
)
1463 RtlEnterCriticalSection( &info
->cs
);
1464 if (info
->magic
== FIND_FIRST_MAGIC
) /* in case someone else freed it in the meantime */
1467 if (info
->handle
) CloseHandle( info
->handle
);
1469 RtlFreeUnicodeString( &info
->path
);
1472 RtlLeaveCriticalSection( &info
->cs
);
1473 info
->cs
.DebugInfo
->Spare
[0] = 0;
1474 RtlDeleteCriticalSection( &info
->cs
);
1475 HeapFree( GetProcessHeap(), 0, info
);
1481 WARN( "illegal handle %p\n", handle
);
1482 SetLastError( ERROR_INVALID_HANDLE
);
1491 /******************************************************************************
1492 * GetCompressedFileSizeA (kernelbase.@)
1494 DWORD WINAPI DECLSPEC_HOTPATCH
GetCompressedFileSizeA( LPCSTR name
, LPDWORD size_high
)
1498 if (!(nameW
= file_name_AtoW( name
, FALSE
))) return INVALID_FILE_SIZE
;
1499 return GetCompressedFileSizeW( nameW
, size_high
);
1503 /******************************************************************************
1504 * GetCompressedFileSizeW (kernelbase.@)
1506 DWORD WINAPI DECLSPEC_HOTPATCH
GetCompressedFileSizeW( LPCWSTR name
, LPDWORD size_high
)
1508 UNICODE_STRING nt_name
;
1509 OBJECT_ATTRIBUTES attr
;
1515 TRACE("%s %p\n", debugstr_w(name
), size_high
);
1517 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1519 SetLastError( ERROR_PATH_NOT_FOUND
);
1520 return INVALID_FILE_SIZE
;
1523 attr
.Length
= sizeof(attr
);
1524 attr
.RootDirectory
= 0;
1525 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1526 attr
.ObjectName
= &nt_name
;
1527 attr
.SecurityDescriptor
= NULL
;
1528 attr
.SecurityQualityOfService
= NULL
;
1530 status
= NtOpenFile( &handle
, SYNCHRONIZE
, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
1531 RtlFreeUnicodeString( &nt_name
);
1532 if (!set_ntstatus( status
)) return INVALID_FILE_SIZE
;
1534 /* we don't support compressed files, simply return the file size */
1535 ret
= GetFileSize( handle
, size_high
);
1541 /***********************************************************************
1542 * GetCurrentDirectoryA (kernelbase.@)
1544 UINT WINAPI DECLSPEC_HOTPATCH
GetCurrentDirectoryA( UINT buflen
, LPSTR buf
)
1546 WCHAR bufferW
[MAX_PATH
];
1549 if (buflen
&& buf
&& ((ULONG_PTR
)buf
>> 16) == 0)
1551 /* Win9x catches access violations here, returning zero.
1552 * This behaviour resulted in some people not noticing
1553 * that they got the argument order wrong. So let's be
1554 * nice and fail gracefully if buf is invalid and looks
1555 * more like a buflen. */
1556 SetLastError( ERROR_INVALID_PARAMETER
);
1560 ret
= RtlGetCurrentDirectory_U( sizeof(bufferW
), bufferW
);
1562 if (ret
> sizeof(bufferW
))
1564 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1567 return copy_filename_WtoA( bufferW
, buf
, buflen
);
1571 /***********************************************************************
1572 * GetCurrentDirectoryW (kernelbase.@)
1574 UINT WINAPI DECLSPEC_HOTPATCH
GetCurrentDirectoryW( UINT buflen
, LPWSTR buf
)
1576 return RtlGetCurrentDirectory_U( buflen
* sizeof(WCHAR
), buf
) / sizeof(WCHAR
);
1580 /**************************************************************************
1581 * GetFileAttributesA (kernelbase.@)
1583 DWORD WINAPI DECLSPEC_HOTPATCH
GetFileAttributesA( LPCSTR name
)
1587 if (!(nameW
= file_name_AtoW( name
, FALSE
))) return INVALID_FILE_ATTRIBUTES
;
1588 return GetFileAttributesW( nameW
);
1592 /**************************************************************************
1593 * GetFileAttributesW (kernelbase.@)
1595 DWORD WINAPI DECLSPEC_HOTPATCH
GetFileAttributesW( LPCWSTR name
)
1597 FILE_BASIC_INFORMATION info
;
1598 UNICODE_STRING nt_name
;
1599 OBJECT_ATTRIBUTES attr
;
1602 TRACE( "%s\n", debugstr_w(name
) );
1604 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1606 SetLastError( ERROR_PATH_NOT_FOUND
);
1607 return INVALID_FILE_ATTRIBUTES
;
1610 attr
.Length
= sizeof(attr
);
1611 attr
.RootDirectory
= 0;
1612 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1613 attr
.ObjectName
= &nt_name
;
1614 attr
.SecurityDescriptor
= NULL
;
1615 attr
.SecurityQualityOfService
= NULL
;
1617 status
= NtQueryAttributesFile( &attr
, &info
);
1618 RtlFreeUnicodeString( &nt_name
);
1620 if (status
== STATUS_SUCCESS
) return info
.FileAttributes
;
1622 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
1623 if (RtlIsDosDeviceName_U( name
)) return FILE_ATTRIBUTE_ARCHIVE
;
1625 SetLastError( RtlNtStatusToDosError(status
) );
1626 return INVALID_FILE_ATTRIBUTES
;
1630 /**************************************************************************
1631 * GetFileAttributesExA (kernelbase.@)
1633 BOOL WINAPI DECLSPEC_HOTPATCH
GetFileAttributesExA( LPCSTR name
, GET_FILEEX_INFO_LEVELS level
, void *ptr
)
1637 if (!(nameW
= file_name_AtoW( name
, FALSE
))) return FALSE
;
1638 return GetFileAttributesExW( nameW
, level
, ptr
);
1642 /**************************************************************************
1643 * GetFileAttributesExW (kernelbase.@)
1645 BOOL WINAPI DECLSPEC_HOTPATCH
GetFileAttributesExW( LPCWSTR name
, GET_FILEEX_INFO_LEVELS level
, void *ptr
)
1647 FILE_NETWORK_OPEN_INFORMATION info
;
1648 WIN32_FILE_ATTRIBUTE_DATA
*data
= ptr
;
1649 UNICODE_STRING nt_name
;
1650 OBJECT_ATTRIBUTES attr
;
1653 TRACE("%s %d %p\n", debugstr_w(name
), level
, ptr
);
1655 if (level
!= GetFileExInfoStandard
)
1657 SetLastError( ERROR_INVALID_PARAMETER
);
1661 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1663 SetLastError( ERROR_PATH_NOT_FOUND
);
1667 attr
.Length
= sizeof(attr
);
1668 attr
.RootDirectory
= 0;
1669 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1670 attr
.ObjectName
= &nt_name
;
1671 attr
.SecurityDescriptor
= NULL
;
1672 attr
.SecurityQualityOfService
= NULL
;
1674 status
= NtQueryFullAttributesFile( &attr
, &info
);
1675 RtlFreeUnicodeString( &nt_name
);
1676 if (!set_ntstatus( status
)) return FALSE
;
1678 data
->dwFileAttributes
= info
.FileAttributes
;
1679 data
->ftCreationTime
.dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
1680 data
->ftCreationTime
.dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
1681 data
->ftLastAccessTime
.dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
1682 data
->ftLastAccessTime
.dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
1683 data
->ftLastWriteTime
.dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
1684 data
->ftLastWriteTime
.dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
1685 data
->nFileSizeLow
= info
.EndOfFile
.u
.LowPart
;
1686 data
->nFileSizeHigh
= info
.EndOfFile
.u
.HighPart
;
1691 /***********************************************************************
1692 * GetFinalPathNameByHandleA (kernelbase.@)
1694 DWORD WINAPI DECLSPEC_HOTPATCH
GetFinalPathNameByHandleA( HANDLE file
, LPSTR path
,
1695 DWORD count
, DWORD flags
)
1700 TRACE( "(%p,%p,%d,%x)\n", file
, path
, count
, flags
);
1702 len
= GetFinalPathNameByHandleW(file
, NULL
, 0, flags
);
1703 if (len
== 0) return 0;
1705 str
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1708 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1712 result
= GetFinalPathNameByHandleW(file
, str
, len
, flags
);
1713 if (result
!= len
- 1)
1715 HeapFree(GetProcessHeap(), 0, str
);
1719 len
= file_name_WtoA( str
, -1, NULL
, 0 );
1722 HeapFree(GetProcessHeap(), 0, str
);
1725 file_name_WtoA( str
, -1, path
, count
);
1726 HeapFree(GetProcessHeap(), 0, str
);
1731 /***********************************************************************
1732 * GetFinalPathNameByHandleW (kernelbase.@)
1734 DWORD WINAPI DECLSPEC_HOTPATCH
GetFinalPathNameByHandleW( HANDLE file
, LPWSTR path
,
1735 DWORD count
, DWORD flags
)
1737 WCHAR buffer
[sizeof(OBJECT_NAME_INFORMATION
) + MAX_PATH
+ 1];
1738 OBJECT_NAME_INFORMATION
*info
= (OBJECT_NAME_INFORMATION
*)&buffer
;
1739 WCHAR drive_part
[MAX_PATH
];
1740 DWORD drive_part_len
= 0;
1746 TRACE( "(%p,%p,%d,%x)\n", file
, path
, count
, flags
);
1748 if (flags
& ~(FILE_NAME_OPENED
| VOLUME_NAME_GUID
| VOLUME_NAME_NONE
| VOLUME_NAME_NT
))
1750 WARN("Unknown flags: %x\n", flags
);
1751 SetLastError( ERROR_INVALID_PARAMETER
);
1755 /* get object name */
1756 status
= NtQueryObject( file
, ObjectNameInformation
, &buffer
, sizeof(buffer
) - sizeof(WCHAR
), &dummy
);
1757 if (!set_ntstatus( status
)) return 0;
1759 if (!info
->Name
.Buffer
)
1761 SetLastError( ERROR_INVALID_HANDLE
);
1764 if (info
->Name
.Length
< 4 * sizeof(WCHAR
) || info
->Name
.Buffer
[0] != '\\' ||
1765 info
->Name
.Buffer
[1] != '?' || info
->Name
.Buffer
[2] != '?' || info
->Name
.Buffer
[3] != '\\' )
1767 FIXME("Unexpected object name: %s\n", debugstr_wn(info
->Name
.Buffer
, info
->Name
.Length
/ sizeof(WCHAR
)));
1768 SetLastError( ERROR_GEN_FAILURE
);
1772 /* add terminating null character, remove "\\??\\" */
1773 info
->Name
.Buffer
[info
->Name
.Length
/ sizeof(WCHAR
)] = 0;
1774 info
->Name
.Length
-= 4 * sizeof(WCHAR
);
1775 info
->Name
.Buffer
+= 4;
1777 /* FILE_NAME_OPENED is not supported yet, and would require Wineserver changes */
1778 if (flags
& FILE_NAME_OPENED
)
1780 FIXME("FILE_NAME_OPENED not supported\n");
1781 flags
&= ~FILE_NAME_OPENED
;
1784 /* Get information required for VOLUME_NAME_NONE, VOLUME_NAME_GUID and VOLUME_NAME_NT */
1785 if (flags
== VOLUME_NAME_NONE
|| flags
== VOLUME_NAME_GUID
|| flags
== VOLUME_NAME_NT
)
1787 if (!GetVolumePathNameW( info
->Name
.Buffer
, drive_part
, MAX_PATH
)) return 0;
1788 drive_part_len
= lstrlenW(drive_part
);
1789 if (!drive_part_len
|| drive_part_len
> lstrlenW(info
->Name
.Buffer
) ||
1790 drive_part
[drive_part_len
-1] != '\\' ||
1791 CompareStringOrdinal( info
->Name
.Buffer
, drive_part_len
, drive_part
, drive_part_len
, TRUE
) != CSTR_EQUAL
)
1793 FIXME( "Path %s returned by GetVolumePathNameW does not match file path %s\n",
1794 debugstr_w(drive_part
), debugstr_w(info
->Name
.Buffer
) );
1795 SetLastError( ERROR_GEN_FAILURE
);
1800 if (flags
== VOLUME_NAME_NONE
)
1802 ptr
= info
->Name
.Buffer
+ drive_part_len
- 1;
1803 result
= lstrlenW(ptr
);
1804 if (result
< count
) memcpy(path
, ptr
, (result
+ 1) * sizeof(WCHAR
));
1807 else if (flags
== VOLUME_NAME_GUID
)
1809 WCHAR volume_prefix
[51];
1811 /* GetVolumeNameForVolumeMountPointW sets error code on failure */
1812 if (!GetVolumeNameForVolumeMountPointW( drive_part
, volume_prefix
, 50 )) return 0;
1813 ptr
= info
->Name
.Buffer
+ drive_part_len
;
1814 result
= lstrlenW(volume_prefix
) + lstrlenW(ptr
);
1817 lstrcpyW(path
, volume_prefix
);
1818 lstrcatW(path
, ptr
);
1822 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1826 else if (flags
== VOLUME_NAME_NT
)
1828 WCHAR nt_prefix
[MAX_PATH
];
1830 /* QueryDosDeviceW sets error code on failure */
1831 drive_part
[drive_part_len
- 1] = 0;
1832 if (!QueryDosDeviceW( drive_part
, nt_prefix
, MAX_PATH
)) return 0;
1833 ptr
= info
->Name
.Buffer
+ drive_part_len
- 1;
1834 result
= lstrlenW(nt_prefix
) + lstrlenW(ptr
);
1837 lstrcpyW(path
, nt_prefix
);
1838 lstrcatW(path
, ptr
);
1842 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1846 else if (flags
== VOLUME_NAME_DOS
)
1848 result
= 4 + lstrlenW(info
->Name
.Buffer
);
1851 lstrcpyW(path
, L
"\\\\?\\");
1852 lstrcatW(path
, info
->Name
.Buffer
);
1856 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1862 /* Windows crashes here, but we prefer returning ERROR_INVALID_PARAMETER */
1863 WARN("Invalid combination of flags: %x\n", flags
);
1864 SetLastError( ERROR_INVALID_PARAMETER
);
1870 /***********************************************************************
1871 * GetFullPathNameA (kernelbase.@)
1873 DWORD WINAPI DECLSPEC_HOTPATCH
GetFullPathNameA( LPCSTR name
, DWORD len
, LPSTR buffer
, LPSTR
*lastpart
)
1876 WCHAR bufferW
[MAX_PATH
], *lastpartW
= NULL
;
1879 if (!(nameW
= file_name_AtoW( name
, FALSE
))) return 0;
1881 ret
= GetFullPathNameW( nameW
, MAX_PATH
, bufferW
, &lastpartW
);
1886 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1889 ret
= copy_filename_WtoA( bufferW
, buffer
, len
);
1890 if (ret
< len
&& lastpart
)
1893 *lastpart
= buffer
+ file_name_WtoA( bufferW
, lastpartW
- bufferW
, NULL
, 0 );
1901 /***********************************************************************
1902 * GetFullPathNameW (kernelbase.@)
1904 DWORD WINAPI DECLSPEC_HOTPATCH
GetFullPathNameW( LPCWSTR name
, DWORD len
, LPWSTR buffer
, LPWSTR
*lastpart
)
1906 return RtlGetFullPathName_U( name
, len
* sizeof(WCHAR
), buffer
, lastpart
) / sizeof(WCHAR
);
1910 /***********************************************************************
1911 * GetLongPathNameA (kernelbase.@)
1913 DWORD WINAPI DECLSPEC_HOTPATCH
GetLongPathNameA( LPCSTR shortpath
, LPSTR longpath
, DWORD longlen
)
1916 WCHAR longpathW
[MAX_PATH
];
1919 TRACE( "%s\n", debugstr_a( shortpath
));
1921 if (!(shortpathW
= file_name_AtoW( shortpath
, FALSE
))) return 0;
1923 ret
= GetLongPathNameW( shortpathW
, longpathW
, MAX_PATH
);
1928 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1931 return copy_filename_WtoA( longpathW
, longpath
, longlen
);
1935 /***********************************************************************
1936 * GetLongPathNameW (kernelbase.@)
1938 DWORD WINAPI DECLSPEC_HOTPATCH
GetLongPathNameW( LPCWSTR shortpath
, LPWSTR longpath
, DWORD longlen
)
1940 WCHAR tmplongpath
[1024];
1941 DWORD sp
= 0, lp
= 0, tmplen
;
1942 WIN32_FIND_DATAW wfd
;
1943 UNICODE_STRING nameW
;
1947 TRACE("%s,%p,%u\n", debugstr_w(shortpath
), longpath
, longlen
);
1951 SetLastError( ERROR_INVALID_PARAMETER
);
1956 SetLastError( ERROR_PATH_NOT_FOUND
);
1960 if (shortpath
[0] == '\\' && shortpath
[1] == '\\')
1962 FIXME( "UNC pathname %s\n", debugstr_w(shortpath
) );
1963 tmplen
= lstrlenW( shortpath
);
1964 if (tmplen
< longlen
)
1966 if (longpath
!= shortpath
) lstrcpyW( longpath
, shortpath
);
1972 /* check for drive letter */
1973 if (shortpath
[0] != '/' && shortpath
[1] == ':' )
1975 tmplongpath
[0] = shortpath
[0];
1976 tmplongpath
[1] = ':';
1980 if (wcspbrk( shortpath
+ sp
, L
"*?" ))
1982 SetLastError( ERROR_INVALID_NAME
);
1986 while (shortpath
[sp
])
1988 /* check for path delimiters and reproduce them */
1989 if (shortpath
[sp
] == '\\' || shortpath
[sp
] == '/')
1991 tmplongpath
[lp
++] = shortpath
[sp
++];
1992 tmplongpath
[lp
] = 0; /* terminate string */
1996 for (p
= shortpath
+ sp
; *p
&& *p
!= '/' && *p
!= '\\'; p
++);
1997 tmplen
= p
- (shortpath
+ sp
);
1998 lstrcpynW( tmplongpath
+ lp
, shortpath
+ sp
, tmplen
+ 1 );
2000 if (tmplongpath
[lp
] == '.')
2002 if (tmplen
== 1 || (tmplen
== 2 && tmplongpath
[lp
+ 1] == '.'))
2010 /* Check if the file exists */
2011 handle
= FindFirstFileW( tmplongpath
, &wfd
);
2012 if (handle
== INVALID_HANDLE_VALUE
)
2014 TRACE( "not found %s\n", debugstr_w( tmplongpath
));
2015 SetLastError ( ERROR_FILE_NOT_FOUND
);
2018 FindClose( handle
);
2020 /* Use the existing file name if it's a short name */
2021 RtlInitUnicodeString( &nameW
, tmplongpath
+ lp
);
2022 if (RtlIsNameLegalDOS8Dot3( &nameW
, NULL
, NULL
)) lstrcpyW( tmplongpath
+ lp
, wfd
.cFileName
);
2023 lp
+= lstrlenW( tmplongpath
+ lp
);
2026 tmplen
= lstrlenW( shortpath
) - 1;
2027 if ((shortpath
[tmplen
] == '/' || shortpath
[tmplen
] == '\\') &&
2028 (tmplongpath
[lp
- 1] != '/' && tmplongpath
[lp
- 1] != '\\'))
2029 tmplongpath
[lp
++] = shortpath
[tmplen
];
2030 tmplongpath
[lp
] = 0;
2032 tmplen
= lstrlenW( tmplongpath
) + 1;
2033 if (tmplen
<= longlen
)
2035 lstrcpyW( longpath
, tmplongpath
);
2036 TRACE("returning %s\n", debugstr_w( longpath
));
2037 tmplen
--; /* length without 0 */
2043 /***********************************************************************
2044 * GetShortPathNameW (kernelbase.@)
2046 DWORD WINAPI DECLSPEC_HOTPATCH
GetShortPathNameW( LPCWSTR longpath
, LPWSTR shortpath
, DWORD shortlen
)
2048 WIN32_FIND_DATAW wfd
;
2049 WCHAR
*tmpshortpath
;
2052 DWORD sp
= 0, lp
= 0, tmplen
, buf_len
;
2054 TRACE( "%s,%p,%u\n", debugstr_w(longpath
), shortpath
, shortlen
);
2058 SetLastError( ERROR_INVALID_PARAMETER
);
2063 SetLastError( ERROR_BAD_PATHNAME
);
2067 /* code below only removes characters from string, never adds, so this is
2068 * the largest buffer that tmpshortpath will need to have */
2069 buf_len
= lstrlenW(longpath
) + 1;
2070 tmpshortpath
= HeapAlloc( GetProcessHeap(), 0, buf_len
* sizeof(WCHAR
) );
2073 SetLastError( ERROR_OUTOFMEMORY
);
2077 if (longpath
[0] == '\\' && longpath
[1] == '\\' && longpath
[2] == '?' && longpath
[3] == '\\')
2079 memcpy( tmpshortpath
, longpath
, 4 * sizeof(WCHAR
) );
2083 if (wcspbrk( longpath
+ lp
, L
"*?" ))
2085 HeapFree( GetProcessHeap(), 0, tmpshortpath
);
2086 SetLastError( ERROR_INVALID_NAME
);
2090 /* check for drive letter */
2091 if (longpath
[lp
] != '/' && longpath
[lp
+ 1] == ':' )
2093 tmpshortpath
[sp
] = longpath
[lp
];
2094 tmpshortpath
[sp
+ 1] = ':';
2099 while (longpath
[lp
])
2101 /* check for path delimiters and reproduce them */
2102 if (longpath
[lp
] == '\\' || longpath
[lp
] == '/')
2104 tmpshortpath
[sp
++] = longpath
[lp
++];
2105 tmpshortpath
[sp
] = 0; /* terminate string */
2110 for (; *p
&& *p
!= '/' && *p
!= '\\'; p
++);
2111 tmplen
= p
- (longpath
+ lp
);
2112 lstrcpynW( tmpshortpath
+ sp
, longpath
+ lp
, tmplen
+ 1 );
2114 if (tmpshortpath
[sp
] == '.')
2116 if (tmplen
== 1 || (tmplen
== 2 && tmpshortpath
[sp
+ 1] == '.'))
2124 /* Check if the file exists and use the existing short file name */
2125 handle
= FindFirstFileW( tmpshortpath
, &wfd
);
2126 if (handle
== INVALID_HANDLE_VALUE
) goto notfound
;
2127 FindClose( handle
);
2129 /* In rare cases (like "a.abcd") short path may be longer than original path.
2130 * Make sure we have enough space in temp buffer. */
2131 if (wfd
.cAlternateFileName
[0] && tmplen
< lstrlenW(wfd
.cAlternateFileName
))
2134 buf_len
+= lstrlenW( wfd
.cAlternateFileName
) - tmplen
;
2135 new_buf
= HeapReAlloc( GetProcessHeap(), 0, tmpshortpath
, buf_len
* sizeof(WCHAR
) );
2138 HeapFree( GetProcessHeap(), 0, tmpshortpath
);
2139 SetLastError( ERROR_OUTOFMEMORY
);
2142 tmpshortpath
= new_buf
;
2145 lstrcpyW( tmpshortpath
+ sp
, wfd
.cAlternateFileName
[0] ? wfd
.cAlternateFileName
: wfd
.cFileName
);
2146 sp
+= lstrlenW( tmpshortpath
+ sp
);
2149 tmpshortpath
[sp
] = 0;
2151 tmplen
= lstrlenW( tmpshortpath
) + 1;
2152 if (tmplen
<= shortlen
)
2154 lstrcpyW( shortpath
, tmpshortpath
);
2155 TRACE( "returning %s\n", debugstr_w( shortpath
));
2156 tmplen
--; /* length without 0 */
2159 HeapFree( GetProcessHeap(), 0, tmpshortpath
);
2163 HeapFree( GetProcessHeap(), 0, tmpshortpath
);
2164 TRACE( "not found\n" );
2165 SetLastError( ERROR_FILE_NOT_FOUND
);
2170 /***********************************************************************
2171 * GetSystemDirectoryA (kernelbase.@)
2173 UINT WINAPI DECLSPEC_HOTPATCH
GetSystemDirectoryA( LPSTR path
, UINT count
)
2175 return copy_filename_WtoA( system_dir
, path
, count
);
2179 /***********************************************************************
2180 * GetSystemDirectoryW (kernelbase.@)
2182 UINT WINAPI DECLSPEC_HOTPATCH
GetSystemDirectoryW( LPWSTR path
, UINT count
)
2184 return copy_filename( system_dir
, path
, count
);
2188 /***********************************************************************
2189 * GetSystemWindowsDirectoryA (kernelbase.@)
2191 UINT WINAPI DECLSPEC_HOTPATCH
GetSystemWindowsDirectoryA( LPSTR path
, UINT count
)
2193 return GetWindowsDirectoryA( path
, count
);
2197 /***********************************************************************
2198 * GetSystemWindowsDirectoryW (kernelbase.@)
2200 UINT WINAPI DECLSPEC_HOTPATCH
GetSystemWindowsDirectoryW( LPWSTR path
, UINT count
)
2202 return GetWindowsDirectoryW( path
, count
);
2206 /***********************************************************************
2207 * GetSystemWow64DirectoryA (kernelbase.@)
2209 UINT WINAPI
/* DECLSPEC_HOTPATCH */ GetSystemWow64DirectoryA( LPSTR path
, UINT count
)
2211 if (!is_win64
&& !is_wow64
)
2213 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
2216 return copy_filename_WtoA( get_machine_wow64_dir( IMAGE_FILE_MACHINE_I386
), path
, count
);
2220 /***********************************************************************
2221 * GetSystemWow64DirectoryW (kernelbase.@)
2223 UINT WINAPI
/* DECLSPEC_HOTPATCH */ GetSystemWow64DirectoryW( LPWSTR path
, UINT count
)
2225 if (!is_win64
&& !is_wow64
)
2227 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
2230 return copy_filename( get_machine_wow64_dir( IMAGE_FILE_MACHINE_I386
), path
, count
);
2234 /***********************************************************************
2235 * GetSystemWow64Directory2A (kernelbase.@)
2237 UINT WINAPI DECLSPEC_HOTPATCH
GetSystemWow64Directory2A( LPSTR path
, UINT count
, WORD machine
)
2239 const WCHAR
*dir
= get_machine_wow64_dir( machine
);
2241 return dir
? copy_filename_WtoA( dir
, path
, count
) : 0;
2245 /***********************************************************************
2246 * GetSystemWow64Directory2W (kernelbase.@)
2248 UINT WINAPI DECLSPEC_HOTPATCH
GetSystemWow64Directory2W( LPWSTR path
, UINT count
, WORD machine
)
2250 const WCHAR
*dir
= get_machine_wow64_dir( machine
);
2252 return dir
? copy_filename( dir
, path
, count
) : 0;
2256 /***********************************************************************
2257 * GetTempFileNameA (kernelbase.@)
2259 UINT WINAPI DECLSPEC_HOTPATCH
GetTempFileNameA( LPCSTR path
, LPCSTR prefix
, UINT unique
, LPSTR buffer
)
2261 WCHAR
*pathW
, *prefixW
= NULL
;
2262 WCHAR bufferW
[MAX_PATH
];
2265 if (!(pathW
= file_name_AtoW( path
, FALSE
))) return 0;
2266 if (prefix
&& !(prefixW
= file_name_AtoW( prefix
, TRUE
))) return 0;
2268 ret
= GetTempFileNameW( pathW
, prefixW
, unique
, bufferW
);
2269 if (ret
) file_name_WtoA( bufferW
, -1, buffer
, MAX_PATH
);
2271 HeapFree( GetProcessHeap(), 0, prefixW
);
2276 /***********************************************************************
2277 * GetTempFileNameW (kernelbase.@)
2279 UINT WINAPI DECLSPEC_HOTPATCH
GetTempFileNameW( LPCWSTR path
, LPCWSTR prefix
, UINT unique
, LPWSTR buffer
)
2285 if (!path
|| !buffer
)
2287 SetLastError( ERROR_INVALID_PARAMETER
);
2291 /* ensure that the provided directory exists */
2292 attr
= GetFileAttributesW( path
);
2293 if (attr
== INVALID_FILE_ATTRIBUTES
|| !(attr
& FILE_ATTRIBUTE_DIRECTORY
))
2295 TRACE( "path not found %s\n", debugstr_w( path
));
2296 SetLastError( ERROR_DIRECTORY
);
2300 lstrcpyW( buffer
, path
);
2301 p
= buffer
+ lstrlenW(buffer
);
2303 /* add a \, if there isn't one */
2304 if ((p
== buffer
) || (p
[-1] != '\\')) *p
++ = '\\';
2306 if (prefix
) for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
2309 if (unique
) swprintf( p
, MAX_PATH
- (p
- buffer
), L
"%x.tmp", unique
);
2312 /* get a "random" unique number and try to create the file */
2314 UINT num
= NtGetTickCount() & 0xffff;
2317 /* avoid using the same name twice in a short interval */
2318 if (last
- num
< 10) num
= last
+ 1;
2323 swprintf( p
, MAX_PATH
- (p
- buffer
), L
"%x.tmp", unique
);
2324 handle
= CreateFileW( buffer
, GENERIC_WRITE
, 0, NULL
, CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, 0 );
2325 if (handle
!= INVALID_HANDLE_VALUE
)
2326 { /* We created it */
2327 CloseHandle( handle
);
2331 if (GetLastError() != ERROR_FILE_EXISTS
&& GetLastError() != ERROR_SHARING_VIOLATION
)
2332 break; /* No need to go on */
2333 if (!(++unique
& 0xffff)) unique
= 1;
2334 } while (unique
!= num
);
2336 TRACE( "returning %s\n", debugstr_w( buffer
));
2341 /***********************************************************************
2342 * GetTempPathA (kernelbase.@)
2344 DWORD WINAPI DECLSPEC_HOTPATCH
GetTempPathA( DWORD count
, LPSTR path
)
2346 WCHAR pathW
[MAX_PATH
];
2349 if (!(ret
= GetTempPathW( MAX_PATH
, pathW
))) return 0;
2352 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
2355 return copy_filename_WtoA( pathW
, path
, count
);
2359 /***********************************************************************
2360 * GetTempPathW (kernelbase.@)
2362 DWORD WINAPI DECLSPEC_HOTPATCH
GetTempPathW( DWORD count
, LPWSTR path
)
2364 WCHAR tmp_path
[MAX_PATH
];
2367 if (!(ret
= GetEnvironmentVariableW( L
"TMP", tmp_path
, MAX_PATH
)) &&
2368 !(ret
= GetEnvironmentVariableW( L
"TEMP", tmp_path
, MAX_PATH
)) &&
2369 !(ret
= GetEnvironmentVariableW( L
"USERPROFILE", tmp_path
, MAX_PATH
)) &&
2370 !(ret
= GetWindowsDirectoryW( tmp_path
, MAX_PATH
)))
2375 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
2378 ret
= GetFullPathNameW( tmp_path
, MAX_PATH
, tmp_path
, NULL
);
2381 if (ret
> MAX_PATH
- 2)
2383 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
2386 if (tmp_path
[ret
-1] != '\\')
2388 tmp_path
[ret
++] = '\\';
2389 tmp_path
[ret
] = '\0';
2392 ret
++; /* add space for terminating 0 */
2395 lstrcpynW( path
, tmp_path
, count
);
2396 /* the remaining buffer must be zeroed up to 32766 bytes in XP or 32767
2397 * bytes after it, we will assume the > XP behavior for now */
2398 memset( path
+ ret
, 0, (min(count
, 32767) - ret
) * sizeof(WCHAR
) );
2399 ret
--; /* return length without 0 */
2403 /* the buffer must be cleared if contents will not fit */
2404 memset( path
, 0, count
* sizeof(WCHAR
) );
2407 TRACE( "returning %u, %s\n", ret
, debugstr_w( path
));
2412 /***********************************************************************
2413 * GetWindowsDirectoryA (kernelbase.@)
2415 UINT WINAPI DECLSPEC_HOTPATCH
GetWindowsDirectoryA( LPSTR path
, UINT count
)
2417 return copy_filename_WtoA( windows_dir
, path
, count
);
2421 /***********************************************************************
2422 * GetWindowsDirectoryW (kernelbase.@)
2424 UINT WINAPI DECLSPEC_HOTPATCH
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
2426 return copy_filename( windows_dir
, path
, count
);
2430 /**************************************************************************
2431 * MoveFileExW (kernelbase.@)
2433 BOOL WINAPI
MoveFileExW( const WCHAR
*source
, const WCHAR
*dest
, DWORD flag
)
2435 return MoveFileWithProgressW( source
, dest
, NULL
, NULL
, flag
);
2439 /**************************************************************************
2440 * MoveFileWithProgressW (kernelbase.@)
2442 BOOL WINAPI DECLSPEC_HOTPATCH
MoveFileWithProgressW( const WCHAR
*source
, const WCHAR
*dest
,
2443 LPPROGRESS_ROUTINE progress
,
2444 void *param
, DWORD flag
)
2446 FILE_RENAME_INFORMATION
*rename_info
;
2447 FILE_BASIC_INFORMATION info
;
2448 UNICODE_STRING nt_name
;
2449 OBJECT_ATTRIBUTES attr
;
2452 HANDLE source_handle
= 0;
2455 TRACE( "(%s,%s,%p,%p,%04x)\n", debugstr_w(source
), debugstr_w(dest
), progress
, param
, flag
);
2457 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) return add_boot_rename_entry( source
, dest
, flag
);
2459 if (!dest
) return DeleteFileW( source
);
2461 /* check if we are allowed to rename the source */
2463 if (!RtlDosPathNameToNtPathName_U( source
, &nt_name
, NULL
, NULL
))
2465 SetLastError( ERROR_PATH_NOT_FOUND
);
2468 attr
.Length
= sizeof(attr
);
2469 attr
.RootDirectory
= 0;
2470 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2471 attr
.ObjectName
= &nt_name
;
2472 attr
.SecurityDescriptor
= NULL
;
2473 attr
.SecurityQualityOfService
= NULL
;
2475 status
= NtOpenFile( &source_handle
, DELETE
| SYNCHRONIZE
, &attr
, &io
,
2476 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
2477 FILE_SYNCHRONOUS_IO_NONALERT
);
2478 RtlFreeUnicodeString( &nt_name
);
2479 if (!set_ntstatus( status
)) goto error
;
2481 status
= NtQueryInformationFile( source_handle
, &io
, &info
, sizeof(info
), FileBasicInformation
);
2482 if (!set_ntstatus( status
)) goto error
;
2484 if (!RtlDosPathNameToNtPathName_U( dest
, &nt_name
, NULL
, NULL
))
2486 SetLastError( ERROR_PATH_NOT_FOUND
);
2490 size
= offsetof( FILE_RENAME_INFORMATION
, FileName
) + nt_name
.Length
;
2491 if (!(rename_info
= HeapAlloc( GetProcessHeap(), 0, size
))) goto error
;
2493 rename_info
->ReplaceIfExists
= !!(flag
& MOVEFILE_REPLACE_EXISTING
);
2494 rename_info
->RootDirectory
= NULL
;
2495 rename_info
->FileNameLength
= nt_name
.Length
;
2496 memcpy( rename_info
->FileName
, nt_name
.Buffer
, nt_name
.Length
);
2497 RtlFreeUnicodeString( &nt_name
);
2498 status
= NtSetInformationFile( source_handle
, &io
, rename_info
, size
, FileRenameInformation
);
2499 HeapFree( GetProcessHeap(), 0, rename_info
);
2500 if (status
== STATUS_NOT_SAME_DEVICE
&& (flag
& MOVEFILE_COPY_ALLOWED
))
2502 NtClose( source_handle
);
2503 if (!CopyFileExW( source
, dest
, progress
, param
, NULL
,
2504 flag
& MOVEFILE_REPLACE_EXISTING
? 0 : COPY_FILE_FAIL_IF_EXISTS
))
2506 return DeleteFileW( source
);
2509 NtClose( source_handle
);
2510 return set_ntstatus( status
);
2513 if (source_handle
) NtClose( source_handle
);
2518 /***********************************************************************
2519 * NeedCurrentDirectoryForExePathA (kernelbase.@)
2521 BOOL WINAPI DECLSPEC_HOTPATCH
NeedCurrentDirectoryForExePathA( LPCSTR name
)
2525 if (!(nameW
= file_name_AtoW( name
, FALSE
))) return TRUE
;
2526 return NeedCurrentDirectoryForExePathW( nameW
);
2530 /***********************************************************************
2531 * NeedCurrentDirectoryForExePathW (kernelbase.@)
2533 BOOL WINAPI DECLSPEC_HOTPATCH
NeedCurrentDirectoryForExePathW( LPCWSTR name
)
2537 if (wcschr( name
, '\\' )) return TRUE
;
2538 /* check the existence of the variable, not value */
2539 return !GetEnvironmentVariableW( L
"NoDefaultCurrentDirectoryInExePath", &env_val
, 1 );
2543 /***********************************************************************
2544 * ReplaceFileW (kernelbase.@)
2546 BOOL WINAPI DECLSPEC_HOTPATCH
ReplaceFileW( const WCHAR
*replaced
, const WCHAR
*replacement
,
2547 const WCHAR
*backup
, DWORD flags
,
2548 void *exclude
, void *reserved
)
2550 UNICODE_STRING nt_replaced_name
, nt_replacement_name
;
2551 HANDLE hReplacement
= NULL
;
2554 OBJECT_ATTRIBUTES attr
;
2555 FILE_BASIC_INFORMATION info
;
2557 TRACE( "%s %s %s 0x%08x %p %p\n", debugstr_w(replaced
), debugstr_w(replacement
), debugstr_w(backup
),
2558 flags
, exclude
, reserved
);
2560 if (flags
) FIXME("Ignoring flags %x\n", flags
);
2562 /* First two arguments are mandatory */
2563 if (!replaced
|| !replacement
)
2565 SetLastError(ERROR_INVALID_PARAMETER
);
2569 attr
.Length
= sizeof(attr
);
2570 attr
.RootDirectory
= 0;
2571 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2572 attr
.ObjectName
= NULL
;
2573 attr
.SecurityDescriptor
= NULL
;
2574 attr
.SecurityQualityOfService
= NULL
;
2576 /* Open the "replaced" file for reading */
2577 if (!RtlDosPathNameToNtPathName_U( replaced
, &nt_replaced_name
, NULL
, NULL
))
2579 SetLastError( ERROR_PATH_NOT_FOUND
);
2582 attr
.ObjectName
= &nt_replaced_name
;
2584 /* Replacement should fail if replaced is READ_ONLY */
2585 status
= NtQueryAttributesFile(&attr
, &info
);
2586 RtlFreeUnicodeString(&nt_replaced_name
);
2587 if (!set_ntstatus( status
)) return FALSE
;
2589 if (info
.FileAttributes
& FILE_ATTRIBUTE_READONLY
)
2591 SetLastError( ERROR_ACCESS_DENIED
);
2596 * Open the replacement file for reading, writing, and deleting
2597 * (writing and deleting are needed when finished)
2599 if (!RtlDosPathNameToNtPathName_U( replacement
, &nt_replacement_name
, NULL
, NULL
))
2601 SetLastError( ERROR_PATH_NOT_FOUND
);
2604 attr
.ObjectName
= &nt_replacement_name
;
2605 status
= NtOpenFile( &hReplacement
, GENERIC_READ
| GENERIC_WRITE
| DELETE
| WRITE_DAC
| SYNCHRONIZE
,
2606 &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
| FILE_NON_DIRECTORY_FILE
);
2607 RtlFreeUnicodeString(&nt_replacement_name
);
2608 if (!set_ntstatus( status
)) return FALSE
;
2609 NtClose( hReplacement
);
2611 /* If the user wants a backup then that needs to be performed first */
2614 if (!MoveFileExW( replaced
, backup
, MOVEFILE_REPLACE_EXISTING
)) return FALSE
;
2618 /* ReplaceFile() can replace an open target. To do this, we need to move
2619 * it out of the way first. */
2620 WCHAR temp_path
[MAX_PATH
], temp_file
[MAX_PATH
];
2622 lstrcpynW( temp_path
, replaced
, ARRAY_SIZE( temp_path
) );
2623 PathRemoveFileSpecW( temp_path
);
2624 if (!GetTempFileNameW( temp_path
, L
"rf", 0, temp_file
) ||
2625 !MoveFileExW( replaced
, temp_file
, MOVEFILE_REPLACE_EXISTING
))
2628 DeleteFileW( temp_file
);
2632 * Now that the backup has been performed (if requested), copy the replacement
2635 if (!MoveFileExW( replacement
, replaced
, 0 ))
2637 /* on failure we need to indicate whether a backup was made */
2639 SetLastError( ERROR_UNABLE_TO_MOVE_REPLACEMENT
);
2641 SetLastError( ERROR_UNABLE_TO_MOVE_REPLACEMENT_2
);
2648 /***********************************************************************
2649 * SearchPathA (kernelbase.@)
2651 DWORD WINAPI DECLSPEC_HOTPATCH
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
2652 DWORD buflen
, LPSTR buffer
, LPSTR
*lastpart
)
2654 WCHAR
*pathW
= NULL
, *nameW
, *extW
= NULL
;
2655 WCHAR bufferW
[MAX_PATH
];
2660 SetLastError( ERROR_INVALID_PARAMETER
);
2664 if (!(nameW
= file_name_AtoW( name
, FALSE
))) return 0;
2665 if (path
&& !(pathW
= file_name_AtoW( path
, TRUE
))) return 0;
2666 if (ext
&& !(extW
= file_name_AtoW( ext
, TRUE
)))
2668 RtlFreeHeap( GetProcessHeap(), 0, pathW
);
2672 ret
= SearchPathW( pathW
, nameW
, extW
, MAX_PATH
, bufferW
, NULL
);
2674 RtlFreeHeap( GetProcessHeap(), 0, pathW
);
2675 RtlFreeHeap( GetProcessHeap(), 0, extW
);
2680 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
2683 ret
= copy_filename_WtoA( bufferW
, buffer
, buflen
);
2684 if (buflen
> ret
&& lastpart
) *lastpart
= strrchr(buffer
, '\\') + 1;
2689 /***********************************************************************
2690 * SearchPathW (kernelbase.@)
2692 DWORD WINAPI DECLSPEC_HOTPATCH
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
, DWORD buflen
,
2693 LPWSTR buffer
, LPWSTR
*lastpart
)
2698 if (!name
|| !name
[0])
2700 SetLastError( ERROR_INVALID_PARAMETER
);
2704 /* If the name contains an explicit path, ignore the path */
2706 if (contains_path( name
))
2708 /* try first without extension */
2709 if (RtlDoesFileExists_U( name
)) return GetFullPathNameW( name
, buflen
, buffer
, lastpart
);
2711 if ((name_ext
= append_ext( name
, ext
)))
2713 if (RtlDoesFileExists_U( name_ext
))
2714 ret
= GetFullPathNameW( name_ext
, buflen
, buffer
, lastpart
);
2715 RtlFreeHeap( GetProcessHeap(), 0, name_ext
);
2718 else if (path
&& path
[0]) /* search in the specified path */
2720 ret
= RtlDosSearchPath_U( path
, name
, ext
, buflen
* sizeof(WCHAR
),
2721 buffer
, lastpart
) / sizeof(WCHAR
);
2723 else /* search in active context and default path */
2725 WCHAR
*dll_path
= NULL
, *name_ext
= append_ext( name
, ext
);
2727 if (name_ext
) name
= name_ext
;
2729 /* When file is found with activation context no attempt is made
2730 to check if it's really exist, path is returned only basing on context info. */
2731 if (find_actctx_dllpath( name
, &dll_path
) == STATUS_SUCCESS
)
2733 ret
= lstrlenW( dll_path
) + lstrlenW( name
) + 1;
2735 /* count null termination char too */
2738 lstrcpyW( buffer
, dll_path
);
2739 lstrcatW( buffer
, name
);
2740 if (lastpart
) *lastpart
= buffer
+ lstrlenW( dll_path
);
2743 else if (lastpart
) *lastpart
= NULL
;
2744 RtlFreeHeap( GetProcessHeap(), 0, dll_path
);
2746 else if (!RtlGetSearchPath( &dll_path
))
2748 ret
= RtlDosSearchPath_U( dll_path
, name
, NULL
, buflen
* sizeof(WCHAR
),
2749 buffer
, lastpart
) / sizeof(WCHAR
);
2750 RtlReleasePath( dll_path
);
2752 RtlFreeHeap( GetProcessHeap(), 0, name_ext
);
2755 if (!ret
) SetLastError( ERROR_FILE_NOT_FOUND
);
2756 else TRACE( "found %s\n", debugstr_w(buffer
) );
2761 /***********************************************************************
2762 * SetCurrentDirectoryA (kernelbase.@)
2764 BOOL WINAPI DECLSPEC_HOTPATCH
SetCurrentDirectoryA( LPCSTR dir
)
2767 UNICODE_STRING strW
;
2769 if (!(dirW
= file_name_AtoW( dir
, FALSE
))) return FALSE
;
2770 RtlInitUnicodeString( &strW
, dirW
);
2771 return set_ntstatus( RtlSetCurrentDirectory_U( &strW
));
2775 /***********************************************************************
2776 * SetCurrentDirectoryW (kernelbase.@)
2778 BOOL WINAPI DECLSPEC_HOTPATCH
SetCurrentDirectoryW( LPCWSTR dir
)
2780 UNICODE_STRING dirW
;
2782 RtlInitUnicodeString( &dirW
, dir
);
2783 return set_ntstatus( RtlSetCurrentDirectory_U( &dirW
));
2787 /**************************************************************************
2788 * SetFileApisToANSI (kernelbase.@)
2790 void WINAPI DECLSPEC_HOTPATCH
SetFileApisToANSI(void)
2792 oem_file_apis
= FALSE
;
2796 /**************************************************************************
2797 * SetFileApisToOEM (kernelbase.@)
2799 void WINAPI DECLSPEC_HOTPATCH
SetFileApisToOEM(void)
2801 oem_file_apis
= TRUE
;
2805 /**************************************************************************
2806 * SetFileAttributesA (kernelbase.@)
2808 BOOL WINAPI DECLSPEC_HOTPATCH
SetFileAttributesA( LPCSTR name
, DWORD attributes
)
2812 if (!(nameW
= file_name_AtoW( name
, FALSE
))) return FALSE
;
2813 return SetFileAttributesW( nameW
, attributes
);
2817 /**************************************************************************
2818 * SetFileAttributesW (kernelbase.@)
2820 BOOL WINAPI DECLSPEC_HOTPATCH
SetFileAttributesW( LPCWSTR name
, DWORD attributes
)
2822 UNICODE_STRING nt_name
;
2823 OBJECT_ATTRIBUTES attr
;
2828 TRACE( "%s %x\n", debugstr_w(name
), attributes
);
2830 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2832 SetLastError( ERROR_PATH_NOT_FOUND
);
2836 attr
.Length
= sizeof(attr
);
2837 attr
.RootDirectory
= 0;
2838 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2839 attr
.ObjectName
= &nt_name
;
2840 attr
.SecurityDescriptor
= NULL
;
2841 attr
.SecurityQualityOfService
= NULL
;
2843 status
= NtOpenFile( &handle
, SYNCHRONIZE
, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
2844 RtlFreeUnicodeString( &nt_name
);
2846 if (status
== STATUS_SUCCESS
)
2848 FILE_BASIC_INFORMATION info
;
2850 memset( &info
, 0, sizeof(info
) );
2851 info
.FileAttributes
= attributes
| FILE_ATTRIBUTE_NORMAL
; /* make sure it's not zero */
2852 status
= NtSetInformationFile( handle
, &io
, &info
, sizeof(info
), FileBasicInformation
);
2855 return set_ntstatus( status
);
2859 /***********************************************************************
2860 * Wow64DisableWow64FsRedirection (kernelbase.@)
2862 BOOL WINAPI DECLSPEC_HOTPATCH
Wow64DisableWow64FsRedirection( PVOID
*old_value
)
2864 return set_ntstatus( RtlWow64EnableFsRedirectionEx( TRUE
, (ULONG
*)old_value
));
2868 /***********************************************************************
2869 * Wow64RevertWow64FsRedirection (kernelbase.@)
2871 BOOL WINAPI DECLSPEC_HOTPATCH
Wow64RevertWow64FsRedirection( PVOID old_value
)
2873 return set_ntstatus( RtlWow64EnableFsRedirection( !old_value
));
2877 /***********************************************************************
2878 * Operations on file handles
2879 ***********************************************************************/
2882 /***********************************************************************
2883 * CancelIo (kernelbase.@)
2885 BOOL WINAPI DECLSPEC_HOTPATCH
CancelIo( HANDLE handle
)
2889 NtCancelIoFile( handle
, &io
);
2890 return set_ntstatus( io
.u
.Status
);
2894 /***********************************************************************
2895 * CancelIoEx (kernelbase.@)
2897 BOOL WINAPI DECLSPEC_HOTPATCH
CancelIoEx( HANDLE handle
, LPOVERLAPPED overlapped
)
2901 NtCancelIoFileEx( handle
, (PIO_STATUS_BLOCK
)overlapped
, &io
);
2902 return set_ntstatus( io
.u
.Status
);
2906 /***********************************************************************
2907 * CancelSynchronousIo (kernelbase.@)
2909 BOOL WINAPI DECLSPEC_HOTPATCH
CancelSynchronousIo( HANDLE thread
)
2911 FIXME( "(%p): stub\n", thread
);
2912 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
2917 /***********************************************************************
2918 * FlushFileBuffers (kernelbase.@)
2920 BOOL WINAPI DECLSPEC_HOTPATCH
FlushFileBuffers( HANDLE file
)
2922 IO_STATUS_BLOCK iosb
;
2924 return set_ntstatus( NtFlushBuffersFile( file
, &iosb
));
2928 /***********************************************************************
2929 * GetFileInformationByHandle (kernelbase.@)
2931 BOOL WINAPI DECLSPEC_HOTPATCH
GetFileInformationByHandle( HANDLE file
, BY_HANDLE_FILE_INFORMATION
*info
)
2933 FILE_FS_VOLUME_INFORMATION volume_info
;
2934 FILE_ALL_INFORMATION all_info
;
2938 status
= NtQueryInformationFile( file
, &io
, &all_info
, sizeof(all_info
), FileAllInformation
);
2939 if (status
== STATUS_BUFFER_OVERFLOW
) status
= STATUS_SUCCESS
;
2940 if (!set_ntstatus( status
)) return FALSE
;
2942 info
->dwFileAttributes
= all_info
.BasicInformation
.FileAttributes
;
2943 info
->ftCreationTime
.dwHighDateTime
= all_info
.BasicInformation
.CreationTime
.u
.HighPart
;
2944 info
->ftCreationTime
.dwLowDateTime
= all_info
.BasicInformation
.CreationTime
.u
.LowPart
;
2945 info
->ftLastAccessTime
.dwHighDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.HighPart
;
2946 info
->ftLastAccessTime
.dwLowDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.LowPart
;
2947 info
->ftLastWriteTime
.dwHighDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.HighPart
;
2948 info
->ftLastWriteTime
.dwLowDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.LowPart
;
2949 info
->dwVolumeSerialNumber
= 0;
2950 info
->nFileSizeHigh
= all_info
.StandardInformation
.EndOfFile
.u
.HighPart
;
2951 info
->nFileSizeLow
= all_info
.StandardInformation
.EndOfFile
.u
.LowPart
;
2952 info
->nNumberOfLinks
= all_info
.StandardInformation
.NumberOfLinks
;
2953 info
->nFileIndexHigh
= all_info
.InternalInformation
.IndexNumber
.u
.HighPart
;
2954 info
->nFileIndexLow
= all_info
.InternalInformation
.IndexNumber
.u
.LowPart
;
2956 status
= NtQueryVolumeInformationFile( file
, &io
, &volume_info
, sizeof(volume_info
), FileFsVolumeInformation
);
2957 if (status
== STATUS_SUCCESS
|| status
== STATUS_BUFFER_OVERFLOW
)
2958 info
->dwVolumeSerialNumber
= volume_info
.VolumeSerialNumber
;
2964 /***********************************************************************
2965 * GetFileInformationByHandleEx (kernelbase.@)
2967 BOOL WINAPI DECLSPEC_HOTPATCH
GetFileInformationByHandleEx( HANDLE handle
, FILE_INFO_BY_HANDLE_CLASS
class,
2968 LPVOID info
, DWORD size
)
2975 case FileStreamInfo
:
2976 case FileCompressionInfo
:
2977 case FileRemoteProtocolInfo
:
2978 case FileFullDirectoryInfo
:
2979 case FileFullDirectoryRestartInfo
:
2980 case FileStorageInfo
:
2981 case FileAlignmentInfo
:
2982 case FileIdExtdDirectoryInfo
:
2983 case FileIdExtdDirectoryRestartInfo
:
2984 FIXME( "%p, %u, %p, %u\n", handle
, class, info
, size
);
2985 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
2988 case FileAttributeTagInfo
:
2989 status
= NtQueryInformationFile( handle
, &io
, info
, size
, FileAttributeTagInformation
);
2993 status
= NtQueryInformationFile( handle
, &io
, info
, size
, FileBasicInformation
);
2996 case FileStandardInfo
:
2997 status
= NtQueryInformationFile( handle
, &io
, info
, size
, FileStandardInformation
);
3001 status
= NtQueryInformationFile( handle
, &io
, info
, size
, FileNameInformation
);
3005 status
= NtQueryInformationFile( handle
, &io
, info
, size
, FileIdInformation
);
3008 case FileIdBothDirectoryRestartInfo
:
3009 case FileIdBothDirectoryInfo
:
3010 status
= NtQueryDirectoryFile( handle
, NULL
, NULL
, NULL
, &io
, info
, size
,
3011 FileIdBothDirectoryInformation
, FALSE
, NULL
,
3012 (class == FileIdBothDirectoryRestartInfo
) );
3015 case FileRenameInfo
:
3016 case FileDispositionInfo
:
3017 case FileAllocationInfo
:
3018 case FileIoPriorityHintInfo
:
3019 case FileEndOfFileInfo
:
3021 SetLastError( ERROR_INVALID_PARAMETER
);
3024 return set_ntstatus( status
);
3028 /***********************************************************************
3029 * GetFileSize (kernelbase.@)
3031 DWORD WINAPI DECLSPEC_HOTPATCH
GetFileSize( HANDLE file
, LPDWORD size_high
)
3035 if (!GetFileSizeEx( file
, &size
)) return INVALID_FILE_SIZE
;
3036 if (size_high
) *size_high
= size
.u
.HighPart
;
3037 if (size
.u
.LowPart
== INVALID_FILE_SIZE
) SetLastError( 0 );
3038 return size
.u
.LowPart
;
3042 /***********************************************************************
3043 * GetFileSizeEx (kernelbase.@)
3045 BOOL WINAPI DECLSPEC_HOTPATCH
GetFileSizeEx( HANDLE file
, PLARGE_INTEGER size
)
3047 FILE_STANDARD_INFORMATION info
;
3050 if (is_console_handle( file
))
3052 SetLastError( ERROR_INVALID_HANDLE
);
3056 if (!set_ntstatus( NtQueryInformationFile( file
, &io
, &info
, sizeof(info
), FileStandardInformation
)))
3059 *size
= info
.EndOfFile
;
3064 /***********************************************************************
3065 * GetFileTime (kernelbase.@)
3067 BOOL WINAPI DECLSPEC_HOTPATCH
GetFileTime( HANDLE file
, FILETIME
*creation
,
3068 FILETIME
*access
, FILETIME
*write
)
3070 FILE_BASIC_INFORMATION info
;
3073 if (!set_ntstatus( NtQueryInformationFile( file
, &io
, &info
, sizeof(info
), FileBasicInformation
)))
3078 creation
->dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
3079 creation
->dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
3083 access
->dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
3084 access
->dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
3088 write
->dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
3089 write
->dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
3095 /***********************************************************************
3096 * GetFileType (kernelbase.@)
3098 DWORD WINAPI DECLSPEC_HOTPATCH
GetFileType( HANDLE file
)
3100 FILE_FS_DEVICE_INFORMATION info
;
3103 if (file
== (HANDLE
)STD_INPUT_HANDLE
||
3104 file
== (HANDLE
)STD_OUTPUT_HANDLE
||
3105 file
== (HANDLE
)STD_ERROR_HANDLE
)
3106 file
= GetStdHandle( (DWORD_PTR
)file
);
3108 if (is_console_handle( file
)) return FILE_TYPE_CHAR
;
3110 if (!set_ntstatus( NtQueryVolumeInformationFile( file
, &io
, &info
, sizeof(info
),
3111 FileFsDeviceInformation
)))
3112 return FILE_TYPE_UNKNOWN
;
3114 switch (info
.DeviceType
)
3116 case FILE_DEVICE_NULL
:
3117 case FILE_DEVICE_SERIAL_PORT
:
3118 case FILE_DEVICE_PARALLEL_PORT
:
3119 case FILE_DEVICE_TAPE
:
3120 case FILE_DEVICE_UNKNOWN
:
3121 return FILE_TYPE_CHAR
;
3122 case FILE_DEVICE_NAMED_PIPE
:
3123 return FILE_TYPE_PIPE
;
3125 return FILE_TYPE_DISK
;
3130 /***********************************************************************
3131 * GetOverlappedResult (kernelbase.@)
3133 BOOL WINAPI DECLSPEC_HOTPATCH
GetOverlappedResult( HANDLE file
, LPOVERLAPPED overlapped
,
3134 LPDWORD result
, BOOL wait
)
3136 return GetOverlappedResultEx( file
, overlapped
, result
, wait
? INFINITE
: 0, FALSE
);
3140 /***********************************************************************
3141 * GetOverlappedResultEx (kernelbase.@)
3143 BOOL WINAPI DECLSPEC_HOTPATCH
GetOverlappedResultEx( HANDLE file
, OVERLAPPED
*overlapped
,
3144 DWORD
*result
, DWORD timeout
, BOOL alertable
)
3149 TRACE( "(%p %p %p %u %d)\n", file
, overlapped
, result
, timeout
, alertable
);
3151 status
= overlapped
->Internal
;
3152 if (status
== STATUS_PENDING
)
3156 SetLastError( ERROR_IO_INCOMPLETE
);
3159 ret
= WaitForSingleObjectEx( overlapped
->hEvent
? overlapped
->hEvent
: file
, timeout
, alertable
);
3160 if (ret
== WAIT_FAILED
)
3164 SetLastError( ret
);
3168 status
= overlapped
->Internal
;
3169 if (status
== STATUS_PENDING
) status
= STATUS_SUCCESS
;
3172 *result
= overlapped
->InternalHigh
;
3173 return set_ntstatus( status
);
3177 /**************************************************************************
3178 * LockFile (kernelbase.@)
3180 BOOL WINAPI DECLSPEC_HOTPATCH
LockFile( HANDLE file
, DWORD offset_low
, DWORD offset_high
,
3181 DWORD count_low
, DWORD count_high
)
3183 LARGE_INTEGER count
, offset
;
3185 TRACE( "%p %x%08x %x%08x\n", file
, offset_high
, offset_low
, count_high
, count_low
);
3187 count
.u
.LowPart
= count_low
;
3188 count
.u
.HighPart
= count_high
;
3189 offset
.u
.LowPart
= offset_low
;
3190 offset
.u
.HighPart
= offset_high
;
3191 return set_ntstatus( NtLockFile( file
, 0, NULL
, NULL
, NULL
, &offset
, &count
, NULL
, TRUE
, TRUE
));
3195 /**************************************************************************
3196 * LockFileEx (kernelbase.@)
3198 BOOL WINAPI DECLSPEC_HOTPATCH
LockFileEx( HANDLE file
, DWORD flags
, DWORD reserved
,
3199 DWORD count_low
, DWORD count_high
, LPOVERLAPPED overlapped
)
3201 LARGE_INTEGER count
, offset
;
3202 LPVOID cvalue
= NULL
;
3206 SetLastError( ERROR_INVALID_PARAMETER
);
3210 TRACE( "%p %x%08x %x%08x flags %x\n",
3211 file
, overlapped
->u
.s
.OffsetHigh
, overlapped
->u
.s
.Offset
, count_high
, count_low
, flags
);
3213 count
.u
.LowPart
= count_low
;
3214 count
.u
.HighPart
= count_high
;
3215 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
3216 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
3218 if (((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) cvalue
= overlapped
;
3220 return set_ntstatus( NtLockFile( file
, overlapped
->hEvent
, NULL
, cvalue
,
3221 NULL
, &offset
, &count
, NULL
,
3222 flags
& LOCKFILE_FAIL_IMMEDIATELY
,
3223 flags
& LOCKFILE_EXCLUSIVE_LOCK
));
3227 /***********************************************************************
3228 * OpenFileById (kernelbase.@)
3230 HANDLE WINAPI DECLSPEC_HOTPATCH
OpenFileById( HANDLE handle
, LPFILE_ID_DESCRIPTOR id
, DWORD access
,
3231 DWORD share
, LPSECURITY_ATTRIBUTES sec_attr
, DWORD flags
)
3235 OBJECT_ATTRIBUTES attr
;
3237 UNICODE_STRING objectName
;
3241 SetLastError( ERROR_INVALID_PARAMETER
);
3242 return INVALID_HANDLE_VALUE
;
3245 options
= FILE_OPEN_BY_FILE_ID
;
3246 if (flags
& FILE_FLAG_BACKUP_SEMANTICS
)
3247 options
|= FILE_OPEN_FOR_BACKUP_INTENT
;
3249 options
|= FILE_NON_DIRECTORY_FILE
;
3250 if (flags
& FILE_FLAG_NO_BUFFERING
) options
|= FILE_NO_INTERMEDIATE_BUFFERING
;
3251 if (!(flags
& FILE_FLAG_OVERLAPPED
)) options
|= FILE_SYNCHRONOUS_IO_NONALERT
;
3252 if (flags
& FILE_FLAG_RANDOM_ACCESS
) options
|= FILE_RANDOM_ACCESS
;
3253 flags
&= FILE_ATTRIBUTE_VALID_FLAGS
;
3255 objectName
.Length
= sizeof(ULONGLONG
);
3256 objectName
.Buffer
= (WCHAR
*)&id
->u
.FileId
;
3257 attr
.Length
= sizeof(attr
);
3258 attr
.RootDirectory
= handle
;
3259 attr
.Attributes
= 0;
3260 attr
.ObjectName
= &objectName
;
3261 attr
.SecurityDescriptor
= sec_attr
? sec_attr
->lpSecurityDescriptor
: NULL
;
3262 attr
.SecurityQualityOfService
= NULL
;
3263 if (sec_attr
&& sec_attr
->bInheritHandle
) attr
.Attributes
|= OBJ_INHERIT
;
3265 if (!set_ntstatus( NtCreateFile( &result
, access
| SYNCHRONIZE
, &attr
, &io
, NULL
, flags
,
3266 share
, OPEN_EXISTING
, options
, NULL
, 0 )))
3267 return INVALID_HANDLE_VALUE
;
3272 /***********************************************************************
3273 * ReOpenFile (kernelbase.@)
3275 HANDLE WINAPI DECLSPEC_HOTPATCH
ReOpenFile( HANDLE handle
, DWORD access
, DWORD sharing
, DWORD attributes
)
3277 SECURITY_QUALITY_OF_SERVICE qos
;
3278 OBJECT_ATTRIBUTES attr
;
3279 UNICODE_STRING empty
= { 0 };
3284 TRACE("handle %p, access %#x, sharing %#x, attributes %#x.\n", handle
, access
, sharing
, attributes
);
3286 if (attributes
& 0x7ffff) /* FILE_ATTRIBUTE_* flags are invalid */
3288 SetLastError(ERROR_INVALID_PARAMETER
);
3289 return INVALID_HANDLE_VALUE
;
3292 if (attributes
& FILE_FLAG_DELETE_ON_CLOSE
)
3295 InitializeObjectAttributes( &attr
, &empty
, OBJ_CASE_INSENSITIVE
, handle
, NULL
);
3296 if (attributes
& SECURITY_SQOS_PRESENT
)
3298 qos
.Length
= sizeof(qos
);
3299 qos
.ImpersonationLevel
= (attributes
>> 16) & 0x3;
3300 qos
.ContextTrackingMode
= attributes
& SECURITY_CONTEXT_TRACKING
? SECURITY_DYNAMIC_TRACKING
: SECURITY_STATIC_TRACKING
;
3301 qos
.EffectiveOnly
= (attributes
& SECURITY_EFFECTIVE_ONLY
) != 0;
3302 attr
.SecurityQualityOfService
= &qos
;
3305 status
= NtCreateFile( &file
, access
| SYNCHRONIZE
| FILE_READ_ATTRIBUTES
, &attr
, &io
, NULL
,
3306 0, sharing
, FILE_OPEN
, get_nt_file_options( attributes
), NULL
, 0 );
3307 if (!set_ntstatus( status
))
3308 return INVALID_HANDLE_VALUE
;
3313 static void WINAPI
invoke_completion( void *context
, IO_STATUS_BLOCK
*io
, ULONG res
)
3315 LPOVERLAPPED_COMPLETION_ROUTINE completion
= context
;
3316 completion( io
->u
.Status
, io
->Information
, (LPOVERLAPPED
)io
);
3319 /****************************************************************************
3320 * ReadDirectoryChangesW (kernelbase.@)
3322 BOOL WINAPI DECLSPEC_HOTPATCH
ReadDirectoryChangesW( HANDLE handle
, LPVOID buffer
, DWORD len
,
3323 BOOL subtree
, DWORD filter
, LPDWORD returned
,
3324 LPOVERLAPPED overlapped
,
3325 LPOVERLAPPED_COMPLETION_ROUTINE completion
)
3327 OVERLAPPED ov
, *pov
;
3328 IO_STATUS_BLOCK
*ios
;
3330 LPVOID cvalue
= NULL
;
3332 TRACE( "%p %p %08x %d %08x %p %p %p\n",
3333 handle
, buffer
, len
, subtree
, filter
, returned
, overlapped
, completion
);
3337 memset( &ov
, 0, sizeof ov
);
3338 ov
.hEvent
= CreateEventW( NULL
, 0, 0, NULL
);
3344 if (completion
) cvalue
= completion
;
3345 else if (((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) cvalue
= overlapped
;
3348 ios
= (PIO_STATUS_BLOCK
)pov
;
3349 ios
->u
.Status
= STATUS_PENDING
;
3351 status
= NtNotifyChangeDirectoryFile( handle
, completion
&& overlapped
? NULL
: pov
->hEvent
,
3352 completion
&& overlapped
? invoke_completion
: NULL
,
3353 cvalue
, ios
, buffer
, len
, filter
, subtree
);
3354 if (status
== STATUS_PENDING
)
3356 if (overlapped
) return TRUE
;
3357 WaitForSingleObjectEx( ov
.hEvent
, INFINITE
, TRUE
);
3358 if (returned
) *returned
= ios
->Information
;
3359 status
= ios
->u
.Status
;
3361 if (!overlapped
) CloseHandle( ov
.hEvent
);
3362 return set_ntstatus( status
);
3366 /***********************************************************************
3367 * ReadFile (kernelbase.@)
3369 BOOL WINAPI DECLSPEC_HOTPATCH
ReadFile( HANDLE file
, LPVOID buffer
, DWORD count
,
3370 LPDWORD result
, LPOVERLAPPED overlapped
)
3372 LARGE_INTEGER offset
;
3373 PLARGE_INTEGER poffset
= NULL
;
3374 IO_STATUS_BLOCK iosb
;
3375 PIO_STATUS_BLOCK io_status
= &iosb
;
3378 LPVOID cvalue
= NULL
;
3380 TRACE( "%p %p %d %p %p\n", file
, buffer
, count
, result
, overlapped
);
3382 if (result
) *result
= 0;
3386 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
3387 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
3389 event
= overlapped
->hEvent
;
3390 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
3391 if (((ULONG_PTR
)event
& 1) == 0) cvalue
= overlapped
;
3393 else io_status
->Information
= 0;
3394 io_status
->u
.Status
= STATUS_PENDING
;
3396 status
= NtReadFile( file
, event
, NULL
, cvalue
, io_status
, buffer
, count
, poffset
, NULL
);
3398 if (status
== STATUS_PENDING
&& !overlapped
)
3400 WaitForSingleObject( file
, INFINITE
);
3401 status
= io_status
->u
.Status
;
3404 if (result
) *result
= overlapped
&& status
? 0 : io_status
->Information
;
3406 if (status
== STATUS_END_OF_FILE
)
3408 if (overlapped
!= NULL
)
3410 SetLastError( RtlNtStatusToDosError(status
) );
3414 else if (status
&& status
!= STATUS_TIMEOUT
)
3416 SetLastError( RtlNtStatusToDosError(status
) );
3423 /***********************************************************************
3424 * ReadFileEx (kernelbase.@)
3426 BOOL WINAPI DECLSPEC_HOTPATCH
ReadFileEx( HANDLE file
, LPVOID buffer
, DWORD count
, LPOVERLAPPED overlapped
,
3427 LPOVERLAPPED_COMPLETION_ROUTINE completion
)
3429 PIO_STATUS_BLOCK io
;
3430 LARGE_INTEGER offset
;
3433 TRACE( "(file=%p, buffer=%p, bytes=%u, ovl=%p, ovl_fn=%p)\n",
3434 file
, buffer
, count
, overlapped
, completion
);
3438 SetLastError( ERROR_INVALID_PARAMETER
);
3441 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
3442 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
3443 io
= (PIO_STATUS_BLOCK
)overlapped
;
3444 io
->u
.Status
= STATUS_PENDING
;
3445 io
->Information
= 0;
3447 status
= NtReadFile( file
, NULL
, read_write_apc
, completion
, io
, buffer
, count
, &offset
, NULL
);
3448 if (status
== STATUS_PENDING
) return TRUE
;
3449 return set_ntstatus( status
);
3453 /***********************************************************************
3454 * ReadFileScatter (kernelbase.@)
3456 BOOL WINAPI DECLSPEC_HOTPATCH
ReadFileScatter( HANDLE file
, FILE_SEGMENT_ELEMENT
*segments
, DWORD count
,
3457 LPDWORD reserved
, LPOVERLAPPED overlapped
)
3459 PIO_STATUS_BLOCK io
;
3460 LARGE_INTEGER offset
;
3461 void *cvalue
= NULL
;
3463 TRACE( "(%p %p %u %p)\n", file
, segments
, count
, overlapped
);
3465 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
3466 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
3467 if (!((ULONG_PTR
)overlapped
->hEvent
& 1)) cvalue
= overlapped
;
3468 io
= (PIO_STATUS_BLOCK
)overlapped
;
3469 io
->u
.Status
= STATUS_PENDING
;
3470 io
->Information
= 0;
3472 return set_ntstatus( NtReadFileScatter( file
, overlapped
->hEvent
, NULL
, cvalue
, io
,
3473 segments
, count
, &offset
, NULL
));
3477 /***********************************************************************
3478 * RemoveDirectoryA (kernelbase.@)
3480 BOOL WINAPI DECLSPEC_HOTPATCH
RemoveDirectoryA( LPCSTR path
)
3484 if (!(pathW
= file_name_AtoW( path
, FALSE
))) return FALSE
;
3485 return RemoveDirectoryW( pathW
);
3489 /***********************************************************************
3490 * RemoveDirectoryW (kernelbase.@)
3492 BOOL WINAPI DECLSPEC_HOTPATCH
RemoveDirectoryW( LPCWSTR path
)
3494 OBJECT_ATTRIBUTES attr
;
3495 UNICODE_STRING nt_name
;
3500 TRACE( "%s\n", debugstr_w(path
) );
3502 status
= RtlDosPathNameToNtPathName_U_WithStatus( path
, &nt_name
, NULL
, NULL
);
3503 if (!set_ntstatus( status
)) return FALSE
;
3505 InitializeObjectAttributes( &attr
, &nt_name
, OBJ_CASE_INSENSITIVE
, 0, NULL
);
3506 status
= NtOpenFile( &handle
, DELETE
| SYNCHRONIZE
, &attr
, &io
,
3507 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
3508 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
3509 RtlFreeUnicodeString( &nt_name
);
3513 FILE_DISPOSITION_INFORMATION info
= { TRUE
};
3514 status
= NtSetInformationFile( handle
, &io
, &info
, sizeof(info
), FileDispositionInformation
);
3517 return set_ntstatus( status
);
3521 /**************************************************************************
3522 * SetEndOfFile (kernelbase.@)
3524 BOOL WINAPI DECLSPEC_HOTPATCH
SetEndOfFile( HANDLE file
)
3526 FILE_POSITION_INFORMATION pos
;
3527 FILE_END_OF_FILE_INFORMATION eof
;
3531 if (!(status
= NtQueryInformationFile( file
, &io
, &pos
, sizeof(pos
), FilePositionInformation
)))
3533 eof
.EndOfFile
= pos
.CurrentByteOffset
;
3534 status
= NtSetInformationFile( file
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
);
3536 return set_ntstatus( status
);
3540 /***********************************************************************
3541 * SetFileInformationByHandle (kernelbase.@)
3543 BOOL WINAPI DECLSPEC_HOTPATCH
SetFileInformationByHandle( HANDLE file
, FILE_INFO_BY_HANDLE_CLASS
class,
3544 void *info
, DWORD size
)
3549 TRACE( "%p %u %p %u\n", file
, class, info
, size
);
3554 case FileRenameInfo
:
3555 case FileAllocationInfo
:
3556 case FileEndOfFileInfo
:
3557 case FileStreamInfo
:
3558 case FileIdBothDirectoryInfo
:
3559 case FileIdBothDirectoryRestartInfo
:
3560 case FileFullDirectoryInfo
:
3561 case FileFullDirectoryRestartInfo
:
3562 case FileStorageInfo
:
3563 case FileAlignmentInfo
:
3565 case FileIdExtdDirectoryInfo
:
3566 case FileIdExtdDirectoryRestartInfo
:
3567 FIXME( "%p, %u, %p, %u\n", file
, class, info
, size
);
3568 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
3572 status
= NtSetInformationFile( file
, &io
, info
, size
, FileBasicInformation
);
3574 case FileDispositionInfo
:
3575 status
= NtSetInformationFile( file
, &io
, info
, size
, FileDispositionInformation
);
3577 case FileIoPriorityHintInfo
:
3578 status
= NtSetInformationFile( file
, &io
, info
, size
, FileIoPriorityHintInformation
);
3580 case FileStandardInfo
:
3581 case FileCompressionInfo
:
3582 case FileAttributeTagInfo
:
3583 case FileRemoteProtocolInfo
:
3585 SetLastError( ERROR_INVALID_PARAMETER
);
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
;
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
)
3622 FILE_POSITION_INFORMATION info
;
3623 FILE_END_OF_FILE_INFORMATION eof
;
3628 pos
= distance
.QuadPart
;
3631 if (NtQueryInformationFile( file
, &io
, &info
, sizeof(info
), FilePositionInformation
))
3633 pos
= info
.CurrentByteOffset
.QuadPart
+ distance
.QuadPart
;
3636 if (NtQueryInformationFile( file
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
))
3638 pos
= eof
.EndOfFile
.QuadPart
+ distance
.QuadPart
;
3641 SetLastError( ERROR_INVALID_PARAMETER
);
3647 SetLastError( ERROR_NEGATIVE_SEEK
);
3651 info
.CurrentByteOffset
.QuadPart
= pos
;
3652 if (!NtSetInformationFile( file
, &io
, &info
, sizeof(info
), FilePositionInformation
))
3654 if (newpos
) newpos
->QuadPart
= pos
;
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
;
3672 memset( &info
, 0, sizeof(info
) );
3675 info
.CreationTime
.u
.HighPart
= ctime
->dwHighDateTime
;
3676 info
.CreationTime
.u
.LowPart
= ctime
->dwLowDateTime
;
3680 info
.LastAccessTime
.u
.HighPart
= atime
->dwHighDateTime
;
3681 info
.LastAccessTime
.u
.LowPart
= atime
->dwLowDateTime
;
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
;
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
)
3731 SetLastError( ERROR_INVALID_PARAMETER
);
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
;
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
);
3758 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
3759 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
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
) );
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
;
3796 PIO_STATUS_BLOCK io
;
3798 TRACE( "%p %p %d %p %p\n", file
, buffer
, count
, overlapped
, completion
);
3802 SetLastError( ERROR_INVALID_PARAMETER
);
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;
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
)
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
;
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
)
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
;
3923 if (!set_ntstatus( NtQuerySystemInformation( SystemTimeAdjustmentInformation
, &st
, sizeof(st
), &len
)))
3925 *adjust
= st
.TimeAdjustment
;
3926 *increment
= st
.TimeIncrement
;
3927 *disabled
= st
.TimeAdjustmentDisabled
;
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
)
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
)
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
)
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
)
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
);
4022 /***********************************************************************
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
)
4053 if (!DeviceIoControl( handle
, IOCTL_SERIAL_GET_COMMSTATUS
, NULL
, 0, &ss
, sizeof(ss
), NULL
, NULL
))
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
);
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
;
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
;
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
;
4096 TRACE( "(%p,%x,%p,%d,%p,%d,%p,%p)\n",
4097 handle
, code
, in_buff
, in_count
, out_buff
, out_count
, returned
, 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
);
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
);
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
);
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
),
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
),
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
;
4214 SetLastError( ERROR_INVALID_PARAMETER
);
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
))
4223 dcb
->DCBlength
= sizeof(*dcb
);
4224 dcb
->BaudRate
= sbr
.BaudRate
;
4225 /* yes, they seem no never be (re)set on NT */
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;
4267 /*****************************************************************************
4268 * GetCommTimeouts (kernelbase.@)
4270 BOOL WINAPI DECLSPEC_HOTPATCH
GetCommTimeouts( HANDLE handle
, COMMTIMEOUTS
*timeouts
)
4274 SetLastError( ERROR_INVALID_PARAMETER
);
4277 return DeviceIoControl( handle
, IOCTL_SERIAL_GET_TIMEOUTS
, NULL
, 0, timeouts
, sizeof(*timeouts
),
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
;
4332 SetLastError( ERROR_INVALID_PARAMETER
);
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;
4351 SetLastError( ERROR_INVALID_PARAMETER
);
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;
4361 SetLastError( ERROR_INVALID_PARAMETER
);
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
;
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
)
4397 SetLastError( ERROR_INVALID_PARAMETER
);
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
),