cmFileLockResult.cxx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmFileLockResult.h"
  4. #include <cerrno>
  5. #include <cstring>
  6. cmFileLockResult cmFileLockResult::MakeOk()
  7. {
  8. return { OK, 0 };
  9. }
  10. cmFileLockResult cmFileLockResult::MakeSystem()
  11. {
  12. #if defined(_WIN32)
  13. const Error lastError = GetLastError();
  14. #else
  15. const Error lastError = errno;
  16. #endif
  17. return { SYSTEM, lastError };
  18. }
  19. cmFileLockResult cmFileLockResult::MakeTimeout()
  20. {
  21. return { TIMEOUT, 0 };
  22. }
  23. cmFileLockResult cmFileLockResult::MakeAlreadyLocked()
  24. {
  25. return { ALREADY_LOCKED, 0 };
  26. }
  27. cmFileLockResult cmFileLockResult::MakeInternal()
  28. {
  29. return { INTERNAL, 0 };
  30. }
  31. cmFileLockResult cmFileLockResult::MakeNoFunction()
  32. {
  33. return { NO_FUNCTION, 0 };
  34. }
  35. bool cmFileLockResult::IsOk() const
  36. {
  37. return this->Type == OK;
  38. }
  39. std::string cmFileLockResult::GetOutputMessage() const
  40. {
  41. switch (this->Type) {
  42. case OK:
  43. return "0";
  44. case SYSTEM:
  45. #if defined(_WIN32)
  46. {
  47. # define WINMSG_BUF_LEN (1024)
  48. char winmsg[WINMSG_BUF_LEN];
  49. DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
  50. if (FormatMessageA(flags, NULL, this->ErrorValue,
  51. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  52. (LPSTR)winmsg, WINMSG_BUF_LEN, NULL)) {
  53. const std::string message = winmsg;
  54. return message;
  55. } else {
  56. return "Internal error (FormatMessageA failed)";
  57. }
  58. }
  59. #else
  60. return strerror(this->ErrorValue);
  61. #endif
  62. case TIMEOUT:
  63. return "Timeout reached";
  64. case ALREADY_LOCKED:
  65. return "File already locked";
  66. case NO_FUNCTION:
  67. return "'GUARD FUNCTION' not used in function definition";
  68. case INTERNAL:
  69. default:
  70. return "Internal error";
  71. }
  72. }
  73. cmFileLockResult::cmFileLockResult(ErrorType typeValue, Error errorValue)
  74. : Type(typeValue)
  75. , ErrorValue(errorValue)
  76. {
  77. }