|
1 // Copyright (c) 2005-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 // |
|
15 |
|
16 |
|
17 #include <f32file.h> |
|
18 #if defined( __WINS__ ) || defined( __WINSCW__ ) |
|
19 #include <e32wins.h> |
|
20 #include <emulator.h> |
|
21 #include <stdlib.h> |
|
22 #else |
|
23 #include <f32image.h> |
|
24 #define MAX_PATH 512 |
|
25 #endif |
|
26 |
|
27 |
|
28 |
|
29 TBool ParseRenameIfEmulator(TInt& err, TParse& pathFrom , TParse& pathTo) |
|
30 { |
|
31 err=KErrNone; |
|
32 |
|
33 TBuf<MAX_PATH> c; |
|
34 User::CommandLine(c); |
|
35 |
|
36 TLex l(c); |
|
37 |
|
38 |
|
39 _LIT( LTokenRenameIfEmulator, "RenameIfEmulator" ); |
|
40 TPtrC commandTokenRenameIfEmulator(l.NextToken()); |
|
41 |
|
42 if (commandTokenRenameIfEmulator!=LTokenRenameIfEmulator ) |
|
43 { // Must be something else. |
|
44 err = KErrNone; |
|
45 return EFalse; |
|
46 } |
|
47 |
|
48 // Get source path |
|
49 if(pathFrom.SetNoWild(l.NextToken(),0,0)!=KErrNone) |
|
50 { // Not considered a path. |
|
51 err = KErrArgument; |
|
52 return ETrue; |
|
53 } |
|
54 |
|
55 // Get target path |
|
56 if(pathTo.SetNoWild(l.NextToken(),0,0)!=KErrNone) |
|
57 { // Not considered a path. |
|
58 err = KErrArgument; |
|
59 return ETrue; |
|
60 } |
|
61 |
|
62 // Managed to parse to parse the command line. |
|
63 // And get in all the parameters correctly. |
|
64 err = KErrNone; |
|
65 |
|
66 return ETrue; |
|
67 } |
|
68 |
|
69 |
|
70 |
|
71 TBool HasValidPath(TParse& pathTo) |
|
72 { |
|
73 |
|
74 TBuf<MAX_PATH> dName1; |
|
75 TBuf<MAX_PATH> dName2; |
|
76 dName1 = pathTo.FullName(); |
|
77 dName2 = pathTo.NameAndExt(); |
|
78 |
|
79 if (dName1 == dName2) |
|
80 { // No directory to create. Its a server or exe at release\winscw\udeb |
|
81 return EFalse; |
|
82 } |
|
83 |
|
84 return ETrue; |
|
85 } |
|
86 |
|
87 |
|
88 #if defined( __WINS__ ) || defined( __WINSCW__ ) |
|
89 |
|
90 TBool CreateDirectory(RFs& Fs, TParse& pathTo) |
|
91 { |
|
92 TInt err = KErrNone; |
|
93 TBool ret=EFalse; |
|
94 |
|
95 |
|
96 TBuf<MAX_PATH> dName1; |
|
97 err = MapEmulatedFileName(dName1, pathTo.DriveAndPath()); |
|
98 if(err!=KErrNone) |
|
99 return EFalse; |
|
100 |
|
101 TBuf8<MAX_PATH> dName1Char; |
|
102 dName1Char.Copy(dName1); |
|
103 |
|
104 ret = Emulator::CreateAllDirectories( (LPCSTR) dName1Char.PtrZ() ); |
|
105 if( !ret ) |
|
106 { // Problem with directory creation. |
|
107 err=KErrGeneral; |
|
108 Fs.Close(); |
|
109 return EFalse; |
|
110 } |
|
111 |
|
112 return ETrue; |
|
113 } |
|
114 |
|
115 #endif |
|
116 |
|
117 // ETrue=> carried out the action succesfully. |
|
118 // EFalse=> some sort of error. |
|
119 |
|
120 TBool DoRenameIfEmulator(TInt& err , TParse& pathFrom , TParse& pathTo) |
|
121 { |
|
122 err=KErrNone; |
|
123 |
|
124 #if defined( __WINS__ ) || defined( __WINSCW__ ) |
|
125 |
|
126 RFs Fs; |
|
127 |
|
128 err = Fs.Connect(); |
|
129 if(err!=KErrNone) |
|
130 { |
|
131 err=KErrGeneral; |
|
132 return EFalse; |
|
133 } |
|
134 |
|
135 |
|
136 if ( HasValidPath(pathTo) ) |
|
137 { |
|
138 if ( !CreateDirectory( Fs , pathTo) ) |
|
139 { // Unable to create the directory. |
|
140 err=KErrGeneral; |
|
141 Fs.Close(); |
|
142 return EFalse; |
|
143 } |
|
144 } |
|
145 // else probably just release\winscw\udeb which is where exe's go ( for the moment ) in the emulator. |
|
146 |
|
147 |
|
148 TBuf<MAX_PATH> sName; |
|
149 TBuf<MAX_PATH> sName1 = pathFrom.FullName(); |
|
150 err = MapEmulatedFileName(sName, sName1); |
|
151 if(err!=KErrNone) |
|
152 { |
|
153 Fs.Close(); |
|
154 return EFalse; |
|
155 } |
|
156 |
|
157 TBuf<MAX_PATH> dName; |
|
158 TBuf<MAX_PATH> dName1=pathTo.FullName(); |
|
159 err = MapEmulatedFileName(dName, dName1); |
|
160 if(err!=KErrNone) |
|
161 { |
|
162 Fs.Close(); |
|
163 return EFalse; |
|
164 } |
|
165 |
|
166 |
|
167 Emulator::DeleteFile((LPCTSTR)dName.PtrZ()); |
|
168 // Always delete before we move. |
|
169 |
|
170 if(!Emulator::MoveFile((LPCTSTR)sName.PtrZ(),(LPCTSTR)dName.PtrZ())) |
|
171 { |
|
172 |
|
173 WIN32_FIND_DATA ff; |
|
174 if ( FindFirstFile((LPCTSTR)sName.PtrZ() , &ff) == INVALID_HANDLE_VALUE ) |
|
175 { // The file does not exists just fail the move silently. |
|
176 err=KErrNone; |
|
177 Fs.Close(); |
|
178 return EFalse; |
|
179 } |
|
180 else |
|
181 { // The file still exists probably locked by CW. |
|
182 // Only thing can do is to issue an error. |
|
183 err=KErrGeneral; |
|
184 Fs.Close(); |
|
185 return EFalse; |
|
186 } |
|
187 } |
|
188 |
|
189 |
|
190 Fs.Close(); |
|
191 |
|
192 #endif |
|
193 // #else not emulator so do nothing. |
|
194 |
|
195 |
|
196 // Managed to rename the file succesfully. |
|
197 // or did nothing at all if not emulator. |
|
198 err=KErrNone; |
|
199 return ETrue; |
|
200 } |
|
201 |
|
202 |
|
203 //************************************** |
|
204 |
|
205 |
|
206 // ret == ETrue => That was the command and it was succesful. |
|
207 // or the file did not exist and we failed silently. |
|
208 // either way we do not want to continue parsing. |
|
209 // ret == EFalse => that the |
|
210 |
|
211 TBool RenameIfEmulator(TInt& err) |
|
212 { |
|
213 TBool ret=EFalse; |
|
214 err=KErrNone; |
|
215 |
|
216 TParse pathFrom; |
|
217 TParse pathTo; |
|
218 |
|
219 |
|
220 ret = ParseRenameIfEmulator(err , pathFrom, pathTo); |
|
221 if ( ret ) |
|
222 { |
|
223 if (err==KErrNone) |
|
224 { // Okay have parsed good arguments. |
|
225 DoRenameIfEmulator(err , pathFrom, pathTo); |
|
226 } |
|
227 // else some sort of error. |
|
228 } |
|
229 // else not recognised the command. |
|
230 |
|
231 return ret; |
|
232 } |
|
233 |
|
234 //************************************** |
|
235 //************************************** |
|
236 |
|
237 TBool ParseCopyIfEmulator(TInt& err, TParse& pathFrom , TParse& pathTo) |
|
238 { |
|
239 err=KErrNone; |
|
240 |
|
241 TBuf<MAX_PATH> c; |
|
242 User::CommandLine(c); |
|
243 |
|
244 TLex l(c); |
|
245 |
|
246 |
|
247 _LIT( LTokenRenameIfEmulator, "CopyIfEmulator" ); |
|
248 TPtrC commandTokenRenameIfEmulator(l.NextToken()); |
|
249 |
|
250 if (commandTokenRenameIfEmulator!=LTokenRenameIfEmulator ) |
|
251 { // Must be something else. |
|
252 err = KErrNone; |
|
253 return EFalse; |
|
254 } |
|
255 |
|
256 // Get source path |
|
257 if(pathFrom.SetNoWild(l.NextToken(),0,0)!=KErrNone) |
|
258 { // Not considered a path. |
|
259 err = KErrArgument; |
|
260 return ETrue; |
|
261 } |
|
262 |
|
263 // Get target path |
|
264 if(pathTo.SetNoWild(l.NextToken(),0,0)!=KErrNone) |
|
265 { // Not considered a path. |
|
266 err = KErrArgument; |
|
267 return ETrue; |
|
268 } |
|
269 |
|
270 // Managed to parse to parse the command line. |
|
271 // And get in all the parameters correctly. |
|
272 err = KErrNone; |
|
273 |
|
274 return ETrue; |
|
275 } |
|
276 |
|
277 |
|
278 TBool DoCopyIfEmulator(TInt& err , TParse& pathFrom , TParse& pathTo) |
|
279 { |
|
280 err=KErrNone; |
|
281 |
|
282 #if defined( __WINS__ ) || defined( __WINSCW__ ) |
|
283 |
|
284 RFs Fs; |
|
285 |
|
286 err = Fs.Connect(); |
|
287 if(err!=KErrNone) |
|
288 return EFalse; |
|
289 |
|
290 if ( HasValidPath(pathTo) ) |
|
291 { |
|
292 if ( !CreateDirectory( Fs , pathTo) ) |
|
293 { // Unable to create the directory. |
|
294 err=KErrGeneral; |
|
295 Fs.Close(); |
|
296 return EFalse; |
|
297 } |
|
298 } |
|
299 // else probably just release\winscw\udeb which is where exe's go ( for the moment ) in the emulator. |
|
300 |
|
301 |
|
302 TBuf<MAX_PATH> sName; |
|
303 err = MapEmulatedFileName(sName, pathFrom.FullName()); |
|
304 if(err!=KErrNone) |
|
305 { |
|
306 Fs.Close(); |
|
307 return EFalse; |
|
308 } |
|
309 |
|
310 TBuf<MAX_PATH> dName; |
|
311 err = MapEmulatedFileName(dName, pathTo.FullName()); |
|
312 if(err!=KErrNone) |
|
313 { |
|
314 Fs.Close(); |
|
315 return EFalse; |
|
316 } |
|
317 |
|
318 if(!Emulator::CopyFile((LPCTSTR)sName.PtrZ(),(LPCTSTR)dName.PtrZ(),FALSE)) |
|
319 { |
|
320 err=KErrGeneral; |
|
321 Fs.Close(); |
|
322 return EFalse; |
|
323 } |
|
324 |
|
325 Fs.Close(); |
|
326 |
|
327 #endif |
|
328 // #else not emulator so do nothing. |
|
329 |
|
330 |
|
331 // Managed to rename the file succesfully. |
|
332 // or did nothing at all if not emulator. |
|
333 err=KErrNone; |
|
334 |
|
335 return ETrue; |
|
336 } |
|
337 |
|
338 |
|
339 |
|
340 //************************************** |
|
341 |
|
342 TBool CopyIfEmulator(TInt& err) |
|
343 { |
|
344 TBool ret=EFalse; |
|
345 err=KErrNone; |
|
346 |
|
347 TParse pathFrom; |
|
348 TParse pathTo; |
|
349 |
|
350 |
|
351 ret = ParseCopyIfEmulator(err , pathFrom, pathTo); |
|
352 if ( ret ) |
|
353 { |
|
354 if (err==KErrNone) |
|
355 { // Okay have parsed good arguments. |
|
356 DoCopyIfEmulator(err , pathFrom, pathTo); |
|
357 } |
|
358 // else some sort of error. |
|
359 } |
|
360 // else not recognised the command. |
|
361 |
|
362 return ret; |
|
363 } |
|
364 |
|
365 //************************************** |
|
366 |
|
367 TBool ParseRenameIfExists(TInt& err, TParse& pathFrom , TParse& pathTo) |
|
368 { |
|
369 err=KErrNone; |
|
370 |
|
371 TBuf<MAX_PATH> c; |
|
372 User::CommandLine(c); |
|
373 |
|
374 TLex l(c); |
|
375 |
|
376 _LIT( LTokenRenameIfEmulator, "RenameIfExists" ); |
|
377 TPtrC commandTokenRenameIfEmulator(l.NextToken()); |
|
378 |
|
379 if (commandTokenRenameIfEmulator!=LTokenRenameIfEmulator ) |
|
380 { // Must be something else. |
|
381 err = KErrNone; |
|
382 return EFalse; |
|
383 } |
|
384 |
|
385 // Get source path |
|
386 if(pathFrom.SetNoWild(l.NextToken(),0,0)!=KErrNone) |
|
387 { // Not considered a path. |
|
388 err = KErrArgument; |
|
389 return ETrue; |
|
390 } |
|
391 |
|
392 // Get target path |
|
393 if(pathTo.SetNoWild(l.NextToken(),0,0)!=KErrNone) |
|
394 { // Not considered a path. |
|
395 err = KErrArgument; |
|
396 return ETrue; |
|
397 } |
|
398 |
|
399 // Managed to parse to parse the command line. |
|
400 // And get in all the parameters correctly. |
|
401 err = KErrNone; |
|
402 |
|
403 return ETrue; |
|
404 } |
|
405 |
|
406 |
|
407 |
|
408 TBool DoRenameIfExists(TInt& err , TParse& pathFrom , TParse& pathTo) |
|
409 { |
|
410 TBool ret=EFalse; |
|
411 err=KErrNone; |
|
412 |
|
413 RFs Fs; |
|
414 |
|
415 err = Fs.Connect(); |
|
416 if(err!=KErrNone) |
|
417 return EFalse; |
|
418 |
|
419 err = Fs.MkDirAll(pathTo.FullName()); |
|
420 |
|
421 if ( (err==KErrNone ) || (err==KErrAlreadyExists) ) |
|
422 { |
|
423 RFile Source; |
|
424 |
|
425 err=Source.Open(Fs,pathFrom.FullName(),EFileRead); |
|
426 if(err!=KErrNone) |
|
427 { |
|
428 // Source does not exist no copy no error. |
|
429 err=KErrNone; |
|
430 Fs.Close(); |
|
431 return EFalse; |
|
432 } |
|
433 Source.Close(); |
|
434 // Okay source exists. |
|
435 |
|
436 |
|
437 ret = Fs.Replace(pathFrom.FullName() , pathTo.FullName()); |
|
438 if(ret!=KErrNone) |
|
439 { |
|
440 Fs.Close(); |
|
441 err=KErrNone; |
|
442 return EFalse; |
|
443 } |
|
444 } |
|
445 |
|
446 Fs.Close(); |
|
447 |
|
448 |
|
449 // Managed to rename the file succesfully. |
|
450 // or did nothing at all if not emulator. |
|
451 ret=ETrue; |
|
452 |
|
453 return ret; |
|
454 } |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 //************************************** |
|
460 //************************************** |
|
461 |
|
462 TBool RenameIfExists(TInt& err) |
|
463 { |
|
464 TBool ret=EFalse; |
|
465 err=KErrNone; |
|
466 |
|
467 TParse pathFrom; |
|
468 TParse pathTo; |
|
469 |
|
470 |
|
471 ret = ParseRenameIfExists(err , pathFrom, pathTo); |
|
472 if ( ret ) |
|
473 { |
|
474 if (err==KErrNone) |
|
475 { // Okay have parsed good arguments. |
|
476 DoRenameIfExists(err , pathFrom, pathTo); |
|
477 } |
|
478 // else some sort of error. |
|
479 } |
|
480 // else not recognised the command. |
|
481 |
|
482 |
|
483 return ret; |
|
484 } |
|
485 //************************************** |
|
486 |
|
487 |
|
488 |
|
489 // RenameIfEmulator z:\a.x c:\a.x |
|
490 // CopyIfEmulator z:\a.x c:\a.x |
|
491 // RenameIfExists z:\a.x c:\a.x |
|
492 |
|
493 TInt E32Main() |
|
494 { |
|
495 TInt err=KErrNone; |
|
496 TBool done=EFalse; |
|
497 |
|
498 |
|
499 done=RenameIfEmulator( err); |
|
500 if ( done ) |
|
501 { // Assumed command was RenameIfEmulator, which may or may not have run correctly (check err). |
|
502 } |
|
503 else |
|
504 { |
|
505 done=CopyIfEmulator( err); |
|
506 if ( done ) |
|
507 { // Assumed command was CopyIfEmulator, which may or may not have run correctly (check err). |
|
508 } |
|
509 else |
|
510 { |
|
511 done=RenameIfExists( err); |
|
512 if ( done ) |
|
513 { // Assumed command was RenameIfExists, which may or may not have run correctly (check err). |
|
514 } |
|
515 else |
|
516 { |
|
517 err=KErrNotFound; |
|
518 } |
|
519 } |
|
520 } |
|
521 |
|
522 return err; |
|
523 } |