Modules/FAT - Fixed write support
authorJohn Hodge <[email protected]>
Fri, 13 Jul 2012 03:28:39 +0000 (11:28 +0800)
committerJohn Hodge <[email protected]>
Fri, 13 Jul 2012 03:28:39 +0000 (11:28 +0800)
- LFN is correct enough for mtools
- Name mangling is a little incorrect, but works
 > A rework to fit the fatgen103 standard is underway
- Timestamps are possibly buggy

KernelLand/Modules/Filesystems/FAT/common.h
KernelLand/Modules/Filesystems/FAT/dir.c
KernelLand/Modules/Filesystems/FAT/fat.c
KernelLand/Modules/Filesystems/FAT/fatio.c
KernelLand/Modules/Filesystems/FAT/nodecache.c

index 5f8539c..79513a9 100644 (file)
@@ -84,6 +84,8 @@ struct sFAT_CachedNode
 
 // --- General Helpers ---
 extern int     FAT_int_GetAddress(tVFS_Node *Node, Uint64 Offset, Uint64 *Addr, Uint32 *Cluster);
+extern tTime   FAT_int_GetAcessTimestamp(Uint16 Date, Uint16 Time, Uint8 MS);
+extern void    FAT_int_GetFATTimestamp(tTime AcessTimestamp, Uint16 *Date, Uint16 *Time, Uint8 *MS);
 
 // --- Node Caching ---
 // NOTE: FAT uses its own node cache that references by cluster (not the inode value that the Inode_* cache uses)
index 3154b17..910b337 100644 (file)
@@ -305,7 +305,7 @@ int FAT_int_GetEntryByCluster(tVFS_Node *DirNode, Uint32 Cluster, fat_filetable
                
                if(fileinfo[i].attrib == ATTR_LFN)      continue;
 
-               LOG("fileinfo[i].cluster = %x %04x", fileinfo[i].clusterHi, fileinfo[i].cluster);
+               LOG("fileinfo[i].cluster = %x:%04x", fileinfo[i].clusterHi, fileinfo[i].cluster);
                #if DEBUG
                {
                        char    tmpName[13];
@@ -752,10 +752,23 @@ static inline int is_valid_83_char(char ch)
        if( 'A' <= ch && ch <= 'Z' )
                return 1;
        if( 'a' <= ch && ch <= 'z' )
-               return 0;
-       if( strchr(";+=[]',\"*\\<>/?:| ", ch) )
-               return 0;
-       return 1;
+               return 1;
+       if( strchr("$%'-_@~`#!(){}^#&", ch) )
+               return 1;
+       if( ch > 128 )
+               return 1;
+       return 0;
+}
+
+Uint8 FAT_int_UnicodeTo83(Uint32 Input)
+{
+       Input = toupper(Input);
+       // Input = unicode_to_oem(Input);
+       if( Input > 256 )
+               Input = '_';
+       if(!is_valid_83_char(Input))
+               Input = '_';
+       return Input;
 }
 
 /**
@@ -788,12 +801,24 @@ int FAT_int_IsValid83Filename(const char *Name)
        }
        
        // After the extension must be the end
-       if( !Name[i+j] )
+       if( Name[i+j] )
                return 0;
        
        return 1;
 }
 
+Uint8 FAT_int_MakeLFNChecksum(const char *ShortName)
+{
+       Uint8 ret = 0;
+       for( int i = 0; i < 11; i++ )
+       {
+               // ret = (ret >>> 1) + ShortName[i]
+               // where >>> is rotate right
+               ret = ((ret & 1) ? 0x80 : 0x00) + (ret >> 1) + ShortName[i];
+       }
+       return ret;
+}
+
 /**
  * \brief Create a new name for a file
  * \note Since FAT doesn't support reference counting, this will cause double-references if
@@ -816,6 +841,50 @@ int FAT_Link(tVFS_Node *DirNode, const char *NewName, tVFS_Node *NewNode)
        }
        
        // -- Create filetable entry --
+       #if 0
+       {
+                int    bDirty = 0;
+                int    inofs = 0;
+               while( NewName[inofs] && NewName[inofs] == '.' )
+                       inofs ++, bDirty = 1;
+               for( int i = 0; i < 8 && NewName[inofs] && NewName[inofs] != '.'; i ++ )
+               {
+                       Uint32  cp;
+                       inofs += ReadUTF8(NewName + inofs, &cp);
+                       // Spaces are silently skipped
+                       if(isspace(cp)) {
+                               i --, bDirty = 1;
+                               continue ;
+                       }
+                       ft.name[i] = FAT_int_UnicodeTo83(cp);
+                       if(ft.name[i] != cp)
+                               bDirty = 1;
+               }
+               while( NewName[inofs] && NewName[inofs] != '.' )
+                       inofs ++, bDirty = 1;
+               for( ; i < 8+3 && NewName[inofs]; i ++ )
+               {
+                       Uint32  cp;
+                       inofs += ReadUTF8(NewName + inofs, &cp);
+                       // Spaces are silently skipped
+                       if(isspace(cp)) {
+                               i --, bDirty = 1;
+                               continue ;
+                       }
+                       ft.name[i] = FAT_int_UnicodeTo83(cp);
+                       if(ft.name[i] != cp)
+                               bDirty = 1;
+               }
+               if( !NewName[inofs] )   bDirty = 1;
+               
+               if( bDirty )
+               {
+                       int lfnlen = FAT_int_ConvertUTF8_to_UTF16(lfn, (const Uint8*)NewName);
+                       lfn[lfnlen] = 0;
+                       nLFNEnt = DivUp(lfnlen, 13);
+               }
+       }
+       #endif
         int    bNeedsLFN = !FAT_int_IsValid83Filename(NewName);
        if( bNeedsLFN )
        {
@@ -834,7 +903,7 @@ int FAT_Link(tVFS_Node *DirNode, const char *NewName, tVFS_Node *NewNode)
                                ft.name[i] = toupper(NewName[j]);
                }
                ft.name[i++] = '~';
-               ft.name[i++] = '0';
+               ft.name[i++] = '1';
                while(i < 8)    ft.name[i++] = ' ';
                while(NewName[j] && NewName[j] != '.')  j ++;
                for( ; i < 8+3 && NewName[j]; i ++, j ++ )
@@ -917,7 +986,16 @@ int FAT_Link(tVFS_Node *DirNode, const char *NewName, tVFS_Node *NewNode)
        ft.attrib = 0;
        if(NewNode->Flags & VFS_FFLAG_DIRECTORY )
                ft.attrib |= ATTR_DIRECTORY;    
-       // TODO: Fill in creation/modifcation times
+       ft.ntres     = 0;
+       FAT_int_GetFATTimestamp(NewNode->CTime, &ft.cdate, &ft.ctime, &ft.ctimems);
+//     ft.ctimems   = ft.ctimems;
+       ft.ctime     = LittleEndian16(ft.ctime);
+       ft.cdate     = LittleEndian16(ft.cdate);
+       FAT_int_GetFATTimestamp(NewNode->MTime, &ft.mdate, &ft.mtime, NULL);
+       ft.mtime     = LittleEndian16(ft.mtime);
+       ft.mdate     = LittleEndian16(ft.mdate);
+       FAT_int_GetFATTimestamp(NewNode->ATime, &ft.adate, NULL, NULL);
+       ft.adate     = LittleEndian16(ft.adate);
        ft.clusterHi = LittleEndian16((NewNode->Inode >> 16) & 0xFFFF);
        ft.cluster   = LittleEndian16(NewNode->Inode & 0xFFFF);
        ft.size      = LittleEndian32(NewNode->Size);
@@ -974,6 +1052,13 @@ int FAT_Link(tVFS_Node *DirNode, const char *NewName, tVFS_Node *NewNode)
                return ENOTIMPL;
        }
 
+       // Calculate the checksum used for LFN
+       Uint8   lfn_checksum = 0;
+       if( nLFNEnt )
+       {
+               lfn_checksum = FAT_int_MakeLFNChecksum(ft.name);
+       }
+
        // Insert entries       
        if( range_first % eps != 0 )
                FAT_int_ReadDirSector(DirNode, range_first/eps, fileinfo);
@@ -1000,7 +1085,7 @@ int FAT_Link(tVFS_Node *DirNode, const char *NewName, tVFS_Node *NewNode)
                        lfnent->attrib = ATTR_LFN;
                        lfnent->type = 0;
                        lfnent->firstCluster = 0;
-                       lfnent->checksum = 0;   // ???
+                       lfnent->checksum = lfn_checksum;        // ???
 
                        for( i = 0; i < 13; i ++ )
                        {
@@ -1014,8 +1099,6 @@ int FAT_Link(tVFS_Node *DirNode, const char *NewName, tVFS_Node *NewNode)
                                else
                                        lfnent->name3[i-5-6] = wd;
                        }
-                       
-                       lfnent->checksum = 0;   // ???
                }
        }
        FAT_int_WriteDirSector(DirNode, range_last/eps, fileinfo);
index f7dd8c5..bb90c3c 100644 (file)
@@ -17,7 +17,7 @@
  * \todo Implement changing of the parent directory when a file is written to\r
  * \todo Implement file creation / deletion\r
  */\r
-#define DEBUG  0\r
+#define DEBUG  1\r
 #define VERBOSE        1\r
 \r
 #include <acess.h>\r
@@ -483,9 +483,12 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe
         int    remLength = Length;\r
        Uint32  cluster, tmpCluster;\r
         int    bNewCluster = 0;\r
+       off_t   original_offset = Offset;\r
        \r
        if(Offset > Node->Size) return 0;\r
        \r
+       ENTER("pNode Xoffset xlength pbuffer", Node, Offset, Length, Buffer);\r
+       \r
        // Seek Clusters\r
        cluster = Node->Inode & 0xFFFFFFFF;\r
        while( Offset > disk->BytesPerCluster )\r
@@ -493,6 +496,7 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe
                cluster = FAT_int_GetFatValue( disk, cluster );\r
                if(cluster == -1) {\r
                        Log_Warning("FAT", "EOC Unexpectedly Reached");\r
+                       LEAVE('i', 0);\r
                        return 0;\r
                }\r
                Offset -= disk->BytesPerCluster;\r
@@ -500,7 +504,10 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe
        if( Offset == disk->BytesPerCluster )\r
        {\r
                Uint32  tmp = FAT_int_AllocateCluster(disk, cluster);\r
-               if(!tmp)        return 0;\r
+               if(!tmp) {\r
+                       LEAVE('i', 0);\r
+                       return 0;\r
+               }\r
                cluster = tmp;\r
                Offset -= disk->BytesPerCluster;\r
        }\r
@@ -509,17 +516,20 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe
        {\r
                char    tmpBuf[disk->BytesPerCluster];\r
                \r
+               LOG("Read-Modify-Write single");\r
+               \r
                // Read-Modify-Write\r
                FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf );\r
                memcpy( tmpBuf + Offset, Buffer, Length );\r
                FAT_int_WriteCluster( disk, cluster, tmpBuf );\r
-               \r
-               return Length;\r
+               goto return_full;\r
        }\r
        \r
        // Clean up changes within a cluster\r
        if( Offset )\r
        {       \r
+               LOG("Read-Modify-Write first");\r
+\r
                // Read-Modify-Write\r
                FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf );\r
                memcpy( tmpBuf + Offset, Buffer, disk->BytesPerCluster - Offset );\r
@@ -532,9 +542,8 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe
                tmpCluster = FAT_int_GetFatValue(disk, cluster);\r
                if(tmpCluster == -1) {\r
                        tmpCluster = FAT_int_AllocateCluster(disk, cluster);\r
-                       if( tmpCluster == 0 ) {\r
-                               return Length - remLength;\r
-                       }\r
+                       if( tmpCluster == 0 )\r
+                               goto ret_incomplete;\r
                }\r
                cluster = tmpCluster;\r
        }\r
@@ -543,28 +552,47 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe
        {\r
                FAT_int_WriteCluster( disk, cluster, Buffer );\r
                Buffer += disk->BytesPerCluster;\r
+               remLength -= disk->BytesPerCluster;\r
                \r
                // Get next cluster (allocating if needed)\r
                tmpCluster = FAT_int_GetFatValue(disk, cluster);\r
                if(tmpCluster == -1) {\r
                        bNewCluster = 1;\r
                        tmpCluster = FAT_int_AllocateCluster(disk, cluster);\r
-                       if( tmpCluster == 0 ) {\r
-                               return Length - remLength;\r
-                       }\r
+                       if( tmpCluster == 0 )\r
+                               goto ret_incomplete;\r
                }\r
                cluster = tmpCluster;\r
        }\r
        \r
        // Finish off\r
-       if( bNewCluster )\r
-               memset(tmpBuf, 0, disk->BytesPerCluster);\r
-       else\r
-               FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf );\r
-       memcpy( tmpBuf, Buffer, remLength );\r
-       FAT_int_WriteCluster( disk, cluster, tmpBuf );\r
-       free( tmpBuf );\r
-       \r
+       if( remLength )\r
+       {\r
+               if( bNewCluster )\r
+                       memset(tmpBuf, 0, disk->BytesPerCluster);\r
+               else\r
+                       FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf );\r
+               memcpy( tmpBuf, Buffer, remLength );\r
+               FAT_int_WriteCluster( disk, cluster, tmpBuf );\r
+       }\r
+\r
+return_full:\r
+       if( original_offset + Length > Node->Size ) {\r
+               Node->Size = original_offset + Length;\r
+               LOG("Updated size to %x", Node->Size);\r
+               Node->ImplInt |= FAT_FLAG_DIRTY;\r
+       }\r
+\r
+       LEAVE('i', Length);\r
+       return Length;\r
+ret_incomplete:\r
+       LOG("Write incomplete");\r
+       Length -= remLength;\r
+       if( original_offset + Length > Node->Size ) {\r
+               Node->Size = original_offset + Length;  \r
+               Node->ImplInt |= FAT_FLAG_DIRTY;\r
+       }\r
+       LEAVE('i', Length);\r
        return Length;\r
 }\r
 #endif\r
@@ -577,7 +605,9 @@ void FAT_CloseFile(tVFS_Node *Node)
 {\r
        tFAT_VolInfo    *disk = Node->ImplPtr;\r
        if(Node == NULL)        return ;\r
-       \r
+\r
+       ENTER("pNode", Node);   \r
+\r
        #if SUPPORT_WRITE\r
        // Update the node if it's dirty (don't bother if it's marked for\r
        // deletion)\r
@@ -589,6 +619,7 @@ void FAT_CloseFile(tVFS_Node *Node)
                dirnode = FAT_int_CreateIncompleteDirNode(disk, Node->Inode >> 32);\r
                if( !dirnode ) {\r
                        Log_Error("FAT", "Can't get node for directory cluster #0x%x", Node->Inode>>32);\r
+                       LEAVE('-');\r
                        return ;\r
                }\r
 \r
@@ -619,4 +650,5 @@ void FAT_CloseFile(tVFS_Node *Node)
                }\r
        }\r
        #endif\r
+       LEAVE('-');\r
 }\r
index ab49b30..0652f0f 100644 (file)
@@ -35,7 +35,8 @@ Uint32 FAT_int_GetFatValue(tFAT_VolInfo *Disk, Uint32 cluster)
                ofs = Disk->bootsect.resvSectCount*512;
                if(Disk->type == FAT12) {
                        VFS_ReadAt(Disk->fileHandle, ofs+(cluster/2)*3, 3, &val);
-                       val = (cluster & 1 ? val>>12 : val & 0xFFF);
+                       LOG("3 bytes at 0x%x are (Uint32)0x%x", ofs+(cluster/2)*3, val);
+                       val = (cluster & 1) ? (val>>12) : (val & 0xFFF);
                        if(val == EOC_FAT12)    val = -1;
                } else if(Disk->type == FAT16) {
                        VFS_ReadAt(Disk->fileHandle, ofs+cluster*2, 2, &val);
@@ -108,6 +109,7 @@ Uint32 FAT_int_AllocateCluster(tFAT_VolInfo *Disk, Uint32 Previous)
                }
                        
                Mutex_Release(&Disk->lFAT);
+               LOG("Allocated cluster %x", ret);
                return ret;
        }
        else
@@ -137,7 +139,7 @@ Uint32 FAT_int_AllocateCluster(tFAT_VolInfo *Disk, Uint32 Previous)
                        
                        // Search within the same block as the previous cluster first
                        do {
-                               VFS_ReadAt(Disk->fileHandle, base + block, block_size, sector_data);
+                               VFS_ReadAt(Disk->fileHandle, base + block*block_size, block_size, sector_data);
                                for( block_ofs = 0; block_ofs < ents_per_block_12; block_ofs ++ )
                                {
                                        Uint32  *valptr = (void*)( sector_data + block_ofs / 2 * 3 );
@@ -166,16 +168,21 @@ Uint32 FAT_int_AllocateCluster(tFAT_VolInfo *Disk, Uint32 Previous)
                                VFS_WriteAt(Disk->fileHandle, base + block, block_size, sector_data);
        
                                // Note the new cluster in the chain
-                               VFS_ReadAt(Disk->fileHandle, base + (Previous>>1)*3, 3, &val); 
-                               if( ret & 1 ) {
-                                       val &= 0x000FFF;
-                                       val |= ret << 12;
-                               }
-                               else {
-                                       val &= 0xFFF000;
-                                       val |= ret << 0;
+                               if( Previous != -1 )
+                               {
+                                       LOG("Updating cluster %x to point to %x (offset %x)", Previous, ret,
+                                               base + (Previous>>1)*3);
+                                       VFS_ReadAt(Disk->fileHandle, base + (Previous>>1)*3, 3, &val); 
+                                       if( Previous & 1 ) {
+                                               val &= 0x000FFF;
+                                               val |= ret << 12;
+                                       }
+                                       else {
+                                               val &= 0xFFF000;
+                                               val |= ret << 0;
+                                       }
+                                       VFS_WriteAt(Disk->fileHandle, base + (Previous>>1)*3, 3, &val);
                                }
-                               VFS_WriteAt(Disk->fileHandle, base + (Previous>>1)*3, 3, &val);
                        }
                        break;
                case FAT16:
@@ -190,6 +197,7 @@ Uint32 FAT_int_AllocateCluster(tFAT_VolInfo *Disk, Uint32 Previous)
                        break;
                }
                Mutex_Release(&Disk->lFAT);
+               LOG("Allocated cluster %x", ret);
                return ret;
        #if CACHE_FAT
        }
index c862dca..a9cdbd6 100644 (file)
 extern tVFS_Node       *FAT_int_CacheNode(tFAT_VolInfo *Disk, const tVFS_Node *Node);
 
 // === CODE ===
+tTime FAT_int_GetAcessTimestamp(Uint16 Date, Uint16 Time, Uint8 MS)
+{
+       return MS * 10 + timestamp(
+               // Seconds         Minutes              Hours
+               (Time & 0x1F) * 2, (Time >> 5) & 0x3F, (Time >> 11) & 0x1F,
+               // Day             Month                    Year
+               (Date & 0x1F) - 1, ((Date >> 5) & 0xF) - 1, 1980 + ((Date >> 9) & 0xFF)
+               );
+}
+
+void FAT_int_GetFATTimestamp(tTime AcessTimestamp, Uint16 *Date, Uint16 *Time, Uint8 *MS)
+{
+        int    y, m, d;
+        int    h, min, s, ms;
+       format_date(AcessTimestamp, &y, &m, &d, &h, &min, &s, &ms);
+       if(Date)
+               *Date = (d + 1) | ((m + 1) << 5) | ((y - 1980) << 9);
+       if(Time)
+               *Time = (s / 2) | (min << 5) | (h << 11);
+       if(MS)
+               *MS = (ms / 10) + (s & 1) * 100;
+}
+
 /**
  * \brief Creates a tVFS_Node structure for a given file entry
  * \param Parent       Parent directory VFS node
@@ -58,30 +81,9 @@ tVFS_Node *FAT_int_CreateNode(tVFS_Node *Parent, fat_filetable *Entry)
        }
        
        // Create timestamps
-       node.ATime = timestamp(0,0,0,
-                       ((Entry->adate&0x1F) - 1),      // Days
-                       ((Entry->adate&0x1E0) - 1),     // Months
-                       1980+((Entry->adate&0xFF00)>>8) // Years
-                       );
-       
-       node.CTime = Entry->ctimems * 10;       // Miliseconds
-       node.CTime += timestamp(
-                       ((Entry->ctime&0x1F)<<1),       // Seconds
-                       ((Entry->ctime&0x3F0)>>5),      // Minutes
-                       ((Entry->ctime&0xF800)>>11),    // Hours
-                       ((Entry->cdate&0x1F)-1),                // Days
-                       ((Entry->cdate&0x1E0)-1),               // Months
-                       1980+((Entry->cdate&0xFF00)>>8) // Years
-                       );
-                       
-       node.MTime = timestamp(
-                       ((Entry->mtime&0x1F)<<1),       // Seconds
-                       ((Entry->mtime&0x3F0)>>5),      // Minutes
-                       ((Entry->mtime&0xF800)>>11),    // Hours
-                       ((Entry->mdate&0x1F)-1),                // Days
-                       ((Entry->mdate&0x1E0)-1),               // Months
-                       1980+((Entry->mdate&0xFF00)>>8) // Years
-                       );
+       node.CTime = FAT_int_GetAcessTimestamp(Entry->cdate, Entry->ctime, Entry->ctimems);
+       node.MTime = FAT_int_GetAcessTimestamp(Entry->mdate, Entry->mtime, 0);
+       node.ATime = FAT_int_GetAcessTimestamp(Entry->adate, 0, 0);
        
        // Set pointers
        if(node.Flags & VFS_FFLAG_DIRECTORY) {
@@ -101,9 +103,28 @@ tVFS_Node *FAT_int_CreateNode(tVFS_Node *Parent, fat_filetable *Entry)
 
 tVFS_Node *FAT_int_CreateIncompleteDirNode(tFAT_VolInfo *Disk, Uint32 Cluster)
 {
+       if( Cluster == Disk->rootOffset )
+               return &Disk->rootNode;
+       
        // If the directory isn't in the cache, what do?
        // - we want to lock it such that we don't collide, but don't want to put crap data in the cache
        // - Put a temp node in with a flag that indicates it's incomplete?
+       
+       Mutex_Acquire(&Disk->lNodeCache);
+       tFAT_CachedNode *cnode;
+
+       for(cnode = Disk->NodeCache; cnode; cnode = cnode->Next)
+       {
+               if( (cnode->Node.Inode & 0xFFFFFFFF) == Cluster ) {
+                       cnode->Node.ReferenceCount ++;
+                       Mutex_Release(&Disk->lNodeCache);
+                       return &cnode->Node;
+               }
+       }       
+
+       // Create a temporary node?
+
+       Mutex_Release(&Disk->lNodeCache);
        return NULL;
 }
 

UCC git Repository :: git.ucc.asn.au