dosservices/dosserver/src/dosshareddatabase.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2002 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: 
       
    15 *    Implementation for the CDosharedDataBase class
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "dosshareddatabase.h"
       
    21 #include "dosclientserver.h"
       
    22 #include "dosserver.h"
       
    23 #include "shareddatafilesystemnotifier.h"
       
    24 #include "dosclishareddata.h"
       
    25 #include "dos_debug.h"
       
    26 #include <e32svr.h>
       
    27 
       
    28 // CONSTANTS
       
    29 _LIT( KSDReserveFileName, "C:\\Private\\101f6efa\\reserve.bin" );
       
    30 _LIT(KSDPath, "C:\\Private\\101f6efa\\");
       
    31 
       
    32 // Definitions for reserve file system
       
    33 const TInt KSDLotsOfFreeDiskSpace = 1000000;
       
    34 const TInt KSDFreeDiskSpaceOverhead = 1024;
       
    35 const TInt KSDReserveFileLoopMaxCount = 5;
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CDosSharedDataBase::CDosSharedDataBase* NewL()
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 EXPORT_C CDosSharedDataBase* CDosSharedDataBase::NewL(CSharedDataFileSystemNotifier* fileSystemNotifier)
       
    42     {
       
    43     COM_TRACE_1( "[DOSSERVER]\t CDosSharedDataBase* CDosSharedDataBase::NewL(0x%x)", fileSystemNotifier );		
       
    44     CDosSharedDataBase* self = NewLC(fileSystemNotifier);
       
    45     CleanupStack::Pop();
       
    46     return self;
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CDosSharedDataBase::CDosSharedDataBase* NewLC()
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 EXPORT_C CDosSharedDataBase* CDosSharedDataBase::NewLC(CSharedDataFileSystemNotifier* fileSystemNotifier)
       
    54     {
       
    55     COM_TRACE_1( "[DOSSERVER]\t CDosSharedDataBase* CDosSharedDataBase::NewL(0x%x)", fileSystemNotifier );
       
    56     CDosSharedDataBase* self = new( ELeave ) CDosSharedDataBase();
       
    57     CleanupStack::PushL( self );
       
    58     self->ConstructL( fileSystemNotifier );
       
    59     return self;
       
    60     }
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CDosSharedDataBase::void ConstructL()
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 void CDosSharedDataBase::ConstructL(CSharedDataFileSystemNotifier* fileSystemNotifier)
       
    67     {
       
    68     COM_TRACE_1( "[DOSSERVER]\t CDosSharedDataBase::ConstructL(0x%x)", fileSystemNotifier );	
       
    69     iFileSystemNotifier = fileSystemNotifier;
       
    70     iFreeDiskSpaceRequest = 0;
       
    71 
       
    72 	TInt err( iFs.Connect() );
       
    73 	if ( err < 0 )
       
    74 	    {
       
    75 		User::LeaveIfError( err );
       
    76 	    }
       
    77 	User::LeaveIfError(iFs.ShareProtected());
       
    78     }
       
    79 
       
    80 // Destructor
       
    81 CDosSharedDataBase::~CDosSharedDataBase()    
       
    82     {
       
    83     COM_TRACE_( "[DOSSERVER]\t CDosSharedDataBase::~CDosSharedDataBase()" );	
       
    84     if ( iFileSystemNotifier )
       
    85         {
       
    86         iFileSystemNotifier->Cancel();
       
    87         }
       
    88     iFs.Close();    
       
    89     }
       
    90     
       
    91 //
       
    92 // ---------------------------------------------------------
       
    93 // CDosSharedDataBase::ExecuteMessageL
       
    94 // ---------------------------------------------------------
       
    95 //
       
    96 EXPORT_C TInt CDosSharedDataBase::ExecuteMessageL(const RMessage2& aMessage)
       
    97 {
       
    98 	COM_TRACE_( "[DOSSERVER]\t CDosSharedDataBase::ExecuteMessageL(...)" );    
       
    99 	switch (aMessage.Function())
       
   100 	{       
       
   101 		case ERequestFreeDiskSpace:
       
   102             {
       
   103             
       
   104             TInt amount( aMessage.Int0() );
       
   105             COM_TRACE_2( "CDosSharedDataBase::ExecuteMessageL() case ERequestFreeDiskSpace, amount = %d iFreeDiskSpaceRequest = %d", amount, iFreeDiskSpaceRequest );
       
   106             
       
   107             if ( amount > iFreeDiskSpaceRequest )
       
   108                 {
       
   109                 if ( amount > KSDReserveFileMaxSize )
       
   110                     {
       
   111                     amount = KSDReserveFileMaxSize;
       
   112                     }
       
   113                 
       
   114                 COM_TRACE_2( "CDosSharedDataBase::ExecuteMessageL() case ERequestFreeDiskSpace, amount = %d iFreeDiskSpaceRequest = %d", amount, iFreeDiskSpaceRequest );
       
   115                 
       
   116                 iFreeDiskSpaceRequest = amount;
       
   117                 RequestFreeDiskSpace( &iFreeDiskSpaceRequest );
       
   118                 }  
       
   119             
       
   120             TInt err(KErrNone);
       
   121 			return err;
       
   122             }
       
   123 
       
   124 		case ERequestFreeDiskSpaceCancel:
       
   125 			{
       
   126 			COM_TRACE_1( "CDosSharedDataBase::ExecuteMessageL() case ERequestFreeDiskSpaceCancel iFreeDiskSpaceRequest = %d", iFreeDiskSpaceRequest );
       
   127             
       
   128 			if ( iFreeDiskSpaceRequest > 0 )
       
   129                 {
       
   130                 // iFreeDiskSpaceRequest = 0;
       
   131                 RequestFreeDiskSpaceCancel(&iFreeDiskSpaceRequest);
       
   132                 }
       
   133                 		    
       
   134             TInt err(KErrNone);
       
   135 			return err;
       
   136             }
       
   137 
       
   138 		default:
       
   139 			COM_TRACE_( "[DOSSERVER]\t Panic: Illegal function!" );
       
   140 			PanicClient(aMessage,EPanicIllegalFunction);
       
   141 			return KErrNone;
       
   142 	}
       
   143 }
       
   144 
       
   145 //
       
   146 // ---------------------------------------------------------
       
   147 // CDosSharedDataBase::RequestFreeDiskSpace
       
   148 // ---------------------------------------------------------
       
   149 //
       
   150 void CDosSharedDataBase::RequestFreeDiskSpace( TInt* aRequest )
       
   151     {
       
   152     __ASSERT_DEBUG( aRequest, User::Invariant() );
       
   153     
       
   154     COM_TRACE_( "[DOSSERVER]\t CDosSharedDataBase::RequestFreeDiskSpace" );
       
   155             
       
   156     TInt* currentFreeDiskSpaceRequest = iFileSystemNotifier->iServer.CurrentFreeDiskSpaceRequest();
       
   157     if ( !currentFreeDiskSpaceRequest || // no request earlier
       
   158          aRequest == currentFreeDiskSpaceRequest || // current request updated
       
   159          *aRequest > *currentFreeDiskSpaceRequest ) // new higher request
       
   160         {
       
   161         COM_TRACE_( "CDosSharedDataBase::RequestFreeDiskSpace - no request earlier,..." );
       
   162         iFileSystemNotifier->Cancel();        
       
   163         iFileSystemNotifier->iServer.SetCurrentFreeDiskSpaceRequest(aRequest);
       
   164         SetReserveFileSize( *aRequest );
       
   165         }
       
   166     COM_TRACE_( "[DOSSERVER]\t CDosSharedDataBase::RequestFreeDiskSpace - return" );
       
   167     }
       
   168     
       
   169 // -----------------------------------------------------------------------------
       
   170 // CDosSharedDataBase::RequestFreeDiskSpaceCancel()
       
   171 // -----------------------------------------------------------------------------
       
   172 void CDosSharedDataBase::RequestFreeDiskSpaceCancel( TInt* aRequest )
       
   173     {
       
   174     __ASSERT_DEBUG( aRequest, User::Invariant() );
       
   175     
       
   176     COM_TRACE_( "[DOSSERVER]\t CDosSharedDataBase::RequestFreeDiskSpaceCancel" );
       
   177     
       
   178     TInt* currentFreeDiskSpaceRequest = iFileSystemNotifier->iServer.CurrentFreeDiskSpaceRequest();
       
   179     if ( currentFreeDiskSpaceRequest == aRequest )
       
   180         {
       
   181         
       
   182         //*currentFreeDiskSpaceRequest = KSDReserveFileMaxSize;
       
   183         *currentFreeDiskSpaceRequest = 0;
       
   184         
       
   185         iFileSystemNotifier->iServer.SetCurrentFreeDiskSpaceRequest( currentFreeDiskSpaceRequest );
       
   186         
       
   187         // Adjust the size of the reserve file to the maximum size
       
   188 
       
   189         // SetReserveFileSize( KSDReserveFileMaxSize);
       
   190         SetReserveFileSize( 0 );
       
   191         }
       
   192     COM_TRACE_( "[DOSSERVER]\t CDosSharedDataBase::RequestFreeDiskSpaceCancel - return" );
       
   193     
       
   194     }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CDosSharedDataBase::SetReserveFileSize()
       
   198 // -----------------------------------------------------------------------------
       
   199 
       
   200 void CDosSharedDataBase::SetReserveFileSize( const TInt aRequiredFreeDiskSpace )
       
   201     {    	
       
   202     COM_TRACE_( "[DOSSERVER]\t CDosSharedDataBase::SetReserveFileSize" );    
       
   203     
       
   204     TInt newSize( 0 );
       
   205     TInt currentSize( 0 );
       
   206     TInt freeSpace( KSDLotsOfFreeDiskSpace );
       
   207 
       
   208 	TVolumeInfo info;
       
   209 	TInt ret = iFs.Volume( info, EDriveC );
       
   210 
       
   211     if ( ret == KErrNone )
       
   212         {
       
   213         RFile file;
       
   214         ret = file.Open( iFs, KSDReserveFileName, EFileShareAny | EFileWrite );
       
   215 
       
   216         switch ( ret )
       
   217             {
       
   218             case KErrPathNotFound:
       
   219                 iFs.MkDirAll( KSDPath );    // no break here 
       
   220             case KErrNotFound:
       
   221                 ret = file.Create( iFs, KSDReserveFileName, 
       
   222                                    EFileShareAny | EFileWrite );      
       
   223             default: {}
       
   224             }
       
   225 
       
   226         if ( ret == KErrNone )
       
   227             {
       
   228             ret = file.Size( currentSize );
       
   229 
       
   230             COM_TRACE_( "SharedData: SetReserveFileSize() start" );
       
   231             COM_TRACE_1( "SharedData: Free disk space: %d", TInt(info.iFree) );    
       
   232             COM_TRACE_1( "SharedData: Reserve file size: %d", currentSize );                
       
   233 
       
   234             if ( ret == KErrNone )
       
   235                 {
       
   236                 if ( info.iFree < freeSpace )
       
   237                     {
       
   238                     freeSpace = I64INT(info.iFree);
       
   239                     }
       
   240 
       
   241                 // to make sure infinite loop can never happen
       
   242                 TInt loopCounter( 0 );
       
   243 
       
   244                 // *** START of reserve file size altering section ***
       
   245 
       
   246                 do  {
       
   247                     loopCounter++;
       
   248 
       
   249                     newSize = freeSpace + currentSize - aRequiredFreeDiskSpace;
       
   250 
       
   251                     if ( aRequiredFreeDiskSpace > 0 )
       
   252                         {
       
   253                         newSize -= KSDFreeDiskSpaceOverhead;
       
   254                         }
       
   255 
       
   256                     if ( newSize < 0 )
       
   257                         {
       
   258                         newSize = 0;
       
   259                         }
       
   260 
       
   261                     else if ( newSize > KSDReserveFileMaxSize )
       
   262                         {
       
   263                         newSize = KSDReserveFileMaxSize;
       
   264                         }
       
   265 
       
   266                     // set the file size to 'newSize' here
       
   267 
       
   268                     TInt position( 0 );
       
   269                     ret = file.Seek( ESeekEnd, position );
       
   270 
       
   271                     if ( ret == KErrNone )
       
   272                         {
       
   273                         if ( position > newSize )
       
   274                             {
       
   275                             ret = file.SetSize( newSize );
       
   276                             
       
   277                             COM_TRACE_1( "SharedData: SetSize returned: %d", ret );
       
   278 
       
   279                             file.Flush();
       
   280                             }
       
   281 
       
   282                         else if ( position < newSize )
       
   283                             {
       
   284                             TInt toBeWritten( newSize - position );
       
   285 
       
   286                             HBufC8* buffer = HBufC8::New( toBeWritten );
       
   287 
       
   288                             // Write the new data in cycles,
       
   289                             // halving the total amount to be written in
       
   290                             // every cycle until we get below 1024 bytes.
       
   291                             // This leads to max 6 cycles and ensures that
       
   292                             // as much as possible gets reserved back.
       
   293 
       
   294                             if ( buffer )
       
   295                                 {
       
   296                                 while ( ret != KErrDiskFull &&
       
   297                                         toBeWritten > 1024 )
       
   298                                     {
       
   299                                     buffer->Des().SetLength( toBeWritten >> 1 );
       
   300 
       
   301                                     ret = file.Write( *buffer );
       
   302 
       
   303                                     if ( ret == KErrNone )
       
   304                                         {
       
   305                                         ret = file.Flush();
       
   306                                         }
       
   307 
       
   308                                     toBeWritten -= ( toBeWritten >> 1 );
       
   309                                     }
       
   310                             
       
   311                                 buffer->Des().SetLength( toBeWritten );
       
   312                                 ret = file.Write( *buffer );
       
   313 
       
   314                                 if ( ret == KErrNone )
       
   315                                     {
       
   316                                     ret = file.Flush();
       
   317                                     }
       
   318 
       
   319                                 delete buffer;
       
   320                                 }
       
   321                             }
       
   322                         }
       
   323 
       
   324                     // update reserve file size info
       
   325                     file.Size( currentSize );
       
   326 
       
   327                     // update free disk space info
       
   328                     ret = iFs.Volume( info, EDriveC );
       
   329 
       
   330                     if ( ret == KErrNone )
       
   331                         {
       
   332                         freeSpace = KSDLotsOfFreeDiskSpace;
       
   333 
       
   334                         if ( info.iFree < freeSpace )
       
   335                             {
       
   336                             freeSpace = I64INT(info.iFree);
       
   337                             }
       
   338                         }
       
   339 
       
   340                     COM_TRACE_( "SharedData: SetReserveFileSize() loop" );
       
   341                     COM_TRACE_1( "SharedData: Free disk space: %d", (TInt) info.iFree );
       
   342                     COM_TRACE_1( "SharedData: Expected reserve file size: %d", newSize );
       
   343                     COM_TRACE_1( "SharedData: Reserve file size: %d", currentSize );                    
       
   344                     }
       
   345 
       
   346                 while ( loopCounter < KSDReserveFileLoopMaxCount &&
       
   347                         aRequiredFreeDiskSpace > 0 &&
       
   348                         currentSize > 0 &&
       
   349                         freeSpace < aRequiredFreeDiskSpace );
       
   350 
       
   351                 // *** END of reserve file size altering section ***
       
   352 
       
   353                 // If there are no more free disk space requests but we didn't
       
   354                 // manage to increase the reserve file size back to max, get
       
   355                 // notification from file server when there is more disk space.
       
   356 
       
   357                 if ( aRequiredFreeDiskSpace == 0 &&
       
   358                      currentSize < KSDReserveFileMaxSize )
       
   359                     {
       
   360                     iFileSystemNotifier->Start( freeSpace + 2500, iFs, this );
       
   361                     }
       
   362                 }
       
   363                 
       
   364             file.Close();
       
   365             }
       
   366         }
       
   367 
       
   368     COM_TRACE_1( "SharedData: Free disk space: %d", (TInt) info.iFree );
       
   369     COM_TRACE_1( "SharedData: Expected reserve file size: %d", newSize );
       
   370     COM_TRACE_1( "SharedData: Reserve file size:  %d", currentSize );
       
   371     
       
   372     COM_TRACE_( "[DOSSERVER]\t CDosSharedDataBase::SetReserveFileSize - return" );    
       
   373     }