03126dfb069fef3a253d975c0f6caa8bf8029730
[reactos.git] / reactos / drivers / filesystems / ntfs / finfo.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystem/ntfs/dirctl.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMERS: Eric Kohl
24 * Hervé Poussineau (hpoussin@reactos.org)
25 */
26
27 /* INCLUDES *****************************************************************/
28
29 #include "ntfs.h"
30
31 #define NDEBUG
32 #include <debug.h>
33
34 /* FUNCTIONS ****************************************************************/
35
36 /*
37 * FUNCTION: Retrieve the standard file information
38 */
39 static
40 NTSTATUS
41 NtfsGetStandardInformation(PNTFS_FCB Fcb,
42 PDEVICE_OBJECT DeviceObject,
43 PFILE_STANDARD_INFORMATION StandardInfo,
44 PULONG BufferLength)
45 {
46 UNREFERENCED_PARAMETER(DeviceObject);
47
48 DPRINT1("NtfsGetStandardInformation(%p, %p, %p, %p)\n", Fcb, DeviceObject, StandardInfo, BufferLength);
49
50 if (*BufferLength < sizeof(FILE_STANDARD_INFORMATION))
51 return STATUS_BUFFER_OVERFLOW;
52
53 /* PRECONDITION */
54 ASSERT(StandardInfo != NULL);
55 ASSERT(Fcb != NULL);
56
57 RtlZeroMemory(StandardInfo,
58 sizeof(FILE_STANDARD_INFORMATION));
59
60 StandardInfo->AllocationSize = Fcb->RFCB.AllocationSize;
61 StandardInfo->EndOfFile = Fcb->RFCB.FileSize;
62 StandardInfo->NumberOfLinks = Fcb->LinkCount;
63 StandardInfo->DeletePending = FALSE;
64 StandardInfo->Directory = NtfsFCBIsDirectory(Fcb);
65
66 *BufferLength -= sizeof(FILE_STANDARD_INFORMATION);
67
68 return STATUS_SUCCESS;
69 }
70
71
72 static
73 NTSTATUS
74 NtfsGetPositionInformation(PFILE_OBJECT FileObject,
75 PFILE_POSITION_INFORMATION PositionInfo,
76 PULONG BufferLength)
77 {
78 DPRINT1("NtfsGetPositionInformation(%p, %p, %p)\n", FileObject, PositionInfo, BufferLength);
79
80 if (*BufferLength < sizeof(FILE_POSITION_INFORMATION))
81 return STATUS_BUFFER_OVERFLOW;
82
83 PositionInfo->CurrentByteOffset.QuadPart = FileObject->CurrentByteOffset.QuadPart;
84
85 DPRINT("Getting position %I64x\n",
86 PositionInfo->CurrentByteOffset.QuadPart);
87
88 *BufferLength -= sizeof(FILE_POSITION_INFORMATION);
89
90 return STATUS_SUCCESS;
91 }
92
93
94 static
95 NTSTATUS
96 NtfsGetBasicInformation(PFILE_OBJECT FileObject,
97 PNTFS_FCB Fcb,
98 PDEVICE_OBJECT DeviceObject,
99 PFILE_BASIC_INFORMATION BasicInfo,
100 PULONG BufferLength)
101 {
102 PFILENAME_ATTRIBUTE FileName = &Fcb->Entry;
103
104 DPRINT1("NtfsGetBasicInformation(%p, %p, %p, %p, %p)\n", FileObject, Fcb, DeviceObject, BasicInfo, BufferLength);
105
106 if (*BufferLength < sizeof(FILE_BASIC_INFORMATION))
107 return STATUS_BUFFER_OVERFLOW;
108
109 BasicInfo->CreationTime.QuadPart = FileName->CreationTime;
110 BasicInfo->LastAccessTime.QuadPart = FileName->LastAccessTime;
111 BasicInfo->LastWriteTime.QuadPart = FileName->LastWriteTime;
112 BasicInfo->ChangeTime.QuadPart = FileName->ChangeTime;
113
114 NtfsFileFlagsToAttributes(FileName->FileAttributes, &BasicInfo->FileAttributes);
115
116 *BufferLength -= sizeof(FILE_BASIC_INFORMATION);
117
118 return STATUS_SUCCESS;
119 }
120
121
122 /*
123 * FUNCTION: Retrieve the file name information
124 */
125 static
126 NTSTATUS
127 NtfsGetNameInformation(PFILE_OBJECT FileObject,
128 PNTFS_FCB Fcb,
129 PDEVICE_OBJECT DeviceObject,
130 PFILE_NAME_INFORMATION NameInfo,
131 PULONG BufferLength)
132 {
133 ULONG BytesToCopy;
134
135 UNREFERENCED_PARAMETER(FileObject);
136 UNREFERENCED_PARAMETER(DeviceObject);
137
138 DPRINT1("NtfsGetNameInformation(%p, %p, %p, %p, %p)\n", FileObject, Fcb, DeviceObject, NameInfo, BufferLength);
139
140 ASSERT(NameInfo != NULL);
141 ASSERT(Fcb != NULL);
142
143 /* If buffer can't hold at least the file name length, bail out */
144 if (*BufferLength < (ULONG)FIELD_OFFSET(FILE_NAME_INFORMATION, FileName[0]))
145 return STATUS_BUFFER_OVERFLOW;
146
147 /* Save file name length, and as much file len, as buffer length allows */
148 NameInfo->FileNameLength = wcslen(Fcb->PathName) * sizeof(WCHAR);
149
150 /* Calculate amount of bytes to copy not to overflow the buffer */
151 BytesToCopy = min(NameInfo->FileNameLength,
152 *BufferLength - FIELD_OFFSET(FILE_NAME_INFORMATION, FileName[0]));
153
154 /* Fill in the bytes */
155 RtlCopyMemory(NameInfo->FileName, Fcb->PathName, BytesToCopy);
156
157 /* Check if we could write more but are not able to */
158 if (*BufferLength < NameInfo->FileNameLength + (ULONG)FIELD_OFFSET(FILE_NAME_INFORMATION, FileName[0]))
159 {
160 /* Return number of bytes written */
161 *BufferLength -= FIELD_OFFSET(FILE_NAME_INFORMATION, FileName[0]) + BytesToCopy;
162 return STATUS_BUFFER_OVERFLOW;
163 }
164
165 /* We filled up as many bytes, as needed */
166 *BufferLength -= (FIELD_OFFSET(FILE_NAME_INFORMATION, FileName[0]) + NameInfo->FileNameLength);
167
168 return STATUS_SUCCESS;
169 }
170
171
172 static
173 NTSTATUS
174 NtfsGetInternalInformation(PNTFS_FCB Fcb,
175 PFILE_INTERNAL_INFORMATION InternalInfo,
176 PULONG BufferLength)
177 {
178 DPRINT1("NtfsGetInternalInformation(%p, %p, %p)\n", Fcb, InternalInfo, BufferLength);
179
180 ASSERT(InternalInfo);
181 ASSERT(Fcb);
182
183 if (*BufferLength < sizeof(FILE_INTERNAL_INFORMATION))
184 return STATUS_BUFFER_OVERFLOW;
185
186 InternalInfo->IndexNumber.QuadPart = Fcb->MFTIndex;
187
188 *BufferLength -= sizeof(FILE_INTERNAL_INFORMATION);
189
190 return STATUS_SUCCESS;
191 }
192
193 static
194 NTSTATUS
195 NtfsGetNetworkOpenInformation(PNTFS_FCB Fcb,
196 PDEVICE_EXTENSION DeviceExt,
197 PFILE_NETWORK_OPEN_INFORMATION NetworkInfo,
198 PULONG BufferLength)
199 {
200 PFILENAME_ATTRIBUTE FileName = &Fcb->Entry;
201
202 DPRINT1("NtfsGetNetworkOpenInformation(%p, %p, %p, %p)\n", Fcb, DeviceExt, NetworkInfo, BufferLength);
203
204 if (*BufferLength < sizeof(FILE_NETWORK_OPEN_INFORMATION))
205 return(STATUS_BUFFER_OVERFLOW);
206
207 NetworkInfo->CreationTime.QuadPart = FileName->CreationTime;
208 NetworkInfo->LastAccessTime.QuadPart = FileName->LastAccessTime;
209 NetworkInfo->LastWriteTime.QuadPart = FileName->LastWriteTime;
210 NetworkInfo->ChangeTime.QuadPart = FileName->ChangeTime;
211
212 NetworkInfo->EndOfFile.QuadPart = FileName->AllocatedSize;
213 NetworkInfo->AllocationSize.QuadPart = ROUND_UP(FileName->AllocatedSize, DeviceExt->NtfsInfo.BytesPerCluster);
214
215 NtfsFileFlagsToAttributes(FileName->FileAttributes, &NetworkInfo->FileAttributes);
216
217 *BufferLength -= sizeof(FILE_NETWORK_OPEN_INFORMATION);
218 return STATUS_SUCCESS;
219 }
220
221 /*
222 * FUNCTION: Retrieve the specified file information
223 */
224 NTSTATUS
225 NtfsQueryInformation(PNTFS_IRP_CONTEXT IrpContext)
226 {
227 FILE_INFORMATION_CLASS FileInformationClass;
228 PIO_STACK_LOCATION Stack;
229 PFILE_OBJECT FileObject;
230 PNTFS_FCB Fcb;
231 PVOID SystemBuffer;
232 ULONG BufferLength;
233 PIRP Irp;
234 PDEVICE_OBJECT DeviceObject;
235 NTSTATUS Status = STATUS_SUCCESS;
236
237 DPRINT1("NtfsQueryInformation(%p)\n", IrpContext);
238
239 Irp = IrpContext->Irp;
240 Stack = IrpContext->Stack;
241 DeviceObject = IrpContext->DeviceObject;
242 FileInformationClass = Stack->Parameters.QueryFile.FileInformationClass;
243 FileObject = IrpContext->FileObject;
244 Fcb = FileObject->FsContext;
245
246 SystemBuffer = Irp->AssociatedIrp.SystemBuffer;
247 BufferLength = Stack->Parameters.QueryFile.Length;
248
249 switch (FileInformationClass)
250 {
251 case FileStandardInformation:
252 Status = NtfsGetStandardInformation(Fcb,
253 DeviceObject,
254 SystemBuffer,
255 &BufferLength);
256 break;
257
258 case FilePositionInformation:
259 Status = NtfsGetPositionInformation(FileObject,
260 SystemBuffer,
261 &BufferLength);
262 break;
263
264 case FileBasicInformation:
265 Status = NtfsGetBasicInformation(FileObject,
266 Fcb,
267 DeviceObject,
268 SystemBuffer,
269 &BufferLength);
270 break;
271
272 case FileNameInformation:
273 Status = NtfsGetNameInformation(FileObject,
274 Fcb,
275 DeviceObject,
276 SystemBuffer,
277 &BufferLength);
278 break;
279
280 case FileInternalInformation:
281 Status = NtfsGetInternalInformation(Fcb,
282 SystemBuffer,
283 &BufferLength);
284 break;
285
286 case FileNetworkOpenInformation:
287 Status = NtfsGetNetworkOpenInformation(Fcb,
288 DeviceObject->DeviceExtension,
289 SystemBuffer,
290 &BufferLength);
291 break;
292
293 case FileAlternateNameInformation:
294 case FileAllInformation:
295 DPRINT1("Unimplemented information class %u\n", FileInformationClass);
296 Status = STATUS_NOT_IMPLEMENTED;
297 break;
298
299 default:
300 DPRINT1("Unimplemented information class %u\n", FileInformationClass);
301 Status = STATUS_INVALID_PARAMETER;
302 }
303
304 if (NT_SUCCESS(Status))
305 Irp->IoStatus.Information =
306 Stack->Parameters.QueryFile.Length - BufferLength;
307 else
308 Irp->IoStatus.Information = 0;
309
310 return Status;
311 }
312
313 /* EOF */