From b77f5fa14cc1b64572b70f80366e1ba56f87503c Mon Sep 17 00:00:00 2001 From: Arne Welzel Date: Thu, 30 Jun 2022 11:34:39 +0200 Subject: [PATCH] Add gulrak/filesystem to auxil, expose via zeek::filesystem This adds https://github.com/gulrak/filesystem as a submodule into auxil as a compiler-independent std::filesystem replacement. The ghc::filesystem namespace is exposed as zeek::filesystem in util.h. In the build directory, we add 3rdparty/ghc as a symlink to auxil in order to support building from the build tree. /src/3rdparty/ghc -> /path/to/zeek/src/auxil/filesystem/include/ghc In the installation tree, the headers are installed into include/zeek/3rdparty: /include/zeek/3rdparty/ghc Note, this differs from how we approached rapidjson which isn't included using a zeek/3rdparty and instead requires an additional include path of the following form for external plugins to find and use it. /include/zeek/3rdparty/rapidjson/include/ We diverge from this approach. Placing ghc directly into 3rdparty appears nicer and avoids changing external components (DynamicPlugin.cmake / spicyc) --- .gitmodules | 3 +++ .lgtm.yml | 1 + CMakeLists.txt | 11 +++++++++++ COPYING.3rdparty | 26 ++++++++++++++++++++++++++ auxil/filesystem | 1 + src/util.cc | 18 ++++++++++++++++++ src/util.h | 6 ++++++ 7 files changed, 66 insertions(+) create mode 160000 auxil/filesystem diff --git a/.gitmodules b/.gitmodules index d84fc4118a..b43d81ffad 100644 --- a/.gitmodules +++ b/.gitmodules @@ -64,3 +64,6 @@ [submodule "auxil/spicy"] path = auxil/spicy/spicy url = https://github.com/zeek/spicy +[submodule "auxil/filesystem"] + path = auxil/filesystem + url = https://github.com/gulrak/filesystem.git diff --git a/.lgtm.yml b/.lgtm.yml index 13772c6e87..566a5249a7 100644 --- a/.lgtm.yml +++ b/.lgtm.yml @@ -4,6 +4,7 @@ path_classifiers: library: - "auxil/broker/caf/" + - "auxil/filesystem/" - "auxil/libkqueue/" - "auxil/highwayhash/" - "auxil/rapidjson/" diff --git a/CMakeLists.txt b/CMakeLists.txt index e4bd4c330b..ad2a2b8aea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -458,6 +458,17 @@ include_directories(BEFORE install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/auxil/rapidjson/include/rapidjson DESTINATION include/zeek/3rdparty/rapidjson/include) +install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/auxil/filesystem/include/ghc + DESTINATION include/zeek/3rdparty/) + +# Create 3rdparty/ghc within the build directory so that the include for +# "zeek/3rdparty/ghc/filesystem.hpp" works within the build tree. +execute_process(COMMAND "${CMAKE_COMMAND}" -E make_directory + "${CMAKE_CURRENT_BINARY_DIR}/3rdparty/") +execute_process(COMMAND "${CMAKE_COMMAND}" -E create_symlink + "${CMAKE_CURRENT_SOURCE_DIR}/auxil/filesystem/include/ghc" + "${CMAKE_CURRENT_BINARY_DIR}/3rdparty/ghc") + # Optional Dependencies set(USE_GEOIP false) diff --git a/COPYING.3rdparty b/COPYING.3rdparty index ffbb0dee0b..e984d6eb40 100644 --- a/COPYING.3rdparty +++ b/COPYING.3rdparty @@ -533,6 +533,32 @@ POSSIBILITY OF SUCH DAMAGE. ============================================================================== +%%% auxil/filesystem + +============================================================================== + +Copyright (c) 2018, Steffen Schümann + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +============================================================================== + %%% auxil/highwayhash ============================================================================== diff --git a/auxil/filesystem b/auxil/filesystem new file mode 160000 index 0000000000..cd6805e94d --- /dev/null +++ b/auxil/filesystem @@ -0,0 +1 @@ +Subproject commit cd6805e94dd5d6346be1b75a54cdc27787319dd2 diff --git a/src/util.cc b/src/util.cc index be016b89f4..125be958bb 100644 --- a/src/util.cc +++ b/src/util.cc @@ -2709,3 +2709,21 @@ extern "C" void out_of_memory(const char* where) abort(); } + +TEST_CASE("util filesystem") + { + zeek::filesystem::path path1("/a/b"); + CHECK(path1.is_absolute()); + CHECK(! path1.is_relative()); + CHECK(path1.filename() == "b"); + CHECK(path1.parent_path() == "/a"); + + zeek::filesystem::path path2("/a//b//conn.log"); + CHECK(path2.lexically_normal() == "/a/b/conn.log"); + + zeek::filesystem::path path3("a//b//"); + CHECK(path3.lexically_normal() == "a/b/"); + + auto info = zeek::filesystem::space("."); + CHECK(info.capacity > 0); + } diff --git a/src/util.h b/src/util.h index 701f6f6736..990a15a6db 100644 --- a/src/util.h +++ b/src/util.h @@ -75,6 +75,8 @@ extern "C" #include "zeek/3rdparty/modp_numtoa.h" } +#include "zeek/3rdparty/ghc/filesystem.hpp" + using bro_int_t = int64_t; using bro_uint_t = uint64_t; @@ -91,6 +93,10 @@ namespace zeek class ODesc; class RecordVal; +// Expose ghc::filesystem as zeek::filesystem until we can +// switch to std::filesystem. +namespace filesystem = ghc::filesystem; + namespace util { namespace detail