-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDir.cpp
1516 lines (1122 loc) · 36.5 KB
/
Dir.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"
#ifdef __amigaos__
#include <proto/utility.h>
#include "OS4Support.h"
#elif defined(__unix__)
#include <errno.h>
#include <fnmatch.h>
#include <time.h>
#include <sys/stat.h>
#endif /* __unix__ */
#include <string.h>
#include "Dir.h"
#ifndef __amigaos4__
/* Size of the memory blocked used when scanning directories. Each structure instance is approximately 44 bytes */
/* in size, but we won't get exactly 100 entries of 44 bytes back, as the space is also used for the filename and */
/* comment strings, and it is possible to request less information than what can be stored in the structure */
#define NUM_ENTRIES 100
#define DATA_BUFFER_SIZE (sizeof(struct ExAllData) * NUM_ENTRIES)
#endif /* ! __amigaos4__ */
/** Structure containing information regarding the desired sort order */
struct SSortInfo
{
enum TDirSortOrder m_eSortOrder; /**< Sort order originally passed to TEntryArray::Sort() */
};
/**
* TEntry constructor.
* Initialises the members of the class to their default values, including setting
* the iAttributes member to something reasonable for each platform.
*
* @date Saturday 03-Nov-2007 7:27 pm
*/
TEntry::TEntry()
{
Reset();
}
/**
* TEntry constructor.
* Initialises most members of the class to their default values, including setting the
* iAttributes member to something reasonable for each platform. This override accepts a
* TDateTime structure that is assigned to the entry, and the entry's platform specific
* timestamp related fields are initialised as appropriate.
*
* @date Sunday 24-Jan-2021 8:03 am, Code HQ Bergmannstrasse
* @param a_roDateTime The timestamp to be assigned to the entry
*/
TEntry::TEntry(const TDateTime &a_roDateTime)
{
Reset();
iModified = a_roDateTime;
#ifdef __amigaos__
ULONG AmigaDate;
struct ClockData ClockData;
/* Extract the time information from the TDateTime structure into an Amiga specific structure */
ClockData.year = a_roDateTime.Year();
ClockData.month = (a_roDateTime.Month() + 1);
ClockData.mday = a_roDateTime.Day();
ClockData.hour = a_roDateTime.Hour();
ClockData.min = a_roDateTime.Minute();
ClockData.sec = a_roDateTime.Second();
AmigaDate = Date2Amiga(&ClockData);
/* And now convert that into the number of seconds since the 1st of January 1978 */
iPlatformDate.ds_Days = (AmigaDate / SECONDS_PER_DAY);
AmigaDate = (AmigaDate % SECONDS_PER_DAY);
iPlatformDate.ds_Minute = (AmigaDate / 60);
iPlatformDate.ds_Tick = ((AmigaDate % 60) * 50);
#elif defined(__unix__)
struct tm Tm;
/* Extract the time information from the TDateTime structure into a UNIX specific structure */
memset(&Tm, 0, sizeof(Tm));
Tm.tm_year = (a_roDateTime.Year() - 1900);
Tm.tm_mon = a_roDateTime.Month();
Tm.tm_mday = a_roDateTime.Day();
Tm.tm_hour = a_roDateTime.Hour();
Tm.tm_min = a_roDateTime.Minute();
Tm.tm_sec = a_roDateTime.Second();
Tm.tm_wday = 0;
Tm.tm_yday = 0;
Tm.tm_isdst = -1;
/* And now convert that into the number of seconds since the UNIX Epoch, which is the */
/* 1st of January 1970 */
iPlatformDate = mktime(&Tm);
#else /* ! __unix__ */
SYSTEMTIME SystemTime;
/* Extract the time information from the TDateTime structure into a Windows specific structure */
SystemTime.wYear = (WORD) a_roDateTime.Year();
SystemTime.wMonth = (WORD) (a_roDateTime.Month() + 1);
/* This member is ignored by SystemTimeToFileTime() but initialise it anyway, for consistency */
SystemTime.wDayOfWeek = 0;
SystemTime.wDay = (WORD) a_roDateTime.Day();
SystemTime.wHour = (WORD) a_roDateTime.Hour();
SystemTime.wMinute = (WORD) a_roDateTime.Minute();
SystemTime.wSecond = (WORD) a_roDateTime.Second();
SystemTime.wMilliseconds = (WORD) a_roDateTime.MilliSecond();
/* And now convert that into the number of 100 nanosecond intervals since the 1st of January 1601 */
DEBUGCHECK(SystemTimeToFileTime(&SystemTime, &iPlatformDate), "Unable to convert date to filetime");
#endif /* ! __unix__ */
}
/**
* Clears a file's archive attribute.
* Clears a file's archive attribute, thus indicating that the file has been archived and is no
* longer changed on disc. The next time the file is edited by a program, the archive attribute
* will again be set, indicating to backup software that the file needs to be backed up.
* This function currently only performs an action on Windows.
*
* @date Thursday 09-Jun-2016 06:44 am, Code HQ Ehinger Tor
*/
void TEntry::ClearArchive()
{
#ifdef WIN32
/* Windows is a little odd with its "normal" file attribute. A sane implementation would */
/* simply consider a file with no attributes to be a normal file, but with Windows a file */
/* can be normal while still having special attribute bits set. So to clear the archive */
/* attribute we have to both clear the archive attribute and set the normal attribute */
iAttributes = (iAttributes & ~FILE_ATTRIBUTE_ARCHIVE);
iAttributes |= FILE_ATTRIBUTE_NORMAL;
#endif /* WIN32 */
}
/* Written: Saturday 03-Nov-2007 8:07 pm */
TBool TEntry::IsDir() const
{
return(iIsDir);
}
/* Written: Saturday 03-Nov-2007 8:15 pm */
TBool TEntry::IsLink() const
{
return(iIsLink);
}
/* Written: Monday 28-Dec-2009 12:15 pm */
TBool TEntry::IsHidden() const
{
#ifdef __amigaos__
/* Amiga OS does not have the concept of hidden files */
return(EFalse);
#elif defined(__unix__)
/* UNIX does not have the concept of hidden files (we won't count files starting */
/* with . as hidden as this is not a function of the file system */
return(EFalse);
#else /* ! __unix__ */
return(iAttributes & FILE_ATTRIBUTE_HIDDEN);
#endif /* ! __unix__ */
}
/* Written: Friday 17-Aug-2012 6:49 am */
TBool TEntry::IsReadable() const
{
#ifdef __amigaos__
return((iAttributes & EXDF_NO_READ) == 0);
#elif defined(__unix__)
return(iAttributes & S_IRUSR);
#else /* ! __unix__ */
return(ETrue);
#endif /* ! __unix__ */
}
/* Written: Friday 17-Aug-2012 6:51 am */
TBool TEntry::IsWriteable() const
{
#ifdef __amigaos__
return((iAttributes & EXDF_NO_WRITE) == 0);
#elif defined(__unix__)
return(iAttributes & S_IWUSR);
#else /* ! __unix__ */
return((iAttributes & FILE_ATTRIBUTE_READONLY) == 0);
#endif /* ! __unix__ */
}
/* Written: Friday 17-Aug-2012 6:52 am */
TBool TEntry::IsExecutable() const
{
#ifdef __amigaos__
return((iAttributes & EXDF_NO_EXECUTE) == 0);
#elif defined(__unix__)
return(iAttributes & S_IXUSR);
#else /* ! __unix__ */
return(ETrue);
#endif /* ! __unix__ */
}
/* Written: Friday 17-Aug-2012 6:53 am */
TBool TEntry::IsDeleteable() const
{
#ifdef __amigaos__
return((iAttributes & EXDF_NO_DELETE) == 0);
#elif defined(__unix__)
return(iAttributes & S_IWUSR);
#else /* ! __unix__ */
return((iAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)) == 0);
#endif /* ! __unix__ */
}
/**
* Reset the class instance's variables to defaults.
* Initialises the members of the class to their default values, including setting
* the iAttributes member to something reasonable for each platform.
*
* @date Monday 25-Jan-2021 6:18 am, Code HQ Bergmannstrasse
* @param Parameter Description
* @return Return value
*/
void TEntry::Reset()
{
iName[0] = iLink[0] = '\0';
iIsDir = iIsLink = EFalse;
iSize = 0;
#ifdef __amigaos__
iAttributes = 0;
#elif defined(__unix__)
iAttributes = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
#else /* WIN32 */
iAttributes = FILE_ATTRIBUTE_NORMAL;
#endif /* WIN32 */
}
/* Written: Saturday 03-Nov-2007 6:42 pm */
void TEntry::Set(TBool a_bIsDir, TBool a_bIsLink, TInt64 a_iSize, TUint a_uiAttributes, const TDateTime &a_roDateTime)
{
iIsDir = a_bIsDir;
iIsLink = a_bIsLink;
iSize = a_iSize;
iAttributes = a_uiAttributes;
iModified = a_roDateTime;
#ifdef WIN32
/* From a functional perspective, knowing that a file on Windows is compressed by the file system is of no use, */
/* but it can confuse software that is trying to compare files for equality. So throw this attribute away, if */
/* it is present */
iAttributes &= ~FILE_ATTRIBUTE_COMPRESSED;
if (iAttributes == 0)
{
iAttributes |= FILE_ATTRIBUTE_NORMAL;
}
#endif /* WIN32 */
}
/* Written: Saturday 03-Nov-2007 5:58 pm */
TEntryArray::~TEntryArray()
{
Purge();
}
/* Written: Friday 19-Jun-2009 7:11 am */
TEntry *TEntryArray::Append(const char *a_pccName)
{
TEntry *Entry;
/* Allocate a new TEntry node */
if ((Entry = new TEntry) != NULL)
{
/* Copy the name of the file */
strcpy(Entry->iName, a_pccName);
/* And append the node to the list */
iEntries.addTail(Entry);
}
return(Entry);
}
/**
* Compares two directory entries and returns whether one is "less" than the other.
* This function is used by the TEntryArray::Sort() function as a callback when the list of
* TEntry structures is being sorted. The sort function calls it in order to determine in which
* order to sort the nodes in the list. The return value will depend on the sort mode being used.
*
* @date Saturday 12-Jul-2014 6:59 am, Code HQ Ehinger Tor
* @param a_poFirst Pointer to the first node to be compared
* @param a_poSecond Pointer to the second node to be compared
* @param a_pvUserData Pointer to a SSortInfo structure that was passed to StdList::Sort()
* @return A negative value to indicate that the first node should go first on the list,
* otherwise a positive value to indicate that the second node should go first
*/
TInt TEntryArray::CompareEntries(const TEntry *a_poFirst, const TEntry *a_poSecond, void *a_pvUserData)
{
TInt RetVal;
struct SSortInfo *SortInfo;
SortInfo = (struct SSortInfo *) a_pvUserData;
ASSERTM((SortInfo != NULL), "TEntryArray::CompareEntries() => User data passed in is invalid");
if (SortInfo->m_eSortOrder == EDirSortNameAscending)
{
RetVal = strcmp(a_poFirst->iName, a_poSecond->iName);
}
else if (SortInfo->m_eSortOrder == EDirSortSizeAscending)
{
RetVal = static_cast<TInt>(a_poFirst->iSize - a_poSecond->iSize);
}
else if (SortInfo->m_eSortOrder == EDirSortSizeDescending)
{
RetVal = static_cast<TInt>(a_poSecond->iSize - a_poFirst->iSize);
}
else if (SortInfo->m_eSortOrder == EDirSortDateAscending)
{
RetVal = (a_poFirst->iModified > a_poSecond->iModified);
}
else if (SortInfo->m_eSortOrder == EDirSortDateDescending)
{
RetVal = (a_poSecond->iModified > a_poFirst->iModified);
}
else
{
RetVal = strcmp(a_poSecond->iName, a_poFirst->iName);
}
return(RetVal);
}
/* Written: Saturday 03-Nov-2007 6:18 pm */
TInt TEntryArray::Count() const
{
return(iEntries.Count());
}
/* Written: Saturday 03-Nov-2007 6:20 pm */
const TEntry &TEntryArray::operator[](TInt a_iIndex) const
{
// TODO: CAW - Will be slow + what if this is NULL? Check for count and assert
TEntry *Entry;
Entry = iEntries.getHead();
while (a_iIndex > 0)
{
Entry = iEntries.getSucc(Entry);
--a_iIndex;
}
return(*Entry);
}
/* Written: Saturday 11-Jul-2008 10:44 pm */
const TEntry *TEntryArray::getHead() const
{
return(iEntries.getHead());
}
const TEntry *TEntryArray::getSucc(const TEntry *a_poEntry) const
{
return(iEntries.getSucc(a_poEntry));
}
/* Written: Saturday 11-Jul-2010 3:36 pm */
void TEntryArray::Purge()
{
TEntry *Entry;
/* Iterate through the list of nodes and delete each one */
while ((Entry = iEntries.remHead()) != NULL)
{
delete Entry;
}
}
/* Written: Saturday 11-Jul-2008 11:42 pm */
void TEntryArray::remove(const TEntry *a_poEntry)
{
iEntries.remove((TEntry *) a_poEntry);
}
/**
* Sorts the array of file entries.
* This function will sort the array of file entries in one of a number of ways, as
* specified by the a_eSortOrder parameter. The entries are sorted in situ.
*
* @date Saturday 12-Jul-2014 7:23 am, Code HQ Ehinger Tor
* @param a_eSortOrder Order in which to sort, as specified by the TDirSortOrder enum
*/
void TEntryArray::Sort(enum TDirSortOrder a_eSortOrder)
{
struct SSortInfo SortInfo;
/* Setup a structure containing information regarding the sort order and call the link list's */
/* sorting function */
SortInfo.m_eSortOrder = a_eSortOrder;
iEntries.Sort(CompareEntries, &SortInfo);
}
/* Written: Saturday 03-Nov-2007 5:24 pm */
RDir::RDir()
{
#ifdef __amigaos__
iPath = NULL;
iPattern = NULL;
iContext = NULL;
#ifndef __amigaos4__
iLock = 0;
iCurrent = iExAllData = NULL;
#endif /* ! __amigaos4__ */
#elif defined(__unix__)
iPathBuffer = iPath = iPattern = NULL;
iDir = NULL;
#else /* ! __unix__ */
iHandle = NULL;
#endif /* __unix__ */
iSingleEntryOk = EFalse;
}
#ifdef WIN32
/* Written: Friday 12-Oct-2012 5:39 am, Maxhotel Lindau */
/* @param a_poFindData Windows specific information about a scanned file */
/* @return KErrNone if the entry was appended successfully */
/* KErrNoMemory if not enough memory was available */
/* KErrGeneral if some other unspecified error occurred */
/* This Windows specific function will take a structure containing information about */
/* a scanned file and will convert it to the internal TEntry format used by the */
/* framework. It will then append it to the list of entries representing the directory */
/* that has been scanned */
TInt RDir::AppendDirectoryEntry(WIN32_FIND_DATA *a_poFindData)
{
TInt RetVal;
TEntry *Entry;
SYSTEMTIME SystemTime;
/* Assume success */
RetVal = KErrNone;
/* Only add the entry if it is not one of the pseudo directory entries */
if ((strcmp(a_poFindData->cFileName, ".")) && (strcmp(a_poFindData->cFileName, "..")))
{
/* Allocate a TEntry structure and simultaneously append it to the list */
if ((Entry = m_entries.Append(a_poFindData->cFileName)) != NULL)
{
/* Determine the time that the object was last written to */
if (FileTimeToSystemTime(&a_poFindData->ftLastWriteTime, &SystemTime))
{
/* Convert the Windows time to a generic framework time structure */
TDateTime DateTime(SystemTime.wYear, (TMonth) (SystemTime.wMonth - 1), SystemTime.wDay,
SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond, SystemTime.wMilliseconds);
/* And populate the TEntry structure with the rest of the information */
Entry->Set((a_poFindData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY), (a_poFindData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT),
(TInt64) a_poFindData->nFileSizeHigh << 32 | a_poFindData->nFileSizeLow, a_poFindData->dwFileAttributes, DateTime);
Entry->iPlatformDate = a_poFindData->ftLastWriteTime;
}
else
{
Utils::info("RDir::AppendDirectoryEntry() => Unable to determine time of file or directory");
RetVal = KErrGeneral;
}
}
else
{
Utils::info("RDir::AppendDirectoryEntry() => Unable to convert Windows time to generic framework time");
RetVal = KErrNoMemory;
}
}
return(RetVal);
}
#endif /* WIN32 */
/**
* Opens an object for scanning.
* This function prepares to scan a file or directory. The a_pccPattern parameter can
* refer to either a directory name, a single filename, a wildcard pattern or a combination
* thereof. Examples are:
*
* ""\n
* "."\n
* "SomeDir"\n
* "SomeDir/"\n
* "*"\n
* "*.cpp"\n
* "SomeFile"\n
* "SomeDir/SomeFile.txt"\n
* "PROGDIR:"\n
*
* @date Saturday 03-Nov-2007 4:43 pm
* @param a_pccPattern OS specific path and wildcard to scan
* @return KErrNone if directory was opened successfully
* @return KErrNotFound if the directory or file could not be opened for scanning
* @return KErrNoMemory if not enough memory was available
* @return KErrGeneral if some other unspecified error occurred
*/
TInt RDir::open(const char *a_pccPattern)
{
TInt RetVal;
TEntry *Entry;
/* Assume failure */
RetVal = KErrNoMemory;
/* Get information about the file or directory being passed in. If it is a file or a link */
/* then save the information for l8r and don't continue */
if (Utils::GetFileInfo(a_pccPattern, &iSingleEntry) == KErrNone)
{
if (!(iSingleEntry.IsDir()))
{
iSingleEntryOk = ETrue;
/* Append the entry to the array of files and directories being listed */
if ((Entry = m_entries.Append(iSingleEntry.iName)) != NULL)
{
RetVal = KErrNone;
Entry->Set(iSingleEntry.iIsDir, iSingleEntry.iIsLink, iSingleEntry.iSize, iSingleEntry.iAttributes,
iSingleEntry.iModified.DateTime());
Entry->iPlatformDate = iSingleEntry.iPlatformDate;
}
}
}
#ifdef __amigaos__
const char *Pattern;
TInt Length;
LONG Result;
STRPTR FileNameOffset;
/* Only try to scan a directory if it wasn't a single filename that was passed in */
if (!(iSingleEntryOk))
{
/* Allocate a buffer for the path passed in and save it for l8r use in RDir::read(). */
/* It is required in order to examine links */
if ((iPath = new char[strlen(a_pccPattern) + 1]) != NULL)
{
strcpy(iPath, a_pccPattern);
/* We may or may not need to use a pattern, depending on whether there is one */
/* passed in, so determine this and build a pattern to scan for as appropriate */
Pattern = Utils::filePart(a_pccPattern);
/* According to dos.doc, the buffer used must be at least twice the size of */
/* the pattern it is scanning + 2 */
Length = (strlen(Pattern) * 2 + 2);
if ((iPattern = new char[Length]) != NULL)
{
RetVal = KErrNone;
/* See if a pattern was passed in */
if ((Result = ParsePatternNoCase(Pattern, iPattern, Length)) == 1)
{
/* We are using a pattern so remove it from the base path, which we */
/* want to point just to the directory */
FileNameOffset = PathPart(iPath);
iPath[FileNameOffset - iPath] = '\0';
}
/* No pattern is in use so indicate this */
else if (Result == 0)
{
delete [] iPattern;
iPattern = NULL;
}
else
{
RetVal = KErrGeneral;
}
}
if (RetVal == KErrNone)
{
#ifdef __amigaos4__
/* Open a context for the directory to be scanned */
iContext = ObtainDirContextTags(EX_StringNameInput, iPath,
EX_DataFields, (EXF_DATE | EXF_PROTECTION | EXF_NAME | EXF_SIZE | EXF_TYPE), TAG_DONE);
#else /* ! __amigaos4__ */
if ((iLock = Lock(iPath, ACCESS_READ)) != 0)
{
/* Allocate a buffer for the file information to be returned, and a control block to keep track */
/* of the progress of the scan */
if ((iExAllData = new struct ExAllData[DATA_BUFFER_SIZE]) != NULL)
{
if ((iContext = (struct ExAllControl *) AllocDosObject(DOS_EXALLCONTROL, NULL)) != NULL)
{
iContext->eac_LastKey = 0;
}
}
}
if (!(iContext))
{
if (iExAllData)
{
delete [] iExAllData;
iExAllData = NULL;
}
if (iLock != 0)
{
UnLock(iLock);
iLock = 0;
}
}
#endif /* ! __amigaos4__ */
if (!(iContext))
{
Result = IoErr();
if (Result == ERROR_OBJECT_NOT_FOUND)
{
RetVal = KErrNotFound;
}
else
{
RetVal = KErrGeneral;
}
}
}
}
else
{
Utils::info("RDir::open() => Out of memory");
RetVal = KErrNoMemory;
}
}
#elif defined(__unix__)
char *ProgDirName;
const char *ToOpen;
TInt Length, FileNameOffset;
/* Only try to scan a directory if it wasn't a single filename that was passed in */
if (!(iSingleEntryOk))
{
/* If the filename is prefixed with an Amiga OS style "PROGDIR:" then resolve it */
if ((ProgDirName = Utils::ResolveProgDirName(a_pccPattern)) != NULL)
{
/* Allocate a buffer to hold the path part of the directory and save the path and */
/* wildcard (if any) into it */
Length = strlen(ProgDirName);
if ((iPathBuffer = new char[Length + 1]) != NULL)
{
strcpy(iPathBuffer, ProgDirName);
FileNameOffset = (Utils::filePart(iPathBuffer) - iPathBuffer);
/* If there is a wildcard present then extract it */
if ((strstr(iPathBuffer, "*")) || (strstr(iPathBuffer, "?")))
{
/* If FileNameOffset is > 0 then there is a path component so extract both it */
/* and the pattern */
if (FileNameOffset > 0)
{
iPathBuffer[FileNameOffset - 1] = '\0';
iPath = iPathBuffer;
iPattern = &iPathBuffer[FileNameOffset];
}
/* Otherwise there is only a pattern */
else
{
iPath = &iPathBuffer[Length];
iPattern = iPathBuffer;
}
}
/* There is no wildcard so extract only the path and set the pattern to empty */
else
{
iPath = iPathBuffer;
iPattern = &iPathBuffer[Length];
}
/* UNIX will not scan a directory represented by an empty string so if this has */
/* been passed in then convert it to a "." for compatibility with the RDir API */
ToOpen = iPath;
if (*ToOpen == '\0')
{
ToOpen = ".";
}
/* Open the directory for scanning. We don't do any actual scanning here - that will */
/* be done in read() */
if ((iDir = opendir(ToOpen)) != NULL)
{
RetVal = KErrNone;
}
else
{
RetVal = KErrNotFound;
}
}
/* And free the resolved filename, but only if it contained the PROGDIR: prefix */
if (ProgDirName != a_pccPattern)
{
delete [] ProgDirName;
}
}
else
{
RetVal = KErrNoMemory;
}
}
#else /* ! __unix__ */
char *Path, *ProgDirName;
const char *FileName;
size_t Length;
WIN32_FIND_DATA FindData;
/* Only try to scan a directory if it wasn't a single filename that was passed in */
if (!(iSingleEntryOk))
{
/* If the filename is prefixed with an Amiga OS style "PROGDIR:" then resolve it */
if ((ProgDirName = Utils::ResolveProgDirName(a_pccPattern)) != NULL)
{
/* Allocate a buffer large enough to hold the path to be scanned and the */
/* wildcard pattern used to scan it (wildcard + \0" == 5 bytes) */
Length = (strlen(ProgDirName) + 5);
if ((Path = new char[Length]) != NULL)
{
/* We may or may not need to append a wildcard, depending on whether there */
/* is already one in the pattern passed in, so determine this and build a */
/* wildcard pattern to scan for as appropriate */
FileName = Utils::filePart(ProgDirName);
if (!(strstr(FileName, "*")) && (!(strstr(FileName, "?"))))
{
strcpy(Path, ProgDirName);
DEBUGCHECK((Utils::addPart(Path, "*.*", Length) != EFalse), "RDir::open() => Unable to build wildcard to scan");
}
else
{
strcpy(Path, ProgDirName);
}
/* Scan the directory using the wildcard and find the first entry */
if ((iHandle = FindFirstFile(Path, &FindData)) != INVALID_HANDLE_VALUE)
{
RetVal = AppendDirectoryEntry(&FindData);
}
else
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
RetVal = KErrNone;
}
else
{
RetVal = KErrNotFound;
}
}
delete [] Path;
}
else
{
Utils::info("RDir::open() => Out of memory");
}
/* And free the resolved filename, but only if it contained the PROGDIR: prefix */
if (ProgDirName != a_pccPattern)
{
delete [] ProgDirName;
}
}
}
#endif /* ! __unix__ */
/* If anything went wrong, clean up whatever was allocated */
if (RetVal != KErrNone)
{
close();
}
return(RetVal);
}
/* Written: Saturday 03-Nov-2007 4:49 pm */
void RDir::close()
{
RDirObject::close();
#ifdef __amigaos__
delete [] iPath;
iPath = NULL;
delete [] iPattern;
iPattern = NULL;
#ifdef __amigaos4__
if (iContext)
{
ReleaseDirContext(iContext);
iContext = NULL;
}
#else /* ! __amigaos4__ */
if (iExAllData)
{
delete [] iExAllData;
iExAllData = NULL;
}
if (iContext)
{
FreeDosObject(DOS_EXALLCONTROL, iContext);
iContext = NULL;
}
if (iLock)
{
UnLock(iLock);
iLock = 0;
}
#endif /* ! __amigaos4__ */
#elif defined(__unix__)