forked from facebookarchive/RakNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_FindFirst.cpp
159 lines (101 loc) · 3.56 KB
/
_FindFirst.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
/**
* Original file by the_viking, fixed by Rômulo Fernandes, fixed by Emmanuel Nars
* Should emulate windows finddata structure
*/
#if (defined(__GNUC__) || defined(__GCCXML__)) && !defined(_WIN32)
#include "_FindFirst.h"
#include "DS_List.h"
#include <sys/stat.h>
#include <fnmatch.h>
static DataStructures::List< _findinfo_t* > fileInfo;
#include "RakMemoryOverride.h"
#include "RakAssert.h"
/**
* _findfirst - equivalent
*/
long _findfirst(const char *name, _finddata_t *f)
{
RakNet::RakString nameCopy = name;
RakNet::RakString filter;
// This is linux only, so don't bother with '\'
const char* lastSep = strrchr(name,'/');
if(!lastSep)
{
// filter pattern only is given, search current directory.
filter = nameCopy;
nameCopy = ".";
} else
{
// strip filter pattern from directory name, leave
// trailing '/' intact.
filter = lastSep+1;
unsigned sepIndex = lastSep - name;
nameCopy.Erase(sepIndex+1, nameCopy.GetLength() - sepIndex-1);
}
DIR* dir = opendir(nameCopy);
if(!dir) return -1;
_findinfo_t* fi = RakNet::OP_NEW<_findinfo_t>( _FILE_AND_LINE_ );
fi->filter = filter;
fi->dirName = nameCopy; // we need to remember this for stat()
fi->openedDir = dir;
fileInfo.Insert(fi, _FILE_AND_LINE_);
long ret = fileInfo.Size()-1;
// Retrieve the first file. We cannot rely on the first item
// being '.'
if (_findnext(ret, f) == -1) return -1;
else return ret;
}
int _findnext(long h, _finddata_t *f)
{
RakAssert(h >= 0 && h < (long)fileInfo.Size());
if (h < 0 || h >= (long)fileInfo.Size()) return -1;
_findinfo_t* fi = fileInfo[h];
while(true)
{
dirent* entry = readdir(fi->openedDir);
if(entry == 0) return -1;
// Only report stuff matching our filter
if (fnmatch(fi->filter, entry->d_name, FNM_PATHNAME) != 0) continue;
// To reliably determine the entry's type, we must do
// a stat... don't rely on entry->d_type, as this
// might be unavailable!
struct stat filestat;
RakNet::RakString fullPath = fi->dirName + entry->d_name;
if (stat(fullPath, &filestat) != 0)
{
RAKNET_DEBUG_PRINTF("Cannot stat %s\n", fullPath.C_String());
continue;
}
if (S_ISREG(filestat.st_mode))
{
f->attrib = _A_NORMAL;
} else if (S_ISDIR(filestat.st_mode))
{
f->attrib = _A_SUBDIR;
} else continue; // We are interested in files and
// directories only. Links currently
// are not supported.
f->size = filestat.st_size;
strncpy(f->name, entry->d_name, STRING_BUFFER_SIZE);
return 0;
}
return -1;
}
/**
* _findclose - equivalent
*/
int _findclose(long h)
{
if (h==-1) return 0;
if (h < 0 || h >= (long)fileInfo.Size())
{
RakAssert(false);
return -1;
}
_findinfo_t* fi = fileInfo[h];
closedir(fi->openedDir);
fileInfo.RemoveAtIndex(h);
RakNet::OP_DELETE(fi, _FILE_AND_LINE_);
return 0;
}
#endif