llvm.org GIT mirror llvm / 909307b
[LockFileManager] Improve error output by using better error messages This is currently used by clang to lock access to modules; improve the error message so that clang can use better output messages from locking error issues. rdar://problem/26529101 Differential Review: http://reviews.llvm.org/D20942 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@271755 91177308-0d34-0410-b5e6-96231b3b80d8 Bruno Cardoso Lopes 4 years ago
2 changed file(s) with 41 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
5656
5757 Optional > Owner;
5858 Optional Error;
59 std::string ErrorDiagMsg;
5960
6061 LockFileManager(const LockFileManager &) = delete;
6162 LockFileManager &operator=(const LockFileManager &) = delete;
8182 /// \brief Remove the lock file. This may delete a different lock file than
8283 /// the one previously read if there is a race.
8384 std::error_code unsafeRemoveLockFile();
85
86 /// \brief Get error message, or "" if there is no error.
87 std::string getErrorMessage() const;
88
89 /// \brief Set error and error message
90 void setError(std::error_code &EC, StringRef ErrorMsg = "") {
91 Error = EC;
92 ErrorDiagMsg = ErrorMsg.str();
93 }
8494 };
8595
8696 } // end namespace llvm
143143 {
144144 this->FileName = FileName;
145145 if (std::error_code EC = sys::fs::make_absolute(this->FileName)) {
146 Error = EC;
146 std::string S("failed to obtain absolute path for ");
147 S.append(this->FileName.str());
148 setError(EC, S);
147149 return;
148150 }
149151 LockFileName = this->FileName;
160162 int UniqueLockFileID;
161163 if (std::error_code EC = sys::fs::createUniqueFile(
162164 UniqueLockFileName, UniqueLockFileID, UniqueLockFileName)) {
163 Error = EC;
165 std::string S("failed to create unique file ");
166 S.append(UniqueLockFileName.str());
167 setError(EC, S);
164168 return;
165169 }
166170
168172 {
169173 SmallString<256> HostID;
170174 if (auto EC = getHostID(HostID)) {
171 Error = EC;
175 setError(EC, "failed to get host id");
172176 return;
173177 }
174178
184188 if (Out.has_error()) {
185189 // We failed to write out PID, so make up an excuse, remove the
186190 // unique lock file, and fail.
187 Error = make_error_code(errc::no_space_on_device);
191 auto EC = make_error_code(errc::no_space_on_device);
192 std::string S("failed to write to ");
193 S.append(UniqueLockFileName.str());
194 setError(EC, S);
188195 sys::fs::remove(UniqueLockFileName);
189196 return;
190197 }
204211 }
205212
206213 if (EC != errc::file_exists) {
207 Error = EC;
214 std::string S("failed to create link ");
215 raw_string_ostream OSS(S);
216 OSS << LockFileName.str() << " to " << UniqueLockFileName.str();
217 setError(EC, OSS.str());
208218 return;
209219 }
210220
225235 // There is a lock file that nobody owns; try to clean it up and get
226236 // ownership.
227237 if ((EC = sys::fs::remove(LockFileName))) {
228 Error = EC;
238 std::string S("failed to remove lockfile ");
239 S.append(UniqueLockFileName.str());
240 setError(EC, S);
229241 return;
230242 }
231243 }
239251 return LFS_Error;
240252
241253 return LFS_Owned;
254 }
255
256 std::string LockFileManager::getErrorMessage() const {
257 if (Error) {
258 std::string Str(ErrorDiagMsg);
259 std::string ErrCodeMsg = Error->message();
260 raw_string_ostream OSS(Str);
261 if (!ErrCodeMsg.empty())
262 OSS << ": " << Error->message();
263 OSS.flush();
264 return Str;
265 }
266 return "";
242267 }
243268
244269 LockFileManager::~LockFileManager() {