persistentstorage/dbms/ustor/US_FILE.CPP
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Fri, 11 Jun 2010 15:29:22 +0300
changeset 25 63532cdadd44
parent 0 08ec8eefde2f
child 29 cce6680bbf1c
permissions -rw-r--r--
Revision: 201023 Kit: 2010123

// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
// All rights reserved.
// This component and the accompanying materials are made available
// under the terms of "Eclipse Public License v1.0"
// which accompanies this distribution, and is available
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
//
// Initial Contributors:
// Nokia Corporation - initial contribution.
//
// Contributors:
//
// Description:
// The file-store database which provides the default format
// 
//

#include "US_STD.H"
#include <s32file.h>

EXPORT_C CDbFileStoreDatabase::CDbFileStoreDatabase(RFs& aFs)
	:iFs(aFs)
	{}

EXPORT_C CDbFileStoreDatabase::~CDbFileStoreDatabase()
	{
	if (iDelete)
		{
		delete iStore;		// must be destroyed before the file is deleted
		iStore=0;

		// If a debug build - record error
		TInt fileDeleteErr=iFs.Delete(*iName);
		#ifdef _DEBUG
			if (fileDeleteErr != KErrNone)
			{
				RDebug::Print(_L("CDbFileStoreDatabase::~CDbFileStoreDatabase - Failed to delete file. Error = %d"), fileDeleteErr);
			}
		#endif
		}
	delete iName;
	}

CDbFileStoreDatabase* CDbFileStoreDatabase::NewLC(RFs& aFs)
	{
	CDbFileStoreDatabase* self=new(ELeave) CDbFileStoreDatabase(aFs);
	CleanupStack::PushL(self);
	return self;
	}

//
// over-ride the store database and just mark the file for deletion
//
EXPORT_C void CDbFileStoreDatabase::DestroyL()
	{
	iDelete=ETrue;
	}

//
// Provide the "size" and "usage" properties
//
EXPORT_C TInt CDbFileStoreDatabase::Property(CDbDatabase::TProperty aProperty)
	{
	switch (aProperty)
		{
	case CDbDatabase::EUpdateStats:
		return 1;
	case CDbDatabase::ESize:
	case CDbDatabase::EUsage:
		{
		TInt size;
		TInt r=STATIC_CAST(CFileStore&,Store()).File().Size(size);
		if (r<0)
			return r;
		if (aProperty==CDbDatabase::ESize)
			return size;
		r=iReclaim;
		if (r<0)
			return r;
		return 100-(r*100)/size;	// usage in %
		}
	default:
		return CDbStoreDatabase::Property(aProperty);
		}
	}


//SYMBIAN_REMOVE_TRIVIAL_ENCRYPTION version of the method.
EXPORT_C void CDbFileStoreDatabase::CreateL(const TDesC& aName, TDbFormat::TCreate aMode,
                                            const TUidType& aType)
	{
	__ASSERT(!iName);	// check construction phase
//
	iName=aName.AllocL();
	CFileStore* store;
	switch (aMode)
		{
	default:
		__LEAVE(KErrNotSupported);
	case TDbFormat::ECreate:
//  When the file cache in file server is on, the order of file writing is not guaranteed which could cause data inconsistency in some circumstances, for example, when the power is lost in the middle of data transaction. Therefore, the file write cache is switched off here to maintain the file integrity .	    
		store=CPermanentFileStore::CreateL(iFs,aName,EFileRead|EFileWrite/EFileWriteDirectIO);
		break;
	case TDbFormat::EReplace:
//  When the file cache in file server is on, the order of file writing is not guaranteed which could cause data inconsistency in some circumstances, for example, when the power is lost in the middle of data transaction. Therefore, the file write cache is switched off here to maintain the file integrity .
		store=CPermanentFileStore::ReplaceL(iFs,aName,EFileRead|EFileWrite/EFileWriteDirectIO);
		break;
		};
	iStore=store;
	iDelete=ETrue;		// cleanup fully in case of failure
	store->SetTypeL(aType);
	store->SetRootL(CreateRootL(CDbStoreDatabase::ConstructL()));
	store->CommitL();
	iDelete=EFalse;				// file is now good
	}

// SYMBIAN_REMOVE_TRIVIAL_ENCRYPTION version of the method.
// Create the standard file database. The database is the root stream
//
EXPORT_C CDbDatabase* CDbFileStoreDatabase::CreateL(RFs& aFs, const TDesC& aName,
                                                    TDbFormat::TCreate aMode, const TUidType& aType)
	{
	CDbFileStoreDatabase* self=NewLC(aFs);
	self->CreateL(aName,aMode,aType);
	CDbDatabase* db=self->InterfaceL();
	CleanupStack::Pop(self);
	return db;
	}


//
// default implementation. Database _is_ the root
//
EXPORT_C TStreamId CDbFileStoreDatabase::CreateRootL(TStreamId aDatabaseId)
	{
	return aDatabaseId;
	}

//
// Open, phase #1
// open the file-store and return the root stream id
//
EXPORT_C void CDbFileStoreDatabase::OpenL(const TDesC& aName,TDbFormat::TOpen aMode)
	{
	__ASSERT(!iName);	// check construction phase
//
	iName=aName.AllocL();
	TUint mode=aMode==TDbFormat::EReadOnly ? EFileShareReadersOnly : EFileWrite;
//	When the file cache in file server is on, the order of file writing is not guaranteed which could cause data inconsistency in some circumstances, for example, when the power is lost in the middle of data transaction. Therefore, the file write cache is switched off here to maintain the file integrity .
	mode = mode|EFileWriteDirectIO;
	CFileStore* store=CPermanentFileStore::OpenL(iFs,*iName,mode);
	iStore=store;
	CDbStoreDatabase::RestoreL(DatabaseIdL(store->Root()));
	}

//
// default implementation. Database _is_ the root
//
EXPORT_C TStreamId CDbFileStoreDatabase::DatabaseIdL(TStreamId aRootId)
	{
	return aRootId;
	}

//
// Create the standard file database. The database is the root stream
//
EXPORT_C CDbSource* CDbFileStoreDatabase::OpenL(RFs& aFs,const TDesC& aName,TDbFormat::TOpen aMode)
	{
	CDbFileStoreDatabase* self=NewLC(aFs);
	self->OpenL(aName,aMode);
	CDbSource* src=self->SourceL();
	CleanupStack::Pop();			// self
	return src;
	}

const TDbFormat KBuiltinFormat=
	{
	_S("epoc"),&CDbFileStoreDatabase::CreateL,&CDbFileStoreDatabase::OpenL,
	{KPermanentFileStoreLayoutUidValue,KDbmsFileDatabaseUidValue}
	};
	
GLDEF_D const TDbDriver KBuiltinDriver={1,&KBuiltinFormat};