installationservices/swi/source/sisregistry/server/sisregistryutil.cpp
changeset 0 ba25891c3a9e
child 25 7333d7932ef7
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2004-2009 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 the License "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 * SisRegistryUtil class - implementation
       
    16 * A collection of static functions to provide useful functionality to a the sisregistryserver as a whole 	
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 /**
       
    22  @file 
       
    23  @released
       
    24  @internalComponent
       
    25 */
       
    26 #include "sisregistryutil.h"
       
    27 #include <s32file.h>
       
    28 #include "log.h"
       
    29 #include "sisregistryserverconst.h"
       
    30 #include "dessisdataprovider.h"
       
    31 #include "sisregistryobject.h"
       
    32 #include "sisinstallblock.h"
       
    33 #include "sisregistryfiledescription.h"
       
    34 #include "sisinfo.h"
       
    35 #include "filesisdataprovider.h"
       
    36 #include "sisuid.h"
       
    37 
       
    38 using namespace Swi;
       
    39 
       
    40 const TInt KStubSisPUidPos = 0x18;
       
    41 
       
    42 HBufC* SisRegistryUtil::BuildControllerFileNameLC(TUid aUid, TInt aIndex, TInt aController, TInt aDrive)
       
    43 	{
       
    44 	TDriveUnit drive(aDrive);
       
    45 	TDriveName driveName = drive.Name();
       
    46 	HBufC *filename = HBufC::NewMaxLC(KMaxFileName);
       
    47 	TPtr filenamePtr(filename->Des());
       
    48 	filenamePtr.Format(KControllerFileNameFormat, &driveName, &KRegistryPath, aUid.iUid,
       
    49 					aIndex, aController);
       
    50 	return filename;
       
    51 	}
       
    52 
       
    53 HBufC* SisRegistryUtil::BuildStubFilesPathLC(TInt aDrive)
       
    54 	{
       
    55 	TDriveUnit drive(aDrive);
       
    56 	TDriveName driveName = drive.Name();
       
    57 	HBufC *filePath = HBufC::NewMaxLC(KMaxFileName);
       
    58 	TPtr filePathPtr(filePath->Des());
       
    59 	filePathPtr.Format(_L("%S%S"), &driveName, &KSwiDaemonPrivateDirectory);
       
    60 	return filePath;
       
    61 	}
       
    62 
       
    63 HBufC* SisRegistryUtil::BuildControllerFileNameLC(TUid aUid, TInt aIndex, TInt aController)
       
    64 	{
       
    65 	return BuildControllerFileNameLC(aUid, aIndex, aController, RFs::GetSystemDrive());
       
    66 	}
       
    67 
       
    68 HBufC* SisRegistryUtil::BuildUninstallLogFileNameLC(TUid aUid, TInt aIndex, TChar aDriveLetter)
       
    69 	{
       
    70 	TDriveUnit systemDrive(RFs::GetSystemDrive());
       
    71 	TDriveName systemDriveName = systemDrive.Name();
       
    72 	HBufC *filename = HBufC::NewMaxLC(KMaxFileName);
       
    73 	TPtr filenamePtr(filename->Des());
       
    74 	filenamePtr.Format(KUninstallLogPathFormat, (TUint)systemDriveName[0], aUid.iUid, aIndex, (TUint)aDriveLetter);	
       
    75 	return filename;
       
    76 	}
       
    77 
       
    78 TBool SisRegistryUtil::FileExistsL(RFs& aFs, const TDesC& aFullPath)
       
    79 	{
       
    80 	TBool found = EFalse;
       
    81 	TEntry entry;
       
    82 	TInt err = aFs.Entry(aFullPath, entry);
       
    83 	if (err == KErrNone)
       
    84 		{
       
    85 		return  ETrue;
       
    86 		}
       
    87 	else if (err != KErrNotFound && err != KErrPathNotFound)
       
    88 		{
       
    89 		User::Leave(err);
       
    90 		}
       
    91 	
       
    92 	return found;
       
    93 	}
       
    94 
       
    95 void SisRegistryUtil::EnsureDirExistsL(RFs& aFs, const TDesC& aPath)
       
    96 	{
       
    97 	TInt err = aFs.MkDirAll(aPath);
       
    98 	if (err != KErrNone && err != KErrAlreadyExists)
       
    99 		{
       
   100 		User::LeaveIfError(err);
       
   101 		}
       
   102 	}
       
   103 
       
   104 // Processes the ROM stub files. Retuns ETrue if the aUid or aName
       
   105 // is found in any of the ROM stub files, else returns EFalse.
       
   106 TBool SisRegistryUtil::RomBasedPackageL(const TUid& aUid)
       
   107 	{	
       
   108 	TBool isInRom = EFalse;
       
   109 	
       
   110 	RFs fileServer;
       
   111 	User::LeaveIfError(fileServer.Connect());
       
   112 	CleanupClosePushL(fileServer);
       
   113 	
       
   114 	CDir* stubList = NULL;
       
   115 	_LIT(KRomStubPath, "z:\\system\\install\\%S");
       
   116 	_LIT(KRomStubWildCard,"z:\\system\\install\\*.sis");
       
   117 	
       
   118 	TEntry stubEntry;
       
   119 	RBuf stubFileName;
       
   120 	stubFileName.CreateL(KMaxFileName);
       
   121 	CleanupClosePushL(stubFileName);
       
   122 	//Iterates through the stub files in the ROM calling CheckIfMatchingStubFileL() 
       
   123 	//to search for the presence of the package name ignoring any SIS parsing errors.
       
   124 	if (fileServer.GetDir(KRomStubWildCard,KEntryAttNormal, ESortByName, stubList)==KErrNone)
       
   125 		{		
       
   126 		CleanupStack::PushL(stubList);		
       
   127 		for (TInt stubCount = 0; stubCount < stubList->Count(); stubCount++)
       
   128 			{			
       
   129 			stubEntry = (*stubList)[stubCount];
       
   130 			stubFileName.Format(KRomStubPath, &(stubEntry.iName));		
       
   131 			
       
   132 			
       
   133 			TRAPD(err, isInRom = SisRegistryUtil::CheckIfMatchingStubPUidL(aUid,  fileServer, stubFileName));
       
   134 			
       
   135 			if ((err >= KErrSISNotEnoughSpaceToInstall)  && (err <= KErrSISFieldIdMissing))
       
   136   				{
       
   137   				//Error reading SIS stub details, skip this one and continue searching the list.
       
   138   				DEBUG_PRINTF3(_L("ERROR: Stub file in ROM failed to parse. Filename: %S. Error code: %d."), &stubEntry.iName, err);
       
   139   				}
       
   140   			else if (err != KErrNone)
       
   141   				{
       
   142   				User::Leave(err);
       
   143   				}
       
   144   			else if (isInRom)
       
   145   				{
       
   146 				break;
       
   147   				}
       
   148 			}
       
   149 		CleanupStack::PopAndDestroy(stubList);
       
   150 		}			
       
   151 	CleanupStack::PopAndDestroy(2, &fileServer); // stubFileName
       
   152 	return isInRom;
       
   153 	}
       
   154 
       
   155  HBufC* SisRegistryUtil::BuildLogFileNameLC()
       
   156  	{
       
   157  	HBufC *filename = HBufC::NewMaxLC(KMaxFileName);
       
   158  	TPtr filenamePtr(filename->Des());
       
   159  	TDriveUnit sysDrive(RFs::GetSystemDrive());
       
   160  	filenamePtr = sysDrive.Name();
       
   161  	filenamePtr.Append(KLogDir);
       
   162  	return filename;
       
   163  	}
       
   164 
       
   165 TUid SisRegistryUtil::IdentifyUidFromSisFileL(RFs& aFs, TDesC& aFilename)
       
   166 	{
       
   167 	// Returns the package UID read from the symbian header in the specified
       
   168 	// file.  Note that there is no check to ensure that it is a valid sisx
       
   169 	// file, but it will leave if the file is too short.
       
   170 	RFile file;
       
   171 	User::LeaveIfError(file.Open(aFs, aFilename, EFileRead | EFileStream));
       
   172 	CleanupClosePushL(file);
       
   173 	TUid packageUid;
       
   174 	TUid uid1;
       
   175 	TUid uid2;
       
   176 	ReadSymbianHeaderL(file, uid1, uid2, packageUid);
       
   177 	CleanupStack::PopAndDestroy(&file);
       
   178 	return packageUid;
       
   179 	}
       
   180 
       
   181 void SisRegistryUtil::ReadSymbianHeaderL(RFile& aFile, TUid& aUid1, TUid& aUid2, TUid& aUid3)
       
   182 	{
       
   183 	// Read the first 3 32-bit values from the file as UIDs 1 to 3, leave with
       
   184 	// KErrUnderflow if the file is too short.
       
   185 	TInt uidLen = sizeof(TInt32);
       
   186 
       
   187 	TPckg<TInt32> uid1(aUid1.iUid);
       
   188 	User::LeaveIfError(aFile.Read(uid1, uidLen));
       
   189 	if (uid1.Length() != uidLen)
       
   190 		{
       
   191 		User::Leave(KErrUnderflow);
       
   192 		}
       
   193 
       
   194 	TPckg<TInt32> uid2(aUid2.iUid);
       
   195 	User::LeaveIfError(aFile.Read(uid2, uidLen));
       
   196 	if (uid1.Length() != uidLen)
       
   197 		{
       
   198 		User::Leave(KErrUnderflow);
       
   199 		}
       
   200 
       
   201 	TPckg<TInt32> uid3(aUid3.iUid);
       
   202 	User::LeaveIfError(aFile.Read(uid3, uidLen));
       
   203 	if (uid1.Length() != uidLen)
       
   204 		{
       
   205 		User::Leave(KErrUnderflow);
       
   206 		}
       
   207 	}
       
   208 
       
   209 /**
       
   210 Reads the pUid of stub sis file from a given position in the file.
       
   211 and match it with aUid
       
   212 @param aName The file name being searched for.
       
   213 @param aUid  The package uid being searched for.
       
   214 @param aFileServer Handle to the file server to use.
       
   215 @param aStubFileName Complete path and filename of the stub file being checked.
       
   216 @return ETrue if the package uid or the pkg name is found, EFalse otherwise.
       
   217 */
       
   218 TBool SisRegistryUtil::CheckIfMatchingStubPUidL(const TUid& aUid, RFs& aFileServer, const TFileName& aStubFileName)
       
   219 	{
       
   220 	
       
   221 	RFile sisFile;
       
   222 	User::LeaveIfError(sisFile.Open(aFileServer, aStubFileName, EFileRead));
       
   223 	CleanupClosePushL(sisFile);
       
   224 	RFileReadStream sisFileReadStream(sisFile, KStubSisPUidPos);
       
   225 	CleanupClosePushL(sisFileReadStream);
       
   226 	
       
   227 	TUid packageUid = TUid::Uid(sisFileReadStream.ReadUint32L());
       
   228 	
       
   229 	CleanupStack::PopAndDestroy(2,&sisFile); //readstream, file
       
   230 	return (aUid == packageUid);
       
   231 	
       
   232 	}
       
   233