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 "../include/debug.h"
24 /* FUNCTIONS ****************************************************************/
29 HANDLE STDCALL
CreateFileA (LPCSTR lpFileName
,
30 DWORD dwDesiredAccess
,
32 LPSECURITY_ATTRIBUTES lpSecurityAttributes
,
33 DWORD dwCreationDisposition
,
34 DWORD dwFlagsAndAttributes
,
40 DPRINT("CreateFileA(lpFileName %s)\n",lpFileName
);
42 if (!(FileNameW
= FilenameA2W(lpFileName
, FALSE
)))
43 return INVALID_HANDLE_VALUE
;
45 FileHandle
= CreateFileW (FileNameW
,
49 dwCreationDisposition
,
60 HANDLE STDCALL
CreateFileW (LPCWSTR lpFileName
,
61 DWORD dwDesiredAccess
,
63 LPSECURITY_ATTRIBUTES lpSecurityAttributes
,
64 DWORD dwCreationDisposition
,
65 DWORD dwFlagsAndAttributes
,
68 OBJECT_ATTRIBUTES ObjectAttributes
;
69 IO_STATUS_BLOCK IoStatusBlock
;
70 UNICODE_STRING NtPathU
;
73 ULONG FileAttributes
, Flags
= 0;
74 CSR_API_MESSAGE Request
;
75 PVOID EaBuffer
= NULL
;
78 DPRINT("CreateFileW(lpFileName %S)\n",lpFileName
);
80 /* validate & translate the creation disposition */
81 switch (dwCreationDisposition
)
84 dwCreationDisposition
= FILE_CREATE
;
88 dwCreationDisposition
= FILE_OVERWRITE_IF
;
92 dwCreationDisposition
= FILE_OPEN
;
96 dwCreationDisposition
= FILE_OPEN_IF
;
99 case TRUNCATE_EXISTING
:
100 dwCreationDisposition
= FILE_OVERWRITE
;
104 SetLastError(ERROR_INVALID_PARAMETER
);
105 return (INVALID_HANDLE_VALUE
);
108 /* validate & translate the filename */
109 if (!RtlDosPathNameToNtPathName_U ((LPWSTR
)lpFileName
,
114 DPRINT("Invalid path\n");
115 SetLastError(ERROR_PATH_NOT_FOUND
);
116 return INVALID_HANDLE_VALUE
;
119 DPRINT("NtPathU \'%S\'\n", NtPathU
.Buffer
);
121 /* validate & translate the flags */
123 /* translate the flags that need no validation */
124 if (!(dwFlagsAndAttributes
& FILE_FLAG_OVERLAPPED
))
126 /* yes, nonalert is correct! apc's are not delivered
127 while waiting for file io to complete */
128 Flags
|= FILE_SYNCHRONOUS_IO_NONALERT
;
131 if(dwFlagsAndAttributes
& FILE_FLAG_WRITE_THROUGH
)
132 Flags
|= FILE_WRITE_THROUGH
;
134 if(dwFlagsAndAttributes
& FILE_FLAG_NO_BUFFERING
)
135 Flags
|= FILE_NO_INTERMEDIATE_BUFFERING
;
137 if(dwFlagsAndAttributes
& FILE_FLAG_RANDOM_ACCESS
)
138 Flags
|= FILE_RANDOM_ACCESS
;
140 if(dwFlagsAndAttributes
& FILE_FLAG_SEQUENTIAL_SCAN
)
141 Flags
|= FILE_SEQUENTIAL_ONLY
;
143 if(dwFlagsAndAttributes
& FILE_FLAG_DELETE_ON_CLOSE
)
144 Flags
|= FILE_DELETE_ON_CLOSE
;
146 if(dwFlagsAndAttributes
& FILE_FLAG_BACKUP_SEMANTICS
)
148 if(dwDesiredAccess
& GENERIC_ALL
)
149 Flags
|= FILE_OPEN_FOR_BACKUP_INTENT
| FILE_OPEN_FOR_RECOVERY
;
152 if(dwDesiredAccess
& GENERIC_READ
)
153 Flags
|= FILE_OPEN_FOR_BACKUP_INTENT
;
155 if(dwDesiredAccess
& GENERIC_WRITE
)
156 Flags
|= FILE_OPEN_FOR_RECOVERY
;
160 Flags
|= FILE_NON_DIRECTORY_FILE
;
162 if(dwFlagsAndAttributes
& FILE_FLAG_OPEN_REPARSE_POINT
)
163 Flags
|= FILE_OPEN_REPARSE_POINT
;
165 if(dwFlagsAndAttributes
& FILE_FLAG_OPEN_NO_RECALL
)
166 Flags
|= FILE_OPEN_NO_RECALL
;
168 FileAttributes
= (dwFlagsAndAttributes
& (FILE_ATTRIBUTE_VALID_FLAGS
& ~FILE_ATTRIBUTE_DIRECTORY
));
170 /* handle may allways be waited on and querying attributes are allways allowed */
171 dwDesiredAccess
|= SYNCHRONIZE
| FILE_READ_ATTRIBUTES
;
173 /* FILE_FLAG_POSIX_SEMANTICS is handled later */
175 /* check for console output */
176 if (0 == _wcsicmp(L
"CONOUT$", lpFileName
))
178 /* FIXME: Send required access rights to Csrss */
179 Status
= CsrClientCallServer(&Request
,
181 MAKE_CSR_API(GET_OUTPUT_HANDLE
, CSR_NATIVE
),
182 sizeof(CSR_API_MESSAGE
));
183 if (!NT_SUCCESS(Status
) || !NT_SUCCESS(Status
= Request
.Status
))
185 SetLastErrorByStatus(Status
);
186 return INVALID_HANDLE_VALUE
;
190 return Request
.Data
.GetOutputHandleRequest
.OutputHandle
;
194 /* check for console input */
195 if (0 == _wcsicmp(L
"CONIN$", lpFileName
))
197 /* FIXME: Send required access rights to Csrss */
198 Status
= CsrClientCallServer(&Request
,
200 MAKE_CSR_API(GET_INPUT_HANDLE
, CSR_NATIVE
),
201 sizeof(CSR_API_MESSAGE
));
202 if (!NT_SUCCESS(Status
) || !NT_SUCCESS(Status
= Request
.Status
))
204 SetLastErrorByStatus(Status
);
205 return INVALID_HANDLE_VALUE
;
209 return Request
.Data
.GetInputHandleRequest
.InputHandle
;
213 if (hTemplateFile
!= NULL
)
215 FILE_EA_INFORMATION EaInformation
;
219 /* try to get the size of the extended attributes, if we fail just continue
220 creating the file without copying the attributes! */
221 Status
= NtQueryInformationFile(hTemplateFile
,
224 sizeof(FILE_EA_INFORMATION
),
226 if (NT_SUCCESS(Status
) && (EaInformation
.EaSize
!= 0))
228 /* there's extended attributes to read, let's give it a try */
229 EaBuffer
= RtlAllocateHeap(RtlGetProcessHeap(),
231 EaInformation
.EaSize
);
232 if (EaBuffer
== NULL
)
234 /* the template file handle is valid and has extended attributes,
235 however we seem to lack some memory here. We should fail here! */
236 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
237 return INVALID_HANDLE_VALUE
;
240 Status
= NtQueryEaFile(hTemplateFile
,
243 EaInformation
.EaSize
,
250 if (NT_SUCCESS(Status
))
252 /* we successfully read the extended attributes, break the loop
254 EaLength
= EaInformation
.EaSize
;
259 RtlFreeHeap(RtlGetProcessHeap(),
264 if (Status
!= STATUS_BUFFER_TOO_SMALL
)
266 /* unless we just allocated not enough memory, break the loop
267 and just continue without copying extended attributes */
274 /* we either failed to get the size of the extended attributes or
275 they're empty, just continue as there's no need to copy
282 /* build the object attributes */
283 InitializeObjectAttributes(&ObjectAttributes
,
289 if (lpSecurityAttributes
)
291 if(lpSecurityAttributes
->bInheritHandle
)
292 ObjectAttributes
.Attributes
|= OBJ_INHERIT
;
294 ObjectAttributes
.SecurityDescriptor
= lpSecurityAttributes
->lpSecurityDescriptor
;
297 if(!(dwFlagsAndAttributes
& FILE_FLAG_POSIX_SEMANTICS
))
298 ObjectAttributes
.Attributes
|= OBJ_CASE_INSENSITIVE
;
300 /* perform the call */
301 Status
= NtCreateFile (&FileHandle
,
308 dwCreationDisposition
,
313 RtlFreeUnicodeString(&NtPathU
);
315 /* free the extended attributes buffer if allocated */
316 if (EaBuffer
!= NULL
)
318 RtlFreeHeap(RtlGetProcessHeap(),
324 if (!NT_SUCCESS(Status
))
326 /* In the case file creation was rejected due to CREATE_NEW flag
327 * was specified and file with that name already exists, correct
328 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
329 * Note: RtlNtStatusToDosError is not the subject to blame here.
331 if (Status
== STATUS_OBJECT_NAME_COLLISION
&&
332 dwCreationDisposition
== FILE_CREATE
)
334 SetLastError( ERROR_FILE_EXISTS
);
338 SetLastErrorByStatus (Status
);
341 return INVALID_HANDLE_VALUE
;
345 create with OPEN_ALWAYS (FILE_OPEN_IF) returns info = FILE_OPENED or FILE_CREATED
346 create with CREATE_ALWAYS (FILE_OVERWRITE_IF) returns info = FILE_OVERWRITTEN or FILE_CREATED
348 if (dwCreationDisposition
== FILE_OPEN_IF
)
350 SetLastError(IoStatusBlock
.Information
== FILE_OPENED
? ERROR_ALREADY_EXISTS
: 0);
352 else if (dwCreationDisposition
== FILE_OVERWRITE_IF
)
354 SetLastError(IoStatusBlock
.Information
== FILE_OVERWRITTEN
? ERROR_ALREADY_EXISTS
: 0);