-void TaskRemoveFiles::ReadDir(const std::string& path,
- std::list<std::string>& filesList)
-{
- LogInfo("Reading directory " << path);
- DIR* dir = NULL;
- struct dirent* ptr = NULL;
- dir = opendir(path.c_str());
- std::string delim = "";
-
- // adding / for path to directory to build a proper path to file under directory
- if (path[path.size() - 1] != '/') {
- delim = "/";
- }
-
- if (dir) {
- while ((ptr = readdir(dir)) != NULL) {
- if ((!strcmp(ptr->d_name, ".")) || (!strcmp(ptr->d_name, ".."))) {
- LogPedantic("Omiting " << ptr->d_name);
- continue;
- }
- std::string childPath = path + delim + ptr->d_name;
-
- struct stat st;
- if (0 != lstat(childPath.c_str(), &st)) {
- switch (errno) {
- case EACCES:
- LogWarning(
- "EACCESS Error occured during lstat with path: " <<
- childPath);
- continue;
- case EBADF:
- LogWarning(
- "EBADF Error occured during lstat with path: " <<
- childPath);
- continue;
- case ENOENT:
- LogWarning(
- "ENOENT Error occured during lstat with path: " <<
- childPath);
- continue;
- case ENOTDIR:
- LogWarning(
- "ENOTDIR Error occured during lstat with path: " <<
- childPath);
- continue;
- default:
- LogWarning(
- "Unknown Error occured during lstat with path: " <<
- childPath);
- continue;
- }
- } else {
- if (S_ISDIR(st.st_mode)) {
- LogPedantic(
- "Calling ReadDir in recursive way " << childPath);
- ReadDir(childPath, filesList);
- } else if (S_ISREG(st.st_mode) ||
- S_ISCHR(st.st_mode) ||
- S_ISBLK(st.st_mode) ||
- S_ISFIFO(st.st_mode) ||
- S_ISLNK(st.st_mode) ||
- S_ISSOCK(st.st_mode)) {
- LogPedantic("Adding to list " << childPath);
- filesList.push_front(childPath);
- } else {
- LogWarning("Uknown file type ??");
- }
- }
- }
- closedir(dir);
- } else if (errno == ENOTDIR) {
- LogDebug("Adding to list " << path);
- filesList.push_front(path);
- } else {
- LogWarning("Unknown error");
- }