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 if( Mutex_Acquire( &Node->Lock ) ) {
454 // TODO: Thread Safety (Lock things)
455 tFAT_LFNCache *cache = Node->Data;
457 // Create a cache if it isn't there
459 cache = Node->Data = malloc( sizeof(tFAT_LFNCache) + sizeof(tFAT_LFNCacheEnt) );
460 cache->NumEntries = 1;
461 cache->Entries[0].ID = ID;
462 cache->Entries[0].Data[0] = 0;
463 Mutex_Release( &Node->Lock );
464 //Log_Debug("FAT", "Return = %p (new)", cache->Entries[0].Data);
465 return cache->Entries[0].Data;
468 // Scan for this entry
470 for( int i = 0; i < cache->NumEntries; i++ )
472 if( cache->Entries[i].ID == ID ) {
473 Mutex_Release( &Node->Lock );
474 //Log_Debug("FAT", "Return = %p (match)", cache->Entries[i].Data);
475 return cache->Entries[i].Data;
477 if( cache->Entries[i].ID == -1 && firstFree == -1 )
481 int cache_entry = firstFree;
485 size_t newsize = offsetof(tFAT_LFNCache, Entries[cache->NumEntries+1]);
486 tFAT_LFNCache *new_alloc = realloc( Node->Data, newsize );
488 Log_Error("FAT", "realloc() fail, unable to allocate %zi for LFN cache", newsize);
489 Mutex_Release( &Node->Lock );
492 Node->Data = new_alloc;
493 //Log_Debug("FAT", "Realloc (%i)\n", i);
495 cache_entry = cache->NumEntries;
496 cache->NumEntries ++;
500 cache_entry = firstFree;
504 cache->Entries[ cache_entry ].ID = ID;
505 cache->Entries[ cache_entry ].Data[0] = '\0';
507 Mutex_Release( &Node->Lock );
508 //Log_Debug("FAT", "Return = %p (firstFree, i = %i)", cache->Entries[i].Data, i);
509 return cache->Entries[ cache_entry ].Data;
513 * \fn void FAT_int_DelLFN(tVFS_Node *node)
514 * \brief Delete a LFN cache entry
515 * \param Node Directory node
516 * \param ID File Entry ID
518 void FAT_int_DelLFN(tVFS_Node *Node, int ID)
520 tFAT_LFNCache *cache = Node->Data;
526 // Scan for a current entry
527 for( i = 0; i < cache->NumEntries; i++ )
529 if( cache->Entries[i].ID == ID )
530 cache->Entries[i].ID = -1;
537 * \fn char *FAT_ReadDir(tVFS_Node *Node, int ID)
538 * \param Node Node structure of directory
539 * \param ID Directory position
540 * \return Filename as a heap string, NULL or VFS_SKIP
542 int FAT_ReadDir(tVFS_Node *Node, int ID, char Dest[FILENAME_MAX])
544 fat_filetable fileinfo[16]; // sizeof(fat_filetable)=32, so 16 per sector
550 ENTER("pNode iID", Node, ID);
552 if(FAT_int_ReadDirSector(Node, ID/16, fileinfo))
554 LOG("End of chain, end of dir");
562 LOG("fileinfo[%i].name[0] = 0x%x", a, (Uint8)fileinfo[a].name[0]);
564 // Check if this is the last entry
565 if( fileinfo[a].name[0] == '\0' ) {
569 return -ENOENT; // break
572 // Check for empty entry
573 if( (Uint8)fileinfo[a].name[0] == 0xE5 ) {
575 LEAVE_RET('i', 1); // Skip
579 // Get Long File Name Cache
580 if(fileinfo[a].attrib == ATTR_LFN)
582 fat_longfilename *lfnInfo;
584 lfnInfo = (fat_longfilename *) &fileinfo[a];
586 // Get cache for corresponding file
587 // > ID + Index gets the corresponding short node
588 lfn = FAT_int_GetLFN( Node, ID + (lfnInfo->id & 0x3F) );
590 a = FAT_int_ParseLFN(&fileinfo[a], lfn);
592 LOG("Invalid LFN, error");
593 LEAVE_RET('i', -EIO);
596 LEAVE_RET('i', 1); // Skip
600 // Check if it is a volume entry
601 if(fileinfo[a].attrib & 0x08) {
602 LEAVE_RET('i', 1); // Skip
605 if(fileinfo[a].name[0] == '.' && fileinfo[a].name[1] == ' ') {
606 LEAVE_RET('i', 1); // Skip
609 if(fileinfo[a].name[0] == '.' && fileinfo[a].name[1] == '.' && fileinfo[a].name[2] == ' ') {
610 LEAVE_RET('i', 1); // Skip
613 LOG("name='%c%c%c%c%c%c%c%c.%c%c%c'",
614 fileinfo[a].name[0], fileinfo[a].name[1], fileinfo[a].name[2], fileinfo[a].name[3],
615 fileinfo[a].name[4], fileinfo[a].name[5], fileinfo[a].name[6], fileinfo[a].name[7],
616 fileinfo[a].name[8], fileinfo[a].name[9], fileinfo[a].name[10] );
619 lfn = FAT_int_GetLFN(Node, ID);
620 //Log_Debug("FAT", "lfn = %p'%s'", lfn, lfn);
621 FAT_int_CreateName(&fileinfo[a], lfn, Dest);
623 FAT_int_CreateName(&fileinfo[a], NULL, Dest);
631 * \fn tVFS_Node *FAT_FindDir(tVFS_Node *node, char *name)
632 * \brief Finds an entry in the current directory
634 tVFS_Node *FAT_FindDir(tVFS_Node *Node, const char *Name, Uint Flags)
636 fat_filetable fileent;
638 ENTER("pNode sname", Node, Name);
641 if(!Name || Name[0] == '\0') {
646 if( FAT_int_GetEntryByName(Node, Name, &fileent) == -1 ) {
652 tVFS_Node *ret = FAT_int_CreateNode(Node, &fileent);
653 LOG("Found %s as %p", Name, ret);
658 tVFS_Node *FAT_GetNodeFromINode(tVFS_Node *Root, Uint64 Inode)
660 tFAT_VolInfo *disk = Root->ImplPtr;
661 tVFS_Node *dirnode, *ret;
664 ENTER("pRoot XInode", Root, Inode);
666 ret = FAT_int_GetNode(disk, Inode & 0xFFFFFFFF);
668 if( (ret->Inode >> 32) != 0 ) {
669 LOG("Node in cache, quick return");
674 LOG("Node cached, but incomplete");
680 dirnode = FAT_int_CreateIncompleteDirNode(disk, Inode >> 32);
682 int id = FAT_int_GetEntryByCluster(dirnode, Inode & 0xFFFFFFFF, &ft);
684 ret = FAT_int_CreateNode(dirnode, &ft);
687 dirnode->Type->Close(dirnode);
695 * \brief Create a new node
697 tVFS_Node *FAT_Mknod(tVFS_Node *DirNode, const char *Name, Uint Flags)
699 tFAT_VolInfo *disk = DirNode->ImplPtr;
702 memset(&ft, 0, sizeof(ft));
704 ENTER("pDirNode sName xFlags", DirNode, Name, Flags);
706 // Allocate a cluster
707 Uint32 cluster = FAT_int_AllocateCluster(disk, -1);
708 LOG("Cluster 0x%07x allocated", cluster);
710 // Create a temporary file table entry for an empty node
711 ft.cluster = cluster & 0xFFFF;
712 ft.clusterHi = cluster >> 16;
714 if( Flags & VFS_FFLAG_DIRECTORY )
715 ft.attrib = ATTR_DIRECTORY;
719 tVFS_Node *newnode = FAT_int_CreateNode(DirNode, &ft);
724 LOG("newnode = %p", newnode);
727 if( (rv = FAT_Link(DirNode, Name, newnode)) ) {
728 newnode->ImplInt |= FAT_FLAG_DELETE;
735 * \brief Internal - Checks if a character is valid in an 8.3 filename
737 static inline int is_valid_83_char(char ch)
739 if( '0' <= ch && ch <= '9' )
741 if( 'A' <= ch && ch <= 'Z' )
743 if( 'a' <= ch && ch <= 'z' )
745 if( strchr("$%'-_@~`#!(){}^#&", ch) )
752 Uint8 FAT_int_UnicodeTo83(Uint32 Input)
754 Input = toupper(Input);
755 // Input = unicode_to_oem(Input);
758 if(!is_valid_83_char(Input))
764 * \brief Internal - Determines if a filename is a valid 8.3 filename
766 int FAT_int_IsValid83Filename(const char *Name)
770 if( !Name[0] || Name[0] == '.' )
773 // Check filename portion
774 for( i = 0; Name[i] && i < 8; i ++ )
778 if( !is_valid_83_char(Name[i]) )
781 // If the next char is not \0 or '.', it's not valid
782 if( Name[i] && Name[i++] != '.' )
785 // Check the extension portion
786 for( j = 0; Name[i+j] && j < 3; j ++ )
788 if( !is_valid_83_char(Name[i+j]) )
792 // After the extension must be the end
799 Uint8 FAT_int_MakeLFNChecksum(const char *ShortName)
802 for( int i = 0; i < 11; i++ )
804 // ret = (ret >>> 1) + ShortName[i]
805 // where >>> is rotate right
806 ret = ((ret & 1) ? 0x80 : 0x00) + (ret >> 1) + ShortName[i];
812 * \brief Create a new name for a file
813 * \note Since FAT doesn't support reference counting, this will cause double-references if
814 * a file is hardlinked and not unlinked
816 int FAT_Link(tVFS_Node *DirNode, const char *NewName, tVFS_Node *NewNode)
821 const int eps = 512 / sizeof(fat_filetable);
822 fat_filetable fileinfo[eps];
824 if( Mutex_Acquire( &DirNode->Lock ) ) {
828 // -- Ensure duplicates aren't created --
829 if( FAT_int_GetEntryByName(DirNode, NewName, &ft) >= 0 ) {
830 Mutex_Release( &DirNode->Lock );
834 // -- Create filetable entry --
839 while( NewName[inofs] && NewName[inofs] == '.' )
840 inofs ++, bDirty = 1;
841 for( int i = 0; i < 8 && NewName[inofs] && NewName[inofs] != '.'; i ++ )
844 inofs += ReadUTF8(NewName + inofs, &cp);
845 // Spaces are silently skipped
850 ft.name[i] = FAT_int_UnicodeTo83(cp);
854 while( NewName[inofs] && NewName[inofs] != '.' )
855 inofs ++, bDirty = 1;
856 for( ; i < 8+3 && NewName[inofs]; i ++ )
859 inofs += ReadUTF8(NewName + inofs, &cp);
860 // Spaces are silently skipped
865 ft.name[i] = FAT_int_UnicodeTo83(cp);
869 if( !NewName[inofs] ) bDirty = 1;
873 int lfnlen = FAT_int_ConvertUTF8_to_UTF16(lfn, (const Uint8*)NewName);
875 nLFNEnt = DivUp(lfnlen, 13);
879 int bNeedsLFN = !FAT_int_IsValid83Filename(NewName);
882 int lfnlen = FAT_int_ConvertUTF8_to_UTF16(lfn, (const Uint8*)NewName);
884 nLFNEnt = DivUp(lfnlen, 13);
886 // Create a base mangled filetable entry
888 while(NewName[j] == '.') j ++; // Eat leading dots
889 for( i = 0; i < 6 && NewName[j] && NewName[j] != '.'; i ++, j ++ )
891 if( !isalpha(NewName[j]) && !is_valid_83_char(NewName[j]) )
894 ft.name[i] = toupper(NewName[j]);
898 while(i < 8) ft.name[i++] = ' ';
899 while(NewName[j] && NewName[j] != '.') j ++;
900 for( ; i < 8+3 && NewName[j]; i ++, j ++ )
902 if( NewName[j] == '.' )
904 else if( !is_valid_83_char(NewName[j]) )
907 ft.name[i] = toupper(NewName[j]);
909 while(i < 8+3) ft.name[i++] = ' ';
911 // - Ensure there isn't a duplicate short-name
912 int bIsDuplicate = 1;
913 while( bIsDuplicate )
915 bIsDuplicate = 0; // Assume none
918 for( int id = 0; ; id ++ )
922 if(FAT_int_ReadDirSector(DirNode, id/eps, fileinfo))
923 break; // end of cluster chain
927 if( fileinfo[id%eps].name[0] == '\0' ) break;
929 if( fileinfo[id%eps].name[0] == '\xE5' ) continue;
931 if( fileinfo[id%eps].attrib == ATTR_LFN ) continue;
933 // Is this a duplicate?
934 if( memcmp(ft.name, fileinfo[id%eps].name, 8+3) == 0 ) {
942 // If a duplicate was found, increment the suffix
945 if( ft.name[7] == '9' ) {
946 // TODO: Expand into ~00
947 Log_Error("FAT", "TODO: Use two digit LFN suffixes");
948 Mutex_Release(&DirNode->Lock);
958 // Create pure filetable entry
961 for( i = 0; i < 8 && *NewName && *NewName != '.'; i ++, NewName++ )
962 ft.name[i] = *NewName;
970 for( ; i < 8+3 && *NewName; i ++, NewName++ )
971 ft.name[i] = *NewName;
973 for( ; i < 8+3; i ++ )
978 if(NewNode->Flags & VFS_FFLAG_DIRECTORY )
979 ft.attrib |= ATTR_DIRECTORY;
981 FAT_int_GetFATTimestamp(NewNode->CTime, &ft.cdate, &ft.ctime, &ft.ctimems);
982 // ft.ctimems = ft.ctimems;
983 ft.ctime = LittleEndian16(ft.ctime);
984 ft.cdate = LittleEndian16(ft.cdate);
985 FAT_int_GetFATTimestamp(NewNode->MTime, &ft.mdate, &ft.mtime, NULL);
986 ft.mtime = LittleEndian16(ft.mtime);
987 ft.mdate = LittleEndian16(ft.mdate);
988 FAT_int_GetFATTimestamp(NewNode->ATime, &ft.adate, NULL, NULL);
989 ft.adate = LittleEndian16(ft.adate);
990 ft.clusterHi = LittleEndian16((NewNode->Inode >> 16) & 0xFFFF);
991 ft.cluster = LittleEndian16(NewNode->Inode & 0xFFFF);
992 ft.size = LittleEndian32(NewNode->Size);
994 LOG("ft.name = '%.11s'", ft.name);
996 // -- Add entry to the directory --
997 // Locate a range of nLFNEnt + 1 free entries
999 int range_first = 0, range_last = -1;
1000 for( int id = 0; ; id ++ )
1004 if(FAT_int_ReadDirSector(DirNode, id/eps, fileinfo))
1005 break; // end of cluster chain
1008 // End of file list, break out
1009 if( fileinfo[id%eps].name[0] == '\0' ) {
1010 if( id - range_first == nLFNEnt )
1016 // If an entry is occupied, clear the range
1017 if( fileinfo[id%eps].name[0] != '\xE5' ) {
1018 range_first = id + 1;
1022 // Free entry, check if we have enough
1023 if( id - range_first == nLFNEnt ) {
1027 // Check the next one
1029 if( range_last == -1 )
1031 // - If there are none, defragment the directory?
1033 // - Else, expand the directory
1034 if( end_id == -1 ) {
1035 // End of cluster chain
1038 // Just end of block
1040 // - and if that fails, return an error
1041 Log_Warning("FAT", "TODO: Impliment directory expansion / defragmenting");
1042 Mutex_Release(&DirNode->Lock);
1046 // Calculate the checksum used for LFN
1047 Uint8 lfn_checksum = 0;
1050 lfn_checksum = FAT_int_MakeLFNChecksum(ft.name);
1054 if( range_first % eps != 0 )
1055 FAT_int_ReadDirSector(DirNode, range_first/eps, fileinfo);
1056 for( int id = range_first; id <= range_last; id ++ )
1058 if( id % eps == 0 ) {
1059 if( id != range_first )
1060 FAT_int_WriteDirSector(DirNode, (id-1)/eps, fileinfo);
1061 FAT_int_ReadDirSector(DirNode, id/eps, fileinfo);
1064 if( id == range_last ) {
1066 memcpy(fileinfo + id % eps, &ft, sizeof(fat_filetable));
1070 int lfnid = (nLFNEnt - (id - range_first));
1071 int ofs = (lfnid-1) * 13;
1073 fat_longfilename *lfnent = (void*)( fileinfo + id%eps );
1075 lfnent->id = 0x40 | lfnid;
1076 lfnent->attrib = ATTR_LFN;
1078 lfnent->firstCluster = 0;
1079 lfnent->checksum = lfn_checksum; // ???
1081 for( i = 0; i < 13; i ++ )
1084 if( (wd = lfn[ofs+j]) ) j ++;
1085 wd = LittleEndian16(wd);
1087 lfnent->name1[i ] = wd;
1089 lfnent->name2[i-5 ] = wd;
1091 lfnent->name3[i-5-6] = wd;
1095 FAT_int_WriteDirSector(DirNode, range_last/eps, fileinfo);
1097 Mutex_Release( &DirNode->Lock );
1102 * \fn int FAT_Relink(tVFS_Node *Node, char *OldName, char *NewName)
1103 * \brief Rename / Delete a file
1105 int FAT_Unlink(tVFS_Node *Node, const char *OldName)
1110 if( Mutex_Acquire(&Node->Lock) ) {
1114 int id = FAT_int_GetEntryByName(Node, OldName, &ft);
1116 Mutex_Release(&Node->Lock);
1120 child = FAT_int_CreateNode(Node->ImplPtr, &ft);
1122 Mutex_Release(&Node->Lock);
1125 child->ImplInt |= FAT_FLAG_DELETE; // Mark for deletion on close
1127 // TODO: If it has a LFN, remove that too
1129 // Delete from the directory
1130 ft.name[0] = '\xE5';
1131 FAT_int_WriteDirEntry(Node, id, &ft);
1134 child->Type->Close( child );
1135 Mutex_Release( &Node->Lock );