1b406fcadbe8addc162503afc7dfa971b1330125
[reactos.git] / reactos / lib / kernel32 / file / iocompl.c
1 /* $Id: iocompl.c,v 1.8 2003/02/03 14:19:30 ekohl Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/file/iocompl.c
6 * PURPOSE: Io Completion functions
7 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
8 * UPDATE HISTORY:
9 * Created 01/11/98
10 */
11
12 #include <k32.h>
13
14
15 #include <kernel32/error.h>
16
17
18 typedef struct _FILE_COMPLETION_INFORMATION {
19 HANDLE CompletionPort;
20 ULONG CompletionKey;
21 } FILE_COMPLETION_INFORMATION;
22 typedef FILE_COMPLETION_INFORMATION *PFILE_COMPLETION_INFORMATION;
23
24
25 VOID
26 STDCALL
27 FileIOCompletionRoutine(
28 DWORD dwErrorCode,
29 DWORD dwNumberOfBytesTransfered,
30 LPOVERLAPPED lpOverlapped
31 );
32
33
34 HANDLE
35 STDCALL
36 CreateIoCompletionPort(
37 HANDLE FileHandle,
38 HANDLE ExistingCompletionPort,
39 DWORD CompletionKey,
40 DWORD NumberOfConcurrentThreads
41 )
42 {
43 HANDLE CompletionPort = NULL;
44 NTSTATUS errCode;
45 FILE_COMPLETION_INFORMATION CompletionInformation;
46 IO_STATUS_BLOCK IoStatusBlock;
47
48 if ( ExistingCompletionPort == NULL && FileHandle == INVALID_HANDLE_VALUE ) {
49 SetLastErrorByStatus (STATUS_INVALID_PARAMETER);
50 return FALSE;
51 }
52
53 if ( ExistingCompletionPort != NULL ) {
54 CompletionPort = ExistingCompletionPort;
55 }
56 else {
57 errCode = NtCreateIoCompletion(&CompletionPort,GENERIC_ALL,&IoStatusBlock,NumberOfConcurrentThreads);
58 if (!NT_SUCCESS(errCode) ) {
59 SetLastErrorByStatus (errCode);
60 return FALSE;
61 }
62
63 }
64 if ( FileHandle != INVALID_HANDLE_VALUE ) {
65
66 CompletionInformation.CompletionPort = CompletionPort;
67 CompletionInformation.CompletionKey = CompletionKey;
68
69 errCode = NtSetInformationFile(FileHandle, &IoStatusBlock,&CompletionInformation,sizeof(FILE_COMPLETION_INFORMATION),FileCompletionInformation);
70 if ( !NT_SUCCESS(errCode) ) {
71 if ( ExistingCompletionPort == NULL )
72 NtClose(CompletionPort);
73 SetLastErrorByStatus (errCode);
74 return FALSE;
75 }
76 }
77
78 return CompletionPort;
79 }
80
81
82 WINBOOL
83 STDCALL
84 GetQueuedCompletionStatus(
85 HANDLE CompletionPort,
86 LPDWORD lpNumberOfBytesTransferred,
87 LPDWORD lpCompletionKey,
88 LPOVERLAPPED *lpOverlapped,
89 DWORD dwMilliseconds
90 )
91 {
92 NTSTATUS errCode;
93 ULONG CompletionStatus;
94 LARGE_INTEGER TimeToWait;
95
96 errCode = NtRemoveIoCompletion(CompletionPort,(PULONG)lpCompletionKey,(PIO_STATUS_BLOCK)lpOverlapped,&CompletionStatus,&TimeToWait);
97 if (!NT_SUCCESS(errCode) ) {
98 SetLastErrorByStatus (errCode);
99 return FALSE;
100 }
101
102 return TRUE;
103 }
104
105
106 WINBOOL
107 STDCALL
108 PostQueuedCompletionStatus(
109 HANDLE CompletionPort,
110 DWORD dwNumberOfBytesTransferred,
111 DWORD dwCompletionKey,
112 LPOVERLAPPED lpOverlapped
113 )
114 {
115 NTSTATUS errCode;
116 errCode = NtSetIoCompletion(CompletionPort, dwCompletionKey, (PIO_STATUS_BLOCK)lpOverlapped , 0, (PULONG)&dwNumberOfBytesTransferred );
117
118 if ( !NT_SUCCESS(errCode) ) {
119 SetLastErrorByStatus (errCode);
120 return FALSE;
121 }
122 return TRUE;
123 }
124
125
126 // this should be a place holder ??????????????????
127 VOID
128 STDCALL
129 FileIOCompletionRoutine(
130 DWORD dwErrorCode,
131 DWORD dwNumberOfBytesTransfered,
132 LPOVERLAPPED lpOverlapped
133 )
134 {
135 return;
136 }
137
138
139 BOOL STDCALL
140 CancelIo(HANDLE hFile)
141 {
142 IO_STATUS_BLOCK IoStatusBlock;
143 NTSTATUS Status;
144
145 Status = NtCancelIoFile(hFile,
146 &IoStatusBlock);
147 if (!NT_SUCCESS(Status))
148 {
149 SetLastErrorByStatus(Status);
150 return(FALSE);
151 }
152
153 return(TRUE);
154 }
155
156 /* EOF */