1 /* |
|
2 * Copyright (c) 2004-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 "cfilemanager.h" |
|
20 #include "rsutils.h" |
|
21 #include "clog.h" |
|
22 |
|
23 // CONSTANTS |
|
24 namespace |
|
25 { |
|
26 const TInt KBufferSize = 1000; |
|
27 } |
|
28 |
|
29 #define USE_CFILEMAN_FIRST |
|
30 |
|
31 CFileManager* CFileManager::NewL( RFs& aFs ) |
|
32 { |
|
33 CFileManager* self = new ( ELeave ) CFileManager( aFs ); |
|
34 CleanupStack::PushL( self ); |
|
35 self->ConstructL(); |
|
36 CleanupStack::Pop(); // self |
|
37 return self; |
|
38 } |
|
39 |
|
40 CFileManager::CFileManager( RFs& aFs ) : CActive( CActive::EPriorityHigh ), |
|
41 iFs( aFs ), |
|
42 iBuffer( NULL ) |
|
43 |
|
44 { |
|
45 CActiveScheduler::Add( this ); |
|
46 } |
|
47 |
|
48 CFileManager::~CFileManager() |
|
49 { |
|
50 Cancel(); |
|
51 CloseCopy(); |
|
52 if(iBuffer) |
|
53 { |
|
54 delete iBuffer; |
|
55 iBuffer = NULL; |
|
56 } |
|
57 delete iFileMan; |
|
58 } |
|
59 |
|
60 void CFileManager::ConstructL() |
|
61 { |
|
62 iFileMan = CFileMan::NewL( iFs ); |
|
63 } |
|
64 |
|
65 |
|
66 TInt CFileManager::Copy( const TDesC& aFile, const TDesC& aDir, TRequestStatus& aStatus ) |
|
67 { |
|
68 LOG( "CFileManager::Copy begin" ); |
|
69 Cancel(); |
|
70 CloseCopy(); |
|
71 iCallerStatus = &aStatus; |
|
72 TInt err( KErrNone ); |
|
73 #ifdef USE_CFILEMAN_FIRST |
|
74 // check if file is used by another application |
|
75 err = iSource.Open( iFs, aFile, EFileRead | EFileShareReadersOnly ); |
|
76 LOG1("CFileManager::Copy check err: %d", err); |
|
77 iSource.Close(); |
|
78 if( !err ) |
|
79 { |
|
80 err = iFileMan->Copy( aFile, aDir, CFileMan::EOverWrite, iStatus ); |
|
81 LOG1("CFileManager::Copy iFileMan->Copy err: %d", err); |
|
82 if( !err ) |
|
83 { |
|
84 iOperation = EFileManCopy; |
|
85 SetActive(); |
|
86 } |
|
87 } |
|
88 else |
|
89 #endif |
|
90 { |
|
91 iOperation = EBufferCopy; |
|
92 TRAP( err, InitCopyL( aFile, aDir ) ); |
|
93 LOG1( "CFileManager::Copy InitCopyL err: %d", err ); |
|
94 if( err ) |
|
95 { |
|
96 CloseCopy(); |
|
97 } |
|
98 else |
|
99 { |
|
100 TPtr8 ptr = iBuffer->Des(); |
|
101 LOG1( "CFileManager::Copy iBytesNum: %d", iBytesNum ); |
|
102 iSource.Read( ptr, iBytesNum, iStatus ); |
|
103 LOG( "CFileManager::Copy call to SetActive()" ); |
|
104 SetActive(); |
|
105 } |
|
106 } |
|
107 LOG1( "CFileManager::Copy end with: %d", err ); |
|
108 return err; |
|
109 } |
|
110 |
|
111 |
|
112 void CFileManager::DoCancel() |
|
113 { |
|
114 LOG( "CFileManager::DoCancel begin" ); |
|
115 CloseCopy(); |
|
116 LOG( "CFileManager::DoCancel end" ); |
|
117 } |
|
118 |
|
119 void CFileManager::RunL() |
|
120 { |
|
121 LOG1( "CFileManager::RunL iStatus: %d", iStatus.Int() ); |
|
122 LOG1( "CFileManager::RunL iOperation: %d", iOperation ); |
|
123 |
|
124 if( iStatus == KErrNone ) |
|
125 { |
|
126 switch( iOperation ) |
|
127 { |
|
128 case ERemoveDir: |
|
129 { |
|
130 LOG( "CFileManager::RunL directory removed" ); |
|
131 User::RequestComplete( iCallerStatus, iStatus.Int() ); |
|
132 } |
|
133 break; |
|
134 case EFileManCopy: |
|
135 { |
|
136 LOG( "CFileManager::RunL file copied" ); |
|
137 User::RequestComplete( iCallerStatus, iStatus.Int() ); |
|
138 } |
|
139 break; |
|
140 case EBufferCopy: |
|
141 { |
|
142 TPtr8 ptr = iBuffer->Des(); |
|
143 LOG( "CFileManager::RunL call to RFile::Write()" ); |
|
144 User::LeaveIfError( iDest.Write( ptr, iBytesNum ) ); |
|
145 iBytesLeft -= iBytesNum; |
|
146 if( iBytesLeft ) |
|
147 { |
|
148 iBytesNum = ( KBufferSize < iBytesLeft ) ? KBufferSize : iBytesLeft; |
|
149 LOG1( "CFileManager::RunL iBytesLeft: %d", iBytesLeft ); |
|
150 iSource.Read( ptr, iBytesNum, iStatus ); |
|
151 SetActive(); |
|
152 } |
|
153 else |
|
154 { |
|
155 User::LeaveIfError( iDest.Flush() ); |
|
156 CloseCopy(); |
|
157 LOG( "CFileManager::RunL file copied" ); |
|
158 User::RequestComplete( iCallerStatus, iStatus.Int() ); |
|
159 } |
|
160 } |
|
161 break; |
|
162 default: |
|
163 break; |
|
164 } |
|
165 } |
|
166 else |
|
167 { |
|
168 CloseCopy(); |
|
169 User::RequestComplete( iCallerStatus, iStatus.Int() ); |
|
170 } |
|
171 } |
|
172 |
|
173 TInt CFileManager::RunError( TInt aError ) |
|
174 { |
|
175 LOG1( "CFileManager::RunError aError: %d", aError ); |
|
176 return KErrNone; |
|
177 } |
|
178 |
|
179 void CFileManager::CloseCopy() |
|
180 { |
|
181 LOG( "CFileManager::CloseCopy begin" ); |
|
182 iSource.Close(); |
|
183 iDest.Close(); |
|
184 delete iBuffer; |
|
185 iBuffer = NULL; |
|
186 iBytesLeft = 0; |
|
187 iBytesNum = 0; |
|
188 LOG( "CFileManager::CloseCopy end" ); |
|
189 } |
|
190 |
|
191 void CFileManager::InitCopyL( const TDesC& aFile, const TDesC& aDir ) |
|
192 { |
|
193 LOG( "CFileManager::InitCopyL begin" ); |
|
194 TInt err = iSource.Open( iFs, aFile, EFileRead | EFileShareReadersOnly ); |
|
195 LOG1("CFileManager::InitCopyL file.Open (shareread mode): %d", err); |
|
196 if ( err != KErrNone ) |
|
197 { |
|
198 err = iSource.Open( iFs, aFile, EFileRead | EFileShareAny ); |
|
199 LOG1("CFileManager::InitCopyL file.Open (shareany mode): %d", err); |
|
200 } |
|
201 User::LeaveIfError( err ); |
|
202 |
|
203 TParsePtrC parse( aFile ); |
|
204 TFileName writeFileName( aDir ); |
|
205 writeFileName.Append( parse.NameAndExt() ); |
|
206 User::LeaveIfError( iDest.Replace( iFs, writeFileName, EFileWrite ) ); |
|
207 iBuffer = HBufC8::NewL( KBufferSize ); |
|
208 User::LeaveIfError( iSource.Size( iBytesLeft ) ); |
|
209 LOG1( "CFileManager::InitCopyL file size: %d", iBytesLeft ); |
|
210 iBytesNum = ( KBufferSize < iBytesLeft ) ? KBufferSize : iBytesLeft; |
|
211 LOG( "CFileManager::InitCopyL end" ); |
|
212 } |
|
213 |
|
214 void CFileManager::CancelCopy() |
|
215 { |
|
216 LOG("CFileManager::CancelCopy begin"); |
|
217 if( IsActive() ) |
|
218 { |
|
219 Cancel(); |
|
220 User::RequestComplete( iCallerStatus, KErrCancel ); |
|
221 } |
|
222 LOG("CFileManager::CancelCopy end"); |
|
223 } |
|
224 |
|
225 void CFileManager::CancelRmDir() |
|
226 { |
|
227 LOG("CFileManager::CancelRmDir begin"); |
|
228 if( IsActive() ) |
|
229 { |
|
230 Cancel(); |
|
231 User::RequestComplete( iCallerStatus, KErrCancel ); |
|
232 } |
|
233 LOG("CFileManager::CancelRmDir end"); |
|
234 } |
|
235 |
|
236 TInt CFileManager::RmDir( const TDesC& aDir, TRequestStatus& aStatus ) |
|
237 { |
|
238 LOG1("CFileManager::RmDir aDir: %S", &aDir); |
|
239 TInt err( KErrNone ); |
|
240 Cancel(); |
|
241 iCallerStatus = &aStatus; |
|
242 if( RsUtils::PathExists( aDir, iFs ) ) |
|
243 { |
|
244 err = iFileMan->RmDir( aDir, iStatus ); |
|
245 iOperation = ERemoveDir; |
|
246 if( !err ) SetActive(); |
|
247 } |
|
248 else |
|
249 { |
|
250 User::RequestComplete( iCallerStatus, KErrNone ); |
|
251 } |
|
252 LOG1("CFileManager::RmDir return: %d", err); |
|
253 return err; |
|
254 } |
|
255 |
|
256 // End of File |
|