Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenlock.c
Go to the documentation of this file.
00001 /* $Id: lock.c 55795 2012-02-21 21:38:08Z ion $ 00002 * 00003 * COPYRIGHT: See COPYING in the top level directory 00004 * PROJECT: ReactOS system libraries 00005 * FILE: dll/win32/kernel32/file/lock.c 00006 * PURPOSE: Directory functions 00007 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl) 00008 * UPDATE HISTORY: 00009 * Created 01/11/98 00010 */ 00011 00012 00013 /* INCLUDES ****************************************************************/ 00014 00015 #include <k32.h> 00016 #define NDEBUG 00017 #include <debug.h> 00018 00019 /* FUNCTIONS ****************************************************************/ 00020 00021 /* 00022 * @implemented 00023 */ 00024 BOOL 00025 WINAPI 00026 LockFile(IN HANDLE hFile, 00027 IN DWORD dwFileOffsetLow, 00028 IN DWORD dwFileOffsetHigh, 00029 IN DWORD nNumberOfBytesToLockLow, 00030 IN DWORD nNumberOfBytesToLockHigh) 00031 { 00032 IO_STATUS_BLOCK IoStatusBlock; 00033 NTSTATUS Status; 00034 LARGE_INTEGER BytesToLock, Offset; 00035 00036 /* Is this a console handle? */ 00037 if (IsConsoleHandle(hFile)) 00038 { 00039 /* Can't "lock" a console! */ 00040 BaseSetLastNTError(STATUS_INVALID_HANDLE); 00041 return FALSE; 00042 } 00043 00044 /* Setup the parameters in NT style and call the native API */ 00045 BytesToLock.u.LowPart = nNumberOfBytesToLockLow; 00046 BytesToLock.u.HighPart = nNumberOfBytesToLockHigh; 00047 Offset.u.LowPart = dwFileOffsetLow; 00048 Offset.u.HighPart = dwFileOffsetHigh; 00049 Status = NtLockFile(hFile, 00050 NULL, 00051 NULL, 00052 NULL, 00053 &IoStatusBlock, 00054 &Offset, 00055 &BytesToLock, 00056 0, 00057 TRUE, 00058 TRUE); 00059 if (Status == STATUS_PENDING) 00060 { 00061 /* Wait for completion if needed */ 00062 Status = NtWaitForSingleObject(hFile, FALSE, NULL); 00063 if (NT_SUCCESS(Status)) Status = IoStatusBlock.Status; 00064 } 00065 00066 /* Check if we failed */ 00067 if (!NT_SUCCESS(Status)) 00068 { 00069 /* Convert the error code and fail */ 00070 BaseSetLastNTError(Status); 00071 return FALSE; 00072 } 00073 00074 /* Success! */ 00075 return TRUE; 00076 } 00077 00078 /* 00079 * @implemented 00080 */ 00081 BOOL 00082 WINAPI 00083 LockFileEx(IN HANDLE hFile, 00084 IN DWORD dwFlags, 00085 IN DWORD dwReserved, 00086 IN DWORD nNumberOfBytesToLockLow, 00087 IN DWORD nNumberOfBytesToLockHigh, 00088 IN LPOVERLAPPED lpOverlapped) 00089 { 00090 LARGE_INTEGER BytesToLock, Offset; 00091 NTSTATUS Status; 00092 00093 /* Is this a console handle? */ 00094 if (IsConsoleHandle(hFile)) 00095 { 00096 /* Can't "lock" a console! */ 00097 BaseSetLastNTError(STATUS_INVALID_HANDLE); 00098 return FALSE; 00099 } 00100 00101 /* This parameter should be zero */ 00102 if (dwReserved) 00103 { 00104 /* Fail since it isn't */ 00105 SetLastError(ERROR_INVALID_PARAMETER); 00106 return FALSE; 00107 } 00108 00109 /* Set the initial status in the IO_STATUS_BLOCK to pending... */ 00110 lpOverlapped->Internal = STATUS_PENDING; 00111 00112 /* Convert the parameters to NT format and call the native API */ 00113 Offset.u.LowPart = lpOverlapped->Offset; 00114 Offset.u.HighPart = lpOverlapped->OffsetHigh; 00115 BytesToLock.u.LowPart = nNumberOfBytesToLockLow; 00116 BytesToLock.u.HighPart = nNumberOfBytesToLockHigh; 00117 Status = NtLockFile(hFile, 00118 lpOverlapped->hEvent, 00119 NULL, 00120 NULL, 00121 (PIO_STATUS_BLOCK)lpOverlapped, 00122 &Offset, 00123 &BytesToLock, 00124 0, 00125 dwFlags & LOCKFILE_FAIL_IMMEDIATELY ? TRUE : FALSE, 00126 dwFlags & LOCKFILE_EXCLUSIVE_LOCK ? TRUE: FALSE); 00127 if ((NT_SUCCESS(Status)) && (Status != STATUS_PENDING)) 00128 { 00129 /* Pending status is *not* allowed in the Ex API */ 00130 return TRUE; 00131 } 00132 00133 /* Convert the error code and fail */ 00134 BaseSetLastNTError(Status); 00135 return FALSE; 00136 } 00137 00138 /* 00139 * @implemented 00140 */ 00141 BOOL 00142 WINAPI 00143 UnlockFile(IN HANDLE hFile, 00144 IN DWORD dwFileOffsetLow, 00145 IN DWORD dwFileOffsetHigh, 00146 IN DWORD nNumberOfBytesToUnlockLow, 00147 IN DWORD nNumberOfBytesToUnlockHigh) 00148 { 00149 OVERLAPPED Overlapped; 00150 NTSTATUS Status; 00151 BOOLEAN Result; 00152 00153 /* Convert parameters to Ex format and call the new API */ 00154 Overlapped.Offset = dwFileOffsetLow; 00155 Overlapped.OffsetHigh = dwFileOffsetHigh; 00156 Result = UnlockFileEx(hFile, 00157 0, 00158 nNumberOfBytesToUnlockLow, 00159 nNumberOfBytesToUnlockHigh, 00160 &Overlapped); 00161 if (!(Result) && (GetLastError() == ERROR_IO_PENDING)) 00162 { 00163 /* Ex fails during STATUS_PENDING, handle that here by waiting */ 00164 Status = NtWaitForSingleObject(hFile, FALSE, NULL); 00165 if (NT_SUCCESS(Status)) Status = Overlapped.Internal; 00166 00167 /* Now if the status is successful, return */ 00168 if (!NT_SUCCESS(Status)) return TRUE; 00169 00170 /* Otherwise the asynchronous operation had a failure, so fail */ 00171 BaseSetLastNTError(Status); 00172 return FALSE; 00173 } 00174 00175 /* Success or error case -- Ex took care of the rest, just return */ 00176 return Result; 00177 } 00178 00179 /* 00180 * @implemented 00181 */ 00182 BOOL 00183 WINAPI 00184 UnlockFileEx(IN HANDLE hFile, 00185 IN DWORD dwReserved, 00186 IN DWORD nNumberOfBytesToUnLockLow, 00187 IN DWORD nNumberOfBytesToUnLockHigh, 00188 IN LPOVERLAPPED lpOverlapped) 00189 { 00190 LARGE_INTEGER BytesToUnLock, StartAddress; 00191 NTSTATUS Status; 00192 00193 /* Is this a console handle? */ 00194 if (IsConsoleHandle(hFile)) 00195 { 00196 /* Can't "unlock" a console! */ 00197 BaseSetLastNTError(STATUS_INVALID_HANDLE); 00198 return FALSE; 00199 } 00200 00201 /* This parameter should be zero */ 00202 if (dwReserved) 00203 { 00204 /* Fail since it isn't */ 00205 SetLastError(ERROR_INVALID_PARAMETER); 00206 return FALSE; 00207 } 00208 00209 /* Convert to NT format and call the native function */ 00210 BytesToUnLock.u.LowPart = nNumberOfBytesToUnLockLow; 00211 BytesToUnLock.u.HighPart = nNumberOfBytesToUnLockHigh; 00212 StartAddress.u.LowPart = lpOverlapped->Offset; 00213 StartAddress.u.HighPart = lpOverlapped->OffsetHigh; 00214 Status = NtUnlockFile(hFile, 00215 (PIO_STATUS_BLOCK)lpOverlapped, 00216 &StartAddress, 00217 &BytesToUnLock, 00218 0); 00219 if (!NT_SUCCESS(Status)) 00220 { 00221 /* Convert the error and fail */ 00222 BaseSetLastNTError(Status); 00223 return FALSE; 00224 } 00225 00226 /* All good */ 00227 return TRUE; 00228 } 00229 00230 /* EOF */ Generated on Sat May 26 2012 04:22:58 for ReactOS by
1.7.6.1
|