sync with trunk r46493
[reactos.git] / drivers / filesystems / ntfs / attrib.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002,2003 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/attrib.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMER: Eric Kohl
24 * Updated by Valentin Verkhovsky 2003/09/12
25 */
26
27 /* INCLUDES *****************************************************************/
28
29 #include "ntfs.h"
30
31 #define NDEBUG
32 #include <debug.h>
33
34 /* GLOBALS *****************************************************************/
35
36
37 /* FUNCTIONS ****************************************************************/
38
39
40
41 static ULONG
42 RunLength(PUCHAR run)
43 {
44 return(*run & 0x0f) + ((*run >> 4) & 0x0f) + 1;
45 }
46
47
48 static LONGLONG
49 RunLCN(PUCHAR run)
50 {
51 UCHAR n1 = *run & 0x0f;
52 UCHAR n2 = (*run >> 4) & 0x0f;
53 LONGLONG lcn = (n2 == 0) ? 0 : (CHAR)(run[n1 + n2]);
54 LONG i = 0;
55
56 for (i = n1 +n2 - 1; i > n1; i--)
57 lcn = (lcn << 8) + run[i];
58 return lcn;
59 }
60
61
62
63 static ULONGLONG
64 RunCount(PUCHAR run)
65 {
66 UCHAR n = *run & 0xf;
67 ULONGLONG count = 0;
68 ULONG i = 0;
69
70 for (i = n; i > 0; i--)
71 count = (count << 8) + run[i];
72 return count;
73 }
74
75
76 BOOLEAN
77 FindRun (PNONRESIDENT_ATTRIBUTE NresAttr,
78 ULONGLONG vcn,
79 PULONGLONG lcn,
80 PULONGLONG count)
81 {
82 PUCHAR run;
83
84 ULONGLONG base = NresAttr->StartVcn;
85
86 if (vcn < NresAttr->StartVcn || vcn > NresAttr->LastVcn)
87 return FALSE;
88
89 *lcn = 0;
90
91 for (run = (PUCHAR)((ULONG_PTR)NresAttr + NresAttr->RunArrayOffset);
92 *run != 0; run += RunLength(run))
93 {
94 *lcn += RunLCN(run);
95 *count = RunCount(run);
96
97 if (base <= vcn && vcn < base + *count)
98 {
99 *lcn = (RunLCN(run) == 0) ? 0 : *lcn + vcn - base;
100 *count -= (ULONG)(vcn - base);
101
102 return TRUE;
103 }
104 else
105 {
106 base += *count;
107 }
108 }
109
110 return FALSE;
111 }
112
113
114 static VOID
115 NtfsDumpFileNameAttribute(PATTRIBUTE Attribute)
116 {
117 PRESIDENT_ATTRIBUTE ResAttr;
118 PFILENAME_ATTRIBUTE FileNameAttr;
119
120 DbgPrint(" $FILE_NAME ");
121
122 ResAttr = (PRESIDENT_ATTRIBUTE)Attribute;
123 // DbgPrint(" Length %lu Offset %hu ", ResAttr->ValueLength, ResAttr->ValueOffset);
124
125 FileNameAttr = (PFILENAME_ATTRIBUTE)((ULONG_PTR)ResAttr + ResAttr->ValueOffset);
126 DbgPrint(" '%.*S' ", FileNameAttr->NameLength, FileNameAttr->Name);
127 }
128
129
130 static VOID
131 NtfsDumpVolumeNameAttribute(PATTRIBUTE Attribute)
132 {
133 PRESIDENT_ATTRIBUTE ResAttr;
134 PWCHAR VolumeName;
135
136 DbgPrint(" $VOLUME_NAME ");
137
138 ResAttr = (PRESIDENT_ATTRIBUTE)Attribute;
139 // DbgPrint(" Length %lu Offset %hu ", ResAttr->ValueLength, ResAttr->ValueOffset);
140
141 VolumeName = (PWCHAR)((ULONG_PTR)ResAttr + ResAttr->ValueOffset);
142 DbgPrint(" '%.*S' ", ResAttr->ValueLength / sizeof(WCHAR), VolumeName);
143 }
144
145
146 static VOID
147 NtfsDumpVolumeInformationAttribute(PATTRIBUTE Attribute)
148 {
149 PRESIDENT_ATTRIBUTE ResAttr;
150 PVOLINFO_ATTRIBUTE VolInfoAttr;
151
152 DbgPrint(" $VOLUME_INFORMATION ");
153
154 ResAttr = (PRESIDENT_ATTRIBUTE)Attribute;
155 // DbgPrint(" Length %lu Offset %hu ", ResAttr->ValueLength, ResAttr->ValueOffset);
156
157 VolInfoAttr = (PVOLINFO_ATTRIBUTE)((ULONG_PTR)ResAttr + ResAttr->ValueOffset);
158 DbgPrint(" NTFS Version %u.%u Flags 0x%04hx ",
159 VolInfoAttr->MajorVersion,
160 VolInfoAttr->MinorVersion,
161 VolInfoAttr->Flags);
162 }
163
164
165 static VOID
166 NtfsDumpAttribute (PATTRIBUTE Attribute)
167 {
168 PNONRESIDENT_ATTRIBUTE NresAttr;
169 UNICODE_STRING Name;
170
171 ULONGLONG lcn = 0;
172 ULONGLONG runcount = 0;
173
174 switch (Attribute->AttributeType)
175 {
176 case AttributeFileName:
177 NtfsDumpFileNameAttribute(Attribute);
178 break;
179
180 case AttributeStandardInformation:
181 DbgPrint(" $STANDARD_INFORMATION ");
182 break;
183
184 case AttributeAttributeList:
185 DbgPrint(" $ATTRIBUTE_LIST ");
186 break;
187
188 case AttributeObjectId:
189 DbgPrint(" $OBJECT_ID ");
190 break;
191
192 case AttributeSecurityDescriptor:
193 DbgPrint(" $SECURITY_DESCRIPTOR ");
194 break;
195
196 case AttributeVolumeName:
197 NtfsDumpVolumeNameAttribute(Attribute);
198 break;
199
200 case AttributeVolumeInformation:
201 NtfsDumpVolumeInformationAttribute(Attribute);
202 break;
203
204 case AttributeData:
205 DbgPrint(" $DATA ");
206 //DataBuf = ExAllocatePool(NonPagedPool,AttributeLengthAllocated(Attribute));
207 break;
208
209 case AttributeIndexRoot:
210 DbgPrint(" $INDEX_ROOT ");
211 break;
212
213 case AttributeIndexAllocation:
214 DbgPrint(" $INDEX_ALLOCATION ");
215 break;
216
217 case AttributeBitmap:
218 DbgPrint(" $BITMAP ");
219 break;
220
221 case AttributeReparsePoint:
222 DbgPrint(" $REPARSE_POINT ");
223 break;
224
225 case AttributeEAInformation:
226 DbgPrint(" $EA_INFORMATION ");
227 break;
228
229 case AttributeEA:
230 DbgPrint(" $EA ");
231 break;
232
233 case AttributePropertySet:
234 DbgPrint(" $PROPERTY_SET ");
235 break;
236
237 case AttributeLoggedUtilityStream:
238 DbgPrint(" $LOGGED_UTILITY_STREAM ");
239 break;
240
241 default:
242 DbgPrint(" Attribute %lx ",
243 Attribute->AttributeType);
244 break;
245 }
246
247 if (Attribute->NameLength != 0)
248 {
249 Name.Length = Attribute->NameLength * sizeof(WCHAR);
250 Name.MaximumLength = Name.Length;
251 Name.Buffer = (PWCHAR)((ULONG_PTR)Attribute + Attribute->NameOffset);
252
253 DbgPrint("'%wZ' ", &Name);
254 }
255
256 DbgPrint("(%s)\n",
257 Attribute->Nonresident ? "non-resident" : "resident");
258
259 if (Attribute->Nonresident)
260 {
261 NresAttr = (PNONRESIDENT_ATTRIBUTE)Attribute;
262
263 FindRun (NresAttr,0,&lcn, &runcount);
264
265 DbgPrint (" AllocatedSize %I64u DataSize %I64u\n",
266 NresAttr->AllocatedSize, NresAttr->DataSize);
267 DbgPrint (" logical clusters: %I64u - %I64u\n",
268 lcn, lcn + runcount - 1);
269 }
270 }
271
272
273 VOID
274 NtfsDumpFileAttributes (PFILE_RECORD_HEADER FileRecord)
275 {
276 PATTRIBUTE Attribute;
277
278 Attribute = (PATTRIBUTE)((ULONG_PTR)FileRecord + FileRecord->AttributeOffset);
279 while (Attribute < (PATTRIBUTE)((ULONG_PTR)FileRecord + FileRecord->BytesInUse) &&
280 Attribute->AttributeType != (ATTRIBUTE_TYPE)-1)
281 {
282 NtfsDumpAttribute (Attribute);
283
284 Attribute = (PATTRIBUTE)((ULONG_PTR)Attribute + Attribute->Length);
285 }
286 }
287
288 /* EOF */