| 61 | } |
| 62 | |
| 63 | void listdir_internal(std::list<std::string>& output_list, |
| 64 | const std::string& dirname, bool recursive, const std::string &prefix) |
| 65 | // Populates output_list with the path names of all the entries found |
| 66 | // in directory dirname. Each entry will be prefixed with prefix. If |
| 67 | // recursive is true then this will follow non-symbolic link subdirectory |
| 68 | // entries recursively with a suitably altered prefix. |
| 69 | { |
| 70 | DIR *dh; |
| 71 | struct dirent *ent; |
| 72 | |
| 73 | dh = opendir(dirname.c_str()); |
| 74 | if(!dh) { |
| 75 | std::ostringstream ss; |
| 76 | ss << "Error opening directory '" << dirname << "': " |
| 77 | << std::strerror(errno); |
| 78 | throw Error(ss.str()); |
| 79 | } |
| 80 | RIIA_DIR dh_guard(dh); |
| 81 | |
| 82 | errno = 0; |
| 83 | while((ent = readdir(dh)) != 0) { |
| 84 | |
| 85 | if(std::strcmp(ent->d_name, ".") != 0 && |
| 86 | std::strcmp(ent->d_name, "..") != 0) { |
| 87 | output_list.push_back(prefix + ent->d_name); |
| 88 | |
| 89 | if(recursive) { |
| 90 | // Stat this file to see if it's a subdirectory; if so dive |
| 91 | // into it. |
| 92 | std::string path; |
| 93 | makeabs(path, dirname, ent->d_name); |
| 94 | struct stat st; |
| 95 | if(stat(path.c_str(), &st) == -1) { |
| 96 | std::ostringstream ss; |
| 97 | ss << "Error stat'ing '" << path << "': " |
| 98 | << std::strerror(errno); |
| 99 | throw Error(ss.str()); |
| 100 | } |
| 101 | |
| 102 | // Look for directories that are not symbolic links to traverse |
| 103 | if(S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode)) { |
| 104 | std::ostringstream new_prefix; |
| 105 | if(prefix.length() > 0) { |
| 106 | new_prefix << prefix; |
| 107 | } |
| 108 | new_prefix << ent->d_name << "/"; |
| 109 | |
| 110 | listdir_internal( |
| 111 | output_list, |
| 112 | path, // search subdirectory |
| 113 | recursive, |
| 114 | new_prefix.str()); // new prefix |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Reset errno |
| 120 | errno = 0; |
no test coverage detected