From dd3449861bfefaa207c636a81f17d4ff78d99c6c Mon Sep 17 00:00:00 2001 From: Federico Aponte Date: Mon, 6 May 2024 00:46:14 +0200 Subject: [PATCH] refactor: use C++ std lib for sinsp::get_file_size Signed-off-by: Federico Aponte --- userspace/libsinsp/sinsp.cpp | 53 ++++++++++++------------------------ 1 file changed, 17 insertions(+), 36 deletions(-) diff --git a/userspace/libsinsp/sinsp.cpp b/userspace/libsinsp/sinsp.cpp index abbe9fae29..ceed2ba1ec 100644 --- a/userspace/libsinsp/sinsp.cpp +++ b/userspace/libsinsp/sinsp.cpp @@ -16,18 +16,6 @@ limitations under the License. */ -#include -#include -#include -#ifndef _WIN32 -#include -#include -#include -#include -#include -#include -#endif // _WIN32 - #include #include #include @@ -47,6 +35,17 @@ limitations under the License. #include #endif +#ifndef _WIN32 +#include +#include +#include +#endif // _WIN32 + +#include +#include +#include +#include + /** * This is the maximum size assigned to the concurrent asynchronous event * queue that can be used to inject async events during an event capture. @@ -677,32 +676,14 @@ std::string sinsp::generate_gvisor_config(std::string socket_path) int64_t sinsp::get_file_size(const std::string& fname, char *error) { - static std::string err_str = "Could not determine capture file size: "; - std::string errdesc; -#ifdef _WIN32 - LARGE_INTEGER li = { 0 }; - HANDLE fh = CreateFile(fname.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, - OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL); - if (fh != INVALID_HANDLE_VALUE) - { - if (0 != GetFileSizeEx(fh, &li)) - { - CloseHandle(fh); - return li.QuadPart; - } - errdesc = get_error_desc(err_str); - CloseHandle(fh); - } -#else - struct stat st; - if (0 == stat(fname.c_str(), &st)) + std::error_code ec; + auto sz = std::filesystem::file_size(fname, ec); + if(ec) { - return st.st_size; + strlcpy(error, ec.message().c_str(), SCAP_LASTERR_SIZE); + return -1; } -#endif - if(errdesc.empty()) errdesc = get_error_desc(err_str); - strlcpy(error, errdesc.c_str(), SCAP_LASTERR_SIZE); - return -1; + return sz; } unsigned sinsp::m_num_possible_cpus = 0;