blob: 18775c06863f3ccefb8775a8b11ab44738b6b610 [file] [log] [blame]
Josiah Gaskin8a39da82011-06-06 17:00:35 -07001//
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
8#include <utils/Vector.h>
9#include <utils/String8.h>
10#include <utils/KeyedVector.h>
11
Josiah Gaskin91021652011-07-20 17:33:08 -070012#include <dirent.h>
13#include <sys/stat.h>
Josiah Gaskin8a39da82011-06-06 17:00:35 -070014
15#include "DirectoryWalker.h"
16#include "FileFinder.h"
17
18//#define DEBUG
19
20using android::String8;
Josiah Gaskin8a39da82011-06-06 17:00:35 -070021
Josiah Gaskin91021652011-07-20 17:33:08 -070022// Private function to check whether a file is a directory or not
23bool isDirectory(const char* filename) {
24 struct stat fileStat;
25 if (stat(filename, &fileStat) == -1) {
26 return false;
27 }
28 return(S_ISDIR(fileStat.st_mode));
29}
30
31
32// Private function to check whether a file is a regular file or not
33bool isFile(const char* filename) {
34 struct stat fileStat;
35 if (stat(filename, &fileStat) == -1) {
36 return false;
37 }
38 return(S_ISREG(fileStat.st_mode));
39}
40
Josiah Gaskin8a39da82011-06-06 17:00:35 -070041bool SystemFileFinder::findFiles(String8 basePath, Vector<String8>& extensions,
42 KeyedVector<String8,time_t>& fileStore,
43 DirectoryWalker* dw)
44{
45 // Scan the directory pointed to by basePath
46 // check files and recurse into subdirectories.
47 if (!dw->openDir(basePath)) {
48 return false;
49 }
Josiah Gaskin8a39da82011-06-06 17:00:35 -070050 /*
51 * Go through all directory entries. Check each file using checkAndAddFile
52 * and recurse into sub-directories.
53 */
54 struct dirent* entry;
55 while ((entry = dw->nextEntry()) != NULL) {
56 String8 entryName(entry->d_name);
57 if (entry->d_name[0] == '.') // Skip hidden files and directories
58 continue;
59
60 String8 fullPath = basePath.appendPathCopy(entryName);
61 // If this entry is a directory we'll recurse into it
Josiah Gaskin91021652011-07-20 17:33:08 -070062 if (isDirectory(fullPath.string()) ) {
Josiah Gaskin8a39da82011-06-06 17:00:35 -070063 DirectoryWalker* copy = dw->clone();
64 findFiles(fullPath, extensions, fileStore,copy);
65 delete copy;
66 }
67
68 // If this entry is a file, we'll pass it over to checkAndAddFile
Josiah Gaskin91021652011-07-20 17:33:08 -070069 if (isFile(fullPath.string()) ) {
Josiah Gaskin8a39da82011-06-06 17:00:35 -070070 checkAndAddFile(fullPath,dw->entryStats(),extensions,fileStore);
71 }
72 }
73
74 // Clean up
75 dw->closeDir();
76
77 return true;
78}
79
80void SystemFileFinder::checkAndAddFile(String8 path, const struct stat* stats,
81 Vector<String8>& extensions,
82 KeyedVector<String8,time_t>& fileStore)
83{
Josiah Gaskin8a39da82011-06-06 17:00:35 -070084 // Loop over the extensions, checking for a match
85 bool done = false;
86 String8 ext(path.getPathExtension());
87 ext.toLower();
88 for (size_t i = 0; i < extensions.size() && !done; ++i) {
89 String8 ext2 = extensions[i].getPathExtension();
90 ext2.toLower();
91 // Compare the extensions. If a match is found, add to storage.
92 if (ext == ext2) {
Josiah Gaskin8a39da82011-06-06 17:00:35 -070093 done = true;
94 fileStore.add(path,stats->st_mtime);
95 }
96 }
Josiah Gaskin91021652011-07-20 17:33:08 -070097}
98