2 * Acess2 FAT12/16/32 Driver
3 * - By John Hodge (thePowersGang)
6 * - FAT-Specific node caching
13 extern tVFS_Node *FAT_int_CacheNode(tFAT_VolInfo *Disk, const tVFS_Node *Node);
16 tTime FAT_int_GetAcessTimestamp(Uint16 Date, Uint16 Time, Uint8 MS)
18 return MS * 10 + timestamp(
19 // Seconds Minutes Hours
20 (Time & 0x1F) * 2, (Time >> 5) & 0x3F, (Time >> 11) & 0x1F,
22 (Date & 0x1F) - 1, ((Date >> 5) & 0xF) - 1, 1980 + ((Date >> 9) & 0xFF)
26 void FAT_int_GetFATTimestamp(tTime AcessTimestamp, Uint16 *Date, Uint16 *Time, Uint8 *MS)
30 format_date(AcessTimestamp, &y, &m, &d, &h, &min, &s, &ms);
32 *Date = (d + 1) | ((m + 1) << 5) | ((y - 1980) << 9);
34 *Time = (s / 2) | (min << 5) | (h << 11);
36 *MS = (ms / 10) + (s & 1) * 100;
40 * \brief Creates a tVFS_Node structure for a given file entry
41 * \param Parent Parent directory VFS node
42 * \param Entry File table entry for the new node
44 tVFS_Node *FAT_int_CreateNode(tVFS_Node *Parent, fat_filetable *Entry)
48 tFAT_VolInfo *disk = Parent->ImplPtr;
50 ENTER("pParent pEntry", Parent, Entry);
51 LOG("disk = %p", disk);
53 if( (ret = FAT_int_GetNode(disk, Entry->cluster | (Entry->clusterHi<<16))) ) {
58 memset(&node, 0, sizeof(tVFS_Node));
61 // 0-27: Cluster, 32-59: Parent Cluster
62 node.Inode = Entry->cluster | (Entry->clusterHi<<16) | (Parent->Inode << 32);
63 LOG("node.Inode = %llx", node.Inode);
67 node.Size = Entry->size;
68 LOG("Entry->size = %i", Entry->size);
70 node.UID = 0; node.GID = 0;
74 if(Entry->attrib & ATTR_DIRECTORY) node.Flags |= VFS_FFLAG_DIRECTORY;
75 if(Entry->attrib & ATTR_READONLY) {
76 node.Flags |= VFS_FFLAG_READONLY;
77 node.ACLs = &gVFS_ACL_EveryoneRX; // R-XR-XR-X
80 node.ACLs = &gVFS_ACL_EveryoneRWX; // RWXRWXRWX
84 node.CTime = FAT_int_GetAcessTimestamp(Entry->cdate, Entry->ctime, Entry->ctimems);
85 node.MTime = FAT_int_GetAcessTimestamp(Entry->mdate, Entry->mtime, 0);
86 node.ATime = FAT_int_GetAcessTimestamp(Entry->adate, 0, 0);
89 if(node.Flags & VFS_FFLAG_DIRECTORY) {
90 //Log_Debug("FAT", "Directory %08x has size 0x%x", node.Inode, node.Size);
91 node.Type = &gFAT_DirType;
95 node.Type = &gFAT_FileType;
99 ret = FAT_int_CacheNode(disk, &node);
104 tVFS_Node *FAT_int_CreateIncompleteDirNode(tFAT_VolInfo *Disk, Uint32 Cluster)
106 if( Cluster == Disk->rootOffset )
107 return &Disk->rootNode;
109 // If the directory isn't in the cache, what do?
110 // - we want to lock it such that we don't collide, but don't want to put crap data in the cache
111 // - Put a temp node in with a flag that indicates it's incomplete?
113 Mutex_Acquire(&Disk->lNodeCache);
114 tFAT_CachedNode *cnode;
116 for(cnode = Disk->NodeCache; cnode; cnode = cnode->Next)
118 if( (cnode->Node.Inode & 0xFFFFFFFF) == Cluster ) {
119 cnode->Node.ReferenceCount ++;
120 Mutex_Release(&Disk->lNodeCache);
125 // Create a temporary node?
126 Log_Warning("FAT", "TODO: Impliment FAT_int_CreateIncompleteDirNode()");
128 Mutex_Release(&Disk->lNodeCache);
132 tVFS_Node *FAT_int_GetNode(tFAT_VolInfo *Disk, Uint32 Cluster)
134 if( Cluster == Disk->rootOffset )
135 return &Disk->rootNode;
136 Mutex_Acquire(&Disk->lNodeCache);
137 tFAT_CachedNode *cnode;
139 for(cnode = Disk->NodeCache; cnode; cnode = cnode->Next)
141 if( (cnode->Node.Inode & 0xFFFFFFFF) == Cluster ) {
142 cnode->Node.ReferenceCount ++;
143 Mutex_Release(&Disk->lNodeCache);
148 Mutex_Release(&Disk->lNodeCache);
152 tVFS_Node *FAT_int_CacheNode(tFAT_VolInfo *Disk, const tVFS_Node *Node)
154 tFAT_CachedNode *cnode, *prev = NULL;
155 Mutex_Acquire(&Disk->lNodeCache);
157 for(cnode = Disk->NodeCache; cnode; prev = cnode, cnode = cnode->Next )
159 if( cnode->Node.Inode == Node->Inode ) {
160 cnode->Node.ReferenceCount ++;
161 Mutex_Release(&Disk->lNodeCache);
166 cnode = malloc(sizeof(tFAT_CachedNode));
168 memcpy(&cnode->Node, Node, sizeof(tVFS_Node));
169 cnode->Node.ReferenceCount = 1;
174 Disk->NodeCache = cnode;
176 Mutex_Release(&Disk->lNodeCache);
180 int FAT_int_DerefNode(tVFS_Node *Node)
182 tFAT_VolInfo *Disk = Node->ImplPtr;
183 tFAT_CachedNode *cnode, *prev = NULL;
186 if( Node == &Disk->rootNode )
189 Mutex_Acquire(&Disk->lNodeCache);
190 Node->ReferenceCount --;
191 for(cnode = Disk->NodeCache; cnode; prev = cnode, cnode = cnode->Next )
193 if(Node == &cnode->Node) {
195 prev->Next = cnode->Next;
197 Disk->NodeCache = cnode->Next;
201 if(Node->ReferenceCount == 0 && cnode) {
202 // Already out of the list :)
203 free(cnode->Node.Data);
207 Mutex_Release(&Disk->lNodeCache);
216 void FAT_int_ClearNodeCache(tFAT_VolInfo *Disk)
218 // TODO: In theory when this is called, all handles will be closed