*** empty log message ***
[reactos.git] / os2 / lib / doscalls / file / openclose.cpp
1 /* $Id: openclose.cpp,v 1.1 2002/07/23 13:00:11 robertk Exp $
2 */
3 /*
4 *
5 * COPYRIGHT: See COPYING in the top level directory
6 * PROJECT: ReactOS OS/2 sub system
7 * FILE: dll/doscalls.c
8 * PURPOSE: Kernelservices for OS/2 apps
9 * PROGRAMMER: Robert K. nonvolatil@yahoo.de
10 * REVISION HISTORY:
11 * 13-03-2002 Created
12 */
13
14
15 #define INCL_DOSFILEMGR
16 #include "../../../include/os2.h"
17 #include <ddk/ntddk.h>
18
19
20
21 APIRET STDCALL Dos32Open(PSZ pszFileName, PHFILE pHf,
22 PULONG pulAction, ULONG cbFile,
23 ULONG ulAttribute, ULONG fsOpenFlags,
24 ULONG fsOpenMode, PVOID reserved ) //ULONGPEAOP2 peaop2)
25 {
26 /* NTAPI
27 ZwCreateFile(
28 OUT PHANDLE FileHandle,
29 IN ACCESS_MASK DesiredAccess,
30 IN POBJECT_ATTRIBUTES ObjectAttributes,
31 OUT PIO_STATUS_BLOCK IoStatusBlock,
32 IN PLARGE_INTEGER AllocationSize OPTIONAL,
33 IN ULONG FileAttributes,
34 IN ULONG ShareAccess,
35 IN ULONG CreateDisposition,
36 IN ULONG CreateOptions,
37 IN PVOID EaBuffer OPTIONAL,
38 IN ULONG EaLength
39 );*/
40
41
42
43 OBJECT_ATTRIBUTES ObjectAttributes;
44 IO_STATUS_BLOCK IoStatusBlock;
45 UNICODE_STRING NtPathU;
46 HANDLE FileHandle;
47 NTSTATUS Status;
48 ULONG Flags = 0;
49
50 switch (dwCreationDisposition)
51 {
52 case CREATE_NEW:
53 dwCreationDisposition = FILE_CREATE;
54 break;
55
56 case CREATE_ALWAYS:
57 dwCreationDisposition = FILE_OVERWRITE_IF;
58 break;
59
60 case OPEN_EXISTING:
61 dwCreationDisposition = FILE_OPEN;
62 break;
63
64 case OPEN_ALWAYS:
65 dwCreationDisposition = OPEN_ALWAYS;
66 break;
67
68 case TRUNCATE_EXISTING:
69 dwCreationDisposition = FILE_OVERWRITE;
70 }
71
72 DPRINT("CreateFileW(lpFileName %S)\n",lpFileName);
73
74 if (dwDesiredAccess & GENERIC_READ)
75 dwDesiredAccess |= FILE_GENERIC_READ;
76
77 if (dwDesiredAccess & GENERIC_WRITE)
78 dwDesiredAccess |= FILE_GENERIC_WRITE;
79
80 if (!(dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED))
81 {
82 Flags |= FILE_SYNCHRONOUS_IO_ALERT;
83 }
84
85 if (!RtlDosPathNameToNtPathName_U ((LPWSTR)lpFileName,
86 &NtPathU,
87 NULL,
88 NULL))
89 return INVALID_HANDLE_VALUE;
90
91 DPRINT("NtPathU \'%S\'\n", NtPathU.Buffer);
92
93 ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
94 ObjectAttributes.RootDirectory = NULL;
95 ObjectAttributes.ObjectName = &NtPathU;
96 ObjectAttributes.Attributes = OBJ_CASE_INSENSITIVE;
97 ObjectAttributes.SecurityDescriptor = NULL;
98 ObjectAttributes.SecurityQualityOfService = NULL;
99
100 Status = NtCreateFile (&FileHandle,
101 dwDesiredAccess,
102 &ObjectAttributes,
103 &IoStatusBlock,
104 NULL,
105 dwFlagsAndAttributes,
106 dwShareMode,
107 dwCreationDisposition,
108 Flags,
109 NULL,
110 0);
111 if (!NT_SUCCESS(Status))
112 {
113 SetLastErrorByStatus (Status);
114 return INVALID_HANDLE_VALUE;
115 }
116
117 return FileHandle;
118
119 return 0;
120 }
121
122
123 /* close a Handle. seems finished */
124 APIRET STDCALL Dos32Close(HFILE hFile)
125 {
126 NTSTATUS nErrCode;
127 nErrCode = NtClose( (HANDLE)hFile );
128 switch( nErrCode )
129 {
130 case STATUS_SUCCESS:
131 return NO_ERROR;
132 case STATUS_INVALID_HANDLE:
133 return ERROR_INVALID_HANDLE;
134 case STATUS_HANDLE_NOT_CLOSABLE:
135 return ERROR_FILE_NOT_FOUND;
136 }
137 return nErrCode;
138 }
139
140
141
142
143 APIRET STDCALL Dos32Read(HFILE hFile, PVOID pBuffer,
144 ULONG cbRead, PULONG pcbActual)
145 {
146 NTSTATUS nErrCode;
147 IO_STATUS_BLOCK isbStatus;
148 // read data from file
149 nErrCode = NtReadFile( (HANDLE)hFile, NULL, NULL, NULL,
150 &isbStatus, pBuffer, cbRead,
151 NULL, NULL );
152 // contains the # bytes actually read.
153 *pcbActual = isbStatus.Information;
154 switch(nErrCode)
155 {
156 case STATUS_INVALID_HANDLE:
157 return ERROR_INVALID_HANDLE;
158 // FIXME: complete this
159 }
160 return NO_ERROR;
161 }
162
163 /* Generic write to a stream given by hFile */
164 APIRET STDCALL Dos32Write(HFILE hFile, PVOID pBuffer,
165 ULONG cbWrite, PULONG pcbActual)
166 {
167 NTSTATUS nErrCode;
168 IO_STATUS_BLOCK StatusBlk;
169 nErrCode = NtWriteFile( (HANDLE)hFile, NULL, NULL, NULL,
170 &StatusBlk, pBuffer, cbWrite, 0, NULL );
171 *pcbActual = StatusBlk.Information;
172 // do an errorcode translation FIXME: correct
173 switch(nErrCode)
174 {
175 case STATUS_SUCCESS:
176 case STATUS_PENDING:
177 case STATUS_ACCESS_DENIED:
178 case STATUS_INVALID_HANDLE:
179 case STATUS_FILE_LOCK_CONFLICT:
180 return 0;
181 }
182 return 0;
183 }
184
185
186