cmake/Source/cmFileLockResult.cxx

88 lines
1.9 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2015-04-27 22:25:09 +02:00
#include "cmFileLockResult.h"
2020-02-01 23:06:01 +01:00
#include <cerrno>
#include <cstring>
2015-04-27 22:25:09 +02:00
2018-01-26 17:06:56 +01:00
#define WINMSG_BUF_LEN (1024)
2015-04-27 22:25:09 +02:00
cmFileLockResult cmFileLockResult::MakeOk()
{
2020-02-01 23:06:01 +01:00
return { OK, 0 };
2015-04-27 22:25:09 +02:00
}
cmFileLockResult cmFileLockResult::MakeSystem()
{
#if defined(_WIN32)
const Error lastError = GetLastError();
#else
const Error lastError = errno;
#endif
2020-02-01 23:06:01 +01:00
return { SYSTEM, lastError };
2015-04-27 22:25:09 +02:00
}
cmFileLockResult cmFileLockResult::MakeTimeout()
{
2020-02-01 23:06:01 +01:00
return { TIMEOUT, 0 };
2015-04-27 22:25:09 +02:00
}
cmFileLockResult cmFileLockResult::MakeAlreadyLocked()
{
2020-02-01 23:06:01 +01:00
return { ALREADY_LOCKED, 0 };
2015-04-27 22:25:09 +02:00
}
cmFileLockResult cmFileLockResult::MakeInternal()
{
2020-02-01 23:06:01 +01:00
return { INTERNAL, 0 };
2015-04-27 22:25:09 +02:00
}
cmFileLockResult cmFileLockResult::MakeNoFunction()
{
2020-02-01 23:06:01 +01:00
return { NO_FUNCTION, 0 };
2015-04-27 22:25:09 +02:00
}
bool cmFileLockResult::IsOk() const
{
return this->Type == OK;
}
std::string cmFileLockResult::GetOutputMessage() const
{
2016-07-09 11:21:54 +02:00
switch (this->Type) {
2015-04-27 22:25:09 +02:00
case OK:
return "0";
case SYSTEM:
#if defined(_WIN32)
2016-07-09 11:21:54 +02:00
{
2018-01-26 17:06:56 +01:00
char winmsg[WINMSG_BUF_LEN];
DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
if (FormatMessageA(flags, NULL, this->ErrorValue,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)winmsg, WINMSG_BUF_LEN, NULL)) {
const std::string message = winmsg;
2015-04-27 22:25:09 +02:00
return message;
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
return "Internal error (FormatMessageA failed)";
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
#else
return strerror(this->ErrorValue);
#endif
case TIMEOUT:
return "Timeout reached";
case ALREADY_LOCKED:
return "File already locked";
case NO_FUNCTION:
return "'GUARD FUNCTION' not used in function definition";
case INTERNAL:
default:
return "Internal error";
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}
2016-07-09 11:21:54 +02:00
cmFileLockResult::cmFileLockResult(ErrorType typeValue, Error errorValue)
: Type(typeValue)
, ErrorValue(errorValue)
2015-04-27 22:25:09 +02:00
{
}