| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "na64util/runtimeDirs.hh" | ||
| 2 | |||
| 3 | #include <wordexp.h> | ||
| 4 | #include <cassert> | ||
| 5 | #include <cstring> | ||
| 6 | |||
| 7 | #include <sys/stat.h> | ||
| 8 | #include <glob.h> | ||
| 9 | #include <unistd.h> | ||
| 10 | |||
| 11 | namespace na64dp { | ||
| 12 | namespace util { | ||
| 13 | |||
| 14 | ✗ | RuntimeDirs::RuntimeDirs( const char * path_ ) { | |
| 15 | ✗ | assert( path_ && path_[0] != '\0' ); // non-empty lookup path | |
| 16 | ✗ | char * searchPath = strdup(path_); | |
| 17 | char * strtokSavePtr; | ||
| 18 | ✗ | for( char * pathEntry = strtok_r(searchPath, ":", &strtokSavePtr) | |
| 19 | ✗ | ; NULL != pathEntry | |
| 20 | ✗ | ; pathEntry = strtok_r(NULL, ":", &strtokSavePtr) ) { | |
| 21 | wordexp_t p; | ||
| 22 | char ** w; | ||
| 23 | |||
| 24 | ✗ | assert( pathEntry[0] != '\0' ); | |
| 25 | ✗ | _mlLog << "Expanding \"" << pathEntry << "\":" << std::endl; | |
| 26 | ✗ | wordexp( pathEntry, &p, 0 ); | |
| 27 | ✗ | w = p.we_wordv; | |
| 28 | ✗ | for( size_t i = 0; i < p.we_wordc; ++i ) { | |
| 29 | struct stat dirPathStat; | ||
| 30 | ✗ | int dirStatRC = stat( w[i], &dirPathStat ); | |
| 31 | ✗ | if( -1 == dirStatRC ) { | |
| 32 | ✗ | int dirStatErrcode = errno; | |
| 33 | _mlLog << " Error accessing \"" | ||
| 34 | ✗ | << w[i] << "\": " | |
| 35 | ✗ | << strerror(dirStatErrcode) | |
| 36 | ✗ | << std::endl; | |
| 37 | ✗ | continue; | |
| 38 | } | ||
| 39 | ✗ | if( ! S_ISDIR( dirPathStat.st_mode ) ) { | |
| 40 | _mlLog << " Omit \"" | ||
| 41 | ✗ | << w[i] << "\": " | |
| 42 | ✗ | << " not a directory, skip" | |
| 43 | ✗ | << std::endl; | |
| 44 | ✗ | continue; | |
| 45 | } | ||
| 46 | ✗ | _mlLog << " -> " << w[i] << std::endl; | |
| 47 | ✗ | push_back( w[i] ); | |
| 48 | } | ||
| 49 | ✗ | wordfree(&p); | |
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | // static int _glob_errfunc() | ||
| 54 | |||
| 55 | std::vector<std::string> | ||
| 56 | ✗ | RuntimeDirs::locate_files( const std::string & filePattern ) const { | |
| 57 | ✗ | std::vector<std::string> results; | |
| 58 | // first, consider a trivial case when provided "pattern" is just a file | ||
| 59 | // name or path | ||
| 60 | ✗ | if( access(filePattern.c_str(), F_OK) == 0 ) { | |
| 61 | ✗ | results.push_back(filePattern); | |
| 62 | ✗ | return results; | |
| 63 | } | ||
| 64 | // do globbing lookup | ||
| 65 | glob_t globbuf; | ||
| 66 | ✗ | globbuf.gl_offs = 0; | |
| 67 | ✗ | bool ranOnce = false; | |
| 68 | ✗ | for( auto dir : *this ) { | |
| 69 | ✗ | /*int globRC =*/ glob( (dir + "/" + filePattern).c_str() | |
| 70 | , GLOB_NOSORT | GLOB_MARK | ( ranOnce ? GLOB_APPEND | GLOB_DOOFFS : 0x0 ) | GLOB_TILDE | GLOB_BRACE | ||
| 71 | , NULL | ||
| 72 | , &globbuf ); | ||
| 73 | // ... | ||
| 74 | //globbuf.gl_offs = globbuf.gl_pathc; | ||
| 75 | ✗ | ranOnce = true; | |
| 76 | } | ||
| 77 | ✗ | for( size_t i = 0; i < globbuf.gl_pathc; ++i ) { | |
| 78 | ✗ | results.push_back( globbuf.gl_pathv[i] ); | |
| 79 | } | ||
| 80 | ✗ | globfree( &globbuf ); | |
| 81 | ✗ | return results; | |
| 82 | } | ||
| 83 | |||
| 84 | #if 0 | ||
| 85 | std::string | ||
| 86 | find_files_by_name_in( const std::string & paths_ | ||
| 87 | , const std::string & patterns_ | ||
| 88 | , bool multiple | ||
| 89 | ) { | ||
| 90 | RuntimeDirs rd(paths_); | ||
| 91 | const auto rdPaths = rd.locate_files(patterns_); | ||
| 92 | if( rdPaths.empty() ) return ""; | ||
| 93 | if( 1 == rdPaths.size() ) return rdPaths[0]; | ||
| 94 | if( !multiple ) { | ||
| 95 | |||
| 96 | } | ||
| 97 | } | ||
| 98 | #endif | ||
| 99 | |||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 |