More details for scan error reports #676

This commit is contained in:
emeric
2025-05-26 23:33:52 +02:00
parent 6d6ea4920b
commit 571c0bc5f4
86 changed files with 1794 additions and 950 deletions
+10 -6
View File
@@ -22,12 +22,12 @@
#include <algorithm>
#include <cassert>
#include <fstream>
#include <system_error>
#include <archive.h>
#include <archive_entry.h>
#include "core/ILogger.hpp"
#include "core/String.hpp"
namespace lms::zip
{
@@ -43,9 +43,13 @@ namespace lms::zip
: Exception{ "File '" + p.string() + "': " + std::string{ message } }
{
}
};
FileException(const std::filesystem::path& p, std::string_view message, int err)
: Exception{ "File '" + p.string() + "': " + std::string{ message } + ": " + core::stringUtils::systemErrorToString(err) }
class FileStdException : public FileException
{
public:
FileStdException(const std::filesystem::path& p, std::string_view message, int err)
: FileException{ p, std::string{ message } + "': " + std::error_code{ err, std::generic_category() }.message() }
{
}
};
@@ -240,7 +244,7 @@ namespace lms::zip
std::ifstream ifs{ _currentEntry->filePath, std::ios_base::binary };
if (!ifs)
throw FileException{ _currentEntry->filePath, "cannot open file", errno };
throw FileStdException{ _currentEntry->filePath, "cannot open file", errno };
ifs.seekg(0, std::ios::end);
const std::uint64_t fileSize{ static_cast<std::uint64_t>(ifs.tellg()) };
@@ -254,10 +258,10 @@ namespace lms::zip
// read from file
if (!ifs.seekg(_currentEntryOffset, std::ios::beg))
throw FileException{ _currentEntry->filePath, "seek failed", errno };
throw FileStdException{ _currentEntry->filePath, "seek failed", errno };
if (!ifs.read(reinterpret_cast<char*>(_readBuffer.data()), bytesToRead))
throw FileException{ _currentEntry->filePath, "read failed", errno };
throw FileStdException{ _currentEntry->filePath, "read failed", errno };
const std::uint64_t actualBytesRead{ static_cast<std::uint64_t>(ifs.gcount()) };
+17 -14
View File
@@ -25,6 +25,7 @@
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <system_error>
#include <unistd.h>
#include <algorithm>
@@ -35,7 +36,6 @@
#include <boost/asio/read.hpp>
#include "core/ILogger.hpp"
#include "core/String.hpp"
namespace lms::core
{
@@ -44,8 +44,8 @@ namespace lms::core
class SystemException : public ChildProcessException
{
public:
SystemException(int err, const std::string& errMsg)
: ChildProcessException{ errMsg + ": " + stringUtils::systemErrorToString(err) }
SystemException(std::error_code err, const std::string& errMsg)
: ChildProcessException{ errMsg + ": " + err.message() }
{
}
@@ -69,20 +69,20 @@ namespace lms::core
// Use 'pipe' instead of 'pipe2', more portable
if (pipe(pipefd) < 0)
throw SystemException{ errno, "pipe failed!" };
throw SystemException{ std::error_code{ errno, std::generic_category() }, "pipe failed!" };
// Manually set the O_NONBLOCK and O_CLOEXEC flags for both ends of the pipe
if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) == -1)
throw SystemException{ errno, "fcntl failed to set O_NONBLOCK!" };
throw SystemException{ std::error_code{ errno, std::generic_category() }, "fcntl failed to set O_NONBLOCK!" };
if (fcntl(pipefd[1], F_SETFL, O_NONBLOCK) == -1)
throw SystemException{ errno, "fcntl failed to set O_NONBLOCK!" };
throw SystemException{ std::error_code{ errno, std::generic_category() }, "fcntl failed to set O_NONBLOCK!" };
if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) == -1)
throw SystemException{ errno, "fcntl failed to set FD_CLOEXEC!" };
throw SystemException{ std::error_code{ errno, std::generic_category() }, "fcntl failed to set FD_CLOEXEC!" };
if (fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) == -1)
throw SystemException{ errno, "fcntl failed to set FD_CLOEXEC!" };
throw SystemException{ std::error_code{ errno, std::generic_category() }, "fcntl failed to set FD_CLOEXEC!" };
#if defined(__linux__) && defined(F_SETPIPE_SZ)
for (const int fd : { pipefd[0], pipefd[1] })
@@ -94,7 +94,7 @@ namespace lms::core
if (pipeSizeRes == -1)
{
const int err{ errno };
LMS_LOG(CHILDPROCESS, DEBUG, "F_GETPIPE_SZ failed: " << stringUtils::systemErrorToString(err));
LMS_LOG(CHILDPROCESS, DEBUG, "F_GETPIPE_SZ failed: " << (std::error_code{ err, std::generic_category() }.message()));
}
else
{
@@ -103,10 +103,10 @@ namespace lms::core
#endif
if (currentPipeSize < targetPipeSize)
{
if (fcntl(fd, F_SETPIPE_SZ, targetPipeSize) == -1)
if (::fcntl(fd, F_SETPIPE_SZ, targetPipeSize) == -1)
{
const int err{ errno };
LMS_LOG(CHILDPROCESS, DEBUG, "F_SETPIPE_SZ failed: " << stringUtils::systemErrorToString(err));
LMS_LOG(CHILDPROCESS, DEBUG, "F_SETPIPE_SZ failed: " << (std::error_code{ err, std::generic_category() }.message()));
}
}
}
@@ -114,7 +114,7 @@ namespace lms::core
int res{ fork() };
if (res == -1)
throw SystemException{ errno, "fork failed!" };
throw SystemException{ std::error_code{ errno, std::generic_category() }, "fork failed!" };
if (res == 0) // CHILD
{
@@ -168,7 +168,10 @@ namespace lms::core
// process may already have finished
LMS_LOG(CHILDPROCESS, DEBUG, "Killing child process...");
if (::kill(_childPID, SIGKILL) == -1)
LMS_LOG(CHILDPROCESS, DEBUG, "Kill failed: " << stringUtils::systemErrorToString(errno));
{
const int err{ errno };
LMS_LOG(CHILDPROCESS, DEBUG, "Kill failed: " << (std::error_code{ err, std::generic_category() }.message()));
}
}
bool ChildProcess::wait(bool block)
@@ -179,7 +182,7 @@ namespace lms::core
const pid_t pid{ waitpid(_childPID, &wstatus, block ? 0 : WNOHANG) };
if (pid == -1)
throw SystemException{ errno, "waitpid failed!" };
throw SystemException{ std::error_code{ errno, std::generic_category() }, "waitpid failed!" };
if (pid == 0)
return false;
+13 -4
View File
@@ -23,6 +23,7 @@
#include <fstream>
#include <sys/stat.h>
#include <sys/types.h>
#include <system_error>
#include <unistd.h>
#include <boost/tokenizer.hpp>
@@ -66,16 +67,24 @@ namespace lms::core::pathUtils
return std::filesystem::create_directory(dir);
}
Wt::WDateTime getLastWriteTime(const std::filesystem::path& file)
Wt::WDateTime getLastWriteTime(const std::filesystem::path& file, std::error_code& ec)
{
Wt::WDateTime res;
struct stat sb
{
};
if (stat(file.c_str(), &sb) == -1)
throw LmsException("Failed to get stats on file '" + file.string() + "'");
{
ec = std::error_code{ errno, std::generic_category() };
}
else
{
ec = std::error_code{};
res = Wt::WDateTime::fromTime_t(sb.st_mtime);
}
return Wt::WDateTime::fromTime_t(sb.st_mtime);
return res;
}
bool exploreFilesRecursive(const std::filesystem::path& directory, std::function<bool(std::error_code, const std::filesystem::path&)> cb, const std::filesystem::path* excludeDirFileName)
-8
View File
@@ -20,7 +20,6 @@
#include "core/String.hpp"
#include <algorithm>
#include <array>
#include <cstring>
#include <iomanip>
#include <utility>
@@ -509,11 +508,4 @@ namespace lms::core::stringUtils
return "[" + std::to_string(mins) + ":" + (secs < 10 ? "0" : "") + std::to_string(secs) + "." + (millis < 100 ? (millis < 10 ? "00" : "0") : "") + std::to_string(millis) + "]";
}
std::string systemErrorToString(int err)
{
std::array<char, 128> buffer{};
::strerror_r(err, buffer.data(), buffer.size());
return std::string{ buffer.data() };
}
} // namespace lms::core::stringUtils