- Update to r53061
[reactos.git] / dll / win32 / kernel32 / client / file / lock.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: dll/win32/kernel32/file/lock.c
6 * PURPOSE: Directory functions
7 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
8 * UPDATE HISTORY:
9 * Created 01/11/98
10 */
11
12 /* FIXME: the large integer manipulations in this file dont handle overflow */
13
14 /* INCLUDES ****************************************************************/
15
16 #include <k32.h>
17 #define NDEBUG
18 #include <debug.h>
19 DEBUG_CHANNEL(kernel32file);
20
21 /* FUNCTIONS ****************************************************************/
22
23 /*
24 * @implemented
25 */
26 BOOL
27 WINAPI
28 LockFile(HANDLE hFile,
29 DWORD dwFileOffsetLow,
30 DWORD dwFileOffsetHigh,
31 DWORD nNumberOfBytesToLockLow,
32 DWORD nNumberOfBytesToLockHigh)
33 {
34 DWORD dwReserved;
35 OVERLAPPED Overlapped;
36
37 Overlapped.Offset = dwFileOffsetLow;
38 Overlapped.OffsetHigh = dwFileOffsetHigh;
39 Overlapped.hEvent = NULL;
40 dwReserved = 0;
41
42 return LockFileEx(hFile,
43 LOCKFILE_FAIL_IMMEDIATELY |
44 LOCKFILE_EXCLUSIVE_LOCK,
45 dwReserved,
46 nNumberOfBytesToLockLow,
47 nNumberOfBytesToLockHigh,
48 &Overlapped ) ;
49 }
50
51
52 /*
53 * @implemented
54 */
55 BOOL
56 WINAPI
57 LockFileEx(HANDLE hFile,
58 DWORD dwFlags,
59 DWORD dwReserved,
60 DWORD nNumberOfBytesToLockLow,
61 DWORD nNumberOfBytesToLockHigh,
62 LPOVERLAPPED lpOverlapped /* required! */)
63 {
64 LARGE_INTEGER BytesToLock;
65 BOOL LockImmediate;
66 BOOL LockExclusive;
67 NTSTATUS errCode;
68 LARGE_INTEGER Offset;
69
70 if(dwReserved != 0 || lpOverlapped==NULL)
71 {
72 SetLastError(ERROR_INVALID_PARAMETER);
73 return FALSE;
74 }
75
76 TRACE( "%p %x%08x %x%08x flags %x\n",
77 hFile, lpOverlapped->OffsetHigh, lpOverlapped->Offset,
78 nNumberOfBytesToLockHigh, nNumberOfBytesToLockLow, dwFlags );
79
80 lpOverlapped->Internal = STATUS_PENDING;
81
82 Offset.u.LowPart = lpOverlapped->Offset;
83 Offset.u.HighPart = lpOverlapped->OffsetHigh;
84
85 if ( (dwFlags & LOCKFILE_FAIL_IMMEDIATELY) == LOCKFILE_FAIL_IMMEDIATELY )
86 LockImmediate = TRUE;
87 else
88 LockImmediate = FALSE;
89
90 if ( (dwFlags & LOCKFILE_EXCLUSIVE_LOCK) == LOCKFILE_EXCLUSIVE_LOCK )
91 LockExclusive = TRUE;
92 else
93 LockExclusive = FALSE;
94
95 BytesToLock.u.LowPart = nNumberOfBytesToLockLow;
96 BytesToLock.u.HighPart = nNumberOfBytesToLockHigh;
97
98 errCode = NtLockFile(hFile,
99 lpOverlapped->hEvent,
100 NULL,
101 NULL,
102 (PIO_STATUS_BLOCK)lpOverlapped,
103 &Offset,
104 &BytesToLock,
105 0,
106 (BOOLEAN)LockImmediate,
107 (BOOLEAN)LockExclusive);
108
109 if ( !NT_SUCCESS(errCode) )
110 {
111 BaseSetLastNTError(errCode);
112 return FALSE;
113 }
114
115 return TRUE;
116 }
117
118
119 /*
120 * @implemented
121 */
122 BOOL
123 WINAPI
124 UnlockFile(HANDLE hFile,
125 DWORD dwFileOffsetLow,
126 DWORD dwFileOffsetHigh,
127 DWORD nNumberOfBytesToUnlockLow,
128 DWORD nNumberOfBytesToUnlockHigh)
129 {
130 OVERLAPPED Overlapped;
131 DWORD dwReserved;
132 Overlapped.Offset = dwFileOffsetLow;
133 Overlapped.OffsetHigh = dwFileOffsetHigh;
134 dwReserved = 0;
135
136 return UnlockFileEx(hFile,
137 dwReserved,
138 nNumberOfBytesToUnlockLow,
139 nNumberOfBytesToUnlockHigh,
140 &Overlapped);
141 }
142
143
144 /*
145 * @implemented
146 */
147 BOOL
148 WINAPI
149 UnlockFileEx(HANDLE hFile,
150 DWORD dwReserved,
151 DWORD nNumberOfBytesToUnLockLow,
152 DWORD nNumberOfBytesToUnLockHigh,
153 LPOVERLAPPED lpOverlapped /* required! */)
154 {
155 LARGE_INTEGER BytesToUnLock;
156 LARGE_INTEGER StartAddress;
157 NTSTATUS errCode;
158
159 if(dwReserved != 0 || lpOverlapped == NULL)
160 {
161 SetLastError(ERROR_INVALID_PARAMETER);
162 return FALSE;
163 }
164
165 TRACE( "%p %x%08x %x%08x\n",
166 hFile, lpOverlapped->OffsetHigh, lpOverlapped->Offset,
167 nNumberOfBytesToUnLockHigh, nNumberOfBytesToUnLockLow);
168
169 BytesToUnLock.u.LowPart = nNumberOfBytesToUnLockLow;
170 BytesToUnLock.u.HighPart = nNumberOfBytesToUnLockHigh;
171
172 StartAddress.u.LowPart = lpOverlapped->Offset;
173 StartAddress.u.HighPart = lpOverlapped->OffsetHigh;
174
175 errCode = NtUnlockFile(hFile,
176 (PIO_STATUS_BLOCK)lpOverlapped,
177 &StartAddress,
178 &BytesToUnLock,
179 0);
180
181 if ( !NT_SUCCESS(errCode) )
182 {
183 BaseSetLastNTError(errCode);
184 return FALSE;
185 }
186
187 return TRUE;
188 }
189
190 /* EOF */