Skip to content

Commit 41de641

Browse files
authored
Output error code and message string upon cache file errors. (#3900)
This makes the error message look like this, for example: ``` Error: Could not create name of temporary file in the cache (errno 13: Permission denied) \---------------------------/ this is the new part ```
1 parent 63d18b4 commit 41de641

File tree

1 file changed

+74
-44
lines changed

1 file changed

+74
-44
lines changed

driver/cache.cpp

Lines changed: 74 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,20 @@
5454

5555
#if LDC_POSIX
5656
#include <unistd.h>
57-
// Returns true upon error.
58-
static bool createHardLink(const char *to, const char *from) {
59-
return link(to, from) == -1;
57+
#include <errno.h>
58+
59+
static std::error_code createHardLink(const char *to, const char *from) {
60+
if (link(to, from) == 0)
61+
return std::error_code(0, std::system_category());
62+
else
63+
return std::error_code(errno, std::system_category());
6064
}
61-
// Returns true upon error.
62-
static bool createSymLink(const char *to, const char *from) {
63-
return symlink(to, from) == -1;
65+
66+
static std::error_code createSymLink(const char *to, const char *from) {
67+
if (symlink(to, from) == 0)
68+
return std::error_code(0, std::system_category());
69+
else
70+
return std::error_code(errno, std::system_category());
6471
}
6572
#elif _WIN32
6673
#include <windows.h>
@@ -82,10 +89,10 @@ std::error_code widenPath(const llvm::Twine &Path8,
8289
#endif // LDC_LLVM_VER < 1100
8390
} // namespace sys
8491
} // namespace llvm
85-
// Returns true upon error.
92+
8693
namespace {
8794
template <typename FType>
88-
bool createLink(FType f, const char *to, const char *from) {
95+
std::error_code createLink(FType f, const char *to, const char *from) {
8996
//===----------------------------------------------------------------------===//
9097
//
9198
// Code copied from LLVM 3.9 llvm/Support/Windows/Path.inc, distributed under
@@ -101,23 +108,23 @@ bool createLink(FType f, const char *to, const char *from) {
101108

102109
llvm::SmallVector<wchar_t, 128> wide_from;
103110
llvm::SmallVector<wchar_t, 128> wide_to;
104-
if (widenPath(from, wide_from))
105-
return true;
106-
if (widenPath(to, wide_to))
107-
return true;
111+
if (auto errorcode = widenPath(from, wide_from))
112+
return errorcode;
113+
if (auto errorcode = widenPath(to, wide_to))
114+
return errorcode;
108115

109116
if (!(*f)(wide_from.begin(), wide_to.begin(), NULL))
110-
return true;
117+
return std::error_code(GetLastError(), std::system_category());;
111118

112-
return false;
119+
return std::error_code(0, std::system_category());
113120
}
114121
}
115-
// Returns true upon error.
116-
static bool createHardLink(const char *to, const char *from) {
122+
123+
static std::error_code createHardLink(const char *to, const char *from) {
117124
return createLink(&CreateHardLinkW, to, from);
118125
}
119-
// Returns true upon error.
120-
static bool createSymLink(const char *to, const char *from) {
126+
127+
static std::error_code createSymLink(const char *to, const char *from) {
121128
return createLink(&CreateSymbolicLinkW, to, from);
122129
}
123130
#endif
@@ -385,11 +392,13 @@ void cacheObjectFile(llvm::StringRef objectFile,
385392
if (opts::cacheDir.empty())
386393
return;
387394

388-
if (!llvm::sys::fs::exists(opts::cacheDir) &&
389-
llvm::sys::fs::create_directories(opts::cacheDir)) {
390-
error(Loc(), "Unable to create cache directory: %s",
391-
opts::cacheDir.c_str());
392-
fatal();
395+
if (!llvm::sys::fs::exists(opts::cacheDir)) {
396+
if (auto errorcode = llvm::sys::fs::create_directories(opts::cacheDir)) {
397+
error(Loc(), "Unable to create cache directory: %s (errno %d: %s)",
398+
opts::cacheDir.c_str(), errorcode.value(),
399+
errorcode.message().c_str());
400+
fatal();
401+
}
393402
}
394403

395404
// To prevent bad cache files, add files to the cache atomically: first copy
@@ -400,24 +409,32 @@ void cacheObjectFile(llvm::StringRef objectFile,
400409
storeCacheFileName(cacheObjectHash, cacheFile);
401410

402411
llvm::SmallString<128> tempFile;
403-
if (llvm::sys::fs::createUniqueFile(llvm::Twine(cacheFile) + ".tmp%%%%%%%",
404-
tempFile)) {
405-
error(Loc(), "Could not create name of temporary file in the cache.");
412+
if (auto errorcode = llvm::sys::fs::createUniqueFile(
413+
llvm::Twine(cacheFile) + ".tmp%%%%%%%", tempFile)) {
414+
error(
415+
Loc(),
416+
"Could not create name of temporary file in the cache (errno %d: %s)",
417+
errorcode.value(), errorcode.message().c_str());
406418
fatal();
407419
}
408420

409421
IF_LOG Logger::println("Copy object file to temp file: %s to %s",
410422
objectFile.str().c_str(), tempFile.c_str());
411-
if (llvm::sys::fs::copy_file(objectFile, tempFile.c_str())) {
412-
error(Loc(), "Failed to copy object file to cache: %s to %s",
413-
objectFile.str().c_str(), tempFile.c_str());
423+
if (auto errorcode = llvm::sys::fs::copy_file(objectFile, tempFile.c_str())) {
424+
error(Loc(),
425+
"Failed to copy object file to cache: %s to %s (errno %d: %s)",
426+
objectFile.str().c_str(), tempFile.c_str(), errorcode.value(),
427+
errorcode.message().c_str());
414428
fatal();
415429
}
416430
IF_LOG Logger::println("Rename temp file to cache file: %s to %s",
417431
tempFile.c_str(), cacheFile.c_str());
418-
if (llvm::sys::fs::rename(tempFile.c_str(), cacheFile.c_str())) {
419-
error(Loc(), "Failed to rename temp file to cache file: %s to %s",
420-
tempFile.c_str(), cacheFile.c_str());
432+
if (auto errorcode =
433+
llvm::sys::fs::rename(tempFile.c_str(), cacheFile.c_str())) {
434+
error(Loc(),
435+
"Failed to rename temp file to cache file: %s to %s (errno %d: %s)",
436+
tempFile.c_str(), cacheFile.c_str(), errorcode.value(),
437+
errorcode.message().c_str());
421438
fatal();
422439
}
423440
}
@@ -434,37 +451,50 @@ void recoverObjectFile(llvm::StringRef cacheObjectHash,
434451
case RetrievalMode::Copy: {
435452
IF_LOG Logger::println("Copy cached object file: %s -> %s",
436453
cacheFile.c_str(), objectFile.str().c_str());
437-
if (llvm::sys::fs::copy_file(cacheFile.c_str(), objectFile)) {
438-
error(Loc(), "Failed to copy the cached file: %s -> %s",
439-
cacheFile.c_str(), objectFile.str().c_str());
454+
if (auto errorcode =
455+
llvm::sys::fs::copy_file(cacheFile.c_str(), objectFile)) {
456+
error(Loc(), "Failed to copy the cached file: %s -> %s (errno %d: %s)",
457+
cacheFile.c_str(), objectFile.str().c_str(), errorcode.value(),
458+
errorcode.message().c_str());
440459
fatal();
441460
}
442461
} break;
443462
case RetrievalMode::HardLink: {
444463
IF_LOG Logger::println("HardLink output to cached object file: %s -> %s",
445464
objectFile.str().c_str(), cacheFile.c_str());
446-
if (createHardLink(cacheFile.c_str(), objectFile.str().c_str())) {
447-
error(Loc(), "Failed to create a hard link to the cached file: %s -> %s",
448-
cacheFile.c_str(), objectFile.str().c_str());
465+
if (auto errorcode =
466+
createHardLink(cacheFile.c_str(), objectFile.str().c_str())) {
467+
error(Loc(),
468+
"Failed to create a hard link to the cached file: %s -> %s (errno "
469+
"%d: %s)",
470+
cacheFile.c_str(), objectFile.str().c_str(), errorcode.value(),
471+
errorcode.message().c_str());
449472
fatal();
450473
}
451474
} break;
452475
case RetrievalMode::AnyLink: {
453476
IF_LOG Logger::println("Link output to cached object file: %s -> %s",
454477
objectFile.str().c_str(), cacheFile.c_str());
455-
if (llvm::sys::fs::create_link(cacheFile.c_str(), objectFile)) {
456-
error(Loc(), "Failed to create a link to the cached file: %s -> %s",
457-
cacheFile.c_str(), objectFile.str().c_str());
478+
if (auto errorcode =
479+
llvm::sys::fs::create_link(cacheFile.c_str(), objectFile)) {
480+
error(
481+
Loc(),
482+
"Failed to create a link to the cached file: %s -> %s (errno %d: %s)",
483+
cacheFile.c_str(), objectFile.str().c_str(), errorcode.value(),
484+
errorcode.message().c_str());
458485
fatal();
459486
}
460487
} break;
461488
case RetrievalMode::SymLink: {
462489
IF_LOG Logger::println("SymLink output to cached object file: %s -> %s",
463490
objectFile.str().c_str(), cacheFile.c_str());
464-
if (createSymLink(cacheFile.c_str(), objectFile.str().c_str())) {
491+
if (auto errorcode =
492+
createSymLink(cacheFile.c_str(), objectFile.str().c_str())) {
465493
error(Loc(),
466-
"Failed to create a symbolic link to the cached file: %s -> %s",
467-
cacheFile.c_str(), objectFile.str().c_str());
494+
"Failed to create a symbolic link to the cached file: %s -> %s "
495+
"(errno %d: %s)",
496+
cacheFile.c_str(), objectFile.str().c_str(), errorcode.value(),
497+
errorcode.message().c_str());
468498
fatal();
469499
}
470500
} break;

0 commit comments

Comments
 (0)