Index: include/llvm/Support/LockFileManager.h =================================================================== --- include/llvm/Support/LockFileManager.h +++ include/llvm/Support/LockFileManager.h @@ -57,6 +57,7 @@ Optional > Owner; Optional Error; + std::string ErrorDiagMsg; LockFileManager(const LockFileManager &) = delete; LockFileManager &operator=(const LockFileManager &) = delete; @@ -82,6 +83,15 @@ /// \brief Remove the lock file. This may delete a different lock file than /// the one previously read if there is a race. std::error_code unsafeRemoveLockFile(); + + /// \brief Get error message + std::string getErrorMessage() const; + + /// \brief Set error and error message + void setError(std::error_code &EC, StringRef ErrorMsg = "") { + Error = EC; + ErrorDiagMsg = ErrorMsg.str(); + } }; } // end namespace llvm Index: lib/Support/LockFileManager.cpp =================================================================== --- lib/Support/LockFileManager.cpp +++ lib/Support/LockFileManager.cpp @@ -144,7 +144,9 @@ { this->FileName = FileName; if (std::error_code EC = sys::fs::make_absolute(this->FileName)) { - Error = EC; + std::string S("failed to obtain absolute path for "); + S.append(this->FileName.str()); + setError(EC, S); return; } LockFileName = this->FileName; @@ -161,7 +163,9 @@ int UniqueLockFileID; if (std::error_code EC = sys::fs::createUniqueFile( UniqueLockFileName, UniqueLockFileID, UniqueLockFileName)) { - Error = EC; + std::string S("failed to create unique file "); + S.append(UniqueLockFileName.str()); + setError(EC, S); return; } @@ -169,7 +173,7 @@ { SmallString<256> HostID; if (auto EC = getHostID(HostID)) { - Error = EC; + setError(EC, "failed to get host id"); return; } @@ -185,7 +189,8 @@ if (Out.has_error()) { // We failed to write out PID, so make up an excuse, remove the // unique lock file, and fail. - Error = make_error_code(errc::no_space_on_device); + auto EC = make_error_code(errc::no_space_on_device); + setError(EC); sys::fs::remove(UniqueLockFileName); return; } @@ -205,7 +210,11 @@ } if (EC != errc::file_exists) { - Error = EC; + std::string S("failed to create link "); + raw_string_ostream OSS(S); + OSS << LockFileName.str() << " to " << UniqueLockFileName.str(); + OSS.flush(); + setError(EC, S); return; } @@ -226,7 +235,9 @@ // There is a lock file that nobody owns; try to clean it up and get // ownership. if ((EC = sys::fs::remove(LockFileName))) { - Error = EC; + std::string S("failed to remove lockfile "); + S.append(UniqueLockFileName.str()); + setError(EC, S); return; } } @@ -242,6 +253,19 @@ return LFS_Owned; } +std::string LockFileManager::getErrorMessage() const { + if (Error) { + std::string Str(ErrorDiagMsg); + std::string ErrCodeMsg = Error->message(); + raw_string_ostream OSS(Str); + if (!ErrCodeMsg.empty()) + OSS << ": " << Error->message(); + OSS.flush(); + return Str; + } + return ""; +} + LockFileManager::~LockFileManager() { if (getState() != LFS_Owned) return;