| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#ifndef DIRECTORYWALKER_H |
| 8 |
#define DIRECTORYWALKER_H |
| 9 |
|
| 10 |
#include <dirent.h> |
| 11 |
#include <sys/types.h> |
| 12 |
#include <sys/param.h> |
| 13 |
#include <sys/stat.h> |
| 14 |
#include <unistd.h> |
| 15 |
#include <utils/String8.h> |
| 16 |
|
| 17 |
#include <stdio.h> |
| 18 |
|
| 19 |
using namespace android; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class DirectoryWalker { |
| 26 |
public: |
| 27 |
virtual ~DirectoryWalker() {}; |
| 28 |
virtual bool openDir(String8 path) = 0; |
| 29 |
virtual bool openDir(const char* path) = 0; |
| 30 |
|
| 31 |
virtual struct dirent* nextEntry() = 0; |
| 32 |
|
| 33 |
virtual struct stat* entryStats() = 0; |
| 34 |
|
| 35 |
virtual void closeDir() = 0; |
| 36 |
|
| 37 |
virtual DirectoryWalker* clone() = 0; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
struct dirent mEntry; |
| 42 |
|
| 43 |
struct stat mStats; |
| 44 |
|
| 45 |
String8 mBasePath; |
| 46 |
}; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
class SystemDirectoryWalker : public DirectoryWalker { |
| 54 |
|
| 55 |
|
| 56 |
public: |
| 57 |
virtual bool openDir(String8 path) { |
| 58 |
mBasePath = path; |
| 59 |
dir = NULL; |
| •60 |
dir = opendir(mBasePath.c_str() ); |
| 61 |
|
| 62 |
if (dir == NULL) |
| 63 |
return false; |
| 64 |
|
| 65 |
return true; |
| 66 |
}; |
| 67 |
virtual bool openDir(const char* path) { |
| 68 |
String8 p(path); |
| 69 |
openDir(p); |
| 70 |
return true; |
| 71 |
}; |
| 72 |
|
| 73 |
virtual struct dirent* nextEntry() { |
| 74 |
struct dirent* entryPtr = readdir(dir); |
| 75 |
if (entryPtr == NULL) |
| 76 |
return NULL; |
| 77 |
|
| 78 |
mEntry = *entryPtr; |
| 79 |
|
| 80 |
String8 fullPath = mBasePath.appendPathCopy(mEntry.d_name); |
| •81 |
stat(fullPath.c_str(),&mStats); |
| 82 |
return &mEntry; |
| 83 |
}; |
| 84 |
|
| 85 |
virtual struct stat* entryStats() { |
| 86 |
return &mStats; |
| 87 |
}; |
| 88 |
virtual void closeDir() { |
| 89 |
closedir(dir); |
| 90 |
}; |
| 91 |
virtual DirectoryWalker* clone() { |
| 92 |
return new SystemDirectoryWalker(*this); |
| 93 |
}; |
| 94 |
private: |
| 95 |
DIR* dir; |
| 96 |
}; |
| 97 |
|
| 98 |
#endif |