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);
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 ) {
88 FAT_int_ConvertUTF16_to_UTF8((Uint8*)Dest, LongFileName);
93 FAT_int_ProperFilename(Dest, ft->name);
101 int FAT_int_CompareUTF16_UTF8(const Uint16 *Str16, const char *Str8)
103 int pos16 = 0, pos8 = 0;
104 const Uint8 *str8 = (const Uint8 *)Str8;
106 while( Str16[pos16] && str8[pos8] )
109 if( Str16[pos16] & 0x8000 ) {
117 pos8 += ReadUTF8(str8 + pos8, &cp8);
119 if(cp16 == cp8) continue ;
126 if(Str16[pos16] == str8[pos8])
128 if(Str16[pos16] < str8[pos8])
134 int FAT_int_ConvertUTF16_to_UTF8(Uint8 *Dest, const Uint16 *Source)
137 for( ; *Source; Source ++ )
139 // TODO: Decode/Reencode
149 int FAT_int_ConvertUTF8_to_UTF16(Uint16 *Dest, const Uint8 *Source)
157 cpl = ReadUTF8(Source, &cp);
172 int FAT_int_ParseLFN(const fat_filetable *Entry, Uint16 *Buffer)
174 const fat_longfilename *lfnInfo;
177 lfnInfo = (const void*)Entry;
179 if(lfnInfo->id & 0x40) {
180 memset(Buffer, 0, 256*2);
182 ofs = (lfnInfo->id & 0x3F) * 13 - 1;
186 Buffer[ofs--] = lfnInfo->name3[1]; Buffer[ofs--] = lfnInfo->name3[0];
187 Buffer[ofs--] = lfnInfo->name2[5]; Buffer[ofs--] = lfnInfo->name2[4];
188 Buffer[ofs--] = lfnInfo->name2[3]; Buffer[ofs--] = lfnInfo->name2[2];
189 Buffer[ofs--] = lfnInfo->name2[1]; Buffer[ofs--] = lfnInfo->name2[0];
190 Buffer[ofs--] = lfnInfo->name1[4]; Buffer[ofs--] = lfnInfo->name1[3];
191 Buffer[ofs--] = lfnInfo->name1[2]; Buffer[ofs--] = lfnInfo->name1[1];
192 Buffer[ofs--] = lfnInfo->name1[0];
194 if((lfnInfo->id&0x3F) == 1)
200 int FAT_int_GetEntryByName(tVFS_Node *DirNode, const char *Name, fat_filetable *Entry)
202 fat_filetable fileinfo[16];
209 ENTER("pDirNode sName pEntry", DirNode, Name, Entry);
211 for( int i = 0; ; i++ )
214 if(FAT_int_ReadDirSector(DirNode, i/16, fileinfo))
221 //Check if the files are free
222 if(fileinfo[i&0xF].name[0] == '\0') break; // End of List marker
223 if(fileinfo[i&0xF].name[0] == '\xE5') continue; // Free entry
227 // Long File Name Entry
228 if(fileinfo[i & 0xF].attrib == ATTR_LFN)
230 if( FAT_int_ParseLFN(&fileinfo[i&0xF], lfn) )
234 // Remove LFN if it does not apply
235 if(lfnId != i) lfn[0] = 0;
237 if(fileinfo[i&0xF].attrib == ATTR_LFN) continue;
241 FAT_int_ProperFilename(tmpName, fileinfo[i&0xF].name);
242 // LOG("tmpName = '%s'", tmpName);
244 // Debug_HexDump("FAT tmpName", tmpName, strlen(tmpName));
250 Uint8 lfntmp[256*3+1];
251 FAT_int_ConvertUTF16_to_UTF8(lfntmp, lfn);
252 LOG("lfntmp = '%s'", lfntmp);
257 // Only the long name is case sensitive, 8.3 is not
259 if(strucmp(tmpName, Name) == 0 || FAT_int_CompareUTF16_UTF8(lfn, Name) == 0)
261 if(strucmp(tmpName, Name) == 0)
264 memcpy(Entry, fileinfo + (i&0xF), sizeof(*Entry));
265 LOG("Found %s at %i", Name, i);
275 int FAT_int_GetEntryByCluster(tVFS_Node *DirNode, Uint32 Cluster, fat_filetable *Entry)
277 int ents_per_sector = 512 / sizeof(fat_filetable);
278 fat_filetable fileinfo[ents_per_sector];
281 Mutex_Acquire(&DirNode->Lock);
285 if( i == 0 || i == ents_per_sector )
287 if(FAT_int_ReadDirSector(DirNode, sector, fileinfo))
289 LOG("ReadDirSector failed");
296 // Check for free/end of list
297 if(fileinfo[i].name[0] == '\0') break; // End of List marker
298 if(fileinfo[i].name[0] == '\xE5') continue; // Free entry
300 if(fileinfo[i].attrib == ATTR_LFN) continue;
302 LOG("fileinfo[i].cluster = %x:%04x", fileinfo[i].clusterHi, fileinfo[i].cluster);
306 FAT_int_ProperFilename(tmpName, fileinfo[i].name);
307 LOG("tmpName = '%s'", tmpName);
312 if(fileinfo[i].cluster != (Cluster & 0xFFFF)) continue;
313 if(fileinfo[i].clusterHi != ((Cluster >> 16) & 0xFFFF)) continue;
315 memcpy(Entry, &fileinfo[i], sizeof(*Entry));
316 Mutex_Release(&DirNode->Lock);
320 Mutex_Release(&DirNode->Lock);
325 * ====================
327 * ====================
331 * \brief Reads a sector from the disk
332 * \param Node Directory node to read
333 * \param Sector Sector number in the directory to read
334 * \param Buffer Destination buffer for the read data
336 int FAT_int_ReadDirSector(tVFS_Node *Node, int Sector, fat_filetable *Buffer)
339 tFAT_VolInfo *disk = Node->ImplPtr;
341 ENTER("pNode iSector pEntry", Node, Sector, Buffer);
344 if(FAT_int_GetAddress(Node, Sector * 512, &addr, NULL))
350 LOG("addr = 0x%llx", addr);
352 if(VFS_ReadAt(disk->fileHandle, addr, 512, Buffer) != 512)
364 * \brief Write a sector to the disk
365 * \param Node Directory node to write
366 * \param Sector Sector number in the directory to write
367 * \param Buffer Source data
369 int FAT_int_WriteDirSector(tVFS_Node *Node, int Sector, const fat_filetable *Buffer)
372 tFAT_VolInfo *disk = Node->ImplPtr;
374 ENTER("pNode iSector pEntry", Node, Sector, Buffer);
377 if(FAT_int_GetAddress(Node, Sector * 512, &addr, NULL))
384 if(VFS_WriteAt(disk->fileHandle, addr, 512, Buffer) != 512)
395 * \brief Writes an entry to the disk
396 * \todo Support expanding a directory
397 * \param Node Directory node
398 * \param ID ID of entry to update
399 * \param Entry Entry data
400 * \return Zero on success, non-zero on error
402 int FAT_int_WriteDirEntry(tVFS_Node *Node, int ID, fat_filetable *Entry)
407 tFAT_VolInfo *disk = Node->ImplPtr;
409 ENTER("pNode iID pEntry", Node, ID, Entry);
411 tmp = FAT_int_GetAddress(Node, ID * sizeof(fat_filetable), &addr, &cluster);
414 //TODO: Allocate a cluster
415 cluster = FAT_int_AllocateCluster(Node->ImplPtr, cluster);
417 Log_Warning("FAT", "Unable to allocate an other cluster for %p", Node);
421 FAT_int_GetAddress(Node, ID * sizeof(fat_filetable), &addr, &cluster);
425 LOG("addr = 0x%llx", addr);
427 // Wriet data to disk
428 VFS_WriteAt(disk->fileHandle, addr, sizeof(fat_filetable), Entry);
437 * \fn Uint16 *FAT_int_GetLFN(tVFS_Node *node)
438 * \brief Return pointer to LFN cache entry
439 * \param Node Directory node
440 * \param ID ID of the short name
441 * \return Pointer to the LFN cache entry
443 Uint16 *FAT_int_GetLFN(tVFS_Node *Node, int ID)
445 tFAT_LFNCache *cache;
448 Mutex_Acquire( &Node->Lock );
450 // TODO: Thread Safety (Lock things)
453 // Create a cache if it isn't there
455 cache = Node->Data = malloc( sizeof(tFAT_LFNCache) + sizeof(tFAT_LFNCacheEnt) );
456 cache->NumEntries = 1;
457 cache->Entries[0].ID = ID;
458 cache->Entries[0].Data[0] = 0;
459 Mutex_Release( &Node->Lock );
460 //Log_Debug("FAT", "Return = %p (new)", cache->Entries[0].Data);
461 return cache->Entries[0].Data;
464 // Scan for this entry
466 for( i = 0; i < cache->NumEntries; i++ )
468 if( cache->Entries[i].ID == ID ) {
469 Mutex_Release( &Node->Lock );
470 //Log_Debug("FAT", "Return = %p (match)", cache->Entries[i].Data);
471 return cache->Entries[i].Data;
473 if( cache->Entries[i].ID == -1 && firstFree == -1 )
477 if(firstFree == -1) {
478 // Use `i` for temp length
479 i = sizeof(tFAT_LFNCache) + (cache->NumEntries+1)*sizeof(tFAT_LFNCacheEnt);
480 Node->Data = realloc( Node->Data, i );
482 Log_Error("FAT", "realloc() fail, unable to allocate %i for LFN cache", i);
483 Mutex_Release( &Node->Lock );
486 //Log_Debug("FAT", "Realloc (%i)\n", i);
488 i = cache->NumEntries;
489 cache->NumEntries ++;
496 cache->Entries[ i ].ID = ID;
497 cache->Entries[ i ].Data[0] = '\0';
499 Mutex_Release( &Node->Lock );
500 //Log_Debug("FAT", "Return = %p (firstFree, i = %i)", cache->Entries[i].Data, i);
501 return cache->Entries[ i ].Data;
505 * \fn void FAT_int_DelLFN(tVFS_Node *node)
506 * \brief Delete a LFN cache entry
507 * \param Node Directory node
508 * \param ID File Entry ID
510 void FAT_int_DelLFN(tVFS_Node *Node, int ID)
512 tFAT_LFNCache *cache = Node->Data;
518 // Scan for a current entry
519 for( i = 0; i < cache->NumEntries; i++ )
521 if( cache->Entries[i].ID == ID )
522 cache->Entries[i].ID = -1;
529 * \fn char *FAT_ReadDir(tVFS_Node *Node, int ID)
530 * \param Node Node structure of directory
531 * \param ID Directory position
532 * \return Filename as a heap string, NULL or VFS_SKIP
534 int FAT_ReadDir(tVFS_Node *Node, int ID, char Dest[FILENAME_MAX])
536 fat_filetable fileinfo[16]; // sizeof(fat_filetable)=32, so 16 per sector
542 ENTER("pNode iID", Node, ID);
544 if(FAT_int_ReadDirSector(Node, ID/16, fileinfo))
546 LOG("End of chain, end of dir");
554 LOG("fileinfo[%i].name[0] = 0x%x", a, (Uint8)fileinfo[a].name[0]);
556 // Check if this is the last entry
557 if( fileinfo[a].name[0] == '\0' ) {
561 return -ENOENT; // break
564 // Check for empty entry
565 if( (Uint8)fileinfo[a].name[0] == 0xE5 ) {
567 LEAVE_RET('i', 1); // Skip
571 // Get Long File Name Cache
572 if(fileinfo[a].attrib == ATTR_LFN)
574 fat_longfilename *lfnInfo;
576 lfnInfo = (fat_longfilename *) &fileinfo[a];
578 // Get cache for corresponding file
579 // > ID + Index gets the corresponding short node
580 lfn = FAT_int_GetLFN( Node, ID + (lfnInfo->id & 0x3F) );
582 a = FAT_int_ParseLFN(&fileinfo[a], lfn);
584 LOG("Invalid LFN, error");
585 LEAVE_RET('i', -EIO);
588 LEAVE_RET('i', 1); // Skip
592 // Check if it is a volume entry
593 if(fileinfo[a].attrib & 0x08) {
594 LEAVE_RET('i', 1); // Skip
597 if(fileinfo[a].name[0] == '.' && fileinfo[a].name[1] == ' ') {
598 LEAVE_RET('i', 1); // Skip
601 if(fileinfo[a].name[0] == '.' && fileinfo[a].name[1] == '.' && fileinfo[a].name[2] == ' ') {
602 LEAVE_RET('i', 1); // Skip
605 LOG("name='%c%c%c%c%c%c%c%c.%c%c%c'",
606 fileinfo[a].name[0], fileinfo[a].name[1], fileinfo[a].name[2], fileinfo[a].name[3],
607 fileinfo[a].name[4], fileinfo[a].name[5], fileinfo[a].name[6], fileinfo[a].name[7],
608 fileinfo[a].name[8], fileinfo[a].name[9], fileinfo[a].name[10] );
611 lfn = FAT_int_GetLFN(Node, ID);
612 //Log_Debug("FAT", "lfn = %p'%s'", lfn, lfn);
613 FAT_int_CreateName(&fileinfo[a], lfn, Dest);
615 FAT_int_CreateName(&fileinfo[a], NULL, Dest);
623 * \fn tVFS_Node *FAT_FindDir(tVFS_Node *node, char *name)
624 * \brief Finds an entry in the current directory
626 tVFS_Node *FAT_FindDir(tVFS_Node *Node, const char *Name)
628 fat_filetable fileent;
630 ENTER("pNode sname", Node, Name);
633 if(!Name || Name[0] == '\0') {
638 if( FAT_int_GetEntryByName(Node, Name, &fileent) == -1 ) {
644 tVFS_Node *ret = FAT_int_CreateNode(Node, &fileent);
645 LOG("Found %s as %p", Name, ret);
650 tVFS_Node *FAT_GetNodeFromINode(tVFS_Node *Root, Uint64 Inode)
652 tFAT_VolInfo *disk = Root->ImplPtr;
653 tVFS_Node *dirnode, *ret;
656 ENTER("pRoot XInode", Root, Inode);
658 ret = FAT_int_GetNode(disk, Inode & 0xFFFFFFFF);
660 if( (ret->Inode >> 32) != 0 ) {
661 LOG("Node in cache, quick return");
666 LOG("Node cached, but incomplete");
672 dirnode = FAT_int_CreateIncompleteDirNode(disk, Inode >> 32);
674 int id = FAT_int_GetEntryByCluster(dirnode, Inode & 0xFFFFFFFF, &ft);
676 ret = FAT_int_CreateNode(dirnode, &ft);
679 dirnode->Type->Close(dirnode);
687 * \brief Create a new node
689 tVFS_Node *FAT_Mknod(tVFS_Node *DirNode, const char *Name, Uint Flags)
691 tFAT_VolInfo *disk = DirNode->ImplPtr;
694 memset(&ft, 0, sizeof(ft));
696 ENTER("pDirNode sName xFlags", DirNode, Name, Flags);
698 // Allocate a cluster
699 Uint32 cluster = FAT_int_AllocateCluster(disk, -1);
700 LOG("Cluster 0x%07x allocated", cluster);
702 // Create a temporary file table entry for an empty node
703 ft.cluster = cluster & 0xFFFF;
704 ft.clusterHi = cluster >> 16;
706 if( Flags & VFS_FFLAG_DIRECTORY )
707 ft.attrib = ATTR_DIRECTORY;
711 tVFS_Node *newnode = FAT_int_CreateNode(DirNode, &ft);
716 LOG("newnode = %p", newnode);
719 if( (rv = FAT_Link(DirNode, Name, newnode)) ) {
720 newnode->ImplInt |= FAT_FLAG_DELETE;
727 * \brief Internal - Checks if a character is valid in an 8.3 filename
729 static inline int is_valid_83_char(char ch)
731 if( '0' <= ch && ch <= '9' )
733 if( 'A' <= ch && ch <= 'Z' )
735 if( 'a' <= ch && ch <= 'z' )
737 if( strchr("$%'-_@~`#!(){}^#&", ch) )
744 Uint8 FAT_int_UnicodeTo83(Uint32 Input)
746 Input = toupper(Input);
747 // Input = unicode_to_oem(Input);
750 if(!is_valid_83_char(Input))
756 * \brief Internal - Determines if a filename is a valid 8.3 filename
758 int FAT_int_IsValid83Filename(const char *Name)
762 if( !Name[0] || Name[0] == '.' )
765 // Check filename portion
766 for( i = 0; Name[i] && i < 8; i ++ )
770 if( !is_valid_83_char(Name[i]) )
773 // If the next char is not \0 or '.', it's not valid
774 if( Name[i] && Name[i++] != '.' )
777 // Check the extension portion
778 for( j = 0; Name[i+j] && j < 3; j ++ )
780 if( !is_valid_83_char(Name[i+j]) )
784 // After the extension must be the end
791 Uint8 FAT_int_MakeLFNChecksum(const char *ShortName)
794 for( int i = 0; i < 11; i++ )
796 // ret = (ret >>> 1) + ShortName[i]
797 // where >>> is rotate right
798 ret = ((ret & 1) ? 0x80 : 0x00) + (ret >> 1) + ShortName[i];
804 * \brief Create a new name for a file
805 * \note Since FAT doesn't support reference counting, this will cause double-references if
806 * a file is hardlinked and not unlinked
808 int FAT_Link(tVFS_Node *DirNode, const char *NewName, tVFS_Node *NewNode)
813 const int eps = 512 / sizeof(fat_filetable);
814 fat_filetable fileinfo[eps];
816 Mutex_Acquire( &DirNode->Lock );
818 // -- Ensure duplicates aren't created --
819 if( FAT_int_GetEntryByName(DirNode, NewName, &ft) >= 0 ) {
820 Mutex_Release( &DirNode->Lock );
824 // -- Create filetable entry --
829 while( NewName[inofs] && NewName[inofs] == '.' )
830 inofs ++, bDirty = 1;
831 for( int i = 0; i < 8 && NewName[inofs] && NewName[inofs] != '.'; i ++ )
834 inofs += ReadUTF8(NewName + inofs, &cp);
835 // Spaces are silently skipped
840 ft.name[i] = FAT_int_UnicodeTo83(cp);
844 while( NewName[inofs] && NewName[inofs] != '.' )
845 inofs ++, bDirty = 1;
846 for( ; i < 8+3 && NewName[inofs]; i ++ )
849 inofs += ReadUTF8(NewName + inofs, &cp);
850 // Spaces are silently skipped
855 ft.name[i] = FAT_int_UnicodeTo83(cp);
859 if( !NewName[inofs] ) bDirty = 1;
863 int lfnlen = FAT_int_ConvertUTF8_to_UTF16(lfn, (const Uint8*)NewName);
865 nLFNEnt = DivUp(lfnlen, 13);
869 int bNeedsLFN = !FAT_int_IsValid83Filename(NewName);
872 int lfnlen = FAT_int_ConvertUTF8_to_UTF16(lfn, (const Uint8*)NewName);
874 nLFNEnt = DivUp(lfnlen, 13);
876 // Create a base mangled filetable entry
878 while(NewName[j] == '.') j ++; // Eat leading dots
879 for( i = 0; i < 6 && NewName[j] && NewName[j] != '.'; i ++, j ++ )
881 if( !isalpha(NewName[j]) && !is_valid_83_char(NewName[j]) )
884 ft.name[i] = toupper(NewName[j]);
888 while(i < 8) ft.name[i++] = ' ';
889 while(NewName[j] && NewName[j] != '.') j ++;
890 for( ; i < 8+3 && NewName[j]; i ++, j ++ )
892 if( NewName[j] == '.' )
894 else if( !is_valid_83_char(NewName[j]) )
897 ft.name[i] = toupper(NewName[j]);
899 while(i < 8+3) ft.name[i++] = ' ';
901 // - Ensure there isn't a duplicate short-name
902 int bIsDuplicate = 1;
903 while( bIsDuplicate )
905 bIsDuplicate = 0; // Assume none
908 for( int id = 0; ; id ++ )
912 if(FAT_int_ReadDirSector(DirNode, id/eps, fileinfo))
913 break; // end of cluster chain
917 if( fileinfo[id%eps].name[0] == '\0' ) break;
919 if( fileinfo[id%eps].name[0] == '\xE5' ) continue;
921 if( fileinfo[id%eps].attrib == ATTR_LFN ) continue;
923 // Is this a duplicate?
924 if( memcmp(ft.name, fileinfo[id%eps].name, 8+3) == 0 ) {
932 // If a duplicate was found, increment the suffix
935 if( ft.name[7] == '9' ) {
936 // TODO: Expand into ~00
937 Log_Error("FAT", "TODO: Use two digit LFN suffixes");
938 Mutex_Release(&DirNode->Lock);
948 // Create pure filetable entry
951 for( i = 0; i < 8 && *NewName && *NewName != '.'; i ++, NewName++ )
952 ft.name[i] = *NewName;
960 for( ; i < 8+3 && *NewName; i ++, NewName++ )
961 ft.name[i] = *NewName;
963 for( ; i < 8+3; i ++ )
968 if(NewNode->Flags & VFS_FFLAG_DIRECTORY )
969 ft.attrib |= ATTR_DIRECTORY;
971 FAT_int_GetFATTimestamp(NewNode->CTime, &ft.cdate, &ft.ctime, &ft.ctimems);
972 // ft.ctimems = ft.ctimems;
973 ft.ctime = LittleEndian16(ft.ctime);
974 ft.cdate = LittleEndian16(ft.cdate);
975 FAT_int_GetFATTimestamp(NewNode->MTime, &ft.mdate, &ft.mtime, NULL);
976 ft.mtime = LittleEndian16(ft.mtime);
977 ft.mdate = LittleEndian16(ft.mdate);
978 FAT_int_GetFATTimestamp(NewNode->ATime, &ft.adate, NULL, NULL);
979 ft.adate = LittleEndian16(ft.adate);
980 ft.clusterHi = LittleEndian16((NewNode->Inode >> 16) & 0xFFFF);
981 ft.cluster = LittleEndian16(NewNode->Inode & 0xFFFF);
982 ft.size = LittleEndian32(NewNode->Size);
984 LOG("ft.name = '%.11s'", ft.name);
986 // -- Add entry to the directory --
987 // Locate a range of nLFNEnt + 1 free entries
989 int range_first = 0, range_last = -1;
990 for( int id = 0; ; id ++ )
994 if(FAT_int_ReadDirSector(DirNode, id/eps, fileinfo))
995 break; // end of cluster chain
998 // End of file list, break out
999 if( fileinfo[id%eps].name[0] == '\0' ) {
1000 if( id - range_first == nLFNEnt )
1006 // If an entry is occupied, clear the range
1007 if( fileinfo[id%eps].name[0] != '\xE5' ) {
1008 range_first = id + 1;
1012 // Free entry, check if we have enough
1013 if( id - range_first == nLFNEnt ) {
1017 // Check the next one
1019 if( range_last == -1 )
1021 // - If there are none, defragment the directory?
1023 // - Else, expand the directory
1024 if( end_id == -1 ) {
1025 // End of cluster chain
1028 // Just end of block
1030 // - and if that fails, return an error
1031 Log_Warning("FAT", "TODO: Impliment directory expansion / defragmenting");
1032 Mutex_Release(&DirNode->Lock);
1036 // Calculate the checksum used for LFN
1037 Uint8 lfn_checksum = 0;
1040 lfn_checksum = FAT_int_MakeLFNChecksum(ft.name);
1044 if( range_first % eps != 0 )
1045 FAT_int_ReadDirSector(DirNode, range_first/eps, fileinfo);
1046 for( int id = range_first; id <= range_last; id ++ )
1048 if( id % eps == 0 ) {
1049 if( id != range_first )
1050 FAT_int_WriteDirSector(DirNode, (id-1)/eps, fileinfo);
1051 FAT_int_ReadDirSector(DirNode, id/eps, fileinfo);
1054 if( id == range_last ) {
1056 memcpy(fileinfo + id % eps, &ft, sizeof(fat_filetable));
1060 int lfnid = (nLFNEnt - (id - range_first));
1061 int ofs = (lfnid-1) * 13;
1063 fat_longfilename *lfnent = (void*)( fileinfo + id%eps );
1065 lfnent->id = 0x40 | lfnid;
1066 lfnent->attrib = ATTR_LFN;
1068 lfnent->firstCluster = 0;
1069 lfnent->checksum = lfn_checksum; // ???
1071 for( i = 0; i < 13; i ++ )
1074 if( (wd = lfn[ofs+j]) ) j ++;
1075 wd = LittleEndian16(wd);
1077 lfnent->name1[i ] = wd;
1079 lfnent->name2[i-5 ] = wd;
1081 lfnent->name3[i-5-6] = wd;
1085 FAT_int_WriteDirSector(DirNode, range_last/eps, fileinfo);
1087 Mutex_Release( &DirNode->Lock );
1092 * \fn int FAT_Relink(tVFS_Node *Node, char *OldName, char *NewName)
1093 * \brief Rename / Delete a file
1095 int FAT_Unlink(tVFS_Node *Node, const char *OldName)
1100 Mutex_Acquire(&Node->Lock);
1102 int id = FAT_int_GetEntryByName(Node, OldName, &ft);
1104 Mutex_Release(&Node->Lock);
1108 child = FAT_int_CreateNode(Node->ImplPtr, &ft);
1110 Mutex_Release(&Node->Lock);
1113 child->ImplInt |= FAT_FLAG_DELETE; // Mark for deletion on close
1115 // TODO: If it has a LFN, remove that too
1117 // Delete from the directory
1118 ft.name[0] = '\xE5';
1119 FAT_int_WriteDirEntry(Node, id, &ft);
1122 child->Type->Close( child );
1123 Mutex_Release( &Node->Lock );