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 CSRSS_API_REQUEST Request
;
75 CSRSS_API_REPLY Reply
;
76 PVOID EaBuffer
= NULL
;
79 DPRINT("CreateFileW(lpFileName %S)\n",lpFileName
);
81 /* validate & translate the creation disposition */
82 switch (dwCreationDisposition
)
85 dwCreationDisposition
= FILE_CREATE
;
89 dwCreationDisposition
= FILE_OVERWRITE_IF
;
93 dwCreationDisposition
= FILE_OPEN
;
97 dwCreationDisposition
= FILE_OPEN_IF
;
100 case TRUNCATE_EXISTING
:
101 dwCreationDisposition
= FILE_OVERWRITE
;
105 SetLastError(ERROR_INVALID_PARAMETER
);
106 return (INVALID_HANDLE_VALUE
);
109 /* validate & translate the filename */
110 if (!RtlDosPathNameToNtPathName_U ((LPWSTR
)lpFileName
,
115 DPRINT("Invalid path\n");
116 SetLastError(ERROR_PATH_NOT_FOUND
);
117 return INVALID_HANDLE_VALUE
;
120 DPRINT("NtPathU \'%S\'\n", NtPathU
.Buffer
);
122 /* validate & translate the flags */
124 /* translate the flags that need no validation */
125 if (!(dwFlagsAndAttributes
& FILE_FLAG_OVERLAPPED
))
127 /* yes, nonalert is correct! apc's are not delivered
128 while waiting for file io to complete */
129 Flags
|= FILE_SYNCHRONOUS_IO_NONALERT
;
132 if(dwFlagsAndAttributes
& FILE_FLAG_WRITE_THROUGH
)
133 Flags
|= FILE_WRITE_THROUGH
;
135 if(dwFlagsAndAttributes
& FILE_FLAG_NO_BUFFERING
)
136 Flags
|= FILE_NO_INTERMEDIATE_BUFFERING
;
138 if(dwFlagsAndAttributes
& FILE_FLAG_RANDOM_ACCESS
)
139 Flags
|= FILE_RANDOM_ACCESS
;
141 if(dwFlagsAndAttributes
& FILE_FLAG_SEQUENTIAL_SCAN
)
142 Flags
|= FILE_SEQUENTIAL_ONLY
;
144 if(dwFlagsAndAttributes
& FILE_FLAG_DELETE_ON_CLOSE
)
145 Flags
|= FILE_DELETE_ON_CLOSE
;
147 if(dwFlagsAndAttributes
& FILE_FLAG_BACKUP_SEMANTICS
)
149 if(dwDesiredAccess
& GENERIC_ALL
)
150 Flags
|= FILE_OPEN_FOR_BACKUP_INTENT
| FILE_OPEN_FOR_RECOVERY
;
153 if(dwDesiredAccess
& GENERIC_READ
)
154 Flags
|= FILE_OPEN_FOR_BACKUP_INTENT
;
156 if(dwDesiredAccess
& GENERIC_WRITE
)
157 Flags
|= FILE_OPEN_FOR_RECOVERY
;
161 Flags
|= FILE_NON_DIRECTORY_FILE
;
163 if(dwFlagsAndAttributes
& FILE_FLAG_OPEN_REPARSE_POINT
)
164 Flags
|= FILE_OPEN_REPARSE_POINT
;
166 if(dwFlagsAndAttributes
& FILE_FLAG_OPEN_NO_RECALL
)
167 Flags
|= FILE_OPEN_NO_RECALL
;
169 FileAttributes
= (dwFlagsAndAttributes
& (FILE_ATTRIBUTE_VALID_FLAGS
& ~FILE_ATTRIBUTE_DIRECTORY
));
171 /* handle may allways be waited on and querying attributes are allways allowed */
172 dwDesiredAccess
|= SYNCHRONIZE
| FILE_READ_ATTRIBUTES
;
174 /* FILE_FLAG_POSIX_SEMANTICS is handled later */
176 /* check for console output */
177 if (0 == _wcsicmp(L
"CONOUT$", lpFileName
))
179 /* FIXME: Send required access rights to Csrss */
180 Request
.Type
= CSRSS_GET_OUTPUT_HANDLE
;
181 Status
= CsrClientCallServer(&Request
,
183 sizeof(CSRSS_API_REQUEST
),
184 sizeof(CSRSS_API_REPLY
));
185 if (!NT_SUCCESS(Status
) || !NT_SUCCESS(Status
= Reply
.Status
))
187 SetLastErrorByStatus(Status
);
188 return INVALID_HANDLE_VALUE
;
192 return Reply
.Data
.GetOutputHandleReply
.OutputHandle
;
196 /* check for console input */
197 if (0 == _wcsicmp(L
"CONIN$", lpFileName
))
199 /* FIXME: Send required access rights to Csrss */
200 Request
.Type
= CSRSS_GET_INPUT_HANDLE
;
201 Status
= CsrClientCallServer(&Request
,
203 sizeof(CSRSS_API_REQUEST
),
204 sizeof(CSRSS_API_REPLY
));
205 if (!NT_SUCCESS(Status
) || !NT_SUCCESS(Status
= Reply
.Status
))
207 SetLastErrorByStatus(Status
);
208 return INVALID_HANDLE_VALUE
;
212 return Reply
.Data
.GetInputHandleReply
.InputHandle
;
216 if (hTemplateFile
!= NULL
)
218 FILE_EA_INFORMATION EaInformation
;
222 /* try to get the size of the extended attributes, if we fail just continue
223 creating the file without copying the attributes! */
224 Status
= NtQueryInformationFile(hTemplateFile
,
227 sizeof(FILE_EA_INFORMATION
),
229 if (NT_SUCCESS(Status
) && (EaInformation
.EaSize
!= 0))
231 /* there's extended attributes to read, let's give it a try */
232 EaBuffer
= RtlAllocateHeap(RtlGetProcessHeap(),
234 EaInformation
.EaSize
);
235 if (EaBuffer
== NULL
)
237 /* the template file handle is valid and has extended attributes,
238 however we seem to lack some memory here. We should fail here! */
239 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
240 return INVALID_HANDLE_VALUE
;
243 Status
= NtQueryEaFile(hTemplateFile
,
246 EaInformation
.EaSize
,
253 if (NT_SUCCESS(Status
))
255 /* we successfully read the extended attributes, break the loop
257 EaLength
= EaInformation
.EaSize
;
262 RtlFreeHeap(RtlGetProcessHeap(),
267 if (Status
!= STATUS_BUFFER_TOO_SMALL
)
269 /* unless we just allocated not enough memory, break the loop
270 and just continue without copying extended attributes */
277 /* we either failed to get the size of the extended attributes or
278 they're empty, just continue as there's no need to copy
285 /* build the object attributes */
286 InitializeObjectAttributes(&ObjectAttributes
,
292 if (lpSecurityAttributes
)
294 if(lpSecurityAttributes
->bInheritHandle
)
295 ObjectAttributes
.Attributes
|= OBJ_INHERIT
;
297 ObjectAttributes
.SecurityDescriptor
= lpSecurityAttributes
->lpSecurityDescriptor
;
300 if(!(dwFlagsAndAttributes
& FILE_FLAG_POSIX_SEMANTICS
))
301 ObjectAttributes
.Attributes
|= OBJ_CASE_INSENSITIVE
;
303 /* perform the call */
304 Status
= NtCreateFile (&FileHandle
,
311 dwCreationDisposition
,
316 RtlFreeUnicodeString(&NtPathU
);
318 /* free the extended attributes buffer if allocated */
319 if (EaBuffer
!= NULL
)
321 RtlFreeHeap(RtlGetProcessHeap(),
327 if (!NT_SUCCESS(Status
))
329 /* In the case file creation was rejected due to CREATE_NEW flag
330 * was specified and file with that name already exists, correct
331 * last error is ERROR_FILE_EXISTS and not ERROR_ALREADY_EXISTS.
332 * Note: RtlNtStatusToDosError is not the subject to blame here.
334 if (Status
== STATUS_OBJECT_NAME_COLLISION
&&
335 dwCreationDisposition
== FILE_CREATE
)
337 SetLastError( ERROR_FILE_EXISTS
);
341 SetLastErrorByStatus (Status
);
344 return INVALID_HANDLE_VALUE
;
348 create with OPEN_ALWAYS (FILE_OPEN_IF) returns info = FILE_OPENED or FILE_CREATED
349 create with CREATE_ALWAYS (FILE_OVERWRITE_IF) returns info = FILE_OVERWRITTEN or FILE_CREATED
351 if (dwCreationDisposition
== FILE_OPEN_IF
)
353 SetLastError(IoStatusBlock
.Information
== FILE_OPENED
? ERROR_ALREADY_EXISTS
: 0);
355 else if (dwCreationDisposition
== FILE_OVERWRITE_IF
)
357 SetLastError(IoStatusBlock
.Information
== FILE_OVERWRITTEN
? ERROR_ALREADY_EXISTS
: 0);