Coverage Report

Created: 2026-02-09 06:05

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