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?
127 Mutex_Release(&Disk->lNodeCache);
131 tVFS_Node *FAT_int_GetNode(tFAT_VolInfo *Disk, Uint32 Cluster)
133 if( Cluster == Disk->rootOffset )
134 return &Disk->rootNode;
135 Mutex_Acquire(&Disk->lNodeCache);
136 tFAT_CachedNode *cnode;
138 for(cnode = Disk->NodeCache; cnode; cnode = cnode->Next)
140 if( (cnode->Node.Inode & 0xFFFFFFFF) == Cluster ) {
141 cnode->Node.ReferenceCount ++;
142 Mutex_Release(&Disk->lNodeCache);
147 Mutex_Release(&Disk->lNodeCache);
151 tVFS_Node *FAT_int_CacheNode(tFAT_VolInfo *Disk, const tVFS_Node *Node)
153 tFAT_CachedNode *cnode, *prev = NULL;
154 Mutex_Acquire(&Disk->lNodeCache);
156 for(cnode = Disk->NodeCache; cnode; prev = cnode, cnode = cnode->Next )
158 if( cnode->Node.Inode == Node->Inode ) {
159 cnode->Node.ReferenceCount ++;
160 Mutex_Release(&Disk->lNodeCache);
165 cnode = malloc(sizeof(tFAT_CachedNode));
167 memcpy(&cnode->Node, Node, sizeof(tVFS_Node));
168 cnode->Node.ReferenceCount = 1;
173 Disk->NodeCache = cnode;
175 Mutex_Release(&Disk->lNodeCache);
179 int FAT_int_DerefNode(tVFS_Node *Node)
181 tFAT_VolInfo *Disk = Node->ImplPtr;
182 tFAT_CachedNode *cnode, *prev = NULL;
185 if( Node == &Disk->rootNode )
188 Mutex_Acquire(&Disk->lNodeCache);
189 Node->ReferenceCount --;
190 for(cnode = Disk->NodeCache; cnode; prev = cnode, cnode = cnode->Next )
192 if(Node == &cnode->Node) {
194 prev->Next = cnode->Next;
196 Disk->NodeCache = cnode->Next;
200 if(Node->ReferenceCount == 0 && cnode) {
201 // Already out of the list :)
202 free(cnode->Node.Data);
206 Mutex_Release(&Disk->lNodeCache);
215 void FAT_int_ClearNodeCache(tFAT_VolInfo *Disk)
217 // TODO: In theory when this is called, all handles will be closed