2 // Copyright 2011 The Android Open Source Project
5 // File Finder implementation.
6 // Implementation for the functions declared and documented in FileFinder.h
8 #include <utils/Vector.h>
9 #include <utils/String8.h>
10 #include <utils/KeyedVector.h>
15 #include "DirectoryWalker.h"
16 #include "FileFinder.h"
20 using android::String8;
22 // Private function to check whether a file is a directory or not
23 bool isDirectory(const char* filename) {
25 if (stat(filename, &fileStat) == -1) {
28 return(S_ISDIR(fileStat.st_mode));
32 // Private function to check whether a file is a regular file or not
33 bool isFile(const char* filename) {
35 if (stat(filename, &fileStat) == -1) {
38 return(S_ISREG(fileStat.st_mode));
41 bool SystemFileFinder::findFiles(String8 basePath, Vector<String8>& extensions,
42 KeyedVector<String8,time_t>& fileStore,
45 // Scan the directory pointed to by basePath
46 // check files and recurse into subdirectories.
47 if (!dw->openDir(basePath)) {
51 * Go through all directory entries. Check each file using checkAndAddFile
52 * and recurse into sub-directories.
55 while ((entry = dw->nextEntry()) != NULL) {
56 String8 entryName(entry->d_name);
57 if (entry->d_name[0] == '.') // Skip hidden files and directories
60 String8 fullPath = basePath.appendPathCopy(entryName);
61 // If this entry is a directory we'll recurse into it
62 if (isDirectory(fullPath.string()) ) {
63 DirectoryWalker* copy = dw->clone();
64 findFiles(fullPath, extensions, fileStore,copy);
68 // If this entry is a file, we'll pass it over to checkAndAddFile
69 if (isFile(fullPath.string()) ) {
70 checkAndAddFile(fullPath,dw->entryStats(),extensions,fileStore);
80 void SystemFileFinder::checkAndAddFile(String8 path, const struct stat* stats,
81 Vector<String8>& extensions,
82 KeyedVector<String8,time_t>& fileStore)
84 // Loop over the extensions, checking for a match
86 String8 ext(path.getPathExtension());
88 for (size_t i = 0; i < extensions.size() && !done; ++i) {
89 String8 ext2 = extensions[i].getPathExtension();
91 // Compare the extensions. If a match is found, add to storage.
94 fileStore.add(path,stats->st_mtime);