Modules/NTFS - ReadDir/FindDir appear to work well
[tpg/acess2.git] / KernelLand / Modules / Filesystems / NTFS / dir.c
1 /*
2  * Acess2 - NTFS Driver
3  * By John Hodge (thePowersGang)
4  * This file is published under the terms of the Acess licence. See the
5  * file COPYING for details.
6  *
7  * dir.c - Directory Handling
8  */
9 #define DEBUG   1
10 #include "common.h"
11 //#include "index.h"
12 #include <utf16.h>
13
14 // === PROTOTYPES ===
15  int    NTFS_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX]);
16 tVFS_Node       *NTFS_FindDir(tVFS_Node *Node, const char *Name, Uint Flags);
17 Uint64  NTFS_int_IndexLookup(Uint64 Inode, const char *IndexName, const char *Str);
18
19 // === CODE ===
20 const tNTFS_IndexEntry_Filename *NTFS_int_IterateIndex(const char *Buf, size_t Len, int *Count)
21 {
22         const tNTFS_IndexEntry_Filename *ent;
23         for( size_t ofs = 0; ofs < Len; ofs += ent->EntrySize )
24         {
25                 ent = (const void*)(Buf + ofs);
26
27                 if( ofs + sizeof(*ent) > Len ) {
28                         break;
29                 }
30                 if( ent->EntrySize < sizeof(*ent) ) {
31                         break;
32                 }
33                 // End of block - move on to the next one
34                 if( ent->IndexFlags & 0x02 ) {
35                         LOG("end of block at ofs %x", ofs);
36                         break;
37                 }
38                 
39                 // A little hacky - hide all internal files
40                 if( (ent->MFTReference & ((1ULL << 48)-1)) < 12 )
41                         continue;
42                 // Skip DOS filenames
43                 if( ent->Filename.FilenameNamespace == NTFS_FilenameNamespace_DOS )
44                         continue ;
45                 
46                 // Located
47                 if( (*Count)-- <= 0 )
48                         return ent;
49         }
50         return NULL;
51 }
52
53 /**
54  * \brief Get the name of an indexed directory entry
55  */
56 int NTFS_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX])
57 {
58         tNTFS_Directory *dir = (void*)Node;
59
60         ENTER("XNode->Inode iPos", Node->Inode, Pos);
61
62         ASSERT(dir->I30Root->IsResident);
63         const tNTFS_Attrib_IndexRoot    *idxroot = dir->I30Root->ResidentData;
64
65         // Check resident root
66         const tNTFS_IndexEntry_Filename *ent;
67         ent = NTFS_int_IterateIndex(
68                 (char*)idxroot + (0x10 + idxroot->FirstEntryOfs),
69                 dir->I30Root->DataSize - (0x10 + idxroot->FirstEntryOfs),
70                 &Pos);
71         char buf[idxroot->AllocEntrySize];
72         size_t  vcn = 0;
73         size_t  len;
74         while( !ent && (len = NTFS_ReadAttribData(dir->I30Allocation, vcn*sizeof(buf), sizeof(buf), buf)) )
75         {
76                 // Check allocation
77                 struct sNTFS_IndexHeader *hdr = (void*)buf;
78                 ASSERT(hdr->EntriesOffset + 0x18 < len);
79                 size_t  ofs = hdr->EntriesOffset + 0x18;
80                 ent = NTFS_int_IterateIndex(buf + ofs, len - ofs, &Pos);
81                 vcn ++;
82         }
83         if( !ent ) {
84                 LEAVE('i', 1);
85                 return -1;
86         }
87
88         // TODO: This is not future-proof
89         const Uint16    *name16 = ent->Filename.Filename;
90         UTF16_ConvertToUTF8(FILENAME_MAX, Dest, ent->Filename.FilenameLength, name16);
91         LOG("Filename '%s'", Dest);
92         LEAVE('i', 0);
93         return 0;
94 }
95
96 typedef int (*tNTFS_BTreeSearch_CmpFcn)(const tNTFS_IndexEntry_Filename *Ent, size_t SLen, const void *Search);
97
98 int NTFS_BTreeSearch_CmpI30(const tNTFS_IndexEntry_Filename *Ent, size_t SLen, const void *Search)
99 {
100         #if 0
101         size_t  fname_len = Ent->Filename.FilenameLength*2;
102         size_t  cmplen = MIN(fnamelen, SLen);
103         int ret = memcmp(Ent->Filename.Filename, Search, cmplen);
104         if( ret != 0 )
105                 return ret;
106         if( cmplen < SLen )
107                 return -1;
108         else if( cmplen == SLen )
109                 return 0;
110         else
111                 return 1;
112         #else
113         //LOG("Cmp '%.*ls' == '%s'", Ent->Filename.FilenameLength, Ent->Filename.Filename, Search);
114 //      return UTF16_CompareWithUTF8(Ent->Filename.FilenameLength, Ent->Filename.Filename, Search);
115         return UTF16_CompareWithUTF8CI(Ent->Filename.FilenameLength, Ent->Filename.Filename, Search);
116         #endif
117 }
118
119 Uint64 NTFS_BTreeSearch(size_t Length, const void *Data,
120         tNTFS_BTreeSearch_CmpFcn Cmp, size_t SLen, const void *Search)
121 {
122         void    *buffer_end = (char*)Data + Length;
123         const tNTFS_IndexEntry_Filename *ent = Data;
124         while( !(ent->IndexFlags & 0x02) )
125         {
126                 if( (void*)(&ent->_rsvd + 1) > buffer_end ) {
127                         // on-disk error
128                         return 0;
129                 }
130                 // TODO: Handle collations?
131                 int cmp = Cmp(ent, SLen, Search);
132                 if( cmp == 0 ) {
133                         LOG("Located at %p: 0x%016llx", ent, ent->MFTReference);
134                         return ent->MFTReference & ((1ULL << 48)-1);
135                 }
136                 if( cmp > 0 )
137                         break;
138                 
139                 ent = (void*)((char*)ent + ent->EntrySize);
140         }
141         if( ent->IndexFlags & 0x01 ) {
142                 LOG("Descend to VCN %llx", *(Uint64*)((char*)ent + ent->EntrySize - 8));
143                 return (1ULL << 63) | *(Uint64*)((char*)ent + ent->EntrySize - 8);
144         }
145         LOG("Not found");
146         return 0;
147 }
148
149 #define _MFTREF_IDX(ref)        ((ref) & ((1ULL<<48)-1))
150 #define _MFTREF_SEQ(ref)        ((ref) >> 48)
151
152 void NTFS_int_DumpIndex(tNTFS_Attrib *Allocation, Uint AttribID)
153 {
154         ENTER("pAllocation xAttribID", Allocation, AttribID);
155         if(!Allocation) {
156                 LEAVE('-');
157                 return ;
158         }
159         Uint32  vcn = 0;
160         size_t  block_size = MAX(2048, Allocation->Disk->ClusterSize);
161         char    buf[block_size];
162         size_t  len;
163         while( (len = NTFS_ReadAttribData(Allocation, vcn*block_size, block_size, buf)) )
164         {
165                 struct sNTFS_IndexHeader        *hdr = (void*)buf;
166                 LOG("VCN %x: Ofs=%x, Size=%x",
167                         vcn, hdr->EntriesOffset, hdr->EntriesSize);
168                 if( hdr->ThisVCN != vcn ) {
169                         Log_Notice("NTFS", "Data error: Index header VCN mismatch (%x!=exp %x)", hdr->ThisVCN, vcn);
170                 }
171                 size_t  ofs = hdr->EntriesOffset + 0x18;
172                 while( ofs < hdr->EntriesSize + (hdr->EntriesOffset + 0x18) )
173                 {
174                         struct sNTFS_IndexEntry *ent = (void*)(buf + ofs);
175                         if( ofs + sizeof(*ent) > len )
176                                 break;
177                         LOG("%03x: L=%02x,M=%02x,F=%02x, Ref=%x/%llx", ofs,
178                                 ent->EntrySize, ent->MessageLen, ent->IndexFlags,
179                                 _MFTREF_SEQ(ent->MFTReference), _MFTREF_IDX(ent->MFTReference));
180
181                         if( ent->EntrySize < sizeof(*ent) ) {
182                                 Log_Notice("NTFS", "Data error: Index entry size too small");
183                                 break ;
184                         }
185                         if( ent->MessageLen + sizeof(*ent) > ent->EntrySize ) {
186                                 Log_Notice("NTFS", "Data error: Index entry message size > entry size");
187                         }
188                         
189                         if( ent->IndexFlags & NTFS_IndexFlag_HasSubNode )
190                         {
191                                 if( ent->EntrySize < sizeof(*ent) + 8 ) {
192                                         Log_Notice("NTFS", "Data error: Index entry size too small (SubVCN)");
193                                 }
194                                 LOG("- SubVCN=%llx", *(Uint64*)((char*)ent + ent->EntrySize - 8));
195                         }
196                         if( ent->IndexFlags & NTFS_IndexFlag_IsLast )
197                                 break;
198                         
199                         switch(AttribID)
200                         {
201                         case 0x30: {
202                                 struct sNTFS_Attrib_Filename    *fname = (void*)(buf + ofs + sizeof(*ent));
203                                 LOG("- Filename: %i %.*ls",
204                                         fname->FilenameNamespace,
205                                         fname->FilenameLength, fname->Filename);
206                                 break; }
207                         }
208
209                         ofs += ent->EntrySize;
210                 }
211                 vcn ++;
212         }
213         LEAVE('-');
214 }
215
216 tVFS_Node *NTFS_int_CreateNode(tNTFS_Disk *Disk, Uint64 MFTEntry)
217 {
218         tNTFS_FILE_Header       *ent = NTFS_GetMFT(Disk, MFTEntry);
219         if( !ent || !(ent->Flags & 0x01) )
220                 return NULL;    
221
222         tVFS_Node       *ret;
223         size_t  size;
224         union {
225                 tNTFS_Directory tpl_dir;
226                 tNTFS_File      tpl_file;
227         } types;
228         memset(&types, 0, sizeof(tVFS_Node));
229         if( ent->Flags & 0x02 )
230         {
231                 // Directory
232                 size = sizeof(types.tpl_dir);
233                 ret = &types.tpl_dir.Node;
234                 ret->Type = &gNTFS_DirType;
235                 ret->Flags = VFS_FFLAG_DIRECTORY;
236                 
237                 types.tpl_dir.I30Root = NTFS_GetAttrib(Disk, MFTEntry, NTFS_FileAttrib_IndexRoot, "$I30", 0);
238                 types.tpl_dir.I30Allocation = NTFS_GetAttrib(Disk, MFTEntry,
239                         NTFS_FileAttrib_IndexAllocation, "$I30", 0);
240                 
241                 ASSERT(types.tpl_dir.I30Root->IsResident);
242                 #if 0
243                 Debug_HexDump("NTFS CreateNode Root",
244                         types.tpl_dir.I30Root->ResidentData,
245                         types.tpl_dir.I30Root->DataSize);
246                 if( types.tpl_dir.I30Allocation ) {
247                         NTFS_int_DumpIndex(types.tpl_dir.I30Allocation, 0x30);
248                 }
249                 #endif
250         
251                 #if 0
252                 if( MFTEntry == 0x1C )
253                 {       
254                         char tmpbuf[Disk->ClusterSize];
255                         NTFS_ReadAttribData(types.tpl_dir.I30Allocation, 0, sizeof(tmpbuf), tmpbuf);
256                         Debug_HexDump("NTFS CreateNode VCN#0", tmpbuf, sizeof(tmpbuf));
257                         NTFS_ReadAttribData(types.tpl_dir.I30Allocation, sizeof(tmpbuf), sizeof(tmpbuf), tmpbuf);
258                         Debug_HexDump("NTFS CreateNode VCN#1", tmpbuf, sizeof(tmpbuf));
259                 }
260                 #endif
261         }
262         else
263         {
264                 // File
265                 size = sizeof(types.tpl_file);
266                 ret = &types.tpl_file.Node;
267                 ret->Type = &gNTFS_FileType;
268                 types.tpl_file.Data = NTFS_GetAttrib(Disk, MFTEntry, NTFS_FileAttrib_Data, "", 0); 
269         }
270         ret->Inode = MFTEntry;
271         ret->ImplPtr = Disk;
272
273         // TODO: Permissions
274         
275         NTFS_ReleaseMFT(Disk, MFTEntry, ent);
276         return Inode_CacheNodeEx(Disk->InodeCache, ret, size);
277 }
278
279 /**
280  * \brief Get an entry from a directory by name
281  */
282 tVFS_Node *NTFS_FindDir(tVFS_Node *Node, const char *Name, Uint Flags)
283 {
284         tNTFS_Directory *dir = (void*)Node;
285         tNTFS_Disk      *disk = Node->ImplPtr;
286         ASSERT(dir->I30Root->IsResident);
287         const tNTFS_Attrib_IndexRoot    *idxroot = dir->I30Root->ResidentData;
288
289         ENTER("XNode->Inode sName xFlags",
290                 Node->Inode, Name, Flags);
291
292         #if 0
293         size_t  name16len = UTF16_ConvertFromUTF8(0, NULL, Name);
294         Uint16  name16[name16len+1];
295         UTF16_ConvertFromUTF8(name16len+1, name16, Name);
296         #endif
297
298         Uint64  mftent = 0;
299
300         // Check resident first
301         size_t  ofs = 0x10 + idxroot->FirstEntryOfs;
302         mftent = NTFS_BTreeSearch(
303                 dir->I30Root->DataSize - ofs, dir->I30Root->ResidentData + ofs,
304                 NTFS_BTreeSearch_CmpI30, -1, Name
305                 );
306         while( mftent & (1ULL << 63) )
307         {
308                 size_t  unit_len = idxroot->AllocEntrySize;
309                 char buf[ unit_len ];
310                 size_t ofs = (mftent & 0xFFFFFF) * unit_len;
311                 size_t len = NTFS_ReadAttribData(dir->I30Allocation, ofs, sizeof(buf), buf);
312                 if( len == 0 )
313                         break;
314                 tNTFS_IndexHeader       *hdr = (void*)buf;
315                 //mftent = NTFS_BTreeSearch(len, (void*)buf, NTFS_BTreeSearch_CmpI30, name16len*2, name16);
316                 mftent = NTFS_BTreeSearch(
317                         len-(hdr->EntriesOffset+0x18), buf+hdr->EntriesOffset+0x18,
318                         NTFS_BTreeSearch_CmpI30, -1, Name
319                         );
320         }
321         
322         if( !mftent ) {
323                 LEAVE('n');
324                 return NULL;
325         }
326         
327         // Allocate node
328         tVFS_Node       *ret = Inode_GetCache(disk->InodeCache, mftent);
329         if(!ret)
330                 ret = NTFS_int_CreateNode(disk, mftent);
331         LEAVE('p', ret);
332         return ret;
333 }
334

UCC git Repository :: git.ucc.asn.au