Skip to content

Commit

Permalink
chore: replace library tinydir with std::filesystem
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Aponte <federico.aponte@sysdig.com>
  • Loading branch information
federico-sysdig authored and poiana committed Nov 28, 2023
1 parent 041477e commit 4a57dbd
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 132 deletions.
9 changes: 0 additions & 9 deletions .github/install-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,6 @@ make -j
make install -j
popd

# === Tinydir ===
echo "=== Building and installing tinydir (1.2.5) ==="

wget "https://github.com/cxong/tinydir/archive/refs/tags/1.2.5.tar.gz"
tar xzf 1.2.5.tar.gz
pushd tinydir-1.2.5/
cp tinydir.h /usr/include
popd

# === uthash ===
echo "=== Downloading uthash.h (1.9.8) ==="

Expand Down
1 change: 0 additions & 1 deletion cmake/modules/libsinsp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ endif()
include(jsoncpp)
include(valijson)
include(re2)
include(tinydir)

set(LIBSINSP_INCLUDE_DIRS ${LIBSINSP_DIR}/userspace/libsinsp ${LIBSCAP_INCLUDE_DIRS} ${DRIVER_CONFIG_DIR})
if(WITH_CHISEL)
Expand Down
49 changes: 0 additions & 49 deletions cmake/modules/tinydir.cmake

This file was deleted.

98 changes: 29 additions & 69 deletions userspace/chisel/chisel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ limitations under the License.
*/

#include <iostream>
#include <fstream>
#include <cctype>
#include <locale>
#ifdef _WIN32
#include <io.h>
#else
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#endif
#include <tinydir.h>
#include "filterchecks.h"

#include "chisel.h"
Expand All @@ -47,6 +35,19 @@ extern "C" {
}
#endif

#include <cctype>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <locale>
#ifdef _WIN32
#include <io.h>
#else
#include <climits>
#include <cstdlib>
#include <unistd.h>
#endif

using namespace std;

extern vector<chiseldir_info>* g_chisel_dirs;
Expand Down Expand Up @@ -1074,84 +1075,43 @@ struct filename
string ext;
};

static filename split_filename(string const &fname)
{
filename res;
string::size_type idx = fname.rfind('.');
if(idx == std::string::npos)
{
res.valid = false;
}
else
{
res.valid = true;
res.name = fname.substr(0, idx);
res.ext = fname.substr(idx+1);
}
return res;
}

//
// 1. Iterates through the chisel files on disk (.sc and .lua)
// 2. Opens them and extracts the fields (name, description, etc)
// 3. Adds them to the chisel_descs vector.
//
void sinsp_chisel::get_chisel_list(vector<chisel_desc>* chisel_descs)
{
for(vector<chiseldir_info>::const_iterator it = g_chisel_dirs->begin();
it != g_chisel_dirs->end(); ++it)
#ifdef HAS_LUA_CHISELS
for(auto& dir_info : *g_chisel_dirs)
{
if(string(it->m_dir).empty())
if(dir_info.m_dir.empty())
{
continue;
}

tinydir_dir dir = {};

tinydir_open(&dir, it->m_dir.c_str());

while(dir.has_next)
for (auto const& dir_entry : filesystem::directory_iterator(dir_info.m_dir))
{
tinydir_file file;
tinydir_readfile(&dir, &file);

string fpath(file.path);
bool add_to_vector = false;
chisel_desc cd;

filename fn = split_filename(string(file.name));
if(fn.ext != "sc" && fn.ext != "lua")
{
goto next_file;
}

for(vector<chisel_desc>::const_iterator it_desc = chisel_descs->begin();
it_desc != chisel_descs->end(); ++it_desc)
if(dir_entry.path().extension() == ".lua")
{
if(fn.name == it_desc->m_name)
auto res = find_if(chisel_descs->begin(), chisel_descs->end(),
[&dir_entry](auto& desc) { return dir_entry.path().filename() == desc.m_name; });
if (res != chisel_descs->end())
{
goto next_file;
continue;
}
}
cd.m_name = fn.name;

#ifdef HAS_LUA_CHISELS
if(fn.ext == "lua")
{
add_to_vector = init_lua_chisel(cd, fpath);
}
chisel_desc cd;
cd.m_name = dir_entry.path().filename();

if(add_to_vector)
{
chisel_descs->push_back(cd);
if (init_lua_chisel(cd, dir_entry.path().generic_string()))
{
chisel_descs->emplace_back(std::move(cd));
}
}
#endif
next_file:
tinydir_next(&dir);
}

tinydir_close(&dir);
}
#endif
}

//
Expand Down
7 changes: 3 additions & 4 deletions userspace/libsinsp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ include(jsoncpp)
if(WITH_CHISEL)
include_directories(../chisel)
include(luajit)
include(tinydir)
endif()

include(zlib)
Expand Down Expand Up @@ -219,7 +218,7 @@ if(USE_BUNDLED_JSONCPP)
endif()

if(WITH_CHISEL)
add_dependencies(sinsp luajit tinydir)
add_dependencies(sinsp luajit)
endif()

function(prepare_cri_grpc api_version)
Expand All @@ -245,7 +244,7 @@ if(NOT WIN32)
if(NOT MINIMAL_BUILD)
if(NOT EMSCRIPTEN)
add_dependencies(sinsp openssl curl)
endif()
endif()
endif()

if(NOT APPLE)
Expand Down Expand Up @@ -294,7 +293,7 @@ if(NOT WIN32)
target_link_libraries(sinsp INTERFACE "${LUAJIT_LIB}")
list(APPEND SINSP_PKGCONFIG_LIBRARIES "${LUAJIT_LIB}")
endif()

target_link_libraries(sinsp INTERFACE dl pthread)
list(APPEND SINSP_PKGCONFIG_LIBRARIES dl pthread)

Expand Down

0 comments on commit 4a57dbd

Please sign in to comment.