Optional<std::pair<std::string, int> > Owner;
Optional<std::error_code> Error;
+ std::string ErrorDiagMsg;
LockFileManager(const LockFileManager &) = delete;
LockFileManager &operator=(const LockFileManager &) = delete;
/// \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, or "" if there is no error.
+ 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
{
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;
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;
}
{
SmallString<256> HostID;
if (auto EC = getHostID(HostID)) {
- Error = EC;
+ setError(EC, "failed to get host id");
return;
}
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);
+ std::string S("failed to write to ");
+ S.append(UniqueLockFileName.str());
+ setError(EC, S);
sys::fs::remove(UniqueLockFileName);
return;
}
}
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();
+ setError(EC, OSS.str());
return;
}
// 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;
}
}
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;