2 // Copyright 2011 The Android Open Source Project
4 // Defines an abstraction for opening a directory on the filesystem and
5 // iterating through it.
7 #ifndef DIRECTORYWALKER_H
8 #define DIRECTORYWALKER_H
11 #include <sys/types.h>
12 #include <sys/param.h>
15 #include <utils/String8.h>
19 using namespace android;
22 // This is an abstraction for walking through a directory and getting files
25 class DirectoryWalker {
27 virtual ~DirectoryWalker() {};
28 virtual bool openDir(String8 path) = 0;
29 virtual bool openDir(const char* path) = 0;
30 // Advance to next directory entry
31 virtual struct dirent* nextEntry() = 0;
32 // Get the stats for the current entry
33 virtual struct stat* entryStats() = 0;
35 virtual void closeDir() = 0;
36 // This class is able to replicate itself on the heap
37 virtual DirectoryWalker* clone() = 0;
40 // Current directory entry
42 // Stats for that directory entry
48 // System Directory Walker
49 // This is an implementation of the above abstraction that calls
50 // real system calls and is fully functional.
51 // functions are inlined since they're very short and simple
53 class SystemDirectoryWalker : public DirectoryWalker {
55 // Default constructor, copy constructor, and destructor are fine
57 virtual bool openDir(String8 path) {
60 dir = opendir(mBasePath.string() );
67 virtual bool openDir(const char* path) {
72 // Advance to next directory entry
73 virtual struct dirent* nextEntry() {
74 struct dirent* entryPtr = readdir(dir);
80 String8 fullPath = mBasePath.appendPathCopy(mEntry.d_name);
81 stat(fullPath.string(),&mStats);
84 // Get the stats for the current entry
85 virtual struct stat* entryStats() {
88 virtual void closeDir() {
91 virtual DirectoryWalker* clone() {
92 return new SystemDirectoryWalker(*this);
98 #endif // DIRECTORYWALKER_H