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