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
;
741 const WCHAR
*vxd_name
= NULL
;
742 SECURITY_QUALITY_OF_SERVICE qos
;
744 static const UINT nt_disposition
[5] =
746 FILE_CREATE
, /* CREATE_NEW */
747 FILE_OVERWRITE_IF
, /* CREATE_ALWAYS */
748 FILE_OPEN
, /* OPEN_EXISTING */
749 FILE_OPEN_IF
, /* OPEN_ALWAYS */
750 FILE_OVERWRITE
/* TRUNCATE_EXISTING */
756 if (!filename
|| !filename
[0])
758 SetLastError( ERROR_PATH_NOT_FOUND
);
759 return INVALID_HANDLE_VALUE
;
762 TRACE( "%s %s%s%s%s%s%s%s creation %d attributes 0x%x\n", debugstr_w(filename
),
763 (access
& GENERIC_READ
) ? "GENERIC_READ " : "",
764 (access
& GENERIC_WRITE
) ? "GENERIC_WRITE " : "",
765 (access
& GENERIC_EXECUTE
) ? "GENERIC_EXECUTE " : "",
766 !access
? "QUERY_ACCESS " : "",
767 (sharing
& FILE_SHARE_READ
) ? "FILE_SHARE_READ " : "",
768 (sharing
& FILE_SHARE_WRITE
) ? "FILE_SHARE_WRITE " : "",
769 (sharing
& FILE_SHARE_DELETE
) ? "FILE_SHARE_DELETE " : "",
770 creation
, attributes
);
772 if ((GetVersion() & 0x80000000) && !wcsncmp( filename
, L
"\\\\.\\", 4 ) &&
773 !RtlIsDosDeviceName_U( filename
+ 4 ) &&
774 wcsnicmp( filename
+ 4, L
"PIPE\\", 5 ) &&
775 wcsnicmp( filename
+ 4, L
"MAILSLOT\\", 9 ))
777 vxd_name
= filename
+ 4;
778 if (!creation
) creation
= OPEN_EXISTING
;
781 if (creation
< CREATE_NEW
|| creation
> TRUNCATE_EXISTING
)
783 SetLastError( ERROR_INVALID_PARAMETER
);
784 return INVALID_HANDLE_VALUE
;
787 if (!RtlDosPathNameToNtPathName_U( filename
, &nameW
, NULL
, NULL
))
789 SetLastError( ERROR_PATH_NOT_FOUND
);
790 return INVALID_HANDLE_VALUE
;
793 /* now call NtCreateFile */
795 if (attributes
& FILE_FLAG_DELETE_ON_CLOSE
)
798 attr
.Length
= sizeof(attr
);
799 attr
.RootDirectory
= 0;
800 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
801 attr
.ObjectName
= &nameW
;
802 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
803 if (attributes
& SECURITY_SQOS_PRESENT
)
805 qos
.Length
= sizeof(qos
);
806 qos
.ImpersonationLevel
= (attributes
>> 16) & 0x3;
807 qos
.ContextTrackingMode
= attributes
& SECURITY_CONTEXT_TRACKING
? SECURITY_DYNAMIC_TRACKING
: SECURITY_STATIC_TRACKING
;
808 qos
.EffectiveOnly
= (attributes
& SECURITY_EFFECTIVE_ONLY
) != 0;
809 attr
.SecurityQualityOfService
= &qos
;
812 attr
.SecurityQualityOfService
= NULL
;
814 if (sa
&& sa
->bInheritHandle
) attr
.Attributes
|= OBJ_INHERIT
;
816 status
= NtCreateFile( &ret
, access
| SYNCHRONIZE
| FILE_READ_ATTRIBUTES
, &attr
, &io
,
817 NULL
, attributes
& FILE_ATTRIBUTE_VALID_FLAGS
, sharing
,
818 nt_disposition
[creation
- CREATE_NEW
],
819 get_nt_file_options( attributes
), NULL
, 0 );
822 if (vxd_name
&& vxd_name
[0])
824 static HANDLE (*vxd_open
)(LPCWSTR
,DWORD
,SECURITY_ATTRIBUTES
*);
825 if (!vxd_open
) vxd_open
= (void *)GetProcAddress( GetModuleHandleW(L
"krnl386.exe16"),
827 if (vxd_open
&& (ret
= vxd_open( vxd_name
, access
, sa
))) goto done
;
830 WARN("Unable to create file %s (status %x)\n", debugstr_w(filename
), status
);
831 ret
= INVALID_HANDLE_VALUE
;
833 /* In the case file creation was rejected due to CREATE_NEW flag
834 * was specified and file with that name already exists, correct
835 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
836 * Note: RtlNtStatusToDosError is not the subject to blame here.
838 if (status
== STATUS_OBJECT_NAME_COLLISION
)
839 SetLastError( ERROR_FILE_EXISTS
);
841 SetLastError( RtlNtStatusToDosError(status
) );
845 if ((creation
== CREATE_ALWAYS
&& io
.Information
== FILE_OVERWRITTEN
) ||
846 (creation
== OPEN_ALWAYS
&& io
.Information
== FILE_OPENED
))
847 SetLastError( ERROR_ALREADY_EXISTS
);
851 RtlFreeUnicodeString( &nameW
);
854 if (!ret
) ret
= INVALID_HANDLE_VALUE
;
855 TRACE("returning %p\n", ret
);
860 /*************************************************************************
861 * CreateHardLinkA (kernelbase.@)
863 BOOL WINAPI DECLSPEC_HOTPATCH
CreateHardLinkA( const char *dest
, const char *source
,
864 SECURITY_ATTRIBUTES
*attr
)
866 WCHAR
*sourceW
, *destW
;
869 if (!(sourceW
= file_name_AtoW( source
, TRUE
))) return FALSE
;
870 if (!(destW
= file_name_AtoW( dest
, TRUE
)))
872 HeapFree( GetProcessHeap(), 0, sourceW
);
875 res
= CreateHardLinkW( destW
, sourceW
, attr
);
876 HeapFree( GetProcessHeap(), 0, sourceW
);
877 HeapFree( GetProcessHeap(), 0, destW
);
882 /*************************************************************************
883 * CreateHardLinkW (kernelbase.@)
885 BOOL WINAPI
CreateHardLinkW( LPCWSTR dest
, LPCWSTR source
, SECURITY_ATTRIBUTES
*sec_attr
)
887 UNICODE_STRING ntDest
, ntSource
;
888 FILE_LINK_INFORMATION
*info
= NULL
;
889 OBJECT_ATTRIBUTES attr
;
895 TRACE( "(%s, %s, %p)\n", debugstr_w(dest
), debugstr_w(source
), sec_attr
);
897 ntDest
.Buffer
= ntSource
.Buffer
= NULL
;
898 if (!RtlDosPathNameToNtPathName_U( dest
, &ntDest
, NULL
, NULL
) ||
899 !RtlDosPathNameToNtPathName_U( source
, &ntSource
, NULL
, NULL
))
901 SetLastError( ERROR_PATH_NOT_FOUND
);
905 size
= offsetof( FILE_LINK_INFORMATION
, FileName
) + ntDest
.Length
;
906 if (!(info
= HeapAlloc( GetProcessHeap(), 0, size
)))
908 SetLastError( ERROR_OUTOFMEMORY
);
912 InitializeObjectAttributes( &attr
, &ntSource
, OBJ_CASE_INSENSITIVE
, 0, NULL
);
913 if (!(ret
= set_ntstatus( NtOpenFile( &file
, SYNCHRONIZE
, &attr
, &io
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
914 FILE_SYNCHRONOUS_IO_NONALERT
) )))
917 info
->ReplaceIfExists
= FALSE
;
918 info
->RootDirectory
= NULL
;
919 info
->FileNameLength
= ntDest
.Length
;
920 memcpy( info
->FileName
, ntDest
.Buffer
, ntDest
.Length
);
921 ret
= set_ntstatus( NtSetInformationFile( file
, &io
, info
, size
, FileLinkInformation
) );
925 RtlFreeUnicodeString( &ntSource
);
926 RtlFreeUnicodeString( &ntDest
);
927 HeapFree( GetProcessHeap(), 0, info
);
932 /*************************************************************************
933 * CreateSymbolicLinkW (kernelbase.@)
935 BOOLEAN WINAPI
/* DECLSPEC_HOTPATCH */ CreateSymbolicLinkW( LPCWSTR link
, LPCWSTR target
, DWORD flags
)
937 FIXME( "(%s %s %d): stub\n", debugstr_w(link
), debugstr_w(target
), flags
);
942 /***********************************************************************
943 * DeleteFileA (kernelbase.@)
945 BOOL WINAPI DECLSPEC_HOTPATCH
DeleteFileA( LPCSTR path
)
949 if (!(pathW
= file_name_AtoW( path
, FALSE
))) return FALSE
;
950 return DeleteFileW( pathW
);
954 /***********************************************************************
955 * DeleteFileW (kernelbase.@)
957 BOOL WINAPI DECLSPEC_HOTPATCH
DeleteFileW( LPCWSTR path
)
959 UNICODE_STRING nameW
;
960 OBJECT_ATTRIBUTES attr
;
965 TRACE( "%s\n", debugstr_w(path
) );
967 if (!RtlDosPathNameToNtPathName_U( path
, &nameW
, NULL
, NULL
))
969 SetLastError( ERROR_PATH_NOT_FOUND
);
973 attr
.Length
= sizeof(attr
);
974 attr
.RootDirectory
= 0;
975 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
976 attr
.ObjectName
= &nameW
;
977 attr
.SecurityDescriptor
= NULL
;
978 attr
.SecurityQualityOfService
= NULL
;
980 status
= NtCreateFile(&hFile
, SYNCHRONIZE
| DELETE
, &attr
, &io
, NULL
, 0,
981 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
982 FILE_OPEN
, FILE_DELETE_ON_CLOSE
| FILE_NON_DIRECTORY_FILE
, NULL
, 0);
983 if (status
== STATUS_SUCCESS
) status
= NtClose(hFile
);
985 RtlFreeUnicodeString( &nameW
);
986 return set_ntstatus( status
);
990 /****************************************************************************
991 * FindCloseChangeNotification (kernelbase.@)
993 BOOL WINAPI DECLSPEC_HOTPATCH
FindCloseChangeNotification( HANDLE handle
)
995 return CloseHandle( handle
);
999 /****************************************************************************
1000 * FindFirstChangeNotificationA (kernelbase.@)
1002 HANDLE WINAPI DECLSPEC_HOTPATCH
FindFirstChangeNotificationA( LPCSTR path
, BOOL subtree
, DWORD filter
)
1006 if (!(pathW
= file_name_AtoW( path
, FALSE
))) return INVALID_HANDLE_VALUE
;
1007 return FindFirstChangeNotificationW( pathW
, subtree
, filter
);
1012 * NtNotifyChangeDirectoryFile may write back to the IO_STATUS_BLOCK
1013 * asynchronously. We don't care about the contents, but it can't
1014 * be placed on the stack since it will go out of scope when we return.
1016 static IO_STATUS_BLOCK dummy_iosb
;
1018 /****************************************************************************
1019 * FindFirstChangeNotificationW (kernelbase.@)
1021 HANDLE WINAPI DECLSPEC_HOTPATCH
FindFirstChangeNotificationW( LPCWSTR path
, BOOL subtree
, DWORD filter
)
1023 UNICODE_STRING nt_name
;
1024 OBJECT_ATTRIBUTES attr
;
1026 HANDLE handle
= INVALID_HANDLE_VALUE
;
1028 TRACE( "%s %d %x\n", debugstr_w(path
), subtree
, filter
);
1030 if (!RtlDosPathNameToNtPathName_U( path
, &nt_name
, NULL
, NULL
))
1032 SetLastError( ERROR_PATH_NOT_FOUND
);
1036 attr
.Length
= sizeof(attr
);
1037 attr
.RootDirectory
= 0;
1038 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1039 attr
.ObjectName
= &nt_name
;
1040 attr
.SecurityDescriptor
= NULL
;
1041 attr
.SecurityQualityOfService
= NULL
;
1043 status
= NtOpenFile( &handle
, FILE_LIST_DIRECTORY
| SYNCHRONIZE
, &attr
, &dummy_iosb
,
1044 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1045 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
1046 RtlFreeUnicodeString( &nt_name
);
1048 if (!set_ntstatus( status
)) return INVALID_HANDLE_VALUE
;
1050 status
= NtNotifyChangeDirectoryFile( handle
, NULL
, NULL
, NULL
, &dummy_iosb
, NULL
, 0, filter
, subtree
);
1051 if (status
!= STATUS_PENDING
)
1054 SetLastError( RtlNtStatusToDosError(status
) );
1055 return INVALID_HANDLE_VALUE
;
1061 /****************************************************************************
1062 * FindNextChangeNotification (kernelbase.@)
1064 BOOL WINAPI DECLSPEC_HOTPATCH
FindNextChangeNotification( HANDLE handle
)
1066 NTSTATUS status
= NtNotifyChangeDirectoryFile( handle
, NULL
, NULL
, NULL
, &dummy_iosb
,
1067 NULL
, 0, FILE_NOTIFY_CHANGE_SIZE
, 0 );
1068 if (status
== STATUS_PENDING
) return TRUE
;
1069 return set_ntstatus( status
);
1073 /******************************************************************************
1074 * FindFirstFileExA (kernelbase.@)
1076 HANDLE WINAPI DECLSPEC_HOTPATCH
FindFirstFileExA( const char *filename
, FINDEX_INFO_LEVELS level
,
1077 void *data
, FINDEX_SEARCH_OPS search_op
,
1078 void *filter
, DWORD flags
)
1081 WIN32_FIND_DATAA
*dataA
= data
;
1082 WIN32_FIND_DATAW dataW
;
1085 if (!(nameW
= file_name_AtoW( filename
, FALSE
))) return INVALID_HANDLE_VALUE
;
1087 handle
= FindFirstFileExW( nameW
, level
, &dataW
, search_op
, filter
, flags
);
1088 if (handle
== INVALID_HANDLE_VALUE
) return handle
;
1090 dataA
->dwFileAttributes
= dataW
.dwFileAttributes
;
1091 dataA
->ftCreationTime
= dataW
.ftCreationTime
;
1092 dataA
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
1093 dataA
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
1094 dataA
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
1095 dataA
->nFileSizeLow
= dataW
.nFileSizeLow
;
1096 file_name_WtoA( dataW
.cFileName
, -1, dataA
->cFileName
, sizeof(dataA
->cFileName
) );
1097 file_name_WtoA( dataW
.cAlternateFileName
, -1, dataA
->cAlternateFileName
,
1098 sizeof(dataA
->cAlternateFileName
) );
1103 /******************************************************************************
1104 * FindFirstFileExW (kernelbase.@)
1106 HANDLE WINAPI DECLSPEC_HOTPATCH
FindFirstFileExW( LPCWSTR filename
, FINDEX_INFO_LEVELS level
,
1107 LPVOID data
, FINDEX_SEARCH_OPS search_op
,
1108 LPVOID filter
, DWORD flags
)
1111 BOOL has_wildcard
= FALSE
;
1112 FIND_FIRST_INFO
*info
= NULL
;
1113 UNICODE_STRING nt_name
;
1114 OBJECT_ATTRIBUTES attr
;
1117 DWORD size
, device
= 0;
1119 TRACE( "%s %d %p %d %p %x\n", debugstr_w(filename
), level
, data
, search_op
, filter
, flags
);
1121 if (flags
& ~FIND_FIRST_EX_LARGE_FETCH
)
1123 FIXME("flags not implemented 0x%08x\n", flags
);
1125 if (search_op
!= FindExSearchNameMatch
&& search_op
!= FindExSearchLimitToDirectories
)
1127 FIXME( "search_op not implemented 0x%08x\n", search_op
);
1128 SetLastError( ERROR_INVALID_PARAMETER
);
1129 return INVALID_HANDLE_VALUE
;
1131 if (level
!= FindExInfoStandard
&& level
!= FindExInfoBasic
)
1133 FIXME("info level %d not implemented\n", level
);
1134 SetLastError( ERROR_INVALID_PARAMETER
);
1135 return INVALID_HANDLE_VALUE
;
1138 if (!RtlDosPathNameToNtPathName_U( filename
, &nt_name
, &mask
, NULL
))
1140 SetLastError( ERROR_PATH_NOT_FOUND
);
1141 return INVALID_HANDLE_VALUE
;
1144 if (!mask
&& (device
= RtlIsDosDeviceName_U( filename
)))
1148 /* we still need to check that the directory can be opened */
1152 if (!(dir
= HeapAlloc( GetProcessHeap(), 0, HIWORD(device
) + sizeof(WCHAR
) )))
1154 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1157 memcpy( dir
, filename
, HIWORD(device
) );
1158 dir
[HIWORD(device
)/sizeof(WCHAR
)] = 0;
1160 RtlFreeUnicodeString( &nt_name
);
1161 if (!RtlDosPathNameToNtPathName_U( dir
? dir
: L
".", &nt_name
, &mask
, NULL
))
1163 HeapFree( GetProcessHeap(), 0, dir
);
1164 SetLastError( ERROR_PATH_NOT_FOUND
);
1167 HeapFree( GetProcessHeap(), 0, dir
);
1170 else if (!mask
|| !*mask
)
1172 SetLastError( ERROR_FILE_NOT_FOUND
);
1177 nt_name
.Length
= (mask
- nt_name
.Buffer
) * sizeof(WCHAR
);
1178 has_wildcard
= wcspbrk( mask
, L
"*?" ) != NULL
;
1179 size
= has_wildcard
? 8192 : max_entry_size
;
1182 if (!(info
= HeapAlloc( GetProcessHeap(), 0, offsetof( FIND_FIRST_INFO
, data
[size
] ))))
1184 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1188 /* check if path is the root of the drive, skipping the \??\ prefix */
1189 info
->is_root
= FALSE
;
1190 if (nt_name
.Length
>= 6 * sizeof(WCHAR
) && nt_name
.Buffer
[5] == ':')
1193 while (pos
* sizeof(WCHAR
) < nt_name
.Length
&& nt_name
.Buffer
[pos
] == '\\') pos
++;
1194 info
->is_root
= (pos
* sizeof(WCHAR
) >= nt_name
.Length
);
1197 attr
.Length
= sizeof(attr
);
1198 attr
.RootDirectory
= 0;
1199 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1200 attr
.ObjectName
= &nt_name
;
1201 attr
.SecurityDescriptor
= NULL
;
1202 attr
.SecurityQualityOfService
= NULL
;
1204 status
= NtOpenFile( &info
->handle
, GENERIC_READ
| SYNCHRONIZE
, &attr
, &io
,
1205 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1206 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
| FILE_OPEN_FOR_BACKUP_INTENT
);
1207 if (status
!= STATUS_SUCCESS
)
1209 if (status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1210 SetLastError( ERROR_PATH_NOT_FOUND
);
1212 SetLastError( RtlNtStatusToDosError(status
) );
1216 RtlInitializeCriticalSection( &info
->cs
);
1217 info
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FIND_FIRST_INFO.cs");
1218 info
->path
= nt_name
;
1219 info
->magic
= FIND_FIRST_MAGIC
;
1220 info
->wildcard
= has_wildcard
;
1223 info
->data_size
= size
;
1224 info
->search_op
= search_op
;
1225 info
->level
= level
;
1229 WIN32_FIND_DATAW
*wfd
= data
;
1231 memset( wfd
, 0, sizeof(*wfd
) );
1232 memcpy( wfd
->cFileName
, filename
+ HIWORD(device
)/sizeof(WCHAR
), LOWORD(device
) );
1233 wfd
->dwFileAttributes
= FILE_ATTRIBUTE_ARCHIVE
;
1234 CloseHandle( info
->handle
);
1239 UNICODE_STRING mask_str
;
1241 RtlInitUnicodeString( &mask_str
, mask
);
1242 status
= NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, info
->data_size
,
1243 FileBothDirectoryInformation
, FALSE
, &mask_str
, TRUE
);
1247 SetLastError( RtlNtStatusToDosError( status
) );
1248 return INVALID_HANDLE_VALUE
;
1251 info
->data_len
= io
.Information
;
1252 if (!has_wildcard
) info
->data_size
= 0; /* we read everything */
1254 if (!FindNextFileW( info
, data
))
1256 TRACE( "%s not found\n", debugstr_w(filename
) );
1258 SetLastError( ERROR_FILE_NOT_FOUND
);
1259 return INVALID_HANDLE_VALUE
;
1261 if (!has_wildcard
) /* we can't find two files with the same name */
1263 CloseHandle( info
->handle
);
1270 HeapFree( GetProcessHeap(), 0, info
);
1271 RtlFreeUnicodeString( &nt_name
);
1272 return INVALID_HANDLE_VALUE
;
1276 /******************************************************************************
1277 * FindFirstFileA (kernelbase.@)
1279 HANDLE WINAPI DECLSPEC_HOTPATCH
FindFirstFileA( const char *filename
, WIN32_FIND_DATAA
*data
)
1281 return FindFirstFileExA( filename
, FindExInfoStandard
, data
, FindExSearchNameMatch
, NULL
, 0 );
1285 /******************************************************************************
1286 * FindFirstFileW (kernelbase.@)
1288 HANDLE WINAPI DECLSPEC_HOTPATCH
FindFirstFileW( const WCHAR
*filename
, WIN32_FIND_DATAW
*data
)
1290 return FindFirstFileExW( filename
, FindExInfoStandard
, data
, FindExSearchNameMatch
, NULL
, 0 );
1294 /**************************************************************************
1295 * FindFirstStreamW (kernelbase.@)
1297 HANDLE WINAPI
FindFirstStreamW( const WCHAR
*filename
, STREAM_INFO_LEVELS level
, void *data
, DWORD flags
)
1299 FIXME("(%s, %d, %p, %x): stub!\n", debugstr_w(filename
), level
, data
, flags
);
1300 SetLastError( ERROR_HANDLE_EOF
);
1301 return INVALID_HANDLE_VALUE
;
1305 /******************************************************************************
1306 * FindNextFileA (kernelbase.@)
1308 BOOL WINAPI DECLSPEC_HOTPATCH
FindNextFileA( HANDLE handle
, WIN32_FIND_DATAA
*data
)
1310 WIN32_FIND_DATAW dataW
;
1312 if (!FindNextFileW( handle
, &dataW
)) return FALSE
;
1313 data
->dwFileAttributes
= dataW
.dwFileAttributes
;
1314 data
->ftCreationTime
= dataW
.ftCreationTime
;
1315 data
->ftLastAccessTime
= dataW
.ftLastAccessTime
;
1316 data
->ftLastWriteTime
= dataW
.ftLastWriteTime
;
1317 data
->nFileSizeHigh
= dataW
.nFileSizeHigh
;
1318 data
->nFileSizeLow
= dataW
.nFileSizeLow
;
1319 file_name_WtoA( dataW
.cFileName
, -1, data
->cFileName
, sizeof(data
->cFileName
) );
1320 file_name_WtoA( dataW
.cAlternateFileName
, -1, data
->cAlternateFileName
,
1321 sizeof(data
->cAlternateFileName
) );
1326 /******************************************************************************
1327 * FindNextFileW (kernelbase.@)
1329 BOOL WINAPI DECLSPEC_HOTPATCH
FindNextFileW( HANDLE handle
, WIN32_FIND_DATAW
*data
)
1331 FIND_FIRST_INFO
*info
= handle
;
1332 FILE_BOTH_DIR_INFORMATION
*dir_info
;
1336 TRACE( "%p %p\n", handle
, data
);
1338 if (!handle
|| handle
== INVALID_HANDLE_VALUE
|| info
->magic
!= FIND_FIRST_MAGIC
)
1340 SetLastError( ERROR_INVALID_HANDLE
);
1344 RtlEnterCriticalSection( &info
->cs
);
1346 if (!info
->handle
) SetLastError( ERROR_NO_MORE_FILES
);
1349 if (info
->data_pos
>= info
->data_len
) /* need to read some more data */
1353 if (info
->data_size
)
1354 status
= NtQueryDirectoryFile( info
->handle
, 0, NULL
, NULL
, &io
, info
->data
, info
->data_size
,
1355 FileBothDirectoryInformation
, FALSE
, NULL
, FALSE
);
1357 status
= STATUS_NO_MORE_FILES
;
1359 if (!set_ntstatus( status
))
1361 if (status
== STATUS_NO_MORE_FILES
)
1363 CloseHandle( info
->handle
);
1368 info
->data_len
= io
.Information
;
1372 dir_info
= (FILE_BOTH_DIR_INFORMATION
*)(info
->data
+ info
->data_pos
);
1374 if (dir_info
->NextEntryOffset
) info
->data_pos
+= dir_info
->NextEntryOffset
;
1375 else info
->data_pos
= info
->data_len
;
1377 /* don't return '.' and '..' in the root of the drive */
1380 if (dir_info
->FileNameLength
== sizeof(WCHAR
) && dir_info
->FileName
[0] == '.') continue;
1381 if (dir_info
->FileNameLength
== 2 * sizeof(WCHAR
) &&
1382 dir_info
->FileName
[0] == '.' && dir_info
->FileName
[1] == '.') continue;
1385 data
->dwFileAttributes
= dir_info
->FileAttributes
;
1386 data
->ftCreationTime
= *(FILETIME
*)&dir_info
->CreationTime
;
1387 data
->ftLastAccessTime
= *(FILETIME
*)&dir_info
->LastAccessTime
;
1388 data
->ftLastWriteTime
= *(FILETIME
*)&dir_info
->LastWriteTime
;
1389 data
->nFileSizeHigh
= dir_info
->EndOfFile
.QuadPart
>> 32;
1390 data
->nFileSizeLow
= (DWORD
)dir_info
->EndOfFile
.QuadPart
;
1391 data
->dwReserved0
= 0;
1392 data
->dwReserved1
= 0;
1394 memcpy( data
->cFileName
, dir_info
->FileName
, dir_info
->FileNameLength
);
1395 data
->cFileName
[dir_info
->FileNameLength
/sizeof(WCHAR
)] = 0;
1397 if (info
->level
!= FindExInfoBasic
)
1399 memcpy( data
->cAlternateFileName
, dir_info
->ShortName
, dir_info
->ShortNameLength
);
1400 data
->cAlternateFileName
[dir_info
->ShortNameLength
/sizeof(WCHAR
)] = 0;
1403 data
->cAlternateFileName
[0] = 0;
1405 TRACE( "returning %s (%s)\n",
1406 debugstr_w(data
->cFileName
), debugstr_w(data
->cAlternateFileName
) );
1412 RtlLeaveCriticalSection( &info
->cs
);
1417 /**************************************************************************
1418 * FindNextStreamW (kernelbase.@)
1420 BOOL WINAPI
FindNextStreamW( HANDLE handle
, void *data
)
1422 FIXME( "(%p, %p): stub!\n", handle
, data
);
1423 SetLastError( ERROR_HANDLE_EOF
);
1428 /******************************************************************************
1429 * FindClose (kernelbase.@)
1431 BOOL WINAPI DECLSPEC_HOTPATCH
FindClose( HANDLE handle
)
1433 FIND_FIRST_INFO
*info
= handle
;
1435 if (!handle
|| handle
== INVALID_HANDLE_VALUE
)
1437 SetLastError( ERROR_INVALID_HANDLE
);
1443 if (info
->magic
== FIND_FIRST_MAGIC
)
1445 RtlEnterCriticalSection( &info
->cs
);
1446 if (info
->magic
== FIND_FIRST_MAGIC
) /* in case someone else freed it in the meantime */
1449 if (info
->handle
) CloseHandle( info
->handle
);
1451 RtlFreeUnicodeString( &info
->path
);
1454 RtlLeaveCriticalSection( &info
->cs
);
1455 info
->cs
.DebugInfo
->Spare
[0] = 0;
1456 RtlDeleteCriticalSection( &info
->cs
);
1457 HeapFree( GetProcessHeap(), 0, info
);
1463 WARN( "illegal handle %p\n", handle
);
1464 SetLastError( ERROR_INVALID_HANDLE
);
1473 /******************************************************************************
1474 * GetCompressedFileSizeA (kernelbase.@)
1476 DWORD WINAPI DECLSPEC_HOTPATCH
GetCompressedFileSizeA( LPCSTR name
, LPDWORD size_high
)
1480 if (!(nameW
= file_name_AtoW( name
, FALSE
))) return INVALID_FILE_SIZE
;
1481 return GetCompressedFileSizeW( nameW
, size_high
);
1485 /******************************************************************************
1486 * GetCompressedFileSizeW (kernelbase.@)
1488 DWORD WINAPI DECLSPEC_HOTPATCH
GetCompressedFileSizeW( LPCWSTR name
, LPDWORD size_high
)
1490 UNICODE_STRING nt_name
;
1491 OBJECT_ATTRIBUTES attr
;
1497 TRACE("%s %p\n", debugstr_w(name
), size_high
);
1499 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1501 SetLastError( ERROR_PATH_NOT_FOUND
);
1502 return INVALID_FILE_SIZE
;
1505 attr
.Length
= sizeof(attr
);
1506 attr
.RootDirectory
= 0;
1507 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1508 attr
.ObjectName
= &nt_name
;
1509 attr
.SecurityDescriptor
= NULL
;
1510 attr
.SecurityQualityOfService
= NULL
;
1512 status
= NtOpenFile( &handle
, SYNCHRONIZE
, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
1513 RtlFreeUnicodeString( &nt_name
);
1514 if (!set_ntstatus( status
)) return INVALID_FILE_SIZE
;
1516 /* we don't support compressed files, simply return the file size */
1517 ret
= GetFileSize( handle
, size_high
);
1523 /***********************************************************************
1524 * GetCurrentDirectoryA (kernelbase.@)
1526 UINT WINAPI DECLSPEC_HOTPATCH
GetCurrentDirectoryA( UINT buflen
, LPSTR buf
)
1528 WCHAR bufferW
[MAX_PATH
];
1531 if (buflen
&& buf
&& ((ULONG_PTR
)buf
>> 16) == 0)
1533 /* Win9x catches access violations here, returning zero.
1534 * This behaviour resulted in some people not noticing
1535 * that they got the argument order wrong. So let's be
1536 * nice and fail gracefully if buf is invalid and looks
1537 * more like a buflen. */
1538 SetLastError( ERROR_INVALID_PARAMETER
);
1542 ret
= RtlGetCurrentDirectory_U( sizeof(bufferW
), bufferW
);
1544 if (ret
> sizeof(bufferW
))
1546 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1549 return copy_filename_WtoA( bufferW
, buf
, buflen
);
1553 /***********************************************************************
1554 * GetCurrentDirectoryW (kernelbase.@)
1556 UINT WINAPI DECLSPEC_HOTPATCH
GetCurrentDirectoryW( UINT buflen
, LPWSTR buf
)
1558 return RtlGetCurrentDirectory_U( buflen
* sizeof(WCHAR
), buf
) / sizeof(WCHAR
);
1562 /**************************************************************************
1563 * GetFileAttributesA (kernelbase.@)
1565 DWORD WINAPI DECLSPEC_HOTPATCH
GetFileAttributesA( LPCSTR name
)
1569 if (!(nameW
= file_name_AtoW( name
, FALSE
))) return INVALID_FILE_ATTRIBUTES
;
1570 return GetFileAttributesW( nameW
);
1574 /**************************************************************************
1575 * GetFileAttributesW (kernelbase.@)
1577 DWORD WINAPI DECLSPEC_HOTPATCH
GetFileAttributesW( LPCWSTR name
)
1579 FILE_BASIC_INFORMATION info
;
1580 UNICODE_STRING nt_name
;
1581 OBJECT_ATTRIBUTES attr
;
1584 TRACE( "%s\n", debugstr_w(name
) );
1586 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1588 SetLastError( ERROR_PATH_NOT_FOUND
);
1589 return INVALID_FILE_ATTRIBUTES
;
1592 attr
.Length
= sizeof(attr
);
1593 attr
.RootDirectory
= 0;
1594 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1595 attr
.ObjectName
= &nt_name
;
1596 attr
.SecurityDescriptor
= NULL
;
1597 attr
.SecurityQualityOfService
= NULL
;
1599 status
= NtQueryAttributesFile( &attr
, &info
);
1600 RtlFreeUnicodeString( &nt_name
);
1602 if (status
== STATUS_SUCCESS
) return info
.FileAttributes
;
1604 /* NtQueryAttributesFile fails on devices, but GetFileAttributesW succeeds */
1605 if (RtlIsDosDeviceName_U( name
)) return FILE_ATTRIBUTE_ARCHIVE
;
1607 SetLastError( RtlNtStatusToDosError(status
) );
1608 return INVALID_FILE_ATTRIBUTES
;
1612 /**************************************************************************
1613 * GetFileAttributesExA (kernelbase.@)
1615 BOOL WINAPI DECLSPEC_HOTPATCH
GetFileAttributesExA( LPCSTR name
, GET_FILEEX_INFO_LEVELS level
, void *ptr
)
1619 if (!(nameW
= file_name_AtoW( name
, FALSE
))) return FALSE
;
1620 return GetFileAttributesExW( nameW
, level
, ptr
);
1624 /**************************************************************************
1625 * GetFileAttributesExW (kernelbase.@)
1627 BOOL WINAPI DECLSPEC_HOTPATCH
GetFileAttributesExW( LPCWSTR name
, GET_FILEEX_INFO_LEVELS level
, void *ptr
)
1629 FILE_NETWORK_OPEN_INFORMATION info
;
1630 WIN32_FILE_ATTRIBUTE_DATA
*data
= ptr
;
1631 UNICODE_STRING nt_name
;
1632 OBJECT_ATTRIBUTES attr
;
1635 TRACE("%s %d %p\n", debugstr_w(name
), level
, ptr
);
1637 if (level
!= GetFileExInfoStandard
)
1639 SetLastError( ERROR_INVALID_PARAMETER
);
1643 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1645 SetLastError( ERROR_PATH_NOT_FOUND
);
1649 attr
.Length
= sizeof(attr
);
1650 attr
.RootDirectory
= 0;
1651 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1652 attr
.ObjectName
= &nt_name
;
1653 attr
.SecurityDescriptor
= NULL
;
1654 attr
.SecurityQualityOfService
= NULL
;
1656 status
= NtQueryFullAttributesFile( &attr
, &info
);
1657 RtlFreeUnicodeString( &nt_name
);
1658 if (!set_ntstatus( status
)) return FALSE
;
1660 data
->dwFileAttributes
= info
.FileAttributes
;
1661 data
->ftCreationTime
.dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
1662 data
->ftCreationTime
.dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
1663 data
->ftLastAccessTime
.dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
1664 data
->ftLastAccessTime
.dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
1665 data
->ftLastWriteTime
.dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
1666 data
->ftLastWriteTime
.dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
1667 data
->nFileSizeLow
= info
.EndOfFile
.u
.LowPart
;
1668 data
->nFileSizeHigh
= info
.EndOfFile
.u
.HighPart
;
1673 /***********************************************************************
1674 * GetFinalPathNameByHandleA (kernelbase.@)
1676 DWORD WINAPI DECLSPEC_HOTPATCH
GetFinalPathNameByHandleA( HANDLE file
, LPSTR path
,
1677 DWORD count
, DWORD flags
)
1682 TRACE( "(%p,%p,%d,%x)\n", file
, path
, count
, flags
);
1684 len
= GetFinalPathNameByHandleW(file
, NULL
, 0, flags
);
1685 if (len
== 0) return 0;
1687 str
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1690 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1694 result
= GetFinalPathNameByHandleW(file
, str
, len
, flags
);
1695 if (result
!= len
- 1)
1697 HeapFree(GetProcessHeap(), 0, str
);
1701 len
= file_name_WtoA( str
, -1, NULL
, 0 );
1704 HeapFree(GetProcessHeap(), 0, str
);
1707 file_name_WtoA( str
, -1, path
, count
);
1708 HeapFree(GetProcessHeap(), 0, str
);
1713 /***********************************************************************
1714 * GetFinalPathNameByHandleW (kernelbase.@)
1716 DWORD WINAPI DECLSPEC_HOTPATCH
GetFinalPathNameByHandleW( HANDLE file
, LPWSTR path
,
1717 DWORD count
, DWORD flags
)
1719 WCHAR buffer
[sizeof(OBJECT_NAME_INFORMATION
) + MAX_PATH
+ 1];
1720 OBJECT_NAME_INFORMATION
*info
= (OBJECT_NAME_INFORMATION
*)&buffer
;
1721 WCHAR drive_part
[MAX_PATH
];
1722 DWORD drive_part_len
= 0;
1728 TRACE( "(%p,%p,%d,%x)\n", file
, path
, count
, flags
);
1730 if (flags
& ~(FILE_NAME_OPENED
| VOLUME_NAME_GUID
| VOLUME_NAME_NONE
| VOLUME_NAME_NT
))
1732 WARN("Unknown flags: %x\n", flags
);
1733 SetLastError( ERROR_INVALID_PARAMETER
);
1737 /* get object name */
1738 status
= NtQueryObject( file
, ObjectNameInformation
, &buffer
, sizeof(buffer
) - sizeof(WCHAR
), &dummy
);
1739 if (!set_ntstatus( status
)) return 0;
1741 if (!info
->Name
.Buffer
)
1743 SetLastError( ERROR_INVALID_HANDLE
);
1746 if (info
->Name
.Length
< 4 * sizeof(WCHAR
) || info
->Name
.Buffer
[0] != '\\' ||
1747 info
->Name
.Buffer
[1] != '?' || info
->Name
.Buffer
[2] != '?' || info
->Name
.Buffer
[3] != '\\' )
1749 FIXME("Unexpected object name: %s\n", debugstr_wn(info
->Name
.Buffer
, info
->Name
.Length
/ sizeof(WCHAR
)));
1750 SetLastError( ERROR_GEN_FAILURE
);
1754 /* add terminating null character, remove "\\??\\" */
1755 info
->Name
.Buffer
[info
->Name
.Length
/ sizeof(WCHAR
)] = 0;
1756 info
->Name
.Length
-= 4 * sizeof(WCHAR
);
1757 info
->Name
.Buffer
+= 4;
1759 /* FILE_NAME_OPENED is not supported yet, and would require Wineserver changes */
1760 if (flags
& FILE_NAME_OPENED
)
1762 FIXME("FILE_NAME_OPENED not supported\n");
1763 flags
&= ~FILE_NAME_OPENED
;
1766 /* Get information required for VOLUME_NAME_NONE, VOLUME_NAME_GUID and VOLUME_NAME_NT */
1767 if (flags
== VOLUME_NAME_NONE
|| flags
== VOLUME_NAME_GUID
|| flags
== VOLUME_NAME_NT
)
1769 if (!GetVolumePathNameW( info
->Name
.Buffer
, drive_part
, MAX_PATH
)) return 0;
1770 drive_part_len
= lstrlenW(drive_part
);
1771 if (!drive_part_len
|| drive_part_len
> lstrlenW(info
->Name
.Buffer
) ||
1772 drive_part
[drive_part_len
-1] != '\\' ||
1773 CompareStringOrdinal( info
->Name
.Buffer
, drive_part_len
, drive_part
, drive_part_len
, TRUE
) != CSTR_EQUAL
)
1775 FIXME( "Path %s returned by GetVolumePathNameW does not match file path %s\n",
1776 debugstr_w(drive_part
), debugstr_w(info
->Name
.Buffer
) );
1777 SetLastError( ERROR_GEN_FAILURE
);
1782 if (flags
== VOLUME_NAME_NONE
)
1784 ptr
= info
->Name
.Buffer
+ drive_part_len
- 1;
1785 result
= lstrlenW(ptr
);
1786 if (result
< count
) memcpy(path
, ptr
, (result
+ 1) * sizeof(WCHAR
));
1789 else if (flags
== VOLUME_NAME_GUID
)
1791 WCHAR volume_prefix
[51];
1793 /* GetVolumeNameForVolumeMountPointW sets error code on failure */
1794 if (!GetVolumeNameForVolumeMountPointW( drive_part
, volume_prefix
, 50 )) return 0;
1795 ptr
= info
->Name
.Buffer
+ drive_part_len
;
1796 result
= lstrlenW(volume_prefix
) + lstrlenW(ptr
);
1799 lstrcpyW(path
, volume_prefix
);
1800 lstrcatW(path
, ptr
);
1804 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1808 else if (flags
== VOLUME_NAME_NT
)
1810 WCHAR nt_prefix
[MAX_PATH
];
1812 /* QueryDosDeviceW sets error code on failure */
1813 drive_part
[drive_part_len
- 1] = 0;
1814 if (!QueryDosDeviceW( drive_part
, nt_prefix
, MAX_PATH
)) return 0;
1815 ptr
= info
->Name
.Buffer
+ drive_part_len
- 1;
1816 result
= lstrlenW(nt_prefix
) + lstrlenW(ptr
);
1819 lstrcpyW(path
, nt_prefix
);
1820 lstrcatW(path
, ptr
);
1824 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1828 else if (flags
== VOLUME_NAME_DOS
)
1830 result
= 4 + lstrlenW(info
->Name
.Buffer
);
1833 lstrcpyW(path
, L
"\\\\?\\");
1834 lstrcatW(path
, info
->Name
.Buffer
);
1838 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1844 /* Windows crashes here, but we prefer returning ERROR_INVALID_PARAMETER */
1845 WARN("Invalid combination of flags: %x\n", flags
);
1846 SetLastError( ERROR_INVALID_PARAMETER
);
1852 /***********************************************************************
1853 * GetFullPathNameA (kernelbase.@)
1855 DWORD WINAPI DECLSPEC_HOTPATCH
GetFullPathNameA( LPCSTR name
, DWORD len
, LPSTR buffer
, LPSTR
*lastpart
)
1858 WCHAR bufferW
[MAX_PATH
], *lastpartW
= NULL
;
1861 if (!(nameW
= file_name_AtoW( name
, FALSE
))) return 0;
1863 ret
= GetFullPathNameW( nameW
, MAX_PATH
, bufferW
, &lastpartW
);
1868 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1871 ret
= copy_filename_WtoA( bufferW
, buffer
, len
);
1872 if (ret
< len
&& lastpart
)
1875 *lastpart
= buffer
+ file_name_WtoA( bufferW
, lastpartW
- bufferW
, NULL
, 0 );
1883 /***********************************************************************
1884 * GetFullPathNameW (kernelbase.@)
1886 DWORD WINAPI DECLSPEC_HOTPATCH
GetFullPathNameW( LPCWSTR name
, DWORD len
, LPWSTR buffer
, LPWSTR
*lastpart
)
1888 return RtlGetFullPathName_U( name
, len
* sizeof(WCHAR
), buffer
, lastpart
) / sizeof(WCHAR
);
1892 /***********************************************************************
1893 * GetLongPathNameA (kernelbase.@)
1895 DWORD WINAPI DECLSPEC_HOTPATCH
GetLongPathNameA( LPCSTR shortpath
, LPSTR longpath
, DWORD longlen
)
1898 WCHAR longpathW
[MAX_PATH
];
1901 TRACE( "%s\n", debugstr_a( shortpath
));
1903 if (!(shortpathW
= file_name_AtoW( shortpath
, FALSE
))) return 0;
1905 ret
= GetLongPathNameW( shortpathW
, longpathW
, MAX_PATH
);
1910 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1913 return copy_filename_WtoA( longpathW
, longpath
, longlen
);
1917 /***********************************************************************
1918 * GetLongPathNameW (kernelbase.@)
1920 DWORD WINAPI DECLSPEC_HOTPATCH
GetLongPathNameW( LPCWSTR shortpath
, LPWSTR longpath
, DWORD longlen
)
1922 WCHAR tmplongpath
[1024];
1923 DWORD sp
= 0, lp
= 0, tmplen
;
1924 WIN32_FIND_DATAW wfd
;
1925 UNICODE_STRING nameW
;
1929 TRACE("%s,%p,%u\n", debugstr_w(shortpath
), longpath
, longlen
);
1933 SetLastError( ERROR_INVALID_PARAMETER
);
1938 SetLastError( ERROR_PATH_NOT_FOUND
);
1942 if (shortpath
[0] == '\\' && shortpath
[1] == '\\')
1944 FIXME( "UNC pathname %s\n", debugstr_w(shortpath
) );
1945 tmplen
= lstrlenW( shortpath
);
1946 if (tmplen
< longlen
)
1948 if (longpath
!= shortpath
) lstrcpyW( longpath
, shortpath
);
1954 /* check for drive letter */
1955 if (shortpath
[0] != '/' && shortpath
[1] == ':' )
1957 tmplongpath
[0] = shortpath
[0];
1958 tmplongpath
[1] = ':';
1962 if (wcspbrk( shortpath
+ sp
, L
"*?" ))
1964 SetLastError( ERROR_INVALID_NAME
);
1968 while (shortpath
[sp
])
1970 /* check for path delimiters and reproduce them */
1971 if (shortpath
[sp
] == '\\' || shortpath
[sp
] == '/')
1973 tmplongpath
[lp
++] = shortpath
[sp
++];
1974 tmplongpath
[lp
] = 0; /* terminate string */
1978 for (p
= shortpath
+ sp
; *p
&& *p
!= '/' && *p
!= '\\'; p
++);
1979 tmplen
= p
- (shortpath
+ sp
);
1980 lstrcpynW( tmplongpath
+ lp
, shortpath
+ sp
, tmplen
+ 1 );
1982 if (tmplongpath
[lp
] == '.')
1984 if (tmplen
== 1 || (tmplen
== 2 && tmplongpath
[lp
+ 1] == '.'))
1992 /* Check if the file exists */
1993 handle
= FindFirstFileW( tmplongpath
, &wfd
);
1994 if (handle
== INVALID_HANDLE_VALUE
)
1996 TRACE( "not found %s\n", debugstr_w( tmplongpath
));
1997 SetLastError ( ERROR_FILE_NOT_FOUND
);
2000 FindClose( handle
);
2002 /* Use the existing file name if it's a short name */
2003 RtlInitUnicodeString( &nameW
, tmplongpath
+ lp
);
2004 if (RtlIsNameLegalDOS8Dot3( &nameW
, NULL
, NULL
)) lstrcpyW( tmplongpath
+ lp
, wfd
.cFileName
);
2005 lp
+= lstrlenW( tmplongpath
+ lp
);
2008 tmplen
= lstrlenW( shortpath
) - 1;
2009 if ((shortpath
[tmplen
] == '/' || shortpath
[tmplen
] == '\\') &&
2010 (tmplongpath
[lp
- 1] != '/' && tmplongpath
[lp
- 1] != '\\'))
2011 tmplongpath
[lp
++] = shortpath
[tmplen
];
2012 tmplongpath
[lp
] = 0;
2014 tmplen
= lstrlenW( tmplongpath
) + 1;
2015 if (tmplen
<= longlen
)
2017 lstrcpyW( longpath
, tmplongpath
);
2018 TRACE("returning %s\n", debugstr_w( longpath
));
2019 tmplen
--; /* length without 0 */
2025 /***********************************************************************
2026 * GetShortPathNameW (kernelbase.@)
2028 DWORD WINAPI DECLSPEC_HOTPATCH
GetShortPathNameW( LPCWSTR longpath
, LPWSTR shortpath
, DWORD shortlen
)
2030 WIN32_FIND_DATAW wfd
;
2031 WCHAR
*tmpshortpath
;
2034 DWORD sp
= 0, lp
= 0, tmplen
, buf_len
;
2036 TRACE( "%s,%p,%u\n", debugstr_w(longpath
), shortpath
, shortlen
);
2040 SetLastError( ERROR_INVALID_PARAMETER
);
2045 SetLastError( ERROR_BAD_PATHNAME
);
2049 /* code below only removes characters from string, never adds, so this is
2050 * the largest buffer that tmpshortpath will need to have */
2051 buf_len
= lstrlenW(longpath
) + 1;
2052 tmpshortpath
= HeapAlloc( GetProcessHeap(), 0, buf_len
* sizeof(WCHAR
) );
2055 SetLastError( ERROR_OUTOFMEMORY
);
2059 if (longpath
[0] == '\\' && longpath
[1] == '\\' && longpath
[2] == '?' && longpath
[3] == '\\')
2061 memcpy( tmpshortpath
, longpath
, 4 * sizeof(WCHAR
) );
2065 if (wcspbrk( longpath
+ lp
, L
"*?" ))
2067 HeapFree( GetProcessHeap(), 0, tmpshortpath
);
2068 SetLastError( ERROR_INVALID_NAME
);
2072 /* check for drive letter */
2073 if (longpath
[lp
] != '/' && longpath
[lp
+ 1] == ':' )
2075 tmpshortpath
[sp
] = longpath
[lp
];
2076 tmpshortpath
[sp
+ 1] = ':';
2081 while (longpath
[lp
])
2083 /* check for path delimiters and reproduce them */
2084 if (longpath
[lp
] == '\\' || longpath
[lp
] == '/')
2086 tmpshortpath
[sp
++] = longpath
[lp
++];
2087 tmpshortpath
[sp
] = 0; /* terminate string */
2092 for (; *p
&& *p
!= '/' && *p
!= '\\'; p
++);
2093 tmplen
= p
- (longpath
+ lp
);
2094 lstrcpynW( tmpshortpath
+ sp
, longpath
+ lp
, tmplen
+ 1 );
2096 if (tmpshortpath
[sp
] == '.')
2098 if (tmplen
== 1 || (tmplen
== 2 && tmpshortpath
[sp
+ 1] == '.'))
2106 /* Check if the file exists and use the existing short file name */
2107 handle
= FindFirstFileW( tmpshortpath
, &wfd
);
2108 if (handle
== INVALID_HANDLE_VALUE
) goto notfound
;
2109 FindClose( handle
);
2111 /* In rare cases (like "a.abcd") short path may be longer than original path.
2112 * Make sure we have enough space in temp buffer. */
2113 if (wfd
.cAlternateFileName
[0] && tmplen
< lstrlenW(wfd
.cAlternateFileName
))
2116 buf_len
+= lstrlenW( wfd
.cAlternateFileName
) - tmplen
;
2117 new_buf
= HeapReAlloc( GetProcessHeap(), 0, tmpshortpath
, buf_len
* sizeof(WCHAR
) );
2120 HeapFree( GetProcessHeap(), 0, tmpshortpath
);
2121 SetLastError( ERROR_OUTOFMEMORY
);
2124 tmpshortpath
= new_buf
;
2127 lstrcpyW( tmpshortpath
+ sp
, wfd
.cAlternateFileName
[0] ? wfd
.cAlternateFileName
: wfd
.cFileName
);
2128 sp
+= lstrlenW( tmpshortpath
+ sp
);
2131 tmpshortpath
[sp
] = 0;
2133 tmplen
= lstrlenW( tmpshortpath
) + 1;
2134 if (tmplen
<= shortlen
)
2136 lstrcpyW( shortpath
, tmpshortpath
);
2137 TRACE( "returning %s\n", debugstr_w( shortpath
));
2138 tmplen
--; /* length without 0 */
2141 HeapFree( GetProcessHeap(), 0, tmpshortpath
);
2145 HeapFree( GetProcessHeap(), 0, tmpshortpath
);
2146 TRACE( "not found\n" );
2147 SetLastError( ERROR_FILE_NOT_FOUND
);
2152 /***********************************************************************
2153 * GetSystemDirectoryA (kernelbase.@)
2155 UINT WINAPI DECLSPEC_HOTPATCH
GetSystemDirectoryA( LPSTR path
, UINT count
)
2157 return copy_filename_WtoA( system_dir
, path
, count
);
2161 /***********************************************************************
2162 * GetSystemDirectoryW (kernelbase.@)
2164 UINT WINAPI DECLSPEC_HOTPATCH
GetSystemDirectoryW( LPWSTR path
, UINT count
)
2166 return copy_filename( system_dir
, path
, count
);
2170 /***********************************************************************
2171 * GetSystemWindowsDirectoryA (kernelbase.@)
2173 UINT WINAPI DECLSPEC_HOTPATCH
GetSystemWindowsDirectoryA( LPSTR path
, UINT count
)
2175 return GetWindowsDirectoryA( path
, count
);
2179 /***********************************************************************
2180 * GetSystemWindowsDirectoryW (kernelbase.@)
2182 UINT WINAPI DECLSPEC_HOTPATCH
GetSystemWindowsDirectoryW( LPWSTR path
, UINT count
)
2184 return GetWindowsDirectoryW( path
, count
);
2188 /***********************************************************************
2189 * GetSystemWow64DirectoryA (kernelbase.@)
2191 UINT WINAPI
/* DECLSPEC_HOTPATCH */ GetSystemWow64DirectoryA( LPSTR path
, UINT count
)
2193 if (!is_win64
&& !is_wow64
)
2195 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
2198 return copy_filename_WtoA( get_machine_wow64_dir( IMAGE_FILE_MACHINE_I386
), path
, count
);
2202 /***********************************************************************
2203 * GetSystemWow64DirectoryW (kernelbase.@)
2205 UINT WINAPI
/* DECLSPEC_HOTPATCH */ GetSystemWow64DirectoryW( LPWSTR path
, UINT count
)
2207 if (!is_win64
&& !is_wow64
)
2209 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
2212 return copy_filename( get_machine_wow64_dir( IMAGE_FILE_MACHINE_I386
), path
, count
);
2216 /***********************************************************************
2217 * GetSystemWow64Directory2A (kernelbase.@)
2219 UINT WINAPI DECLSPEC_HOTPATCH
GetSystemWow64Directory2A( LPSTR path
, UINT count
, WORD machine
)
2221 const WCHAR
*dir
= get_machine_wow64_dir( machine
);
2223 return dir
? copy_filename_WtoA( dir
, path
, count
) : 0;
2227 /***********************************************************************
2228 * GetSystemWow64Directory2W (kernelbase.@)
2230 UINT WINAPI DECLSPEC_HOTPATCH
GetSystemWow64Directory2W( LPWSTR path
, UINT count
, WORD machine
)
2232 const WCHAR
*dir
= get_machine_wow64_dir( machine
);
2234 return dir
? copy_filename( dir
, path
, count
) : 0;
2238 /***********************************************************************
2239 * GetTempFileNameA (kernelbase.@)
2241 UINT WINAPI DECLSPEC_HOTPATCH
GetTempFileNameA( LPCSTR path
, LPCSTR prefix
, UINT unique
, LPSTR buffer
)
2243 WCHAR
*pathW
, *prefixW
= NULL
;
2244 WCHAR bufferW
[MAX_PATH
];
2247 if (!(pathW
= file_name_AtoW( path
, FALSE
))) return 0;
2248 if (prefix
&& !(prefixW
= file_name_AtoW( prefix
, TRUE
))) return 0;
2250 ret
= GetTempFileNameW( pathW
, prefixW
, unique
, bufferW
);
2251 if (ret
) file_name_WtoA( bufferW
, -1, buffer
, MAX_PATH
);
2253 HeapFree( GetProcessHeap(), 0, prefixW
);
2258 /***********************************************************************
2259 * GetTempFileNameW (kernelbase.@)
2261 UINT WINAPI DECLSPEC_HOTPATCH
GetTempFileNameW( LPCWSTR path
, LPCWSTR prefix
, UINT unique
, LPWSTR buffer
)
2267 if (!path
|| !buffer
)
2269 SetLastError( ERROR_INVALID_PARAMETER
);
2273 /* ensure that the provided directory exists */
2274 attr
= GetFileAttributesW( path
);
2275 if (attr
== INVALID_FILE_ATTRIBUTES
|| !(attr
& FILE_ATTRIBUTE_DIRECTORY
))
2277 TRACE( "path not found %s\n", debugstr_w( path
));
2278 SetLastError( ERROR_DIRECTORY
);
2282 lstrcpyW( buffer
, path
);
2283 p
= buffer
+ lstrlenW(buffer
);
2285 /* add a \, if there isn't one */
2286 if ((p
== buffer
) || (p
[-1] != '\\')) *p
++ = '\\';
2288 if (prefix
) for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
2291 if (unique
) swprintf( p
, MAX_PATH
- (p
- buffer
), L
"%x.tmp", unique
);
2294 /* get a "random" unique number and try to create the file */
2296 UINT num
= NtGetTickCount() & 0xffff;
2299 /* avoid using the same name twice in a short interval */
2300 if (last
- num
< 10) num
= last
+ 1;
2305 swprintf( p
, MAX_PATH
- (p
- buffer
), L
"%x.tmp", unique
);
2306 handle
= CreateFileW( buffer
, GENERIC_WRITE
, 0, NULL
, CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, 0 );
2307 if (handle
!= INVALID_HANDLE_VALUE
)
2308 { /* We created it */
2309 CloseHandle( handle
);
2313 if (GetLastError() != ERROR_FILE_EXISTS
&& GetLastError() != ERROR_SHARING_VIOLATION
)
2314 break; /* No need to go on */
2315 if (!(++unique
& 0xffff)) unique
= 1;
2316 } while (unique
!= num
);
2318 TRACE( "returning %s\n", debugstr_w( buffer
));
2323 /***********************************************************************
2324 * GetTempPathA (kernelbase.@)
2326 DWORD WINAPI DECLSPEC_HOTPATCH
GetTempPathA( DWORD count
, LPSTR path
)
2328 WCHAR pathW
[MAX_PATH
];
2331 if (!(ret
= GetTempPathW( MAX_PATH
, pathW
))) return 0;
2334 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
2337 return copy_filename_WtoA( pathW
, path
, count
);
2341 /***********************************************************************
2342 * GetTempPathW (kernelbase.@)
2344 DWORD WINAPI DECLSPEC_HOTPATCH
GetTempPathW( DWORD count
, LPWSTR path
)
2346 WCHAR tmp_path
[MAX_PATH
];
2349 if (!(ret
= GetEnvironmentVariableW( L
"TMP", tmp_path
, MAX_PATH
)) &&
2350 !(ret
= GetEnvironmentVariableW( L
"TEMP", tmp_path
, MAX_PATH
)) &&
2351 !(ret
= GetEnvironmentVariableW( L
"USERPROFILE", tmp_path
, MAX_PATH
)) &&
2352 !(ret
= GetWindowsDirectoryW( tmp_path
, MAX_PATH
)))
2357 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
2360 ret
= GetFullPathNameW( tmp_path
, MAX_PATH
, tmp_path
, NULL
);
2363 if (ret
> MAX_PATH
- 2)
2365 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
2368 if (tmp_path
[ret
-1] != '\\')
2370 tmp_path
[ret
++] = '\\';
2371 tmp_path
[ret
] = '\0';
2374 ret
++; /* add space for terminating 0 */
2377 lstrcpynW( path
, tmp_path
, count
);
2378 /* the remaining buffer must be zeroed up to 32766 bytes in XP or 32767
2379 * bytes after it, we will assume the > XP behavior for now */
2380 memset( path
+ ret
, 0, (min(count
, 32767) - ret
) * sizeof(WCHAR
) );
2381 ret
--; /* return length without 0 */
2385 /* the buffer must be cleared if contents will not fit */
2386 memset( path
, 0, count
* sizeof(WCHAR
) );
2389 TRACE( "returning %u, %s\n", ret
, debugstr_w( path
));
2394 /***********************************************************************
2395 * GetWindowsDirectoryA (kernelbase.@)
2397 UINT WINAPI DECLSPEC_HOTPATCH
GetWindowsDirectoryA( LPSTR path
, UINT count
)
2399 return copy_filename_WtoA( windows_dir
, path
, count
);
2403 /***********************************************************************
2404 * GetWindowsDirectoryW (kernelbase.@)
2406 UINT WINAPI DECLSPEC_HOTPATCH
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
2408 return copy_filename( windows_dir
, path
, count
);
2412 /**************************************************************************
2413 * MoveFileExW (kernelbase.@)
2415 BOOL WINAPI
MoveFileExW( const WCHAR
*source
, const WCHAR
*dest
, DWORD flag
)
2417 return MoveFileWithProgressW( source
, dest
, NULL
, NULL
, flag
);
2421 /**************************************************************************
2422 * MoveFileWithProgressW (kernelbase.@)
2424 BOOL WINAPI DECLSPEC_HOTPATCH
MoveFileWithProgressW( const WCHAR
*source
, const WCHAR
*dest
,
2425 LPPROGRESS_ROUTINE progress
,
2426 void *param
, DWORD flag
)
2428 FILE_RENAME_INFORMATION
*rename_info
;
2429 FILE_BASIC_INFORMATION info
;
2430 UNICODE_STRING nt_name
;
2431 OBJECT_ATTRIBUTES attr
;
2434 HANDLE source_handle
= 0;
2437 TRACE( "(%s,%s,%p,%p,%04x)\n", debugstr_w(source
), debugstr_w(dest
), progress
, param
, flag
);
2439 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
) return add_boot_rename_entry( source
, dest
, flag
);
2441 if (!dest
) return DeleteFileW( source
);
2443 /* check if we are allowed to rename the source */
2445 if (!RtlDosPathNameToNtPathName_U( source
, &nt_name
, NULL
, NULL
))
2447 SetLastError( ERROR_PATH_NOT_FOUND
);
2450 attr
.Length
= sizeof(attr
);
2451 attr
.RootDirectory
= 0;
2452 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2453 attr
.ObjectName
= &nt_name
;
2454 attr
.SecurityDescriptor
= NULL
;
2455 attr
.SecurityQualityOfService
= NULL
;
2457 status
= NtOpenFile( &source_handle
, DELETE
| SYNCHRONIZE
, &attr
, &io
,
2458 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
2459 FILE_SYNCHRONOUS_IO_NONALERT
);
2460 RtlFreeUnicodeString( &nt_name
);
2461 if (!set_ntstatus( status
)) goto error
;
2463 status
= NtQueryInformationFile( source_handle
, &io
, &info
, sizeof(info
), FileBasicInformation
);
2464 if (!set_ntstatus( status
)) goto error
;
2466 if (!RtlDosPathNameToNtPathName_U( dest
, &nt_name
, NULL
, NULL
))
2468 SetLastError( ERROR_PATH_NOT_FOUND
);
2472 size
= offsetof( FILE_RENAME_INFORMATION
, FileName
) + nt_name
.Length
;
2473 if (!(rename_info
= HeapAlloc( GetProcessHeap(), 0, size
))) goto error
;
2475 rename_info
->ReplaceIfExists
= !!(flag
& MOVEFILE_REPLACE_EXISTING
);
2476 rename_info
->RootDirectory
= NULL
;
2477 rename_info
->FileNameLength
= nt_name
.Length
;
2478 memcpy( rename_info
->FileName
, nt_name
.Buffer
, nt_name
.Length
);
2479 RtlFreeUnicodeString( &nt_name
);
2480 status
= NtSetInformationFile( source_handle
, &io
, rename_info
, size
, FileRenameInformation
);
2481 HeapFree( GetProcessHeap(), 0, rename_info
);
2482 if (status
== STATUS_NOT_SAME_DEVICE
&& (flag
& MOVEFILE_COPY_ALLOWED
))
2484 NtClose( source_handle
);
2485 if (!CopyFileExW( source
, dest
, progress
, param
, NULL
,
2486 flag
& MOVEFILE_REPLACE_EXISTING
? 0 : COPY_FILE_FAIL_IF_EXISTS
))
2488 return DeleteFileW( source
);
2491 NtClose( source_handle
);
2492 return set_ntstatus( status
);
2495 if (source_handle
) NtClose( source_handle
);
2500 /***********************************************************************
2501 * NeedCurrentDirectoryForExePathA (kernelbase.@)
2503 BOOL WINAPI DECLSPEC_HOTPATCH
NeedCurrentDirectoryForExePathA( LPCSTR name
)
2507 if (!(nameW
= file_name_AtoW( name
, FALSE
))) return TRUE
;
2508 return NeedCurrentDirectoryForExePathW( nameW
);
2512 /***********************************************************************
2513 * NeedCurrentDirectoryForExePathW (kernelbase.@)
2515 BOOL WINAPI DECLSPEC_HOTPATCH
NeedCurrentDirectoryForExePathW( LPCWSTR name
)
2519 if (wcschr( name
, '\\' )) return TRUE
;
2520 /* check the existence of the variable, not value */
2521 return !GetEnvironmentVariableW( L
"NoDefaultCurrentDirectoryInExePath", &env_val
, 1 );
2525 /***********************************************************************
2526 * ReplaceFileW (kernelbase.@)
2528 BOOL WINAPI DECLSPEC_HOTPATCH
ReplaceFileW( const WCHAR
*replaced
, const WCHAR
*replacement
,
2529 const WCHAR
*backup
, DWORD flags
,
2530 void *exclude
, void *reserved
)
2532 UNICODE_STRING nt_replaced_name
, nt_replacement_name
;
2533 HANDLE hReplacement
= NULL
;
2536 OBJECT_ATTRIBUTES attr
;
2537 FILE_BASIC_INFORMATION info
;
2539 TRACE( "%s %s %s 0x%08x %p %p\n", debugstr_w(replaced
), debugstr_w(replacement
), debugstr_w(backup
),
2540 flags
, exclude
, reserved
);
2542 if (flags
) FIXME("Ignoring flags %x\n", flags
);
2544 /* First two arguments are mandatory */
2545 if (!replaced
|| !replacement
)
2547 SetLastError(ERROR_INVALID_PARAMETER
);
2551 attr
.Length
= sizeof(attr
);
2552 attr
.RootDirectory
= 0;
2553 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2554 attr
.ObjectName
= NULL
;
2555 attr
.SecurityDescriptor
= NULL
;
2556 attr
.SecurityQualityOfService
= NULL
;
2558 /* Open the "replaced" file for reading */
2559 if (!RtlDosPathNameToNtPathName_U( replaced
, &nt_replaced_name
, NULL
, NULL
))
2561 SetLastError( ERROR_PATH_NOT_FOUND
);
2564 attr
.ObjectName
= &nt_replaced_name
;
2566 /* Replacement should fail if replaced is READ_ONLY */
2567 status
= NtQueryAttributesFile(&attr
, &info
);
2568 RtlFreeUnicodeString(&nt_replaced_name
);
2569 if (!set_ntstatus( status
)) return FALSE
;
2571 if (info
.FileAttributes
& FILE_ATTRIBUTE_READONLY
)
2573 SetLastError( ERROR_ACCESS_DENIED
);
2578 * Open the replacement file for reading, writing, and deleting
2579 * (writing and deleting are needed when finished)
2581 if (!RtlDosPathNameToNtPathName_U( replacement
, &nt_replacement_name
, NULL
, NULL
))
2583 SetLastError( ERROR_PATH_NOT_FOUND
);
2586 attr
.ObjectName
= &nt_replacement_name
;
2587 status
= NtOpenFile( &hReplacement
, GENERIC_READ
| GENERIC_WRITE
| DELETE
| WRITE_DAC
| SYNCHRONIZE
,
2588 &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
| FILE_NON_DIRECTORY_FILE
);
2589 RtlFreeUnicodeString(&nt_replacement_name
);
2590 if (!set_ntstatus( status
)) return FALSE
;
2591 NtClose( hReplacement
);
2593 /* If the user wants a backup then that needs to be performed first */
2596 if (!MoveFileExW( replaced
, backup
, MOVEFILE_REPLACE_EXISTING
)) return FALSE
;
2600 /* ReplaceFile() can replace an open target. To do this, we need to move
2601 * it out of the way first. */
2602 WCHAR temp_path
[MAX_PATH
], temp_file
[MAX_PATH
];
2604 lstrcpynW( temp_path
, replaced
, ARRAY_SIZE( temp_path
) );
2605 PathRemoveFileSpecW( temp_path
);
2606 if (!GetTempFileNameW( temp_path
, L
"rf", 0, temp_file
) ||
2607 !MoveFileExW( replaced
, temp_file
, MOVEFILE_REPLACE_EXISTING
))
2610 DeleteFileW( temp_file
);
2614 * Now that the backup has been performed (if requested), copy the replacement
2617 if (!MoveFileExW( replacement
, replaced
, 0 ))
2619 /* on failure we need to indicate whether a backup was made */
2621 SetLastError( ERROR_UNABLE_TO_MOVE_REPLACEMENT
);
2623 SetLastError( ERROR_UNABLE_TO_MOVE_REPLACEMENT_2
);
2630 /***********************************************************************
2631 * SearchPathA (kernelbase.@)
2633 DWORD WINAPI DECLSPEC_HOTPATCH
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
2634 DWORD buflen
, LPSTR buffer
, LPSTR
*lastpart
)
2636 WCHAR
*pathW
= NULL
, *nameW
, *extW
= NULL
;
2637 WCHAR bufferW
[MAX_PATH
];
2642 SetLastError( ERROR_INVALID_PARAMETER
);
2646 if (!(nameW
= file_name_AtoW( name
, FALSE
))) return 0;
2647 if (path
&& !(pathW
= file_name_AtoW( path
, TRUE
))) return 0;
2648 if (ext
&& !(extW
= file_name_AtoW( ext
, TRUE
)))
2650 RtlFreeHeap( GetProcessHeap(), 0, pathW
);
2654 ret
= SearchPathW( pathW
, nameW
, extW
, MAX_PATH
, bufferW
, NULL
);
2656 RtlFreeHeap( GetProcessHeap(), 0, pathW
);
2657 RtlFreeHeap( GetProcessHeap(), 0, extW
);
2662 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
2665 ret
= copy_filename_WtoA( bufferW
, buffer
, buflen
);
2666 if (buflen
> ret
&& lastpart
) *lastpart
= strrchr(buffer
, '\\') + 1;
2671 /***********************************************************************
2672 * SearchPathW (kernelbase.@)
2674 DWORD WINAPI DECLSPEC_HOTPATCH
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
, DWORD buflen
,
2675 LPWSTR buffer
, LPWSTR
*lastpart
)
2680 if (!name
|| !name
[0])
2682 SetLastError( ERROR_INVALID_PARAMETER
);
2686 /* If the name contains an explicit path, ignore the path */
2688 if (contains_path( name
))
2690 /* try first without extension */
2691 if (RtlDoesFileExists_U( name
)) return GetFullPathNameW( name
, buflen
, buffer
, lastpart
);
2693 if ((name_ext
= append_ext( name
, ext
)))
2695 if (RtlDoesFileExists_U( name_ext
))
2696 ret
= GetFullPathNameW( name_ext
, buflen
, buffer
, lastpart
);
2697 RtlFreeHeap( GetProcessHeap(), 0, name_ext
);
2700 else if (path
&& path
[0]) /* search in the specified path */
2702 ret
= RtlDosSearchPath_U( path
, name
, ext
, buflen
* sizeof(WCHAR
),
2703 buffer
, lastpart
) / sizeof(WCHAR
);
2705 else /* search in active context and default path */
2707 WCHAR
*dll_path
= NULL
, *name_ext
= append_ext( name
, ext
);
2709 if (name_ext
) name
= name_ext
;
2711 /* When file is found with activation context no attempt is made
2712 to check if it's really exist, path is returned only basing on context info. */
2713 if (find_actctx_dllpath( name
, &dll_path
) == STATUS_SUCCESS
)
2715 ret
= lstrlenW( dll_path
) + lstrlenW( name
) + 1;
2717 /* count null termination char too */
2720 lstrcpyW( buffer
, dll_path
);
2721 lstrcatW( buffer
, name
);
2722 if (lastpart
) *lastpart
= buffer
+ lstrlenW( dll_path
);
2725 else if (lastpart
) *lastpart
= NULL
;
2726 RtlFreeHeap( GetProcessHeap(), 0, dll_path
);
2728 else if (!RtlGetSearchPath( &dll_path
))
2730 ret
= RtlDosSearchPath_U( dll_path
, name
, NULL
, buflen
* sizeof(WCHAR
),
2731 buffer
, lastpart
) / sizeof(WCHAR
);
2732 RtlReleasePath( dll_path
);
2734 RtlFreeHeap( GetProcessHeap(), 0, name_ext
);
2737 if (!ret
) SetLastError( ERROR_FILE_NOT_FOUND
);
2738 else TRACE( "found %s\n", debugstr_w(buffer
) );
2743 /***********************************************************************
2744 * SetCurrentDirectoryA (kernelbase.@)
2746 BOOL WINAPI DECLSPEC_HOTPATCH
SetCurrentDirectoryA( LPCSTR dir
)
2749 UNICODE_STRING strW
;
2751 if (!(dirW
= file_name_AtoW( dir
, FALSE
))) return FALSE
;
2752 RtlInitUnicodeString( &strW
, dirW
);
2753 return set_ntstatus( RtlSetCurrentDirectory_U( &strW
));
2757 /***********************************************************************
2758 * SetCurrentDirectoryW (kernelbase.@)
2760 BOOL WINAPI DECLSPEC_HOTPATCH
SetCurrentDirectoryW( LPCWSTR dir
)
2762 UNICODE_STRING dirW
;
2764 RtlInitUnicodeString( &dirW
, dir
);
2765 return set_ntstatus( RtlSetCurrentDirectory_U( &dirW
));
2769 /**************************************************************************
2770 * SetFileApisToANSI (kernelbase.@)
2772 void WINAPI DECLSPEC_HOTPATCH
SetFileApisToANSI(void)
2774 oem_file_apis
= FALSE
;
2778 /**************************************************************************
2779 * SetFileApisToOEM (kernelbase.@)
2781 void WINAPI DECLSPEC_HOTPATCH
SetFileApisToOEM(void)
2783 oem_file_apis
= TRUE
;
2787 /**************************************************************************
2788 * SetFileAttributesA (kernelbase.@)
2790 BOOL WINAPI DECLSPEC_HOTPATCH
SetFileAttributesA( LPCSTR name
, DWORD attributes
)
2794 if (!(nameW
= file_name_AtoW( name
, FALSE
))) return FALSE
;
2795 return SetFileAttributesW( nameW
, attributes
);
2799 /**************************************************************************
2800 * SetFileAttributesW (kernelbase.@)
2802 BOOL WINAPI DECLSPEC_HOTPATCH
SetFileAttributesW( LPCWSTR name
, DWORD attributes
)
2804 UNICODE_STRING nt_name
;
2805 OBJECT_ATTRIBUTES attr
;
2810 TRACE( "%s %x\n", debugstr_w(name
), attributes
);
2812 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
2814 SetLastError( ERROR_PATH_NOT_FOUND
);
2818 attr
.Length
= sizeof(attr
);
2819 attr
.RootDirectory
= 0;
2820 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2821 attr
.ObjectName
= &nt_name
;
2822 attr
.SecurityDescriptor
= NULL
;
2823 attr
.SecurityQualityOfService
= NULL
;
2825 status
= NtOpenFile( &handle
, SYNCHRONIZE
, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
2826 RtlFreeUnicodeString( &nt_name
);
2828 if (status
== STATUS_SUCCESS
)
2830 FILE_BASIC_INFORMATION info
;
2832 memset( &info
, 0, sizeof(info
) );
2833 info
.FileAttributes
= attributes
| FILE_ATTRIBUTE_NORMAL
; /* make sure it's not zero */
2834 status
= NtSetInformationFile( handle
, &io
, &info
, sizeof(info
), FileBasicInformation
);
2837 return set_ntstatus( status
);
2841 /***********************************************************************
2842 * Wow64DisableWow64FsRedirection (kernelbase.@)
2844 BOOL WINAPI DECLSPEC_HOTPATCH
Wow64DisableWow64FsRedirection( PVOID
*old_value
)
2846 return set_ntstatus( RtlWow64EnableFsRedirectionEx( TRUE
, (ULONG
*)old_value
));
2850 /***********************************************************************
2851 * Wow64RevertWow64FsRedirection (kernelbase.@)
2853 BOOL WINAPI DECLSPEC_HOTPATCH
Wow64RevertWow64FsRedirection( PVOID old_value
)
2855 return set_ntstatus( RtlWow64EnableFsRedirection( !old_value
));
2859 /***********************************************************************
2860 * Operations on file handles
2861 ***********************************************************************/
2864 /***********************************************************************
2865 * CancelIo (kernelbase.@)
2867 BOOL WINAPI DECLSPEC_HOTPATCH
CancelIo( HANDLE handle
)
2871 NtCancelIoFile( handle
, &io
);
2872 return set_ntstatus( io
.u
.Status
);
2876 /***********************************************************************
2877 * CancelIoEx (kernelbase.@)
2879 BOOL WINAPI DECLSPEC_HOTPATCH
CancelIoEx( HANDLE handle
, LPOVERLAPPED overlapped
)
2883 NtCancelIoFileEx( handle
, (PIO_STATUS_BLOCK
)overlapped
, &io
);
2884 return set_ntstatus( io
.u
.Status
);
2888 /***********************************************************************
2889 * CancelSynchronousIo (kernelbase.@)
2891 BOOL WINAPI DECLSPEC_HOTPATCH
CancelSynchronousIo( HANDLE thread
)
2893 FIXME( "(%p): stub\n", thread
);
2894 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
2899 /***********************************************************************
2900 * FlushFileBuffers (kernelbase.@)
2902 BOOL WINAPI DECLSPEC_HOTPATCH
FlushFileBuffers( HANDLE file
)
2904 IO_STATUS_BLOCK iosb
;
2906 return set_ntstatus( NtFlushBuffersFile( file
, &iosb
));
2910 /***********************************************************************
2911 * GetFileInformationByHandle (kernelbase.@)
2913 BOOL WINAPI DECLSPEC_HOTPATCH
GetFileInformationByHandle( HANDLE file
, BY_HANDLE_FILE_INFORMATION
*info
)
2915 FILE_FS_VOLUME_INFORMATION volume_info
;
2916 FILE_ALL_INFORMATION all_info
;
2920 status
= NtQueryInformationFile( file
, &io
, &all_info
, sizeof(all_info
), FileAllInformation
);
2921 if (status
== STATUS_BUFFER_OVERFLOW
) status
= STATUS_SUCCESS
;
2922 if (!set_ntstatus( status
)) return FALSE
;
2924 info
->dwFileAttributes
= all_info
.BasicInformation
.FileAttributes
;
2925 info
->ftCreationTime
.dwHighDateTime
= all_info
.BasicInformation
.CreationTime
.u
.HighPart
;
2926 info
->ftCreationTime
.dwLowDateTime
= all_info
.BasicInformation
.CreationTime
.u
.LowPart
;
2927 info
->ftLastAccessTime
.dwHighDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.HighPart
;
2928 info
->ftLastAccessTime
.dwLowDateTime
= all_info
.BasicInformation
.LastAccessTime
.u
.LowPart
;
2929 info
->ftLastWriteTime
.dwHighDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.HighPart
;
2930 info
->ftLastWriteTime
.dwLowDateTime
= all_info
.BasicInformation
.LastWriteTime
.u
.LowPart
;
2931 info
->dwVolumeSerialNumber
= 0;
2932 info
->nFileSizeHigh
= all_info
.StandardInformation
.EndOfFile
.u
.HighPart
;
2933 info
->nFileSizeLow
= all_info
.StandardInformation
.EndOfFile
.u
.LowPart
;
2934 info
->nNumberOfLinks
= all_info
.StandardInformation
.NumberOfLinks
;
2935 info
->nFileIndexHigh
= all_info
.InternalInformation
.IndexNumber
.u
.HighPart
;
2936 info
->nFileIndexLow
= all_info
.InternalInformation
.IndexNumber
.u
.LowPart
;
2938 status
= NtQueryVolumeInformationFile( file
, &io
, &volume_info
, sizeof(volume_info
), FileFsVolumeInformation
);
2939 if (status
== STATUS_SUCCESS
|| status
== STATUS_BUFFER_OVERFLOW
)
2940 info
->dwVolumeSerialNumber
= volume_info
.VolumeSerialNumber
;
2946 /***********************************************************************
2947 * GetFileInformationByHandleEx (kernelbase.@)
2949 BOOL WINAPI DECLSPEC_HOTPATCH
GetFileInformationByHandleEx( HANDLE handle
, FILE_INFO_BY_HANDLE_CLASS
class,
2950 LPVOID info
, DWORD size
)
2957 case FileStreamInfo
:
2958 case FileCompressionInfo
:
2959 case FileRemoteProtocolInfo
:
2960 case FileFullDirectoryInfo
:
2961 case FileFullDirectoryRestartInfo
:
2962 case FileStorageInfo
:
2963 case FileAlignmentInfo
:
2964 case FileIdExtdDirectoryInfo
:
2965 case FileIdExtdDirectoryRestartInfo
:
2966 FIXME( "%p, %u, %p, %u\n", handle
, class, info
, size
);
2967 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
2970 case FileAttributeTagInfo
:
2971 status
= NtQueryInformationFile( handle
, &io
, info
, size
, FileAttributeTagInformation
);
2975 status
= NtQueryInformationFile( handle
, &io
, info
, size
, FileBasicInformation
);
2978 case FileStandardInfo
:
2979 status
= NtQueryInformationFile( handle
, &io
, info
, size
, FileStandardInformation
);
2983 status
= NtQueryInformationFile( handle
, &io
, info
, size
, FileNameInformation
);
2987 status
= NtQueryInformationFile( handle
, &io
, info
, size
, FileIdInformation
);
2990 case FileIdBothDirectoryRestartInfo
:
2991 case FileIdBothDirectoryInfo
:
2992 status
= NtQueryDirectoryFile( handle
, NULL
, NULL
, NULL
, &io
, info
, size
,
2993 FileIdBothDirectoryInformation
, FALSE
, NULL
,
2994 (class == FileIdBothDirectoryRestartInfo
) );
2997 case FileRenameInfo
:
2998 case FileDispositionInfo
:
2999 case FileAllocationInfo
:
3000 case FileIoPriorityHintInfo
:
3001 case FileEndOfFileInfo
:
3003 SetLastError( ERROR_INVALID_PARAMETER
);
3006 return set_ntstatus( status
);
3010 /***********************************************************************
3011 * GetFileSize (kernelbase.@)
3013 DWORD WINAPI DECLSPEC_HOTPATCH
GetFileSize( HANDLE file
, LPDWORD size_high
)
3017 if (!GetFileSizeEx( file
, &size
)) return INVALID_FILE_SIZE
;
3018 if (size_high
) *size_high
= size
.u
.HighPart
;
3019 if (size
.u
.LowPart
== INVALID_FILE_SIZE
) SetLastError( 0 );
3020 return size
.u
.LowPart
;
3024 /***********************************************************************
3025 * GetFileSizeEx (kernelbase.@)
3027 BOOL WINAPI DECLSPEC_HOTPATCH
GetFileSizeEx( HANDLE file
, PLARGE_INTEGER size
)
3029 FILE_STANDARD_INFORMATION info
;
3032 if (!set_ntstatus( NtQueryInformationFile( file
, &io
, &info
, sizeof(info
), FileStandardInformation
)))
3035 *size
= info
.EndOfFile
;
3040 /***********************************************************************
3041 * GetFileTime (kernelbase.@)
3043 BOOL WINAPI DECLSPEC_HOTPATCH
GetFileTime( HANDLE file
, FILETIME
*creation
,
3044 FILETIME
*access
, FILETIME
*write
)
3046 FILE_BASIC_INFORMATION info
;
3049 if (!set_ntstatus( NtQueryInformationFile( file
, &io
, &info
, sizeof(info
), FileBasicInformation
)))
3054 creation
->dwHighDateTime
= info
.CreationTime
.u
.HighPart
;
3055 creation
->dwLowDateTime
= info
.CreationTime
.u
.LowPart
;
3059 access
->dwHighDateTime
= info
.LastAccessTime
.u
.HighPart
;
3060 access
->dwLowDateTime
= info
.LastAccessTime
.u
.LowPart
;
3064 write
->dwHighDateTime
= info
.LastWriteTime
.u
.HighPart
;
3065 write
->dwLowDateTime
= info
.LastWriteTime
.u
.LowPart
;
3071 /***********************************************************************
3072 * GetFileType (kernelbase.@)
3074 DWORD WINAPI DECLSPEC_HOTPATCH
GetFileType( HANDLE file
)
3076 FILE_FS_DEVICE_INFORMATION info
;
3079 if (file
== (HANDLE
)STD_INPUT_HANDLE
||
3080 file
== (HANDLE
)STD_OUTPUT_HANDLE
||
3081 file
== (HANDLE
)STD_ERROR_HANDLE
)
3082 file
= GetStdHandle( (DWORD_PTR
)file
);
3084 if (!set_ntstatus( NtQueryVolumeInformationFile( file
, &io
, &info
, sizeof(info
),
3085 FileFsDeviceInformation
)))
3086 return FILE_TYPE_UNKNOWN
;
3088 switch (info
.DeviceType
)
3090 case FILE_DEVICE_NULL
:
3091 case FILE_DEVICE_CONSOLE
:
3092 case FILE_DEVICE_SERIAL_PORT
:
3093 case FILE_DEVICE_PARALLEL_PORT
:
3094 case FILE_DEVICE_TAPE
:
3095 case FILE_DEVICE_UNKNOWN
:
3096 return FILE_TYPE_CHAR
;
3097 case FILE_DEVICE_NAMED_PIPE
:
3098 return FILE_TYPE_PIPE
;
3100 return FILE_TYPE_DISK
;
3105 /***********************************************************************
3106 * GetOverlappedResult (kernelbase.@)
3108 BOOL WINAPI DECLSPEC_HOTPATCH
GetOverlappedResult( HANDLE file
, LPOVERLAPPED overlapped
,
3109 LPDWORD result
, BOOL wait
)
3111 return GetOverlappedResultEx( file
, overlapped
, result
, wait
? INFINITE
: 0, FALSE
);
3115 /***********************************************************************
3116 * GetOverlappedResultEx (kernelbase.@)
3118 BOOL WINAPI DECLSPEC_HOTPATCH
GetOverlappedResultEx( HANDLE file
, OVERLAPPED
*overlapped
,
3119 DWORD
*result
, DWORD timeout
, BOOL alertable
)
3124 TRACE( "(%p %p %p %u %d)\n", file
, overlapped
, result
, timeout
, alertable
);
3126 status
= overlapped
->Internal
;
3127 if (status
== STATUS_PENDING
)
3131 SetLastError( ERROR_IO_INCOMPLETE
);
3134 ret
= WaitForSingleObjectEx( overlapped
->hEvent
? overlapped
->hEvent
: file
, timeout
, alertable
);
3135 if (ret
== WAIT_FAILED
)
3139 SetLastError( ret
);
3143 status
= overlapped
->Internal
;
3144 if (status
== STATUS_PENDING
) status
= STATUS_SUCCESS
;
3147 *result
= overlapped
->InternalHigh
;
3148 return set_ntstatus( status
);
3152 /**************************************************************************
3153 * LockFile (kernelbase.@)
3155 BOOL WINAPI DECLSPEC_HOTPATCH
LockFile( HANDLE file
, DWORD offset_low
, DWORD offset_high
,
3156 DWORD count_low
, DWORD count_high
)
3158 LARGE_INTEGER count
, offset
;
3160 TRACE( "%p %x%08x %x%08x\n", file
, offset_high
, offset_low
, count_high
, count_low
);
3162 count
.u
.LowPart
= count_low
;
3163 count
.u
.HighPart
= count_high
;
3164 offset
.u
.LowPart
= offset_low
;
3165 offset
.u
.HighPart
= offset_high
;
3166 return set_ntstatus( NtLockFile( file
, 0, NULL
, NULL
, NULL
, &offset
, &count
, NULL
, TRUE
, TRUE
));
3170 /**************************************************************************
3171 * LockFileEx (kernelbase.@)
3173 BOOL WINAPI DECLSPEC_HOTPATCH
LockFileEx( HANDLE file
, DWORD flags
, DWORD reserved
,
3174 DWORD count_low
, DWORD count_high
, LPOVERLAPPED overlapped
)
3176 LARGE_INTEGER count
, offset
;
3177 LPVOID cvalue
= NULL
;
3181 SetLastError( ERROR_INVALID_PARAMETER
);
3185 TRACE( "%p %x%08x %x%08x flags %x\n",
3186 file
, overlapped
->u
.s
.OffsetHigh
, overlapped
->u
.s
.Offset
, count_high
, count_low
, flags
);
3188 count
.u
.LowPart
= count_low
;
3189 count
.u
.HighPart
= count_high
;
3190 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
3191 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
3193 if (((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) cvalue
= overlapped
;
3195 return set_ntstatus( NtLockFile( file
, overlapped
->hEvent
, NULL
, cvalue
,
3196 NULL
, &offset
, &count
, NULL
,
3197 flags
& LOCKFILE_FAIL_IMMEDIATELY
,
3198 flags
& LOCKFILE_EXCLUSIVE_LOCK
));
3202 /***********************************************************************
3203 * OpenFileById (kernelbase.@)
3205 HANDLE WINAPI DECLSPEC_HOTPATCH
OpenFileById( HANDLE handle
, LPFILE_ID_DESCRIPTOR id
, DWORD access
,
3206 DWORD share
, LPSECURITY_ATTRIBUTES sec_attr
, DWORD flags
)
3210 OBJECT_ATTRIBUTES attr
;
3212 UNICODE_STRING objectName
;
3216 SetLastError( ERROR_INVALID_PARAMETER
);
3217 return INVALID_HANDLE_VALUE
;
3220 options
= FILE_OPEN_BY_FILE_ID
;
3221 if (flags
& FILE_FLAG_BACKUP_SEMANTICS
)
3222 options
|= FILE_OPEN_FOR_BACKUP_INTENT
;
3224 options
|= FILE_NON_DIRECTORY_FILE
;
3225 if (flags
& FILE_FLAG_NO_BUFFERING
) options
|= FILE_NO_INTERMEDIATE_BUFFERING
;
3226 if (!(flags
& FILE_FLAG_OVERLAPPED
)) options
|= FILE_SYNCHRONOUS_IO_NONALERT
;
3227 if (flags
& FILE_FLAG_RANDOM_ACCESS
) options
|= FILE_RANDOM_ACCESS
;
3228 flags
&= FILE_ATTRIBUTE_VALID_FLAGS
;
3230 objectName
.Length
= sizeof(ULONGLONG
);
3231 objectName
.Buffer
= (WCHAR
*)&id
->u
.FileId
;
3232 attr
.Length
= sizeof(attr
);
3233 attr
.RootDirectory
= handle
;
3234 attr
.Attributes
= 0;
3235 attr
.ObjectName
= &objectName
;
3236 attr
.SecurityDescriptor
= sec_attr
? sec_attr
->lpSecurityDescriptor
: NULL
;
3237 attr
.SecurityQualityOfService
= NULL
;
3238 if (sec_attr
&& sec_attr
->bInheritHandle
) attr
.Attributes
|= OBJ_INHERIT
;
3240 if (!set_ntstatus( NtCreateFile( &result
, access
| SYNCHRONIZE
, &attr
, &io
, NULL
, flags
,
3241 share
, OPEN_EXISTING
, options
, NULL
, 0 )))
3242 return INVALID_HANDLE_VALUE
;
3247 /***********************************************************************
3248 * ReOpenFile (kernelbase.@)
3250 HANDLE WINAPI DECLSPEC_HOTPATCH
ReOpenFile( HANDLE handle
, DWORD access
, DWORD sharing
, DWORD attributes
)
3252 SECURITY_QUALITY_OF_SERVICE qos
;
3253 OBJECT_ATTRIBUTES attr
;
3254 UNICODE_STRING empty
= { 0 };
3259 TRACE("handle %p, access %#x, sharing %#x, attributes %#x.\n", handle
, access
, sharing
, attributes
);
3261 if (attributes
& 0x7ffff) /* FILE_ATTRIBUTE_* flags are invalid */
3263 SetLastError(ERROR_INVALID_PARAMETER
);
3264 return INVALID_HANDLE_VALUE
;
3267 if (attributes
& FILE_FLAG_DELETE_ON_CLOSE
)
3270 InitializeObjectAttributes( &attr
, &empty
, OBJ_CASE_INSENSITIVE
, handle
, NULL
);
3271 if (attributes
& SECURITY_SQOS_PRESENT
)
3273 qos
.Length
= sizeof(qos
);
3274 qos
.ImpersonationLevel
= (attributes
>> 16) & 0x3;
3275 qos
.ContextTrackingMode
= attributes
& SECURITY_CONTEXT_TRACKING
? SECURITY_DYNAMIC_TRACKING
: SECURITY_STATIC_TRACKING
;
3276 qos
.EffectiveOnly
= (attributes
& SECURITY_EFFECTIVE_ONLY
) != 0;
3277 attr
.SecurityQualityOfService
= &qos
;
3280 status
= NtCreateFile( &file
, access
| SYNCHRONIZE
| FILE_READ_ATTRIBUTES
, &attr
, &io
, NULL
,
3281 0, sharing
, FILE_OPEN
, get_nt_file_options( attributes
), NULL
, 0 );
3282 if (!set_ntstatus( status
))
3283 return INVALID_HANDLE_VALUE
;
3288 static void WINAPI
invoke_completion( void *context
, IO_STATUS_BLOCK
*io
, ULONG res
)
3290 LPOVERLAPPED_COMPLETION_ROUTINE completion
= context
;
3291 completion( io
->u
.Status
, io
->Information
, (LPOVERLAPPED
)io
);
3294 /****************************************************************************
3295 * ReadDirectoryChangesW (kernelbase.@)
3297 BOOL WINAPI DECLSPEC_HOTPATCH
ReadDirectoryChangesW( HANDLE handle
, LPVOID buffer
, DWORD len
,
3298 BOOL subtree
, DWORD filter
, LPDWORD returned
,
3299 LPOVERLAPPED overlapped
,
3300 LPOVERLAPPED_COMPLETION_ROUTINE completion
)
3302 OVERLAPPED ov
, *pov
;
3303 IO_STATUS_BLOCK
*ios
;
3305 LPVOID cvalue
= NULL
;
3307 TRACE( "%p %p %08x %d %08x %p %p %p\n",
3308 handle
, buffer
, len
, subtree
, filter
, returned
, overlapped
, completion
);
3312 memset( &ov
, 0, sizeof ov
);
3313 ov
.hEvent
= CreateEventW( NULL
, 0, 0, NULL
);
3319 if (completion
) cvalue
= completion
;
3320 else if (((ULONG_PTR
)overlapped
->hEvent
& 1) == 0) cvalue
= overlapped
;
3323 ios
= (PIO_STATUS_BLOCK
)pov
;
3324 ios
->u
.Status
= STATUS_PENDING
;
3326 status
= NtNotifyChangeDirectoryFile( handle
, completion
&& overlapped
? NULL
: pov
->hEvent
,
3327 completion
&& overlapped
? invoke_completion
: NULL
,
3328 cvalue
, ios
, buffer
, len
, filter
, subtree
);
3329 if (status
== STATUS_PENDING
)
3331 if (overlapped
) return TRUE
;
3332 WaitForSingleObjectEx( ov
.hEvent
, INFINITE
, TRUE
);
3333 if (returned
) *returned
= ios
->Information
;
3334 status
= ios
->u
.Status
;
3336 if (!overlapped
) CloseHandle( ov
.hEvent
);
3337 return set_ntstatus( status
);
3341 /***********************************************************************
3342 * ReadFile (kernelbase.@)
3344 BOOL WINAPI DECLSPEC_HOTPATCH
ReadFile( HANDLE file
, LPVOID buffer
, DWORD count
,
3345 LPDWORD result
, LPOVERLAPPED overlapped
)
3347 LARGE_INTEGER offset
;
3348 PLARGE_INTEGER poffset
= NULL
;
3349 IO_STATUS_BLOCK iosb
;
3350 PIO_STATUS_BLOCK io_status
= &iosb
;
3353 LPVOID cvalue
= NULL
;
3355 TRACE( "%p %p %d %p %p\n", file
, buffer
, count
, result
, overlapped
);
3357 if (result
) *result
= 0;
3361 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
3362 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
3364 event
= overlapped
->hEvent
;
3365 io_status
= (PIO_STATUS_BLOCK
)overlapped
;
3366 if (((ULONG_PTR
)event
& 1) == 0) cvalue
= overlapped
;
3368 else io_status
->Information
= 0;
3369 io_status
->u
.Status
= STATUS_PENDING
;
3371 status
= NtReadFile( file
, event
, NULL
, cvalue
, io_status
, buffer
, count
, poffset
, NULL
);
3373 if (status
== STATUS_PENDING
&& !overlapped
)
3375 WaitForSingleObject( file
, INFINITE
);
3376 status
= io_status
->u
.Status
;
3379 if (result
) *result
= overlapped
&& status
? 0 : io_status
->Information
;
3381 if (status
== STATUS_END_OF_FILE
)
3383 if (overlapped
!= NULL
)
3385 SetLastError( RtlNtStatusToDosError(status
) );
3389 else if (status
&& status
!= STATUS_TIMEOUT
)
3391 SetLastError( RtlNtStatusToDosError(status
) );
3398 /***********************************************************************
3399 * ReadFileEx (kernelbase.@)
3401 BOOL WINAPI DECLSPEC_HOTPATCH
ReadFileEx( HANDLE file
, LPVOID buffer
, DWORD count
, LPOVERLAPPED overlapped
,
3402 LPOVERLAPPED_COMPLETION_ROUTINE completion
)
3404 PIO_STATUS_BLOCK io
;
3405 LARGE_INTEGER offset
;
3408 TRACE( "(file=%p, buffer=%p, bytes=%u, ovl=%p, ovl_fn=%p)\n",
3409 file
, buffer
, count
, overlapped
, completion
);
3413 SetLastError( ERROR_INVALID_PARAMETER
);
3416 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
3417 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
3418 io
= (PIO_STATUS_BLOCK
)overlapped
;
3419 io
->u
.Status
= STATUS_PENDING
;
3420 io
->Information
= 0;
3422 status
= NtReadFile( file
, NULL
, read_write_apc
, completion
, io
, buffer
, count
, &offset
, NULL
);
3423 if (status
== STATUS_PENDING
) return TRUE
;
3424 return set_ntstatus( status
);
3428 /***********************************************************************
3429 * ReadFileScatter (kernelbase.@)
3431 BOOL WINAPI DECLSPEC_HOTPATCH
ReadFileScatter( HANDLE file
, FILE_SEGMENT_ELEMENT
*segments
, DWORD count
,
3432 LPDWORD reserved
, LPOVERLAPPED overlapped
)
3434 PIO_STATUS_BLOCK io
;
3435 LARGE_INTEGER offset
;
3436 void *cvalue
= NULL
;
3438 TRACE( "(%p %p %u %p)\n", file
, segments
, count
, overlapped
);
3440 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
3441 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
3442 if (!((ULONG_PTR
)overlapped
->hEvent
& 1)) cvalue
= overlapped
;
3443 io
= (PIO_STATUS_BLOCK
)overlapped
;
3444 io
->u
.Status
= STATUS_PENDING
;
3445 io
->Information
= 0;
3447 return set_ntstatus( NtReadFileScatter( file
, overlapped
->hEvent
, NULL
, cvalue
, io
,
3448 segments
, count
, &offset
, NULL
));
3452 /***********************************************************************
3453 * RemoveDirectoryA (kernelbase.@)
3455 BOOL WINAPI DECLSPEC_HOTPATCH
RemoveDirectoryA( LPCSTR path
)
3459 if (!(pathW
= file_name_AtoW( path
, FALSE
))) return FALSE
;
3460 return RemoveDirectoryW( pathW
);
3464 /***********************************************************************
3465 * RemoveDirectoryW (kernelbase.@)
3467 BOOL WINAPI DECLSPEC_HOTPATCH
RemoveDirectoryW( LPCWSTR path
)
3469 OBJECT_ATTRIBUTES attr
;
3470 UNICODE_STRING nt_name
;
3475 TRACE( "%s\n", debugstr_w(path
) );
3477 status
= RtlDosPathNameToNtPathName_U_WithStatus( path
, &nt_name
, NULL
, NULL
);
3478 if (!set_ntstatus( status
)) return FALSE
;
3480 InitializeObjectAttributes( &attr
, &nt_name
, OBJ_CASE_INSENSITIVE
, 0, NULL
);
3481 status
= NtOpenFile( &handle
, DELETE
| SYNCHRONIZE
, &attr
, &io
,
3482 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
3483 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
3484 RtlFreeUnicodeString( &nt_name
);
3488 FILE_DISPOSITION_INFORMATION info
= { TRUE
};
3489 status
= NtSetInformationFile( handle
, &io
, &info
, sizeof(info
), FileDispositionInformation
);
3492 return set_ntstatus( status
);
3496 /**************************************************************************
3497 * SetEndOfFile (kernelbase.@)
3499 BOOL WINAPI DECLSPEC_HOTPATCH
SetEndOfFile( HANDLE file
)
3501 FILE_POSITION_INFORMATION pos
;
3502 FILE_END_OF_FILE_INFORMATION eof
;
3506 if (!(status
= NtQueryInformationFile( file
, &io
, &pos
, sizeof(pos
), FilePositionInformation
)))
3508 eof
.EndOfFile
= pos
.CurrentByteOffset
;
3509 status
= NtSetInformationFile( file
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
);
3511 return set_ntstatus( status
);
3515 /***********************************************************************
3516 * SetFileInformationByHandle (kernelbase.@)
3518 BOOL WINAPI DECLSPEC_HOTPATCH
SetFileInformationByHandle( HANDLE file
, FILE_INFO_BY_HANDLE_CLASS
class,
3519 void *info
, DWORD size
)
3524 TRACE( "%p %u %p %u\n", file
, class, info
, size
);
3529 case FileRenameInfo
:
3530 case FileAllocationInfo
:
3531 case FileStreamInfo
:
3532 case FileIdBothDirectoryInfo
:
3533 case FileIdBothDirectoryRestartInfo
:
3534 case FileFullDirectoryInfo
:
3535 case FileFullDirectoryRestartInfo
:
3536 case FileStorageInfo
:
3537 case FileAlignmentInfo
:
3539 case FileIdExtdDirectoryInfo
:
3540 case FileIdExtdDirectoryRestartInfo
:
3541 FIXME( "%p, %u, %p, %u\n", file
, class, info
, size
);
3542 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
3545 case FileEndOfFileInfo
:
3546 status
= NtSetInformationFile( file
, &io
, info
, size
, FileEndOfFileInformation
);
3549 status
= NtSetInformationFile( file
, &io
, info
, size
, FileBasicInformation
);
3551 case FileDispositionInfo
:
3552 status
= NtSetInformationFile( file
, &io
, info
, size
, FileDispositionInformation
);
3554 case FileIoPriorityHintInfo
:
3555 status
= NtSetInformationFile( file
, &io
, info
, size
, FileIoPriorityHintInformation
);
3557 case FileStandardInfo
:
3558 case FileCompressionInfo
:
3559 case FileAttributeTagInfo
:
3560 case FileRemoteProtocolInfo
:
3562 SetLastError( ERROR_INVALID_PARAMETER
);
3565 return set_ntstatus( status
);
3569 /***********************************************************************
3570 * SetFilePointer (kernelbase.@)
3572 DWORD WINAPI DECLSPEC_HOTPATCH
SetFilePointer( HANDLE file
, LONG distance
, LONG
*highword
, DWORD method
)
3574 LARGE_INTEGER dist
, newpos
;
3578 dist
.u
.LowPart
= distance
;
3579 dist
.u
.HighPart
= *highword
;
3581 else dist
.QuadPart
= distance
;
3583 if (!SetFilePointerEx( file
, dist
, &newpos
, method
)) return INVALID_SET_FILE_POINTER
;
3585 if (highword
) *highword
= newpos
.u
.HighPart
;
3586 if (newpos
.u
.LowPart
== INVALID_SET_FILE_POINTER
) SetLastError( 0 );
3587 return newpos
.u
.LowPart
;
3591 /***********************************************************************
3592 * SetFilePointerEx (kernelbase.@)
3594 BOOL WINAPI DECLSPEC_HOTPATCH
SetFilePointerEx( HANDLE file
, LARGE_INTEGER distance
,
3595 LARGE_INTEGER
*newpos
, DWORD method
)
3599 FILE_POSITION_INFORMATION info
;
3600 FILE_END_OF_FILE_INFORMATION eof
;
3605 pos
= distance
.QuadPart
;
3608 if (NtQueryInformationFile( file
, &io
, &info
, sizeof(info
), FilePositionInformation
))
3610 pos
= info
.CurrentByteOffset
.QuadPart
+ distance
.QuadPart
;
3613 if (NtQueryInformationFile( file
, &io
, &eof
, sizeof(eof
), FileEndOfFileInformation
))
3615 pos
= eof
.EndOfFile
.QuadPart
+ distance
.QuadPart
;
3618 SetLastError( ERROR_INVALID_PARAMETER
);
3624 SetLastError( ERROR_NEGATIVE_SEEK
);
3628 info
.CurrentByteOffset
.QuadPart
= pos
;
3629 if (!NtSetInformationFile( file
, &io
, &info
, sizeof(info
), FilePositionInformation
))
3631 if (newpos
) newpos
->QuadPart
= pos
;
3636 return set_ntstatus( io
.u
.Status
);
3640 /***********************************************************************
3641 * SetFileTime (kernelbase.@)
3643 BOOL WINAPI DECLSPEC_HOTPATCH
SetFileTime( HANDLE file
, const FILETIME
*ctime
,
3644 const FILETIME
*atime
, const FILETIME
*mtime
)
3646 FILE_BASIC_INFORMATION info
;
3649 memset( &info
, 0, sizeof(info
) );
3652 info
.CreationTime
.u
.HighPart
= ctime
->dwHighDateTime
;
3653 info
.CreationTime
.u
.LowPart
= ctime
->dwLowDateTime
;
3657 info
.LastAccessTime
.u
.HighPart
= atime
->dwHighDateTime
;
3658 info
.LastAccessTime
.u
.LowPart
= atime
->dwLowDateTime
;
3662 info
.LastWriteTime
.u
.HighPart
= mtime
->dwHighDateTime
;
3663 info
.LastWriteTime
.u
.LowPart
= mtime
->dwLowDateTime
;
3666 return set_ntstatus( NtSetInformationFile( file
, &io
, &info
, sizeof(info
), FileBasicInformation
));
3670 /***********************************************************************
3671 * SetFileValidData (kernelbase.@)
3673 BOOL WINAPI DECLSPEC_HOTPATCH
SetFileValidData( HANDLE file
, LONGLONG length
)
3675 FILE_VALID_DATA_LENGTH_INFORMATION info
;
3678 info
.ValidDataLength
.QuadPart
= length
;
3679 return set_ntstatus( NtSetInformationFile( file
, &io
, &info
, sizeof(info
),
3680 FileValidDataLengthInformation
));
3684 /**************************************************************************
3685 * UnlockFile (kernelbase.@)
3687 BOOL WINAPI DECLSPEC_HOTPATCH
UnlockFile( HANDLE file
, DWORD offset_low
, DWORD offset_high
,
3688 DWORD count_low
, DWORD count_high
)
3690 LARGE_INTEGER count
, offset
;
3692 count
.u
.LowPart
= count_low
;
3693 count
.u
.HighPart
= count_high
;
3694 offset
.u
.LowPart
= offset_low
;
3695 offset
.u
.HighPart
= offset_high
;
3696 return set_ntstatus( NtUnlockFile( file
, NULL
, &offset
, &count
, NULL
));
3700 /**************************************************************************
3701 * UnlockFileEx (kernelbase.@)
3703 BOOL WINAPI DECLSPEC_HOTPATCH
UnlockFileEx( HANDLE file
, DWORD reserved
,
3704 DWORD count_low
, DWORD count_high
, LPOVERLAPPED overlapped
)
3708 SetLastError( ERROR_INVALID_PARAMETER
);
3711 if (overlapped
->hEvent
) FIXME("Unimplemented overlapped operation\n");
3713 return UnlockFile( file
, overlapped
->u
.s
.Offset
, overlapped
->u
.s
.OffsetHigh
, count_low
, count_high
);
3717 /***********************************************************************
3718 * WriteFile (kernelbase.@)
3720 BOOL WINAPI DECLSPEC_HOTPATCH
WriteFile( HANDLE file
, LPCVOID buffer
, DWORD count
,
3721 LPDWORD result
, LPOVERLAPPED overlapped
)
3723 HANDLE event
= NULL
;
3724 LARGE_INTEGER offset
;
3725 PLARGE_INTEGER poffset
= NULL
;
3727 IO_STATUS_BLOCK iosb
;
3728 PIO_STATUS_BLOCK piosb
= &iosb
;
3729 LPVOID cvalue
= NULL
;
3731 TRACE( "%p %p %d %p %p\n", file
, buffer
, count
, result
, overlapped
);
3735 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
3736 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
3738 event
= overlapped
->hEvent
;
3739 piosb
= (PIO_STATUS_BLOCK
)overlapped
;
3740 if (((ULONG_PTR
)event
& 1) == 0) cvalue
= overlapped
;
3742 else piosb
->Information
= 0;
3743 piosb
->u
.Status
= STATUS_PENDING
;
3745 status
= NtWriteFile( file
, event
, NULL
, cvalue
, piosb
, buffer
, count
, poffset
, NULL
);
3747 if (status
== STATUS_PENDING
&& !overlapped
)
3749 WaitForSingleObject( file
, INFINITE
);
3750 status
= piosb
->u
.Status
;
3753 if (result
) *result
= overlapped
&& status
? 0 : piosb
->Information
;
3755 if (status
&& status
!= STATUS_TIMEOUT
)
3757 SetLastError( RtlNtStatusToDosError(status
) );
3764 /***********************************************************************
3765 * WriteFileEx (kernelbase.@)
3767 BOOL WINAPI DECLSPEC_HOTPATCH
WriteFileEx( HANDLE file
, LPCVOID buffer
,
3768 DWORD count
, LPOVERLAPPED overlapped
,
3769 LPOVERLAPPED_COMPLETION_ROUTINE completion
)
3771 LARGE_INTEGER offset
;
3773 PIO_STATUS_BLOCK io
;
3775 TRACE( "%p %p %d %p %p\n", file
, buffer
, count
, overlapped
, completion
);
3779 SetLastError( ERROR_INVALID_PARAMETER
);
3782 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
3783 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
3785 io
= (PIO_STATUS_BLOCK
)overlapped
;
3786 io
->u
.Status
= STATUS_PENDING
;
3787 io
->Information
= 0;
3789 status
= NtWriteFile( file
, NULL
, read_write_apc
, completion
, io
, buffer
, count
, &offset
, NULL
);
3790 if (status
== STATUS_PENDING
) return TRUE
;
3791 return set_ntstatus( status
);
3795 /***********************************************************************
3796 * WriteFileGather (kernelbase.@)
3798 BOOL WINAPI DECLSPEC_HOTPATCH
WriteFileGather( HANDLE file
, FILE_SEGMENT_ELEMENT
*segments
, DWORD count
,
3799 LPDWORD reserved
, LPOVERLAPPED overlapped
)
3801 PIO_STATUS_BLOCK io
;
3802 LARGE_INTEGER offset
;
3803 void *cvalue
= NULL
;
3805 TRACE( "%p %p %u %p\n", file
, segments
, count
, overlapped
);
3807 offset
.u
.LowPart
= overlapped
->u
.s
.Offset
;
3808 offset
.u
.HighPart
= overlapped
->u
.s
.OffsetHigh
;
3809 if (!((ULONG_PTR
)overlapped
->hEvent
& 1)) cvalue
= overlapped
;
3810 io
= (PIO_STATUS_BLOCK
)overlapped
;
3811 io
->u
.Status
= STATUS_PENDING
;
3812 io
->Information
= 0;
3814 return set_ntstatus( NtWriteFileGather( file
, overlapped
->hEvent
, NULL
, cvalue
,
3815 io
, segments
, count
, &offset
, NULL
));
3819 /***********************************************************************
3820 * Operations on file times
3821 ***********************************************************************/
3824 /*********************************************************************
3825 * CompareFileTime (kernelbase.@)
3827 INT WINAPI DECLSPEC_HOTPATCH
CompareFileTime( const FILETIME
*x
, const FILETIME
*y
)
3829 if (!x
|| !y
) return -1;
3830 if (x
->dwHighDateTime
> y
->dwHighDateTime
) return 1;
3831 if (x
->dwHighDateTime
< y
->dwHighDateTime
) return -1;
3832 if (x
->dwLowDateTime
> y
->dwLowDateTime
) return 1;
3833 if (x
->dwLowDateTime
< y
->dwLowDateTime
) return -1;
3838 /*********************************************************************
3839 * FileTimeToLocalFileTime (kernelbase.@)
3841 BOOL WINAPI DECLSPEC_HOTPATCH
FileTimeToLocalFileTime( const FILETIME
*utc
, FILETIME
*local
)
3843 return set_ntstatus( RtlSystemTimeToLocalTime( (const LARGE_INTEGER
*)utc
, (LARGE_INTEGER
*)local
));
3847 /*********************************************************************
3848 * FileTimeToSystemTime (kernelbase.@)
3850 BOOL WINAPI DECLSPEC_HOTPATCH
FileTimeToSystemTime( const FILETIME
*ft
, SYSTEMTIME
*systime
)
3854 RtlTimeToTimeFields( (const LARGE_INTEGER
*)ft
, &tf
);
3855 systime
->wYear
= tf
.Year
;
3856 systime
->wMonth
= tf
.Month
;
3857 systime
->wDay
= tf
.Day
;
3858 systime
->wHour
= tf
.Hour
;
3859 systime
->wMinute
= tf
.Minute
;
3860 systime
->wSecond
= tf
.Second
;
3861 systime
->wMilliseconds
= tf
.Milliseconds
;
3862 systime
->wDayOfWeek
= tf
.Weekday
;
3867 /*********************************************************************
3868 * GetLocalTime (kernelbase.@)
3870 void WINAPI DECLSPEC_HOTPATCH
GetLocalTime( SYSTEMTIME
*systime
)
3872 LARGE_INTEGER ft
, ft2
;
3874 NtQuerySystemTime( &ft
);
3875 RtlSystemTimeToLocalTime( &ft
, &ft2
);
3876 FileTimeToSystemTime( (FILETIME
*)&ft2
, systime
);
3880 /*********************************************************************
3881 * GetSystemTime (kernelbase.@)
3883 void WINAPI DECLSPEC_HOTPATCH
GetSystemTime( SYSTEMTIME
*systime
)
3887 NtQuerySystemTime( &ft
);
3888 FileTimeToSystemTime( (FILETIME
*)&ft
, systime
);
3892 /***********************************************************************
3893 * GetSystemTimeAdjustment (kernelbase.@)
3895 BOOL WINAPI DECLSPEC_HOTPATCH
GetSystemTimeAdjustment( DWORD
*adjust
, DWORD
*increment
, BOOL
*disabled
)
3897 SYSTEM_TIME_ADJUSTMENT_QUERY st
;
3900 if (!set_ntstatus( NtQuerySystemInformation( SystemTimeAdjustmentInformation
, &st
, sizeof(st
), &len
)))
3902 *adjust
= st
.TimeAdjustment
;
3903 *increment
= st
.TimeIncrement
;
3904 *disabled
= st
.TimeAdjustmentDisabled
;
3909 /***********************************************************************
3910 * GetSystemTimeAsFileTime (kernelbase.@)
3912 void WINAPI DECLSPEC_HOTPATCH
GetSystemTimeAsFileTime( FILETIME
*time
)
3914 NtQuerySystemTime( (LARGE_INTEGER
*)time
);
3918 /***********************************************************************
3919 * GetSystemTimePreciseAsFileTime (kernelbase.@)
3921 void WINAPI DECLSPEC_HOTPATCH
GetSystemTimePreciseAsFileTime( FILETIME
*time
)
3925 t
.QuadPart
= RtlGetSystemTimePrecise();
3926 time
->dwLowDateTime
= t
.u
.LowPart
;
3927 time
->dwHighDateTime
= t
.u
.HighPart
;
3931 /*********************************************************************
3932 * LocalFileTimeToFileTime (kernelbase.@)
3934 BOOL WINAPI DECLSPEC_HOTPATCH
LocalFileTimeToFileTime( const FILETIME
*local
, FILETIME
*utc
)
3936 return set_ntstatus( RtlLocalTimeToSystemTime( (const LARGE_INTEGER
*)local
, (LARGE_INTEGER
*)utc
));
3940 /***********************************************************************
3941 * SetLocalTime (kernelbase.@)
3943 BOOL WINAPI DECLSPEC_HOTPATCH
SetLocalTime( const SYSTEMTIME
*systime
)
3948 if (!SystemTimeToFileTime( systime
, &ft
)) return FALSE
;
3949 RtlLocalTimeToSystemTime( (LARGE_INTEGER
*)&ft
, &st
);
3950 return set_ntstatus( NtSetSystemTime( &st
, NULL
));
3954 /***********************************************************************
3955 * SetSystemTime (kernelbase.@)
3957 BOOL WINAPI DECLSPEC_HOTPATCH
SetSystemTime( const SYSTEMTIME
*systime
)
3961 if (!SystemTimeToFileTime( systime
, &ft
)) return FALSE
;
3962 return set_ntstatus( NtSetSystemTime( (LARGE_INTEGER
*)&ft
, NULL
));
3966 /***********************************************************************
3967 * SetSystemTimeAdjustment (kernelbase.@)
3969 BOOL WINAPI DECLSPEC_HOTPATCH
SetSystemTimeAdjustment( DWORD adjust
, BOOL disabled
)
3971 SYSTEM_TIME_ADJUSTMENT st
;
3973 st
.TimeAdjustment
= adjust
;
3974 st
.TimeAdjustmentDisabled
= disabled
;
3975 return set_ntstatus( NtSetSystemInformation( SystemTimeAdjustmentInformation
, &st
, sizeof(st
) ));
3979 /*********************************************************************
3980 * SystemTimeToFileTime (kernelbase.@)
3982 BOOL WINAPI DECLSPEC_HOTPATCH
SystemTimeToFileTime( const SYSTEMTIME
*systime
, FILETIME
*ft
)
3986 tf
.Year
= systime
->wYear
;
3987 tf
.Month
= systime
->wMonth
;
3988 tf
.Day
= systime
->wDay
;
3989 tf
.Hour
= systime
->wHour
;
3990 tf
.Minute
= systime
->wMinute
;
3991 tf
.Second
= systime
->wSecond
;
3992 tf
.Milliseconds
= systime
->wMilliseconds
;
3993 if (RtlTimeFieldsToTime( &tf
, (LARGE_INTEGER
*)ft
)) return TRUE
;
3994 SetLastError( ERROR_INVALID_PARAMETER
);
3999 /***********************************************************************
4001 ***********************************************************************/
4004 static void dump_dcb( const DCB
*dcb
)
4006 TRACE( "size=%d rate=%d fParity=%d Parity=%d stopbits=%d %sIXON %sIXOFF CTS=%d RTS=%d DSR=%d DTR=%d %sCRTSCTS\n",
4007 dcb
->ByteSize
, dcb
->BaudRate
, dcb
->fParity
, dcb
->Parity
,
4008 (dcb
->StopBits
== ONESTOPBIT
) ? 1 : (dcb
->StopBits
== TWOSTOPBITS
) ? 2 : 0,
4009 dcb
->fOutX
? "" : "~", dcb
->fInX
? "" : "~",
4010 dcb
->fOutxCtsFlow
, dcb
->fRtsControl
, dcb
->fOutxDsrFlow
, dcb
->fDtrControl
,
4011 (dcb
->fOutxCtsFlow
|| dcb
->fRtsControl
== RTS_CONTROL_HANDSHAKE
) ? "" : "~" );
4014 /*****************************************************************************
4015 * ClearCommBreak (kernelbase.@)
4017 BOOL WINAPI DECLSPEC_HOTPATCH
ClearCommBreak( HANDLE handle
)
4019 return EscapeCommFunction( handle
, CLRBREAK
);
4023 /*****************************************************************************
4024 * ClearCommError (kernelbase.@)
4026 BOOL WINAPI DECLSPEC_HOTPATCH
ClearCommError( HANDLE handle
, DWORD
*errors
, COMSTAT
*stat
)
4030 if (!DeviceIoControl( handle
, IOCTL_SERIAL_GET_COMMSTATUS
, NULL
, 0, &ss
, sizeof(ss
), NULL
, NULL
))
4033 TRACE( "status %#x,%#x, in %u, out %u, eof %d, wait %d\n", ss
.Errors
, ss
.HoldReasons
,
4034 ss
.AmountInInQueue
, ss
.AmountInOutQueue
, ss
.EofReceived
, ss
.WaitForImmediate
);
4039 if (ss
.Errors
& SERIAL_ERROR_BREAK
) *errors
|= CE_BREAK
;
4040 if (ss
.Errors
& SERIAL_ERROR_FRAMING
) *errors
|= CE_FRAME
;
4041 if (ss
.Errors
& SERIAL_ERROR_OVERRUN
) *errors
|= CE_OVERRUN
;
4042 if (ss
.Errors
& SERIAL_ERROR_QUEUEOVERRUN
) *errors
|= CE_RXOVER
;
4043 if (ss
.Errors
& SERIAL_ERROR_PARITY
) *errors
|= CE_RXPARITY
;
4047 stat
->fCtsHold
= !!(ss
.HoldReasons
& SERIAL_TX_WAITING_FOR_CTS
);
4048 stat
->fDsrHold
= !!(ss
.HoldReasons
& SERIAL_TX_WAITING_FOR_DSR
);
4049 stat
->fRlsdHold
= !!(ss
.HoldReasons
& SERIAL_TX_WAITING_FOR_DCD
);
4050 stat
->fXoffHold
= !!(ss
.HoldReasons
& SERIAL_TX_WAITING_FOR_XON
);
4051 stat
->fXoffSent
= !!(ss
.HoldReasons
& SERIAL_TX_WAITING_XOFF_SENT
);
4052 stat
->fEof
= !!ss
.EofReceived
;
4053 stat
->fTxim
= !!ss
.WaitForImmediate
;
4054 stat
->cbInQue
= ss
.AmountInInQueue
;
4055 stat
->cbOutQue
= ss
.AmountInOutQueue
;
4061 /****************************************************************************
4062 * DeviceIoControl (kernelbase.@)
4064 BOOL WINAPI DECLSPEC_HOTPATCH
DeviceIoControl( HANDLE handle
, DWORD code
, void *in_buff
, DWORD in_count
,
4065 void *out_buff
, DWORD out_count
, DWORD
*returned
,
4066 OVERLAPPED
*overlapped
)
4068 IO_STATUS_BLOCK iosb
, *piosb
= &iosb
;
4069 void *cvalue
= NULL
;
4073 TRACE( "(%p,%x,%p,%d,%p,%d,%p,%p)\n",
4074 handle
, code
, in_buff
, in_count
, out_buff
, out_count
, returned
, overlapped
);
4078 piosb
= (IO_STATUS_BLOCK
*)overlapped
;
4079 if (!((ULONG_PTR
)overlapped
->hEvent
& 1)) cvalue
= overlapped
;
4080 event
= overlapped
->hEvent
;
4081 overlapped
->Internal
= STATUS_PENDING
;
4082 overlapped
->InternalHigh
= 0;
4085 if (HIWORD(code
) == FILE_DEVICE_FILE_SYSTEM
)
4086 status
= NtFsControlFile( handle
, event
, NULL
, cvalue
, piosb
, code
,
4087 in_buff
, in_count
, out_buff
, out_count
);
4089 status
= NtDeviceIoControlFile( handle
, event
, NULL
, cvalue
, piosb
, code
,
4090 in_buff
, in_count
, out_buff
, out_count
);
4092 if (returned
) *returned
= piosb
->Information
;
4093 return set_ntstatus( status
);
4097 /*****************************************************************************
4098 * EscapeCommFunction (kernelbase.@)
4100 BOOL WINAPI DECLSPEC_HOTPATCH
EscapeCommFunction( HANDLE handle
, DWORD func
)
4102 static const DWORD ioctls
[] =
4105 IOCTL_SERIAL_SET_XOFF
, /* SETXOFF */
4106 IOCTL_SERIAL_SET_XON
, /* SETXON */
4107 IOCTL_SERIAL_SET_RTS
, /* SETRTS */
4108 IOCTL_SERIAL_CLR_RTS
, /* CLRRTS */
4109 IOCTL_SERIAL_SET_DTR
, /* SETDTR */
4110 IOCTL_SERIAL_CLR_DTR
, /* CLRDTR */
4111 IOCTL_SERIAL_RESET_DEVICE
, /* RESETDEV */
4112 IOCTL_SERIAL_SET_BREAK_ON
, /* SETBREAK */
4113 IOCTL_SERIAL_SET_BREAK_OFF
/* CLRBREAK */
4116 if (func
>= ARRAY_SIZE(ioctls
) || !ioctls
[func
])
4118 SetLastError( ERROR_INVALID_PARAMETER
);
4121 return DeviceIoControl( handle
, ioctls
[func
], NULL
, 0, NULL
, 0, NULL
, NULL
);
4125 /***********************************************************************
4126 * GetCommConfig (kernelbase.@)
4128 BOOL WINAPI DECLSPEC_HOTPATCH
GetCommConfig( HANDLE handle
, COMMCONFIG
*config
, DWORD
*size
)
4130 if (!config
) return FALSE
;
4132 TRACE( "(%p, %p, %p %u)\n", handle
, config
, size
, *size
);
4134 if (*size
< sizeof(COMMCONFIG
))
4136 *size
= sizeof(COMMCONFIG
);
4139 *size
= sizeof(COMMCONFIG
);
4140 config
->dwSize
= sizeof(COMMCONFIG
);
4141 config
->wVersion
= 1;
4142 config
->wReserved
= 0;
4143 config
->dwProviderSubType
= PST_RS232
;
4144 config
->dwProviderOffset
= 0;
4145 config
->dwProviderSize
= 0;
4146 return GetCommState( handle
, &config
->dcb
);
4150 /*****************************************************************************
4151 * GetCommMask (kernelbase.@)
4153 BOOL WINAPI DECLSPEC_HOTPATCH
GetCommMask( HANDLE handle
, DWORD
*mask
)
4155 return DeviceIoControl( handle
, IOCTL_SERIAL_GET_WAIT_MASK
, NULL
, 0, mask
, sizeof(*mask
),
4160 /***********************************************************************
4161 * GetCommModemStatus (kernelbase.@)
4163 BOOL WINAPI DECLSPEC_HOTPATCH
GetCommModemStatus( HANDLE handle
, DWORD
*status
)
4165 return DeviceIoControl( handle
, IOCTL_SERIAL_GET_MODEMSTATUS
, NULL
, 0, status
, sizeof(*status
),
4170 /***********************************************************************
4171 * GetCommProperties (kernelbase.@)
4173 BOOL WINAPI DECLSPEC_HOTPATCH
GetCommProperties( HANDLE handle
, COMMPROP
*prop
)
4175 return DeviceIoControl( handle
, IOCTL_SERIAL_GET_PROPERTIES
, NULL
, 0, prop
, sizeof(*prop
), NULL
, NULL
);
4179 /*****************************************************************************
4180 * GetCommState (kernelbase.@)
4182 BOOL WINAPI DECLSPEC_HOTPATCH
GetCommState( HANDLE handle
, DCB
*dcb
)
4184 SERIAL_BAUD_RATE sbr
;
4185 SERIAL_LINE_CONTROL slc
;
4186 SERIAL_HANDFLOW shf
;
4191 SetLastError( ERROR_INVALID_PARAMETER
);
4194 if (!DeviceIoControl(handle
, IOCTL_SERIAL_GET_BAUD_RATE
, NULL
, 0, &sbr
, sizeof(sbr
), NULL
, NULL
) ||
4195 !DeviceIoControl(handle
, IOCTL_SERIAL_GET_LINE_CONTROL
, NULL
, 0, &slc
, sizeof(slc
), NULL
, NULL
) ||
4196 !DeviceIoControl(handle
, IOCTL_SERIAL_GET_HANDFLOW
, NULL
, 0, &shf
, sizeof(shf
), NULL
, NULL
) ||
4197 !DeviceIoControl(handle
, IOCTL_SERIAL_GET_CHARS
, NULL
, 0, &sc
, sizeof(sc
), NULL
, NULL
))
4200 dcb
->DCBlength
= sizeof(*dcb
);
4201 dcb
->BaudRate
= sbr
.BaudRate
;
4202 /* yes, they seem no never be (re)set on NT */
4205 dcb
->fOutxCtsFlow
= !!(shf
.ControlHandShake
& SERIAL_CTS_HANDSHAKE
);
4206 dcb
->fOutxDsrFlow
= !!(shf
.ControlHandShake
& SERIAL_DSR_HANDSHAKE
);
4207 dcb
->fDsrSensitivity
= !!(shf
.ControlHandShake
& SERIAL_DSR_SENSITIVITY
);
4208 dcb
->fTXContinueOnXoff
= !!(shf
.FlowReplace
& SERIAL_XOFF_CONTINUE
);
4209 dcb
->fOutX
= !!(shf
.FlowReplace
& SERIAL_AUTO_TRANSMIT
);
4210 dcb
->fInX
= !!(shf
.FlowReplace
& SERIAL_AUTO_RECEIVE
);
4211 dcb
->fErrorChar
= !!(shf
.FlowReplace
& SERIAL_ERROR_CHAR
);
4212 dcb
->fNull
= !!(shf
.FlowReplace
& SERIAL_NULL_STRIPPING
);
4213 dcb
->fAbortOnError
= !!(shf
.ControlHandShake
& SERIAL_ERROR_ABORT
);
4214 dcb
->XonLim
= shf
.XonLimit
;
4215 dcb
->XoffLim
= shf
.XoffLimit
;
4216 dcb
->ByteSize
= slc
.WordLength
;
4217 dcb
->Parity
= slc
.Parity
;
4218 dcb
->StopBits
= slc
.StopBits
;
4219 dcb
->XonChar
= sc
.XonChar
;
4220 dcb
->XoffChar
= sc
.XoffChar
;
4221 dcb
->ErrorChar
= sc
.ErrorChar
;
4222 dcb
->EofChar
= sc
.EofChar
;
4223 dcb
->EvtChar
= sc
.EventChar
;
4225 switch (shf
.ControlHandShake
& (SERIAL_DTR_CONTROL
| SERIAL_DTR_HANDSHAKE
))
4227 case SERIAL_DTR_CONTROL
: dcb
->fDtrControl
= DTR_CONTROL_ENABLE
; break;
4228 case SERIAL_DTR_HANDSHAKE
: dcb
->fDtrControl
= DTR_CONTROL_HANDSHAKE
; break;
4229 default: dcb
->fDtrControl
= DTR_CONTROL_DISABLE
; break;
4231 switch (shf
.FlowReplace
& (SERIAL_RTS_CONTROL
| SERIAL_RTS_HANDSHAKE
))
4233 case SERIAL_RTS_CONTROL
: dcb
->fRtsControl
= RTS_CONTROL_ENABLE
; break;
4234 case SERIAL_RTS_HANDSHAKE
: dcb
->fRtsControl
= RTS_CONTROL_HANDSHAKE
; break;
4235 case SERIAL_RTS_CONTROL
| SERIAL_RTS_HANDSHAKE
:
4236 dcb
->fRtsControl
= RTS_CONTROL_TOGGLE
; break;
4237 default: dcb
->fRtsControl
= RTS_CONTROL_DISABLE
; break;
4244 /*****************************************************************************
4245 * GetCommTimeouts (kernelbase.@)
4247 BOOL WINAPI DECLSPEC_HOTPATCH
GetCommTimeouts( HANDLE handle
, COMMTIMEOUTS
*timeouts
)
4251 SetLastError( ERROR_INVALID_PARAMETER
);
4254 return DeviceIoControl( handle
, IOCTL_SERIAL_GET_TIMEOUTS
, NULL
, 0, timeouts
, sizeof(*timeouts
),
4258 /********************************************************************
4259 * PurgeComm (kernelbase.@)
4261 BOOL WINAPI DECLSPEC_HOTPATCH
PurgeComm(HANDLE handle
, DWORD flags
)
4263 return DeviceIoControl( handle
, IOCTL_SERIAL_PURGE
, &flags
, sizeof(flags
),
4264 NULL
, 0, NULL
, NULL
);
4268 /*****************************************************************************
4269 * SetCommBreak (kernelbase.@)
4271 BOOL WINAPI DECLSPEC_HOTPATCH
SetCommBreak( HANDLE handle
)
4273 return EscapeCommFunction( handle
, SETBREAK
);
4277 /***********************************************************************
4278 * SetCommConfig (kernelbase.@)
4280 BOOL WINAPI DECLSPEC_HOTPATCH
SetCommConfig( HANDLE handle
, COMMCONFIG
*config
, DWORD size
)
4282 TRACE( "(%p, %p, %u)\n", handle
, config
, size
);
4283 return SetCommState( handle
, &config
->dcb
);
4287 /*****************************************************************************
4288 * SetCommMask (kernelbase.@)
4290 BOOL WINAPI DECLSPEC_HOTPATCH
SetCommMask( HANDLE handle
, DWORD mask
)
4292 return DeviceIoControl( handle
, IOCTL_SERIAL_SET_WAIT_MASK
, &mask
, sizeof(mask
),
4293 NULL
, 0, NULL
, NULL
);
4297 /*****************************************************************************
4298 * SetCommState (kernelbase.@)
4300 BOOL WINAPI DECLSPEC_HOTPATCH
SetCommState( HANDLE handle
, DCB
*dcb
)
4302 SERIAL_BAUD_RATE sbr
;
4303 SERIAL_LINE_CONTROL slc
;
4304 SERIAL_HANDFLOW shf
;
4309 SetLastError( ERROR_INVALID_PARAMETER
);
4314 sbr
.BaudRate
= dcb
->BaudRate
;
4315 slc
.StopBits
= dcb
->StopBits
;
4316 slc
.Parity
= dcb
->Parity
;
4317 slc
.WordLength
= dcb
->ByteSize
;
4318 shf
.ControlHandShake
= 0;
4319 shf
.FlowReplace
= 0;
4320 if (dcb
->fOutxCtsFlow
) shf
.ControlHandShake
|= SERIAL_CTS_HANDSHAKE
;
4321 if (dcb
->fOutxDsrFlow
) shf
.ControlHandShake
|= SERIAL_DSR_HANDSHAKE
;
4322 switch (dcb
->fDtrControl
)
4324 case DTR_CONTROL_DISABLE
: break;
4325 case DTR_CONTROL_ENABLE
: shf
.ControlHandShake
|= SERIAL_DTR_CONTROL
; break;
4326 case DTR_CONTROL_HANDSHAKE
: shf
.ControlHandShake
|= SERIAL_DTR_HANDSHAKE
; break;
4328 SetLastError( ERROR_INVALID_PARAMETER
);
4331 switch (dcb
->fRtsControl
)
4333 case RTS_CONTROL_DISABLE
: break;
4334 case RTS_CONTROL_ENABLE
: shf
.FlowReplace
|= SERIAL_RTS_CONTROL
; break;
4335 case RTS_CONTROL_HANDSHAKE
: shf
.FlowReplace
|= SERIAL_RTS_HANDSHAKE
; break;
4336 case RTS_CONTROL_TOGGLE
: shf
.FlowReplace
|= SERIAL_RTS_CONTROL
| SERIAL_RTS_HANDSHAKE
; break;
4338 SetLastError( ERROR_INVALID_PARAMETER
);
4341 if (dcb
->fDsrSensitivity
) shf
.ControlHandShake
|= SERIAL_DSR_SENSITIVITY
;
4342 if (dcb
->fAbortOnError
) shf
.ControlHandShake
|= SERIAL_ERROR_ABORT
;
4343 if (dcb
->fErrorChar
) shf
.FlowReplace
|= SERIAL_ERROR_CHAR
;
4344 if (dcb
->fNull
) shf
.FlowReplace
|= SERIAL_NULL_STRIPPING
;
4345 if (dcb
->fTXContinueOnXoff
) shf
.FlowReplace
|= SERIAL_XOFF_CONTINUE
;
4346 if (dcb
->fOutX
) shf
.FlowReplace
|= SERIAL_AUTO_TRANSMIT
;
4347 if (dcb
->fInX
) shf
.FlowReplace
|= SERIAL_AUTO_RECEIVE
;
4348 shf
.XonLimit
= dcb
->XonLim
;
4349 shf
.XoffLimit
= dcb
->XoffLim
;
4350 sc
.EofChar
= dcb
->EofChar
;
4351 sc
.ErrorChar
= dcb
->ErrorChar
;
4353 sc
.EventChar
= dcb
->EvtChar
;
4354 sc
.XonChar
= dcb
->XonChar
;
4355 sc
.XoffChar
= dcb
->XoffChar
;
4357 /* note: change DTR/RTS lines after setting the comm attributes,
4358 * so flow control does not interfere.
4360 return (DeviceIoControl( handle
, IOCTL_SERIAL_SET_BAUD_RATE
, &sbr
, sizeof(sbr
), NULL
, 0, NULL
, NULL
) &&
4361 DeviceIoControl( handle
, IOCTL_SERIAL_SET_LINE_CONTROL
, &slc
, sizeof(slc
), NULL
, 0, NULL
, NULL
) &&
4362 DeviceIoControl( handle
, IOCTL_SERIAL_SET_HANDFLOW
, &shf
, sizeof(shf
), NULL
, 0, NULL
, NULL
) &&
4363 DeviceIoControl( handle
, IOCTL_SERIAL_SET_CHARS
, &sc
, sizeof(sc
), NULL
, 0, NULL
, NULL
));
4367 /*****************************************************************************
4368 * SetCommTimeouts (kernelbase.@)
4370 BOOL WINAPI DECLSPEC_HOTPATCH
SetCommTimeouts( HANDLE handle
, COMMTIMEOUTS
*timeouts
)
4374 SetLastError( ERROR_INVALID_PARAMETER
);
4377 return DeviceIoControl( handle
, IOCTL_SERIAL_SET_TIMEOUTS
, timeouts
, sizeof(*timeouts
),
4378 NULL
, 0, NULL
, NULL
);
4382 /*****************************************************************************
4383 * SetupComm (kernelbase.@)
4385 BOOL WINAPI DECLSPEC_HOTPATCH
SetupComm( HANDLE handle
, DWORD in_size
, DWORD out_size
)
4387 SERIAL_QUEUE_SIZE sqs
;
4389 sqs
.InSize
= in_size
;
4390 sqs
.OutSize
= out_size
;
4391 return DeviceIoControl( handle
, IOCTL_SERIAL_SET_QUEUE_SIZE
, &sqs
, sizeof(sqs
), NULL
, 0, NULL
, NULL
);
4395 /*****************************************************************************
4396 * TransmitCommChar (kernelbase.@)
4398 BOOL WINAPI DECLSPEC_HOTPATCH
TransmitCommChar( HANDLE handle
, CHAR ch
)
4400 return DeviceIoControl( handle
, IOCTL_SERIAL_IMMEDIATE_CHAR
, &ch
, sizeof(ch
), NULL
, 0, NULL
, NULL
);
4404 /***********************************************************************
4405 * WaitCommEvent (kernelbase.@)
4407 BOOL WINAPI DECLSPEC_HOTPATCH
WaitCommEvent( HANDLE handle
, DWORD
*events
, OVERLAPPED
*overlapped
)
4409 return DeviceIoControl( handle
, IOCTL_SERIAL_WAIT_ON_MASK
, NULL
, 0, events
, sizeof(*events
),