userlibandfileserver/fileserver/sfat/sl_vfat.cpp
changeset 9 96e5fb8b040d
equal deleted inserted replaced
-1:000000000000 9:96e5fb8b040d
       
     1 // Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // f32\sfat\sl_vfat.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
       
    19 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
       
    20 //!!
       
    21 //!! WARNING!! DO NOT edit this file !! '\sfat' component is obsolete and is not being used. '\sfat32'replaces it
       
    22 //!!
       
    23 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
       
    24 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
       
    25 
       
    26 
       
    27 #include "sl_std.h"
       
    28 #include "sl_cache.h"
       
    29 #include <e32svr.h>
       
    30 #include <e32math.h>
       
    31 
       
    32 
       
    33 IMPORT_C const TFatUtilityFunctions* GetFatUtilityFunctions();
       
    34 
       
    35 const TInt KMaxLengthWithoutTilde = 8;
       
    36 const TUint8 KLeadingE5Replacement = 0x05;
       
    37 
       
    38 // use second half of ISO Latin 1 character set for extended chars
       
    39 const TUint KExtendedCharStart=0x80;
       
    40 const TUint KExtendedCharEnd=0xff;
       
    41 
       
    42 LOCAL_C TBool IsLegalChar(TChar aCharacter,TBool aAllowWildChars,TBool aUseExtendedChars=EFalse,TBool aInScanDrive=EFalse)
       
    43 //
       
    44 // Returns ETrue if aCharacter is legal inside a dos filename
       
    45 //
       
    46     {
       
    47     if ((aCharacter==KMatchOne) || (aCharacter==KMatchAny))
       
    48         return(aAllowWildChars);
       
    49     if ((TUint)aCharacter < 0x20)
       
    50         return EFalse;
       
    51     // Don't check illegal ascii char because some non-English char value may
       
    52     // fall in this area
       
    53     if (aInScanDrive)
       
    54         return ETrue;
       
    55     return LocaleUtils::IsLegalShortNameCharacter(aCharacter,aUseExtendedChars);
       
    56     }
       
    57 
       
    58 LOCAL_C void ReplaceFirstCharacterIfClashesWithE5L(TDes8& aShortName)
       
    59     {
       
    60     if (0 < aShortName.Length() && aShortName[0] == KEntryErasedMarker)
       
    61         {
       
    62         aShortName[0] = KLeadingE5Replacement;
       
    63         }
       
    64     }
       
    65 
       
    66 LOCAL_C void ReplaceIllegalCharactersL(TDes& aLongName, TUint aCharacterToReplaceWith)
       
    67     {
       
    68     TBool alreadyFoundExtensionDelimiter=EFalse;
       
    69 
       
    70     TInt LongNameLen = aLongName.Length();
       
    71     TInt extDelimiterIndex = aLongName.LocateReverse(KExtDelimiter);
       
    72 
       
    73     for (TInt i=LongNameLen-1; i>=0; --i) // iterate backwards as aLongName may change length during the loop, and also because we want to leave the *right-most* occurrence of KExtDelimiter unchanged
       
    74         {
       
    75         TUint character=aLongName[i];
       
    76         if (character==(TUint)KExtDelimiter)
       
    77             {
       
    78             if (alreadyFoundExtensionDelimiter)
       
    79                 {
       
    80                 aLongName[i]=(TText)aCharacterToReplaceWith; // A.B.C becomes A_B.C
       
    81                 }
       
    82             alreadyFoundExtensionDelimiter=ETrue;
       
    83             }
       
    84         else
       
    85             {
       
    86             // the code below doesn't need any #if defined(_UNICODE) stuff as a narrow-build aLongName would not contain values above 0xff (which is well below the surrogates area in Unicode 0xd800-0xdfff)
       
    87             TBool isSurrogatePair=EFalse;
       
    88 
       
    89             // LAST character in file name or file ext CAN NOT be HIGH surrogate
       
    90             if (i==LongNameLen-1 || i==extDelimiterIndex-1)
       
    91                 {
       
    92                 if (IsHighSurrogate((TText16)character))
       
    93                     {
       
    94                     // Corrupt surrogate
       
    95                     User::Leave(KErrBadName);
       
    96                     }
       
    97                 }
       
    98             // FIRST character in file name or file ext CAN NOT be LOW surrogate
       
    99             if (i==0 || i==extDelimiterIndex+1)
       
   100                 {
       
   101                 if (IsLowSurrogate((TText16)character))
       
   102                     {
       
   103                     // Corrupt surrogate
       
   104                     User::Leave(KErrBadName);
       
   105                     }
       
   106                 }
       
   107             // if LOW Surrogate
       
   108             if (IsLowSurrogate((TText16)character))
       
   109                 {
       
   110                 // check for HIGH surrogate
       
   111                 if (!IsHighSurrogate(aLongName[--i]))
       
   112                     {
       
   113                     // Corrupt surrogate
       
   114                     User::Leave(KErrBadName);
       
   115                     }
       
   116                 // surrogate pair found
       
   117                 character&=~0xdc00;
       
   118                 character|=((aLongName[i]&~0xd800)<<10);
       
   119                 character+=0x00010000; // this must be added - it cannot be bitwise-"or"-ed
       
   120                 isSurrogatePair=ETrue;
       
   121                 }
       
   122 
       
   123             // if High Surrogate
       
   124             if (!isSurrogatePair && IsHighSurrogate((TText16)character))
       
   125                 {
       
   126                 // Corrupt surrogate
       
   127                 User::Leave(KErrBadName);
       
   128                 }
       
   129 
       
   130             if (!IsLegalChar(character, EFalse))
       
   131                 {
       
   132                 if (isSurrogatePair)
       
   133                     {
       
   134                     aLongName.Delete(i+1, 1);
       
   135                     }
       
   136                 aLongName[i]=(TText)aCharacterToReplaceWith;
       
   137                 }
       
   138             }
       
   139         }
       
   140     }
       
   141 
       
   142 TShortName DoGenerateShortNameL(const TDesC& aLongName,TInt& aNum,TBool aUseTildeSelectively)
       
   143 //
       
   144 // Create a legal shortname from aLongName
       
   145 //
       
   146     {
       
   147 
       
   148     TFileName longName(aLongName);
       
   149     longName.UpperCase();
       
   150     ReplaceIllegalCharactersL(longName, '_');
       
   151     TPtrC longNameWithoutExtension(longName);
       
   152     TPtrC longNameExtension(KNullDesC);
       
   153     const TInt positionOfExtension=longName.LocateReverse(KExtDelimiter);
       
   154     if (positionOfExtension==0)
       
   155         {
       
   156         // No filename specified, so use the extension as the basis of the shortname.
       
   157         // Make sure we always append a tilde+number in this case to avoid generating the same 
       
   158         // short filename as one of the protected folders ("\SYS", "\RESOURCE","\PRIVATE")
       
   159         longNameWithoutExtension.Set(longName.Mid(positionOfExtension+1));
       
   160         aUseTildeSelectively = EFalse;
       
   161         if (aNum < 0)
       
   162             aNum = 1;
       
   163         }
       
   164     else if (positionOfExtension!=KErrNotFound)
       
   165         {
       
   166         longNameWithoutExtension.Set(longName.Left(positionOfExtension));
       
   167         longNameExtension.Set(longName.Mid(positionOfExtension+1));
       
   168         }
       
   169     
       
   170     // Converts the original file name main part into 8-bit character string
       
   171     TShortName tempShortName(0);
       
   172 
       
   173     LocaleUtils::ConvertFromUnicodeL(tempShortName, longNameWithoutExtension);
       
   174     const TInt originalNameLength = tempShortName.Length();
       
   175 
       
   176     // Converts the original file name extension part into 8-bit character string
       
   177     TShortName tempShortNameExt(0);
       
   178     
       
   179     LocaleUtils::ConvertFromUnicodeL(tempShortNameExt, longNameExtension);
       
   180     const TInt extensionNameLength = tempShortNameExt.Length();
       
   181     // // const TInt extensionNameLength = tempShortNameExt.Length();
       
   182 
       
   183     // Checks the length of both original file name main part and original file name extension part
       
   184     if(aUseTildeSelectively)
       
   185         {
       
   186         // don't append ~<aNum>
       
   187         if(originalNameLength<=KMaxLengthWithoutTilde && extensionNameLength<=KMaxFatFileNameExt)
       
   188             aNum=-1;
       
   189         }
       
   190 
       
   191     // Applies tilde and number if necessary
       
   192     TBuf8<5> tildeAndNumber;
       
   193     if (aNum>=0)
       
   194         {
       
   195         tildeAndNumber.Append('~');
       
   196         tildeAndNumber.AppendNumUC(aNum,EHex);
       
   197         }
       
   198     const TInt lengthOfTildeAndNumber=tildeAndNumber.Length();
       
   199 
       
   200     // Creates actual shortname from longname of the original file
       
   201     TShortName shortName(11);
       
   202 #if defined(_DEBUG)
       
   203     shortName.Fill(0x01); // fill shortName with garbage to ensure that every byte is written to by this function
       
   204 #endif
       
   205 
       
   206     // Fills the main part of the shortname of the original file
       
   207     const TInt numberOfBytesFreeBeforeTilde=KMaxFatFileNameWithoutExt-lengthOfTildeAndNumber;
       
   208 
       
   209     TPtr8 portionOfShortNameBeforeTilde((TUint8*)shortName.Ptr(), 0, numberOfBytesFreeBeforeTilde);
       
   210     TInt lengthOfPortionOfShortNameBeforeTilde = 
       
   211                 (originalNameLength < numberOfBytesFreeBeforeTilde) ? originalNameLength : numberOfBytesFreeBeforeTilde;
       
   212 
       
   213     portionOfShortNameBeforeTilde.Copy((TUint8*)tempShortName.Ptr(), lengthOfPortionOfShortNameBeforeTilde);
       
   214     if( lengthOfPortionOfShortNameBeforeTilde != originalNameLength)
       
   215         {
       
   216         for( int i = 0; i<lengthOfPortionOfShortNameBeforeTilde; i++)
       
   217             {
       
   218             if(portionOfShortNameBeforeTilde[i] >= 0x80) //leading byte found
       
   219                 {
       
   220                 if( i == lengthOfPortionOfShortNameBeforeTilde - 1) //leading byte found on the edge
       
   221                     {
       
   222                     lengthOfPortionOfShortNameBeforeTilde -= 1;
       
   223                     break;
       
   224                     }
       
   225                 else
       
   226                     {
       
   227                     i++;
       
   228                     }
       
   229                 }
       
   230             }
       
   231         }
       
   232     Mem::Copy(((TUint8*)shortName.Ptr())+lengthOfPortionOfShortNameBeforeTilde, tildeAndNumber.Ptr(), lengthOfTildeAndNumber);
       
   233     TInt i;
       
   234     for (i=lengthOfPortionOfShortNameBeforeTilde+lengthOfTildeAndNumber; i<KMaxFatFileNameWithoutExt; ++i)
       
   235         {
       
   236         shortName[i]=' ';
       
   237         }
       
   238 
       
   239     // Fills the extension part of the shortname of the original file
       
   240     TInt lengthOfExt = 
       
   241                 (extensionNameLength < KMaxFatFileNameExt) ? extensionNameLength : KMaxFatFileNameExt;
       
   242     
       
   243     if( lengthOfExt != extensionNameLength)
       
   244         {
       
   245         for( int i = 0; i<lengthOfExt; i++)
       
   246             {
       
   247             if(tempShortNameExt[i] >= 0x80)
       
   248                 {
       
   249                 if( i == lengthOfExt - 1)
       
   250                     {
       
   251                     lengthOfExt -= 1;
       
   252                     break;
       
   253                     }
       
   254                 else
       
   255                     {
       
   256                     i++;
       
   257                     }
       
   258                 }
       
   259             }
       
   260         }           
       
   261     Mem::Copy(((TUint8*)shortName.Ptr()) + KMaxFatFileNameWithoutExt, tempShortNameExt.Ptr(), lengthOfExt);
       
   262     for (i = KMaxFatFileNameWithoutExt + lengthOfExt; i<KMaxFatFileNameWithoutExt+KMaxFatFileNameExt; ++i)
       
   263         {
       
   264         shortName[i]=' ';
       
   265         }
       
   266     
       
   267     ReplaceFirstCharacterIfClashesWithE5L(shortName);       
       
   268     return shortName;
       
   269     }
       
   270 
       
   271 
       
   272 /**
       
   273 Check whether a Dos name is legal or not.
       
   274 
       
   275 @param aName                The entry name to be analysed (may be represented as TDes16& or TDes8&)
       
   276 @param anAllowWildCards     Flag to indicate whether to allow wildcards in name or not
       
   277 @param aUseExtendedChars    Flag to indicate if extended characters are allowed
       
   278 @param aInScanDrive         Flag to indicate whether called when scanning drive
       
   279 @param aAllowLowerCase      ETrue to allow lower case in the analysed DOS name
       
   280 
       
   281 @return ETrue if the name is a legal DOS one.
       
   282 */
       
   283 
       
   284 static TBool DoCheckLegalDosName(const TDesC& aName, TBool anAllowWildCards, TBool aUseExtendedChars, TBool aInScanDrive, TBool aAllowLowerCase, TBool aIsForFileCreation)
       
   285     {
       
   286     const TInt count=aName.Length();
       
   287     if (count==0)
       
   288         return EFalse;
       
   289 
       
   290     TInt valid=0;
       
   291     TInt i=0;
       
   292     
       
   293     //-- check the entry name
       
   294     while (i<count)
       
   295         {
       
   296         TChar c=aName[i++];
       
   297         if (c==KExtDelimiter)
       
   298             {
       
   299             // DOS entry names must contain at least one valid character before the extension
       
   300             if (i == 1)
       
   301                 return EFalse;
       
   302             break;
       
   303             }
       
   304         
       
   305           if(!aAllowLowerCase && c.IsLower())
       
   306             return EFalse; //-- low case is not allowed
       
   307 
       
   308         if (!IsLegalChar(c,anAllowWildCards,aUseExtendedChars,aInScanDrive))
       
   309             {
       
   310             return EFalse;
       
   311             }
       
   312         
       
   313         if (aIsForFileCreation)
       
   314             {
       
   315             if ((aUseExtendedChars && (TUint) c > KExtendedCharEnd) || 
       
   316                     (!aUseExtendedChars && (TUint) c > KExtendedCharStart))
       
   317                 {
       
   318                 return EFalse;
       
   319                 }
       
   320             }
       
   321 
       
   322         if (c!=KMatchAny)
       
   323             if (++valid>KMaxFatFileNameWithoutExt)
       
   324                 return EFalse;
       
   325         }
       
   326     
       
   327     //-- check entry extension
       
   328     valid=0;
       
   329     while (i<count)
       
   330         {
       
   331         TChar c=aName[i++];
       
   332         if (c==KExtDelimiter)
       
   333             return EFalse;
       
   334         
       
   335         if(!aAllowLowerCase && c.IsLower())
       
   336             return EFalse; //-- low case is not allowed
       
   337 
       
   338         if (!IsLegalChar(c,anAllowWildCards,aUseExtendedChars,aInScanDrive))
       
   339             return EFalse;
       
   340         
       
   341         if (aIsForFileCreation)
       
   342             {
       
   343             if ((aUseExtendedChars && (TUint) c > KExtendedCharEnd) || 
       
   344                     (!aUseExtendedChars && (TUint) c > KExtendedCharStart))
       
   345                 {
       
   346                 return EFalse;
       
   347                 }
       
   348             }
       
   349 
       
   350         if (c!=KMatchAny)
       
   351             if (++valid>KMaxFatFileNameExt)
       
   352                 return EFalse;
       
   353         }
       
   354     
       
   355     // Unicode file name checking for file opening.
       
   356     if (!aIsForFileCreation && GetFatUtilityFunctions())
       
   357         {
       
   358         TBuf8<KMaxFileName*2> convertedName8;
       
   359         TRAPD(err, LocaleUtils::ConvertFromUnicodeL(convertedName8, aName, TFatUtilityFunctions::EOverflowActionLeave));
       
   360         if (err != KErrNone)
       
   361             return EFalse;
       
   362         
       
   363         const TInt len8 = convertedName8.Length();
       
   364         TInt j = 0; 
       
   365         TInt nonWildChar = 0;
       
   366         while (j < len8)
       
   367             {
       
   368             const TUint8 c8 = convertedName8[j++];
       
   369             if (c8 == KExtDelimiter)
       
   370                 break;
       
   371             if (c8 == '*' && !anAllowWildCards)
       
   372                 return EFalse;
       
   373             if (c8 == '*' && anAllowWildCards)
       
   374                 continue;
       
   375             
       
   376             if (++nonWildChar > KMaxFatFileNameWithoutExt)
       
   377                 return EFalse;
       
   378             }
       
   379         
       
   380         // check extension part
       
   381         nonWildChar = 0;
       
   382         while (j < len8)
       
   383             {
       
   384             const TUint8 c8 = convertedName8[j++];
       
   385             if (c8 == KExtDelimiter)
       
   386                 return EFalse;
       
   387             if (c8 == '*' && !anAllowWildCards)
       
   388                 return EFalse;
       
   389             if (c8 == '*' && anAllowWildCards)
       
   390                 continue;
       
   391             
       
   392             if (++nonWildChar > KMaxFatFileNameExt)
       
   393                 return EFalse;
       
   394             }
       
   395         }
       
   396 
       
   397     return ETrue;
       
   398     }
       
   399 
       
   400 /**
       
   401     Check whether a Dos name is legal or not. Unicode version
       
   402     parameters and return value absolutely the same as in DoCheckLegalDosName()
       
   403 */
       
   404 TBool IsLegalDosName(const TDesC16& aName, TBool anAllowWildCards, TBool aUseExtendedChars, TBool aInScanDrive, TBool aAllowLowerCase, TBool aIsForFileCreation)
       
   405     {
       
   406 
       
   407     __PRINT(_L("IsLegalDosName 16"));
       
   408 
       
   409     return DoCheckLegalDosName(aName, anAllowWildCards, aUseExtendedChars, aInScanDrive, aAllowLowerCase, aIsForFileCreation);  
       
   410     }
       
   411 
       
   412 TBool CFatMountCB::FindShortNameL(const TShortName& aName,TEntryPos& anEntryPos)
       
   413 //
       
   414 // Returns ETrue and the entryPos of aName if found or EFalse
       
   415 //
       
   416     {
       
   417     
       
   418     __PRINT(_L("VFAT::CFatMountCB::FindShortNameL"));   
       
   419     TFatDirEntry fatEntry;
       
   420     TInt count=0;
       
   421     FOREVER
       
   422         {
       
   423         count++;
       
   424         ReadDirEntryL(anEntryPos,fatEntry);
       
   425         MoveToDosEntryL(anEntryPos,fatEntry);
       
   426         if (fatEntry.IsEndOfDirectory())
       
   427             break;
       
   428         if (IsRootDir(anEntryPos)&&(anEntryPos.iPos+StartOfRootDirInBytes()==(RootDirEnd()-KSizeOfFatDirEntry)))    
       
   429             if (fatEntry.IsErased())
       
   430                 break;//Allows maximum number of entries in root directory
       
   431         if (fatEntry.Name()==aName)
       
   432             return ETrue;
       
   433         MoveToNextEntryL(anEntryPos);
       
   434         if (IsRootDir(anEntryPos)&&(StartOfRootDirInBytes()+anEntryPos.iPos==RootDirEnd()))
       
   435             break;//Allows maximum number of entries in root directory
       
   436         }
       
   437     return EFalse;
       
   438     }
       
   439     
       
   440 TBool CFatMountCB::IsUniqueNameL(const TShortName& aName,TInt aDirCluster)
       
   441 //
       
   442 // Returns ETrue if aName is unique, EFalse if a matching name is found.
       
   443 //
       
   444     {
       
   445 
       
   446     __PRINT(_L("VFAT::CFatMountCB::IsUniqueNameL"));    
       
   447     TEntryPos entryPos(aDirCluster,0);
       
   448     if (FindShortNameL(aName,entryPos))
       
   449         return(EFalse);
       
   450     return(ETrue);
       
   451     }
       
   452 
       
   453 void CFatMountCB::ReplaceClashingNameL(const TShortName& aNewName,const TEntryPos& anEntryPos)
       
   454 //
       
   455 // A legal dos name has been typed that clashes with a computer generated shortname
       
   456 // Change the shortname to something else.
       
   457 //
       
   458     {
       
   459 
       
   460     __PRINT(_L("VFAT::CFatMountCB::ReplaceClashingNameL")); 
       
   461     TFatDirEntry entry;
       
   462     ReadDirEntryL(anEntryPos,entry);
       
   463     __ASSERT_ALWAYS(entry.IsEndOfDirectory()==EFalse,User::Leave(KErrCorrupt));
       
   464     entry.SetName(aNewName);
       
   465     WriteDirEntryL(anEntryPos,entry);
       
   466 //  We now need to fix up VFAT entries with a new checksum reflecting new shortname
       
   467 //  Calculate new checksum
       
   468     TUint8 checksum=CalculateShortNameCheckSum(aNewName);
       
   469 //  Now go back and adjust all VFAT entries corresponding to this shortname
       
   470     TEntryPos entryPos=anEntryPos;
       
   471     FOREVER
       
   472         {
       
   473         entryPos.iPos-=KSizeOfFatDirEntry;  
       
   474         ReadDirEntryL(entryPos,entry);
       
   475         entry.iData[0x0D]=checksum;
       
   476         if (entry.iData[0]&0x40)
       
   477             break;
       
   478         }
       
   479     }
       
   480 
       
   481 TBool CFatMountCB::GenerateShortNameL(TInt aDirCluster,const TDesC& aName,TShortName& aGeneratedName, TBool aForceRandomize)
       
   482 //
       
   483 // Generate a legal dos filename as an alias for aName.
       
   484 // Returns ETrue if aName is a legal dos name.
       
   485 //
       
   486     {
       
   487 
       
   488     __PRINT(_L("VFAT::CFatMountCB::GenerateShortNameL"));
       
   489     // Given the long file-name "ABCDEFGHI.TXT", EPOC used to generate short 
       
   490     // file-names in the following pecking order:
       
   491     //     "ABCDEFGH.TXT",
       
   492     //     "ABCDEF~0.TXT",
       
   493     //     "ABCDEF~1.TXT",
       
   494     //     "ABCDEF~2.TXT",
       
   495     //     etc.
       
   496     // Now, however, EPOC behaves in a more Windows-like manner and 
       
   497     // generates short file-names in this pecking order:
       
   498     //     "ABCDEF~1.TXT",
       
   499     //     "ABCDEF~2.TXT",
       
   500     //     "ABCDEF~3.TXT",
       
   501     //     "ABCDEF~4.TXT",
       
   502     // After failing to find an unused short name 4 times in a row, 
       
   503     // a random number is used to speed up the process. So subsequent
       
   504     // short-file names become
       
   505     //     "ABC~nnnn.TXT"   where nnnn is a random number
       
   506     //    
       
   507     TBool useTildeSelectively = ETrue;
       
   508     TInt endNum = KMaxDuplicateShortName;   //  0xFFFF
       
   509     const TInt KMaxNonRandomShortFileNames = 4;
       
   510 
       
   511     TInt i = 1;
       
   512 
       
   513     TBool randomize = aForceRandomize;
       
   514     if (randomize)
       
   515         {
       
   516         i = (TInt) (Math::Random() & KMaxDuplicateShortName);
       
   517         endNum = (i - 1) & KMaxDuplicateShortName;
       
   518         }
       
   519 
       
   520     while(i != endNum)
       
   521         {
       
   522         aGeneratedName=DoGenerateShortNameL(aName,i,useTildeSelectively);
       
   523 
       
   524         if (IsUniqueNameL(aGeneratedName,aDirCluster))
       
   525             break;
       
   526 
       
   527         if (i == KMaxNonRandomShortFileNames && !randomize)
       
   528             {
       
   529             randomize = ETrue;
       
   530             i = (TInt) (Math::Random() & KMaxDuplicateShortName);
       
   531             endNum = (i - 1) & KMaxDuplicateShortName;
       
   532             }
       
   533         else if (i == -1)
       
   534             {
       
   535             useTildeSelectively=EFalse;
       
   536             i = 1;
       
   537             }
       
   538         else
       
   539             i = (i + 1) & KMaxDuplicateShortName;
       
   540         }
       
   541 
       
   542     if (i == endNum)
       
   543         User::Leave(KErrAlreadyExists);
       
   544 
       
   545     if((i == -1) && IsLegalDosName(aName,EFalse,EFalse,EFalse,EFalse,ETrue))
       
   546         {
       
   547         // Original file name is a legal 8.3 name
       
   548         return(ETrue);
       
   549         }
       
   550     else
       
   551         {
       
   552         return(EFalse);
       
   553         }
       
   554 
       
   555 
       
   556     }
       
   557 
       
   558 void TFatDirEntry::InitializeAsVFat(TUint8 aCheckSum)
       
   559 //
       
   560 // Initialize a FAT entry as a VFAT filename
       
   561 //
       
   562     {
       
   563 
       
   564     Mem::Fill(this,sizeof(SFatDirEntry),0xFF);
       
   565     iData[0x0B]=0x0F;
       
   566     iData[0x0C]=0x00; iData[0x0D]=aCheckSum;
       
   567     iData[0x1A]=0x00; iData[0x1B]=0x00;
       
   568     }
       
   569 
       
   570 void TFatDirEntry::SetVFatEntry(const TDesC& aName,TInt aLen)
       
   571 //
       
   572 // Write up to KMaxVFatEntryName unicode chars from aName to the entry
       
   573 //
       
   574     {
       
   575 
       
   576     TInt rem=aName.Length()-aLen;
       
   577     TPtrC section(aName.Ptr()+aLen,Min(rem,KMaxVFatEntryName));
       
   578     TBuf16<KMaxVFatEntryName> buf16;
       
   579     buf16.Copy(section);
       
   580     if (rem<KMaxVFatEntryName)
       
   581         {
       
   582         rem++;
       
   583         buf16.ZeroTerminate();
       
   584         buf16.SetLength(rem); // Zero termination doesn't increase the buf length
       
   585         }
       
   586     TUint8 orderNo=(TUint8)(aLen/KMaxVFatEntryName+1);
       
   587     TInt s=Min(rem,5);
       
   588     Mem::Copy(&iData[0x01],buf16.Ptr(),s*2);//Copy up to 10 bytes of buf16 into iData
       
   589     TInt offset=s;
       
   590     rem-=s;
       
   591     s=Min(rem,6);
       
   592     Mem::Copy(&iData[0x0E],buf16.Ptr()+offset,s*2);
       
   593     offset+=s;
       
   594     rem-=s;
       
   595     s=Min(rem,2);
       
   596     Mem::Copy(&iData[0x1C],buf16.Ptr()+offset,s*2);
       
   597     rem-=s;
       
   598     if (rem==0)
       
   599         orderNo|=0x40;
       
   600     iData[0]=orderNo;
       
   601     }
       
   602 
       
   603 void TFatDirEntry::ReadVFatEntry(TDes16& aBuf) const
       
   604 //
       
   605 // Read KMaxVFatEntryName unicode chars from the entry
       
   606 //
       
   607     {
       
   608 
       
   609     aBuf.SetLength(KMaxVFatEntryName);
       
   610     Mem::Copy(&aBuf[0],&iData[0x01],5*2);
       
   611     Mem::Copy(&aBuf[5],&iData[0x0E],6*2);
       
   612     Mem::Copy(&aBuf[11],&iData[0x1C],2*2);
       
   613     }
       
   614 
       
   615 void CFatMountCB::WriteDirEntryL(TEntryPos& aPos,const TFatDirEntry& aFatDirEntry,const TDesC& aLongName)
       
   616 //
       
   617 // Write a VFAT directory entry to disk at position aPos - leave aPos refering to the dos entry
       
   618 // Assumes sufficient space has been created for it by AddDirEntry.
       
   619 //
       
   620     {
       
   621 
       
   622     __PRINT(_L("VFAT::CFatMountCB::WriteDirEntryL"));   
       
   623     __ASSERT_DEBUG(aLongName.Length(),Fault(EVFatNoLongName));
       
   624     TEntryPos startPos(aPos.iCluster,aPos.iPos);
       
   625     TUint8  localBuf[KDefaultSectorSize];
       
   626     TUint8 cksum=CalculateShortNameCheckSum(aFatDirEntry.Name());
       
   627     TInt numEntries=NumberOfVFatEntries(aLongName.Length())-1; // Excluding dos entry
       
   628     // see if all entries written to one sector
       
   629     // single sector writes not supported if sector size>default size 
       
   630     TInt dosOffset=numEntries<<KSizeOfFatDirEntryLog2;
       
   631     TInt absolutePos=(aPos.iCluster<<ClusterSizeLog2())+ClusterRelativePos(aPos.iPos);
       
   632     TBool isSameSector=(((absolutePos^(absolutePos+dosOffset))>>SectorSizeLog2())==0 && ((TUint)(1<<SectorSizeLog2())<=KDefaultSectorSize));
       
   633     TFatDirEntry vFatEntry;
       
   634     vFatEntry.InitializeAsVFat(cksum);
       
   635     TInt offset=0;
       
   636     while (numEntries--)
       
   637         {
       
   638         vFatEntry.SetVFatEntry(aLongName,KMaxVFatEntryName*numEntries);//   KMaxVFatEntryName=13
       
   639         if(isSameSector)
       
   640             {
       
   641             Mem::Copy(&localBuf[offset],&vFatEntry,KSizeOfFatDirEntry);
       
   642             offset+=KSizeOfFatDirEntry;
       
   643             MoveToNextEntryL(aPos);
       
   644             }
       
   645         else
       
   646             {
       
   647             WriteDirEntryL(aPos,vFatEntry);
       
   648             MoveToNextEntryL(aPos);
       
   649             }
       
   650         }
       
   651     if(isSameSector)
       
   652         {
       
   653         Mem::Copy(&localBuf[offset],&aFatDirEntry,KSizeOfFatDirEntry);
       
   654         
       
   655         //-- use special interface to access FAT directory file
       
   656         DirWriteL(startPos,TPtrC8(&localBuf[0],dosOffset+KSizeOfFatDirEntry));
       
   657         }
       
   658     else
       
   659         WriteDirEntryL(aPos,aFatDirEntry);
       
   660     }
       
   661 
       
   662 void CFatMountCB::EraseDirEntryL(TEntryPos aPos,const TFatDirEntry& aFirstEntry)
       
   663 //
       
   664 // Mark all entries in a VFat directory entry as erased
       
   665 //
       
   666     {
       
   667     __PRINT(_L("VFAT::CFatMountCB::EraseDirEntryL"));
       
   668     TInt numEntries=0;
       
   669     if (aFirstEntry.IsVFatEntry())
       
   670         numEntries=aFirstEntry.NumFollowing();
       
   671     if(IsRuggedFSys()&&numEntries)
       
   672         {
       
   673         TInt count=numEntries;
       
   674         TEntryPos pos=aPos;
       
   675         while(count--)
       
   676             MoveToNextEntryL(pos);
       
   677         EraseDirEntryL(pos);
       
   678         numEntries--;
       
   679         }
       
   680     FOREVER
       
   681         {
       
   682         EraseDirEntryL(aPos);
       
   683         if (!numEntries--)
       
   684             break;
       
   685         MoveToNextEntryL(aPos);
       
   686         }
       
   687     }
       
   688 
       
   689 
       
   690 void  LocaleUtils::ConvertFromUnicodeL(TDes8& aForeign, const TDesC16& aUnicode, TFatUtilityFunctions::TOverflowAction aOverflowAction)
       
   691 //
       
   692 // Convert the volume label using the algorithm specified in the current locale-DLL.
       
   693 //
       
   694     {
       
   695     if(aOverflowAction == TFatUtilityFunctions::EOverflowActionLeave)
       
   696         {
       
   697         GetCodePage().ConvertFromUnicodeL(aForeign, aUnicode, TCodePageUtils::EOverflowActionLeave);
       
   698         }
       
   699     else
       
   700         {
       
   701         GetCodePage().ConvertFromUnicodeL(aForeign, aUnicode, TCodePageUtils::EOverflowActionTruncate);
       
   702         }
       
   703     }
       
   704 
       
   705 void  LocaleUtils::ConvertToUnicodeL(TDes16& aUnicode, const TDesC8& aForeign, TFatUtilityFunctions::TOverflowAction aOverflowAction)
       
   706 //
       
   707 // Convert the volume label using the algorithm specified in the current locale-DLL.
       
   708 //
       
   709     {
       
   710     if(aOverflowAction == TFatUtilityFunctions::EOverflowActionLeave)
       
   711         {
       
   712         GetCodePage().ConvertToUnicodeL(aUnicode, aForeign, TCodePageUtils::EOverflowActionLeave);
       
   713         }
       
   714     else
       
   715         {
       
   716         GetCodePage().ConvertToUnicodeL(aUnicode, aForeign, TCodePageUtils::EOverflowActionTruncate);
       
   717         }
       
   718     }
       
   719 
       
   720 TBool LocaleUtils::IsLegalShortNameCharacter(TUint aCharacter,TBool aUseExtendedChars)
       
   721 //
       
   722 // Convert the volume label using the algorithm specified in the current locale-DLL.
       
   723 //
       
   724     {
       
   725     return GetCodePage().IsLegalShortNameCharacter(aCharacter, aUseExtendedChars);
       
   726     }