2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: dll/win32/kernel32/file/lock.c
5 * PURPOSE: Directory functions
6 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
12 /* INCLUDES ****************************************************************/
18 /* FUNCTIONS ****************************************************************/
25 LockFile(IN HANDLE hFile
,
26 IN DWORD dwFileOffsetLow
,
27 IN DWORD dwFileOffsetHigh
,
28 IN DWORD nNumberOfBytesToLockLow
,
29 IN DWORD nNumberOfBytesToLockHigh
)
31 IO_STATUS_BLOCK IoStatusBlock
;
33 LARGE_INTEGER BytesToLock
, Offset
;
35 /* Is this a console handle? */
36 if (IsConsoleHandle(hFile
))
38 /* Can't "lock" a console! */
39 BaseSetLastNTError(STATUS_INVALID_HANDLE
);
43 /* Setup the parameters in NT style and call the native API */
44 BytesToLock
.u
.LowPart
= nNumberOfBytesToLockLow
;
45 BytesToLock
.u
.HighPart
= nNumberOfBytesToLockHigh
;
46 Offset
.u
.LowPart
= dwFileOffsetLow
;
47 Offset
.u
.HighPart
= dwFileOffsetHigh
;
48 Status
= NtLockFile(hFile
,
58 if (Status
== STATUS_PENDING
)
60 /* Wait for completion if needed */
61 Status
= NtWaitForSingleObject(hFile
, FALSE
, NULL
);
62 if (NT_SUCCESS(Status
)) Status
= IoStatusBlock
.Status
;
65 /* Check if we failed */
66 if (!NT_SUCCESS(Status
))
68 /* Convert the error code and fail */
69 BaseSetLastNTError(Status
);
82 LockFileEx(IN HANDLE hFile
,
85 IN DWORD nNumberOfBytesToLockLow
,
86 IN DWORD nNumberOfBytesToLockHigh
,
87 IN LPOVERLAPPED lpOverlapped
)
89 LARGE_INTEGER BytesToLock
, Offset
;
92 /* Is this a console handle? */
93 if (IsConsoleHandle(hFile
))
95 /* Can't "lock" a console! */
96 BaseSetLastNTError(STATUS_INVALID_HANDLE
);
100 /* This parameter should be zero */
103 /* Fail since it isn't */
104 SetLastError(ERROR_INVALID_PARAMETER
);
108 /* Set the initial status in the IO_STATUS_BLOCK to pending... */
109 lpOverlapped
->Internal
= STATUS_PENDING
;
111 /* Convert the parameters to NT format and call the native API */
112 Offset
.u
.LowPart
= lpOverlapped
->Offset
;
113 Offset
.u
.HighPart
= lpOverlapped
->OffsetHigh
;
114 BytesToLock
.u
.LowPart
= nNumberOfBytesToLockLow
;
115 BytesToLock
.u
.HighPart
= nNumberOfBytesToLockHigh
;
116 Status
= NtLockFile(hFile
,
117 lpOverlapped
->hEvent
,
120 (PIO_STATUS_BLOCK
)lpOverlapped
,
124 dwFlags
& LOCKFILE_FAIL_IMMEDIATELY
? TRUE
: FALSE
,
125 dwFlags
& LOCKFILE_EXCLUSIVE_LOCK
? TRUE
: FALSE
);
126 if ((NT_SUCCESS(Status
)) && (Status
!= STATUS_PENDING
))
128 /* Pending status is *not* allowed in the Ex API */
132 /* Convert the error code and fail */
133 BaseSetLastNTError(Status
);
142 UnlockFile(IN HANDLE hFile
,
143 IN DWORD dwFileOffsetLow
,
144 IN DWORD dwFileOffsetHigh
,
145 IN DWORD nNumberOfBytesToUnlockLow
,
146 IN DWORD nNumberOfBytesToUnlockHigh
)
148 OVERLAPPED Overlapped
;
152 /* Convert parameters to Ex format and call the new API */
153 Overlapped
.Offset
= dwFileOffsetLow
;
154 Overlapped
.OffsetHigh
= dwFileOffsetHigh
;
155 Result
= UnlockFileEx(hFile
,
157 nNumberOfBytesToUnlockLow
,
158 nNumberOfBytesToUnlockHigh
,
160 if (!(Result
) && (GetLastError() == ERROR_IO_PENDING
))
162 /* Ex fails during STATUS_PENDING, handle that here by waiting */
163 Status
= NtWaitForSingleObject(hFile
, FALSE
, NULL
);
164 if (NT_SUCCESS(Status
)) Status
= Overlapped
.Internal
;
166 /* Now if the status is successful, return */
167 if (!NT_SUCCESS(Status
)) return TRUE
;
169 /* Otherwise the asynchronous operation had a failure, so fail */
170 BaseSetLastNTError(Status
);
174 /* Success or error case -- Ex took care of the rest, just return */
183 UnlockFileEx(IN HANDLE hFile
,
185 IN DWORD nNumberOfBytesToUnLockLow
,
186 IN DWORD nNumberOfBytesToUnLockHigh
,
187 IN LPOVERLAPPED lpOverlapped
)
189 LARGE_INTEGER BytesToUnLock
, StartAddress
;
192 /* Is this a console handle? */
193 if (IsConsoleHandle(hFile
))
195 /* Can't "unlock" a console! */
196 BaseSetLastNTError(STATUS_INVALID_HANDLE
);
200 /* This parameter should be zero */
203 /* Fail since it isn't */
204 SetLastError(ERROR_INVALID_PARAMETER
);
208 /* Convert to NT format and call the native function */
209 BytesToUnLock
.u
.LowPart
= nNumberOfBytesToUnLockLow
;
210 BytesToUnLock
.u
.HighPart
= nNumberOfBytesToUnLockHigh
;
211 StartAddress
.u
.LowPart
= lpOverlapped
->Offset
;
212 StartAddress
.u
.HighPart
= lpOverlapped
->OffsetHigh
;
213 Status
= NtUnlockFile(hFile
,
214 (PIO_STATUS_BLOCK
)lpOverlapped
,
218 if (!NT_SUCCESS(Status
))
220 /* Convert the error and fail */
221 BaseSetLastNTError(Status
);