dosservices/tsrc/public/basic/dosservertestplugin/src/Tfdosshareddatatc.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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32std.h>
       
    20 #include <e32svr.h> // RDebug
       
    21 #include <e32base.h>
       
    22 
       
    23 #include <Sysutil.h>
       
    24 #include "CTFDosSharedDataTC.h"
       
    25 
       
    26 //FORWARD DECLARATIONS
       
    27 
       
    28 
       
    29 // CONSTANTS
       
    30 _LIT( KSDReserveFileName, "C:\\Private\\101f6efa\\reserve.bin" );
       
    31 _LIT( KFileName, "C:\\logs\\MemoryEaterFile" );
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // CSdDisk::CSdDisk
       
    35 // C++ default constructor can NOT contain any code, that
       
    36 // might leave.
       
    37 // -----------------------------------------------------------------------------
       
    38 CSdDisk::CSdDisk() 
       
    39     {
       
    40     }
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // CSdDisk* CSdDisk::NewL
       
    44 // 
       
    45 // 
       
    46 // -----------------------------------------------------------------------------
       
    47 CSdDisk* CSdDisk::NewL() 
       
    48     {
       
    49 	CSdDisk* sdn = new (ELeave) CSdDisk();
       
    50 	CleanupStack::PushL( sdn );
       
    51 	sdn->ConstructL();
       
    52 	CleanupStack::Pop();
       
    53 	return sdn;
       
    54     }
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CSdDisk::ConstructL()
       
    58 // 
       
    59 // Symbian 2nd phase constructor can leave.
       
    60 // -----------------------------------------------------------------------------
       
    61 void CSdDisk::ConstructL()
       
    62     {
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CSdDisk::~CSdDisk
       
    67 // Destructor
       
    68 // 
       
    69 // -----------------------------------------------------------------------------
       
    70 CSdDisk::~CSdDisk() 
       
    71     {
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CSdDisk::ReleaseFiles
       
    76 // This method deletes files created for filling the drive C
       
    77 // 
       
    78 // -----------------------------------------------------------------------------
       
    79 void CSdDisk::ReleaseDisk(RFs& aFs)
       
    80     {
       
    81 	// RDebug::Print(_L("Shareddatatest ## ReleaseDisk"));
       
    82 	TInt i = 0;
       
    83 	TInt err = KErrNone;
       
    84 		
       
    85 	do
       
    86 	    {
       
    87 		TBuf16<35> str;
       
    88 		str.Copy(KFileName);
       
    89 		str.AppendNum(i,EDecimal);
       
    90 		
       
    91 		err = aFs.Delete (str) ;
       
    92 		
       
    93 		i++;
       
    94 	    }
       
    95 	while ( err == KErrNone );
       
    96 }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CSdDisk::CheckDiskSpace()
       
   100 // This method checks free disk space 
       
   101 //
       
   102 // Method is the almost the same than FreeSpaceL(RFs* aFs, TInt aDrive) in sysutil.cpp
       
   103 // -----------------------------------------------------------------------------
       
   104 TReal64 CSdDisk::CheckDiskSpace(RFs& aFs)
       
   105     {
       
   106 	// RDebug::Print(_L("Shareddatatest ## CheckDiskSpace"));
       
   107     TInt err = KErrNone;
       
   108 
       
   109 	TVolumeInfo vinfo;
       
   110 
       
   111 	err = aFs.Volume(vinfo, EDriveC);
       
   112 	
       
   113 	if ( err != KErrNone )
       
   114 	    {
       
   115 	    RDebug::Print(_L("Shareddatatest ## Error = %d"), err);
       
   116 		User::LeaveIfError(err);
       
   117 	    }
       
   118 
       
   119 	// RDebug::Print(_L("Shareddatatest ## Disk space: %f "), TReal64(vinfo.iFree));
       
   120 	
       
   121 	return TReal64(vinfo.iFree);
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CSdDisk::CheckDiskSpace()
       
   126 // This method checks the reserve file size
       
   127 //
       
   128 // -----------------------------------------------------------------------------
       
   129 TInt CSdDisk::CheckReserveFileSize(RFs& aFs)
       
   130     {
       
   131     RFile iReserveFile;
       
   132     
       
   133     TInt err = iReserveFile.Open( aFs, KSDReserveFileName, EFileShareAny | EFileWrite );
       
   134     
       
   135     if (err == KErrNone)
       
   136         {
       
   137         TInt iReserveFileSize(0);
       
   138         iReserveFile.Size(iReserveFileSize);
       
   139         iReserveFile.Close();
       
   140         
       
   141         RDebug::Print( _L("Shareddatatest ## Reservefilesize: %d"), iReserveFileSize );
       
   142         
       
   143         return iReserveFileSize;
       
   144         }
       
   145     else
       
   146         {
       
   147         return KErrGeneral;
       
   148         }
       
   149     }
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // CSdDisk::FillDisk()
       
   153 // Fills the diskspace 
       
   154 // 
       
   155 // -----------------------------------------------------------------------------
       
   156 void CSdDisk::FillDisk(RFs& aFs, TInt& aFreeDiskSpaceToLeave)
       
   157     {
       
   158 	RFile iEaterFile;
       
   159 	TInt fileSize(0);	
       
   160 	TInt err(KErrNone);
       
   161 	
       
   162 	TReal64 diskFree = CheckDiskSpace(aFs);
       
   163 	// RDebug::Print(_L("Shareddatatest ## Eat disk space") );
       
   164 	
       
   165 	// For loop to create eaterfiles
       
   166 	for ( TInt i = 0 ; diskFree != 0 ; i++ )
       
   167 	    {	
       
   168 		TBuf16<30> str;
       
   169 		str.Copy(KFileName);
       
   170 		str.AppendNum(i,EDecimal);
       
   171 			
       
   172 		// RDebug::Print(_L("Shareddatatest ## Open file %s"), &str);
       
   173 	
       
   174 		err = iEaterFile.Open( aFs, str, EFileShareAny | EFileWrite );
       
   175 
       
   176 		if ( err == KErrNotFound )	// file does not exist - create it
       
   177 		    {
       
   178 			err = iEaterFile.Create( aFs, str, EFileShareAny | EFileWrite );
       
   179 		    }
       
   180 
       
   181 		if ( err == KErrNone )
       
   182 		    {
       
   183 			// Create maximum size eaterfile if there is more disk space available than the maximum file size
       
   184 			if ( diskFree > TReal64(KMaxTInt) )
       
   185 			    {
       
   186 				// RDebug::Print( _L("Shareddatatest ## Creating %d. EaterFile"), i+1);
       
   187 				err = iEaterFile.SetSize( KMaxTInt );
       
   188 				
       
   189 				diskFree = diskFree - TReal64(KMaxTInt);
       
   190 			    }
       
   191 			
       
   192 			// ... And if available disk space is less than maximum file size
       
   193 			else
       
   194 			    {
       
   195 				// If aFreeDiskSpaceToLeave parameter says we have to leave some free disk
       
   196 				if ( aFreeDiskSpaceToLeave != 0 )
       
   197 				    {
       
   198 					// RDebug::Print( _L("Shareddatatest ## Creating %d. EaterFile"), i+1);
       
   199 					TInt sizeToSet = STATIC_CAST(TInt, I64INT(diskFree));
       
   200 					err = iEaterFile.SetSize( sizeToSet - aFreeDiskSpaceToLeave * 1024 );
       
   201 					iEaterFile.Size( fileSize );
       
   202 					// RDebug::Print( _L("Shareddatatest ## Last EaterFileSize: %d"), fileSize);
       
   203 					diskFree = 0;	
       
   204 				    }
       
   205 				
       
   206 				// Otherwise try to eat all the disk space
       
   207 				else
       
   208 				    {
       
   209 					// RDebug::Print( _L("Shareddatatest ## Creating %d. EaterFile"), i+1);
       
   210 					TInt sizeToSet = STATIC_CAST(TInt, I64INT(diskFree));
       
   211 					// RDebug::Print( _L("Shareddatatest ## Filesize in the end should be around %d bytes"), sizeToSet);
       
   212 					
       
   213 					for ( TInt j = 64 ; err != KErrDiskFull ; j-- )
       
   214 					    {	
       
   215 						err = iEaterFile.SetSize( sizeToSet - j * 1024 );
       
   216 					    }
       
   217 					iEaterFile.Size( fileSize );
       
   218 					// RDebug::Print( _L("Shareddatatest ## Last EaterFileSize: %d bytes"), fileSize);
       
   219 					// RDebug::Print( _L("Shareddatatest ## There should be %d bytes of free disk"),sizeToSet - fileSize);
       
   220 					diskFree = 0;
       
   221 				    }
       
   222 			    }
       
   223 		    }
       
   224 		
       
   225 		else
       
   226 		    {
       
   227 			RDebug::Print(_L("Shareddatatest ## Error = %d"), err);
       
   228 			diskFree = 0;
       
   229 		    }
       
   230 
       
   231 		// RDebug::Print(_L("Shareddatatest ## Close file"));
       
   232 		iEaterFile.Close();
       
   233 	    }
       
   234     }