Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [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 | |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 8 | #include <androidfw/PathUtils.h> |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 9 | #include <utils/Vector.h> |
| 10 | #include <utils/String8.h> |
| 11 | #include <utils/KeyedVector.h> |
| 12 | |
| 13 | #include <dirent.h> |
| 14 | #include <sys/stat.h> |
| 15 | |
| 16 | #include "DirectoryWalker.h" |
| 17 | #include "FileFinder.h" |
| 18 | |
| 19 | //#define DEBUG |
| 20 | |
| 21 | using android::String8; |
| 22 | |
| 23 | // Private function to check whether a file is a directory or not |
| 24 | bool isDirectory(const char* filename) { |
| 25 | struct stat fileStat; |
| 26 | if (stat(filename, &fileStat) == -1) { |
| 27 | return false; |
| 28 | } |
| 29 | return(S_ISDIR(fileStat.st_mode)); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | // Private function to check whether a file is a regular file or not |
| 34 | bool isFile(const char* filename) { |
| 35 | struct stat fileStat; |
| 36 | if (stat(filename, &fileStat) == -1) { |
| 37 | return false; |
| 38 | } |
| 39 | return(S_ISREG(fileStat.st_mode)); |
| 40 | } |
| 41 | |
| 42 | bool SystemFileFinder::findFiles(String8 basePath, Vector<String8>& extensions, |
| 43 | KeyedVector<String8,time_t>& fileStore, |
| 44 | DirectoryWalker* dw) |
| 45 | { |
| 46 | // Scan the directory pointed to by basePath |
| 47 | // check files and recurse into subdirectories. |
| 48 | if (!dw->openDir(basePath)) { |
| 49 | return false; |
| 50 | } |
| 51 | /* |
| 52 | * Go through all directory entries. Check each file using checkAndAddFile |
| 53 | * and recurse into sub-directories. |
| 54 | */ |
| 55 | struct dirent* entry; |
| 56 | while ((entry = dw->nextEntry()) != NULL) { |
| 57 | String8 entryName(entry->d_name); |
| 58 | if (entry->d_name[0] == '.') // Skip hidden files and directories |
| 59 | continue; |
| 60 | |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 61 | String8 fullPath = appendPathCopy(basePath, entryName); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 62 | // If this entry is a directory we'll recurse into it |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 63 | if (isDirectory(fullPath.c_str()) ) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 64 | DirectoryWalker* copy = dw->clone(); |
| 65 | findFiles(fullPath, extensions, fileStore,copy); |
| 66 | delete copy; |
| 67 | } |
| 68 | |
| 69 | // If this entry is a file, we'll pass it over to checkAndAddFile |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 70 | if (isFile(fullPath.c_str()) ) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 71 | checkAndAddFile(fullPath,dw->entryStats(),extensions,fileStore); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Clean up |
| 76 | dw->closeDir(); |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
Chih-Hung Hsieh | 9b8528f | 2016-08-10 14:15:30 -0700 | [diff] [blame] | 81 | void SystemFileFinder::checkAndAddFile(const String8& path, const struct stat* stats, |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 82 | Vector<String8>& extensions, |
| 83 | KeyedVector<String8,time_t>& fileStore) |
| 84 | { |
| 85 | // Loop over the extensions, checking for a match |
| 86 | bool done = false; |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 87 | String8 ext(getPathExtension(path)); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 88 | ext.toLower(); |
| 89 | for (size_t i = 0; i < extensions.size() && !done; ++i) { |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 90 | String8 ext2 = getPathExtension(extensions[i]); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 91 | ext2.toLower(); |
| 92 | // Compare the extensions. If a match is found, add to storage. |
| 93 | if (ext == ext2) { |
| 94 | done = true; |
| 95 | fileStore.add(path,stats->st_mtime); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |