|
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 TInt fileDeleteErr=iFs.Delete(*iName); |
|
34 #ifdef _DEBUG |
|
35 if (fileDeleteErr != KErrNone) |
|
36 { |
|
37 RDebug::Print(_L("CDbFileStoreDatabase::~CDbFileStoreDatabase - Failed to delete file. Error = %d"), fileDeleteErr); |
|
38 } |
|
39 #endif |
|
40 } |
|
41 delete iName; |
|
42 } |
|
43 |
|
44 CDbFileStoreDatabase* CDbFileStoreDatabase::NewLC(RFs& aFs) |
|
45 { |
|
46 CDbFileStoreDatabase* self=new(ELeave) CDbFileStoreDatabase(aFs); |
|
47 CleanupStack::PushL(self); |
|
48 return self; |
|
49 } |
|
50 |
|
51 // |
|
52 // over-ride the store database and just mark the file for deletion |
|
53 // |
|
54 EXPORT_C void CDbFileStoreDatabase::DestroyL() |
|
55 { |
|
56 iDelete=ETrue; |
|
57 } |
|
58 |
|
59 // |
|
60 // Provide the "size" and "usage" properties |
|
61 // |
|
62 EXPORT_C TInt CDbFileStoreDatabase::Property(CDbDatabase::TProperty aProperty) |
|
63 { |
|
64 switch (aProperty) |
|
65 { |
|
66 case CDbDatabase::EUpdateStats: |
|
67 return 1; |
|
68 case CDbDatabase::ESize: |
|
69 case CDbDatabase::EUsage: |
|
70 { |
|
71 TInt size; |
|
72 TInt r=STATIC_CAST(CFileStore&,Store()).File().Size(size); |
|
73 if (r<0) |
|
74 return r; |
|
75 if (aProperty==CDbDatabase::ESize) |
|
76 return size; |
|
77 r=iReclaim; |
|
78 if (r<0) |
|
79 return r; |
|
80 return 100-(r*100)/size; // usage in % |
|
81 } |
|
82 default: |
|
83 return CDbStoreDatabase::Property(aProperty); |
|
84 } |
|
85 } |
|
86 |
|
87 |
|
88 //SYMBIAN_REMOVE_TRIVIAL_ENCRYPTION version of the method. |
|
89 EXPORT_C void CDbFileStoreDatabase::CreateL(const TDesC& aName, TDbFormat::TCreate aMode, |
|
90 const TUidType& aType) |
|
91 { |
|
92 __ASSERT(!iName); // check construction phase |
|
93 // |
|
94 iName=aName.AllocL(); |
|
95 CFileStore* store; |
|
96 switch (aMode) |
|
97 { |
|
98 default: |
|
99 __LEAVE(KErrNotSupported); |
|
100 case TDbFormat::ECreate: |
|
101 store=CPermanentFileStore::CreateL(iFs,aName,EFileRead|EFileWrite); |
|
102 break; |
|
103 case TDbFormat::EReplace: |
|
104 store=CPermanentFileStore::ReplaceL(iFs,aName,EFileRead|EFileWrite); |
|
105 break; |
|
106 }; |
|
107 iStore=store; |
|
108 iDelete=ETrue; // cleanup fully in case of failure |
|
109 store->SetTypeL(aType); |
|
110 store->SetRootL(CreateRootL(CDbStoreDatabase::ConstructL())); |
|
111 store->CommitL(); |
|
112 iDelete=EFalse; // file is now good |
|
113 } |
|
114 |
|
115 // SYMBIAN_REMOVE_TRIVIAL_ENCRYPTION version of the method. |
|
116 // Create the standard file database. The database is the root stream |
|
117 // |
|
118 EXPORT_C CDbDatabase* CDbFileStoreDatabase::CreateL(RFs& aFs, const TDesC& aName, |
|
119 TDbFormat::TCreate aMode, const TUidType& aType) |
|
120 { |
|
121 CDbFileStoreDatabase* self=NewLC(aFs); |
|
122 self->CreateL(aName,aMode,aType); |
|
123 CDbDatabase* db=self->InterfaceL(); |
|
124 CleanupStack::Pop(self); |
|
125 return db; |
|
126 } |
|
127 |
|
128 |
|
129 // |
|
130 // default implementation. Database _is_ the root |
|
131 // |
|
132 EXPORT_C TStreamId CDbFileStoreDatabase::CreateRootL(TStreamId aDatabaseId) |
|
133 { |
|
134 return aDatabaseId; |
|
135 } |
|
136 |
|
137 // |
|
138 // Open, phase #1 |
|
139 // open the file-store and return the root stream id |
|
140 // |
|
141 EXPORT_C void CDbFileStoreDatabase::OpenL(const TDesC& aName,TDbFormat::TOpen aMode) |
|
142 { |
|
143 __ASSERT(!iName); // check construction phase |
|
144 // |
|
145 iName=aName.AllocL(); |
|
146 const TUint mode=aMode==TDbFormat::EReadOnly ? EFileShareReadersOnly : EFileWrite; |
|
147 CFileStore* store=CPermanentFileStore::OpenL(iFs,*iName,mode); |
|
148 iStore=store; |
|
149 CDbStoreDatabase::RestoreL(DatabaseIdL(store->Root())); |
|
150 } |
|
151 |
|
152 // |
|
153 // default implementation. Database _is_ the root |
|
154 // |
|
155 EXPORT_C TStreamId CDbFileStoreDatabase::DatabaseIdL(TStreamId aRootId) |
|
156 { |
|
157 return aRootId; |
|
158 } |
|
159 |
|
160 // |
|
161 // Create the standard file database. The database is the root stream |
|
162 // |
|
163 EXPORT_C CDbSource* CDbFileStoreDatabase::OpenL(RFs& aFs,const TDesC& aName,TDbFormat::TOpen aMode) |
|
164 { |
|
165 CDbFileStoreDatabase* self=NewLC(aFs); |
|
166 self->OpenL(aName,aMode); |
|
167 CDbSource* src=self->SourceL(); |
|
168 CleanupStack::Pop(); // self |
|
169 return src; |
|
170 } |
|
171 |
|
172 const TDbFormat KBuiltinFormat= |
|
173 { |
|
174 _S("epoc"),&CDbFileStoreDatabase::CreateL,&CDbFileStoreDatabase::OpenL, |
|
175 {KPermanentFileStoreLayoutUidValue,KDbmsFileDatabaseUidValue} |
|
176 }; |
|
177 |
|
178 GLDEF_D const TDbDriver KBuiltinDriver={1,&KBuiltinFormat}; |