|
1 /* |
|
2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <e32base.h> |
|
20 #include <f32file.h> // for TDriveNumber |
|
21 #include <centralrepository.h> |
|
22 #include <e32property.h> |
|
23 |
|
24 #include <UiklafInternalCRKeys.h> // for disc space |
|
25 |
|
26 #include "DevEncCommonUtils.hrh" |
|
27 #include "DevEncDiskUtils.h" |
|
28 #include "DevEncLog.h" |
|
29 |
|
30 /*static const TPtrC KDriveLetter[] = { |
|
31 _L("A"), _L("B"), _L("C"), _L("D"), _L("E"), _L("F"), _L("G"), _L("H"), |
|
32 _L("I"), _L("J"), _L("K"), _L("L"), _L("M"), _L("N"), _L("O"), _L("P"), |
|
33 _L("Q"), _L("R"), _L("S"), _L("T"), _L("U"), _L("V"), _L("W"), _L("X"), |
|
34 _L("Y"), _L("Z") };*/ |
|
35 |
|
36 _LIT( KA, "A"); _LIT( KB, "B");_LIT( KC, "C"); _LIT( KD, "D"); |
|
37 _LIT( KE, "E"); _LIT( KF, "F"); _LIT( KG, "G"); _LIT( KH, "H"); |
|
38 _LIT( KI, "I"); _LIT( KJ, "J");_LIT( KK, "K"); _LIT( KL, "L"); |
|
39 _LIT( KM, "M"); _LIT( KN, "N"); _LIT( KO, "O"); _LIT( KP, "P"); |
|
40 _LIT( KQ, "Q"); _LIT( KR, "R"); _LIT( KS, "S"); _LIT( KT, "T"); |
|
41 _LIT( KU, "U"); _LIT( KV, "V"); _LIT( KW, "W"); _LIT( KX, "X"); |
|
42 _LIT( KY, "Y"); _LIT( KZ, "Z"); |
|
43 |
|
44 _LIT( KContentTestFile, "c:\\content_test.txt" ); |
|
45 static const TUint32 KTestBlockSizeBt = 1024 * sizeof( TUint32 ); |
|
46 static const TUint32 KTestBlocks = 320; |
|
47 static const TInt32 KMaxInt = 2147483647; |
|
48 |
|
49 // -------------------------------------------------------------------------- |
|
50 // CDevEncDiskUtils::DriveLetter() |
|
51 // |
|
52 // -------------------------------------------------------------------------- |
|
53 EXPORT_C TInt CDevEncDiskUtils::DriveLetter( TInt aNumber, |
|
54 TDes& aResult ) |
|
55 { |
|
56 const TInt KNumOfDriveLetters( 25 ); |
|
57 const TPtrC KDriveLetter[] = { |
|
58 KA(), KB(), KC(), KD(), KE(), KF(), KG(), KH(), |
|
59 KI(), KJ(), KK(), KL(), KM(), KN(), KO(), KP(), |
|
60 KQ(), KR(), KS(), KT(), KU(), KV(), KW(), KX(), |
|
61 KY(), KZ() }; |
|
62 if ( aNumber > KNumOfDriveLetters ) |
|
63 { |
|
64 return KErrOverflow; |
|
65 } |
|
66 aResult.Append( KDriveLetter[ aNumber ] ); |
|
67 return KErrNone; |
|
68 } |
|
69 |
|
70 // -------------------------------------------------------------------------- |
|
71 // CDevEncDiskUtils::DrivePath() |
|
72 // |
|
73 // -------------------------------------------------------------------------- |
|
74 EXPORT_C void CDevEncDiskUtils::DrivePath( TDes& aDes, TDriveNumber aNumber ) |
|
75 { |
|
76 DriveLetter( aNumber, aDes ); |
|
77 aDes.Append( _L(":\\") ); |
|
78 } |
|
79 |
|
80 // -------------------------------------------------------------------------- |
|
81 // CDevEncDiskUtils::DoWriteTestFileL() |
|
82 // |
|
83 // -------------------------------------------------------------------------- |
|
84 void CDevEncDiskUtils::DoWriteTestFileL() |
|
85 { |
|
86 RFile file; |
|
87 RFs fs; |
|
88 |
|
89 User::LeaveIfError( fs.Connect() ); |
|
90 CleanupClosePushL( fs ); |
|
91 User::LeaveIfError( file.Replace( fs, KContentTestFile, EFileWrite ) ); |
|
92 CleanupClosePushL( file ); |
|
93 |
|
94 HBufC8* buf = HBufC8::NewL( KTestBlockSizeBt ); |
|
95 CleanupStack::PushL( buf ); |
|
96 |
|
97 TPtr8 pbuf = buf->Des(); |
|
98 pbuf.SetLength( KTestBlockSizeBt ); |
|
99 |
|
100 for( TUint32 i=0; i < KTestBlocks; i++ ) |
|
101 { |
|
102 for( TUint32 k=0; k < KTestBlockSizeBt; k++ ) |
|
103 pbuf[k] = 0xff; |
|
104 |
|
105 User::LeaveIfError( file.Write( pbuf ) ); |
|
106 } |
|
107 |
|
108 CleanupStack::PopAndDestroy(3); |
|
109 } |
|
110 |
|
111 // -------------------------------------------------------------------------- |
|
112 // CDevEncDiskUtils::WriteTestFile() |
|
113 // |
|
114 // -------------------------------------------------------------------------- |
|
115 EXPORT_C void CDevEncDiskUtils::WriteTestFile() |
|
116 { |
|
117 if ( ! RProcess().HasCapability( ECapabilityDiskAdmin ) ) |
|
118 { |
|
119 DFLOG( "Process does not have DiskAdmin capability" ); |
|
120 return; |
|
121 } |
|
122 |
|
123 DFLOG("Writing test file"); |
|
124 TRAPD( err, DoWriteTestFileL() ); |
|
125 if( err ) |
|
126 { |
|
127 DFLOG2("...failed %d", err); |
|
128 } |
|
129 else |
|
130 { |
|
131 DFLOG( "...ok" ); |
|
132 } |
|
133 } |
|
134 |
|
135 // -------------------------------------------------------------------------- |
|
136 // CDevEncDiskUtils::DoVerifyTestFileL() |
|
137 // |
|
138 // -------------------------------------------------------------------------- |
|
139 void CDevEncDiskUtils::DoVerifyTestFileL() |
|
140 { |
|
141 if ( ! RProcess().HasCapability( ECapabilityDiskAdmin ) ) |
|
142 { |
|
143 DFLOG( "Process does not have DiskAdmin capability" ); |
|
144 return; |
|
145 } |
|
146 |
|
147 RFile file; |
|
148 RFs fs; |
|
149 |
|
150 User::LeaveIfError( fs.Connect() ); |
|
151 CleanupClosePushL( fs ); |
|
152 User::LeaveIfError( file.Open( fs, KContentTestFile, EFileRead ) ); |
|
153 CleanupClosePushL( file ); |
|
154 |
|
155 HBufC8* buf = HBufC8::NewL(KTestBlockSizeBt); |
|
156 CleanupStack::PushL( buf ); |
|
157 |
|
158 TPtr8 pbuf = buf->Des(); |
|
159 pbuf.SetLength( KTestBlockSizeBt ); |
|
160 |
|
161 for( TUint32 i=0; i < KTestBlocks; i++ ) |
|
162 { |
|
163 User::LeaveIfError( file.Read( pbuf ) ); |
|
164 |
|
165 for( TUint32 k=0; k < KTestBlockSizeBt; k++ ) |
|
166 { |
|
167 if( pbuf[k] != 0xff ) |
|
168 DFLOG3("pos=%d val=%d", (i * KTestBlockSizeBt + k), pbuf[k]); |
|
169 } |
|
170 } |
|
171 |
|
172 CleanupStack::PopAndDestroy(3); |
|
173 } |
|
174 |
|
175 // -------------------------------------------------------------------------- |
|
176 // CDevEncDiskUtils::VerifyTestFile() |
|
177 // |
|
178 // -------------------------------------------------------------------------- |
|
179 EXPORT_C void CDevEncDiskUtils::VerifyTestFile() |
|
180 { |
|
181 DFLOG("Verifying test file"); |
|
182 TRAPD( err, DoVerifyTestFileL() ); |
|
183 if( err ) |
|
184 { |
|
185 DFLOG2("...failed %d", err); |
|
186 } |
|
187 else |
|
188 { |
|
189 DFLOG( "..ok" ); |
|
190 } |
|
191 } |
|
192 |
|
193 // -------------------------------------------------------------------------- |
|
194 // CDevEncDiskUtils::DeleteTestFile() |
|
195 // |
|
196 // -------------------------------------------------------------------------- |
|
197 EXPORT_C void CDevEncDiskUtils::DeleteTestFile() |
|
198 { |
|
199 DFLOG("Deleting test file"); |
|
200 RFs fs; |
|
201 |
|
202 TInt err = fs.Connect(); |
|
203 |
|
204 if( !err ) |
|
205 { |
|
206 err = fs.Delete( KContentTestFile ); |
|
207 |
|
208 if( !err || err == KErrNotFound ) |
|
209 { |
|
210 DFLOG("...ok"); |
|
211 } |
|
212 else |
|
213 { |
|
214 DFLOG2( "...failed %d", err ); |
|
215 } |
|
216 } |
|
217 else |
|
218 { |
|
219 DFLOG2( "fs.Connect() failed %d", err ); |
|
220 } |
|
221 } |
|
222 |
|
223 // -------------------------------------------------------------------------- |
|
224 // CDevEncDiskUtils::DiskFinalize() |
|
225 // |
|
226 // -------------------------------------------------------------------------- |
|
227 EXPORT_C TInt CDevEncDiskUtils::DiskFinalize( TDriveNumber aNumber ) |
|
228 { |
|
229 if ( ! RProcess().HasCapability( ECapabilityDiskAdmin ) ) |
|
230 { |
|
231 DFLOG( "Process does not have DiskAdmin capability" ); |
|
232 return KErrAccessDenied; |
|
233 } |
|
234 |
|
235 DFLOG("Finalizing"); |
|
236 |
|
237 RArray <TFileName> temps; |
|
238 RArray <RFile> files; |
|
239 RFs fs; |
|
240 |
|
241 TInt err = fs.Connect(); |
|
242 if( err ) |
|
243 DFLOG2( "..fs.Connect fail %d", err); |
|
244 |
|
245 TBool freeSpace = ETrue; |
|
246 |
|
247 TVolumeInfo volumeInfo; |
|
248 err = fs.Volume( volumeInfo, aNumber ); |
|
249 if( err ) |
|
250 DFLOG2( "..fs.Volume fail %d", err ); |
|
251 |
|
252 TInt64 size = 0; |
|
253 TInt64 memoryCriticalSpace(0); |
|
254 TRAP(err, memoryCriticalSpace = FindCriticalLevelTresholdL()); |
|
255 if (!err) |
|
256 return err; |
|
257 TInt64 memoryWarningSpace(0); |
|
258 TRAP(err, memoryWarningSpace = FindWarningLevelTresholdL()); |
|
259 if (!err) |
|
260 return err; |
|
261 |
|
262 DFLOG2( "CDevEncDiskUtils::DiskFinalize => memoryCriticalSpace = %d", (TInt) memoryCriticalSpace ); |
|
263 DFLOG2( "CDevEncDiskUtils::DiskFinalize => memoryWarningSpace = %d", (TInt) memoryWarningSpace ); |
|
264 |
|
265 //create the number of files needed to fill the mmc free space |
|
266 while( freeSpace ) |
|
267 { |
|
268 /* The real warning space is a percentage of the free space */ |
|
269 TInt64 newMemoryWarningSpace = ( TInt64 ) ( volumeInfo.iSize*( 100 - memoryWarningSpace ) ) / 100; |
|
270 DFLOG2( "CDevEncDiskUtils::DiskFinalize => newMemoryWarningSpace = %d", (TInt) newMemoryWarningSpace ); |
|
271 |
|
272 if( TInt64( volumeInfo.iFree ) > TInt64( KMaxInt ) ) |
|
273 { |
|
274 size = KMaxInt; |
|
275 } |
|
276 else |
|
277 { |
|
278 size = Max( 0LL, TInt64( volumeInfo.iFree ) - memoryCriticalSpace - newMemoryWarningSpace ); |
|
279 } |
|
280 |
|
281 TFileName temp; |
|
282 RFile file; |
|
283 |
|
284 if( !err ) |
|
285 { |
|
286 TBuf<8> path; |
|
287 DrivePath( path, aNumber ); |
|
288 |
|
289 err = file.Temp( fs, path, temp, EFileWrite ); |
|
290 if( err ) |
|
291 DFLOG2( "..file.Temp fail %d", err ); |
|
292 } |
|
293 |
|
294 err = file.SetSize( ( TInt ) size ); |
|
295 if( err ) |
|
296 DFLOG2( "..file.SetSize fail %d", err ); |
|
297 |
|
298 files.Append( file ); |
|
299 temps.Append( temp ); |
|
300 |
|
301 err = fs.Volume( volumeInfo, aNumber ); |
|
302 if( err ) |
|
303 DFLOG2( "..fs.Volume fail %d", err ); |
|
304 |
|
305 if( TInt64( volumeInfo.iFree ) <= memoryCriticalSpace + newMemoryWarningSpace ) |
|
306 { |
|
307 freeSpace = EFalse; |
|
308 } |
|
309 } |
|
310 |
|
311 //delete the created files |
|
312 for( TInt i=0;i < files.Count();i++ ) |
|
313 { |
|
314 files[i].Close(); |
|
315 fs.Delete( temps[i] ); // Error value ignored |
|
316 } |
|
317 |
|
318 files.Close(); |
|
319 temps.Close(); |
|
320 fs.Close(); |
|
321 return err; |
|
322 } |
|
323 |
|
324 // -------------------------------------------------------------------------- |
|
325 // CDevEncDiskUtils::FindValueL() |
|
326 // |
|
327 // -------------------------------------------------------------------------- |
|
328 TInt CDevEncDiskUtils::FindValueL( const TUid aRepositoryUid, const TUint aCRKey ) |
|
329 { |
|
330 TInt val( 0 ); |
|
331 CRepository* repository = CRepository::NewLC( aRepositoryUid ); |
|
332 User::LeaveIfError( repository->Get( aCRKey, val ) ); |
|
333 CleanupStack::PopAndDestroy( repository ); |
|
334 |
|
335 return val; |
|
336 } |
|
337 |
|
338 // -------------------------------------------------------------------------- |
|
339 // CDevEncDiskUtils::FindCriticalLevelTresholdL() |
|
340 // |
|
341 // -------------------------------------------------------------------------- |
|
342 TInt64 CDevEncDiskUtils::FindCriticalLevelTresholdL() |
|
343 { |
|
344 return (TInt64) FindValueL( KCRUidUiklaf, KUikOODDiskCriticalThreshold ); |
|
345 } |
|
346 |
|
347 // -------------------------------------------------------------------------- |
|
348 // CDevEncDiskUtils::FindWarningLevelTresholdL() |
|
349 // |
|
350 // -------------------------------------------------------------------------- |
|
351 TInt64 CDevEncDiskUtils::FindWarningLevelTresholdL() |
|
352 { |
|
353 return (TInt64) FindValueL( KCRUidUiklaf, /*KUikOODDiskWarningThreshold*/KUikOODDiskFreeSpaceWarningNoteLevel ); |
|
354 } |
|
355 |
|
356 // End of file |