-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScanner.cpp
1687 lines (1327 loc) · 41.8 KB
/
Scanner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <StdFuncs.h>
#include <Args.h>
#include <Dir.h>
#include <File.h>
#include <FileUtils.h>
#include <StdTextFile.h>
#include <StdWildcard.h>
#include <stdio.h>
#include <string>
#include <string.h>
#include "Scanner.h"
using namespace std;
extern volatile bool g_bBreak; /* Set to true if when ctrl-c is hit by the user */
extern RArgs g_oArgs; /* Contains the parsed command line arguments */
/* # of bytes to read and write when copying files */
#define BUFFER_SIZE (1024 * 1024)
/* Written: Wednesday 21-Jul-2010 8:38 am */
int RScanner::open()
{
bool Inclusion;
char *Line, *LineCopy;
const char *FilterListName, *NewLine;
int RetVal;
/* Initialise the CRC class's lookup tables */
m_oCRC.Init();
/* If the name of a file to use as the filter list has been passed in then open and parse it */
if ((FilterListName = g_oArgs[ARGS_FILTERLIST]) != NULL)
{
RTextFile TextFile;
/* Read the filter list into memory */
if ((RetVal = TextFile.open(FilterListName)) == KErrNone)
{
/* Scan through and extract the lines from the filter list and build the filter lists */
while ((NewLine = TextFile.GetLine()) != NULL)
{
/* Make a copy of the line returned as the one returned is read only */
if ((Line = LineCopy = Utils::DuplicateString(NewLine, -1)) != NULL)
{
/* Remove white space from the start and end of the string */
Utils::TrimString(Line);
/* Normalise the path so it only contains the '/' directory separator */
Utils::NormalisePath(Line);
/* Check for a filter */
if ((*Line == '-') || (*Line == '+'))
{
Inclusion = (*Line == '+');
/* Skip the '-' and remove any further white space after it */
++Line;
Utils::TrimString(Line);
/* Append the filter to the filter list */
if ((RetVal = AddFilter(Line, Inclusion)) != KErrNone)
{
break;
}
}
/* Check to see if this is a comment and if not, display an error */
else if (*Line != '#')
{
printf("Warning: Unknown filter list entry: %s\n", Line);
}
/* And free the copy of the line */
delete [] LineCopy;
}
else
{
RetVal = KErrNoMemory;
Utils::Error("Unable to duplicate filter list entry");
break;
}
}
TextFile.close();
}
else
{
Utils::Error("Unable to open filter list \"%s\"", FilterListName);
}
}
/* Otherwise don't do anything and just return success */
else
{
RetVal = KErrNone;
}
return(RetVal);
}
/* Written: Wednesday 21-Jul-2010 8:40 am */
void RScanner::close()
{
TFilter *Filter;
/* Iterate through the items in the path filter list and delete them */
while ((Filter = m_oPaths.remHead()) != NULL)
{
delete Filter;
}
/* Iterate through the items in the directory filter list and delete them */
while ((Filter = m_oDirectories.remHead()) != NULL)
{
delete Filter;
}
/* Iterate through the items in the file filter wildcard list and delete them */
while ((Filter = m_oFiles.remHead()) != NULL)
{
delete Filter;
}
/* And reset the class to is initial state for reuse */
m_bBreakPrinted = false;
m_poLastFilter = NULL;
}
/* Written: Wednesday 04-Aug-2010 10:18 am */
int RScanner::AddFilter(char *a_pcLine, bool a_bInclusion)
{
char *Path;
const char *FileName;
int Index, NumSlashes, RetVal;
size_t Length;
TFilter *Filter;
/* Assume failure */
RetVal = KErrNoMemory;
/* Allocate a buffer large enough to hold the filter string and a list node */
/* to hold it */
Length = strlen(a_pcLine);
NumSlashes = 0;
if ((Path = new char[Length + 1]) != NULL)
{
if ((Filter = new TFilter(Path)) != NULL)
{
RetVal = KErrNone;
/* See if the filter is a filename wildcard and if so add it to the file filter wildcard list */
FileName = Utils::filePart(a_pcLine);
if (*FileName != '\0')
{
strcpy(Path, FileName);
if (a_bInclusion)
{
/* File inclusion filters can only can only get added as embedded filters */
if (m_poLastFilter)
{
m_poLastFilter->m_oFilters.addTail(Filter);
if (g_oArgs[ARGS_VERBOSE]) printf(" Added inclusion filter \"%s\" to directory filter \"%s\"\n", Path, m_poLastFilter->m_pccName);
}
else
{
printf("Warning: Ignoring file filter \"%s\"\n", Path);
delete Filter;
}
}
else
{
m_oFiles.addTail(Filter);
if (g_oArgs[ARGS_VERBOSE]) printf("Added file filter \"%s\"\n", Path);
}
}
/* Otherwise add it to the directory list */
else
{
if (!(a_bInclusion))
{
for (Index = 0; Index < (int) Length; ++Index)
{
if (a_pcLine[Index] == '/')
{
++NumSlashes;
}
}
/* First, remove the trailing '/' that is appended to the directory name to be filtered */
a_pcLine[Length - 1] = '\0';
/* And add it to the directory or path list as appropriate */
strcpy(Path, a_pcLine);
if (NumSlashes >= 2)
{
m_oPaths.addTail(Filter);
}
else
{
m_oDirectories.addTail(Filter);
}
/* And save the ptr to the directory filter so that embedded filters can be added to it l8r */
m_poLastFilter = Filter;
if (g_oArgs[ARGS_VERBOSE]) printf("Added directory filter \"%s\"\n", a_pcLine);
}
else
{
printf(" Warning: Ignoring inclusion filter \"%s\"\n", a_pcLine);
delete Filter;
}
}
}
else
{
delete [] Path;
Utils::Error("Out of memory");
}
}
else
{
Utils::Error("Out of memory");
}
return(RetVal);
}
/**
* Calculates the CRC of two files and determines whether they match.
* This function will parse two files and will calculate the CRC of both, to determine whether
* they are a binary match. It returns whether the files match and also the calculated CRC values.
* If any error occurs (apart from the CRC values not matching), the returned CRC values will set to 0.
*
* @date Saturday 17-May-2014 11:16 am, Code HQ Ehinger Tor
* @param a_pccSource Ptr to the name of the source file to be checked
* @param a_pccDest Ptr to the name of the destination file to be checked
* @param a_puiSourceCRC Ptr to a variable into which to return the CRC of the source file
* @param a_puiDestCRC Ptr to a variable into which to return the CRC of the destination file
* @return KErrNone if the CRCs of the two files match
* @return KErrCorrupt if the CRCs of the two files do not match
* @return KErrNotFound if either the source or the destination files could not be opened
* @return Otherwise any of the errors returned by RFile::open() or RFile::read()
*/
TInt RScanner::CheckCRC(const char *a_pccSource, const char *a_pccDest, TUint *a_puiSourceCRC, TUint *a_puiDestCRC)
{
unsigned char *Source, *Dest;
TInt RetVal, SourceSize, DestSize;
TUint SourceCRC, DestCRC;
RFile SourceFile, DestFile;
SourceCRC = DestCRC = 0;
*a_puiSourceCRC = *a_puiDestCRC = 0;
/* Open the source and destination files which need to have their CRCs checked */
if ((RetVal = SourceFile.open(a_pccSource, EFileRead)) == KErrNone)
{
if ((RetVal = DestFile.open(a_pccDest, EFileRead)) == KErrNone)
{
/* Allocate two large buffers into which chunks of the source and destination files can be read */
Source = new unsigned char[BUFFER_SIZE];
Dest = new unsigned char[BUFFER_SIZE];
if ((Source) && (Dest))
{
/* Loop around and read the two files in, in chunks of BUFFER_SIZE, incrementally calculating their CRCs. */
/* Stop when the entire file has been read in and checked or if an error occurs */
do
{
SourceSize = SourceFile.read(Source, BUFFER_SIZE);
if (SourceSize > 0)
{
if ((DestSize = DestFile.read(Dest, SourceSize)) == SourceSize)
{
/* Incrementally calculate the two CRCs, based on the CRC of the previous chunk */
SourceCRC = m_oCRC.CRC32(SourceCRC, Source, SourceSize);
DestCRC = m_oCRC.CRC32(DestCRC, Dest, DestSize);
}
else
{
/* If the amount read from the destination file read was less than what was requested, then we */
/* have hit the end of the file so return this fact. Otherwise return whatever error was returned */
/* from RFile::read() */
RetVal = (DestSize >= 0) ? KErrGeneral : DestSize;
Utils::Error("Unable to read from file \"%s\" (Error %d)", a_pccDest, RetVal);
}
}
else if (SourceSize < 0)
{
RetVal = SourceSize;
Utils::Error("Unable to read from file \"%s\" (Error %d)", a_pccSource, RetVal);
}
}
while ((SourceSize > 0) && (RetVal == KErrNone));
/* If the CRCs were calculated successfully then return whether they match, and also return the CRCs themselves */
if (RetVal == KErrNone)
{
if (SourceCRC != DestCRC)
{
RetVal = KErrCorrupt;
}
*a_puiSourceCRC = SourceCRC;
*a_puiDestCRC = DestCRC;
}
}
else
{
RetVal = KErrNoMemory;
Utils::Error("Out of memory");
}
delete [] Dest;
delete [] Source;
DestFile.close();
}
else
{
Utils::Error("Unable to open dest file \"%s\" (Error %d)", a_pccDest, RetVal);
}
SourceFile.close();
}
else
{
Utils::Error("Unable to open source file \"%s\" (Error %d)", a_pccSource, RetVal);
}
return(RetVal);
}
/* Written: Saturday 23-Oct-2010 11:27 am */
bool RScanner::CheckFilterList(const char *a_pccFileName)
{
bool RetVal;
const char *FileName;
TFilter *Filter;
/* Assume the file is not on the filter list */
RetVal = false;
/* Get the name of the file part of the path */
FileName = Utils::filePart(a_pccFileName);
/* Iterate through the list of file filters and see if there is a match */
if ((Filter = m_oFiles.getHead()) != NULL)
{
do
{
/* Perform a wildcard match of the file filter on the filename */
RWildcard Wildcard;
if (Wildcard.open(Filter->m_pccName) == KErrNone)
{
/* If the file matches the file filter then we want to bail out and not copy */
/* the file */
if (Wildcard.Match(FileName))
{
RetVal = true;
break;
}
Wildcard.close();
}
}
while ((Filter = m_oFiles.getSucc(Filter)) != NULL);
}
return(RetVal);
}
/* Written: Friday 02-Jan-2009 8:38 pm */
int RScanner::CopyFile(const char *a_pccSource, const char *a_pccDest, const TEntry &a_roEntry)
{
int RetVal;
unsigned char *Buffer;
RFile SourceFile, DestFile;
/* Assume success */
RetVal = KErrNone;
printf("Copying file \"%s\" to \"%s\"\n", a_pccSource, a_pccDest);
if ((RetVal = SourceFile.open(a_pccSource, EFileRead)) == KErrNone)
{
/* RFile::Create() will fail if a destination file already exists so delete it before trying */
/* to create a new one. RScanner::deleteFile() will also remove any read only protection bit */
/* that is set before trying to delete the file */
if ((RetVal = deleteFile(a_pccDest)) == KErrNotFound)
{
RetVal = KErrNone;
}
if (RetVal == KErrNone)
{
if ((RetVal = DestFile.create(a_pccDest, EFileWrite)) == KErrNone)
{
if ((Buffer = new unsigned char[BUFFER_SIZE]) != NULL)
{
do
{
if ((RetVal = SourceFile.read(Buffer, BUFFER_SIZE)) > 0)
{
if ((RetVal = DestFile.write(Buffer, RetVal)) < 0)
{
Utils::Error("Unable to write to file \"%s\" (Error %d)", a_pccDest, RetVal);
}
}
else
{
if (RetVal < 0)
{
Utils::Error("Unable to read from file \"%s\" (Error %d)", a_pccSource, RetVal);
}
}
}
while (RetVal > 0);
delete [] Buffer;
}
else
{
RetVal = KErrNoMemory;
Utils::Error("Out of memory");
}
DestFile.close();
}
else
{
Utils::Error("Unable to open dest file \"%s\" (Error %d)", a_pccDest, RetVal);
}
}
else
{
Utils::Error("Unable to delete dest file \"%s\" (Error %d)", a_pccDest, RetVal);
}
SourceFile.close();
/* If successful, set the date and time and protection bits in the destination file to match those */
/* in the source file */
if (RetVal == KErrNone)
{
if ((RetVal = Utils::setFileDate(a_pccDest, a_roEntry, EFalse)) == KErrNone)
{
if ((RetVal = Utils::setProtection(a_pccDest, a_roEntry.iAttributes)) == KErrNone)
{
#ifdef WIN32
/* If requested, set the archive attribute on the source file to indicate that it has been */
/* archived. This attribute exists only on Windows, so to prevent unnecessarily setting the */
/* protection bits on onther systems we do this conditionally */
if (g_oArgs[ARGS_ARCHIVE])
{
TEntry Entry = a_roEntry;
/* Clear the archive attribute */
Entry.ClearArchive();
/* And write the new protection bits back to the source file */
if ((RetVal = Utils::setProtection(a_pccSource, Entry.iAttributes)) != KErrNone)
{
Utils::Error("Unable to set protection bits for file \"%s\" (Error %d)", a_pccSource, RetVal);
}
}
#endif /* WIN32 */
}
else
{
Utils::Error("Unable to set protection bits for file \"%s\" (Error %d)", a_pccDest, RetVal);
}
}
else
{
Utils::Error("Unable to set datestamp for file \"%s\" (Error %d)", a_pccDest, RetVal);
}
}
}
else
{
Utils::Error("Unable to open source file \"%s\" (Error %d)", a_pccSource, RetVal);
}
if (g_oArgs[ARGS_NOERRORS])
{
RetVal = KErrNone;
}
return(RetVal);
}
/**
* Copies a file or a link to a destination.
* Depending on whether the file system object given is a file or a link, this function will call
* the appropriate function to perform a copy of the object. This is just a convenience function
* as both the CopyFile() and CopyLink() functions are quite large and do not belong in a single
* function.
*
* @date Monday 30-Nov-2015 07:05 am, Code HQ Ehinger Tor
* @param a_pccSource Pointer to the name of the source file or link
* @param a_pccDest Pointer to the name of the destination file
* @param a_roEntry Reference to information about the object to be copied
* @return One of the return values of CopyFile() or CopyLink()
*/
int RScanner::CopyFileOrLink(const char *a_pccSource, const char *a_pccDest, const TEntry &a_roEntry)
{
int RetVal;
if (a_roEntry.IsLink())
{
RetVal = CopyLink(a_pccSource, a_pccDest, a_roEntry);
}
else
{
RetVal = CopyFile(a_pccSource, a_pccDest, a_roEntry);
}
return(RetVal);
}
/* Written: Saturday 03-Jan-2009 8:42 am */
int RScanner::CopyDirectory(char *a_pcSource, char *a_pcDest)
{
int RetVal;
printf("Copying directory \"%s\" to \"%s\"\n", a_pcSource, a_pcDest);
/* Create the new subdirectory in the target directory */
if ((RetVal = CreateDirectoryTree(a_pcDest)) == KErrNone)
{
TEntry Entry;
/* If CreateDirectoryTree() returned KErrNone then either the directory was created successfully */
/* or no creation was required (for instance, the user passed in x:\). In this case we want to */
/* ensure that the directory actually exists so that don't get into a loop of trying to create it */
if ((RetVal = Utils::GetFileInfo(a_pcDest, &Entry)) == KErrNone)
{
/* Call scan on the source and new destination directory, which will automatically copy all files */
/* found in the source directory, and recurse into any directories found */
if ((RetVal = Scan(a_pcSource, a_pcDest)) == KErrNone)
{
/* Now give the destination directory the same timestamp as the source one */
if ((RetVal = Utils::GetFileInfo(a_pcSource, &Entry)) == KErrNone)
{
if ((RetVal = Utils::setFileDate(a_pcDest, Entry)) != KErrNone)
{
Utils::Error("Unable to set datestamp for directory \"%s\" (Error = %d)", a_pcDest, RetVal);
}
}
else
{
Utils::Error("Unable to get file information for directory \"%s\" (Error = %d)", a_pcSource, RetVal);
}
}
}
else
{
Utils::Error("Target directory \"%s\" does not exist (Error = %d)", a_pcDest, RetVal);
}
}
else
{
Utils::Error("Unable to create directory \"%s\" (Error = %d)", a_pcDest, RetVal);
}
return(RetVal);
}
/**
* Copies a link to a destination.
* This function will create a link in the destination given by a_pccDest that is a
* clone of the link given by a_pccSource. The function will determine the relative
* path to the source link and will recreate that relative path on the destination
* link, thus allowing links to be recreated on different drives. For example, if
* the source link points to Work:Some/Directory/File.txt and it is being copied to
* Ram: then the newly created link will point to Ram:Some/Directory/File.txt.
*
* This function only handles links where the file being linked to is in the same
* directory as the link being linked from.
*
* @date Monday 30-Nov-2015 07:10 am, Code HQ Ehinger Tor
* @param a_pccSource Pointer to the name of the source link
* @param a_pccDest Pointer to the name of the destination link
* @param a_roEntry Reference to information about the object to be copied
* @return KErrNone if successful
* @return KErrGeneral if the link could not be created
* @return KErrNoMemory if not enough memory was available
*/
int RScanner::CopyLink(const char *a_pccSource, const char *a_pccDest, const TEntry &a_roEntry)
{
char *ResolvedSourceFile, *ResolvedSourcePath;
const char *SourceSlash, *DestSlash;
int RetVal;
size_t Offset;
string SourcePath, DestPath;
TEntry Entry;
/* Assume success */
RetVal = KErrNone;
printf("Copying link \"%s\" to \"%s\"\n", a_pccSource, a_pccDest);
/* Determine the path of the source link, without a trailing '/' */
SourcePath = a_pccSource;
SourceSlash = Utils::filePart(SourcePath.c_str());
Offset = (SourceSlash - SourcePath.c_str());
if ((Offset > 0) && (SourcePath[Offset - 1] == '/'))
{
--Offset;
}
SourcePath.resize(Offset);
/* Determine the path of the destination link, without a trailing '/' */
DestPath = a_pccDest;
DestSlash = Utils::filePart(DestPath.c_str());
Offset = (DestSlash - DestPath.c_str());
if ((Offset > 0) && (DestPath[Offset - 1] == '/'))
{
--Offset;
}
DestPath.resize(Offset);
/* Now determine the real name and path of the source link; that is, the name and path of the */
/* file that the source link points to. This will be used for determining the name of the file */
/* that the desination link will point to */
ResolvedSourceFile = Utils::ResolveFileName(a_pccSource);
ResolvedSourcePath = Utils::ResolveFileName(SourcePath.c_str());
if ((ResolvedSourceFile) && (ResolvedSourcePath))
{
/* Assuming that the target of the link exists in the same directory as the source link, or */
/* below, strip out the base path to the source link (keeping the relative path) to obtain the */
/* relative path to the target file */
string LinkTarget = ResolvedSourceFile;
if ((Offset = LinkTarget.rfind(ResolvedSourcePath)) != string::npos)
{
LinkTarget.erase(0, (Offset + strlen(ResolvedSourcePath)));
}
/* If the path ends in ':' then it will not be included in the filename so we don't need to */
/* worry about it. If it ends in '/' then it will be at the start of the filename so we need */
/* to remove it. This is because we are handling the paths ourselves in here rather than using */
/* a helper function */
if (LinkTarget[0] == '/')
{
LinkTarget.erase(0, 1);
}
/* Creating a link will fail if it already exists so delete it if necessary. Don't check for its */
/* existence first as this is in itself tricky, due to it being a link! */
deleteFile(a_pccDest);
/* And finally after much work, create the link to the destination file */
printf(" %s -> %s\n", a_pccDest, LinkTarget.c_str());
RetVal = Utils::makeLink(a_pccDest, LinkTarget.c_str());
if (RetVal == KErrNone)
{
if ((RetVal = Utils::setFileDate(a_pccDest, a_roEntry, EFalse)) != KErrNone)
{
Utils::Error("Unable to set datestamp on link \"%s\" (Error %d)", a_pccDest, RetVal);
}
}
else
{
Utils::Error("Unable to copy link from \"%s\" to \"%s\" (Error %d)", a_pccSource, a_pccDest, RetVal);
}
if (g_oArgs[ARGS_NOERRORS])
{
RetVal = KErrNone;
}
}
else
{
RetVal = KErrNoMemory;
}
delete [] ResolvedSourceFile;
delete [] ResolvedSourcePath;
return(RetVal);
}
/* Written: Saturday 18-Jul-2009 8:48 pm */
int RScanner::CompareDirectories(char *a_pcSource, char *a_pcDest, const TEntry &a_roEntry, TEntryArray &a_roDestEntries)
{
int RetVal;
const TEntry *DestEntry;
/* Scan the source and destination directories and mirror the source into the destination, but only */
/* if the NORECURSE parameter has not been specified */
if (!(g_oArgs[ARGS_NORECURSE]))
{
RetVal = Scan(a_pcSource, a_pcDest);
}
else
{
RetVal = KErrNone;
}
/* Iterate through the destination list and find the directory we have just mirrored into */
DestEntry = a_roDestEntries.getHead();
while (DestEntry)
{
if (_stricmp(a_roEntry.iName, DestEntry->iName) == 0)
{
/* Remove the entry from the destination list to speed up future searches and facilitate the */
/* ability to detect files that only exist in the destination directory */
a_roDestEntries.remove(DestEntry);
delete (TEntry *) DestEntry;
break;
}
DestEntry = a_roDestEntries.getSucc(DestEntry);
}
if (g_oArgs[ARGS_NOERRORS])
{
RetVal = KErrNone;
}
return(RetVal);
}
/* Written: Saturday 03-Nov-2007 10:31 pm */
int RScanner::CompareFiles(const char *a_pccSource, const char *a_pccDest, const TEntry &a_roEntry, TEntryArray &a_roDestEntries)
{
bool CRCOk, Match, ModifiedOk;
int SourceMilliSeconds, DestMilliSeconds, RetVal;
TUint SourceCRC, DestCRC;
const TEntry *DestEntry;
/* Assume success */
RetVal = KErrNone;
/* Iterate through the entries in the destination directory and see if the source file already */
/* exists and has the identical propereties */
DestEntry = a_roDestEntries.getHead();
while (DestEntry)
{
/* Perform a case dependent or non case dependent comparison of the filenames, depending on */
/* the user's preference */
if (g_oArgs[ARGS_NOCASE])
{
Match = (!(_stricmp(a_roEntry.iName, DestEntry->iName)));
}
else
{
Match = (!(strcmp(a_roEntry.iName, DestEntry->iName)));
}
if (Match)
{
/* Only check the modification time if the user has not explicitly disabled */
/* the checking */
if (g_oArgs[ARGS_NODATES])
{
ModifiedOk = ETrue;
}
else
{
/* Do a special check of the modification time and date. Some file systems (such as ext2 over */
/* Samba) are not particularly accurate and their timestamps can be up to a second out, due to */
/* the file's milliseconds value not being tracked. This is an unfortunately empirical hack but */
/* it's required for backing up from Amiga OS SFS -> ext2 and NTFS -> ext2 */
ModifiedOk = (a_roEntry.iModified == DestEntry->iModified) ? ETrue : EFalse;
if (!(ModifiedOk))
{
/* Convert the source and destination times to milliseconds */
SourceMilliSeconds = ((((a_roEntry.iModified.DateTime().Hour() * 60) +
a_roEntry.iModified.DateTime().Minute()) * 60) + a_roEntry.iModified.DateTime().Second());
SourceMilliSeconds = ((SourceMilliSeconds * 1000) + a_roEntry.iModified.DateTime().MilliSecond());
DestMilliSeconds = ((((DestEntry->iModified.DateTime().Hour() * 60) +
DestEntry->iModified.DateTime().Minute()) * 60) + DestEntry->iModified.DateTime().Second());
DestMilliSeconds = ((DestMilliSeconds * 1000) + DestEntry->iModified.DateTime().MilliSecond());
/* And ensure that there is no more than 1 second difference */
if (abs(DestMilliSeconds - SourceMilliSeconds) < 1000)
{
ModifiedOk = true;
}
}
}
/* If the user has asked to check the CRC then do that now. Otherwise just pretend that the CRC is valid */
SourceCRC = DestCRC = 0;
CRCOk = true;
if (g_oArgs[ARGS_CRC])
{
/* Calculate the CRCs of the source and destination files */
RetVal = CheckCRC(a_pccSource, a_pccDest, &SourceCRC, &DestCRC);
/* If the CRCs match or if they don't match then this is considered a valid result so indicate this. */
/* Any other return value from CheckCRC() is considered an error and will halt processing */
if (RetVal == KErrNone)
{
CRCOk = true;
}
else if (RetVal == KErrCorrupt)
{
RetVal = KErrNone;
CRCOk = false;
}
}
if (RetVal == KErrNone)
{
/* Only copy the file or print a message if the file is not on the filter list */
if (!(CheckFilterList(a_pccSource)))
{
/* If the source and destination files are different sizes, or their modification times are */
/* different, or their attributes are different and the user has not specified the NOPROTECT */
/* command line option, then the two files are classified as not matching and must be either */
/* copied or at least information printed about them */
if ((a_roEntry.iSize != DestEntry->iSize) || (!(ModifiedOk)) || (!(CRCOk)) ||
((!(g_oArgs[ARGS_NOPROTECT])) && (a_roEntry.iAttributes != DestEntry->iAttributes)))
{
/* If the user has specified the COPY command line option then copy the file now */
if (g_oArgs[ARGS_COPY])
{
RetVal = CopyFileOrLink(a_pccSource, a_pccDest, a_roEntry);
}
/* Otherwise just display information on why the files do not match, possibly */
/* repairing their metadata if requested to do so */
else
{
printf("File \"%s\" does not match: ", a_pccSource);
if (a_roEntry.iSize != DestEntry->iSize)
{
/* Amiga OS has no support for the %lld format specifier, so we have no choice but to just */
/* cast the 64 bit value to an integer and hope for the best */
#ifdef __amigaos__
printf("size = %d -vs- %d\n", (int) a_roEntry.iSize, (int) DestEntry->iSize);
#else /* ! __amigaos__ */
printf("size = %lld -vs- %lld\n", a_roEntry.iSize, DestEntry->iSize);
#endif /* ! __amigaos__ */
}
else if (!(ModifiedOk))
{
std::string SourceDate, SourceTime, DestDate, DestTime;
Utils::TimeToString(SourceDate, SourceTime, a_roEntry);
Utils::TimeToString(DestDate, DestTime, *DestEntry);
/* Set the target file's date and time to match that of the source, if requested */
if (g_oArgs[ARGS_FIXDATES])
{
printf("%s %s -> %s %s\n", SourceDate.c_str(), SourceTime.c_str(), DestDate.c_str(), DestTime.c_str());
if ((RetVal = Utils::setFileDate(a_pccDest, a_roEntry)) != KErrNone)
{
Utils::Error("Unable to set datestamp for file \"%s\" (Error %d)", a_pccDest, RetVal);
}
}
else
{
printf("%s %s -vs- %s %s\n", SourceDate.c_str(), SourceTime.c_str(), DestDate.c_str(), DestTime.c_str());
}
}
else if (!(CRCOk))
{
printf("CRC = %x -vs- %x\n", SourceCRC, DestCRC);
}
else
{
/* Set the target file's attributes to match that of the source, if requested */
if (g_oArgs[ARGS_FIXPROTECT])