2 * Acess2 FAT12/16/32 Driver
3 * - By John Hodge (thePowersGang)
6 * - Directory access/manipulation code
14 void FAT_int_ProperFilename(char *dest, const char *src);
15 int FAT_int_CreateName(fat_filetable *ft, const Uint16 *LongFileName, char *Dest);
16 int FAT_int_ConvertUTF16_to_UTF8(Uint8 *Dest, const Uint16 *Source);
17 int FAT_int_ConvertUTF8_to_UTF16(Uint16 *Dest, const Uint8 *Source);
19 int FAT_int_GetEntryByName(tVFS_Node *DirNode, const char *Name, fat_filetable *Entry);
20 int FAT_int_GetEntryByCluster(tVFS_Node *DirNode, Uint32 Cluster, fat_filetable *Entry);
21 int FAT_int_ReadDirSector(tVFS_Node *Node, int Sector, fat_filetable *Buffer);
23 int FAT_int_WriteDirEntry(tVFS_Node *Node, int ID, fat_filetable *Entry);
26 Uint16 *FAT_int_GetLFN(tVFS_Node *Node, int ID);
27 void FAT_int_DelLFN(tVFS_Node *Node, int ID);
29 int FAT_ReadDir(tVFS_Node *Node, int ID, char Dest[FILENAME_MAX]);
30 tVFS_Node *FAT_FindDir(tVFS_Node *Node, const char *Name, Uint Flags);
31 tVFS_Node *FAT_GetNodeFromINode(tVFS_Node *Root, Uint64 Inode);
33 tVFS_Node *FAT_Mknod(tVFS_Node *Node, const char *Name, Uint Flags);
34 int FAT_int_IsValid83Filename(const char *Name);
35 int FAT_Link(tVFS_Node *DirNode, const char *NewName, tVFS_Node *Node);
36 int FAT_Relink(tVFS_Node *node, const char *OldName, const char *NewName);
42 * \brief Converts a FAT directory entry name into a proper filename
43 * \param dest Destination array (must be at least 13 bytes in size)
44 * \param src 8.3 filename (concatenated, e.g 'FILE1 TXT')
46 void FAT_int_ProperFilename(char *dest, const char *src)
52 for( inpos = 0; inpos < 8; inpos++ ) {
53 if(src[inpos] == ' ') break;
54 dest[outpos++] = src[inpos];
57 // Check for empty extensions
61 for( ; inpos < 11; inpos++) {
62 if(src[inpos] == ' ') break;
63 dest[outpos++] = src[inpos];
66 dest[outpos++] = '\0';
68 //LOG("dest='%s'", dest);
72 * \fn char *FAT_int_CreateName(fat_filetable *ft, Uint8 *LongFileName)
73 * \brief Converts either a LFN or a 8.3 Name into a proper name
74 * \param ft Pointer to the file's entry in the parent directory
75 * \param LongFileName Long file name pointer
76 * \return Filename as a heap string
78 int FAT_int_CreateName(fat_filetable *ft, const Uint16 *LongFileName, char *Dest)
80 ENTER("pft sLongFileName", ft, LongFileName);
82 if(LongFileName && LongFileName[0] != 0)
84 int len = FAT_int_ConvertUTF16_to_UTF8(NULL, LongFileName);
85 if( len > FILENAME_MAX ) {
89 FAT_int_ConvertUTF16_to_UTF8((Uint8*)Dest, LongFileName);
94 FAT_int_ProperFilename(Dest, ft->name);
103 int FAT_int_CompareUTF16_UTF8(const Uint16 *Str16, const char *Str8)
105 int pos16 = 0, pos8 = 0;
106 const Uint8 *str8 = (const Uint8 *)Str8;
108 while( Str16[pos16] && str8[pos8] )
111 if( Str16[pos16] & 0x8000 ) {
119 pos8 += ReadUTF8(str8 + pos8, &cp8);
121 if(cp16 == cp8) continue ;
128 if(Str16[pos16] == str8[pos8])
130 if(Str16[pos16] < str8[pos8])
136 int FAT_int_ConvertUTF16_to_UTF8(Uint8 *Dest, const Uint16 *Source)
139 for( ; *Source; Source ++ )
141 // TODO: Decode/Reencode
151 int FAT_int_ConvertUTF8_to_UTF16(Uint16 *Dest, const Uint8 *Source)
159 cpl = ReadUTF8(Source, &cp);
174 int FAT_int_ParseLFN(const fat_filetable *Entry, Uint16 *Buffer)
176 const fat_longfilename *lfnInfo;
179 lfnInfo = (const void*)Entry;
181 if(lfnInfo->id & 0x40) {
182 memset(Buffer, 0, 256*2);
184 ofs = (lfnInfo->id & 0x3F) * 13 - 1;
188 Buffer[ofs--] = lfnInfo->name3[1]; Buffer[ofs--] = lfnInfo->name3[0];
189 Buffer[ofs--] = lfnInfo->name2[5]; Buffer[ofs--] = lfnInfo->name2[4];
190 Buffer[ofs--] = lfnInfo->name2[3]; Buffer[ofs--] = lfnInfo->name2[2];
191 Buffer[ofs--] = lfnInfo->name2[1]; Buffer[ofs--] = lfnInfo->name2[0];
192 Buffer[ofs--] = lfnInfo->name1[4]; Buffer[ofs--] = lfnInfo->name1[3];
193 Buffer[ofs--] = lfnInfo->name1[2]; Buffer[ofs--] = lfnInfo->name1[1];
194 Buffer[ofs--] = lfnInfo->name1[0];
196 if((lfnInfo->id&0x3F) == 1)
202 int FAT_int_GetEntryByName(tVFS_Node *DirNode, const char *Name, fat_filetable *Entry)
204 fat_filetable fileinfo[16];
211 ENTER("pDirNode sName pEntry", DirNode, Name, Entry);
213 for( int i = 0; ; i++ )
216 if(FAT_int_ReadDirSector(DirNode, i/16, fileinfo))
223 //Check if the files are free
224 if(fileinfo[i&0xF].name[0] == '\0') break; // End of List marker
225 if(fileinfo[i&0xF].name[0] == '\xE5') continue; // Free entry
229 // Long File Name Entry
230 if(fileinfo[i & 0xF].attrib == ATTR_LFN)
232 if( FAT_int_ParseLFN(&fileinfo[i&0xF], lfn) )
236 // Remove LFN if it does not apply
237 if(lfnId != i) lfn[0] = 0;
239 if(fileinfo[i&0xF].attrib == ATTR_LFN) continue;
243 FAT_int_ProperFilename(tmpName, fileinfo[i&0xF].name);
244 // LOG("tmpName = '%s'", tmpName);
246 // Debug_HexDump("FAT tmpName", tmpName, strlen(tmpName));
252 Uint8 lfntmp[256*3+1];
253 FAT_int_ConvertUTF16_to_UTF8(lfntmp, lfn);
254 LOG("lfntmp = '%s'", lfntmp);
259 // Only the long name is case sensitive, 8.3 is not
261 if(strucmp(tmpName, Name) == 0 || FAT_int_CompareUTF16_UTF8(lfn, Name) == 0)
263 if(strucmp(tmpName, Name) == 0)
266 memcpy(Entry, fileinfo + (i&0xF), sizeof(*Entry));
267 LOG("Found %s at %i", Name, i);
277 int FAT_int_GetEntryByCluster(tVFS_Node *DirNode, Uint32 Cluster, fat_filetable *Entry)
279 int ents_per_sector = 512 / sizeof(fat_filetable);
280 fat_filetable fileinfo[ents_per_sector];
283 if( Mutex_Acquire(&DirNode->Lock) ) {
290 if( i == 0 || i == ents_per_sector )
292 if(FAT_int_ReadDirSector(DirNode, sector, fileinfo))
294 LOG("ReadDirSector failed");
301 // Check for free/end of list
302 if(fileinfo[i].name[0] == '\0') break; // End of List marker
303 if(fileinfo[i].name[0] == '\xE5') continue; // Free entry
305 if(fileinfo[i].attrib == ATTR_LFN) continue;
307 LOG("fileinfo[i].cluster = %x:%04x", fileinfo[i].clusterHi, fileinfo[i].cluster);
311 FAT_int_ProperFilename(tmpName, fileinfo[i].name);
312 LOG("tmpName = '%s'", tmpName);
317 if(fileinfo[i].cluster != (Cluster & 0xFFFF)) continue;
318 if(fileinfo[i].clusterHi != ((Cluster >> 16) & 0xFFFF)) continue;
320 memcpy(Entry, &fileinfo[i], sizeof(*Entry));
321 Mutex_Release(&DirNode->Lock);
325 Mutex_Release(&DirNode->Lock);
330 * ====================
332 * ====================
336 * \brief Reads a sector from the disk
337 * \param Node Directory node to read
338 * \param Sector Sector number in the directory to read
339 * \param Buffer Destination buffer for the read data
341 int FAT_int_ReadDirSector(tVFS_Node *Node, int Sector, fat_filetable *Buffer)
344 tFAT_VolInfo *disk = Node->ImplPtr;
346 ENTER("pNode iSector pEntry", Node, Sector, Buffer);
349 if(FAT_int_GetAddress(Node, Sector * 512, &addr, NULL))
355 LOG("addr = 0x%llx", addr);
357 if(VFS_ReadAt(disk->fileHandle, addr, 512, Buffer) != 512)
369 * \brief Write a sector to the disk
370 * \param Node Directory node to write
371 * \param Sector Sector number in the directory to write
372 * \param Buffer Source data
374 int FAT_int_WriteDirSector(tVFS_Node *Node, int Sector, const fat_filetable *Buffer)
377 tFAT_VolInfo *disk = Node->ImplPtr;
379 ENTER("pNode iSector pEntry", Node, Sector, Buffer);
382 if(FAT_int_GetAddress(Node, Sector * 512, &addr, NULL))
389 if(VFS_WriteAt(disk->fileHandle, addr, 512, Buffer) != 512)
400 * \brief Writes an entry to the disk
401 * \todo Support expanding a directory
402 * \param Node Directory node
403 * \param ID ID of entry to update
404 * \param Entry Entry data
405 * \return Zero on success, non-zero on error
407 int FAT_int_WriteDirEntry(tVFS_Node *Node, int ID, fat_filetable *Entry)
412 tFAT_VolInfo *disk = Node->ImplPtr;
414 ENTER("pNode iID pEntry", Node, ID, Entry);
416 tmp = FAT_int_GetAddress(Node, ID * sizeof(fat_filetable), &addr, &cluster);
419 //TODO: Allocate a cluster
420 cluster = FAT_int_AllocateCluster(Node->ImplPtr, cluster);
422 Log_Warning("FAT", "Unable to allocate an other cluster for %p", Node);
426 FAT_int_GetAddress(Node, ID * sizeof(fat_filetable), &addr, &cluster);
430 LOG("addr = 0x%llx", addr);
432 // Wriet data to disk
433 VFS_WriteAt(disk->fileHandle, addr, sizeof(fat_filetable), Entry);
442 * \fn Uint16 *FAT_int_GetLFN(tVFS_Node *node)
443 * \brief Return pointer to LFN cache entry
444 * \param Node Directory node
445 * \param ID ID of the short name
446 * \return Pointer to the LFN cache entry
448 Uint16 *FAT_int_GetLFN(tVFS_Node *Node, int ID)
450 tFAT_LFNCache *cache;
453 if( Mutex_Acquire( &Node->Lock ) ) {
457 // TODO: Thread Safety (Lock things)
460 // Create a cache if it isn't there
462 cache = Node->Data = malloc( sizeof(tFAT_LFNCache) + sizeof(tFAT_LFNCacheEnt) );
463 cache->NumEntries = 1;
464 cache->Entries[0].ID = ID;
465 cache->Entries[0].Data[0] = 0;
466 Mutex_Release( &Node->Lock );
467 //Log_Debug("FAT", "Return = %p (new)", cache->Entries[0].Data);
468 return cache->Entries[0].Data;
471 // Scan for this entry
473 for( i = 0; i < cache->NumEntries; i++ )
475 if( cache->Entries[i].ID == ID ) {
476 Mutex_Release( &Node->Lock );
477 //Log_Debug("FAT", "Return = %p (match)", cache->Entries[i].Data);
478 return cache->Entries[i].Data;
480 if( cache->Entries[i].ID == -1 && firstFree == -1 )
484 if(firstFree == -1) {
485 // Use `i` for temp length
486 i = sizeof(tFAT_LFNCache) + (cache->NumEntries+1)*sizeof(tFAT_LFNCacheEnt);
487 Node->Data = realloc( Node->Data, i );
489 Log_Error("FAT", "realloc() fail, unable to allocate %i for LFN cache", i);
490 Mutex_Release( &Node->Lock );
493 //Log_Debug("FAT", "Realloc (%i)\n", i);
495 i = cache->NumEntries;
496 cache->NumEntries ++;
503 cache->Entries[ i ].ID = ID;
504 cache->Entries[ i ].Data[0] = '\0';
506 Mutex_Release( &Node->Lock );
507 //Log_Debug("FAT", "Return = %p (firstFree, i = %i)", cache->Entries[i].Data, i);
508 return cache->Entries[ i ].Data;
512 * \fn void FAT_int_DelLFN(tVFS_Node *node)
513 * \brief Delete a LFN cache entry
514 * \param Node Directory node
515 * \param ID File Entry ID
517 void FAT_int_DelLFN(tVFS_Node *Node, int ID)
519 tFAT_LFNCache *cache = Node->Data;
525 // Scan for a current entry
526 for( i = 0; i < cache->NumEntries; i++ )
528 if( cache->Entries[i].ID == ID )
529 cache->Entries[i].ID = -1;
536 * \fn char *FAT_ReadDir(tVFS_Node *Node, int ID)
537 * \param Node Node structure of directory
538 * \param ID Directory position
539 * \return Filename as a heap string, NULL or VFS_SKIP
541 int FAT_ReadDir(tVFS_Node *Node, int ID, char Dest[FILENAME_MAX])
543 fat_filetable fileinfo[16]; // sizeof(fat_filetable)=32, so 16 per sector
549 ENTER("pNode iID", Node, ID);
551 if(FAT_int_ReadDirSector(Node, ID/16, fileinfo))
553 LOG("End of chain, end of dir");
561 LOG("fileinfo[%i].name[0] = 0x%x", a, (Uint8)fileinfo[a].name[0]);
563 // Check if this is the last entry
564 if( fileinfo[a].name[0] == '\0' ) {
568 return -ENOENT; // break
571 // Check for empty entry
572 if( (Uint8)fileinfo[a].name[0] == 0xE5 ) {
574 LEAVE_RET('i', 1); // Skip
578 // Get Long File Name Cache
579 if(fileinfo[a].attrib == ATTR_LFN)
581 fat_longfilename *lfnInfo;
583 lfnInfo = (fat_longfilename *) &fileinfo[a];
585 // Get cache for corresponding file
586 // > ID + Index gets the corresponding short node
587 lfn = FAT_int_GetLFN( Node, ID + (lfnInfo->id & 0x3F) );
589 a = FAT_int_ParseLFN(&fileinfo[a], lfn);
591 LOG("Invalid LFN, error");
592 LEAVE_RET('i', -EIO);
595 LEAVE_RET('i', 1); // Skip
599 // Check if it is a volume entry
600 if(fileinfo[a].attrib & 0x08) {
601 LEAVE_RET('i', 1); // Skip
604 if(fileinfo[a].name[0] == '.' && fileinfo[a].name[1] == ' ') {
605 LEAVE_RET('i', 1); // Skip
608 if(fileinfo[a].name[0] == '.' && fileinfo[a].name[1] == '.' && fileinfo[a].name[2] == ' ') {
609 LEAVE_RET('i', 1); // Skip
612 LOG("name='%c%c%c%c%c%c%c%c.%c%c%c'",
613 fileinfo[a].name[0], fileinfo[a].name[1], fileinfo[a].name[2], fileinfo[a].name[3],
614 fileinfo[a].name[4], fileinfo[a].name[5], fileinfo[a].name[6], fileinfo[a].name[7],
615 fileinfo[a].name[8], fileinfo[a].name[9], fileinfo[a].name[10] );
618 lfn = FAT_int_GetLFN(Node, ID);
619 //Log_Debug("FAT", "lfn = %p'%s'", lfn, lfn);
620 FAT_int_CreateName(&fileinfo[a], lfn, Dest);
622 FAT_int_CreateName(&fileinfo[a], NULL, Dest);
630 * \fn tVFS_Node *FAT_FindDir(tVFS_Node *node, char *name)
631 * \brief Finds an entry in the current directory
633 tVFS_Node *FAT_FindDir(tVFS_Node *Node, const char *Name, Uint Flags)
635 fat_filetable fileent;
637 ENTER("pNode sname", Node, Name);
640 if(!Name || Name[0] == '\0') {
645 if( FAT_int_GetEntryByName(Node, Name, &fileent) == -1 ) {
651 tVFS_Node *ret = FAT_int_CreateNode(Node, &fileent);
652 LOG("Found %s as %p", Name, ret);
657 tVFS_Node *FAT_GetNodeFromINode(tVFS_Node *Root, Uint64 Inode)
659 tFAT_VolInfo *disk = Root->ImplPtr;
660 tVFS_Node *dirnode, *ret;
663 ENTER("pRoot XInode", Root, Inode);
665 ret = FAT_int_GetNode(disk, Inode & 0xFFFFFFFF);
667 if( (ret->Inode >> 32) != 0 ) {
668 LOG("Node in cache, quick return");
673 LOG("Node cached, but incomplete");
679 dirnode = FAT_int_CreateIncompleteDirNode(disk, Inode >> 32);
681 int id = FAT_int_GetEntryByCluster(dirnode, Inode & 0xFFFFFFFF, &ft);
683 ret = FAT_int_CreateNode(dirnode, &ft);
686 dirnode->Type->Close(dirnode);
694 * \brief Create a new node
696 tVFS_Node *FAT_Mknod(tVFS_Node *DirNode, const char *Name, Uint Flags)
698 tFAT_VolInfo *disk = DirNode->ImplPtr;
701 memset(&ft, 0, sizeof(ft));
703 ENTER("pDirNode sName xFlags", DirNode, Name, Flags);
705 // Allocate a cluster
706 Uint32 cluster = FAT_int_AllocateCluster(disk, -1);
707 LOG("Cluster 0x%07x allocated", cluster);
709 // Create a temporary file table entry for an empty node
710 ft.cluster = cluster & 0xFFFF;
711 ft.clusterHi = cluster >> 16;
713 if( Flags & VFS_FFLAG_DIRECTORY )
714 ft.attrib = ATTR_DIRECTORY;
718 tVFS_Node *newnode = FAT_int_CreateNode(DirNode, &ft);
723 LOG("newnode = %p", newnode);
726 if( (rv = FAT_Link(DirNode, Name, newnode)) ) {
727 newnode->ImplInt |= FAT_FLAG_DELETE;
734 * \brief Internal - Checks if a character is valid in an 8.3 filename
736 static inline int is_valid_83_char(char ch)
738 if( '0' <= ch && ch <= '9' )
740 if( 'A' <= ch && ch <= 'Z' )
742 if( 'a' <= ch && ch <= 'z' )
744 if( strchr("$%'-_@~`#!(){}^#&", ch) )
751 Uint8 FAT_int_UnicodeTo83(Uint32 Input)
753 Input = toupper(Input);
754 // Input = unicode_to_oem(Input);
757 if(!is_valid_83_char(Input))
763 * \brief Internal - Determines if a filename is a valid 8.3 filename
765 int FAT_int_IsValid83Filename(const char *Name)
769 if( !Name[0] || Name[0] == '.' )
772 // Check filename portion
773 for( i = 0; Name[i] && i < 8; i ++ )
777 if( !is_valid_83_char(Name[i]) )
780 // If the next char is not \0 or '.', it's not valid
781 if( Name[i] && Name[i++] != '.' )
784 // Check the extension portion
785 for( j = 0; Name[i+j] && j < 3; j ++ )
787 if( !is_valid_83_char(Name[i+j]) )
791 // After the extension must be the end
798 Uint8 FAT_int_MakeLFNChecksum(const char *ShortName)
801 for( int i = 0; i < 11; i++ )
803 // ret = (ret >>> 1) + ShortName[i]
804 // where >>> is rotate right
805 ret = ((ret & 1) ? 0x80 : 0x00) + (ret >> 1) + ShortName[i];
811 * \brief Create a new name for a file
812 * \note Since FAT doesn't support reference counting, this will cause double-references if
813 * a file is hardlinked and not unlinked
815 int FAT_Link(tVFS_Node *DirNode, const char *NewName, tVFS_Node *NewNode)
820 const int eps = 512 / sizeof(fat_filetable);
821 fat_filetable fileinfo[eps];
823 if( Mutex_Acquire( &DirNode->Lock ) ) {
827 // -- Ensure duplicates aren't created --
828 if( FAT_int_GetEntryByName(DirNode, NewName, &ft) >= 0 ) {
829 Mutex_Release( &DirNode->Lock );
833 // -- Create filetable entry --
838 while( NewName[inofs] && NewName[inofs] == '.' )
839 inofs ++, bDirty = 1;
840 for( int i = 0; i < 8 && NewName[inofs] && NewName[inofs] != '.'; i ++ )
843 inofs += ReadUTF8(NewName + inofs, &cp);
844 // Spaces are silently skipped
849 ft.name[i] = FAT_int_UnicodeTo83(cp);
853 while( NewName[inofs] && NewName[inofs] != '.' )
854 inofs ++, bDirty = 1;
855 for( ; i < 8+3 && NewName[inofs]; i ++ )
858 inofs += ReadUTF8(NewName + inofs, &cp);
859 // Spaces are silently skipped
864 ft.name[i] = FAT_int_UnicodeTo83(cp);
868 if( !NewName[inofs] ) bDirty = 1;
872 int lfnlen = FAT_int_ConvertUTF8_to_UTF16(lfn, (const Uint8*)NewName);
874 nLFNEnt = DivUp(lfnlen, 13);
878 int bNeedsLFN = !FAT_int_IsValid83Filename(NewName);
881 int lfnlen = FAT_int_ConvertUTF8_to_UTF16(lfn, (const Uint8*)NewName);
883 nLFNEnt = DivUp(lfnlen, 13);
885 // Create a base mangled filetable entry
887 while(NewName[j] == '.') j ++; // Eat leading dots
888 for( i = 0; i < 6 && NewName[j] && NewName[j] != '.'; i ++, j ++ )
890 if( !isalpha(NewName[j]) && !is_valid_83_char(NewName[j]) )
893 ft.name[i] = toupper(NewName[j]);
897 while(i < 8) ft.name[i++] = ' ';
898 while(NewName[j] && NewName[j] != '.') j ++;
899 for( ; i < 8+3 && NewName[j]; i ++, j ++ )
901 if( NewName[j] == '.' )
903 else if( !is_valid_83_char(NewName[j]) )
906 ft.name[i] = toupper(NewName[j]);
908 while(i < 8+3) ft.name[i++] = ' ';
910 // - Ensure there isn't a duplicate short-name
911 int bIsDuplicate = 1;
912 while( bIsDuplicate )
914 bIsDuplicate = 0; // Assume none
917 for( int id = 0; ; id ++ )
921 if(FAT_int_ReadDirSector(DirNode, id/eps, fileinfo))
922 break; // end of cluster chain
926 if( fileinfo[id%eps].name[0] == '\0' ) break;
928 if( fileinfo[id%eps].name[0] == '\xE5' ) continue;
930 if( fileinfo[id%eps].attrib == ATTR_LFN ) continue;
932 // Is this a duplicate?
933 if( memcmp(ft.name, fileinfo[id%eps].name, 8+3) == 0 ) {
941 // If a duplicate was found, increment the suffix
944 if( ft.name[7] == '9' ) {
945 // TODO: Expand into ~00
946 Log_Error("FAT", "TODO: Use two digit LFN suffixes");
947 Mutex_Release(&DirNode->Lock);
957 // Create pure filetable entry
960 for( i = 0; i < 8 && *NewName && *NewName != '.'; i ++, NewName++ )
961 ft.name[i] = *NewName;
969 for( ; i < 8+3 && *NewName; i ++, NewName++ )
970 ft.name[i] = *NewName;
972 for( ; i < 8+3; i ++ )
977 if(NewNode->Flags & VFS_FFLAG_DIRECTORY )
978 ft.attrib |= ATTR_DIRECTORY;
980 FAT_int_GetFATTimestamp(NewNode->CTime, &ft.cdate, &ft.ctime, &ft.ctimems);
981 // ft.ctimems = ft.ctimems;
982 ft.ctime = LittleEndian16(ft.ctime);
983 ft.cdate = LittleEndian16(ft.cdate);
984 FAT_int_GetFATTimestamp(NewNode->MTime, &ft.mdate, &ft.mtime, NULL);
985 ft.mtime = LittleEndian16(ft.mtime);
986 ft.mdate = LittleEndian16(ft.mdate);
987 FAT_int_GetFATTimestamp(NewNode->ATime, &ft.adate, NULL, NULL);
988 ft.adate = LittleEndian16(ft.adate);
989 ft.clusterHi = LittleEndian16((NewNode->Inode >> 16) & 0xFFFF);
990 ft.cluster = LittleEndian16(NewNode->Inode & 0xFFFF);
991 ft.size = LittleEndian32(NewNode->Size);
993 LOG("ft.name = '%.11s'", ft.name);
995 // -- Add entry to the directory --
996 // Locate a range of nLFNEnt + 1 free entries
998 int range_first = 0, range_last = -1;
999 for( int id = 0; ; id ++ )
1003 if(FAT_int_ReadDirSector(DirNode, id/eps, fileinfo))
1004 break; // end of cluster chain
1007 // End of file list, break out
1008 if( fileinfo[id%eps].name[0] == '\0' ) {
1009 if( id - range_first == nLFNEnt )
1015 // If an entry is occupied, clear the range
1016 if( fileinfo[id%eps].name[0] != '\xE5' ) {
1017 range_first = id + 1;
1021 // Free entry, check if we have enough
1022 if( id - range_first == nLFNEnt ) {
1026 // Check the next one
1028 if( range_last == -1 )
1030 // - If there are none, defragment the directory?
1032 // - Else, expand the directory
1033 if( end_id == -1 ) {
1034 // End of cluster chain
1037 // Just end of block
1039 // - and if that fails, return an error
1040 Log_Warning("FAT", "TODO: Impliment directory expansion / defragmenting");
1041 Mutex_Release(&DirNode->Lock);
1045 // Calculate the checksum used for LFN
1046 Uint8 lfn_checksum = 0;
1049 lfn_checksum = FAT_int_MakeLFNChecksum(ft.name);
1053 if( range_first % eps != 0 )
1054 FAT_int_ReadDirSector(DirNode, range_first/eps, fileinfo);
1055 for( int id = range_first; id <= range_last; id ++ )
1057 if( id % eps == 0 ) {
1058 if( id != range_first )
1059 FAT_int_WriteDirSector(DirNode, (id-1)/eps, fileinfo);
1060 FAT_int_ReadDirSector(DirNode, id/eps, fileinfo);
1063 if( id == range_last ) {
1065 memcpy(fileinfo + id % eps, &ft, sizeof(fat_filetable));
1069 int lfnid = (nLFNEnt - (id - range_first));
1070 int ofs = (lfnid-1) * 13;
1072 fat_longfilename *lfnent = (void*)( fileinfo + id%eps );
1074 lfnent->id = 0x40 | lfnid;
1075 lfnent->attrib = ATTR_LFN;
1077 lfnent->firstCluster = 0;
1078 lfnent->checksum = lfn_checksum; // ???
1080 for( i = 0; i < 13; i ++ )
1083 if( (wd = lfn[ofs+j]) ) j ++;
1084 wd = LittleEndian16(wd);
1086 lfnent->name1[i ] = wd;
1088 lfnent->name2[i-5 ] = wd;
1090 lfnent->name3[i-5-6] = wd;
1094 FAT_int_WriteDirSector(DirNode, range_last/eps, fileinfo);
1096 Mutex_Release( &DirNode->Lock );
1101 * \fn int FAT_Relink(tVFS_Node *Node, char *OldName, char *NewName)
1102 * \brief Rename / Delete a file
1104 int FAT_Unlink(tVFS_Node *Node, const char *OldName)
1109 if( Mutex_Acquire(&Node->Lock) ) {
1113 int id = FAT_int_GetEntryByName(Node, OldName, &ft);
1115 Mutex_Release(&Node->Lock);
1119 child = FAT_int_CreateNode(Node->ImplPtr, &ft);
1121 Mutex_Release(&Node->Lock);
1124 child->ImplInt |= FAT_FLAG_DELETE; // Mark for deletion on close
1126 // TODO: If it has a LFN, remove that too
1128 // Delete from the directory
1129 ft.name[0] = '\xE5';
1130 FAT_int_WriteDirEntry(Node, id, &ft);
1133 child->Type->Close( child );
1134 Mutex_Release( &Node->Lock );