#include "file-lock.h"
+#include <dpl/errno_string.h>
+
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
FileLock::FileLock(const char* const file)
{
- char errbuf[512] = {0, };
-
// Open lock file
m_lockFd = TEMP_FAILURE_RETRY(creat(file, 0644));
if (m_lockFd == -1) {
- throw io_exception("Cannot open lock file. Errno: ", strerror_r(errno, errbuf, sizeof(errbuf)));
+ throw io_exception("Cannot open lock file. Errno: ", GetErrnoString(errno));
}
if (-1 == lockf(m_lockFd, F_TLOCK, 0)) {
if (errno == EACCES || errno == EAGAIN)
throw io_exception("Can't acquire lock. Another instance must be running.");
else
- throw io_exception("Can't acquire lock. Errno: ", strerror_r(errno, errbuf, sizeof(errbuf)));
+ throw io_exception("Can't acquire lock. Errno: ", GetErrnoString(errno));
}
std::string pid = std::to_string(getpid());
ssize_t written = TEMP_FAILURE_RETRY(write(m_lockFd, pid.c_str(), pid.size()));
if (-1 == written || static_cast<ssize_t>(pid.size()) > written)
- throw io_exception("Can't write file lock. Errno: ", strerror_r(errno, errbuf, sizeof(errbuf)));
+ throw io_exception("Can't write file lock. Errno: ", GetErrnoString(errno));
int ret = fsync(m_lockFd);
if (-1 == ret)
- throw io_exception("Fsync failed. Errno: ", strerror_r(errno, errbuf, sizeof(errbuf)));
+ throw io_exception("Fsync failed. Errno: ", GetErrnoString(errno));
}
FileLock::~FileLock()