|
45 | 45 | #include <stack> |
46 | 46 | #include <stdexcept> |
47 | 47 | #include <string> |
| 48 | +#include <system_error> |
48 | 49 | #ifdef SIMPLECPP_WINDOWS |
49 | 50 | # include <mutex> |
50 | 51 | #endif |
@@ -3051,8 +3052,18 @@ std::pair<simplecpp::FileData *, bool> simplecpp::FileDataCache::tryload(FileDat |
3051 | 3052 | const std::string &path = name_it->first; |
3052 | 3053 | FileID fileId; |
3053 | 3054 |
|
3054 | | - if (!getFileId(path, fileId)) |
| 3055 | + std::string errmsg; |
| 3056 | + if (!getFileId(path, fileId, errmsg)) { |
| 3057 | + if (outputList && !errmsg.empty()) { |
| 3058 | + simplecpp::Output err = { |
| 3059 | + Output::CACHE_ERROR, |
| 3060 | + Location{filenames}, |
| 3061 | + "Failed to get ID for " + path + " (" + errmsg + ")" |
| 3062 | + }; |
| 3063 | + outputList->push_back(std::move(err)); |
| 3064 | + } |
3055 | 3065 | return {nullptr, false}; |
| 3066 | + } |
3056 | 3067 |
|
3057 | 3068 | const auto id_it = mIdMap.find(fileId); |
3058 | 3069 | if (id_it != mIdMap.end()) { |
@@ -3118,24 +3129,36 @@ std::pair<simplecpp::FileData *, bool> simplecpp::FileDataCache::get(const std:: |
3118 | 3129 | return {nullptr, false}; |
3119 | 3130 | } |
3120 | 3131 |
|
3121 | | -bool simplecpp::FileDataCache::getFileId(const std::string &path, FileID &id) |
3122 | | -{ |
| 3132 | +bool simplecpp::FileDataCache::getFileId(const std::string &path, FileID &id, std::string& errmsg) { |
| 3133 | + errmsg = ""; |
| 3134 | + |
3123 | 3135 | #ifdef _WIN32 |
3124 | 3136 | HANDLE hFile = CreateFileA(path.c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
3125 | | - |
3126 | | - if (hFile == INVALID_HANDLE_VALUE) |
| 3137 | + if (hFile == INVALID_HANDLE_VALUE) { |
| 3138 | + const DWORD err = GetLastError(); |
| 3139 | + if (err != ERROR_FILE_NOT_FOUND) |
| 3140 | + errmsg = "CreateFileA() failed with '" + std::system_category().message(err) + "'"; |
3127 | 3141 | return false; |
| 3142 | + } |
3128 | 3143 |
|
3129 | 3144 | const BOOL ret = GetFileInformationByHandleEx(hFile, FileIdInfo, &id.fileIdInfo, sizeof(id.fileIdInfo)); |
| 3145 | + if (!ret) { |
| 3146 | + const DWORD err = GetLastError(); |
| 3147 | + errmsg = "GetFileInformationByHandleEx() failed with '" + std::system_category().message(err) + "'"; |
| 3148 | + } |
3130 | 3149 |
|
3131 | 3150 | CloseHandle(hFile); |
3132 | 3151 |
|
3133 | 3152 | return ret == TRUE; |
3134 | 3153 | #else |
3135 | 3154 | struct stat statbuf; |
3136 | 3155 |
|
3137 | | - if (stat(path.c_str(), &statbuf) != 0) |
| 3156 | + if (stat(path.c_str(), &statbuf) == -1) { |
| 3157 | + const int err = errno; |
| 3158 | + if (err != ENOENT) |
| 3159 | + errmsg = std::string("stat() failed with '") + strerror(err) + "'"; |
3138 | 3160 | return false; |
| 3161 | + } |
3139 | 3162 |
|
3140 | 3163 | id.dev = statbuf.st_dev; |
3141 | 3164 | id.ino = statbuf.st_ino; |
|
0 commit comments