blob: 58423789ce8b775699b0f5aa7dc870717ec1868a [file] [log] [blame]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001#include "files.h"
2#include <stdio.h>
3#include <errno.h>
4#include <sys/stat.h>
5#include <unistd.h>
6#include <dirent.h>
7#include <fnmatch.h>
8
9static bool
10is_comment_line(const char* p)
11{
12 while (*p && isspace(*p)) {
13 p++;
14 }
15 return *p == '#';
16}
17
18static string
19path_append(const string& base, const string& leaf)
20{
21 string full = base;
22 if (base.length() > 0 && leaf.length() > 0) {
23 full += '/';
24 }
25 full += leaf;
26 return full;
27}
28
29static bool
30is_whitespace_line(const char* p)
31{
32 while (*p) {
33 if (!isspace(*p)) {
34 return false;
35 }
36 p++;
37 }
38 return true;
39}
40
41static bool
42is_exclude_line(const char* p) {
43 while (*p) {
44 if (*p == '-') {
45 return true;
46 }
47 else if (isspace(*p)) {
48 p++;
49 }
50 else {
51 return false;
52 }
53 }
54 return false;
55}
56
57void
58split_line(const char* p, vector<string>* out)
59{
60 const char* q = p;
61 enum { WHITE, TEXT } state = WHITE;
62 while (*p) {
63 if (*p == '#') {
64 break;
65 }
66
67 switch (state)
68 {
69 case WHITE:
70 if (!isspace(*p)) {
71 q = p;
72 state = TEXT;
73 }
74 break;
75 case TEXT:
76 if (isspace(*p)) {
77 if (q != p) {
78 out->push_back(string(q, p-q));
79 }
80 state = WHITE;
81 }
82 break;
83 }
84 p++;
85 }
86 if (state == TEXT) {
87 out->push_back(string(q, p-q));
88 }
89}
90
91static void
92add_file(vector<FileRecord>* files, const string& listFile, int listLine,
93 const string& sourceName, const string& outName)
94{
95 FileRecord rec;
96 rec.listFile = listFile;
97 rec.listLine = listLine;
98 rec.sourceName = sourceName;
99 rec.outName = outName;
100 files->push_back(rec);
101}
102
103int
104read_list_file(const string& filename, vector<FileRecord>* files,
105 vector<string>* excludes)
106{
107 int err = 0;
108 FILE* f = NULL;
109 long size;
110 char* buf = NULL;
111 char *p, *q;
112 int i, lineCount;
113
114 f = fopen(filename.c_str(), "r");
115 if (f == NULL) {
116 fprintf(stderr, "Could not open list file (%s): %s\n",
117 filename.c_str(), strerror(errno));
118 err = errno;
119 goto cleanup;
120 }
121
122 err = fseek(f, 0, SEEK_END);
123 if (err != 0) {
124 fprintf(stderr, "Could not seek to the end of file %s. (%s)\n",
125 filename.c_str(), strerror(errno));
126 err = errno;
127 goto cleanup;
128 }
129
130 size = ftell(f);
131
132 err = fseek(f, 0, SEEK_SET);
133 if (err != 0) {
134 fprintf(stderr, "Could not seek to the beginning of file %s. (%s)\n",
135 filename.c_str(), strerror(errno));
136 err = errno;
137 goto cleanup;
138 }
139
140 buf = (char*)malloc(size+1);
141 if (buf == NULL) {
142 // (potentially large)
143 fprintf(stderr, "out of memory (%ld)\n", size);
144 err = ENOMEM;
145 goto cleanup;
146 }
147
148 if (1 != fread(buf, size, 1, f)) {
149 fprintf(stderr, "error reading file %s. (%s)\n",
150 filename.c_str(), strerror(errno));
151 err = errno;
152 goto cleanup;
153 }
154
155 // split on lines
156 p = buf;
157 q = buf+size;
158 lineCount = 0;
159 while (p<q) {
160 if (*p == '\r' || *p == '\n') {
161 *p = '\0';
162 lineCount++;
163 }
164 p++;
165 }
166
167 // read lines
168 p = buf;
169 for (i=0; i<lineCount; i++) {
170 int len = strlen(p);
171 q = p + len + 1;
172 if (is_whitespace_line(p) || is_comment_line(p)) {
173 ;
174 }
175 else if (is_exclude_line(p)) {
176 while (*p != '-') p++;
177 p++;
178 excludes->push_back(string(p));
179 }
180 else {
181 vector<string> words;
182
183 split_line(p, &words);
184
185#if 0
186 printf("[ ");
187 for (size_t k=0; k<words.size(); k++) {
188 printf("'%s' ", words[k].c_str());
189 }
190 printf("]\n");
191#endif
192
193 if (words.size() == 1) {
194 // pattern: DEST
195 add_file(files, filename, i+1, words[0], words[0]);
196 }
197 else if (words.size() == 2) {
198 // pattern: SRC DEST
199 add_file(files, filename, i+1, words[0], words[1]);
200 }
201 else {
202 fprintf(stderr, "%s:%d: bad format: %s\n", filename.c_str(),
203 i+1, p);
204 err = 1;
205 }
206 }
207 p = q;
208 }
209
210cleanup:
211 if (buf != NULL) {
212 free(buf);
213 }
214 if (f != NULL) {
215 fclose(f);
216 }
217 return err;
218}
219
220
221int
222locate(FileRecord* rec, const vector<string>& search)
223{
224 int err;
225
226 for (vector<string>::const_iterator it=search.begin();
227 it!=search.end(); it++) {
228 string full = path_append(*it, rec->sourceName);
229 struct stat st;
230 err = stat(full.c_str(), &st);
231 if (err == 0) {
232 rec->sourceBase = *it;
233 rec->sourcePath = full;
234 rec->sourceMod = st.st_mtime;
235 rec->sourceIsDir = S_ISDIR(st.st_mode);
236 return 0;
237 }
238 }
239
240 fprintf(stderr, "%s:%d: couldn't locate source file: %s\n",
241 rec->listFile.c_str(), rec->listLine, rec->sourceName.c_str());
242 return 1;
243}
244
245void
246stat_out(const string& base, FileRecord* rec)
247{
248 rec->outPath = path_append(base, rec->outName);
249
250 int err;
251 struct stat st;
252 err = stat(rec->outPath.c_str(), &st);
253 if (err == 0) {
254 rec->outMod = st.st_mtime;
255 rec->outIsDir = S_ISDIR(st.st_mode);
256 } else {
257 rec->outMod = 0;
258 rec->outIsDir = false;
259 }
260}
261
262string
263dir_part(const string& filename)
264{
265 int pos = filename.rfind('/');
266 if (pos <= 0) {
267 return ".";
268 }
269 return filename.substr(0, pos);
270}
271
272static void
273add_more(const string& entry, bool isDir,
274 const FileRecord& rec, vector<FileRecord>*more)
275{
276 FileRecord r;
277 r.listFile = rec.listFile;
278 r.listLine = rec.listLine;
279 r.sourceName = path_append(rec.sourceName, entry);
280 r.sourcePath = path_append(rec.sourceBase, r.sourceName);
281 struct stat st;
282 int err = stat(r.sourcePath.c_str(), &st);
283 if (err == 0) {
284 r.sourceMod = st.st_mtime;
285 }
286 r.sourceIsDir = isDir;
287 r.outName = path_append(rec.outName, entry);
288 more->push_back(r);
289}
290
291static bool
292matches_excludes(const char* file, const vector<string>& excludes)
293{
294 for (vector<string>::const_iterator it=excludes.begin();
295 it!=excludes.end(); it++) {
296 if (0 == fnmatch(it->c_str(), file, FNM_PERIOD)) {
297 return true;
298 }
299 }
300 return false;
301}
302
303static int
304list_dir(const string& path, const FileRecord& rec,
305 const vector<string>& excludes,
306 vector<FileRecord>* more)
307{
308 int err;
309
310 string full = path_append(rec.sourceBase, rec.sourceName);
311 full = path_append(full, path);
312
313 DIR *d = opendir(full.c_str());
314 if (d == NULL) {
315 return errno;
316 }
317
318 vector<string> dirs;
319
320 struct dirent *ent;
321 while (NULL != (ent = readdir(d))) {
322 if (0 == strcmp(".", ent->d_name)
323 || 0 == strcmp("..", ent->d_name)) {
324 continue;
325 }
326 if (matches_excludes(ent->d_name, excludes)) {
327 continue;
328 }
329 string entry = path_append(path, ent->d_name);
330#ifdef HAVE_DIRENT_D_TYPE
331 bool is_directory = (ent->d_type == DT_DIR);
332#else
333 // If dirent.d_type is missing, then use stat instead
334 struct stat stat_buf;
335 stat(entry.c_str(), &stat_buf);
336 bool is_directory = S_ISDIR(stat_buf.st_mode);
337#endif
338 add_more(entry, is_directory, rec, more);
339 if (is_directory) {
340 dirs.push_back(entry);
341 }
342 }
343 closedir(d);
344
345 for (vector<string>::iterator it=dirs.begin(); it!=dirs.end(); it++) {
346 list_dir(*it, rec, excludes, more);
347 }
348
349 return 0;
350}
351
352int
353list_dir(const FileRecord& rec, const vector<string>& excludes,
354 vector<FileRecord>* files)
355{
356 return list_dir("", rec, excludes, files);
357}