|
| 1 | +// |
| 2 | +// FileScanner.cpp |
| 3 | +// libprojectM |
| 4 | +// |
| 5 | +// |
| 6 | + |
| 7 | +#include "FileScanner.hpp" |
| 8 | + |
| 9 | +FileScanner::FileScanner() {} |
| 10 | + |
| 11 | +FileScanner::FileScanner(std::vector<std::string> &rootDirs, std::vector<std::string> &extensions) : _rootDirs(rootDirs), _extensions(extensions) {} |
| 12 | + |
| 13 | +void FileScanner::scan(ScanCallback cb) { |
| 14 | +#ifdef WIN32 |
| 15 | + for (auto dir : _rootDirs) |
| 16 | + scanGeneric(cb, dir.c_str()); |
| 17 | +#else |
| 18 | + scanPosix(cb); |
| 19 | +#endif |
| 20 | +} |
| 21 | + |
| 22 | +void FileScanner::handleDirectoryError(std::string dir) { |
| 23 | +#ifdef WIN32 |
| 24 | + std::cerr << "[PresetLoader] warning: errno unsupported on win32 platforms. fix me" << std::endl; |
| 25 | +#else |
| 26 | + |
| 27 | + std::cerr << dir << " scan error: "; |
| 28 | + |
| 29 | + switch ( errno ) |
| 30 | + { |
| 31 | + case ENOENT: |
| 32 | + std::cerr << "ENOENT error. The path \"" << dir << "\" probably does not exist. \"man open\" for more info." << std::endl; |
| 33 | + break; |
| 34 | + case ENOMEM: |
| 35 | + std::cerr << "out of memory!" << std::endl; |
| 36 | + abort(); |
| 37 | + case ENOTDIR: |
| 38 | + std::cerr << "directory specified is not a directory! Trying to continue..." << std::endl; |
| 39 | + break; |
| 40 | + case ENFILE: |
| 41 | + std::cerr << "Your system has reached its open file limit. Trying to continue..." << std::endl; |
| 42 | + break; |
| 43 | + case EMFILE: |
| 44 | + std::cerr << "too many files in use by projectM! Bailing!" << std::endl; |
| 45 | + break; |
| 46 | + case EACCES: |
| 47 | + std::cerr << "permissions issue reading the specified preset directory." << std::endl; |
| 48 | + break; |
| 49 | + default: |
| 50 | + break; |
| 51 | + } |
| 52 | +#endif |
| 53 | +} |
| 54 | + |
| 55 | +std::string FileScanner::extensionMatches(std::string &filename) { |
| 56 | + // returns file name without extension |
| 57 | + // TODO: optimize me |
| 58 | + |
| 59 | + std::string lowerCaseFileName(filename); |
| 60 | + std::transform(lowerCaseFileName.begin(), lowerCaseFileName.end(), lowerCaseFileName.begin(), tolower); |
| 61 | + |
| 62 | + // Remove extension |
| 63 | + for (auto ext : _extensions) |
| 64 | + { |
| 65 | + size_t found = lowerCaseFileName.find(ext); |
| 66 | + if (found != std::string::npos) |
| 67 | + { |
| 68 | + std::string name = filename; |
| 69 | + name.replace(int(found), ext.size(), ""); |
| 70 | + return name; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return {}; |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | +// generic implementation using dirent |
| 79 | +void FileScanner::scanGeneric(ScanCallback cb, const char *currentDir) { |
| 80 | + DIR * m_dir; |
| 81 | + |
| 82 | + // Allocate a new a stream given the current directory name |
| 83 | + if ((m_dir = opendir(currentDir)) == NULL) |
| 84 | + { |
| 85 | + return; // no files found in here |
| 86 | + } |
| 87 | + |
| 88 | + struct dirent * dir_entry; |
| 89 | + |
| 90 | + while ((dir_entry = readdir(m_dir)) != NULL) |
| 91 | + { |
| 92 | + // Convert char * to friendly string |
| 93 | + std::string filename(dir_entry->d_name); |
| 94 | + |
| 95 | + if (filename.length() > 0 && filename[0] == '.') |
| 96 | + continue; |
| 97 | + |
| 98 | + std::string fullPath = std::string(currentDir) + PATH_SEPARATOR + filename; |
| 99 | + |
| 100 | + if (dir_entry->d_type == DT_DIR) { |
| 101 | + // recurse into dir |
| 102 | + scanGeneric(cb, fullPath.c_str()); |
| 103 | + continue; |
| 104 | + } else if (dir_entry->d_type != DT_REG && dir_entry->d_type != DT_LNK) { |
| 105 | + // not regular file/link |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + auto nameMatched = extensionMatches(filename); |
| 110 | + if (! nameMatched.empty()) |
| 111 | + cb(fullPath, nameMatched); |
| 112 | + } |
| 113 | + |
| 114 | + if (m_dir) |
| 115 | + { |
| 116 | + closedir(m_dir); |
| 117 | + m_dir = 0; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +#ifndef WIN32 |
| 122 | +// more optimized posix "fts" directory traversal |
| 123 | +int fts_compare(const FTSENT** one, const FTSENT** two) { |
| 124 | + return (strcmp((*one)->fts_name, (*two)->fts_name)); |
| 125 | +} |
| 126 | +#endif |
| 127 | + |
| 128 | +void FileScanner::scanPosix(ScanCallback cb) { |
| 129 | +#ifndef WIN32 |
| 130 | + |
| 131 | + // efficient directory traversal |
| 132 | + FTS* fileSystem = NULL; |
| 133 | + FTSENT *node = NULL; |
| 134 | + |
| 135 | + // list of directories to scan |
| 136 | + auto rootDirCount = _rootDirs.size(); |
| 137 | + char **dirList = (char **)malloc(sizeof(char*) * (rootDirCount + 1)); |
| 138 | + for (unsigned long i = 0; i < rootDirCount; i++) { |
| 139 | + dirList[i] = (char *) _rootDirs[i].c_str(); |
| 140 | + } |
| 141 | + dirList[rootDirCount] = NULL; |
| 142 | + |
| 143 | + // initialize file hierarchy traversal |
| 144 | + fileSystem = fts_open(dirList, FTS_LOGICAL|FTS_NOCHDIR|FTS_NOSTAT, &fts_compare); |
| 145 | + if (fileSystem == NULL) { |
| 146 | + std::string s; |
| 147 | + for (int i = 0; i < _rootDirs.size(); i++) |
| 148 | + s += _rootDirs[i] + ' '; |
| 149 | + handleDirectoryError(s); |
| 150 | + |
| 151 | + free(dirList); |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + std::string path, name, nameMatched; |
| 156 | + |
| 157 | + // traverse dirList |
| 158 | + while( (node = fts_read(fileSystem)) != NULL) { |
| 159 | + switch (node->fts_info) { |
| 160 | + case FTS_F: |
| 161 | + case FTS_SL: |
| 162 | + case FTS_NSOK: |
| 163 | + // found a file |
| 164 | + path = std::string(node->fts_path); |
| 165 | + name = std::string(node->fts_name); |
| 166 | + |
| 167 | + // check extension |
| 168 | + nameMatched = extensionMatches(name); |
| 169 | + if (! nameMatched.empty()) |
| 170 | + cb(path, nameMatched); |
| 171 | + break; |
| 172 | + default: |
| 173 | + break; |
| 174 | + } |
| 175 | + } |
| 176 | + fts_close(fileSystem); |
| 177 | + free(dirList); |
| 178 | + |
| 179 | +#endif |
| 180 | +} |
0 commit comments