54
54
55
55
#if LDC_POSIX
56
56
#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 ());
60
64
}
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 ());
64
71
}
65
72
#elif _WIN32
66
73
#include < windows.h>
@@ -82,10 +89,10 @@ std::error_code widenPath(const llvm::Twine &Path8,
82
89
#endif // LDC_LLVM_VER < 1100
83
90
} // namespace sys
84
91
} // namespace llvm
85
- // Returns true upon error.
92
+
86
93
namespace {
87
94
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) {
89
96
// ===----------------------------------------------------------------------===//
90
97
//
91
98
// 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) {
101
108
102
109
llvm::SmallVector<wchar_t , 128 > wide_from;
103
110
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 ;
108
115
109
116
if (!(*f)(wide_from.begin (), wide_to.begin (), NULL ))
110
- return true ;
117
+ return std::error_code ( GetLastError (), std::system_category ()); ;
111
118
112
- return false ;
119
+ return std::error_code ( 0 , std::system_category ()) ;
113
120
}
114
121
}
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) {
117
124
return createLink (&CreateHardLinkW, to, from);
118
125
}
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) {
121
128
return createLink (&CreateSymbolicLinkW, to, from);
122
129
}
123
130
#endif
@@ -385,11 +392,13 @@ void cacheObjectFile(llvm::StringRef objectFile,
385
392
if (opts::cacheDir.empty ())
386
393
return ;
387
394
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
+ }
393
402
}
394
403
395
404
// To prevent bad cache files, add files to the cache atomically: first copy
@@ -400,24 +409,32 @@ void cacheObjectFile(llvm::StringRef objectFile,
400
409
storeCacheFileName (cacheObjectHash, cacheFile);
401
410
402
411
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 ());
406
418
fatal ();
407
419
}
408
420
409
421
IF_LOG Logger::println (" Copy object file to temp file: %s to %s" ,
410
422
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 ());
414
428
fatal ();
415
429
}
416
430
IF_LOG Logger::println (" Rename temp file to cache file: %s to %s" ,
417
431
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 ());
421
438
fatal ();
422
439
}
423
440
}
@@ -434,37 +451,50 @@ void recoverObjectFile(llvm::StringRef cacheObjectHash,
434
451
case RetrievalMode::Copy: {
435
452
IF_LOG Logger::println (" Copy cached object file: %s -> %s" ,
436
453
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 ());
440
459
fatal ();
441
460
}
442
461
} break ;
443
462
case RetrievalMode::HardLink: {
444
463
IF_LOG Logger::println (" HardLink output to cached object file: %s -> %s" ,
445
464
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 ());
449
472
fatal ();
450
473
}
451
474
} break ;
452
475
case RetrievalMode::AnyLink: {
453
476
IF_LOG Logger::println (" Link output to cached object file: %s -> %s" ,
454
477
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 ());
458
485
fatal ();
459
486
}
460
487
} break ;
461
488
case RetrievalMode::SymLink: {
462
489
IF_LOG Logger::println (" SymLink output to cached object file: %s -> %s" ,
463
490
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 ())) {
465
493
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 ());
468
498
fatal ();
469
499
}
470
500
} break ;
0 commit comments