3 * By John Hodge (thePowersGang)
4 * This file is published under the terms of the Acess licence. See the
5 * file COPYING for details.
7 * dir.c - Directory Handling
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);
20 const tNTFS_IndexEntry_Filename *NTFS_int_IterateIndex(const char *Buf, size_t Len, int *Count)
22 const tNTFS_IndexEntry_Filename *ent;
23 for( size_t ofs = 0; ofs < Len; ofs += ent->EntrySize )
25 ent = (const void*)(Buf + ofs);
27 if( ofs + sizeof(*ent) > Len ) {
30 if( ent->EntrySize < sizeof(*ent) ) {
33 // End of block - move on to the next one
34 if( ent->IndexFlags & 0x02 ) {
35 LOG("end of block at ofs %x", ofs);
39 // A little hacky - hide all internal files
40 if( (ent->MFTReference & ((1ULL << 48)-1)) < 12 )
43 if( ent->Filename.FilenameNamespace == NTFS_FilenameNamespace_DOS )
54 * \brief Get the name of an indexed directory entry
56 int NTFS_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX])
58 tNTFS_Directory *dir = (void*)Node;
60 ENTER("XNode->Inode iPos", Node->Inode, Pos);
62 ASSERT(dir->I30Root->IsResident);
63 const tNTFS_Attrib_IndexRoot *idxroot = dir->I30Root->ResidentData;
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),
71 char buf[idxroot->AllocEntrySize];
74 while( !ent && (len = NTFS_ReadAttribData(dir->I30Allocation, vcn*sizeof(buf), sizeof(buf), buf)) )
77 struct sNTFS_IndexHeader *hdr = (void*)buf;
78 ASSERT(hdr->EntriesOffset + 0x18 < len);
79 // Apply update sequence
80 ASSERT(hdr->UpdateSequenceOfs + 2*hdr->UpdateSequenceSize <= len);
81 NTFS_int_ApplyUpdateSequence(buf,len, (void*)(buf+hdr->UpdateSequenceOfs), hdr->UpdateSequenceSize);
82 size_t ofs = hdr->EntriesOffset + 0x18;
83 ent = NTFS_int_IterateIndex(buf + ofs, len - ofs, &Pos);
91 // TODO: This is not future-proof
92 const Uint16 *name16 = ent->Filename.Filename;
93 UTF16_ConvertToUTF8(FILENAME_MAX, Dest, ent->Filename.FilenameLength, name16);
94 LOG("Filename '%s'", Dest);
99 typedef int (*tNTFS_BTreeSearch_CmpFcn)(const tNTFS_IndexEntry_Filename *Ent, size_t SLen, const void *Search);
101 int NTFS_BTreeSearch_CmpI30(const tNTFS_IndexEntry_Filename *Ent, size_t SLen, const void *Search)
104 size_t fname_len = Ent->Filename.FilenameLength*2;
105 size_t cmplen = MIN(fnamelen, SLen);
106 int ret = memcmp(Ent->Filename.Filename, Search, cmplen);
111 else if( cmplen == SLen )
116 //LOG("Cmp '%.*ls' == '%s'", Ent->Filename.FilenameLength, Ent->Filename.Filename, Search);
117 // return UTF16_CompareWithUTF8(Ent->Filename.FilenameLength, Ent->Filename.Filename, Search);
118 return UTF16_CompareWithUTF8CI(Ent->Filename.FilenameLength, Ent->Filename.Filename, Search);
122 Uint64 NTFS_BTreeSearch(size_t Length, const void *Data,
123 tNTFS_BTreeSearch_CmpFcn Cmp, size_t SLen, const void *Search)
125 void *buffer_end = (char*)Data + Length;
126 const tNTFS_IndexEntry_Filename *ent = Data;
127 while( !(ent->IndexFlags & 0x02) )
129 if( (void*)(&ent->_rsvd + 1) > buffer_end ) {
133 // TODO: Handle collations?
134 int cmp = Cmp(ent, SLen, Search);
136 LOG("Located at %p: 0x%016llx", ent, ent->MFTReference);
137 return ent->MFTReference & ((1ULL << 48)-1);
142 ent = (void*)((char*)ent + ent->EntrySize);
144 if( ent->IndexFlags & 0x01 ) {
145 LOG("Descend to VCN %llx", *(Uint64*)((char*)ent + ent->EntrySize - 8));
146 return (1ULL << 63) | *(Uint64*)((char*)ent + ent->EntrySize - 8);
152 #define _MFTREF_IDX(ref) ((ref) & ((1ULL<<48)-1))
153 #define _MFTREF_SEQ(ref) ((ref) >> 48)
155 void NTFS_int_DumpIndex(tNTFS_Attrib *Allocation, Uint AttribID)
157 ENTER("pAllocation xAttribID", Allocation, AttribID);
163 size_t block_size = MAX(2048, Allocation->Disk->ClusterSize);
164 char buf[block_size];
166 while( (len = NTFS_ReadAttribData(Allocation, vcn*block_size, block_size, buf)) )
168 struct sNTFS_IndexHeader *hdr = (void*)buf;
169 // Apply update sequence
170 ASSERT(hdr->UpdateSequenceOfs + 2*hdr->UpdateSequenceSize <= len);
171 NTFS_int_ApplyUpdateSequence(buf,len, (void*)(buf+hdr->UpdateSequenceOfs), hdr->UpdateSequenceSize);
173 LOG("VCN %x: Ofs=%x, Size=%x",
174 vcn, hdr->EntriesOffset, hdr->EntriesSize);
175 if( hdr->ThisVCN != vcn ) {
176 Log_Notice("NTFS", "Data error: Index header VCN mismatch (%x!=exp %x)", hdr->ThisVCN, vcn);
178 size_t ofs = hdr->EntriesOffset + 0x18;
179 while( ofs < hdr->EntriesSize + (hdr->EntriesOffset + 0x18) )
181 struct sNTFS_IndexEntry *ent = (void*)(buf + ofs);
182 if( ofs + sizeof(*ent) > len )
184 LOG("%03x: L=%02x,M=%02x,F=%02x, Ref=%x/%llx", ofs,
185 ent->EntrySize, ent->MessageLen, ent->IndexFlags,
186 _MFTREF_SEQ(ent->MFTReference), _MFTREF_IDX(ent->MFTReference));
188 if( ent->EntrySize < sizeof(*ent) ) {
189 Log_Notice("NTFS", "Data error: Index entry size too small");
192 if( ent->MessageLen + sizeof(*ent) > ent->EntrySize ) {
193 Log_Notice("NTFS", "Data error: Index entry message size > entry size");
196 if( ent->IndexFlags & NTFS_IndexFlag_HasSubNode )
198 if( ent->EntrySize < sizeof(*ent) + 8 ) {
199 Log_Notice("NTFS", "Data error: Index entry size too small (SubVCN)");
201 LOG("- SubVCN=%llx", *(Uint64*)((char*)ent + ent->EntrySize - 8));
203 if( ent->IndexFlags & NTFS_IndexFlag_IsLast )
209 struct sNTFS_Attrib_Filename *fname = (void*)(buf + ofs + sizeof(*ent));
210 LOG("- Filename: %i %.*ls",
211 fname->FilenameNamespace,
212 fname->FilenameLength, fname->Filename);
216 ofs += ent->EntrySize;
223 tVFS_Node *NTFS_int_CreateNode(tNTFS_Disk *Disk, Uint64 MFTEntry)
225 tNTFS_FILE_Header *ent = NTFS_GetMFT(Disk, MFTEntry);
226 if( !ent || !(ent->Flags & 0x01) )
232 tNTFS_Directory tpl_dir;
235 memset(&types, 0, sizeof(tVFS_Node));
236 if( ent->Flags & 0x02 )
239 size = sizeof(types.tpl_dir);
240 ret = &types.tpl_dir.Node;
241 ret->Type = &gNTFS_DirType;
242 ret->Flags = VFS_FFLAG_DIRECTORY;
244 types.tpl_dir.I30Root = NTFS_GetAttrib(Disk, MFTEntry, NTFS_FileAttrib_IndexRoot, "$I30", 0);
245 types.tpl_dir.I30Allocation = NTFS_GetAttrib(Disk, MFTEntry,
246 NTFS_FileAttrib_IndexAllocation, "$I30", 0);
248 ASSERT(types.tpl_dir.I30Root->IsResident);
250 Debug_HexDump("NTFS CreateNode Root",
251 types.tpl_dir.I30Root->ResidentData,
252 types.tpl_dir.I30Root->DataSize);
253 if( types.tpl_dir.I30Allocation ) {
254 NTFS_int_DumpIndex(types.tpl_dir.I30Allocation, 0x30);
259 if( MFTEntry == 0x1C )
261 char tmpbuf[Disk->ClusterSize];
262 NTFS_ReadAttribData(types.tpl_dir.I30Allocation, 0, sizeof(tmpbuf), tmpbuf);
263 Debug_HexDump("NTFS CreateNode VCN#0", tmpbuf, sizeof(tmpbuf));
264 NTFS_ReadAttribData(types.tpl_dir.I30Allocation, sizeof(tmpbuf), sizeof(tmpbuf), tmpbuf);
265 Debug_HexDump("NTFS CreateNode VCN#1", tmpbuf, sizeof(tmpbuf));
272 size = sizeof(types.tpl_file);
273 ret = &types.tpl_file.Node;
274 ret->Type = &gNTFS_FileType;
275 types.tpl_file.Data = NTFS_GetAttrib(Disk, MFTEntry, NTFS_FileAttrib_Data, "", 0);
277 ret->Inode = MFTEntry;
282 NTFS_ReleaseMFT(Disk, MFTEntry, ent);
283 return Inode_CacheNodeEx(Disk->InodeCache, ret, size);
287 * \brief Get an entry from a directory by name
289 tVFS_Node *NTFS_FindDir(tVFS_Node *Node, const char *Name, Uint Flags)
291 tNTFS_Directory *dir = (void*)Node;
292 tNTFS_Disk *disk = Node->ImplPtr;
293 ASSERT(dir->I30Root->IsResident);
294 const tNTFS_Attrib_IndexRoot *idxroot = dir->I30Root->ResidentData;
296 ENTER("XNode->Inode sName xFlags",
297 Node->Inode, Name, Flags);
300 size_t name16len = UTF16_ConvertFromUTF8(0, NULL, Name);
301 Uint16 name16[name16len+1];
302 UTF16_ConvertFromUTF8(name16len+1, name16, Name);
307 // Check resident first
308 size_t ofs = 0x10 + idxroot->FirstEntryOfs;
309 mftent = NTFS_BTreeSearch(
310 dir->I30Root->DataSize - ofs, dir->I30Root->ResidentData + ofs,
311 NTFS_BTreeSearch_CmpI30, -1, Name
313 while( mftent & (1ULL << 63) )
315 size_t unit_len = idxroot->AllocEntrySize;
316 char buf[ unit_len ];
317 size_t ofs = (mftent & 0xFFFFFF) * unit_len;
318 size_t len = NTFS_ReadAttribData(dir->I30Allocation, ofs, sizeof(buf), buf);
321 tNTFS_IndexHeader *hdr = (void*)buf;
322 // Apply update sequence
323 ASSERT(hdr->UpdateSequenceOfs + 2*hdr->UpdateSequenceSize <= len);
324 NTFS_int_ApplyUpdateSequence(buf,len, (void*)(buf+hdr->UpdateSequenceOfs), hdr->UpdateSequenceSize);
326 //mftent = NTFS_BTreeSearch(len, (void*)buf, NTFS_BTreeSearch_CmpI30, name16len*2, name16);
327 mftent = NTFS_BTreeSearch(
328 len-(hdr->EntriesOffset+0x18), buf+hdr->EntriesOffset+0x18,
329 NTFS_BTreeSearch_CmpI30, -1, Name
339 tVFS_Node *ret = Inode_GetCache(disk->InodeCache, mftent);
341 ret = NTFS_int_CreateNode(disk, mftent);