1 // 2 // Copyright 2011 The Android Open Source Project 3 // 4 5 // File Finder. 6 // This is a collection of useful functions for finding paths and modification 7 // times of files that match an extension pattern in a directory tree. 8 // and finding files in it. 9 10 #ifndef FILEFINDER_H 11 #define FILEFINDER_H 12 13 #include <utils/Vector.h> 14 #include <utils/KeyedVector.h> 15 #include <utils/String8.h> 16 17 #include "DirectoryWalker.h" 18 19 using namespace android; 20 21 // Abstraction to allow for dependency injection. See MockFileFinder.h 22 // for the testing implementation. 23 class FileFinder { 24 public: 25 virtual bool findFiles(String8 basePath, Vector<String8>& extensions, 26 KeyedVector<String8,time_t>& fileStore, 27 DirectoryWalker* dw) = 0; 28 29 virtual ~FileFinder() {}; 30 }; 31 32 class SystemFileFinder : public FileFinder { 33 public: 34 35 /* findFiles takes a path, a Vector of extensions, and a destination KeyedVector 36 * and places path/modification date key/values pointing to 37 * all files with matching extensions found into the KeyedVector 38 * PRECONDITIONS 39 * path is a valid system path 40 * extensions should include leading "." 41 * This is not necessary, but the comparison directly 42 * compares the end of the path string so if the "." 43 * is excluded there is a small chance you could have 44 * a false positive match. (For example: extension "png" 45 * would match a file called "blahblahpng") 46 * 47 * POSTCONDITIONS 48 * fileStore contains (in no guaranteed order) paths to all 49 * matching files encountered in subdirectories of path 50 * as keys in the KeyedVector. Each key has the modification time 51 * of the file as its value. 52 * 53 * Calls checkAndAddFile on each file encountered in the directory tree 54 * Recursively descends into subdirectories. 55 */ 56 virtual bool findFiles(String8 basePath, Vector<String8>& extensions, 57 KeyedVector<String8,time_t>& fileStore, 58 DirectoryWalker* dw); 59 60 private: 61 /** 62 * checkAndAddFile looks at a single file path and stat combo 63 * to determine whether it is a matching file (by looking at 64 * the extension) 65 * 66 * PRECONDITIONS 67 * no setup is needed 68 * 69 * POSTCONDITIONS 70 * If the given file has a matching extension then a new entry 71 * is added to the KeyedVector with the path as the key and the modification 72 * time as the value. 73 * 74 */ 75 static void checkAndAddFile(const String8& path, const struct stat* stats, 76 Vector<String8>& extensions, 77 KeyedVector<String8,time_t>& fileStore); 78 79 }; 80 #endif // FILEFINDER_H 81