3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/file/create.c
6 * PURPOSE: Directory functions
7 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
8 * GetTempFileName is modified from WINE [ Alexandre Juiliard ]
11 * Removed use of SearchPath (not used by Windows)
12 * 18/08/2002: CreateFileW mess cleaned up (KJK::Hyperion)
13 * 24/08/2002: removed superfluous DPRINTs (KJK::Hyperion)
16 /* INCLUDES *****************************************************************/
21 #include <kernel32/kernel32.h>
24 /* FUNCTIONS ****************************************************************/
26 HANDLE STDCALL CreateFileA (LPCSTR lpFileName,
27 DWORD dwDesiredAccess,
29 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
30 DWORD dwCreationDisposition,
31 DWORD dwFlagsAndAttributes,
34 UNICODE_STRING FileNameU;
38 DPRINT("CreateFileA(lpFileName %s)\n",lpFileName);
40 RtlInitAnsiString (&FileName,
43 /* convert ansi (or oem) string to unicode */
45 RtlAnsiStringToUnicodeString (&FileNameU,
49 RtlOemStringToUnicodeString (&FileNameU,
53 FileHandle = CreateFileW (FileNameU.Buffer,
57 dwCreationDisposition,
61 RtlFreeHeap (RtlGetProcessHeap (),
69 HANDLE STDCALL CreateFileW (LPCWSTR lpFileName,
70 DWORD dwDesiredAccess,
72 LPSECURITY_ATTRIBUTES lpSecurityAttributes,
73 DWORD dwCreationDisposition,
74 DWORD dwFlagsAndAttributes,
77 OBJECT_ATTRIBUTES ObjectAttributes;
78 IO_STATUS_BLOCK IoStatusBlock;
79 UNICODE_STRING NtPathU;
84 DPRINT("CreateFileW(lpFileName %S)\n",lpFileName);
86 if(hTemplateFile != NULL)
89 DPRINT("Template file feature not supported yet\n");
90 SetLastError(ERROR_NOT_SUPPORTED);
91 return INVALID_HANDLE_VALUE;
94 /* validate & translate the creation disposition */
95 switch (dwCreationDisposition)
98 dwCreationDisposition = FILE_CREATE;
102 dwCreationDisposition = FILE_OVERWRITE_IF;
106 dwCreationDisposition = FILE_OPEN;
110 dwCreationDisposition = FILE_OPEN_IF;
113 case TRUNCATE_EXISTING:
114 dwCreationDisposition = FILE_OVERWRITE;
118 SetLastError(ERROR_INVALID_PARAMETER);
119 return (INVALID_HANDLE_VALUE);
122 /* validate & translate the flags */
123 if (dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED)
125 DPRINT("Overlapped I/O not supported\n");
126 SetLastError(ERROR_NOT_SUPPORTED);
127 return INVALID_HANDLE_VALUE;
130 Flags |= FILE_SYNCHRONOUS_IO_ALERT;
132 /* validate & translate the filename */
133 if (!RtlDosPathNameToNtPathName_U ((LPWSTR)lpFileName,
138 DPRINT("Invalid path\n");
139 SetLastError(ERROR_BAD_PATHNAME);
140 return INVALID_HANDLE_VALUE;
143 DPRINT("NtPathU \'%S\'\n", NtPathU.Buffer);
145 /* translate the flags that need no validation */
146 if(dwFlagsAndAttributes & FILE_FLAG_WRITE_THROUGH)
147 Flags |= FILE_WRITE_THROUGH;
149 if(dwFlagsAndAttributes & FILE_FLAG_NO_BUFFERING)
150 Flags |= FILE_NO_INTERMEDIATE_BUFFERING;
152 if(dwFlagsAndAttributes & FILE_FLAG_RANDOM_ACCESS)
153 Flags |= FILE_RANDOM_ACCESS;
155 if(dwFlagsAndAttributes & FILE_FLAG_SEQUENTIAL_SCAN)
156 Flags |= FILE_SEQUENTIAL_ONLY;
158 if(dwFlagsAndAttributes & FILE_FLAG_DELETE_ON_CLOSE)
159 Flags |= FILE_DELETE_ON_CLOSE;
161 if(dwFlagsAndAttributes & FILE_FLAG_BACKUP_SEMANTICS)
163 if(dwDesiredAccess & GENERIC_ALL)
164 Flags |= FILE_OPEN_FOR_BACKUP_INTENT | FILE_OPEN_FOR_RECOVERY;
167 if(dwDesiredAccess & GENERIC_READ)
168 Flags |= FILE_OPEN_FOR_BACKUP_INTENT;
170 if(dwDesiredAccess & GENERIC_WRITE)
171 Flags |= FILE_OPEN_FOR_RECOVERY;
175 Flags |= FILE_NON_DIRECTORY_FILE;
177 /* FILE_FLAG_POSIX_SEMANTICS is handled later */
180 /* FIXME: Win32 constants to be defined */
181 if(dwFlagsAndAttributes & FILE_FLAG_OPEN_REPARSE_POINT)
182 Flags |= FILE_OPEN_REPARSE_POINT;
184 if(dwFlagsAndAttributes & FILE_FLAG_OPEN_NO_RECALL)
185 Flags |= FILE_OPEN_NO_RECALL;
188 /* translate the desired access */
189 if (dwDesiredAccess & GENERIC_ALL)
190 dwDesiredAccess |= FILE_ALL_ACCESS;
193 if (dwDesiredAccess & GENERIC_READ)
194 dwDesiredAccess |= FILE_GENERIC_READ;
196 if (dwDesiredAccess & GENERIC_WRITE)
197 dwDesiredAccess |= FILE_GENERIC_WRITE;
199 if (dwDesiredAccess & GENERIC_EXECUTE)
200 dwDesiredAccess |= FILE_GENERIC_EXECUTE;
203 /* build the object attributes */
204 InitializeObjectAttributes(
212 if (lpSecurityAttributes)
214 if(lpSecurityAttributes->bInheritHandle)
215 ObjectAttributes.Attributes |= OBJ_INHERIT;
217 ObjectAttributes.SecurityDescriptor = lpSecurityAttributes->lpSecurityDescriptor;
220 if(!(dwFlagsAndAttributes & FILE_FLAG_POSIX_SEMANTICS))
221 ObjectAttributes.Attributes |= OBJ_CASE_INSENSITIVE;
223 /* perform the call */
224 Status = NtCreateFile (&FileHandle,
229 dwFlagsAndAttributes,
231 dwCreationDisposition,
236 RtlFreeUnicodeString(&NtPathU);
239 if (!NT_SUCCESS(Status))
241 SetLastErrorByStatus (Status);
242 return INVALID_HANDLE_VALUE;
245 switch(IoStatusBlock.Information)
249 SetLastError(ERROR_ALREADY_EXISTS);