cmFileLockResult.cxx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2014 Ruslan Baratov
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmFileLockResult.h"
  11. #include <errno.h>
  12. #include <string.h>
  13. cmFileLockResult cmFileLockResult::MakeOk()
  14. {
  15. return cmFileLockResult(OK, 0);
  16. }
  17. cmFileLockResult cmFileLockResult::MakeSystem()
  18. {
  19. #if defined(_WIN32)
  20. const Error lastError = GetLastError();
  21. #else
  22. const Error lastError = errno;
  23. #endif
  24. return cmFileLockResult(SYSTEM, lastError);
  25. }
  26. cmFileLockResult cmFileLockResult::MakeTimeout()
  27. {
  28. return cmFileLockResult(TIMEOUT, 0);
  29. }
  30. cmFileLockResult cmFileLockResult::MakeAlreadyLocked()
  31. {
  32. return cmFileLockResult(ALREADY_LOCKED, 0);
  33. }
  34. cmFileLockResult cmFileLockResult::MakeInternal()
  35. {
  36. return cmFileLockResult(INTERNAL, 0);
  37. }
  38. cmFileLockResult cmFileLockResult::MakeNoFunction()
  39. {
  40. return cmFileLockResult(NO_FUNCTION, 0);
  41. }
  42. bool cmFileLockResult::IsOk() const
  43. {
  44. return this->Type == OK;
  45. }
  46. std::string cmFileLockResult::GetOutputMessage() const
  47. {
  48. switch (this->Type) {
  49. case OK:
  50. return "0";
  51. case SYSTEM:
  52. #if defined(_WIN32)
  53. {
  54. char* errorText = NULL;
  55. // http://stackoverflow.com/a/455533/2288008
  56. DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM |
  57. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS;
  58. ::FormatMessageA(flags, NULL, this->ErrorValue,
  59. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  60. (LPSTR)&errorText, 0, NULL);
  61. if (errorText != NULL) {
  62. const std::string message = errorText;
  63. ::LocalFree(errorText);
  64. return message;
  65. } else {
  66. return "Internal error (FormatMessageA failed)";
  67. }
  68. }
  69. #else
  70. return strerror(this->ErrorValue);
  71. #endif
  72. case TIMEOUT:
  73. return "Timeout reached";
  74. case ALREADY_LOCKED:
  75. return "File already locked";
  76. case NO_FUNCTION:
  77. return "'GUARD FUNCTION' not used in function definition";
  78. case INTERNAL:
  79. default:
  80. return "Internal error";
  81. }
  82. }
  83. cmFileLockResult::cmFileLockResult(ErrorType typeValue, Error errorValue)
  84. : Type(typeValue)
  85. , ErrorValue(errorValue)
  86. {
  87. }