ReactOS  0.4.13-dev-99-g7e18b6d
lock.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS system libraries
4  * FILE: dll/win32/kernel32/client/file/lock.c
5  * PURPOSE: Directory functions
6  * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
7  * UPDATE HISTORY:
8  * Created 01/11/98
9  */
10 
11 
12 /* INCLUDES ****************************************************************/
13 
14 #include <k32.h>
15 #define NDEBUG
16 #include <debug.h>
17 
18 /* FUNCTIONS ****************************************************************/
19 
20 /*
21  * @implemented
22  */
23 BOOL
24 WINAPI
26  IN DWORD dwFileOffsetLow,
27  IN DWORD dwFileOffsetHigh,
28  IN DWORD nNumberOfBytesToLockLow,
29  IN DWORD nNumberOfBytesToLockHigh)
30 {
33  LARGE_INTEGER BytesToLock, Offset;
34 
35  /* Is this a console handle? */
37  {
38  /* Can't "lock" a console! */
40  return FALSE;
41  }
42 
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;
49  NULL,
50  NULL,
51  NULL,
53  &Offset,
54  &BytesToLock,
55  0,
56  TRUE,
57  TRUE);
58  if (Status == STATUS_PENDING)
59  {
60  /* Wait for completion if needed */
63  }
64 
65  /* Check if we failed */
66  if (!NT_SUCCESS(Status))
67  {
68  /* Convert the error code and fail */
70  return FALSE;
71  }
72 
73  /* Success! */
74  return TRUE;
75 }
76 
77 /*
78  * @implemented
79  */
80 BOOL
81 WINAPI
85  IN DWORD nNumberOfBytesToLockLow,
86  IN DWORD nNumberOfBytesToLockHigh,
88 {
89  LARGE_INTEGER BytesToLock, Offset;
91 
92  /* Is this a console handle? */
94  {
95  /* Can't "lock" a console! */
97  return FALSE;
98  }
99 
100  /* This parameter should be zero */
101  if (dwReserved)
102  {
103  /* Fail since it isn't */
105  return FALSE;
106  }
107 
108  /* Set the initial status in the IO_STATUS_BLOCK to pending... */
109  lpOverlapped->Internal = STATUS_PENDING;
110 
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;
117  lpOverlapped->hEvent,
118  NULL,
119  NULL,
121  &Offset,
122  &BytesToLock,
123  0,
126  if ((NT_SUCCESS(Status)) && (Status != STATUS_PENDING))
127  {
128  /* Pending status is *not* allowed in the Ex API */
129  return TRUE;
130  }
131 
132  /* Convert the error code and fail */
134  return FALSE;
135 }
136 
137 /*
138  * @implemented
139  */
140 BOOL
141 WINAPI
143  IN DWORD dwFileOffsetLow,
144  IN DWORD dwFileOffsetHigh,
145  IN DWORD nNumberOfBytesToUnlockLow,
146  IN DWORD nNumberOfBytesToUnlockHigh)
147 {
148  OVERLAPPED Overlapped;
150  BOOLEAN Result;
151 
152  /* Convert parameters to Ex format and call the new API */
153  Overlapped.Offset = dwFileOffsetLow;
154  Overlapped.OffsetHigh = dwFileOffsetHigh;
156  0,
157  nNumberOfBytesToUnlockLow,
158  nNumberOfBytesToUnlockHigh,
159  &Overlapped);
160  if (!(Result) && (GetLastError() == ERROR_IO_PENDING))
161  {
162  /* Ex fails during STATUS_PENDING, handle that here by waiting */
164  if (NT_SUCCESS(Status)) Status = Overlapped.Internal;
165 
166  /* Now if the status is successful, return */
167  if (!NT_SUCCESS(Status)) return TRUE;
168 
169  /* Otherwise the asynchronous operation had a failure, so fail */
171  return FALSE;
172  }
173 
174  /* Success or error case -- Ex took care of the rest, just return */
175  return Result;
176 }
177 
178 /*
179  * @implemented
180  */
181 BOOL
182 WINAPI
185  IN DWORD nNumberOfBytesToUnLockLow,
186  IN DWORD nNumberOfBytesToUnLockHigh,
188 {
189  LARGE_INTEGER BytesToUnLock, StartAddress;
191 
192  /* Is this a console handle? */
193  if (IsConsoleHandle(hFile))
194  {
195  /* Can't "unlock" a console! */
197  return FALSE;
198  }
199 
200  /* This parameter should be zero */
201  if (dwReserved)
202  {
203  /* Fail since it isn't */
205  return FALSE;
206  }
207 
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;
215  &StartAddress,
216  &BytesToUnLock,
217  0);
218  if (!NT_SUCCESS(Status))
219  {
220  /* Convert the error and fail */
222  return FALSE;
223  }
224 
225  /* All good */
226  return TRUE;
227 }
228 
229 /* EOF */
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
DWORD Offset
Definition: winbase.h:787
#define ERROR_IO_PENDING
Definition: dderror.h:15
#define STATUS_INVALID_HANDLE
Definition: ntstatus.h:231
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
BOOL WINAPI UnlockFile(IN HANDLE hFile, IN DWORD dwFileOffsetLow, IN DWORD dwFileOffsetHigh, IN DWORD nNumberOfBytesToUnlockLow, IN DWORD nNumberOfBytesToUnlockHigh)
Definition: lock.c:142
_In_ HANDLE _In_ DWORD _In_ DWORD _Inout_opt_ LPOVERLAPPED _In_opt_ LPTRANSMIT_FILE_BUFFERS _In_ DWORD dwReserved
Definition: mswsock.h:90
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned char BOOLEAN
#define LOCKFILE_FAIL_IMMEDIATELY
Definition: winbase.h:366
smooth NULL
Definition: ftsmooth.c:416
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:166
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_PENDING
Definition: ntstatus.h:82
#define WINAPI
Definition: msvc.h:8
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:409
#define IsConsoleHandle(h)
Definition: console.h:14
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1175
BOOL WINAPI UnlockFileEx(IN HANDLE hFile, IN DWORD dwReserved, IN DWORD nNumberOfBytesToUnLockLow, IN DWORD nNumberOfBytesToUnLockHigh, IN LPOVERLAPPED lpOverlapped)
Definition: lock.c:183
_In_ HANDLE hFile
Definition: mswsock.h:90
BOOL WINAPI LockFile(IN HANDLE hFile, IN DWORD dwFileOffsetLow, IN DWORD dwFileOffsetHigh, IN DWORD nNumberOfBytesToLockLow, IN DWORD nNumberOfBytesToLockHigh)
Definition: lock.c:25
Status
Definition: gdiplustypes.h:24
NTSTATUS NTAPI NtUnlockFile(IN HANDLE FileHandle, OUT PIO_STATUS_BLOCK IoStatusBlock, IN PLARGE_INTEGER ByteOffset, IN PLARGE_INTEGER Length, IN ULONG Key OPTIONAL)
Definition: iofunc.c:3497
BOOL WINAPI LockFileEx(IN HANDLE hFile, IN DWORD dwFlags, IN DWORD dwReserved, IN DWORD nNumberOfBytesToLockLow, IN DWORD nNumberOfBytesToLockHigh, IN LPOVERLAPPED lpOverlapped)
Definition: lock.c:82
_In_ HANDLE _In_ DWORD _In_ DWORD _Inout_opt_ LPOVERLAPPED lpOverlapped
Definition: mswsock.h:90
DWORD OffsetHigh
Definition: winbase.h:788
#define LOCKFILE_EXCLUSIVE_LOCK
Definition: winbase.h:367
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
struct _LARGE_INTEGER::@2192 u
ULONG_PTR Internal
Definition: winbase.h:783
NTSTATUS NTAPI NtLockFile(IN HANDLE FileHandle, IN HANDLE EventHandle OPTIONAL, IN PIO_APC_ROUTINE ApcRoutine OPTIONAL, IN PVOID ApcContext OPTIONAL, OUT PIO_STATUS_BLOCK IoStatusBlock, IN PLARGE_INTEGER ByteOffset, IN PLARGE_INTEGER Length, IN ULONG Key, IN BOOLEAN FailImmediately, IN BOOLEAN ExclusiveLock)
Definition: iofunc.c:1754
NTSYSAPI NTSTATUS NTAPI NtWaitForSingleObject(IN HANDLE hObject, IN BOOLEAN bAlertable, IN PLARGE_INTEGER Timeout)