terminalsecurity/server/src/TcFileScan.cpp
changeset 0 b497e44ab2fc
child 73 ae69c2e8bc34
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2000 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Implementation of terminalsecurity components
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include <e32base.h>
       
    21 #include "TcFileScan.h"
       
    22 #include "TerminalControlClient.h" // For TFileName8
       
    23 #include "debug.h"
       
    24 
       
    25 // MACROS
       
    26 _LIT8(KFormatFolderEmpty, "<folder name=\"%S\"/>\r\n");
       
    27 _LIT8(KFormatFolderOpen,  "<folder name=\"%S\">\r\n");
       
    28 _LIT8(KFormatFolderClose, "</folder>\r\n");
       
    29 _LIT8(KFormatFileOpen,    "<file name=\"%S\"/>\r\n");
       
    30 _LIT8(KFormatDriveName,   "<drive name=\"%S\"/>\r\n");
       
    31 _LIT8(KFormatListingOpen, "<folder-listing version=\"1.1\">\r\n");
       
    32 _LIT8(KFormatListingClose,"</folder-listing>\r\n");
       
    33 
       
    34 // METHODS
       
    35 
       
    36 
       
    37 CTcFileScan* CTcFileScan::NewL()
       
    38 	{
       
    39 	return new (ELeave) CTcFileScan();	
       
    40 	}
       
    41 
       
    42 
       
    43 // ----------------------------------------------------------------------------------------
       
    44 // CTcFileScan::CTcFileScan
       
    45 // ----------------------------------------------------------------------------------------
       
    46 CTcFileScan::CTcFileScan()
       
    47 : iFileScanResults(0)
       
    48     {
       
    49 	RDEBUG("CTcFileScan::CTcFileScan");
       
    50 
       
    51     }
       
    52 
       
    53 // ----------------------------------------------------------------------------------------
       
    54 // CTcFileScan::~CTcFileScan
       
    55 // ----------------------------------------------------------------------------------------
       
    56 CTcFileScan::~CTcFileScan()
       
    57     {
       
    58 	RDEBUG("CTcFileScan::~CTcFileScan");
       
    59 
       
    60     delete iFileScanResults;
       
    61     }    
       
    62 
       
    63 // ----------------------------------------------------------------------------------------
       
    64 // CTcFileScan::FileScanL
       
    65 // ----------------------------------------------------------------------------------------
       
    66 void CTcFileScan::FileScanL( const TDesC8 &aFileName, TBool aRecursive )
       
    67     {
       
    68 		RDEBUG_2( "CTcFileScan::FileScanL: %S", &aFileName );
       
    69 		RDEBUG_2( "CTcFileScan::FileScanL: Recursive: %d", aRecursive );
       
    70 
       
    71     RFs fileSession;
       
    72     User::LeaveIfError(fileSession.Connect());
       
    73     CleanupClosePushL(fileSession);
       
    74 
       
    75     TBuf<sizeof(TFileName8)+2> startDirBuf;
       
    76     startDirBuf.Copy(aFileName.Left(sizeof(TFileName8)));
       
    77     startDirBuf.TrimAll();
       
    78 
       
    79     //
       
    80     // Add asterisk if path is specified
       
    81     //
       
    82     if(startDirBuf.Length() > 0)
       
    83         {
       
    84         if(startDirBuf.Right(1) != _L("\\"))
       
    85             {
       
    86             startDirBuf.Append(TChar('\\'));
       
    87             }
       
    88             
       
    89         startDirBuf.Append(TChar('*'));
       
    90         }        
       
    91     
       
    92     if( iFileScanResults != 0 )
       
    93         {
       
    94         delete iFileScanResults;
       
    95         iFileScanResults = 0;
       
    96         }
       
    97 
       
    98     iFileScanResults = CBufFlat::NewL(128);
       
    99 
       
   100     // Every scan result begins with <folder-listing>
       
   101     //
       
   102     iFileScanResults->InsertL(0, KFormatListingOpen);
       
   103     iRecurseLevel = 0;
       
   104     
       
   105     if( startDirBuf.Length() > 0 )
       
   106         {        
       
   107         ScanDirectoryL( fileSession, startDirBuf, aRecursive );
       
   108         }
       
   109     else
       
   110         {
       
   111         GetDriveListL();
       
   112         }
       
   113 
       
   114     // Every scan result ends with </folder-listing>
       
   115     //
       
   116     iFileScanResults->InsertL(iFileScanResults->Size(), KFormatListingClose);
       
   117 
       
   118     CleanupStack::PopAndDestroy( &fileSession );
       
   119     }
       
   120 
       
   121 // ----------------------------------------------------------------------------------------
       
   122 // CTcFileScan::ScanDirectoryL
       
   123 // ----------------------------------------------------------------------------------------
       
   124 void CTcFileScan::ScanDirectoryL( RFs& aFileSession,
       
   125                                   const TDesC &aDirectory,
       
   126                                   TBool aRecursive )
       
   127     {
       
   128 		RDEBUG_2( "CTcFileScan::ScanDirectoryL: %S", &aDirectory);
       
   129 
       
   130     // Note: this function is used recursively
       
   131     // Stack use is minimized to avoid overflow as long as possible
       
   132     //
       
   133     iRecurseLevel ++;
       
   134     TFindFile* fileFinder1 = new (ELeave) TFindFile( aFileSession );
       
   135     CDir*      fileList1;
       
   136 
       
   137     TInt err = fileFinder1->FindWildByPath(aDirectory, &KNullDesC(), fileList1);
       
   138 
       
   139     while( err == KErrNone )
       
   140         {
       
   141         TInt i;
       
   142 
       
   143         for (i=0; i<fileList1->Count(); i++)
       
   144             {
       
   145             const TEntry & entry = (*fileList1)[i];
       
   146             TInt iFilesInDirectory = 0;
       
   147 
       
   148             //
       
   149             // Is there any files in directory?
       
   150             //
       
   151             if(aRecursive && entry.IsDir())
       
   152                 {
       
   153                 HBufC *fullPathName1 = CreateScanPathNameL(aDirectory, entry.iName, _L("*"));
       
   154                 CleanupStack::PushL( fullPathName1 );
       
   155                 
       
   156                 iFilesInDirectory = FilesInDirectoryL( aFileSession, *fullPathName1 );
       
   157                 
       
   158                 CleanupStack::PopAndDestroy( fullPathName1 );    
       
   159                 }
       
   160 
       
   161             //
       
   162             // Add file information into result buffer
       
   163             //
       
   164             BeginAddFileToResultsL( TPtrC ( entry.iName ), entry.IsDir(), iFilesInDirectory );
       
   165 
       
   166             //
       
   167             // Scan subdirectory
       
   168             //
       
   169             if(aRecursive && entry.IsDir() && (iRecurseLevel < 200))
       
   170                 {
       
   171                 HBufC *fullPathName2 = CreateScanPathNameL(aDirectory, entry.iName, _L("*"));                
       
   172                 CleanupStack::PushL( fullPathName2 );
       
   173                 
       
   174                 ScanDirectoryL( aFileSession, fullPathName2->Des(), ETrue );
       
   175                                 
       
   176                 CleanupStack::PopAndDestroy( fullPathName2 );
       
   177                 }
       
   178             
       
   179             //
       
   180             // More file information into result buffer
       
   181             //
       
   182             EndAddFileToResultsL( entry.IsDir(), iFilesInDirectory );
       
   183             }
       
   184 
       
   185         delete fileList1;
       
   186 
       
   187         err = fileFinder1->FindWild( fileList1 );
       
   188         }
       
   189 
       
   190     delete fileFinder1;
       
   191     
       
   192     iRecurseLevel --;
       
   193     }
       
   194 
       
   195 // ----------------------------------------------------------------------------------------
       
   196 // CTcFileScan::BeginAddFileToResults
       
   197 // ----------------------------------------------------------------------------------------
       
   198 void CTcFileScan::BeginAddFileToResultsL( const TDesC &aFileName,
       
   199                                          TBool aIsDir,
       
   200                                          TInt aFiles )
       
   201     {    
       
   202 	RDEBUG("CTcFileScan::BeginAddFileToResultsL");
       
   203 
       
   204     iTabString.Zero();
       
   205     if( iRecurseLevel > 0)
       
   206         {
       
   207         TInt max = (iRecurseLevel>sizeof(iTabString)?sizeof(iTabString):iRecurseLevel);
       
   208         for(TInt i=0; i<max; i++)
       
   209             {
       
   210             iTabString.Append(TChar('\t'));
       
   211             }
       
   212         }
       
   213         
       
   214     TFileName8 filename; filename.Copy( aFileName.Left( sizeof( TFileName8 ) ) );
       
   215     
       
   216     if(aIsDir)
       
   217         {
       
   218         if(aFiles == 0)
       
   219             {
       
   220             TInt appendPosition = iFileScanResults->Size();
       
   221             TInt appendSize = filename.Length() + KFormatFolderEmpty().Length() + iTabString.Length();
       
   222 
       
   223             HBufC8* appendString = HBufC8::NewLC(appendSize);
       
   224             appendString->Des().Format(KFormatFolderEmpty, &filename);
       
   225             appendString->Des().Insert(0, iTabString);
       
   226 
       
   227             iFileScanResults->InsertL(appendPosition, *appendString);
       
   228             CleanupStack::PopAndDestroy(appendString);
       
   229             }
       
   230         else
       
   231             {
       
   232             TInt appendPosition = iFileScanResults->Size();
       
   233             TInt appendSize = filename.Length() + KFormatFolderOpen().Length() + iTabString.Length();
       
   234             
       
   235             HBufC8* appendString = HBufC8::NewLC(appendSize);
       
   236             appendString->Des().Format(KFormatFolderOpen, &filename);
       
   237             appendString->Des().Insert(0, iTabString);
       
   238 
       
   239             iFileScanResults->InsertL(appendPosition, *appendString);
       
   240             CleanupStack::PopAndDestroy(appendString);            
       
   241             }
       
   242         }
       
   243     else
       
   244         {
       
   245         TInt appendPosition = iFileScanResults->Size();
       
   246         TInt appendSize = filename.Length() + KFormatFileOpen().Length() + iTabString.Length();
       
   247         
       
   248         HBufC8* appendString = HBufC8::NewLC(appendSize);
       
   249         appendString->Des().Format(KFormatFileOpen, &filename);
       
   250         appendString->Des().Insert(0, iTabString);
       
   251 
       
   252         iFileScanResults->InsertL(appendPosition, *appendString);
       
   253         CleanupStack::PopAndDestroy(appendString);                        
       
   254         }
       
   255     }
       
   256 
       
   257 // ----------------------------------------------------------------------------------------
       
   258 // CTcFileScan::EndAddFileToResults
       
   259 // ----------------------------------------------------------------------------------------
       
   260 void CTcFileScan::EndAddFileToResultsL( TBool aIsDir,
       
   261                                         TInt aFiles )
       
   262     {   
       
   263 	RDEBUG("CTcFileScan::EndAddFileToResultsL");
       
   264 
       
   265     iTabString.Zero();
       
   266     if( iRecurseLevel > 0)
       
   267         {
       
   268         TInt max = (iRecurseLevel>sizeof(iTabString)?sizeof(iTabString):iRecurseLevel);
       
   269         for(TInt i=0; i<max; i++)
       
   270             {
       
   271             iTabString.Append(TChar('\t'));
       
   272             }
       
   273         }
       
   274 
       
   275     if(aIsDir)
       
   276         {
       
   277         if(aFiles > 0)
       
   278             {
       
   279             TInt appendPosition = iFileScanResults->Size();
       
   280             TInt appendSize = KFormatFolderClose().Length() + iTabString.Length();
       
   281             
       
   282             HBufC8* appendString = HBufC8::NewLC(appendSize);
       
   283             appendString->Des().Copy(KFormatFolderClose);
       
   284             appendString->Des().Insert(0, iTabString);
       
   285 
       
   286             iFileScanResults->InsertL(appendPosition, *appendString);
       
   287             CleanupStack::PopAndDestroy(appendString);
       
   288             }
       
   289         }
       
   290     }
       
   291 
       
   292 // ----------------------------------------------------------------------------------------
       
   293 // CTcFileScan::AddDriveToResults
       
   294 // ----------------------------------------------------------------------------------------
       
   295 void CTcFileScan::AddDriveToResultsL( const TDesC& aDriveName )
       
   296     {
       
   297 	RDEBUG("CTcFileScan::AddDriveToResultsL");
       
   298 
       
   299     iTabString.Zero();
       
   300     if( iRecurseLevel > 0)
       
   301         {
       
   302         TInt max = (iRecurseLevel>sizeof(iTabString)?sizeof(iTabString):iRecurseLevel);
       
   303         for(TInt i=0; i<max; i++)
       
   304             {
       
   305             iTabString.Append(TChar('\t'));
       
   306             }
       
   307         }
       
   308 
       
   309     TFileName8 filename; filename.Copy( aDriveName.Left( sizeof( TFileName8 ) ) );
       
   310 
       
   311     TInt appendPosition = iFileScanResults->Size();
       
   312     TInt appendSize = filename.Length() + KFormatDriveName().Length() + iTabString.Length();
       
   313 
       
   314     HBufC8* appendString = HBufC8::NewLC(appendSize);
       
   315     appendString->Des().Format(KFormatDriveName, &filename);
       
   316     appendString->Des().Insert(0, iTabString);
       
   317 
       
   318     iFileScanResults->InsertL(appendPosition, *appendString);
       
   319     CleanupStack::PopAndDestroy(appendString);
       
   320     }
       
   321 
       
   322 // ----------------------------------------------------------------------------------------
       
   323 // CTcFileScan::FilesInDirectoryL
       
   324 // ----------------------------------------------------------------------------------------
       
   325 TInt CTcFileScan::FilesInDirectoryL(RFs& aFs, TDesC &aDirectory)
       
   326     {
       
   327 	RDEBUG("CTcFileScan::FilesInDirectoryL");
       
   328 
       
   329     TInt retVal = 0;
       
   330 
       
   331     TFindFile* fileFinder = new (ELeave) TFindFile( aFs );
       
   332 
       
   333     CDir* fileList = NULL;
       
   334     if(KErrNone == fileFinder->FindWildByPath(aDirectory, &KNullDesC(), fileList))
       
   335         {
       
   336             retVal = fileList->Count();
       
   337             delete fileList;
       
   338         }
       
   339 
       
   340     delete fileFinder;
       
   341     
       
   342     return retVal;
       
   343     }
       
   344 
       
   345 // ----------------------------------------------------------------------------------------
       
   346 // CTcFileScan::CreateScanPathNameL
       
   347 // @return String "aDirectory\aSubDirectory\*"
       
   348 // @note aDirectory may contain one extra '*' in the end
       
   349 // ----------------------------------------------------------------------------------------
       
   350 HBufC* CTcFileScan::CreateScanPathNameL ( const TDesC &aDirectory, const TDesC &aSubDirectory, const TDesC& aAsterisk )
       
   351     {
       
   352 	RDEBUG("CTcFileScan::CreateScanPathNameL");
       
   353 
       
   354     TInt fullLength = aDirectory.Length() + aSubDirectory.Length() + aAsterisk.Length() + 2;
       
   355 
       
   356     HBufC *fullPathName = HBufC::NewL(fullLength);
       
   357 
       
   358     fullPathName->Des().Append(aDirectory);
       
   359 
       
   360     // Remove last star character '*'
       
   361     if(( fullPathName->Des().Length() > 0 ) && ( fullPathName->Des().Right(1) == _L("*") ))
       
   362         {
       
   363         fullPathName->Des().SetLength(fullPathName->Des().Length()-1);
       
   364         }
       
   365 
       
   366     // Add last backslash character '\'
       
   367     if(( fullPathName->Des().Length() > 0 ) && ( fullPathName->Des().Right(1) != _L("\\") ))
       
   368         {
       
   369         fullPathName->Des().Append(TChar('\\'));
       
   370         }
       
   371 
       
   372     fullPathName->Des().Append(aSubDirectory);
       
   373 
       
   374     // Add last backslash character '\'
       
   375     if(( fullPathName->Des().Length() > 0 ) && ( fullPathName->Des().Right(1) != _L("\\") ))
       
   376         {
       
   377         fullPathName->Des().Append(TChar('\\'));
       
   378         }
       
   379 
       
   380     fullPathName->Des().Append(aAsterisk);
       
   381 
       
   382     return fullPathName;
       
   383     }
       
   384 
       
   385 // ----------------------------------------------------------------------------------------
       
   386 // CTcFileScan::GetCopyOfResults
       
   387 // ----------------------------------------------------------------------------------------
       
   388 HBufC8* CTcFileScan::GetCopyOfResultsL()
       
   389     {
       
   390 	RDEBUG("CTcFileScan::GetCopyOfResultsL");
       
   391 
       
   392     if(iFileScanResults == 0)
       
   393         return 0;
       
   394     
       
   395     HBufC8* results = HBufC8::NewL( iFileScanResults->Size() );
       
   396     results->Des().Append(iFileScanResults->Ptr(0));
       
   397     return results;
       
   398     }
       
   399 
       
   400 // ----------------------------------------------------------------------------------------
       
   401 // CTcFileScan::GetDriveListL
       
   402 // This method will list available local drives (letters from 'a' to 'i', including 'z')
       
   403 // Drive letters from 'j' to 'y' are substed or redirected and NOT included in results
       
   404 // ----------------------------------------------------------------------------------------
       
   405 void CTcFileScan::GetDriveListL()
       
   406     {
       
   407 	RDEBUG("CTcFileScan::GetDriveListL");
       
   408 
       
   409     RFs fs;
       
   410     User::LeaveIfError( fs.Connect() );
       
   411     CleanupClosePushL( fs );
       
   412     
       
   413     TDriveList list;
       
   414     fs.DriveList( list );
       
   415 
       
   416     iRecurseLevel = 0;
       
   417     TInt   i;
       
   418     TUint8 c = 'a';
       
   419         
       
   420     for(i= 0; i<sizeof(TDriveList) && c<'i'; i++, c++)
       
   421         {
       
   422         if( list[i] != 0 )
       
   423             {
       
   424             TBuf<10> driveName;
       
   425             driveName.Append(TChar(c));
       
   426             AddDriveToResultsL( driveName );
       
   427             }
       
   428         }
       
   429         
       
   430     // Look for drive 'z'
       
   431     if( list['z' - 'a'] != 0 )
       
   432         {
       
   433         TBuf<10> driveName;
       
   434         driveName.Append(TChar('z'));
       
   435         AddDriveToResultsL( driveName );
       
   436         }
       
   437         
       
   438     CleanupStack::PopAndDestroy( &fs );       
       
   439     }
       
   440 
       
   441 // ----------------------------------------------------------------------------------------
       
   442 // End of file