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