Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame^] | 1 | // |
| 2 | // Copyright 2011 The Android Open Source Project |
| 3 | // |
| 4 | |
| 5 | // File Finder implementation. |
| 6 | // Implementation for the functions declared and documented in FileFinder.h |
| 7 | |
| 8 | #include <utils/Vector.h> |
| 9 | #include <utils/String8.h> |
| 10 | #include <utils/KeyedVector.h> |
| 11 | |
| 12 | |
| 13 | #include <iostream> |
| 14 | |
| 15 | #include "DirectoryWalker.h" |
| 16 | #include "FileFinder.h" |
| 17 | |
| 18 | //#define DEBUG |
| 19 | |
| 20 | using android::String8; |
| 21 | using std::cout; |
| 22 | using std::endl; |
| 23 | |
| 24 | bool SystemFileFinder::findFiles(String8 basePath, Vector<String8>& extensions, |
| 25 | KeyedVector<String8,time_t>& fileStore, |
| 26 | DirectoryWalker* dw) |
| 27 | { |
| 28 | // Scan the directory pointed to by basePath |
| 29 | // check files and recurse into subdirectories. |
| 30 | if (!dw->openDir(basePath)) { |
| 31 | return false; |
| 32 | } |
| 33 | #ifdef DEBUG |
| 34 | cout << "FileFinder looking in " << basePath << endl; |
| 35 | #endif // DEBUG |
| 36 | /* |
| 37 | * Go through all directory entries. Check each file using checkAndAddFile |
| 38 | * and recurse into sub-directories. |
| 39 | */ |
| 40 | struct dirent* entry; |
| 41 | while ((entry = dw->nextEntry()) != NULL) { |
| 42 | String8 entryName(entry->d_name); |
| 43 | if (entry->d_name[0] == '.') // Skip hidden files and directories |
| 44 | continue; |
| 45 | |
| 46 | String8 fullPath = basePath.appendPathCopy(entryName); |
| 47 | // If this entry is a directory we'll recurse into it |
| 48 | if (entry->d_type == DT_DIR) { |
| 49 | DirectoryWalker* copy = dw->clone(); |
| 50 | findFiles(fullPath, extensions, fileStore,copy); |
| 51 | delete copy; |
| 52 | } |
| 53 | |
| 54 | // If this entry is a file, we'll pass it over to checkAndAddFile |
| 55 | if (entry->d_type == DT_REG) { |
| 56 | checkAndAddFile(fullPath,dw->entryStats(),extensions,fileStore); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // Clean up |
| 61 | dw->closeDir(); |
| 62 | |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | void SystemFileFinder::checkAndAddFile(String8 path, const struct stat* stats, |
| 67 | Vector<String8>& extensions, |
| 68 | KeyedVector<String8,time_t>& fileStore) |
| 69 | { |
| 70 | #ifdef DEBUG |
| 71 | cout << "Checking file " << path << "..."; |
| 72 | #endif // DEBUG |
| 73 | // Loop over the extensions, checking for a match |
| 74 | bool done = false; |
| 75 | String8 ext(path.getPathExtension()); |
| 76 | ext.toLower(); |
| 77 | for (size_t i = 0; i < extensions.size() && !done; ++i) { |
| 78 | String8 ext2 = extensions[i].getPathExtension(); |
| 79 | ext2.toLower(); |
| 80 | // Compare the extensions. If a match is found, add to storage. |
| 81 | if (ext == ext2) { |
| 82 | #ifdef DEBUG |
| 83 | cout << "Match"; |
| 84 | #endif // DEBUG |
| 85 | done = true; |
| 86 | fileStore.add(path,stats->st_mtime); |
| 87 | } |
| 88 | } |
| 89 | #ifdef DEBUG |
| 90 | cout << endl; |
| 91 | #endif //DEBUG |
| 92 | } |