Esempio n. 1
0
void CCreGenerator::CommitChangesToCreL(RFs& aFs,TUint8 aPersistVersion,CHeapRepository& aRep,const TDesC& aTargetFilePath)
{
    HBufC* tmpFilePath=aTargetFilePath.AllocLC();
    TPtr tmpFilePathPtr(tmpFilePath->Des());
    tmpFilePathPtr.Replace(tmpFilePath->Length()-3,3,KTmpExtension());

    CDirectFileStore* store = CDirectFileStore::ReplaceLC(aFs, *tmpFilePath,(EFileWrite | EFileShareExclusive));

    const TUid uid2	 = KNullUid ;
    store->SetTypeL(TUidType(KDirectFileStoreLayoutUid, uid2, KServerUid3)) ;

    // Write the stream index/dictionary as root stream within the store
    // so we can access it when we do a restore later on
    RStoreWriteStream rootStream ;
    TStreamId rootStreamId = rootStream.CreateLC(*store) ;
    ExternalizeCre(aPersistVersion,aRep, rootStream) ;
    rootStream.CommitL() ;

    CleanupStack::PopAndDestroy(&rootStream) ;
    store->SetRootL(rootStreamId);
    store->CommitL();
    CleanupStack::PopAndDestroy(store) ;
    User::LeaveIfError(aFs.Replace(*tmpFilePath,aTargetFilePath));
    CleanupStack::PopAndDestroy();
}
TInt CTestStepFlogger::replaceFloggerIniL( const TDesC8& newConfig )
/**
Replace the flogger.ini with a new one made up of the contents of newConfig
@param newConfig string with all the config items to be set in flogger.ini
@return KErrNone if no probs, otherwise an error code.
 */
	{
	RFile outFile;
	RFs fileServer; 
	User::LeaveIfError(fileServer.Connect());
	HBufC8 * fileContentsHeap;
	TInt returnCode;	

	// allocate the heap space for the string given size of 
	// the items string.
	TInt newFileSize = newConfig.Length();  //just so we can see this during debugging

	fileContentsHeap = HBufC8::NewLC(newFileSize);

	TPtr8 newFileContents(fileContentsHeap->Des());
	
	// add the config items
	newFileContents.Append(newConfig);

	// We must assume the flogger.ini is either there or not there,
	// If the flogger.ini is already there, and the flogger server is already running
	// and watching this file, we cannot do any of the following:
	//* Open and overwrite - if we overwrite a large file with a smaller one, it fails to overwrite the whole file
	//* Delete and create - after the delete but before the create flogger server will attempt to access
	//* use RFile::Replace - between the replace and the write flogger server will attempt access
	
	// so we must create a temporary file and then use the file system to overwrite the
	// current with our temp using "replace" which is an atomic operation as far as flogger server is concerned

	returnCode = outFile.Create(fileServer,KTempDuringCreationFloggerIniFile,EFileWrite);
	if (returnCode == KErrNone)
		{
		TInt pos = 0;
		outFile.Seek(ESeekStart,pos);
		outFile.Write(newFileContents);
		
		outFile.Close();
		
		fileServer.Replace(KTempDuringCreationFloggerIniFile,KFloggerIniFile);
		}
	CleanupStack::PopAndDestroy(fileContentsHeap);
	
	fileServer.Close();

	if (returnCode != KErrNone)
		{
		return returnCode;
		}
	else
		{
		return KErrNone;
		}

	}
int DeviceManagementNode::renameFileInCwd(const char* src, const char* dst)
{
    RFs fileSession;
    RFile file;

    StringBuffer srcSb(currentDir);
    concatDirs(srcSb, src);
    StringBuffer dstSb(currentDir);
    concatDirs(dstSb, dst);

    RBuf srcDes, dstDes;
    srcDes.Assign(stringBufferToNewBuf(srcSb));
    dstDes.Assign(stringBufferToNewBuf(dstSb));

    // Connect to the file server
    fileSession.Connect();
    CleanupClosePushL(fileSession);

    // Replace 'config.ini' file with 'config.ini.tmp'
    TInt err = fileSession.Replace(srcDes, dstDes);

    CleanupStack::PopAndDestroy(&fileSession);
    srcDes.Close();
    dstDes.Close();

    if (err == KErrNone) {
        return 0;
    }
    else {
        LOG.error("Error (code %d) replacing file '%s'", err, dstSb.c_str());
        if (err == KErrAccessDenied) {
            LOG.error("Access denied");
        }
        else if (err == KErrPathNotFound) {
            LOG.error("Unable to find the specified folder");
        }
        return -1;
    }
}
TInt CTestStepFlogger::constructFloggerIniL( const TDesC8& additionalConfig )
/**
Replace the flogger.ini with a new one made up of the contents of ts_flogger.ini and additionalConfig.
@param additionalConfig string with the extra config items to be set in flogger.ini
@return KErrNone if no probs, otherwise an error code.
@note This function deletes then recreates the flogger.ini file, so during the short time between
 the delete and the recreate, if flogger server is running, it will report that there were errors in the ini file (because
 the ini file is not there).
 */
	{
	RFile theFile;
	RFile outFile;
	RFs fileServer; 
	User::LeaveIfError(fileServer.Connect());
	HBufC8 * fileContentsHeap;
	TInt fileSize;
	TInt returnCode;	

	returnCode = theFile.Open(fileServer,KFloggerTestIniMediaSourceFile,EFileRead);
	
	if (returnCode == KErrNone)
		{
		theFile.Size(fileSize);
		
		// allocate the heap space for the string given size of ts_flogger and
		// the length of the additional items string.
		TInt newFileSize = fileSize + additionalConfig.Length();  //just so we can see this during debugging

		fileContentsHeap = HBufC8::NewLC(newFileSize);

		TPtr8 fileContentsAppend(fileContentsHeap->Des());
		
		// read into the buffer the contents of ts_flogger.ini
		User::LeaveIfError(returnCode = theFile.Read(fileContentsAppend));
		
		// append addition items
		fileContentsAppend.Append(additionalConfig);

		// We must assume the flogger.ini is either there or not there,
		// If the flogger.ini is already there, and the flogger server is already running
		// and watching this file, we cannot do any of the following:
		//* Open and overwrite - if we overwrite a large file with a smaller one, it fails to overwrite the whole file
		//* Delete and create - after the delete but before the create flogger server will attempt to access
		//* use RFile::Replace - between the replace and the write flogger server will attempt access
		
		// so we must create a temporary file and then use the file system to overwrite the
		// current with our temp using "replace" which is an atomic operation as far as flogger server is concerned

		returnCode = outFile.Create(fileServer,KTempDuringCreationFloggerIniFile,EFileWrite);
		if (returnCode == KErrNone)
			{
			TInt pos = 0;
			outFile.Seek(ESeekStart,pos);
			outFile.Write(fileContentsAppend);
			
			outFile.Close();
			
			fileServer.Replace(KTempDuringCreationFloggerIniFile,KFloggerIniFile);
			}
		CleanupStack::PopAndDestroy(fileContentsHeap);
		theFile.Close();
		}
	
	fileServer.Close();

	if (returnCode != KErrNone)
		{
		return returnCode;
		}
	else
		{
		return KErrNone;
		}

	}