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
|
/* Copyright (C) 2007-2008 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
*/
#include "android/utils/dirscanner.h"
#include "android/utils/bufprint.h"
#include "android/utils/system.h"
#include "android/utils/path.h"
#include <stddef.h>
#define DIRSCANNER_BASE \
char root[PATH_MAX]; \
int rootLen; \
char full[PATH_MAX]; \
#if _WIN32
#include <io.h>
struct DirScanner {
DIRSCANNER_BASE
intptr_t findIndex1;
struct _finddata_t findData;
};
/* note: findIndex1 contains the find index + 1
* so a value of 0 means 'invalid'
*/
static int
_dirScannerInit( DirScanner* s )
{
char* p = s->root + s->rootLen;
char* end = s->root + sizeof s->root;
int ret;
/* create file spec by appending \* to root */
p = bufprint(p, end, "\\*");
if (p >= end)
return -1;
ret = _findfirst(s->root, &s->findData);
s->findIndex1 = ret+1;
return ret;
}
static void
_dirScanner_done( DirScanner* s )
{
if (s->findIndex1 > 0) {
_findclose(s->findIndex1-1);
s->findIndex1 = 0;
}
}
const char*
dirScanner_next( DirScanner* s )
{
char* ret = NULL;
if (!s || s->findIndex1 <= 0)
return NULL;
while (ret == NULL) {
ret = s->findData.name;
/* ignore special directories */
if (!strcmp(ret, ".") || !strcmp(ret, "..")) {
ret = NULL;
}
/* find next one */
if (_findnext(s->findIndex1-1, &s->findData) < 0) {
_dirScanner_done(s);
break;
}
}
return ret;
}
#else /* !_WIN32 */
#include <dirent.h>
struct DirScanner {
DIRSCANNER_BASE
DIR* dir;
struct dirent* entry;
};
static int
_dirScannerInit( DirScanner* s )
{
s->dir = opendir(s->root);
if (s->dir == NULL)
return -1;
s->entry = NULL;
return 0;
}
static void
_dirScanner_done( DirScanner* s )
{
if (s->dir) {
closedir(s->dir);
s->dir = NULL;
}
}
const char*
dirScanner_next( DirScanner* s )
{
char* ret = NULL;
if (!s || s->dir == NULL)
return NULL;
for (;;)
{
/* read new entry if needed */
s->entry = readdir(s->dir);
if (s->entry == NULL) {
_dirScanner_done(s);
break;
}
/* ignore special directories */
ret = s->entry->d_name;
if (!strcmp(ret,".") || !strcmp(ret,"..")) {
ret = NULL;
continue;
}
break;
}
return ret;
}
#endif /* !_WIN32 */
DirScanner*
dirScanner_new ( const char* rootPath )
{
DirScanner* s = android_alloc0(sizeof *s);
char* p = s->root;
char* end = p + sizeof s->root;
p = bufprint(p, end, "%s", rootPath);
if (p >= end)
goto FAIL;
s->rootLen = (p - s->root);
if (_dirScannerInit(s) < 0)
goto FAIL;
return s;
FAIL:
dirScanner_free(s);
return NULL;
}
void
dirScanner_free( DirScanner* s )
{
if (!s)
return;
_dirScanner_done(s);
AFREE(s);
}
const char*
dirScanner_nextFull( DirScanner* s )
{
const char* name = dirScanner_next(s);
char* p;
char* end;
if (name == NULL)
return NULL;
p = s->full;
end = p + sizeof s->full;
p = bufprint(p, end, "%.*s/%s", s->rootLen, s->root, name);
if (p >= end) {
/* ignore if the full name is too long */
return dirScanner_nextFull(s);
}
return s->full;
}
|