os/persistentdata/persistentstorage/dbms/pcdbms/ustor/US_FILE.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // The file-store database which provides the default format
    15 // 
    16 //
    17 
    18 #include "US_STD.H"
    19 #include <s32file.h>
    20 
    21 EXPORT_C CDbFileStoreDatabase::CDbFileStoreDatabase(RFs& aFs)
    22 	:iFs(aFs)
    23 	{}
    24 
    25 EXPORT_C CDbFileStoreDatabase::~CDbFileStoreDatabase()
    26 	{
    27 	if (iDelete)
    28 		{
    29 		delete iStore;		// must be destroyed before the file is deleted
    30 		iStore=0;
    31 
    32 		// If a debug build - record error
    33 		(void)iFs.Delete(*iName);
    34 		}
    35 	delete iName;
    36 	}
    37 
    38 CDbFileStoreDatabase* CDbFileStoreDatabase::NewLC(RFs& aFs)
    39 	{
    40 	CDbFileStoreDatabase* self=new(ELeave) CDbFileStoreDatabase(aFs);
    41 	CleanupStack::PushL(self);
    42 	return self;
    43 	}
    44 
    45 //
    46 // over-ride the store database and just mark the file for deletion
    47 //
    48 EXPORT_C void CDbFileStoreDatabase::DestroyL()
    49 	{
    50 	iDelete=ETrue;
    51 	}
    52 
    53 //
    54 // Provide the "size" and "usage" properties
    55 //
    56 EXPORT_C TInt CDbFileStoreDatabase::Property(CDbDatabase::TProperty aProperty)
    57 	{
    58 	switch (aProperty)
    59 		{
    60 	case CDbDatabase::EUpdateStats:
    61 		return 1;
    62 	case CDbDatabase::ESize:
    63 	case CDbDatabase::EUsage:
    64 		{
    65 		TInt size;
    66 		TInt r=STATIC_CAST(CFileStore&,Store()).File().Size(size);
    67 		if (r<0)
    68 			return r;
    69 		if (aProperty==CDbDatabase::ESize)
    70 			return size;
    71 		r=iReclaim;
    72 		if (r<0)
    73 			return r;
    74 		return 100-(r*100)/size;	// usage in %
    75 		}
    76 	default:
    77 		return CDbStoreDatabase::Property(aProperty);
    78 		}
    79 	}
    80 
    81 
    82 //SYMBIAN_REMOVE_TRIVIAL_ENCRYPTION version of the method.
    83 EXPORT_C void CDbFileStoreDatabase::CreateL(const TDesC& aName, TDbFormat::TCreate aMode,
    84                                             const TUidType& aType)
    85 	{
    86 	__ASSERT(!iName);	// check construction phase
    87 //
    88 	iName=aName.AllocL();
    89 	CFileStore* store;
    90 	switch (aMode)
    91 		{
    92 	default:
    93 		__LEAVE(KErrNotSupported);
    94 	case TDbFormat::ECreate:
    95 		store=CPermanentFileStore::CreateL(iFs,aName,EFileRead|EFileWrite);
    96 		break;
    97 	case TDbFormat::EReplace:
    98 		store=CPermanentFileStore::ReplaceL(iFs,aName,EFileRead|EFileWrite);
    99 		break;
   100 		};
   101 	iStore=store;
   102 	iDelete=ETrue;		// cleanup fully in case of failure
   103 	store->SetTypeL(aType);
   104 	store->SetRootL(CreateRootL(CDbStoreDatabase::ConstructL()));
   105 	store->CommitL();
   106 	iDelete=EFalse;				// file is now good
   107 	}
   108 
   109 // SYMBIAN_REMOVE_TRIVIAL_ENCRYPTION version of the method.
   110 // Create the standard file database. The database is the root stream
   111 //
   112 EXPORT_C CDbDatabase* CDbFileStoreDatabase::CreateL(RFs& aFs, const TDesC& aName,
   113                                                     TDbFormat::TCreate aMode, const TUidType& aType)
   114 	{
   115 	CDbFileStoreDatabase* self=NewLC(aFs);
   116 	self->CreateL(aName,aMode,aType);
   117 	CDbDatabase* db=self->InterfaceL();
   118 	CleanupStack::Pop(self);
   119 	return db;
   120 	}
   121 
   122 
   123 //
   124 // default implementation. Database _is_ the root
   125 //
   126 EXPORT_C TStreamId CDbFileStoreDatabase::CreateRootL(TStreamId aDatabaseId)
   127 	{
   128 	return aDatabaseId;
   129 	}
   130 
   131 //
   132 // Open, phase #1
   133 // open the file-store and return the root stream id
   134 //
   135 EXPORT_C void CDbFileStoreDatabase::OpenL(const TDesC& aName,TDbFormat::TOpen aMode)
   136 	{
   137 	__ASSERT(!iName);	// check construction phase
   138 //
   139 	iName=aName.AllocL();
   140 	const TUint mode=aMode==TDbFormat::EReadOnly ? EFileShareReadersOnly : EFileWrite;
   141 	CFileStore* store=CPermanentFileStore::OpenL(iFs,*iName,mode);
   142 	iStore=store;
   143 	CDbStoreDatabase::RestoreL(DatabaseIdL(store->Root()));
   144 	}
   145 
   146 //
   147 // default implementation. Database _is_ the root
   148 //
   149 EXPORT_C TStreamId CDbFileStoreDatabase::DatabaseIdL(TStreamId aRootId)
   150 	{
   151 	return aRootId;
   152 	}
   153 
   154 //
   155 // Create the standard file database. The database is the root stream
   156 //
   157 EXPORT_C CDbSource* CDbFileStoreDatabase::OpenL(RFs& aFs,const TDesC& aName,TDbFormat::TOpen aMode)
   158 	{
   159 	CDbFileStoreDatabase* self=NewLC(aFs);
   160 	self->OpenL(aName,aMode);
   161 	CDbSource* src=self->SourceL();
   162 	CleanupStack::Pop();			// self
   163 	return src;
   164 	}
   165 
   166 const TDbFormat KBuiltinFormat=
   167 	{
   168 	_S("epoc"),&CDbFileStoreDatabase::CreateL,&CDbFileStoreDatabase::OpenL,
   169 	{KPermanentFileStoreLayoutUidValue,KDbmsFileDatabaseUidValue}
   170 	};
   171 	
   172 GLDEF_D const TDbDriver KBuiltinDriver={1,&KBuiltinFormat};