|
1 /* |
|
2 * Copyright (c) 2006 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 #include "messagestoreutils.h" |
|
19 #include "ContainerStoreUtils.h" |
|
20 |
|
21 |
|
22 //internal file copying buffer; the mass drives perform optimally with sizes of |
|
23 //128KBs and 256KBs. |
|
24 const TInt KInternalCopyBufferSize = 128*1024; |
|
25 |
|
26 |
|
27 /** |
|
28 * |
|
29 */ |
|
30 /*public static*/ |
|
31 void MessageStoreUtils::PrependBufferAndCopyFileL( |
|
32 RFs& aRfs, |
|
33 CContainerStoreUtils& aUtils, |
|
34 const TDesC& aSource, |
|
35 const TDesC& aTarget, |
|
36 const TDesC8& aPrepend ) |
|
37 { |
|
38 RFile source; |
|
39 User::LeaveIfError( source.Open( |
|
40 aRfs, aSource, EFileRead | EFileShareAny ) ); |
|
41 CleanupClosePushL( source ); |
|
42 |
|
43 PrependBufferAndCopyFileInternalL( |
|
44 aRfs, aUtils, source, aTarget, aPrepend ); |
|
45 |
|
46 CleanupStack::PopAndDestroy( &source ); |
|
47 } |
|
48 |
|
49 /** |
|
50 * |
|
51 */ |
|
52 /*public static*/ |
|
53 void MessageStoreUtils::PrependBufferAndMoveFileL( |
|
54 RFs& aRfs, |
|
55 CContainerStoreUtils& aUtils, |
|
56 const TDesC& aSource, |
|
57 const TDesC& aTarget, |
|
58 const TDesC8& aPrepend ) |
|
59 { |
|
60 //open the source file. |
|
61 RFile source; |
|
62 User::LeaveIfError( source.Open( |
|
63 aRfs, aSource, EFileRead | EFileShareAny ) ); |
|
64 CleanupClosePushL( source ); |
|
65 |
|
66 PrependBufferAndCopyFileInternalL( |
|
67 aRfs, aUtils, source, aTarget, aPrepend ); |
|
68 |
|
69 CleanupStack::PopAndDestroy( &source ); |
|
70 |
|
71 User::LeaveIfError( aRfs.Delete( aSource ) ); |
|
72 User::LeaveIfError( aRfs.Rename( aTarget, aSource ) ); |
|
73 } |
|
74 |
|
75 /** |
|
76 * |
|
77 */ |
|
78 /*public static*/ |
|
79 void MessageStoreUtils::ReplaceFileWithFileL( |
|
80 RFs& aRfs, |
|
81 CContainerStoreUtils& aUtils, |
|
82 RFile& aSource, |
|
83 const TDesC& aTarget ) |
|
84 { |
|
85 PrependBufferAndCopyFileInternalL( |
|
86 aRfs, aUtils, aSource, aTarget, KNullDesC8 ); |
|
87 } |
|
88 |
|
89 /** |
|
90 * |
|
91 */ |
|
92 /*private static*/ |
|
93 void MessageStoreUtils::PrependBufferAndCopyFileInternalL( |
|
94 RFs& aRfs, |
|
95 CContainerStoreUtils& aUtils, |
|
96 RFile& aSource, |
|
97 const TDesC& aTarget, |
|
98 const TDesC8& aPrepend ) |
|
99 { |
|
100 //disk space check. |
|
101 { |
|
102 TInt fileSize; |
|
103 User::LeaveIfError( aSource.Size( fileSize ) ); |
|
104 aUtils.LeaveIfLowDiskSpaceL( fileSize + aPrepend.Length() ); |
|
105 } |
|
106 |
|
107 //prepare the target file. |
|
108 RFile target; |
|
109 User::LeaveIfError( target.Replace( |
|
110 aRfs, aTarget, EFileRead | EFileWrite ) ); |
|
111 CleanupClosePushL( target ); |
|
112 |
|
113 if ( aPrepend.Size() ) |
|
114 { |
|
115 User::LeaveIfError( target.Write( aPrepend ) ); |
|
116 } |
|
117 |
|
118 //source-to-target file copying loop. |
|
119 { |
|
120 HBufC8* buf = HBufC8::NewLC( KInternalCopyBufferSize ); |
|
121 TPtr8 des = buf->Des(); |
|
122 |
|
123 TInt pos = 0; |
|
124 aSource.Seek( ESeekStart, pos ); |
|
125 |
|
126 User::LeaveIfError( aSource.Read( des ) ); |
|
127 while ( des.Size() ) |
|
128 { |
|
129 User::LeaveIfError( target.Write( des ) ); |
|
130 User::LeaveIfError( aSource.Read( des ) ); |
|
131 } |
|
132 |
|
133 CleanupStack::PopAndDestroy( buf ); |
|
134 } |
|
135 |
|
136 CleanupStack::PopAndDestroy( &target ); |
|
137 } |