file: Use 'long' to represent the parsed LOCK TIMEOUT value
Convert the StringToInt helper into a StringToLong helper with a 'long' result type. This will make the helper more useful to other callers that want to use strtol. While at it, also check errno after calling strtol in case the conversion fails with a range error.
This commit is contained in:
parent
356f7cf445
commit
97841dad2b
|
@ -3521,7 +3521,7 @@ bool cmFileCommand::HandleLockCommand(
|
|||
};
|
||||
Guard guard = GUARD_PROCESS;
|
||||
std::string resultVariable;
|
||||
unsigned timeout = static_cast<unsigned>(-1);
|
||||
unsigned long timeout = static_cast<unsigned long>(-1);
|
||||
|
||||
// Parse arguments
|
||||
if(args.size() < 2)
|
||||
|
@ -3597,15 +3597,16 @@ bool cmFileCommand::HandleLockCommand(
|
|||
"expected timeout value after TIMEOUT");
|
||||
return false;
|
||||
}
|
||||
int scanned;
|
||||
if(!cmSystemTools::StringToInt(args[i].c_str(), &scanned) || scanned < 0)
|
||||
long scanned;
|
||||
if(!cmSystemTools::StringToLong(args[i].c_str(), &scanned)
|
||||
|| scanned < 0)
|
||||
{
|
||||
cmOStringStream e;
|
||||
e << "TIMEOUT value \"" << args[i] << "\" is not an unsigned integer.";
|
||||
this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
|
||||
return false;
|
||||
}
|
||||
timeout = static_cast<unsigned>(scanned);
|
||||
timeout = static_cast<unsigned long>(scanned);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -28,7 +28,7 @@ cmFileLock::~cmFileLock()
|
|||
}
|
||||
|
||||
cmFileLockResult cmFileLock::Lock(
|
||||
const std::string& filename, unsigned timeout)
|
||||
const std::string& filename, unsigned long timeout)
|
||||
{
|
||||
if (filename.empty())
|
||||
{
|
||||
|
@ -48,7 +48,7 @@ cmFileLockResult cmFileLock::Lock(
|
|||
cmFileLockResult result = this->OpenFile();
|
||||
if (result.IsOk())
|
||||
{
|
||||
if (timeout == static_cast<unsigned>(-1))
|
||||
if (timeout == static_cast<unsigned long>(-1))
|
||||
{
|
||||
result = this->LockWithoutTimeout();
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ class cmFileLock
|
|||
* @brief Lock the file.
|
||||
* @param timeoutSec Lock timeout. If -1 try until success or fatal error.
|
||||
*/
|
||||
cmFileLockResult Lock(const std::string& filename, unsigned timeoutSec);
|
||||
cmFileLockResult Lock(const std::string& filename, unsigned long timeoutSec);
|
||||
|
||||
/**
|
||||
* @brief Unlock the file.
|
||||
|
@ -57,7 +57,7 @@ class cmFileLock
|
|||
|
||||
cmFileLockResult OpenFile();
|
||||
cmFileLockResult LockWithoutTimeout();
|
||||
cmFileLockResult LockWithTimeout(unsigned timeoutSec);
|
||||
cmFileLockResult LockWithTimeout(unsigned long timeoutSec);
|
||||
|
||||
#if defined(_WIN32)
|
||||
typedef HANDLE FileId;
|
||||
|
|
|
@ -60,7 +60,7 @@ void cmFileLockPool::PopFileScope()
|
|||
}
|
||||
|
||||
cmFileLockResult cmFileLockPool::LockFunctionScope(
|
||||
const std::string& filename, unsigned timeoutSec)
|
||||
const std::string& filename, unsigned long timeoutSec)
|
||||
{
|
||||
if (this->IsAlreadyLocked(filename))
|
||||
{
|
||||
|
@ -74,7 +74,7 @@ cmFileLockResult cmFileLockPool::LockFunctionScope(
|
|||
}
|
||||
|
||||
cmFileLockResult cmFileLockPool::LockFileScope(
|
||||
const std::string& filename, unsigned timeoutSec)
|
||||
const std::string& filename, unsigned long timeoutSec)
|
||||
{
|
||||
if (this->IsAlreadyLocked(filename))
|
||||
{
|
||||
|
@ -85,7 +85,7 @@ cmFileLockResult cmFileLockPool::LockFileScope(
|
|||
}
|
||||
|
||||
cmFileLockResult cmFileLockPool::LockProcessScope(
|
||||
const std::string& filename, unsigned timeoutSec)
|
||||
const std::string& filename, unsigned long timeoutSec)
|
||||
{
|
||||
if (this->IsAlreadyLocked(filename))
|
||||
{
|
||||
|
@ -155,7 +155,7 @@ cmFileLockPool::ScopePool::~ScopePool()
|
|||
}
|
||||
|
||||
cmFileLockResult cmFileLockPool::ScopePool::Lock(
|
||||
const std::string& filename, unsigned timeoutSec)
|
||||
const std::string& filename, unsigned long timeoutSec)
|
||||
{
|
||||
cmFileLock *lock = new cmFileLock();
|
||||
const cmFileLockResult result = lock->Lock(filename, timeoutSec);
|
||||
|
|
|
@ -45,13 +45,13 @@ class cmFileLockPool
|
|||
* @param timeoutSec Lock timeout. If -1 try until success or fatal error.
|
||||
*/
|
||||
cmFileLockResult LockFunctionScope(
|
||||
const std::string& filename, unsigned timeoutSec
|
||||
const std::string& filename, unsigned long timeoutSec
|
||||
);
|
||||
cmFileLockResult LockFileScope(
|
||||
const std::string& filename, unsigned timeoutSec
|
||||
const std::string& filename, unsigned long timeoutSec
|
||||
);
|
||||
cmFileLockResult LockProcessScope(
|
||||
const std::string& filename, unsigned timeoutSec
|
||||
const std::string& filename, unsigned long timeoutSec
|
||||
);
|
||||
//@}
|
||||
|
||||
|
@ -72,7 +72,9 @@ class cmFileLockPool
|
|||
ScopePool();
|
||||
~ScopePool();
|
||||
|
||||
cmFileLockResult Lock(const std::string& filename, unsigned timeoutSec);
|
||||
cmFileLockResult Lock(
|
||||
const std::string& filename, unsigned long timeoutSec
|
||||
);
|
||||
cmFileLockResult Release(const std::string& filename);
|
||||
bool IsAlreadyLocked(const std::string& filename) const;
|
||||
|
||||
|
|
|
@ -66,7 +66,7 @@ cmFileLockResult cmFileLock::LockWithoutTimeout()
|
|||
}
|
||||
}
|
||||
|
||||
cmFileLockResult cmFileLock::LockWithTimeout(unsigned seconds)
|
||||
cmFileLockResult cmFileLock::LockWithTimeout(unsigned long seconds)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
|
|
|
@ -86,7 +86,7 @@ cmFileLockResult cmFileLock::LockWithoutTimeout()
|
|||
}
|
||||
}
|
||||
|
||||
cmFileLockResult cmFileLock::LockWithTimeout(unsigned seconds)
|
||||
cmFileLockResult cmFileLock::LockWithTimeout(unsigned long seconds)
|
||||
{
|
||||
const DWORD flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY;
|
||||
while (true)
|
||||
|
|
|
@ -2925,9 +2925,10 @@ std::vector<std::string> cmSystemTools::tokenize(const std::string& str,
|
|||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool cmSystemTools::StringToInt(const char* str, int* value)
|
||||
bool cmSystemTools::StringToLong(const char* str, long* value)
|
||||
{
|
||||
errno = 0;
|
||||
char *endp;
|
||||
*value = static_cast<int>(strtol(str, &endp, 10));
|
||||
return (*endp == '\0') && (endp != str);
|
||||
*value = strtol(str, &endp, 10);
|
||||
return (*endp == '\0') && (endp != str) && (errno == 0);
|
||||
}
|
||||
|
|
|
@ -458,8 +458,8 @@ public:
|
|||
static std::vector<std::string> tokenize(const std::string& str,
|
||||
const std::string& sep);
|
||||
|
||||
/** Convert string to int. Expected that the whole string is an integer */
|
||||
static bool StringToInt(const char* str, int* value);
|
||||
/** Convert string to long. Expected that the whole string is an integer */
|
||||
static bool StringToLong(const char* str, long* value);
|
||||
|
||||
#ifdef _WIN32
|
||||
struct WindowsFileRetry
|
||||
|
|
Loading…
Reference in New Issue