diff --git a/.clangd b/.clangd new file mode 100644 index 00000000..37b844d1 --- /dev/null +++ b/.clangd @@ -0,0 +1,5 @@ +{ + "CompileFlags": { + "Remove": ["-flarge-source-files", "-fext-numeric-literals"] + } +} diff --git a/.cspell/words.txt b/.cspell/words.txt index 656e658a..a5c9ea70 100644 --- a/.cspell/words.txt +++ b/.cspell/words.txt @@ -1,3 +1,7 @@ +_lseeki64 +_sh_denyno +_sh_denyrd +_sh_denyrw aarch64 advapi32 armv8 @@ -22,6 +26,8 @@ crypto_aead_xchacha20poly1305_ietf_npubbytes cstdint cxxflags cxxstd +d_largefile64_source +d_largefile_source d_ndebug dbackward_shared dbghelp @@ -83,13 +89,18 @@ ecdh endforeach endfunction eventlib +expect_streq +fallocate_impl fext fgetattr flac_version +flag_nopath flarge fontconfig_version freetype2_version fsetattr_x +fusermount +futimens getxtimes glapi gmock @@ -107,6 +118,7 @@ iphlpapi libbitcoin libbitcoinsystem libcurl +libdsm libevent libexample libfuse3 @@ -119,14 +131,18 @@ libsfml libsodium_type libuuid libuuid_include_dirs +libvlc linkflags mbig msvc msvcr120 mtune +nana ncrypt nlohmann_json nmakeprg +nominmax +ntstatus nuspell_version oleaut32 openal_version @@ -134,13 +150,20 @@ openssldir pkgconfig project_enable_fontconfig project_enable_gtkmm +project_enable_libdsm +project_enable_nana propgrid +psecurity_descriptor pugi pugixml_project +puint32 +pvoid +pwstr remote_winfsp richtext rocksdb_library rpcrt4 +sddl_revision_1 secp256k1 secur32 sfml_project @@ -148,13 +171,19 @@ shlwapi source_subdir spdlog spdlog_project +st_ctim static-libgcc static-libstdc++ +stbuf stduuid_project strequal +ularge_integer uring userenv +utimens_impl +utimensat vorbis_version +waggressive wall wcast-align wconversion @@ -162,6 +191,7 @@ wdouble-promotion wduplicated-branches wduplicated-cond wextra +wfloat wformat=2 winfsp winhttp @@ -179,5 +209,6 @@ wserialization wshadow wsign-conversion wunused +wuseless wxwidgets_version -xattr \ No newline at end of file +xattr diff --git a/.jenkins_builds b/.jenkins_builds index 0038a617..5ac02aaf 100644 --- a/.jenkins_builds +++ b/.jenkins_builds @@ -52,7 +52,7 @@ pipeline { agent any steps { - sh 'scripts/deliver.sh /mnt/repertory "" "" "" "" 1 0' + sh 'scripts/deliver.sh /mnt/repertory "" "" "" "" 1 1' sh 'scripts/deliver.sh /mnt/repertory "" aarch64' sh 'scripts/deliver.sh /mnt/repertory' } diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b436d57..79d48de3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,8 @@ if(HAS_SETXATTR) add_definitions(-DHAS_SETXATTR) endif() +include(cmake/hashes.cmake) + include(cmake/versions.cmake) include(cmake/arch.cmake) include(cmake/os.cmake) @@ -32,6 +34,9 @@ endif() if(PROJECT_IS_MINGW) add_definitions(-DPROJECT_IS_MINGW) + if(PROJECT_IS_MINGW_UNIX) + add_definitions(-DPROJECT_IS_MINGW_UNIX) + endif() endif() if(PROJECT_REQUIRE_ALPINE) @@ -42,6 +47,13 @@ if(PROJECT_IS_ARM64) add_definitions(-DPROJECT_IS_ARM64) endif() +if(PROJECT_IS_MINGW) + option(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES "Enable path sizes of 32767 characters on Windows" OFF) + if(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES) + add_definitions(-DPROJECT_ENABLE_WIN32_LONG_PATH_NAMES) + endif() +endif() + include(cmake/settings.cmake) include(cmake/flags.cmake) @@ -127,7 +139,7 @@ endif() -DPROJECT_COPYRIGHT=${PROJECT_COPYRIGHT} -DPROJECT_DESC=${PROJECT_DESC} -DPROJECT_DIST_DIR=${PROJECT_DIST_DIR} - -DPROJECT_ENABLE_BACKWARD_CPP=${PROJECT_ENABLE_BACKWARD_CPP} + -DPROJECT_ENABLE_WIN32_LONG_PATH_NAMES=${PROJECT_ENABLE_WIN32_LONG_PATH_NAMES} -DPROJECT_ENABLE_BOOST=${PROJECT_ENABLE_BOOST} -DPROJECT_ENABLE_CPP_HTTPLIB=${PROJECT_ENABLE_CPP_HTTPLIB} -DPROJECT_ENABLE_CURL=${PROJECT_ENABLE_CURL} diff --git a/cmake/flags.cmake b/cmake/flags.cmake index c73b6b38..b6691f55 100644 --- a/cmake/flags.cmake +++ b/cmake/flags.cmake @@ -59,8 +59,7 @@ list(APPEND PROJECT_CFLAGS_LIST list(APPEND PROJECT_CXXFLAGS_LIST ${PROJECT_COMMON_FLAG_LIST} - -fext-numeric-literals - -std=c++${CMAKE_CXX_STANDARD} + -std=gnu++${CMAKE_CXX_STANDARD} ) if(PROJECT_STATIC_LINK) diff --git a/cmake/functions.cmake b/cmake/functions.cmake index 5da081f2..2bcedf85 100644 --- a/cmake/functions.cmake +++ b/cmake/functions.cmake @@ -26,7 +26,7 @@ function(set_common_target_options name) endif() endfunction(set_common_target_options) -function(add_project_executable2 name dependencies libraries headers sources) +function(add_project_executable2 name dependencies libraries headers sources is_win32) if (PROJECT_WINDOWS_VERSION_RC) list(APPEND sources ${PROJECT_WINDOWS_VERSION_RC}) endif() @@ -52,6 +52,10 @@ function(add_project_executable2 name dependencies libraries headers sources) if(PROJECT_ENABLE_SDL AND PROJECT_IS_MINGW) target_link_libraries(${name} PRIVATE SDL2::SDL2main) endif () + + if (is_win32 AND PROJECT_IS_MINGW) + target_link_options(${name} PRIVATE -mwindows) + endif() endfunction(add_project_executable2) function(add_project_executable name dependencies libraries) @@ -69,7 +73,15 @@ function(add_project_executable name dependencies libraries) ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}/${name}/src/*.cxx ) - add_project_executable2(${name} "${dependencies}" "${libraries}" "${headers}" "${sources}") + if(PROJECT_ENABLE_WXWIDGETS OR PROJECT_ENABLE_SDL OR PROJECT_ENABLE_SFML OR PROJECT_ENABLE_NANA) + set(IS_WIN32 ON) + endif() + + add_project_executable2(${name} "${dependencies}" "${libraries}" "${headers}" "${sources}" "${IS_WIN32}") + + if(PROJECT_ENABLE_WXWIDGETS) + target_link_libraries(${name} PRIVATE ${wxWidgets_LIBRARIES}) + endif() endfunction(add_project_executable) function(add_project_library name dependencies libraries additional_sources) @@ -121,7 +133,7 @@ function(add_project_test_executable name dependencies libraries) ${additional_sources} ) - add_project_executable2(${name} "${dependencies}" "${libraries}" "${headers}" "${sources}") + add_project_executable2(${name} "${dependencies}" "${libraries}" "${headers}" "${sources}" OFF) target_compile_definitions(${name} PRIVATE -DPROJECT_TESTING) diff --git a/cmake/hashes.cmake b/cmake/hashes.cmake new file mode 100644 index 00000000..6547c33c --- /dev/null +++ b/cmake/hashes.cmake @@ -0,0 +1,41 @@ +set(BINUTILS_HASH ae9a5789e23459e59606e6714723f2d3ffc31c03174191ef0d015bdf06007450) +set(BOOST2_HASH 7bd7ddceec1a1dfdcbdb3e609b60d01739c38390a5f956385a12f3122049f0ca) +set(BOOST_HASH be0d91732d5b0cc6fbb275c7939974457e79b54d6f07ce2e3dfdd68bef883b0b) +set(CLI11_HASH f2d893a65c3b1324c50d4e682c0cdc021dd0477ae2c048544f39eed6654b699a) +set(CPP_HTTPLIB_HASH c1742fc7179aaae2a67ad9bba0740b7e9ffaf4f5e62feef53101ecdef1478716) +set(CURL_HASH d714818f6ac41ae9154850158fed44b7a87650a6d52f83d3bcb9aa527be354d7) +set(CXXOPTS_HASH 841f49f2e045b9c6365997c2a8fbf76e6f215042dda4511a5bb04bc5ebc7f88a) +set(EXPAT_HASH fbd032683370d761ba68dba2566d3280a154f5290634172d60a79b24d366d9dc) +set(FLAC_HASH 0a4bb82a30609b606650d538a804a7b40205366ce8fc98871b0ecf3fbb0611ee) +set(FMT_HASH 6cb1e6d37bdcb756dbbe59be438790db409cdb4868c66e888d5df9f13f7c027f) +set(FONTCONFIG_HASH f5f359d6332861bd497570848fcb42520964a9e83d5e3abe397b6b6db9bcaaf4) +set(FREETYPE2_HASH 5c3a8e78f7b24c20b25b54ee575d6daa40007a5f4eea2845861c3409b3021747) +set(GCC_HASH 7d376d445f93126dc545e2c0086d0f647c3094aae081cdb78f42ce2bc25e7293) +set(GTEST_HASH 7b42b4d6ed48810c5362c265a17faebe90dc2373c885e5216439d37927f02926) +set(ICU_HASH 925e6b4b8cf8856e0ac214f6f34e30dee63b7bb7a50460ab4603950eff48f89e) +set(JSON_HASH 0d8ef5af7f9794e3263480193c491549b2ba6cc74bb018906202ada498a79406) +set(LIBDSM_HASH 747c4563d6291303d9b085c9e7dc96ac44f91871dcac3e20480fdcc066eee88a) +set(LIBEVENT_HASH 7180a979aaa7000e1264da484f712d403fcf7679b1e9212c4e3d09f5c93efc24) +set(LIBICONV_HASH 8f74213b56238c85a50a5329f77e06198771e70dd9a739779f4c02f65d971313) +set(LIBJPEG_TURBO_HASH a649205a90e39a548863a3614a9576a3fb4465f8e8e66d54999f127957c25b21) +set(LIBPNG_HASH fecc95b46cf05e8e3fc8a414750e0ba5aad00d89e9fdf175e94ff041caf1a03a) +set(LIBSODIUM_HASH 8e5aeca07a723a27bbecc3beef14b0068d37e7fc0e97f51b3f1c82d2a58005c1) +set(LIBTASN_HASH 1613f0ac1cf484d6ec0ce3b8c06d56263cc7242f1c23b30d82d23de345a63f7a) +set(MINGW_HASH 3f66bce069ee8bed7439a1a13da7cb91a5e67ea6170f21317ac7f5794625ee10) +set(NANA_HASH 56f7b1ed006c750fccf8ef15ab1e83f96751f2dfdcb68d93e5f712a6c9b58bcb) +set(NUSPELL_HASH 5d4baa1daf833a18dc06ae0af0571d9574cc849d47daff6b9ce11dac0a5ded6a) +set(OGG_HASH 0eb4b4b9420a0f51db142ba3f9c64b333f826532dc0f48c6410ae51f4799b664) +set(OPENAL_HASH dfddf3a1f61059853c625b7bb03de8433b455f2f79f89548cbcbd5edca3d4a4a) +set(OPENSSL_HASH 777cd596284c883375a2a7a11bf5d2786fc5413255efab20c50d6ffe6d020b7e) +set(PKG_CONFIG_HASH 6fc69c01688c9458a57eb9a1664c9aba372ccda420a02bf4429fe610e7e7d591) +set(PUGIXML_HASH 2f10e276870c64b1db6809050a75e11a897a8d7456c4be5c6b2e35a11168a015) +set(ROCKSDB_HASH b20780586d3df4a3c5bcbde341a2c1946b03d18237960bda5bc5e9538f42af40) +set(SDL_HASH 254a767aa486fa6308d4473159c1f23c794610be775d63e98084111d96814b85) +set(SECP256K1_HASH 61583939f1f25b92e6401e5b819e399da02562de663873df3056993b40148701) +set(SFML_HASH 82535db9e57105d4f3a8aedabd138631defaedc593cab589c924b7d7a11ffb9d) +set(SPDLOG_HASH 1586508029a7d0670dfcb2d97575dcdc242d3868a259742b69f100801ab4e16b) +set(SQLITE_HASH 77823cb110929c2bcb0f5d48e4833b5c59a8a6e40cdea3936b99e199dbbe5784) +set(STDUUID_HASH b1176597e789531c38481acbbed2a6894ad419aab0979c10410d59eb0ebf40d3) +set(VORBIS_HASH 270c76933d0934e42c5ee0a54a36280e2d87af1de3cc3e584806357e237afd13) +set(WXWIDGETS_HASH 0ad86a3ad3e2e519b6a705248fc9226e3a09bbf069c6c692a02acf7c2d1c6b51) +set(ZLIB_HASH 17e88863f3600672ab49182f217281b6fc4d3c762bde361935e436a95214d05c) diff --git a/cmake/libraries.cmake b/cmake/libraries.cmake index 1897e130..97ab4ad5 100644 --- a/cmake/libraries.cmake +++ b/cmake/libraries.cmake @@ -11,7 +11,6 @@ include(cmake/libraries/openssl.cmake) include(cmake/libraries/boost.cmake) -include(cmake/libraries/backward_cpp.cmake) include(cmake/libraries/cpp_httplib.cmake) include(cmake/libraries/curl.cmake) include(cmake/libraries/fuse.cmake) diff --git a/cmake/libraries/backward_cpp.cmake b/cmake/libraries/backward_cpp.cmake deleted file mode 100644 index 0b22e8ce..00000000 --- a/cmake/libraries/backward_cpp.cmake +++ /dev/null @@ -1,11 +0,0 @@ -if(PROJECT_ENABLE_BACKWARD_CPP AND PROJECT_BUILD) - if(PROJECT_IS_MINGW) - add_definitions(-DPROJECT_ENABLE_BACKWARD_CPP) - - link_libraries(msvcr90) - else() - add_definitions(-DBACKWARD_HAS_BFD) - - link_libraries(bfd) - endif() -endif() diff --git a/cmake/libraries/boost.cmake b/cmake/libraries/boost.cmake index 225a2aea..7a69a978 100644 --- a/cmake/libraries/boost.cmake +++ b/cmake/libraries/boost.cmake @@ -46,66 +46,72 @@ if(PROJECT_ENABLE_BOOST) ${Boost_LIBRARIES} ) elseif(NOT PROJECT_IS_MINGW OR CMAKE_HOST_WIN32) - if(PROJECT_CMAKE_BUILD_TYPE STREQUAL "Debug") - set(BOOST_BUILD_TYPE_LOWER debug) - else() - set(BOOST_BUILD_TYPE_LOWER release) + if (PROJECT_ENABLE_LIBBITCOIN_SYSTEM OR NOT CMAKE_HOST_WIN32) + if(PROJECT_CMAKE_BUILD_TYPE STREQUAL "Debug") + set(BOOST_BUILD_TYPE_LOWER debug) + else() + set(BOOST_BUILD_TYPE_LOWER release) + endif() + + if(PROJECT_IS_ARM64) + set(BOOST_ARCH arm) + else() + set(BOOST_ARCH x86) + endif() + + if(PROJECT_STATIC_LINK) + set(BOOST_LINK static) + else() + set(BOOST_LINK "static,shared") + endif() + + set(BOOST_BUILD_ARGS + --openssldir=$ENV{OPENSSL_ROOT_DIR} + --prefix=${PROJECT_EXTERNAL_BUILD_ROOT} + address-model=64 + architecture=${BOOST_ARCH} + cxxstd=20 + cxxstd-dialect=gnu + cxxflags=-std=gnu++${CMAKE_CXX_STANDARD} + cxxstd=${CMAKE_CXX_STANDARD} + define=BOOST_ASIO_HAS_STD_STRING_VIEW + define=BOOST_SYSTEM_NO_DEPRECATED + link=${BOOST_LINK} + linkflags=-std=gnu++${CMAKE_CXX_STANDARD} + threading=multi + variant=${BOOST_BUILD_TYPE_LOWER} + ) + + if(PROJECT_ENABLE_LIBBITCOIN_SYSTEM) + set(BOOST_URL_HASH SHA256=${BOOST2_HASH}) + else() + set(BOOST_URL_HASH SHA256=${BOOST_HASH}) + endif() + + ExternalProject_Add(boost_project + PREFIX external + URL ${PROJECT_3RD_PARTY_DIR}/boost_${BOOST_MAJOR_VERSION}_${BOOST_MINOR_VERSION}_${BOOST_PATCH_VERSION}.tar.gz + URL_HASH ${BOOST_URL_HASH} + BUILD_IN_SOURCE 1 + LIST_SEPARATOR | + CONFIGURE_COMMAND ./bootstrap.sh + --with-libraries=atomic,chrono,date_time,filesystem,iostreams,locale,log,program_options,random,regex,serialization,system,test,thread + BUILD_COMMAND + ./b2 + -j1 + ${BOOST_BUILD_ARGS} + INSTALL_COMMAND + ./b2 + -j1 + ${BOOST_BUILD_ARGS} + install + ) + + list(APPEND PROJECT_DEPENDENCIES boost_project) + + if (NOT CMAKE_HOST_WIN32) + add_dependencies(boost_project openssl_project) + endif() endif() - - if(PROJECT_IS_ARM64) - set(BOOST_ARCH arm) - else() - set(BOOST_ARCH x86) - endif() - - if(PROJECT_STATIC_LINK) - set(BOOST_LINK static) - else() - set(BOOST_LINK "static,shared") - endif() - - set(BOOST_BUILD_ARGS - --openssldir=$ENV{OPENSSL_ROOT_DIR} - --prefix=${PROJECT_EXTERNAL_BUILD_ROOT} - address-model=64 - architecture=${BOOST_ARCH} - cxxflags=-std=c++${CMAKE_CXX_STANDARD} - cxxstd=${CMAKE_CXX_STANDARD} - define=BOOST_ASIO_HAS_STD_STRING_VIEW - define=BOOST_SYSTEM_NO_DEPRECATED - link=${BOOST_LINK} - linkflags=-std=c++${CMAKE_CXX_STANDARD} - threading=multi - variant=${BOOST_BUILD_TYPE_LOWER} - ) - - if(PROJECT_ENABLE_LIBBITCOIN_SYSTEM) - set(BOOST_URL_HASH SHA256=7bd7ddceec1a1dfdcbdb3e609b60d01739c38390a5f956385a12f3122049f0ca) - else() - set(BOOST_URL_HASH SHA256=be0d91732d5b0cc6fbb275c7939974457e79b54d6f07ce2e3dfdd68bef883b0b) - endif() - - ExternalProject_Add(boost_project - PREFIX external - URL ${PROJECT_3RD_PARTY_DIR}/boost_${BOOST_MAJOR_VERSION}_${BOOST_MINOR_VERSION}_${BOOST_PATCH_VERSION}.tar.gz - URL_HASH ${BOOST_URL_HASH} - BUILD_IN_SOURCE 1 - LIST_SEPARATOR | - CONFIGURE_COMMAND ./bootstrap.sh - --with-libraries=atomic,chrono,date_time,filesystem,iostreams,locale,log,program_options,random,regex,serialization,system,test,thread - BUILD_COMMAND - ./b2 - -j1 - ${BOOST_BUILD_ARGS} - INSTALL_COMMAND - ./b2 - -j1 - ${BOOST_BUILD_ARGS} - install - ) - - list(APPEND PROJECT_DEPENDENCIES boost_project) - - add_dependencies(boost_project openssl_project) endif() endif() diff --git a/cmake/libraries/cpp_httplib.cmake b/cmake/libraries/cpp_httplib.cmake index bc2f667e..014fc9a2 100644 --- a/cmake/libraries/cpp_httplib.cmake +++ b/cmake/libraries/cpp_httplib.cmake @@ -10,7 +10,7 @@ if(PROJECT_ENABLE_CPP_HTTPLIB) ExternalProject_Add(cpphttplib_project PREFIX external URL ${PROJECT_3RD_PARTY_DIR}/cpp-httplib-${CPP_HTTPLIB_VERSION}.tar.gz - URL_HASH SHA256=c125022eb85eaa12235518dc4638be93b62c3216d0f87b655af7b17b71b38851 + URL_HASH SHA256=${CPP_HTTPLIB_HASH} LIST_SEPARATOR | CMAKE_ARGS ${PROJECT_EXTERNAL_CMAKE_FLAGS} -DBUILD_SHARED_LIBS=${PROJECT_BUILD_SHARED_LIBS} @@ -24,9 +24,9 @@ if(PROJECT_ENABLE_CPP_HTTPLIB) list(APPEND PROJECT_DEPENDENCIES cpphttplib_project) - add_dependencies(cpphttplib_project - curl_project - openssl_project - ) + add_dependencies(cpphttplib_project curl_project) + if (NOT CMAKE_HOST_WIN32) + add_dependencies(cpphttplib_project openssl_project) + endif() endif() endif() diff --git a/cmake/libraries/curl.cmake b/cmake/libraries/curl.cmake index a96c5c64..f18b5208 100644 --- a/cmake/libraries/curl.cmake +++ b/cmake/libraries/curl.cmake @@ -16,7 +16,7 @@ if(PROJECT_ENABLE_CURL) ExternalProject_Add(curl_project PREFIX external URL ${PROJECT_3RD_PARTY_DIR}/curl-${CURL_VERSION}.tar.gz - URL_HASH SHA256=77c0e1cd35ab5b45b659645a93b46d660224d0024f1185e8a95cdb27ae3d787d + URL_HASH SHA256=${CURL_HASH} LIST_SEPARATOR | CMAKE_ARGS ${PROJECT_EXTERNAL_CMAKE_FLAGS} @@ -42,6 +42,8 @@ if(PROJECT_ENABLE_CURL) list(APPEND PROJECT_DEPENDENCIES curl_project) - add_dependencies(curl_project openssl_project) + if (NOT CMAKE_HOST_WIN32) + add_dependencies(curl_project openssl_project) + endif() endif() endif() diff --git a/cmake/libraries/json.cmake b/cmake/libraries/json.cmake index 1648375f..632e6ba6 100644 --- a/cmake/libraries/json.cmake +++ b/cmake/libraries/json.cmake @@ -13,7 +13,7 @@ if(PROJECT_ENABLE_JSON) ExternalProject_Add(json_project PREFIX external URL ${PROJECT_3RD_PARTY_DIR}/json-${JSON_VERSION}.tar.gz - URL_HASH SHA256=0d8ef5af7f9794e3263480193c491549b2ba6cc74bb018906202ada498a79406 + URL_HASH SHA256=${JSON_HASH} LIST_SEPARATOR | CMAKE_ARGS ${PROJECT_EXTERNAL_CMAKE_FLAGS} -DBUILD_SHARED_LIBS=${PROJECT_BUILD_SHARED_LIBS} diff --git a/cmake/libraries/libsodium.cmake b/cmake/libraries/libsodium.cmake index 4b04a832..b547b592 100644 --- a/cmake/libraries/libsodium.cmake +++ b/cmake/libraries/libsodium.cmake @@ -1,6 +1,6 @@ if(PROJECT_ENABLE_LIBSODIUM) if(PROJECT_BUILD) - pkg_check_modules(SODIUM libsodium=${LIBSODIUM_VERSION} REQUIRED) + pkg_check_modules(SODIUM libsodium>=${LIBSODIUM_VERSION} REQUIRED) add_definitions(-DPROJECT_ENABLE_LIBSODIUM) @@ -15,7 +15,7 @@ if(PROJECT_ENABLE_LIBSODIUM) else() link_libraries(${SODIUM_LIBRARIES}) endif() - elseif(NOT PROJECT_IS_MINGW OR CMAKE_HOST_WIN32) + elseif(NOT PROJECT_IS_MINGW) if(PROJECT_IS_MINGW) set(LIBSODIUM_TYPE mingw64) else() @@ -31,7 +31,7 @@ if(PROJECT_ENABLE_LIBSODIUM) ExternalProject_Add(libsodium_project PREFIX external URL ${PROJECT_3RD_PARTY_DIR}/libsodium-${LIBSODIUM_VERSION}.tar.gz - URL_HASH SHA256=ebb65ef6ca439333c2bb41a0c1990587288da07f6c7fd07cb3a18cc18d30ce19 + URL_HASH SHA256=${LIBSODIUM_HASH} BUILD_IN_SOURCE 1 LIST_SEPARATOR | CONFIGURE_COMMAND ${PROJECT_3RD_PARTY_DIR}/libsodium_configure.sh diff --git a/cmake/libraries/openssl.cmake b/cmake/libraries/openssl.cmake index 9758745d..942aa069 100644 --- a/cmake/libraries/openssl.cmake +++ b/cmake/libraries/openssl.cmake @@ -12,7 +12,7 @@ if(PROJECT_ENABLE_OPENSSL) OpenSSL::Crypto OpenSSL::SSL ) - elseif(NOT PROJECT_IS_MINGW OR CMAKE_HOST_WIN32) + elseif(NOT PROJECT_IS_MINGW) if(PROJECT_IS_MINGW) set(OPENSSL_COMPILE_TYPE mingw64) elseif(PROJECT_IS_ARM64) @@ -34,7 +34,7 @@ if(PROJECT_ENABLE_OPENSSL) ExternalProject_Add(openssl_project PREFIX external URL ${PROJECT_3RD_PARTY_DIR}/openssl-${OPENSSL_VERSION}.tar.gz - URL_HASH SHA256=777cd596284c883375a2a7a11bf5d2786fc5413255efab20c50d6ffe6d020b7e + URL_HASH SHA256=${OPENSSL_HASH} BUILD_IN_SOURCE 1 LIST_SEPARATOR | CONFIGURE_COMMAND ./Configure diff --git a/cmake/libraries/pugixml.cmake b/cmake/libraries/pugixml.cmake index f16f84e5..6427f11a 100644 --- a/cmake/libraries/pugixml.cmake +++ b/cmake/libraries/pugixml.cmake @@ -11,11 +11,11 @@ if(PROJECT_ENABLE_PUGIXML) else() link_libraries(pugixml::pugixml) endif() - elseif(NOT PROJECT_IS_MINGW OR CMAKE_HOST_WIN32) + elseif(NOT PROJECT_IS_MINGW) ExternalProject_Add(pugixml_project PREFIX external URL ${PROJECT_3RD_PARTY_DIR}/pugixml-${PUGIXML_VERSION}.tar.gz - URL_HASH SHA256=2f10e276870c64b1db6809050a75e11a897a8d7456c4be5c6b2e35a11168a015 + URL_HASH SHA256=${PUGIXML_HASH} LIST_SEPARATOR | CMAKE_ARGS ${PROJECT_EXTERNAL_CMAKE_FLAGS} -DBUILD_SHARED_LIBS=${PROJECT_BUILD_SHARED_LIBS} diff --git a/cmake/libraries/spdlog.cmake b/cmake/libraries/spdlog.cmake index 430b0964..21972d54 100644 --- a/cmake/libraries/spdlog.cmake +++ b/cmake/libraries/spdlog.cmake @@ -11,11 +11,10 @@ if(PROJECT_ENABLE_SPDLOG) ExternalProject_Add(spdlog_project PREFIX external URL ${PROJECT_3RD_PARTY_DIR}/spdlog-${SPDLOG_VERSION}.tar.gz - URL_HASH SHA256=1586508029a7d0670dfcb2d97575dcdc242d3868a259742b69f100801ab4e16b + URL_HASH SHA256=${SPDLOG_HASH} LIST_SEPARATOR | CMAKE_ARGS ${PROJECT_EXTERNAL_CMAKE_FLAGS} -DBUILD_SHARED_LIBS=${PROJECT_BUILD_SHARED_LIBS} - -DBUILD_STATIC_LIBS=ON -DSPDLOG_BUILD_EXAMPLE=OFF -DSPDLOG_FMT_EXTERNAL=OFF -DSPDLOG_FMT_EXTERNAL_HO=OFF diff --git a/cmake/libraries/sqlite.cmake b/cmake/libraries/sqlite.cmake index d46120be..49b87217 100644 --- a/cmake/libraries/sqlite.cmake +++ b/cmake/libraries/sqlite.cmake @@ -1,19 +1,24 @@ if(PROJECT_ENABLE_SQLITE) if(PROJECT_BUILD) add_definitions(-DPROJECT_ENABLE_SQLITE) + if (PROJECT_IS_MINGW AND NOT PROJECT_IS_MINGW_UNIX) + pkg_check_modules(SQLITE3 REQUIRED sqlite3>=${SQLITE2_VERSION}) + include_directories(SYSTEM BEFORE ${SQLITE3_INCLUDE_DIRS}) + link_libraries(${SQLITE3_LIBRARIES}) + else() + set(SQLITE_SYSTEM_ROOT ${PROJECT_BUILD_DIR}/external/src/sqlite_project) - set(SQLITE_SYSTEM_ROOT ${PROJECT_BUILD_DIR}/external/src/sqlite_project) + include_directories(SYSTEM BEFORE ${SQLITE_SYSTEM_ROOT}) - include_directories(SYSTEM BEFORE ${SQLITE_SYSTEM_ROOT}) - - list(APPEND PROJECT_ADDITIONAL_SOURCES - ${SQLITE_SYSTEM_ROOT}/sqlite3.c - ) - else() + list(APPEND PROJECT_ADDITIONAL_SOURCES + ${SQLITE_SYSTEM_ROOT}/sqlite3.c + ) + endif() + elseif(NOT PROJECT_IS_MINGW OR PROJECT_IS_MINGW_UNIX) ExternalProject_Add(sqlite_project PREFIX external URL ${PROJECT_3RD_PARTY_DIR}/sqlite-amalgamation-${SQLITE_VERSION}.zip - URL_HASH SHA256=712a7d09d2a22652fb06a49af516e051979a3984adb067da86760e60ed51a7f5 + URL_HASH SHA256=${SQLITE_HASH} CONFIGURE_COMMAND echo "No configure" BUILD_COMMAND echo "No build" INSTALL_COMMAND echo "No install" diff --git a/cmake/libraries/stduuid.cmake b/cmake/libraries/stduuid.cmake index 94e7d3db..87ffc05a 100644 --- a/cmake/libraries/stduuid.cmake +++ b/cmake/libraries/stduuid.cmake @@ -11,7 +11,7 @@ if(PROJECT_ENABLE_STDUUID) ExternalProject_Add(stduuid_project PREFIX external URL ${PROJECT_3RD_PARTY_DIR}/stduuid-${STDUUID_VERSION}.tar.gz - URL_HASH SHA256=b1176597e789531c38481acbbed2a6894ad419aab0979c10410d59eb0ebf40d3 + URL_HASH SHA256=${STDUUID_HASH} LIST_SEPARATOR | CMAKE_ARGS ${PROJECT_EXTERNAL_CMAKE_FLAGS} -DBUILD_SHARED_LIBS=${PROJECT_BUILD_SHARED_LIBS} diff --git a/cmake/libraries/testing.cmake b/cmake/libraries/testing.cmake index 03a2de85..5161d42e 100644 --- a/cmake/libraries/testing.cmake +++ b/cmake/libraries/testing.cmake @@ -1,11 +1,11 @@ if (PROJECT_ENABLE_TESTING) if(PROJECT_BUILD) add_definitions(-DPROJECT_ENABLE_TESTING) - elseif(NOT PROJECT_IS_MINGW OR CMAKE_HOST_WIN32) + elseif(NOT PROJECT_IS_MINGW) ExternalProject_Add(gtest_project PREFIX external URL ${PROJECT_3RD_PARTY_DIR}/googletest-${GTEST_VERSION}.tar.gz - URL_HASH SHA256=7315acb6bf10e99f332c8a43f00d5fbb1ee6ca48c52f6b936991b216c586aaad + URL_HASH SHA256=${GTEST_HASH} LIST_SEPARATOR | CMAKE_ARGS ${PROJECT_EXTERNAL_CMAKE_FLAGS} -DBUILD_SHARED_LIBS=${PROJECT_BUILD_SHARED_LIBS} diff --git a/cmake/options.cmake b/cmake/options.cmake index c8ac4db0..5432b5a2 100644 --- a/cmake/options.cmake +++ b/cmake/options.cmake @@ -1,4 +1,3 @@ -option(PROJECT_ENABLE_BACKWARD_CPP "Enable backward-cpp" ON) option(PROJECT_ENABLE_BOOST "Enable boost libraries" ON) option(PROJECT_ENABLE_CPP_HTTPLIB "Enable cpp-httplib" ON) option(PROJECT_ENABLE_CURL "Enable curl library" ON) diff --git a/cmake/versions.cmake b/cmake/versions.cmake index 8e98987a..cd3ac409 100644 --- a/cmake/versions.cmake +++ b/cmake/versions.cmake @@ -6,41 +6,48 @@ set(BOOST_MAJOR_VERSION 1) set(BOOST_MINOR_VERSION 85) set(BOOST_PATCH_VERSION 0) set(CLI11_VERSION 2.4.2) -set(CPP_HTTPLIB_VERSION 0.16.0) -set(CURL_VERSION 8.8.0) -set(CXXOPTS_VERSION 3.2.0) +set(CPP_HTTPLIB_VERSION 0.16.3) +set(CURL2_VERSION 8_9_1) +set(CURL_VERSION 8.9.1) +set(CXXOPTS_VERSION 3.2.1) set(DTL_VERSION 2.01) +set(EXPAT2_VERSION 2_6_2) set(EXPAT_VERSION 2.6.2) set(FLAC_VERSION 1.4.3) -set(FMT_VERSION 10.2.1) +set(FMT_VERSION 11.0.2) set(FONTCONFIG_VERSION 2.15.0) -set(FREETYPE2_VERSION 2.13.2) -set(GCC_VERSION 13.2.0) -set(GTEST_VERSION 1.15.0) +set(FREETYPE2_VERSION 2.13.3) +set(GCC_VERSION 14.2.0) +set(GTEST_VERSION 1.15.2) set(GTKMM_VERSION 3.0) set(ICU_VERSION 75-1) set(JSON_VERSION 3.11.3) set(LIBBITCOIN_SYSTEM_VERSION 3.8.0) +set(LIBDSM_VERSION 0.4.3) set(LIBEVENT_VERSION 2.1.12) +set(LIBICONV_VERSION 1.17) set(LIBJPEG_TURBO_VERSION 3.0.3) set(LIBPNG_VERSION 1.6.43) set(LIBSODIUM_VERSION 1.0.20) +set(LIBTASN_VERSION 4.19.0) set(MESA_VERSION 23.3.3) set(MINGW_VERSION 11.0.1) set(NANA_VERSION 1.7.4) -set(NUSPELL_VERSION 5.1.4) +set(NUSPELL_VERSION 5.1.6) set(OGG_VERSION 1.3.5) set(OPENAL_VERSION 1.23.1) set(OPENSSL_VERSION 3.3.1) set(PKG_CONFIG_VERSION 0.29.2) set(PUGIXML_VERSION 1.14) -set(ROCKSDB_VERSION 9.4.0) -set(SDL_VERSION 2.30.5) +set(ROCKSDB_VERSION 9.5.2) +set(SDL_VERSION 2.30.6) set(SECP256K1_VERSION 0.1.0.20) set(SFML_VERSION 2.6.1) set(SPDLOG_VERSION 1.14.1) -set(SQLITE_VERSION 3460000) +set(SQLITE2_VERSION 3.46.1) +set(SQLITE_VERSION 3460100) set(STDUUID_VERSION 1.2.3) +set(VLC_VERSION 3.0) set(VORBIS_VERSION 1.3.7) set(WXWIDGETS_VERSION 3.2.5) set(ZLIB_VERSION 1.3.1) diff --git a/config.sh b/config.sh index 76bd46f7..eadc79f5 100755 --- a/config.sh +++ b/config.sh @@ -19,7 +19,9 @@ PROJECT_APP_LIST=(${PROJECT_NAME}) PROJECT_PRIVATE_KEY=${DEVELOPER_PRIVATE_KEY} PROJECT_PUBLIC_KEY=${DEVELOPER_PUBLIC_KEY} -PROJECT_ENABLE_BACKWARD_CPP=ON +PROJECT_ENABLE_WIN32_LONG_PATH_NAMES=OFF + +PROJECT_ENABLE_BACKWARD_CPP=OFF PROJECT_ENABLE_BOOST=ON PROJECT_ENABLE_CPP_HTTPLIB=ON PROJECT_ENABLE_CURL=ON @@ -34,7 +36,6 @@ PROJECT_ENABLE_STDUUID=ON PROJECT_ENABLE_TESTING=ON PROJECT_ENABLE_WINFSP=ON -PROJECT_KEEP_BACKWARD_CPP=1 PROJECT_STATIC_LINK=ON PROJECT_MINGW64_COPY_DEPENDENCIES+=() diff --git a/docker/aarch64/alpine b/docker/aarch64/alpine index 77103cec..b0ca5ae3 100644 --- a/docker/aarch64/alpine +++ b/docker/aarch64/alpine @@ -10,6 +10,7 @@ RUN apk add \ bash \ binutils \ binutils-dev \ + bison \ boost-dev \ bzip2-static \ cmake \ diff --git a/docker/x86_64/alpine b/docker/x86_64/alpine index 3baa1759..f23b5b3f 100644 --- a/docker/x86_64/alpine +++ b/docker/x86_64/alpine @@ -10,6 +10,7 @@ RUN apk add \ bash \ binutils \ binutils-dev \ + bison \ boost-dev \ bzip2-static \ cmake \ diff --git a/docker/x86_64/mingw64 b/docker/x86_64/mingw64 index 4de4d4b1..dbfb5ac0 100644 --- a/docker/x86_64/mingw64 +++ b/docker/x86_64/mingw64 @@ -1,4 +1,4 @@ -# comment +#comment FROM alpine:3.20.2 RUN apk update @@ -60,6 +60,9 @@ ENV MY_MINGW_PREFIX=${MINGW_PREFIX} ARG NUM_JOBS=2 ENV MY_NUM_JOBS=${NUM_JOBS} +ARG CXX_STANDARD=20 +ENV MY_CXX_STANDARD=${CXX_STANDARD} + ARG TOOLCHAIN_FILE_CMAKE=/cmake_toolchain.cmake ENV MY_TOOLCHAIN_FILE_CMAKE=${TOOLCHAIN_FILE_CMAKE} RUN echo -e \ @@ -89,8 +92,8 @@ RUN echo -e \ "windres = '${MY_MINGW_PREFIX}-windres'\n"\ "exe_wrapper = 'wine64'\n"\ "[properties]\n"\ - "c_args = []\n"\ - "c_link_args = []\n"\ + "c_args = ['-I${MY_MINGW_DIR}/include']\n"\ + "c_link_args = ['-L${MY_MINGW_DIR}/lib', '-L${MY_MINGW_DIR}/lib64']\n"\ "[host_machine]\n"\ "cpu = 'x86_64'\n"\ "cpu_family = 'x86_64'\n"\ @@ -106,15 +109,12 @@ RUN mkdir -p \ ${MY_MINGW_DIR}/lib/pkgconfig \ ${MY_MINGW_DIR}/lib64/pkgconfig -ENV LDFLAGS="-L${MY_MINGW_DIR}/lib -L${MY_MINGW_DIR}/lib64" -ENV PATH="${MY_MINGW_DIR}/bin:/usr/local/bin:${PATH}" -ENV PKG_CONFIG_PATH="${MY_MINGW_DIR}/lib/pkgconfig:${MY_MINGW_DIR}/lib64/pkgconfig" - ADD ./3rd_party /3rd_party ARG BINUTILS_VERSION ENV MY_BINUTILS_VERSION=${BINUTILS_VERSION} -RUN tar xvJf /3rd_party/mingw64/binutils-${MY_BINUTILS_VERSION}.tar.xz \ +RUN cd /3rd_party/mingw64 && sha256sum -c ./binutils-${MY_BINUTILS_VERSION}.tar.xz.sha256 && cd - \ + && tar xvJf /3rd_party/mingw64/binutils-${MY_BINUTILS_VERSION}.tar.xz \ && cd binutils-${MY_BINUTILS_VERSION} \ && ./configure \ --disable-lto \ @@ -134,10 +134,11 @@ RUN tar xvJf /3rd_party/mingw64/binutils-${MY_BINUTILS_VERSION}.tar.xz \ ARG MINGW_VERSION ENV MY_MINGW_VERSION=${MINGW_VERSION} -RUN tar xvzf /3rd_party/mingw64/mingw-w64-${MY_MINGW_VERSION}.tar.gz \ +RUN cd /3rd_party/mingw64 && sha256sum -c ./mingw-w64-v${MY_MINGW_VERSION}.tar.bz2.sha256 && cd - \ + && tar xvjf /3rd_party/mingw64/mingw-w64-v${MY_MINGW_VERSION}.tar.bz2 \ && mkdir mingw-w64 \ && cd mingw-w64 \ - && ../mingw-w64-${MY_MINGW_VERSION}/mingw-w64-headers/configure \ + && ../mingw-w64-v${MY_MINGW_VERSION}/mingw-w64-headers/configure \ --enable-sdk=all \ --host=${MY_MINGW_PREFIX} \ --prefix=/usr/local/${MY_MINGW_PREFIX} \ @@ -145,7 +146,8 @@ RUN tar xvzf /3rd_party/mingw64/mingw-w64-${MY_MINGW_VERSION}.tar.gz \ ARG GCC_VERSION ENV MY_GCC_VERSION=${GCC_VERSION} -RUN tar xvzf /3rd_party/mingw64/gcc-${MY_GCC_VERSION}.tar.gz \ +RUN cd /3rd_party/mingw64 && sha256sum -c ./gcc-${MY_GCC_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/mingw64/gcc-${MY_GCC_VERSION}.tar.gz \ && (mv gcc-releases-gcc-${MY_GCC_VERSION} gcc-${MY_GCC_VERSION} || echo "") \ && mkdir gcc \ && cd gcc \ @@ -188,7 +190,7 @@ RUN tar xvzf /3rd_party/mingw64/gcc-${MY_GCC_VERSION}.tar.gz \ && make install-gcc RUN cd mingw-w64 \ - && ../mingw-w64-${MY_MINGW_VERSION}/mingw-w64-crt/configure \ + && ../mingw-w64-v${MY_MINGW_VERSION}/mingw-w64-crt/configure \ --disable-lib32 \ --enable-lib64 \ --enable-wildcard \ @@ -198,7 +200,7 @@ RUN cd mingw-w64 \ && make install RUN cd mingw-w64 \ - && ../mingw-w64-${MY_MINGW_VERSION}/mingw-w64-libraries/winpthreads/configure \ + && ../mingw-w64-v${MY_MINGW_VERSION}/mingw-w64-libraries/winpthreads/configure \ --enable-shared \ --enable-static \ --host=${MY_MINGW_PREFIX} \ @@ -213,11 +215,12 @@ RUN cd gcc \ RUN cp /usr/local/${MY_MINGW_PREFIX}/lib/*.dll ${MY_MINGW_DIR}/bin \ && cp /usr/local/${MY_MINGW_PREFIX}/bin/*.dll ${MY_MINGW_DIR}/bin \ && rm -r gcc gcc-${MY_GCC_VERSION} \ - && rm -r mingw-w64 mingw-w64-${MY_MINGW_VERSION} + && rm -r mingw-w64 mingw-w64-v${MY_MINGW_VERSION} ARG PKG_CONFIG_VERSION ENV MY_PKG_CONFIG_VERSION=${PKG_CONFIG_VERSION} -RUN tar xvzf /3rd_party/mingw64/pkg-config-${MY_PKG_CONFIG_VERSION}.tar.gz \ +RUN cd /3rd_party/mingw64 && sha256sum -c ./pkg-config-${MY_PKG_CONFIG_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/mingw64/pkg-config-${MY_PKG_CONFIG_VERSION}.tar.gz \ && cd pkg-config-${MY_PKG_CONFIG_VERSION} \ && ./configure \ --disable-nls \ @@ -234,16 +237,24 @@ RUN python3 -m pip install --break-system-packages -U mako RUN python3 -m pip install --break-system-packages -U meson RUN python3 -m pip install --break-system-packages -U packaging +ENV CXXFLAGS="-std=gnu++20" +ENV LDFLAGS="-L${MY_MINGW_DIR}/lib -L${MY_MINGW_DIR}/lib64" +ENV PATH="${MY_MINGW_DIR}/bin:/usr/local/bin:${PATH}" +ENV PKG_CONFIG_PATH="${MY_MINGW_DIR}/lib/pkgconfig:${MY_MINGW_DIR}/lib64/pkgconfig" + ARG ZLIB_VERSION ENV MY_ZLIB_VERSION=${ZLIB_VERSION} -RUN tar xvzf /3rd_party/mingw64/zlib-${MY_ZLIB_VERSION}.tar.gz \ +RUN cd /3rd_party/mingw64 && sha256sum -c ./zlib-${MY_ZLIB_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/mingw64/zlib-${MY_ZLIB_VERSION}.tar.gz \ && cd zlib-${MY_ZLIB_VERSION} \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ -DBUILD_SHARED_LIBS=ON \ -DBUILD_STATIC_LIBS=ON \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ && make -j${MY_NUM_JOBS} \ && make install \ && cd ${MY_WORKDIR} \ @@ -252,18 +263,21 @@ RUN tar xvzf /3rd_party/mingw64/zlib-${MY_ZLIB_VERSION}.tar.gz \ ARG LIBJPEG_TURBO_VERSION ENV MY_LIBJPEG_TURBO_VERSION=${LIBJPEG_TURBO_VERSION} RUN if [ -f "/3rd_party/libjpeg_turbo-${MY_LIBJPEG_TURBO_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/libjpeg_turbo-${MY_LIBJPEG_TURBO_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./libjpeg_turbo-${MY_LIBJPEG_TURBO_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/libjpeg_turbo-${MY_LIBJPEG_TURBO_VERSION}.tar.gz \ && cd libjpeg-turbo-${MY_LIBJPEG_TURBO_VERSION} \ && mkdir _build \ && cd _build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ - -DCMAKE_SYSTEM_PROCESSOR=AMD64 \ + && cmake .. \ -DBUILD_SHARED_LIBS=ON \ -DBUILD_STATIC_LIBS=ON \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ + -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_SYSTEM_PROCESSOR=AMD64 \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ -DENABLE_SHARED=ON \ -DENABLE_STATIC=ON \ - -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ && make -j${MY_NUM_JOBS} \ && make install \ && cd ${MY_WORKDIR} \ @@ -271,17 +285,20 @@ RUN if [ -f "/3rd_party/libjpeg_turbo-${MY_LIBJPEG_TURBO_VERSION}.tar.gz" ]; the ; fi ARG LIBPNG_VERSION -ENV MY_LIBPNG_VERSION=${LIBPNG_VERSION} +ENV MY_LIBPNG_VERSION=${LIBPNG_VERSION} RUN if [ -f "/3rd_party/libpng-v${MY_LIBPNG_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/libpng-v${MY_LIBPNG_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./libpng-v${MY_LIBPNG_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/libpng-v${MY_LIBPNG_VERSION}.tar.gz \ && cd libpng-${MY_LIBPNG_VERSION} \ && mkdir _build \ && cd _build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ -DBUILD_SHARED_LIBS=ON \ -DBUILD_STATIC_LIBS=ON \ -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ -DPNG_SHARED=ON \ -DPNG_STATIC=ON \ -DPNG_TESTS=OFF \ @@ -295,7 +312,8 @@ RUN if [ -f "/3rd_party/libpng-v${MY_LIBPNG_VERSION}.tar.gz" ]; then \ ARG FREETYPE2_VERSION ENV MY_FREETYPE2_VERSION=${FREETYPE2_VERSION} RUN if [ -f "/3rd_party/freetype-${MY_FREETYPE2_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/freetype-${MY_FREETYPE2_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./freetype-${MY_FREETYPE2_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/freetype-${MY_FREETYPE2_VERSION}.tar.gz \ && cd freetype-${MY_FREETYPE2_VERSION} \ && meson setup \ --cross-file ${MY_TOOLCHAIN_FILE_MESON} \ @@ -311,26 +329,30 @@ RUN if [ -f "/3rd_party/freetype-${MY_FREETYPE2_VERSION}.tar.gz" ]; then \ ARG EXPAT_VERSION ENV MY_EXPAT_VERSION=${EXPAT_VERSION} -RUN tar xvzf /3rd_party/mingw64/expat-${MY_EXPAT_VERSION}.tar.gz \ - && cd expat-${MY_EXPAT_VERSION} \ +RUN cd /3rd_party/mingw64 && sha256sum -c ./expat-${MY_EXPAT_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/mingw64/expat-${MY_EXPAT_VERSION}.tar.gz \ + && cd libexpat-*/expat \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ -DBUILD_SHARED_LIBS=ON \ -DBUILD_STATIC_LIBS=ON \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ -DEXPAT_BUILD_DOCS=OFF \ -DEXPAT_BUILD_EXAMPLES=OFF \ -DEXPAT_BUILD_TESTS=OFF \ && make -j${MY_NUM_JOBS} \ && make install \ && cd ${MY_WORKDIR} \ - && rm -r expat-${MY_EXPAT_VERSION} + && rm -r libexpat-* ARG FONTCONFIG_VERSION ENV MY_FONTCONFIG_VERSION=${FONTCONFIG_VERSION} RUN if [ -f "/3rd_party/fontconfig-${MY_FONTCONFIG_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/fontconfig-${MY_FONTCONFIG_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./fontconfig-${MY_FONTCONFIG_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/fontconfig-${MY_FONTCONFIG_VERSION}.tar.gz \ && cd fontconfig-${MY_FONTCONFIG_VERSION} \ && meson setup \ --cross-file ${MY_TOOLCHAIN_FILE_MESON} \ @@ -350,12 +372,15 @@ RUN if [ -f "/3rd_party/fontconfig-${MY_FONTCONFIG_VERSION}.tar.gz" ]; then \ ARG OPENAL_VERSION ENV MY_OPENAL_VERSION=${OPENAL_VERSION} RUN if [ -f "/3rd_party/openal-${MY_OPENAL_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/openal-${MY_OPENAL_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./openal-${MY_OPENAL_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/openal-${MY_OPENAL_VERSION}.tar.gz \ && cd openal-soft-${MY_OPENAL_VERSION} \ && mkdir _build \ && cd _build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ && make -j${MY_NUM_JOBS} \ && make install \ && mv ${MY_MINGW_DIR}/bin/OpenAL32.dll ${MY_MINGW_DIR}/bin/openal32.dll \ @@ -365,10 +390,11 @@ RUN if [ -f "/3rd_party/openal-${MY_OPENAL_VERSION}.tar.gz" ]; then \ ARG ICU_VERSION ENV MY_ICU_VERSION=${ICU_VERSION} -RUN tar xvzf /3rd_party/mingw64/icu-release-${MY_ICU_VERSION}.tar.gz \ +RUN cd /3rd_party/mingw64 && sha256sum -c ./icu-release-${MY_ICU_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/mingw64/icu-release-${MY_ICU_VERSION}.tar.gz \ && cp -r icu-release-${MY_ICU_VERSION} icu-release-${MY_ICU_VERSION}-cross \ && cd icu-release-${MY_ICU_VERSION}-cross/icu4c/source \ - && ./configure \ + && CXXFLAGS="-std=gnu++17" ./configure \ --disable-samples \ --disable-tests \ --enable-shared \ @@ -379,7 +405,7 @@ RUN tar xvzf /3rd_party/mingw64/icu-release-${MY_ICU_VERSION}.tar.gz \ RUN cd ${MY_WORKDIR} \ && cd icu-release-${MY_ICU_VERSION}/icu4c/source \ - && ./configure \ + && CXXFLAGS="-std=gnu++17" ./configure \ --build=x86_64-alpine-linux-musl \ --disable-samples \ --disable-tests \ @@ -398,7 +424,8 @@ RUN cd ${MY_WORKDIR} \ ARG OPENSSL_VERSION ENV MY_OPENSSL_VERSION=${OPENSSL_VERSION} RUN if [ -f "/3rd_party/openssl-${MY_OPENSSL_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/openssl-${MY_OPENSSL_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./openssl-${MY_OPENSSL_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/openssl-${MY_OPENSSL_VERSION}.tar.gz \ && cd openssl-${MY_OPENSSL_VERSION} \ && ./Configure \ --cross-compile-prefix=${MY_MINGW_PREFIX}- \ @@ -421,11 +448,14 @@ ARG BOOST2_PATCH_VERSION ENV MY_BOOST2_PATCH_VERSION=${BOOST2_PATCH_VERSION} ENV MY_BOOST2_VERSION=${MY_BOOST2_MAJOR_VERSION}_${MY_BOOST2_MINOR_VERSION}_${MY_BOOST2_PATCH_VERSION} RUN if [ -f "/3rd_party/boost_${MY_BOOST2_MAJOR_VERSION}_${MY_BOOST2_MINOR_VERSION}_${MY_BOOST2_PATCH_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/boost_${MY_BOOST2_MAJOR_VERSION}_${MY_BOOST2_MINOR_VERSION}_${MY_BOOST2_PATCH_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./boost_${MY_BOOST2_MAJOR_VERSION}_${MY_BOOST2_MINOR_VERSION}_${MY_BOOST2_PATCH_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/boost_${MY_BOOST2_MAJOR_VERSION}_${MY_BOOST2_MINOR_VERSION}_${MY_BOOST2_PATCH_VERSION}.tar.gz \ && cd boost_${MY_BOOST2_VERSION} \ && ./bootstrap.sh \ --with-libraries=atomic,chrono,date_time,filesystem,iostreams,locale,log,program_options,random,regex,serialization,system,test,thread \ address-model=64 \ + cxxstd=${MY_CXX_STANDARD} \ + cxxstd-dialect=gnu \ architecture=x86 \ link=static,shared \ target-os=windows \ @@ -438,6 +468,8 @@ RUN if [ -f "/3rd_party/boost_${MY_BOOST2_MAJOR_VERSION}_${MY_BOOST2_MINOR_VERSI --prefix=${MY_MINGW_DIR} \ -j${MY_NUM_JOBS} \ address-model=64 \ + cxxstd=${MY_CXX_STANDARD} \ + cxxstd-dialect=gnu \ architecture=x86 \ link=static,shared \ target-os=windows \ @@ -457,7 +489,8 @@ ARG BOOST_PATCH_VERSION ENV MY_BOOST_PATCH_VERSION=${BOOST_PATCH_VERSION} ENV MY_BOOST_VERSION=${MY_BOOST_MAJOR_VERSION}_${MY_BOOST_MINOR_VERSION}_${MY_BOOST_PATCH_VERSION} RUN if [ -f "/3rd_party/boost_${MY_BOOST_MAJOR_VERSION}_${MY_BOOST_MINOR_VERSION}_${MY_BOOST_PATCH_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/boost_${MY_BOOST_MAJOR_VERSION}_${MY_BOOST_MINOR_VERSION}_${MY_BOOST_PATCH_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./boost_${MY_BOOST_MAJOR_VERSION}_${MY_BOOST_MINOR_VERSION}_${MY_BOOST_PATCH_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/boost_${MY_BOOST_MAJOR_VERSION}_${MY_BOOST_MINOR_VERSION}_${MY_BOOST_PATCH_VERSION}.tar.gz \ && cd boost_${MY_BOOST_VERSION} \ && echo "using gcc : gcc : ${MY_MINGW_PREFIX}-g++ ;" \ >./user-config.jam \ @@ -465,6 +498,8 @@ RUN if [ -f "/3rd_party/boost_${MY_BOOST_MAJOR_VERSION}_${MY_BOOST_MINOR_VERSION --with-libraries=atomic,chrono,date_time,filesystem,iostreams,locale,log,program_options,random,regex,serialization,system,test,thread \ address-model=64 \ architecture=x86 \ + cxxstd=${MY_CXX_STANDARD} \ + cxxstd-dialect=gnu \ link=static,shared \ target-os=windows \ threading=multi \ @@ -476,6 +511,8 @@ RUN if [ -f "/3rd_party/boost_${MY_BOOST_MAJOR_VERSION}_${MY_BOOST_MINOR_VERSION -j${MY_NUM_JOBS} \ address-model=64 \ architecture=x86 \ + cxxstd=${MY_CXX_STANDARD} \ + cxxstd-dialect=gnu \ link=static,shared \ target-os=windows \ threading=multi \ @@ -488,30 +525,37 @@ RUN if [ -f "/3rd_party/boost_${MY_BOOST_MAJOR_VERSION}_${MY_BOOST_MINOR_VERSION ARG OGG_VERSION ENV MY_OGG_VERSION=${OGG_VERSION} RUN if [ -f "/3rd_party/ogg-v${MY_OGG_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/ogg-v${MY_OGG_VERSION}.tar.gz \ - && cd ogg-${MY_OGG_VERSION} \ + cd /3rd_party && sha256sum -c ./ogg-v${MY_OGG_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/ogg-v${MY_OGG_VERSION}.tar.gz \ + && cd libogg-${MY_OGG_VERSION} \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ -DBUILD_SHARED_LIBS=ON \ -DBUILD_STATIC_LIBS=ON \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ && make -j${MY_NUM_JOBS} \ && make install \ && cd ${MY_WORKDIR} \ - && rm -r ogg-${MY_OGG_VERSION} \ + && rm -r libogg-${MY_OGG_VERSION} \ ; fi ARG VORBIS_VERSION ENV MY_VORBIS_VERSION=${VORBIS_VERSION} RUN if [ -f "/3rd_party/vorbis-v${MY_VORBIS_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/vorbis-v${MY_VORBIS_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./vorbis-v${MY_VORBIS_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/vorbis-v${MY_VORBIS_VERSION}.tar.gz \ && cd vorbis-${MY_VORBIS_VERSION} \ + && /3rd_party/vorbis_patch.sh /3rd_party . \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ -DBUILD_SHARED_LIBS=OFF \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ && make -j${MY_NUM_JOBS} \ && make install \ && cd ${MY_WORKDIR} \ @@ -521,18 +565,21 @@ RUN if [ -f "/3rd_party/vorbis-v${MY_VORBIS_VERSION}.tar.gz" ]; then \ ARG FLAC_VERSION ENV MY_FLAC_VERSION=${FLAC_VERSION} RUN if [ -f "/3rd_party/flac-${MY_FLAC_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/flac-${MY_FLAC_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./flac-${MY_FLAC_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/flac-${MY_FLAC_VERSION}.tar.gz \ && cd flac-${MY_FLAC_VERSION} \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ - -DBUILD_SHARED_LIBS=ON \ - -DBUILD_STATIC_LIBS=ON \ - -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + && cmake .. \ + -DBUILD_DOCS=OFF \ -DBUILD_EXAMPLES=ON \ -DBUILD_PROGRAMS=ON \ + -DBUILD_SHARED_LIBS=ON \ + -DBUILD_STATIC_LIBS=ON \ -DBUILD_TESTING=OFF \ - -DBUILD_DOCS=OFF \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ + -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ -DINSTALL_MANPAGES=OFF \ && make -j${MY_NUM_JOBS} \ && make install \ @@ -543,7 +590,8 @@ RUN if [ -f "/3rd_party/flac-${MY_FLAC_VERSION}.tar.gz" ]; then \ ARG SFML_VERSION ENV MY_SFML_VERSION=${SFML_VERSION} RUN if [ -f "/3rd_party/SFML-${MY_SFML_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/SFML-${MY_SFML_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./SFML-${MY_SFML_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/SFML-${MY_SFML_VERSION}.tar.gz \ && cd SFML-${MY_SFML_VERSION} \ && sed -i s/set_target_properties\(\$\{target\}\ PROPERTIES\ PREFIX\ \"\"\)// \ cmake/Macros.cmake \ @@ -551,10 +599,12 @@ RUN if [ -f "/3rd_party/SFML-${MY_SFML_VERSION}.tar.gz" ]; then \ cmake/Macros.cmake \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ -DBUILD_SHARED_LIBS=ON \ -DBUILD_STATIC_LIBS=ON \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ && make -j${MY_NUM_JOBS} \ && make install \ && cd ${MY_WORKDIR} \ @@ -564,14 +614,16 @@ RUN if [ -f "/3rd_party/SFML-${MY_SFML_VERSION}.tar.gz" ]; then \ ARG SPDLOG_VERSION ENV MY_SPDLOG_VERSION=${SPDLOG_VERSION} RUN if [ -f "/3rd_party/spdlog-${MY_SPDLOG_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/spdlog-${MY_SPDLOG_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./spdlog-${MY_SPDLOG_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/spdlog-${MY_SPDLOG_VERSION}.tar.gz \ && cd spdlog-${MY_SPDLOG_VERSION} \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ + -DBUILD_SHARED_LIBS=OFF \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ - -DSPDLOG_BUILD_SHARED=ON \ - -DSPDLOG_BUILD_STATIC=ON \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ -DSPDLOG_BUILD_EXAMPLE=OFF \ -DSPDLOG_FMT_EXTERNAL=OFF \ -DSPDLOG_FMT_EXTERNAL_HO=OFF \ @@ -584,19 +636,22 @@ RUN if [ -f "/3rd_party/spdlog-${MY_SPDLOG_VERSION}.tar.gz" ]; then \ ARG CLI11_VERSION ENV MY_CLI11_VERSION=${CLI11_VERSION} RUN if [ -f "/3rd_party/CLI11-${MY_CLI11_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/CLI11-${MY_CLI11_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./CLI11-${MY_CLI11_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/CLI11-${MY_CLI11_VERSION}.tar.gz \ && cd CLI11-${MY_CLI11_VERSION} \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ - -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ - -DCLI11_WARNINGS_AS_ERRORS=OFF \ - -DCLI11_SINGLE_FILE=ON \ + && cmake .. \ -DCLI11_BUILD_DOCS=OFF \ - -DCLI11_BUILD_TESTS=OFF \ -DCLI11_BUILD_EXAMPLES=OFF \ + -DCLI11_BUILD_TESTS=OFF \ -DCLI11_INSTALL=ON \ + -DCLI11_SINGLE_FILE=ON \ + -DCLI11_WARNINGS_AS_ERRORS=OFF \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ + -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ && make -j${MY_NUM_JOBS} \ && make install \ && cd ${MY_WORKDIR} \ @@ -605,17 +660,22 @@ RUN if [ -f "/3rd_party/CLI11-${MY_CLI11_VERSION}.tar.gz" ]; then \ ARG CURL_VERSION ENV MY_CURL_VERSION=${CURL_VERSION} +ARG CURL2_VERSION +ENV MY_CURL2_VERSION=${CURL2_VERSION} RUN if [ -f "/3rd_party/curl-${MY_CURL_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/curl-${MY_CURL_VERSION}.tar.gz \ - && cd curl-${MY_CURL_VERSION} \ + cd /3rd_party && sha256sum -c ./curl-${MY_CURL_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/curl-${MY_CURL_VERSION}.tar.gz \ + && cd curl-curl-${MY_CURL2_VERSION} \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ -DBUILD_CURL_EXE=ON \ -DBUILD_SHARED_LIBS=OFF \ -DBUILD_STATIC_LIBS=ON \ -DBUILD_TESTING=OFF \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ -DCURL_CA_BUNDLE=./cacert.pem \ -DCURL_CA_FALLBACK=ON \ -DCURL_DISABLE_LDAP=ON \ @@ -630,22 +690,25 @@ RUN if [ -f "/3rd_party/curl-${MY_CURL_VERSION}.tar.gz" ]; then \ && make -j${MY_NUM_JOBS} \ && make install \ && cd ${MY_WORKDIR} \ - && rm -r curl-${MY_CURL_VERSION} \ + && rm -r curl-curl-${MY_CURL2_VERSION} \ ; fi ARG CPP_HTTPLIB_VERSION ENV MY_CPP_HTTPLIB_VERSION=${CPP_HTTPLIB_VERSION} RUN if [ -f "/3rd_party/cpp-httplib-${MY_CPP_HTTPLIB_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/cpp-httplib-${MY_CPP_HTTPLIB_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./cpp-httplib-${MY_CPP_HTTPLIB_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/cpp-httplib-${MY_CPP_HTTPLIB_VERSION}.tar.gz \ && cd cpp-httplib-${MY_CPP_HTTPLIB_VERSION} \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + -DHTTPLIB_REQUIRE_BROTLI=OFF \ -DHTTPLIB_REQUIRE_OPENSSL=ON \ -DHTTPLIB_REQUIRE_ZLIB=ON \ - -DHTTPLIB_REQUIRE_BROTLI=OFF \ -DHTTPLIB_TEST=OFF \ && make -j${MY_NUM_JOBS} \ && make install \ @@ -656,14 +719,17 @@ RUN if [ -f "/3rd_party/cpp-httplib-${MY_CPP_HTTPLIB_VERSION}.tar.gz" ]; then \ ARG FMT_VERSION ENV MY_FMT_VERSION=${FMT_VERSION} RUN if [ -f "/3rd_party/fmt-${MY_FMT_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/fmt-${MY_FMT_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./fmt-${MY_FMT_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/fmt-${MY_FMT_VERSION}.tar.gz \ && cd fmt-${MY_FMT_VERSION} \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ - -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + && cmake .. \ -DBUILD_SHARED_LIBS=ON \ -DBUILD_STATIC_LIBS=ON \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ + -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ -DFMT_DOC=OFF \ -DFMT_TEST=OFF \ && make -j${MY_NUM_JOBS} \ @@ -675,13 +741,16 @@ RUN if [ -f "/3rd_party/fmt-${MY_FMT_VERSION}.tar.gz" ]; then \ ARG GTEST_VERSION ENV MY_GTEST_VERSION=${GTEST_VERSION} RUN if [ -f "/3rd_party/googletest-${MY_GTEST_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/googletest-${MY_GTEST_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./googletest-${MY_GTEST_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/googletest-${MY_GTEST_VERSION}.tar.gz \ && cd googletest-${MY_GTEST_VERSION} \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ && make -j${MY_NUM_JOBS} \ && make install \ && cd ${MY_WORKDIR} \ @@ -691,13 +760,16 @@ RUN if [ -f "/3rd_party/googletest-${MY_GTEST_VERSION}.tar.gz" ]; then \ ARG JSON_VERSION ENV MY_JSON_VERSION=${JSON_VERSION} RUN if [ -f "/3rd_party/json-${MY_JSON_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/json-${MY_JSON_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./json-${MY_JSON_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/json-${MY_JSON_VERSION}.tar.gz \ && cd json-${MY_JSON_VERSION} \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ -DJSON_BuildTests=OFF \ -DJSON_Install=ON \ -DJSON_MultipleHeaders=OFF \ @@ -710,13 +782,16 @@ RUN if [ -f "/3rd_party/json-${MY_JSON_VERSION}.tar.gz" ]; then \ ARG LIBEVENT_VERSION ENV MY_LIBEVENT_VERSION=${LIBEVENT_VERSION} RUN if [ -f "/3rd_party/libevent-${MY_LIBEVENT_VERSION}-stable.tar.gz" ]; then \ - tar xvzf /3rd_party/libevent-${MY_LIBEVENT_VERSION}-stable.tar.gz \ - && cd libevent-${MY_LIBEVENT_VERSION}-stable \ + cd /3rd_party && sha256sum -c ./libevent-${MY_LIBEVENT_VERSION}-stable.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/libevent-${MY_LIBEVENT_VERSION}-stable.tar.gz \ + && cd libevent-release-${MY_LIBEVENT_VERSION}-stable \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ -DEVENT__DISABLE_OPENSSL=ON \ -DEVENT__DISABLE_SAMPLES=ON \ -DEVENT__DISABLE_TESTS=ON \ @@ -724,14 +799,15 @@ RUN if [ -f "/3rd_party/libevent-${MY_LIBEVENT_VERSION}-stable.tar.gz" ]; then \ && make -j${MY_NUM_JOBS} \ && make install \ && cd ${MY_WORKDIR} \ - && rm -r libevent-${MY_LIBEVENT_VERSION}-stable \ + && rm -r libevent-release-${MY_LIBEVENT_VERSION}-stable \ ; fi ARG LIBSODIUM_VERSION ENV MY_LIBSODIUM_VERSION=${LIBSODIUM_VERSION} RUN if [ -f "/3rd_party/libsodium-${MY_LIBSODIUM_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/libsodium-${MY_LIBSODIUM_VERSION}.tar.gz \ - && cd libsodium-${MY_LIBSODIUM_VERSION} \ + cd /3rd_party && sha256sum -c ./libsodium-${MY_LIBSODIUM_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/libsodium-${MY_LIBSODIUM_VERSION}.tar.gz \ + && cd libsodium-${MY_LIBSODIUM_VERSION}-RELEASE \ && CFLAGS="-O3 -fomit-frame-pointer -m64 -mtune=generic" ./configure \ --enable-shared=yes \ --enable-static=yes \ @@ -740,21 +816,25 @@ RUN if [ -f "/3rd_party/libsodium-${MY_LIBSODIUM_VERSION}.tar.gz" ]; then \ && make -j${MY_NUM_JOBS} \ && make install \ && cd ${MY_WORKDIR} \ - && rm -r libsodium-${MY_LIBSODIUM_VERSION} \ + && rm -r libsodium-${MY_LIBSODIUM_VERSION}-RELEASE \ ; fi ARG NUSPELL_VERSION ENV MY_NUSPELL_VERSION=${NUSPELL_VERSION} RUN if [ -f "/3rd_party/nuspell-v${MY_NUSPELL_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/nuspell-v${MY_NUSPELL_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./nuspell-v${MY_NUSPELL_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/nuspell-v${MY_NUSPELL_VERSION}.tar.gz \ && cd nuspell-${MY_NUSPELL_VERSION} \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ - -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + && cmake .. \ + -DBUILD_DOCS=OFF \ -DBUILD_TESTING=OFF \ -DBUILD_TOOLS=OFF \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ + -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ && make -j${MY_NUM_JOBS} \ && make install \ && cd ${MY_WORKDIR} \ @@ -764,13 +844,16 @@ RUN if [ -f "/3rd_party/nuspell-v${MY_NUSPELL_VERSION}.tar.gz" ]; then \ ARG PUGIXML_VERSION ENV MY_PUGIXML_VERSION=${PUGIXML_VERSION} RUN if [ -f "/3rd_party/pugixml-${MY_PUGIXML_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/pugixml-${MY_PUGIXML_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./pugixml-${MY_PUGIXML_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/pugixml-${MY_PUGIXML_VERSION}.tar.gz \ && cd pugixml-${MY_PUGIXML_VERSION} \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ && make -j${MY_NUM_JOBS} \ && make install \ && cd ${MY_WORKDIR} \ @@ -780,14 +863,17 @@ RUN if [ -f "/3rd_party/pugixml-${MY_PUGIXML_VERSION}.tar.gz" ]; then \ ARG ROCKSDB_VERSION ENV MY_ROCKSDB_VERSION=${ROCKSDB_VERSION} RUN if [ -f "/3rd_party/rocksdb-${MY_ROCKSDB_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/rocksdb-${MY_ROCKSDB_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./rocksdb-${MY_ROCKSDB_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/rocksdb-${MY_ROCKSDB_VERSION}.tar.gz \ && cd rocksdb-${MY_ROCKSDB_VERSION} \ && echo -e "add_definitions(-include cstdint)">>CMakeLists.txt \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ -DFAIL_ON_WARNINGS=OFF \ -DPORTABLE=1 \ -DROCKSDB_INSTALL_ON_WINDOWS=ON \ @@ -811,7 +897,8 @@ RUN if [ -f "/3rd_party/rocksdb-${MY_ROCKSDB_VERSION}.tar.gz" ]; then \ ARG SECP256K1_VERSION ENV MY_SECP256K1_VERSION=${SECP256K1_VERSION} RUN if [ -f "/3rd_party/secp256k1-${MY_SECP256K1_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/secp256k1-${MY_SECP256K1_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./secp256k1-${MY_SECP256K1_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/secp256k1-${MY_SECP256K1_VERSION}.tar.gz \ && cd secp256k1-${MY_SECP256K1_VERSION} \ && ./autogen.sh && ./configure \ --host=${MY_MINGW_PREFIX} \ @@ -829,13 +916,16 @@ RUN if [ -f "/3rd_party/secp256k1-${MY_SECP256K1_VERSION}.tar.gz" ]; then \ ARG STDUUID_VERSION ENV MY_STDUUID_VERSION=${STDUUID_VERSION} RUN if [ -f "/3rd_party/stduuid-${MY_STDUUID_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/stduuid-${MY_STDUUID_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./stduuid-${MY_STDUUID_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/stduuid-${MY_STDUUID_VERSION}.tar.gz \ && cd stduuid-${MY_STDUUID_VERSION} \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ -DUUID_BUILD_TESTS=OFF \ -DUUID_ENABLE_INSTALL=ON \ -DUUID_USING_CXX20_SPAN=ON \ @@ -846,16 +936,19 @@ RUN if [ -f "/3rd_party/stduuid-${MY_STDUUID_VERSION}.tar.gz" ]; then \ ; fi RUN if [ -f "/3rd_party/tiny-process-library.tar.gz" ]; then \ - tar xvzf /3rd_party/tiny-process-library.tar.gz \ + cd /3rd_party && sha256sum -c ./tiny-process-library.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/tiny-process-library.tar.gz \ && cd tiny-process-library-master \ && mkdir build \ && cd build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ - -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + && cmake .. \ -DBUILD_SHARED_LIBS=ON \ -DBUILD_STATIC_LIBS=ON \ -DBUILD_TESTING=OFF \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ + -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ && make -j${MY_NUM_JOBS} \ && make install \ && cd ${MY_WORKDIR} \ @@ -865,15 +958,18 @@ RUN if [ -f "/3rd_party/tiny-process-library.tar.gz" ]; then \ ARG WXWIDGETS_VERSION ENV MY_WXWIDGETS_VERSION=${WXWIDGETS_VERSION} RUN if [ -f "/3rd_party/wxWidgets-${MY_WXWIDGETS_VERSION}.tar.bz2" ]; then \ - tar xvjf /3rd_party/wxWidgets-${MY_WXWIDGETS_VERSION}.tar.bz2 \ + cd /3rd_party && sha256sum -c ./wxWidgets-${MY_WXWIDGETS_VERSION}.tar.bz2.sha256 && cd - \ + && tar xvjf /3rd_party/wxWidgets-${MY_WXWIDGETS_VERSION}.tar.bz2 \ && cd wxWidgets-${MY_WXWIDGETS_VERSION} \ && mkdir _build \ && cd _build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ -DBUILD_SHARED_LIBS=ON \ -DBUILD_STATIC_LIBS=ON \ -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ -DwxBUILD_MONOLITHIC=OFF \ -DwxBUILD_SHARED=ON \ && make -j${MY_NUM_JOBS} \ @@ -885,15 +981,18 @@ RUN if [ -f "/3rd_party/wxWidgets-${MY_WXWIDGETS_VERSION}.tar.bz2" ]; then \ ARG CXXOPTS_VERSION ENV MY_CXXOPTS_VERSION=${CXXOPTS_VERSION} RUN if [ -f "/3rd_party/cxxopts-v${MY_CXXOPTS_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/cxxopts-v${MY_CXXOPTS_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./cxxopts-v${MY_CXXOPTS_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/cxxopts-v${MY_CXXOPTS_VERSION}.tar.gz \ && cd cxxopts-${MY_CXXOPTS_VERSION} \ && mkdir _build \ && cd _build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ -DBUILD_SHARED_LIBS=ON \ -DBUILD_STATIC_LIBS=ON \ -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ -DCXXOPTS_BUILD_EXAMPLES=OFF \ -DCXXOPTS_BUILD_TESTS=OFF \ -DCXXOPTS_ENABLE_INSTALL=ON \ @@ -908,15 +1007,19 @@ RUN if [ -f "/3rd_party/cxxopts-v${MY_CXXOPTS_VERSION}.tar.gz" ]; then \ ARG NANA_VERSION ENV MY_NANA_VERSION=${NANA_VERSION} RUN if [ -f "/3rd_party/nana-v${MY_NANA_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/nana-v${MY_NANA_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./nana-v${MY_NANA_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/nana-v${MY_NANA_VERSION}.tar.gz \ && cd nana-${MY_NANA_VERSION} \ + && /3rd_party/nana_patch.sh /3rd_party . \ && mkdir _build \ && cd _build \ - && cmake .. -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ + && cmake .. \ -DBUILD_SHARED_LIBS=ON \ -DBUILD_STATIC_LIBS=ON \ -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CXX_STANDARD=${MY_CXX_STANDARD} \ -DCMAKE_INSTALL_PREFIX=${MY_MINGW_DIR} \ + -DCMAKE_TOOLCHAIN_FILE=${MY_TOOLCHAIN_FILE_CMAKE} \ -DNANA_CMAKE_ENABLE_AUDIO=OFF \ -DNANA_CMAKE_ENABLE_JPEG=ON \ -DNANA_CMAKE_ENABLE_PNG=ON \ @@ -930,7 +1033,8 @@ RUN if [ -f "/3rd_party/nana-v${MY_NANA_VERSION}.tar.gz" ]; then \ ARG SDL_VERSION ENV MY_SDL_VERSION=${SDL_VERSION} RUN if [ -f "/3rd_party/sdl-${MY_SDL_VERSION}.tar.gz" ]; then \ - tar xvzf /3rd_party/sdl-${MY_SDL_VERSION}.tar.gz \ + cd /3rd_party && sha256sum -c ./sdl-${MY_SDL_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/sdl-${MY_SDL_VERSION}.tar.gz \ && cd SDL-release-${MY_SDL_VERSION} \ && ./configure \ --host=${MY_MINGW_PREFIX} \ @@ -943,6 +1047,69 @@ RUN if [ -f "/3rd_party/sdl-${MY_SDL_VERSION}.tar.gz" ]; then \ && rm -r SDL-release-${MY_SDL_VERSION} \ ; fi +ARG LIBTASN_VERSION +ENV MY_LIBTASN_VERSION=${LIBTASN_VERSION} +RUN if [ -f "/3rd_party/libtasn1-${MY_LIBTASN_VERSION}.tar.gz" ]; then \ + cd /3rd_party && sha256sum -c ./libtasn1-${MY_LIBTASN_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/libtasn1-${MY_LIBTASN_VERSION}.tar.gz \ + && cd libtasn1-${MY_LIBTASN_VERSION} \ + && ./configure \ + --disable-doc \ + --enable-static=yes \ + --enable-shared=no \ + --host=${MY_MINGW_PREFIX} \ + --prefix=${MY_MINGW_DIR} \ + && make -j${MY_NUM_JOBS} \ + && make install \ + && cd ${MY_WORKDIR} \ + && rm -r libtasn1-${MY_LIBTASN_VERSION} \ + && echo -e \ + "#!/bin/bash\n"\ + "\n"\ + "wine ${MY_MINGW_DIR}/bin/asn1Parser.exe \$@\n"\ + > ${MY_MINGW_DIR}/bin/asn1Parser \ + && chmod +x ${MY_MINGW_DIR}/bin/asn1Parser \ + ; fi + +ARG LIBICONV_VERSION +ENV MY_LIBICONV_VERSION=${LIBICONV_VERSION} +RUN if [ -f "/3rd_party/libiconv-${MY_LIBICONV_VERSION}.tar.gz" ]; then \ + cd /3rd_party && sha256sum -c ./libiconv-${MY_LIBICONV_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/libiconv-${MY_LIBICONV_VERSION}.tar.gz \ + && cd libiconv-${MY_LIBICONV_VERSION} \ + && ./configure \ + --enable-static=yes \ + --enable-shared=no \ + --host=${MY_MINGW_PREFIX} \ + --prefix=${MY_MINGW_DIR} \ + && make -j${MY_NUM_JOBS} \ + && make install \ + && cd ${MY_WORKDIR} \ + && rm -r libiconv-${MY_LIBICONV_VERSION} \ + ; fi + +ARG LIBDSM_VERSION +ENV MY_LIBDSM_VERSION=${LIBDSM_VERSION} +RUN if [ -f "/3rd_party/libdsm-${MY_LIBDSM_VERSION}.tar.gz" ]; then \ + cd /3rd_party && sha256sum -c ./libdsm-${MY_LIBDSM_VERSION}.tar.gz.sha256 && cd - \ + && tar xvzf /3rd_party/libdsm-${MY_LIBDSM_VERSION}.tar.gz \ + && cd libdsm-${MY_LIBDSM_VERSION} \ + && /3rd_party/libdsm_patch.sh /3rd_party . \ + && meson setup \ + --bindir=${MY_MINGW_DIR}/bin \ + --cross-file ${MY_TOOLCHAIN_FILE_MESON} \ + --prefix=${MY_MINGW_DIR} \ + -Dbinaries=false \ + -Ddefault_library=static \ + _build \ + && meson compile \ + -C _build \ + && meson install \ + -C _build \ + && cd ${MY_WORKDIR} \ + && rm -r libdsm-${MY_LIBDSM_VERSION} \ + ; fi + RUN (mv ${MY_MINGW_DIR}/lib/*.dll ${MY_MINGW_DIR}/bin || echo "no dll's found") \ && chmod 0777 -R ${MY_MINGW_DIR} \ && rm -rf /3rd_party diff --git a/project.cmake b/project.cmake index 1b1230f7..a89eada3 100644 --- a/project.cmake +++ b/project.cmake @@ -1,15 +1,3 @@ -if (NOT PROJECT_STATIC_LINK) - include_directories(SYSTEM ${CMAKE_CURRENT_SOURCE_DIR}/deps/include) - - if(PROJECT_IS_MINGW) - link_libraries(msvcr90) - endif() - - list(APPEND PROJECT_ADDITIONAL_SOURCES - ${CMAKE_CURRENT_SOURCE_DIR}/deps/src/backward.cpp - ) -endif() - set(CMAKE_CXX_FLAGS "-include common.hpp ${CMAKE_CXX_FLAGS}") add_project_library(lib${PROJECT_NAME} "" "" "${PROJECT_ADDITIONAL_SOURCES}") diff --git a/repertory/librepertory/include/app_config.hpp b/repertory/librepertory/include/app_config.hpp index 8e042fcb..c2de1a3f 100644 --- a/repertory/librepertory/include/app_config.hpp +++ b/repertory/librepertory/include/app_config.hpp @@ -45,9 +45,6 @@ public: [[nodiscard]] static auto default_rpc_port(const provider_type &prov) -> std::uint16_t; - [[nodiscard]] static auto - get_provider_api_password(const provider_type &prov) -> std::string; - [[nodiscard]] static auto get_provider_display_name(const provider_type &prov) -> std::string; diff --git a/repertory/librepertory/include/comm/curl/curl_comm.hpp b/repertory/librepertory/include/comm/curl/curl_comm.hpp index 8ab68076..f655033b 100644 --- a/repertory/librepertory/include/comm/curl/curl_comm.hpp +++ b/repertory/librepertory/include/comm/curl/curl_comm.hpp @@ -26,8 +26,7 @@ #include "comm/i_http_comm.hpp" #include "events/event_system.hpp" #include "events/events.hpp" -#include "utils/encrypt.hpp" -#include "utils/utils.hpp" +#include "utils/encryption.hpp" namespace repertory { class curl_comm final : public i_http_comm { @@ -42,8 +41,8 @@ private: using write_callback = size_t (*)(char *, size_t, size_t, void *); struct read_write_info final { - repertory::data_buffer data{}; - repertory::stop_type &stop_requested; + data_buffer data{}; + stop_type &stop_requested; }; static const write_callback write_data; @@ -56,6 +55,11 @@ private: private: bool use_s3_path_style_{false}; +public: + [[nodiscard]] static auto create_curl() -> CURL *; + + [[nodiscard]] static auto reset_curl(CURL *curl_handle) -> CURL *; + public: [[nodiscard]] static auto construct_url(CURL *curl, const std::string &relative_path, @@ -92,32 +96,31 @@ public: const auto key = utils::encryption::generate_key( request.decryption_token.value()); - const auto result = utils::encryption::read_encrypted_range( - request.range.value(), key, - [&](data_buffer &ct, std::uint64_t start_offset, - std::uint64_t end_offset) -> api_error { - auto encrypted_request = request; - encrypted_request.decryption_token = std::nullopt; - encrypted_request.range = {{start_offset, end_offset}}; - encrypted_request.response_handler = [&ct](const auto &encrypted_data, - long /*response_code*/) { - ct = encrypted_data; - }; - encrypted_request.total_size = std::nullopt; + if (not utils::encryption::read_encrypted_range( + request.range.value(), key, + [&](data_buffer &ct, std::uint64_t start_offset, + std::uint64_t end_offset) -> bool { + auto encrypted_request = request; + encrypted_request.decryption_token = std::nullopt; + encrypted_request.range = {{start_offset, end_offset}}; + encrypted_request.response_handler = + [&ct](const auto &encrypted_data, long /*response_code*/) { + ct = encrypted_data; + }; + encrypted_request.total_size = std::nullopt; - if (not make_request(cfg, encrypted_request, response_code, - stop_requested)) { - return api_error::comm_error; - } + if (not make_request(cfg, encrypted_request, response_code, + stop_requested)) { + return false; + } - if (response_code != 200) { - return api_error::comm_error; - } + if (response_code != 200) { + return false; + } - return api_error::success; - }, - request.total_size.value(), data); - if (result != api_error::success) { + return true; + }, + request.total_size.value(), data)) { return false; } @@ -140,7 +143,7 @@ public: response_code = 0; - auto *curl = utils::create_curl(); + auto *curl = create_curl(); if (not request.set_method(curl, stop_requested)) { return false; } diff --git a/repertory/librepertory/include/comm/curl/requests/http_request_base.hpp b/repertory/librepertory/include/comm/curl/requests/http_request_base.hpp index f2e44adb..347cd89c 100644 --- a/repertory/librepertory/include/comm/curl/requests/http_request_base.hpp +++ b/repertory/librepertory/include/comm/curl/requests/http_request_base.hpp @@ -23,7 +23,7 @@ #define INCLUDE_COMM_CURL_CURL_REQUESTS_HTTP_REQUEST_BASE_HPP_ #include "types/repertory.hpp" -#include "utils/native_file.hpp" +#include "utils/file.hpp" namespace repertory::curl::requests { using read_callback = size_t (*)(char *, size_t, size_t, void *); @@ -33,7 +33,7 @@ using response_callback = struct read_file_info final { stop_type &stop_requested; - native_file::native_file_ptr nf{}; + std::unique_ptr file{}; std::uint64_t offset{}; }; @@ -41,9 +41,9 @@ inline const auto read_file_data = static_cast( [](char *buffer, size_t size, size_t nitems, void *instream) -> size_t { auto *read_info = reinterpret_cast(instream); std::size_t bytes_read{}; - auto ret = read_info->nf->read_bytes( - reinterpret_cast(buffer), size * nitems, - read_info->offset, bytes_read); + auto ret = + read_info->file->read(reinterpret_cast(buffer), + size * nitems, read_info->offset, &bytes_read); if (ret) { read_info->offset += bytes_read; } @@ -66,7 +66,7 @@ struct http_request_base { std::optional decryption_token{}; http_headers headers{}; std::string path{}; - query_parameters query{}; + http_query_parameters query{}; std::optional range{}; std::optional response_handler; std::optional response_headers; diff --git a/repertory/librepertory/include/common.hpp b/repertory/librepertory/include/common.hpp index 0d45f556..5ac2c3e8 100644 --- a/repertory/librepertory/include/common.hpp +++ b/repertory/librepertory/include/common.hpp @@ -61,17 +61,7 @@ inline constexpr const std::uint64_t REPERTORY_CONFIG_VERSION = 0ULL; inline constexpr const std::string_view REPERTORY_DATA_NAME = "repertory2"; inline constexpr const std::string_view REPERTORY_MIN_REMOTE_VERSION = "2.0.0"; -#if defined(_WIN32) #define REPERTORY_INVALID_HANDLE INVALID_HANDLE_VALUE -#define REPERTORY_API_INVALID_HANDLE static_cast(-1) -using native_handle = HANDLE; -#else -#define REPERTORY_INVALID_HANDLE (-1) -#define REPERTORY_API_INVALID_HANDLE REPERTORY_INVALID_HANDLE -using native_handle = int; -#endif - -inline constexpr const auto NANOS_PER_SECOND = 1000000000L; #define WINFSP_ALLOCATION_UNIT UINT64(4096U) diff --git a/repertory/librepertory/include/drives/fuse/fuse_base.hpp b/repertory/librepertory/include/drives/fuse/fuse_base.hpp index b9a885c5..401e5160 100644 --- a/repertory/librepertory/include/drives/fuse/fuse_base.hpp +++ b/repertory/librepertory/include/drives/fuse/fuse_base.hpp @@ -64,15 +64,15 @@ private: struct fuse_operations fuse_ops_ {}; private: - [[nodiscard]] auto execute_callback( - std::string_view function_name, const char *from, const char *to, - const std::function - &cb, - bool disable_logging = false) -> int; + [[nodiscard]] auto + execute_callback(std::string_view function_name, const char *from, + const char *to, + const std::function &cb, + bool disable_logging = false) -> int; [[nodiscard]] auto execute_callback(std::string_view function_name, const char *path, - const std::function &cb, + const std::function &cb, bool disable_logging = false) -> int; static void execute_void_callback(std::string_view function_name, @@ -83,7 +83,7 @@ private: const std::function &cb) -> void *; void raise_fuse_event(std::string_view function_name, - const std::string &api_path, int ret, + std::string_view api_path, int ret, bool disable_logging); private: diff --git a/repertory/librepertory/include/drives/remote/remote_server_base.hpp b/repertory/librepertory/include/drives/remote/remote_server_base.hpp index 91a3a1ad..2c23911e 100644 --- a/repertory/librepertory/include/drives/remote/remote_server_base.hpp +++ b/repertory/librepertory/include/drives/remote/remote_server_base.hpp @@ -1269,7 +1269,7 @@ public: data_buffer buffer(write_size); if ((ret = request->decode(buffer.data(), buffer.size())) == 0) { buffer = macaron::Base64::Decode( - std::string{buffer.begin(), buffer.end()}); + std::string(buffer.begin(), buffer.end())); write_size = buffer.size(); remote::file_offset write_offset{}; diff --git a/repertory/librepertory/include/drives/winfsp/remotewinfsp/remote_client.hpp b/repertory/librepertory/include/drives/winfsp/remotewinfsp/remote_client.hpp index da3eed0e..126b6208 100644 --- a/repertory/librepertory/include/drives/winfsp/remotewinfsp/remote_client.hpp +++ b/repertory/librepertory/include/drives/winfsp/remotewinfsp/remote_client.hpp @@ -42,9 +42,9 @@ private: private: #if defined(_WIN32) #define to_handle(x) (x) -#else +#else // !defined(_WIN32) static auto to_handle(PVOID file_desc) -> native_handle; -#endif +#endif // defined(_WIN32) public: auto json_create_directory_snapshot(const std::string &path, json &json_data) diff --git a/repertory/librepertory/include/file_manager/file_manager.hpp b/repertory/librepertory/include/file_manager/file_manager.hpp index 2a4e8647..fac90ff3 100644 --- a/repertory/librepertory/include/file_manager/file_manager.hpp +++ b/repertory/librepertory/include/file_manager/file_manager.hpp @@ -30,7 +30,7 @@ #include "file_manager/i_upload_manager.hpp" #include "platform/platform.hpp" #include "types/repertory.hpp" -#include "utils/native_file.hpp" +#include "utils/file.hpp" namespace repertory { class app_config; @@ -131,7 +131,7 @@ public: std::atomic last_access_{ std::chrono::system_clock::now()}; bool modified_{false}; - native_file_ptr nf_; + std::unique_ptr nf_; mutable std::mutex io_thread_mtx_; std::condition_variable io_thread_notify_; std::deque> io_thread_queue_; diff --git a/repertory/librepertory/include/providers/encrypt/encrypt_provider.hpp b/repertory/librepertory/include/providers/encrypt/encrypt_provider.hpp index a57af6b6..fd9c9cc8 100644 --- a/repertory/librepertory/include/providers/encrypt/encrypt_provider.hpp +++ b/repertory/librepertory/include/providers/encrypt/encrypt_provider.hpp @@ -29,6 +29,9 @@ namespace repertory { class encrypt_provider final : public i_provider { +public: + static const constexpr auto type{provider_type::encrypt}; + public: explicit encrypt_provider(app_config &config); @@ -70,10 +73,9 @@ private: const std::string &source_path)> callback) const -> api_error; - auto - process_directory_entry(const std::filesystem::directory_entry &dir_entry, - const encrypt_config &cfg, - std::string &api_path) const -> bool; + auto process_directory_entry(const utils::file::i_fs_item &dir_entry, + const encrypt_config &cfg, + std::string &api_path) const -> bool; void remove_deleted_files(); @@ -81,74 +83,68 @@ public: [[nodiscard]] auto create_directory(const std::string &api_path, api_meta_map &meta) -> api_error override; - [[nodiscard]] auto - create_directory_clone_source_meta(const std::string & /*source_api_path*/, - const std::string & /*api_path*/) - -> api_error override { - return api_error::not_implemented; - } - - [[nodiscard]] auto create_file(const std::string & /*api_path*/, - api_meta_map & /*meta*/) - -> api_error override { + [[nodiscard]] auto create_directory_clone_source_meta( + const std::string & /*source_api_path*/, + const std::string & /*api_path*/) -> api_error override { return api_error::not_implemented; } [[nodiscard]] auto - get_api_path_from_source(const std::string & /*source_path*/, - std::string & /*api_path*/) const - -> api_error override; + create_file(const std::string & /*api_path*/, + api_meta_map & /*meta*/) -> api_error override { + return api_error::not_implemented; + } + + [[nodiscard]] auto get_api_path_from_source( + const std::string & /*source_path*/, + std::string & /*api_path*/) const -> api_error override; [[nodiscard]] auto get_directory_item_count(const std::string &api_path) const -> std::uint64_t override; - [[nodiscard]] auto get_directory_items(const std::string &api_path, - directory_item_list &list) const - -> api_error override; + [[nodiscard]] auto + get_directory_items(const std::string &api_path, + directory_item_list &list) const -> api_error override; - [[nodiscard]] auto get_file(const std::string &api_path, api_file &file) const - -> api_error override; - - [[nodiscard]] auto get_file_list(api_file_list &list) const - -> api_error override; - - [[nodiscard]] auto get_file_size(const std::string &api_path, - std::uint64_t &file_size) const - -> api_error override; - - [[nodiscard]] auto get_filesystem_item(const std::string &api_path, - bool directory, - filesystem_item &fsi) const - -> api_error override; - - [[nodiscard]] auto get_filesystem_item_and_file(const std::string &api_path, - api_file &file, - filesystem_item &fsi) const - -> api_error override; + [[nodiscard]] auto get_file(const std::string &api_path, + api_file &file) const -> api_error override; [[nodiscard]] auto - get_filesystem_item_from_source_path(const std::string &source_path, - filesystem_item &fsi) const - -> api_error override; + get_file_list(api_file_list &list) const -> api_error override; - [[nodiscard]] auto get_pinned_files() const - -> std::vector override; + [[nodiscard]] auto + get_file_size(const std::string &api_path, + std::uint64_t &file_size) const -> api_error override; - [[nodiscard]] auto get_item_meta(const std::string &api_path, - api_meta_map &meta) const - -> api_error override; + [[nodiscard]] auto + get_filesystem_item(const std::string &api_path, bool directory, + filesystem_item &fsi) const -> api_error override; - [[nodiscard]] auto get_item_meta(const std::string &api_path, - const std::string &key, - std::string &value) const - -> api_error override; + [[nodiscard]] auto get_filesystem_item_and_file( + const std::string &api_path, api_file &file, + filesystem_item &fsi) const -> api_error override; + + [[nodiscard]] auto get_filesystem_item_from_source_path( + const std::string &source_path, + filesystem_item &fsi) const -> api_error override; + + [[nodiscard]] auto + get_pinned_files() const -> std::vector override; + + [[nodiscard]] auto + get_item_meta(const std::string &api_path, + api_meta_map &meta) const -> api_error override; + + [[nodiscard]] auto + get_item_meta(const std::string &api_path, const std::string &key, + std::string &value) const -> api_error override; [[nodiscard]] auto get_total_drive_space() const -> std::uint64_t override; [[nodiscard]] auto get_total_item_count() const -> std::uint64_t override; [[nodiscard]] auto get_provider_type() const -> provider_type override { - return provider_type::encrypt; + return type; } [[nodiscard]] auto get_used_drive_space() const -> std::uint64_t override; @@ -158,54 +154,52 @@ public: [[nodiscard]] auto is_directory(const std::string &api_path, bool &exists) const -> api_error override; - [[nodiscard]] auto is_file(const std::string &api_path, bool &exists) const - -> api_error override; + [[nodiscard]] auto is_file(const std::string &api_path, + bool &exists) const -> api_error override; - [[nodiscard]] auto is_file_writeable(const std::string &api_path) const - -> bool override; + [[nodiscard]] auto + is_file_writeable(const std::string &api_path) const -> bool override; [[nodiscard]] auto is_online() const -> bool override; [[nodiscard]] auto is_rename_supported() const -> bool override; - [[nodiscard]] auto read_file_bytes(const std::string &api_path, - std::size_t size, std::uint64_t offset, - data_buffer &data, - stop_type &stop_requested) - -> api_error override; + [[nodiscard]] auto + read_file_bytes(const std::string &api_path, std::size_t size, + std::uint64_t offset, data_buffer &data, + stop_type &stop_requested) -> api_error override; - [[nodiscard]] auto remove_directory(const std::string & /*api_path*/) - -> api_error override { + [[nodiscard]] auto + remove_directory(const std::string & /*api_path*/) -> api_error override { return api_error::not_implemented; } - [[nodiscard]] auto remove_file(const std::string & /*api_path*/) - -> api_error override { + [[nodiscard]] auto + remove_file(const std::string & /*api_path*/) -> api_error override { return api_error::not_implemented; } - [[nodiscard]] auto remove_item_meta(const std::string & /*api_path*/, - const std::string & /*key*/) - -> api_error override { + [[nodiscard]] auto + remove_item_meta(const std::string & /*api_path*/, + const std::string & /*key*/) -> api_error override { return api_error::success; } - [[nodiscard]] auto rename_file(const std::string & /*from_api_path*/, - const std::string & /*to_api_path*/) - -> api_error override { + [[nodiscard]] auto + rename_file(const std::string & /*from_api_path*/, + const std::string & /*to_api_path*/) -> api_error override { return api_error::not_implemented; } - [[nodiscard]] auto set_item_meta(const std::string & /*api_path*/, - const std::string & /*key*/, - const std::string & /*value*/) - -> api_error override { + [[nodiscard]] auto + set_item_meta(const std::string & /*api_path*/, const std::string & /*key*/, + const std::string & /*value*/) -> api_error override { return api_error::success; } - [[nodiscard]] auto set_item_meta(const std::string & /*api_path*/, - const api_meta_map & /*meta*/) - -> api_error override { + [[nodiscard]] auto + set_item_meta(const std::string & /*api_path*/, + const api_meta_map & /*meta*/) -> api_error override { return api_error::success; } @@ -214,10 +208,10 @@ public: void stop() override; - [[nodiscard]] auto upload_file(const std::string & /*api_path*/, - const std::string & /*source_path*/, - stop_type & /*stop_requested*/) - -> api_error override { + [[nodiscard]] auto + upload_file(const std::string & /*api_path*/, + const std::string & /*source_path*/, + stop_type & /*stop_requested*/) -> api_error override { return api_error::not_implemented; } }; diff --git a/repertory/librepertory/include/providers/s3/s3_provider.hpp b/repertory/librepertory/include/providers/s3/s3_provider.hpp index 0d8643fd..1ee77b0e 100644 --- a/repertory/librepertory/include/providers/s3/s3_provider.hpp +++ b/repertory/librepertory/include/providers/s3/s3_provider.hpp @@ -32,6 +32,9 @@ class i_http_comm; struct head_object_result; class s3_provider final : public base_provider { +public: + static const constexpr auto type{provider_type::s3}; + public: s3_provider(app_config &config, i_http_comm &comm); @@ -92,6 +95,9 @@ protected: stop_type &stop_requested) -> api_error override; public: + [[nodiscard]] static auto + convert_api_date(std::string_view date) -> std::uint64_t; + [[nodiscard]] auto get_directory_item_count(const std::string &api_path) const -> std::uint64_t override; @@ -104,7 +110,7 @@ public: [[nodiscard]] auto get_total_drive_space() const -> std::uint64_t override; [[nodiscard]] auto get_provider_type() const -> provider_type override { - return provider_type::s3; + return type; } [[nodiscard]] auto is_direct_only() const -> bool override { return false; } diff --git a/repertory/librepertory/include/providers/sia/sia_provider.hpp b/repertory/librepertory/include/providers/sia/sia_provider.hpp index 3d7a30c0..9213e747 100644 --- a/repertory/librepertory/include/providers/sia/sia_provider.hpp +++ b/repertory/librepertory/include/providers/sia/sia_provider.hpp @@ -31,6 +31,9 @@ class i_file_manager; class i_http_comm; class sia_provider : public base_provider { +public: + static const constexpr auto type{provider_type::sia}; + public: sia_provider(app_config &config, i_http_comm &comm); @@ -50,40 +53,39 @@ private: nlohmann::json &object_list) const -> bool; protected: - [[nodiscard]] auto create_directory_impl(const std::string &api_path, - api_meta_map &meta) - -> api_error override; + [[nodiscard]] auto + create_directory_impl(const std::string &api_path, + api_meta_map &meta) -> api_error override; [[nodiscard]] auto get_directory_items_impl(const std::string &api_path, directory_item_list &list) const -> api_error override; - [[nodiscard]] auto get_used_drive_space_impl() const - -> std::uint64_t override; + [[nodiscard]] auto + get_used_drive_space_impl() const -> std::uint64_t override; - [[nodiscard]] auto remove_directory_impl(const std::string &api_path) - -> api_error override; + [[nodiscard]] auto + remove_directory_impl(const std::string &api_path) -> api_error override; - [[nodiscard]] auto remove_file_impl(const std::string &api_path) - -> api_error override; + [[nodiscard]] auto + remove_file_impl(const std::string &api_path) -> api_error override; - [[nodiscard]] auto upload_file_impl(const std::string &api_path, - const std::string &source_path, - stop_type &stop_requested) - -> api_error override; + [[nodiscard]] auto + upload_file_impl(const std::string &api_path, const std::string &source_path, + stop_type &stop_requested) -> api_error override; public: [[nodiscard]] auto get_directory_item_count(const std::string &api_path) const -> std::uint64_t override; - [[nodiscard]] auto get_file(const std::string &api_path, api_file &file) const - -> api_error override; + [[nodiscard]] auto get_file(const std::string &api_path, + api_file &file) const -> api_error override; - [[nodiscard]] auto get_file_list(api_file_list &list) const - -> api_error override; + [[nodiscard]] auto + get_file_list(api_file_list &list) const -> api_error override; [[nodiscard]] auto get_provider_type() const -> provider_type override { - return provider_type::sia; + return type; } [[nodiscard]] auto get_total_drive_space() const -> std::uint64_t override; @@ -93,8 +95,8 @@ public: [[nodiscard]] auto is_directory(const std::string &api_path, bool &exists) const -> api_error override; - [[nodiscard]] auto is_file(const std::string &api_path, bool &exists) const - -> api_error override; + [[nodiscard]] auto is_file(const std::string &api_path, + bool &exists) const -> api_error override; [[nodiscard]] auto is_online() const -> bool override; @@ -102,15 +104,14 @@ public: return true; } - [[nodiscard]] auto read_file_bytes(const std::string &api_path, - std::size_t size, std::uint64_t offset, - data_buffer &buffer, - stop_type &stop_requested) - -> api_error override; + [[nodiscard]] auto + read_file_bytes(const std::string &api_path, std::size_t size, + std::uint64_t offset, data_buffer &buffer, + stop_type &stop_requested) -> api_error override; - [[nodiscard]] auto rename_file(const std::string &from_api_path, - const std::string &to_api_path) - -> api_error override; + [[nodiscard]] auto + rename_file(const std::string &from_api_path, + const std::string &to_api_path) -> api_error override; [[nodiscard]] auto start(api_item_added_callback api_item_added, i_file_manager *mgr) -> bool override; diff --git a/repertory/librepertory/include/types/remote.hpp b/repertory/librepertory/include/types/remote.hpp index 627252f0..3ebaf84c 100644 --- a/repertory/librepertory/include/types/remote.hpp +++ b/repertory/librepertory/include/types/remote.hpp @@ -22,14 +22,14 @@ #ifndef INCLUDE_TYPES_REMOTE_HPP_ #define INCLUDE_TYPES_REMOTE_HPP_ -#define PACKET_SERVICE_FUSE 1U -#define PACKET_SERVICE_WINFSP 2U +inline constexpr const auto PACKET_SERVICE_FUSE{1U}; +inline constexpr const auto PACKET_SERVICE_WINFSP{2U}; #if defined(_WIN32) -#define PACKET_SERVICE_FLAGS PACKET_SERVICE_WINFSP -#else -#define PACKET_SERVICE_FLAGS PACKET_SERVICE_FUSE -#endif +inline constexpr const auto PACKET_SERVICE_FLAGS{PACKET_SERVICE_WINFSP}; +#else // !defined(_WIN32) +inline constexpr const auto PACKET_SERVICE_FLAGS{PACKET_SERVICE_FUSE}; +#endif // defined(_WIN32) namespace repertory::remote { using block_count = std::uint64_t; @@ -65,16 +65,19 @@ enum class open_flags : std::uint32_t { dsync = 131072U, }; -inline auto operator|(const open_flags &flag_1, - const open_flags &flag_2) -> open_flags { - using t = std::underlying_type_t; - return static_cast(static_cast(flag_1) | - static_cast(flag_2)); +#if defined(__GNUG__) +__attribute__((unused)) +#endif // defined(__GNUG__) +inline auto +operator|(const open_flags &flag_1, const open_flags &flag_2) -> open_flags { + using flag_t = std::underlying_type_t; + return static_cast(static_cast(flag_1) | + static_cast(flag_2)); } #if defined(__GNUG__) __attribute__((unused)) -#endif +#endif // defined(__GNUG__) inline auto operator|=(open_flags &flag_1, const open_flags &flag_2) -> open_flags & { flag_1 = flag_1 | flag_2; @@ -83,69 +86,69 @@ operator|=(open_flags &flag_1, const open_flags &flag_2) -> open_flags & { #if defined(__GNUG__) __attribute__((unused)) -#endif +#endif // defined(__GNUG__) inline auto operator&(const open_flags &flag_1, const open_flags &flag_2) -> open_flags { - using t = std::underlying_type_t; - return static_cast(static_cast(flag_1) & - static_cast(flag_2)); + using flag_t = std::underlying_type_t; + return static_cast(static_cast(flag_1) & + static_cast(flag_2)); } #pragma pack(1) -struct file_info { - UINT32 FileAttributes; - UINT32 ReparseTag; - UINT64 AllocationSize; - UINT64 FileSize; - UINT64 CreationTime; - UINT64 LastAccessTime; - UINT64 LastWriteTime; - UINT64 ChangeTime; - UINT64 IndexNumber; - UINT32 HardLinks; - UINT32 EaSize; +struct file_info final { + UINT32 FileAttributes{}; + UINT32 ReparseTag{}; + UINT64 AllocationSize{}; + UINT64 FileSize{}; + UINT64 CreationTime{}; + UINT64 LastAccessTime{}; + UINT64 LastWriteTime{}; + UINT64 ChangeTime{}; + UINT64 IndexNumber{}; + UINT32 HardLinks{}; + UINT32 EaSize{}; }; -struct setattr_x { - std::int32_t valid; - file_mode mode; - user_id uid; - group_id gid; - file_size size; - file_time acctime; - file_time modtime; - file_time crtime; - file_time chgtime; - file_time bkuptime; - std::uint32_t flags; +struct setattr_x final { + std::int32_t valid{}; + file_mode mode{}; + user_id uid{}; + group_id gid{}; + file_size size{}; + file_time acctime{}; + file_time modtime{}; + file_time crtime{}; + file_time chgtime{}; + file_time bkuptime{}; + std::uint32_t flags{}; }; -struct stat { - file_mode st_mode; - file_nlink st_nlink; - user_id st_uid; - group_id st_gid; - file_time st_atimespec; - file_time st_mtimespec; - file_time st_ctimespec; - file_time st_birthtimespec; - file_size st_size; - block_count st_blocks; - block_size st_blksize; - std::uint32_t st_flags; +struct stat final { + file_mode st_mode{}; + file_nlink st_nlink{}; + user_id st_uid{}; + group_id st_gid{}; + file_time st_atimespec{}; + file_time st_mtimespec{}; + file_time st_ctimespec{}; + file_time st_birthtimespec{}; + file_size st_size{}; + block_count st_blocks{}; + block_size st_blksize{}; + std::uint32_t st_flags{}; }; struct statfs { - std::uint64_t f_bavail; - std::uint64_t f_bfree; - std::uint64_t f_blocks; - std::uint64_t f_favail; - std::uint64_t f_ffree; - std::uint64_t f_files; + std::uint64_t f_bavail{}; + std::uint64_t f_bfree{}; + std::uint64_t f_blocks{}; + std::uint64_t f_favail{}; + std::uint64_t f_ffree{}; + std::uint64_t f_files{}; }; -struct statfs_x : public statfs { - char f_mntfromname[1024]; +struct statfs_x final : public statfs { + std::array f_mntfromname{}; }; #pragma pack() @@ -154,7 +157,7 @@ struct statfs_x : public statfs { [[nodiscard]] auto create_os_open_flags(const open_flags &flags) -> std::uint32_t; -#endif +#endif // !defined(_WIN32) } // namespace repertory::remote #endif // INCLUDE_TYPES_REMOTE_HPP_ diff --git a/repertory/librepertory/include/types/repertory.hpp b/repertory/librepertory/include/types/repertory.hpp index 80822f06..db917117 100644 --- a/repertory/librepertory/include/types/repertory.hpp +++ b/repertory/librepertory/include/types/repertory.hpp @@ -23,26 +23,26 @@ #define INCLUDE_TYPES_REPERTORY_HPP_ namespace repertory { -constexpr const auto max_time = 0xFFFFFFFFFFFFFFFFULL; +inline constexpr const auto max_time{std::numeric_limits::max()}; -const std::string META_ACCESSED = "accessed"; -const std::string META_ATTRIBUTES = "attributes"; -const std::string META_BACKUP = "backup"; -const std::string META_CHANGED = "changed"; -const std::string META_CREATION = "creation"; -const std::string META_DIRECTORY = "directory"; -const std::string META_GID = "gid"; -const std::string META_KEY = "key"; -const std::string META_MODE = "mode"; -const std::string META_MODIFIED = "modified"; -const std::string META_OSXFLAGS = "flags"; -const std::string META_PINNED = "pinned"; -const std::string META_SIZE = "size"; -const std::string META_SOURCE = "source"; -const std::string META_UID = "uid"; -const std::string META_WRITTEN = "written"; +inline constexpr const std::string META_ACCESSED{"accessed"}; +inline constexpr const std::string META_ATTRIBUTES{"attributes"}; +inline constexpr const std::string META_BACKUP{"backup"}; +inline constexpr const std::string META_CHANGED{"changed"}; +inline constexpr const std::string META_CREATION{"creation"}; +inline constexpr const std::string META_DIRECTORY{"directory"}; +inline constexpr const std::string META_GID{"gid"}; +inline constexpr const std::string META_KEY{"key"}; +inline constexpr const std::string META_MODE{"mode"}; +inline constexpr const std::string META_MODIFIED{"modified"}; +inline constexpr const std::string META_OSXFLAGS{"flags"}; +inline constexpr const std::string META_PINNED{"pinned"}; +inline constexpr const std::string META_SIZE{"size"}; +inline constexpr const std::string META_SOURCE{"source"}; +inline constexpr const std::string META_UID{"uid"}; +inline constexpr const std::string META_WRITTEN{"written"}; -const std::vector META_USED_NAMES = { +inline constexpr const std::array META_USED_NAMES = { META_ACCESSED, META_ATTRIBUTES, META_BACKUP, META_CHANGED, META_CREATION, META_DIRECTORY, META_GID, META_KEY, META_MODE, META_MODIFIED, META_OSXFLAGS, META_PINNED, @@ -51,8 +51,6 @@ const std::vector META_USED_NAMES = { using api_meta_map = std::map; -using stop_type = std::atomic; - enum class api_error { success = 0, access_denied, @@ -152,28 +150,28 @@ enum class provider_type : std::size_t { }; #if defined(_WIN32) -struct open_file_data { +struct open_file_data final { void *directory_buffer{}; }; #else using open_file_data = int; #endif -struct api_file { - std::string api_path; - std::string api_parent; +struct api_file final { + std::string api_path{}; + std::string api_parent{}; std::uint64_t accessed_date{}; std::uint64_t changed_date{}; std::uint64_t creation_date{}; std::uint64_t file_size{}; - std::string key; + std::string key{}; std::uint64_t modified_date{}; std::string source_path; }; -struct directory_item { - std::string api_path; - std::string api_parent; +struct directory_item final { + std::string api_path{}; + std::string api_parent{}; bool directory{false}; std::uint64_t size{}; api_meta_map meta{}; @@ -190,26 +188,30 @@ struct directory_item { } [[nodiscard]] auto to_json() const -> json { - return {{"path", api_path}, - {"parent", api_parent}, - {"size", size}, - {"directory", directory}, - {"meta", meta}}; + return { + {"path", api_path}, {"parent", api_parent}, {"size", size}, + {"directory", directory}, {"meta", meta}, + }; } }; -struct filesystem_item { - std::string api_path; - std::string api_parent; - bool directory{false}; - std::uint64_t size{}; - std::string source_path; +struct encrypt_config final { + std::string encryption_token{}; + std::string path{}; }; -struct host_config { - std::string agent_string; - std::string api_password; - std::string api_user; +struct filesystem_item final { + std::string api_path{}; + std::string api_parent{}; + bool directory{false}; + std::uint64_t size{}; + std::string source_path{}; +}; + +struct host_config final { + std::string agent_string{}; + std::string api_password{}; + std::string api_user{}; std::uint16_t api_port{}; std::string host_name_or_ip{"localhost"}; std::string path{}; @@ -265,25 +267,15 @@ from_json(const json &j, host_config &hc) { j.at("TimeoutMs").get_to(hc.timeout_ms); } -struct http_range { - std::uint64_t begin; - std::uint64_t end; -}; - -struct encrypt_config { - std::string encryption_token; - std::string path; -}; - -struct s3_config { - std::string access_key; - std::string bucket; +struct s3_config final { + std::string access_key{}; + std::string bucket{}; std::uint16_t cache_timeout_secs{60U}; - std::string encryption_token; + std::string encryption_token{}; std::string region{"any"}; - std::string secret_key; + std::string secret_key{}; std::uint32_t timeout_ms{60000U}; - std::string url; + std::string url{}; bool use_path_style{false}; bool use_region_in_url{false}; }; @@ -292,11 +284,7 @@ using api_file_list = std::vector; using api_file_provider_callback = std::function; using api_item_added_callback = std::function; using directory_item_list = std::vector; -using http_headers = std::unordered_map; -using http_parameters = std::unordered_map; -using http_ranges = std::vector; using meta_provider_callback = std::function; -using query_parameters = std::map; } // namespace repertory #endif // INCLUDE_TYPES_REPERTORY_HPP_ diff --git a/repertory/librepertory/include/types/s3.hpp b/repertory/librepertory/include/types/s3.hpp index 20bf1928..6ac87c98 100644 --- a/repertory/librepertory/include/types/s3.hpp +++ b/repertory/librepertory/include/types/s3.hpp @@ -79,8 +79,8 @@ struct head_object_result { #else strptime(date.c_str(), "%a, %d %b %Y %H:%M:%S %Z", &tm1); #endif - last_modified = - static_cast(mktime(&tm1)) * NANOS_PER_SECOND; + last_modified = static_cast(mktime(&tm1)) * + utils::time::NANOS_PER_SECOND; } return *this; } diff --git a/repertory/librepertory/include/utils/file_utils.hpp b/repertory/librepertory/include/utils/file_utils.hpp index aa80fefa..709ad525 100644 --- a/repertory/librepertory/include/utils/file_utils.hpp +++ b/repertory/librepertory/include/utils/file_utils.hpp @@ -22,64 +22,17 @@ #ifndef INCLUDE_UTILS_FILE_UTILS_HPP_ #define INCLUDE_UTILS_FILE_UTILS_HPP_ -#include "types/repertory.hpp" #include "utils/file.hpp" -#include "utils/native_file.hpp" namespace repertory::utils::file { -// Prototypes -[[nodiscard]] auto calculate_used_space(std::string path, - bool recursive) -> std::uint64_t; - -void change_to_process_directory(); - -[[nodiscard]] auto copy_directory_recursively(std::string from_path, - std::string to_path) -> bool; - -[[nodiscard]] auto copy_file(std::string from_path, - std::string to_path) -> bool; - -[[nodiscard]] auto create_full_directory_path(std::string path) -> bool; - -[[nodiscard]] auto delete_directory(std::string path, - bool recursive = false) -> bool; - -[[nodiscard]] auto delete_directory_recursively(std::string path) -> bool; - -[[nodiscard]] auto delete_file(std::string path) -> bool; - -[[nodiscard]] auto generate_sha256(const std::string &file_path) -> std::string; - -[[nodiscard]] auto get_accessed_time(const std::string &path, - std::uint64_t &accessed) -> bool; - [[nodiscard]] auto -get_directory_files(std::string path, bool oldest_first, +get_directory_files(std::string_view path, bool oldest_first, bool recursive = false) -> std::deque; -[[nodiscard]] auto -get_free_drive_space(const std::string &path) -> std::uint64_t; - -[[nodiscard]] auto -get_total_drive_space(const std::string &path) -> std::uint64_t; - -[[nodiscard]] auto get_modified_time(const std::string &path, - std::uint64_t &modified) -> bool; - -[[nodiscard]] auto -is_modified_date_older_than(const std::string &path, - const std::chrono::hours &hours) -> bool; - -[[nodiscard]] auto move_file(std::string from, std::string to) -> bool; - [[nodiscard]] auto read_file_lines(const std::string &path) -> std::vector; [[nodiscard]] auto reset_modified_time(const std::string &path) -> bool; - -[[nodiscard]] auto retry_delete_directory(const std::string &dir) -> bool; - -[[nodiscard]] auto retry_delete_file(const std::string &file) -> bool; } // namespace repertory::utils::file #endif // INCLUDE_UTILS_FILE_UTILS_HPP_ diff --git a/repertory/librepertory/include/utils/native_file.hpp b/repertory/librepertory/include/utils/native_file.hpp deleted file mode 100644 index 5b82e631..00000000 --- a/repertory/librepertory/include/utils/native_file.hpp +++ /dev/null @@ -1,117 +0,0 @@ -/* - Copyright <2018-2024> - - 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. -*/ -#ifndef INCLUDE_UTILS_NATIVEFILE_HPP_ -#define INCLUDE_UTILS_NATIVEFILE_HPP_ - -#include "types/repertory.hpp" - -namespace repertory { -class native_file final { -public: - native_file(const native_file &) = delete; - native_file(native_file &&) = delete; - auto operator=(const native_file &) -> native_file & = delete; - auto operator=(native_file &&) -> native_file & = delete; - - using native_file_ptr = std::shared_ptr; - -public: - [[nodiscard]] static auto attach(native_handle handle) -> native_file_ptr { - return std::shared_ptr(new native_file(handle)); - } - - [[nodiscard]] static auto - clone(const native_file_ptr &ptr) -> native_file_ptr; - - [[nodiscard]] static auto create_or_open(std::string_view source_path, - bool read_only, - native_file_ptr &ptr) -> api_error; - - [[nodiscard]] static auto create_or_open(std::string_view source_path, - native_file_ptr &ptr) -> api_error; - - [[nodiscard]] static auto open(std::string_view source_path, - native_file_ptr &ptr) -> api_error; - - [[nodiscard]] static auto open(std::string_view source_path, bool read_only, - native_file_ptr &ptr) -> api_error; - -private: - explicit native_file(const native_handle &handle) : handle_(handle) {} - -public: - ~native_file(); - -private: - native_handle handle_; - -private: - bool auto_close{false}; -#if defined(_WIN32) - std::recursive_mutex read_write_mutex_; -#endif - -public: - [[nodiscard]] auto allocate(std::uint64_t file_size) -> bool; - - void close(); - - [[nodiscard]] auto copy_from(const native_file_ptr &ptr) -> bool; - - [[nodiscard]] auto copy_from(const std::string &path) -> bool; - - void flush(); - - [[nodiscard]] auto get_file_size(std::uint64_t &file_size) -> bool; - - [[nodiscard]] auto get_handle() -> native_handle; - -#if defined(_WIN32) - [[nodiscard]] auto read_bytes(unsigned char *buffer, std::size_t read_size, - std::uint64_t read_offset, - std::size_t &bytes_read) -> bool; -#else - [[nodiscard]] auto read_bytes(unsigned char *buffer, std::size_t read_size, - std::uint64_t read_offset, - std::size_t &bytes_read) -> bool; -#endif - void set_auto_close(bool b) { auto_close = b; } - - [[nodiscard]] auto truncate(std::uint64_t file_size) -> bool; - -#if defined(_WIN32) - [[nodiscard]] auto write_bytes(const unsigned char *buffer, - std::size_t write_size, - std::uint64_t write_offset, - std::size_t &bytes_written) -> bool; -#else - [[nodiscard]] auto write_bytes(const unsigned char *buffer, - std::size_t write_size, - std::uint64_t write_offset, - std::size_t &bytes_written) -> bool; -#endif -}; - -using native_file_ptr = native_file::native_file_ptr; -} // namespace repertory - -#endif // INCLUDE_UTILS_NATIVEFILE_HPP_ diff --git a/repertory/librepertory/include/utils/unix/unix_utils.hpp b/repertory/librepertory/include/utils/unix/unix_utils.hpp index 1d19242c..00e3d3d6 100644 --- a/repertory/librepertory/include/utils/unix/unix_utils.hpp +++ b/repertory/librepertory/include/utils/unix/unix_utils.hpp @@ -43,15 +43,9 @@ inline const std::array attribute_namespaces = { [[nodiscard]] auto unix_error_to_windows(int err) -> std::uint32_t; -[[nodiscard]] auto -unix_time_to_windows_time(const remote::file_time &file_time) -> UINT64; - void windows_create_to_unix(const UINT32 &create_options, const UINT32 &granted_access, std::uint32_t &flags, remote::file_mode &mode); - -[[nodiscard]] auto -windows_time_to_unix_time(std::uint64_t win_time) -> remote::file_time; } // namespace repertory::utils #endif // !_WIN32 diff --git a/repertory/librepertory/include/utils/utils.hpp b/repertory/librepertory/include/utils/utils.hpp index 5d4ac1a2..d4efa9a8 100644 --- a/repertory/librepertory/include/utils/utils.hpp +++ b/repertory/librepertory/include/utils/utils.hpp @@ -29,27 +29,10 @@ void calculate_allocation_size(bool directory, std::uint64_t file_size, UINT64 allocation_size, std::string &allocation_meta_size); -[[nodiscard]] auto convert_api_date(const std::string &date) -> std::uint64_t; - -[[nodiscard]] auto create_curl() -> CURL *; - [[nodiscard]] auto create_volume_label(const provider_type &prov) -> std::string; [[nodiscard]] auto get_attributes_from_meta(const api_meta_map &meta) -> DWORD; - -[[nodiscard]] auto reset_curl(CURL *curl_handle) -> CURL *; - -[[nodiscard]] auto -retryable_action(const std::function &action) -> bool; - -void spin_wait_for_mutex(std::function complete, - std::condition_variable &cond, std::mutex &mtx, - const std::string &text = ""); - -void spin_wait_for_mutex(bool &complete, std::condition_variable &cond, - std::mutex &mtx, const std::string &text = ""); - } // namespace repertory::utils #endif // INCLUDE_UTILS_UTILS_HPP_ diff --git a/repertory/librepertory/src/app_config.cpp b/repertory/librepertory/src/app_config.cpp index 87d46ab5..24e76800 100644 --- a/repertory/librepertory/src/app_config.cpp +++ b/repertory/librepertory/src/app_config.cpp @@ -100,18 +100,17 @@ app_config::app_config(const provider_type &prov, log_directory_ = utils::path::combine(data_directory_, {"logs"}); hc_.agent_string = default_agent_name(prov_); - hc_.api_password = get_provider_api_password(prov_); hc_.api_port = default_api_port(prov_); - if (not utils::file::create_full_directory_path(data_directory_)) { + if (not utils::file::directory(data_directory_).create_directory()) { throw startup_exception("unable to create: " + data_directory_); } - if (not utils::file::create_full_directory_path(cache_directory_)) { + if (not utils::file::directory(cache_directory_).create_directory()) { throw startup_exception("unable to create: " + cache_directory_); } - if (not utils::file::create_full_directory_path(log_directory_)) { + if (not utils::file::directory(log_directory_).create_directory()) { throw startup_exception("unable to create: " + log_directory_); } @@ -318,39 +317,12 @@ auto app_config::get_json() const -> json { } auto app_config::get_max_cache_size_bytes() const -> std::uint64_t { - const auto max_space = + auto max_space = std::max(static_cast(100ULL * 1024ULL * 1024ULL), max_cache_size_bytes_); - return std::min(utils::file::get_free_drive_space(get_cache_directory()), - max_space); -} - -auto app_config::get_provider_api_password(const provider_type &prov) - -> std::string { -#if defined(_WIN32) - auto api_file = utils::path::combine(utils::get_local_app_data_directory(), - { - get_provider_display_name(prov), - "apipassword", - }); -#else -#if defined(__APPLE__) - auto api_file = utils::path::combine("~", { - "/", - "Library", - "Application Support", - get_provider_display_name(prov), - "apipassword", - }); -#else - auto api_file = utils::path::combine("~/.", { - get_provider_name(prov), - "apipassword", - }); -#endif -#endif - auto lines = utils::file::read_file_lines(api_file); - return lines.empty() ? "" : utils::string::trim(lines[0U]); + auto free_space = utils::file::get_free_drive_space(get_cache_directory()); + return free_space.has_value() ? std::min(free_space.value(), max_space) + : max_space; } auto app_config::get_provider_display_name(const provider_type &prov) @@ -542,12 +514,11 @@ auto app_config::load() -> bool { static_cast(__FUNCTION__), }; - auto ret = false; + auto ret{false}; const auto config_file_path = get_config_file_path(); - std::cout << config_file_path << std::endl; recur_mutex_lock lock(read_write_mutex_); - if (utils::file::is_file(config_file_path)) { + if (utils::file::file(config_file_path).exists()) { try { std::ifstream config_file(config_file_path.data()); if (config_file.is_open()) { @@ -606,13 +577,6 @@ auto app_config::load() -> bool { ret = false; } - if (hc_.api_password.empty()) { - hc_.api_password = get_provider_api_password(prov_); - if (hc_.api_password.empty()) { - ret = false; - } - } - if (json_document.find("S3Config") != json_document.end()) { auto s3_config_json = json_document["S3Config"]; auto s3_cfg = s3_config_; @@ -713,9 +677,9 @@ void app_config::save() { const auto file_path = get_config_file_path(); recur_mutex_lock lock(read_write_mutex_); - if (config_changed_ || not utils::file::is_file(file_path)) { - if (not utils::file::is_directory(data_directory_)) { - if (not utils::file::create_full_directory_path(data_directory_)) { + if (config_changed_ || not utils::file::file(file_path).exists()) { + if (not utils::file::directory(data_directory_).exists()) { + if (not utils::file::directory{data_directory_}.create_directory()) { utils::error::raise_error( function_name, "failed to create directory|sp|" + data_directory_ + "|err|" + diff --git a/repertory/librepertory/src/comm/curl/curl_comm.cpp b/repertory/librepertory/src/comm/curl/curl_comm.cpp index 964580f5..b2f576cd 100644 --- a/repertory/librepertory/src/comm/curl/curl_comm.cpp +++ b/repertory/librepertory/src/comm/curl/curl_comm.cpp @@ -92,6 +92,16 @@ auto curl_comm::construct_url(CURL *curl, const std::string &relative_path, relative_path, url); } +auto curl_comm::create_curl() -> CURL * { return reset_curl(curl_easy_init()); } + +auto curl_comm::reset_curl(CURL *curl_handle) -> CURL * { + curl_easy_reset(curl_handle); +#if defined(__APPLE__) + curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1); +#endif // defined(__APPLE__) + return curl_handle; +} + auto curl_comm::create_host_config(const s3_config &cfg, bool use_s3_path_style) -> host_config { host_config host_cfg{}; diff --git a/repertory/librepertory/src/comm/curl/requests/http_put_file.cpp b/repertory/librepertory/src/comm/curl/requests/http_put_file.cpp index 94010077..09f2f8a5 100644 --- a/repertory/librepertory/src/comm/curl/requests/http_put_file.cpp +++ b/repertory/librepertory/src/comm/curl/requests/http_put_file.cpp @@ -24,8 +24,8 @@ #include "utils/string.hpp" namespace repertory::curl::requests { -auto http_put_file::set_method(CURL *curl, stop_type &stop_requested) const - -> bool { +auto http_put_file::set_method(CURL *curl, + stop_type &stop_requested) const -> bool { curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); @@ -46,19 +46,14 @@ auto http_put_file::set_method(CURL *curl, stop_type &stop_requested) const read_info = std::make_shared(read_file_info{ stop_requested, + utils::file::file::open_or_create_file(source_path), }); - if (native_file::create_or_open(source_path, read_info->nf) != - api_error::success) { + if (not *read_info->file) { return false; } - read_info->nf->set_auto_close(true); - - std::uint64_t file_size{}; - if (not read_info->nf->get_file_size(file_size)) { - return false; - } + auto file_size = read_info->file->size(); curl_easy_setopt(curl, CURLOPT_READDATA, read_info.get()); curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_file_data); diff --git a/repertory/librepertory/src/comm/packet/packet.cpp b/repertory/librepertory/src/comm/packet/packet.cpp index f1907c0d..a08cfa14 100644 --- a/repertory/librepertory/src/comm/packet/packet.cpp +++ b/repertory/librepertory/src/comm/packet/packet.cpp @@ -38,7 +38,7 @@ void packet::clear() { auto packet::decode(std::string &data) -> packet::error_type { const auto *str = reinterpret_cast(&buffer_[decode_offset_]); const auto length = strnlen(str, buffer_.size() - decode_offset_); - data = std::string{str, length}; + data = std::string(str, length); decode_offset_ += (length + 1); return utils::from_api_error(api_error::success); @@ -194,7 +194,7 @@ auto packet::decode(remote::statfs &val) -> packet::error_type { auto packet::decode(remote::statfs_x &val) -> packet::error_type { auto ret = decode(*dynamic_cast(&val)); if (ret == 0) { - ret = decode(&val.f_mntfromname[0U], sizeof(val.f_mntfromname)); + ret = decode(val.f_mntfromname.data(), val.f_mntfromname.size()); } return ret; } @@ -372,9 +372,9 @@ void packet::encode(remote::statfs val, bool should_reserve) { void packet::encode(remote::statfs_x val) { buffer_.reserve(buffer_.size() + sizeof(remote::statfs) + - sizeof(val.f_mntfromname)); + val.f_mntfromname.size()); encode(*dynamic_cast(&val), false); - encode(&val.f_mntfromname[0], sizeof(val.f_mntfromname), false); + encode(val.f_mntfromname.data(), val.f_mntfromname.size(), false); } void packet::encode(remote::file_info val) { @@ -501,8 +501,8 @@ void packet::encode_top(remote::statfs val, bool should_reserve) { void packet::encode_top(remote::statfs_x val) { buffer_.reserve(buffer_.size() + sizeof(remote::statfs) + - sizeof(val.f_mntfromname)); - encode_top(&val.f_mntfromname[0], sizeof(val.f_mntfromname), false); + val.f_mntfromname.size()); + encode_top(val.f_mntfromname.data(), val.f_mntfromname.size(), false); encode_top(*dynamic_cast(&val), false); } diff --git a/repertory/librepertory/src/drives/directory_cache.cpp b/repertory/librepertory/src/drives/directory_cache.cpp index 34ccbc41..025c2844 100644 --- a/repertory/librepertory/src/drives/directory_cache.cpp +++ b/repertory/librepertory/src/drives/directory_cache.cpp @@ -85,7 +85,7 @@ void directory_cache::service_function() { auto lookup = directory_lookup_; directory_lock.unlock(); - for (const auto &kv : lookup) { + for (auto &&kv : lookup) { if (std::chrono::duration_cast( std::chrono::system_clock::now() - kv.second.last_update) >= 120s) { directory_lock.lock(); diff --git a/repertory/librepertory/src/drives/eviction.cpp b/repertory/librepertory/src/drives/eviction.cpp index ceb0eab5..28805349 100644 --- a/repertory/librepertory/src/drives/eviction.cpp +++ b/repertory/librepertory/src/drives/eviction.cpp @@ -38,37 +38,31 @@ auto eviction::check_minimum_requirements(const std::string &file_path) static_cast(__FUNCTION__), }; - std::uint64_t file_size{}; - if (not utils::file::get_file_size(file_path, file_size)) { + auto opt_size = utils::file::file{file_path}.size(); + if (not opt_size.has_value()) { utils::error::raise_error(function_name, utils::get_last_error_code(), file_path, "failed to get file size"); return false; } - auto ret = false; - if (file_size != 0U) { - std::uint64_t reference_time{}; - ret = config_.get_eviction_uses_accessed_time() - ? utils::file::get_accessed_time(file_path, reference_time) - : utils::file::get_modified_time(file_path, reference_time); - if (ret) { -#if defined(_WIN32) - const auto now = std::chrono::system_clock::now(); - const auto delay = - std::chrono::minutes(config_.get_eviction_delay_mins()); - ret = ((std::chrono::system_clock::from_time_t( - static_cast(reference_time)) + - delay) <= now); -#else - const auto now = utils::time::get_time_now(); - const auto delay = - (config_.get_eviction_delay_mins() * 60L) * NANOS_PER_SECOND; - ret = ((reference_time + static_cast(delay)) <= now); -#endif - } + auto file_size{opt_size.value()}; + if (file_size == 0U) { + return false; } - return ret; + auto reference_time = utils::file::file{file_path}.get_time( + config_.get_eviction_uses_accessed_time() + ? utils::file::time_type::accessed + : utils::file::time_type::modified); + + if (not reference_time.has_value()) { + return false; + } + + auto delay = (config_.get_eviction_delay_mins() * 60UL) * + utils::time::NANOS_PER_SECOND; + return ((reference_time.value() + static_cast(delay)) <= + utils::time::get_time_now()); } auto eviction::get_filtered_cached_files() -> std::deque { @@ -91,7 +85,7 @@ void eviction::service_function() { // Handle maximum cache size eviction auto used_bytes = - utils::file::calculate_used_space(config_.get_cache_directory(), false); + utils::file::directory{config_.get_cache_directory()}.size(); if (config_.get_enable_max_cache_size()) { should_evict = (used_bytes > config_.get_max_cache_size_bytes()); } @@ -110,9 +104,9 @@ void eviction::service_function() { if (provider_.get_filesystem_item_and_file(api_path, file, fsi) == api_error::success) { // Only evict files that match expected size - std::uint64_t file_size{}; - if (utils::file::get_file_size(cached_files_list.front(), - file_size)) { + auto opt_size = utils::file::file{cached_files_list.front()}.size(); + if (opt_size.has_value()) { + auto file_size{opt_size.value()}; if (file_size == fsi.size) { // Try to evict file if (fm_.evict_file(fsi.api_path) && diff --git a/repertory/librepertory/src/drives/fuse/fuse_base.cpp b/repertory/librepertory/src/drives/fuse/fuse_base.cpp index 97ddbcf2..4bb52800 100644 --- a/repertory/librepertory/src/drives/fuse/fuse_base.cpp +++ b/repertory/librepertory/src/drives/fuse/fuse_base.cpp @@ -106,8 +106,8 @@ auto fuse_base::access_(const char *path, int mask) -> int { }; return instance().instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().access_impl(api_path, mask); + function_name, path, [&](std::string api_path) -> api_error { + return instance().access_impl(std::move(api_path), mask); }); } @@ -118,8 +118,8 @@ auto fuse_base::chflags_(const char *path, uint32_t flags) -> int { }; return instance().instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().chflags_impl(api_path, flags); + function_name, path, [&](std::string api_path) -> api_error { + return instance().chflags_impl(std::move(api_path), flags); }); } #endif // __APPLE__ @@ -132,8 +132,8 @@ auto fuse_base::chmod_(const char *path, mode_t mode, }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().chmod_impl(api_path, mode, fi); + function_name, path, [&](std::string api_path) -> api_error { + return instance().chmod_impl(std::move(api_path), mode, fi); }); } #else @@ -143,8 +143,8 @@ auto fuse_base::chmod_(const char *path, mode_t mode) -> int { }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().chmod_impl(api_path, mode); + function_name, path, [&](std::string api_path) -> api_error { + return instance().chmod_impl(std::move(api_path), mode); }); } #endif @@ -157,8 +157,8 @@ auto fuse_base::chown_(const char *path, uid_t uid, gid_t gid, }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().chown_impl(api_path, uid, gid, fi); + function_name, path, [&](std::string api_path) -> api_error { + return instance().chown_impl(std::move(api_path), uid, gid, fi); }); } #else @@ -168,8 +168,8 @@ auto fuse_base::chown_(const char *path, uid_t uid, gid_t gid) -> int { }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().chown_impl(api_path, uid, gid); + function_name, path, [&](std::string api_path) -> api_error { + return instance().chown_impl(std::move(api_path), uid, gid); }); } #endif @@ -181,8 +181,8 @@ auto fuse_base::create_(const char *path, mode_t mode, }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().create_impl(api_path, mode, fi); + function_name, path, [&](std::string api_path) -> api_error { + return instance().create_impl(std::move(api_path), mode, fi); }); } @@ -223,12 +223,12 @@ void fuse_base::display_version_information(std::vector args) { auto fuse_base::execute_callback( std::string_view function_name, const char *from, const char *to, - const std::function &cb, + const std::function &cb, bool disable_logging) -> int { - const auto from_api_file = utils::path::create_api_path(from ? from : ""); - const auto to_api_file = utils::path::create_api_path(to ? to : ""); - const auto res = utils::from_api_error(cb(from_api_file, to_api_file)); + auto from_api_file = utils::path::create_api_path(from ? from : ""); + auto to_api_file = utils::path::create_api_path(to ? to : ""); + auto res = utils::from_api_error(cb(from_api_file, to_api_file)); raise_fuse_event(function_name, "from|" + from_api_file + "|to|" + to_api_file, res, disable_logging); @@ -237,7 +237,7 @@ auto fuse_base::execute_callback( auto fuse_base::execute_callback( std::string_view function_name, const char *path, - const std::function &cb, + const std::function &cb, bool disable_logging) -> int { const auto api_path = utils::path::create_api_path(path ? path : ""); const auto res = utils::from_api_error(cb(api_path)); @@ -269,8 +269,9 @@ auto fuse_base::fallocate_(const char *path, int mode, off_t offset, }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().fallocate_impl(api_path, mode, offset, length, fi); + function_name, path, [&](std::string api_path) -> api_error { + return instance().fallocate_impl(std::move(api_path), mode, offset, + length, fi); }); } @@ -282,8 +283,8 @@ auto fuse_base::fgetattr_(const char *path, struct stat *st, }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().fgetattr_impl(api_path, st, fi); + function_name, path, [&](std::string api_path) -> api_error { + return instance().fgetattr_impl(std::move(api_path), st, fi); }); } #endif @@ -296,8 +297,8 @@ auto fuse_base::fsetattr_x_(const char *path, struct setattr_x *attr, }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().fsetattr_x_impl(api_path, attr, fi); + function_name, path, [&](std::string api_path) -> api_error { + return instance().fsetattr_x_impl(std::move(api_path), attr, fi); }); } #endif // __APPLE__ @@ -309,8 +310,8 @@ auto fuse_base::fsync_(const char *path, int datasync, }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().fsync_impl(api_path, datasync, fi); + function_name, path, [&](std::string api_path) -> api_error { + return instance().fsync_impl(std::move(api_path), datasync, fi); }); } @@ -322,8 +323,8 @@ auto fuse_base::ftruncate_(const char *path, off_t size, }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().ftruncate_impl(api_path, size, fi); + function_name, path, [&](std::string api_path) -> api_error { + return instance().ftruncate_impl(std::move(api_path), size, fi); }); } #endif @@ -336,8 +337,8 @@ auto fuse_base::getattr_(const char *path, struct stat *st, }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().getattr_impl(api_path, st, fi); + function_name, path, [&](std::string api_path) -> api_error { + return instance().getattr_impl(std::move(api_path), st, fi); }); } #else @@ -347,8 +348,8 @@ auto fuse_base::getattr_(const char *path, struct stat *st) -> int { }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().getattr_impl(api_path, st); + function_name, path, [&](std::string api_path) -> api_error { + return instance().getattr_impl(std::move(api_path), st); }); } #endif @@ -361,8 +362,8 @@ auto fuse_base::getxtimes_(const char *path, struct timespec *bkuptime, }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().getxtimes_impl(api_path, bkuptime, crtime); + function_name, path, [&](std::string api_path) -> api_error { + return instance().getxtimes_impl(std::move(api_path), bkuptime, crtime); }); } #endif // __APPLE__ @@ -392,31 +393,34 @@ auto fuse_base::init_(struct fuse_conn_info *conn) -> void * { #if FUSE_USE_VERSION >= 30 auto fuse_base::init_impl([[maybe_unused]] struct fuse_conn_info *conn, struct fuse_config *cfg) -> void * { -#else +#else // FUSE_USE_VERSION < 30 auto fuse_base::init_impl(struct fuse_conn_info *conn) -> void * { -#endif +#endif // FUSE_USE_VERSION >= 30 static constexpr const std::string_view function_name{ static_cast(__FUNCTION__), }; - utils::file::change_to_process_directory(); - if (not console_enabled_) { - if (not repertory::project_initialize()) { - utils::error::raise_error(function_name, - "failed to initialize repertory"); - event_system::instance().raise(); - } - } - #if defined(__APPLE__) conn->want |= FUSE_CAP_VOL_RENAME; conn->want |= FUSE_CAP_XTIMES; -#endif // __APPLE__ +#endif // defined(__APPLE__) #if FUSE_USE_VERSION >= 30 cfg->nullpath_ok = 0; cfg->hard_remove = 1; -#endif +#endif // FUSE_USE_VERSION >= 30 + + if (not utils::file::change_to_process_directory()) { + utils::error::raise_error(function_name, + "failed to change to process directory"); + event_system::instance().raise(); + return this; + } + + if (not console_enabled_ && not repertory::project_initialize()) { + utils::error::raise_error(function_name, "failed to initialize repertory"); + event_system::instance().raise(); + } return this; } @@ -427,8 +431,8 @@ auto fuse_base::mkdir_(const char *path, mode_t mode) -> int { }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().mkdir_impl(api_path, mode); + function_name, path, [&](std::string api_path) -> api_error { + return instance().mkdir_impl(std::move(api_path), mode); }); } @@ -436,7 +440,7 @@ auto fuse_base::mount(std::vector args) -> int { auto ret = parse_args(args); if (ret == 0) { std::vector fuse_argv(args.size()); - for (std::size_t i = 0u; i < args.size(); i++) { + for (std::size_t i = 0u; i < args.size(); ++i) { fuse_argv[i] = args[i].c_str(); } @@ -486,8 +490,8 @@ auto fuse_base::open_(const char *path, struct fuse_file_info *fi) -> int { }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().open_impl(api_path, fi); + function_name, path, [&](std::string api_path) -> api_error { + return instance().open_impl(std::move(api_path), fi); }); } @@ -497,8 +501,8 @@ auto fuse_base::opendir_(const char *path, struct fuse_file_info *fi) -> int { }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().opendir_impl(api_path, fi); + function_name, path, [&](std::string api_path) -> api_error { + return instance().opendir_impl(std::move(api_path), fi); }); } @@ -511,9 +515,9 @@ auto fuse_base::read_(const char *path, char *buffer, size_t read_size, std::size_t bytes_read{}; const auto res = instance().execute_callback( function_name, path, - [&](const std::string &api_path) -> api_error { - return instance().read_impl(api_path, buffer, read_size, read_offset, - fi, bytes_read); + [&](std::string api_path) -> api_error { + return instance().read_impl(std::move(api_path), buffer, read_size, + read_offset, fi, bytes_read); }, true); return (res == 0) ? static_cast(bytes_read) : res; @@ -529,9 +533,9 @@ auto fuse_base::readdir_(const char *path, void *buf, }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().readdir_impl(api_path, buf, fuse_fill_dir, offset, fi, - flags); + function_name, path, [&](std::string api_path) -> api_error { + return instance().readdir_impl(std::move(api_path), buf, fuse_fill_dir, + offset, fi, flags); }); } #else @@ -543,9 +547,9 @@ auto fuse_base::readdir_(const char *path, void *buf, }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().readdir_impl(api_path, buf, fuse_fill_dir, offset, - fi); + function_name, path, [&](std::string api_path) -> api_error { + return instance().readdir_impl(std::move(api_path), buf, fuse_fill_dir, + offset, fi); }); } #endif @@ -556,8 +560,8 @@ auto fuse_base::release_(const char *path, struct fuse_file_info *fi) -> int { }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().release_impl(api_path, fi); + function_name, path, [&](std::string api_path) -> api_error { + return instance().release_impl(std::move(api_path), fi); }); } @@ -568,8 +572,8 @@ auto fuse_base::releasedir_(const char *path, }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().releasedir_impl(api_path, fi); + function_name, path, [&](std::string api_path) -> api_error { + return instance().releasedir_impl(std::move(api_path), fi); }); } @@ -582,9 +586,9 @@ auto fuse_base::rename_(const char *from, const char *to, return instance().execute_callback( function_name, from, to, - [&](const std::string &from_api_file, - const std::string &to_api_path) -> api_error { - return instance().rename_impl(from_api_file, to_api_path, flags); + [&](std::string from_api_file, std::string to_api_path) -> api_error { + return instance().rename_impl(std::move(from_api_file), + std::move(to_api_path), flags); }); } #else @@ -595,9 +599,9 @@ auto fuse_base::rename_(const char *from, const char *to) -> int { return instance().execute_callback( function_name, from, to, - [&](const std::string &from_api_file, - const std::string &to_api_path) -> api_error { - return instance().rename_impl(from_api_file, to_api_path); + [&](std::string from_api_file, std::string to_api_path) -> api_error { + return instance().rename_impl(std::move(from_api_file), + std::move(to_api_path)); }); } #endif @@ -608,8 +612,8 @@ auto fuse_base::rmdir_(const char *path) -> int { }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().rmdir_impl(api_path); + function_name, path, [&](std::string api_path) -> api_error { + return instance().rmdir_impl(std::move(api_path)); }); } @@ -623,9 +627,9 @@ auto fuse_base::getxattr_(const char *path, const char *name, char *value, int attribute_size = 0; const auto res = instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().getxattr_impl(api_path, name, value, size, position, - attribute_size); + function_name, path, [&](std::string api_path) -> api_error { + return instance().getxattr_impl(std::move(api_path), name, value, size, + position, attribute_size); }); return res == 0 ? attribute_size : res; @@ -639,8 +643,8 @@ auto fuse_base::getxattr_(const char *path, const char *name, char *value, int attribute_size = 0; const auto res = instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().getxattr_impl(api_path, name, value, size, + function_name, path, [&](std::string api_path) -> api_error { + return instance().getxattr_impl(std::move(api_path), name, value, size, attribute_size); }); @@ -657,9 +661,9 @@ auto fuse_base::listxattr_(const char *path, char *buffer, size_t size) -> int { bool return_size = false; const auto res = instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().listxattr_impl(api_path, buffer, size, required_size, - return_size); + function_name, path, [&](std::string api_path) -> api_error { + return instance().listxattr_impl(std::move(api_path), buffer, size, + required_size, return_size); }); return return_size ? required_size : res; @@ -676,14 +680,14 @@ void fuse_base::notify_fuse_args_parsed(const std::vector &args) { auto fuse_base::parse_args(std::vector &args) -> int { auto force_no_console = false; - for (std::size_t i = 1u; !force_no_console && (i < args.size()); i++) { + for (std::size_t i = 1u; !force_no_console && (i < args.size()); ++i) { if (args[i] == "-nc") { force_no_console = true; } } utils::collection::remove_element(args, "-nc"); - for (std::size_t i = 1u; i < args.size(); i++) { + for (std::size_t i = 1u; i < args.size(); ++i) { if (args[i] == "-f") { console_enabled_ = not force_no_console; } else if (args[i].find("-o") == 0) { @@ -697,7 +701,7 @@ auto fuse_base::parse_args(std::vector &args) -> int { } const auto option_parts = utils::string::split(options, ',', true); - for (const auto &option : option_parts) { + for (auto &&option : option_parts) { if (option.find("gid") == 0) { const auto parts = utils::string::split(option, '=', true); if (parts.size() == 2u) { @@ -752,7 +756,7 @@ auto fuse_base::parse_args(std::vector &args) -> int { } void fuse_base::raise_fuse_event(std::string_view function_name, - const std::string &api_path, int ret, + std::string_view api_path, int ret, bool disable_logging) { if ((ret >= 0) && disable_logging) { return; @@ -766,7 +770,7 @@ void fuse_base::raise_fuse_event(std::string_view function_name, (config_.get_event_level() >= event_level::trace)) { std::string func{function_name}; event_system::instance().raise( - utils::string::right_trim(func, '_'), api_path, ret); + utils::string::right_trim(func, '_'), std::string{api_path}, ret); } } @@ -776,8 +780,8 @@ auto fuse_base::removexattr_(const char *path, const char *name) -> int { }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().removexattr_impl(api_path, name); + function_name, path, [&](std::string api_path) -> api_error { + return instance().removexattr_impl(std::move(api_path), name); }); } @@ -789,9 +793,9 @@ auto fuse_base::setxattr_(const char *path, const char *name, const char *value, }; const auto res = instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().setxattr_impl(api_path, name, value, size, flags, - position); + function_name, path, [&](std::string api_path) -> api_error { + return instance().setxattr_impl(std::move(api_path), name, value, size, + flags, position); }); if (res != 0) { errno = std::abs(res); @@ -807,8 +811,9 @@ auto fuse_base::setxattr_(const char *path, const char *name, const char *value, }; const auto res = instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().setxattr_impl(api_path, name, value, size, flags); + function_name, path, [&](std::string api_path) -> api_error { + return instance().setxattr_impl(std::move(api_path), name, value, size, + flags); }); if (res != 0) { errno = std::abs(res); @@ -832,8 +837,8 @@ auto fuse_base::setattr_x_(const char *path, struct setattr_x *attr) -> int { }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().setattr_x_impl(api_path, attr); + function_name, path, [&](std::string api_path) -> api_error { + return instance().setattr_x_impl(std::move(api_path), attr); }); } @@ -844,8 +849,8 @@ auto fuse_base::setbkuptime_(const char *path, }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().setbkuptime_impl(api_path, bkuptime); + function_name, path, [&](std::string api_path) -> api_error { + return instance().setbkuptime_impl(std::move(api_path), bkuptime); }); } @@ -856,8 +861,8 @@ auto fuse_base::setchgtime_(const char *path, }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().setchgtime_impl(api_path, chgtime); + function_name, path, [&](std::string api_path) -> api_error { + return instance().setchgtime_impl(std::move(api_path), chgtime); }); } @@ -868,8 +873,8 @@ auto fuse_base::setcrtime_(const char *path, }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().setcrtime_impl(api_path, crtime); + function_name, path, [&](std::string api_path) -> api_error { + return instance().setcrtime_impl(std::move(api_path), crtime); }); } @@ -879,8 +884,8 @@ auto fuse_base::setvolname_(const char *volname) -> int { }; return instance().execute_callback( - function_name, volname, [&](const std::string &api_path) -> api_error { - return instance().setvolname_impl(volname); + function_name, volname, [&](std::string api_path) -> api_error { + return instance().setvolname_impl(std::move(volname)); }); } @@ -890,8 +895,8 @@ auto fuse_base::statfs_x_(const char *path, struct statfs *stbuf) -> int { }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().statfs_x_impl(api_path, stbuf); + function_name, path, [&](std::string api_path) -> api_error { + return instance().statfs_x_impl(std::move(api_path), stbuf); }); } #else // __APPLE__ @@ -901,8 +906,8 @@ auto fuse_base::statfs_(const char *path, struct statvfs *stbuf) -> int { }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().statfs_impl(api_path, stbuf); + function_name, path, [&](std::string api_path) -> api_error { + return instance().statfs_impl(std::move(api_path), stbuf); }); } #endif // __APPLE__ @@ -915,8 +920,8 @@ auto fuse_base::truncate_(const char *path, off_t size, }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().truncate_impl(api_path, size, fi); + function_name, path, [&](std::string api_path) -> api_error { + return instance().truncate_impl(std::move(api_path), size, fi); }); } #else @@ -926,8 +931,8 @@ auto fuse_base::truncate_(const char *path, off_t size) -> int { }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().truncate_impl(api_path, size); + function_name, path, [&](std::string api_path) -> api_error { + return instance().truncate_impl(std::move(api_path), size); }); } #endif @@ -938,8 +943,8 @@ auto fuse_base::unlink_(const char *path) -> int { }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().unlink_impl(api_path); + function_name, path, [&](std::string api_path) -> api_error { + return instance().unlink_impl(std::move(api_path)); }); } @@ -965,8 +970,8 @@ auto fuse_base::utimens_(const char *path, const struct timespec tv[2], }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().utimens_impl(api_path, tv, fi); + function_name, path, [&](std::string api_path) -> api_error { + return instance().utimens_impl(std::move(api_path), tv, fi); }); } #else @@ -976,8 +981,8 @@ auto fuse_base::utimens_(const char *path, const struct timespec tv[2]) -> int { }; return instance().execute_callback( - function_name, path, [&](const std::string &api_path) -> api_error { - return instance().utimens_impl(api_path, tv); + function_name, path, [&](std::string api_path) -> api_error { + return instance().utimens_impl(std::move(api_path), tv); }); } #endif @@ -992,9 +997,9 @@ auto fuse_base::write_(const char *path, const char *buffer, size_t write_size, const auto res = instance().execute_callback( function_name, path, - [&](const std::string &api_path) -> api_error { - return instance().write_impl(api_path, buffer, write_size, write_offset, - fi, bytes_written); + [&](std::string api_path) -> api_error { + return instance().write_impl(std::move(api_path), buffer, write_size, + write_offset, fi, bytes_written); }, true); return (res == 0) ? static_cast(bytes_written) : res; diff --git a/repertory/librepertory/src/drives/fuse/fuse_drive.cpp b/repertory/librepertory/src/drives/fuse/fuse_drive.cpp index 38d3ec4d..53993645 100644 --- a/repertory/librepertory/src/drives/fuse/fuse_drive.cpp +++ b/repertory/librepertory/src/drives/fuse/fuse_drive.cpp @@ -163,7 +163,7 @@ auto fuse_drive::create_impl(std::string api_path, mode_t mode, { std::shared_ptr open_file; if (is_create_op) { - const auto now = utils::time::get_file_time_now(); + const auto now = utils::time::get_time_now(); #if defined(__APPLE__) const auto osx_flags = static_cast(file_info->flags); #else // !defined(__APPLE__) @@ -614,7 +614,7 @@ auto fuse_drive::mkdir_impl(std::string api_path, mode_t mode) -> api_error { return res; } - const auto now = utils::time::get_file_time_now(); + const auto now = utils::time::get_time_now(); auto meta = create_meta_attributes(now, FILE_ATTRIBUTE_DIRECTORY, now, now, true, get_effective_gid(), "", mode, now, 0U, 0U, 0U, "", get_effective_uid(), now); @@ -915,7 +915,7 @@ auto fuse_drive::listxattr_impl(std::string api_path, char *buffer, size_t size, api_meta_map meta; if ((res = provider_.get_item_meta(api_path, meta)) == api_error::success) { - for (const auto &meta_item : meta) { + for (auto &&meta_item : meta) { if (utils::collection::excludes(META_USED_NAMES, meta_item.first)) { auto attribute_name = meta_item.first; #if defined(__APPLE__) @@ -1155,8 +1155,8 @@ auto fuse_drive::setbkuptime_impl( std::string api_path, const struct timespec *bkuptime) -> api_error { return check_and_perform( api_path, X_OK, [&](api_meta_map &meta) -> api_error { - const auto nanos = - bkuptime->tv_nsec + (bkuptime->tv_nsec * NANOS_PER_SECOND); + const auto nanos = bkuptime->tv_nsec + + (bkuptime->tv_nsec * utils::time::NANOS_PER_SECOND); return provider_.set_item_meta(api_path, META_BACKUP, std::to_string(nanos)); }); @@ -1166,8 +1166,8 @@ auto fuse_drive::setchgtime_impl(std::string api_path, const struct timespec *chgtime) -> api_error { return check_and_perform( api_path, X_OK, [&](api_meta_map &meta) -> api_error { - const auto nanos = - chgtime->tv_nsec + (chgtime->tv_nsec * NANOS_PER_SECOND); + const auto nanos = chgtime->tv_nsec + + (chgtime->tv_nsec * utils::time::NANOS_PER_SECOND); return provider_.set_item_meta(api_path, META_CHANGED, std::to_string(nanos)); }); @@ -1178,7 +1178,7 @@ auto fuse_drive::setcrtime_impl(std::string api_path, return check_and_perform( api_path, X_OK, [&](api_meta_map &meta) -> api_error { const auto nanos = - crtime->tv_nsec + (crtime->tv_nsec * NANOS_PER_SECOND); + crtime->tv_nsec + (crtime->tv_nsec * utils::time::NANOS_PER_SECOND); return provider_.set_item_meta(api_path, META_CREATION, std::to_string(nanos)); }); @@ -1310,19 +1310,24 @@ auto fuse_drive::utimens_impl(std::string api_path, } meta.clear(); - if ((tv == nullptr) || (tv[0U].tv_nsec == UTIME_NOW)) { - meta[META_ACCESSED] = std::to_string(utils::time::get_file_time_now()); - } else if (tv[0U].tv_nsec != UTIME_OMIT) { - const auto val = tv[0U].tv_nsec + (tv[0U].tv_sec * NANOS_PER_SECOND); - meta[META_ACCESSED] = std::to_string(val); - } - if ((tv == nullptr) || (tv[1U].tv_nsec == UTIME_NOW)) { - meta[META_MODIFIED] = std::to_string(utils::time::get_file_time_now()); - } else if (tv[1U].tv_nsec != UTIME_OMIT) { - const auto val = tv[1U].tv_nsec + (tv[1U].tv_sec * NANOS_PER_SECOND); - meta[META_MODIFIED] = std::to_string(val); - } + const auto process_timespec = [&meta, &tv](const auto &src, + std::string attr) { + if ((tv == nullptr) || (src.tv_nsec == UTIME_NOW)) { + meta[attr] = std::to_string(utils::time::get_time_now()); + return; + } + + if (src.tv_nsec != UTIME_OMIT) { + meta[attr] = std::to_string( + src.tv_nsec + + (src.tv_sec * static_cast>( + utils::time::NANOS_PER_SECOND))); + } + }; + + process_timespec(tv[0U], META_ACCESSED); + process_timespec(tv[1U], META_MODIFIED); if (not meta.empty()) { return provider_.set_item_meta(api_path, meta); @@ -1369,8 +1374,7 @@ void fuse_drive::update_accessed_time(const std::string &api_path) { if (atime_enabled_) { auto res = provider_.set_item_meta( - api_path, META_ACCESSED, - std::to_string(utils::time::get_file_time_now())); + api_path, META_ACCESSED, std::to_string(utils::time::get_time_now())); if (res != api_error::success) { utils::error::raise_api_path_error(function_name, api_path, res, "failed to set accessed time"); diff --git a/repertory/librepertory/src/drives/fuse/fuse_drive_base.cpp b/repertory/librepertory/src/drives/fuse/fuse_drive_base.cpp index b228c016..bd57071b 100644 --- a/repertory/librepertory/src/drives/fuse/fuse_drive_base.cpp +++ b/repertory/librepertory/src/drives/fuse/fuse_drive_base.cpp @@ -26,6 +26,7 @@ #include "platform/platform.hpp" #include "providers/i_provider.hpp" #include "utils/common.hpp" +#include "utils/time.hpp" namespace repertory { auto fuse_drive_base::access_impl(std::string api_path, int mask) -> api_error { @@ -165,9 +166,9 @@ auto fuse_drive_base::check_parent_access(const std::string &api_path, // Ignore root if (api_path != "/") { if ((mask & X_OK) == X_OK) { - for (auto parent = utils::path::get_parent_directory(api_path); + for (auto parent = utils::path::get_parent_path(api_path); (ret == api_error::success) && not parent.empty(); - parent = utils::path::get_parent_directory(parent)) { + parent = utils::path::get_parent_path(parent)) { if (((ret = check_access(parent, X_OK)) == api_error::success) && (parent == "/")) { break; @@ -178,7 +179,7 @@ auto fuse_drive_base::check_parent_access(const std::string &api_path, if (ret == api_error::success) { mask &= (~X_OK); if (mask != 0) { - ret = check_access(utils::path::get_parent_directory(api_path), mask); + ret = check_access(utils::path::get_parent_path(api_path), mask); } } } @@ -233,9 +234,9 @@ auto fuse_drive_base::get_mode_from_meta(const api_meta_map &meta) -> mode_t { void fuse_drive_base::get_timespec_from_meta(const api_meta_map &meta, const std::string &name, struct timespec &ts) { - const auto meta_time = utils::string::to_int64(meta.at(name)); - ts.tv_nsec = meta_time % NANOS_PER_SECOND; - ts.tv_sec = meta_time / NANOS_PER_SECOND; + auto meta_time = utils::string::to_uint64(meta.at(name)); + ts.tv_nsec = meta_time % utils::time::NANOS_PER_SECOND; + ts.tv_sec = meta_time / utils::time::NANOS_PER_SECOND; } auto fuse_drive_base::get_uid_from_meta(const api_meta_map &meta) -> uid_t { @@ -299,7 +300,7 @@ void fuse_drive_base::populate_stat(const std::string &api_path, std::uint64_t size_or_count, const api_meta_map &meta, bool directory, i_provider &provider, struct stat *st) { - memset(st, 0, sizeof(struct stat)); + std::memset(st, 0, sizeof(struct stat)); st->st_nlink = static_cast( directory ? 2 + (size_or_count == 0U ? provider.get_directory_item_count(api_path) @@ -341,9 +342,9 @@ void fuse_drive_base::populate_stat(const std::string &api_path, void fuse_drive_base::set_timespec_from_meta(const api_meta_map &meta, const std::string &name, struct timespec &ts) { - const auto meta_time = utils::string::to_int64(meta.at(name)); - ts.tv_nsec = meta_time % NANOS_PER_SECOND; - ts.tv_sec = meta_time / NANOS_PER_SECOND; + auto meta_time = utils::string::to_uint64(meta.at(name)); + ts.tv_nsec = meta_time % utils::time::NANOS_PER_SECOND; + ts.tv_sec = meta_time / utils::time::NANOS_PER_SECOND; } } // namespace repertory diff --git a/repertory/librepertory/src/drives/fuse/remotefuse/remote_fuse_drive.cpp b/repertory/librepertory/src/drives/fuse/remotefuse/remote_fuse_drive.cpp index 2331079c..e4b57c68 100644 --- a/repertory/librepertory/src/drives/fuse/remotefuse/remote_fuse_drive.cpp +++ b/repertory/librepertory/src/drives/fuse/remotefuse/remote_fuse_drive.cpp @@ -36,6 +36,7 @@ #include "utils/error_utils.hpp" #include "utils/file_utils.hpp" #include "utils/path.hpp" +#include "utils/time.hpp" #include "utils/utils.hpp" namespace repertory::remote_fuse { @@ -142,16 +143,17 @@ api_error remote_fuse_drive::fsetattr_x_impl(std::string api_path, attributes.uid = attr->uid; attributes.gid = attr->gid; attributes.size = attr->size; - attributes.acctime = - ((attr->acctime.tv_sec * NANOS_PER_SECOND) + attr->acctime.tv_nsec); - attributes.modtime = - ((attr->modtime.tv_sec * NANOS_PER_SECOND) + attr->modtime.tv_nsec); - attributes.crtime = - ((attr->crtime.tv_sec * NANOS_PER_SECOND) + attr->crtime.tv_nsec); - attributes.chgtime = - ((attr->chgtime.tv_sec * NANOS_PER_SECOND) + attr->chgtime.tv_nsec); + attributes.acctime = ((attr->acctime.tv_sec * utils::time::NANOS_PER_SECOND) + + attr->acctime.tv_nsec); + attributes.modtime = ((attr->modtime.tv_sec * utils::time::NANOS_PER_SECOND) + + attr->modtime.tv_nsec); + attributes.crtime = ((attr->crtime.tv_sec * utils::time::NANOS_PER_SECOND) + + attr->crtime.tv_nsec); + attributes.chgtime = ((attr->chgtime.tv_sec * utils::time::NANOS_PER_SECOND) + + attr->chgtime.tv_nsec); attributes.bkuptime = - ((attr->bkuptime.tv_sec * NANOS_PER_SECOND) + attr->bkuptime.tv_nsec); + ((attr->bkuptime.tv_sec * utils::time::NANOS_PER_SECOND) + + attr->bkuptime.tv_nsec); attributes.flags = attr->flags; return utils::to_api_error(remote_instance_->fuse_fsetattr_x( api_path.c_str(), attributes, f_info->fh)); @@ -208,10 +210,11 @@ api_error remote_fuse_drive::getxtimes_impl(std::string api_path, api_path.c_str(), repertory_bkuptime, repertory_crtime); if (res == 0) { bkuptime->tv_nsec = - static_cast(repertory_bkuptime % NANOS_PER_SECOND); - bkuptime->tv_sec = repertory_bkuptime / NANOS_PER_SECOND; - crtime->tv_nsec = static_cast(repertory_crtime % NANOS_PER_SECOND); - crtime->tv_sec = repertory_crtime / NANOS_PER_SECOND; + static_cast(repertory_bkuptime % utils::time::NANOS_PER_SECOND); + bkuptime->tv_sec = repertory_bkuptime / utils::time::NANOS_PER_SECOND; + crtime->tv_nsec = + static_cast(repertory_crtime % utils::time::NANOS_PER_SECOND); + crtime->tv_sec = repertory_crtime / utils::time::NANOS_PER_SECOND; } return utils::to_api_error(res); @@ -296,42 +299,51 @@ auto remote_fuse_drive::opendir_impl( void remote_fuse_drive::populate_stat(const remote::stat &r_stat, bool directory, struct stat &unix_st) { - memset(&unix_st, 0, sizeof(struct stat)); + std::memset(&unix_st, 0, sizeof(struct stat)); #if defined(__APPLE__) unix_st.st_blksize = 0; - unix_st.st_atimespec.tv_nsec = r_stat.st_atimespec % NANOS_PER_SECOND; - unix_st.st_atimespec.tv_sec = r_stat.st_atimespec / NANOS_PER_SECOND; + unix_st.st_atimespec.tv_nsec = + r_stat.st_atimespec % utils::time::NANOS_PER_SECOND; + unix_st.st_atimespec.tv_sec = + r_stat.st_atimespec / utils::time::NANOS_PER_SECOND; - unix_st.st_birthtimespec.tv_nsec = r_stat.st_birthtimespec % NANOS_PER_SECOND; - unix_st.st_birthtimespec.tv_sec = r_stat.st_birthtimespec / NANOS_PER_SECOND; + unix_st.st_birthtimespec.tv_nsec = + r_stat.st_birthtimespec % utils::time::NANOS_PER_SECOND; + unix_st.st_birthtimespec.tv_sec = + r_stat.st_birthtimespec / utils::time::NANOS_PER_SECOND; - unix_st.st_ctimespec.tv_nsec = r_stat.st_ctimespec % NANOS_PER_SECOND; - unix_st.st_ctimespec.tv_sec = r_stat.st_ctimespec / NANOS_PER_SECOND; + unix_st.st_ctimespec.tv_nsec = + r_stat.st_ctimespec % utils::time::NANOS_PER_SECOND; + unix_st.st_ctimespec.tv_sec = + r_stat.st_ctimespec / utils::time::NANOS_PER_SECOND; - unix_st.st_mtimespec.tv_nsec = r_stat.st_mtimespec % NANOS_PER_SECOND; - unix_st.st_mtimespec.tv_sec = r_stat.st_mtimespec / NANOS_PER_SECOND; + unix_st.st_mtimespec.tv_nsec = + r_stat.st_mtimespec % utils::time::NANOS_PER_SECOND; + unix_st.st_mtimespec.tv_sec = + r_stat.st_mtimespec / utils::time::NANOS_PER_SECOND; unix_st.st_flags = r_stat.st_flags; -#else +#else // !defined(__APPLE__) unix_st.st_blksize = 4096; - unix_st.st_atim.tv_nsec = - static_cast(r_stat.st_atimespec % NANOS_PER_SECOND); - unix_st.st_atim.tv_sec = - static_cast(r_stat.st_atimespec / NANOS_PER_SECOND); + unix_st.st_atim.tv_nsec = static_cast( + r_stat.st_atimespec % utils::time::NANOS_PER_SECOND); + unix_st.st_atim.tv_sec = static_cast( + r_stat.st_atimespec / utils::time::NANOS_PER_SECOND); - unix_st.st_ctim.tv_nsec = - static_cast(r_stat.st_ctimespec % NANOS_PER_SECOND); - unix_st.st_ctim.tv_sec = - static_cast(r_stat.st_ctimespec / NANOS_PER_SECOND); + unix_st.st_ctim.tv_nsec = static_cast( + r_stat.st_ctimespec % utils::time::NANOS_PER_SECOND); + unix_st.st_ctim.tv_sec = static_cast( + r_stat.st_ctimespec / utils::time::NANOS_PER_SECOND); + + unix_st.st_mtim.tv_nsec = static_cast( + r_stat.st_mtimespec % utils::time::NANOS_PER_SECOND); + unix_st.st_mtim.tv_sec = static_cast( + r_stat.st_mtimespec / utils::time::NANOS_PER_SECOND); +#endif // defined(__APPLE__) - unix_st.st_mtim.tv_nsec = - static_cast(r_stat.st_mtimespec % NANOS_PER_SECOND); - unix_st.st_mtim.tv_sec = - static_cast(r_stat.st_mtimespec / NANOS_PER_SECOND); -#endif if (not directory) { const auto block_size_stat = static_cast(512U); const auto block_size = static_cast(4096U); @@ -440,16 +452,17 @@ api_error remote_fuse_drive::setattr_x_impl(std::string api_path, attributes.uid = attr->uid; attributes.gid = attr->gid; attributes.size = attr->size; - attributes.acctime = - ((attr->acctime.tv_sec * NANOS_PER_SECOND) + attr->acctime.tv_nsec); - attributes.modtime = - ((attr->modtime.tv_sec * NANOS_PER_SECOND) + attr->modtime.tv_nsec); - attributes.crtime = - ((attr->crtime.tv_sec * NANOS_PER_SECOND) + attr->crtime.tv_nsec); - attributes.chgtime = - ((attr->chgtime.tv_sec * NANOS_PER_SECOND) + attr->chgtime.tv_nsec); + attributes.acctime = ((attr->acctime.tv_sec * utils::time::NANOS_PER_SECOND) + + attr->acctime.tv_nsec); + attributes.modtime = ((attr->modtime.tv_sec * utils::time::NANOS_PER_SECOND) + + attr->modtime.tv_nsec); + attributes.crtime = ((attr->crtime.tv_sec * utils::time::NANOS_PER_SECOND) + + attr->crtime.tv_nsec); + attributes.chgtime = ((attr->chgtime.tv_sec * utils::time::NANOS_PER_SECOND) + + attr->chgtime.tv_nsec); attributes.bkuptime = - ((attr->bkuptime.tv_sec * NANOS_PER_SECOND) + attr->bkuptime.tv_nsec); + ((attr->bkuptime.tv_sec * utils::time::NANOS_PER_SECOND) + + attr->bkuptime.tv_nsec); attributes.flags = attr->flags; return utils::to_api_error( remote_instance_->fuse_setattr_x(api_path.c_str(), attributes)); @@ -458,7 +471,7 @@ api_error remote_fuse_drive::setattr_x_impl(std::string api_path, api_error remote_fuse_drive::setbkuptime_impl(std::string api_path, const struct timespec *bkuptime) { remote::file_time repertory_bkuptime = - ((bkuptime->tv_sec * NANOS_PER_SECOND) + bkuptime->tv_nsec); + ((bkuptime->tv_sec * utils::time::NANOS_PER_SECOND) + bkuptime->tv_nsec); return utils::to_api_error( remote_instance_->fuse_setbkuptime(api_path.c_str(), repertory_bkuptime)); } @@ -466,7 +479,7 @@ api_error remote_fuse_drive::setbkuptime_impl(std::string api_path, api_error remote_fuse_drive::setchgtime_impl(std::string api_path, const struct timespec *chgtime) { remote::file_time repertory_chgtime = - ((chgtime->tv_sec * NANOS_PER_SECOND) + chgtime->tv_nsec); + ((chgtime->tv_sec * utils::time::NANOS_PER_SECOND) + chgtime->tv_nsec); return utils::to_api_error( remote_instance_->fuse_setchgtime(api_path.c_str(), repertory_chgtime)); } @@ -474,7 +487,7 @@ api_error remote_fuse_drive::setchgtime_impl(std::string api_path, api_error remote_fuse_drive::setcrtime_impl(std::string api_path, const struct timespec *crtime) { remote::file_time repertory_crtime = - ((crtime->tv_sec * NANOS_PER_SECOND) + crtime->tv_nsec); + ((crtime->tv_sec * utils::time::NANOS_PER_SECOND) + crtime->tv_nsec); return utils::to_api_error( remote_instance_->fuse_setcrtime(api_path.c_str(), repertory_crtime)); } @@ -555,10 +568,14 @@ auto remote_fuse_drive::utimens_impl(std::string api_path, #endif remote::file_time rtv[2U] = {0}; if (tv != nullptr) { - rtv[0U] = static_cast( - tv[0U].tv_nsec + (tv[0U].tv_sec * NANOS_PER_SECOND)); - rtv[1U] = static_cast( - tv[1U].tv_nsec + (tv[1U].tv_sec * NANOS_PER_SECOND)); + const auto update_timespec = [](auto &dst, const auto &src) { + dst = static_cast( + src.tv_nsec + + (src.tv_sec * static_cast>( + utils::time::NANOS_PER_SECOND))); + }; + update_timespec(rtv[0U], tv[0U]); + update_timespec(rtv[1U], tv[1U]); } return utils::to_api_error(remote_instance_->fuse_utimens( diff --git a/repertory/librepertory/src/drives/fuse/remotefuse/remote_server.cpp b/repertory/librepertory/src/drives/fuse/remotefuse/remote_server.cpp index 0c44b19a..ea768877 100644 --- a/repertory/librepertory/src/drives/fuse/remotefuse/remote_server.cpp +++ b/repertory/librepertory/src/drives/fuse/remotefuse/remote_server.cpp @@ -93,9 +93,10 @@ auto remote_server::populate_file_info(const std::string &api_path, auto error = drive_.get_item_meta(api_path, META_ATTRIBUTES, meta_attributes); if (error == api_error::success) { if (meta_attributes.empty()) { - meta_attributes = utils::file::is_directory(construct_path(api_path)) - ? std::to_string(FILE_ATTRIBUTE_DIRECTORY) - : std::to_string(FILE_ATTRIBUTE_NORMAL); + meta_attributes = + utils::file::directory(construct_path(api_path)).exists() + ? std::to_string(FILE_ATTRIBUTE_DIRECTORY) + : std::to_string(FILE_ATTRIBUTE_NORMAL); drive_.set_item_meta(api_path, META_ATTRIBUTES, meta_attributes); } const auto attributes = utils::string::to_uint32(meta_attributes); @@ -124,18 +125,18 @@ void remote_server::populate_file_info(const std::string &api_path, file_info.AllocationSize = utils::divide_with_ceiling(file_size, WINFSP_ALLOCATION_UNIT) * WINFSP_ALLOCATION_UNIT; - file_info.ChangeTime = utils::unix_time_to_windows_time( + file_info.ChangeTime = utils::time::unix_time_to_windows_time( utils::string::to_uint64(empty_as_zero(meta[META_MODIFIED]))); - file_info.CreationTime = utils::unix_time_to_windows_time( + file_info.CreationTime = utils::time::unix_time_to_windows_time( utils::string::to_uint64(empty_as_zero(meta[META_CREATION]))); file_info.EaSize = 0; file_info.FileAttributes = attributes; file_info.FileSize = file_size; file_info.HardLinks = 0; file_info.IndexNumber = 0; - file_info.LastAccessTime = utils::unix_time_to_windows_time( + file_info.LastAccessTime = utils::time::unix_time_to_windows_time( utils::string::to_uint64(empty_as_zero(meta[META_ACCESSED]))); - file_info.LastWriteTime = utils::unix_time_to_windows_time( + file_info.LastWriteTime = utils::time::unix_time_to_windows_time( utils::string::to_uint64(empty_as_zero(meta[META_WRITTEN]))); if (meta[META_WRITTEN].empty() || (meta[META_WRITTEN] == "0") || (meta[META_WRITTEN] == "116444736000000000")) { @@ -147,30 +148,45 @@ void remote_server::populate_file_info(const std::string &api_path, void remote_server::populate_stat(const struct stat64 &unix_st, remote::stat &r_stat) { - memset(&r_stat, 0, sizeof(r_stat)); + r_stat = {}; + #if defined(__APPLE__) r_stat.st_flags = unix_st.st_flags; - r_stat.st_atimespec = unix_st.st_atimespec.tv_nsec + - (unix_st.st_atimespec.tv_sec * NANOS_PER_SECOND); + r_stat.st_atimespec = + unix_st.st_atimespec.tv_nsec + + (unix_st.st_atimespec.tv_sec * utils::time::NANOS_PER_SECOND); r_stat.st_birthtimespec = unix_st.st_birthtimespec.tv_nsec + - (unix_st.st_birthtimespec.tv_sec * NANOS_PER_SECOND); - r_stat.st_ctimespec = unix_st.st_ctimespec.tv_nsec + - (unix_st.st_ctimespec.tv_sec * NANOS_PER_SECOND); - r_stat.st_mtimespec = unix_st.st_mtimespec.tv_nsec + - (unix_st.st_mtimespec.tv_sec * NANOS_PER_SECOND); + (unix_st.st_birthtimespec.tv_sec * utils::time::NANOS_PER_SECOND); + r_stat.st_ctimespec = + unix_st.st_ctimespec.tv_nsec + + (unix_st.st_ctimespec.tv_sec * utils::time::NANOS_PER_SECOND); + r_stat.st_mtimespec = + unix_st.st_mtimespec.tv_nsec + + (unix_st.st_mtimespec.tv_sec * utils::time::NANOS_PER_SECOND); #else // !defined(__APPLE__) r_stat.st_flags = 0; - r_stat.st_atimespec = static_cast( - unix_st.st_atim.tv_nsec + (unix_st.st_atim.tv_sec * NANOS_PER_SECOND)); - r_stat.st_birthtimespec = static_cast( - unix_st.st_ctim.tv_nsec + (unix_st.st_ctim.tv_sec * NANOS_PER_SECOND)); - r_stat.st_ctimespec = static_cast( - unix_st.st_ctim.tv_nsec + (unix_st.st_ctim.tv_sec * NANOS_PER_SECOND)); - r_stat.st_mtimespec = static_cast( - unix_st.st_mtim.tv_nsec + (unix_st.st_mtim.tv_sec * NANOS_PER_SECOND)); + r_stat.st_atimespec = + static_cast(unix_st.st_atim.tv_nsec) + + (static_cast(unix_st.st_atim.tv_sec) * + utils::time::NANOS_PER_SECOND); + + r_stat.st_birthtimespec = + static_cast(unix_st.st_ctim.tv_nsec) + + (static_cast(unix_st.st_ctim.tv_sec) * + utils::time::NANOS_PER_SECOND); + + r_stat.st_ctimespec = + static_cast(unix_st.st_ctim.tv_nsec) + + (static_cast(unix_st.st_ctim.tv_sec) * + utils::time::NANOS_PER_SECOND); + + r_stat.st_mtimespec = + static_cast(unix_st.st_mtim.tv_nsec) + + (static_cast(unix_st.st_mtim.tv_sec) * + utils::time::NANOS_PER_SECOND); #endif // defined(__APPLE__) r_stat.st_blksize = static_cast(unix_st.st_blksize); r_stat.st_blocks = static_cast(unix_st.st_blocks); @@ -319,12 +335,13 @@ auto remote_server::fuse_fgetattr( static_cast(__FUNCTION__), }; + r_stat = {}; + const auto file_path = construct_path(path); - memset(&r_stat, 0, sizeof(remote::stat)); auto res = has_open_info(static_cast(handle), EBADF); if (res == 0) { - directory = utils::file::is_directory(file_path); + directory = utils::file::directory(file_path).exists(); struct stat64 unix_st {}; res = fstat64(static_cast(handle), &unix_st); if (res == 0) { @@ -405,16 +422,18 @@ auto remote_server::fuse_fsetattr_x( if (SETATTR_WANTS_MODTIME(&attr)) { struct timeval val[2]; if (SETATTR_WANTS_ACCTIME(&attr)) { - val[0U].tv_sec = static_cast(attr.acctime / NANOS_PER_SECOND); - val[0U].tv_usec = - static_cast((attr.acctime % NANOS_PER_SECOND) / 1000); + val[0U].tv_sec = + static_cast(attr.acctime / utils::time::NANOS_PER_SECOND); + val[0U].tv_usec = static_cast( + (attr.acctime % utils::time::NANOS_PER_SECOND) / 1000); } else { gettimeofday(&val[0U], nullptr); } - val[1U].tv_sec = static_cast(attr.modtime / NANOS_PER_SECOND); - val[1U].tv_usec = - static_cast((attr.modtime % NANOS_PER_SECOND) / 1000); + val[1U].tv_sec = + static_cast(attr.modtime / utils::time::NANOS_PER_SECOND); + val[1U].tv_usec = static_cast( + (attr.modtime % utils::time::NANOS_PER_SECOND) / 1000); res = utimes(file_path.c_str(), &val[0U]); } @@ -486,9 +505,10 @@ auto remote_server::fuse_getattr(const char *path, remote::stat &r_stat, const auto api_path = utils::path::create_api_path(path); const auto file_path = construct_path(api_path); const auto parent_api_path = utils::path::get_parent_api_path(api_path); - memset(&r_stat, 0, sizeof(remote::stat)); - directory = utils::file::is_directory(file_path); + r_stat = {}; + + directory = utils::file::directory(file_path).exists(); struct stat64 unix_st {}; auto res = stat64(file_path.c_str(), &unix_st); @@ -651,7 +671,7 @@ auto remote_server::fuse_opendir(const char *path, remote::file_handle &handle) auto res = -1; errno = ENOENT; - if (utils::file::is_directory(file_path)) { + if (utils::file::directory(file_path).exists()) { auto iter = std::make_shared( drive_.get_directory_items(utils::path::create_api_path(path))); @@ -949,10 +969,10 @@ auto remote_server::fuse_statfs_x(const char *path, std::uint64_t bsize, r_stat.f_blocks ? (r_stat.f_blocks - used_blocks) : 0; r_stat.f_ffree = r_stat.f_favail = r_stat.f_files - drive_.get_total_item_count(); - std::memset(&r_stat.f_mntfromname[0U], 0, sizeof(r_stat.f_mntfromname)); - strncpy(&r_stat.f_mntfromname[0U], + std::memset(r_stat.f_mntfromname.data(), 0, r_stat.f_mntfromname.size()); + strncpy(r_stat.f_mntfromname.data(), (utils::create_volume_label(config_.get_provider_type())).c_str(), - sizeof(r_stat.f_mntfromname) - 1U); + r_stat.f_mntfromname.size() - 1U); RAISE_REMOTE_FUSE_SERVER_EVENT(function_name, file_path, 0); return 0; @@ -993,21 +1013,19 @@ auto remote_server::fuse_utimens(const char *path, const remote::file_time *tv, const auto file_path = construct_path(path); struct timespec tv2[2] = {{0, 0}}; - if ((op0 == UTIME_NOW) || (op0 == UTIME_OMIT)) { - tv2[0U].tv_nsec = static_cast(op0); - tv2[0U].tv_sec = 0; - } else { - tv2[0U].tv_nsec = static_cast(tv[0U] % NANOS_PER_SECOND); - tv2[0U].tv_sec = static_cast(tv[0U] / NANOS_PER_SECOND); - } + const auto process_timespec = [](auto op, const auto &src, auto &dst) { + if ((op == UTIME_NOW) || (op == UTIME_OMIT)) { + dst.tv_nsec = static_cast(op); + dst.tv_sec = 0; + return; + } - if ((op1 == UTIME_NOW) || (op1 == UTIME_OMIT)) { - tv2[1U].tv_nsec = static_cast(op1); - tv2[1U].tv_sec = 0; - } else { - tv2[1U].tv_nsec = static_cast(tv[1U] % NANOS_PER_SECOND); - tv2[1U].tv_sec = static_cast(tv[1U] / NANOS_PER_SECOND); - } + dst.tv_nsec = static_cast(src % utils::time::NANOS_PER_SECOND); + dst.tv_sec = static_cast(src / utils::time::NANOS_PER_SECOND); + }; + + process_timespec(op0, tv[0U], tv2[0U]); + process_timespec(op1, tv[1U], tv2[1U]); const auto res = utimensat(0, file_path.c_str(), &tv2[0U], AT_SYMLINK_NOFOLLOW); @@ -1065,7 +1083,7 @@ auto remote_server::winfsp_can_delete(PVOID file_desc, STATUS_INVALID_HANDLE)); if (ret == STATUS_SUCCESS) { ret = static_cast( - utils::file::is_directory(file_path) + utils::file::directory(file_path).exists() ? drive_.get_directory_item_count( utils::path::create_api_path(relative_path)) ? STATUS_DIRECTORY_NOT_EMPTY @@ -1090,7 +1108,7 @@ auto remote_server::winfsp_cleanup(PVOID /*file_desc*/, PWSTR file_name, const auto file_path = construct_path(relative_path); was_closed = 0; - const auto directory = utils::file::is_directory(file_path); + const auto directory = utils::file::directory(file_path).exists(); if (flags & FileSystemBase::FspCleanupDelete) { remove_all(file_path); was_closed = 1; @@ -1170,7 +1188,7 @@ auto remote_server::winfsp_create(PWSTR file_name, UINT32 create_options, const auto relative_path = utils::string::to_utf8(file_name); const auto file_path = construct_path(relative_path); - exists = utils::file::is_file(file_path); + exists = utils::file::file(file_path).exists(); if ((create_options & FILE_DIRECTORY_FILE) != 0U) { attributes |= FILE_ATTRIBUTE_DIRECTORY; @@ -1268,8 +1286,8 @@ auto remote_server::winfsp_get_security_by_name( auto ret = static_cast(STATUS_SUCCESS); const auto file_path = construct_path(file_name); - if (utils::file::is_file(file_path) || - (utils::file::is_directory(file_path))) { + if (utils::file::file(file_path).exists() || + (utils::file::directory(file_path).exists())) { if (attributes) { remote::file_info file_info{}; if ((ret = populate_file_info(construct_api_path(file_path), @@ -1318,7 +1336,7 @@ auto remote_server::winfsp_open( const auto relative_path = utils::string::to_utf8(file_name); const auto file_path = construct_path(relative_path); - const auto directory = utils::file::is_directory(file_path); + const auto directory = utils::file::directory(file_path).exists(); if (directory) { create_options |= FILE_DIRECTORY_FILE; } @@ -1489,11 +1507,11 @@ auto remote_server::winfsp_rename( auto res = -1; errno = ENOENT; - if (utils::file::is_file(file_path)) { + if (utils::file::file(file_path).exists()) { res = drive_.rename_file(construct_api_path(file_path), construct_api_path(new_file_path), replace_if_exists != 0U); - } else if (utils::file::is_directory(file_path)) { + } else if (utils::file::directory(file_path).exists()) { res = drive_.rename_directory(construct_api_path(file_path), construct_api_path(new_file_path)); } @@ -1523,7 +1541,7 @@ auto remote_server::winfsp_set_basic_info( if (attributes == INVALID_FILE_ATTRIBUTES) { attributes = 0; } else if (attributes == 0) { - attributes = utils::file::is_directory(file_path) + attributes = utils::file::directory(file_path).exists() ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL; } @@ -1542,25 +1560,28 @@ auto remote_server::winfsp_set_basic_info( if ((creation_time != 0U) && (creation_time != max_time)) { drive_.set_item_meta( api_path, META_CREATION, - std::to_string(utils::windows_time_to_unix_time(creation_time))); + std::to_string( + utils::time::windows_time_to_unix_time(creation_time))); } if ((last_access_time != 0U) && (last_access_time != max_time)) { drive_.set_item_meta( api_path, META_ACCESSED, - std::to_string(utils::windows_time_to_unix_time(last_access_time))); + std::to_string( + utils::time::windows_time_to_unix_time(last_access_time))); } if ((last_write_time != 0U) && (last_write_time != max_time)) { drive_.set_item_meta( api_path, META_WRITTEN, - std::to_string(utils::windows_time_to_unix_time(last_write_time))); + std::to_string( + utils::time::windows_time_to_unix_time(last_write_time))); } if ((change_time != 0U) && (change_time != max_time)) { drive_.set_item_meta( api_path, META_MODIFIED, - std::to_string(utils::windows_time_to_unix_time(change_time))); + std::to_string(utils::time::windows_time_to_unix_time(change_time))); } ret = populate_file_info(api_path, *file_info); @@ -1679,7 +1700,7 @@ auto remote_server::json_create_directory_snapshot( auto res = -1; errno = ENOENT; - if (utils::file::is_directory(file_path)) { + if (utils::file::directory(file_path).exists()) { auto iter = std::make_shared( drive_.get_directory_items(api_path)); auto handle = get_next_handle(); @@ -1745,12 +1766,22 @@ auto remote_server::json_release_directory_snapshot( auto remote_server::update_to_windows_format(json &item) -> json & { const auto api_path = item["path"].get(); - item["meta"][META_MODIFIED] = std::to_string(utils::unix_time_to_windows_time( - utils::string::to_uint64(empty_as_zero(item["meta"][META_MODIFIED])))); - item["meta"][META_CREATION] = std::to_string(utils::unix_time_to_windows_time( - utils::string::to_uint64(empty_as_zero(item["meta"][META_CREATION])))); - item["meta"][META_ACCESSED] = std::to_string(utils::unix_time_to_windows_time( - utils::string::to_uint64(empty_as_zero(item["meta"][META_ACCESSED])))); + item["meta"][META_ACCESSED] = std::to_string( + utils::string::to_uint64(empty_as_zero(item["meta"][META_ACCESSED]))); + item["meta"][META_CREATION] = std::to_string( + utils::string::to_uint64(empty_as_zero(item["meta"][META_CREATION]))); + item["meta"][META_MODIFIED] = std::to_string( + utils::string::to_uint64(empty_as_zero(item["meta"][META_MODIFIED]))); + + if (item["meta"][META_WRITTEN].empty() || + (item["meta"][META_WRITTEN].get() == "0") || + (item["meta"][META_WRITTEN].get() == + std::to_string(utils::time::WIN32_TIME_CONVERSION))) { + drive_.set_item_meta(api_path, META_WRITTEN, + item["meta"][META_MODIFIED].get()); + item["meta"][META_WRITTEN] = item["meta"][META_MODIFIED]; + } + if (item["meta"][META_ATTRIBUTES].empty()) { item["meta"][META_ATTRIBUTES] = item["directory"].get() ? std::to_string(FILE_ATTRIBUTE_DIRECTORY) @@ -1759,19 +1790,6 @@ auto remote_server::update_to_windows_format(json &item) -> json & { item["meta"][META_ATTRIBUTES].get()); } - if (item["meta"][META_WRITTEN].empty() || - (item["meta"][META_WRITTEN].get() == "0") || - (item["meta"][META_WRITTEN].get() == "116444736000000000")) { - drive_.set_item_meta(api_path, META_WRITTEN, - item["meta"][META_MODIFIED].get()); - item["meta"][META_WRITTEN] = std::to_string( - utils::unix_time_to_windows_time(utils::string::to_uint64( - empty_as_zero(item["meta"][META_MODIFIED])))); - } else { - item["meta"][META_WRITTEN] = std::to_string( - utils::unix_time_to_windows_time(utils::string::to_uint64( - empty_as_zero(item["meta"][META_WRITTEN])))); - } return item; } } // namespace repertory::remote_fuse diff --git a/repertory/librepertory/src/drives/remote/remote_open_file_table.cpp b/repertory/librepertory/src/drives/remote/remote_open_file_table.cpp index 1bbf7395..b4925dfc 100644 --- a/repertory/librepertory/src/drives/remote/remote_open_file_table.cpp +++ b/repertory/librepertory/src/drives/remote/remote_open_file_table.cpp @@ -39,14 +39,14 @@ void remote_open_file_table::add_directory(const std::string &client_id, void remote_open_file_table::close_all(const std::string &client_id) { std::vector compat_handles; unique_recur_mutex_lock compat_lock(compat_mutex_); - for (auto &kv : compat_lookup_) { + for (auto &&kv : compat_lookup_) { if (kv.second.client_id == client_id) { compat_handles.emplace_back(kv.first); } } compat_lock.unlock(); - for (auto &handle : compat_handles) { + for (auto &&handle : compat_handles) { #if defined(_WIN32) _close(static_cast(handle)); #else @@ -57,14 +57,14 @@ void remote_open_file_table::close_all(const std::string &client_id) { std::vector handles; unique_recur_mutex_lock file_lock(file_mutex_); - for (auto &kv : file_lookup_) { + for (auto &&kv : file_lookup_) { if (kv.second.client_id == client_id) { handles.emplace_back(kv.first); } } file_lock.unlock(); - for (auto &handle : handles) { + for (auto &&handle : handles) { #if defined(_WIN32) ::CloseHandle(handle); #else @@ -75,7 +75,7 @@ void remote_open_file_table::close_all(const std::string &client_id) { std::vector dirs; unique_recur_mutex_lock directory_lock(directory_mutex_); - for (auto &kv : directory_lookup_) { + for (auto &&kv : directory_lookup_) { if (kv.first == client_id) { dirs.insert(dirs.end(), kv.second.begin(), kv.second.end()); } @@ -183,11 +183,11 @@ void remote_open_file_table::remove_all(const std::string &file_path) { }); file_lock.unlock(); - for (auto &handle : open_list) { + for (auto &&handle : open_list) { remove_open_info(handle); } - for (auto &handle : compat_open_list) { + for (auto &&handle : compat_open_list) { remove_compat_open_info(handle); } } diff --git a/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_client.cpp b/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_client.cpp index 4cf11984..3b1c9213 100644 --- a/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_client.cpp +++ b/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_client.cpp @@ -65,7 +65,10 @@ auto remote_client::winfsp_can_delete(PVOID file_desc, request.encode(file_name); std::uint32_t service_flags{}; - const auto ret = packet_client_.send(function_name, request, service_flags); + auto ret{ + packet_client_.send(function_name, request, service_flags), + }; + RAISE_REMOTE_WINFSP_CLIENT_EVENT( function_name, utils::path::create_api_path(utils::string::to_utf8(file_name)), ret); @@ -83,8 +86,9 @@ auto remote_client::json_create_directory_snapshot( packet response; std::uint32_t service_flags{}; - auto ret = - packet_client_.send(function_name, request, response, service_flags); + auto ret{ + packet_client_.send(function_name, request, response, service_flags), + }; if (ret == 0) { ret = packet::decode_json(response, json_data); } @@ -107,8 +111,9 @@ auto remote_client::json_read_directory_snapshot( packet response; std::uint32_t service_flags{}; - auto ret = - packet_client_.send(function_name, request, response, service_flags); + auto ret{ + packet_client_.send(function_name, request, response, service_flags), + }; if (ret == 0) { ret = packet::decode_json(response, json_data); } @@ -129,7 +134,9 @@ auto remote_client::json_release_directory_snapshot( request.encode(handle); std::uint32_t service_flags{}; - const auto ret = packet_client_.send(function_name, request, service_flags); + auto ret{ + packet_client_.send(function_name, request, service_flags), + }; RAISE_REMOTE_WINFSP_CLIENT_EVENT(function_name, path, ret); return ret; @@ -142,8 +149,14 @@ auto remote_client::winfsp_cleanup(PVOID file_desc, PWSTR file_name, static_cast(__FUNCTION__), }; - auto handle = to_handle(file_desc); - const auto file_path = get_open_file_path(handle); + auto handle{ + to_handle(file_desc), + }; + + auto file_path{ + get_open_file_path(handle), + }; + was_closed = 0; packet request; @@ -153,8 +166,9 @@ auto remote_client::winfsp_cleanup(PVOID file_desc, PWSTR file_name, packet response; std::uint32_t service_flags{}; - auto ret = - packet_client_.send(function_name, request, response, service_flags); + auto ret{ + packet_client_.send(function_name, request, response, service_flags), + }; DECODE_OR_IGNORE(&response, was_closed); if (was_closed != 0U) { remove_all(file_path); @@ -168,15 +182,21 @@ auto remote_client::winfsp_close(PVOID file_desc) -> packet::error_type { static_cast(__FUNCTION__), }; - auto handle = to_handle(file_desc); + auto handle{ + to_handle(file_desc), + }; if (has_open_info(handle, STATUS_INVALID_HANDLE) == STATUS_SUCCESS) { - const auto file_path = get_open_file_path(handle); + auto file_path{ + get_open_file_path(handle), + }; packet request; request.encode(file_desc); std::uint32_t service_flags{}; - const auto ret = packet_client_.send(function_name, request, service_flags); + auto ret{ + packet_client_.send(function_name, request, service_flags), + }; if ((ret == STATUS_SUCCESS) || (ret == static_cast(STATUS_INVALID_HANDLE))) { remove_open_info(handle); @@ -206,8 +226,9 @@ auto remote_client::winfsp_create(PWSTR file_name, UINT32 create_options, packet response; std::uint32_t service_flags{}; - auto ret = - packet_client_.send(function_name, request, response, service_flags); + auto ret{ + packet_client_.send(function_name, request, response, service_flags), + }; if (ret == STATUS_SUCCESS) { HANDLE handle{}; DECODE_OR_IGNORE(&response, handle); @@ -243,8 +264,9 @@ auto remote_client::winfsp_flush(PVOID file_desc, remote::file_info *file_info) packet response; std::uint32_t service_flags{}; - auto ret = - packet_client_.send(function_name, request, response, service_flags); + auto ret{ + packet_client_.send(function_name, request, response, service_flags), + }; DECODE_OR_IGNORE(&response, *file_info); RAISE_REMOTE_WINFSP_CLIENT_EVENT( @@ -274,8 +296,9 @@ auto remote_client::winfsp_get_file_info( packet response; std::uint32_t service_flags{}; - auto ret = - packet_client_.send(function_name, request, response, service_flags); + auto ret{ + packet_client_.send(function_name, request, response, service_flags), + }; DECODE_OR_IGNORE(&response, *file_info); RAISE_REMOTE_WINFSP_CLIENT_EVENT( @@ -298,8 +321,9 @@ auto remote_client::winfsp_get_security_by_name( packet response; std::uint32_t service_flags{}; - auto ret = - packet_client_.send(function_name, request, response, service_flags); + auto ret{ + packet_client_.send(function_name, request, response, service_flags), + }; string_descriptor.clear(); DECODE_OR_IGNORE(&response, string_descriptor); @@ -326,8 +350,9 @@ auto remote_client::winfsp_get_volume_info( packet request; packet response; std::uint32_t service_flags{}; - auto ret = - packet_client_.send(function_name, request, response, service_flags); + auto ret{ + packet_client_.send(function_name, request, response, service_flags), + }; DECODE_OR_IGNORE(&response, total_size); DECODE_OR_IGNORE(&response, free_size); DECODE_OR_IGNORE(&response, volume_label); @@ -346,8 +371,13 @@ auto remote_client::winfsp_mounted(const std::wstring &location) request.encode(location); std::uint32_t service_flags{}; - const auto ret = packet_client_.send(function_name, request, service_flags); - const auto mount_location = utils::string::to_utf8(location); + auto ret{ + packet_client_.send(function_name, request, service_flags), + }; + + auto mount_location{ + utils::string::to_utf8(location), + }; event_system::instance().raise(mount_location); RAISE_REMOTE_WINFSP_CLIENT_EVENT(function_name, mount_location, ret); @@ -369,8 +399,9 @@ auto remote_client::winfsp_open( packet response; std::uint32_t service_flags{}; - auto ret = - packet_client_.send(function_name, request, response, service_flags); + auto ret{ + packet_client_.send(function_name, request, response, service_flags), + }; if (ret == STATUS_SUCCESS) { HANDLE handle{}; DECODE_OR_IGNORE(&response, handle); @@ -406,8 +437,9 @@ auto remote_client::winfsp_overwrite( packet response; std::uint32_t service_flags{}; - auto ret = - packet_client_.send(function_name, request, response, service_flags); + auto ret{ + packet_client_.send(function_name, request, response, service_flags), + }; DECODE_OR_IGNORE(&response, *file_info); RAISE_REMOTE_WINFSP_CLIENT_EVENT( @@ -429,8 +461,9 @@ auto remote_client::winfsp_read(PVOID file_desc, PVOID buffer, UINT64 offset, packet response; std::uint32_t service_flags{}; - auto ret = - packet_client_.send(function_name, request, response, service_flags); + auto ret{ + packet_client_.send(function_name, request, response, service_flags), + }; DECODE_OR_IGNORE(&response, *bytes_transferred); if (ret == STATUS_SUCCESS) { ret = response.decode(buffer, *bytes_transferred); @@ -464,8 +497,9 @@ auto remote_client::winfsp_read_directory(PVOID file_desc, PWSTR pattern, packet response; std::uint32_t service_flags{}; - auto ret = - packet_client_.send(function_name, request, response, service_flags); + auto ret{ + packet_client_.send(function_name, request, response, service_flags), + }; if (ret == STATUS_SUCCESS) { ret = packet::decode_json(response, item_list); } @@ -489,7 +523,10 @@ auto remote_client::winfsp_rename( request.encode(replace_if_exists); std::uint32_t service_flags{}; - const auto ret = packet_client_.send(function_name, request, service_flags); + auto ret{ + packet_client_.send(function_name, request, service_flags), + }; + RAISE_REMOTE_WINFSP_CLIENT_EVENT( function_name, utils::path::create_api_path(utils::string::to_utf8(file_name)) + "|" + @@ -516,8 +553,9 @@ auto remote_client::winfsp_set_basic_info( packet response; std::uint32_t service_flags{}; - auto ret = - packet_client_.send(function_name, request, response, service_flags); + auto ret{ + packet_client_.send(function_name, request, response, service_flags), + }; DECODE_OR_IGNORE(&response, *file_info); RAISE_REMOTE_WINFSP_CLIENT_EVENT( @@ -539,8 +577,9 @@ auto remote_client::winfsp_set_file_size( packet response; std::uint32_t service_flags{}; - auto ret = - packet_client_.send(function_name, request, response, service_flags); + auto ret{ + packet_client_.send(function_name, request, response, service_flags), + }; DECODE_OR_IGNORE(&response, *file_info); RAISE_REMOTE_WINFSP_CLIENT_EVENT( @@ -554,13 +593,17 @@ auto remote_client::winfsp_unmounted(const std::wstring &location) static_cast(__FUNCTION__), }; - const auto mount_location = utils::string::to_utf8(location); + auto mount_location{ + utils::string::to_utf8(location), + }; event_system::instance().raise(mount_location); packet request; request.encode(location); std::uint32_t service_flags{}; - const auto ret = packet_client_.send(function_name, request, service_flags); + auto ret{ + packet_client_.send(function_name, request, service_flags), + }; event_system::instance().raise(mount_location); RAISE_REMOTE_WINFSP_CLIENT_EVENT(function_name, mount_location, ret); @@ -587,8 +630,9 @@ auto remote_client::winfsp_write( packet response; std::uint32_t service_flags{}; - auto ret = - packet_client_.send(function_name, request, response, service_flags); + auto ret{ + packet_client_.send(function_name, request, response, service_flags), + }; DECODE_OR_IGNORE(&response, *bytes_transferred); DECODE_OR_IGNORE(&response, *file_info); @@ -603,5 +647,5 @@ auto remote_client::winfsp_write( auto remote_client::to_handle(PVOID file_desc) -> native_handle { return static_cast(reinterpret_cast(file_desc)); } -#endif +#endif // !defined(_WIN32) } // namespace repertory::remote_winfsp diff --git a/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_server.cpp b/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_server.cpp index be961850..8ed80c1c 100644 --- a/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_server.cpp +++ b/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_server.cpp @@ -38,18 +38,11 @@ #include "types/remote.hpp" #include "types/repertory.hpp" #include "utils/common.hpp" +#include "utils/file.hpp" #include "utils/file_utils.hpp" #include "utils/path.hpp" #include "utils/time.hpp" -#if !defined(_SH_DENYNO) -#define _SH_DENYRW 0x10 // deny read/write mode -#define _SH_DENYWR 0x20 // deny write mode -#define _SH_DENYRD 0x30 // deny read mode -#define _SH_DENYNO 0x40 // deny none mode -#define _SH_SECURE 0x80 // secure mode -#endif - namespace repertory::remote_winfsp { #define RAISE_REMOTE_WINFSP_SERVER_EVENT(func, file, ret) \ if (config_.get_enable_drive_events() && \ @@ -99,10 +92,14 @@ void remote_server::populate_stat(const char *path, bool directory, directory ? 2 + drive_.get_directory_item_count( utils::path::create_api_path(path)) : 1); - r_stat.st_atimespec = utils::time::time64_to_unix_time(unix_st.st_atime); - r_stat.st_birthtimespec = utils::time::time64_to_unix_time(unix_st.st_ctime); - r_stat.st_ctimespec = utils::time::time64_to_unix_time(unix_st.st_ctime); - r_stat.st_mtimespec = utils::time::time64_to_unix_time(unix_st.st_mtime); + r_stat.st_atimespec = + utils::time::windows_time_t_to_unix_time(unix_st.st_atime); + r_stat.st_birthtimespec = + utils::time::windows_time_t_to_unix_time(unix_st.st_ctime); + r_stat.st_ctimespec = + utils::time::windows_time_t_to_unix_time(unix_st.st_ctime); + r_stat.st_mtimespec = + utils::time::windows_time_t_to_unix_time(unix_st.st_mtime); r_stat.st_size = static_cast(unix_st.st_size); r_stat.st_mode = unix_st.st_mode; } @@ -114,10 +111,22 @@ auto remote_server::fuse_access(const char *path, const std::int32_t &mask) static_cast(__FUNCTION__), }; - const auto file_path = construct_path(path); - const auto windows_mask = utils::unix_access_mask_to_windows(mask); - const auto res = _access(file_path.c_str(), windows_mask); - const auto ret = ((res < 0) ? -errno : 0); + auto file_path{ + construct_path(path), + }; + + auto windows_mask{ + utils::unix_access_mask_to_windows(mask), + }; + + auto res{ + _access(file_path.c_str(), windows_mask), + }; + + auto ret{ + ((res < 0) ? -errno : 0), + }; + RAISE_REMOTE_WINFSP_SERVER_EVENT(function_name, file_path, ret); return ret; } @@ -188,12 +197,15 @@ auto remote_server::fuse_fgetattr( static_cast(__FUNCTION__), }; - const auto file_path = construct_path(path); - memset(&r_stat, 0, sizeof(remote::stat)); + r_stat = {}; - auto res = has_compat_open_info(handle, EBADF); + const auto file_path = construct_path(path); + + auto res{ + has_compat_open_info(handle, EBADF), + }; if (res == 0) { - directory = utils::file::is_directory(file_path); + directory = utils::file::directory(file_path).exists(); struct _stat64 unix_st {}; res = _fstat64(static_cast(handle), &unix_st); if (res == 0) { @@ -228,7 +240,9 @@ auto remote_server::fuse_fsync( const auto file_path = construct_path(path); - auto res = has_compat_open_info(handle, EBADF); + auto res{ + has_compat_open_info(handle, EBADF), + }; if (res == 0) { res = -1; errno = EBADF; @@ -257,7 +271,9 @@ auto remote_server::fuse_ftruncate( const auto file_path = construct_path(path); - auto res = has_compat_open_info(handle, EBADF); + auto res{ + has_compat_open_info(handle, EBADF), + }; if (res == 0) { res = -1; errno = EBADF; @@ -286,13 +302,16 @@ auto remote_server::fuse_getattr(const char *path, remote::stat &r_st, static_cast(__FUNCTION__), }; - const auto file_path = construct_path(path); - memset(&r_st, 0, sizeof(remote::stat)); + r_st = {}; - directory = utils::file::is_directory(file_path); + const auto file_path = construct_path(path); + + directory = utils::file::directory(file_path).exists(); struct _stat64 st1 {}; - const auto res = _stat64(file_path.c_str(), &st1); + auto res{ + _stat64(file_path.c_str(), &st1), + }; if (res == 0) { populate_stat(path, directory, r_st, st1); } @@ -352,7 +371,9 @@ auto remote_server::fuse_mkdir(const char *path, }; const auto file_path = construct_path(path); - const auto res = _mkdir(file_path.c_str()); + auto res{ + _mkdir(file_path.c_str()), + }; const auto ret = ((res < 0) ? -errno : 0); RAISE_REMOTE_WINFSP_SERVER_EVENT(function_name, file_path, ret); return ret; @@ -366,7 +387,7 @@ auto remote_server::fuse_opendir(const char *path, remote::file_handle &handle) const auto file_path = construct_path(path); const auto unicode_file_path = utils::string::from_utf8(file_path); - auto res = -1; + auto res{-1}; errno = ENOENT; if (::PathIsDirectoryW(unicode_file_path.c_str()) != 0) { @@ -409,8 +430,9 @@ auto remote_server::fuse_create(const char *path, const remote::file_mode &mode, ret = -EACCES; } else { int file = -1; - const auto res = - _sopen_s(&file, file_path.c_str(), open_flags, _SH_DENYNO, perms); + auto res{ + _sopen_s(&file, file_path.c_str(), open_flags, _SH_DENYNO, perms), + }; if (res == 0) { handle = static_cast(file); ret = 0; @@ -432,7 +454,7 @@ auto remote_server::fuse_open(const char *path, const remote::open_flags &flags, }; const auto file_path = construct_path(path); - auto res = -1; + auto res{-1}; if ((flags & remote::open_flags::directory) == remote::open_flags::directory) { @@ -467,34 +489,21 @@ auto remote_server::fuse_read( const auto file_path = construct_path(path); auto &data = *reinterpret_cast(buffer); - auto res = 0; + auto res{0}; if (read_size > std::numeric_limits::max()) { res = -1; errno = ERANGE; } else if ((res = has_compat_open_info(handle, EBADF)) == 0) { - res = -1; - errno = EBADF; - auto *os_handle = - reinterpret_cast(_get_osfhandle(static_cast(handle))); - if (os_handle != INVALID_HANDLE_VALUE) { - errno = EFAULT; - - auto file = native_file::attach(os_handle); - std::uint64_t file_size{}; - if (file->get_file_size(file_size)) { - data.resize(utils::calculate_read_size( - file_size, static_cast(read_size), read_offset)); - if (!data.empty()) { - std::size_t bytes_read{}; - if (file->read_bytes(data.data(), data.size(), read_offset, - bytes_read)) { - res = 0; - errno = 0; - } - } else { - res = 0; - errno = 0; - } + res = static_cast>(_lseeki64( + static_cast(handle), static_cast<__int64>(read_offset), SEEK_SET)); + if (res != -1) { + data.resize(read_size); + res = read(static_cast(handle), data.data(), + static_cast(data.size())); + if (res == -1) { + data.resize(0U); + } else if (data.size() != static_cast(res)) { + data.resize(static_cast(res)); } } } @@ -514,7 +523,9 @@ auto remote_server::fuse_rename(const char *from, const auto from_path = utils::path::combine(mount_location_, {from}); const auto to_path = utils::path::combine(mount_location_, {to}); - const auto res = rename(from_path.c_str(), to_path.c_str()); + auto res{ + rename(from_path.c_str(), to_path.c_str()), + }; const auto ret = ((res < 0) ? -errno : 0); RAISE_REMOTE_WINFSP_SERVER_EVENT(function_name, from + std::string("|") + to, @@ -532,26 +543,21 @@ auto remote_server::fuse_write( const auto file_path = construct_path(path); std::size_t bytes_written{}; - auto res = 0; + auto res{0}; if (write_size > std::numeric_limits::max()) { res = -1; errno = ERANGE; } else { res = has_compat_open_info(handle, EBADF); if (res == 0) { - res = -1; - errno = EBADF; - auto *os_handle = - reinterpret_cast(_get_osfhandle(static_cast(handle))); - if (os_handle != INVALID_HANDLE_VALUE) { - errno = EFAULT; - if ((write_size == 0) || - native_file::attach(os_handle)->write_bytes( - reinterpret_cast(buffer), - static_cast(write_size), write_offset, - bytes_written)) { - res = 0; - errno = 0; + res = static_cast>( + _lseeki64(static_cast(handle), + static_cast<__int64>(write_offset), SEEK_SET)); + if (res != -1) { + res = write(static_cast(handle), buffer, + static_cast(write_size)); + if (res != -1) { + bytes_written = static_cast(res); } } } @@ -582,7 +588,7 @@ auto remote_server::fuse_readdir(const char *path, }; const auto file_path = construct_path(path); - auto res = 0; + auto res{0}; if (offset > std::numeric_limits::max()) { errno = ERANGE; res = -1; @@ -610,7 +616,9 @@ auto remote_server::fuse_release( const auto file_path = construct_path(path); remove_compat_open_info(handle); - const auto res = _close(static_cast(handle)); + auto res{ + _close(static_cast(handle)), + }; const auto ret = ((res < 0) ? -errno : 0); RAISE_REMOTE_WINFSP_SERVER_EVENT(function_name, file_path, ret); @@ -642,7 +650,9 @@ auto remote_server::fuse_rmdir(const char *path) -> packet::error_type { }; const auto file_path = construct_path(path); - const auto res = _rmdir(file_path.c_str()); + auto res{ + _rmdir(file_path.c_str()), + }; const auto ret = ((res < 0) ? -errno : 0); RAISE_REMOTE_WINFSP_SERVER_EVENT(function_name, file_path, ret); return ret; @@ -767,9 +777,9 @@ auto remote_server::fuse_statfs_x(const char *path, std::uint64_t bsize, r_stat.f_files = 4294967295; r_stat.f_ffree = r_stat.f_favail = r_stat.f_files - drive_.get_total_item_count(); - strncpy(&r_stat.f_mntfromname[0U], + strncpy(r_stat.f_mntfromname.data(), (utils::create_volume_label(config_.get_provider_type())).c_str(), - sizeof(r_stat.f_mntfromname) - 1U); + r_stat.f_mntfromname.size() - 1U); RAISE_REMOTE_WINFSP_SERVER_EVENT(function_name, file_path, 0); return 0; @@ -783,7 +793,7 @@ auto remote_server::fuse_truncate( const auto file_path = construct_path(path); const auto unicode_file_path = utils::string::from_utf8(file_path); - auto res = -1; + auto res{-1}; errno = ENOENT; const auto flags_and_attributes = @@ -818,7 +828,9 @@ auto remote_server::fuse_unlink(const char *path) -> packet::error_type { }; const auto file_path = construct_path(path); - const auto res = _unlink(file_path.c_str()); + auto res{ + _unlink(file_path.c_str()), + }; const auto ret = ((res < 0) ? -errno : 0); RAISE_REMOTE_WINFSP_SERVER_EVENT(function_name, file_path, ret); return ret; @@ -834,7 +846,7 @@ auto remote_server::fuse_utimens(const char *path, const remote::file_time *tv, const auto file_path = construct_path(path); const auto unicode_file_path = utils::string::from_utf8(file_path); - auto res = -1; + auto res{-1}; errno = ENOENT; const auto flags_and_attributes = @@ -849,29 +861,27 @@ auto remote_server::fuse_utimens(const char *path, const remote::file_time *tv, if (os_handle != INVALID_HANDLE_VALUE) { FILETIME access_time{}; FILETIME write_time{}; - FILETIME *access_time_ptr = nullptr; - FILETIME *write_time_ptr = nullptr; + FILETIME *access_time_ptr{nullptr}; + FILETIME *write_time_ptr{nullptr}; - if ((tv[0U] == 0U) || (op0 == UTIME_NOW)) { - const auto now = utils::time::get_file_time_now(); - access_time.dwHighDateTime = - static_cast((now >> 32U) & 0xFFFFFFFF); - access_time.dwLowDateTime = now & 0xFFFFFFFF; - access_time_ptr = &access_time; - } else if (op0 != UTIME_OMIT) { - access_time = utils::time::unix_time_to_filetime(tv[0U]); - access_time_ptr = &access_time; - } + const auto proccess_timespec = [](auto op, const auto &src, auto &dst, + auto *&dst_ptr) { + if ((src == 0U) || (op == UTIME_NOW)) { + auto now = + utils::time::unix_time_to_windows_time(utils::time::get_time_now()); + dst.dwHighDateTime = static_cast((now >> 32U) & 0xFFFFFFFF); + dst.dwLowDateTime = now & 0xFFFFFFFF; + dst_ptr = &dst; + return; + } - if ((tv[1U] == 0U) || (op1 == UTIME_NOW)) { - const auto now = utils::time::get_file_time_now(); - write_time.dwHighDateTime = static_cast((now >> 32U) & 0xFFFFFFFF); - write_time.dwLowDateTime = now & 0xFFFFFFFF; - write_time_ptr = &write_time; - } else if (op1 != UTIME_OMIT) { - write_time = utils::time::unix_time_to_filetime(tv[1U]); - write_time_ptr = &write_time; - } + if (op != UTIME_OMIT) { + dst = utils::time::unix_time_to_filetime(src); + dst_ptr = &dst; + } + }; + proccess_timespec(op0, tv[0U], access_time, access_time_ptr); + proccess_timespec(op1, tv[1U], write_time, write_time_ptr); errno = EFAULT; if (::SetFileTime(os_handle, nullptr, access_time_ptr, write_time_ptr) != @@ -879,6 +889,7 @@ auto remote_server::fuse_utimens(const char *path, const remote::file_time *tv, res = 0; errno = 0; } + ::CloseHandle(os_handle); } @@ -896,10 +907,10 @@ auto remote_server::json_create_directory_snapshot( const auto file_path = construct_path(path); - auto res = -1; + auto res{-1}; errno = ENOENT; - if (utils::file::is_directory(file_path)) { + if (utils::file::directory(file_path).exists()) { auto iter = std::make_shared( drive_.get_directory_items(utils::path::create_api_path(path))); auto handle = get_next_handle(); @@ -1037,8 +1048,11 @@ auto remote_server::winfsp_create(PWSTR file_name, UINT32 create_options, const auto file_path = utils::string::from_utf8(utils::path::combine( mount_location_, {utils::string::to_utf8(file_name)})); - exists = static_cast(utils::file::is_file(utils::path::combine( - mount_location_, {utils::string::to_utf8(file_name)}))); + exists = static_cast( + utils::file::file( + utils::path::combine(mount_location_, + {utils::string::to_utf8(file_name)})) + .exists()); auto create_flags = FILE_FLAG_BACKUP_SEMANTICS; if ((create_options & FILE_DIRECTORY_FILE) != 0U) { @@ -1500,4 +1514,4 @@ auto remote_server::winfsp_get_dir_buffer(PVOID /*file_desc*/, PVOID *& /*ptr*/) } } // namespace repertory::remote_winfsp -#endif // _WIN32 +#endif // defined(_WIN32) diff --git a/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_winfsp_drive.cpp b/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_winfsp_drive.cpp index c8c238fa..bc2d0e38 100644 --- a/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_winfsp_drive.cpp +++ b/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_winfsp_drive.cpp @@ -62,7 +62,7 @@ auto remote_winfsp_drive::winfsp_service::OnStart(ULONG, PWSTR *) -> NTSTATUS { (mount_location[1u] == ':'); auto ret = drive_letter ? STATUS_DEVICE_BUSY : STATUS_NOT_SUPPORTED; - if ((drive_letter && not utils::file::is_directory(mount_location))) { + if ((drive_letter && not utils::file::directory(mount_location).exists())) { auto unicode_mount_location = utils::string::from_utf8(mount_location); host_.SetFileSystemName(&unicode_mount_location[0u]); if (config_.get_enable_mount_manager()) { @@ -230,7 +230,7 @@ auto remote_winfsp_drive::Init(PVOID host) -> NTSTATUS { file_system_host->SetPersistentAcls(FALSE); file_system_host->SetPostCleanupWhenModifiedOnly(TRUE); file_system_host->SetPassQueryDirectoryPattern(FALSE); - file_system_host->SetVolumeCreationTime(utils::time::get_file_time_now()); + file_system_host->SetVolumeCreationTime(utils::time::get_time_now()); file_system_host->SetVolumeSerialNumber(0); return STATUS_SUCCESS; } @@ -363,33 +363,33 @@ auto remote_winfsp_drive::ReadDirectory(PVOID /*file_node*/, PVOID file_desc, utils::path::strip_to_file_name(item_path)); if (not marker || (marker && item_found)) { // if (not utils::path::is_ads_file_path(item_path)) { - union { - UINT8 B[FIELD_OFFSET(FSP_FSCTL_DIR_INFO, FileNameBuf) + - ((MAX_PATH + 1) * sizeof(WCHAR))]; - FSP_FSCTL_DIR_INFO D; - } directory_info_buffer; + union { + UINT8 B[FIELD_OFFSET(FSP_FSCTL_DIR_INFO, FileNameBuf) + + ((MAX_PATH + 1) * sizeof(WCHAR))]; + FSP_FSCTL_DIR_INFO D; + } directory_info_buffer; - auto *directory_info = &directory_info_buffer.D; - ::ZeroMemory(directory_info, sizeof(*directory_info)); - directory_info->Size = static_cast( - FIELD_OFFSET(FSP_FSCTL_DIR_INFO, FileNameBuf) + - (std::min((size_t)MAX_PATH, display_name.size()) * - sizeof(WCHAR))); + auto *directory_info = &directory_info_buffer.D; + ::ZeroMemory(directory_info, sizeof(*directory_info)); + directory_info->Size = static_cast( + FIELD_OFFSET(FSP_FSCTL_DIR_INFO, FileNameBuf) + + (std::min(static_cast(MAX_PATH), display_name.size()) * + sizeof(WCHAR))); - if (not item["meta"].empty() || - ((item_path != ".") && (item_path != ".."))) { - populate_file_info(item, directory_info->FileInfo); - } - if (ret == STATUS_SUCCESS) { - ::wcscpy_s(&directory_info->FileNameBuf[0], MAX_PATH, - &display_name[0]); - - FspFileSystemFillDirectoryBuffer(directory_buffer, - directory_info, &ret); - if (ret != STATUS_SUCCESS) { - break; - } + if (not item["meta"].empty() || + ((item_path != ".") && (item_path != ".."))) { + populate_file_info(item, directory_info->FileInfo); + } + if (ret == STATUS_SUCCESS) { + ::wcscpy_s(&directory_info->FileNameBuf[0], MAX_PATH, + &display_name[0]); + + FspFileSystemFillDirectoryBuffer(directory_buffer, directory_info, + &ret); + if (ret != STATUS_SUCCESS) { + break; } + } // } } else { item_found = display_name == std::wstring(marker); diff --git a/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp b/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp index bf5eaf33..99081fb5 100644 --- a/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp +++ b/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp @@ -81,7 +81,7 @@ auto winfsp_drive::winfsp_service::OnStart(ULONG /*Argc*/, (mount_location[1U] == ':'); auto ret = drive_letter ? STATUS_DEVICE_BUSY : STATUS_NOT_SUPPORTED; - if ((drive_letter && not utils::file::is_directory(mount_location))) { + if ((drive_letter && not utils::file::directory(mount_location).exists())) { auto unicode_mount_location = utils::string::from_utf8(mount_location); host_.SetFileSystemName(unicode_mount_location.data()); if (config_.get_enable_mount_manager()) { @@ -215,7 +215,7 @@ VOID winfsp_drive::Cleanup(PVOID file_node, PVOID file_desc, if ((flags & (FspCleanupSetLastAccessTime | FspCleanupSetLastWriteTime | FspCleanupSetChangeTime)) != 0U) { - const auto now = utils::time::get_file_time_now(); + const auto now = utils::time::get_time_now(); if ((flags & FspCleanupSetLastAccessTime) != 0U) { auto res = provider_.set_item_meta(api_path, META_ACCESSED, std::to_string(now)); @@ -238,8 +238,10 @@ VOID winfsp_drive::Cleanup(PVOID file_node, PVOID file_desc, if ((flags & FspCleanupSetChangeTime) != 0U) { auto res = provider_.set_item_meta( - api_path, {{META_CHANGED, std::to_string(now)}, - {META_MODIFIED, std::to_string(now)}}); + api_path, { + {META_CHANGED, std::to_string(now)}, + {META_MODIFIED, std::to_string(now)}, + }); if (res != api_error::success) { utils::error::raise_api_path_error( function_name, api_path, res, @@ -310,7 +312,7 @@ auto winfsp_drive::Create(PWSTR file_name, UINT32 create_options, attributes = FILE_ATTRIBUTE_NORMAL; } - const auto now = utils::time::get_file_time_now(); + const auto now = utils::time::get_time_now(); auto meta = create_meta_attributes( now, attributes, now, now, (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0U, 0U, "", 0U, now, 0U, 0U, 0U, @@ -477,8 +479,7 @@ auto winfsp_drive::get_security_by_name( if (descriptor_size != nullptr) { ULONG size{}; PSECURITY_DESCRIPTOR sec_desc{}; - - if (::ConvertStringSecurityDescriptorToSecurityDescriptor( + if (::ConvertStringSecurityDescriptorToSecurityDescriptorA( "O:BAG:BAD:P(A;;FA;;;SY)(A;;FA;;;BA)(A;;FA;;;WD)", SDDL_REVISION_1, &sec_desc, &size) != 0) { if (size > *descriptor_size) { @@ -512,6 +513,7 @@ auto winfsp_drive::GetSecurityByName(PWSTR file_name, PUINT32 attributes, if (sds != 0U) { *descriptor_size = static_cast(sds); } + RAISE_WINFSP_EVENT(function_name, api_path, ret); return ret; } @@ -579,7 +581,7 @@ auto winfsp_drive::Init(PVOID host) -> NTSTATUS { file_system_host->SetPersistentAcls(FALSE); file_system_host->SetPostCleanupWhenModifiedOnly(TRUE); file_system_host->SetPassQueryDirectoryPattern(FALSE); - file_system_host->SetVolumeCreationTime(utils::time::get_file_time_now()); + file_system_host->SetVolumeCreationTime(utils::time::get_time_now()); file_system_host->SetVolumeSerialNumber(0); return STATUS_SUCCESS; } @@ -590,7 +592,7 @@ auto winfsp_drive::mount(const std::vector &drive_args) -> int { const auto force_no_console = utils::collection::includes(drive_args, "-nc"); auto enable_console = false; - for (const auto &arg : drive_args) { + for (auto &&arg : drive_args) { if (arg == "-f") { if (not force_no_console) { enable_console = true; @@ -620,10 +622,14 @@ auto winfsp_drive::Mounted(PVOID host) -> NTSTATUS { static_cast(__FUNCTION__), }; - auto ret = STATUS_SUCCESS; - utils::file::change_to_process_directory(); + auto ret{STATUS_SUCCESS}; + if (not utils::file::change_to_process_directory()) { + return static_cast(utils::get_last_error_code()); + } - auto *file_system_host = reinterpret_cast(host); + auto *file_system_host{ + reinterpret_cast(host), + }; polling::instance().start(&config_); fm_ = std::make_unique(config_, provider_); server_ = std::make_unique(config_, provider_, *fm_); @@ -868,7 +874,7 @@ auto winfsp_drive::Read(PVOID /*file_node*/, PVOID file_desc, PVOID buffer, data.clear(); auto res = provider_.set_item_meta( api_path, META_ACCESSED, - std::to_string(utils::time::get_file_time_now())); + std::to_string(utils::time::get_time_now())); if (res != api_error::success) { utils::error::raise_api_path_error( function_name, api_path, res, @@ -929,10 +935,9 @@ auto winfsp_drive::ReadDirectory(PVOID /*file_node*/, PVOID file_desc, api_path, {utils::string::to_utf8(marker)}))); while ((error = iterator.get_directory_item( offset++, dir_item)) == api_error::success) { - // if (utils::path::is_ads_file_path(dir_item.api_path) || - // dir_item.api_path == "." || dir_item.api_path == "..") { - // continue; - // } + if (dir_item.api_path == "." || dir_item.api_path == "..") { + continue; + } if (dir_item.meta.empty()) { utils::error::raise_api_path_error( @@ -1058,17 +1063,22 @@ auto winfsp_drive::SetBasicInfo(PVOID /*file_node*/, PVOID file_desc, meta[META_ATTRIBUTES] = std::to_string(attributes); } if ((creation_time != 0U) && (creation_time != max_time)) { - meta[META_CREATION] = std::to_string(creation_time); + meta[META_CREATION] = std::to_string( + utils::time::windows_time_to_unix_time(creation_time)); } if ((last_access_time != 0U) && (last_access_time != max_time)) { - meta[META_ACCESSED] = std::to_string(last_access_time); + meta[META_ACCESSED] = std::to_string( + utils::time::windows_time_to_unix_time(last_access_time)); } if ((last_write_time != 0U) && (last_write_time != max_time)) { - meta[META_WRITTEN] = std::to_string(last_write_time); + meta[META_WRITTEN] = std::to_string( + utils::time::windows_time_to_unix_time(last_write_time)); } if ((change_time != 0U) && (change_time != max_time)) { - meta[META_CHANGED] = std::to_string(change_time); - meta[META_MODIFIED] = std::to_string(change_time); + meta[META_CHANGED] = + std::to_string(utils::time::windows_time_to_unix_time(change_time)); + meta[META_MODIFIED] = + std::to_string(utils::time::windows_time_to_unix_time(change_time)); } error = provider_.set_item_meta(api_path, meta); diff --git a/repertory/librepertory/src/file_manager/file_manager.cpp b/repertory/librepertory/src/file_manager/file_manager.cpp index f27477c1..296a8a27 100644 --- a/repertory/librepertory/src/file_manager/file_manager.cpp +++ b/repertory/librepertory/src/file_manager/file_manager.cpp @@ -116,7 +116,7 @@ file_manager::file_manager(app_config &config, i_provider &provider) } db_.reset(db3); - for (const auto &create_item : sql_create_tables) { + for (auto &&create_item : sql_create_tables) { std::string err; if (not db::execute_sql(*db_, create_item.second, err)) { db_.reset(); @@ -176,12 +176,12 @@ void file_manager::close_timed_out_files() { } return items; }); - for (const auto &closeable_file : closeable_list) { + for (auto &&closeable_file : closeable_list) { open_file_lookup_.erase(closeable_file->get_api_path()); } file_lock.unlock(); - for (auto &closeable_file : closeable_list) { + for (auto &&closeable_file : closeable_list) { closeable_file->close(); event_system::instance().raise( closeable_file->get_api_path()); @@ -244,7 +244,7 @@ auto file_manager::evict_file(const std::string &api_path) -> bool { open_file_lookup_.erase(api_path); - auto removed = utils::file::retry_delete_file(source_path); + auto removed = utils::file::file(source_path).remove(); if (removed) { event_system::instance().raise(api_path, source_path); @@ -329,7 +329,7 @@ auto file_manager::get_open_files() const std::unordered_map ret; recur_mutex_lock open_lock(open_file_mtx_); - for (const auto &item : open_file_lookup_) { + for (auto &&item : open_file_lookup_) { ret[item.first] = item.second->get_open_file_count(); } @@ -513,8 +513,8 @@ void file_manager::queue_upload(const std::string &api_path, db::db_insert{*db_.get(), upload_table} .or_replace() .column_value("api_path", api_path) - .column_value("date_time", static_cast( - utils::time::get_file_time_now())) + .column_value("date_time", + static_cast(utils::time::get_time_now())) .column_value("source_path", source_path) .go(); if (result.ok()) { @@ -557,7 +557,7 @@ auto file_manager::remove_file(const std::string &api_path) -> api_error { return res; } - if (not utils::file::retry_delete_file(fsi.source_path)) { + if (not utils::file::file(fsi.source_path).remove()) { utils::error::raise_api_path_error( function_name, fsi.api_path, fsi.source_path, utils::get_last_error_code(), "failed to delete source"); @@ -778,14 +778,9 @@ auto file_manager::rename_file(const std::string &from_api_path, return res; } - std::uint64_t file_size{}; - if (not utils::file::get_file_size(fsi.source_path, file_size)) { - return api_error::os_error; - } - res = remove_file(to_api_path); if ((res == api_error::success) || (res == api_error::item_not_found)) { - if (not utils::file::retry_delete_file(fsi.source_path)) { + if (not utils::file::file(fsi.source_path).remove()) { utils::error::raise_api_path_error( function_name, fsi.api_path, fsi.source_path, utils::get_last_error_code(), "failed to delete source path"); @@ -848,7 +843,7 @@ void file_manager::start() { } } - for (const auto &active_item : active_items) { + for (auto &&active_item : active_items) { queue_upload(active_item.api_path, active_item.source_path, false); } active_items.clear(); @@ -878,8 +873,9 @@ void file_manager::start() { auto res = provider_.get_filesystem_item(api_path, false, fsi); if (res == api_error::success) { if (source_path == fsi.source_path) { - std::uint64_t file_size{}; - if (utils::file::get_file_size(fsi.source_path, file_size)) { + auto opt_size = utils::file::file{fsi.source_path}.size(); + if (opt_size.has_value()) { + auto file_size{opt_size.value()}; if (file_size == fsi.size) { auto closeable_file = std::make_shared( chunk_size, @@ -941,7 +937,7 @@ void file_manager::stop() { open_file_lookup_.clear(); upload_lock.lock(); - for (auto &item : upload_lookup_) { + for (auto &&item : upload_lookup_) { item.second->stop(); } upload_notify_.notify_all(); @@ -1027,7 +1023,7 @@ void file_manager::upload_completed(const file_upload_completed &evt) { bool exists{}; auto res = provider_.is_file(evt.get_api_path(), exists); if ((res == api_error::success && not exists) || - not utils::file::is_file(evt.get_source().get())) { + not utils::file::file(evt.get_source().get()).exists()) { event_system::instance().raise( evt.get_api_path(), evt.get_source()); remove_upload(evt.get_api_path(), true); @@ -1129,7 +1125,7 @@ void file_manager::upload_handler() { void file_manager::update_used_space(std::uint64_t &used_space) const { recur_mutex_lock open_lock(open_file_mtx_); - for (const auto &item : open_file_lookup_) { + for (auto &&item : open_file_lookup_) { std::uint64_t file_size{}; auto res = provider_.get_file_size(item.second->get_api_path(), file_size); if ((res == api_error::success) && diff --git a/repertory/librepertory/src/file_manager/file_manager_open_file.cpp b/repertory/librepertory/src/file_manager/file_manager_open_file.cpp index 973c6248..fdb0c3a7 100644 --- a/repertory/librepertory/src/file_manager/file_manager_open_file.cpp +++ b/repertory/librepertory/src/file_manager/file_manager_open_file.cpp @@ -66,8 +66,9 @@ file_manager::open_file::open_file( } if (not fsi.directory) { - set_api_error(native_file::create_or_open(fsi.source_path, - provider_.is_direct_only(), nf_)); + nf_ = utils::file::file::open_or_create_file(fsi.source_path, + provider_.is_direct_only()); + set_api_error(*nf_ ? api_error::success : api_error::os_error); if (get_api_error() == api_error::success) { if (read_state.has_value()) { read_state_ = read_state.value(); @@ -77,19 +78,15 @@ file_manager::open_file::open_file( fsi_.size, chunk_size)), false); - std::uint64_t file_size{}; - if (nf_->get_file_size(file_size)) { - if (provider_.is_direct_only() || file_size == fsi.size) { - read_state_.set(0U, read_state_.size(), true); - } else if (not nf_->truncate(fsi.size)) { - set_api_error(api_error::os_error); - } - } else { + auto file_size = nf_->size(); + if (provider_.is_direct_only() || file_size == fsi.size) { + read_state_.set(0U, read_state_.size(), true); + } else if (not nf_->truncate(fsi.size)) { set_api_error(api_error::os_error); } } - if (get_api_error() != api_error::success && nf_) { + if (get_api_error() != api_error::success && *nf_) { nf_->close(); } } @@ -185,8 +182,7 @@ void file_manager::open_file::download_chunk(std::size_t chunk, res = do_io([&]() -> api_error { std::size_t bytes_written{}; - if (not nf_->write_bytes(data.data(), data.size(), data_offset, - bytes_written)) { + if (not nf_->write(data, data_offset, &bytes_written)) { return api_error::os_error; } @@ -296,14 +292,7 @@ auto file_manager::open_file::native_operation( } { - std::uint64_t file_size{}; - if (not nf_->get_file_size(file_size)) { - utils::error::raise_api_path_error(function_name, get_api_path(), - utils::get_last_error_code(), - "failed to get file size"); - return set_api_error(api_error::os_error); - } - + auto file_size = nf_->size().value_or(0U); if (file_size != new_file_size) { utils::error::raise_api_path_error( function_name, get_api_path(), api_error::file_size_mismatch, @@ -331,7 +320,7 @@ auto file_manager::open_file::native_operation( set_modified(); fsi_.size = new_file_size; - const auto now = std::to_string(utils::time::get_file_time_now()); + const auto now = std::to_string(utils::time::get_time_now()); res = provider_.set_item_meta( fsi_.api_path, { {META_CHANGED, now}, @@ -372,7 +361,7 @@ auto file_manager::open_file::read(std::size_t read_size, data.resize(read_size); std::size_t bytes_read{}; - return nf_->read_bytes(data.data(), read_size, read_offset, bytes_read) + return nf_->read(data.data(), read_size, read_offset, &bytes_read) ? api_error::success : api_error::os_error; }); @@ -461,7 +450,6 @@ auto file_manager::open_file::close() -> bool { } nf_->close(); - nf_.reset(); if (modified_ && (get_api_error() == api_error::success)) { mgr_.queue_upload(*this); @@ -470,13 +458,13 @@ auto file_manager::open_file::close() -> bool { mgr_.store_resume(*this); } else if (get_api_error() != api_error::success) { mgr_.remove_resume(get_api_path(), get_source_path()); - if (not utils::file::retry_delete_file(fsi_.source_path)) { + if (not utils::file::file(fsi_.source_path).remove()) { utils::error::raise_api_path_error( function_name, get_api_path(), fsi_.source_path, utils::get_last_error_code(), "failed to delete file"); } - auto parent = utils::path::remove_file_name(fsi_.source_path); + auto parent = utils::path::get_parent_path(fsi_.source_path); fsi_.source_path = utils::path::combine(parent, {utils::create_uuid_string()}); const auto res = provider_.set_item_meta(fsi_.api_path, META_SOURCE, @@ -587,8 +575,7 @@ auto file_manager::open_file::write(std::uint64_t write_offset, } auto res = do_io([&]() -> api_error { - if (not nf_->write_bytes(data.data(), data.size(), write_offset, - bytes_written)) { + if (not nf_->write(data, write_offset, &bytes_written)) { return api_error::os_error; } @@ -599,7 +586,7 @@ auto file_manager::open_file::write(std::uint64_t write_offset, return set_api_error(res); } - const auto now = std::to_string(utils::time::get_file_time_now()); + const auto now = std::to_string(utils::time::get_time_now()); res = provider_.set_item_meta(fsi_.api_path, { {META_CHANGED, now}, {META_MODIFIED, now}, diff --git a/repertory/librepertory/src/file_manager/file_manager_open_file_base.cpp b/repertory/librepertory/src/file_manager/file_manager_open_file_base.cpp index 32e50bce..7ee9adc8 100644 --- a/repertory/librepertory/src/file_manager/file_manager_open_file_base.cpp +++ b/repertory/librepertory/src/file_manager/file_manager_open_file_base.cpp @@ -157,7 +157,7 @@ auto file_manager::open_file_base::get_handles() const -> std::vector { recur_mutex_lock file_lock(file_mtx_); std::vector ret; - for (const auto &item : open_data_) { + for (auto &&item : open_data_) { ret.emplace_back(item.first); } diff --git a/repertory/librepertory/src/file_manager/file_manager_ring_buffer_open_file.cpp b/repertory/librepertory/src/file_manager/file_manager_ring_buffer_open_file.cpp index f488f4dd..f61540f8 100644 --- a/repertory/librepertory/src/file_manager/file_manager_ring_buffer_open_file.cpp +++ b/repertory/librepertory/src/file_manager/file_manager_ring_buffer_open_file.cpp @@ -64,7 +64,7 @@ file_manager::ring_buffer_open_file::ring_buffer_open_file( ring_state_.set(0U, ring_state_.size(), true); buffer_directory = utils::path::absolute(buffer_directory); - if (not utils::file::create_full_directory_path(buffer_directory)) { + if (not utils::file::directory(buffer_directory).create_directory()) { throw std::runtime_error("failed to create buffer directory|path|" + buffer_directory + "|err|" + std::to_string(utils::get_last_error_code())); @@ -72,8 +72,8 @@ file_manager::ring_buffer_open_file::ring_buffer_open_file( fsi_.source_path = utils::path::combine(buffer_directory, {utils::create_uuid_string()}); - auto res = native_file::create_or_open(fsi_.source_path, nf_); - if (res != api_error::success) { + nf_ = utils::file::file::open_or_create_file(fsi_.source_path); + if (not *nf_) { throw std::runtime_error("failed to create buffer file|err|" + std::to_string(utils::get_last_error_code())); } @@ -93,7 +93,7 @@ file_manager::ring_buffer_open_file::~ring_buffer_open_file() { close(); nf_->close(); - if (not utils::file::retry_delete_file(fsi_.source_path)) { + if (not utils::file::file(fsi_.source_path).remove()) { utils::error::raise_api_path_error( function_name, fsi_.api_path, fsi_.source_path, utils::get_last_error_code(), "failed to delete file"); @@ -128,9 +128,8 @@ auto file_manager::file_manager::ring_buffer_open_file::download_chunk( if (res == api_error::success) { res = do_io([&]() -> api_error { std::size_t bytes_written{}; - if (not nf_->write_bytes(buffer.data(), buffer.size(), - (chunk % ring_state_.size()) * chunk_size_, - bytes_written)) { + if (not nf_->write(buffer, (chunk % ring_state_.size()) * chunk_size_, + &bytes_written)) { return api_error::os_error; } @@ -169,8 +168,8 @@ void file_manager::ring_buffer_open_file::forward(std::size_t count) { last_chunk_ = std::min(total_chunks_ - 1U, first_chunk_ + ring_state_.size() - 1U); } else { - for (std::size_t i = 0U; i < added; i++) { - ring_state_[(first_chunk_ + i) % ring_state_.size()] = true; + for (std::size_t idx = 0U; idx < added; ++idx) { + ring_state_[(first_chunk_ + idx) % ring_state_.size()] = true; } first_chunk_ += added; current_chunk_ += count; @@ -221,8 +220,8 @@ void file_manager::ring_buffer_open_file::reverse(std::size_t count) { last_chunk_ = std::min(total_chunks_ - 1U, first_chunk_ + ring_state_.size() - 1U); } else { - for (std::size_t i = 0U; i < removed; i++) { - ring_state_[(last_chunk_ - i) % ring_state_.size()] = true; + for (std::size_t idx = 0U; idx < removed; ++idx) { + ring_state_[(last_chunk_ - idx) % ring_state_.size()] = true; } first_chunk_ -= removed; current_chunk_ -= count; @@ -255,7 +254,7 @@ auto file_manager::ring_buffer_open_file::read(std::size_t read_size, auto res = api_error::success; for (std::size_t chunk = start_chunk_index; - (res == api_error::success) && (read_size > 0U); chunk++) { + (res == api_error::success) && (read_size > 0U); ++chunk) { if (chunk > current_chunk_) { forward(chunk - current_chunk_); } else if (chunk < current_chunk_) { @@ -270,11 +269,11 @@ auto file_manager::ring_buffer_open_file::read(std::size_t read_size, res = do_io([this, &buffer, &chunk, &data, read_offset, &to_read]() -> api_error { std::size_t bytes_read{}; - auto ret = nf_->read_bytes(buffer.data(), buffer.size(), - ((chunk % ring_state_.size()) * chunk_size_), - bytes_read) - ? api_error::success - : api_error::os_error; + auto ret = + nf_->read(buffer, ((chunk % ring_state_.size()) * chunk_size_), + &bytes_read) + ? api_error::success + : api_error::os_error; if (ret == api_error::success) { data.insert(data.end(), buffer.begin() + static_cast(read_offset), diff --git a/repertory/librepertory/src/initialize.cpp b/repertory/librepertory/src/initialize.cpp index 3e5b01dd..2a0d9211 100644 --- a/repertory/librepertory/src/initialize.cpp +++ b/repertory/librepertory/src/initialize.cpp @@ -29,8 +29,8 @@ #if defined(PROJECT_REQUIRE_ALPINE) && !defined(PROJECT_IS_MINGW) #include -#include #include +#include #endif // defined(PROJECT_REQUIRE_ALPINE) && !defined (PROJECT_IS_MINGW) #if defined(PROJECT_ENABLE_LIBSODIUM) @@ -44,6 +44,9 @@ #include "spdlog/spdlog.h" #include "initialize.hpp" +#if defined(PROJECT_REQUIRE_ALPINE) && !defined(PROJECT_IS_MINGW) +#include "utils/path.hpp" +#endif // defined(PROJECT_REQUIRE_ALPINE) && !defined (PROJECT_IS_MINGW) namespace repertory { auto project_initialize() -> bool { @@ -61,11 +64,7 @@ auto project_initialize() -> bool { pthread_attr_setguardsize(&attr, guard_size); pthread_setattr_default_np(&attr); - const auto icu_dir = - std::filesystem::absolute(std::filesystem::path{"./icu"}) - .lexically_normal() - .string(); - setenv("ICU_DATA", icu_dir.c_str(), 1); + setenv("ICU_DATA", utils::path::combine(".", {"/icu"}).c_str(), 1); } #endif // defined(PROJECT_REQUIRE_ALPINE) && !defined (PROJECT_IS_MINGW) diff --git a/repertory/librepertory/src/platform/unix_platform.cpp b/repertory/librepertory/src/platform/unix_platform.cpp index 6f411f8b..1bd07b48 100644 --- a/repertory/librepertory/src/platform/unix_platform.cpp +++ b/repertory/librepertory/src/platform/unix_platform.cpp @@ -57,7 +57,7 @@ lock_data::~lock_data() { auto lock_data::get_lock_data_file() -> std::string { const auto dir = get_state_directory(); - if (not utils::file::create_full_directory_path(dir)) { + if (not utils::file::directory(dir).create_directory()) { throw startup_exception("failed to create directory|sp|" + dir + "|err|" + std::to_string(utils::get_last_error_code())); } @@ -67,7 +67,7 @@ auto lock_data::get_lock_data_file() -> std::string { auto lock_data::get_lock_file() -> std::string { const auto dir = get_state_directory(); - if (not utils::file::create_full_directory_path(dir)) { + if (not utils::file::directory(dir).create_directory()) { throw startup_exception("failed to create directory|sp|" + dir + "|err|" + std::to_string(utils::get_last_error_code())); } @@ -184,8 +184,11 @@ auto lock_data::wait_for_lock(int fd, std::uint8_t retry_count) -> int { if (lock_status == -1) { lock_status = errno; if (lock_status == EWOULDBLOCK) { - auto sleep_ms = - utils::generate_random_between(1U, std::min(remain, max_sleep)); + auto sleep_ms = std::min(remain, max_sleep); + if (sleep_ms > 1U) { + sleep_ms = utils::generate_random_between(1U, sleep_ms); + } + std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms)); remain -= sleep_ms; } diff --git a/repertory/librepertory/src/providers/base_provider.cpp b/repertory/librepertory/src/providers/base_provider.cpp index 31b348ff..3d2877de 100644 --- a/repertory/librepertory/src/providers/base_provider.cpp +++ b/repertory/librepertory/src/providers/base_provider.cpp @@ -37,10 +37,10 @@ auto base_provider::create_api_file(std::string path, std::string key, api_file file{}; file.api_path = utils::path::create_api_path(path); file.api_parent = utils::path::get_parent_api_path(file.api_path); - file.accessed_date = utils::time::get_file_time_now(); - file.changed_date = utils::time::get_file_time_now(); - file.creation_date = utils::time::get_file_time_now(); - file.modified_date = utils::time::get_file_time_now(); + file.accessed_date = utils::time::get_time_now(); + file.changed_date = utils::time::get_time_now(); + file.creation_date = utils::time::get_time_now(); + file.modified_date = utils::time::get_time_now(); file.key = key; file.file_size = size; return file; @@ -455,7 +455,7 @@ void base_provider::remove_deleted_files() { std::vector removed_list{}; - for (const auto &api_path : db3_->get_api_path_list()) { + for (auto &&api_path : db3_->get_api_path_list()) { api_meta_map meta{}; if (get_item_meta(api_path, meta) == api_error::success) { if (utils::string::to_bool(meta[META_DIRECTORY])) { @@ -480,12 +480,12 @@ void base_provider::remove_deleted_files() { } } - for (const auto &item : removed_list) { + for (auto &&item : removed_list) { if (not item.directory) { - if (utils::file::is_file(item.source_path)) { + if (utils::file::file(item.source_path).exists()) { const auto orphaned_directory = utils::path::combine(config_.get_data_directory(), {"orphaned"}); - if (utils::file::create_full_directory_path(orphaned_directory)) { + if (utils::file::directory(orphaned_directory).create_directory()) { const auto parts = utils::string::split(item.api_path, '/', false); const auto orphaned_file = utils::path::combine( orphaned_directory, @@ -495,7 +495,8 @@ void base_provider::remove_deleted_files() { event_system::instance().raise( item.source_path); if (utils::file::reset_modified_time(item.source_path) && - utils::file::copy_file(item.source_path, orphaned_file)) { + utils::file::file(item.source_path) + .copy_to(orphaned_file, true)) { event_system::instance().raise( item.source_path, orphaned_file); } else { @@ -506,7 +507,7 @@ void base_provider::remove_deleted_files() { } else { utils::error::raise_error( function_name, std::to_string(utils::get_last_error_code()), - "failed to create orphaned director|sp|" + orphaned_directory); + "failed to create orphaned directory|sp|" + orphaned_directory); continue; } } @@ -519,7 +520,7 @@ void base_provider::remove_deleted_files() { } } - for (const auto &item : removed_list) { + for (auto &&item : removed_list) { if (item.directory) { db3_->remove_api_path(item.api_path); event_system::instance().raise( @@ -640,9 +641,9 @@ auto base_provider::start(api_item_added_callback api_item_added, repertory::event_consumer consumer( "unmount_requested", [&unmount_requested](const event &) { unmount_requested = true; }); - for (std::uint16_t i = 0U; not online && not unmount_requested && - (i < config_.get_online_check_retry_secs()); - i++) { + for (std::uint16_t idx = 0U; not online && not unmount_requested && + (idx < config_.get_online_check_retry_secs()); + ++idx) { online = is_online(); if (not online) { event_system::instance().raise( diff --git a/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp b/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp index 07e33b91..6990b973 100644 --- a/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp +++ b/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp @@ -28,12 +28,11 @@ #include "events/events.hpp" #include "types/repertory.hpp" #include "utils/collection.hpp" -#include "utils/encrypt.hpp" #include "utils/encrypting_reader.hpp" +#include "utils/encryption.hpp" #include "utils/file_utils.hpp" #include "utils/path.hpp" #include "utils/polling.hpp" -#include "utils/time.hpp" namespace { const std::string directory_table = "directory"; @@ -72,61 +71,24 @@ encrypt_provider::encrypt_provider(app_config &config) : config_(config) {} auto encrypt_provider::create_api_file( const std::string &api_path, bool directory, const std::string &source_path) -> api_file { -#if defined(_WIN32) - struct _stat64 buf {}; - _stat64(source_path.c_str(), &buf); -#else - struct stat buf {}; - stat(source_path.c_str(), &buf); -#endif + auto times = utils::file::get_times(source_path); + if (not times.has_value()) { + throw std::runtime_error("failed to get file times"); + } api_file file{}; + file.accessed_date = times->get(utils::file::time_type::accessed); file.api_path = api_path; file.api_parent = utils::path::get_parent_api_path(api_path); + file.changed_date = times->get(utils::file::time_type::modified); + file.creation_date = times->get(utils::file::time_type::created); file.file_size = directory ? 0U : utils::encryption::encrypting_reader::calculate_encrypted_size( source_path); + file.modified_date = times->get(utils::file::time_type::written); file.source_path = source_path; -#if defined(__APPLE__) - file.changed_date = - buf.st_ctimespec.tv_nsec + (buf.st_ctimespec.tv_sec * NANOS_PER_SECOND); - file.accessed_date = - buf.st_atimespec.tv_nsec + (buf.st_atimespec.tv_sec * NANOS_PER_SECOND); - file.creation_date = buf.st_birthtimespec.tv_nsec + - (buf.st_birthtimespec.tv_sec * NANOS_PER_SECOND); - file.modified_date = - buf.st_mtimespec.tv_nsec + (buf.st_mtimespec.tv_sec * NANOS_PER_SECOND); -#elif defined(_WIN32) - auto ft = utils::time::unix_time_to_filetime( - utils::time::time64_to_unix_time(buf.st_atime)); - file.accessed_date = - (static_cast(ft.dwHighDateTime) << 32U) | ft.dwLowDateTime; - - ft = utils::time::unix_time_to_filetime( - utils::time::time64_to_unix_time(buf.st_mtime)); - file.changed_date = - (static_cast(ft.dwHighDateTime) << 32U) | ft.dwLowDateTime; - - ft = utils::time::unix_time_to_filetime( - utils::time::time64_to_unix_time(buf.st_ctime)); - file.creation_date = - (static_cast(ft.dwHighDateTime) << 32U) | ft.dwLowDateTime; - - ft = utils::time::unix_time_to_filetime(utils::time::time64_to_unix_time(buf.st_mtime)); - file.modified_date = - (static_cast(ft.dwHighDateTime) << 32U) | ft.dwLowDateTime; -#else - file.changed_date = static_cast( - buf.st_mtim.tv_nsec + (buf.st_mtim.tv_sec * NANOS_PER_SECOND)); - file.accessed_date = static_cast( - buf.st_atim.tv_nsec + (buf.st_atim.tv_sec * NANOS_PER_SECOND)); - file.creation_date = static_cast( - buf.st_ctim.tv_nsec + (buf.st_ctim.tv_sec * NANOS_PER_SECOND)); - file.modified_date = static_cast( - buf.st_mtim.tv_nsec + (buf.st_mtim.tv_sec * NANOS_PER_SECOND)); -#endif return file; } @@ -136,19 +98,21 @@ void encrypt_provider::create_item_meta(api_meta_map &meta, bool directory, #if defined(_WIN32) struct _stat64 buf {}; _stat64(file.source_path.c_str(), &buf); -#else +#else // !defined(_WIN32) struct stat buf {}; stat(file.source_path.c_str(), &buf); -#endif +#endif // defined(_WIN32) meta[META_ACCESSED] = std::to_string(file.accessed_date); #if defined(_WIN32) meta[META_ATTRIBUTES] = - std::to_string(::GetFileAttributesA(file.source_path.c_str())); -#endif + std::to_string(::GetFileAttributesA(file.source_path.c_str()) & + ~static_cast(FILE_ATTRIBUTE_REPARSE_POINT)); + +#endif // defined(_WIN32) #if defined(__APPLE__) meta[META_BACKUP]; -#endif +#endif // defined(__APPLE__) meta[META_CHANGED] = std::to_string(file.changed_date); meta[META_CREATION] = std::to_string(file.creation_date); meta[META_DIRECTORY] = utils::string::from_bool(directory); @@ -157,7 +121,7 @@ void encrypt_provider::create_item_meta(api_meta_map &meta, bool directory, meta[META_MODIFIED] = std::to_string(file.modified_date); #if defined(__APPLE__) meta[META_OSXFLAGS]; -#endif +#endif // defined(__APPLE__) meta[META_SIZE] = std::to_string(file.file_size); meta[META_SOURCE] = file.source_path; meta[META_UID] = std::to_string(buf.st_uid); @@ -180,13 +144,10 @@ auto encrypt_provider::do_fs_operation( callback) const -> api_error { auto cfg = config_.get_encrypt_config(); std::string source_path{api_path}; - if (api_path != "/") { - auto res = - utils::encryption::decrypt_file_path(cfg.encryption_token, source_path); - if (res != api_error::success) { - return directory ? api_error::directory_not_found - : api_error::item_not_found; - } + if (api_path != "/" && not utils::encryption::decrypt_file_path( + cfg.encryption_token, source_path)) { + return directory ? api_error::directory_not_found + : api_error::item_not_found; } source_path = utils::path::combine(cfg.path, {source_path}); @@ -197,7 +158,11 @@ auto encrypt_provider::do_fs_operation( : api_error::item_not_found; } - auto exists = utils::file::is_file(source_path); + auto exists = + utils::file::file{ + source_path, + } + .exists(); if (exists && directory) { return api_error::item_exists; } @@ -205,7 +170,11 @@ auto encrypt_provider::do_fs_operation( return api_error::item_not_found; } - exists = utils::file::is_directory(source_path); + exists = + utils::file::directory{ + source_path, + } + .exists(); if (exists && not directory) { return api_error::item_exists; } @@ -271,10 +240,7 @@ auto encrypt_provider::get_directory_item_count( [&api_path, &count](const encrypt_config & /* cfg */, const std::string &source_path) -> api_error { try { - for ([[maybe_unused]] const auto &dir_entry : - std::filesystem::directory_iterator(source_path)) { - count++; - } + count = utils::file::directory{source_path}.count(); } catch (const std::exception &ex) { utils::error::raise_api_path_error( function_name, api_path, source_path, ex, @@ -301,15 +267,15 @@ auto encrypt_provider::get_directory_items( [this, &list](const encrypt_config &cfg, const std::string &source_path) -> api_error { try { - for (const auto &dir_entry : - std::filesystem::directory_iterator(source_path)) { + for (auto &&dir_entry : + utils::file::directory{source_path}.get_items()) { try { std::string current_api_path{}; - if (dir_entry.is_directory()) { + if (dir_entry->is_directory_item()) { auto result = db::db_select{*db_, directory_table} .column("api_path") .where("source_path") - .equals(dir_entry.path().string()) + .equals(dir_entry->get_path()) .go(); std::optional row; if (result.get_row(row) && row.has_value()) { @@ -317,12 +283,13 @@ auto encrypt_provider::get_directory_items( row->get_column("api_path").get_value(); } if (current_api_path.empty()) { - process_directory_entry(dir_entry, cfg, current_api_path); + process_directory_entry(*dir_entry.get(), cfg, + current_api_path); result = db::db_select{*db_, directory_table} .column("api_path") .where("source_path") - .equals(dir_entry.path().string()) + .equals(dir_entry->get_path()) .go(); row.reset(); if (not(result.get_row(row) && row.has_value())) { @@ -337,7 +304,7 @@ auto encrypt_provider::get_directory_items( auto result = db::db_select{*db_, file_table} .column("data") .where("source_path") - .equals(dir_entry.path().string()) + .equals(dir_entry->get_path()) .go(); std::optional row; if (result.get_row(row) && row.has_value()) { @@ -346,7 +313,7 @@ auto encrypt_provider::get_directory_items( } if (api_path_data.empty()) { - if (not process_directory_entry(dir_entry, cfg, + if (not process_directory_entry(*dir_entry.get(), cfg, current_api_path)) { continue; } @@ -357,14 +324,14 @@ auto encrypt_provider::get_directory_items( } } - auto file = - create_api_file(current_api_path, dir_entry.is_directory(), - dir_entry.path().string()); + auto file = create_api_file(current_api_path, + dir_entry->is_directory_item(), + dir_entry->get_path()); directory_item dir_item{}; dir_item.api_parent = file.api_parent; dir_item.api_path = file.api_path; - dir_item.directory = dir_entry.is_directory(); + dir_item.directory = dir_entry->is_directory_item(); dir_item.resolved = true; dir_item.size = file.file_size; create_item_meta(dir_item.meta, dir_item.directory, file); @@ -372,7 +339,7 @@ auto encrypt_provider::get_directory_items( list.emplace_back(std::move(dir_item)); } catch (const std::exception &ex) { utils::error::raise_error(function_name, ex, - dir_entry.path().string(), + dir_entry->get_path(), "failed to process directory item"); } } @@ -405,28 +372,40 @@ auto encrypt_provider::get_directory_items( auto encrypt_provider::get_file(const std::string &api_path, api_file &file) const -> api_error { - bool exists{}; - auto res = is_directory(api_path, exists); - if (res != api_error::success) { - return res; - } - if (exists) { - return api_error::directory_exists; + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + bool exists{}; + auto res = is_directory(api_path, exists); + if (res != api_error::success) { + return res; + } + if (exists) { + return api_error::directory_exists; + } + + auto result = db::db_select{*db_, source_table} + .column("source_path") + .where("api_path") + .equals(api_path) + .go(); + std::optional row; + if (not(result.get_row(row) && row.has_value())) { + return api_error::item_not_found; + } + + file = create_api_file( + api_path, false, + row->get_column("source_path").get_value()); + return api_error::success; + } catch (const std::exception &ex) { + utils::error::raise_error(function_name, ex, api_path, + "failed to get file"); } - auto result = db::db_select{*db_, source_table} - .column("source_path") - .where("api_path") - .equals(api_path) - .go(); - std::optional row; - if (not(result.get_row(row) && row.has_value())) { - return api_error::item_not_found; - } - - file = create_api_file( - api_path, false, row->get_column("source_path").get_value()); - return api_error::success; + return api_error::error; } auto encrypt_provider::get_file_list(api_file_list &list) const -> api_error { @@ -435,14 +414,15 @@ auto encrypt_provider::get_file_list(api_file_list &list) const -> api_error { }; const auto cfg = config_.get_encrypt_config(); + event_system::instance().raise(std::string{function_name}, + cfg.path, ""); try { - for (const auto &dir_entry : - std::filesystem::recursive_directory_iterator(cfg.path)) { + for (auto &&dir_entry : utils::file::directory{cfg.path}.get_items()) { std::string api_path{}; - if (process_directory_entry(dir_entry, cfg, api_path)) { - list.emplace_back(create_api_file(api_path, dir_entry.is_directory(), - dir_entry.path().string())); + if (process_directory_entry(*dir_entry.get(), cfg, api_path)) { + list.emplace_back(create_api_file( + api_path, dir_entry->is_directory_item(), dir_entry->get_path())); } } @@ -565,22 +545,33 @@ auto encrypt_provider::get_filesystem_item_from_source_path( auto encrypt_provider::get_filesystem_item_and_file( const std::string &api_path, api_file &file, filesystem_item &fsi) const -> api_error { - bool exists{}; - auto res = is_directory(api_path, exists); - if (res != api_error::success) { - return res; - } - if (exists) { - return api_error::directory_exists; + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + bool exists{}; + auto res = is_directory(api_path, exists); + if (res != api_error::success) { + return res; + } + if (exists) { + return api_error::directory_exists; + } + + auto ret = get_filesystem_item(api_path, exists, fsi); + if (ret != api_error::success) { + return ret; + } + + file = create_api_file(api_path, false, fsi.source_path); + return api_error::success; + } catch (const std::exception &ex) { + utils::error::raise_error(function_name, ex, api_path, + "failed to get filesystem_item and file"); } - auto ret = get_filesystem_item(api_path, exists, fsi); - if (ret != api_error::success) { - return ret; - } - - file = create_api_file(api_path, false, fsi.source_path); - return api_error::success; + return api_error::error; } auto encrypt_provider::get_pinned_files() const -> std::vector { @@ -589,27 +580,38 @@ auto encrypt_provider::get_pinned_files() const -> std::vector { auto encrypt_provider::get_item_meta(const std::string &api_path, api_meta_map &meta) const -> api_error { - auto result = db::db_select{*db_, source_table} - .column("source_path") - .where("api_path") - .equals(api_path) - .go(); - std::optional row; - if (not(result.get_row(row) && row.has_value())) { - return api_error::item_not_found; + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + auto result = db::db_select{*db_, source_table} + .column("source_path") + .where("api_path") + .equals(api_path) + .go(); + std::optional row; + if (not(result.get_row(row) && row.has_value())) { + return api_error::item_not_found; + } + + auto source_path = row->get_column("source_path").get_value(); + + bool exists{}; + auto res = is_directory(api_path, exists); + if (res != api_error::success) { + return res; + } + + auto file = create_api_file(api_path, exists, source_path); + create_item_meta(meta, exists, file); + return api_error::success; + } catch (const std::exception &ex) { + utils::error::raise_error(function_name, ex, api_path, + "failed to get item meta"); } - auto source_path = row->get_column("source_path").get_value(); - - bool exists{}; - auto res = is_directory(api_path, exists); - if (res != api_error::success) { - return res; - } - - auto file = create_api_file(api_path, exists, source_path); - create_item_meta(meta, exists, file); - return api_error::success; + return api_error::error; } auto encrypt_provider::get_item_meta(const std::string &api_path, @@ -626,8 +628,9 @@ auto encrypt_provider::get_item_meta(const std::string &api_path, } auto encrypt_provider::get_total_drive_space() const -> std::uint64_t { - const auto cfg = config_.get_encrypt_config(); - return utils::file::get_total_drive_space(cfg.path); + auto total_space = + utils::file::get_total_drive_space(config_.get_encrypt_config().path); + return total_space.value_or(0U); } auto encrypt_provider::get_total_item_count() const -> std::uint64_t { @@ -644,8 +647,10 @@ auto encrypt_provider::get_total_item_count() const -> std::uint64_t { } auto encrypt_provider::get_used_drive_space() const -> std::uint64_t { - const auto cfg = config_.get_encrypt_config(); - return get_total_drive_space() - utils::file::get_free_drive_space(cfg.path); + auto free_space = + utils::file::get_free_drive_space(config_.get_encrypt_config().path); + return free_space.has_value() ? get_total_drive_space() - free_space.value() + : 0U; } auto encrypt_provider::is_directory(const std::string &api_path, @@ -661,8 +666,11 @@ auto encrypt_provider::is_directory(const std::string &api_path, return api_error::success; } - exists = utils::file::is_directory( - row->get_column("source_path").get_value()); + exists = + utils::file::directory{ + row->get_column("source_path").get_value(), + } + .exists(); return api_error::success; } @@ -679,8 +687,11 @@ auto encrypt_provider::is_file(const std::string &api_path, return api_error::success; } - exists = utils::file::is_file( - row->get_column("source_path").get_value()); + exists = + utils::file::file{ + row->get_column("source_path").get_value(), + } + .exists(); return api_error::success; } @@ -690,20 +701,24 @@ auto encrypt_provider::is_file_writeable(const std::string & /*api_path*/) const } auto encrypt_provider::is_online() const -> bool { - return std::filesystem::exists(config_.get_encrypt_config().path); + return utils::file::directory{ + config_.get_encrypt_config().path, + } + .exists(); } auto encrypt_provider::is_rename_supported() const -> bool { return false; } auto encrypt_provider::process_directory_entry( - const std::filesystem::directory_entry &dir_entry, - const encrypt_config &cfg, std::string &api_path) const -> bool { - const auto add_directory = [this, &cfg](auto dir_path) -> std::string { + const utils::file::i_fs_item &dir_entry, const encrypt_config &cfg, + std::string &api_path) const -> bool { + const auto add_directory = [this, + &cfg](std::string_view dir_path) -> std::string { auto encrypted_parts = utils::string::split( - utils::path::create_api_path(dir_path.string()), '/', false); + utils::path::create_api_path(dir_path), '/', false); for (std::size_t part_idx = 1U; part_idx < encrypted_parts.size(); - part_idx++) { + ++part_idx) { data_buffer encrypted_data; utils::encryption::encrypt_data( cfg.encryption_token, @@ -718,14 +733,9 @@ auto encrypt_provider::process_directory_entry( std::size_t current_idx{1U}; std::string current_encrypted_path{}; - std::string current_source_path{cfg.path}; - for (const auto &part : dir_path) { - if (part.string() == "/") { - continue; - } - - current_source_path = - utils::path::combine(current_source_path, {part.string()}); + auto current_source_path{cfg.path}; + for (auto &&part : utils::path::get_parts(dir_path)) { + current_source_path = utils::path::combine(current_source_path, {part}); std::string current_api_path{}; auto result = db::db_select{*db_, directory_table} @@ -767,19 +777,21 @@ auto encrypt_provider::process_directory_entry( return current_encrypted_path; }; - if (dir_entry.is_directory()) { - api_path = add_directory(dir_entry.path().lexically_relative(cfg.path)); + if (dir_entry.is_directory_item()) { + api_path = add_directory( + utils::path::get_relative_path(dir_entry.get_path(), cfg.path)); return false; } - if (dir_entry.is_regular_file() && not dir_entry.is_symlink()) { - const auto relative_path = dir_entry.path().lexically_relative(cfg.path); + if (dir_entry.is_file_item() && not dir_entry.is_symlink()) { + auto relative_path = + utils::path::get_relative_path(dir_entry.get_path(), cfg.path); std::string api_path_data{}; auto result = db::db_select{*db_, file_table} .column("data") .where("source_path") - .equals(dir_entry.path().string()) + .equals(dir_entry.get_path()) .go(); std::optional row; if (result.get_row(row) && row.has_value()) { @@ -790,7 +802,7 @@ auto encrypt_provider::process_directory_entry( result = db::db_select{*db_, directory_table} .column("api_path") .where("source_path") - .equals(dir_entry.path().parent_path().string()) + .equals(utils::path::get_parent_path(dir_entry.get_path())) .go(); row.reset(); if (result.get_row(row) && row.has_value()) { @@ -798,15 +810,15 @@ auto encrypt_provider::process_directory_entry( } if (api_parent.empty()) { - api_parent = add_directory(relative_path.parent_path()); + api_parent = add_directory(utils::path::get_parent_path(relative_path)); } if (api_path_data.empty()) { stop_type stop_requested = false; utils::encryption::encrypting_reader reader( - relative_path.filename().string(), dir_entry.path().string(), + utils::path::strip_to_file_name(relative_path), dir_entry.get_path(), stop_requested, cfg.encryption_token, - relative_path.parent_path().string()); + utils::path::get_parent_path(relative_path)); api_path = utils::path::create_api_path(api_parent + "/" + reader.get_encrypted_file_name()); @@ -814,17 +826,22 @@ auto encrypt_provider::process_directory_entry( json data = { {"api_path", api_path}, {"iv_list", iv_list}, - {"original_file_size", dir_entry.file_size()}, + { + "original_file_size", + (dynamic_cast(&dir_entry) + ->size() + .value_or(0U)), + }, }; auto ins_res = db::db_insert{*db_, file_table} - .column_value("source_path", dir_entry.path().string()) + .column_value("source_path", dir_entry.get_path()) .column_value("data", data.dump()) .go(); // TODO handle error ins_res = db::db_insert{*db_, source_table} .column_value("api_path", api_path) - .column_value("source_path", dir_entry.path().string()) + .column_value("source_path", dir_entry.get_path()) .go(); // TODO handle error event_system::instance().raise(api_path, @@ -874,11 +891,13 @@ auto encrypt_provider::read_file_bytes(const std::string &api_path, auto file_data = row->get_column("data").get_value_as_json(); - std::uint64_t file_size{}; - if (not utils::file::get_file_size(source_path, file_size)) { + auto opt_size = utils::file::file{source_path}.size(); + if (not opt_size.has_value()) { return api_error::os_error; } + auto file_size{opt_size.value()}; + std::vector< std::array> iv_list{}; @@ -888,13 +907,12 @@ auto encrypt_provider::read_file_bytes(const std::string &api_path, unique_recur_mutex_lock reader_lookup_lock(reader_lookup_mtx_); if (file_data.at("original_file_size").get() != file_size) { - const auto relative_path = - std::filesystem::path(source_path).lexically_relative(cfg.path); + auto relative_path = utils::path::get_relative_path(source_path, cfg.path); auto info = std::make_shared(); info->reader = std::make_unique( - relative_path.filename().string(), source_path, stop_requested, - cfg.encryption_token, relative_path.parent_path().string()); + relative_path, source_path, stop_requested, cfg.encryption_token, + utils::path::get_parent_path(relative_path)); reader_lookup_[source_path] = info; iv_list = info->reader->get_iv_list(); @@ -965,9 +983,9 @@ void encrypt_provider::remove_deleted_files() { } } - for (const auto &row : row_list) { + for (auto &&row : row_list) { auto source_path = row.get_column("source_path").get_value(); - if (not std::filesystem::exists(source_path)) { + if (not utils::path::exists(source_path)) { auto api_path = row.get_column("api_path").get_value(); result = db::db_select{*db_, file_table} .column("source_path") @@ -979,7 +997,7 @@ void encrypt_provider::remove_deleted_files() { } } - for (const auto &item : removed_list) { + for (auto &&item : removed_list) { if (not item.directory) { auto del_res = db::db_select{*db_, source_table} .delete_query() @@ -998,7 +1016,7 @@ void encrypt_provider::remove_deleted_files() { } } - for (const auto &item : removed_list) { + for (auto &&item : removed_list) { if (item.directory) { auto del_res = db::db_select{*db_, source_table} .delete_query() @@ -1043,7 +1061,7 @@ auto encrypt_provider::start(api_item_added_callback /*api_item_added*/, } db_.reset(db3); - for (const auto &create : sql_create_tables) { + for (auto &&create : sql_create_tables) { std::string err; if (not db::execute_sql(*db_, create.second, err)) { utils::error::raise_error(function_name, "failed to create table|" + diff --git a/repertory/librepertory/src/providers/meta_db.cpp b/repertory/librepertory/src/providers/meta_db.cpp index 2a3396e1..cfdd22df 100644 --- a/repertory/librepertory/src/providers/meta_db.cpp +++ b/repertory/librepertory/src/providers/meta_db.cpp @@ -288,7 +288,7 @@ auto meta_db::set_item_meta(const std::string &api_path, // TODO handle error } - for (const auto &item : meta) { + for (auto &&item : meta) { existing_meta[item.first] = item.second; } diff --git a/repertory/librepertory/src/providers/s3/s3_provider.cpp b/repertory/librepertory/src/providers/s3/s3_provider.cpp index 7240e099..b91dcbd7 100644 --- a/repertory/librepertory/src/providers/s3/s3_provider.cpp +++ b/repertory/librepertory/src/providers/s3/s3_provider.cpp @@ -27,12 +27,11 @@ #include "file_manager/i_file_manager.hpp" #include "types/repertory.hpp" #include "types/s3.hpp" -#include "types/startup_exception.hpp" #include "utils/collection.hpp" -#include "utils/encrypt.hpp" #include "utils/encrypting_reader.hpp" +#include "utils/encryption.hpp" #include "utils/error_utils.hpp" -#include "utils/file_utils.hpp" +#include "utils/file.hpp" #include "utils/path.hpp" #include "utils/polling.hpp" #include "utils/string.hpp" @@ -60,6 +59,24 @@ auto s3_provider::add_if_not_found( return api_error::success; } +auto s3_provider::convert_api_date(std::string_view date) -> std::uint64_t { + // 2009-10-12T17:50:30.000Z + auto date_parts = utils::string::split(date, '.', true); + auto date_time = date_parts.at(0U); + auto nanos = utils::string::to_uint64( + utils::string::split(date_parts.at(1U), 'Z', true).at(0U)); + + struct tm tm1 {}; +#if defined(_WIN32) + utils::time::strptime(date_time.c_str(), "%Y-%m-%dT%T", &tm1); +#else + strptime(date_time.c_str(), "%Y-%m-%dT%T", &tm1); +#endif + + return nanos + (static_cast(mktime(&tm1)) * + utils::time::NANOS_PER_SECOND); +} + auto s3_provider::create_directory_impl(const std::string &api_path, api_meta_map &meta) -> api_error { static constexpr const std::string_view function_name{ @@ -162,13 +179,13 @@ auto s3_provider::create_path_directories( std::string cur_key{'/'}; std::string cur_path{'/'}; - for (std::size_t i = 0U; i < path_parts.size(); i++) { + for (std::size_t idx = 0U; idx < path_parts.size(); ++idx) { if (is_encrypted) { cur_key = utils::path::create_api_path( - utils::path::combine(cur_key, {key_parts.at(i)})); + utils::path::combine(cur_key, {key_parts.at(idx)})); } cur_path = utils::path::create_api_path( - utils::path::combine(cur_path, {path_parts.at(i)})); + utils::path::combine(cur_path, {path_parts.at(idx)})); api_meta_map meta{}; auto res = get_item_meta(cur_path, meta); @@ -189,11 +206,10 @@ auto s3_provider::create_path_directories( auto s3_provider::decrypt_object_name(std::string &object_name) const -> api_error { auto parts = utils::string::split(object_name, '/', false); - for (auto &part : parts) { - auto err = utils::encryption::decrypt_file_name( - get_config().get_s3_config().encryption_token, part); - if (err != api_error::success) { - return err; + for (auto &&part : parts) { + if (not utils::encryption::decrypt_file_name( + get_config().get_s3_config().encryption_token, part)) { + return api_error::decryption_error; } } @@ -313,10 +329,9 @@ auto s3_provider::get_directory_items_impl( std::string child_object_name; if (is_encrypted) { child_object_name = child_api_path; - ret = utils::encryption::decrypt_file_path(cfg.encryption_token, - child_api_path); - if (ret != api_error::success) { - return ret; + if (not utils::encryption::decrypt_file_path(cfg.encryption_token, + child_api_path)) { + return api_error::decryption_error; } } @@ -353,14 +368,14 @@ auto s3_provider::get_directory_items_impl( }; auto node_list = doc.select_nodes("/ListBucketResult/CommonPrefixes/Prefix"); - for (const auto &node : node_list) { + for (auto &&node : node_list) { add_directory_item( true, node.node().text().as_string(), [](const directory_item &) -> std::uint64_t { return 0U; }); } node_list = doc.select_nodes("/ListBucketResult/Contents"); - for (const auto &node : node_list) { + for (auto &&node : node_list) { auto child_object_name = utils::path::create_api_path( node.node().select_node("Key").node().text().as_string()); if (child_object_name != utils::path::create_api_path(prefix)) { @@ -394,7 +409,7 @@ auto s3_provider::get_file(const std::string &api_path, return res; } - file.accessed_date = utils::time::get_file_time_now(); + file.accessed_date = utils::time::get_time_now(); file.api_path = api_path; file.api_parent = utils::path::get_parent_api_path(file.api_path); file.changed_date = utils::aws::format_time(result.last_modified); @@ -431,7 +446,7 @@ auto s3_provider::get_file_list(api_file_list &list) const -> api_error { } auto node_list = doc.select_nodes("/ListBucketResult/Contents"); - for (const auto &node : node_list) { + for (auto &&node : node_list) { auto api_path = std::string{node.node().select_node("Key").node().text().as_string()}; if (not utils::string::ends_with(api_path, "/")) { @@ -449,8 +464,8 @@ auto s3_provider::get_file_list(api_file_list &list) const -> api_error { api_file file{}; file.api_path = utils::path::create_api_path(api_path); file.api_parent = utils::path::get_parent_api_path(file.api_path); - file.accessed_date = utils::time::get_file_time_now(); - file.changed_date = utils::convert_api_date( + file.accessed_date = utils::time::get_time_now(); + file.changed_date = convert_api_date( node.node().select_node("LastModified").node().text().as_string()); file.creation_date = file.changed_date; file.file_size = @@ -674,10 +689,10 @@ auto s3_provider::read_file_bytes(const std::string &api_path, std::size_t size, &stop_requested](std::size_t read_size, std::size_t read_offset, data_buffer &read_buffer) -> api_error { auto res = api_error::error; - for (std::uint32_t i = 0U; + for (std::uint32_t idx = 0U; not stop_requested && res != api_error::success && - i < get_config().get_retry_read_count() + 1U; - i++) { + idx < get_config().get_retry_read_count() + 1U; + ++idx) { curl::requests::http_get get{}; get.aws_service = "aws:amz:" + cfg.region + ":s3"; get.headers["response-content-type"] = "binary/octet-stream"; @@ -698,13 +713,13 @@ auto s3_provider::read_file_bytes(const std::string &api_path, std::size_t size, function_name, api_path, api_error::comm_error, "read file bytes failed|offset|" + std::to_string(read_offset) + "|size|" + std::to_string(read_size) + "|retry|" + - std::to_string(i + 1U)); + std::to_string(idx + 1U)); } else { utils::error::raise_api_path_error( function_name, api_path, response_code, "read file bytes failed|offset|" + std::to_string(read_offset) + "|size|" + std::to_string(read_size) + "|retry|" + - std::to_string(i + 1U)); + std::to_string(idx + 1U)); } std::this_thread::sleep_for(1s); }; @@ -735,15 +750,18 @@ auto s3_provider::read_file_bytes(const std::string &api_path, std::size_t size, const auto total_size = utils::string::to_uint64(temp); return utils::encryption::read_encrypted_range( - {offset, offset + size - 1U}, - utils::encryption::generate_key( - cfg.encryption_token), - [&](data_buffer &ct_buffer, std::uint64_t start_offset, - std::uint64_t end_offset) -> api_error { - return read_bytes((end_offset - start_offset + 1U), start_offset, - ct_buffer); - }, - total_size, data); + {offset, offset + size - 1U}, + utils::encryption::generate_key( + cfg.encryption_token), + [&](data_buffer &ct_buffer, std::uint64_t start_offset, + std::uint64_t end_offset) -> bool { + return read_bytes((end_offset - start_offset + 1U), + start_offset, + ct_buffer) == api_error::success; + }, + total_size, data) + ? api_error::success + : api_error::decryption_error; } return read_bytes(size, offset, data); @@ -865,9 +883,12 @@ auto s3_provider::upload_file_impl(const std::string &api_path, const std::string &source_path, stop_type &stop_requested) -> api_error { std::uint64_t file_size{}; - if (utils::file::is_file(source_path) && - not utils::file::get_file_size(source_path, file_size)) { - return api_error::comm_error; + if (utils::file::file{source_path}.exists()) { + auto opt_size = utils::file::file{source_path}.size(); + if (not opt_size.has_value()) { + return api_error::comm_error; + } + file_size = opt_size.value(); } const auto cfg = get_config().get_s3_config(); diff --git a/repertory/librepertory/src/providers/sia/sia_provider.cpp b/repertory/librepertory/src/providers/sia/sia_provider.cpp index 4e0d05ff..ecf18538 100644 --- a/repertory/librepertory/src/providers/sia/sia_provider.cpp +++ b/repertory/librepertory/src/providers/sia/sia_provider.cpp @@ -80,7 +80,7 @@ auto sia_provider::get_directory_item_count(const std::string &api_path) const std::uint64_t item_count{}; if (object_list.contains("entries")) { - for (const auto &entry : object_list.at("entries")) { + for (auto &&entry : object_list.at("entries")) { try { auto name = entry.at("name").get(); auto entry_api_path = utils::path::create_api_path(name); @@ -118,7 +118,7 @@ auto sia_provider::get_directory_items_impl( } if (object_list.contains("entries")) { - for (const auto &entry : object_list.at("entries")) { + for (auto &&entry : object_list.at("entries")) { try { auto name = entry.at("name").get(); auto entry_api_path = utils::path::create_api_path(name); @@ -208,7 +208,7 @@ auto sia_provider::get_file_list(api_file_list &list) const -> api_error { } if (object_list.contains("entries")) { - for (const auto &entry : object_list.at("entries")) { + for (auto &&entry : object_list.at("entries")) { auto name = entry.at("name").get(); auto entry_api_path = utils::path::create_api_path(name); @@ -534,9 +534,10 @@ auto sia_provider::read_file_bytes(const std::string &api_path, long /*response_code*/) { buffer = data; }; auto res = api_error::comm_error; - for (std::uint32_t i = 0U; not stop_requested && res != api_error::success && - i < get_config().get_retry_read_count() + 1U; - i++) { + for (std::uint32_t idx = 0U; + not stop_requested && res != api_error::success && + idx < get_config().get_retry_read_count() + 1U; + ++idx) { long response_code{}; const auto notify_retry = [&]() { if (response_code == 0) { @@ -544,13 +545,13 @@ auto sia_provider::read_file_bytes(const std::string &api_path, function_name, api_path, api_error::comm_error, "read file bytes failed|offset|" + std::to_string(offset) + "|size|" + std::to_string(size) + "|retry|" + - std::to_string(i + 1U)); + std::to_string(idx + 1U)); } else { utils::error::raise_api_path_error( function_name, api_path, response_code, "read file bytes failed|offset|" + std::to_string(offset) + "|size|" + std::to_string(size) + "|retry|" + - std::to_string(i + 1U)); + std::to_string(idx + 1U)); } std::this_thread::sleep_for(1s); }; diff --git a/repertory/librepertory/src/rpc/server/full_server.cpp b/repertory/librepertory/src/rpc/server/full_server.cpp index 7e794ae9..e09d27b6 100644 --- a/repertory/librepertory/src/rpc/server/full_server.cpp +++ b/repertory/librepertory/src/rpc/server/full_server.cpp @@ -22,12 +22,11 @@ #include "rpc/server/full_server.hpp" #include "app_config.hpp" -#include "drives/directory_iterator.hpp" #include "file_manager/i_file_manager.hpp" #include "providers/i_provider.hpp" #include "types/repertory.hpp" #include "types/rpc.hpp" -#include "utils/file_utils.hpp" +#include "utils/file.hpp" #include "utils/path.hpp" namespace repertory { @@ -51,11 +50,11 @@ void full_server::handle_get_directory_items(const httplib::Request &req, void full_server::handle_get_drive_information(const httplib::Request & /*req*/, httplib::Response &res) { + auto dir_size = + utils::file::directory(get_config().get_cache_directory()).size(); res.set_content( json({ - {"cache_space_used", - utils::file::calculate_used_space( - get_config().get_cache_directory(), false)}, + {"cache_space_used", dir_size}, {"drive_space_total", provider_.get_total_drive_space()}, {"drive_space_used", provider_.get_used_drive_space()}, {"item_count", provider_.get_total_item_count()}, diff --git a/repertory/librepertory/src/utils/action_queue.cpp b/repertory/librepertory/src/utils/action_queue.cpp deleted file mode 100644 index 7cd9d76c..00000000 --- a/repertory/librepertory/src/utils/action_queue.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "utils/action_queue.hpp" - -#include "types/repertory.hpp" - -namespace repertory::utils::action_queue { -action_queue::action_queue(const std::string &id, - std::uint8_t max_concurrent_actions) - : single_thread_service_base("action_queue_" + id), - id_(id), - max_concurrent_actions_(max_concurrent_actions) {} - -void action_queue::service_function() { - // -} - -void action_queue::push(std::function action) { - unique_mutex_lock queue_lock(queue_mtx_); - queue_.emplace_back(action); - queue_notify_.notify_all(); -} -} // namespace repertory::utils::action_queue diff --git a/repertory/librepertory/src/utils/cli_utils.cpp b/repertory/librepertory/src/utils/cli_utils.cpp index 85418639..8538bf30 100644 --- a/repertory/librepertory/src/utils/cli_utils.cpp +++ b/repertory/librepertory/src/utils/cli_utils.cpp @@ -23,7 +23,8 @@ #include "app_config.hpp" #include "utils/collection.hpp" -#include "utils/file_utils.hpp" +#include "utils/common.hpp" +#include "utils/file.hpp" #include "utils/path.hpp" #include "utils/string.hpp" #include "utils/utils.hpp" @@ -38,7 +39,7 @@ void get_api_authentication_data(std::string &user, std::string &password, {"config.json"}); json data; - const auto success = utils::retryable_action([&]() -> bool { + const auto success = utils::retry_action([&]() -> bool { return utils::file::read_json_file(cfg_file_path, data); }); diff --git a/repertory/librepertory/src/utils/encrypt.cpp b/repertory/librepertory/src/utils/encrypt.cpp deleted file mode 100644 index b9982743..00000000 --- a/repertory/librepertory/src/utils/encrypt.cpp +++ /dev/null @@ -1,117 +0,0 @@ -/* - Copyright <2018-2024> - - 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. -*/ -#include "utils/encrypt.hpp" - -#include "events/event_system.hpp" -#include "events/events.hpp" -#include "types/repertory.hpp" -#include "utils/collection.hpp" -#include "utils/encrypting_reader.hpp" -#include "utils/encryption.hpp" -#include "utils/utils.hpp" - -namespace repertory::utils::encryption { -auto decrypt_file_path(std::string_view encryption_token, - std::string &file_path) -> api_error { - std::string decrypted_file_path{}; - for (const auto &part : std::filesystem::path(file_path)) { - auto file_name = part.string(); - if (file_name == "/") { - continue; - } - - auto res = decrypt_file_name(encryption_token, file_name); - if (res != api_error::success) { - return res; - } - - decrypted_file_path += '/' + file_name; - } - - file_path = decrypted_file_path; - return api_error::success; -} - -auto decrypt_file_name(std::string_view encryption_token, - std::string &file_name) -> api_error { - data_buffer buffer; - if (not utils::collection::from_hex_string(file_name, buffer)) { - return api_error::error; - } - - file_name.clear(); - if (not utils::encryption::decrypt_data(encryption_token, buffer, - file_name)) { - return api_error::decryption_error; - } - - return api_error::success; -} - -auto read_encrypted_range(const http_range &range, - const utils::encryption::hash_256_t &key, - reader_func reader, std::uint64_t total_size, - data_buffer &data) -> api_error { - const auto encrypted_chunk_size = - utils::encryption::encrypting_reader::get_encrypted_chunk_size(); - const auto data_chunk_size = - utils::encryption::encrypting_reader::get_data_chunk_size(); - - const auto start_chunk = - static_cast(range.begin / data_chunk_size); - const auto end_chunk = static_cast(range.end / data_chunk_size); - auto remain = range.end - range.begin + 1U; - auto source_offset = static_cast(range.begin % data_chunk_size); - - for (std::size_t chunk = start_chunk; chunk <= end_chunk; chunk++) { - data_buffer cypher; - const auto start_offset = chunk * encrypted_chunk_size; - const auto end_offset = std::min( - start_offset + (total_size - (chunk * data_chunk_size)) + - encryption_header_size - 1U, - static_cast(start_offset + encrypted_chunk_size - 1U)); - - const auto result = reader(cypher, start_offset, end_offset); - if (result != api_error::success) { - return result; - } - - data_buffer source_buffer; - if (not utils::encryption::decrypt_data(key, cypher, source_buffer)) { - return api_error::decryption_error; - } - cypher.clear(); - - const auto data_size = static_cast(std::min( - remain, static_cast(data_chunk_size - source_offset))); - std::copy(std::next(source_buffer.begin(), - static_cast(source_offset)), - std::next(source_buffer.begin(), - static_cast(source_offset + data_size)), - std::back_inserter(data)); - remain -= data_size; - source_offset = 0U; - } - - return api_error::success; -} -} // namespace repertory::utils::encryption diff --git a/repertory/librepertory/src/utils/file_utils.cpp b/repertory/librepertory/src/utils/file_utils.cpp index 5ef9e1d6..0d6eeaa2 100644 --- a/repertory/librepertory/src/utils/file_utils.cpp +++ b/repertory/librepertory/src/utils/file_utils.cpp @@ -21,392 +21,77 @@ */ #include "utils/file_utils.hpp" -#include "types/repertory.hpp" -#include "utils/collection.hpp" #include "utils/error_utils.hpp" +#include "utils/file.hpp" #include "utils/path.hpp" #include "utils/string.hpp" #include "utils/time.hpp" -#include "utils/utils.hpp" namespace repertory::utils::file { -auto calculate_used_space(std::string path, bool recursive) -> std::uint64_t { - path = utils::path::absolute(path); - std::uint64_t ret{}; -#if defined(_WIN32) - WIN32_FIND_DATA fd{}; - const auto search = utils::path::combine(path, {"*.*"}); - auto find = ::FindFirstFile(search.c_str(), &fd); - if (find != INVALID_HANDLE_VALUE) { - do { - const auto file_name = std::string(fd.cFileName); - if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { - if (recursive && (file_name != ".") && (file_name != "..")) { - ret += calculate_used_space(utils::path::combine(path, {file_name}), - recursive); - } - } else { - std::uint64_t file_size{}; - if (get_file_size(utils::path::combine(path, {file_name}), file_size)) { - ret += file_size; - } - } - } while (::FindNextFile(find, &fd) != 0); - ::FindClose(find); - } -#else - auto *root = opendir(path.c_str()); - if (root) { - struct dirent *de{}; - while ((de = readdir(root)) != nullptr) { - if (de->d_type == DT_DIR) { - if (recursive && (strcmp(de->d_name, ".") != 0) && - (strcmp(de->d_name, "..") != 0)) { - ret += calculate_used_space(utils::path::combine(path, {de->d_name}), - recursive); - } - } else { - std::uint64_t file_size{}; - if (get_file_size(utils::path::combine(path, {de->d_name}), - file_size)) { - ret += file_size; - } - } - } - closedir(root); - } -#endif - - return ret; -} - -void change_to_process_directory() { -#if defined(_WIN32) - std::string file_name; - file_name.resize(MAX_PATH); - ::GetModuleFileNameA(nullptr, &file_name[0U], - static_cast(file_name.size())); - - std::string path = file_name.c_str(); - ::PathRemoveFileSpecA(&path[0U]); - ::SetCurrentDirectoryA(&path[0U]); -#else - std::string path; - path.resize(PATH_MAX + 1); -#if defined(__APPLE__) - proc_pidpath(getpid(), &path[0U], path.size()); -#else - readlink("/proc/self/exe", &path[0U], path.size()); -#endif - path = utils::path::get_parent_directory(path); - chdir(path.c_str()); -#endif -} - -auto copy_file(std::string from_path, std::string to_path) -> bool { - from_path = utils::path::absolute(from_path); - to_path = utils::path::absolute(to_path); - if (is_file(from_path) && not is_directory(to_path)) { - return std::filesystem::copy_file(from_path, to_path); - } - - return false; -} - -auto copy_directory_recursively(std::string from_path, - std::string to_path) -> bool { - from_path = utils::path::absolute(from_path); - to_path = utils::path::absolute(to_path); - auto ret = create_full_directory_path(to_path); - if (ret) { -#if defined(_WIN32) - WIN32_FIND_DATA fd{}; - const auto search = utils::path::combine(from_path, {"*.*"}); - auto find = ::FindFirstFile(search.c_str(), &fd); - if (find != INVALID_HANDLE_VALUE) { - ret = true; - do { - if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { - if ((std::string(fd.cFileName) != ".") && - (std::string(fd.cFileName) != "..")) { - ret = copy_directory_recursively( - utils::path::combine(from_path, {fd.cFileName}), - utils::path::combine(to_path, {fd.cFileName})); - } - } else { - ret = copy_file(utils::path::combine(from_path, {fd.cFileName}), - utils::path::combine(to_path, {fd.cFileName})); - } - } while (ret && (::FindNextFile(find, &fd) != 0)); - - ::FindClose(find); - } -#else - auto *root = opendir(from_path.c_str()); - if (root) { - struct dirent *de{}; - while (ret && (de = readdir(root))) { - if (de->d_type == DT_DIR) { - if ((strcmp(de->d_name, ".") != 0) && - (strcmp(de->d_name, "..") != 0)) { - ret = copy_directory_recursively( - utils::path::combine(from_path, {de->d_name}), - utils::path::combine(to_path, {de->d_name})); - } - } else { - ret = copy_file(utils::path::combine(from_path, {de->d_name}), - utils::path::combine(to_path, {de->d_name})); - } - } - - closedir(root); - } -#endif - } - - return ret; -} - -auto create_full_directory_path(std::string path) -> bool { -#if defined(_WIN32) - const auto unicode_path = - utils::string::from_utf8(utils::path::absolute(path)); - return is_directory(path) || - (::SHCreateDirectory(nullptr, unicode_path.c_str()) == ERROR_SUCCESS); -#else - auto ret = true; - const auto paths = utils::string::split( - utils::path::absolute(path), utils::path::directory_seperator[0U], false); - std::string current_path; - for (std::size_t i = 0U; ret && (i < paths.size()); i++) { - if (paths[i].empty()) { // Skip root - current_path = utils::path::directory_seperator; - } else { - current_path = utils::path::combine(current_path, {paths[i]}); - const auto status = mkdir(current_path.c_str(), S_IRWXU); - ret = ((status == 0) || (errno == EEXIST)); - } - } - - return ret; -#endif -} - -auto delete_directory(std::string path, bool recursive) -> bool { - if (recursive) { - return delete_directory_recursively(path); - } - - path = utils::path::absolute(path); -#if defined(_WIN32) - return (not is_directory(path) || utils::retryable_action([&]() -> bool { - return !!::RemoveDirectoryA(path.c_str()); - })); -#else - return not is_directory(path) || (rmdir(path.c_str()) == 0); -#endif -} - -auto delete_directory_recursively(std::string path) -> bool { - path = utils::path::absolute(path); -#if defined(_WIN32) - - WIN32_FIND_DATA fd{}; - const auto search = utils::path::combine(path, {"*.*"}); - auto find = ::FindFirstFile(search.c_str(), &fd); - if (find != INVALID_HANDLE_VALUE) { - auto res = true; - do { - if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { - if ((std::string(fd.cFileName) != ".") && - (std::string(fd.cFileName) != "..")) { - res = delete_directory_recursively( - utils::path::combine(path, {fd.cFileName})); - } - } else { - res = retry_delete_file(utils::path::combine(path, {fd.cFileName})); - } - } while (res && (::FindNextFile(find, &fd) != 0)); - - ::FindClose(find); - } -#else - auto *root = opendir(path.c_str()); - if (root) { - auto res = true; - struct dirent *de{}; - while (res && (de = readdir(root))) { - if (de->d_type == DT_DIR) { - if ((strcmp(de->d_name, ".") != 0) && (strcmp(de->d_name, "..") != 0)) { - res = delete_directory_recursively( - utils::path::combine(path, {de->d_name})); - } - } else { - res = retry_delete_file(utils::path::combine(path, {de->d_name})); - } - } - - closedir(root); - } -#endif - - return delete_directory(path, false); -} - -auto delete_file(std::string path) -> bool { - path = utils::path::absolute(path); -#if defined(_WIN32) - return (not is_file(path) || utils::retryable_action([&]() -> bool { - const auto ret = !!::DeleteFileA(path.c_str()); - if (not ret) { - std::cout << "delete failed:" << path << std::endl; - } - - return ret; - })); -#else - return (not is_file(path) || (unlink(path.c_str()) == 0)); -#endif -} - -auto generate_sha256(const std::string &file_path) -> std::string { - crypto_hash_sha256_state state{}; - auto res = crypto_hash_sha256_init(&state); - if (res != 0) { - throw std::runtime_error("failed to initialize sha256|" + - std::to_string(res)); - } - - native_file_ptr nf; - if (native_file::open(file_path, nf) != api_error::success) { - throw std::runtime_error("failed to open file|" + file_path); - } - - { - data_buffer buffer(1048576u); - std::uint64_t read_offset = 0U; - std::size_t bytes_read = 0U; - while ( - nf->read_bytes(buffer.data(), buffer.size(), read_offset, bytes_read)) { - if (not bytes_read) { - break; - } - - read_offset += bytes_read; - res = crypto_hash_sha256_update( - &state, reinterpret_cast(buffer.data()), - bytes_read); - if (res != 0) { - nf->close(); - throw std::runtime_error("failed to update sha256|" + - std::to_string(res)); - } - } - nf->close(); - } - - std::array out{}; - res = crypto_hash_sha256_final(&state, out.data()); - if (res != 0) { - throw std::runtime_error("failed to finalize sha256|" + - std::to_string(res)); - } - - return utils::collection::to_hex_string(out); -} - -auto get_free_drive_space(const std::string &path) -> std::uint64_t { -#if defined(_WIN32) - ULARGE_INTEGER li{}; - ::GetDiskFreeSpaceEx(path.c_str(), &li, nullptr, nullptr); - return li.QuadPart; -#endif -#if defined(__linux__) - std::uint64_t ret = 0; - struct statfs64 st {}; - if (statfs64(path.c_str(), &st) == 0) { - ret = st.f_bfree * st.f_bsize; - } - return ret; -#endif -#if defined(__APPLE__) - struct statvfs st {}; - statvfs(path.c_str(), &st); - return st.f_bfree * st.f_frsize; -#endif -} - -auto get_total_drive_space(const std::string &path) -> std::uint64_t { -#if defined(_WIN32) - ULARGE_INTEGER li{}; - ::GetDiskFreeSpaceEx(path.c_str(), nullptr, &li, nullptr); - return li.QuadPart; -#endif -#if defined(__linux__) - std::uint64_t ret = 0; - struct statfs64 st {}; - if (statfs64(path.c_str(), &st) == 0) { - ret = st.f_blocks * st.f_bsize; - } - return ret; -#endif -#if defined(__APPLE__) - struct statvfs st {}; - statvfs(path.c_str(), &st); - return st.f_blocks * st.f_frsize; -#endif -} - -auto get_directory_files(std::string path, bool oldest_first, +auto get_directory_files(std::string_view path, bool oldest_first, bool recursive) -> std::deque { - path = utils::path::absolute(path); + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + auto abs_path = utils::path::absolute(path); std::deque ret; std::unordered_map lookup; #if defined(_WIN32) WIN32_FIND_DATA fd{}; - const auto search = utils::path::combine(path, {"*.*"}); + auto search = utils::path::combine(abs_path, {"*.*"}); auto find = ::FindFirstFile(search.c_str(), &fd); if (find != INVALID_HANDLE_VALUE) { - do { - const auto full_path = utils::path::combine(path, {fd.cFileName}); - if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == - FILE_ATTRIBUTE_DIRECTORY) { - if (recursive) { - const auto sub_files = - get_directory_files(full_path, oldest_first, recursive); - ret.insert(ret.end(), sub_files.begin(), sub_files.end()); - } else { - ULARGE_INTEGER li{}; - li.HighPart = fd.ftLastWriteTime.dwHighDateTime; - li.LowPart = fd.ftLastWriteTime.dwLowDateTime; - lookup[full_path] = li.QuadPart; - ret.emplace_back(full_path); + try { + do { + auto full_path = utils::path::combine(abs_path, {fd.cFileName}); + if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == + FILE_ATTRIBUTE_DIRECTORY) { + if (recursive) { + auto sub_files = + get_directory_files(full_path, oldest_first, recursive); + ret.insert(ret.end(), sub_files.begin(), sub_files.end()); + } else { + ULARGE_INTEGER li{}; + li.HighPart = fd.ftLastWriteTime.dwHighDateTime; + li.LowPart = fd.ftLastWriteTime.dwLowDateTime; + lookup[full_path] = li.QuadPart; + ret.emplace_back(full_path); + } } - } - } while (::FindNextFile(find, &fd) != 0); + } while (::FindNextFile(find, &fd) != 0); + } catch (const std::exception &e) { + utils::error::raise_error(function_name, e, + "failed to get directory files"); + } ::FindClose(find); - std::sort(ret.begin(), ret.end(), - [&](const auto &p1, const auto &p2) -> bool { - return (oldest_first != 0) == (lookup[p1] < lookup[p2]); - }); + std::sort(ret.begin(), ret.end(), [&](auto &&path1, auto &&path2) -> bool { + return (oldest_first != 0) == (lookup[path1] < lookup[path2]); + }); } -#else - auto *root = opendir(path.c_str()); +#else // !defined(_WIN32) + auto *root = opendir(abs_path.c_str()); if (root) { - struct dirent *de{}; - while ((de = readdir(root)) != nullptr) { - if (de->d_type == DT_DIR) { - if (recursive) { - const auto sub_files = - get_directory_files(utils::path::combine(path, {de->d_name}), - oldest_first, recursive); - ret.insert(ret.end(), sub_files.begin(), sub_files.end()); + try { + struct dirent *de{}; + while ((de = readdir(root)) != nullptr) { + if (de->d_type == DT_DIR) { + if (recursive) { + auto sub_files = get_directory_files( + utils::path::combine(abs_path, {de->d_name}), oldest_first, + recursive); + ret.insert(ret.end(), sub_files.begin(), sub_files.end()); + } + } else { + ret.emplace_back(utils::path::combine(abs_path, {de->d_name})); } - } else { - ret.emplace_back(utils::path::combine(path, {de->d_name})); } + } catch (const std::exception &e) { + utils::error::raise_error(function_name, e, + "failed to get directory files"); } + closedir(root); const auto add_to_lookup = [&](const std::string &lookup_path) { @@ -414,118 +99,32 @@ auto get_directory_files(std::string path, bool oldest_first, struct stat st {}; stat(lookup_path.c_str(), &st); #if defined(__APPLE__) - lookup[lookup_path] = static_cast( - (st.st_mtimespec.tv_sec * NANOS_PER_SECOND) + - st.st_mtimespec.tv_nsec); -#else - lookup[lookup_path] = static_cast( - (st.st_mtim.tv_sec * NANOS_PER_SECOND) + st.st_mtim.tv_nsec); -#endif + lookup[lookup_path] = + (static_cast(st.st_mtimespec.tv_sec) * + utils::time::NANOS_PER_SECOND) + + static_cast(st.st_mtimespec.tv_nsec); +#else // !defined(__APPLE__) + lookup[lookup_path] = (static_cast(st.st_mtim.tv_sec) * + utils::time::NANOS_PER_SECOND) + + static_cast(st.st_mtim.tv_nsec); +#endif // defined(__APPLE__) } }; - std::sort(ret.begin(), ret.end(), - [&](const auto &p1, const auto &p2) -> bool { - add_to_lookup(p1); - add_to_lookup(p2); - return (oldest_first != 0) == (lookup[p1] < lookup[p2]); - }); + std::sort(ret.begin(), ret.end(), [&](auto &&path1, auto &&path2) -> bool { + add_to_lookup(path1); + add_to_lookup(path2); + return (oldest_first != 0) == (lookup.at(path1) < lookup.at(path2)); + }); } -#endif - return ret; -} - -auto get_accessed_time(const std::string &path, - std::uint64_t &accessed) -> bool { - auto ret = false; - accessed = 0; -#if defined(_WIN32) - struct _stat64 st {}; - if (_stat64(path.c_str(), &st) != -1) { - accessed = static_cast(st.st_atime); -#else - struct stat st {}; - if (stat(path.c_str(), &st) != -1) { -#if defined(__APPLE__) - accessed = static_cast( - st.st_atimespec.tv_nsec + (st.st_atimespec.tv_sec * NANOS_PER_SECOND)); -#else - accessed = static_cast(st.st_atim.tv_nsec + - (st.st_atim.tv_sec * NANOS_PER_SECOND)); -#endif -#endif - ret = true; - } - - return ret; -} - -auto get_modified_time(const std::string &path, - std::uint64_t &modified) -> bool { - auto ret = false; - modified = 0U; -#if defined(_WIN32) - struct _stat64 st {}; - if (_stat64(path.c_str(), &st) != -1) { - modified = static_cast(st.st_mtime); -#else - struct stat st {}; - if (stat(path.c_str(), &st) != -1) { -#if defined(__APPLE__) - modified = static_cast( - st.st_mtimespec.tv_nsec + (st.st_mtimespec.tv_sec * NANOS_PER_SECOND)); -#else - modified = static_cast(st.st_mtim.tv_nsec + - (st.st_mtim.tv_sec * NANOS_PER_SECOND)); -#endif -#endif - ret = true; - } - - return ret; -} - -auto is_modified_date_older_than(const std::string &path, - const std::chrono::hours &hours) -> bool { - auto ret = false; - std::uint64_t modified{}; - if (get_modified_time(path, modified)) { - const auto seconds = - std::chrono::duration_cast(hours); -#if defined(_WIN32) - return (std::chrono::system_clock::from_time_t( - static_cast(modified)) + - seconds) < std::chrono::system_clock::now(); -#else - return (modified + - static_cast(seconds.count() * NANOS_PER_SECOND)) < - utils::time::get_time_now(); -#endif - } - return ret; -} - -auto move_file(std::string from, std::string to) -> bool { - from = utils::path::absolute(from); - to = utils::path::absolute(to); - - const auto directory = utils::path::remove_file_name(to); - if (not create_full_directory_path(directory)) { - return false; - } - -#if defined(_WIN32) - const bool ret = ::MoveFile(from.c_str(), to.c_str()) != 0; -#else - const bool ret = (rename(from.c_str(), to.c_str()) == 0); -#endif +#endif // defined(_WIN32) return ret; } auto read_file_lines(const std::string &path) -> std::vector { std::vector ret; - if (is_file(path)) { + if (utils::file::file(path).exists()) { std::ifstream fs(path); std::string current_line; while (not fs.eof() && std::getline(fs, current_line)) { @@ -538,7 +137,7 @@ auto read_file_lines(const std::string &path) -> std::vector { } auto reset_modified_time(const std::string &path) -> bool { - auto ret = false; + auto ret{false}; #if defined(_WIN32) SYSTEMTIME st{}; ::GetSystemTime(&st); @@ -554,33 +153,14 @@ auto reset_modified_time(const std::string &path) -> bool { ::CloseHandle(handle); } } -#else +#else // !defined(_WIN32) auto fd = open(path.c_str(), O_RDWR); if ((ret = (fd != -1))) { - ret = not futimens(fd, nullptr); + ret = futimens(fd, nullptr) == 0; close(fd); } -#endif +#endif // defined(_WIN32) + return ret; } - -auto retry_delete_directory(const std::string &dir) -> bool { - auto deleted = false; - for (std::uint8_t i = 0U; not(deleted = delete_directory(dir)) && (i < 200U); - i++) { - std::this_thread::sleep_for(10ms); - } - - return deleted; -} - -auto retry_delete_file(const std::string &file) -> bool { - auto deleted = false; - for (std::uint8_t i = 0U; not(deleted = delete_file(file)) && (i < 200U); - i++) { - std::this_thread::sleep_for(10ms); - } - - return deleted; -} } // namespace repertory::utils::file diff --git a/repertory/librepertory/src/utils/native_file.cpp b/repertory/librepertory/src/utils/native_file.cpp deleted file mode 100644 index ec56a68e..00000000 --- a/repertory/librepertory/src/utils/native_file.cpp +++ /dev/null @@ -1,319 +0,0 @@ -/* - Copyright <2018-2024> - - 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. -*/ -#include "utils/native_file.hpp" - -#include "platform/platform.hpp" -#include "types/repertory.hpp" -#include "utils/string.hpp" -#include "utils/utils.hpp" - -namespace repertory { -auto native_file::get_handle() -> native_handle { return handle_; } - -native_file::~native_file() { - if (auto_close) { - close(); - } -} - -auto native_file::clone(const native_file_ptr &ptr) -> native_file_ptr { - std::string source_path; - -#if defined(_WIN32) - source_path.resize(MAX_PATH + 1); - ::GetFinalPathNameByHandleA(ptr->get_handle(), source_path.data(), - MAX_PATH + 1, - FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); -#else - source_path.resize(PATH_MAX + 1); -#if defined(__APPLE__) - fcntl(ptr->get_handle(), F_GETPATH, source_path.data()); -#else - readlink(("/proc/self/fd/" + std::to_string(ptr->get_handle())).c_str(), - source_path.data(), source_path.size()); -#endif -#endif - source_path = source_path.c_str(); - - native_file_ptr clone; - auto res = native_file::open(source_path, clone); - if (res != api_error::success) { - throw std::runtime_error("unable to open file|sp|" + source_path + "|err|" + - api_error_to_string(res)); - } - - return clone; -} - -auto native_file::create_or_open(std::string_view source_path, bool read_only, - native_file_ptr &ptr) -> api_error { -#if defined(_WIN32) - auto handle = - read_only ? ::CreateFileA(std::string{source_path}.c_str(), GENERIC_READ, - FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, - OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr) - : ::CreateFileA(std::string{source_path}.c_str(), - GENERIC_READ | GENERIC_WRITE, - FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, - OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr); -#else - auto handle = read_only ? ::open(std::string{source_path}.c_str(), - O_CREAT | O_RDONLY | O_CLOEXEC, 0600U) - : ::open(std::string{source_path}.c_str(), - O_CREAT | O_RDWR | O_CLOEXEC, 0600U); - if (not read_only) { - chmod(std::string{source_path}.c_str(), 0600U); - } -#endif - ptr = native_file::attach(handle); - return ((handle == REPERTORY_INVALID_HANDLE) ? api_error::os_error - : api_error::success); -} - -auto native_file::create_or_open(std::string_view source_path, - native_file_ptr &ptr) -> api_error { - return create_or_open(source_path, false, ptr); -} - -auto native_file::open(std::string_view source_path, - native_file_ptr &ptr) -> api_error { - return open(source_path, false, ptr); -} - -auto native_file::open(std::string_view source_path, bool read_only, - native_file_ptr &ptr) -> api_error { -#if defined(_WIN32) - auto handle = - read_only - ? ::CreateFileA(std::string{source_path}.c_str(), GENERIC_READ, - FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, - OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, nullptr) - : ::CreateFileA(std::string{source_path}.c_str(), - GENERIC_READ | GENERIC_WRITE, - FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, - OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, nullptr); -#else - auto handle = - read_only ? ::open(std::string{source_path}.c_str(), O_RDONLY | O_CLOEXEC) - : ::open(std::string{source_path}.c_str(), O_RDWR | O_CLOEXEC); - if (not read_only) { - chmod(std::string{source_path}.c_str(), 0600U); - } -#endif - ptr = native_file::attach(handle); - return ((handle == REPERTORY_INVALID_HANDLE) ? api_error::os_error - : api_error::success); -} - -auto native_file::allocate(std::uint64_t file_size) -> bool { -#if defined(_WIN32) - LARGE_INTEGER li{}; - li.QuadPart = static_cast(file_size); - return (::SetFilePointerEx(handle_, li, nullptr, FILE_BEGIN) && - ::SetEndOfFile(handle_)); -#endif -#if defined(__linux__) - return (fallocate(handle_, 0, 0, static_cast(file_size)) >= 0); -#endif -#if defined(__APPLE__) - return (ftruncate(handle_, file_size) >= 0); -#endif -} - -void native_file::close() { - if (handle_ != REPERTORY_INVALID_HANDLE) { -#if defined(_WIN32) - ::CloseHandle(handle_); -#else - ::close(handle_); -#endif - handle_ = REPERTORY_INVALID_HANDLE; - } -} - -auto native_file::copy_from(const native_file_ptr &ptr) -> bool { - std::uint64_t file_size{}; - auto ret = ptr->get_file_size(file_size); - if (ret) { - data_buffer buffer; - buffer.resize(65536ULL * 2ULL); - std::uint64_t offset{}; - while (ret && (file_size > 0U)) { - std::size_t bytes_read{}; - ret = ptr->read_bytes(buffer.data(), buffer.size(), offset, bytes_read); - if (ret) { - std::size_t bytes_written{}; - ret = write_bytes(buffer.data(), bytes_read, offset, bytes_written); - file_size -= bytes_read; - offset += bytes_read; - } - } - flush(); - } - - return ret; -} - -auto native_file::copy_from(const std::string &path) -> bool { - auto ret = false; - native_file_ptr ptr; - if (native_file::create_or_open(path, ptr) == api_error ::success) { - ret = copy_from(ptr); - ptr->close(); - } - - return ret; -} - -void native_file::flush() { -#if defined(_WIN32) - recur_mutex_lock l(read_write_mutex_); - ::FlushFileBuffers(handle_); -#else - fsync(handle_); -#endif -} - -auto native_file::get_file_size(std::uint64_t &file_size) -> bool { - auto ret = false; -#if defined(_WIN32) - LARGE_INTEGER li{}; - if ((ret = ::GetFileSizeEx(handle_, &li) && (li.QuadPart >= 0))) { - file_size = static_cast(li.QuadPart); - } -#else -#if defined(__APPLE__) - struct stat unix_st {}; - if (fstat(handle_, &unix_st) >= 0) { -#else - struct stat64 unix_st {}; - if (fstat64(handle_, &unix_st) >= 0) { -#endif - ret = (unix_st.st_size >= 0); - if (ret) { - file_size = static_cast(unix_st.st_size); - } - } -#endif - return ret; -} - -#if defined(_WIN32) -auto native_file::read_bytes(unsigned char *buffer, std::size_t read_size, - std::uint64_t read_offset, - std::size_t &bytes_read) -> bool { - recur_mutex_lock l(read_write_mutex_); - - auto ret = false; - bytes_read = 0u; - LARGE_INTEGER li{}; - li.QuadPart = static_cast(read_offset); - if ((ret = !!::SetFilePointerEx(handle_, li, nullptr, FILE_BEGIN))) { - DWORD current_read = 0u; - do { - current_read = 0u; - ret = !!::ReadFile(handle_, &buffer[bytes_read], - static_cast(read_size - bytes_read), - ¤t_read, nullptr); - bytes_read += current_read; - } while (ret && (bytes_read < read_size) && (current_read != 0)); - } - - if (ret && (read_size != bytes_read)) { - ::SetLastError(ERROR_HANDLE_EOF); - } - - return ret; -} -#else -auto native_file::read_bytes(unsigned char *buffer, std::size_t read_size, - std::uint64_t read_offset, - std::size_t &bytes_read) -> bool { - bytes_read = 0U; - ssize_t result = 0; - do { - result = pread64(handle_, &buffer[bytes_read], read_size - bytes_read, - static_cast(read_offset + bytes_read)); - if (result > 0) { - bytes_read += static_cast(result); - } - } while ((result > 0) && (bytes_read < read_size)); - - return (result >= 0); -} -#endif -auto native_file::truncate(std::uint64_t file_size) -> bool { -#if defined(_WIN32) - recur_mutex_lock l(read_write_mutex_); - LARGE_INTEGER li{}; - li.QuadPart = static_cast(file_size); - return (::SetFilePointerEx(handle_, li, nullptr, FILE_BEGIN) && - ::SetEndOfFile(handle_)); -#else - return (ftruncate(handle_, static_cast(file_size)) >= 0); -#endif -} - -#if defined(_WIN32) -auto native_file::write_bytes(const unsigned char *buffer, - std::size_t write_size, - std::uint64_t write_offset, - std::size_t &bytes_written) -> bool { - recur_mutex_lock l(read_write_mutex_); - - bytes_written = 0u; - auto ret = true; - - LARGE_INTEGER li{}; - li.QuadPart = static_cast(write_offset); - if ((ret = !!::SetFilePointerEx(handle_, li, nullptr, FILE_BEGIN))) { - do { - DWORD current_write = 0u; - ret = !!::WriteFile(handle_, &buffer[bytes_written], - static_cast(write_size - bytes_written), - ¤t_write, nullptr); - bytes_written += current_write; - } while (ret && (bytes_written < write_size)); - } - - return ret; -} -#else -auto native_file::write_bytes(const unsigned char *buffer, - std::size_t write_size, - std::uint64_t write_offset, - std::size_t &bytes_written) -> bool { - bytes_written = 0U; - ssize_t result{}; - do { - result = - pwrite64(handle_, &buffer[bytes_written], write_size - bytes_written, - static_cast(write_offset + bytes_written)); - if (result > 0) { - bytes_written += static_cast(result); - } - } while ((result >= 0) && (bytes_written < write_size)); - - return (bytes_written == write_size); -} -#endif -} // namespace repertory diff --git a/repertory/librepertory/src/utils/unix/unix_utils.cpp b/repertory/librepertory/src/utils/unix/unix_utils.cpp index 428ae4e8..60ac4f75 100644 --- a/repertory/librepertory/src/utils/unix/unix_utils.cpp +++ b/repertory/librepertory/src/utils/unix/unix_utils.cpp @@ -201,10 +201,6 @@ auto unix_error_to_windows(int err) -> std::uint32_t { } } -auto unix_time_to_windows_time(const remote::file_time &file_time) -> UINT64 { - return (file_time / 100ULL) + 116444736000000000ULL; -} - void windows_create_to_unix(const UINT32 &create_options, const UINT32 &granted_access, std::uint32_t &flags, remote::file_mode &mode) { @@ -221,10 +217,6 @@ void windows_create_to_unix(const UINT32 &create_options, mode |= (S_IXUSR); } } - -auto windows_time_to_unix_time(std::uint64_t win_time) -> remote::file_time { - return (win_time - 116444736000000000ULL) * 100ULL; -} } // namespace repertory::utils #endif // !_WIN32 diff --git a/repertory/librepertory/src/utils/utils.cpp b/repertory/librepertory/src/utils/utils.cpp index 4c5e9cb0..72a1b855 100644 --- a/repertory/librepertory/src/utils/utils.cpp +++ b/repertory/librepertory/src/utils/utils.cpp @@ -22,16 +22,8 @@ #include "utils/utils.hpp" #include "app_config.hpp" -#include "events/event_system.hpp" -#include "events/events.hpp" -#include "providers/i_provider.hpp" -#include "types/startup_exception.hpp" -#include "utils/com_init_wrapper.hpp" #include "utils/common.hpp" -#include "utils/native_file.hpp" -#include "utils/path.hpp" #include "utils/string.hpp" -#include "utils/time.hpp" namespace repertory::utils { void calculate_allocation_size(bool directory, std::uint64_t file_size, @@ -53,32 +45,6 @@ void calculate_allocation_size(bool directory, std::uint64_t file_size, allocation_meta_size = std::to_string(allocation_size); } -auto convert_api_date(const std::string &date) -> std::uint64_t { - // 2009-10-12T17:50:30.000Z - const auto date_parts = utils::string::split(date, '.', true); - const auto date_time = date_parts[0U]; - const auto nanos = utils::string::to_uint64( - utils::string::split(date_parts[1U], 'Z', true)[0U]); - - struct tm tm1 {}; -#if defined(_WIN32) - utils::time::strptime(date_time.c_str(), "%Y-%m-%dT%T", &tm1); -#else - strptime(date_time.c_str(), "%Y-%m-%dT%T", &tm1); -#endif - return nanos + (static_cast(mktime(&tm1)) * NANOS_PER_SECOND); -} - -auto create_curl() -> CURL * { - static std::recursive_mutex mtx; - - unique_recur_mutex_lock lock(mtx); - curl_global_init(CURL_GLOBAL_DEFAULT); - lock.unlock(); - - return reset_curl(curl_easy_init()); -} - auto create_volume_label(const provider_type &prov) -> std::string { return "repertory_" + app_config::get_provider_name(prov); } @@ -86,54 +52,4 @@ auto create_volume_label(const provider_type &prov) -> std::string { auto get_attributes_from_meta(const api_meta_map &meta) -> DWORD { return static_cast(utils::string::to_uint32(meta.at(META_ATTRIBUTES))); } - -auto reset_curl(CURL *curl_handle) -> CURL * { - curl_easy_reset(curl_handle); -#if defined(__APPLE__) - curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1); -#endif - return curl_handle; -} - -auto retryable_action(const std::function &action) -> bool { - static constexpr const auto retry_count = 20U; - - auto succeeded = false; - for (std::uint8_t i = 0U; not(succeeded = action()) && (i < retry_count); - i++) { - std::this_thread::sleep_for(100ms); - } - return succeeded; -} - -void spin_wait_for_mutex(std::function complete, - std::condition_variable &cond, std::mutex &mtx, - const std::string &text) { - while (not complete()) { - unique_mutex_lock lock(mtx); - if (not complete()) { - if (not text.empty()) { - /* event_system::instance().raise(__FUNCTION__, - * "spin_wait_for_mutex", text); */ - } - cond.wait_for(lock, 1s); - } - lock.unlock(); - } -} - -void spin_wait_for_mutex(bool &complete, std::condition_variable &cond, - std::mutex &mtx, const std::string &text) { - while (not complete) { - unique_mutex_lock lock(mtx); - if (not complete) { - if (not text.empty()) { - /* event_system::instance().raise(__FUNCTION__, - * "spin_wait_for_mutex", text); */ - } - cond.wait_for(lock, 1s); - } - lock.unlock(); - } -} } // namespace repertory::utils diff --git a/repertory/librepertory/src/utils/windows/windows_utils.cpp b/repertory/librepertory/src/utils/windows/windows_utils.cpp index 367f4e6c..73888b98 100644 --- a/repertory/librepertory/src/utils/windows/windows_utils.cpp +++ b/repertory/librepertory/src/utils/windows/windows_utils.cpp @@ -23,9 +23,8 @@ #include "utils/windows/windows_utils.hpp" -#include "types/startup_exception.hpp" -#include "utils/com_init_wrapper.hpp" #include "utils/string.hpp" +#include "utils/time.hpp" #if !defined(STATUS_DEVICE_INSUFFICIENT_RESOURCES) #define STATUS_DEVICE_INSUFFICIENT_RESOURCES static_cast(0xC0000468L) @@ -88,19 +87,23 @@ auto from_api_error(const api_error &e) -> NTSTATUS { } auto get_accessed_time_from_meta(const api_meta_map &meta) -> std::uint64_t { - return utils::string::to_uint64(meta.at(META_ACCESSED)); + return utils::time::unix_time_to_windows_time( + utils::string::to_uint64(meta.at(META_ACCESSED))); } auto get_changed_time_from_meta(const api_meta_map &meta) -> std::uint64_t { - return utils::string::to_uint64(meta.at(META_MODIFIED)); + return utils::time::unix_time_to_windows_time( + utils::string::to_uint64(meta.at(META_MODIFIED))); } auto get_creation_time_from_meta(const api_meta_map &meta) -> std::uint64_t { - return utils::string::to_uint64(meta.at(META_CREATION)); + return utils::time::unix_time_to_windows_time( + utils::string::to_uint64(meta.at(META_CREATION))); } auto get_written_time_from_meta(const api_meta_map &meta) -> std::uint64_t { - return utils::string::to_uint64(meta.at(META_WRITTEN)); + return utils::time::unix_time_to_windows_time( + utils::string::to_uint64(meta.at(META_WRITTEN))); } auto unix_access_mask_to_windows(std::int32_t mask) -> int { diff --git a/repertory/repertory/include/cli/mount.hpp b/repertory/repertory/include/cli/mount.hpp index 220bdc12..c545e4f6 100644 --- a/repertory/repertory/include/cli/mount.hpp +++ b/repertory/repertory/include/cli/mount.hpp @@ -80,7 +80,7 @@ mount(std::vector args, std::string data_directory, std::cout << "Generated " << app_config::get_provider_display_name(prov) << " Configuration" << std::endl; std::cout << config.get_config_file_path() << std::endl; - ret = utils::file::is_file(config.get_config_file_path()) + ret = utils::file::file(config.get_config_file_path()).exists() ? exit_code::success : exit_code::file_creation_failed; } else { diff --git a/repertory/repertory/main.cpp b/repertory/repertory/main.cpp index 074ae8e7..791b9eea 100644 --- a/repertory/repertory/main.cpp +++ b/repertory/repertory/main.cpp @@ -21,7 +21,7 @@ */ #if defined(PROJECT_ENABLE_BACKWARD_CPP) #include "backward.hpp" -#endif +#endif // defined(PROJECT_ENABLE_BACKWARD_CPP) #include "cli/actions.hpp" #include "initialize.hpp" @@ -34,7 +34,7 @@ using namespace repertory; auto main(int argc, char **argv) -> int { #if defined(PROJECT_ENABLE_BACKWARD_CPP) static backward::SignalHandling sh; -#endif +#endif // defined(PROJECT_ENABLE_BACKWARD_CPP) if (not repertory::project_initialize()) { std::cerr << "fatal: failed to initialize repertory" << std::endl; diff --git a/repertory/repertory_test/include/fixtures/fuse_fixture.hpp b/repertory/repertory_test/include/fixtures/fuse_fixture.hpp new file mode 100644 index 00000000..0849a227 --- /dev/null +++ b/repertory/repertory_test/include/fixtures/fuse_fixture.hpp @@ -0,0 +1,235 @@ +/* + Copyright <2018-2024> + + 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. +*/ +#ifndef REPERTORY_TEST_INCLUDE_FIXTURES_FUSE_FIXTURE_HPP +#define REPERTORY_TEST_INCLUDE_FIXTURES_FUSE_FIXTURE_HPP +#if !defined(_WIN32) + +#include "test_common.hpp" + +#include "app_config.hpp" +#include "comm/curl/curl_comm.hpp" +#include "drives/fuse/fuse_drive.hpp" +#include "platform/platform.hpp" +#include "providers/encrypt/encrypt_provider.hpp" +#include "providers/s3/s3_provider.hpp" +#include "providers/sia/sia_provider.hpp" +#include "utils/file_utils.hpp" +#include "utils/path.hpp" + +#if !defined(ACCESSPERMS) +#define ACCESSPERMS (S_IRWXU | S_IRWXG | S_IRWXO) /* 0777 */ +#endif + +namespace repertory { +inline constexpr const auto SLEEP_SECONDS{1.5s}; + +template class fuse_test : public ::testing::Test { +public: + static std::string cfg_directory; + static std::unique_ptr comm; + static std::unique_ptr config; + static std::filesystem::path current_directory; + static std::unique_ptr drive; + static lock_data lock_data_; + static std::string mount_location; + static std::unique_ptr provider; + static std::string test_directory; + +protected: + static void SetUpTestCase() { + current_directory = std::filesystem::current_path(); + + test_directory = utils::path::combine( + test::get_test_output_dir(), + { + "fuse_test", + std::to_string(static_cast(provider_t::type)), + }); + + ASSERT_TRUE(utils::file::directory(test_directory).remove_recursively()); + + mount_location = utils::path::combine(test_directory, {"mount"}); + ASSERT_TRUE(utils::file::directory(mount_location).create_directory()); + + cfg_directory = utils::path::combine(test_directory, {"cfg"}); + ASSERT_TRUE(utils::file::directory(cfg_directory).create_directory()); + + config = std::make_unique(provider_t::type, cfg_directory); + + std::vector drive_args{}; + switch (provider_t::type) { + case provider_type::s3: { + { + app_config src_cfg{ + provider_type::s3, + utils::path::combine(test::get_test_input_dir(), {"storj"}), + }; + config->set_enable_drive_events(true); + config->set_event_level(event_level::trace); + config->set_s3_config(src_cfg.get_s3_config()); + } + + comm = std::make_unique(config->get_s3_config()); + drive_args = std::vector({ + "-s3", + "-na", + "storj", + }); + } break; + + case provider_type::sia: { + { + app_config src_cfg{ + provider_type::sia, + utils::path::combine(test::get_test_input_dir(), {"sia"}), + }; + config->set_enable_drive_events(true); + config->set_event_level(event_level::debug); + config->set_host_config(src_cfg.get_host_config()); + } + + comm = std::make_unique(config->get_host_config()); + } break; + // case 0U: { + // config = + // std::make_unique(provider_type::encrypt, + // cfg_directory); + // { + // app_config src_cfg( + // provider_type::s3, + // utils::path::combine(test::get_test_input_dir(), {"encrypt"})); + // config->set_enable_drive_events(true); + // config->set_event_level(event_level::trace); + // config->set_s3_config(src_cfg.get_s3_config()); + // } + // + // comm = std::make_unique(config->get_s3_config()); + // provider = std::make_unique(*config, *comm); + // drive_args = std::vector({"-en"}); + // } break; + + default: + throw std::runtime_error("provider type is not implemented"); + return; + } + + provider = std::make_unique(*config, *comm); + drive_args.push_back(mount_location); + execute_mount(drive_args); + } + + static void TearDownTestCase() { + execute_unmount(); + std::filesystem::current_path(current_directory); + + [[maybe_unused]] auto ret = + utils::file::directory(test_directory).remove_recursively(); + } + +public: + static auto create_file_and_test(std::string name) -> std::string { + auto file_path = utils::path::combine(mount_location, {name}); + + auto fd = + open(file_path.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP); + EXPECT_LE(1, fd); + + EXPECT_TRUE(utils::file::file(file_path).exists()); + EXPECT_FALSE(utils::file::directory(file_path).exists()); + + auto opt_size = utils::file::file{file_path}.size(); + EXPECT_TRUE(opt_size.has_value()); + EXPECT_EQ(0U, opt_size.value()); + + EXPECT_EQ(0, close(fd)); + std::this_thread::sleep_for(SLEEP_SECONDS); + + return file_path; + } + + static void execute_mount(auto &&drive_args) { + auto mount_cmd = "./repertory -dd \"" + config->get_data_directory() + + "\"" + " " + utils::string::join(drive_args, ' '); + std::cout << "mount command: " << mount_cmd << std::endl; + ASSERT_EQ(0, system(mount_cmd.c_str())); + std::this_thread::sleep_for(5s); + EXPECT_EQ(0, system(("mount|grep \"" + mount_location + "\"").c_str())); + } + + static void execute_unmount() { + auto unmounted{false}; + for (int i = 0; not unmounted && (i < 50); i++) { + unmounted = (fuse_base::unmount(mount_location) == 0); + if (not unmounted) { + std::this_thread::sleep_for(100ms); + } + } + + EXPECT_TRUE(unmounted); + } + + static void unlink_file_and_test(const std::string &file_path) { + int ret = 0; + for (auto i = 0; ((ret = unlink(file_path.c_str())) != 0) && (i < 20); + i++) { + std::this_thread::sleep_for(100ms); + } + + EXPECT_EQ(0, ret); + + std::this_thread::sleep_for(SLEEP_SECONDS); + EXPECT_FALSE(utils::file::directory(file_path).exists()); + EXPECT_FALSE(utils::file::file(file_path).exists()); + } +}; + +template +std::string fuse_test::cfg_directory{}; + +template +std::unique_ptr fuse_test::comm{}; + +template +std::unique_ptr fuse_test::config{}; + +template +std::filesystem::path fuse_test::current_directory{}; + +template +std::unique_ptr fuse_test::drive{}; + +template lock_data fuse_test::lock_data_{}; + +template +std::string fuse_test::mount_location{}; + +template +std::unique_ptr fuse_test::provider{}; + +template +std::string fuse_test::test_directory; + +typedef ::testing::Types fuse_provider_types; +} // namespace repertory + +#endif // !defined(_WIN32) +#endif // REPERTORY_TEST_INCLUDE_FIXTURES_FUSE_FIXTURE_HPP diff --git a/repertory/repertory_test/include/fixtures/winfsp_fixture.hpp b/repertory/repertory_test/include/fixtures/winfsp_fixture.hpp index 3984453a..e530ea15 100644 --- a/repertory/repertory_test/include/fixtures/winfsp_fixture.hpp +++ b/repertory/repertory_test/include/fixtures/winfsp_fixture.hpp @@ -19,8 +19,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifndef REPERTORY_WINFSP_FIXTURE_HPP -#define REPERTORY_WINFSP_FIXTURE_HPP +#ifndef REPERTORY_TEST_INCLUDE_FIXTURES_WINFSP_FIXTURE_HPP +#define REPERTORY_TEST_INCLUDE_FIXTURES_WINFSP_FIXTURE_HPP #if defined(_WIN32) #include "test_common.hpp" @@ -31,6 +31,8 @@ #include "platform/platform.hpp" #include "providers/s3/s3_provider.hpp" #include "providers/sia/sia_provider.hpp" +#include "utils/file_utils.hpp" +#include "utils/path.hpp" extern std::size_t PROVIDER_INDEX; @@ -47,14 +49,20 @@ protected: void SetUp() override { if (PROVIDER_INDEX != 0) { if (PROVIDER_INDEX == 1) { - EXPECT_TRUE(utils::file::delete_directory_recursively( - "./test_config/winfsp_test" + std::to_string(PROVIDER_INDEX))); + EXPECT_TRUE(utils::file::directory( + utils::path::combine( + test::get_test_output_dir(), + {"winfsp_test" + std::to_string(PROVIDER_INDEX)})) + .remove_recursively()); - app_config src_cfg(provider_type::s3, - utils::path::combine(get_test_dir(), {"storj"})); + app_config src_cfg( + provider_type::s3, + utils::path::combine(test::get_test_input_dir(), {"storj"})); config = std::make_unique( provider_type::s3, - "./test_config/winfsp_test" + std::to_string(PROVIDER_INDEX)); + utils::path::combine( + test::get_test_output_dir(), + {"winfsp_test" + std::to_string(PROVIDER_INDEX)})); EXPECT_FALSE(config ->set_value_by_name("S3Config.AccessKey", src_cfg.get_s3_config().access_key) @@ -89,14 +97,20 @@ protected: } if (PROVIDER_INDEX == 2) { - EXPECT_TRUE(utils::file::delete_directory_recursively( - "./test_config/winfsp_test" + std::to_string(PROVIDER_INDEX))); + EXPECT_TRUE(utils::file::directory( + utils::path::combine( + test::get_test_output_dir(), + {"winfsp_test" + std::to_string(PROVIDER_INDEX)})) + .remove_recursively()); - app_config src_cfg(provider_type::sia, - utils::path::combine(get_test_dir(), {"sia"})); + app_config src_cfg( + provider_type::sia, + utils::path::combine(test::get_test_input_dir(), {"sia"})); config = std::make_unique( provider_type::sia, - "./test_config/winfsp_test" + std::to_string(PROVIDER_INDEX)); + utils::path::combine( + test::get_test_output_dir(), + {"winfsp_test" + std::to_string(PROVIDER_INDEX)})); [[maybe_unused]] auto val = config->set_value_by_name( "HostConfig.AgentString", src_cfg.get_host_config().agent_string); EXPECT_FALSE( @@ -135,12 +149,15 @@ protected: config.reset(); event_system::instance().stop(); - EXPECT_TRUE(utils::file::delete_directory_recursively( - "./test_config/winfsp_test" + std::to_string(PROVIDER_INDEX))); + EXPECT_TRUE(utils::file::directory( + utils::path::combine( + test::get_test_output_dir(), + {"winfsp_test" + std::to_string(PROVIDER_INDEX)})) + .remove_recursively()); } } }; } // namespace repertory #endif -#endif // REPERTORY_WINFSP_FIXTURE_HPP +#endif // REPERTORY_TEST_INCLUDE_FIXTURES_WINFSP_FIXTURE_HPP diff --git a/repertory/repertory_test/include/mocks/mock_fuse_drive.hpp b/repertory/repertory_test/include/mocks/mock_fuse_drive.hpp index efbcca76..45d3ed44 100644 --- a/repertory/repertory_test/include/mocks/mock_fuse_drive.hpp +++ b/repertory/repertory_test/include/mocks/mock_fuse_drive.hpp @@ -19,8 +19,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifndef TESTS_MOCKS_MOCK_FUSE_DRIVE_HPP_ -#define TESTS_MOCKS_MOCK_FUSE_DRIVE_HPP_ +#ifndef REPERTORY_TEST_INCLUDE_FIXTURES_MOCKS_MOCK_FUSE_DRIVE_HPP_ +#define REPERTORY_TEST_INCLUDE_FIXTURES_MOCKS_MOCK_FUSE_DRIVE_HPP_ #if !defined(_WIN32) #include "test_common.hpp" @@ -68,10 +68,10 @@ public: dir_item.size = 0; dir_item.meta = { {META_ATTRIBUTES, "16"}, - {META_MODIFIED, std::to_string(utils::time::get_file_time_now())}, - {META_WRITTEN, std::to_string(utils::time::get_file_time_now())}, - {META_ACCESSED, std::to_string(utils::time::get_file_time_now())}, - {META_CREATION, std::to_string(utils::time::get_file_time_now())}}; + {META_MODIFIED, std::to_string(utils::time::get_time_now())}, + {META_WRITTEN, std::to_string(utils::time::get_time_now())}, + {META_ACCESSED, std::to_string(utils::time::get_time_now())}, + {META_CREATION, std::to_string(utils::time::get_time_now())}}; list.emplace_back(dir_item); dir_item.api_path = ".."; @@ -132,11 +132,11 @@ public: utils::path::combine(mount_location_, {to_api_path}); if (overwrite) { - if (not utils::file::retry_delete_file(to_file_path)) { + if (not utils::file::file(to_file_path).remove()) { return -1; } - } else if (utils::file::is_directory(to_file_path) || - utils::file::is_file(to_file_path)) { + } else if (utils::file::directory(to_file_path).exists() || + utils::file::file(to_file_path).exists()) { errno = EEXIST; return -1; } @@ -155,5 +155,5 @@ public: }; } // namespace repertory -#endif // _WIN32 -#endif // TESTS_MOCKS_MOCK_FUSE_DRIVE_HPP_ +#endif // !defined(_WIN32) +#endif // REPERTORY_TEST_INCLUDE_FIXTURES_MOCKS_MOCK_FUSE_DRIVE_HPP_ diff --git a/repertory/repertory_test/include/mocks/mock_open_file.hpp b/repertory/repertory_test/include/mocks/mock_open_file.hpp index 1de5bf44..e56786a4 100644 --- a/repertory/repertory_test/include/mocks/mock_open_file.hpp +++ b/repertory/repertory_test/include/mocks/mock_open_file.hpp @@ -19,8 +19,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifndef TESTS_MOCKS_MOCK_OPEN_FILE_HPP_ -#define TESTS_MOCKS_MOCK_OPEN_FILE_HPP_ +#ifndef REPERTORY_TEST_INCLUDE_MOCKS_MOCK_OPEN_FILE_HPP_ +#define REPERTORY_TEST_INCLUDE_MOCKS_MOCK_OPEN_FILE_HPP_ #include "test_common.hpp" @@ -95,4 +95,4 @@ public: }; } // namespace repertory -#endif // TESTS_MOCKS_MOCK_OPEN_FILE_HPP_ +#endif // REPERTORY_TEST_INCLUDE_MOCKS_MOCK_OPEN_FILE_HPP_ diff --git a/repertory/repertory_test/include/mocks/mock_provider.hpp b/repertory/repertory_test/include/mocks/mock_provider.hpp index 5c0c33a0..d8bcb978 100644 --- a/repertory/repertory_test/include/mocks/mock_provider.hpp +++ b/repertory/repertory_test/include/mocks/mock_provider.hpp @@ -19,8 +19,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifndef TESTS_MOCKS_MOCK_PROVIDER_HPP_ -#define TESTS_MOCKS_MOCK_PROVIDER_HPP_ +#ifndef REPERTORY_TEST_INCLUDE_MOCKS_MOCK_PROVIDER_HPP_ +#define REPERTORY_TEST_INCLUDE_MOCKS_MOCK_PROVIDER_HPP_ #include "test_common.hpp" @@ -159,4 +159,4 @@ public: }; } // namespace repertory -#endif // TESTS_MOCKS_MOCK_PROVIDER_HPP_ +#endif // REPERTORY_TEST_INCLUDE_MOCKS_MOCK_PROVIDER_HPP_ diff --git a/repertory/repertory_test/include/mocks/mock_upload_manager.hpp b/repertory/repertory_test/include/mocks/mock_upload_manager.hpp index f842a29d..5c02a58b 100644 --- a/repertory/repertory_test/include/mocks/mock_upload_manager.hpp +++ b/repertory/repertory_test/include/mocks/mock_upload_manager.hpp @@ -19,8 +19,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifndef TESTS_MOCKS_MOCK_UPLOAD_MANAGER_HPP_ -#define TESTS_MOCKS_MOCK_UPLOAD_MANAGER_HPP_ +#ifndef REPERTORY_TEST_INCLUDE_MOCKS_MOCK_UPLOAD_MANAGER_HPP_ +#define REPERTORY_TEST_INCLUDE_MOCKS_MOCK_UPLOAD_MANAGER_HPP_ #include "test_common.hpp" @@ -41,4 +41,4 @@ public: }; } // namespace repertory -#endif // TESTS_MOCKS_MOCK_UPLOAD_MANAGER_HPP_ +#endif // REPERTORY_TEST_INCLUDE_MOCKS_MOCK_UPLOAD_MANAGER_HPP_ diff --git a/repertory/repertory_test/include/mocks/mock_winfsp_drive.hpp b/repertory/repertory_test/include/mocks/mock_winfsp_drive.hpp index 50063dd7..cee27b20 100644 --- a/repertory/repertory_test/include/mocks/mock_winfsp_drive.hpp +++ b/repertory/repertory_test/include/mocks/mock_winfsp_drive.hpp @@ -19,8 +19,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifndef TESTS_MOCKS_MOCK_WINFSP_DRIVE_HPP_ -#define TESTS_MOCKS_MOCK_WINFSP_DRIVE_HPP_ +#ifndef REPERTORY_TEST_INCLUDE_MOCKS_MOCK_WINFSP_DRIVE_HPP_ +#define REPERTORY_TEST_INCLUDE_MOCKS_MOCK_WINFSP_DRIVE_HPP_ #if defined(_WIN32) #include "test_common.hpp" @@ -57,10 +57,10 @@ public: di.size = 0u; di.meta = { {META_ATTRIBUTES, "16"}, - {META_MODIFIED, std::to_string(utils::time::get_file_time_now())}, - {META_WRITTEN, std::to_string(utils::time::get_file_time_now())}, - {META_ACCESSED, std::to_string(utils::time::get_file_time_now())}, - {META_CREATION, std::to_string(utils::time::get_file_time_now())}}; + {META_MODIFIED, std::to_string(utils::time::get_time_now())}, + {META_WRITTEN, std::to_string(utils::time::get_time_now())}, + {META_ACCESSED, std::to_string(utils::time::get_time_now())}, + {META_CREATION, std::to_string(utils::time::get_time_now())}}; list.emplace_back(di); di.api_path = ".."; @@ -138,7 +138,7 @@ public: auto populate_file_info(const std::string &api_path, remote::file_info &file_info) -> api_error override { const auto file_path = utils::path::combine(mount_location_, {api_path}); - const auto directory = utils::file::is_directory(file_path); + const auto directory = utils::file::directory(file_path).exists(); const auto attributes = FILE_FLAG_BACKUP_SEMANTICS | (directory ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL); @@ -148,8 +148,14 @@ public: FILE_BASIC_INFO fi{}; ::GetFileInformationByHandleEx(handle, FileBasicInfo, &fi, sizeof(fi)); if (not directory) { - utils::file::get_file_size(file_path, file_info.FileSize); + auto opt_size = utils::file::file{file_path}.size(); + if (not opt_size.has_value()) { + return api_error::os_error; + } + + file_info.FileSize = opt_size.value(); } + file_info.AllocationSize = directory ? 0 : utils::divide_with_ceiling(file_info.FileSize, @@ -167,4 +173,4 @@ public: } // namespace repertory #endif // _WIN32 -#endif // TESTS_MOCKS_MOCK_WINFSP_DRIVE_HPP_ +#endif // REPERTORY_TEST_INCLUDE_MOCKS_MOCK_WINFSP_DRIVE_HPP_ diff --git a/repertory/repertory_test/include/test_common.hpp b/repertory/repertory_test/include/test_common.hpp index 9bce4959..d6b5d584 100644 --- a/repertory/repertory_test/include/test_common.hpp +++ b/repertory/repertory_test/include/test_common.hpp @@ -19,47 +19,15 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifndef TESTS_TEST_COMMON_HPP_ -#define TESTS_TEST_COMMON_HPP_ - -#if defined(U) -#undef U -#endif +#ifndef REPERTORY_TEST_INCLUDE_TEST_COMMON_HPP_ +#define REPERTORY_TEST_INCLUDE_TEST_COMMON_HPP_ REPERTORY_IGNORE_WARNINGS_ENABLE() -#include -#include +#include "test.hpp" REPERTORY_IGNORE_WARNINGS_DISABLE() #include "events/consumers/console_consumer.hpp" #include "events/event_system.hpp" #include "events/events.hpp" -#include "utils/encrypt.hpp" -#include "utils/file_utils.hpp" -#include "utils/native_file.hpp" -#define COMMA , - -using ::testing::_; -using namespace ::testing; - -namespace repertory { -[[nodiscard]] auto create_random_file(std::string path, - std::size_t size) -> native_file_ptr; - -void delete_generated_files(); - -[[nodiscard]] auto generate_test_file_name( - const std::string &directory, - const std::string &file_name_no_extension) -> std::string; - -template -static void decrypt_and_verify(const T &buffer, const std::string &token, - T2 &result) { - EXPECT_TRUE(utils::encryption::decrypt_data(token, buffer, result)); -} - -[[nodiscard]] auto get_test_dir() -> std::string; -} // namespace repertory - -#endif // TESTS_TEST_COMMON_HPP_ +#endif // REPERTORY_TEST_INCLUDE_TEST_COMMON_HPP_ diff --git a/repertory/repertory_test/include/utils/event_capture.hpp b/repertory/repertory_test/include/utils/event_capture.hpp index 2a25eb46..42d4d55b 100644 --- a/repertory/repertory_test/include/utils/event_capture.hpp +++ b/repertory/repertory_test/include/utils/event_capture.hpp @@ -19,8 +19,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifndef TESTS_UTILS_EVENT_CAPTURE_HPP_ -#define TESTS_UTILS_EVENT_CAPTURE_HPP_ +#ifndef REPERTORY_TEST_INCLUDE_UTILS_EVENT_CAPTURE_HPP_ +#define REPERTORY_TEST_INCLUDE_UTILS_EVENT_CAPTURE_HPP_ #include "test_common.hpp" @@ -106,4 +106,4 @@ public: }; } // namespace repertory -#endif // TESTS_UTILS_EVENT_CAPTURE_HPP_ +#endif // REPERTORY_TEST_INCLUDE_UTILS_EVENT_CAPTURE_HPP_ diff --git a/repertory/repertory_test/main.cpp b/repertory/repertory_test/main.cpp index 8e202fe1..05390ec5 100644 --- a/repertory/repertory_test/main.cpp +++ b/repertory/repertory_test/main.cpp @@ -63,8 +63,6 @@ auto main(int argc, char **argv) -> int { ::testing::InitGoogleTest(&argc, argv); auto ret = RUN_ALL_TESTS(); - delete_generated_files(); - repertory::project_cleanup(); return ret; diff --git a/repertory/repertory_test/src/config_test.cpp b/repertory/repertory_test/src/config_test.cpp index f9a38a5a..00ba67f9 100644 --- a/repertory/repertory_test/src/config_test.cpp +++ b/repertory/repertory_test/src/config_test.cpp @@ -33,20 +33,24 @@ public: static console_consumer cs; std::string s3_directory{ - utils::path::combine("./test_config", {"config_test", "s3"})}; + utils::path::combine(test::get_test_output_dir(), {"config_test", "s3"})}; - std::string sia_directory{ - utils::path::combine("./test_config", {"config_test", "sia"})}; + std::string sia_directory{utils::path::combine(test::get_test_output_dir(), + {"config_test", "sia"})}; void SetUp() override { event_system::instance().start(); - ASSERT_TRUE(utils::file::delete_directory_recursively( - utils::path::combine("./test_config", {"config_test"}))); + ASSERT_TRUE( + utils::file::directory( + utils::path::combine(test::get_test_output_dir(), {"config_test"})) + .remove_recursively()); } void TearDown() override { - ASSERT_TRUE(utils::file::delete_directory_recursively( - utils::path::combine("./test_config", {"config_test"}))); + ASSERT_TRUE( + utils::file::directory( + utils::path::combine(test::get_test_output_dir(), {"config_test"})) + .remove_recursively()); event_system::instance().stop(); } }; @@ -163,10 +167,12 @@ TEST_F(config_test, sia_default_settings) { json data; EXPECT_TRUE(utils::file::read_json_file(config_file, data)); EXPECT_STREQ(DEFAULT_SIA_CONFIG.c_str(), data.dump(2).c_str()); - EXPECT_TRUE(utils::file::is_directory( - utils::path::combine(sia_directory, {"cache"}))); - EXPECT_TRUE(utils::file::is_directory( - utils::path::combine(sia_directory, {"logs"}))); + EXPECT_TRUE( + utils::file::directory(utils::path::combine(sia_directory, {"cache"})) + .exists()); + EXPECT_TRUE( + utils::file::directory(utils::path::combine(sia_directory, {"logs"})) + .exists()); } } @@ -180,10 +186,12 @@ TEST_F(config_test, s3_default_settings) { json data; EXPECT_TRUE(utils::file::read_json_file(config_file, data)); EXPECT_STREQ(DEFAULT_S3_CONFIG.c_str(), data.dump(2).c_str()); - EXPECT_TRUE(utils::file::is_directory( - utils::path::combine(s3_directory, {"cache"}))); - EXPECT_TRUE(utils::file::is_directory( - utils::path::combine(s3_directory, {"logs"}))); + EXPECT_TRUE( + utils::file::directory(utils::path::combine(s3_directory, {"cache"})) + .exists()); + EXPECT_TRUE( + utils::file::directory(utils::path::combine(s3_directory, {"logs"})) + .exists()); } } diff --git a/repertory/repertory_test/src/database_test.cpp b/repertory/repertory_test/src/database_test.cpp index f00c8dc3..447f1725 100644 --- a/repertory/repertory_test/src/database_test.cpp +++ b/repertory/repertory_test/src/database_test.cpp @@ -35,8 +35,9 @@ TEST(database, db_insert) { { sqlite3 *db3_ptr{nullptr}; auto res = sqlite3_open_v2( - utils::path::combine(get_test_dir(), {"test.db3"}).c_str(), &db3_ptr, - SQLITE_OPEN_READWRITE, nullptr); + utils::path::combine(test::get_test_input_dir(), {"test.db3"}) + .c_str(), + &db3_ptr, SQLITE_OPEN_READWRITE, nullptr); ASSERT_EQ(SQLITE_OK, res); ASSERT_TRUE(db3_ptr != nullptr); @@ -78,8 +79,9 @@ TEST(database, db_select) { { sqlite3 *db3_ptr{nullptr}; auto res = sqlite3_open_v2( - utils::path::combine(get_test_dir(), {"test.db3"}).c_str(), &db3_ptr, - SQLITE_OPEN_READWRITE, nullptr); + utils::path::combine(test::get_test_input_dir(), {"test.db3"}) + .c_str(), + &db3_ptr, SQLITE_OPEN_READWRITE, nullptr); ASSERT_EQ(SQLITE_OK, res); ASSERT_TRUE(db3_ptr != nullptr); diff --git a/repertory/repertory_test/src/file_manager_open_file_test.cpp b/repertory/repertory_test/src/file_manager_open_file_test.cpp index 94e9d9f3..d322cc67 100644 --- a/repertory/repertory_test/src/file_manager_open_file_test.cpp +++ b/repertory/repertory_test/src/file_manager_open_file_test.cpp @@ -55,7 +55,7 @@ static void validate_write(file_manager::open_file &o, std::size_t offset, TEST(open_file, properly_initializes_state_for_0_byte_file) { const auto source_path = - generate_test_file_name("./test_config", "file_manager_open_file_test"); + test::generate_test_file_name("file_manager_open_file_test"); mock_provider mp; mock_upload_manager um; @@ -76,7 +76,7 @@ TEST(open_file, properly_initializes_state_for_0_byte_file) { TEST(open_file, properly_initializes_state_based_on_chunk_size) { const auto source_path = - generate_test_file_name("./test_config", "file_manager_open_file_test"); + test::generate_test_file_name("file_manager_open_file_test"); mock_provider mp; mock_upload_manager um; @@ -109,7 +109,7 @@ TEST(open_file, properly_initializes_state_based_on_chunk_size) { TEST(open_file, will_not_change_source_path_for_0_byte_file) { const auto source_path = - generate_test_file_name("./test_config", "file_manager_open_file_test"); + test::generate_test_file_name("file_manager_open_file_test"); mock_provider mp; mock_upload_manager um; @@ -128,12 +128,12 @@ TEST(open_file, will_not_change_source_path_for_0_byte_file) { o.close(); EXPECT_EQ(api_error::success, o.get_api_error()); EXPECT_STREQ(source_path.c_str(), o.get_source_path().c_str()); - EXPECT_TRUE(utils::file::is_file(fsi.source_path)); + EXPECT_TRUE(utils::file::file(fsi.source_path).exists()); } TEST(open_file, will_change_source_path_if_file_size_is_greater_than_0) { const auto source_path = - generate_test_file_name("./test_config", "file_manager_open_file_test"); + test::generate_test_file_name("file_manager_open_file_test"); mock_provider mp; mock_upload_manager um; @@ -166,14 +166,14 @@ TEST(open_file, will_change_source_path_if_file_size_is_greater_than_0) { o.close(); EXPECT_EQ(api_error::download_stopped, o.get_api_error()); EXPECT_STRNE(source_path.c_str(), o.get_source_path().c_str()); - EXPECT_FALSE(utils::file::is_file(source_path)); + EXPECT_FALSE(utils::file::file(source_path).exists()); } TEST(open_file, will_not_change_source_path_if_file_size_matches_existing_source) { - const auto source_path = - generate_test_file_name("./test_config", "file_manager_open_file_test"); - create_random_file(source_path, test_chunk_size)->close(); + auto &rf = test::create_random_file(test_chunk_size); + const auto source_path = rf.get_path(); + rf.close(); mock_provider mp; mock_upload_manager um; @@ -192,15 +192,12 @@ TEST(open_file, o.close(); EXPECT_EQ(api_error::success, o.get_api_error()); EXPECT_STREQ(source_path.c_str(), o.get_source_path().c_str()); - EXPECT_TRUE(utils::file::is_file(source_path)); + EXPECT_TRUE(utils::file::file(source_path).exists()); } TEST(open_file, write_with_incomplete_download) { - const auto source_path = - generate_test_file_name("./test_config", "file_manager_open_file_test"); - auto nf = create_random_file( - generate_test_file_name("./test_config", "file_manager_open_file_test"), - test_chunk_size * 2u); + const auto source_path = test::generate_test_file_name("test"); + auto &nf = test::create_random_file(test_chunk_size * 2u); mock_provider mp; mock_upload_manager um; @@ -236,9 +233,8 @@ TEST(open_file, write_with_incomplete_download) { if (offset == 0u) { std::size_t bytes_read{}; data.resize(size); - auto ret = nf->read_bytes(&data[0u], size, offset, bytes_read) - ? api_error::success - : api_error::os_error; + auto ret = nf.read(data, offset, &bytes_read) ? api_error::success + : api_error::os_error; EXPECT_EQ(bytes_read, data.size()); return ret; } @@ -278,18 +274,18 @@ TEST(open_file, write_with_incomplete_download) { test_state(); o.close(); - nf->close(); + nf.close(); test_state(); EXPECT_EQ(api_error::download_incomplete, o.get_api_error()); - EXPECT_TRUE(utils::file::is_file(fsi.source_path)); + EXPECT_TRUE(utils::file::file(fsi.source_path).exists()); } TEST(open_file, write_new_file) { const auto source_path = - generate_test_file_name("./test_config", "file_manager_open_file_test"); + test::generate_test_file_name("file_manager_open_file_test"); mock_provider mp; mock_upload_manager um; @@ -357,12 +353,12 @@ TEST(open_file, write_new_file) { EXPECT_EQ(api_error::success, o.get_api_error()); - EXPECT_TRUE(utils::file::is_file(fsi.source_path)); + EXPECT_TRUE(utils::file::file(fsi.source_path).exists()); } TEST(open_file, write_new_file_multiple_chunks) { const auto source_path = - generate_test_file_name("./test_config", "file_manager_open_file_test"); + test::generate_test_file_name("file_manager_open_file_test"); mock_provider mp; mock_upload_manager um; @@ -449,13 +445,13 @@ TEST(open_file, write_new_file_multiple_chunks) { EXPECT_EQ(api_error::success, o.get_api_error()); - EXPECT_TRUE(utils::file::is_file(fsi.source_path)); + EXPECT_TRUE(utils::file::file(fsi.source_path).exists()); } TEST(open_file, resize_file_to_0_bytes) { - const auto source_path = - generate_test_file_name("./test_config", "file_manager_open_file_test"); - create_random_file(source_path, test_chunk_size * 4u)->close(); + auto &rf = test::create_random_file(test_chunk_size * 4u); + const auto source_path = rf.get_path(); + rf.close(); mock_provider mp; mock_upload_manager um; @@ -503,9 +499,9 @@ TEST(open_file, resize_file_to_0_bytes) { } TEST(open_file, resize_file_by_full_chunk) { - const auto source_path = - generate_test_file_name("./test_config", "file_manager_open_file_test"); - create_random_file(source_path, test_chunk_size * 4u)->close(); + auto &rf = test::create_random_file(test_chunk_size * 4u); + const auto source_path = rf.get_path(); + rf.close(); mock_provider mp; mock_upload_manager um; @@ -556,7 +552,7 @@ TEST(open_file, can_add_handle) { event_system::instance().start(); console_consumer c; const auto source_path = - generate_test_file_name("./test_config", "file_manager_open_file_test"); + test::generate_test_file_name("file_manager_open_file_test"); mock_provider mp; mock_upload_manager um; @@ -618,7 +614,7 @@ TEST(open_file, can_remove_handle) { console_consumer c; const auto source_path = - generate_test_file_name("./test_config", "file_manager_open_file_test"); + test::generate_test_file_name("file_manager_open_file_test"); mock_provider mp; mock_upload_manager um; diff --git a/repertory/repertory_test/src/file_manager_ring_buffer_open_file_test.cpp b/repertory/repertory_test/src/file_manager_ring_buffer_open_file_test.cpp index 509cf37b..2ad27551 100644 --- a/repertory/repertory_test/src/file_manager_ring_buffer_open_file_test.cpp +++ b/repertory/repertory_test/src/file_manager_ring_buffer_open_file_test.cpp @@ -31,11 +31,11 @@ namespace repertory { static constexpr const std::size_t test_chunk_size = 1024u; static std::string ring_buffer_dir = utils::path::combine( - "./test_config", {"file_manager_ring_buffer_open_file_test"}); + test::get_test_output_dir(), {"file_manager_ring_buffer_open_file_test"}); TEST(ring_buffer_open_file, can_forward_to_last_chunk) { const auto source_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); + test::generate_test_file_name("ring_buffer_open_file"); mock_provider mp; @@ -61,13 +61,13 @@ TEST(ring_buffer_open_file, can_forward_to_last_chunk) { } } - EXPECT_TRUE(utils::file::delete_directory_recursively(ring_buffer_dir)); + EXPECT_TRUE(utils::file::directory(ring_buffer_dir).remove_recursively()); } TEST(ring_buffer_open_file, can_forward_to_last_chunk_if_count_is_greater_than_remaining) { const auto source_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); + test::generate_test_file_name("ring_buffer_open_file"); mock_provider mp; @@ -93,12 +93,12 @@ TEST(ring_buffer_open_file, } } - EXPECT_TRUE(utils::file::delete_directory_recursively(ring_buffer_dir)); + EXPECT_TRUE(utils::file::directory(ring_buffer_dir).remove_recursively()); } TEST(ring_buffer_open_file, can_forward_after_last_chunk) { const auto source_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); + test::generate_test_file_name("ring_buffer_open_file"); mock_provider mp; @@ -125,12 +125,12 @@ TEST(ring_buffer_open_file, can_forward_after_last_chunk) { } } - EXPECT_TRUE(utils::file::delete_directory_recursively(ring_buffer_dir)); + EXPECT_TRUE(utils::file::directory(ring_buffer_dir).remove_recursively()); } TEST(ring_buffer_open_file, can_forward_and_rollover_after_last_chunk) { const auto source_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); + test::generate_test_file_name("ring_buffer_open_file"); mock_provider mp; @@ -153,12 +153,12 @@ TEST(ring_buffer_open_file, can_forward_and_rollover_after_last_chunk) { EXPECT_EQ(std::size_t(28u), rb.get_last_chunk()); } - EXPECT_TRUE(utils::file::delete_directory_recursively(ring_buffer_dir)); + EXPECT_TRUE(utils::file::directory(ring_buffer_dir).remove_recursively()); } TEST(ring_buffer_open_file, can_reverse_to_first_chunk) { const auto source_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); + test::generate_test_file_name("ring_buffer_open_file"); mock_provider mp; @@ -184,13 +184,13 @@ TEST(ring_buffer_open_file, can_reverse_to_first_chunk) { } } - EXPECT_TRUE(utils::file::delete_directory_recursively(ring_buffer_dir)); + EXPECT_TRUE(utils::file::directory(ring_buffer_dir).remove_recursively()); } TEST(ring_buffer_open_file, can_reverse_to_first_chunk_if_count_is_greater_than_remaining) { const auto source_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); + test::generate_test_file_name("ring_buffer_open_file"); mock_provider mp; @@ -216,12 +216,12 @@ TEST(ring_buffer_open_file, } } - EXPECT_TRUE(utils::file::delete_directory_recursively(ring_buffer_dir)); + EXPECT_TRUE(utils::file::directory(ring_buffer_dir).remove_recursively()); } TEST(ring_buffer_open_file, can_reverse_before_first_chunk) { const auto source_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); + test::generate_test_file_name("ring_buffer_open_file"); mock_provider mp; @@ -248,12 +248,12 @@ TEST(ring_buffer_open_file, can_reverse_before_first_chunk) { } } - EXPECT_TRUE(utils::file::delete_directory_recursively(ring_buffer_dir)); + EXPECT_TRUE(utils::file::directory(ring_buffer_dir).remove_recursively()); } TEST(ring_buffer_open_file, can_reverse_and_rollover_before_first_chunk) { const auto source_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); + test::generate_test_file_name("ring_buffer_open_file"); mock_provider mp; @@ -284,12 +284,12 @@ TEST(ring_buffer_open_file, can_reverse_and_rollover_before_first_chunk) { } } - EXPECT_TRUE(utils::file::delete_directory_recursively(ring_buffer_dir)); + EXPECT_TRUE(utils::file::directory(ring_buffer_dir).remove_recursively()); } TEST(ring_buffer_open_file, can_reverse_full_ring) { const auto source_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); + test::generate_test_file_name("ring_buffer_open_file"); mock_provider mp; @@ -316,16 +316,14 @@ TEST(ring_buffer_open_file, can_reverse_full_ring) { } } - EXPECT_TRUE(utils::file::delete_directory_recursively(ring_buffer_dir)); + EXPECT_TRUE(utils::file::directory(ring_buffer_dir).remove_recursively()); } TEST(ring_buffer_open_file, read_full_file) { - const auto download_source_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); - auto nf = create_random_file(download_source_path, test_chunk_size * 32u); + auto &nf = test::create_random_file(test_chunk_size * 32u); + const auto download_source_path = nf.get_path(); - const auto dest_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); + const auto dest_path = test::generate_test_file_name("ring_buffer_open_file"); mock_provider mp; @@ -335,8 +333,7 @@ TEST(ring_buffer_open_file, read_full_file) { fsi.directory = false; fsi.api_path = "/test.txt"; fsi.size = test_chunk_size * 32u; - fsi.source_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); + fsi.source_path = test::generate_test_file_name("ring_buffer_open_file"); EXPECT_CALL(mp, read_file_bytes) .WillRepeatedly([&nf](const std::string & /* api_path */, @@ -346,9 +343,8 @@ TEST(ring_buffer_open_file, read_full_file) { EXPECT_FALSE(stop_requested); std::size_t bytes_read{}; data.resize(size); - auto ret = nf->read_bytes(&data[0u], size, offset, bytes_read) - ? api_error::success - : api_error::os_error; + auto ret = nf.read(data, offset, &bytes_read) ? api_error::success + : api_error::os_error; EXPECT_EQ(bytes_read, data.size()); return ret; }); @@ -356,8 +352,9 @@ TEST(ring_buffer_open_file, read_full_file) { file_manager::ring_buffer_open_file rb(ring_buffer_dir, test_chunk_size, 30U, fsi, mp, 8u); - native_file_ptr nf2; - EXPECT_EQ(api_error::success, native_file::create_or_open(dest_path, nf2)); + auto ptr = utils::file::file::open_or_create_file(dest_path); + auto &nf2 = *ptr; + EXPECT_TRUE(nf2); auto to_read = fsi.size; std::size_t chunk = 0u; @@ -367,28 +364,31 @@ TEST(ring_buffer_open_file, read_full_file) { rb.read(test_chunk_size, chunk * test_chunk_size, data)); std::size_t bytes_written{}; - EXPECT_TRUE(nf2->write_bytes(data.data(), data.size(), - chunk * test_chunk_size, bytes_written)); + EXPECT_TRUE(nf2.write(data, chunk * test_chunk_size, &bytes_written)); chunk++; to_read -= data.size(); } - nf2->close(); - nf->close(); + nf2.close(); + nf.close(); - EXPECT_STREQ(utils::file::generate_sha256(download_source_path).c_str(), - utils::file::generate_sha256(dest_path).c_str()); + auto hash1 = utils::file::file(download_source_path).sha256(); + auto hash2 = utils::file::file(dest_path).sha256(); + + EXPECT_TRUE(hash1.has_value()); + EXPECT_TRUE(hash2.has_value()); + if (hash1.has_value() && hash2.has_value()) { + EXPECT_STREQ(hash1.value().c_str(), hash2.value().c_str()); + } } - EXPECT_TRUE(utils::file::delete_directory_recursively(ring_buffer_dir)); + EXPECT_TRUE(utils::file::directory(ring_buffer_dir).remove_recursively()); } TEST(ring_buffer_open_file, read_full_file_in_reverse) { - const auto download_source_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); - auto nf = create_random_file(download_source_path, test_chunk_size * 32u); + auto &nf = test::create_random_file(test_chunk_size * 32u); + const auto download_source_path = nf.get_path(); - const auto dest_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); + const auto dest_path = test::generate_test_file_name("ring_buffer_open_file"); mock_provider mp; @@ -398,8 +398,7 @@ TEST(ring_buffer_open_file, read_full_file_in_reverse) { fsi.directory = false; fsi.api_path = "/test.txt"; fsi.size = test_chunk_size * 32u; - fsi.source_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); + fsi.source_path = test::generate_test_file_name("ring_buffer_open_file"); EXPECT_CALL(mp, read_file_bytes) .WillRepeatedly([&nf](const std::string & /* api_path */, @@ -409,9 +408,8 @@ TEST(ring_buffer_open_file, read_full_file_in_reverse) { EXPECT_FALSE(stop_requested); std::size_t bytes_read{}; data.resize(size); - auto ret = nf->read_bytes(&data[0u], size, offset, bytes_read) - ? api_error::success - : api_error::os_error; + auto ret = nf.read(data, offset, &bytes_read) ? api_error::success + : api_error::os_error; EXPECT_EQ(bytes_read, data.size()); return ret; }); @@ -419,8 +417,9 @@ TEST(ring_buffer_open_file, read_full_file_in_reverse) { file_manager::ring_buffer_open_file rb(ring_buffer_dir, test_chunk_size, 30U, fsi, mp, 8u); - native_file_ptr nf2; - EXPECT_EQ(api_error::success, native_file::create_or_open(dest_path, nf2)); + auto ptr = utils::file::file::open_or_create_file(dest_path); + auto &nf2 = *ptr; + EXPECT_TRUE(nf2); auto to_read = fsi.size; std::size_t chunk = rb.get_total_chunks() - 1u; @@ -430,28 +429,31 @@ TEST(ring_buffer_open_file, read_full_file_in_reverse) { rb.read(test_chunk_size, chunk * test_chunk_size, data)); std::size_t bytes_written{}; - EXPECT_TRUE(nf2->write_bytes(data.data(), data.size(), - chunk * test_chunk_size, bytes_written)); + EXPECT_TRUE(nf2.write(data, chunk * test_chunk_size, &bytes_written)); chunk--; to_read -= data.size(); } - nf2->close(); - nf->close(); + nf2.close(); + nf.close(); - EXPECT_STREQ(utils::file::generate_sha256(download_source_path).c_str(), - utils::file::generate_sha256(dest_path).c_str()); + auto hash1 = utils::file::file(download_source_path).sha256(); + auto hash2 = utils::file::file(dest_path).sha256(); + + EXPECT_TRUE(hash1.has_value()); + EXPECT_TRUE(hash2.has_value()); + if (hash1.has_value() && hash2.has_value()) { + EXPECT_STREQ(hash1.value().c_str(), hash2.value().c_str()); + } } - EXPECT_TRUE(utils::file::delete_directory_recursively(ring_buffer_dir)); + EXPECT_TRUE(utils::file::directory(ring_buffer_dir).remove_recursively()); } TEST(ring_buffer_open_file, read_full_file_in_partial_chunks) { - const auto download_source_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); - auto nf = create_random_file(download_source_path, test_chunk_size * 32u); + auto &nf = test::create_random_file(test_chunk_size * 32u); + const auto download_source_path = nf.get_path(); - const auto dest_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); + const auto dest_path = test::generate_test_file_name("test"); mock_provider mp; @@ -461,8 +463,7 @@ TEST(ring_buffer_open_file, read_full_file_in_partial_chunks) { fsi.directory = false; fsi.api_path = "/test.txt"; fsi.size = test_chunk_size * 32u; - fsi.source_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); + fsi.source_path = test::generate_test_file_name("test"); EXPECT_CALL(mp, read_file_bytes) .WillRepeatedly([&nf](const std::string & /* api_path */, @@ -472,9 +473,8 @@ TEST(ring_buffer_open_file, read_full_file_in_partial_chunks) { EXPECT_FALSE(stop_requested); std::size_t bytes_read{}; data.resize(size); - auto ret = nf->read_bytes(&data[0u], size, offset, bytes_read) - ? api_error::success - : api_error::os_error; + auto ret = nf.read(data, offset, &bytes_read) ? api_error::success + : api_error::os_error; EXPECT_EQ(bytes_read, data.size()); return ret; }); @@ -482,8 +482,11 @@ TEST(ring_buffer_open_file, read_full_file_in_partial_chunks) { file_manager::ring_buffer_open_file rb(ring_buffer_dir, test_chunk_size, 30U, fsi, mp, 8u); - native_file_ptr nf2; - EXPECT_EQ(api_error::success, native_file::create_or_open(dest_path, nf2)); + auto ptr = utils::file::file::open_or_create_file(dest_path); + auto &nf2 = *ptr; + EXPECT_TRUE(nf2); + // EXPECT_EQ(api_error::success, native_file::create_or_open(dest_path, + // nf2)); auto total_read = std::uint64_t(0u); @@ -492,27 +495,31 @@ TEST(ring_buffer_open_file, read_full_file_in_partial_chunks) { EXPECT_EQ(api_error::success, rb.read(3u, total_read, data)); std::size_t bytes_written{}; - EXPECT_TRUE(nf2->write_bytes(data.data(), data.size(), total_read, - bytes_written)); + EXPECT_TRUE( + nf2.write(data.data(), data.size(), total_read, &bytes_written)); total_read += data.size(); } - nf2->close(); - nf->close(); + nf2.close(); + nf.close(); - EXPECT_STREQ(utils::file::generate_sha256(download_source_path).c_str(), - utils::file::generate_sha256(dest_path).c_str()); + auto hash1 = utils::file::file(download_source_path).sha256(); + auto hash2 = utils::file::file(dest_path).sha256(); + + EXPECT_TRUE(hash1.has_value()); + EXPECT_TRUE(hash2.has_value()); + if (hash1.has_value() && hash2.has_value()) { + EXPECT_STREQ(hash1.value().c_str(), hash2.value().c_str()); + } } - EXPECT_TRUE(utils::file::delete_directory_recursively(ring_buffer_dir)); + EXPECT_TRUE(utils::file::directory(ring_buffer_dir).remove_recursively()); } TEST(ring_buffer_open_file, read_full_file_in_partial_chunks_in_reverse) { - const auto download_source_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); - auto nf = create_random_file(download_source_path, test_chunk_size * 32u); + auto &nf = test::create_random_file(test_chunk_size * 32u); + const auto download_source_path = nf.get_path(); - const auto dest_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); + const auto dest_path = test::generate_test_file_name("ring_buffer_open_file"); mock_provider mp; @@ -522,8 +529,7 @@ TEST(ring_buffer_open_file, read_full_file_in_partial_chunks_in_reverse) { fsi.directory = false; fsi.api_path = "/test.txt"; fsi.size = test_chunk_size * 32u; - fsi.source_path = - generate_test_file_name("./test_config", "ring_buffer_open_file"); + fsi.source_path = test::generate_test_file_name("ring_buffer_open_file"); EXPECT_CALL(mp, read_file_bytes) .WillRepeatedly([&nf](const std::string & /* api_path */, @@ -533,9 +539,8 @@ TEST(ring_buffer_open_file, read_full_file_in_partial_chunks_in_reverse) { EXPECT_FALSE(stop_requested); std::size_t bytes_read{}; data.resize(size); - auto ret = nf->read_bytes(&data[0u], size, offset, bytes_read) - ? api_error::success - : api_error::os_error; + auto ret = nf.read(data, offset, &bytes_read) ? api_error::success + : api_error::os_error; EXPECT_EQ(bytes_read, data.size()); return ret; }); @@ -543,11 +548,12 @@ TEST(ring_buffer_open_file, read_full_file_in_partial_chunks_in_reverse) { file_manager::ring_buffer_open_file rb(ring_buffer_dir, test_chunk_size, 30U, fsi, mp, 8u); - native_file_ptr nf2; - EXPECT_EQ(api_error::success, native_file::create_or_open(dest_path, nf2)); + auto ptr = utils::file::file::open_or_create_file(dest_path); + auto &nf2 = *ptr; + EXPECT_TRUE(nf2); - auto total_read = std::uint64_t(0u); - const auto read_size = 3u; + std::uint64_t total_read{0U}; + const auto read_size{3U}; while (total_read < fsi.size) { const auto offset = fsi.size - total_read - read_size; @@ -560,18 +566,23 @@ TEST(ring_buffer_open_file, read_full_file_in_partial_chunks_in_reverse) { (remain >= read_size) ? offset : 0u, data)); std::size_t bytes_written{}; - EXPECT_TRUE(nf2->write_bytes(data.data(), data.size(), - (remain >= read_size) ? offset : 0u, - bytes_written)); + EXPECT_TRUE( + nf2.write(data, (remain >= read_size) ? offset : 0u, &bytes_written)); total_read += data.size(); } - nf2->close(); - nf->close(); + nf2.close(); + nf.close(); - EXPECT_STREQ(utils::file::generate_sha256(download_source_path).c_str(), - utils::file::generate_sha256(dest_path).c_str()); + auto hash1 = utils::file::file(download_source_path).sha256(); + auto hash2 = utils::file::file(dest_path).sha256(); + + EXPECT_TRUE(hash1.has_value()); + EXPECT_TRUE(hash2.has_value()); + if (hash1.has_value() && hash2.has_value()) { + EXPECT_STREQ(hash1.value().c_str(), hash2.value().c_str()); + } } - EXPECT_TRUE(utils::file::delete_directory_recursively(ring_buffer_dir)); + EXPECT_TRUE(utils::file::directory(ring_buffer_dir).remove_recursively()); } } // namespace repertory diff --git a/repertory/repertory_test/src/file_manager_test.cpp b/repertory/repertory_test/src/file_manager_test.cpp index a7020222..b75a6a35 100644 --- a/repertory/repertory_test/src/file_manager_test.cpp +++ b/repertory/repertory_test/src/file_manager_test.cpp @@ -34,7 +34,6 @@ #include "utils/encrypting_reader.hpp" #include "utils/event_capture.hpp" #include "utils/file_utils.hpp" -#include "utils/native_file.hpp" #include "utils/path.hpp" #include "utils/polling.hpp" #include "utils/string.hpp" @@ -43,7 +42,7 @@ namespace repertory { static std::string file_manager_dir = - utils::path::combine("./test_config", {"file_manager_test"}); + utils::path::combine(test::get_test_output_dir(), {"file_manager_test"}); auto file_manager::open(std::shared_ptr of, const open_file_data &ofd, std::uint64_t &handle, @@ -85,7 +84,7 @@ TEST(file_manager, can_start_and_stop) { } event_system::instance().stop(); - // EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + // EXPECT_TRUE(utils::file::remove_directory(file_manager_dir, true)); } TEST(file_manager, can_create_and_close_file) { @@ -118,7 +117,7 @@ TEST(file_manager, can_create_and_close_file) { { std::shared_ptr f; - const auto now = utils::time::get_file_time_now(); + const auto now = utils::time::get_time_now(); auto meta = create_meta_attributes( now, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE, now + 1u, now + 2u, false, 1, "key", 2, now + 3u, 3u, 4u, 0u, source_path, 10, @@ -202,7 +201,7 @@ TEST(file_manager, can_create_and_close_file) { polling::instance().stop(); event_system::instance().stop(); - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, can_open_and_close_file) { @@ -233,7 +232,7 @@ TEST(file_manager, can_open_and_close_file) { std::uint64_t handle{}; { - const auto now = utils::time::get_file_time_now(); + const auto now = utils::time::get_time_now(); auto meta = create_meta_attributes( now, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE, now + 1u, now + 2u, false, 1, "key", 2, now + 3u, 3u, 4u, 0u, source_path, 10, @@ -318,7 +317,7 @@ TEST(file_manager, can_open_and_close_file) { polling::instance().stop(); event_system::instance().stop(); - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, can_open_and_close_multiple_handles_for_same_file) { @@ -340,7 +339,7 @@ TEST(file_manager, can_open_and_close_multiple_handles_for_same_file) { const auto source_path = utils::path::combine( cfg.get_cache_directory(), {utils::create_uuid_string()}); - const auto now = utils::time::get_file_time_now(); + const auto now = utils::time::get_time_now(); auto meta = create_meta_attributes( now, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE, now + 1u, now + 2u, false, 1, "key", 2, now + 3u, 3u, 4u, 0u, source_path, 10, @@ -391,7 +390,7 @@ TEST(file_manager, can_open_and_close_multiple_handles_for_same_file) { polling::instance().stop(); event_system::instance().stop(); - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, download_is_stored_after_write_if_partially_downloaded) { @@ -420,15 +419,14 @@ TEST(file_manager, download_is_stored_after_write_if_partially_downloaded) { event_capture ec({"download_stored"}, {"file_upload_completed", "file_upload_queued"}); - const auto now = utils::time::get_file_time_now(); + const auto now = utils::time::get_time_now(); auto meta = create_meta_attributes( now, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE, now + 1u, now + 2u, false, 1, "key", 2, now + 3u, 3u, 4u, utils::encryption::encrypting_reader::get_data_chunk_size() * 4u, source_path, 10, now + 4u); - auto nf = create_random_file( - generate_test_file_name("./test_config", "file_manage_test"), - utils::string::to_uint64(meta[META_SIZE])); + auto &nf = + test::create_random_file(utils::string::to_uint64(meta[META_SIZE])); EXPECT_CALL(mp, get_filesystem_item) .WillRepeatedly([&meta](const std::string &api_path, bool directory, @@ -465,9 +463,8 @@ TEST(file_manager, download_is_stored_after_write_if_partially_downloaded) { if (offset == 0u) { std::size_t bytes_read{}; data.resize(size); - auto ret = nf->read_bytes(&data[0u], size, offset, bytes_read) - ? api_error::success - : api_error::os_error; + auto ret = nf.read(data, offset, &bytes_read) ? api_error::success + : api_error::os_error; EXPECT_EQ(bytes_read, data.size()); return ret; } @@ -542,11 +539,11 @@ TEST(file_manager, download_is_stored_after_write_if_partially_downloaded) { EXPECT_EQ(std::size_t(0u), fm.get_open_file_count()); EXPECT_EQ(std::size_t(0u), fm.get_open_handle_count()); - nf->close(); + nf.close(); } event_system::instance().stop(); - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, upload_occurs_after_write_if_fully_downloaded) { @@ -583,15 +580,14 @@ TEST(file_manager, upload_occurs_after_write_if_fully_downloaded) { }); event_capture ec({"download_end"}); - const auto now = utils::time::get_file_time_now(); + const auto now = utils::time::get_time_now(); auto meta = create_meta_attributes( now, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE, now + 1u, now + 2u, false, 1, "key", 2, now + 3u, 3u, 4u, utils::encryption::encrypting_reader::get_data_chunk_size() * 4u, source_path, 10, now + 4u); - auto nf = create_random_file( - generate_test_file_name("./test_config", "file_manage_test"), - utils::string::to_uint64(meta[META_SIZE])); + auto &nf = + test::create_random_file(utils::string::to_uint64(meta[META_SIZE])); EXPECT_CALL(mp, get_filesystem_item) .WillRepeatedly([&meta](const std::string &api_path, bool directory, @@ -623,9 +619,8 @@ TEST(file_manager, upload_occurs_after_write_if_fully_downloaded) { stop_type & /* stop_requested */) -> api_error { std::size_t bytes_read{}; data.resize(size); - auto ret = nf->read_bytes(&data[0u], size, offset, bytes_read) - ? api_error::success - : api_error::os_error; + auto ret = nf.read(data, offset, &bytes_read) ? api_error::success + : api_error::os_error; EXPECT_EQ(bytes_read, data.size()); return ret; }); @@ -660,16 +655,16 @@ TEST(file_manager, upload_occurs_after_write_if_fully_downloaded) { fm.stop(); - nf->close(); + nf.close(); } polling::instance().stop(); event_system::instance().stop(); - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, can_evict_file) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { console_consumer c; event_system::instance().start(); @@ -694,7 +689,7 @@ TEST(file_manager, can_evict_file) { const auto source_path = utils::path::combine( cfg.get_cache_directory(), {utils::create_uuid_string()}); - const auto now = utils::time::get_file_time_now(); + const auto now = utils::time::get_time_now(); auto meta = create_meta_attributes( now, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE, now + 1u, now + 2u, @@ -735,15 +730,15 @@ TEST(file_manager, can_evict_file) { std::size_t bytes_written{}; EXPECT_EQ(api_error::success, f->write(0U, data, bytes_written)); - std::uint64_t file_size{}; - EXPECT_TRUE(utils::file::get_file_size(source_path, file_size)); - EXPECT_EQ(static_cast(data.size()), file_size); + auto opt_size = utils::file::file{source_path}.size(); + EXPECT_TRUE(opt_size.has_value()); + EXPECT_EQ(static_cast(data.size()), opt_size.value()); } fm.close(handle); capture.wait_for_empty(); - EXPECT_TRUE(utils::retryable_action( + EXPECT_TRUE(utils::retry_action( [&fm]() -> bool { return not fm.is_processing("/test_evict.txt"); })); EXPECT_CALL(mp, get_item_meta(_, META_SOURCE, _)) @@ -764,17 +759,17 @@ TEST(file_manager, can_evict_file) { return api_error::success; }); EXPECT_TRUE(fm.evict_file("/test_evict.txt")); - EXPECT_FALSE(utils::file::is_file(source_path)); + EXPECT_FALSE(utils::file::file(source_path).exists()); fm.stop(); } event_system::instance().stop(); - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, evict_file_fails_if_file_is_pinned) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); cfg.set_enable_chunk_downloader_timeout(false); @@ -796,11 +791,11 @@ TEST(file_manager, evict_file_fails_if_file_is_pinned) { } event_system::instance().stop(); - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, evict_file_fails_if_provider_is_direct_only) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -812,11 +807,11 @@ TEST(file_manager, evict_file_fails_if_provider_is_direct_only) { EXPECT_FALSE(fm.evict_file("/test.txt")); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, evict_file_fails_if_file_is_open) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -862,12 +857,12 @@ TEST(file_manager, evict_file_fails_if_file_is_open) { fm.close(handle); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, evict_file_fails_if_unable_to_get_source_path_from_item_meta) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -897,11 +892,11 @@ TEST(file_manager, EXPECT_FALSE(fm.evict_file("/test_open.txt")); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, evict_file_fails_if_source_path_is_empty) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -931,11 +926,11 @@ TEST(file_manager, evict_file_fails_if_source_path_is_empty) { EXPECT_FALSE(fm.evict_file("/test_open.txt")); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, evict_file_fails_if_file_is_uploading) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { console_consumer c; event_system::instance().start(); @@ -960,7 +955,7 @@ TEST(file_manager, evict_file_fails_if_file_is_uploading) { const auto source_path = utils::path::combine( cfg.get_cache_directory(), {utils::create_uuid_string()}); - const auto now = utils::time::get_file_time_now(); + const auto now = utils::time::get_time_now(); auto meta = create_meta_attributes( now, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE, now + 1u, now + 2u, @@ -1008,29 +1003,30 @@ TEST(file_manager, evict_file_fails_if_file_is_uploading) { std::size_t bytes_written{}; EXPECT_EQ(api_error::success, f->write(0U, data, bytes_written)); - std::uint64_t file_size{}; - EXPECT_TRUE(utils::file::get_file_size(source_path, file_size)); - EXPECT_EQ(static_cast(data.size()), file_size); + auto opt_size = utils::file::file{source_path}.size(); + EXPECT_TRUE(opt_size.has_value()); + EXPECT_EQ(static_cast(data.size()), opt_size.value()); + fm.close(handle); - EXPECT_TRUE(utils::retryable_action( + EXPECT_TRUE(utils::retry_action( [&fm]() -> bool { return fm.is_processing("/test_evict.txt"); })); EXPECT_FALSE(fm.evict_file("/test_evict.txt")); } capture.wait_for_empty(); - EXPECT_TRUE(utils::file::is_file(source_path)); + EXPECT_TRUE(utils::file::file(source_path).exists()); fm.stop(); } event_system::instance().stop(); - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, evict_file_fails_if_file_is_in_upload_queue) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -1051,11 +1047,11 @@ TEST(file_manager, evict_file_fails_if_file_is_in_upload_queue) { EXPECT_FALSE(fm.evict_file("/test_evict.txt")); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, evict_file_fails_if_file_is_modified) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -1097,11 +1093,11 @@ TEST(file_manager, evict_file_fails_if_file_is_modified) { EXPECT_FALSE(fm.evict_file("/test_evict.txt")); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, evict_file_fails_if_file_is_not_complete) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -1145,11 +1141,11 @@ TEST(file_manager, evict_file_fails_if_file_is_not_complete) { EXPECT_FALSE(fm.evict_file("/test_evict.txt")); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, can_get_directory_items) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -1179,11 +1175,11 @@ TEST(file_manager, can_get_directory_items) { EXPECT_EQ(std::size_t(2U), list.size()); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, file_is_not_opened_if_provider_create_file_fails) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -1192,7 +1188,7 @@ TEST(file_manager, file_is_not_opened_if_provider_create_file_fails) { EXPECT_CALL(mp, is_direct_only()).WillRepeatedly(Return(false)); - const auto now = utils::time::get_file_time_now(); + const auto now = utils::time::get_time_now(); auto meta = create_meta_attributes( now, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE, now + 1u, now + 2u, false, 1, "", 2, now + 3u, 3u, 4u, 0u, "/test_create.src", 10, @@ -1215,11 +1211,11 @@ TEST(file_manager, file_is_not_opened_if_provider_create_file_fails) { EXPECT_EQ(std::size_t(0U), fm.get_open_file_count()); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, create_fails_if_provider_create_is_unsuccessful) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -1246,12 +1242,12 @@ TEST(file_manager, create_fails_if_provider_create_is_unsuccessful) { EXPECT_EQ(std::size_t(0U), fm.get_open_file_count()); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, get_open_file_fails_if_file_is_not_open) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -1270,12 +1266,12 @@ TEST(file_manager, get_open_file_fails_if_file_is_not_open) { EXPECT_FALSE(f); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, get_open_file_promotes_non_writeable_file_if_writeable_is_specified) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -1355,11 +1351,11 @@ TEST(file_manager, EXPECT_EQ(std::size_t(1U), fm.get_open_file_count()); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, open_file_fails_if_file_is_not_found) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -1380,11 +1376,11 @@ TEST(file_manager, open_file_fails_if_file_is_not_found) { EXPECT_EQ(std::size_t(0U), fm.get_open_file_count()); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, open_file_fails_if_provider_get_filesystem_item_fails) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -1424,11 +1420,11 @@ TEST(file_manager, open_file_fails_if_provider_get_filesystem_item_fails) { EXPECT_EQ(std::size_t(0U), fm.get_open_file_count()); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, open_file_fails_if_provider_set_item_meta_fails) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -1475,11 +1471,11 @@ TEST(file_manager, open_file_fails_if_provider_set_item_meta_fails) { EXPECT_EQ(std::size_t(0U), fm.get_open_file_count()); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, open_file_creates_source_path_if_empty) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -1538,11 +1534,11 @@ TEST(file_manager, open_file_creates_source_path_if_empty) { EXPECT_EQ(std::size_t(1U), fm.get_open_file_count()); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, open_file_first_file_handle_is_not_zero) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -1586,11 +1582,11 @@ TEST(file_manager, open_file_first_file_handle_is_not_zero) { EXPECT_GT(handle, std::uint64_t(0U)); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, can_remove_file) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -1601,11 +1597,11 @@ TEST(file_manager, can_remove_file) { file_manager fm(cfg, mp); - native_file::native_file_ptr f{}; - EXPECT_EQ(api_error::success, - native_file::create_or_open("./test_remove.txt", f)); - f->close(); - EXPECT_TRUE(utils::file::is_file("./test_remove.txt")); + { + auto file = utils::file::file::open_or_create_file("./test_remove.txt"); + EXPECT_TRUE(*file); + } + EXPECT_TRUE(utils::file::file("./test_remove.txt").exists()); EXPECT_CALL(mp, get_filesystem_item) .WillOnce([](const std::string &api_path, bool directory, @@ -1624,14 +1620,14 @@ TEST(file_manager, can_remove_file) { EXPECT_EQ(api_error::success, fm.remove_file("/test_remove.txt")); - EXPECT_FALSE(utils::file::is_file("./test_remove.txt")); + EXPECT_FALSE(utils::file::file("./test_remove.txt").exists()); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, can_queue_and_remove_upload) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); console_consumer c; event_system::instance().start(); @@ -1662,11 +1658,11 @@ TEST(file_manager, can_queue_and_remove_upload) { } event_system::instance().stop(); - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, remove_file_fails_if_open_file_is_modified) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -1716,7 +1712,7 @@ TEST(file_manager, remove_file_fails_if_open_file_is_modified) { EXPECT_EQ(api_error::file_in_use, fm.remove_file("/test_remove.txt")); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, file_is_closed_after_download_timeout) { @@ -1744,7 +1740,7 @@ TEST(file_manager, file_is_closed_after_download_timeout) { ee.get_api_path().get().c_str()); }); - const auto now = utils::time::get_file_time_now(); + const auto now = utils::time::get_time_now(); auto meta = create_meta_attributes( now, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE, now + 1u, now + 2u, false, 1, "key", 2, now + 3u, 3u, 4u, @@ -1814,11 +1810,11 @@ TEST(file_manager, file_is_closed_after_download_timeout) { } event_system::instance().stop(); - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, remove_file_fails_if_file_does_not_exist) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); cfg.set_enable_chunk_downloader_timeout(false); @@ -1839,11 +1835,11 @@ TEST(file_manager, remove_file_fails_if_file_does_not_exist) { EXPECT_EQ(api_error::item_not_found, fm.remove_file("/test_remove.txt")); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } TEST(file_manager, remove_file_fails_if_provider_remove_file_fails) { - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); { app_config cfg(provider_type::sia, file_manager_dir); @@ -1871,6 +1867,6 @@ TEST(file_manager, remove_file_fails_if_provider_remove_file_fails) { EXPECT_EQ(api_error::item_not_found, fm.remove_file("/test_remove.txt")); } - EXPECT_TRUE(utils::file::delete_directory_recursively(file_manager_dir)); + EXPECT_TRUE(utils::file::directory(file_manager_dir).remove_recursively()); } } // namespace repertory diff --git a/repertory/repertory_test/src/file_manager_upload_test.cpp b/repertory/repertory_test/src/file_manager_upload_test.cpp index deb3cce2..b55af6ec 100644 --- a/repertory/repertory_test/src/file_manager_upload_test.cpp +++ b/repertory/repertory_test/src/file_manager_upload_test.cpp @@ -35,8 +35,7 @@ TEST(upload, can_upload_a_valid_file) { event_system::instance().start(); - const auto source_path = - generate_test_file_name("./test_config", "upload_test"); + const auto source_path = test::generate_test_file_name("upload_test"); mock_provider mp; @@ -79,8 +78,7 @@ TEST(upload, can_cancel_upload) { event_system::instance().start(); - const auto source_path = - generate_test_file_name("./test_config", "upload_test"); + const auto source_path = test::generate_test_file_name("upload_test"); mock_provider mp; @@ -145,8 +143,7 @@ TEST(upload, can_stop_upload) { event_system::instance().start(); - const auto source_path = - generate_test_file_name("./test_config", "upload_test"); + const auto source_path = test::generate_test_file_name("upload_test"); mock_provider mp; diff --git a/repertory/repertory_test/src/fuse_drive_test.cpp b/repertory/repertory_test/src/fuse_drive_test.cpp index cc78ff84..ed79c146 100644 --- a/repertory/repertory_test/src/fuse_drive_test.cpp +++ b/repertory/repertory_test/src/fuse_drive_test.cpp @@ -21,115 +21,515 @@ */ #if !defined(_WIN32) -#include "test_common.hpp" +#include "fixtures/fuse_fixture.hpp" -#include "app_config.hpp" -#include "comm/curl/curl_comm.hpp" -#include "drives/fuse/fuse_drive.hpp" -#include "platform/platform.hpp" -#include "providers/s3/s3_provider.hpp" -#include "providers/sia/sia_provider.hpp" -#include "types/repertory.hpp" -#include "utils/event_capture.hpp" -#include "utils/file_utils.hpp" -#include "utils/utils.hpp" - -#if !defined(ACCESSPERMS) -#define ACCESSPERMS (S_IRWXU | S_IRWXG | S_IRWXO) /* 0777 */ -#endif - -static constexpr const auto SLEEP_SECONDS = 1.5s; +// #include "app_config.hpp" +// #include "comm/curl/curl_comm.hpp" +// #include "drives/fuse/fuse_drive.hpp" +// #include "platform/platform.hpp" +// #include "providers/s3/s3_provider.hpp" +// #include "providers/sia/sia_provider.hpp" +// #include "types/repertory.hpp" +// #include "utils/event_capture.hpp" +// #include "utils/file_utils.hpp" +// #include "utils/utils.hpp" namespace repertory { -static void execute_mount(const std::string &data_directory, - const std::vector &drive_args, - std::thread &mount_thread) { - auto cmd = "./repertory -dd \"" + data_directory + "\"" + " " + - utils::string::join(drive_args, ' '); - std::cout << cmd << std::endl; - EXPECT_EQ(0, system(cmd.c_str())); - mount_thread.join(); -} +// static void rmdir_and_test(const std::string &directory_path) { +// std::cout << __FUNCTION__ << std::endl; +// int ret = 0; +// for (auto i = 0; ((ret = rmdir(directory_path.c_str())) != 0) && (i < 20); +// i++) { +// std::this_thread::sleep_for(100ms); +// } +// +// EXPECT_EQ(0, ret); +// std::this_thread::sleep_for(SLEEP_SECONDS); +// +// EXPECT_FALSE(utils::file::is_directory(directory_path)); +// EXPECT_FALSE(utils::file::is_file(directory_path)); +// } +// +// static void test_mkdir(const std::string & /* api_path */, +// const std::string &directory_path) { +// std::cout << __FUNCTION__ << std::endl; +// EXPECT_EQ(0, mkdir(directory_path.c_str(), +// S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP)); +// +// EXPECT_TRUE(utils::file::is_directory(directory_path)); +// EXPECT_FALSE(utils::file::is_file(directory_path)); +// +// struct stat64 unix_st {}; +// stat64(directory_path.c_str(), &unix_st); +// +// EXPECT_EQ(getuid(), unix_st.st_uid); +// EXPECT_EQ(getgid(), unix_st.st_gid); +// +// EXPECT_EQ(static_cast(S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP +// | +// S_IXGRP), +// ACCESSPERMS & unix_st.st_mode); +// } +// +// static void test_write_and_read(const std::string & /* api_path */, +// const std::string &file_path) { +// std::cout << __FUNCTION__ << std::endl; +// auto fd = +// open(file_path.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP); +// EXPECT_LE(1, fd); +// +// std::string data = "TestData"; +// EXPECT_EQ(data.size(), +// static_cast(write(fd, data.data(), data.size()))); +// EXPECT_EQ(0, lseek(fd, 0, SEEK_SET)); +// fsync(fd); +// +// data_buffer read_data; +// read_data.resize(data.size()); +// EXPECT_EQ(data.size(), static_cast( +// read(fd, read_data.data(), read_data.size()))); +// +// EXPECT_EQ(0, memcmp(data.data(), read_data.data(), data.size())); +// +// EXPECT_EQ(0, close(fd)); +// +// std::this_thread::sleep_for(SLEEP_SECONDS); +// +// std::uint64_t file_size{}; +// EXPECT_TRUE(utils::file::get_file_size(file_path, file_size)); +// EXPECT_EQ(data.size(), file_size); +// +// // filesystem_item fsi{}; +// // EXPECT_EQ(api_error::success, +// // provider.get_filesystem_item(api_path, false, fsi)); +// // EXPECT_TRUE(utils::file::get_file_size(fsi.source_path, file_size)); +// // EXPECT_EQ(data.size(), file_size); +// } +// +// static void test_rename_file(const std::string &from_file_path, +// const std::string &to_file_path, +// bool is_rename_supported) { +// std::cout << __FUNCTION__ << std::endl; +// auto fd = open(from_file_path.c_str(), O_RDWR, S_IRUSR | S_IWUSR | +// S_IRGRP); EXPECT_LE(1, fd); close(fd); +// +// std::this_thread::sleep_for(SLEEP_SECONDS); +// +// if (is_rename_supported) { +// EXPECT_EQ(0, rename(from_file_path.c_str(), to_file_path.c_str())); +// EXPECT_FALSE(utils::file::is_file(from_file_path)); +// EXPECT_TRUE(utils::file::is_file(to_file_path)); +// } else { +// EXPECT_EQ(-1, rename(from_file_path.c_str(), to_file_path.c_str())); +// EXPECT_TRUE(utils::file::is_file(from_file_path)); +// EXPECT_FALSE(utils::file::is_file(to_file_path)); +// } +// } +// +// static void test_rename_directory(const std::string &from_dir_path, +// const std::string &to_dir_path, +// bool is_rename_supported) { +// std::cout << __FUNCTION__ << std::endl; +// EXPECT_EQ(0, mkdir(from_dir_path.c_str(), +// S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP)); +// std::this_thread::sleep_for(SLEEP_SECONDS); +// +// EXPECT_TRUE(utils::file::is_directory(from_dir_path)); +// if (is_rename_supported) { +// EXPECT_EQ(0, rename(from_dir_path.c_str(), to_dir_path.c_str())); +// EXPECT_FALSE(utils::file::is_directory(from_dir_path)); +// EXPECT_TRUE(utils::file::is_directory(to_dir_path)); +// } else { +// EXPECT_EQ(-1, rename(from_dir_path.c_str(), to_dir_path.c_str())); +// EXPECT_TRUE(utils::file::is_directory(from_dir_path)); +// EXPECT_FALSE(utils::file::is_directory(to_dir_path)); +// } +// } +// +// static void test_truncate(const std::string &file_path) { +// std::cout << __FUNCTION__ << std::endl; +// EXPECT_EQ(0, truncate(file_path.c_str(), 10u)); +// +// std::uint64_t file_size{}; +// EXPECT_TRUE(utils::file::get_file_size(file_path, file_size)); +// +// EXPECT_EQ(std::uint64_t(10u), file_size); +// } +// +// static void test_ftruncate(const std::string &file_path) { +// std::cout << __FUNCTION__ << std::endl; +// auto fd = open(file_path.c_str(), O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP); +// EXPECT_LE(1, fd); +// +// EXPECT_EQ(0, ftruncate(fd, 10u)); +// +// std::uint64_t file_size{}; +// EXPECT_TRUE(utils::file::get_file_size(file_path, file_size)); +// +// EXPECT_EQ(std::uint64_t(10u), file_size); +// +// close(fd); +// } +// +// #if !defined(__APPLE__) +// static void test_fallocate(const std::string & /* api_path */, +// const std::string &file_path) { +// std::cout << __FUNCTION__ << std::endl; +// auto file = +// open(file_path.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP); +// EXPECT_LE(1, file); +// EXPECT_EQ(0, fallocate(file, 0, 0, 16)); +// +// std::uint64_t file_size{}; +// EXPECT_TRUE(utils::file::get_file_size(file_path, file_size)); +// EXPECT_EQ(16U, file_size); +// +// EXPECT_EQ(0, close(file)); +// +// file_size = 0U; +// EXPECT_TRUE(utils::file::get_file_size(file_path, file_size)); +// EXPECT_EQ(16U, file_size); +// } +// #endif +// +// static void test_file_getattr(const std::string & /* api_path */, +// const std::string &file_path) { +// std::cout << __FUNCTION__ << std::endl; +// auto fd = +// open(file_path.c_str(), O_CREAT | O_RDONLY, S_IRUSR | S_IWUSR | +// S_IRGRP); +// EXPECT_LE(1, fd); +// +// EXPECT_EQ(0, close(fd)); +// +// struct stat64 unix_st {}; +// EXPECT_EQ(0, stat64(file_path.c_str(), &unix_st)); +// EXPECT_EQ(static_cast(S_IRUSR | S_IWUSR | S_IRGRP), +// ACCESSPERMS & unix_st.st_mode); +// EXPECT_FALSE(S_ISDIR(unix_st.st_mode)); +// EXPECT_TRUE(S_ISREG(unix_st.st_mode)); +// } +// +// static void test_directory_getattr(const std::string & /* api_path */, +// const std::string &directory_path) { +// std::cout << __FUNCTION__ << std::endl; +// EXPECT_EQ(0, mkdir(directory_path.c_str(), +// S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP)); +// +// struct stat64 unix_st {}; +// EXPECT_EQ(0, stat64(directory_path.c_str(), &unix_st)); +// EXPECT_EQ(static_cast(S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP +// | +// S_IXGRP), +// ACCESSPERMS & unix_st.st_mode); +// EXPECT_TRUE(S_ISDIR(unix_st.st_mode)); +// EXPECT_FALSE(S_ISREG(unix_st.st_mode)); +// } +// +// static void +// test_write_operations_fail_if_read_only(const std::string & /* api_path */, +// const std::string &file_path) { +// std::cout << __FUNCTION__ << std::endl; +// auto fd = +// open(file_path.c_str(), O_CREAT | O_RDONLY, S_IRUSR | S_IWUSR | +// S_IRGRP); +// EXPECT_LE(1, fd); +// +// std::string data = "TestData"; +// EXPECT_EQ(-1, write(fd, data.data(), data.size())); +// +// EXPECT_EQ(-1, ftruncate(fd, 9u)); +// +// #if !defined(__APPLE__) +// EXPECT_EQ(-1, fallocate(fd, 0, 0, 16)); +// #endif +// +// EXPECT_EQ(0, close(fd)); +// +// std::this_thread::sleep_for(SLEEP_SECONDS); +// +// std::uint64_t file_size{}; +// EXPECT_TRUE(utils::file::get_file_size(file_path, file_size)); +// EXPECT_EQ(std::size_t(0u), file_size); +// +// // filesystem_item fsi{}; +// // EXPECT_EQ(api_error::success, +// // provider.get_filesystem_item(api_path, false, fsi)); +// // EXPECT_TRUE(utils::file::get_file_size(fsi.source_path, file_size)); +// // EXPECT_EQ(std::size_t(0u), file_size); +// } +// +// #if !__APPLE__ && HAS_SETXATTR +// static void test_xattr_invalid_parameters(const std::string &file_path) { +// std::cout << __FUNCTION__ << std::endl; +// std::string attr = "moose"; +// EXPECT_EQ(-1, setxattr(nullptr, "user.test_attr", attr.data(), attr.size(), +// XATTR_CREATE)); +// EXPECT_EQ(errno, EFAULT); +// EXPECT_EQ(-1, setxattr(file_path.c_str(), nullptr, attr.data(), +// attr.size(), +// XATTR_CREATE)); +// EXPECT_EQ(errno, EFAULT); +// EXPECT_EQ(-1, setxattr(file_path.c_str(), "user.test_attr", nullptr, +// attr.size(), XATTR_CREATE)); +// EXPECT_EQ(errno, EFAULT); +// EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", nullptr, 0, +// XATTR_CREATE)); +// } +// +// static void test_xattr_create_and_get(const std::string &file_path) { +// std::cout << __FUNCTION__ << std::endl; +// std::string attr = "moose"; +// EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", attr.data(), +// attr.size(), XATTR_CREATE)); +// +// std::string val; +// val.resize(attr.size()); +// EXPECT_EQ(attr.size(), +// static_cast(getxattr( +// file_path.c_str(), "user.test_attr", val.data(), +// val.size()))); +// EXPECT_STREQ(attr.c_str(), val.c_str()); +// } +// +// static void test_xattr_listxattr(const std::string &file_path) { +// std::cout << __FUNCTION__ << std::endl; +// std::string attr = "moose"; +// EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", attr.data(), +// attr.size(), XATTR_CREATE)); +// EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr2", attr.data(), +// attr.size(), XATTR_CREATE)); +// +// std::string val; +// val.resize(attr.size()); +// EXPECT_EQ(attr.size(), +// static_cast(getxattr( +// file_path.c_str(), "user.test_attr", val.data(), +// val.size()))); +// EXPECT_STREQ(attr.c_str(), val.c_str()); +// +// std::string data; +// auto size = listxattr(file_path.c_str(), data.data(), 0U); +// EXPECT_EQ(31, size); +// +// data.resize(static_cast(size)); +// EXPECT_EQ(size, listxattr(file_path.c_str(), data.data(), +// static_cast(size))); +// +// auto *ptr = data.data(); +// EXPECT_STREQ("user.test_attr", ptr); +// +// ptr += strlen(ptr) + 1; +// EXPECT_STREQ("user.test_attr2", ptr); +// } +// +// static void test_xattr_replace(const std::string &file_path) { +// std::cout << __FUNCTION__ << std::endl; +// std::string attr = "moose"; +// EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", attr.data(), +// attr.size(), XATTR_CREATE)); +// +// attr = "cow"; +// EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", attr.data(), +// attr.size(), XATTR_REPLACE)); +// +// std::string val; +// val.resize(attr.size()); +// EXPECT_EQ(attr.size(), +// static_cast(getxattr( +// file_path.c_str(), "user.test_attr", val.data(), +// val.size()))); +// EXPECT_STREQ(attr.c_str(), val.c_str()); +// } +// +// static void test_xattr_default_create(const std::string &file_path) { +// std::cout << __FUNCTION__ << std::endl; +// std::string attr = "moose"; +// EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", attr.data(), +// attr.size(), 0)); +// +// std::string val; +// val.resize(attr.size()); +// EXPECT_EQ(attr.size(), +// static_cast(getxattr( +// file_path.c_str(), "user.test_attr", val.data(), +// val.size()))); +// EXPECT_STREQ(attr.c_str(), val.c_str()); +// } +// +// static void test_xattr_default_replace(const std::string &file_path) { +// std::cout << __FUNCTION__ << std::endl; +// std::string attr = "moose"; +// EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", attr.data(), +// attr.size(), 0)); +// +// attr = "cow"; +// EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", attr.data(), +// attr.size(), 0)); +// +// std::string val; +// val.resize(attr.size()); +// EXPECT_EQ(attr.size(), +// static_cast(getxattr( +// file_path.c_str(), "user.test_attr", val.data(), +// val.size()))); +// EXPECT_STREQ(attr.c_str(), val.c_str()); +// } +// +// static void test_xattr_create_fails_if_exists(const std::string &file_path) { +// std::cout << __FUNCTION__ << std::endl; +// std::string attr = "moose"; +// EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", attr.data(), +// attr.size(), 0)); +// EXPECT_EQ(-1, setxattr(file_path.c_str(), "user.test_attr", attr.data(), +// attr.size(), XATTR_CREATE)); +// EXPECT_EQ(EEXIST, errno); +// } +// +// static void +// test_xattr_create_fails_if_not_exists(const std::string &file_path) { +// std::cout << __FUNCTION__ << std::endl; +// std::string attr = "moose"; +// EXPECT_EQ(-1, setxattr(file_path.c_str(), "user.test_attr", attr.data(), +// attr.size(), XATTR_REPLACE)); +// EXPECT_EQ(ENODATA, errno); +// } +// +// static void test_xattr_removexattr(const std::string &file_path) { +// std::cout << __FUNCTION__ << std::endl; +// std::string attr = "moose"; +// EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", attr.data(), +// attr.size(), XATTR_CREATE)); +// +// EXPECT_EQ(0, removexattr(file_path.c_str(), "user.test_attr")); +// +// std::string val; +// val.resize(attr.size()); +// EXPECT_EQ(-1, getxattr(file_path.c_str(), "user.test_attr", val.data(), +// val.size())); +// EXPECT_EQ(ENODATA, errno); +// } +// #endif +// +// // file_path = create_file_and_test(mount_location, "chown_test"); +// // test_chown(utils::path::create_api_path("chown_test"), file_path); +// // unlink_file_and_test(file_path); +// // +// // file_path = utils::path::combine(mount_location, {"mkdir_test"}); +// // test_mkdir(utils::path::create_api_path("mkdir_test"), file_path); +// // rmdir_and_test(file_path); +// // +// // file_path = create_file_and_test(mount_location, +// // "write_read_test"); +// // test_write_and_read(utils::path::create_api_path("write_read_test"), +// // file_path); +// // unlink_file_and_test(file_path); +// // +// // file_path = +// // create_file_and_test(mount_location, "from_rename_file_test"); +// // auto to_file_path = +// // utils::path::combine(mount_location, {"to_rename_file_test"}); +// // test_rename_file(file_path, to_file_path, +// // provider_ptr->is_rename_supported()); +// // EXPECT_TRUE(utils::file::file(file_path).remove()); +// // EXPECT_TRUE(utils::file::file(to_file_path).remove()); +// // +// // file_path = +// // utils::path::combine(mount_location, +// {"from_rename_dir_test"}); +// // to_file_path = +// // utils::path::combine(mount_location, {"to_rename_dir_test"}); +// // test_rename_directory(file_path, to_file_path, +// // provider_ptr->is_rename_supported()); +// // EXPECT_TRUE(utils::file::retry_delete_directory(file_path.c_str())); +// // EXPECT_TRUE(utils::file::retry_delete_directory(to_file_path.c_str())); +// // +// // file_path = create_file_and_test(mount_location, +// // "truncate_file_test"); test_truncate(file_path); +// // unlink_file_and_test(file_path); +// // +// // file_path = create_file_and_test(mount_location, +// // "ftruncate_file_test"); test_ftruncate(file_path); +// // unlink_file_and_test(file_path); +// // +// // #if !defined(__APPLE__) +// // file_path = create_file_and_test(mount_location, +// // "fallocate_file_test"); +// // test_fallocate(utils::path::create_api_path("fallocate_file_test"), +// // file_path); +// // unlink_file_and_test(file_path); +// // #endif +// // +// // file_path = create_file_and_test(mount_location, +// // "write_fails_ro_test"); test_write_operations_fail_if_read_only( +// // utils::path::create_api_path("write_fails_ro_test"), +// // file_path); +// // unlink_file_and_test(file_path); +// // +// // file_path = create_file_and_test(mount_location, "getattr.txt"); +// // test_file_getattr(utils::path::create_api_path("getattr.txt"), +// // file_path); +// // unlink_file_and_test(file_path); +// // +// // file_path = utils::path::combine(mount_location, {"getattr_dir"}); +// // test_directory_getattr(utils::path::create_api_path("getattr_dir"), +// // file_path); +// // rmdir_and_test(file_path); +// // +// // #if !__APPLE__ && HAS_SETXATTR +// // file_path = +// // create_file_and_test(mount_location, +// // "xattr_invalid_names_test"); +// // test_xattr_invalid_parameters(file_path); +// // unlink_file_and_test(file_path); +// // +// // file_path = +// // create_file_and_test(mount_location, "xattr_create_get_test"); +// // test_xattr_create_and_get(file_path); +// // unlink_file_and_test(file_path); +// // +// // file_path = +// // create_file_and_test(mount_location, "xattr_listxattr_test"); +// // test_xattr_listxattr(file_path); +// // unlink_file_and_test(file_path); +// // +// // file_path = create_file_and_test(mount_location, +// // "xattr_replace_test"); test_xattr_replace(file_path); +// // unlink_file_and_test(file_path); +// // +// // file_path = +// // create_file_and_test(mount_location, +// // "xattr_default_create_test"); +// // test_xattr_default_create(file_path); +// // unlink_file_and_test(file_path); +// // +// // file_path = +// // create_file_and_test(mount_location, +// // "xattr_default_replace_test"); +// // test_xattr_default_replace(file_path); +// // unlink_file_and_test(file_path); +// // +// // file_path = create_file_and_test(mount_location, +// // "xattr_create_fails_exists_test"); +// // test_xattr_create_fails_if_exists(file_path); +// // unlink_file_and_test(file_path); +// // +// // file_path = create_file_and_test(mount_location, +// // "xattr_create_fails_not_exists_test"); +// // test_xattr_create_fails_if_not_exists(file_path); +// // unlink_file_and_test(file_path); +// // +// // file_path = +// // create_file_and_test(mount_location, +// "xattr_removexattr_test"); +// // test_xattr_removexattr(file_path); +// // unlink_file_and_test(file_path); +// // #endif -static void execute_unmount(const std::string &mount_location) { - // filesystem_item fsi{}; - // EXPECT_EQ(api_error::success, provider.get_filesystem_item("/", true, - // fsi)); EXPECT_STREQ("/", fsi.api_path.c_str()); - // EXPECT_TRUE(fsi.api_parent.empty()); - // EXPECT_TRUE(fsi.directory); - // EXPECT_EQ(std::uint64_t(0u), fsi.size); - // EXPECT_TRUE(fsi.source_path.empty()); - // - // api_meta_map meta{}; - // EXPECT_EQ(api_error::success, provider.get_item_meta("/", meta)); - // for (const auto &kv : meta) { - // std::cout << kv.first << '=' << kv.second << std::endl; - // } +TYPED_TEST_CASE(fuse_test, fuse_provider_types); - auto unmounted = false; - for (int i = 0; not unmounted && (i < 50); i++) { - unmounted = (fuse_base::unmount(mount_location) == 0); - if (not unmounted) { - std::this_thread::sleep_for(100ms); - } - } - - EXPECT_TRUE(unmounted); -} - -static auto create_file_and_test(const std::string &mount_location, - std::string name) -> std::string { - std::cout << __FUNCTION__ << std::endl; - auto file_path = utils::path::combine(mount_location, {name}); - - auto fd = - open(file_path.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP); - EXPECT_LE(1, fd); - EXPECT_TRUE(utils::file::is_file(file_path)); - EXPECT_FALSE(utils::file::is_directory(file_path)); - - std::uint64_t file_size{}; - EXPECT_TRUE(utils::file::get_file_size(file_path, file_size)); - EXPECT_EQ(0u, file_size); - - EXPECT_EQ(0, close(fd)); - std::this_thread::sleep_for(SLEEP_SECONDS); - - return file_path; -} - -static void rmdir_and_test(const std::string &directory_path) { - std::cout << __FUNCTION__ << std::endl; - int ret = 0; - for (auto i = 0; ((ret = rmdir(directory_path.c_str())) != 0) && (i < 20); - i++) { - std::this_thread::sleep_for(100ms); - } - - EXPECT_EQ(0, ret); - std::this_thread::sleep_for(SLEEP_SECONDS); - - EXPECT_FALSE(utils::file::is_directory(directory_path)); - EXPECT_FALSE(utils::file::is_file(directory_path)); -} - -static void unlink_file_and_test(const std::string &file_path) { - std::cout << __FUNCTION__ << std::endl; - int ret = 0; - for (auto i = 0; ((ret = unlink(file_path.c_str())) != 0) && (i < 20); i++) { - std::this_thread::sleep_for(100ms); - } - - EXPECT_EQ(0, ret); - - std::this_thread::sleep_for(SLEEP_SECONDS); - EXPECT_FALSE(utils::file::is_directory(file_path)); - EXPECT_FALSE(utils::file::is_file(file_path)); -} - -static void test_chmod(const std::string & /* api_path */, - const std::string &file_path) { - std::cout << __FUNCTION__ << std::endl; +TYPED_TEST(fuse_test, chmod) { + auto file_path = this->create_file_and_test("chmod_test"); EXPECT_EQ(0, chmod(file_path.c_str(), S_IRUSR | S_IWUSR)); std::this_thread::sleep_for(SLEEP_SECONDS); @@ -137,11 +537,11 @@ static void test_chmod(const std::string & /* api_path */, stat64(file_path.c_str(), &unix_st); EXPECT_EQ(static_cast(S_IRUSR | S_IWUSR), ACCESSPERMS & unix_st.st_mode); + this->unlink_file_and_test(file_path); } -static void test_chown(const std::string & /* api_path */, - const std::string &file_path) { - std::cout << __FUNCTION__ << std::endl; +TYPED_TEST(fuse_test, chown) { + auto file_path = this->create_file_and_test("chown_test"); EXPECT_EQ(0, chown(file_path.c_str(), static_cast(-1), 0)); std::this_thread::sleep_for(SLEEP_SECONDS); @@ -154,548 +554,9 @@ static void test_chown(const std::string & /* api_path */, stat64(file_path.c_str(), &unix_st); EXPECT_EQ(0U, unix_st.st_gid); -} -static void test_mkdir(const std::string & /* api_path */, - const std::string &directory_path) { - std::cout << __FUNCTION__ << std::endl; - EXPECT_EQ(0, mkdir(directory_path.c_str(), - S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP)); - - EXPECT_TRUE(utils::file::is_directory(directory_path)); - EXPECT_FALSE(utils::file::is_file(directory_path)); - - struct stat64 unix_st {}; - stat64(directory_path.c_str(), &unix_st); - - EXPECT_EQ(getuid(), unix_st.st_uid); - EXPECT_EQ(getgid(), unix_st.st_gid); - - EXPECT_EQ(static_cast(S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | - S_IXGRP), - ACCESSPERMS & unix_st.st_mode); -} - -static void test_write_and_read(const std::string & /* api_path */, - const std::string &file_path) { - std::cout << __FUNCTION__ << std::endl; - auto fd = - open(file_path.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP); - EXPECT_LE(1, fd); - - std::string data = "TestData"; - EXPECT_EQ(data.size(), - static_cast(write(fd, data.data(), data.size()))); - EXPECT_EQ(0, lseek(fd, 0, SEEK_SET)); - fsync(fd); - - data_buffer read_data; - read_data.resize(data.size()); - EXPECT_EQ(data.size(), static_cast( - read(fd, read_data.data(), read_data.size()))); - - EXPECT_EQ(0, memcmp(data.data(), read_data.data(), data.size())); - - EXPECT_EQ(0, close(fd)); - - std::this_thread::sleep_for(SLEEP_SECONDS); - - std::uint64_t file_size{}; - EXPECT_TRUE(utils::file::get_file_size(file_path, file_size)); - EXPECT_EQ(data.size(), file_size); - - // filesystem_item fsi{}; - // EXPECT_EQ(api_error::success, - // provider.get_filesystem_item(api_path, false, fsi)); - // EXPECT_TRUE(utils::file::get_file_size(fsi.source_path, file_size)); - // EXPECT_EQ(data.size(), file_size); -} - -static void test_rename_file(const std::string &from_file_path, - const std::string &to_file_path, - bool is_rename_supported) { - std::cout << __FUNCTION__ << std::endl; - auto fd = open(from_file_path.c_str(), O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP); - EXPECT_LE(1, fd); - close(fd); - - std::this_thread::sleep_for(SLEEP_SECONDS); - - if (is_rename_supported) { - EXPECT_EQ(0, rename(from_file_path.c_str(), to_file_path.c_str())); - EXPECT_FALSE(utils::file::is_file(from_file_path)); - EXPECT_TRUE(utils::file::is_file(to_file_path)); - } else { - EXPECT_EQ(-1, rename(from_file_path.c_str(), to_file_path.c_str())); - EXPECT_TRUE(utils::file::is_file(from_file_path)); - EXPECT_FALSE(utils::file::is_file(to_file_path)); - } -} - -static void test_rename_directory(const std::string &from_dir_path, - const std::string &to_dir_path, - bool is_rename_supported) { - std::cout << __FUNCTION__ << std::endl; - EXPECT_EQ(0, mkdir(from_dir_path.c_str(), - S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP)); - std::this_thread::sleep_for(SLEEP_SECONDS); - - EXPECT_TRUE(utils::file::is_directory(from_dir_path)); - if (is_rename_supported) { - EXPECT_EQ(0, rename(from_dir_path.c_str(), to_dir_path.c_str())); - EXPECT_FALSE(utils::file::is_directory(from_dir_path)); - EXPECT_TRUE(utils::file::is_directory(to_dir_path)); - } else { - EXPECT_EQ(-1, rename(from_dir_path.c_str(), to_dir_path.c_str())); - EXPECT_TRUE(utils::file::is_directory(from_dir_path)); - EXPECT_FALSE(utils::file::is_directory(to_dir_path)); - } -} - -static void test_truncate(const std::string &file_path) { - std::cout << __FUNCTION__ << std::endl; - EXPECT_EQ(0, truncate(file_path.c_str(), 10u)); - - std::uint64_t file_size{}; - EXPECT_TRUE(utils::file::get_file_size(file_path, file_size)); - - EXPECT_EQ(std::uint64_t(10u), file_size); -} - -static void test_ftruncate(const std::string &file_path) { - std::cout << __FUNCTION__ << std::endl; - auto fd = open(file_path.c_str(), O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP); - EXPECT_LE(1, fd); - - EXPECT_EQ(0, ftruncate(fd, 10u)); - - std::uint64_t file_size{}; - EXPECT_TRUE(utils::file::get_file_size(file_path, file_size)); - - EXPECT_EQ(std::uint64_t(10u), file_size); - - close(fd); -} - -#if !defined(__APPLE__) -static void test_fallocate(const std::string & /* api_path */, - const std::string &file_path) { - std::cout << __FUNCTION__ << std::endl; - auto file = - open(file_path.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP); - EXPECT_LE(1, file); - EXPECT_EQ(0, fallocate(file, 0, 0, 16)); - - std::uint64_t file_size{}; - EXPECT_TRUE(utils::file::get_file_size(file_path, file_size)); - EXPECT_EQ(16U, file_size); - - EXPECT_EQ(0, close(file)); - - file_size = 0U; - EXPECT_TRUE(utils::file::get_file_size(file_path, file_size)); - EXPECT_EQ(16U, file_size); -} -#endif - -static void test_file_getattr(const std::string & /* api_path */, - const std::string &file_path) { - std::cout << __FUNCTION__ << std::endl; - auto fd = - open(file_path.c_str(), O_CREAT | O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP); - EXPECT_LE(1, fd); - - EXPECT_EQ(0, close(fd)); - - struct stat64 unix_st {}; - EXPECT_EQ(0, stat64(file_path.c_str(), &unix_st)); - EXPECT_EQ(static_cast(S_IRUSR | S_IWUSR | S_IRGRP), - ACCESSPERMS & unix_st.st_mode); - EXPECT_FALSE(S_ISDIR(unix_st.st_mode)); - EXPECT_TRUE(S_ISREG(unix_st.st_mode)); -} - -static void test_directory_getattr(const std::string & /* api_path */, - const std::string &directory_path) { - std::cout << __FUNCTION__ << std::endl; - EXPECT_EQ(0, mkdir(directory_path.c_str(), - S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP)); - - struct stat64 unix_st {}; - EXPECT_EQ(0, stat64(directory_path.c_str(), &unix_st)); - EXPECT_EQ(static_cast(S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | - S_IXGRP), - ACCESSPERMS & unix_st.st_mode); - EXPECT_TRUE(S_ISDIR(unix_st.st_mode)); - EXPECT_FALSE(S_ISREG(unix_st.st_mode)); -} - -static void -test_write_operations_fail_if_read_only(const std::string & /* api_path */, - const std::string &file_path) { - std::cout << __FUNCTION__ << std::endl; - auto fd = - open(file_path.c_str(), O_CREAT | O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP); - EXPECT_LE(1, fd); - - std::string data = "TestData"; - EXPECT_EQ(-1, write(fd, data.data(), data.size())); - - EXPECT_EQ(-1, ftruncate(fd, 9u)); - -#if !defined(__APPLE__) - EXPECT_EQ(-1, fallocate(fd, 0, 0, 16)); -#endif - - EXPECT_EQ(0, close(fd)); - - std::this_thread::sleep_for(SLEEP_SECONDS); - - std::uint64_t file_size{}; - EXPECT_TRUE(utils::file::get_file_size(file_path, file_size)); - EXPECT_EQ(std::size_t(0u), file_size); - - // filesystem_item fsi{}; - // EXPECT_EQ(api_error::success, - // provider.get_filesystem_item(api_path, false, fsi)); - // EXPECT_TRUE(utils::file::get_file_size(fsi.source_path, file_size)); - // EXPECT_EQ(std::size_t(0u), file_size); -} - -#if !__APPLE__ && HAS_SETXATTR -static void test_xattr_invalid_parameters(const std::string &file_path) { - std::cout << __FUNCTION__ << std::endl; - std::string attr = "moose"; - EXPECT_EQ(-1, setxattr(nullptr, "user.test_attr", attr.data(), attr.size(), - XATTR_CREATE)); - EXPECT_EQ(errno, EFAULT); - EXPECT_EQ(-1, setxattr(file_path.c_str(), nullptr, attr.data(), attr.size(), - XATTR_CREATE)); - EXPECT_EQ(errno, EFAULT); - EXPECT_EQ(-1, setxattr(file_path.c_str(), "user.test_attr", nullptr, - attr.size(), XATTR_CREATE)); - EXPECT_EQ(errno, EFAULT); - EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", nullptr, 0, - XATTR_CREATE)); -} - -static void test_xattr_create_and_get(const std::string &file_path) { - std::cout << __FUNCTION__ << std::endl; - std::string attr = "moose"; - EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", attr.data(), - attr.size(), XATTR_CREATE)); - - std::string val; - val.resize(attr.size()); - EXPECT_EQ(attr.size(), - static_cast(getxattr( - file_path.c_str(), "user.test_attr", val.data(), val.size()))); - EXPECT_STREQ(attr.c_str(), val.c_str()); -} - -static void test_xattr_listxattr(const std::string &file_path) { - std::cout << __FUNCTION__ << std::endl; - std::string attr = "moose"; - EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", attr.data(), - attr.size(), XATTR_CREATE)); - EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr2", attr.data(), - attr.size(), XATTR_CREATE)); - - std::string val; - val.resize(attr.size()); - EXPECT_EQ(attr.size(), - static_cast(getxattr( - file_path.c_str(), "user.test_attr", val.data(), val.size()))); - EXPECT_STREQ(attr.c_str(), val.c_str()); - - std::string data; - auto size = listxattr(file_path.c_str(), data.data(), 0U); - EXPECT_EQ(31, size); - - data.resize(static_cast(size)); - EXPECT_EQ(size, listxattr(file_path.c_str(), data.data(), - static_cast(size))); - - auto *ptr = data.data(); - EXPECT_STREQ("user.test_attr", ptr); - - ptr += strlen(ptr) + 1; - EXPECT_STREQ("user.test_attr2", ptr); -} - -static void test_xattr_replace(const std::string &file_path) { - std::cout << __FUNCTION__ << std::endl; - std::string attr = "moose"; - EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", attr.data(), - attr.size(), XATTR_CREATE)); - - attr = "cow"; - EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", attr.data(), - attr.size(), XATTR_REPLACE)); - - std::string val; - val.resize(attr.size()); - EXPECT_EQ(attr.size(), - static_cast(getxattr( - file_path.c_str(), "user.test_attr", val.data(), val.size()))); - EXPECT_STREQ(attr.c_str(), val.c_str()); -} - -static void test_xattr_default_create(const std::string &file_path) { - std::cout << __FUNCTION__ << std::endl; - std::string attr = "moose"; - EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", attr.data(), - attr.size(), 0)); - - std::string val; - val.resize(attr.size()); - EXPECT_EQ(attr.size(), - static_cast(getxattr( - file_path.c_str(), "user.test_attr", val.data(), val.size()))); - EXPECT_STREQ(attr.c_str(), val.c_str()); -} - -static void test_xattr_default_replace(const std::string &file_path) { - std::cout << __FUNCTION__ << std::endl; - std::string attr = "moose"; - EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", attr.data(), - attr.size(), 0)); - - attr = "cow"; - EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", attr.data(), - attr.size(), 0)); - - std::string val; - val.resize(attr.size()); - EXPECT_EQ(attr.size(), - static_cast(getxattr( - file_path.c_str(), "user.test_attr", val.data(), val.size()))); - EXPECT_STREQ(attr.c_str(), val.c_str()); -} - -static void test_xattr_create_fails_if_exists(const std::string &file_path) { - std::cout << __FUNCTION__ << std::endl; - std::string attr = "moose"; - EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", attr.data(), - attr.size(), 0)); - EXPECT_EQ(-1, setxattr(file_path.c_str(), "user.test_attr", attr.data(), - attr.size(), XATTR_CREATE)); - EXPECT_EQ(EEXIST, errno); -} - -static void -test_xattr_create_fails_if_not_exists(const std::string &file_path) { - std::cout << __FUNCTION__ << std::endl; - std::string attr = "moose"; - EXPECT_EQ(-1, setxattr(file_path.c_str(), "user.test_attr", attr.data(), - attr.size(), XATTR_REPLACE)); - EXPECT_EQ(ENODATA, errno); -} - -static void test_xattr_removexattr(const std::string &file_path) { - std::cout << __FUNCTION__ << std::endl; - std::string attr = "moose"; - EXPECT_EQ(0, setxattr(file_path.c_str(), "user.test_attr", attr.data(), - attr.size(), XATTR_CREATE)); - - EXPECT_EQ(0, removexattr(file_path.c_str(), "user.test_attr")); - - std::string val; - val.resize(attr.size()); - EXPECT_EQ(-1, getxattr(file_path.c_str(), "user.test_attr", val.data(), - val.size())); - EXPECT_EQ(ENODATA, errno); -} -#endif - -TEST(fuse_drive, all_tests) { - auto current_directory = std::filesystem::current_path(); - - for (std::size_t idx = 0U; idx < 2U; idx++) { - std::filesystem::current_path(current_directory); - - const auto test_directory = utils::path::combine( - "./test_config", {"fuse_drive" + std::to_string(idx)}); - EXPECT_TRUE(utils::file::delete_directory_recursively(test_directory)); - - const auto mount_location = - utils::path::combine(test_directory, {"mount", std::to_string(idx)}); - - EXPECT_TRUE(utils::file::create_full_directory_path(mount_location)); - { - std::unique_ptr config_ptr{}; - std::unique_ptr comm_ptr{}; - std::unique_ptr provider_ptr{}; - - const auto cfg_directory = - utils::path::combine(test_directory, {"cfg", std::to_string(idx)}); - EXPECT_TRUE(utils::file::create_full_directory_path(cfg_directory)); - - std::vector drive_args{}; - - switch (idx) { - case 0U: { - config_ptr = - std::make_unique(provider_type::s3, cfg_directory); - { - app_config src_cfg(provider_type::s3, - utils::path::combine(get_test_dir(), {"storj"})); - config_ptr->set_enable_drive_events(true); - config_ptr->set_event_level(event_level::trace); - config_ptr->set_s3_config(src_cfg.get_s3_config()); - } - - comm_ptr = std::make_unique(config_ptr->get_s3_config()); - provider_ptr = std::make_unique(*config_ptr, *comm_ptr); - drive_args = std::vector({"-s3", "-na", "storj"}); - } break; - - case 1U: { - config_ptr = - std::make_unique(provider_type::sia, cfg_directory); - { - app_config src_cfg(provider_type::sia, - utils::path::combine(get_test_dir(), {"sia"})); - config_ptr->set_enable_drive_events(true); - config_ptr->set_event_level(event_level::debug); - config_ptr->set_host_config(src_cfg.get_host_config()); - } - - comm_ptr = std::make_unique(config_ptr->get_host_config()); - provider_ptr = std::make_unique(*config_ptr, *comm_ptr); - } break; - - default: - std::cerr << "provider at index " << idx << " is not implemented" - << std::endl; - continue; - } - - std::thread th([&] { - std::this_thread::sleep_for(5s); - EXPECT_EQ(0, system(("mount|grep \"" + mount_location + "\"").c_str())); - - auto file_path = create_file_and_test(mount_location, "chmod_test"); - test_chmod(utils::path::create_api_path("chmod_test"), file_path); - unlink_file_and_test(file_path); - - file_path = create_file_and_test(mount_location, "chown_test"); - test_chown(utils::path::create_api_path("chown_test"), file_path); - unlink_file_and_test(file_path); - - file_path = utils::path::combine(mount_location, {"mkdir_test"}); - test_mkdir(utils::path::create_api_path("mkdir_test"), file_path); - rmdir_and_test(file_path); - - file_path = create_file_and_test(mount_location, "write_read_test"); - test_write_and_read(utils::path::create_api_path("write_read_test"), - file_path); - unlink_file_and_test(file_path); - - file_path = - create_file_and_test(mount_location, "from_rename_file_test"); - auto to_file_path = - utils::path::combine(mount_location, {"to_rename_file_test"}); - test_rename_file(file_path, to_file_path, - provider_ptr->is_rename_supported()); - EXPECT_TRUE(utils::file::retry_delete_file(file_path)); - EXPECT_TRUE(utils::file::retry_delete_file(to_file_path)); - - file_path = - utils::path::combine(mount_location, {"from_rename_dir_test"}); - to_file_path = - utils::path::combine(mount_location, {"to_rename_dir_test"}); - test_rename_directory(file_path, to_file_path, - provider_ptr->is_rename_supported()); - EXPECT_TRUE(utils::file::retry_delete_directory(file_path.c_str())); - EXPECT_TRUE(utils::file::retry_delete_directory(to_file_path.c_str())); - - file_path = create_file_and_test(mount_location, "truncate_file_test"); - test_truncate(file_path); - unlink_file_and_test(file_path); - - file_path = create_file_and_test(mount_location, "ftruncate_file_test"); - test_ftruncate(file_path); - unlink_file_and_test(file_path); - -#if !defined(__APPLE__) - file_path = create_file_and_test(mount_location, "fallocate_file_test"); - test_fallocate(utils::path::create_api_path("fallocate_file_test"), - file_path); - unlink_file_and_test(file_path); -#endif - - file_path = create_file_and_test(mount_location, "write_fails_ro_test"); - test_write_operations_fail_if_read_only( - utils::path::create_api_path("write_fails_ro_test"), file_path); - unlink_file_and_test(file_path); - - file_path = create_file_and_test(mount_location, "getattr.txt"); - test_file_getattr(utils::path::create_api_path("getattr.txt"), - file_path); - unlink_file_and_test(file_path); - - file_path = utils::path::combine(mount_location, {"getattr_dir"}); - test_directory_getattr(utils::path::create_api_path("getattr_dir"), - file_path); - rmdir_and_test(file_path); - -#if !__APPLE__ && HAS_SETXATTR - file_path = - create_file_and_test(mount_location, "xattr_invalid_names_test"); - test_xattr_invalid_parameters(file_path); - unlink_file_and_test(file_path); - - file_path = - create_file_and_test(mount_location, "xattr_create_get_test"); - test_xattr_create_and_get(file_path); - unlink_file_and_test(file_path); - - file_path = - create_file_and_test(mount_location, "xattr_listxattr_test"); - test_xattr_listxattr(file_path); - unlink_file_and_test(file_path); - - file_path = create_file_and_test(mount_location, "xattr_replace_test"); - test_xattr_replace(file_path); - unlink_file_and_test(file_path); - - file_path = - create_file_and_test(mount_location, "xattr_default_create_test"); - test_xattr_default_create(file_path); - unlink_file_and_test(file_path); - - file_path = - create_file_and_test(mount_location, "xattr_default_replace_test"); - test_xattr_default_replace(file_path); - unlink_file_and_test(file_path); - - file_path = create_file_and_test(mount_location, - "xattr_create_fails_exists_test"); - test_xattr_create_fails_if_exists(file_path); - unlink_file_and_test(file_path); - - file_path = create_file_and_test(mount_location, - "xattr_create_fails_not_exists_test"); - test_xattr_create_fails_if_not_exists(file_path); - unlink_file_and_test(file_path); - - file_path = - create_file_and_test(mount_location, "xattr_removexattr_test"); - test_xattr_removexattr(file_path); - unlink_file_and_test(file_path); -#endif - - execute_unmount(mount_location); - }); - - drive_args.push_back(mount_location); - execute_mount(config_ptr->get_data_directory(), drive_args, th); - } - } - - std::filesystem::current_path(current_directory); + this->unlink_file_and_test(file_path); } } // namespace repertory -#endif +#endif // !defined(_WIN32) diff --git a/repertory/repertory_test/src/providers_test.cpp b/repertory/repertory_test/src/providers_test.cpp index 1ecbb4cc..fc7fe195 100644 --- a/repertory/repertory_test/src/providers_test.cpp +++ b/repertory/repertory_test/src/providers_test.cpp @@ -57,7 +57,7 @@ const auto check_forced_dirs = [](const repertory::directory_item_list &list) { const auto create_directory = [](repertory::i_provider &provider, const std::string &api_path) { - auto date = repertory::utils::time::get_file_time_now(); + auto date = repertory::utils::time::get_time_now(); auto meta = repertory::create_meta_attributes( date, 1U, date + 1U, date + 2U, true, getgid(), "", 0700, date + 3U, 2U, 3U, 0U, api_path + "_src", getuid(), date + 4U); @@ -107,10 +107,9 @@ const auto create_directory = [](repertory::i_provider &provider, const auto create_file = [](repertory::i_provider &provider, const std::string &api_path) { - auto source_path = - repertory::generate_test_file_name("./test_config", "providers_test"); + auto source_path = repertory::test::generate_test_file_name("providers_test"); - auto date = repertory::utils::time::get_file_time_now(); + auto date = repertory::utils::time::get_time_now(); auto meta = repertory::create_meta_attributes( date, 1U, date + 1U, date + 2U, false, getgid(), "", 0700, date + 3U, 2U, 3U, 0U, source_path, getuid(), date + 4U); @@ -121,7 +120,7 @@ const auto create_file = [](repertory::i_provider &provider, EXPECT_EQ(repertory::api_error::success, provider.is_file(api_path, exists)); EXPECT_TRUE(exists); - EXPECT_TRUE(repertory::utils::file::delete_file(source_path)); + EXPECT_TRUE(repertory::utils::file::file{source_path}.remove()); repertory::api_meta_map meta2{}; EXPECT_EQ(repertory::api_error::success, @@ -167,9 +166,8 @@ const auto decrypt_parts = [](const repertory::app_config &cfg, continue; } - EXPECT_EQ(repertory::api_error::success, - repertory::utils::encryption::decrypt_file_name( - cfg.get_encrypt_config().encryption_token, part)); + EXPECT_EQ(true, repertory::utils::encryption::decrypt_file_name( + cfg.get_encrypt_config().encryption_token, part)); } path = repertory::utils::string::join(parts, '/'); } @@ -398,7 +396,7 @@ static void get_directory_item_count(const app_config &cfg, EXPECT_EQ(std::size_t(0U), provider.get_directory_item_count("/not_found")); const auto source_path = - utils::path::combine("./test_data/encrypt", {"sub10"}); + utils::path::combine(test::get_test_input_dir(), {"encrypt", "sub10"}); std::string api_path{}; EXPECT_EQ(api_error::success, @@ -630,8 +628,8 @@ static void run_tests(const app_config &cfg, i_provider &provider) { TEST(providers, encrypt_provider) { const auto config_path = - utils::path::combine("./test_config", {"encrypt_provider"}); - ASSERT_TRUE(utils::file::delete_directory_recursively(config_path)); + utils::path::combine(test::get_test_output_dir(), {"encrypt_provider"}); + ASSERT_TRUE(utils::file::directory(config_path).remove_recursively()); console_consumer consumer{}; event_system::instance().start(); @@ -639,7 +637,7 @@ TEST(providers, encrypt_provider) { app_config cfg(provider_type::encrypt, config_path); const auto encrypt_path = - utils::path::combine("./test_data/encrypt", {"encrypt"}); + utils::path::combine(test::get_test_input_dir(), {"encrypt"}); EXPECT_STREQ( encrypt_path.c_str(), @@ -673,16 +671,17 @@ TEST(providers, encrypt_provider) { TEST(providers, s3_provider) { const auto config_path = - utils::path::combine("./test_config", {"s3_provider"}); - ASSERT_TRUE(utils::file::delete_directory_recursively(config_path)); + utils::path::combine(test::get_test_output_dir(), {"s3_provider"}); + ASSERT_TRUE(utils::file::directory(config_path).remove_recursively()); console_consumer consumer{}; event_system::instance().start(); { app_config cfg(provider_type::s3, config_path); { - app_config src_cfg(provider_type::s3, - utils::path::combine(get_test_dir(), {"storj"})); + app_config src_cfg( + provider_type::s3, + utils::path::combine(test::get_test_input_dir(), {"storj"})); cfg.set_s3_config(src_cfg.get_s3_config()); } @@ -711,16 +710,17 @@ TEST(providers, s3_provider) { TEST(providers, sia_provider) { const auto config_path = - utils::path::combine("./test_config", {"sia_provider"}); - ASSERT_TRUE(utils::file::delete_directory_recursively(config_path)); + utils::path::combine(test::get_test_output_dir(), {"sia_provider"}); + ASSERT_TRUE(utils::file::directory(config_path).remove_recursively()); console_consumer consumer{}; event_system::instance().start(); { app_config cfg(provider_type::sia, config_path); { - app_config src_cfg(provider_type::sia, - utils::path::combine(get_test_dir(), {"sia"})); + app_config src_cfg( + provider_type::sia, + utils::path::combine(test::get_test_input_dir(), {"sia"})); cfg.set_host_config(src_cfg.get_host_config()); } diff --git a/repertory/repertory_test/src/remote_fuse_test.cpp b/repertory/repertory_test/src/remote_fuse_test.cpp index 2fd1d4b9..c66c3909 100644 --- a/repertory/repertory_test/src/remote_fuse_test.cpp +++ b/repertory/repertory_test/src/remote_fuse_test.cpp @@ -47,12 +47,12 @@ using namespace repertory::remote_fuse; namespace fuse_test { static std::string mount_location_; static std::string fuse_remote_dir = - utils::path::combine("./test_config", {"fuse_remote_test"}); + utils::path::combine(test::get_test_output_dir(), {"fuse_remote_test"}); static void access_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"access.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -64,13 +64,13 @@ static void access_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_access(api_path.c_str(), 0)); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void chflags_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"chflags.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -86,13 +86,13 @@ static void chflags_test(repertory::remote_fuse::remote_client &client) { #endif } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void chmod_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"chmod.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -108,13 +108,13 @@ static void chmod_test(repertory::remote_fuse::remote_client &client) { #endif } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void chown_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"chown.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -134,7 +134,7 @@ static void chown_test(repertory::remote_fuse::remote_client &client) { #endif } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void @@ -143,7 +143,7 @@ create_and_release_test(repertory::remote_fuse::remote_client &client, const auto test_file = utils::path::combine(fuse_remote_dir, {"create_and_release.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -156,7 +156,7 @@ create_and_release_test(repertory::remote_fuse::remote_client &client, EXPECT_EQ(0u, server.get_open_file_count(test_file)); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void destroy_test(repertory::remote_fuse::remote_client &client) { @@ -167,7 +167,7 @@ static void destroy_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"fallocate.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); remote::file_handle handle; const auto ret = client.fuse_create( @@ -181,14 +181,14 @@ remote::open_flags::ReadWrite, handle); EXPECT_EQ(0, ret); if (ret == 0) { EXPECT_EQ(100, file_size); } - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); }*/ static void fgetattr_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"fgetattr.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -219,23 +219,23 @@ static void fgetattr_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(st1.st_nlink, st.st_nlink); EXPECT_EQ(st1.st_mode, st.st_mode); EXPECT_LE(static_cast(st1.st_atime), - st.st_atimespec / NANOS_PER_SECOND); + st.st_atimespec / utils::time::NANOS_PER_SECOND); EXPECT_EQ(static_cast(st1.st_mtime), - st.st_mtimespec / NANOS_PER_SECOND); + st.st_mtimespec / utils::time::NANOS_PER_SECOND); EXPECT_EQ(static_cast(st1.st_ctime), - st.st_ctimespec / NANOS_PER_SECOND); + st.st_ctimespec / utils::time::NANOS_PER_SECOND); EXPECT_EQ(static_cast(st1.st_ctime), - st.st_birthtimespec / NANOS_PER_SECOND); + st.st_birthtimespec / utils::time::NANOS_PER_SECOND); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void fsetattr_x_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"fsetattr_x.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -253,13 +253,13 @@ static void fsetattr_x_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void fsync_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"fsync.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -271,14 +271,14 @@ static void fsync_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void ftruncate_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"ftruncate.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -289,18 +289,18 @@ static void ftruncate_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_ftruncate(api_path.c_str(), 100, handle)); EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); - std::uint64_t file_size; - EXPECT_TRUE(utils::file::get_file_size(test_file, file_size)); - EXPECT_EQ(100u, file_size); + auto opt_size = utils::file::file{test_file}.size(); + EXPECT_TRUE(opt_size.has_value()); + EXPECT_EQ(100U, opt_size.value()); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void getattr_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"getattr.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -330,23 +330,23 @@ static void getattr_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(st1.st_nlink, st.st_nlink); EXPECT_EQ(st1.st_mode, st.st_mode); EXPECT_LE(static_cast(st1.st_atime), - st.st_atimespec / NANOS_PER_SECOND); + st.st_atimespec / utils::time::NANOS_PER_SECOND); EXPECT_EQ(static_cast(st1.st_mtime), - st.st_mtimespec / NANOS_PER_SECOND); + st.st_mtimespec / utils::time::NANOS_PER_SECOND); EXPECT_EQ(static_cast(st1.st_ctime), - st.st_ctimespec / NANOS_PER_SECOND); + st.st_ctimespec / utils::time::NANOS_PER_SECOND); EXPECT_EQ(static_cast(st1.st_ctime), - st.st_birthtimespec / NANOS_PER_SECOND); + st.st_birthtimespec / utils::time::NANOS_PER_SECOND); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } /*static void getxattr_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"getxattr.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); remote::file_handle handle; const auto ret = client.fuse_create( @@ -359,14 +359,14 @@ nullptr, 0)); #else EXPECT_EQ(-EACCES, client.fuse_getxattr(api_path.c_str(), "test", nullptr, 0)); #endif } - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); } static void getxattr_osx_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"getxattr_osx.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); remote::file_handle handle; const auto ret = client.fuse_create( @@ -377,14 +377,14 @@ remote::open_flags::ReadWrite, handle); EXPECT_EQ(0, ret); if (ret == 0) { client.fuse_getxattrOSX(api_path.c_str(), "test", nullptr, 0, 0)); } - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); }*/ static void getxtimes_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"getxtimes.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -403,7 +403,7 @@ static void getxtimes_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void init_test(repertory::remote_fuse::remote_client &client) { @@ -414,7 +414,7 @@ static void init_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"listxattr.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); remote::file_handle handle; const auto ret = client.fuse_create( @@ -427,29 +427,29 @@ remote::open_flags::ReadWrite, handle); EXPECT_EQ(0, ret); if (ret == 0) { #endif } - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); }*/ static void mkdir_test(repertory::remote_fuse::remote_client &client) { const auto test_directory = utils::path::combine(fuse_remote_dir, {"mkdir_test"}); const auto api_path = test_directory.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::delete_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).remove()); #if defined(_WIN32) EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), 0)); #else EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), S_IRWXU)); #endif - EXPECT_TRUE(utils::file::is_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).exists()); - EXPECT_TRUE(utils::file::delete_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).remove()); } static void open_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"open.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; #if defined(_WIN32) @@ -471,7 +471,7 @@ static void open_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle2)); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void @@ -479,27 +479,27 @@ opendir_and_releasedir_test(repertory::remote_fuse::remote_client &client) { const auto test_directory = utils::path::combine(fuse_remote_dir, {"opendir_and_release_dir"}); const auto api_path = test_directory.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::delete_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).remove()); #if defined(_WIN32) EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), 0)); #else EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), S_IRWXU)); #endif - EXPECT_TRUE(utils::file::is_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).exists()); remote::file_handle handle = 0; EXPECT_EQ(0, client.fuse_opendir(api_path.c_str(), handle)); EXPECT_EQ(0, client.fuse_releasedir(api_path.c_str(), handle)); - EXPECT_TRUE(utils::file::delete_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).remove()); } static void read_and_write_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"read_and_write.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -517,7 +517,7 @@ static void read_and_write_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void @@ -525,7 +525,7 @@ read_and_write_base64_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"read_and_write_base64.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -534,7 +534,7 @@ read_and_write_base64_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, ret); if (ret == 0) { const auto data = macaron::Base64::Encode("1234567890"); - EXPECT_EQ(10, client.fuse_write_base64(api_path.c_str(), &data[0], + EXPECT_EQ(10, client.fuse_write_base64(api_path.c_str(), data.data(), data.size(), 0, handle)); data_buffer buffer(10); EXPECT_EQ(10, client.fuse_read(api_path.c_str(), @@ -544,21 +544,21 @@ read_and_write_base64_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void readdir_test(repertory::remote_fuse::remote_client &client) { const auto test_directory = utils::path::combine(fuse_remote_dir, {"readdir_test"}); const auto api_path = test_directory.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::delete_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).remove()); #if defined(_WIN32) EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), 0)); #else EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), S_IRWXU)); #endif - EXPECT_TRUE(utils::file::is_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).exists()); remote::file_handle handle = 0; EXPECT_EQ(0, client.fuse_opendir(api_path.c_str(), handle)); @@ -572,14 +572,14 @@ static void readdir_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_releasedir(api_path.c_str(), handle)); - EXPECT_TRUE(utils::file::delete_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).remove()); } /*static void removexattr_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"removexattr.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); remote::file_handle handle; const auto ret = client.fuse_create( @@ -592,7 +592,7 @@ remote::open_flags::ReadWrite, handle); EXPECT_EQ(0, ret); if (ret == 0) { "test")); #endif } - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); }*/ static void rename_test(repertory::remote_fuse::remote_client &client) { @@ -602,8 +602,8 @@ static void rename_test(repertory::remote_fuse::remote_client &client) { const auto api_path = test_file.substr(mount_location_.size()); const auto renamed_api_path = renamed_test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); - EXPECT_TRUE(utils::file::retry_delete_file(renamed_test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); + EXPECT_TRUE(utils::file::file(renamed_test_file).remove()); remote::file_handle handle; #if defined(_WIN32) @@ -621,38 +621,38 @@ static void rename_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); EXPECT_EQ(0, client.fuse_rename(api_path.c_str(), renamed_api_path.c_str())); - EXPECT_FALSE(utils::file::is_file(test_file)); - EXPECT_TRUE(utils::file::is_file(renamed_test_file)); + EXPECT_FALSE(utils::file::file(test_file).exists()); + EXPECT_TRUE(utils::file::file(renamed_test_file).exists()); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); - EXPECT_TRUE(utils::file::retry_delete_file(renamed_test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); + EXPECT_TRUE(utils::file::file(renamed_test_file).remove()); } static void rmdir_test(repertory::remote_fuse::remote_client &client) { const auto test_directory = utils::path::combine(fuse_remote_dir, {"rmdir_test"}); const auto api_path = test_directory.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::delete_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).remove()); #if defined(_WIN32) EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), 0)); #else EXPECT_EQ(0, client.fuse_mkdir(api_path.c_str(), S_IRWXU)); #endif - EXPECT_TRUE(utils::file::is_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).exists()); EXPECT_EQ(0, client.fuse_rmdir(api_path.c_str())); - EXPECT_FALSE(utils::file::is_directory(test_directory)); + EXPECT_FALSE(utils::file::directory(test_directory).exists()); - EXPECT_TRUE(utils::file::delete_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).remove()); } static void setattr_x_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"setattr_x.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -670,14 +670,14 @@ static void setattr_x_test(repertory::remote_fuse::remote_client &client) { #endif } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void setbkuptime_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"setbkuptime.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -687,7 +687,7 @@ static void setbkuptime_test(repertory::remote_fuse::remote_client &client) { if (ret == 0) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); - remote::file_time ts = utils::time::get_file_time_now(); + remote::file_time ts = utils::time::get_time_now(); #if defined(_WIN32) EXPECT_EQ(NOT_IMPLEMENTED, client.fuse_setbkuptime(api_path.c_str(), ts)); #else @@ -695,14 +695,14 @@ static void setbkuptime_test(repertory::remote_fuse::remote_client &client) { #endif } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void setchgtime_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"setchgtime.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -712,7 +712,7 @@ static void setchgtime_test(repertory::remote_fuse::remote_client &client) { if (ret == 0) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); - remote::file_time ts = utils::time::get_file_time_now(); + remote::file_time ts = utils::time::get_time_now(); #if defined(_WIN32) EXPECT_EQ(NOT_IMPLEMENTED, client.fuse_setchgtime(api_path.c_str(), ts)); #else @@ -720,14 +720,14 @@ static void setchgtime_test(repertory::remote_fuse::remote_client &client) { #endif } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void setcrtime_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"setcrtime.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -737,7 +737,7 @@ static void setcrtime_test(repertory::remote_fuse::remote_client &client) { if (ret == 0) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); - remote::file_time ts = utils::time::get_file_time_now(); + remote::file_time ts = utils::time::get_time_now(); #if defined(_WIN32) EXPECT_EQ(NOT_IMPLEMENTED, client.fuse_setcrtime(api_path.c_str(), ts)); #else @@ -745,7 +745,7 @@ static void setcrtime_test(repertory::remote_fuse::remote_client &client) { #endif } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void setvolname_test(repertory::remote_fuse::remote_client &client) { @@ -756,7 +756,7 @@ static void setvolname_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"setxattr.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); remote::file_handle handle; const auto ret = client.fuse_create( @@ -771,14 +771,14 @@ remote::open_flags::ReadWrite, handle); EXPECT_EQ(0, ret); if (ret == 0) { 5, 0)); #endif } - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); } static void setxattr_osx_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"setxattr_osx.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); remote::file_handle handle; const auto ret = client.fuse_create( @@ -791,7 +791,7 @@ remote::open_flags::ReadWrite, handle); EXPECT_EQ(0, ret); if (ret == 0) { 0)); } - utils::file::retry_delete_file(test_file); + utils::file::file(test_file).remove(); }*/ #if defined(_WIN32) @@ -832,7 +832,7 @@ static void statfs_x_test(repertory::remote_fuse::remote_client &client, remote::statfs_x st{}; EXPECT_EQ(0, client.fuse_statfs_x(api_path.c_str(), 4096, st)); - EXPECT_STREQ(&st.f_mntfromname[0], + EXPECT_STREQ(st.f_mntfromname.data(), utils::create_volume_label(provider_type::remote).c_str()); const auto total_bytes = drive.get_total_drive_space(); @@ -853,7 +853,7 @@ static void truncate_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"truncate.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; #if defined(_WIN32) @@ -871,18 +871,18 @@ static void truncate_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_truncate(api_path.c_str(), 100)); - std::uint64_t file_size; - EXPECT_TRUE(utils::file::get_file_size(test_file, file_size)); - EXPECT_EQ(100u, file_size); + auto opt_size = utils::file::file{test_file}.size(); + EXPECT_TRUE(opt_size.has_value()); + EXPECT_EQ(100U, opt_size.value()); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void unlink_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"unlink.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -892,16 +892,16 @@ static void unlink_test(repertory::remote_fuse::remote_client &client) { if (ret == 0) { EXPECT_EQ(0, client.fuse_release(api_path.c_str(), handle)); EXPECT_EQ(0, client.fuse_unlink(api_path.c_str())); - EXPECT_FALSE(utils::file::is_file(test_file)); + EXPECT_FALSE(utils::file::file(test_file).exists()); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void utimens_test(repertory::remote_fuse::remote_client &client) { const auto test_file = utils::path::combine(fuse_remote_dir, {"utimens.txt"}); const auto api_path = test_file.substr(mount_location_.size()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); remote::file_handle handle; const auto ret = client.fuse_create( @@ -915,7 +915,7 @@ static void utimens_test(repertory::remote_fuse::remote_client &client) { EXPECT_EQ(0, client.fuse_utimens(api_path.c_str(), tv, 0, 0)); } - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } TEST(remote_fuse, all_tests) { @@ -934,11 +934,11 @@ TEST(remote_fuse, all_tests) { event_system::instance().start(); #if defined(_WIN32) - mount_location_ = std::string("./test_config").substr(0, 2); + mount_location_ = fuse_remote_dir.substr(0, 2); mock_winfsp_drive drive(mount_location_); remote_server server(config, drive, mount_location_); #else - mount_location_ = utils::path::absolute("."); + mount_location_ = fuse_remote_dir; mock_fuse_drive drive(mount_location_); remote_server server(config, drive, mount_location_); #endif @@ -990,6 +990,6 @@ TEST(remote_fuse, all_tests) { } event_system::instance().stop(); - EXPECT_TRUE(utils::file::delete_directory_recursively(fuse_remote_dir)); + EXPECT_TRUE(utils::file::directory(fuse_remote_dir).remove_recursively()); } } // namespace fuse_test diff --git a/repertory/repertory_test/src/remote_winfsp_test.cpp b/repertory/repertory_test/src/remote_winfsp_test.cpp index 86115d12..2c0ab2ed 100644 --- a/repertory/repertory_test/src/remote_winfsp_test.cpp +++ b/repertory/repertory_test/src/remote_winfsp_test.cpp @@ -33,6 +33,7 @@ #include "platform/platform.hpp" #include "types/repertory.hpp" #include "utils/common.hpp" +#include "utils/time.hpp" using namespace repertory; using namespace repertory::remote_winfsp; @@ -40,32 +41,32 @@ using namespace repertory::remote_winfsp; namespace winfsp_test { static std::string mount_location_; static std::string win_remote_dir = - utils::path::combine("./test_config", {"win_remote_test"}); + utils::path::combine(test::get_test_output_dir(), {"win_remote_test"}); static void can_delete_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"candelete.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); - native_file::native_file_ptr nf; - EXPECT_EQ(api_error::success, native_file::create_or_open(test_file, nf)); + auto ptr = utils::file::file::open_or_create_file(test_file); + auto &nf = *ptr; EXPECT_TRUE(nf); if (nf) { EXPECT_EQ(STATUS_INVALID_HANDLE, - client.winfsp_can_delete( - reinterpret_cast(nf->get_handle()), &api_path[0])); + client.winfsp_can_delete(reinterpret_cast(nf.get_handle()), + api_path.data())); - nf->close(); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + nf.close(); + EXPECT_TRUE(utils::file::file(test_file).remove()); } } template static void create_and_close_test(remote_client &client, t &server) { const auto test_file = utils::path::combine(win_remote_dir, {"create.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -83,12 +84,12 @@ static void create_and_close_test(remote_client &client, t &server) { EXPECT_EQ(0u, client.get_open_file_count(utils::string::to_utf8(api_path))); EXPECT_EQ(0u, server.get_open_file_count(test_file)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void cleanup_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"cleanup.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -107,12 +108,12 @@ static void cleanup_test(remote_client &client) { EXPECT_FALSE(was_closed); EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void flush_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"flush.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -129,13 +130,13 @@ static void flush_test(remote_client &client) { EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void get_file_info_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"get_file_info.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -152,13 +153,13 @@ static void get_file_info_test(remote_client &client) { EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void get_security_by_name_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"get_security_by_name.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -181,7 +182,7 @@ static void get_security_by_name_test(remote_client &client) { EXPECT_EQ(static_cast(FILE_ATTRIBUTE_NORMAL), attributes); EXPECT_FALSE(str_descriptor.empty()); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void get_volume_info_test(remote_client &client) { @@ -202,7 +203,7 @@ static void mounted_test(remote_client &client) { static void open_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"open.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -229,13 +230,13 @@ static void open_test(remote_client &client) { EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc2)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void overwrite_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"overwrite.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -259,13 +260,13 @@ static void overwrite_test(remote_client &client) { EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void create_and_read_directory_test(remote_client &client) { const auto test_directory = utils::path::combine(win_remote_dir, {"read_directory"}); - EXPECT_TRUE(utils::file::delete_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).remove()); auto api_path = utils::string::from_utf8(test_directory).substr(mount_location_.size()); @@ -278,7 +279,7 @@ static void create_and_read_directory_test(remote_client &client) { FILE_ATTRIBUTE_DIRECTORY, 0, &file_desc, &fi, normalized_name, exists); EXPECT_EQ(STATUS_SUCCESS, ret); - EXPECT_TRUE(utils::file::is_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).exists()); json list; ret = client.winfsp_read_directory(file_desc, nullptr, nullptr, list); @@ -287,13 +288,13 @@ static void create_and_read_directory_test(remote_client &client) { EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::delete_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).remove()); } static void open_and_read_directory_test(remote_client &client) { const auto test_directory = utils::path::combine(win_remote_dir, {"open_and_read_directory"}); - EXPECT_TRUE(utils::file::delete_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).remove()); auto api_path = utils::string::from_utf8(test_directory).substr(mount_location_.size()); @@ -308,7 +309,7 @@ static void open_and_read_directory_test(remote_client &client) { EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::is_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).exists()); file_desc = reinterpret_cast(REPERTORY_INVALID_HANDLE); ret = client.winfsp_open(&api_path[0], FILE_DIRECTORY_FILE, @@ -323,13 +324,13 @@ static void open_and_read_directory_test(remote_client &client) { EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::delete_directory(test_directory)); + EXPECT_TRUE(utils::file::directory(test_directory).remove()); } static void read_and_write_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"read_and_write.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -365,14 +366,14 @@ static void read_and_write_test(remote_client &client) { EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void rename_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"rename.txt"}); const auto test_file2 = utils::path::combine(win_remote_dir, {"rename2.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file2)); + EXPECT_TRUE(utils::file::file(test_file).remove()); + EXPECT_TRUE(utils::file::file(test_file2).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); auto api_path2 = @@ -389,20 +390,20 @@ static void rename_test(remote_client &client) { ret = client.winfsp_rename(file_desc, &api_path[0], &api_path2[0], 0); EXPECT_EQ(STATUS_SUCCESS, ret); - EXPECT_TRUE(utils::file::is_file(test_file2)); - EXPECT_FALSE(utils::file::is_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file2).exists()); + EXPECT_FALSE(utils::file::file(test_file).exists()); EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file2)); + EXPECT_TRUE(utils::file::file(test_file).remove()); + EXPECT_TRUE(utils::file::file(test_file2).remove()); } static void set_basic_info_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"set_basic_info.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -429,7 +430,7 @@ static void set_basic_info_test(remote_client &client) { const auto change_time = last_write_time; #else const auto creation_time = - utils::unix_time_to_windows_time(utils::time::get_time_now()); + utils::time::unix_time_to_windows_time(utils::time::get_time_now()); const auto last_access_time = creation_time + 1; const auto last_write_time = creation_time + 2; const auto change_time = last_write_time; @@ -447,13 +448,13 @@ static void set_basic_info_test(remote_client &client) { EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void set_file_size_test(remote_client &client) { const auto test_file = utils::path::combine(win_remote_dir, {"set_file_size.txt"}); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); auto api_path = utils::string::from_utf8(test_file).substr(mount_location_.size()); @@ -472,13 +473,13 @@ static void set_file_size_test(remote_client &client) { client.winfsp_set_file_size(file_desc, new_file_size, set_allocation_size, &fi)); - std::uint64_t file_size = 0u; - EXPECT_TRUE(utils::file::get_file_size(test_file, file_size)); - EXPECT_EQ(34u, file_size); + auto opt_size = utils::file::file{test_file}.size(); + EXPECT_TRUE(opt_size.has_value()); + EXPECT_EQ(34U, opt_size.value()); EXPECT_EQ(STATUS_SUCCESS, client.winfsp_close(file_desc)); - EXPECT_TRUE(utils::file::retry_delete_file(test_file)); + EXPECT_TRUE(utils::file::file(test_file).remove()); } static void unmounted_test(remote_client &client) { @@ -492,7 +493,6 @@ TEST(remote_winfsp, all_tests) { EXPECT_TRUE(found_port); if (found_port) { console_consumer c; - app_config config(provider_type::remote, win_remote_dir); config.set_remote_host_name_or_ip("localhost"); config.set_remote_port(port); @@ -502,11 +502,11 @@ TEST(remote_winfsp, all_tests) { event_system::instance().start(); #if defined(_WIN32) - mount_location_ = std::string("./test_config").substr(0, 2); + mount_location_ = win_remote_dir.substr(0, 2); mock_winfsp_drive drive(mount_location_); remote_server server(config, drive, mount_location_); #else - mount_location_ = utils::path::absolute("."); + mount_location_ = win_remote_dir; mock_fuse_drive drive(mount_location_); remote_fuse::remote_server server(config, drive, mount_location_); #endif @@ -536,6 +536,6 @@ TEST(remote_winfsp, all_tests) { } event_system::instance().stop(); - EXPECT_TRUE(utils::file::delete_directory_recursively(win_remote_dir)); + EXPECT_TRUE(utils::file::directory(win_remote_dir).remove_recursively()); } } // namespace winfsp_test diff --git a/repertory/repertory_test/src/test_common.cpp b/repertory/repertory_test/src/test_common.cpp deleted file mode 100644 index 74dc7eee..00000000 --- a/repertory/repertory_test/src/test_common.cpp +++ /dev/null @@ -1,73 +0,0 @@ -/* - Copyright <2018-2024> - - 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. -*/ -#include "test_common.hpp" - -#include "types/repertory.hpp" -#include "utils/common.hpp" -#include "utils/path.hpp" - -namespace repertory { -std::vector generated_files; - -void delete_generated_files() { - for (const auto &file : generated_files) { - EXPECT_TRUE(utils::file::retry_delete_file(file)); - } -} - -auto create_random_file(std::string path, std::size_t size) -> native_file_ptr { - native_file_ptr file; - if (native_file::create_or_open(path, file) == api_error::success) { - generated_files.emplace_back(utils::path::absolute(path)); - - EXPECT_TRUE(file->truncate(0U)); - - data_buffer buf(size); - randombytes_buf(buf.data(), buf.size()); - - std::size_t bytes_written{}; - EXPECT_TRUE(file->write_bytes(buf.data(), buf.size(), 0U, bytes_written)); - file->flush(); - - std::uint64_t current_size{}; - EXPECT_TRUE(utils::file::get_file_size(path, current_size)); - EXPECT_EQ(size, current_size); - } - - return file; -} - -auto generate_test_file_name(const std::string &directory, - const std::string &file_name_no_extension) - -> std::string { - static std::atomic idx{0U}; - auto path = utils::path::combine( - directory, {file_name_no_extension + std::to_string(idx++) + ".dat"}); - generated_files.emplace_back(path); - return path; -} - -auto get_test_dir() -> std::string { - auto dir = utils::get_environment_variable("PROJECT_TEST_DIR"); - return utils::path::combine(dir.empty() ? "." : dir, {"test_config"}); -} -} // namespace repertory diff --git a/repertory/repertory_test/src/utils_test.cpp b/repertory/repertory_test/src/utils_test.cpp index 7a10a938..100cdadc 100644 --- a/repertory/repertory_test/src/utils_test.cpp +++ b/repertory/repertory_test/src/utils_test.cpp @@ -21,17 +21,14 @@ */ #include "test_common.hpp" -#include "platform/platform.hpp" -#include "utils/common.hpp" -#include "utils/file_utils.hpp" -#include "utils/string.hpp" -#include "utils/utils.hpp" +#include "providers/s3/s3_provider.hpp" +#include "utils/file.hpp" namespace repertory { #if defined(_WIN32) TEST(utils, convert_api_date) { LARGE_INTEGER li{}; - li.QuadPart = utils::convert_api_date("2009-10-12T17:50:30.111Z"); + li.QuadPart = s3_provider::convert_api_date("2009-10-12T17:50:30.111Z"); SYSTEMTIME st{}; FileTimeToSystemTime(reinterpret_cast(&li), &st); @@ -48,10 +45,13 @@ TEST(utils, convert_api_date) { EXPECT_EQ(20, lt.wSecond); EXPECT_EQ(111, lt.wMilliseconds); } -#endif +#endif // defined(_WIN32) TEST(utils, generate_sha256) { - const auto res = utils::file::generate_sha256(__FILE__); - std::cout << res << std::endl; + auto res = utils::file::file{__FILE__}.sha256(); + EXPECT_TRUE(res.has_value()); + if (res.has_value()) { + std::cout << res.value() << std::endl; + } } } // namespace repertory diff --git a/repertory/repertory_test/src/winfsp_test.cpp b/repertory/repertory_test/src/winfsp_test.cpp index 909f7042..b6482c45 100644 --- a/repertory/repertory_test/src/winfsp_test.cpp +++ b/repertory/repertory_test/src/winfsp_test.cpp @@ -69,19 +69,19 @@ static void execute_mount(winfsp_test *test, static void unmount(winfsp_test *test, const std::string &mount_point) { test->drive->shutdown(); - auto mounted = utils::file::is_directory(mount_point); + auto mounted = utils::file::directory(mount_point).exists(); for (auto i = 0; mounted && (i < 50); i++) { std::this_thread::sleep_for(100ms); - mounted = utils::file::is_directory(mount_point); + mounted = utils::file::directory(mount_point).exists(); } - EXPECT_FALSE(utils::file::is_directory(mount_point)); + EXPECT_FALSE(utils::file::directory(mount_point).exists()); } static void root_creation_test(const std::string &mount_point) { TEST_HEADER(__FUNCTION__); WIN32_FILE_ATTRIBUTE_DATA ad{}; EXPECT_TRUE( - ::GetFileAttributesEx(&mount_point[0], GetFileExInfoStandard, &ad)); + ::GetFileAttributesEx(mount_point.c_str(), GetFileExInfoStandard, &ad)); EXPECT_EQ(FILE_ATTRIBUTE_DIRECTORY, ad.dwFileAttributes); EXPECT_EQ(0, ad.nFileSizeHigh); EXPECT_EQ(0, ad.nFileSizeLow); @@ -96,11 +96,11 @@ static auto create_test(winfsp_test *test, const std::string &mount_point) { EXPECT_NE(INVALID_HANDLE_VALUE, handle); EXPECT_TRUE(::CloseHandle(handle)); - EXPECT_TRUE(utils::file::is_file(file)); + EXPECT_TRUE(utils::file::file(file).exists()); - std::uint64_t file_size; - EXPECT_TRUE(utils::file::get_file_size(file, file_size)); - EXPECT_EQ(0, file_size); + auto opt_size = utils::file::file(file).size(); + EXPECT_TRUE(opt_size.has_value()); + EXPECT_EQ(0, opt_size.value()); std::string attr; EXPECT_EQ(api_error::success, test->provider->get_item_meta( @@ -113,8 +113,8 @@ static auto create_test(winfsp_test *test, const std::string &mount_point) { static void delete_file_test(const std::string &file) { TEST_HEADER(__FUNCTION__); event_capture ec({"file_removed"}); - EXPECT_TRUE(utils::file::retry_delete_file(file)); - EXPECT_FALSE(utils::file::is_file(file)); + EXPECT_TRUE(utils::file::file(file).remove()); + EXPECT_FALSE(utils::file::file(file).exists()); } static void create_directory_test(const std::string &directory) { @@ -149,11 +149,11 @@ static void write_file_test(const std::string &mount_point) { EXPECT_EQ(10, bytes_written); EXPECT_TRUE(::CloseHandle(handle)); - EXPECT_TRUE(utils::file::is_file(file)); + EXPECT_TRUE(utils::file::file(file).exists()); - std::uint64_t file_size; - EXPECT_TRUE(utils::file::get_file_size(file, file_size)); - EXPECT_EQ(10, file_size); + auto opt_size = utils::file::file(file).size(); + EXPECT_TRUE(opt_size.has_value()); + EXPECT_EQ(10U, opt_size.value()); } static void read_file_test(const std::string &mount_point) { @@ -199,8 +199,8 @@ static void rename_file_test(winfsp_test *test, const auto file2 = utils::path::combine(mount_point, {"rename_file2.txt"}); EXPECT_TRUE(::MoveFile(&file[0], &file2[0])); - EXPECT_TRUE(utils::file::is_file(file2)); - EXPECT_FALSE(utils::file::is_file(file)); + EXPECT_TRUE(utils::file::file(file2).exists()); + EXPECT_FALSE(utils::file::file(file).exists()); api_meta_map meta2{}; EXPECT_EQ(api_error::success, diff --git a/scripts/copy_mingw64_deps.sh b/scripts/copy_mingw64_deps.sh index c2772b06..5a1b0671 100755 --- a/scripts/copy_mingw64_deps.sh +++ b/scripts/copy_mingw64_deps.sh @@ -18,82 +18,139 @@ if [ "${PROJECT_IS_MINGW}" == "1" ] && [ "${PROJECT_STATIC_LINK}" == "OFF" ]; th /mingw64/bin/zlib1.dll ) - if [ "${PROJECT_IS_MINGW_UNIX}" == "1" ]; then - if [ "${PROJECT_ENABLE_BOOST}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libboost*.dll) - fi + if [ "${PROJECT_ENABLE_BOOST}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libboost*.dll) + fi + if [ "${PROJECT_ENABLE_CLI11}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libcli11*.dll) + fi + + if [ "${PROJECT_ENABLE_FLAC}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libFLAC*.dll) + fi + + if [ "${PROJECT_ENABLE_FONTCONFIG}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=( + /mingw64/bin/libexpat*.dll + /mingw64/bin/libfontconfig*.dll + ) + fi + + if [ "${PROJECT_ENABLE_FREETYPE2}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libfreetype*.dll) + fi + + if [ "${PROJECT_ENABLE_LIBDSM}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libdsm.dll) + fi + + if [ "${PROJECT_ENABLE_LIBEVENT}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libevent*.dll) + fi + + if [ "${PROJECT_ENABLE_LIBICONV}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libiconv.dll) + fi + + if [ "${PROJECT_ENABLE_LIBJPEG_TURBO}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libjpeg*.dll) + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libturbo*.dll) + fi + + if [ "${PROJECT_ENABLE_LIBPNG}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libpng*.dll) + fi + + if [ "${PROJECT_ENABLE_LIBSODIUM}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libsodium*.dll) + fi + + if [ "${PROJECT_ENABLE_NANA}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libnana*.dll) + fi + + if [ "${PROJECT_ENABLE_NUSPELL}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libnuspell-*.dll) + fi + + if [ "${PROJECT_ENABLE_OGG}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libogg*.dll) + fi + + if [ "${PROJECT_ENABLE_OPENAL}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libopenal*.dll) + fi + + if [ "${PROJECT_ENABLE_OPENSSL}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=( + /mingw64/bin/libssl*.dll + /mingw64/bin/libcrypt*.dll + ) + fi + + if [ "${PROJECT_ENABLE_PUGIXML}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libpugi*.dll) + fi + + if [ "${PROJECT_ENABLE_SDL}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/SDL2*.dll) + fi + + if [ "${PROJECT_ENABLE_SFML}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libsfml*.dll) + fi + + if [ "${PROJECT_ENABLE_SQLITE}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libsqlite3-*.dll) + fi + + if [ "${PROJECT_ENABLE_TASN}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libtasn1.dll) + fi + + if [ "${PROJECT_ENABLE_TESTING}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=( + /mingw64/bin/libgtest*.dll + /mingw64/bin/libgmock*.dll + ) + fi + + if [ "${PROJECT_ENABLE_VORBIS}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libvorbis*.dll) + fi + + if [ "${PROJECT_ENABLE_WXWIDGETS}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libwxm*.dll) + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/wxm*.dll) + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/lib/gcc_x64_dll/libwxm*.dll) + PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/lib/gcc_x64_dll/wxm*.dll) + fi + + if [ "${PROJECT_ENABLE_SDL}" == "ON" ] || [ "${PROJECT_ENABLE_SFML}" == "ON" ] || + [ "${PROJECT_ENABLE_WXWIDGETS}" == "ON" ] || [ "${PROJECT_ENABLE_NANA}" == "ON" ]; then + PROJECT_MINGW64_COPY_DEPENDENCIES+=( + /mingw64/bin/libOpenCL*.dll + /mingw64/bin/libopencl*.dll + /mingw64/bin/libva*.dll + /mingw64/bin/opengl*.dll + /mingw64/bin/vulkan*.dll + ) + fi + + if [ "${PROJECT_IS_MINGW_UNIX}" == "1" ]; then if [ "${PROJECT_ENABLE_BACKWARD_CPP}" == "ON" ]; then PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/msvcr90.dll) fi - if [ "${PROJECT_ENABLE_CLI11}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libcli11*.dll) - fi - - if [ "${PROJECT_ENABLE_FLAC}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libFLAC*.dll) - fi - if [ "${PROJECT_ENABLE_FMT}" == "ON" ]; then PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libfmt*.dll) fi - if [ "${PROJECT_ENABLE_FONTCONFIG}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=( - /mingw64/bin/libexpat*.dll - /mingw64/bin/libfontconfig*.dll - ) - fi - - if [ "${PROJECT_ENABLE_FREETYPE2}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libfreetype*.dll) - fi - if [ "${PROJECT_ENABLE_CURL}" == "ON" ]; then PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libcurl*.dll) fi - if [ "${PROJECT_ENABLE_LIBJPEG_TURBO}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libjpeg*.dll) - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libturbo*.dll) - fi - - if [ "${PROJECT_ENABLE_LIBSODIUM}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libsodium*.dll) - fi - - if [ "${PROJECT_ENABLE_NUSPELL}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libnuspell*.dll) - fi - - if [ "${PROJECT_ENABLE_OGG}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libogg*.dll) - fi - - if [ "${PROJECT_ENABLE_OPENSSL}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=( - /mingw64/bin/libssl*.dll - /mingw64/bin/libcrypt*.dll - ) - fi - - if [ "${PROJECT_ENABLE_OPENAL}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libopenal*.dll) - fi - - if [ "${PROJECT_ENABLE_LIBPNG}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libpng*.dll) - fi - - if [ "${PROJECT_ENABLE_NANA}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libnana*.dll) - fi - - if [ "${PROJECT_ENABLE_PUGIXML}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libpugi*.dll) - fi - if [ "${PROJECT_ENABLE_ROCKSDB}" == "ON" ]; then PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/librocksdb*.dll) fi @@ -102,14 +159,6 @@ if [ "${PROJECT_IS_MINGW}" == "1" ] && [ "${PROJECT_STATIC_LINK}" == "OFF" ]; th PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libsecp256k1*.dll) fi - if [ "${PROJECT_ENABLE_SDL}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/SDL*.dll) - fi - - if [ "${PROJECT_ENABLE_SFML}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libsfml*.dll) - fi - if [ "${PROJECT_ENABLE_SPDLOG}" == "ON" ]; then PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libspdlog*.dll) fi @@ -117,21 +166,6 @@ if [ "${PROJECT_IS_MINGW}" == "1" ] && [ "${PROJECT_STATIC_LINK}" == "OFF" ]; th if [ "${PROJECT_ENABLE_TPL}" == "ON" ]; then PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libtiny-process*.dll) fi - - if [ "${PROJECT_ENABLE_VORBIS}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libvorbis*.dll) - fi - - if [ "${PROJECT_ENABLE_WXWIDGETS}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libwxm*.dll) - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/wxm*.dll) - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/lib/gcc_x64_dll/libwxm*.dll) - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/lib/gcc_x64_dll/wxm*.dll) - fi - fi - - if [ "${PROJECT_ENABLE_SFML}" == "ON" ] || [ "${PROJECT_ENABLE_WXWIDGETS}" == "ON" ]; then - PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/opengl*.dll) fi rsync -av --progress ${PROJECT_EXTERNAL_BUILD_ROOT}/bin/*.dll "${PROJECT_DIST_DIR}/" @@ -151,6 +185,10 @@ if [ "${PROJECT_IS_MINGW}" == "1" ] && [ "${PROJECT_ENABLE_WINFSP}" == "ON" ]; t fi fi +if [ "${PROJECT_IS_MINGW}" == "1" ] && [ "${PROJECT_ENABLE_VLC}" == "ON" ]; then + rsync -av --progress ${PROJECT_3RD_PARTY_DIR}/vlc/ "${PROJECT_DIST_DIR}/vlc/" +fi + for PROJECT_DEPENDENCY in "${PROJECT_MINGW64_COPY_DEPENDENCIES[@]}"; do rsync -av --progress ${PROJECT_DEPENDENCY} "${PROJECT_DIST_DIR}/" done diff --git a/scripts/env.sh b/scripts/env.sh index 75cfb19b..b65d2e0b 100755 --- a/scripts/env.sh +++ b/scripts/env.sh @@ -25,6 +25,7 @@ pushd "${PROJECT_SOURCE_DIR}" PROJECT_GIT_REV=$(git rev-parse --short HEAD) +. "${PROJECT_SCRIPTS_DIR}/versions.sh" . "${PROJECT_SCRIPTS_DIR}/options.sh" for PROJECT_LIBRARY in "${PROJECT_LIBRARIES[@]}"; do @@ -40,6 +41,7 @@ done PROJECT_APP_LIST=() PROJECT_CMAKE_OPTS="" +PROJECT_ENABLE_WIN32_LONG_PATH_NAMES=OFF PROJECT_IS_ALPINE=0 PROJECT_IS_ARM64=0 PROJECT_MINGW64_COPY_DEPENDENCIES=() @@ -73,6 +75,10 @@ fi . "${PROJECT_SOURCE_DIR}/config.sh" +if [ "${PROJECT_IS_MINGW}" == "0" ]; then + PROJECT_ENABLE_WIN32_LONG_PATH_NAMES=OFF +fi + if [ "${PROJECT_ENABLE_SFML}" == "ON" ]; then PROJECT_ENABLE_FLAC=ON PROJECT_ENABLE_FONTCONFIG=ON @@ -123,7 +129,9 @@ if [ "${PROJECT_ENABLE_BOOST}" == "ON" ]; then PROJECT_ENABLE_OPENSSL=ON fi -if [ "${PROJECT_ENABLE_FONTCONFIG}" == "ON" ] || [ "${PROJECT_ENABLE_NANA}" == "ON" ] || [ "${PROJECT_ENABLE_WXWIDGETS}" == "ON" ]; then +if [ "${PROJECT_ENABLE_FONTCONFIG}" == "ON" ] || [ "${PROJECT_ENABLE_NANA}" == "ON" ] || + [ "${PROJECT_ENABLE_SFML}" == "ON" ] || [ "${PROJECT_ENABLE_WXWIDGETS}" == "ON" ] || + [ "${PROJECT_ENABLE_SDL}" == "ON" ]; then PROJECT_ENABLE_LIBJPEG_TURBO=ON PROJECT_ENABLE_LIBPNG=ON fi @@ -132,8 +140,10 @@ if [ "${PROJECT_IS_MINGW}" == "1" ]; then PROJECT_ENABLE_BACKWARD_CPP=OFF fi -if [ "${PROJECT_IS_MINGW}" == "1" ] && [ "${PROJECT_IS_MINGW_UNIX}" != "1" ]; then - MSYS=winsymlinks:nativestrict +if [ "${PROJECT_ENABLE_LIBDSM}" == "ON" ]; then + PROJECT_ENABLE_LIBICONV=ON + PROJECT_ENABLE_LIBTASN=ON + PROJECT_ENABLE_OPENSSL=ON fi if [ "${PROJECT_CMAKE_BUILD_TYPE}" == "" ]; then @@ -147,30 +157,21 @@ elif [ "${PROJECT_CMAKE_BUILD_TYPE_LOWER}" == "debug" ]; then PROJECT_CMAKE_BUILD_TYPE=Debug fi +if [ "${PROJECT_IS_MINGW}" == "1" ] && [ "${PROJECT_IS_MINGW_UNIX}" != "1" ]; then + PROJECT_STATIC_LINK=OFF +fi + if [ "${PROJECT_STATIC_LINK}" == "ON" ]; then + PROJECT_BUILD_SHARED_LIBS=OFF + PROJECT_ENABLE_BACKWARD_CPP=OFF PROJECT_LINK_TYPE=static PROJECT_REQUIRE_ALPINE=ON else + PROJECT_BUILD_SHARED_LIBS=ON PROJECT_LINK_TYPE=shared PROJECT_REQUIRE_ALPINE=OFF fi -if [ -f "${PROJECT_SOURCE_DIR}/cmake/versions.cmake" ]; then - VERSIONS=($(sed -e s/\ /=/g -e s/set\(//g -e s/\)//g "${PROJECT_SOURCE_DIR}/cmake/versions.cmake")) - - PROJECT_MINGW64_DOCKER_BUILD_ARGS=() - - for VERSION in "${VERSIONS[@]}"; do - LOOKUP_NAME=$(echo ${VERSION} | sed s/_VERSION.*// | sed s/GTEST/TESTING/g) - ENABLE_NAME=PROJECT_ENABLE_${LOOKUP_NAME} - if [ "${!ENABLE_NAME}" != "OFF" ]; then - PROJECT_MINGW64_DOCKER_BUILD_ARGS+=("--build-arg ${VERSION}") - fi - done - - PROJECT_MINGW64_DOCKER_BUILD_ARGS=${PROJECT_MINGW64_DOCKER_BUILD_ARGS[*]} -fi - PROJECT_BUILD_DIR=${PROJECT_SOURCE_DIR}/build/${PROJECT_CMAKE_BUILD_TYPE_LOWER}/${PROJECT_LINK_TYPE} PROJECT_DIST_DIR=${PROJECT_SOURCE_DIR}/dist/${PROJECT_CMAKE_BUILD_TYPE_LOWER}/${PROJECT_LINK_TYPE} @@ -191,13 +192,13 @@ else fi PROJECT_DIST_DIR=${PROJECT_DIST_DIR}/${PROJECT_NAME} - PROJECT_EXTERNAL_BUILD_ROOT=${PROJECT_BUILD_DIR}/deps + PROJECT_SUPPORT_DIR=${PROJECT_SOURCE_DIR}/support PROJECT_3RD_PARTY_DIR=${PROJECT_SUPPORT_DIR}/3rd_party if [ "${PROJECT_ENABLE_OPENSSL}" == "ON" ]; then - if [ "${PROJECT_IS_MINGW}" == "1" ] && [ "${PROJECT_IS_MINGW_UNIX}" == "1" ]; then + if [ "${PROJECT_IS_MINGW}" == "1" ]; then OPENSSL_ROOT_DIR=/mingw64 else OPENSSL_ROOT_DIR=${PROJECT_EXTERNAL_BUILD_ROOT} @@ -210,11 +211,20 @@ if [ "${PROJECT_IS_MINGW}" == "1" ] && [ "${PROJECT_IS_MINGW_UNIX}" == "1" ]; th PROJECT_CMAKE_OPTS="-DCMAKE_TOOLCHAIN_FILE=${PROJECT_TOOLCHAIN_FILE_CMAKE} ${PROJECT_CMAKE_OPTS}" fi -if [ "${PROJECT_STATIC_LINK}" == "ON" ]; then - PROJECT_BUILD_SHARED_LIBS=OFF - PROJECT_ENABLE_BACKWARD_CPP=OFF -else - PROJECT_BUILD_SHARED_LIBS=ON +if [ -f "${PROJECT_SOURCE_DIR}/cmake/versions.cmake" ]; then + VERSIONS=($(sed -e s/\ /=/g -e s/set\(//g -e s/\)//g "${PROJECT_SOURCE_DIR}/cmake/versions.cmake")) + + PROJECT_MINGW64_DOCKER_BUILD_ARGS=() + + for VERSION in "${VERSIONS[@]}"; do + LOOKUP_NAME=$(echo ${VERSION} | sed s/_VERSION.*// | sed s/GTEST/TESTING/g) + ENABLE_NAME=PROJECT_ENABLE_${LOOKUP_NAME} + if [ "${!ENABLE_NAME}" != "OFF" ]; then + PROJECT_MINGW64_DOCKER_BUILD_ARGS+=("--build-arg ${VERSION}") + fi + done + + PROJECT_MINGW64_DOCKER_BUILD_ARGS=${PROJECT_MINGW64_DOCKER_BUILD_ARGS[*]} fi PROJECT_CMAKE_OPTS="-DPROJECT_3RD_PARTY_DIR=${PROJECT_3RD_PARTY_DIR} ${PROJECT_CMAKE_OPTS}" @@ -223,6 +233,7 @@ PROJECT_CMAKE_OPTS="-DPROJECT_BUILD_DIR=${PROJECT_BUILD_DIR} ${PROJECT_CMAKE_OPT PROJECT_CMAKE_OPTS="-DPROJECT_BUILD_SHARED_LIBS=${PROJECT_BUILD_SHARED_LIBS} ${PROJECT_CMAKE_OPTS}" PROJECT_CMAKE_OPTS="-DPROJECT_CMAKE_BUILD_TYPE=${PROJECT_CMAKE_BUILD_TYPE} ${PROJECT_CMAKE_OPTS}" PROJECT_CMAKE_OPTS="-DPROJECT_DIST_DIR=${PROJECT_DIST_DIR} ${PROJECT_CMAKE_OPTS}" +PROJECT_CMAKE_OPTS="-DPROJECT_ENABLE_WIN32_LONG_PATH_NAMES=${PROJECT_ENABLE_WIN32_LONG_PATH_NAMES} ${PROJECT_CMAKE_OPTS}" PROJECT_CMAKE_OPTS="-DPROJECT_EXTERNAL_BUILD_ROOT=${PROJECT_EXTERNAL_BUILD_ROOT} ${PROJECT_CMAKE_OPTS}" PROJECT_CMAKE_OPTS="-DPROJECT_GIT_REV=${PROJECT_GIT_REV} ${PROJECT_CMAKE_OPTS}" PROJECT_CMAKE_OPTS="-DPROJECT_IS_ALPINE=${PROJECT_IS_ALPINE} ${PROJECT_CMAKE_OPTS}" @@ -268,6 +279,10 @@ if [ "${PROJECT_IS_MINGW}" == "1" ]; then PROJECT_APP_BINARY_EXT=.exe fi +if [ "${PROJECT_IS_MINGW}" == "1" ] && [ "${PROJECT_IS_MINGW_UNIX}" != "1" ]; then + MSYS=winsymlinks:nativestrict +fi + export MSYS export NUM_JOBS export OPENSSL_ROOT_DIR @@ -288,6 +303,7 @@ export PROJECT_COMPANY_NAME export PROJECT_COPYRIGHT export PROJECT_DESC export PROJECT_DIST_DIR +export PROJECT_ENABLE_WIN32_LONG_PATH_NAMES export PROJECT_FILE_PART export PROJECT_GIT_REV export PROJECT_IS_ALPINE @@ -346,6 +362,9 @@ echo " Is MINGW on Unix: ${PROJECT_IS_MINGW_UNIX}" echo " Is MINGW: ${PROJECT_IS_MINGW}" echo " Job count: ${NUM_JOBS}" echo " Link type: ${PROJECT_LINK_TYPE}" +if [ "${PROJECT_IS_MINGW}" == "1" ]; then + echo " Long path names: ${PROJECT_ENABLE_WIN32_LONG_PATH_NAMES}" +fi echo " Meson toolchain file: ${PROJECT_TOOLCHAIN_FILE_MESON}" if [ "${PROJECT_IS_MINGW}" == "1" ] && [ "${PROJECT_IS_MINGW_UNIX}" == "1" ]; then echo " MinGW docker build args: ${PROJECT_MINGW64_DOCKER_BUILD_ARGS}" diff --git a/scripts/make_common.sh b/scripts/make_common.sh index e612ac5c..1ae6b863 100755 --- a/scripts/make_common.sh +++ b/scripts/make_common.sh @@ -15,6 +15,8 @@ if [ -f "${PROJECT_SOURCE_DIR}/pre_build.sh" ]; then "${PROJECT_SOURCE_DIR}/pre_build.sh" fi +ln -sf "${PROJECT_BUILD_DIR}/build/compile_commands.json" "${PROJECT_SOURCE_DIR}/compile_commands.json" + pushd "${PROJECT_BUILD_DIR}" cmake "${PROJECT_SOURCE_DIR}" \ -G"Unix Makefiles" \ @@ -80,8 +82,6 @@ if [ "${PROJECT_IS_MINGW}" == "1" ]; then . "${PROJECT_SCRIPTS_DIR}/copy_mingw64_deps.sh" "$1" "$2" "$3" "$4" "$5" fi -ln -sf "${PROJECT_BUILD_DIR}/build/compile_commands.json" "${PROJECT_SOURCE_DIR}/compile_commands.json" - if [ -f "${PROJECT_SOURCE_DIR}/post_build.sh" ]; then "${PROJECT_SOURCE_DIR}/post_build.sh" fi diff --git a/scripts/options.sh b/scripts/options.sh index d88aba19..6a24e91b 100755 --- a/scripts/options.sh +++ b/scripts/options.sh @@ -1,7 +1,6 @@ #!/bin/bash PROJECT_LIBRARIES=( - BACKWARD_CPP BOOST CPP_HTTPLIB CURL @@ -18,13 +17,12 @@ PROJECT_LIBRARIES=( ) declare -A PROJECT_CLEANUP -PROJECT_CLEANUP[BACKWARD_CPP]="include/backward.hpp:src/backward.cpp" PROJECT_CLEANUP[BOOST]="3rd_party/boost_*" PROJECT_CLEANUP[CPP_HTTPLIB]="3rd_party/cpp-httplib-*" PROJECT_CLEANUP[CURL]="3rd_party/curl-*" PROJECT_CLEANUP[JSON]="3rd_party/json-*" -PROJECT_CLEANUP[LIBBITCOIN_SYSTEM_ON]="3rd_party/boost_1_85_*" -PROJECT_CLEANUP[LIBSODIUM]="3rd_party/libsodium*" +PROJECT_CLEANUP[LIBBITCOIN_SYSTEM_ON]="3rd_party/boost_${PROJECT_VERSIONS[BOOST_MAJOR]}_${PROJECT_VERSIONS[BOOST_MINOR]}_*" +PROJECT_CLEANUP[LIBSODIUM]="3rd_party/libsodium-*:3rd_party/libsodium*" PROJECT_CLEANUP[OPENSSL]="3rd_party/openssl-*" PROJECT_CLEANUP[PUGIXML]="3rd_party/pugixml-*" PROJECT_CLEANUP[SPDLOG]="3rd_party/spdlog-*" diff --git a/scripts/setup_msys2.sh b/scripts/setup_msys2.sh index b4181507..7a91c90d 100755 --- a/scripts/setup_msys2.sh +++ b/scripts/setup_msys2.sh @@ -33,6 +33,154 @@ PROJECT_MSYS2_PACKAGE_LIST+=( make ) +if [ "${PROJECT_ENABLE_BOOST}" == "ON" ]; then + if [ "${PROJECT_ENABLE_LIBBITCOIN_SYSTEM}" == "ON" ]; then + yes | pacman -Ry mingw64/mingw-w64-x86_64-boost + else + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-boost + ) + fi +fi + +if [ "${PROJECT_ENABLE_CLI11}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-cli11 + ) +fi + +if [ "${PROJECT_ENABLE_FLAC}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-flac + ) +fi + +if [ "${PROJECT_ENABLE_FONTCONFIG}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-fontconfig + ) +fi + +if [ "${PROJECT_ENABLE_FREETYPE2}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-freetype + ) +fi + +if [ "${PROJECT_ENABLE_LIBDSM}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-libdsm + ) +fi + +if [ "${PROJECT_ENABLE_LIBEVENT}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-libevent + ) +fi + +if [ "${PROJECT_ENABLE_LIBICONV}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-libiconv + ) +fi + +if [ "${PROJECT_ENABLE_LIBJPEG_TURBO}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-libjpeg-turbo + ) +fi + +if [ "${PROJECT_ENABLE_LIBPNG}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-libpng + ) +fi + +if [ "${PROJECT_ENABLE_LIBSODIUM}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-libsodium + ) +fi + +if [ "${PROJECT_ENABLE_LIBTASN}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-libtasn1 + ) +fi + +if [ "${PROJECT_ENABLE_NANA}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-nana + ) +fi + +if [ "${PROJECT_ENABLE_NUSPELL}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-nuspell + ) +fi + +if [ "${PROJECT_ENABLE_OGG}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-libogg + ) +fi + +if [ "${PROJECT_ENABLE_OPENAL}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-openal + ) +fi + +if [ "${PROJECT_ENABLE_OPENSSL}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-openssl + ) +fi + +if [ "${PROJECT_ENABLE_PUGIXML}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-pugixml + ) +fi + +if [ "${PROJECT_ENABLE_SDL}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-SDL2 + ) +fi + +if [ "${PROJECT_ENABLE_SFML}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-sfml + ) +fi + +if [ "${PROJECT_ENABLE_SQLITE}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-sqlite3 + ) +fi + +if [ "${PROJECT_ENABLE_TESTING}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-gtest + ) +fi + +if [ "${PROJECT_ENABLE_VORBIS}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-libvorbis + ) +fi + +if [ "${PROJECT_ENABLE_WXWIDGETS}" == "ON" ]; then + PROJECT_MSYS2_PACKAGE_LIST+=( + mingw64/mingw-w64-x86_64-wxwidgets3.2-msw + ) +fi + pacman -Sqyuu --noconfirm && pacman -S --noconfirm --needed --disable-download-timeout msys2-keyring && pacman -S --noconfirm --needed --disable-download-timeout \ diff --git a/scripts/versions.sh b/scripts/versions.sh new file mode 100755 index 00000000..76b751e0 --- /dev/null +++ b/scripts/versions.sh @@ -0,0 +1,101 @@ +#!/bin/bash + +declare -A PROJECT_VERSIONS +PROJECT_VERSIONS[BINUTILS]="2.41" +PROJECT_VERSIONS[BOOST2_MAJOR]="1" +PROJECT_VERSIONS[BOOST2_MINOR]="76" +PROJECT_VERSIONS[BOOST2_PATCH]="0" +PROJECT_VERSIONS[BOOST_MAJOR]="1" +PROJECT_VERSIONS[BOOST_MINOR]="85" +PROJECT_VERSIONS[BOOST_PATCH]="0" +PROJECT_VERSIONS[CLI11]="2.4.2" +PROJECT_VERSIONS[CPP_HTTPLIB]="0.16.3" +PROJECT_VERSIONS[CURL]="8.9.1" +PROJECT_VERSIONS[CURL2]="8_9_1" +PROJECT_VERSIONS[CXXOPTS]="3.2.1" +PROJECT_VERSIONS[DTL]="2.01" +PROJECT_VERSIONS[EXPAT]="2.6.2" +PROJECT_VERSIONS[EXPAT2]="2_6_2" +PROJECT_VERSIONS[FLAC]="1.4.3" +PROJECT_VERSIONS[FMT]="11.0.2" +PROJECT_VERSIONS[FONTCONFIG]="2.15.0" +PROJECT_VERSIONS[FREETYPE2]="2.13.3" +PROJECT_VERSIONS[GCC]="14.2.0" +PROJECT_VERSIONS[GTEST]="1.15.2" +PROJECT_VERSIONS[GTKMM]="3.0" +PROJECT_VERSIONS[ICU]="75-1" +PROJECT_VERSIONS[JSON]="3.11.3" +PROJECT_VERSIONS[LIBBITCOIN_SYSTEM]="3.8.0" +PROJECT_VERSIONS[LIBDSM]="0.4.3" +PROJECT_VERSIONS[LIBEVENT]="2.1.12" +PROJECT_VERSIONS[LIBICONV]="1.17" +PROJECT_VERSIONS[LIBJPEG_TURBO]="3.0.3" +PROJECT_VERSIONS[LIBPNG]="1.6.43" +PROJECT_VERSIONS[LIBSODIUM]="1.0.20" +PROJECT_VERSIONS[LIBTASN]="4.19.0" +PROJECT_VERSIONS[MESA]="23.3.3" +PROJECT_VERSIONS[MINGW]="11.0.1" +PROJECT_VERSIONS[NANA]="1.7.4" +PROJECT_VERSIONS[NUSPELL]="5.1.6" +PROJECT_VERSIONS[OGG]="1.3.5" +PROJECT_VERSIONS[OPENAL]="1.23.1" +PROJECT_VERSIONS[OPENSSL]="3.3.1" +PROJECT_VERSIONS[PKG_CONFIG]="0.29.2" +PROJECT_VERSIONS[PUGIXML]="1.14" +PROJECT_VERSIONS[ROCKSDB]="9.5.2" +PROJECT_VERSIONS[SDL]="2.30.6" +PROJECT_VERSIONS[SECP256K1]="0.1.0.20" +PROJECT_VERSIONS[SFML]="2.6.1" +PROJECT_VERSIONS[SPDLOG]="1.14.1" +PROJECT_VERSIONS[SQLITE]="3460100" +PROJECT_VERSIONS[SQLITE2]="3.46.1" +PROJECT_VERSIONS[STDUUID]="1.2.3" +PROJECT_VERSIONS[VLC]="3.0" +PROJECT_VERSIONS[VORBIS]="1.3.7" +PROJECT_VERSIONS[WXWIDGETS]="3.2.5" +PROJECT_VERSIONS[ZLIB]="1.3.1" +export PROJECT_VERSIONS + +declare -A PROJECT_DOWNLOADS +PROJECT_DOWNLOADS[BINUTILS]="https://ftp.gnu.org/gnu/binutils/binutils-${PROJECT_VERSIONS[BINUTILS]}.tar.xz;binutils-${PROJECT_VERSIONS[BINUTILS]}.tar.xz;3rd_party/mingw64" +PROJECT_DOWNLOADS[BOOST2]="https://archives.boost.io/release/${PROJECT_VERSIONS[BOOST2_MAJOR]}.${PROJECT_VERSIONS[BOOST2_MINOR]}.${PROJECT_VERSIONS[BOOST2_PATCH]}/source/boost_${PROJECT_VERSIONS[BOOST2_MAJOR]}_${PROJECT_VERSIONS[BOOST2_MINOR]}_${PROJECT_VERSIONS[BOOST2_PATCH]}.tar.gz;boost_${PROJECT_VERSIONS[BOOST2_MAJOR]}_${PROJECT_VERSIONS[BOOST2_MINOR]}_${PROJECT_VERSIONS[BOOST2_PATCH]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[BOOST]="https://archives.boost.io/release/${PROJECT_VERSIONS[BOOST_MAJOR]}.${PROJECT_VERSIONS[BOOST_MINOR]}.${PROJECT_VERSIONS[BOOST_PATCH]}/source/boost_${PROJECT_VERSIONS[BOOST_MAJOR]}_${PROJECT_VERSIONS[BOOST_MINOR]}_${PROJECT_VERSIONS[BOOST_PATCH]}.tar.gz;boost_${PROJECT_VERSIONS[BOOST_MAJOR]}_${PROJECT_VERSIONS[BOOST_MINOR]}_${PROJECT_VERSIONS[BOOST_PATCH]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[CLI11]="https://github.com/CLIUtils/CLI11/archive/refs/tags/v${PROJECT_VERSIONS[CLI11]}.tar.gz;CLI11-${PROJECT_VERSIONS[CLI11]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[CPP_HTTPLIB]="https://github.com/yhirose/cpp-httplib/archive/refs/tags/v${PROJECT_VERSIONS[CPP_HTTPLIB]}.tar.gz;cpp-httplib-${PROJECT_VERSIONS[CPP_HTTPLIB]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[CURL]="https://github.com/curl/curl/archive/refs/tags/curl-${PROJECT_VERSIONS[CURL2]}.tar.gz;curl-${PROJECT_VERSIONS[CURL]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[CXXOPTS]="https://github.com/jarro2783/cxxopts/archive/refs/tags/v${PROJECT_VERSIONS[CXXOPTS]}.tar.gz;cxxopts-v${PROJECT_VERSIONS[CXXOPTS]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[EXPAT]="https://github.com/libexpat/libexpat/archive/refs/tags/R_${PROJECT_VERSIONS[EXPAT2]}.tar.gz;expat-${PROJECT_VERSIONS[EXPAT]}.tar.gz;3rd_party/mingw64" +PROJECT_DOWNLOADS[FLAC]="https://github.com/xiph/flac/archive/refs/tags/${PROJECT_VERSIONS[FLAC]}.tar.gz;flac-${PROJECT_VERSIONS[FLAC]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[FMT]="https://github.com/fmtlib/fmt/archive/refs/tags/${PROJECT_VERSIONS[FMT]}.tar.gz;fmt-${PROJECT_VERSIONS[FMT]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[FONTCONFIG]="https://www.freedesktop.org/software/fontconfig/release/fontconfig-${PROJECT_VERSIONS[FONTCONFIG]}.tar.gz;fontconfig-${PROJECT_VERSIONS[FONTCONFIG]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[FREETYPE2]="https://download.savannah.gnu.org/releases/freetype/freetype-${PROJECT_VERSIONS[FREETYPE2]}.tar.gz;freetype-${PROJECT_VERSIONS[FREETYPE2]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[GCC]="https://ftp.gnu.org/gnu/gcc/gcc-${PROJECT_VERSIONS[GCC]}/gcc-${PROJECT_VERSIONS[GCC]}.tar.gz;gcc-${PROJECT_VERSIONS[GCC]}.tar.gz;3rd_party/mingw64" +PROJECT_DOWNLOADS[GTEST]="https://github.com/google/googletest/archive/refs/tags/v${PROJECT_VERSIONS[GTEST]}.tar.gz;googletest-${PROJECT_VERSIONS[GTEST]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[ICU]="https://github.com/unicode-org/icu/archive/refs/tags/release-${PROJECT_VERSIONS[ICU]}.tar.gz;icu-release-${PROJECT_VERSIONS[ICU]}.tar.gz;3rd_party/mingw64" +PROJECT_DOWNLOADS[JSON]="https://github.com/nlohmann/json/archive/refs/tags/v${PROJECT_VERSIONS[JSON]}.tar.gz;json-${PROJECT_VERSIONS[JSON]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[LIBDSM]="https://github.com/videolabs/libdsm/archive/refs/tags/v${PROJECT_VERSIONS[LIBDSM]}.tar.gz;libdsm-${PROJECT_VERSIONS[LIBDSM]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[LIBEVENT]="https://github.com/libevent/libevent/archive/refs/tags/release-${PROJECT_VERSIONS[LIBEVENT]}-stable.tar.gz;libevent-${PROJECT_VERSIONS[LIBEVENT]}-stable.tar.gz;3rd_party" +PROJECT_DOWNLOADS[LIBICONV]="https://ftp.gnu.org/pub/gnu/libiconv/libiconv-${PROJECT_VERSIONS[LIBICONV]}.tar.gz;libiconv-${PROJECT_VERSIONS[LIBICONV]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[LIBJPEG_TURBO]="https://github.com/libjpeg-turbo/libjpeg-turbo/archive/refs/tags/${PROJECT_VERSIONS[LIBJPEG_TURBO]}.tar.gz;libjpeg_turbo-${PROJECT_VERSIONS[LIBJPEG_TURBO]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[LIBPNG]="https://github.com/pnggroup/libpng/archive/refs/tags/v${PROJECT_VERSIONS[LIBPNG]}.tar.gz;libpng-v${PROJECT_VERSIONS[LIBPNG]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[LIBSODIUM]="https://github.com/jedisct1/libsodium/archive/refs/tags/${PROJECT_VERSIONS[LIBSODIUM]}-RELEASE.tar.gz;libsodium-${PROJECT_VERSIONS[LIBSODIUM]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[LIBTASN]="https://ftp.gnu.org/gnu/libtasn1/libtasn1-${PROJECT_VERSIONS[LIBTASN]}.tar.gz;libtasn1-${PROJECT_VERSIONS[LIBTASN]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[MINGW]="https://phoenixnap.dl.sourceforge.net/project/mingw-w64/mingw-w64/mingw-w64-release/mingw-w64-v${PROJECT_VERSIONS[MINGW]}.tar.bz2;mingw-w64-v${PROJECT_VERSIONS[MINGW]}.tar.bz2;3rd_party/mingw64" +PROJECT_DOWNLOADS[NANA]="https://github.com/cnjinhao/nana/archive/refs/tags/v${PROJECT_VERSIONS[NANA]}.tar.gz;nana-v${PROJECT_VERSIONS[NANA]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[NUSPELL]="https://github.com/nuspell/nuspell/archive/refs/tags/v${PROJECT_VERSIONS[NUSPELL]}.tar.gz;nuspell-v${PROJECT_VERSIONS[NUSPELL]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[OGG]="https://github.com/xiph/ogg/releases/download/v${PROJECT_VERSIONS[OGG]}/libogg-${PROJECT_VERSIONS[OGG]}.tar.gz;ogg-v${PROJECT_VERSIONS[OGG]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[OPENAL]="https://github.com/kcat/openal-soft/archive/refs/tags/${PROJECT_VERSIONS[OPENAL]}.tar.gz;openal-${PROJECT_VERSIONS[OPENAL]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[OPENSSL]="https://github.com/openssl/openssl/releases/download/openssl-${PROJECT_VERSIONS[OPENSSL]}/openssl-${PROJECT_VERSIONS[OPENSSL]}.tar.gz;openssl-${PROJECT_VERSIONS[OPENSSL]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[PKG_CONFIG]="https://pkgconfig.freedesktop.org/releases/pkg-config-${PROJECT_VERSIONS[PKG_CONFIG]}.tar.gz;pkg-config-${PROJECT_VERSIONS[PKG_CONFIG]}.tar.gz;3rd_party/mingw64" +PROJECT_DOWNLOADS[PUGIXML]="https://github.com/zeux/pugixml/releases/download/v${PROJECT_VERSIONS[PUGIXML]}/pugixml-${PROJECT_VERSIONS[PUGIXML]}.tar.gz;pugixml-${PROJECT_VERSIONS[PUGIXML]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[ROCKSDB]="https://github.com/facebook/rocksdb/archive/refs/tags/v${PROJECT_VERSIONS[ROCKSDB]}.tar.gz;rocksdb-${PROJECT_VERSIONS[ROCKSDB]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[SDL]="https://github.com/libsdl-org/SDL/archive/refs/tags/release-${PROJECT_VERSIONS[SDL]}.tar.gz;sdl-${PROJECT_VERSIONS[SDL]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[SECP256K1]="https://github.com/libbitcoin/secp256k1/archive/refs/tags/v${PROJECT_VERSIONS[SECP256K1]}.tar.gz;secp256k1-${PROJECT_VERSIONS[SECP256K1]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[SFML]="https://github.com/SFML/SFML/archive/refs/tags/${PROJECT_VERSIONS[SFML]}.tar.gz;SFML-${PROJECT_VERSIONS[SFML]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[SPDLOG]="https://github.com/gabime/spdlog/archive/refs/tags/v${PROJECT_VERSIONS[SPDLOG]}.tar.gz;spdlog-${PROJECT_VERSIONS[SPDLOG]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[SQLITE]="https://www.sqlite.org/2024/sqlite-amalgamation-${PROJECT_VERSIONS[SQLITE]}.zip;sqlite-amalgamation-${PROJECT_VERSIONS[SQLITE]}.zip;3rd_party" +PROJECT_DOWNLOADS[STDUUID]="https://github.com/mariusbancila/stduuid/archive/refs/tags/v${PROJECT_VERSIONS[STDUUID]}.tar.gz;stduuid-${PROJECT_VERSIONS[STDUUID]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[VORBIS]="https://github.com/xiph/vorbis/archive/refs/tags/v${PROJECT_VERSIONS[VORBIS]}.tar.gz;vorbis-v${PROJECT_VERSIONS[VORBIS]}.tar.gz;3rd_party" +PROJECT_DOWNLOADS[WXWIDGETS]="https://github.com/wxWidgets/wxWidgets/releases/download/v${PROJECT_VERSIONS[WXWIDGETS]}/wxWidgets-${PROJECT_VERSIONS[WXWIDGETS]}.tar.bz2;wxWidgets-${PROJECT_VERSIONS[WXWIDGETS]}.tar.bz2;3rd_party" +PROJECT_DOWNLOADS[ZLIB]="https://github.com/madler/zlib/archive/refs/tags/v${PROJECT_VERSIONS[ZLIB]}.tar.gz;zlib-${PROJECT_VERSIONS[ZLIB]}.tar.gz;3rd_party/mingw64" +export PROJECT_DOWNLOADS diff --git a/support/3rd_party/boost_1_85_0.tar.gz.sha256 b/support/3rd_party/boost_1_85_0.tar.gz.sha256 index 2f7a5e20..45585133 100644 --- a/support/3rd_party/boost_1_85_0.tar.gz.sha256 +++ b/support/3rd_party/boost_1_85_0.tar.gz.sha256 @@ -1 +1 @@ -be0d91732d5b0cc6fbb275c7939974457e79b54d6f07ce2e3dfdd68bef883b0b ./boost_1_85_0.tar.gz +be0d91732d5b0cc6fbb275c7939974457e79b54d6f07ce2e3dfdd68bef883b0b boost_1_85_0.tar.gz diff --git a/support/3rd_party/cpp-httplib-0.16.0.tar.gz b/support/3rd_party/cpp-httplib-0.16.0.tar.gz deleted file mode 100644 index e0c7f0e1..00000000 Binary files a/support/3rd_party/cpp-httplib-0.16.0.tar.gz and /dev/null differ diff --git a/support/3rd_party/cpp-httplib-0.16.0.tar.gz.sha256 b/support/3rd_party/cpp-httplib-0.16.0.tar.gz.sha256 deleted file mode 100644 index 43e0d7d8..00000000 --- a/support/3rd_party/cpp-httplib-0.16.0.tar.gz.sha256 +++ /dev/null @@ -1 +0,0 @@ -c125022eb85eaa12235518dc4638be93b62c3216d0f87b655af7b17b71b38851 *cpp-httplib-0.16.0.tar.gz diff --git a/support/3rd_party/cpp-httplib-0.16.3.tar.gz b/support/3rd_party/cpp-httplib-0.16.3.tar.gz new file mode 100644 index 00000000..7d4e9c0f Binary files /dev/null and b/support/3rd_party/cpp-httplib-0.16.3.tar.gz differ diff --git a/support/3rd_party/cpp-httplib-0.16.3.tar.gz.sha256 b/support/3rd_party/cpp-httplib-0.16.3.tar.gz.sha256 new file mode 100644 index 00000000..2120f205 --- /dev/null +++ b/support/3rd_party/cpp-httplib-0.16.3.tar.gz.sha256 @@ -0,0 +1 @@ +c1742fc7179aaae2a67ad9bba0740b7e9ffaf4f5e62feef53101ecdef1478716 cpp-httplib-0.16.3.tar.gz diff --git a/support/3rd_party/curl-8.8.0.tar.gz b/support/3rd_party/curl-8.8.0.tar.gz deleted file mode 100644 index 122fbe27..00000000 Binary files a/support/3rd_party/curl-8.8.0.tar.gz and /dev/null differ diff --git a/support/3rd_party/curl-8.8.0.tar.gz.sha256 b/support/3rd_party/curl-8.8.0.tar.gz.sha256 deleted file mode 100644 index e9b395c9..00000000 --- a/support/3rd_party/curl-8.8.0.tar.gz.sha256 +++ /dev/null @@ -1 +0,0 @@ -77c0e1cd35ab5b45b659645a93b46d660224d0024f1185e8a95cdb27ae3d787d *curl-8.8.0.tar.gz diff --git a/support/3rd_party/curl-8.9.1.tar.gz b/support/3rd_party/curl-8.9.1.tar.gz new file mode 100644 index 00000000..d357899e Binary files /dev/null and b/support/3rd_party/curl-8.9.1.tar.gz differ diff --git a/support/3rd_party/curl-8.9.1.tar.gz.sha256 b/support/3rd_party/curl-8.9.1.tar.gz.sha256 new file mode 100644 index 00000000..81a3824b --- /dev/null +++ b/support/3rd_party/curl-8.9.1.tar.gz.sha256 @@ -0,0 +1 @@ +d714818f6ac41ae9154850158fed44b7a87650a6d52f83d3bcb9aa527be354d7 curl-8.9.1.tar.gz diff --git a/support/3rd_party/googletest-1.15.0.tar.gz b/support/3rd_party/googletest-1.15.0.tar.gz deleted file mode 100644 index 738710c3..00000000 Binary files a/support/3rd_party/googletest-1.15.0.tar.gz and /dev/null differ diff --git a/support/3rd_party/googletest-1.15.0.tar.gz.sha256 b/support/3rd_party/googletest-1.15.0.tar.gz.sha256 deleted file mode 100644 index c5f63033..00000000 --- a/support/3rd_party/googletest-1.15.0.tar.gz.sha256 +++ /dev/null @@ -1 +0,0 @@ -7315acb6bf10e99f332c8a43f00d5fbb1ee6ca48c52f6b936991b216c586aaad *googletest-1.15.0.tar.gz diff --git a/support/3rd_party/googletest-1.15.2.tar.gz b/support/3rd_party/googletest-1.15.2.tar.gz new file mode 100644 index 00000000..fd665549 Binary files /dev/null and b/support/3rd_party/googletest-1.15.2.tar.gz differ diff --git a/support/3rd_party/googletest-1.15.2.tar.gz.sha256 b/support/3rd_party/googletest-1.15.2.tar.gz.sha256 new file mode 100644 index 00000000..1b4192ec --- /dev/null +++ b/support/3rd_party/googletest-1.15.2.tar.gz.sha256 @@ -0,0 +1 @@ +7b42b4d6ed48810c5362c265a17faebe90dc2373c885e5216439d37927f02926 googletest-1.15.2.tar.gz diff --git a/support/3rd_party/json-3.11.3.tar.gz.sha256 b/support/3rd_party/json-3.11.3.tar.gz.sha256 index e7852dbf..bff77da4 100644 --- a/support/3rd_party/json-3.11.3.tar.gz.sha256 +++ b/support/3rd_party/json-3.11.3.tar.gz.sha256 @@ -1 +1 @@ -0d8ef5af7f9794e3263480193c491549b2ba6cc74bb018906202ada498a79406 *json-3.11.3.tar.gz +0d8ef5af7f9794e3263480193c491549b2ba6cc74bb018906202ada498a79406 json-3.11.3.tar.gz diff --git a/support/3rd_party/libsodium-1.0.20.tar.gz b/support/3rd_party/libsodium-1.0.20.tar.gz index 2f6efbdb..ce186739 100644 Binary files a/support/3rd_party/libsodium-1.0.20.tar.gz and b/support/3rd_party/libsodium-1.0.20.tar.gz differ diff --git a/support/3rd_party/libsodium-1.0.20.tar.gz.sha256 b/support/3rd_party/libsodium-1.0.20.tar.gz.sha256 index 880c6610..5fddcda5 100644 --- a/support/3rd_party/libsodium-1.0.20.tar.gz.sha256 +++ b/support/3rd_party/libsodium-1.0.20.tar.gz.sha256 @@ -1 +1 @@ -ebb65ef6ca439333c2bb41a0c1990587288da07f6c7fd07cb3a18cc18d30ce19 *libsodium-1.0.20.tar.gz +8e5aeca07a723a27bbecc3beef14b0068d37e7fc0e97f51b3f1c82d2a58005c1 libsodium-1.0.20.tar.gz diff --git a/support/3rd_party/mingw64/binutils-2.41.tar.xz.sha256 b/support/3rd_party/mingw64/binutils-2.41.tar.xz.sha256 index 16601147..09a6ce51 100644 --- a/support/3rd_party/mingw64/binutils-2.41.tar.xz.sha256 +++ b/support/3rd_party/mingw64/binutils-2.41.tar.xz.sha256 @@ -1 +1 @@ -ae9a5789e23459e59606e6714723f2d3ffc31c03174191ef0d015bdf06007450 *binutils-2.41.tar.xz +ae9a5789e23459e59606e6714723f2d3ffc31c03174191ef0d015bdf06007450 binutils-2.41.tar.xz diff --git a/support/3rd_party/mingw64/expat-2.6.2.tar.gz b/support/3rd_party/mingw64/expat-2.6.2.tar.gz index d9310666..4edd900c 100644 Binary files a/support/3rd_party/mingw64/expat-2.6.2.tar.gz and b/support/3rd_party/mingw64/expat-2.6.2.tar.gz differ diff --git a/support/3rd_party/mingw64/expat-2.6.2.tar.gz.sha256 b/support/3rd_party/mingw64/expat-2.6.2.tar.gz.sha256 index ad0aa246..14662b3e 100644 --- a/support/3rd_party/mingw64/expat-2.6.2.tar.gz.sha256 +++ b/support/3rd_party/mingw64/expat-2.6.2.tar.gz.sha256 @@ -1 +1 @@ -d4cf38d26e21a56654ffe4acd9cd5481164619626802328506a2869afab29ab3 expat-2.6.2.tar.gz +fbd032683370d761ba68dba2566d3280a154f5290634172d60a79b24d366d9dc expat-2.6.2.tar.gz diff --git a/support/3rd_party/mingw64/gcc-13.2.0.tar.gz.sha256 b/support/3rd_party/mingw64/gcc-13.2.0.tar.gz.sha256 deleted file mode 100644 index 6f2819cd..00000000 --- a/support/3rd_party/mingw64/gcc-13.2.0.tar.gz.sha256 +++ /dev/null @@ -1 +0,0 @@ -8cb4be3796651976f94b9356fa08d833524f62420d6292c5033a9a26af315078 gcc-13.2.0.tar.gz diff --git a/support/3rd_party/mingw64/gcc-13.2.0.tar.gz b/support/3rd_party/mingw64/gcc-14.2.0.tar.gz similarity index 86% rename from support/3rd_party/mingw64/gcc-13.2.0.tar.gz rename to support/3rd_party/mingw64/gcc-14.2.0.tar.gz index c88c4e3f..e5c2a901 100644 Binary files a/support/3rd_party/mingw64/gcc-13.2.0.tar.gz and b/support/3rd_party/mingw64/gcc-14.2.0.tar.gz differ diff --git a/support/3rd_party/mingw64/gcc-14.2.0.tar.gz.sha256 b/support/3rd_party/mingw64/gcc-14.2.0.tar.gz.sha256 new file mode 100644 index 00000000..c5176fb9 --- /dev/null +++ b/support/3rd_party/mingw64/gcc-14.2.0.tar.gz.sha256 @@ -0,0 +1 @@ +7d376d445f93126dc545e2c0086d0f647c3094aae081cdb78f42ce2bc25e7293 gcc-14.2.0.tar.gz diff --git a/support/3rd_party/mingw64/mingw-w64-11.0.1.tar.gz b/support/3rd_party/mingw64/mingw-w64-11.0.1.tar.gz deleted file mode 100644 index 73aad9f1..00000000 Binary files a/support/3rd_party/mingw64/mingw-w64-11.0.1.tar.gz and /dev/null differ diff --git a/support/3rd_party/mingw64/mingw-w64-11.0.1.tar.gz.sha256 b/support/3rd_party/mingw64/mingw-w64-11.0.1.tar.gz.sha256 deleted file mode 100644 index 8030a39a..00000000 --- a/support/3rd_party/mingw64/mingw-w64-11.0.1.tar.gz.sha256 +++ /dev/null @@ -1 +0,0 @@ -9c82a58713786c95b0594443a1c6190cd986401ed2b03677a90acc470140af28 *mingw-w64-11.0.1.tar.gz diff --git a/support/3rd_party/mingw64/mingw-w64-v11.0.1.tar.bz2 b/support/3rd_party/mingw64/mingw-w64-v11.0.1.tar.bz2 new file mode 100644 index 00000000..403fddb4 Binary files /dev/null and b/support/3rd_party/mingw64/mingw-w64-v11.0.1.tar.bz2 differ diff --git a/support/3rd_party/mingw64/mingw-w64-v11.0.1.tar.bz2.sha256 b/support/3rd_party/mingw64/mingw-w64-v11.0.1.tar.bz2.sha256 new file mode 100644 index 00000000..15f920f1 --- /dev/null +++ b/support/3rd_party/mingw64/mingw-w64-v11.0.1.tar.bz2.sha256 @@ -0,0 +1 @@ +3f66bce069ee8bed7439a1a13da7cb91a5e67ea6170f21317ac7f5794625ee10 mingw-w64-v11.0.1.tar.bz2 diff --git a/support/3rd_party/mingw64/pkg-config-0.29.2.tar.gz.sha256 b/support/3rd_party/mingw64/pkg-config-0.29.2.tar.gz.sha256 index b4d64c05..d23b500e 100644 --- a/support/3rd_party/mingw64/pkg-config-0.29.2.tar.gz.sha256 +++ b/support/3rd_party/mingw64/pkg-config-0.29.2.tar.gz.sha256 @@ -1 +1 @@ -6fc69c01688c9458a57eb9a1664c9aba372ccda420a02bf4429fe610e7e7d591 *pkg-config-0.29.2.tar.gz +6fc69c01688c9458a57eb9a1664c9aba372ccda420a02bf4429fe610e7e7d591 pkg-config-0.29.2.tar.gz diff --git a/support/3rd_party/mingw64/zlib-1.3.1.tar.gz b/support/3rd_party/mingw64/zlib-1.3.1.tar.gz index 53fa48bf..9c29e948 100644 Binary files a/support/3rd_party/mingw64/zlib-1.3.1.tar.gz and b/support/3rd_party/mingw64/zlib-1.3.1.tar.gz differ diff --git a/support/3rd_party/mingw64/zlib-1.3.1.tar.gz.sha256 b/support/3rd_party/mingw64/zlib-1.3.1.tar.gz.sha256 index e03ea2c2..9440c847 100644 --- a/support/3rd_party/mingw64/zlib-1.3.1.tar.gz.sha256 +++ b/support/3rd_party/mingw64/zlib-1.3.1.tar.gz.sha256 @@ -1 +1 @@ -9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23 zlib-1.3.1.tar.gz +17e88863f3600672ab49182f217281b6fc4d3c762bde361935e436a95214d05c zlib-1.3.1.tar.gz diff --git a/support/3rd_party/openssl-3.3.1.tar.gz.sha256 b/support/3rd_party/openssl-3.3.1.tar.gz.sha256 index f41b9b64..1fdc8443 100644 --- a/support/3rd_party/openssl-3.3.1.tar.gz.sha256 +++ b/support/3rd_party/openssl-3.3.1.tar.gz.sha256 @@ -1 +1 @@ -777cd596284c883375a2a7a11bf5d2786fc5413255efab20c50d6ffe6d020b7e *openssl-3.3.1.tar.gz +777cd596284c883375a2a7a11bf5d2786fc5413255efab20c50d6ffe6d020b7e openssl-3.3.1.tar.gz diff --git a/support/3rd_party/pugixml-1.14.tar.gz.sha256 b/support/3rd_party/pugixml-1.14.tar.gz.sha256 index e57e747c..bad0993f 100644 --- a/support/3rd_party/pugixml-1.14.tar.gz.sha256 +++ b/support/3rd_party/pugixml-1.14.tar.gz.sha256 @@ -1 +1 @@ -2f10e276870c64b1db6809050a75e11a897a8d7456c4be5c6b2e35a11168a015 ./pugixml-1.14.tar.gz +2f10e276870c64b1db6809050a75e11a897a8d7456c4be5c6b2e35a11168a015 pugixml-1.14.tar.gz diff --git a/support/3rd_party/spdlog-1.14.1.tar.gz.sha256 b/support/3rd_party/spdlog-1.14.1.tar.gz.sha256 index abd59d97..28c68fea 100644 --- a/support/3rd_party/spdlog-1.14.1.tar.gz.sha256 +++ b/support/3rd_party/spdlog-1.14.1.tar.gz.sha256 @@ -1 +1 @@ -1586508029a7d0670dfcb2d97575dcdc242d3868a259742b69f100801ab4e16b *spdlog-1.14.1.tar.gz +1586508029a7d0670dfcb2d97575dcdc242d3868a259742b69f100801ab4e16b spdlog-1.14.1.tar.gz diff --git a/support/3rd_party/sqlite-amalgamation-3460000.zip b/support/3rd_party/sqlite-amalgamation-3460000.zip deleted file mode 100644 index c8fbd06a..00000000 Binary files a/support/3rd_party/sqlite-amalgamation-3460000.zip and /dev/null differ diff --git a/support/3rd_party/sqlite-amalgamation-3460000.zip.sha256 b/support/3rd_party/sqlite-amalgamation-3460000.zip.sha256 deleted file mode 100644 index fa86519a..00000000 --- a/support/3rd_party/sqlite-amalgamation-3460000.zip.sha256 +++ /dev/null @@ -1 +0,0 @@ -712a7d09d2a22652fb06a49af516e051979a3984adb067da86760e60ed51a7f5 *sqlite-amalgamation-3460000.zip diff --git a/support/3rd_party/sqlite-amalgamation-3460100.zip b/support/3rd_party/sqlite-amalgamation-3460100.zip new file mode 100644 index 00000000..750873a0 Binary files /dev/null and b/support/3rd_party/sqlite-amalgamation-3460100.zip differ diff --git a/support/3rd_party/sqlite-amalgamation-3460100.zip.sha256 b/support/3rd_party/sqlite-amalgamation-3460100.zip.sha256 new file mode 100644 index 00000000..ce14d6dd --- /dev/null +++ b/support/3rd_party/sqlite-amalgamation-3460100.zip.sha256 @@ -0,0 +1 @@ +77823cb110929c2bcb0f5d48e4833b5c59a8a6e40cdea3936b99e199dbbe5784 sqlite-amalgamation-3460100.zip diff --git a/support/3rd_party/stduuid-1.2.3.tar.gz.sha256 b/support/3rd_party/stduuid-1.2.3.tar.gz.sha256 index ec4b19c8..3d4a3e09 100644 --- a/support/3rd_party/stduuid-1.2.3.tar.gz.sha256 +++ b/support/3rd_party/stduuid-1.2.3.tar.gz.sha256 @@ -1 +1 @@ -b1176597e789531c38481acbbed2a6894ad419aab0979c10410d59eb0ebf40d3 *stduuid-1.2.3.tar.gz +b1176597e789531c38481acbbed2a6894ad419aab0979c10410d59eb0ebf40d3 stduuid-1.2.3.tar.gz diff --git a/support/include/backward.hpp b/support/include/backward.hpp deleted file mode 100644 index bbb94046..00000000 --- a/support/include/backward.hpp +++ /dev/null @@ -1,4509 +0,0 @@ -#if defined(PROJECT_ENABLE_BACKWARD_CPP) -/* - * backward.hpp - * Copyright 2013 Google Inc. All Rights Reserved. - * - * 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. - */ - -#ifndef H_6B9572DA_A64B_49E6_B234_051480991C89 -#define H_6B9572DA_A64B_49E6_B234_051480991C89 - -#ifndef __cplusplus -#error "It's not going to compile without a C++ compiler..." -#endif - -#if defined(BACKWARD_CXX11) -#elif defined(BACKWARD_CXX98) -#else -#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800) -#define BACKWARD_CXX11 -#define BACKWARD_ATLEAST_CXX11 -#define BACKWARD_ATLEAST_CXX98 -#if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) -#define BACKWARD_ATLEAST_CXX17 -#endif -#else -#define BACKWARD_CXX98 -#define BACKWARD_ATLEAST_CXX98 -#endif -#endif - -// You can define one of the following (or leave it to the auto-detection): -// -// #define BACKWARD_SYSTEM_LINUX -// - specialization for linux -// -// #define BACKWARD_SYSTEM_DARWIN -// - specialization for Mac OS X 10.5 and later. -// -// #define BACKWARD_SYSTEM_WINDOWS -// - specialization for Windows (Clang 9 and MSVC2017) -// -// #define BACKWARD_SYSTEM_UNKNOWN -// - placebo implementation, does nothing. -// -#if defined(BACKWARD_SYSTEM_LINUX) -#elif defined(BACKWARD_SYSTEM_DARWIN) -#elif defined(BACKWARD_SYSTEM_UNKNOWN) -#elif defined(BACKWARD_SYSTEM_WINDOWS) -#else -#if defined(__linux) || defined(__linux__) -#define BACKWARD_SYSTEM_LINUX -#elif defined(__APPLE__) -#define BACKWARD_SYSTEM_DARWIN -#elif defined(_WIN32) -#define BACKWARD_SYSTEM_WINDOWS -#else -#define BACKWARD_SYSTEM_UNKNOWN -#endif -#endif - -#define NOINLINE __attribute__((noinline)) - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#if defined(BACKWARD_SYSTEM_LINUX) - -// On linux, backtrace can back-trace or "walk" the stack using the following -// libraries: -// -// #define BACKWARD_HAS_UNWIND 1 -// - unwind comes from libgcc, but I saw an equivalent inside clang itself. -// - with unwind, the stacktrace is as accurate as it can possibly be, since -// this is used by the C++ runtime in gcc/clang for stack unwinding on -// exception. -// - normally libgcc is already linked to your program by default. -// -// #define BACKWARD_HAS_LIBUNWIND 1 -// - libunwind provides, in some cases, a more accurate stacktrace as it knows -// to decode signal handler frames and lets us edit the context registers when -// unwinding, allowing stack traces over bad function references. -// -// #define BACKWARD_HAS_BACKTRACE == 1 -// - backtrace seems to be a little bit more portable than libunwind, but on -// linux, it uses unwind anyway, but abstract away a tiny information that is -// sadly really important in order to get perfectly accurate stack traces. -// - backtrace is part of the (e)glib library. -// -// The default is: -// #define BACKWARD_HAS_UNWIND == 1 -// -// Note that only one of the define should be set to 1 at a time. -// -#if BACKWARD_HAS_UNWIND == 1 -#elif BACKWARD_HAS_LIBUNWIND == 1 -#elif BACKWARD_HAS_BACKTRACE == 1 -#else -#undef BACKWARD_HAS_UNWIND -#define BACKWARD_HAS_UNWIND 1 -#undef BACKWARD_HAS_LIBUNWIND -#define BACKWARD_HAS_LIBUNWIND 0 -#undef BACKWARD_HAS_BACKTRACE -#define BACKWARD_HAS_BACKTRACE 0 -#endif - -// On linux, backward can extract detailed information about a stack trace -// using one of the following libraries: -// -// #define BACKWARD_HAS_DW 1 -// - libdw gives you the most juicy details out of your stack traces: -// - object filename -// - function name -// - source filename -// - line and column numbers -// - source code snippet (assuming the file is accessible) -// - variable names (if not optimized out) -// - variable values (not supported by backward-cpp) -// - You need to link with the lib "dw": -// - apt-get install libdw-dev -// - g++/clang++ -ldw ... -// -// #define BACKWARD_HAS_BFD 1 -// - With libbfd, you get a fair amount of details: -// - object filename -// - function name -// - source filename -// - line numbers -// - source code snippet (assuming the file is accessible) -// - You need to link with the lib "bfd": -// - apt-get install binutils-dev -// - g++/clang++ -lbfd ... -// -// #define BACKWARD_HAS_DWARF 1 -// - libdwarf gives you the most juicy details out of your stack traces: -// - object filename -// - function name -// - source filename -// - line and column numbers -// - source code snippet (assuming the file is accessible) -// - variable names (if not optimized out) -// - variable values (not supported by backward-cpp) -// - You need to link with the lib "dwarf": -// - apt-get install libdwarf-dev -// - g++/clang++ -ldwarf ... -// -// #define BACKWARD_HAS_BACKTRACE_SYMBOL 1 -// - backtrace provides minimal details for a stack trace: -// - object filename -// - function name -// - backtrace is part of the (e)glib library. -// -// The default is: -// #define BACKWARD_HAS_BACKTRACE_SYMBOL == 1 -// -// Note that only one of the define should be set to 1 at a time. -// -#if BACKWARD_HAS_DW == 1 -#elif BACKWARD_HAS_BFD == 1 -#elif BACKWARD_HAS_DWARF == 1 -#elif BACKWARD_HAS_BACKTRACE_SYMBOL == 1 -#else -#undef BACKWARD_HAS_DW -#define BACKWARD_HAS_DW 0 -#undef BACKWARD_HAS_BFD -#define BACKWARD_HAS_BFD 0 -#undef BACKWARD_HAS_DWARF -#define BACKWARD_HAS_DWARF 0 -#undef BACKWARD_HAS_BACKTRACE_SYMBOL -#define BACKWARD_HAS_BACKTRACE_SYMBOL 1 -#endif - -#include -#include -#ifdef __ANDROID__ -// Old Android API levels define _Unwind_Ptr in both link.h and -// unwind.h Rename the one in link.h as we are not going to be using -// it -#define _Unwind_Ptr _Unwind_Ptr_Custom -#include -#undef _Unwind_Ptr -#else -#include -#endif -#if defined(__ppc__) || defined(__powerpc) || defined(__powerpc__) || \ - defined(__POWERPC__) -// Linux kernel header required for the struct pt_regs definition -// to access the NIP (Next Instruction Pointer) register value -#include -#endif -#include -#include -#include -#include -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#include -#undef _GNU_SOURCE -#else -#include -#endif - -#if BACKWARD_HAS_BFD == 1 -// NOTE: defining PACKAGE{,_VERSION} is required before including -// bfd.h on some platforms, see also: -// https://sourceware.org/bugzilla/show_bug.cgi?id=14243 -#ifndef PACKAGE -#define PACKAGE -#endif -#ifndef PACKAGE_VERSION -#define PACKAGE_VERSION -#endif -#include -#endif - -#if BACKWARD_HAS_DW == 1 -#include -#include -#include -#endif - -#if BACKWARD_HAS_DWARF == 1 -#include -#include -#include -#include -#include -#endif - -#if (BACKWARD_HAS_BACKTRACE == 1) || (BACKWARD_HAS_BACKTRACE_SYMBOL == 1) -// then we shall rely on backtrace -#include -#endif - -#endif // defined(BACKWARD_SYSTEM_LINUX) - -#if defined(BACKWARD_SYSTEM_DARWIN) -// On Darwin, backtrace can back-trace or "walk" the stack using the following -// libraries: -// -// #define BACKWARD_HAS_UNWIND 1 -// - unwind comes from libgcc, but I saw an equivalent inside clang itself. -// - with unwind, the stacktrace is as accurate as it can possibly be, since -// this is used by the C++ runtime in gcc/clang for stack unwinding on -// exception. -// - normally libgcc is already linked to your program by default. -// -// #define BACKWARD_HAS_LIBUNWIND 1 -// - libunwind comes from clang, which implements an API compatible version. -// - libunwind provides, in some cases, a more accurate stacktrace as it knows -// to decode signal handler frames and lets us edit the context registers when -// unwinding, allowing stack traces over bad function references. -// -// #define BACKWARD_HAS_BACKTRACE == 1 -// - backtrace is available by default, though it does not produce as much -// information as another library might. -// -// The default is: -// #define BACKWARD_HAS_UNWIND == 1 -// -// Note that only one of the define should be set to 1 at a time. -// -#if BACKWARD_HAS_UNWIND == 1 -#elif BACKWARD_HAS_BACKTRACE == 1 -#elif BACKWARD_HAS_LIBUNWIND == 1 -#else -#undef BACKWARD_HAS_UNWIND -#define BACKWARD_HAS_UNWIND 1 -#undef BACKWARD_HAS_BACKTRACE -#define BACKWARD_HAS_BACKTRACE 0 -#undef BACKWARD_HAS_LIBUNWIND -#define BACKWARD_HAS_LIBUNWIND 0 -#endif - -// On Darwin, backward can extract detailed information about a stack trace -// using one of the following libraries: -// -// #define BACKWARD_HAS_BACKTRACE_SYMBOL 1 -// - backtrace provides minimal details for a stack trace: -// - object filename -// - function name -// -// The default is: -// #define BACKWARD_HAS_BACKTRACE_SYMBOL == 1 -// -#if BACKWARD_HAS_BACKTRACE_SYMBOL == 1 -#else -#undef BACKWARD_HAS_BACKTRACE_SYMBOL -#define BACKWARD_HAS_BACKTRACE_SYMBOL 1 -#endif - -#include -#include -#include -#include -#include -#include - -#if (BACKWARD_HAS_BACKTRACE == 1) || (BACKWARD_HAS_BACKTRACE_SYMBOL == 1) -#include -#endif -#endif // defined(BACKWARD_SYSTEM_DARWIN) - -#if defined(BACKWARD_SYSTEM_WINDOWS) - -#include -#include -#include - -#include - -#ifdef _WIN64 -typedef SSIZE_T ssize_t; -#else -typedef int ssize_t; -#endif - -#ifndef NOMINMAX -#define NOMINMAX -#endif -#include -#include - -#include -#include - -#ifndef __clang__ -#undef NOINLINE -#define NOINLINE __declspec(noinline) -#endif - -#ifdef _MSC_VER -#pragma comment(lib, "psapi.lib") -#pragma comment(lib, "dbghelp.lib") -#endif - -// Comment / packing is from stackoverflow: -// https://stackoverflow.com/questions/6205981/windows-c-stack-trace-from-a-running-app/28276227#28276227 -// Some versions of imagehlp.dll lack the proper packing directives themselves -// so we need to do it. -#pragma pack(push, before_imagehlp, 8) -#include -#pragma pack(pop, before_imagehlp) - -// TODO maybe these should be undefined somewhere else? -#undef BACKWARD_HAS_UNWIND -#undef BACKWARD_HAS_BACKTRACE -#if BACKWARD_HAS_PDB_SYMBOL == 1 -#else -#undef BACKWARD_HAS_PDB_SYMBOL -#define BACKWARD_HAS_PDB_SYMBOL 1 -#endif - -#endif - -#if BACKWARD_HAS_UNWIND == 1 - -#include -// while gcc's unwind.h defines something like that: -// extern _Unwind_Ptr _Unwind_GetIP (struct _Unwind_Context *); -// extern _Unwind_Ptr _Unwind_GetIPInfo (struct _Unwind_Context *, int *); -// -// clang's unwind.h defines something like this: -// uintptr_t _Unwind_GetIP(struct _Unwind_Context* __context); -// -// Even if the _Unwind_GetIPInfo can be linked to, it is not declared, worse we -// cannot just redeclare it because clang's unwind.h doesn't define _Unwind_Ptr -// anyway. -// -// Luckily we can play on the fact that the guard macros have a different name: -#ifdef __CLANG_UNWIND_H -// In fact, this function still comes from libgcc (on my different linux boxes, -// clang links against libgcc). -#include -extern "C" uintptr_t _Unwind_GetIPInfo(_Unwind_Context *, int *); -#endif - -#endif // BACKWARD_HAS_UNWIND == 1 - -#if BACKWARD_HAS_LIBUNWIND == 1 -#define UNW_LOCAL_ONLY -#include -#endif // BACKWARD_HAS_LIBUNWIND == 1 - -#ifdef BACKWARD_ATLEAST_CXX11 -#include -#include // for std::swap -namespace backward { -namespace details { -template struct hashtable { - typedef std::unordered_map type; -}; -using std::move; -} // namespace details -} // namespace backward -#else // NOT BACKWARD_ATLEAST_CXX11 -#define nullptr NULL -#define override -#include -namespace backward { -namespace details { -template struct hashtable { - typedef std::map type; -}; -template const T &move(const T &v) { return v; } -template T &move(T &v) { return v; } -} // namespace details -} // namespace backward -#endif // BACKWARD_ATLEAST_CXX11 - -namespace backward { -namespace details { -#if defined(BACKWARD_SYSTEM_WINDOWS) -const char kBackwardPathDelimiter[] = ";"; -#else -const char kBackwardPathDelimiter[] = ":"; -#endif -} // namespace details -} // namespace backward - -namespace backward { - -namespace system_tag { -struct linux_tag; // seems that I cannot call that "linux" because the name -// is already defined... so I am adding _tag everywhere. -struct darwin_tag; -struct windows_tag; -struct unknown_tag; - -#if defined(BACKWARD_SYSTEM_LINUX) -typedef linux_tag current_tag; -#elif defined(BACKWARD_SYSTEM_DARWIN) -typedef darwin_tag current_tag; -#elif defined(BACKWARD_SYSTEM_WINDOWS) -typedef windows_tag current_tag; -#elif defined(BACKWARD_SYSTEM_UNKNOWN) -typedef unknown_tag current_tag; -#else -#error "May I please get my system defines?" -#endif -} // namespace system_tag - -namespace trace_resolver_tag { -#if defined(BACKWARD_SYSTEM_LINUX) -struct libdw; -struct libbfd; -struct libdwarf; -struct backtrace_symbol; - -#if BACKWARD_HAS_DW == 1 -typedef libdw current; -#elif BACKWARD_HAS_BFD == 1 -typedef libbfd current; -#elif BACKWARD_HAS_DWARF == 1 -typedef libdwarf current; -#elif BACKWARD_HAS_BACKTRACE_SYMBOL == 1 -typedef backtrace_symbol current; -#else -#error "You shall not pass, until you know what you want." -#endif -#elif defined(BACKWARD_SYSTEM_DARWIN) -struct backtrace_symbol; - -#if BACKWARD_HAS_BACKTRACE_SYMBOL == 1 -typedef backtrace_symbol current; -#else -#error "You shall not pass, until you know what you want." -#endif -#elif defined(BACKWARD_SYSTEM_WINDOWS) -struct pdb_symbol; -#if BACKWARD_HAS_PDB_SYMBOL == 1 -typedef pdb_symbol current; -#else -#error "You shall not pass, until you know what you want." -#endif -#endif -} // namespace trace_resolver_tag - -namespace details { - -template struct rm_ptr { - typedef T type; -}; - -template struct rm_ptr { - typedef T type; -}; - -template struct rm_ptr { - typedef const T type; -}; - -template struct deleter { - template void operator()(U &ptr) const { (*F)(ptr); } -}; - -template struct default_delete { - void operator()(T &ptr) const { delete ptr; } -}; - -template > -class handle { - struct dummy; - T _val; - bool _empty; - -#ifdef BACKWARD_ATLEAST_CXX11 - handle(const handle &) = delete; - handle &operator=(const handle &) = delete; -#endif - -public: - ~handle() { - if (!_empty) { - Deleter()(_val); - } - } - - explicit handle() : _val(), _empty(true) {} - explicit handle(T val) : _val(val), _empty(false) { - if (!_val) - _empty = true; - } - -#ifdef BACKWARD_ATLEAST_CXX11 - handle(handle &&from) : _empty(true) { swap(from); } - handle &operator=(handle &&from) { - swap(from); - return *this; - } -#else - explicit handle(const handle &from) : _empty(true) { - // some sort of poor man's move semantic. - swap(const_cast(from)); - } - handle &operator=(const handle &from) { - // some sort of poor man's move semantic. - swap(const_cast(from)); - return *this; - } -#endif - - void reset(T new_val) { - handle tmp(new_val); - swap(tmp); - } - - void update(T new_val) { - _val = new_val; - _empty = !static_cast(new_val); - } - - operator const dummy *() const { - if (_empty) { - return nullptr; - } - return reinterpret_cast(_val); - } - T get() { return _val; } - T release() { - _empty = true; - return _val; - } - void swap(handle &b) { - using std::swap; - swap(b._val, _val); // can throw, we are safe here. - swap(b._empty, _empty); // should not throw: if you cannot swap two - // bools without throwing... It's a lost cause anyway! - } - - T &operator->() { return _val; } - const T &operator->() const { return _val; } - - typedef typename rm_ptr::type &ref_t; - typedef const typename rm_ptr::type &const_ref_t; - ref_t operator*() { return *_val; } - const_ref_t operator*() const { return *_val; } - ref_t operator[](size_t idx) { return _val[idx]; } - - // Watch out, we've got a badass over here - T *operator&() { - _empty = false; - return &_val; - } -}; - -// Default demangler implementation (do nothing). -template struct demangler_impl { - static std::string demangle(const char *funcname) { return funcname; } -}; - -#if defined(BACKWARD_SYSTEM_LINUX) || defined(BACKWARD_SYSTEM_DARWIN) - -template <> struct demangler_impl { - demangler_impl() : _demangle_buffer_length(0) {} - - std::string demangle(const char *funcname) { - using namespace details; - char *result = abi::__cxa_demangle(funcname, _demangle_buffer.get(), - &_demangle_buffer_length, nullptr); - if (result) { - _demangle_buffer.update(result); - return result; - } - return funcname; - } - -private: - details::handle _demangle_buffer; - size_t _demangle_buffer_length; -}; - -#endif // BACKWARD_SYSTEM_LINUX || BACKWARD_SYSTEM_DARWIN - -struct demangler : public demangler_impl {}; - -// Split a string on the platform's PATH delimiter. Example: if delimiter -// is ":" then: -// "" --> [] -// ":" --> ["",""] -// "::" --> ["","",""] -// "/a/b/c" --> ["/a/b/c"] -// "/a/b/c:/d/e/f" --> ["/a/b/c","/d/e/f"] -// etc. -inline std::vector split_source_prefixes(const std::string &s) { - std::vector out; - size_t last = 0; - size_t next = 0; - size_t delimiter_size = sizeof(kBackwardPathDelimiter) - 1; - while ((next = s.find(kBackwardPathDelimiter, last)) != std::string::npos) { - out.push_back(s.substr(last, next - last)); - last = next + delimiter_size; - } - if (last <= s.length()) { - out.push_back(s.substr(last)); - } - return out; -} - -} // namespace details - -/*************** A TRACE ***************/ - -struct Trace { - void *addr; - size_t idx; - - Trace() : addr(nullptr), idx(0) {} - - explicit Trace(void *_addr, size_t _idx) : addr(_addr), idx(_idx) {} -}; - -struct ResolvedTrace : public Trace { - - struct SourceLoc { - std::string function; - std::string filename; - unsigned line; - unsigned col; - - SourceLoc() : line(0), col(0) {} - - bool operator==(const SourceLoc &b) const { - return function == b.function && filename == b.filename && - line == b.line && col == b.col; - } - - bool operator!=(const SourceLoc &b) const { return !(*this == b); } - }; - - // In which binary object this trace is located. - std::string object_filename; - - // The function in the object that contain the trace. This is not the same - // as source.function which can be an function inlined in object_function. - std::string object_function; - - // The source location of this trace. It is possible for filename to be - // empty and for line/col to be invalid (value 0) if this information - // couldn't be deduced, for example if there is no debug information in the - // binary object. - SourceLoc source; - - // An optionals list of "inliners". All the successive sources location - // from where the source location of the trace (the attribute right above) - // is inlined. It is especially useful when you compiled with optimization. - typedef std::vector source_locs_t; - source_locs_t inliners; - - ResolvedTrace() : Trace() {} - ResolvedTrace(const Trace &mini_trace) : Trace(mini_trace) {} -}; - -/*************** STACK TRACE ***************/ - -// default implemention. -template class StackTraceImpl { -public: - size_t size() const { return 0; } - Trace operator[](size_t) const { return Trace(); } - size_t load_here(size_t = 0) { return 0; } - size_t load_from(void *, size_t = 0, void * = nullptr, void * = nullptr) { - return 0; - } - size_t thread_id() const { return 0; } - void skip_n_firsts(size_t) {} - void *const *begin() const { return nullptr; } -}; - -class StackTraceImplBase { -public: - StackTraceImplBase() - : _thread_id(0), _skip(0), _context(nullptr), _error_addr(nullptr) {} - - size_t thread_id() const { return _thread_id; } - - void skip_n_firsts(size_t n) { _skip = n; } - -protected: - void load_thread_info() { -#ifdef BACKWARD_SYSTEM_LINUX -#ifndef __ANDROID__ - _thread_id = static_cast(syscall(SYS_gettid)); -#else - _thread_id = static_cast(gettid()); -#endif - if (_thread_id == static_cast(getpid())) { - // If the thread is the main one, let's hide that. - // I like to keep little secret sometimes. - _thread_id = 0; - } -#elif defined(BACKWARD_SYSTEM_DARWIN) - _thread_id = reinterpret_cast(pthread_self()); - if (pthread_main_np() == 1) { - // If the thread is the main one, let's hide that. - _thread_id = 0; - } -#endif - } - - void set_context(void *context) { _context = context; } - void *context() const { return _context; } - - void set_error_addr(void *error_addr) { _error_addr = error_addr; } - void *error_addr() const { return _error_addr; } - - size_t skip_n_firsts() const { return _skip; } - -private: - size_t _thread_id; - size_t _skip; - void *_context; - void *_error_addr; -}; - -class StackTraceImplHolder : public StackTraceImplBase { -public: - size_t size() const { - return (_stacktrace.size() >= skip_n_firsts()) - ? _stacktrace.size() - skip_n_firsts() - : 0; - } - Trace operator[](size_t idx) const { - if (idx >= size()) { - return Trace(); - } - return Trace(_stacktrace[idx + skip_n_firsts()], idx); - } - void *const *begin() const { - if (size()) { - return &_stacktrace[skip_n_firsts()]; - } - return nullptr; - } - -protected: - std::vector _stacktrace; -}; - -#if BACKWARD_HAS_UNWIND == 1 - -namespace details { - -template class Unwinder { -public: - size_t operator()(F &f, size_t depth) { - _f = &f; - _index = -1; - _depth = depth; - _Unwind_Backtrace(&this->backtrace_trampoline, this); - if (_index == -1) { - // _Unwind_Backtrace has failed to obtain any backtraces - return 0; - } else { - return static_cast(_index); - } - } - -private: - F *_f; - ssize_t _index; - size_t _depth; - - static _Unwind_Reason_Code backtrace_trampoline(_Unwind_Context *ctx, - void *self) { - return (static_cast(self))->backtrace(ctx); - } - - _Unwind_Reason_Code backtrace(_Unwind_Context *ctx) { - if (_index >= 0 && static_cast(_index) >= _depth) - return _URC_END_OF_STACK; - - int ip_before_instruction = 0; - uintptr_t ip = _Unwind_GetIPInfo(ctx, &ip_before_instruction); - - if (!ip_before_instruction) { - // calculating 0-1 for unsigned, looks like a possible bug to sanitizers, - // so let's do it explicitly: - if (ip == 0) { - ip = std::numeric_limits::max(); // set it to 0xffff... (as - // from casting 0-1) - } else { - ip -= 1; // else just normally decrement it (no overflow/underflow will - // happen) - } - } - - if (_index >= 0) { // ignore first frame. - (*_f)(static_cast(_index), reinterpret_cast(ip)); - } - _index += 1; - return _URC_NO_REASON; - } -}; - -template size_t unwind(F f, size_t depth) { - Unwinder unwinder; - return unwinder(f, depth); -} - -} // namespace details - -template <> -class StackTraceImpl : public StackTraceImplHolder { -public: - NOINLINE - size_t load_here(size_t depth = 32, void *context = nullptr, - void *error_addr = nullptr) { - load_thread_info(); - set_context(context); - set_error_addr(error_addr); - if (depth == 0) { - return 0; - } - _stacktrace.resize(depth); - size_t trace_cnt = details::unwind(callback(*this), depth); - _stacktrace.resize(trace_cnt); - skip_n_firsts(0); - return size(); - } - size_t load_from(void *addr, size_t depth = 32, void *context = nullptr, - void *error_addr = nullptr) { - load_here(depth + 8, context, error_addr); - - for (size_t i = 0; i < _stacktrace.size(); ++i) { - if (_stacktrace[i] == addr) { - skip_n_firsts(i); - break; - } - } - - _stacktrace.resize(std::min(_stacktrace.size(), skip_n_firsts() + depth)); - return size(); - } - -private: - struct callback { - StackTraceImpl &self; - callback(StackTraceImpl &_self) : self(_self) {} - - void operator()(size_t idx, void *addr) { self._stacktrace[idx] = addr; } - }; -}; - -#elif BACKWARD_HAS_LIBUNWIND == 1 - -template <> -class StackTraceImpl : public StackTraceImplHolder { -public: - __attribute__((noinline)) size_t load_here(size_t depth = 32, - void *_context = nullptr, - void *_error_addr = nullptr) { - set_context(_context); - set_error_addr(_error_addr); - load_thread_info(); - if (depth == 0) { - return 0; - } - _stacktrace.resize(depth + 1); - - int result = 0; - - unw_context_t ctx; - size_t index = 0; - - // Add the tail call. If the Instruction Pointer is the crash address it - // means we got a bad function pointer dereference, so we "unwind" the - // bad pointer manually by using the return address pointed to by the - // Stack Pointer as the Instruction Pointer and letting libunwind do - // the rest - - if (context()) { - ucontext_t *uctx = reinterpret_cast(context()); -#ifdef REG_RIP // x86_64 - if (uctx->uc_mcontext.gregs[REG_RIP] == - reinterpret_cast(error_addr())) { - uctx->uc_mcontext.gregs[REG_RIP] = - *reinterpret_cast(uctx->uc_mcontext.gregs[REG_RSP]); - } - _stacktrace[index] = - reinterpret_cast(uctx->uc_mcontext.gregs[REG_RIP]); - ++index; - ctx = *reinterpret_cast(uctx); -#elif defined(REG_EIP) // x86_32 - if (uctx->uc_mcontext.gregs[REG_EIP] == - reinterpret_cast(error_addr())) { - uctx->uc_mcontext.gregs[REG_EIP] = - *reinterpret_cast(uctx->uc_mcontext.gregs[REG_ESP]); - } - _stacktrace[index] = - reinterpret_cast(uctx->uc_mcontext.gregs[REG_EIP]); - ++index; - ctx = *reinterpret_cast(uctx); -#elif defined(__arm__) - // libunwind uses its own context type for ARM unwinding. - // Copy the registers from the signal handler's context so we can - // unwind - unw_getcontext(&ctx); - ctx.regs[UNW_ARM_R0] = uctx->uc_mcontext.arm_r0; - ctx.regs[UNW_ARM_R1] = uctx->uc_mcontext.arm_r1; - ctx.regs[UNW_ARM_R2] = uctx->uc_mcontext.arm_r2; - ctx.regs[UNW_ARM_R3] = uctx->uc_mcontext.arm_r3; - ctx.regs[UNW_ARM_R4] = uctx->uc_mcontext.arm_r4; - ctx.regs[UNW_ARM_R5] = uctx->uc_mcontext.arm_r5; - ctx.regs[UNW_ARM_R6] = uctx->uc_mcontext.arm_r6; - ctx.regs[UNW_ARM_R7] = uctx->uc_mcontext.arm_r7; - ctx.regs[UNW_ARM_R8] = uctx->uc_mcontext.arm_r8; - ctx.regs[UNW_ARM_R9] = uctx->uc_mcontext.arm_r9; - ctx.regs[UNW_ARM_R10] = uctx->uc_mcontext.arm_r10; - ctx.regs[UNW_ARM_R11] = uctx->uc_mcontext.arm_fp; - ctx.regs[UNW_ARM_R12] = uctx->uc_mcontext.arm_ip; - ctx.regs[UNW_ARM_R13] = uctx->uc_mcontext.arm_sp; - ctx.regs[UNW_ARM_R14] = uctx->uc_mcontext.arm_lr; - ctx.regs[UNW_ARM_R15] = uctx->uc_mcontext.arm_pc; - - // If we have crashed in the PC use the LR instead, as this was - // a bad function dereference - if (reinterpret_cast(error_addr()) == - uctx->uc_mcontext.arm_pc) { - ctx.regs[UNW_ARM_R15] = - uctx->uc_mcontext.arm_lr - sizeof(unsigned long); - } - _stacktrace[index] = reinterpret_cast(ctx.regs[UNW_ARM_R15]); - ++index; -#elif defined(__APPLE__) && defined(__x86_64__) - unw_getcontext(&ctx); - // OS X's implementation of libunwind uses its own context object - // so we need to convert the passed context to libunwind's format - // (information about the data layout taken from unw_getcontext.s - // in Apple's libunwind source - ctx.data[0] = uctx->uc_mcontext->__ss.__rax; - ctx.data[1] = uctx->uc_mcontext->__ss.__rbx; - ctx.data[2] = uctx->uc_mcontext->__ss.__rcx; - ctx.data[3] = uctx->uc_mcontext->__ss.__rdx; - ctx.data[4] = uctx->uc_mcontext->__ss.__rdi; - ctx.data[5] = uctx->uc_mcontext->__ss.__rsi; - ctx.data[6] = uctx->uc_mcontext->__ss.__rbp; - ctx.data[7] = uctx->uc_mcontext->__ss.__rsp; - ctx.data[8] = uctx->uc_mcontext->__ss.__r8; - ctx.data[9] = uctx->uc_mcontext->__ss.__r9; - ctx.data[10] = uctx->uc_mcontext->__ss.__r10; - ctx.data[11] = uctx->uc_mcontext->__ss.__r11; - ctx.data[12] = uctx->uc_mcontext->__ss.__r12; - ctx.data[13] = uctx->uc_mcontext->__ss.__r13; - ctx.data[14] = uctx->uc_mcontext->__ss.__r14; - ctx.data[15] = uctx->uc_mcontext->__ss.__r15; - ctx.data[16] = uctx->uc_mcontext->__ss.__rip; - - // If the IP is the same as the crash address we have a bad function - // dereference The caller's address is pointed to by %rsp, so we - // dereference that value and set it to be the next frame's IP. - if (uctx->uc_mcontext->__ss.__rip == - reinterpret_cast<__uint64_t>(error_addr())) { - ctx.data[16] = - *reinterpret_cast<__uint64_t *>(uctx->uc_mcontext->__ss.__rsp); - } - _stacktrace[index] = reinterpret_cast(ctx.data[16]); - ++index; -#elif defined(__APPLE__) - unw_getcontext(&ctx) - // TODO: Convert the ucontext_t to libunwind's unw_context_t like - // we do in 64 bits - if (ctx.uc_mcontext->__ss.__eip == - reinterpret_cast(error_addr())) { - ctx.uc_mcontext->__ss.__eip = ctx.uc_mcontext->__ss.__esp; - } - _stacktrace[index] = - reinterpret_cast(ctx.uc_mcontext->__ss.__eip); - ++index; -#endif - } - - unw_cursor_t cursor; - if (context()) { -#if defined(UNW_INIT_SIGNAL_FRAME) - result = unw_init_local2(&cursor, &ctx, UNW_INIT_SIGNAL_FRAME); -#else - result = unw_init_local(&cursor, &ctx); -#endif - } else { - unw_getcontext(&ctx); - ; - result = unw_init_local(&cursor, &ctx); - } - - if (result != 0) - return 1; - - unw_word_t ip = 0; - - while (index <= depth && unw_step(&cursor) > 0) { - result = unw_get_reg(&cursor, UNW_REG_IP, &ip); - if (result == 0) { - _stacktrace[index] = reinterpret_cast(--ip); - ++index; - } - } - --index; - - _stacktrace.resize(index + 1); - skip_n_firsts(0); - return size(); - } - - size_t load_from(void *addr, size_t depth = 32, void *context = nullptr, - void *error_addr = nullptr) { - load_here(depth + 8, context, error_addr); - - for (size_t i = 0; i < _stacktrace.size(); ++i) { - if (_stacktrace[i] == addr) { - skip_n_firsts(i); - _stacktrace[i] = (void *)((uintptr_t)_stacktrace[i]); - break; - } - } - - _stacktrace.resize(std::min(_stacktrace.size(), skip_n_firsts() + depth)); - return size(); - } -}; - -#elif defined(BACKWARD_HAS_BACKTRACE) - -template <> -class StackTraceImpl : public StackTraceImplHolder { -public: - NOINLINE - size_t load_here(size_t depth = 32, void *context = nullptr, - void *error_addr = nullptr) { - set_context(context); - set_error_addr(error_addr); - load_thread_info(); - if (depth == 0) { - return 0; - } - _stacktrace.resize(depth + 1); - size_t trace_cnt = backtrace(&_stacktrace[0], _stacktrace.size()); - _stacktrace.resize(trace_cnt); - skip_n_firsts(1); - return size(); - } - - size_t load_from(void *addr, size_t depth = 32, void *context = nullptr, - void *error_addr = nullptr) { - load_here(depth + 8, context, error_addr); - - for (size_t i = 0; i < _stacktrace.size(); ++i) { - if (_stacktrace[i] == addr) { - skip_n_firsts(i); - _stacktrace[i] = (void *)((uintptr_t)_stacktrace[i] + 1); - break; - } - } - - _stacktrace.resize(std::min(_stacktrace.size(), skip_n_firsts() + depth)); - return size(); - } -}; - -#elif defined(BACKWARD_SYSTEM_WINDOWS) - -template <> -class StackTraceImpl : public StackTraceImplHolder { -public: - // We have to load the machine type from the image info - // So we first initialize the resolver, and it tells us this info - void set_machine_type(DWORD machine_type) { machine_type_ = machine_type; } - void set_context(CONTEXT *ctx) { ctx_ = ctx; } - void set_thread_handle(HANDLE handle) { thd_ = handle; } - - NOINLINE - size_t load_here(size_t depth = 32, void *context = nullptr, - void *error_addr = nullptr) { - set_context(static_cast(context)); - set_error_addr(error_addr); - CONTEXT localCtx; // used when no context is provided - - if (depth == 0) { - return 0; - } - - if (!ctx_) { - ctx_ = &localCtx; - RtlCaptureContext(ctx_); - } - - if (!thd_) { - thd_ = GetCurrentThread(); - } - - HANDLE process = GetCurrentProcess(); - - STACKFRAME64 s; - memset(&s, 0, sizeof(STACKFRAME64)); - - // TODO: 32 bit context capture - s.AddrStack.Mode = AddrModeFlat; - s.AddrFrame.Mode = AddrModeFlat; - s.AddrPC.Mode = AddrModeFlat; -#ifdef _M_X64 - s.AddrPC.Offset = ctx_->Rip; - s.AddrStack.Offset = ctx_->Rsp; - s.AddrFrame.Offset = ctx_->Rbp; -#else - s.AddrPC.Offset = ctx_->Eip; - s.AddrStack.Offset = ctx_->Esp; - s.AddrFrame.Offset = ctx_->Ebp; -#endif - - if (!machine_type_) { -#ifdef _M_X64 - machine_type_ = IMAGE_FILE_MACHINE_AMD64; -#else - machine_type_ = IMAGE_FILE_MACHINE_I386; -#endif - } - - for (;;) { - // NOTE: this only works if PDBs are already loaded! - SetLastError(0); - if (!StackWalk64(machine_type_, process, thd_, &s, ctx_, NULL, - SymFunctionTableAccess64, SymGetModuleBase64, NULL)) - break; - - if (s.AddrReturn.Offset == 0) - break; - - _stacktrace.push_back(reinterpret_cast(s.AddrPC.Offset)); - - if (size() >= depth) - break; - } - - return size(); - } - - size_t load_from(void *addr, size_t depth = 32, void *context = nullptr, - void *error_addr = nullptr) { - load_here(depth + 8, context, error_addr); - - for (size_t i = 0; i < _stacktrace.size(); ++i) { - if (_stacktrace[i] == addr) { - skip_n_firsts(i); - break; - } - } - - _stacktrace.resize(std::min(_stacktrace.size(), skip_n_firsts() + depth)); - return size(); - } - -private: - DWORD machine_type_ = 0; - HANDLE thd_ = 0; - CONTEXT *ctx_ = nullptr; -}; - -#endif - -class StackTrace : public StackTraceImpl {}; - -/*************** TRACE RESOLVER ***************/ - -class TraceResolverImplBase { -public: - virtual ~TraceResolverImplBase() {} - - virtual void load_addresses(void *const *addresses, int address_count) { - (void)addresses; - (void)address_count; - } - - template void load_stacktrace(ST &st) { - load_addresses(st.begin(), static_cast(st.size())); - } - - virtual ResolvedTrace resolve(ResolvedTrace t) { return t; } - -protected: - std::string demangle(const char *funcname) { - return _demangler.demangle(funcname); - } - -private: - details::demangler _demangler; -}; - -template class TraceResolverImpl; - -#ifdef BACKWARD_SYSTEM_UNKNOWN - -template <> -class TraceResolverImpl - : public TraceResolverImplBase {}; - -#endif - -#ifdef BACKWARD_SYSTEM_LINUX - -class TraceResolverLinuxBase : public TraceResolverImplBase { -public: - TraceResolverLinuxBase() - : argv0_(get_argv0()), exec_path_(read_symlink("/proc/self/exe")) {} - std::string resolve_exec_path(Dl_info &symbol_info) const { - // mutates symbol_info.dli_fname to be filename to open and returns filename - // to display - if (symbol_info.dli_fname == argv0_) { - // dladdr returns argv[0] in dli_fname for symbols contained in - // the main executable, which is not a valid path if the - // executable was found by a search of the PATH environment - // variable; In that case, we actually open /proc/self/exe, which - // is always the actual executable (even if it was deleted/replaced!) - // but display the path that /proc/self/exe links to. - // However, this right away reduces probability of successful symbol - // resolution, because libbfd may try to find *.debug files in the - // same dir, in case symbols are stripped. As a result, it may try - // to find a file /proc/self/.debug, which obviously does - // not exist. /proc/self/exe is a last resort. First load attempt - // should go for the original executable file path. - symbol_info.dli_fname = "/proc/self/exe"; - return exec_path_; - } else { - return symbol_info.dli_fname; - } - } - -private: - std::string argv0_; - std::string exec_path_; - - static std::string get_argv0() { - std::string argv0; - std::ifstream ifs("/proc/self/cmdline"); - std::getline(ifs, argv0, '\0'); - return argv0; - } - - static std::string read_symlink(std::string const &symlink_path) { - std::string path; - path.resize(100); - - while (true) { - ssize_t len = - ::readlink(symlink_path.c_str(), &*path.begin(), path.size()); - if (len < 0) { - return ""; - } - if (static_cast(len) == path.size()) { - path.resize(path.size() * 2); - } else { - path.resize(static_cast(len)); - break; - } - } - - return path; - } -}; - -template class TraceResolverLinuxImpl; - -#if BACKWARD_HAS_BACKTRACE_SYMBOL == 1 - -template <> -class TraceResolverLinuxImpl - : public TraceResolverLinuxBase { -public: - void load_addresses(void *const *addresses, int address_count) override { - if (address_count == 0) { - return; - } - _symbols.reset(backtrace_symbols(addresses, address_count)); - } - - ResolvedTrace resolve(ResolvedTrace trace) override { - char *filename = _symbols[trace.idx]; - char *funcname = filename; - while (*funcname && *funcname != '(') { - funcname += 1; - } - trace.object_filename.assign(filename, - funcname); // ok even if funcname is the ending - // \0 (then we assign entire string) - - if (*funcname) { // if it's not end of string (e.g. from last frame ip==0) - funcname += 1; - char *funcname_end = funcname; - while (*funcname_end && *funcname_end != ')' && *funcname_end != '+') { - funcname_end += 1; - } - *funcname_end = '\0'; - trace.object_function = this->demangle(funcname); - trace.source.function = trace.object_function; // we cannot do better. - } - return trace; - } - -private: - details::handle _symbols; -}; - -#endif // BACKWARD_HAS_BACKTRACE_SYMBOL == 1 - -#if BACKWARD_HAS_BFD == 1 - -template <> -class TraceResolverLinuxImpl - : public TraceResolverLinuxBase { -public: - TraceResolverLinuxImpl() : _bfd_loaded(false) {} - - ResolvedTrace resolve(ResolvedTrace trace) override { - Dl_info symbol_info; - - // trace.addr is a virtual address in memory pointing to some code. - // Let's try to find from which loaded object it comes from. - // The loaded object can be yourself btw. - if (!dladdr(trace.addr, &symbol_info)) { - return trace; // dat broken trace... - } - - // Now we get in symbol_info: - // .dli_fname: - // pathname of the shared object that contains the address. - // .dli_fbase: - // where the object is loaded in memory. - // .dli_sname: - // the name of the nearest symbol to trace.addr, we expect a - // function name. - // .dli_saddr: - // the exact address corresponding to .dli_sname. - - if (symbol_info.dli_sname) { - trace.object_function = demangle(symbol_info.dli_sname); - } - - if (!symbol_info.dli_fname) { - return trace; - } - - trace.object_filename = resolve_exec_path(symbol_info); - bfd_fileobject *fobj; - // Before rushing to resolution need to ensure the executable - // file still can be used. For that compare inode numbers of - // what is stored by the executable's file path, and in the - // dli_fname, which not necessarily equals to the executable. - // It can be a shared library, or /proc/self/exe, and in the - // latter case has drawbacks. See the exec path resolution for - // details. In short - the dli object should be used only as - // the last resort. - // If inode numbers are equal, it is known dli_fname and the - // executable file are the same. This is guaranteed by Linux, - // because if the executable file is changed/deleted, it will - // be done in a new inode. The old file will be preserved in - // /proc/self/exe, and may even have inode 0. The latter can - // happen if the inode was actually reused, and the file was - // kept only in the main memory. - // - struct stat obj_stat; - struct stat dli_stat; - if (stat(trace.object_filename.c_str(), &obj_stat) == 0 && - stat(symbol_info.dli_fname, &dli_stat) == 0 && - obj_stat.st_ino == dli_stat.st_ino) { - // The executable file, and the shared object containing the - // address are the same file. Safe to use the original path. - // this is preferable. Libbfd will search for stripped debug - // symbols in the same directory. - fobj = load_object_with_bfd(trace.object_filename); - } else { - // The original object file was *deleted*! The only hope is - // that the debug symbols are either inside the shared - // object file, or are in the same directory, and this is - // not /proc/self/exe. - fobj = nullptr; - } - if (fobj == nullptr || !fobj->handle) { - fobj = load_object_with_bfd(symbol_info.dli_fname); - if (!fobj->handle) { - return trace; - } - } - - find_sym_result *details_selected; // to be filled. - - // trace.addr is the next instruction to be executed after returning - // from the nested stack frame. In C++ this usually relate to the next - // statement right after the function call that leaded to a new stack - // frame. This is not usually what you want to see when printing out a - // stacktrace... - find_sym_result details_call_site = - find_symbol_details(fobj, trace.addr, symbol_info.dli_fbase); - details_selected = &details_call_site; - -#if BACKWARD_HAS_UNWIND == 0 - // ...this is why we also try to resolve the symbol that is right - // before the return address. If we are lucky enough, we will get the - // line of the function that was called. But if the code is optimized, - // we might get something absolutely not related since the compiler - // can reschedule the return address with inline functions and - // tail-call optimization (among other things that I don't even know - // or cannot even dream about with my tiny limited brain). - find_sym_result details_adjusted_call_site = find_symbol_details( - fobj, (void *)(uintptr_t(trace.addr) - 1), symbol_info.dli_fbase); - - // In debug mode, we should always get the right thing(TM). - if (details_call_site.found && details_adjusted_call_site.found) { - // Ok, we assume that details_adjusted_call_site is a better estimation. - details_selected = &details_adjusted_call_site; - trace.addr = (void *)(uintptr_t(trace.addr) - 1); - } - - if (details_selected == &details_call_site && details_call_site.found) { - // we have to re-resolve the symbol in order to reset some - // internal state in BFD... so we can call backtrace_inliners - // thereafter... - details_call_site = - find_symbol_details(fobj, trace.addr, symbol_info.dli_fbase); - } -#endif // BACKWARD_HAS_UNWIND - - if (details_selected->found) { - if (details_selected->filename) { - trace.source.filename = details_selected->filename; - } - trace.source.line = details_selected->line; - - if (details_selected->funcname) { - // this time we get the name of the function where the code is - // located, instead of the function were the address is - // located. In short, if the code was inlined, we get the - // function corresponding to the code. Else we already got in - // trace.function. - trace.source.function = demangle(details_selected->funcname); - - if (!symbol_info.dli_sname) { - // for the case dladdr failed to find the symbol name of - // the function, we might as well try to put something - // here. - trace.object_function = trace.source.function; - } - } - - // Maybe the source of the trace got inlined inside the function - // (trace.source.function). Let's see if we can get all the inlined - // calls along the way up to the initial call site. - trace.inliners = backtrace_inliners(fobj, *details_selected); - -#if 0 - if (trace.inliners.size() == 0) { - // Maybe the trace was not inlined... or maybe it was and we - // are lacking the debug information. Let's try to make the - // world better and see if we can get the line number of the - // function (trace.source.function) now. - // - // We will get the location of where the function start (to be - // exact: the first instruction that really start the - // function), not where the name of the function is defined. - // This can be quite far away from the name of the function - // btw. - // - // If the source of the function is the same as the source of - // the trace, we cannot say if the trace was really inlined or - // not. However, if the filename of the source is different - // between the function and the trace... we can declare it as - // an inliner. This is not 100% accurate, but better than - // nothing. - - if (symbol_info.dli_saddr) { - find_sym_result details = find_symbol_details(fobj, - symbol_info.dli_saddr, - symbol_info.dli_fbase); - - if (details.found) { - ResolvedTrace::SourceLoc diy_inliner; - diy_inliner.line = details.line; - if (details.filename) { - diy_inliner.filename = details.filename; - } - if (details.funcname) { - diy_inliner.function = demangle(details.funcname); - } else { - diy_inliner.function = trace.source.function; - } - if (diy_inliner != trace.source) { - trace.inliners.push_back(diy_inliner); - } - } - } - } -#endif - } - - return trace; - } - -private: - bool _bfd_loaded; - - typedef details::handle> - bfd_handle_t; - - typedef details::handle bfd_symtab_t; - - struct bfd_fileobject { - bfd_handle_t handle; - bfd_vma base_addr; - bfd_symtab_t symtab; - bfd_symtab_t dynamic_symtab; - }; - - typedef details::hashtable::type fobj_bfd_map_t; - fobj_bfd_map_t _fobj_bfd_map; - - bfd_fileobject *load_object_with_bfd(const std::string &filename_object) { - using namespace details; - - if (!_bfd_loaded) { - using namespace details; - bfd_init(); - _bfd_loaded = true; - } - - fobj_bfd_map_t::iterator it = _fobj_bfd_map.find(filename_object); - if (it != _fobj_bfd_map.end()) { - return &it->second; - } - - // this new object is empty for now. - bfd_fileobject *r = &_fobj_bfd_map[filename_object]; - - // we do the work temporary in this one; - bfd_handle_t bfd_handle; - - int fd = open(filename_object.c_str(), O_RDONLY); - bfd_handle.reset(bfd_fdopenr(filename_object.c_str(), "default", fd)); - if (!bfd_handle) { - close(fd); - return r; - } - - if (!bfd_check_format(bfd_handle.get(), bfd_object)) { - return r; // not an object? You lose. - } - - if ((bfd_get_file_flags(bfd_handle.get()) & HAS_SYMS) == 0) { - return r; // that's what happen when you forget to compile in debug. - } - - ssize_t symtab_storage_size = bfd_get_symtab_upper_bound(bfd_handle.get()); - - ssize_t dyn_symtab_storage_size = - bfd_get_dynamic_symtab_upper_bound(bfd_handle.get()); - - if (symtab_storage_size <= 0 && dyn_symtab_storage_size <= 0) { - return r; // weird, is the file is corrupted? - } - - bfd_symtab_t symtab, dynamic_symtab; - ssize_t symcount = 0, dyn_symcount = 0; - - if (symtab_storage_size > 0) { - symtab.reset(static_cast( - malloc(static_cast(symtab_storage_size)))); - symcount = bfd_canonicalize_symtab(bfd_handle.get(), symtab.get()); - } - - if (dyn_symtab_storage_size > 0) { - dynamic_symtab.reset(static_cast( - malloc(static_cast(dyn_symtab_storage_size)))); - dyn_symcount = bfd_canonicalize_dynamic_symtab(bfd_handle.get(), - dynamic_symtab.get()); - } - - if (symcount <= 0 && dyn_symcount <= 0) { - return r; // damned, that's a stripped file that you got there! - } - - r->handle = move(bfd_handle); - r->symtab = move(symtab); - r->dynamic_symtab = move(dynamic_symtab); - return r; - } - - struct find_sym_result { - bool found; - const char *filename; - const char *funcname; - unsigned int line; - }; - - struct find_sym_context { - TraceResolverLinuxImpl *self; - bfd_fileobject *fobj; - void *addr; - void *base_addr; - find_sym_result result; - }; - - find_sym_result find_symbol_details(bfd_fileobject *fobj, void *addr, - void *base_addr) { - find_sym_context context; - context.self = this; - context.fobj = fobj; - context.addr = addr; - context.base_addr = base_addr; - context.result.found = false; - bfd_map_over_sections(fobj->handle.get(), &find_in_section_trampoline, - static_cast(&context)); - return context.result; - } - - static void find_in_section_trampoline(bfd *, asection *section, void *data) { - find_sym_context *context = static_cast(data); - context->self->find_in_section( - reinterpret_cast(context->addr), - reinterpret_cast(context->base_addr), context->fobj, section, - context->result); - } - - void find_in_section(bfd_vma addr, bfd_vma base_addr, bfd_fileobject *fobj, - asection *section, find_sym_result &result) { - if (result.found) - return; - -#ifdef bfd_get_section_flags - if ((bfd_get_section_flags(fobj->handle.get(), section) & SEC_ALLOC) == 0) -#else - if ((bfd_section_flags(section) & SEC_ALLOC) == 0) -#endif - return; // a debug section is never loaded automatically. - -#ifdef bfd_get_section_vma - bfd_vma sec_addr = bfd_get_section_vma(fobj->handle.get(), section); -#else - bfd_vma sec_addr = bfd_section_vma(section); -#endif -#ifdef bfd_get_section_size - bfd_size_type size = bfd_get_section_size(section); -#else - bfd_size_type size = bfd_section_size(section); -#endif - - // are we in the boundaries of the section? - if (addr < sec_addr || addr >= sec_addr + size) { - addr -= base_addr; // oops, a relocated object, lets try again... - if (addr < sec_addr || addr >= sec_addr + size) { - return; - } - } - -#if defined(__clang__) -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" -#endif - if (!result.found && fobj->symtab) { - result.found = bfd_find_nearest_line( - fobj->handle.get(), section, fobj->symtab.get(), addr - sec_addr, - &result.filename, &result.funcname, &result.line); - } - - if (!result.found && fobj->dynamic_symtab) { - result.found = bfd_find_nearest_line( - fobj->handle.get(), section, fobj->dynamic_symtab.get(), - addr - sec_addr, &result.filename, &result.funcname, &result.line); - } -#if defined(__clang__) -#pragma clang diagnostic pop -#endif - } - - ResolvedTrace::source_locs_t - backtrace_inliners(bfd_fileobject *fobj, find_sym_result previous_result) { - // This function can be called ONLY after a SUCCESSFUL call to - // find_symbol_details. The state is global to the bfd_handle. - ResolvedTrace::source_locs_t results; - while (previous_result.found) { - find_sym_result result; - result.found = bfd_find_inliner_info(fobj->handle.get(), &result.filename, - &result.funcname, &result.line); - - if (result - .found) /* and not ( - cstrings_eq(previous_result.filename, - result.filename) and - cstrings_eq(previous_result.funcname, result.funcname) - and result.line == previous_result.line - )) */ - { - ResolvedTrace::SourceLoc src_loc; - src_loc.line = result.line; - if (result.filename) { - src_loc.filename = result.filename; - } - if (result.funcname) { - src_loc.function = demangle(result.funcname); - } - results.push_back(src_loc); - } - previous_result = result; - } - return results; - } - - bool cstrings_eq(const char *a, const char *b) { - if (!a || !b) { - return false; - } - return strcmp(a, b) == 0; - } -}; -#endif // BACKWARD_HAS_BFD == 1 - -#if BACKWARD_HAS_DW == 1 - -template <> -class TraceResolverLinuxImpl - : public TraceResolverLinuxBase { -public: - TraceResolverLinuxImpl() : _dwfl_handle_initialized(false) {} - - ResolvedTrace resolve(ResolvedTrace trace) override { - using namespace details; - - Dwarf_Addr trace_addr = reinterpret_cast(trace.addr); - - if (!_dwfl_handle_initialized) { - // initialize dwfl... - _dwfl_cb.reset(new Dwfl_Callbacks); - _dwfl_cb->find_elf = &dwfl_linux_proc_find_elf; - _dwfl_cb->find_debuginfo = &dwfl_standard_find_debuginfo; - _dwfl_cb->debuginfo_path = 0; - - _dwfl_handle.reset(dwfl_begin(_dwfl_cb.get())); - _dwfl_handle_initialized = true; - - if (!_dwfl_handle) { - return trace; - } - - // ...from the current process. - dwfl_report_begin(_dwfl_handle.get()); - int r = dwfl_linux_proc_report(_dwfl_handle.get(), getpid()); - dwfl_report_end(_dwfl_handle.get(), NULL, NULL); - if (r < 0) { - return trace; - } - } - - if (!_dwfl_handle) { - return trace; - } - - // find the module (binary object) that contains the trace's address. - // This is not using any debug information, but the addresses ranges of - // all the currently loaded binary object. - Dwfl_Module *mod = dwfl_addrmodule(_dwfl_handle.get(), trace_addr); - if (mod) { - // now that we found it, lets get the name of it, this will be the - // full path to the running binary or one of the loaded library. - const char *module_name = dwfl_module_info(mod, 0, 0, 0, 0, 0, 0, 0); - if (module_name) { - trace.object_filename = module_name; - } - // We also look after the name of the symbol, equal or before this - // address. This is found by walking the symtab. We should get the - // symbol corresponding to the function (mangled) containing the - // address. If the code corresponding to the address was inlined, - // this is the name of the out-most inliner function. - const char *sym_name = dwfl_module_addrname(mod, trace_addr); - if (sym_name) { - trace.object_function = demangle(sym_name); - } - } - - // now let's get serious, and find out the source location (file and - // line number) of the address. - - // This function will look in .debug_aranges for the address and map it - // to the location of the compilation unit DIE in .debug_info and - // return it. - Dwarf_Addr mod_bias = 0; - Dwarf_Die *cudie = dwfl_module_addrdie(mod, trace_addr, &mod_bias); - -#if 1 - if (!cudie) { - // Sadly clang does not generate the section .debug_aranges, thus - // dwfl_module_addrdie will fail early. Clang doesn't either set - // the lowpc/highpc/range info for every compilation unit. - // - // So in order to save the world: - // for every compilation unit, we will iterate over every single - // DIEs. Normally functions should have a lowpc/highpc/range, which - // we will use to infer the compilation unit. - - // note that this is probably badly inefficient. - while ((cudie = dwfl_module_nextcu(mod, cudie, &mod_bias))) { - Dwarf_Die die_mem; - Dwarf_Die *fundie = - find_fundie_by_pc(cudie, trace_addr - mod_bias, &die_mem); - if (fundie) { - break; - } - } - } -#endif - -// #define BACKWARD_I_DO_NOT_RECOMMEND_TO_ENABLE_THIS_HORRIBLE_PIECE_OF_CODE -#ifdef BACKWARD_I_DO_NOT_RECOMMEND_TO_ENABLE_THIS_HORRIBLE_PIECE_OF_CODE - if (!cudie) { - // If it's still not enough, lets dive deeper in the shit, and try - // to save the world again: for every compilation unit, we will - // load the corresponding .debug_line section, and see if we can - // find our address in it. - - Dwarf_Addr cfi_bias; - Dwarf_CFI *cfi_cache = dwfl_module_eh_cfi(mod, &cfi_bias); - - Dwarf_Addr bias; - while ((cudie = dwfl_module_nextcu(mod, cudie, &bias))) { - if (dwarf_getsrc_die(cudie, trace_addr - bias)) { - - // ...but if we get a match, it might be a false positive - // because our (address - bias) might as well be valid in a - // different compilation unit. So we throw our last card on - // the table and lookup for the address into the .eh_frame - // section. - - handle frame; - dwarf_cfi_addrframe(cfi_cache, trace_addr - cfi_bias, &frame); - if (frame) { - break; - } - } - } - } -#endif - - if (!cudie) { - return trace; // this time we lost the game :/ - } - - // Now that we have a compilation unit DIE, this function will be able - // to load the corresponding section in .debug_line (if not already - // loaded) and hopefully find the source location mapped to our - // address. - Dwarf_Line *srcloc = dwarf_getsrc_die(cudie, trace_addr - mod_bias); - - if (srcloc) { - const char *srcfile = dwarf_linesrc(srcloc, 0, 0); - if (srcfile) { - trace.source.filename = srcfile; - } - int line = 0, col = 0; - dwarf_lineno(srcloc, &line); - dwarf_linecol(srcloc, &col); - trace.source.line = static_cast(line); - trace.source.col = static_cast(col); - } - - deep_first_search_by_pc(cudie, trace_addr - mod_bias, - inliners_search_cb(trace)); - if (trace.source.function.size() == 0) { - // fallback. - trace.source.function = trace.object_function; - } - - return trace; - } - -private: - typedef details::handle> - dwfl_handle_t; - details::handle> - _dwfl_cb; - dwfl_handle_t _dwfl_handle; - bool _dwfl_handle_initialized; - - // defined here because in C++98, template function cannot take locally - // defined types... grrr. - struct inliners_search_cb { - void operator()(Dwarf_Die *die) { - switch (dwarf_tag(die)) { - const char *name; - case DW_TAG_subprogram: - if ((name = dwarf_diename(die))) { - trace.source.function = name; - } - break; - - case DW_TAG_inlined_subroutine: - ResolvedTrace::SourceLoc sloc; - Dwarf_Attribute attr_mem; - - if ((name = dwarf_diename(die))) { - sloc.function = name; - } - if ((name = die_call_file(die))) { - sloc.filename = name; - } - - Dwarf_Word line = 0, col = 0; - dwarf_formudata(dwarf_attr(die, DW_AT_call_line, &attr_mem), &line); - dwarf_formudata(dwarf_attr(die, DW_AT_call_column, &attr_mem), &col); - sloc.line = static_cast(line); - sloc.col = static_cast(col); - - trace.inliners.push_back(sloc); - break; - }; - } - ResolvedTrace &trace; - inliners_search_cb(ResolvedTrace &t) : trace(t) {} - }; - - static bool die_has_pc(Dwarf_Die *die, Dwarf_Addr pc) { - Dwarf_Addr low, high; - - // continuous range - if (dwarf_hasattr(die, DW_AT_low_pc) && dwarf_hasattr(die, DW_AT_high_pc)) { - if (dwarf_lowpc(die, &low) != 0) { - return false; - } - if (dwarf_highpc(die, &high) != 0) { - Dwarf_Attribute attr_mem; - Dwarf_Attribute *attr = dwarf_attr(die, DW_AT_high_pc, &attr_mem); - Dwarf_Word value; - if (dwarf_formudata(attr, &value) != 0) { - return false; - } - high = low + value; - } - return pc >= low && pc < high; - } - - // non-continuous range. - Dwarf_Addr base; - ptrdiff_t offset = 0; - while ((offset = dwarf_ranges(die, offset, &base, &low, &high)) > 0) { - if (pc >= low && pc < high) { - return true; - } - } - return false; - } - - static Dwarf_Die *find_fundie_by_pc(Dwarf_Die *parent_die, Dwarf_Addr pc, - Dwarf_Die *result) { - if (dwarf_child(parent_die, result) != 0) { - return 0; - } - - Dwarf_Die *die = result; - do { - switch (dwarf_tag(die)) { - case DW_TAG_subprogram: - case DW_TAG_inlined_subroutine: - if (die_has_pc(die, pc)) { - return result; - } - }; - bool declaration = false; - Dwarf_Attribute attr_mem; - dwarf_formflag(dwarf_attr(die, DW_AT_declaration, &attr_mem), - &declaration); - if (!declaration) { - // let's be curious and look deeper in the tree, - // function are not necessarily at the first level, but - // might be nested inside a namespace, structure etc. - Dwarf_Die die_mem; - Dwarf_Die *indie = find_fundie_by_pc(die, pc, &die_mem); - if (indie) { - *result = die_mem; - return result; - } - } - } while (dwarf_siblingof(die, result) == 0); - return 0; - } - - template - static bool deep_first_search_by_pc(Dwarf_Die *parent_die, Dwarf_Addr pc, - CB cb) { - Dwarf_Die die_mem; - if (dwarf_child(parent_die, &die_mem) != 0) { - return false; - } - - bool branch_has_pc = false; - Dwarf_Die *die = &die_mem; - do { - bool declaration = false; - Dwarf_Attribute attr_mem; - dwarf_formflag(dwarf_attr(die, DW_AT_declaration, &attr_mem), - &declaration); - if (!declaration) { - // let's be curious and look deeper in the tree, function are - // not necessarily at the first level, but might be nested - // inside a namespace, structure, a function, an inlined - // function etc. - branch_has_pc = deep_first_search_by_pc(die, pc, cb); - } - if (!branch_has_pc) { - branch_has_pc = die_has_pc(die, pc); - } - if (branch_has_pc) { - cb(die); - } - } while (dwarf_siblingof(die, &die_mem) == 0); - return branch_has_pc; - } - - static const char *die_call_file(Dwarf_Die *die) { - Dwarf_Attribute attr_mem; - Dwarf_Word file_idx = 0; - - dwarf_formudata(dwarf_attr(die, DW_AT_call_file, &attr_mem), &file_idx); - - if (file_idx == 0) { - return 0; - } - - Dwarf_Die die_mem; - Dwarf_Die *cudie = dwarf_diecu(die, &die_mem, 0, 0); - if (!cudie) { - return 0; - } - - Dwarf_Files *files = 0; - size_t nfiles; - dwarf_getsrcfiles(cudie, &files, &nfiles); - if (!files) { - return 0; - } - - return dwarf_filesrc(files, file_idx, 0, 0); - } -}; -#endif // BACKWARD_HAS_DW == 1 - -#if BACKWARD_HAS_DWARF == 1 - -template <> -class TraceResolverLinuxImpl - : public TraceResolverLinuxBase { -public: - TraceResolverLinuxImpl() : _dwarf_loaded(false) {} - - ResolvedTrace resolve(ResolvedTrace trace) override { - // trace.addr is a virtual address in memory pointing to some code. - // Let's try to find from which loaded object it comes from. - // The loaded object can be yourself btw. - - Dl_info symbol_info; - int dladdr_result = 0; -#if defined(__GLIBC__) - link_map *link_map; - // We request the link map so we can get information about offsets - dladdr_result = - dladdr1(trace.addr, &symbol_info, reinterpret_cast(&link_map), - RTLD_DL_LINKMAP); -#else - // Android doesn't have dladdr1. Don't use the linker map. - dladdr_result = dladdr(trace.addr, &symbol_info); -#endif - if (!dladdr_result) { - return trace; // dat broken trace... - } - - // Now we get in symbol_info: - // .dli_fname: - // pathname of the shared object that contains the address. - // .dli_fbase: - // where the object is loaded in memory. - // .dli_sname: - // the name of the nearest symbol to trace.addr, we expect a - // function name. - // .dli_saddr: - // the exact address corresponding to .dli_sname. - // - // And in link_map: - // .l_addr: - // difference between the address in the ELF file and the address - // in memory - // l_name: - // absolute pathname where the object was found - - if (symbol_info.dli_sname) { - trace.object_function = demangle(symbol_info.dli_sname); - } - - if (!symbol_info.dli_fname) { - return trace; - } - - trace.object_filename = resolve_exec_path(symbol_info); - dwarf_fileobject &fobj = load_object_with_dwarf(symbol_info.dli_fname); - if (!fobj.dwarf_handle) { - return trace; // sad, we couldn't load the object :( - } - -#if defined(__GLIBC__) - // Convert the address to a module relative one by looking at - // the module's loading address in the link map - Dwarf_Addr address = reinterpret_cast(trace.addr) - - reinterpret_cast(link_map->l_addr); -#else - Dwarf_Addr address = reinterpret_cast(trace.addr); -#endif - - if (trace.object_function.empty()) { - symbol_cache_t::iterator it = fobj.symbol_cache.lower_bound(address); - - if (it != fobj.symbol_cache.end()) { - if (it->first != address) { - if (it != fobj.symbol_cache.begin()) { - --it; - } - } - trace.object_function = demangle(it->second.c_str()); - } - } - - // Get the Compilation Unit DIE for the address - Dwarf_Die die = find_die(fobj, address); - - if (!die) { - return trace; // this time we lost the game :/ - } - - // libdwarf doesn't give us direct access to its objects, it always - // allocates a copy for the caller. We keep that copy alive in a cache - // and we deallocate it later when it's no longer required. - die_cache_entry &die_object = get_die_cache(fobj, die); - if (die_object.isEmpty()) - return trace; // We have no line section for this DIE - - die_linemap_t::iterator it = die_object.line_section.lower_bound(address); - - if (it != die_object.line_section.end()) { - if (it->first != address) { - if (it == die_object.line_section.begin()) { - // If we are on the first item of the line section - // but the address does not match it means that - // the address is below the range of the DIE. Give up. - return trace; - } else { - --it; - } - } - } else { - return trace; // We didn't find the address. - } - - // Get the Dwarf_Line that the address points to and call libdwarf - // to get source file, line and column info. - Dwarf_Line line = die_object.line_buffer[it->second]; - Dwarf_Error error = DW_DLE_NE; - - char *filename; - if (dwarf_linesrc(line, &filename, &error) == DW_DLV_OK) { - trace.source.filename = std::string(filename); - dwarf_dealloc(fobj.dwarf_handle.get(), filename, DW_DLA_STRING); - } - - Dwarf_Unsigned number = 0; - if (dwarf_lineno(line, &number, &error) == DW_DLV_OK) { - trace.source.line = number; - } else { - trace.source.line = 0; - } - - if (dwarf_lineoff_b(line, &number, &error) == DW_DLV_OK) { - trace.source.col = number; - } else { - trace.source.col = 0; - } - - std::vector namespace_stack; - deep_first_search_by_pc(fobj, die, address, namespace_stack, - inliners_search_cb(trace, fobj, die)); - - dwarf_dealloc(fobj.dwarf_handle.get(), die, DW_DLA_DIE); - - return trace; - } - -public: - static int close_dwarf(Dwarf_Debug dwarf) { - return dwarf_finish(dwarf, NULL); - } - -private: - bool _dwarf_loaded; - - typedef details::handle> - dwarf_file_t; - - typedef details::handle> - dwarf_elf_t; - - typedef details::handle> - dwarf_handle_t; - - typedef std::map die_linemap_t; - - typedef std::map die_specmap_t; - - struct die_cache_entry { - die_specmap_t spec_section; - die_linemap_t line_section; - Dwarf_Line *line_buffer; - Dwarf_Signed line_count; - Dwarf_Line_Context line_context; - - inline bool isEmpty() { - return line_buffer == NULL || line_count == 0 || line_context == NULL || - line_section.empty(); - } - - die_cache_entry() : line_buffer(0), line_count(0), line_context(0) {} - - ~die_cache_entry() { - if (line_context) { - dwarf_srclines_dealloc_b(line_context); - } - } - }; - - typedef std::map die_cache_t; - - typedef std::map symbol_cache_t; - - struct dwarf_fileobject { - dwarf_file_t file_handle; - dwarf_elf_t elf_handle; - dwarf_handle_t dwarf_handle; - symbol_cache_t symbol_cache; - - // Die cache - die_cache_t die_cache; - die_cache_entry *current_cu; - }; - - typedef details::hashtable::type - fobj_dwarf_map_t; - fobj_dwarf_map_t _fobj_dwarf_map; - - static bool cstrings_eq(const char *a, const char *b) { - if (!a || !b) { - return false; - } - return strcmp(a, b) == 0; - } - - dwarf_fileobject &load_object_with_dwarf(const std::string &filename_object) { - - if (!_dwarf_loaded) { - // Set the ELF library operating version - // If that fails there's nothing we can do - _dwarf_loaded = elf_version(EV_CURRENT) != EV_NONE; - } - - fobj_dwarf_map_t::iterator it = _fobj_dwarf_map.find(filename_object); - if (it != _fobj_dwarf_map.end()) { - return it->second; - } - - // this new object is empty for now - dwarf_fileobject &r = _fobj_dwarf_map[filename_object]; - - dwarf_file_t file_handle; - file_handle.reset(open(filename_object.c_str(), O_RDONLY)); - if (file_handle.get() < 0) { - return r; - } - - // Try to get an ELF handle. We need to read the ELF sections - // because we want to see if there is a .gnu_debuglink section - // that points to a split debug file - dwarf_elf_t elf_handle; - elf_handle.reset(elf_begin(file_handle.get(), ELF_C_READ, NULL)); - if (!elf_handle) { - return r; - } - - const char *e_ident = elf_getident(elf_handle.get(), 0); - if (!e_ident) { - return r; - } - - // Get the number of sections - // We use the new APIs as elf_getshnum is deprecated - size_t shdrnum = 0; - if (elf_getshdrnum(elf_handle.get(), &shdrnum) == -1) { - return r; - } - - // Get the index to the string section - size_t shdrstrndx = 0; - if (elf_getshdrstrndx(elf_handle.get(), &shdrstrndx) == -1) { - return r; - } - - std::string debuglink; - // Iterate through the ELF sections to try to get a gnu_debuglink - // note and also to cache the symbol table. - // We go the preprocessor way to avoid having to create templated - // classes or using gelf (which might throw a compiler error if 64 bit - // is not supported -#define ELF_GET_DATA(ARCH) \ - Elf_Scn *elf_section = 0; \ - Elf_Data *elf_data = 0; \ - Elf##ARCH##_Shdr *section_header = 0; \ - Elf_Scn *symbol_section = 0; \ - size_t symbol_count = 0; \ - size_t symbol_strings = 0; \ - Elf##ARCH##_Sym *symbol = 0; \ - const char *section_name = 0; \ - \ - while ((elf_section = elf_nextscn(elf_handle.get(), elf_section)) != NULL) { \ - section_header = elf##ARCH##_getshdr(elf_section); \ - if (section_header == NULL) { \ - return r; \ - } \ - \ - if ((section_name = elf_strptr(elf_handle.get(), shdrstrndx, \ - section_header->sh_name)) == NULL) { \ - return r; \ - } \ - \ - if (cstrings_eq(section_name, ".gnu_debuglink")) { \ - elf_data = elf_getdata(elf_section, NULL); \ - if (elf_data && elf_data->d_size > 0) { \ - debuglink = \ - std::string(reinterpret_cast(elf_data->d_buf)); \ - } \ - } \ - \ - switch (section_header->sh_type) { \ - case SHT_SYMTAB: \ - symbol_section = elf_section; \ - symbol_count = section_header->sh_size / section_header->sh_entsize; \ - symbol_strings = section_header->sh_link; \ - break; \ - \ - /* We use .dynsyms as a last resort, we prefer .symtab */ \ - case SHT_DYNSYM: \ - if (!symbol_section) { \ - symbol_section = elf_section; \ - symbol_count = section_header->sh_size / section_header->sh_entsize; \ - symbol_strings = section_header->sh_link; \ - } \ - break; \ - } \ - } \ - \ - if (symbol_section && symbol_count && symbol_strings) { \ - elf_data = elf_getdata(symbol_section, NULL); \ - symbol = reinterpret_cast(elf_data->d_buf); \ - for (size_t i = 0; i < symbol_count; ++i) { \ - int type = ELF##ARCH##_ST_TYPE(symbol->st_info); \ - if (type == STT_FUNC && symbol->st_value > 0) { \ - r.symbol_cache[symbol->st_value] = std::string( \ - elf_strptr(elf_handle.get(), symbol_strings, symbol->st_name)); \ - } \ - ++symbol; \ - } \ - } - - if (e_ident[EI_CLASS] == ELFCLASS32) { - ELF_GET_DATA(32) - } else if (e_ident[EI_CLASS] == ELFCLASS64) { - // libelf might have been built without 64 bit support -#if __LIBELF64 - ELF_GET_DATA(64) -#endif - } - - if (!debuglink.empty()) { - // We have a debuglink section! Open an elf instance on that - // file instead. If we can't open the file, then return - // the elf handle we had already opened. - dwarf_file_t debuglink_file; - debuglink_file.reset(open(debuglink.c_str(), O_RDONLY)); - if (debuglink_file.get() > 0) { - dwarf_elf_t debuglink_elf; - debuglink_elf.reset(elf_begin(debuglink_file.get(), ELF_C_READ, NULL)); - - // If we have a valid elf handle, return the new elf handle - // and file handle and discard the original ones - if (debuglink_elf) { - elf_handle = move(debuglink_elf); - file_handle = move(debuglink_file); - } - } - } - - // Ok, we have a valid ELF handle, let's try to get debug symbols - Dwarf_Debug dwarf_debug; - Dwarf_Error error = DW_DLE_NE; - dwarf_handle_t dwarf_handle; - - int dwarf_result = dwarf_elf_init(elf_handle.get(), DW_DLC_READ, NULL, NULL, - &dwarf_debug, &error); - - // We don't do any special handling for DW_DLV_NO_ENTRY specially. - // If we get an error, or the file doesn't have debug information - // we just return. - if (dwarf_result != DW_DLV_OK) { - return r; - } - - dwarf_handle.reset(dwarf_debug); - - r.file_handle = move(file_handle); - r.elf_handle = move(elf_handle); - r.dwarf_handle = move(dwarf_handle); - - return r; - } - - die_cache_entry &get_die_cache(dwarf_fileobject &fobj, Dwarf_Die die) { - Dwarf_Error error = DW_DLE_NE; - - // Get the die offset, we use it as the cache key - Dwarf_Off die_offset; - if (dwarf_dieoffset(die, &die_offset, &error) != DW_DLV_OK) { - die_offset = 0; - } - - die_cache_t::iterator it = fobj.die_cache.find(die_offset); - - if (it != fobj.die_cache.end()) { - fobj.current_cu = &it->second; - return it->second; - } - - die_cache_entry &de = fobj.die_cache[die_offset]; - fobj.current_cu = &de; - - Dwarf_Addr line_addr; - Dwarf_Small table_count; - - // The addresses in the line section are not fully sorted (they might - // be sorted by block of code belonging to the same file), which makes - // it necessary to do so before searching is possible. - // - // As libdwarf allocates a copy of everything, let's get the contents - // of the line section and keep it around. We also create a map of - // program counter to line table indices so we can search by address - // and get the line buffer index. - // - // To make things more difficult, the same address can span more than - // one line, so we need to keep the index pointing to the first line - // by using insert instead of the map's [ operator. - - // Get the line context for the DIE - if (dwarf_srclines_b(die, 0, &table_count, &de.line_context, &error) == - DW_DLV_OK) { - // Get the source lines for this line context, to be deallocated - // later - if (dwarf_srclines_from_linecontext(de.line_context, &de.line_buffer, - &de.line_count, - &error) == DW_DLV_OK) { - - // Add all the addresses to our map - for (int i = 0; i < de.line_count; i++) { - if (dwarf_lineaddr(de.line_buffer[i], &line_addr, &error) != - DW_DLV_OK) { - line_addr = 0; - } - de.line_section.insert(std::pair(line_addr, i)); - } - } - } - - // For each CU, cache the function DIEs that contain the - // DW_AT_specification attribute. When building with -g3 the function - // DIEs are separated in declaration and specification, with the - // declaration containing only the name and parameters and the - // specification the low/high pc and other compiler attributes. - // - // We cache those specifications so we don't skip over the declarations, - // because they have no pc, and we can do namespace resolution for - // DWARF function names. - Dwarf_Debug dwarf = fobj.dwarf_handle.get(); - Dwarf_Die current_die = 0; - if (dwarf_child(die, ¤t_die, &error) == DW_DLV_OK) { - for (;;) { - Dwarf_Die sibling_die = 0; - - Dwarf_Half tag_value; - dwarf_tag(current_die, &tag_value, &error); - - if (tag_value == DW_TAG_subprogram || - tag_value == DW_TAG_inlined_subroutine) { - - Dwarf_Bool has_attr = 0; - if (dwarf_hasattr(current_die, DW_AT_specification, &has_attr, - &error) == DW_DLV_OK) { - if (has_attr) { - Dwarf_Attribute attr_mem; - if (dwarf_attr(current_die, DW_AT_specification, &attr_mem, - &error) == DW_DLV_OK) { - Dwarf_Off spec_offset = 0; - if (dwarf_formref(attr_mem, &spec_offset, &error) == - DW_DLV_OK) { - Dwarf_Off spec_die_offset; - if (dwarf_dieoffset(current_die, &spec_die_offset, &error) == - DW_DLV_OK) { - de.spec_section[spec_offset] = spec_die_offset; - } - } - } - dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR); - } - } - } - - int result = dwarf_siblingof(dwarf, current_die, &sibling_die, &error); - if (result == DW_DLV_ERROR) { - break; - } else if (result == DW_DLV_NO_ENTRY) { - break; - } - - if (current_die != die) { - dwarf_dealloc(dwarf, current_die, DW_DLA_DIE); - current_die = 0; - } - - current_die = sibling_die; - } - } - return de; - } - - static Dwarf_Die get_referenced_die(Dwarf_Debug dwarf, Dwarf_Die die, - Dwarf_Half attr, bool global) { - Dwarf_Error error = DW_DLE_NE; - Dwarf_Attribute attr_mem; - - Dwarf_Die found_die = NULL; - if (dwarf_attr(die, attr, &attr_mem, &error) == DW_DLV_OK) { - Dwarf_Off offset; - int result = 0; - if (global) { - result = dwarf_global_formref(attr_mem, &offset, &error); - } else { - result = dwarf_formref(attr_mem, &offset, &error); - } - - if (result == DW_DLV_OK) { - if (dwarf_offdie(dwarf, offset, &found_die, &error) != DW_DLV_OK) { - found_die = NULL; - } - } - dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR); - } - return found_die; - } - - static std::string get_referenced_die_name(Dwarf_Debug dwarf, Dwarf_Die die, - Dwarf_Half attr, bool global) { - Dwarf_Error error = DW_DLE_NE; - std::string value; - - Dwarf_Die found_die = get_referenced_die(dwarf, die, attr, global); - - if (found_die) { - char *name; - if (dwarf_diename(found_die, &name, &error) == DW_DLV_OK) { - if (name) { - value = std::string(name); - } - dwarf_dealloc(dwarf, name, DW_DLA_STRING); - } - dwarf_dealloc(dwarf, found_die, DW_DLA_DIE); - } - - return value; - } - - // Returns a spec DIE linked to the passed one. The caller should - // deallocate the DIE - static Dwarf_Die get_spec_die(dwarf_fileobject &fobj, Dwarf_Die die) { - Dwarf_Debug dwarf = fobj.dwarf_handle.get(); - Dwarf_Error error = DW_DLE_NE; - Dwarf_Off die_offset; - if (fobj.current_cu && - dwarf_die_CU_offset(die, &die_offset, &error) == DW_DLV_OK) { - die_specmap_t::iterator it = - fobj.current_cu->spec_section.find(die_offset); - - // If we have a DIE that completes the current one, check if - // that one has the pc we are looking for - if (it != fobj.current_cu->spec_section.end()) { - Dwarf_Die spec_die = 0; - if (dwarf_offdie(dwarf, it->second, &spec_die, &error) == DW_DLV_OK) { - return spec_die; - } - } - } - - // Maybe we have an abstract origin DIE with the function information? - return get_referenced_die(fobj.dwarf_handle.get(), die, - DW_AT_abstract_origin, true); - } - - static bool die_has_pc(dwarf_fileobject &fobj, Dwarf_Die die, Dwarf_Addr pc) { - Dwarf_Addr low_pc = 0, high_pc = 0; - Dwarf_Half high_pc_form = 0; - Dwarf_Form_Class return_class; - Dwarf_Error error = DW_DLE_NE; - Dwarf_Debug dwarf = fobj.dwarf_handle.get(); - bool has_lowpc = false; - bool has_highpc = false; - bool has_ranges = false; - - if (dwarf_lowpc(die, &low_pc, &error) == DW_DLV_OK) { - // If we have a low_pc check if there is a high pc. - // If we don't have a high pc this might mean we have a base - // address for the ranges list or just an address. - has_lowpc = true; - - if (dwarf_highpc_b(die, &high_pc, &high_pc_form, &return_class, &error) == - DW_DLV_OK) { - // We do have a high pc. In DWARF 4+ this is an offset from the - // low pc, but in earlier versions it's an absolute address. - - has_highpc = true; - // In DWARF 2/3 this would be a DW_FORM_CLASS_ADDRESS - if (return_class == DW_FORM_CLASS_CONSTANT) { - high_pc = low_pc + high_pc; - } - - // We have low and high pc, check if our address - // is in that range - return pc >= low_pc && pc < high_pc; - } - } else { - // Reset the low_pc, in case dwarf_lowpc failing set it to some - // undefined value. - low_pc = 0; - } - - // Check if DW_AT_ranges is present and search for the PC in the - // returned ranges list. We always add the low_pc, as it not set it will - // be 0, in case we had a DW_AT_low_pc and DW_AT_ranges pair - bool result = false; - - Dwarf_Attribute attr; - if (dwarf_attr(die, DW_AT_ranges, &attr, &error) == DW_DLV_OK) { - - Dwarf_Off offset; - if (dwarf_global_formref(attr, &offset, &error) == DW_DLV_OK) { - Dwarf_Ranges *ranges; - Dwarf_Signed ranges_count = 0; - Dwarf_Unsigned byte_count = 0; - - if (dwarf_get_ranges_a(dwarf, offset, die, &ranges, &ranges_count, - &byte_count, &error) == DW_DLV_OK) { - has_ranges = ranges_count != 0; - for (int i = 0; i < ranges_count; i++) { - if (ranges[i].dwr_addr1 != 0 && - pc >= ranges[i].dwr_addr1 + low_pc && - pc < ranges[i].dwr_addr2 + low_pc) { - result = true; - break; - } - } - dwarf_ranges_dealloc(dwarf, ranges, ranges_count); - } - } - } - - // Last attempt. We might have a single address set as low_pc. - if (!result && low_pc != 0 && pc == low_pc) { - result = true; - } - - // If we don't have lowpc, highpc and ranges maybe this DIE is a - // declaration that relies on a DW_AT_specification DIE that happens - // later. Use the specification cache we filled when we loaded this CU. - if (!result && (!has_lowpc && !has_highpc && !has_ranges)) { - Dwarf_Die spec_die = get_spec_die(fobj, die); - if (spec_die) { - result = die_has_pc(fobj, spec_die, pc); - dwarf_dealloc(dwarf, spec_die, DW_DLA_DIE); - } - } - - return result; - } - - static void get_type(Dwarf_Debug dwarf, Dwarf_Die die, std::string &type) { - Dwarf_Error error = DW_DLE_NE; - - Dwarf_Die child = 0; - if (dwarf_child(die, &child, &error) == DW_DLV_OK) { - get_type(dwarf, child, type); - } - - if (child) { - type.insert(0, "::"); - dwarf_dealloc(dwarf, child, DW_DLA_DIE); - } - - char *name; - if (dwarf_diename(die, &name, &error) == DW_DLV_OK) { - type.insert(0, std::string(name)); - dwarf_dealloc(dwarf, name, DW_DLA_STRING); - } else { - type.insert(0, ""); - } - } - - static std::string get_type_by_signature(Dwarf_Debug dwarf, Dwarf_Die die) { - Dwarf_Error error = DW_DLE_NE; - - Dwarf_Sig8 signature; - Dwarf_Bool has_attr = 0; - if (dwarf_hasattr(die, DW_AT_signature, &has_attr, &error) == DW_DLV_OK) { - if (has_attr) { - Dwarf_Attribute attr_mem; - if (dwarf_attr(die, DW_AT_signature, &attr_mem, &error) == DW_DLV_OK) { - if (dwarf_formsig8(attr_mem, &signature, &error) != DW_DLV_OK) { - return std::string(""); - } - } - dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR); - } - } - - Dwarf_Unsigned next_cu_header; - Dwarf_Sig8 tu_signature; - std::string result; - bool found = false; - - while (dwarf_next_cu_header_d(dwarf, 0, 0, 0, 0, 0, 0, 0, &tu_signature, 0, - &next_cu_header, 0, &error) == DW_DLV_OK) { - - if (strncmp(signature.signature, tu_signature.signature, 8) == 0) { - Dwarf_Die type_cu_die = 0; - if (dwarf_siblingof_b(dwarf, 0, 0, &type_cu_die, &error) == DW_DLV_OK) { - Dwarf_Die child_die = 0; - if (dwarf_child(type_cu_die, &child_die, &error) == DW_DLV_OK) { - get_type(dwarf, child_die, result); - found = !result.empty(); - dwarf_dealloc(dwarf, child_die, DW_DLA_DIE); - } - dwarf_dealloc(dwarf, type_cu_die, DW_DLA_DIE); - } - } - } - - if (found) { - while (dwarf_next_cu_header_d(dwarf, 0, 0, 0, 0, 0, 0, 0, 0, 0, - &next_cu_header, 0, &error) == DW_DLV_OK) { - // Reset the cu header state. Unfortunately, libdwarf's - // next_cu_header API keeps its own iterator per Dwarf_Debug - // that can't be reset. We need to keep fetching elements until - // the end. - } - } else { - // If we couldn't resolve the type just print out the signature - std::ostringstream string_stream; - string_stream << "<0x" << std::hex << std::setfill('0'); - for (int i = 0; i < 8; ++i) { - string_stream << std::setw(2) << std::hex - << (int)(unsigned char)(signature.signature[i]); - } - string_stream << ">"; - result = string_stream.str(); - } - return result; - } - - struct type_context_t { - bool is_const; - bool is_typedef; - bool has_type; - bool has_name; - std::string text; - - type_context_t() - : is_const(false), is_typedef(false), has_type(false), has_name(false) { - } - }; - - // Types are resolved from right to left: we get the variable name first - // and then all specifiers (like const or pointer) in a chain of DW_AT_type - // DIEs. Call this function recursively until we get a complete type - // string. - static void set_parameter_string(dwarf_fileobject &fobj, Dwarf_Die die, - type_context_t &context) { - char *name; - Dwarf_Error error = DW_DLE_NE; - - // typedefs contain also the base type, so we skip it and only - // print the typedef name - if (!context.is_typedef) { - if (dwarf_diename(die, &name, &error) == DW_DLV_OK) { - if (!context.text.empty()) { - context.text.insert(0, " "); - } - context.text.insert(0, std::string(name)); - dwarf_dealloc(fobj.dwarf_handle.get(), name, DW_DLA_STRING); - } - } else { - context.is_typedef = false; - context.has_type = true; - if (context.is_const) { - context.text.insert(0, "const "); - context.is_const = false; - } - } - - bool next_type_is_const = false; - bool is_keyword = true; - - Dwarf_Half tag = 0; - Dwarf_Bool has_attr = 0; - if (dwarf_tag(die, &tag, &error) == DW_DLV_OK) { - switch (tag) { - case DW_TAG_structure_type: - case DW_TAG_union_type: - case DW_TAG_class_type: - case DW_TAG_enumeration_type: - context.has_type = true; - if (dwarf_hasattr(die, DW_AT_signature, &has_attr, &error) == - DW_DLV_OK) { - // If we have a signature it means the type is defined - // in .debug_types, so we need to load the DIE pointed - // at by the signature and resolve it - if (has_attr) { - std::string type = - get_type_by_signature(fobj.dwarf_handle.get(), die); - if (context.is_const) - type.insert(0, "const "); - - if (!context.text.empty()) - context.text.insert(0, " "); - context.text.insert(0, type); - } - - // Treat enums like typedefs, and skip printing its - // base type - context.is_typedef = (tag == DW_TAG_enumeration_type); - } - break; - case DW_TAG_const_type: - next_type_is_const = true; - break; - case DW_TAG_pointer_type: - context.text.insert(0, "*"); - break; - case DW_TAG_reference_type: - context.text.insert(0, "&"); - break; - case DW_TAG_restrict_type: - context.text.insert(0, "restrict "); - break; - case DW_TAG_rvalue_reference_type: - context.text.insert(0, "&&"); - break; - case DW_TAG_volatile_type: - context.text.insert(0, "volatile "); - break; - case DW_TAG_typedef: - // Propagate the const-ness to the next type - // as typedefs are linked to its base type - next_type_is_const = context.is_const; - context.is_typedef = true; - context.has_type = true; - break; - case DW_TAG_base_type: - context.has_type = true; - break; - case DW_TAG_formal_parameter: - context.has_name = true; - break; - default: - is_keyword = false; - break; - } - } - - if (!is_keyword && context.is_const) { - context.text.insert(0, "const "); - } - - context.is_const = next_type_is_const; - - Dwarf_Die ref = - get_referenced_die(fobj.dwarf_handle.get(), die, DW_AT_type, true); - if (ref) { - set_parameter_string(fobj, ref, context); - dwarf_dealloc(fobj.dwarf_handle.get(), ref, DW_DLA_DIE); - } - - if (!context.has_type && context.has_name) { - context.text.insert(0, "void "); - context.has_type = true; - } - } - - // Resolve the function return type and parameters - static void set_function_parameters(std::string &function_name, - std::vector &ns, - dwarf_fileobject &fobj, Dwarf_Die die) { - Dwarf_Debug dwarf = fobj.dwarf_handle.get(); - Dwarf_Error error = DW_DLE_NE; - Dwarf_Die current_die = 0; - std::string parameters; - bool has_spec = true; - // Check if we have a spec DIE. If we do we use it as it contains - // more information, like parameter names. - Dwarf_Die spec_die = get_spec_die(fobj, die); - if (!spec_die) { - has_spec = false; - spec_die = die; - } - - std::vector::const_iterator it = ns.begin(); - std::string ns_name; - for (it = ns.begin(); it < ns.end(); ++it) { - ns_name.append(*it).append("::"); - } - - if (!ns_name.empty()) { - function_name.insert(0, ns_name); - } - - // See if we have a function return type. It can be either on the - // current die or in its spec one (usually true for inlined functions) - std::string return_type = - get_referenced_die_name(dwarf, die, DW_AT_type, true); - if (return_type.empty()) { - return_type = get_referenced_die_name(dwarf, spec_die, DW_AT_type, true); - } - if (!return_type.empty()) { - return_type.append(" "); - function_name.insert(0, return_type); - } - - if (dwarf_child(spec_die, ¤t_die, &error) == DW_DLV_OK) { - for (;;) { - Dwarf_Die sibling_die = 0; - - Dwarf_Half tag_value; - dwarf_tag(current_die, &tag_value, &error); - - if (tag_value == DW_TAG_formal_parameter) { - // Ignore artificial (ie, compiler generated) parameters - bool is_artificial = false; - Dwarf_Attribute attr_mem; - if (dwarf_attr(current_die, DW_AT_artificial, &attr_mem, &error) == - DW_DLV_OK) { - Dwarf_Bool flag = 0; - if (dwarf_formflag(attr_mem, &flag, &error) == DW_DLV_OK) { - is_artificial = flag != 0; - } - dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR); - } - - if (!is_artificial) { - type_context_t context; - set_parameter_string(fobj, current_die, context); - - if (parameters.empty()) { - parameters.append("("); - } else { - parameters.append(", "); - } - parameters.append(context.text); - } - } - - int result = dwarf_siblingof(dwarf, current_die, &sibling_die, &error); - if (result == DW_DLV_ERROR) { - break; - } else if (result == DW_DLV_NO_ENTRY) { - break; - } - - if (current_die != die) { - dwarf_dealloc(dwarf, current_die, DW_DLA_DIE); - current_die = 0; - } - - current_die = sibling_die; - } - } - if (parameters.empty()) - parameters = "("; - parameters.append(")"); - - // If we got a spec DIE we need to deallocate it - if (has_spec) - dwarf_dealloc(dwarf, spec_die, DW_DLA_DIE); - - function_name.append(parameters); - } - - // defined here because in C++98, template function cannot take locally - // defined types... grrr. - struct inliners_search_cb { - void operator()(Dwarf_Die die, std::vector &ns) { - Dwarf_Error error = DW_DLE_NE; - Dwarf_Half tag_value; - Dwarf_Attribute attr_mem; - Dwarf_Debug dwarf = fobj.dwarf_handle.get(); - - dwarf_tag(die, &tag_value, &error); - - switch (tag_value) { - char *name; - case DW_TAG_subprogram: - if (!trace.source.function.empty()) - break; - if (dwarf_diename(die, &name, &error) == DW_DLV_OK) { - trace.source.function = std::string(name); - dwarf_dealloc(dwarf, name, DW_DLA_STRING); - } else { - // We don't have a function name in this DIE. - // Check if there is a referenced non-defining - // declaration. - trace.source.function = - get_referenced_die_name(dwarf, die, DW_AT_abstract_origin, true); - if (trace.source.function.empty()) { - trace.source.function = - get_referenced_die_name(dwarf, die, DW_AT_specification, true); - } - } - - // Append the function parameters, if available - set_function_parameters(trace.source.function, ns, fobj, die); - - // If the object function name is empty, it's possible that - // there is no dynamic symbol table (maybe the executable - // was stripped or not built with -rdynamic). See if we have - // a DWARF linkage name to use instead. We try both - // linkage_name and MIPS_linkage_name because the MIPS tag - // was the unofficial one until it was adopted in DWARF4. - // Old gcc versions generate MIPS_linkage_name - if (trace.object_function.empty()) { - details::demangler demangler; - - if (dwarf_attr(die, DW_AT_linkage_name, &attr_mem, &error) != - DW_DLV_OK) { - if (dwarf_attr(die, DW_AT_MIPS_linkage_name, &attr_mem, &error) != - DW_DLV_OK) { - break; - } - } - - char *linkage; - if (dwarf_formstring(attr_mem, &linkage, &error) == DW_DLV_OK) { - trace.object_function = demangler.demangle(linkage); - dwarf_dealloc(dwarf, linkage, DW_DLA_STRING); - } - dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR); - } - break; - - case DW_TAG_inlined_subroutine: - ResolvedTrace::SourceLoc sloc; - - if (dwarf_diename(die, &name, &error) == DW_DLV_OK) { - sloc.function = std::string(name); - dwarf_dealloc(dwarf, name, DW_DLA_STRING); - } else { - // We don't have a name for this inlined DIE, it could - // be that there is an abstract origin instead. - // Get the DW_AT_abstract_origin value, which is a - // reference to the source DIE and try to get its name - sloc.function = - get_referenced_die_name(dwarf, die, DW_AT_abstract_origin, true); - } - - set_function_parameters(sloc.function, ns, fobj, die); - - std::string file = die_call_file(dwarf, die, cu_die); - if (!file.empty()) - sloc.filename = file; - - Dwarf_Unsigned number = 0; - if (dwarf_attr(die, DW_AT_call_line, &attr_mem, &error) == DW_DLV_OK) { - if (dwarf_formudata(attr_mem, &number, &error) == DW_DLV_OK) { - sloc.line = number; - } - dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR); - } - - if (dwarf_attr(die, DW_AT_call_column, &attr_mem, &error) == - DW_DLV_OK) { - if (dwarf_formudata(attr_mem, &number, &error) == DW_DLV_OK) { - sloc.col = number; - } - dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR); - } - - trace.inliners.push_back(sloc); - break; - }; - } - ResolvedTrace &trace; - dwarf_fileobject &fobj; - Dwarf_Die cu_die; - inliners_search_cb(ResolvedTrace &t, dwarf_fileobject &f, Dwarf_Die c) - : trace(t), fobj(f), cu_die(c) {} - }; - - static Dwarf_Die find_fundie_by_pc(dwarf_fileobject &fobj, - Dwarf_Die parent_die, Dwarf_Addr pc, - Dwarf_Die result) { - Dwarf_Die current_die = 0; - Dwarf_Error error = DW_DLE_NE; - Dwarf_Debug dwarf = fobj.dwarf_handle.get(); - - if (dwarf_child(parent_die, ¤t_die, &error) != DW_DLV_OK) { - return NULL; - } - - for (;;) { - Dwarf_Die sibling_die = 0; - Dwarf_Half tag_value; - dwarf_tag(current_die, &tag_value, &error); - - switch (tag_value) { - case DW_TAG_subprogram: - case DW_TAG_inlined_subroutine: - if (die_has_pc(fobj, current_die, pc)) { - return current_die; - } - }; - bool declaration = false; - Dwarf_Attribute attr_mem; - if (dwarf_attr(current_die, DW_AT_declaration, &attr_mem, &error) == - DW_DLV_OK) { - Dwarf_Bool flag = 0; - if (dwarf_formflag(attr_mem, &flag, &error) == DW_DLV_OK) { - declaration = flag != 0; - } - dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR); - } - - if (!declaration) { - // let's be curious and look deeper in the tree, functions are - // not necessarily at the first level, but might be nested - // inside a namespace, structure, a function, an inlined - // function etc. - Dwarf_Die die_mem = 0; - Dwarf_Die indie = find_fundie_by_pc(fobj, current_die, pc, die_mem); - if (indie) { - result = die_mem; - return result; - } - } - - int res = dwarf_siblingof(dwarf, current_die, &sibling_die, &error); - if (res == DW_DLV_ERROR) { - return NULL; - } else if (res == DW_DLV_NO_ENTRY) { - break; - } - - if (current_die != parent_die) { - dwarf_dealloc(dwarf, current_die, DW_DLA_DIE); - current_die = 0; - } - - current_die = sibling_die; - } - return NULL; - } - - template - static bool deep_first_search_by_pc(dwarf_fileobject &fobj, - Dwarf_Die parent_die, Dwarf_Addr pc, - std::vector &ns, CB cb) { - Dwarf_Die current_die = 0; - Dwarf_Debug dwarf = fobj.dwarf_handle.get(); - Dwarf_Error error = DW_DLE_NE; - - if (dwarf_child(parent_die, ¤t_die, &error) != DW_DLV_OK) { - return false; - } - - bool branch_has_pc = false; - bool has_namespace = false; - for (;;) { - Dwarf_Die sibling_die = 0; - - Dwarf_Half tag; - if (dwarf_tag(current_die, &tag, &error) == DW_DLV_OK) { - if (tag == DW_TAG_namespace || tag == DW_TAG_class_type) { - char *ns_name = NULL; - if (dwarf_diename(current_die, &ns_name, &error) == DW_DLV_OK) { - if (ns_name) { - ns.push_back(std::string(ns_name)); - } else { - ns.push_back(""); - } - dwarf_dealloc(dwarf, ns_name, DW_DLA_STRING); - } else { - ns.push_back(""); - } - has_namespace = true; - } - } - - bool declaration = false; - Dwarf_Attribute attr_mem; - if (tag != DW_TAG_class_type && - dwarf_attr(current_die, DW_AT_declaration, &attr_mem, &error) == - DW_DLV_OK) { - Dwarf_Bool flag = 0; - if (dwarf_formflag(attr_mem, &flag, &error) == DW_DLV_OK) { - declaration = flag != 0; - } - dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR); - } - - if (!declaration) { - // let's be curious and look deeper in the tree, function are - // not necessarily at the first level, but might be nested - // inside a namespace, structure, a function, an inlined - // function etc. - branch_has_pc = deep_first_search_by_pc(fobj, current_die, pc, ns, cb); - } - - if (!branch_has_pc) { - branch_has_pc = die_has_pc(fobj, current_die, pc); - } - - if (branch_has_pc) { - cb(current_die, ns); - } - - int result = dwarf_siblingof(dwarf, current_die, &sibling_die, &error); - if (result == DW_DLV_ERROR) { - return false; - } else if (result == DW_DLV_NO_ENTRY) { - break; - } - - if (current_die != parent_die) { - dwarf_dealloc(dwarf, current_die, DW_DLA_DIE); - current_die = 0; - } - - if (has_namespace) { - has_namespace = false; - ns.pop_back(); - } - current_die = sibling_die; - } - - if (has_namespace) { - ns.pop_back(); - } - return branch_has_pc; - } - - static std::string die_call_file(Dwarf_Debug dwarf, Dwarf_Die die, - Dwarf_Die cu_die) { - Dwarf_Attribute attr_mem; - Dwarf_Error error = DW_DLE_NE; - Dwarf_Unsigned file_index; - - std::string file; - - if (dwarf_attr(die, DW_AT_call_file, &attr_mem, &error) == DW_DLV_OK) { - if (dwarf_formudata(attr_mem, &file_index, &error) != DW_DLV_OK) { - file_index = 0; - } - dwarf_dealloc(dwarf, attr_mem, DW_DLA_ATTR); - - if (file_index == 0) { - return file; - } - - char **srcfiles = 0; - Dwarf_Signed file_count = 0; - if (dwarf_srcfiles(cu_die, &srcfiles, &file_count, &error) == DW_DLV_OK) { - if (file_count > 0 && - file_index <= static_cast(file_count)) { - file = std::string(srcfiles[file_index - 1]); - } - - // Deallocate all strings! - for (int i = 0; i < file_count; ++i) { - dwarf_dealloc(dwarf, srcfiles[i], DW_DLA_STRING); - } - dwarf_dealloc(dwarf, srcfiles, DW_DLA_LIST); - } - } - return file; - } - - Dwarf_Die find_die(dwarf_fileobject &fobj, Dwarf_Addr addr) { - // Let's get to work! First see if we have a debug_aranges section so - // we can speed up the search - - Dwarf_Debug dwarf = fobj.dwarf_handle.get(); - Dwarf_Error error = DW_DLE_NE; - Dwarf_Arange *aranges; - Dwarf_Signed arange_count; - - Dwarf_Die returnDie; - bool found = false; - if (dwarf_get_aranges(dwarf, &aranges, &arange_count, &error) != - DW_DLV_OK) { - aranges = NULL; - } - - if (aranges) { - // We have aranges. Get the one where our address is. - Dwarf_Arange arange; - if (dwarf_get_arange(aranges, arange_count, addr, &arange, &error) == - DW_DLV_OK) { - - // We found our address. Get the compilation-unit DIE offset - // represented by the given address range. - Dwarf_Off cu_die_offset; - if (dwarf_get_cu_die_offset(arange, &cu_die_offset, &error) == - DW_DLV_OK) { - // Get the DIE at the offset returned by the aranges search. - // We set is_info to 1 to specify that the offset is from - // the .debug_info section (and not .debug_types) - int dwarf_result = - dwarf_offdie_b(dwarf, cu_die_offset, 1, &returnDie, &error); - - found = dwarf_result == DW_DLV_OK; - } - dwarf_dealloc(dwarf, arange, DW_DLA_ARANGE); - } - } - - if (found) - return returnDie; // The caller is responsible for freeing the die - - // The search for aranges failed. Try to find our address by scanning - // all compilation units. - Dwarf_Unsigned next_cu_header; - Dwarf_Half tag = 0; - returnDie = 0; - - while (!found && - dwarf_next_cu_header_d(dwarf, 1, 0, 0, 0, 0, 0, 0, 0, 0, - &next_cu_header, 0, &error) == DW_DLV_OK) { - - if (returnDie) - dwarf_dealloc(dwarf, returnDie, DW_DLA_DIE); - - if (dwarf_siblingof(dwarf, 0, &returnDie, &error) == DW_DLV_OK) { - if ((dwarf_tag(returnDie, &tag, &error) == DW_DLV_OK) && - tag == DW_TAG_compile_unit) { - if (die_has_pc(fobj, returnDie, addr)) { - found = true; - } - } - } - } - - if (found) { - while (dwarf_next_cu_header_d(dwarf, 1, 0, 0, 0, 0, 0, 0, 0, 0, - &next_cu_header, 0, &error) == DW_DLV_OK) { - // Reset the cu header state. Libdwarf's next_cu_header API - // keeps its own iterator per Dwarf_Debug that can't be reset. - // We need to keep fetching elements until the end. - } - } - - if (found) - return returnDie; - - // We couldn't find any compilation units with ranges or a high/low pc. - // Try again by looking at all DIEs in all compilation units. - Dwarf_Die cudie; - while (dwarf_next_cu_header_d(dwarf, 1, 0, 0, 0, 0, 0, 0, 0, 0, - &next_cu_header, 0, &error) == DW_DLV_OK) { - if (dwarf_siblingof(dwarf, 0, &cudie, &error) == DW_DLV_OK) { - Dwarf_Die die_mem = 0; - Dwarf_Die resultDie = find_fundie_by_pc(fobj, cudie, addr, die_mem); - - if (resultDie) { - found = true; - break; - } - } - } - - if (found) { - while (dwarf_next_cu_header_d(dwarf, 1, 0, 0, 0, 0, 0, 0, 0, 0, - &next_cu_header, 0, &error) == DW_DLV_OK) { - // Reset the cu header state. Libdwarf's next_cu_header API - // keeps its own iterator per Dwarf_Debug that can't be reset. - // We need to keep fetching elements until the end. - } - } - - if (found) - return cudie; - - // We failed. - return NULL; - } -}; -#endif // BACKWARD_HAS_DWARF == 1 - -template <> -class TraceResolverImpl - : public TraceResolverLinuxImpl {}; - -#endif // BACKWARD_SYSTEM_LINUX - -#ifdef BACKWARD_SYSTEM_DARWIN - -template class TraceResolverDarwinImpl; - -template <> -class TraceResolverDarwinImpl - : public TraceResolverImplBase { -public: - void load_addresses(void *const *addresses, int address_count) override { - if (address_count == 0) { - return; - } - _symbols.reset(backtrace_symbols(addresses, address_count)); - } - - ResolvedTrace resolve(ResolvedTrace trace) override { - // parse: - // + - char *filename = _symbols[trace.idx]; - - // skip " " - while (*filename && *filename != ' ') - filename++; - while (*filename == ' ') - filename++; - - // find start of from end ( may contain a space) - char *p = filename + strlen(filename) - 1; - // skip to start of " + " - while (p > filename && *p != ' ') - p--; - while (p > filename && *p == ' ') - p--; - while (p > filename && *p != ' ') - p--; - while (p > filename && *p == ' ') - p--; - char *funcname_end = p + 1; - - // skip to start of "" - while (p > filename && *p != ' ') - p--; - char *funcname = p + 1; - - // skip to start of " " - while (p > filename && *p == ' ') - p--; - while (p > filename && *p != ' ') - p--; - while (p > filename && *p == ' ') - p--; - - // skip "", handling the case where it contains a - char *filename_end = p + 1; - if (p == filename) { - // something went wrong, give up - filename_end = filename + strlen(filename); - funcname = filename_end; - } - trace.object_filename.assign( - filename, filename_end); // ok even if filename_end is the ending \0 - // (then we assign entire string) - - if (*funcname) { // if it's not end of string - *funcname_end = '\0'; - - trace.object_function = this->demangle(funcname); - trace.object_function += " "; - trace.object_function += (funcname_end + 1); - trace.source.function = trace.object_function; // we cannot do better. - } - return trace; - } - -private: - details::handle _symbols; -}; - -template <> -class TraceResolverImpl - : public TraceResolverDarwinImpl {}; - -#endif // BACKWARD_SYSTEM_DARWIN - -#ifdef BACKWARD_SYSTEM_WINDOWS - -// Load all symbol info -// Based on: -// https://stackoverflow.com/questions/6205981/windows-c-stack-trace-from-a-running-app/28276227#28276227 - -struct module_data { - std::string image_name; - std::string module_name; - void *base_address; - DWORD load_size; -}; - -class get_mod_info { - HANDLE process; - static const int buffer_length = 4096; - -public: - get_mod_info(HANDLE h) : process(h) {} - - module_data operator()(HMODULE module) { - module_data ret; - char temp[buffer_length]; - MODULEINFO mi; - - GetModuleInformation(process, module, &mi, sizeof(mi)); - ret.base_address = mi.lpBaseOfDll; - ret.load_size = mi.SizeOfImage; - - GetModuleFileNameExA(process, module, temp, sizeof(temp)); - ret.image_name = temp; - GetModuleBaseNameA(process, module, temp, sizeof(temp)); - ret.module_name = temp; - std::vector img(ret.image_name.begin(), ret.image_name.end()); - std::vector mod(ret.module_name.begin(), ret.module_name.end()); - SymLoadModule64(process, 0, &img[0], &mod[0], (DWORD64)ret.base_address, - ret.load_size); - return ret; - } -}; - -template <> -class TraceResolverImpl - : public TraceResolverImplBase { -public: - TraceResolverImpl() { - - HANDLE process = GetCurrentProcess(); - - std::vector modules; - DWORD cbNeeded; - std::vector module_handles(1); - SymInitialize(process, NULL, false); - DWORD symOptions = SymGetOptions(); - symOptions |= SYMOPT_LOAD_LINES | SYMOPT_UNDNAME; - SymSetOptions(symOptions); - EnumProcessModules( - process, &module_handles[0], - static_cast(module_handles.size() * sizeof(HMODULE)), &cbNeeded); - module_handles.resize(cbNeeded / sizeof(HMODULE)); - EnumProcessModules( - process, &module_handles[0], - static_cast(module_handles.size() * sizeof(HMODULE)), &cbNeeded); - std::transform(module_handles.begin(), module_handles.end(), - std::back_inserter(modules), get_mod_info(process)); - void *base = modules[0].base_address; - IMAGE_NT_HEADERS *h = ImageNtHeader(base); - image_type = h->FileHeader.Machine; - } - - static const int max_sym_len = 255; - struct symbol_t { - SYMBOL_INFO sym; - char buffer[max_sym_len]; - } sym; - - DWORD64 displacement; - - ResolvedTrace resolve(ResolvedTrace t) override { - HANDLE process = GetCurrentProcess(); - - char name[256]; - - memset(&sym, 0, sizeof(sym)); - sym.sym.SizeOfStruct = sizeof(SYMBOL_INFO); - sym.sym.MaxNameLen = max_sym_len; - - if (!SymFromAddr(process, (ULONG64)t.addr, &displacement, &sym.sym)) { - // TODO: error handling everywhere - char *lpMsgBuf; - DWORD dw = GetLastError(); - - if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (char *)&lpMsgBuf, 0, NULL)) { - std::fprintf(stderr, "%s\n", lpMsgBuf); - LocalFree(lpMsgBuf); - } - - // abort(); - } - UnDecorateSymbolName(sym.sym.Name, (PSTR)name, 256, UNDNAME_COMPLETE); - - DWORD offset = 0; - IMAGEHLP_LINE line; - if (SymGetLineFromAddr(process, (ULONG64)t.addr, &offset, &line)) { - t.object_filename = line.FileName; - t.source.filename = line.FileName; - t.source.line = line.LineNumber; - t.source.col = offset; - } - - t.source.function = name; - t.object_filename = ""; - t.object_function = name; - - return t; - } - - DWORD machine_type() const { return image_type; } - -private: - DWORD image_type; -}; - -#endif - -class TraceResolver : public TraceResolverImpl {}; - -/*************** CODE SNIPPET ***************/ - -class SourceFile { -public: - typedef std::vector> lines_t; - - SourceFile() {} - SourceFile(const std::string &path) { - // 1. If BACKWARD_CXX_SOURCE_PREFIXES is set then assume it contains - // a colon-separated list of path prefixes. Try prepending each - // to the given path until a valid file is found. - const std::vector &prefixes = get_paths_from_env_variable(); - for (size_t i = 0; i < prefixes.size(); ++i) { - // Double slashes (//) should not be a problem. - std::string new_path = prefixes[i] + '/' + path; - _file.reset(new std::ifstream(new_path.c_str())); - if (is_open()) - break; - } - // 2. If no valid file found then fallback to opening the path as-is. - if (!_file || !is_open()) { - _file.reset(new std::ifstream(path.c_str())); - } - } - bool is_open() const { return _file->is_open(); } - - lines_t &get_lines(unsigned line_start, unsigned line_count, lines_t &lines) { - using namespace std; - // This function make uses of the dumbest algo ever: - // 1) seek(0) - // 2) read lines one by one and discard until line_start - // 3) read line one by one until line_start + line_count - // - // If you are getting snippets many time from the same file, it is - // somewhat a waste of CPU, feel free to benchmark and propose a - // better solution ;) - - _file->clear(); - _file->seekg(0); - string line; - unsigned line_idx; - - for (line_idx = 1; line_idx < line_start; ++line_idx) { - std::getline(*_file, line); - if (!*_file) { - return lines; - } - } - - // think of it like a lambda in C++98 ;) - // but look, I will reuse it two times! - // What a good boy am I. - struct isspace { - bool operator()(char c) { return std::isspace(c); } - }; - - bool started = false; - for (; line_idx < line_start + line_count; ++line_idx) { - getline(*_file, line); - if (!*_file) { - return lines; - } - if (!started) { - if (std::find_if(line.begin(), line.end(), not_isspace()) == line.end()) - continue; - started = true; - } - lines.push_back(make_pair(line_idx, line)); - } - - lines.erase( - std::find_if(lines.rbegin(), lines.rend(), not_isempty()).base(), - lines.end()); - return lines; - } - - lines_t get_lines(unsigned line_start, unsigned line_count) { - lines_t lines; - return get_lines(line_start, line_count, lines); - } - - // there is no find_if_not in C++98, lets do something crappy to - // workaround. - struct not_isspace { - bool operator()(char c) { return !std::isspace(c); } - }; - // and define this one here because C++98 is not happy with local defined - // struct passed to template functions, fuuuu. - struct not_isempty { - bool operator()(const lines_t::value_type &p) { - return !(std::find_if(p.second.begin(), p.second.end(), not_isspace()) == - p.second.end()); - } - }; - - void swap(SourceFile &b) { _file.swap(b._file); } - -#ifdef BACKWARD_ATLEAST_CXX11 - SourceFile(SourceFile &&from) : _file(nullptr) { swap(from); } - SourceFile &operator=(SourceFile &&from) { - swap(from); - return *this; - } -#else - explicit SourceFile(const SourceFile &from) { - // some sort of poor man's move semantic. - swap(const_cast(from)); - } - SourceFile &operator=(const SourceFile &from) { - // some sort of poor man's move semantic. - swap(const_cast(from)); - return *this; - } -#endif - - // Allow adding to paths gotten from BACKWARD_CXX_SOURCE_PREFIXES after - // loading the library; this can be useful when the library is loaded when the - // locations are unknown Warning: Because this edits the static paths - // variable, it is *not* intrinsiclly thread safe - static void add_paths_to_env_variable_impl(const std::string &to_add) { - get_mutable_paths_from_env_variable().push_back(to_add); - } - -private: - details::handle> - _file; - - static std::vector get_paths_from_env_variable_impl() { - std::vector paths; - const char *prefixes_str = std::getenv("BACKWARD_CXX_SOURCE_PREFIXES"); - if (prefixes_str && prefixes_str[0]) { - paths = details::split_source_prefixes(prefixes_str); - } - return paths; - } - - static std::vector &get_mutable_paths_from_env_variable() { - static volatile std::vector paths = - get_paths_from_env_variable_impl(); - return const_cast &>(paths); - } - - static const std::vector &get_paths_from_env_variable() { - return get_mutable_paths_from_env_variable(); - } - -#ifdef BACKWARD_ATLEAST_CXX11 - SourceFile(const SourceFile &) = delete; - SourceFile &operator=(const SourceFile &) = delete; -#endif -}; - -class SnippetFactory { -public: - typedef SourceFile::lines_t lines_t; - - lines_t get_snippet(const std::string &filename, unsigned line_start, - unsigned context_size) { - - SourceFile &src_file = get_src_file(filename); - unsigned start = line_start - context_size / 2; - return src_file.get_lines(start, context_size); - } - - lines_t get_combined_snippet(const std::string &filename_a, unsigned line_a, - const std::string &filename_b, unsigned line_b, - unsigned context_size) { - SourceFile &src_file_a = get_src_file(filename_a); - SourceFile &src_file_b = get_src_file(filename_b); - - lines_t lines = - src_file_a.get_lines(line_a - context_size / 4, context_size / 2); - src_file_b.get_lines(line_b - context_size / 4, context_size / 2, lines); - return lines; - } - - lines_t get_coalesced_snippet(const std::string &filename, unsigned line_a, - unsigned line_b, unsigned context_size) { - SourceFile &src_file = get_src_file(filename); - - using std::max; - using std::min; - unsigned a = min(line_a, line_b); - unsigned b = max(line_a, line_b); - - if ((b - a) < (context_size / 3)) { - return src_file.get_lines((a + b - context_size + 1) / 2, context_size); - } - - lines_t lines = src_file.get_lines(a - context_size / 4, context_size / 2); - src_file.get_lines(b - context_size / 4, context_size / 2, lines); - return lines; - } - -private: - typedef details::hashtable::type src_files_t; - src_files_t _src_files; - - SourceFile &get_src_file(const std::string &filename) { - src_files_t::iterator it = _src_files.find(filename); - if (it != _src_files.end()) { - return it->second; - } - SourceFile &new_src_file = _src_files[filename]; - new_src_file = SourceFile(filename); - return new_src_file; - } -}; - -/*************** PRINTER ***************/ - -namespace ColorMode { -enum type { automatic, never, always }; -} - -class cfile_streambuf : public std::streambuf { -public: - cfile_streambuf(FILE *_sink) : sink(_sink) {} - int_type underflow() override { return traits_type::eof(); } - int_type overflow(int_type ch) override { - if (traits_type::not_eof(ch) && fputc(ch, sink) != EOF) { - return ch; - } - return traits_type::eof(); - } - - std::streamsize xsputn(const char_type *s, std::streamsize count) override { - return static_cast( - fwrite(s, sizeof *s, static_cast(count), sink)); - } - -#ifdef BACKWARD_ATLEAST_CXX11 -public: - cfile_streambuf(const cfile_streambuf &) = delete; - cfile_streambuf &operator=(const cfile_streambuf &) = delete; -#else -private: - cfile_streambuf(const cfile_streambuf &); - cfile_streambuf &operator=(const cfile_streambuf &); -#endif - -private: - FILE *sink; - std::vector buffer; -}; - -#ifdef BACKWARD_SYSTEM_LINUX - -namespace Color { -enum type { yellow = 33, purple = 35, reset = 39 }; -} // namespace Color - -class Colorize { -public: - Colorize(std::ostream &os) : _os(os), _reset(false), _enabled(false) {} - - void activate(ColorMode::type mode) { _enabled = mode == ColorMode::always; } - - void activate(ColorMode::type mode, FILE *fp) { activate(mode, fileno(fp)); } - - void set_color(Color::type ccode) { - if (!_enabled) - return; - - // I assume that the terminal can handle basic colors. Seriously I - // don't want to deal with all the termcap shit. - _os << "\033[" << static_cast(ccode) << "m"; - _reset = (ccode != Color::reset); - } - - ~Colorize() { - if (_reset) { - set_color(Color::reset); - } - } - -private: - void activate(ColorMode::type mode, int fd) { - activate(mode == ColorMode::automatic && isatty(fd) ? ColorMode::always - : mode); - } - - std::ostream &_os; - bool _reset; - bool _enabled; -}; - -#else // ndef BACKWARD_SYSTEM_LINUX - -namespace Color { -enum type { yellow = 0, purple = 0, reset = 0 }; -} // namespace Color - -class Colorize { -public: - Colorize(std::ostream &) {} - void activate(ColorMode::type) {} - void activate(ColorMode::type, FILE *) {} - void set_color(Color::type) {} -}; - -#endif // BACKWARD_SYSTEM_LINUX - -class Printer { -public: - bool snippet; - ColorMode::type color_mode; - bool address; - bool object; - int inliner_context_size; - int trace_context_size; - bool reverse; - - Printer() - : snippet(true), color_mode(ColorMode::automatic), address(false), - object(false), inliner_context_size(5), trace_context_size(7), - reverse(true) {} - - template FILE *print(ST &st, FILE *fp = stderr) { - cfile_streambuf obuf(fp); - std::ostream os(&obuf); - Colorize colorize(os); - colorize.activate(color_mode, fp); - print_stacktrace(st, os, colorize); - return fp; - } - - template std::ostream &print(ST &st, std::ostream &os) { - Colorize colorize(os); - colorize.activate(color_mode); - print_stacktrace(st, os, colorize); - return os; - } - - template - FILE *print(IT begin, IT end, FILE *fp = stderr, size_t thread_id = 0) { - cfile_streambuf obuf(fp); - std::ostream os(&obuf); - Colorize colorize(os); - colorize.activate(color_mode, fp); - print_stacktrace(begin, end, os, thread_id, colorize); - return fp; - } - - template - std::ostream &print(IT begin, IT end, std::ostream &os, - size_t thread_id = 0) { - Colorize colorize(os); - colorize.activate(color_mode); - print_stacktrace(begin, end, os, thread_id, colorize); - return os; - } - - TraceResolver const &resolver() const { return _resolver; } - -private: - TraceResolver _resolver; - SnippetFactory _snippets; - - template - void print_stacktrace(ST &st, std::ostream &os, Colorize &colorize) { - print_header(os, st.thread_id()); - _resolver.load_stacktrace(st); - if (reverse) { - for (size_t trace_idx = st.size(); trace_idx > 0; --trace_idx) { - print_trace(os, _resolver.resolve(st[trace_idx - 1]), colorize); - } - } else { - for (size_t trace_idx = 0; trace_idx < st.size(); ++trace_idx) { - print_trace(os, _resolver.resolve(st[trace_idx]), colorize); - } - } - } - - template - void print_stacktrace(IT begin, IT end, std::ostream &os, size_t thread_id, - Colorize &colorize) { - print_header(os, thread_id); - for (; begin != end; ++begin) { - print_trace(os, *begin, colorize); - } - } - - void print_header(std::ostream &os, size_t thread_id) { - os << "Stack trace (most recent call last)"; - if (thread_id) { - os << " in thread " << thread_id; - } - os << ":\n"; - } - - void print_trace(std::ostream &os, const ResolvedTrace &trace, - Colorize &colorize) { - os << "#" << std::left << std::setw(2) << trace.idx << std::right; - bool already_indented = true; - - if (!trace.source.filename.size() || object) { - os << " Object \"" << trace.object_filename << "\", at " << trace.addr - << ", in " << trace.object_function << "\n"; - already_indented = false; - } - - for (size_t inliner_idx = trace.inliners.size(); inliner_idx > 0; - --inliner_idx) { - if (!already_indented) { - os << " "; - } - const ResolvedTrace::SourceLoc &inliner_loc = - trace.inliners[inliner_idx - 1]; - print_source_loc(os, " | ", inliner_loc); - if (snippet) { - print_snippet(os, " | ", inliner_loc, colorize, Color::purple, - inliner_context_size); - } - already_indented = false; - } - - if (trace.source.filename.size()) { - if (!already_indented) { - os << " "; - } - print_source_loc(os, " ", trace.source, trace.addr); - if (snippet) { - print_snippet(os, " ", trace.source, colorize, Color::yellow, - trace_context_size); - } - } - } - - void print_snippet(std::ostream &os, const char *indent, - const ResolvedTrace::SourceLoc &source_loc, - Colorize &colorize, Color::type color_code, - int context_size) { - using namespace std; - typedef SnippetFactory::lines_t lines_t; - - lines_t lines = _snippets.get_snippet(source_loc.filename, source_loc.line, - static_cast(context_size)); - - for (lines_t::const_iterator it = lines.begin(); it != lines.end(); ++it) { - if (it->first == source_loc.line) { - colorize.set_color(color_code); - os << indent << ">"; - } else { - os << indent << " "; - } - os << std::setw(4) << it->first << ": " << it->second << "\n"; - if (it->first == source_loc.line) { - colorize.set_color(Color::reset); - } - } - } - - void print_source_loc(std::ostream &os, const char *indent, - const ResolvedTrace::SourceLoc &source_loc, - void *addr = nullptr) { - os << indent << "Source \"" << source_loc.filename << "\", line " - << source_loc.line << ", in " << source_loc.function; - - if (address && addr != nullptr) { - os << " [" << addr << "]"; - } - os << "\n"; - } -}; - -/*************** SIGNALS HANDLING ***************/ - -#if defined(BACKWARD_SYSTEM_LINUX) || defined(BACKWARD_SYSTEM_DARWIN) - -class SignalHandling { -public: - static std::vector make_default_signals() { - const int posix_signals[] = { - // Signals for which the default action is "Core". - SIGABRT, // Abort signal from abort(3) - SIGBUS, // Bus error (bad memory access) - SIGFPE, // Floating point exception - SIGILL, // Illegal Instruction - SIGIOT, // IOT trap. A synonym for SIGABRT - SIGQUIT, // Quit from keyboard - SIGSEGV, // Invalid memory reference - SIGSYS, // Bad argument to routine (SVr4) - SIGTRAP, // Trace/breakpoint trap - SIGXCPU, // CPU time limit exceeded (4.2BSD) - SIGXFSZ, // File size limit exceeded (4.2BSD) -#if defined(BACKWARD_SYSTEM_DARWIN) - SIGEMT, // emulation instruction executed -#endif - }; - return std::vector(posix_signals, - posix_signals + - sizeof posix_signals / sizeof posix_signals[0]); - } - - SignalHandling(const std::vector &posix_signals = make_default_signals()) - : _loaded(false) { - bool success = true; - - const size_t stack_size = 1024 * 1024 * 8; - _stack_content.reset(static_cast(malloc(stack_size))); - if (_stack_content) { - stack_t ss; - ss.ss_sp = _stack_content.get(); - ss.ss_size = stack_size; - ss.ss_flags = 0; - if (sigaltstack(&ss, nullptr) < 0) { - success = false; - } - } else { - success = false; - } - - for (size_t i = 0; i < posix_signals.size(); ++i) { - struct sigaction action; - memset(&action, 0, sizeof action); - action.sa_flags = - static_cast(SA_SIGINFO | SA_ONSTACK | SA_NODEFER | SA_RESETHAND); - sigfillset(&action.sa_mask); - sigdelset(&action.sa_mask, posix_signals[i]); -#if defined(__clang__) -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdisabled-macro-expansion" -#endif - action.sa_sigaction = &sig_handler; -#if defined(__clang__) -#pragma clang diagnostic pop -#endif - - int r = sigaction(posix_signals[i], &action, nullptr); - if (r < 0) - success = false; - } - - _loaded = success; - } - - bool loaded() const { return _loaded; } - - static void handleSignal(int, siginfo_t *info, void *_ctx) { - ucontext_t *uctx = static_cast(_ctx); - - StackTrace st; - void *error_addr = nullptr; -#ifdef REG_RIP // x86_64 - error_addr = reinterpret_cast(uctx->uc_mcontext.gregs[REG_RIP]); -#elif defined(REG_EIP) // x86_32 - error_addr = reinterpret_cast(uctx->uc_mcontext.gregs[REG_EIP]); -#elif defined(__arm__) - error_addr = reinterpret_cast(uctx->uc_mcontext.arm_pc); -#elif defined(__aarch64__) -#if defined(__APPLE__) - error_addr = reinterpret_cast(uctx->uc_mcontext->__ss.__pc); -#else - error_addr = reinterpret_cast(uctx->uc_mcontext.pc); -#endif -#elif defined(__mips__) - error_addr = reinterpret_cast( - reinterpret_cast(&uctx->uc_mcontext)->sc_pc); -#elif defined(__ppc__) || defined(__powerpc) || defined(__powerpc__) || \ - defined(__POWERPC__) - error_addr = reinterpret_cast(uctx->uc_mcontext.regs->nip); -#elif defined(__riscv) - error_addr = reinterpret_cast(uctx->uc_mcontext.__gregs[REG_PC]); -#elif defined(__s390x__) - error_addr = reinterpret_cast(uctx->uc_mcontext.psw.addr); -#elif defined(__APPLE__) && defined(__x86_64__) - error_addr = reinterpret_cast(uctx->uc_mcontext->__ss.__rip); -#elif defined(__APPLE__) - error_addr = reinterpret_cast(uctx->uc_mcontext->__ss.__eip); -#else -#warning ":/ sorry, ain't know no nothing none not of your architecture!" -#endif - if (error_addr) { - st.load_from(error_addr, 32, reinterpret_cast(uctx), - info->si_addr); - } else { - st.load_here(32, reinterpret_cast(uctx), info->si_addr); - } - - Printer printer; - printer.address = true; - printer.print(st, stderr); - -#if (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 700) || \ - (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809L) - psiginfo(info, nullptr); -#else - (void)info; -#endif - } - -private: - details::handle _stack_content; - bool _loaded; - -#ifdef __GNUC__ - __attribute__((noreturn)) -#endif - static void - sig_handler(int signo, siginfo_t *info, void *_ctx) { - handleSignal(signo, info, _ctx); - - // try to forward the signal. - raise(info->si_signo); - - // terminate the process immediately. - puts("watf? exit"); - _exit(EXIT_FAILURE); - } -}; - -#endif // BACKWARD_SYSTEM_LINUX || BACKWARD_SYSTEM_DARWIN - -#ifdef BACKWARD_SYSTEM_WINDOWS - -class SignalHandling { -public: - SignalHandling(const std::vector & = std::vector()) - : reporter_thread_([]() { - /* We handle crashes in a utility thread: - backward structures and some Windows functions called here - need stack space, which we do not have when we encounter a - stack overflow. - To support reporting stack traces during a stack overflow, - we create a utility thread at startup, which waits until a - crash happens or the program exits normally. */ - - { - std::unique_lock lk(mtx()); - cv().wait(lk, [] { return crashed() != crash_status::running; }); - } - if (crashed() == crash_status::crashed) { - handle_stacktrace(skip_recs()); - } - { - std::unique_lock lk(mtx()); - crashed() = crash_status::ending; - } - cv().notify_one(); - }) { - SetUnhandledExceptionFilter(crash_handler); - - signal(SIGABRT, signal_handler); - _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); - - std::set_terminate(&terminator); -#ifndef BACKWARD_ATLEAST_CXX17 - std::set_unexpected(&terminator); -#endif - _set_purecall_handler(&terminator); - _set_invalid_parameter_handler(&invalid_parameter_handler); - } - bool loaded() const { return true; } - - ~SignalHandling() { - { - std::unique_lock lk(mtx()); - crashed() = crash_status::normal_exit; - } - - cv().notify_one(); - - reporter_thread_.join(); - } - -private: - static CONTEXT *ctx() { - static CONTEXT data; - return &data; - } - - enum class crash_status { running, crashed, normal_exit, ending }; - - static crash_status &crashed() { - static crash_status data; - return data; - } - - static std::mutex &mtx() { - static std::mutex data; - return data; - } - - static std::condition_variable &cv() { - static std::condition_variable data; - return data; - } - - static HANDLE &thread_handle() { - static HANDLE handle; - return handle; - } - - std::thread reporter_thread_; - - // TODO: how not to hardcode these? - static const constexpr int signal_skip_recs = -#ifdef __clang__ - // With clang, RtlCaptureContext also captures the stack frame of the - // current function Below that, there are 3 internal Windows functions - 4 -#else - // With MSVC cl, RtlCaptureContext misses the stack frame of the current - // function The first entries during StackWalk are the 3 internal Windows - // functions - 3 -#endif - ; - - static int &skip_recs() { - static int data; - return data; - } - - static inline void terminator() { - crash_handler(signal_skip_recs); - abort(); - } - - static inline void signal_handler(int) { - crash_handler(signal_skip_recs); - abort(); - } - - static inline void __cdecl invalid_parameter_handler(const wchar_t *, - const wchar_t *, - const wchar_t *, - unsigned int, - uintptr_t) { - crash_handler(signal_skip_recs); - abort(); - } - - NOINLINE static LONG WINAPI crash_handler(EXCEPTION_POINTERS *info) { - // The exception info supplies a trace from exactly where the issue was, - // no need to skip records - crash_handler(0, info->ContextRecord); - return EXCEPTION_CONTINUE_SEARCH; - } - - NOINLINE static void crash_handler(int skip, CONTEXT *ct = nullptr) { - - if (ct == nullptr) { - RtlCaptureContext(ctx()); - } else { - memcpy(ctx(), ct, sizeof(CONTEXT)); - } - DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), - GetCurrentProcess(), &thread_handle(), 0, FALSE, - DUPLICATE_SAME_ACCESS); - - skip_recs() = skip; - - { - std::unique_lock lk(mtx()); - crashed() = crash_status::crashed; - } - - cv().notify_one(); - - { - std::unique_lock lk(mtx()); - cv().wait(lk, [] { return crashed() != crash_status::crashed; }); - } - } - - static void handle_stacktrace(int skip_frames = 0) { - // printer creates the TraceResolver, which can supply us a machine type - // for stack walking. Without this, StackTrace can only guess using some - // macros. - // StackTrace also requires that the PDBs are already loaded, which is done - // in the constructor of TraceResolver - Printer printer; - - StackTrace st; - st.set_machine_type(printer.resolver().machine_type()); - st.set_thread_handle(thread_handle()); - st.load_here(32 + skip_frames, ctx()); - st.skip_n_firsts(skip_frames); - - printer.address = true; - printer.print(st, std::cerr); - } -}; - -#endif // BACKWARD_SYSTEM_WINDOWS - -#ifdef BACKWARD_SYSTEM_UNKNOWN - -class SignalHandling { -public: - SignalHandling(const std::vector & = std::vector()) {} - bool init() { return false; } - bool loaded() { return false; } -}; - -#endif // BACKWARD_SYSTEM_UNKNOWN - -} // namespace backward - -#endif /* H_GUARD */ -#endif // defined(PROJECT_ENABLE_BACKWARD_CPP) diff --git a/support/include/utils/all.hpp b/support/include/utils/all.hpp index d0a9455f..ead01732 100644 --- a/support/include/utils/all.hpp +++ b/support/include/utils/all.hpp @@ -24,13 +24,19 @@ #include "utils/config.hpp" +#include "utils/base64.hpp" #include "utils/collection.hpp" #include "utils/com_init_wrapper.hpp" #include "utils/common.hpp" +#include "utils/encrypting_reader.hpp" +#include "utils/encryption.hpp" +#include "utils/error.hpp" +#include "utils/file.hpp" +#include "utils/hash.hpp" #include "utils/path.hpp" #include "utils/string.hpp" #include "utils/time.hpp" #include "utils/unix.hpp" #include "utils/windows.hpp" -#endif // REPERTORY_INCLUDE_UTILS_ALL_HPP_ \ No newline at end of file +#endif // REPERTORY_INCLUDE_UTILS_ALL_HPP_ diff --git a/support/include/utils/collection.hpp b/support/include/utils/collection.hpp index 61ea7e11..e03f3d86 100644 --- a/support/include/utils/collection.hpp +++ b/support/include/utils/collection.hpp @@ -24,6 +24,7 @@ #include "utils/config.hpp" +#include "utils/error.hpp" #include "utils/string.hpp" namespace repertory::utils::collection { @@ -69,24 +70,75 @@ inline auto includes(const col_t &collection, collection.end(); } -template -[[nodiscard]] inline auto -from_hex_string_t(std::basic_string_view str, - val_t &val) -> bool { +template +[[nodiscard]] inline auto from_hex_string_t(std::string_view str, + val_t &val) -> bool { static constexpr const auto base16{16}; - val.clear(); - if (not(str.length() % 2U)) { - for (std::size_t i = 0U; i < str.length(); i += 2U) { - val.emplace_back(static_cast( - std::strtol(string_t{str.substr(i, 2U)}.c_str(), nullptr, base16))); + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + val.clear(); + + std::string fmt_val{str}; + utils::string::trim(fmt_val); + if (fmt_val.empty()) { + return true; + } + + fmt_val = utils::string::to_lower(fmt_val); + if (utils::string::begins_with(fmt_val, "0x")) { + fmt_val = fmt_val.substr(2U); + } + + if (fmt_val.empty()) { + throw std::runtime_error("hex string is invalid|" + std::string{str}); + } + + if (fmt_val.length() % 2U) { + fmt_val = '0' + fmt_val; + } + + auto iter = std::find_if_not( + fmt_val.begin(), fmt_val.end(), [](auto cur_char) -> bool { + auto check = static_cast(cur_char); + return ((check >= 48U && check <= 57U) || + (check >= 97U && check <= 102U)); + }); + if (iter != fmt_val.end()) { + auto invalid_idx{std::distance(fmt_val.begin(), iter)}; + throw std::range_error( + "invalid character in hex string|" + std::to_string(invalid_idx) + + '|' + std::string(1U, str.at(invalid_idx)) + '|' + std::string{str}); + } + + val.resize(fmt_val.length() / 2U); + for (std::size_t idx = 0U; idx < fmt_val.length(); idx += 2U) { + val.at(idx / 2U) = static_cast( + std::strtoul(fmt_val.substr(idx, 2U).c_str(), nullptr, base16)); } return true; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); } + val.clear(); return false; } +template +inline auto from_hex_string(std::string_view str, val_t &val) -> bool { + return from_hex_string_t(str, val); +} + +template +inline auto from_hex_string(std::wstring_view str, val_t &val) -> bool { + return from_hex_string_t(utils::string::to_utf8(str), val); +} template inline auto remove_element(col_t &collection, @@ -96,16 +148,6 @@ inline auto remove_element(col_t &collection, return collection; } -template -inline auto from_hex_string(std::string_view str, val_t &val) -> bool { - return from_hex_string_t(str, val); -} - -template -inline auto from_hex_string(std::wstring_view str, val_t &val) -> bool { - return from_hex_string_t(str, val); -} - template inline auto to_hex_string(const col_t &collection) -> std::string { static_assert(sizeof(typename col_t::value_type) == 1U, diff --git a/support/include/utils/common.hpp b/support/include/utils/common.hpp index 952e426e..006ea367 100644 --- a/support/include/utils/common.hpp +++ b/support/include/utils/common.hpp @@ -33,6 +33,8 @@ struct result final { [[nodiscard]] operator bool() const { return ok; } }; +using retryable_action_t = std::function; + [[nodiscard]] inline constexpr auto calculate_read_size(std::uint64_t total_size, std::size_t read_size, std::uint64_t offset) -> std::size_t { @@ -60,22 +62,20 @@ template [[nodiscard]] inline constexpr auto divide_with_ceiling(result_t numerator, data_t denominator) -> result_t; -#if defined(PROJECT_ENABLE_LIBSODIUM) -template -[[nodiscard]] inline auto generate_random() -> data_t; - -template -[[nodiscard]] inline auto generate_random(std::size_t size) -> data_t; -#endif // defined(PROJECT_ENABLE_LIBSODIUM) - template [[nodiscard]] inline auto generate_random_between(data_t begin, data_t end) -> data_t; -#if defined(PROJECT_ENABLE_LIBSODIUM) [[nodiscard]] auto generate_random_string(std::size_t length) -> std::string; [[nodiscard]] auto generate_random_wstring(std::size_t length) -> std::wstring; + +#if defined(PROJECT_ENABLE_LIBSODIUM) +template +[[nodiscard]] inline auto generate_secure_random() -> data_t; + +template +[[nodiscard]] inline auto generate_secure_random(std::size_t size) -> data_t; #endif // defined(PROJECT_ENABLE_LIBSODIUM) [[nodiscard]] auto @@ -90,6 +90,11 @@ get_next_available_port(std::uint16_t first_port, std::uint16_t &available_port) -> bool; #endif // defined(PROJECT_ENABLE_BOOST) +[[nodiscard]] auto retry_action(retryable_action_t action, + std::size_t retry_count = 200U, + std::chrono::milliseconds retry_wait = + std::chrono::milliseconds(10)) -> bool; + template inline constexpr auto divide_with_ceiling(result_t numerator, data_t denominator) -> result_t { @@ -101,26 +106,6 @@ inline constexpr auto divide_with_ceiling(result_t numerator, : (numerator / denominator) + (numerator % denominator != 0); } -#if defined(PROJECT_ENABLE_LIBSODIUM) -template inline auto generate_random() -> data_t { - static_assert(!is_collection>::value, - "data_t is a vector or collection"); - data_t ret{}; - randombytes_buf(&ret, sizeof(ret)); - return ret; -} - -template -inline auto generate_random(std::size_t size) -> data_t { - static_assert(is_collection>::value, - "data_t is not a vector or collection"); - data_t ret; - ret.resize(size); - randombytes_buf(ret.data(), ret.size() * sizeof(typename data_t::value_type)); - return ret; -} -#endif // defined(PROJECT_ENABLE_LIBSODIUM) - template inline auto generate_random_between(data_t begin, data_t end) -> data_t { static_assert(std::is_integral_v>, @@ -129,10 +114,31 @@ inline auto generate_random_between(data_t begin, data_t end) -> data_t { throw std::range_error("end must be greater than begin"); } - static std::mt19937 gen(std::random_device{}()); + thread_local std::mt19937 gen( + static_cast(std::time(nullptr) ^ std::random_device{}())); std::uniform_int_distribution dis(begin, end); return dis(gen); } + +#if defined(PROJECT_ENABLE_LIBSODIUM) +template inline auto generate_secure_random() -> data_t { + static_assert(!is_collection>::value, + "data_t is a vector or collection"); + data_t ret{}; + randombytes_buf(&ret, sizeof(ret)); + return ret; +} + +template +inline auto generate_secure_random(std::size_t size) -> data_t { + static_assert(is_collection>::value, + "data_t is not a vector or collection"); + data_t ret; + ret.resize(size); + randombytes_buf(ret.data(), ret.size() * sizeof(typename data_t::value_type)); + return ret; +} +#endif // defined(PROJECT_ENABLE_LIBSODIUM) } // namespace repertory::utils #endif // REPERTORY_INCLUDE_UTILS_COMMON_HPP_ diff --git a/support/include/utils/config.hpp b/support/include/utils/config.hpp index 11136ead..8794abba 100644 --- a/support/include/utils/config.hpp +++ b/support/include/utils/config.hpp @@ -22,6 +22,8 @@ #ifndef REPERTORY_INCLUDE_UTILS_CONFIG_HPP_ #define REPERTORY_INCLUDE_UTILS_CONFIG_HPP_ +#define NOMINMAX + #if defined(_WIN32) #define WINVER 0x0602 #define _WIN32_WINNT WINVER @@ -163,6 +165,50 @@ extern "C" { #endif // defined(__cplusplus) #endif // defined(PROJECT_ENABLE_FZF) +#if defined(PROJECT_ENABLE_LIBDSM) +#if defined(__cplusplus) +extern "C" { +#endif // defined(__cplusplus) +#include "bdsm/bdsm.h" +#if defined(__cplusplus) +} + +struct netbios_ns_deleter final { + void operator()(netbios_ns *ns) { + if (ns != nullptr) { + netbios_ns_destroy(ns); + } + } +}; +using netbios_ns_t = std::unique_ptr; + +inline const auto smb_session_deleter = [](smb_session *session) { + if (session != nullptr) { + smb_session_destroy(session); + } +}; +using smb_session_t = std::shared_ptr; + +struct smb_stat_deleter final { + void operator()(smb_stat st) { + if (st != nullptr) { + smb_stat_destroy(st); + } + } +}; +using smb_stat_t = std::unique_ptr; + +struct smb_stat_list_deleter final { + void operator()(smb_file *list) { + if (list != nullptr) { + smb_stat_list_destroy(list); + } + } +}; +using smb_stat_list_t = std::unique_ptr; +#endif // defined(__cplusplus) +#endif // defined(PROJECT_ENABLE_LIBDSM) + #if defined(PROJECT_ENABLE_LIBEVENT) #include "event2/buffer.h" #include "event2/bufferevent.h" @@ -171,20 +217,68 @@ extern "C" { #include "event2/util.h" #endif // defined(PROJECT_ENABLE_LIBEVENT) +#if defined(PROJECT_ENABLE_LIBSODIUM) +#include "sodium.h" +#endif // defined(PROJECT_ENABLE_LIBSODIUM) + #if defined(PROJECT_ENABLE_SDL) #include "SDL.h" #include "SDL_gamecontroller.h" #include "SDL_joystick.h" #endif // defined(PROJECT_ENABLE_SDL) -#if defined(PROJECT_ENABLE_LIBSODIUM) -#include "sodium.h" -#endif // defined(PROJECT_ENABLE_LIBSODIUM) - #if defined(PROJECT_ENABLE_SQLITE) #include "sqlite3.h" #endif // defined(PROJECT_ENABLE_SQLITE) +#if defined(PROJECT_ENABLE_VLC) +#include + +#if defined(__cplusplus) +[[nodiscard]] inline auto get_libvlc_error_msg() -> std::string { + const auto *msg = libvlc_errmsg(); + return msg == nullptr ? "none" : msg; +} + +struct vlc_deleter final { + void operator()(libvlc_instance_t *inst) { + if (inst != nullptr) { + libvlc_release(inst); + } + } +}; +using vlc_t = std::unique_ptr; + +struct vlc_media_deleter final { + void operator()(libvlc_media_t *media) { + if (media != nullptr) { + libvlc_media_release(media); + } + } +}; +using vlc_media_t = std::unique_ptr; + +struct vlc_media_list_deleter final { + void operator()(libvlc_media_list_t *media_list) { + if (media_list != nullptr) { + libvlc_media_list_release(media_list); + } + } +}; +using vlc_media_list_t = + std::unique_ptr; + +struct vlc_string_deleter final { + void operator()(char *str) { + if (str != nullptr) { + libvlc_free(str); + } + } +}; +using vlc_string_t = std::unique_ptr; +#endif // defined(__cplusplus) +#endif // defined(PROJECT_ENABLE_VLC) + #if !defined(fstat64) #define fstat64 fstat #endif // !defined(fstat64) @@ -231,6 +325,14 @@ extern "C" { #include "boost/serialization/vector.hpp" #endif // defined(PROJECT_ENABLE_BOOST) +#if defined(PROJECT_ENABLE_CLI11) +#if defined(PROJECT_IS_MINGW) && !defined(PROJECT_IS_MINGW_UNIX) +#include "CLI/CLI.hpp" +#else // !defined(PROJECT_IS_MINGW) || defined(PROJECT_IS_MINGW_UNIX) +#include "CLI11.hpp" +#endif // defined(PROJECT_IS_MINGW) && !defined(PROJECT_IS_MINGW_UNIX) +#endif // defined(PROJECT_ENABLE_CLI11) + #if defined(PROJECT_ENABLE_CPP_HTTPLIB) #include "httplib.h" #endif // defined(PROJECT_ENABLE_JSON) @@ -300,9 +402,26 @@ namespace repertory { using data_buffer = std::vector; using mutex_lock = std::lock_guard; using recur_mutex_lock = std::lock_guard; +using stop_type = std::atomic_bool; using unique_mutex_lock = std::unique_lock; using unique_recur_mutex_lock = std::unique_lock; +#if defined(_WIN32) +#if defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES) +inline constexpr const auto max_path_length = std::size_t{32767U}; +#else // !defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES) +inline constexpr const auto max_path_length = std::size_t{MAX_PATH}; +#endif // defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES) + +using native_handle = HANDLE; +#else // !defined(_WIN32) +inline constexpr const auto max_path_length = std::size_t{PATH_MAX}; +using native_handle = int; +#if !defined(INVALID_HANDLE_VALUE) +#define INVALID_HANDLE_VALUE (-1) +#endif // !defined(INVALID_HANDLE_VALUE) +#endif // defined(_WIN32) + template struct overloaded : Ts... { using Ts::operator()...; }; @@ -329,6 +448,17 @@ struct file_deleter final { } }; using file_t = std::unique_ptr; + +#if defined(PROJECT_ENABLE_CURL) +struct http_range final { + std::uint64_t begin{}; + std::uint64_t end{}; +}; + +using http_headers = std::unordered_map; +using http_query_parameters = std::map; +using http_ranges = std::vector; +#endif // defined(PROJECT_ENABLE_CURL) } // namespace repertory #endif // defined(__cplusplus) diff --git a/repertory/librepertory/include/utils/encrypting_reader.hpp b/support/include/utils/encrypting_reader.hpp similarity index 88% rename from repertory/librepertory/include/utils/encrypting_reader.hpp rename to support/include/utils/encrypting_reader.hpp index b4d4bb6a..ead01442 100644 --- a/repertory/librepertory/include/utils/encrypting_reader.hpp +++ b/support/include/utils/encrypting_reader.hpp @@ -1,140 +1,145 @@ -/* - Copyright <2018-2024> - - 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. -*/ -#ifndef INCLUDE_UTILS_ENCRYPTING_READER_HPP_ -#define INCLUDE_UTILS_ENCRYPTING_READER_HPP_ - -#include "types/repertory.hpp" -#include "utils/encryption.hpp" -#include "utils/native_file.hpp" - -namespace repertory::utils::encryption { -class encrypting_reader final { -public: - encrypting_reader(std::string_view file_name, std::string_view source_path, - stop_type &stop_requested, std::string_view token, - std::optional relative_parent_path, - std::size_t error_return = 0U); - - encrypting_reader(std::string_view encrypted_file_path, - std::string_view source_path, stop_type &stop_requested, - std::string_view token, std::size_t error_return = 0U); - - encrypting_reader( - std::string_view encrypted_file_path, std::string_view source_path, - stop_type &stop_requested, std::string_view token, - std::vector> - iv_list, - std::size_t error_return = 0U); - - encrypting_reader(const encrypting_reader &reader); - encrypting_reader(encrypting_reader &&) = delete; - - auto operator=(const encrypting_reader &) -> encrypting_reader & = delete; - auto operator=(encrypting_reader &&) -> encrypting_reader & = delete; - - ~encrypting_reader(); - -public: - using iostream = std::basic_iostream>; - using streambuf = std::basic_streambuf>; - -private: - utils::encryption::hash_256_t key_; - stop_type &stop_requested_; - size_t error_return_; - std::unordered_map chunk_buffers_; - std::string encrypted_file_name_; - std::string encrypted_file_path_; - std::vector< - std::array> - iv_list_; - std::size_t last_data_chunk_{}; - std::size_t last_data_chunk_size_{}; - std::uint64_t read_offset_{}; - native_file_ptr source_file_; - std::uint64_t total_size_{}; - -private: - static const std::size_t header_size_; - static const std::size_t data_chunk_size_; - static const std::size_t encrypted_chunk_size_; - -private: - auto reader_function(char *buffer, size_t size, size_t nitems) -> size_t; - -public: - [[nodiscard]] static auto - calculate_decrypted_size(std::uint64_t total_size) -> std::uint64_t; - - [[nodiscard]] static auto - calculate_encrypted_size(std::string_view source_path) -> std::uint64_t; - - [[nodiscard]] auto create_iostream() const -> std::shared_ptr; - - [[nodiscard]] static constexpr auto - get_encrypted_chunk_size() -> std::size_t { - return encrypted_chunk_size_; - } - - [[nodiscard]] static constexpr auto get_data_chunk_size() -> std::size_t { - return data_chunk_size_; - } - - [[nodiscard]] auto get_encrypted_file_name() const -> std::string { - return encrypted_file_name_; - } - - [[nodiscard]] auto get_encrypted_file_path() const -> std::string { - return encrypted_file_path_; - } - - [[nodiscard]] auto get_error_return() const -> std::size_t { - return error_return_; - } - - [[nodiscard]] auto get_iv_list() - -> std::vector> { - return iv_list_; - } - - [[nodiscard]] auto get_stop_requested() const -> bool { - return stop_requested_; - } - - [[nodiscard]] auto get_total_size() const -> std::uint64_t { - return total_size_; - } - - [[nodiscard]] static auto reader_function(char *buffer, size_t size, - size_t nitems, - void *instream) -> size_t { - return reinterpret_cast(instream)->reader_function( - buffer, size, nitems); - } - - void set_read_position(std::uint64_t position) { read_offset_ = position; } -}; -} // namespace repertory::utils::encryption - -#endif // INCLUDE_UTILS_ENCRYPTING_READER_HPP_ +/* + Copyright <2018-2024> + + 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. +*/ +#ifndef REPERTORY_INCLUDE_UTILS_ENCRYPTING_READER_HPP_ +#define REPERTORY_INCLUDE_UTILS_ENCRYPTING_READER_HPP_ +#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) + +#include "utils/config.hpp" + +#include "utils/hash.hpp" +#include "utils/types/file/i_file.hpp" + +namespace repertory::utils::encryption { +class encrypting_reader final { +public: + encrypting_reader(std::string_view file_name, std::string_view source_path, + stop_type &stop_requested, std::string_view token, + std::optional relative_parent_path, + std::size_t error_return = 0U); + + encrypting_reader(std::string_view encrypted_file_path, + std::string_view source_path, stop_type &stop_requested, + std::string_view token, std::size_t error_return = 0U); + + encrypting_reader( + std::string_view encrypted_file_path, std::string_view source_path, + stop_type &stop_requested, std::string_view token, + std::vector> + iv_list, + std::size_t error_return = 0U); + + encrypting_reader(const encrypting_reader &reader); + encrypting_reader(encrypting_reader &&) = delete; + + auto operator=(const encrypting_reader &) -> encrypting_reader & = delete; + auto operator=(encrypting_reader &&) -> encrypting_reader & = delete; + + ~encrypting_reader() noexcept = default; + +public: + using iostream = std::basic_iostream>; + using streambuf = std::basic_streambuf>; + +private: + utils::encryption::hash_256_t key_; + stop_type &stop_requested_; + size_t error_return_; + std::unique_ptr source_file_; + +private: + std::unordered_map chunk_buffers_; + std::string encrypted_file_name_; + std::string encrypted_file_path_; + std::vector< + std::array> + iv_list_; + std::size_t last_data_chunk_{}; + std::size_t last_data_chunk_size_{}; + std::uint64_t read_offset_{}; + std::uint64_t total_size_{}; + +private: + static const std::size_t header_size_; + static const std::size_t data_chunk_size_; + static const std::size_t encrypted_chunk_size_; + +private: + auto reader_function(char *buffer, size_t size, size_t nitems) -> size_t; + +public: + [[nodiscard]] static auto + calculate_decrypted_size(std::uint64_t total_size) -> std::uint64_t; + + [[nodiscard]] static auto + calculate_encrypted_size(std::string_view source_path) -> std::uint64_t; + + [[nodiscard]] auto create_iostream() const -> std::shared_ptr; + + [[nodiscard]] static constexpr auto + get_encrypted_chunk_size() -> std::size_t { + return encrypted_chunk_size_; + } + + [[nodiscard]] static constexpr auto get_data_chunk_size() -> std::size_t { + return data_chunk_size_; + } + + [[nodiscard]] auto get_encrypted_file_name() const -> std::string { + return encrypted_file_name_; + } + + [[nodiscard]] auto get_encrypted_file_path() const -> std::string { + return encrypted_file_path_; + } + + [[nodiscard]] auto get_error_return() const -> std::size_t { + return error_return_; + } + + [[nodiscard]] auto get_iv_list() + -> std::vector> { + return iv_list_; + } + + [[nodiscard]] auto get_stop_requested() const -> bool { + return stop_requested_; + } + + [[nodiscard]] auto get_total_size() const -> std::uint64_t { + return total_size_; + } + + [[nodiscard]] static auto reader_function(char *buffer, size_t size, + size_t nitems, + void *instream) -> size_t { + return reinterpret_cast(instream)->reader_function( + buffer, size, nitems); + } + + void set_read_position(std::uint64_t position) { read_offset_ = position; } +}; +} // namespace repertory::utils::encryption + +#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) +#endif // REPERTORY_INCLUDE_UTILS_ENCRYPTING_READER_HPP_ diff --git a/support/include/utils/encryption.hpp b/support/include/utils/encryption.hpp index 28e55ca0..9834d305 100644 --- a/support/include/utils/encryption.hpp +++ b/support/include/utils/encryption.hpp @@ -33,38 +33,30 @@ inline constexpr const std::uint32_t encryption_header_size{ crypto_aead_xchacha20poly1305_IETF_ABYTES, }; -template -[[nodiscard]] inline auto default_create_hash(std::string_view) -> hash_t; - -template -[[nodiscard]] inline auto default_create_hash(std::wstring_view) -> hash_t; - template inline auto generate_key( std::string_view password, - std::optional< - std::function> - hasher = std::nullopt) -> hash_t; + std::function hasher = + default_create_hash()) -> hash_t; template inline auto generate_key( std::wstring_view password, - std::optional< - std::function> - hasher = std::nullopt) -> hash_t; + std::function hasher = + default_create_hash()) -> hash_t; #if defined(PROJECT_ENABLE_BOOST) -[[nodiscard]] auto decrypt_data(std::string_view password, - std::string_view data) -> data_buffer; +[[nodiscard]] auto decrypt_file_name(std::string_view encryption_token, + std::string &file_name) -> bool; -[[nodiscard]] auto encrypt_data(std::string_view password, - std::string_view data) -> data_buffer; +[[nodiscard]] auto decrypt_file_path(std::string_view encryption_token, + std::string &file_path) -> bool; -template +template [[nodiscard]] inline auto decrypt_data(const std::array &key, const unsigned char *buffer, std::size_t buffer_size, - result &res) -> bool { + result_t &res) -> bool { if (buffer_size > encryption_header_size) { const std::uint32_t size = boost::endian::native_to_big(static_cast(buffer_size)); @@ -80,43 +72,42 @@ template return false; } -template [[nodiscard]] inline auto decrypt_data(const std::array &key, - const buffer &buf, result &res) -> bool { - return decrypt_data( + const buffer_t &buf, + result_t &res) -> bool { + return decrypt_data( key, reinterpret_cast(buf.data()), buf.size(), res); } -template +template [[nodiscard]] inline auto decrypt_data( - std::string_view password, const buffer &buf, result &res, - std::optional< - std::function> - hasher = std::nullopt) -> bool { - return decrypt_data(generate_key(password, hasher), buf, res); + std::string_view password, const buffer_t &buf, result_t &res, + std::function hasher = + default_create_hash()) -> bool { + return decrypt_data(generate_key(password, hasher), buf, + res); } -template +template [[nodiscard]] inline auto decrypt_data( std::string_view password, const unsigned char *buffer, - std::size_t buffer_size, result &res, - - std::optional< - std::function> - hasher = std::nullopt) -> bool { - return decrypt_data(generate_key(password, hasher), buffer, - buffer_size, res); + std::size_t buffer_size, result_t &res, + std::function hasher = + default_create_hash()) -> bool { + return decrypt_data(generate_key(password, hasher), buffer, + buffer_size, res); } -template +template inline void encrypt_data(const std::array &iv, const std::array &key, const unsigned char *buffer, std::size_t buffer_size, - result &res) { + result_t &res) { std::array mac{}; const std::uint32_t size = boost::endian::native_to_big( @@ -137,115 +128,86 @@ encrypt_data(const std::array -inline void encrypt_data(const std::array &key, +template +inline void encrypt_data(const std::array &key, const unsigned char *buffer, std::size_t buffer_size, - result &res) { + result_t &res) { std::array iv{}; randombytes_buf(iv.data(), iv.size()); - encrypt_data(iv, key, buffer, buffer_size, res); + encrypt_data(iv, key, buffer, buffer_size, res); } -template +template inline void encrypt_data( std::string_view password, const unsigned char *buffer, - std::size_t buffer_size, result &res, - std::optional< - std::function> - hasher = std::nullopt) { - encrypt_data(generate_key(password, hasher), buffer, buffer_size, - res); + std::size_t buffer_size, result_t &res, + std::function hasher = + default_create_hash()) { + encrypt_data(generate_key(password, hasher), buffer, buffer_size, + res); } -template +template inline void encrypt_data( - std::string_view password, const buffer &buf, result &res, - std::optional< - std::function> - hasher = std::nullopt) { - encrypt_data(generate_key(password, hasher), - reinterpret_cast(buf.data()), - buf.size(), res); + std::string_view password, const buffer_t &buf, result_t &res, + std::function hasher = + default_create_hash()) { + encrypt_data(generate_key(password, hasher), + reinterpret_cast(buf.data()), + buf.size(), res); } -template -inline void encrypt_data(const std::array &key, const buffer &buf, - result &res) { - encrypt_data(key, reinterpret_cast(buf.data()), - buf.size(), res); +template +inline void encrypt_data(const std::array &key, + const buffer_t &buf, result_t &res) { + encrypt_data(key, + reinterpret_cast(buf.data()), + buf.size(), res); } -template +template inline void encrypt_data(const std::array &iv, - const std::array &key, const buffer &buf, result &res) { - encrypt_data(iv, key, - reinterpret_cast(buf.data()), - buf.size(), res); + const std::array &key, const buffer_t &buf, + result_t &res) { + encrypt_data(iv, key, + reinterpret_cast(buf.data()), + buf.size(), res); } + +#if defined(PROJECT_ENABLE_CURL) +using reader_func_t = + std::function; + +[[nodiscard]] auto +read_encrypted_range(const http_range &range, + const utils::encryption::hash_256_t &key, + reader_func_t reader_func, std::uint64_t total_size, + data_buffer &data) -> bool; +#endif // defined(PROJECT_ENABLE_CURL) #endif // defined(PROJECT_ENABLE_BOOST) -template <> -inline auto -default_create_hash(std::string_view data) -> hash_256_t { - return create_hash_sha256(data); -} - -template <> -inline auto -default_create_hash(std::wstring_view data) -> hash_256_t { - return create_hash_sha256(data); -} - -template <> -inline auto -default_create_hash(std::string_view data) -> hash_384_t { - return create_hash_blake2b_384(data); -} - -template <> -inline auto -default_create_hash(std::wstring_view data) -> hash_384_t { - return create_hash_blake2b_384(data); -} - -template <> -inline auto -default_create_hash(std::string_view data) -> hash_512_t { - return create_hash_sha512(data); -} - -template <> -inline auto -default_create_hash(std::wstring_view data) -> hash_512_t { - return create_hash_sha512(data); -} - template inline auto generate_key( std::string_view password, - std::optional< - std::function> - hasher) -> hash_t { - return hasher.has_value() ? (*hasher)(reinterpret_cast( - password.data()), - password.size()) - : default_create_hash(password); + std::function hasher) + -> hash_t { + return hasher(reinterpret_cast(password.data()), + password.size()); } template inline auto generate_key( std::wstring_view password, - std::optional< - std::function> - hasher) -> hash_t { - return hasher.has_value() - ? (*hasher)( - reinterpret_cast(password.data()), - password.size() * sizeof(std::wstring_view::value_type)) - : default_create_hash(password); + std::function hasher) + -> hash_t { + return hasher(reinterpret_cast(password.data()), + password.size() * sizeof(wchar_t)); } } // namespace repertory::utils::encryption diff --git a/support/include/utils/error.hpp b/support/include/utils/error.hpp index 11671fdf..bf3e9778 100644 --- a/support/include/utils/error.hpp +++ b/support/include/utils/error.hpp @@ -42,11 +42,33 @@ protected: i_exception_handler() = default; }; +struct iostream_exception_handler final : i_exception_handler { + void handle_exception(std::string_view function_name) const override { + std::cerr << function_name << "|exception|unknown" << std::endl; + } + + void handle_exception(std::string_view function_name, + const std::exception &ex) const override { + std::cerr << function_name << "|exception|" + << (ex.what() == nullptr ? "unknown" : ex.what()) << std::endl; + } +}; +inline const iostream_exception_handler default_exception_handler{}; + +extern std::atomic exception_handler; + +#if defined(PROJECT_ENABLE_TESTING) +[[nodiscard]] inline auto +get_exception_handler() -> const i_exception_handler * { + return exception_handler; +} +#endif // defined(PROJECT_ENABLE_TESTING) + void handle_exception(std::string_view function_name); void handle_exception(std::string_view function_name, const std::exception &ex); -void set_exception_handler(i_exception_handler *handler); +void set_exception_handler(const i_exception_handler *handler); } // namespace repertory::utils::error #endif // REPERTORY_INCLUDE_UTILS_ERROR_HPP_ diff --git a/support/include/utils/file.hpp b/support/include/utils/file.hpp index 3f5c3df2..370b0bd0 100644 --- a/support/include/utils/file.hpp +++ b/support/include/utils/file.hpp @@ -24,128 +24,833 @@ #include "utils/config.hpp" +#include "utils/path.hpp" +#include "utils/string.hpp" +#include "utils/types/file/i_directory.hpp" +#include "utils/types/file/i_file.hpp" +#include "utils/types/file/i_fs_item.hpp" + namespace repertory::utils::file { -class file final { +[[nodiscard]] auto change_to_process_directory() -> bool; + +// INFO: has test +[[nodiscard]] auto create_temp_name(std::string_view file_part) -> std::string; + +// INFO: has test +[[nodiscard]] auto +create_temp_name(std::wstring_view file_part) -> std::wstring; + +// INFO: has test +[[nodiscard]] inline auto +directory_exists_in_path(std::string_view path, + std::string_view sub_directory) -> bool; + +// INFO: has test +[[nodiscard]] inline auto +directory_exists_in_path(std::wstring_view path, + std::wstring_view sub_directory) -> bool; + +// INFO: has test +[[nodiscard]] inline auto +file_exists_in_path(std::string_view path, std::string_view file_name) -> bool; + +// INFO: has test +[[nodiscard]] inline auto +file_exists_in_path(std::wstring_view path, + std::wstring_view file_name) -> bool; + +// INFO: has test +[[nodiscard]] auto +get_free_drive_space(std::string_view path) -> std::optional; + +// INFO: has test +[[nodiscard]] auto +get_free_drive_space(std::wstring_view path) -> std::optional; + +// INFO: has test +[[nodiscard]] auto get_time(std::string_view path, + time_type type) -> std::optional; + +// INFO: has test +[[nodiscard]] auto get_time(std::wstring_view path, + time_type type) -> std::optional; + +// INFO: has test +[[nodiscard]] auto +get_times(std::string_view path) -> std::optional; + +// INFO: has test +[[nodiscard]] auto +get_times(std::wstring_view path) -> std::optional; + +// INFO: has test +[[nodiscard]] auto +get_total_drive_space(std::string_view path) -> std::optional; + +// INFO: has test +[[nodiscard]] auto +get_total_drive_space(std::wstring_view path) -> std::optional; + +#if defined(PROJECT_ENABLE_LIBDSM) +[[nodiscard]] auto +smb_create_and_validate_relative_path(std::string_view smb_path, + std::string_view rel_path) -> std::string; + +// INFO: has test +[[nodiscard]] auto +smb_create_relative_path(std::string_view smb_path) -> std::string; + +// INFO: has test +[[nodiscard]] auto +smb_create_search_path(std::string_view smb_path) -> std::string; + +// INFO: has test +[[nodiscard]] auto +smb_create_smb_path(std::string_view smb_path, + std::string_view rel_path) -> std::string; + +[[nodiscard]] auto +smb_get_parent_path(std::string_view smb_path) -> std::string; + +[[nodiscard]] auto smb_get_root_path(std::string_view smb_path) -> std::string; + +[[nodiscard]] auto smb_get_unc_path(std::string_view smb_path) -> std::string; + +[[nodiscard]] auto smb_get_uri_path(std::string_view smb_path) -> std::string; + +[[nodiscard]] auto smb_get_uri_path(std::string_view smb_path, + std::string_view user, + std::string_view password) -> std::string; + +// INFO: has test +[[nodiscard]] auto smb_parent_is_same(std::string_view smb_path1, + std::string_view smb_path2) -> bool; +#endif // defined(PROJECT_ENABLE_LIBDSM) + +class file final : public i_file { public: - [[nodiscard]] static auto open_file(std::filesystem::path path) -> file; + // [[nodiscard]] static auto + // attach_file(native_handle handle, + // bool read_only = false) -> fs_file_t; + + // INFO: has test + [[nodiscard]] static auto open_file(std::string_view path, + bool read_only = false) -> fs_file_t; + + [[nodiscard]] static auto open_file(std::wstring_view path, + bool read_only = false) -> fs_file_t { + return open_file(utils::string::to_utf8(path), read_only); + } + + // INFO: has test + [[nodiscard]] static auto + open_or_create_file(std::string_view path, + bool read_only = false) -> fs_file_t; [[nodiscard]] static auto - open_or_create_file(std::filesystem::path path) -> file; + open_or_create_file(std::wstring_view path, + bool read_only = false) -> fs_file_t { + return open_or_create_file(utils::string::to_utf8(path), read_only); + } -protected: - file(std::fstream stream, std::filesystem::path path) - : path_(std::move(path)), stream_(std::move(stream)) {} - file() = default; +public: + file() noexcept = default; + + file(std::string_view path) + : file_(nullptr), path_(utils::path::absolute(path)) {} + + file(std::wstring_view path) + : file_(nullptr), + path_(utils::path::absolute(utils::string::to_utf8(path))) {} + +private: + file(file_t file_ptr, std::string_view path, bool read_only) + : file_(std::move(file_ptr)), path_(path), read_only_(read_only) {} public: file(const file &) = delete; - file(file &&file_) noexcept = default; - ~file() { close(); } + file(file &&move_file) noexcept + : file_(std::move(move_file.file_)), + path_(std::move(move_file.path_)), + read_only_(move_file.read_only_) {} - auto operator=(const file &) noexcept -> file & = delete; - auto operator=(file &&file_) noexcept -> file & = default; + ~file() override { close(); } private: - std::error_code error_{}; - std::filesystem::path path_; - std::fstream stream_; + file_t file_; + std::string path_; + bool read_only_{false}; + +private: + std::atomic_uint32_t read_buffer_size{65536U}; + +private: + void open(); public: - void close(); + void close() override; - [[nodiscard]] auto get_error_code() const -> std::error_code { - return error_; + [[nodiscard]] auto copy_to(std::string_view new_path, + bool overwrite) const -> bool override; + + [[nodiscard]] auto exists() const -> bool override; + + void flush() const override; + + [[nodiscard]] auto get_handle() const -> native_handle override; + + [[nodiscard]] auto get_path() const -> std::string override { return path_; } + + [[nodiscard]] auto get_read_buffer_size() const -> std::uint32_t override { + return read_buffer_size; } - [[nodiscard]] auto get_path() const -> std::filesystem::path { return path_; } - - [[nodiscard]] auto move_to(std::filesystem::path new_path) -> bool; - - [[nodiscard]] auto read(data_buffer &data, std::uint64_t offset, - std::size_t *total_read = nullptr) -> bool { - return read_(reinterpret_cast(data.data()), data.size(), - offset, total_read); + [[nodiscard]] auto is_read_only() const -> bool override { + return read_only_; } - [[nodiscard]] auto remove() -> bool; + [[nodiscard]] auto is_symlink() const -> bool override; - [[nodiscard]] auto truncate() -> bool { return truncate(0U); } + [[nodiscard]] auto move_to(std::string_view new_path) -> bool override; - [[nodiscard]] auto truncate(std::size_t size) -> bool; + [[nodiscard]] auto read(unsigned char *data, std::size_t to_read, + std::uint64_t offset, + std::size_t *total_read = nullptr) -> bool override; - [[nodiscard]] auto write(const data_buffer &data, std::uint64_t offset, - std::size_t *total_written = nullptr) -> bool { - return write_(reinterpret_cast(data.data()), - data.size(), offset, total_written); + [[nodiscard]] auto remove() -> bool override; + + auto set_read_buffer_size(std::uint32_t size) -> std::uint32_t override { + read_buffer_size = size; + return read_buffer_size; } -#if defined(PROJECT_ENABLE_JSON) - [[nodiscard]] auto write_json(const nlohmann::json &data, - std::size_t *total_written = nullptr) -> bool { - auto str_data = data.dump(); - return write_(reinterpret_cast(str_data.c_str()), - str_data.size(), 0U, total_written); +#if defined(PROJECT_ENABLE_LIBSODIUM) + [[nodiscard]] auto sha256() -> std::optional; + +#endif // defined(PROJECT_ENABLE_LIBSODIUM) + + [[nodiscard]] auto size() const -> std::optional override; + + [[nodiscard]] auto truncate(std::size_t size) -> bool override; + + [[nodiscard]] auto + write(const unsigned char *data, std::size_t to_write, std::size_t offset, + std::size_t *total_written = nullptr) -> bool override; + +public: + auto operator=(const file &) noexcept -> file & = delete; + + auto operator=(file &&move_file) noexcept -> file & { + if (&move_file != this) { + file_ = std::move(move_file.file_); + path_ = std::move(move_file.path_); + read_only_ = move_file.read_only_; + } + + return *this; } -#endif // defined(PROJECT_ENABLE_JSON) - [[nodiscard]] operator bool() const { return stream_.is_open(); } - -private: - [[nodiscard]] auto read_(unsigned char *data, std::size_t to_read, - std::uint64_t offset, - std::size_t *total_read) -> bool; - - [[nodiscard]] auto write_(const unsigned char *data, std::size_t to_write, - std::size_t offset, - std::size_t *total_written) -> bool; + [[nodiscard]] operator bool() const override { return file_ != nullptr; } }; -[[nodiscard]] auto get_file_size(std::string_view path, - std::uint64_t &file_size) -> bool; +#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) +class enc_file final : public i_file { +public: + [[nodiscard]] static auto attach_file(fs_file_t file) -> fs_file_t; -[[nodiscard]] auto get_file_size(std::wstring_view path, - std::uint64_t &file_size) -> bool; +public: + enc_file() noexcept = default; -[[nodiscard]] auto is_directory(std::string_view path) -> bool; +protected: + enc_file(fs_file_t file); -[[nodiscard]] auto is_directory(std::wstring_view path) -> bool; +public: + enc_file(const enc_file &) = delete; -[[nodiscard]] auto is_file(std::string_view path) -> bool; + enc_file(enc_file &&move_file) noexcept : file_(std::move(move_file.file_)) {} -[[nodiscard]] auto is_file(std::wstring_view path) -> bool; + ~enc_file() override { close(); } + +private: + fs_file_t file_; + +public: + void close() override; + + [[nodiscard]] auto copy_to(std::string_view new_path, + bool overwrite) const -> bool override; + + [[nodiscard]] auto exists() const -> bool override { return file_->exists(); } + + void flush() const override; + + [[nodiscard]] auto get_handle() const -> native_handle override { + return file_->get_handle(); + } + + [[nodiscard]] auto get_path() const -> std::string override { + return file_->get_path(); + } + + [[nodiscard]] auto get_read_buffer_size() const -> std::uint32_t override { + return file_->get_read_buffer_size(); + } + + [[nodiscard]] auto + get_time(time_type type) const -> std::optional override { + return file_->get_time(type); + } + + [[nodiscard]] auto is_read_only() const -> bool override { + return file_->is_read_only(); + } + + [[nodiscard]] auto is_symlink() const -> bool override { + return file_->is_symlink(); + } + + [[nodiscard]] auto move_to(std::string_view new_path) -> bool override; + + [[nodiscard]] auto read(unsigned char *data, std::size_t to_read, + std::uint64_t offset, + std::size_t *total_read = nullptr) -> bool override; + + [[nodiscard]] auto remove() -> bool override; + + auto set_read_buffer_size(std::uint32_t size) -> std::uint32_t override { + return file_->set_read_buffer_size(size); + } + + [[nodiscard]] auto size() const -> std::optional override; + + [[nodiscard]] auto truncate(std::size_t size) -> bool override; + + [[nodiscard]] auto + write(const unsigned char *data, std::size_t to_write, std::size_t offset, + std::size_t *total_written = nullptr) -> bool override; + +public: + [[nodiscard]] operator bool() const override { + return static_cast(*file_); + } + + auto operator=(const file &) noexcept -> enc_file & = delete; + + auto operator=(enc_file &&move_file) noexcept -> enc_file & { + if (&move_file != this) { + file_ = std::move(move_file.file_); + } + + return *this; + } +}; +#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) + +class thread_file final : public i_file { +public: + // [[nodiscard]] static auto + // attach_file(native_handle handle, + // bool read_only = false) -> fs_file_t; + + [[nodiscard]] static auto attach_file(fs_file_t file) -> fs_file_t; + + [[nodiscard]] static auto open_file(std::string_view path, + bool read_only = false) -> fs_file_t; + + [[nodiscard]] static auto open_file(std::wstring_view path, + bool read_only = false) -> fs_file_t { + return open_file(utils::string::to_utf8(path), read_only); + } + + [[nodiscard]] static auto + open_or_create_file(std::string_view path, + bool read_only = false) -> fs_file_t; + + [[nodiscard]] static auto + open_or_create_file(std::wstring_view path, + bool read_only = false) -> fs_file_t { + return open_or_create_file(utils::string::to_utf8(path), read_only); + } + +public: + thread_file() noexcept = default; + + thread_file(std::string_view path) : file_(new file(path)) {} + + thread_file(std::wstring_view path) + : file_(new file(utils::string::to_utf8(path))) {} + +protected: + thread_file(fs_file_t file); + +public: + thread_file(const thread_file &) = delete; + + thread_file(thread_file &&move_file) noexcept + : file_(std::move(move_file.file_)) {} + + ~thread_file() override { close(); } + +private: + fs_file_t file_; + +public: + void close() override; + + [[nodiscard]] auto copy_to(std::string_view new_path, + bool overwrite) const -> bool override; + + [[nodiscard]] auto exists() const -> bool override { return file_->exists(); } + + void flush() const override; + + [[nodiscard]] auto get_handle() const -> native_handle override { + return file_->get_handle(); + } + + [[nodiscard]] auto get_path() const -> std::string override { + return file_->get_path(); + } + + [[nodiscard]] auto get_read_buffer_size() const -> std::uint32_t override { + return file_->get_read_buffer_size(); + } + + [[nodiscard]] auto + get_time(time_type type) const -> std::optional override { + return file_->get_time(type); + } + + [[nodiscard]] auto is_read_only() const -> bool override { + return file_->is_read_only(); + } + + [[nodiscard]] auto is_symlink() const -> bool override { + return file_->is_symlink(); + } + + [[nodiscard]] auto move_to(std::string_view new_path) -> bool override; + + [[nodiscard]] auto read(unsigned char *data, std::size_t to_read, + std::uint64_t offset, + std::size_t *total_read = nullptr) -> bool override; + + [[nodiscard]] auto remove() -> bool override; + + auto set_read_buffer_size(std::uint32_t size) -> std::uint32_t override { + return file_->set_read_buffer_size(size); + } + + [[nodiscard]] auto size() const -> std::optional override; + + [[nodiscard]] auto truncate(std::size_t size) -> bool override; + + [[nodiscard]] auto + write(const unsigned char *data, std::size_t to_write, std::size_t offset, + std::size_t *total_written = nullptr) -> bool override; + +public: + [[nodiscard]] operator bool() const override { + return static_cast(*file_); + } + + auto operator=(const file &) noexcept -> thread_file & = delete; + + auto operator=(thread_file &&move_file) noexcept -> thread_file & { + if (&move_file != this) { + file_ = std::move(move_file.file_); + } + + return *this; + } +}; + +class directory final : public i_directory { +public: + using directory_t = std::unique_ptr; + + directory() noexcept = default; + + directory(std::string_view path) : path_(utils::path::absolute(path)) {} + + directory(std::wstring_view path) + : path_(utils::path::absolute(utils::string::to_utf8(path))) {} + + directory(const directory &) noexcept = delete; + + directory(directory &&move_dir) noexcept = default; + + ~directory() override = default; + +private: + std::string path_; + +public: + [[nodiscard]] auto copy_to(std::string_view new_path, + bool overwrite) const -> bool override; + + [[nodiscard]] auto + count(bool recursive = false) const -> std::uint64_t override; + + [[nodiscard]] auto + create_directory(std::string_view path = "") const -> fs_directory_t override; + + [[nodiscard]] auto create_file(std::string_view file_name, + bool read_only) const -> fs_file_t override; + + [[nodiscard]] auto exists() const -> bool override; + + [[nodiscard]] auto + get_directory(std::string_view path) const -> fs_directory_t override; + + [[nodiscard]] auto + get_directories() const -> std::vector override; + + [[nodiscard]] auto + get_file(std::string_view path) const -> fs_file_t override; + + [[nodiscard]] auto get_files() const -> std::vector override; + + [[nodiscard]] auto get_items() const -> std::vector override; + + [[nodiscard]] auto get_path() const -> std::string override { return path_; } + + [[nodiscard]] auto is_symlink() const -> bool override; + + [[nodiscard]] auto move_to(std::string_view new_path) -> bool override; + + [[nodiscard]] auto remove() -> bool override; + + [[nodiscard]] auto remove_recursively() -> bool override; + + [[nodiscard]] auto + size(bool recursive = false) const -> std::uint64_t override; + +public: + auto operator=(const directory &) noexcept -> directory & = delete; + + auto operator=(directory &&move_dir) noexcept -> directory & = default; + + [[nodiscard]] operator bool() const override { return exists(); } +}; + +#if defined(PROJECT_ENABLE_LIBDSM) +#define SMB_MOD_RW2 \ + (SMB_MOD_READ | SMB_MOD_WRITE | SMB_MOD_READ_EXT | SMB_MOD_WRITE_EXT | \ + SMB_MOD_READ_ATTR | SMB_MOD_WRITE_ATTR | SMB_MOD_READ_CTL) + +class smb_file final : public i_file { +public: + smb_file() = default; + + smb_file(std::optional fd, std::string path, smb_session_t session, + std::string_view share_name, smb_tid tid) + : fd_(std::move(fd)), + path_(std::move(path)), + session_(std::move(session)), + share_name_(share_name), + tid_(tid) {} + + smb_file(const smb_file &) = delete; + + smb_file(smb_file &&f) noexcept + : fd_(std::move(f.fd_)), + path_(std::move(f.path_)), + read_buffer_size(f.get_read_buffer_size()), + read_only_(f.read_only_), + session_(std::move(f.session_)), + share_name_(std::move(f.share_name_)), + tid_(f.tid_) {} + + ~smb_file() override { close(); } + +private: + std::optional fd_; + std::string path_; + std::atomic_uint32_t read_buffer_size{65536U}; + bool read_only_; + smb_session_t session_; + std::string share_name_; + smb_tid tid_; + +public: + void close() override; + + [[nodiscard]] auto copy_to(std::string_view new_path, + bool overwrite) const -> bool override; + + [[nodiscard]] auto exists() const -> bool override; + + void flush() const override; + + [[nodiscard]] auto get_handle() const -> native_handle override { + return INVALID_HANDLE_VALUE; + } + + [[nodiscard]] auto get_path() const -> std::string override { return path_; } + + [[nodiscard]] auto get_read_buffer_size() const -> std::uint32_t override { + return read_buffer_size; + } + + [[nodiscard]] static auto + get_time(smb_session *session, smb_tid tid, std::string path, + time_type type) -> std::optional; + + [[nodiscard]] auto + get_time(time_type type) const -> std::optional override { + return get_time(session_.get(), tid_, path_, type); + } + + [[nodiscard]] auto get_unc_path() const -> std::string { + return smb_get_unc_path(path_); + } + + [[nodiscard]] auto get_uri_path() const -> std::string { + return smb_get_uri_path(path_); + } + + [[nodiscard]] auto + get_uri_path(std::string_view user, + std::string_view password) const -> std::string { + return smb_get_uri_path(path_, user, password); + } + + [[nodiscard]] auto is_read_only() const -> bool override { + return read_only_; + } + + [[nodiscard]] auto is_symlink() const -> bool override; + [[nodiscard]] auto move_to(std::string_view new_path) -> bool override; + + [[nodiscard]] auto open(bool read_only) -> bool; + + [[nodiscard]] auto read(unsigned char *data, std::size_t to_read, + std::uint64_t offset, + std::size_t *total_read = nullptr) -> bool override; + + [[nodiscard]] auto remove() -> bool override; + + auto set_read_buffer_size(std::uint32_t size) -> std::uint32_t override { + read_buffer_size = size; + return read_buffer_size; + } + + [[nodiscard]] auto size() const -> std::optional override; + + [[nodiscard]] auto truncate(std::size_t size) -> bool override; + + [[nodiscard]] auto + write(const unsigned char *data, std::size_t to_write, std::size_t offset, + std::size_t *total_written = nullptr) -> bool override; + +public: + auto operator=(const smb_file &) noexcept -> smb_file & = delete; + + auto operator=(smb_file &&move_file) noexcept -> smb_file & { + if (this != &move_file) { + fd_ = std::move(move_file.fd_); + path_ = std::move(move_file.path_); + read_buffer_size = move_file.get_read_buffer_size(); + read_only_ = move_file.read_only_; + session_ = std::move(move_file.session_); + share_name_ = std::move(move_file.share_name_); + tid_ = move_file.tid_; + } + + return *this; + } + + [[nodiscard]] operator bool() const override { return fd_.has_value(); } +}; + +class smb_directory final : public i_directory { +public: + using smb_directory_t = std::unique_ptr; + + [[nodiscard]] static auto + open(std::string_view host, std::string_view user, std::string_view password, + std::string_view share_name) -> smb_directory_t; + + [[nodiscard]] static auto + open(std::wstring_view host, std::wstring_view user, + std::wstring_view password, + std::wstring_view share_name) -> smb_directory_t; + +public: + smb_directory() noexcept = default; + + smb_directory(const smb_directory &) noexcept = delete; + + smb_directory(smb_directory &&) noexcept = default; + + ~smb_directory() override = default; + +private: + smb_directory(std::string path, smb_session_t session, + std::string_view share_name, smb_tid tid) + : path_(std::move(path)), + session_(std::move(session)), + share_name_(share_name), + tid_(tid) {} + +private: + std::string path_{}; + smb_session_t session_{}; + std::string share_name_{}; + smb_tid tid_{}; + +public: + [[nodiscard]] auto + count(bool recursive = false) const -> std::uint64_t override; + + [[nodiscard]] auto copy_to(std::string_view new_path, + bool overwrite) const -> bool override; + + [[nodiscard]] auto + create_directory(std::string_view path = "") const -> fs_directory_t override; + + [[nodiscard]] auto create_file(std::string_view file_name, + bool read_only) const -> fs_file_t override; + + [[nodiscard]] auto exists() const -> bool override; + + [[nodiscard]] auto + get_directory(std::string_view path) const -> fs_directory_t override; + + [[nodiscard]] auto + get_directories() const -> std::vector override; + + [[nodiscard]] auto + get_file(std::string_view path) const -> fs_file_t override; + + [[nodiscard]] auto get_files() const -> std::vector override; + + [[nodiscard]] auto get_items() const -> std::vector override; + + [[nodiscard]] auto get_path() const -> std::string override { return path_; } + + [[nodiscard]] auto + get_time(time_type type) const -> std::optional override { + return smb_file::get_time(session_.get(), tid_, path_, type); + } + + [[nodiscard]] auto get_unc_path() const -> std::string { + return smb_get_unc_path(path_); + } + + [[nodiscard]] auto get_uri_path() const -> std::string { + return smb_get_uri_path(path_); + } + + [[nodiscard]] auto + get_uri_path(std::string_view user, + std::string_view password) const -> std::string { + return smb_get_uri_path(path_, user, password); + } + + [[nodiscard]] auto is_symlink() const -> bool override; + + [[nodiscard]] auto move_to(std::string_view new_path) -> bool override; + + [[nodiscard]] auto remove() -> bool override; + + [[nodiscard]] auto remove_recursively() -> bool override; + + [[nodiscard]] auto + size(bool recursive = false) const -> std::uint64_t override; + +public: + auto operator=(const smb_directory &) noexcept -> smb_directory & = delete; + + auto + operator=(smb_directory &&move_dir) noexcept -> smb_directory & = default; + + [[nodiscard]] operator bool() const override { return session_ != nullptr; } +}; +#endif // defined(PROJECT_ENABLE_LIBDSM) #if defined(PROJECT_ENABLE_JSON) #if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) +// INFO: has test [[nodiscard]] auto read_json_file(std::string_view path, nlohmann::json &data, std::optional password = std::nullopt) -> bool; +// INFO: has test [[nodiscard]] auto read_json_file( std::wstring_view path, nlohmann::json &data, std::optional password = std::nullopt) -> bool; +// INFO: has test [[nodiscard]] auto write_json_file( std::string_view path, const nlohmann::json &data, std::optional password = std::nullopt) -> bool; +// INFO: has test [[nodiscard]] auto write_json_file( std::wstring_view path, const nlohmann::json &data, std::optional password = std::nullopt) -> bool; #else // !defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) +// INFO: has test [[nodiscard]] auto read_json_file(std::string_view path, nlohmann::json &data) -> bool; +// INFO: has test [[nodiscard]] auto read_json_file(std::wstring_view path, nlohmann::json &data) -> bool; +// INFO: has test [[nodiscard]] auto write_json_file(std::string_view path, const nlohmann::json &data) -> bool; +// INFO: has test [[nodiscard]] auto write_json_file(std::wstring_view path, const nlohmann::json &data) -> bool; #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) #endif // defined(PROJECT_ENABLE_JSON) + +// INFO: has test +template +[[nodiscard]] inline auto directory_exists_in_path_t( + std::basic_string_view path, + std::basic_string_view sub_directory) + -> bool { + return directory(utils::path::combine(path, {sub_directory})).exists(); +} + +// INFO: has test +template +[[nodiscard]] inline auto file_exists_in_path_t( + std::basic_string_view path, + std::basic_string_view file_name) -> bool { + return file(utils::path::combine(path, {file_name})).exists(); +} + +// INFO: has test +inline auto directory_exists_in_path(std::string_view path, + std::string_view sub_directory) -> bool { + return directory_exists_in_path_t(path, sub_directory); +} + +// INFO: has test +inline auto directory_exists_in_path(std::wstring_view path, + std::wstring_view sub_directory) -> bool { + return directory_exists_in_path_t(path, sub_directory); +} + +// INFO: has test +inline auto file_exists_in_path(std::string_view path, + std::string_view file_name) -> bool { + return file_exists_in_path_t(path, file_name); +} + +// INFO: has test +inline auto file_exists_in_path(std::wstring_view path, + std::wstring_view file_name) -> bool { + return file_exists_in_path_t(path, file_name); +} } // namespace repertory::utils::file #endif // REPERTORY_INCLUDE_UTILS_FILE_HPP_ diff --git a/support/include/utils/hash.hpp b/support/include/utils/hash.hpp index 9c74f216..a79c9023 100644 --- a/support/include/utils/hash.hpp +++ b/support/include/utils/hash.hpp @@ -35,219 +35,132 @@ using hash_512_t = std::array; [[nodiscard]] auto create_hash_blake2b_256(std::wstring_view data) -> hash_256_t; +[[nodiscard]] auto +create_hash_blake2b_256(const data_buffer &data) -> hash_256_t; + [[nodiscard]] auto create_hash_blake2b_384(std::string_view data) -> hash_384_t; [[nodiscard]] auto create_hash_blake2b_384(std::wstring_view data) -> hash_384_t; -[[nodiscard]] auto create_hash_blake2b_512(std::string_view data) -> hash_512_t; +[[nodiscard]] auto +create_hash_blake2b_384(const data_buffer &data) -> hash_384_t; [[nodiscard]] auto create_hash_blake2b_512(std::wstring_view data) -> hash_512_t; -template [[nodiscard]] auto -create_hash_blake2b_t(std::basic_string_view data) -> hash_t; +create_hash_blake2b_512(const data_buffer &data) -> hash_512_t; + +[[nodiscard]] auto create_hash_blake2b_512(std::string_view data) -> hash_512_t; template -[[nodiscard]] auto create_hash_blake2b_t(const data_buffer &data) -> hash_t; - -template -[[nodiscard]] auto create_hash_blake2b_t( - const std::vector> &data) -> hash_t; - -template -[[nodiscard]] auto -create_hash_blake2b_t(const std::vector> &data) - -> std::array; +[[nodiscard]] auto create_hash_blake2b_t(const unsigned char *data, + std::size_t data_size) -> hash_t; [[nodiscard]] auto create_hash_sha256(std::string_view data) -> hash_256_t; [[nodiscard]] auto create_hash_sha256(std::wstring_view data) -> hash_256_t; +[[nodiscard]] auto create_hash_sha256(const data_buffer &data) -> hash_256_t; + +[[nodiscard]] auto create_hash_sha256(const unsigned char *data, + std::size_t data_size) -> hash_256_t; + [[nodiscard]] auto create_hash_sha512(std::string_view data) -> hash_512_t; [[nodiscard]] auto create_hash_sha512(std::wstring_view data) -> hash_512_t; -template -[[nodiscard]] auto -create_hash_sha256_t(std::basic_string_view data) -> hash_256_t; +[[nodiscard]] auto create_hash_sha512(const data_buffer &data) -> hash_512_t; -template -[[nodiscard]] auto create_hash_sha512_t(std::basic_string_view data) - -> repertory::utils::encryption::hash_512_t; +[[nodiscard]] auto create_hash_sha512(const unsigned char *data, + std::size_t data_size) -> hash_512_t; template -auto create_hash_blake2b_t(const data_buffer &data) -> hash_t { +[[nodiscard]] inline auto default_create_hash() -> const + std::function &; + +template +auto create_hash_blake2b_t(const unsigned char *data, + std::size_t data_size) -> hash_t { hash_t hash{}; crypto_generichash_blake2b_state state{}; auto res = crypto_generichash_blake2b_init(&state, nullptr, 0U, hash.size()); if (res != 0) { - throw std::runtime_error("failed to initialize blake2b|" + + throw std::runtime_error("failed to initialize blake2b-" + + std::to_string(hash.size() * 8U) + "|" + std::to_string(res)); } - res = crypto_generichash_blake2b_update( - &state, reinterpret_cast(data.data()), - data.size() * sizeof(data_buffer::value_type)); + res = crypto_generichash_blake2b_update(&state, data, data_size); if (res != 0) { - throw std::runtime_error("failed to update blake2b|" + std::to_string(res)); + throw std::runtime_error("failed to update blake2b-" + + std::to_string(hash.size() * 8U) + "|" + + std::to_string(res)); } res = crypto_generichash_blake2b_final(&state, hash.data(), hash.size()); if (res != 0) { - throw std::runtime_error("failed to finalize blake2b|" + + throw std::runtime_error("failed to finalize blake2b-" + + std::to_string(hash.size() * 8U) + "|" + std::to_string(res)); } return hash; } -template -auto create_hash_blake2b_t(const std::vector> &data) - -> std::array { - using hash_t = std::array; - hash_t hash{}; +inline const std::function + blake2b_256_hasher = + [](const unsigned char *data, std::size_t data_size) -> hash_256_t { + return create_hash_blake2b_t(data, data_size); +}; - crypto_generichash_blake2b_state state{}; - auto res = crypto_generichash_blake2b_init(&state, nullptr, 0U, hash.size()); - if (res != 0) { - throw std::runtime_error("failed to initialize blake2b|" + - std::to_string(res)); - } +inline const std::function + blake2b_384_hasher = + [](const unsigned char *data, std::size_t data_size) -> hash_384_t { + return create_hash_blake2b_t(data, data_size); +}; - for (const auto &item : data) { - res = crypto_generichash_blake2b_update( - &state, reinterpret_cast(item.data()), - item.size()); - if (res != 0) { - throw std::runtime_error("failed to update blake2b|" + - std::to_string(res)); - } - } +inline const std::function + blake2b_512_hasher = + [](const unsigned char *data, std::size_t data_size) -> hash_512_t { + return create_hash_blake2b_t(data, data_size); +}; - res = crypto_generichash_blake2b_final(&state, hash.data(), hash.size()); - if (res != 0) { - throw std::runtime_error("failed to finalize blake2b|" + - std::to_string(res)); - } +inline const std::function + sha256_hasher = + [](const unsigned char *data, std::size_t data_size) -> hash_256_t { + return create_hash_sha256(data, data_size); +}; - return hash; +inline const std::function + sha512_hasher = + [](const unsigned char *data, std::size_t data_size) -> hash_512_t { + return create_hash_sha512(data, data_size); +}; + +template <> +[[nodiscard]] inline auto default_create_hash() -> const + std::function & { + return blake2b_256_hasher; } -template -auto create_hash_blake2b_t(const std::vector> &data) - -> hash_t { - hash_t hash{}; - - crypto_generichash_blake2b_state state{}; - auto res = crypto_generichash_blake2b_init(&state, nullptr, 0U, hash.size()); - if (res != 0) { - throw std::runtime_error("failed to initialize blake2b|" + - std::to_string(res)); - } - - for (const auto &item : data) { - res = crypto_generichash_blake2b_update( - &state, reinterpret_cast(item.data()), - item.size() * sizeof(char_t)); - if (res != 0) { - throw std::runtime_error("failed to update blake2b|" + - std::to_string(res)); - } - } - - res = crypto_generichash_blake2b_final(&state, hash.data(), hash.size()); - if (res != 0) { - throw std::runtime_error("failed to finalize blake2b|" + - std::to_string(res)); - } - - return hash; +template <> +[[nodiscard]] inline auto default_create_hash() -> const + std::function & { + return blake2b_384_hasher; } -template -auto create_hash_blake2b_t(std::basic_string_view data) -> hash_t { - hash_t hash{}; - - crypto_generichash_blake2b_state state{}; - auto res = crypto_generichash_blake2b_init(&state, nullptr, 0U, hash.size()); - if (res != 0) { - throw std::runtime_error("failed to initialize blake2b|" + - std::to_string(res)); - } - - res = crypto_generichash_blake2b_update( - &state, reinterpret_cast(data.data()), - data.size() * sizeof(char_t)); - if (res != 0) { - throw std::runtime_error("failed to update blake2b|" + std::to_string(res)); - } - - res = crypto_generichash_blake2b_final(&state, hash.data(), hash.size()); - if (res != 0) { - throw std::runtime_error("failed to finalize blake2b|" + - std::to_string(res)); - } - - return hash; -} - -template -auto create_hash_sha256_t(std::basic_string_view data) - -> repertory::utils::encryption::hash_256_t { - repertory::utils::encryption::hash_256_t hash{}; - - crypto_hash_sha256_state state{}; - auto res = crypto_hash_sha256_init(&state); - if (res != 0) { - throw std::runtime_error("failed to initialize sha256|" + - std::to_string(res)); - } - - res = crypto_hash_sha256_update( - &state, reinterpret_cast(data.data()), - data.size() * sizeof(char_t)); - if (res != 0) { - throw std::runtime_error("failed to update sha256|" + std::to_string(res)); - } - - res = crypto_hash_sha256_final(&state, hash.data()); - if (res != 0) { - throw std::runtime_error("failed to finalize sha256|" + - std::to_string(res)); - } - - return hash; -} - -template -auto create_hash_sha512_t(std::basic_string_view data) - -> repertory::utils::encryption::hash_512_t { - repertory::utils::encryption::hash_512_t hash{}; - - crypto_hash_sha512_state state{}; - auto res = crypto_hash_sha512_init(&state); - if (res != 0) { - throw std::runtime_error("failed to initialize sha512|" + - std::to_string(res)); - } - - res = crypto_hash_sha512_update( - &state, reinterpret_cast(data.data()), - data.size() * sizeof(char_t)); - if (res != 0) { - throw std::runtime_error("failed to update sha512|" + std::to_string(res)); - } - - res = crypto_hash_sha512_final(&state, hash.data()); - if (res != 0) { - throw std::runtime_error("failed to finalize sha512|" + - std::to_string(res)); - } - - return hash; +template <> +[[nodiscard]] inline auto default_create_hash() -> const + std::function & { + return blake2b_512_hasher; } } // namespace repertory::utils::encryption diff --git a/support/include/utils/path.hpp b/support/include/utils/path.hpp index 897cb8ef..7eebd638 100644 --- a/support/include/utils/path.hpp +++ b/support/include/utils/path.hpp @@ -30,8 +30,12 @@ inline constexpr const std::string_view backslash{"\\"}; inline constexpr const std::wstring_view backslash_w{L"\\"}; inline constexpr const std::string_view dot{"."}; inline constexpr const std::wstring_view dot_w{L"."}; +inline constexpr const std::string_view dot_backslash{".\\"}; +inline constexpr const std::wstring_view dot_backslash_w{L".\\"}; inline constexpr const std::string_view dot_slash{"./"}; inline constexpr const std::wstring_view dot_slash_w{L"./"}; +inline constexpr const std::string_view long_notation{"\\\\?\\"}; +inline constexpr const std::wstring_view long_notation_w{L"\\\\?\\"}; inline constexpr const std::string_view slash{"/"}; inline constexpr const std::wstring_view slash_w{L"/"}; #if defined(_WIN32) @@ -39,6 +43,8 @@ inline constexpr const std::string_view directory_seperator{backslash}; inline constexpr const std::wstring_view directory_seperator_w{backslash_w}; inline constexpr const std::string_view not_directory_seperator{slash}; inline constexpr const std::wstring_view not_directory_seperator_w{slash_w}; +inline constexpr const std::string_view unc_notation{"\\\\"}; +inline constexpr const std::wstring_view unc_notation_w{L"\\\\"}; #else // !defined(_WIN32) inline constexpr const std::string_view directory_seperator{slash}; inline constexpr const std::wstring_view directory_seperator_w{slash_w}; @@ -62,53 +68,6 @@ get_backslash() -> std::basic_string_view { return backslash_w; } -template -[[nodiscard]] inline constexpr auto get_dot() -> std::basic_string_view; - -template <> -[[nodiscard]] inline constexpr auto -get_dot() -> std::basic_string_view { - return dot; -} - -template <> -[[nodiscard]] inline constexpr auto -get_dot() -> std::basic_string_view { - return dot_w; -} - -template -[[nodiscard]] inline constexpr auto -get_dot_slash() -> std::basic_string_view; - -template <> -[[nodiscard]] inline constexpr auto -get_dot_slash() -> std::basic_string_view { - return dot_slash; -} - -template <> -[[nodiscard]] inline constexpr auto -get_dot_slash() -> std::basic_string_view { - return dot_slash_w; -} - -template -[[nodiscard]] inline constexpr auto -get_slash() -> std::basic_string_view; - -template <> -[[nodiscard]] inline constexpr auto -get_slash() -> std::basic_string_view { - return slash; -} - -template <> -[[nodiscard]] inline constexpr auto -get_slash() -> std::basic_string_view { - return slash_w; -} - template [[nodiscard]] inline constexpr auto get_directory_seperator() -> std::basic_string_view; @@ -141,37 +100,130 @@ get_not_directory_seperator() -> std::basic_string_view { return not_directory_seperator_w; } +template +[[nodiscard]] inline constexpr auto get_dot() -> std::basic_string_view; + +template <> +[[nodiscard]] inline constexpr auto +get_dot() -> std::basic_string_view { + return dot; +} + +template <> +[[nodiscard]] inline constexpr auto +get_dot() -> std::basic_string_view { + return dot_w; +} + +template +[[nodiscard]] inline constexpr auto +get_dot_backslash() -> std::basic_string_view; + +template <> +[[nodiscard]] inline constexpr auto +get_dot_backslash() -> std::basic_string_view { + return dot_backslash; +} + +template <> +[[nodiscard]] inline constexpr auto +get_dot_backslash() -> std::basic_string_view { + return dot_backslash_w; +} + +template +[[nodiscard]] inline constexpr auto +get_dot_slash() -> std::basic_string_view; + +template <> +[[nodiscard]] inline constexpr auto +get_dot_slash() -> std::basic_string_view { + return dot_slash; +} + +template <> +[[nodiscard]] inline constexpr auto +get_dot_slash() -> std::basic_string_view { + return dot_slash_w; +} + +template +[[nodiscard]] inline constexpr auto +get_long_notation() -> std::basic_string_view; + +template <> +[[nodiscard]] inline constexpr auto +get_long_notation() -> std::basic_string_view { + return long_notation; +} + +template <> +[[nodiscard]] inline constexpr auto +get_long_notation() -> std::basic_string_view { + return long_notation_w; +} + +template +[[nodiscard]] inline constexpr auto +get_slash() -> std::basic_string_view; + +template <> +[[nodiscard]] inline constexpr auto +get_slash() -> std::basic_string_view { + return slash; +} + +template <> +[[nodiscard]] inline constexpr auto +get_slash() -> std::basic_string_view { + return slash_w; +} + +#if defined(_WIN32) +template +[[nodiscard]] inline constexpr auto +get_unc_notation() -> std::basic_string_view; + +template <> +[[nodiscard]] inline constexpr auto +get_unc_notation() -> std::basic_string_view { + return unc_notation; +} + +template <> +[[nodiscard]] inline constexpr auto +get_unc_notation() -> std::basic_string_view { + return unc_notation_w; +} +#endif // defined(_WIN32) + +template +[[nodiscard]] inline auto get_current_path() -> string_t; + [[nodiscard]] auto absolute(std::string_view path) -> std::string; [[nodiscard]] auto absolute(std::wstring_view path) -> std::wstring; [[nodiscard]] inline auto -combine(std::string path, +combine(std::string_view path, const std::vector &paths) -> std::string; [[nodiscard]] inline auto -combine(std::wstring path, +combine(std::wstring_view path, const std::vector &paths) -> std::wstring; +[[nodiscard]] auto contains_trash_directory(std::string_view path) -> bool; + +[[nodiscard]] auto contains_trash_directory(std::wstring_view path) -> bool; + [[nodiscard]] auto inline create_api_path(std::string_view path) -> std::string; [[nodiscard]] auto inline create_api_path(std::wstring_view path) -> std::wstring; -[[nodiscard]] inline auto -directory_exists_in_path(std::string_view path, - std::string_view sub_directory) -> bool; +[[nodiscard]] auto exists(std::string_view path) -> bool; -[[nodiscard]] inline auto -directory_exists_in_path(std::wstring_view path, - std::wstring_view sub_directory) -> bool; - -[[nodiscard]] inline auto -file_exists_in_path(std::string_view path, std::string_view file_name) -> bool; - -[[nodiscard]] inline auto -file_exists_in_path(std::wstring_view path, - std::wstring_view file_name) -> bool; +[[nodiscard]] auto exists(std::wstring_view path) -> bool; [[nodiscard]] inline auto finalize(std::string_view path) -> std::string; @@ -196,22 +248,27 @@ get_parent_api_path(std::string_view path) -> std::string; [[nodiscard]] inline auto get_parent_api_path(std::wstring_view path) -> std::wstring; -[[nodiscard]] auto get_parent_directory(std::string_view path) -> std::string; +[[nodiscard]] auto get_parent_path(std::string_view path) -> std::string; -[[nodiscard]] auto get_parent_directory(std::wstring_view path) -> std::wstring; +[[nodiscard]] auto get_parent_path(std::wstring_view path) -> std::wstring; -[[nodiscard]] auto is_trash_directory(std::string_view path) -> bool; +[[nodiscard]] inline auto +get_parts(std::string_view path) -> std::vector; -[[nodiscard]] auto is_trash_directory(std::wstring_view path) -> bool; +[[nodiscard]] inline auto +get_parts_w(std::wstring_view path) -> std::vector; + +[[nodiscard]] auto get_relative_path(std::string_view path, + std::string_view root_path) -> std::string; + +[[nodiscard]] auto +get_relative_path(std::wstring_view path, + std::wstring_view root_path) -> std::wstring; [[nodiscard]] auto make_file_uri(std::string_view path) -> std::string; [[nodiscard]] auto make_file_uri(std::wstring_view path) -> std::wstring; -[[nodiscard]] auto remove_file_name(std::string_view path) -> std::string; - -[[nodiscard]] auto remove_file_name(std::wstring_view path) -> std::wstring; - [[nodiscard]] auto strip_to_file_name(std::string path) -> std::string; [[nodiscard]] auto strip_to_file_name(std::wstring path) -> std::wstring; @@ -222,31 +279,30 @@ get_parent_api_path(std::wstring_view path) -> std::wstring; template [[nodiscard]] inline auto combine_t( - string_t path, + std::basic_string_view path, const std::vector> &paths) -> string_t { - path = std::accumulate( - paths.begin(), paths.end(), path, [](auto next_path, auto &&path_part) { - if (next_path.empty()) { - return string_t{path_part}; - } + auto dir_sep_t = + string_t{get_directory_seperator()}; + return absolute( + std::accumulate(paths.begin(), paths.end(), + std::basic_string{path}, + [&dir_sep_t](auto next_path, auto &&path_part) { + if (next_path.empty()) { + return string_t{path_part}; + } - return next_path + - string_t{ - get_directory_seperator()} + - string_t{path_part}; - }); - - return absolute(path); + return next_path + dir_sep_t + string_t{path_part}; + })); } -inline auto combine(std::string path, +inline auto combine(std::string_view path, const std::vector &paths) -> std::string { return combine_t(path, paths); } inline auto -combine(std::wstring path, +combine(std::wstring_view path, const std::vector &paths) -> std::wstring { return combine_t(path, paths); } @@ -255,12 +311,20 @@ template [[nodiscard]] inline auto create_api_path_t( std::basic_string_view path) -> string_t { auto backslash_t = get_backslash(); - auto dot_t = get_dot(); + auto dot_backslash_t = get_dot_backslash(); auto dot_slash_t = get_dot_slash(); + auto dot_t = get_dot(); auto slash_t = get_slash(); +#if defined(_WIN32) + auto long_notation_t = get_long_notation(); + if (utils::string::begins_with(path, long_notation_t)) { + path = path.substr(long_notation_t.size()); + } +#endif // defined(_WIN32) + if (path.empty() || path == backslash_t || path == dot_t || - path == dot_slash_t || path == slash_t) { + path == dot_slash_t || path == slash_t || path == dot_backslash_t) { return string_t{slash_t}; } @@ -271,16 +335,12 @@ template } #endif // defined(_WIN32) + format_path(api_path, slash_t, backslash_t); + while (utils::string::begins_with(api_path, dot_slash_t)) { api_path = api_path.substr(dot_slash_t.size()); } - while (utils::string::begins_with(api_path, dot_t)) { - api_path = api_path.substr(dot_t.size()); - } - - format_path(api_path, slash_t, backslash_t); - if (api_path.at(0U) != slash_t.at(0U)) { return string_t{slash_t} + api_path; } @@ -296,50 +356,57 @@ inline auto create_api_path(std::wstring_view path) -> std::wstring { return create_api_path_t(path); } -template -[[nodiscard]] inline auto directory_exists_in_path_t( - std::basic_string_view path, - std::basic_string_view sub_directory) - -> bool { - return std::filesystem::is_directory( - std::filesystem::path(path).append(sub_directory)); -} - -inline auto directory_exists_in_path(std::string_view path, - std::string_view sub_directory) -> bool { - return directory_exists_in_path_t(path, sub_directory); -} - -inline auto directory_exists_in_path(std::wstring_view path, - std::wstring_view sub_directory) -> bool { - return directory_exists_in_path_t(path, sub_directory); -} - -template -[[nodiscard]] inline auto file_exists_in_path_t( - std::basic_string_view path, - std::basic_string_view file_name) -> bool { - return std::filesystem::is_regular_file( - std::filesystem::path(path).append(file_name)); -} - -inline auto file_exists_in_path(std::string_view path, - std::string_view file_name) -> bool { - return file_exists_in_path_t(path, file_name); -} - -inline auto file_exists_in_path(std::wstring_view path, - std::wstring_view file_name) -> bool { - return file_exists_in_path_t(path, file_name); -} - template [[nodiscard]] inline auto finalize_t( std::basic_string_view path) -> string_t { + string_t dir_sep_t{get_directory_seperator()}; string_t fmt_path{path}; - format_path(fmt_path, - get_directory_seperator(), + if (fmt_path.empty()) { + return fmt_path; + } + + format_path(fmt_path, dir_sep_t, get_not_directory_seperator()); + +#if defined(_WIN32) + auto unc_notation_t = get_unc_notation(); + if (utils::string::begins_with(fmt_path, unc_notation_t)) { + return fmt_path; + } + + auto dot_t = get_dot(); + auto dot_sep_t = string_t{dot_t} + dir_sep_t; + if (fmt_path == dot_t || fmt_path == dot_sep_t) { + return get_current_path(); + } + + if (fmt_path == dir_sep_t) { +#if defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES) + return get_current_path().substr(0U, long_notation.size() + 2U); +#else // !defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES) + return get_current_path().substr(0U, 2U); +#endif // defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES) + } + + if (utils::string::begins_with(fmt_path, dir_sep_t)) { +#if defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES) + return get_current_path().substr(0U, long_notation.size() + 2U) + + fmt_path; +#else // !defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES) + return get_current_path().substr(0U, 2U) + fmt_path; +#endif // defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES) + } + + if (utils::string::begins_with(fmt_path, dot_sep_t)) { + return get_current_path() + dir_sep_t + fmt_path.substr(2U); + } + +#if defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES) + return string_t{get_long_notation()} + + fmt_path; +#endif // defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES) +#endif // defined(_WIN32) + return fmt_path; } @@ -359,6 +426,19 @@ format_path(string_t &path, -> string_t & { utils::string::replace(path, not_sep, sep); +#if defined(_WIN32) + auto is_unc{false}; + auto long_notation_t = get_long_notation(); + auto unc_notation_t = get_unc_notation(); + if (utils::string::begins_with(path, long_notation_t)) { + path = path.substr(long_notation_t.size()); + } else if (utils::string::begins_with(path, unc_notation_t)) { + path = path.substr(unc_notation_t.size()); + utils::string::left_trim(path, sep.at(0U)); + is_unc = true; + } +#endif // defined(_WIN32) + string_t double_sep(2U, sep.at(0U)); while (utils::string::contains(path, double_sep)) { utils::string::replace(path, double_sep, sep); @@ -369,7 +449,9 @@ format_path(string_t &path, } #if defined(_WIN32) - if ((path.size() >= 2U) && (path.at(1U) == ':')) { + if (is_unc) { + path = string_t{unc_notation_t} + path; + } else if ((path.size() >= 2U) && (path.at(1U) == ':')) { path[0U] = utils::string::to_lower(string_t(1U, path.at(0U))).at(0U); } #endif // defined(_WIN32) @@ -377,6 +459,24 @@ format_path(string_t &path, return path; } +template <> +[[nodiscard]] inline auto get_current_path() -> std::string { +#if defined(_WIN32) + std::string path; + path.resize(repertory::max_path_length + 1); + ::GetCurrentDirectoryA(static_cast(path.size()), path.data()); + path = path.c_str(); + return finalize(path); +#else // !defined(_WIN32) + return finalize(std::filesystem::current_path().string()); +#endif // defined(_WIN32) +} + +template <> +[[nodiscard]] inline auto get_current_path() -> std::wstring { + return utils::string::from_utf8(get_current_path()); +} + template [[nodiscard]] inline auto get_parent_api_path_t( std::basic_string_view path) -> string_t { @@ -387,12 +487,12 @@ template return ret; } - ret = path.substr(0, path.rfind('/') + 1); + ret = path.substr(0, path.rfind(slash_t) + 1); if (ret == slash_t) { return ret; } - return utils::string::right_trim(ret, '/'); + return utils::string::right_trim(ret, slash_t.at(0U)); } inline auto get_parent_api_path(std::string_view path) -> std::string { @@ -402,6 +502,23 @@ inline auto get_parent_api_path(std::string_view path) -> std::string { inline auto get_parent_api_path(std::wstring_view path) -> std::wstring { return get_parent_api_path_t(path); } + +template +[[nodiscard]] inline auto +get_parts_t(std::basic_string_view path) + -> std::vector { + return utils::string::split( + path, get_directory_seperator().at(0U), + false); +} + +inline auto get_parts(std::string_view path) -> std::vector { + return get_parts_t(path); +} + +inline auto get_parts_w(std::wstring_view path) -> std::vector { + return get_parts_t(path); +} } // namespace repertory::utils::path #endif // REPERTORY_INCLUDE_UTILS_PATH_HPP_ diff --git a/support/include/utils/time.hpp b/support/include/utils/time.hpp index 2db765ef..c790d840 100644 --- a/support/include/utils/time.hpp +++ b/support/include/utils/time.hpp @@ -25,7 +25,9 @@ #include "utils/config.hpp" namespace repertory::utils::time { -inline constexpr const auto NANOS_PER_SECOND = 1000000000L; +inline constexpr const auto NANOS_PER_SECOND{1000000000ULL}; +inline constexpr const auto WIN32_TIME_CONVERSION{116444736000000000ULL}; +inline constexpr const auto WIN32_TIME_NANOS_PER_TICK{100ULL}; #if defined(PROJECT_ENABLE_SPDLOG) || defined(PROJECT_ENABLE_FMT) [[nodiscard]] inline auto convert_to_utc(time_t time) -> std::time_t { @@ -34,11 +36,6 @@ inline constexpr const auto NANOS_PER_SECOND = 1000000000L; } #endif // defined(PROJECT_ENABLE_SPDLOG) || defined(PROJECT_ENABLE_FMT) -#if defined(_WIN32) -[[nodiscard]] auto -filetime_to_unix_time(const FILETIME &file_time) -> std::uint64_t; -#endif // defined(_WIN32) - #if defined(PROJECT_ENABLE_SPDLOG) || defined(PROJECT_ENABLE_FMT) [[nodiscard]] inline auto get_current_time_utc() -> std::time_t { auto calendar_time = fmt::gmtime(std::time(nullptr)); @@ -46,8 +43,6 @@ filetime_to_unix_time(const FILETIME &file_time) -> std::uint64_t; } #endif // defined(PROJECT_ENABLE_SPDLOG) || defined(PROJECT_ENABLE_FMT) -[[nodiscard]] auto get_file_time_now() -> std::uint64_t; - void get_local_time_now(struct tm &local_time); [[nodiscard]] auto get_time_now() -> std::uint64_t; @@ -55,10 +50,20 @@ void get_local_time_now(struct tm &local_time); #if defined(_WIN32) auto strptime(const char *s, const char *f, struct tm *tm) -> const char *; -[[nodiscard]] auto time64_to_unix_time(const __time64_t &time) -> std::uint64_t; - [[nodiscard]] auto unix_time_to_filetime(std::uint64_t unix_time) -> FILETIME; + +[[nodiscard]] auto +windows_file_time_to_unix_time(FILETIME win_time) -> std::uint64_t; + +[[nodiscard]] auto +windows_time_t_to_unix_time(__time64_t win_time) -> std::uint64_t; #endif // defined(_WIN32) + +[[nodiscard]] auto +unix_time_to_windows_time(std::uint64_t unix_time) -> std::uint64_t; + +[[nodiscard]] auto +windows_time_to_unix_time(std::uint64_t win_time) -> std::uint64_t; } // namespace repertory::utils::time #endif // REPERTORY_INCLUDE_UTILS_TIME_HPP_ diff --git a/support/include/utils/types/file/i_directory.hpp b/support/include/utils/types/file/i_directory.hpp new file mode 100644 index 00000000..464e9667 --- /dev/null +++ b/support/include/utils/types/file/i_directory.hpp @@ -0,0 +1,79 @@ +/* + Copyright <2018-2024> + + 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. +*/ +#ifndef REPERTORY_INCLUDE_TYPES_FILE_I_DIRECTORY_HPP_ +#define REPERTORY_INCLUDE_TYPES_FILE_I_DIRECTORY_HPP_ + +#include "utils/config.hpp" + +#include "utils/types/file/i_file.hpp" +#include "utils/types/file/i_fs_item.hpp" + +namespace repertory::utils::file { +struct i_directory : public i_fs_item { + using fs_directory_t = std::unique_ptr; + using fs_file_t = i_file::fs_file_t; + + virtual ~i_directory() = default; + + [[nodiscard]] virtual auto + count(bool recursive = false) const -> std::uint64_t = 0; + + [[nodiscard]] virtual auto + create_directory(std::string_view path = "") const -> fs_directory_t = 0; + + [[nodiscard]] virtual auto create_file(std::string_view file_name, + bool read_only) const -> fs_file_t = 0; + + [[nodiscard]] auto is_directory_item() const -> bool override { return true; } + + [[nodiscard]] virtual auto + get_directory(std::string_view path) const -> fs_directory_t = 0; + + [[nodiscard]] virtual auto + get_directories() const -> std::vector = 0; + + [[nodiscard]] virtual auto + get_file(std::string_view path) const -> fs_file_t = 0; + + [[nodiscard]] virtual auto get_files() const -> std::vector = 0; + + [[nodiscard]] virtual auto get_items() const -> std::vector = 0; + + [[nodiscard]] virtual auto remove_recursively() -> bool = 0; + + [[nodiscard]] virtual auto + size(bool recursive = false) const -> std::uint64_t = 0; + +protected: + i_directory() noexcept = default; + + i_directory(const i_directory &) noexcept = default; + + i_directory(i_directory &&) noexcept = default; + + auto operator=(i_directory &&) noexcept -> i_directory & = default; + + auto operator=(const i_directory &) noexcept -> i_directory & = default; +}; +} // namespace repertory::utils::file + +#endif // REPERTORY_INCLUDE_TYPES_FILE_I_DIRECTORY_HPP_ diff --git a/support/include/utils/types/file/i_file.hpp b/support/include/utils/types/file/i_file.hpp new file mode 100644 index 00000000..f870d693 --- /dev/null +++ b/support/include/utils/types/file/i_file.hpp @@ -0,0 +1,93 @@ +/* + Copyright <2018-2024> + + 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. +*/ +#ifndef REPERTORY_INCLUDE_TYPES_FILE_I_FILE_HPP_ +#define REPERTORY_INCLUDE_TYPES_FILE_I_FILE_HPP_ + +#include "utils/config.hpp" + +#include "utils/types/file/i_fs_item.hpp" + +namespace repertory::utils::file { +struct i_file : public i_fs_item { + using fs_file_t = std::unique_ptr; + + virtual ~i_file() = default; + + virtual void close() = 0; + + virtual void flush() const = 0; + + [[nodiscard]] virtual auto get_handle() const -> native_handle = 0; + + [[nodiscard]] virtual auto get_read_buffer_size() const -> std::uint32_t = 0; + + [[nodiscard]] auto is_directory_item() const -> bool override { + return false; + } + + [[nodiscard]] virtual auto is_read_only() const -> bool = 0; + + [[nodiscard]] virtual auto read(data_buffer &data, std::uint64_t offset, + std::size_t *total_read = nullptr) -> bool { + return read(data.data(), data.size(), offset, total_read); + } + + [[nodiscard]] virtual auto + read(unsigned char *data, std::size_t to_read, std::uint64_t offset, + std::size_t *total_read = nullptr) -> bool = 0; + + [[nodiscard]] virtual auto + read_all(data_buffer &data, std::uint64_t offset, + std::size_t *total_read = nullptr) -> bool; + + virtual auto set_read_buffer_size(std::uint32_t size) -> std::uint32_t = 0; + + [[nodiscard]] virtual auto size() const -> std::optional = 0; + + [[nodiscard]] virtual auto truncate() -> bool { return truncate(0U); } + + [[nodiscard]] virtual auto truncate(std::size_t size) -> bool = 0; + + [[nodiscard]] virtual auto + write(const data_buffer &data, std::uint64_t offset, + std::size_t *total_written = nullptr) -> bool { + return write(data.data(), data.size(), offset, total_written); + } + + [[nodiscard]] virtual auto + write(const unsigned char *data, std::size_t to_write, std::size_t offset, + std::size_t *total_written = nullptr) -> bool = 0; + +protected: + i_file() noexcept = default; + + i_file(const i_file &) noexcept = default; + + i_file(i_file &&) noexcept = default; + + auto operator=(i_file &&) noexcept -> i_file & = default; + + auto operator=(const i_file &) noexcept -> i_file & = default; +}; +} // namespace repertory::utils::file + +#endif // REPERTORY_INCLUDE_TYPES_FILE_I_FILE_HPP_ diff --git a/support/include/utils/types/file/i_fs_item.hpp b/support/include/utils/types/file/i_fs_item.hpp new file mode 100644 index 00000000..db4e647c --- /dev/null +++ b/support/include/utils/types/file/i_fs_item.hpp @@ -0,0 +1,111 @@ +/* + Copyright <2018-2024> + + 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. +*/ +#ifndef REPERTORY_INCLUDE_TYPES_FILE_I_FS_ITEM_HPP_ +#define REPERTORY_INCLUDE_TYPES_FILE_I_FS_ITEM_HPP_ + +#include "utils/config.hpp" + +#include "utils/string.hpp" + +namespace repertory::utils::file { +enum class time_type { + accessed, + created, + modified, + written, +}; + +struct file_times final { + std::uint64_t accessed{}; + std::uint64_t created{}; + std::uint64_t modified{}; + std::uint64_t written{}; + + [[nodiscard]] auto get(time_type type) const -> std::uint64_t { + switch (type) { + case time_type::accessed: + return accessed; + case time_type::created: + return created; + case time_type::modified: + return modified; + case time_type::written: + return written; + } + + throw std::runtime_error("type_type not supported"); + } +}; + +struct i_fs_item { + using fs_item_t = std::unique_ptr; + + virtual ~i_fs_item() = default; + + [[nodiscard]] virtual auto copy_to(std::string_view to_path, + bool overwrite) const -> bool = 0; + + [[nodiscard]] virtual auto copy_to(std::wstring_view new_path, + bool overwrite) -> bool { + return copy_to(utils::string::to_utf8(new_path), overwrite); + } + + [[nodiscard]] virtual auto exists() const -> bool = 0; + + [[nodiscard]] virtual auto get_path() const -> std::string = 0; + + [[nodiscard]] virtual auto + get_time(time_type type) const -> std::optional; + + [[nodiscard]] virtual auto is_directory_item() const -> bool = 0; + + [[nodiscard]] auto is_file_item() const -> bool { + return not is_directory_item(); + } + + [[nodiscard]] virtual auto is_symlink() const -> bool = 0; + + [[nodiscard]] virtual auto move_to(std::string_view new_path) -> bool = 0; + + [[nodiscard]] virtual auto move_to(std::wstring_view new_path) -> bool { + return move_to(utils::string::to_utf8(new_path)); + } + + [[nodiscard]] virtual auto remove() -> bool = 0; + +public: + [[nodiscard]] virtual operator bool() const = 0; + +protected: + i_fs_item() noexcept = default; + + i_fs_item(const i_fs_item &) noexcept = default; + + i_fs_item(i_fs_item &&) noexcept = default; + + auto operator=(i_fs_item &&) noexcept -> i_fs_item & = default; + + auto operator=(const i_fs_item &) noexcept -> i_fs_item & = default; +}; +} // namespace repertory::utils::file + +#endif // REPERTORY_INCLUDE_TYPES_FILE_I_FS_ITEM_HPP_ diff --git a/support/src/backward.cpp b/support/src/backward.cpp deleted file mode 100644 index c0330166..00000000 --- a/support/src/backward.cpp +++ /dev/null @@ -1,45 +0,0 @@ -// Pick your poison. -// -// On GNU/Linux, you have few choices to get the most out of your stack trace. -// -// By default you get: -// - object filename -// - function name -// -// In order to add: -// - source filename -// - line and column numbers -// - source code snippet (assuming the file is accessible) - -// Install one of the following libraries then uncomment one of the macro (or -// better, add the detection of the lib and the macro definition in your build -// system) - -// - apt-get install libdw-dev ... -// - g++/clang++ -ldw ... -// #define BACKWARD_HAS_DW 1 - -// - apt-get install binutils-dev ... -// - g++/clang++ -lbfd ... -// #define BACKWARD_HAS_BFD 1 - -// - apt-get install libdwarf-dev ... -// - g++/clang++ -ldwarf ... -// #define BACKWARD_HAS_DWARF 1 - -// Regardless of the library you choose to read the debug information, -// for potentially more detailed stack traces you can use libunwind -// - apt-get install libunwind-dev -// - g++/clang++ -lunwind -// #define BACKWARD_HAS_LIBUNWIND 1 -#include "backward.hpp" - -#if defined(PROJECT_ENABLE_BACKWARD_CPP) - -namespace backward { - -backward::SignalHandling sh; - -} // namespace backward - -#endif // defined(PROJECT_ENABLE_BACKWARD_CPP) diff --git a/support/src/utils/common.cpp b/support/src/utils/common.cpp index 72b926e6..57f309f1 100644 --- a/support/src/utils/common.cpp +++ b/support/src/utils/common.cpp @@ -21,7 +21,7 @@ */ #include "utils/common.hpp" -#include "utils/path.hpp" +#include "utils/error.hpp" #include "utils/string.hpp" namespace repertory::utils { @@ -67,7 +67,7 @@ auto compare_version_strings(std::wstring_view version1, #if defined(PROJECT_ENABLE_STDUUID) auto create_uuid_string() -> std::string { - std::random_device random_device; + std::random_device random_device{}; auto seed_data = std::array{}; std::generate(std::begin(seed_data), std::end(seed_data), std::ref(random_device)); @@ -83,7 +83,6 @@ auto create_uuid_wstring() -> std::wstring { } #endif // defined(PROJECT_ENABLE_STDUUID) -#if defined(PROJECT_ENABLE_LIBSODIUM) auto generate_random_string(std::size_t length) -> std::string { std::string ret; if (length == 0U) { @@ -98,9 +97,7 @@ auto generate_random_string(std::size_t length) -> std::string { generate_random_between(97U, 255U), }; ch = static_cast( - random_list.at(repertory::utils::generate_random_between(0U, 2U)) % - 74U + - 48U); + random_list.at(generate_random_between(0U, 2U)) % 74U + 48U); } return ret; @@ -109,7 +106,6 @@ auto generate_random_string(std::size_t length) -> std::string { auto generate_random_wstring(std::size_t length) -> std::wstring { return utils::string::from_utf8(generate_random_string(length)); } -#endif // defined(PROJECT_ENABLE_LIBSODIUM) auto get_environment_variable(std::string_view variable) -> std::string { static std::mutex mtx{}; @@ -154,4 +150,27 @@ auto get_next_available_port(std::uint16_t first_port, return not error_code; } #endif // defined(PROJECT_ENABLE_BOOST) + +auto retry_action(retryable_action_t action, std::size_t retry_count, + std::chrono::milliseconds retry_wait) -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + for (std::size_t idx = 0U; idx < retry_count; ++idx) { + if (action()) { + return true; + } + + std::this_thread::sleep_for(retry_wait); + } + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} } // namespace repertory::utils diff --git a/repertory/librepertory/src/utils/encrypting_reader.cpp b/support/src/utils/encrypting_reader.cpp similarity index 79% rename from repertory/librepertory/src/utils/encrypting_reader.cpp rename to support/src/utils/encrypting_reader.cpp index c61e2610..36bb5321 100644 --- a/repertory/librepertory/src/utils/encrypting_reader.cpp +++ b/support/src/utils/encrypting_reader.cpp @@ -1,405 +1,415 @@ -/* - Copyright <2018-2024> - - 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. -*/ -#include "utils/encrypting_reader.hpp" - -#include "platform/platform.hpp" -#include "types/repertory.hpp" -#include "utils/collection.hpp" -#include "utils/common.hpp" -#include "utils/error_utils.hpp" -#include "utils/file.hpp" - -namespace repertory::utils::encryption { -class encrypting_streambuf final : public encrypting_reader::streambuf { -public: - encrypting_streambuf(const encrypting_streambuf &) = default; - encrypting_streambuf(encrypting_streambuf &&) = delete; - auto - operator=(const encrypting_streambuf &) -> encrypting_streambuf & = delete; - auto operator=(encrypting_streambuf &&) -> encrypting_streambuf & = delete; - - explicit encrypting_streambuf(const encrypting_reader &reader) - : reader_(reader) { - setg(reinterpret_cast(0), reinterpret_cast(0), - reinterpret_cast(reader_.get_total_size())); - } - - ~encrypting_streambuf() override = default; - -private: - encrypting_reader reader_; - -protected: - auto seekoff(off_type off, std::ios_base::seekdir dir, - std::ios_base::openmode which = std::ios_base::out | - std::ios_base::in) - -> pos_type override { - if ((which & std::ios_base::in) != std::ios_base::in) { - throw std::runtime_error("output is not supported"); - } - - const auto set_position = [this](char *next) -> pos_type { - if ((next <= egptr()) && (next >= eback())) { - setg(eback(), next, reinterpret_cast(reader_.get_total_size())); - return static_cast( - reinterpret_cast(gptr())); - } - - return {traits_type::eof()}; - }; - - switch (dir) { - case std::ios_base::beg: - return set_position(eback() + off); - - case std::ios_base::cur: - return set_position(gptr() + off); - - case std::ios_base::end: - return set_position(egptr() + off); - } - - return encrypting_reader::streambuf::seekoff(off, dir, which); - } - - auto seekpos(pos_type pos, std::ios_base::openmode which = - std::ios_base::out | - std::ios_base::in) -> pos_type override { - return seekoff(pos, std::ios_base::beg, which); - } - - auto uflow() -> int_type override { - auto ret = underflow(); - if (ret == traits_type::eof()) { - return ret; - } - - gbump(1); - - return ret; - } - - auto underflow() -> int_type override { - if (gptr() == egptr()) { - return traits_type::eof(); - } - - reader_.set_read_position(reinterpret_cast(gptr())); - - char c{}; - const auto res = encrypting_reader::reader_function(&c, 1U, 1U, &reader_); - if (res != 1) { - return traits_type::eof(); - } - - return c; - } - - auto xsgetn(char *ptr, std::streamsize count) -> std::streamsize override { - if (gptr() == egptr()) { - return traits_type::eof(); - } - - reader_.set_read_position(reinterpret_cast(gptr())); - - const auto res = encrypting_reader::reader_function( - ptr, 1U, static_cast(count), &reader_); - if ((res == reader_.get_error_return()) || - (reader_.get_stop_requested() && (res == CURL_READFUNC_ABORT))) { - return traits_type::eof(); - } - - setg(eback(), gptr() + res, - reinterpret_cast(reader_.get_total_size())); - return static_cast(res); - } -}; - -class encrypting_reader_iostream final : public encrypting_reader::iostream { -public: - encrypting_reader_iostream(const encrypting_reader_iostream &) = delete; - encrypting_reader_iostream(encrypting_reader_iostream &&) = delete; - - auto operator=(const encrypting_reader_iostream &) - -> encrypting_reader_iostream & = delete; - auto operator=(encrypting_reader_iostream &&) - -> encrypting_reader_iostream & = delete; - - explicit encrypting_reader_iostream( - std::unique_ptr buffer) - : encrypting_reader::iostream(buffer.get()), buffer_(std::move(buffer)) {} - - ~encrypting_reader_iostream() override = default; - -private: - std::unique_ptr buffer_; -}; - -const std::size_t encrypting_reader::header_size_ = ([]() { - return crypto_aead_xchacha20poly1305_IETF_NPUBBYTES + - crypto_aead_xchacha20poly1305_IETF_ABYTES; -})(); -const std::size_t encrypting_reader::data_chunk_size_ = (8UL * 1024UL * 1024UL); -const std::size_t encrypting_reader::encrypted_chunk_size_ = - data_chunk_size_ + header_size_; - -encrypting_reader::encrypting_reader( - std::string_view file_name, std::string_view source_path, - stop_type &stop_requested, std::string_view token, - std::optional relative_parent_path, - std::size_t error_return) - : key_(utils::encryption::generate_key( - token)), - stop_requested_(stop_requested), - error_return_(error_return) { - const auto res = native_file::create_or_open(source_path, true, source_file_); - if (res != api_error::success) { - throw std::runtime_error("file open failed|src|" + - std::string{source_path} + '|' + - api_error_to_string(res)); - } - - data_buffer result; - utils::encryption::encrypt_data( - key_, reinterpret_cast(file_name.data()), - file_name.size(), result); - encrypted_file_name_ = utils::collection::to_hex_string(result); - - if (relative_parent_path.has_value()) { - for (auto &&part : std::filesystem::path(relative_parent_path.value())) { - utils::encryption::encrypt_data( - key_, reinterpret_cast(part.string().c_str()), - strnlen(part.string().c_str(), part.string().size()), result); - encrypted_file_path_ += '/' + utils::collection::to_hex_string(result); - } - encrypted_file_path_ += '/' + encrypted_file_name_; - } - - std::uint64_t file_size{}; - if (not utils::file::get_file_size(source_path, file_size)) { - throw std::runtime_error("get file size failed|src|" + - std::string{source_path} + '|' + - std::to_string(utils::get_last_error_code())); - } - - const auto total_chunks = utils::divide_with_ceiling( - file_size, static_cast(data_chunk_size_)); - total_size_ = file_size + (total_chunks * encryption_header_size); - last_data_chunk_ = total_chunks - 1U; - last_data_chunk_size_ = (file_size <= data_chunk_size_) ? file_size - : (file_size % data_chunk_size_) == 0U - ? data_chunk_size_ - : file_size % data_chunk_size_; - iv_list_.resize(total_chunks); - for (auto &iv : iv_list_) { - randombytes_buf(iv.data(), iv.size()); - } -} - -encrypting_reader::encrypting_reader(std::string_view encrypted_file_path, - std::string_view source_path, - stop_type &stop_requested, - std::string_view token, - std::size_t error_return) - : key_(utils::encryption::generate_key( - token)), - stop_requested_(stop_requested), - error_return_(error_return) { - const auto res = native_file::create_or_open(source_path, true, source_file_); - if (res != api_error::success) { - throw std::runtime_error("file open failed|src|" + - std::string{source_path} + '|' + - api_error_to_string(res)); - } - - encrypted_file_path_ = encrypted_file_path; - encrypted_file_name_ = - std::filesystem::path(encrypted_file_path_).filename().string(); - - std::uint64_t file_size{}; - if (not utils::file::get_file_size(source_path, file_size)) { - throw std::runtime_error("get file size failed|src|" + - std::string{source_path} + '|' + - std::to_string(utils::get_last_error_code())); - } - - const auto total_chunks = utils::divide_with_ceiling( - file_size, static_cast(data_chunk_size_)); - total_size_ = file_size + (total_chunks * encryption_header_size); - last_data_chunk_ = total_chunks - 1U; - last_data_chunk_size_ = (file_size <= data_chunk_size_) ? file_size - : (file_size % data_chunk_size_) == 0U - ? data_chunk_size_ - : file_size % data_chunk_size_; - iv_list_.resize(total_chunks); - for (auto &iv : iv_list_) { - randombytes_buf(iv.data(), iv.size()); - } -} - -encrypting_reader::encrypting_reader( - std::string_view encrypted_file_path, std::string_view source_path, - stop_type &stop_requested, std::string_view token, - std::vector< - std::array> - iv_list, - std::size_t error_return) - : key_(utils::encryption::generate_key( - token)), - stop_requested_(stop_requested), - error_return_(error_return) { - const auto res = native_file::create_or_open(source_path, true, source_file_); - if (res != api_error::success) { - throw std::runtime_error("file open failed|src|" + - std::string{source_path} + '|' + - api_error_to_string(res)); - } - - encrypted_file_path_ = encrypted_file_path; - encrypted_file_name_ = - std::filesystem::path(encrypted_file_path_).filename().string(); - - std::uint64_t file_size{}; - if (not utils::file::get_file_size(source_path, file_size)) { - throw std::runtime_error("get file size failed|src|" + - std::string{source_path} + '|' + - std::to_string(utils::get_last_error_code())); - } - - const auto total_chunks = utils::divide_with_ceiling( - file_size, static_cast(data_chunk_size_)); - total_size_ = file_size + (total_chunks * encryption_header_size); - last_data_chunk_ = total_chunks - 1U; - last_data_chunk_size_ = (file_size <= data_chunk_size_) ? file_size - : (file_size % data_chunk_size_) == 0U - ? data_chunk_size_ - : file_size % data_chunk_size_; - iv_list_ = std::move(iv_list); -} - -encrypting_reader::encrypting_reader(const encrypting_reader &reader) - : key_(reader.key_), - stop_requested_(reader.stop_requested_), - error_return_(reader.error_return_), - chunk_buffers_(reader.chunk_buffers_), - encrypted_file_name_(reader.encrypted_file_name_), - encrypted_file_path_(reader.encrypted_file_path_), - iv_list_(reader.iv_list_), - last_data_chunk_(reader.last_data_chunk_), - last_data_chunk_size_(reader.last_data_chunk_size_), - read_offset_(reader.read_offset_), - source_file_(native_file::clone(reader.source_file_)), - total_size_(reader.total_size_) {} - -encrypting_reader::~encrypting_reader() { - if (source_file_) { - source_file_->close(); - } -} - -auto encrypting_reader::calculate_decrypted_size(std::uint64_t total_size) - -> std::uint64_t { - return total_size - (utils::divide_with_ceiling( - total_size, static_cast( - get_encrypted_chunk_size())) * - encryption_header_size); -} - -auto encrypting_reader::calculate_encrypted_size(std::string_view source_path) - -> std::uint64_t { - std::uint64_t file_size{}; - if (not utils::file::get_file_size(source_path, file_size)) { - throw std::runtime_error("get file size failed|src|" + - std::string{source_path} + '|' + - std::to_string(utils::get_last_error_code())); - } - - const auto total_chunks = utils::divide_with_ceiling( - file_size, static_cast(data_chunk_size_)); - return file_size + (total_chunks * encryption_header_size); -} - -auto encrypting_reader::create_iostream() const - -> std::shared_ptr { - return std::make_shared( - std::make_unique(*this)); -} - -auto encrypting_reader::reader_function(char *buffer, size_t size, - size_t nitems) -> size_t { - static constexpr const std::string_view function_name{ - static_cast(__FUNCTION__), - }; - - const auto read_size = static_cast(std::min( - static_cast(size * nitems), total_size_ - read_offset_)); - - auto chunk = read_offset_ / encrypted_chunk_size_; - auto chunk_offset = read_offset_ % encrypted_chunk_size_; - std::size_t total_read{}; - - auto ret = false; - if (read_offset_ < total_size_) { - try { - ret = true; - auto remain = read_size; - while (not stop_requested_ && ret && (remain != 0U)) { - if (chunk_buffers_.find(chunk) == chunk_buffers_.end()) { - auto &chunk_buffer = chunk_buffers_[chunk]; - data_buffer file_data(chunk == last_data_chunk_ - ? last_data_chunk_size_ - : data_chunk_size_); - chunk_buffer.resize(file_data.size() + encryption_header_size); - - std::size_t bytes_read{}; - if ((ret = source_file_->read_bytes(&file_data[0u], file_data.size(), - chunk * data_chunk_size_, - bytes_read))) { - utils::encryption::encrypt_data(iv_list_.at(chunk), key_, file_data, - chunk_buffer); - } - } else if (chunk) { - chunk_buffers_.erase(chunk - 1u); - } - - auto &chunk_buffer = chunk_buffers_[chunk]; - const auto to_read = std::min( - static_cast(chunk_buffer.size() - chunk_offset), - remain); - std::memcpy(buffer + total_read, &chunk_buffer[chunk_offset], to_read); - total_read += to_read; - remain -= to_read; - chunk_offset = 0u; - chunk++; - read_offset_ += to_read; - } - } catch (const std::exception &e) { - utils::error::raise_error(function_name, e, "exception occurred"); - ret = false; - } - } - - return stop_requested_ ? CURL_READFUNC_ABORT - : ret ? total_read - : error_return_; -} -} // namespace repertory::utils::encryption +/* + Copyright <2018-2024> + + 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. +*/ +#include "utils/encrypting_reader.hpp" + +#include "utils/collection.hpp" +#include "utils/common.hpp" +#include "utils/encryption.hpp" +#include "utils/error.hpp" +#include "utils/file.hpp" +#include "utils/unix.hpp" +#include "utils/windows.hpp" + +#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) + +#if !defined(CURL_READFUNC_ABORT) +#define CURL_READFUNC_ABORT (-1) +#endif // !defined(CURL_READFUNC_ABORT) + +namespace repertory::utils::encryption { +class encrypting_streambuf final : public encrypting_reader::streambuf { +public: + encrypting_streambuf(const encrypting_streambuf &) = default; + encrypting_streambuf(encrypting_streambuf &&) = delete; + auto + operator=(const encrypting_streambuf &) -> encrypting_streambuf & = delete; + auto operator=(encrypting_streambuf &&) -> encrypting_streambuf & = delete; + + explicit encrypting_streambuf(const encrypting_reader &reader) + : reader_(reader) { + setg(reinterpret_cast(0), reinterpret_cast(0), + reinterpret_cast(reader_.get_total_size())); + } + + ~encrypting_streambuf() override = default; + +private: + encrypting_reader reader_; + +protected: + auto seekoff(off_type off, std::ios_base::seekdir dir, + std::ios_base::openmode which = std::ios_base::out | + std::ios_base::in) + -> pos_type override { + if ((which & std::ios_base::in) != std::ios_base::in) { + throw std::runtime_error("output is not supported"); + } + + const auto set_position = [this](char *next) -> pos_type { + if ((next <= egptr()) && (next >= eback())) { + setg(eback(), next, reinterpret_cast(reader_.get_total_size())); + return static_cast( + reinterpret_cast(gptr())); + } + + return {traits_type::eof()}; + }; + + switch (dir) { + case std::ios_base::beg: + return set_position(eback() + off); + + case std::ios_base::cur: + return set_position(gptr() + off); + + case std::ios_base::end: + return set_position(egptr() + off); + } + + return encrypting_reader::streambuf::seekoff(off, dir, which); + } + + auto seekpos(pos_type pos, std::ios_base::openmode which = + std::ios_base::out | + std::ios_base::in) -> pos_type override { + return seekoff(pos, std::ios_base::beg, which); + } + + auto uflow() -> int_type override { + auto ret = underflow(); + if (ret == traits_type::eof()) { + return ret; + } + + gbump(1); + + return ret; + } + + auto underflow() -> int_type override { + if (gptr() == egptr()) { + return traits_type::eof(); + } + + reader_.set_read_position(reinterpret_cast(gptr())); + + char c{}; + const auto res = encrypting_reader::reader_function(&c, 1U, 1U, &reader_); + if (res != 1) { + return traits_type::eof(); + } + + return c; + } + + auto xsgetn(char *ptr, std::streamsize count) -> std::streamsize override { + if (gptr() == egptr()) { + return traits_type::eof(); + } + + reader_.set_read_position(reinterpret_cast(gptr())); + + auto res = encrypting_reader::reader_function( + ptr, 1U, static_cast(count), &reader_); + if ((res == reader_.get_error_return()) || + (reader_.get_stop_requested() && + (res == static_cast(CURL_READFUNC_ABORT)))) { + return traits_type::eof(); + } + + setg(eback(), gptr() + res, + reinterpret_cast(reader_.get_total_size())); + return static_cast(res); + } +}; + +class encrypting_reader_iostream final : public encrypting_reader::iostream { +public: + encrypting_reader_iostream(const encrypting_reader_iostream &) = delete; + encrypting_reader_iostream(encrypting_reader_iostream &&) = delete; + + auto operator=(const encrypting_reader_iostream &) + -> encrypting_reader_iostream & = delete; + auto operator=(encrypting_reader_iostream &&) + -> encrypting_reader_iostream & = delete; + + explicit encrypting_reader_iostream( + std::unique_ptr buffer) + : encrypting_reader::iostream(buffer.get()), buffer_(std::move(buffer)) {} + + ~encrypting_reader_iostream() override = default; + +private: + std::unique_ptr buffer_; +}; + +const std::size_t encrypting_reader::header_size_ = ([]() { + return crypto_aead_xchacha20poly1305_IETF_NPUBBYTES + + crypto_aead_xchacha20poly1305_IETF_ABYTES; +})(); +const std::size_t encrypting_reader::data_chunk_size_ = (8UL * 1024UL * 1024UL); +const std::size_t encrypting_reader::encrypted_chunk_size_ = + data_chunk_size_ + header_size_; + +encrypting_reader::encrypting_reader( + std::string_view file_name, std::string_view source_path, + stop_type &stop_requested, std::string_view token, + std::optional relative_parent_path, std::size_t error_return) + : key_(utils::encryption::generate_key( + token)), + stop_requested_(stop_requested), + error_return_(error_return), + source_file_(utils::file::file::open_or_create_file(source_path, true)) { + if (not *source_file_) { + throw std::runtime_error("file open failed|src|" + + std::string{source_path}); + } + + data_buffer result; + utils::encryption::encrypt_data( + key_, reinterpret_cast(file_name.data()), + file_name.size(), result); + encrypted_file_name_ = utils::collection::to_hex_string(result); + + if (relative_parent_path.has_value()) { + for (auto &&part : + utils::string::split(relative_parent_path.value(), + utils::path::directory_seperator, false)) { + utils::encryption::encrypt_data( + key_, reinterpret_cast(part.c_str()), + strnlen(part.c_str(), part.size()), result); + encrypted_file_path_ += '/' + utils::collection::to_hex_string(result); + } + encrypted_file_path_ += '/' + encrypted_file_name_; + } + + auto opt_size = source_file_->size(); + if (not opt_size.has_value()) { + throw std::runtime_error("failed to get file size|" + + source_file_->get_path()); + } + auto file_size = opt_size.value(); + + const auto total_chunks = utils::divide_with_ceiling( + file_size, static_cast(data_chunk_size_)); + total_size_ = file_size + (total_chunks * encryption_header_size); + last_data_chunk_ = total_chunks - 1U; + last_data_chunk_size_ = (file_size <= data_chunk_size_) ? file_size + : (file_size % data_chunk_size_) == 0U + ? data_chunk_size_ + : file_size % data_chunk_size_; + iv_list_.resize(total_chunks); + for (auto &iv : iv_list_) { + randombytes_buf(iv.data(), iv.size()); + } +} + +encrypting_reader::encrypting_reader(std::string_view encrypted_file_path, + std::string_view source_path, + stop_type &stop_requested, + std::string_view token, + std::size_t error_return) + : key_(utils::encryption::generate_key( + token)), + stop_requested_(stop_requested), + error_return_(error_return), + source_file_(utils::file::file::open_or_create_file(source_path, true)) { + if (not *source_file_) { + throw std::runtime_error("file open failed|src|" + + std::string{source_path}); + } + + encrypted_file_path_ = encrypted_file_path; + encrypted_file_name_ = utils::path::strip_to_file_name(encrypted_file_path_); + + auto opt_size = source_file_->size(); + if (not opt_size.has_value()) { + throw std::runtime_error("failed to get file size|" + + source_file_->get_path()); + } + auto file_size = opt_size.value(); + + const auto total_chunks = utils::divide_with_ceiling( + file_size, static_cast(data_chunk_size_)); + total_size_ = file_size + (total_chunks * encryption_header_size); + last_data_chunk_ = total_chunks - 1U; + last_data_chunk_size_ = (file_size <= data_chunk_size_) ? file_size + : (file_size % data_chunk_size_) == 0U + ? data_chunk_size_ + : file_size % data_chunk_size_; + iv_list_.resize(total_chunks); + for (auto &iv : iv_list_) { + randombytes_buf(iv.data(), iv.size()); + } +} + +encrypting_reader::encrypting_reader( + std::string_view encrypted_file_path, std::string_view source_path, + stop_type &stop_requested, std::string_view token, + std::vector< + std::array> + iv_list, + std::size_t error_return) + : key_(utils::encryption::generate_key( + token)), + stop_requested_(stop_requested), + error_return_(error_return), + source_file_(utils::file::file::open_or_create_file(source_path, true)) { + if (not *source_file_) { + throw std::runtime_error("file open failed|src|" + + std::string{source_path}); + } + + encrypted_file_path_ = encrypted_file_path; + encrypted_file_name_ = utils::path::strip_to_file_name(encrypted_file_path_); + + auto opt_size = source_file_->size(); + if (not opt_size.has_value()) { + throw std::runtime_error("get file size failed|src|" + + source_file_->get_path() + '|' + + std::to_string(utils::get_last_error_code())); + } + auto file_size{opt_size.value()}; + + const auto total_chunks = utils::divide_with_ceiling( + file_size, static_cast(data_chunk_size_)); + total_size_ = file_size + (total_chunks * encryption_header_size); + last_data_chunk_ = total_chunks - 1U; + last_data_chunk_size_ = (file_size <= data_chunk_size_) ? file_size + : (file_size % data_chunk_size_) == 0U + ? data_chunk_size_ + : file_size % data_chunk_size_; + iv_list_ = std::move(iv_list); +} + +encrypting_reader::encrypting_reader(const encrypting_reader &reader) + : key_(reader.key_), + stop_requested_(reader.stop_requested_), + error_return_(reader.error_return_), + source_file_( + utils::file::file::open_file(reader.source_file_->get_path(), true)), + chunk_buffers_(reader.chunk_buffers_), + encrypted_file_name_(reader.encrypted_file_name_), + encrypted_file_path_(reader.encrypted_file_path_), + iv_list_(reader.iv_list_), + last_data_chunk_(reader.last_data_chunk_), + last_data_chunk_size_(reader.last_data_chunk_size_), + read_offset_(reader.read_offset_), + total_size_(reader.total_size_) { + if (not *source_file_) { + throw std::runtime_error("file open failed|src|" + + source_file_->get_path()); + } +} + +auto encrypting_reader::calculate_decrypted_size(std::uint64_t total_size) + -> std::uint64_t { + return total_size - (utils::divide_with_ceiling( + total_size, static_cast( + get_encrypted_chunk_size())) * + encryption_header_size); +} + +auto encrypting_reader::calculate_encrypted_size(std::string_view source_path) + -> std::uint64_t { + auto opt_size = utils::file::file{source_path}.size(); + if (not opt_size.has_value()) { + throw std::runtime_error("get file size failed|src|" + + std::string{source_path} + '|' + + std::to_string(utils::get_last_error_code())); + } + auto file_size{opt_size.value()}; + + const auto total_chunks = utils::divide_with_ceiling( + file_size, static_cast(data_chunk_size_)); + return file_size + (total_chunks * encryption_header_size); +} + +auto encrypting_reader::create_iostream() const + -> std::shared_ptr { + return std::make_shared( + std::make_unique(*this)); +} + +auto encrypting_reader::reader_function(char *buffer, size_t size, + size_t nitems) -> size_t { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + const auto read_size = static_cast(std::min( + static_cast(size * nitems), total_size_ - read_offset_)); + + auto chunk = read_offset_ / encrypted_chunk_size_; + auto chunk_offset = read_offset_ % encrypted_chunk_size_; + std::size_t total_read{}; + + auto ret = false; + if (read_offset_ < total_size_) { + try { + ret = true; + auto remain = read_size; + while (not stop_requested_ && ret && (remain != 0U)) { + if (chunk_buffers_.find(chunk) == chunk_buffers_.end()) { + auto &chunk_buffer = chunk_buffers_[chunk]; + data_buffer file_data(chunk == last_data_chunk_ + ? last_data_chunk_size_ + : data_chunk_size_); + chunk_buffer.resize(file_data.size() + encryption_header_size); + + std::size_t bytes_read{}; + if ((ret = source_file_->read(file_data, chunk * data_chunk_size_, + &bytes_read))) { + utils::encryption::encrypt_data(iv_list_.at(chunk), key_, file_data, + chunk_buffer); + } + } else if (chunk) { + chunk_buffers_.erase(chunk - 1u); + } + + auto &chunk_buffer = chunk_buffers_[chunk]; + const auto to_read = std::min( + static_cast(chunk_buffer.size() - chunk_offset), + remain); + std::memcpy(buffer + total_read, &chunk_buffer[chunk_offset], to_read); + total_read += to_read; + remain -= to_read; + chunk_offset = 0u; + chunk++; + read_offset_ += to_read; + } + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + ret = false; + } catch (...) { + utils::error::handle_exception(function_name); + ret = false; + } + } + + return stop_requested_ ? static_cast(CURL_READFUNC_ABORT) + : ret ? total_read + : error_return_; +} +} // namespace repertory::utils::encryption + +#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) diff --git a/support/src/utils/encryption.cpp b/support/src/utils/encryption.cpp index cb146c69..d5c0be90 100644 --- a/support/src/utils/encryption.cpp +++ b/support/src/utils/encryption.cpp @@ -21,34 +21,97 @@ */ #include "utils/encryption.hpp" -#if defined(PROJECT_ENABLE_LIBSODIUM) -namespace repertory::utils::encryption { -#if defined(PROJECT_ENABLE_BOOST) -auto decrypt_data(std::string_view password, - std::string_view data) -> data_buffer { - auto key = generate_key(password); +#include "utils/collection.hpp" +#include "utils/encrypting_reader.hpp" - data_buffer buf{}; - if (not decrypt_data(key, - reinterpret_cast(data.data()), - data.size(), buf)) { - throw std::runtime_error("decryption failed"); +#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) + +namespace repertory::utils::encryption { +auto decrypt_file_path(std::string_view encryption_token, + std::string &file_path) -> bool { + std::string decrypted_file_path{}; + for (const auto &part : std::filesystem::path(file_path)) { + auto file_name = part.string(); + if (file_name == "/") { + continue; + } + + auto res = decrypt_file_name(encryption_token, file_name); + if (not res) { + return res; + } + + decrypted_file_path += '/' + file_name; } - return buf; + file_path = decrypted_file_path; + return true; } -auto encrypt_data(std::string_view password, - std::string_view data) -> data_buffer { - auto key = generate_key(password); +auto decrypt_file_name(std::string_view encryption_token, + std::string &file_name) -> bool { + data_buffer buffer; + if (not utils::collection::from_hex_string(file_name, buffer)) { + return false; + } - data_buffer buf{}; - encrypt_data(key, reinterpret_cast(data.data()), - data.size(), buf); + file_name.clear(); + if (not utils::encryption::decrypt_data(encryption_token, buffer, + file_name)) { + return false; + } - return buf; + return true; } -#endif // defined(PROJECT_ENABLE_BOOST) + +#if defined(PROJECT_ENABLE_CURL) +auto read_encrypted_range(const http_range &range, + const utils::encryption::hash_256_t &key, + reader_func_t reader_func, std::uint64_t total_size, + data_buffer &data) -> bool { + const auto encrypted_chunk_size = + utils::encryption::encrypting_reader::get_encrypted_chunk_size(); + const auto data_chunk_size = + utils::encryption::encrypting_reader::get_data_chunk_size(); + + const auto start_chunk = + static_cast(range.begin / data_chunk_size); + const auto end_chunk = static_cast(range.end / data_chunk_size); + auto remain = range.end - range.begin + 1U; + auto source_offset = static_cast(range.begin % data_chunk_size); + + for (std::size_t chunk = start_chunk; chunk <= end_chunk; chunk++) { + data_buffer cypher; + const auto start_offset = chunk * encrypted_chunk_size; + const auto end_offset = std::min( + start_offset + (total_size - (chunk * data_chunk_size)) + + encryption_header_size - 1U, + static_cast(start_offset + encrypted_chunk_size - 1U)); + + if (not reader_func(cypher, start_offset, end_offset)) { + return false; + } + + data_buffer source_buffer; + if (not utils::encryption::decrypt_data(key, cypher, source_buffer)) { + return false; + } + cypher.clear(); + + const auto data_size = static_cast(std::min( + remain, static_cast(data_chunk_size - source_offset))); + std::copy(std::next(source_buffer.begin(), + static_cast(source_offset)), + std::next(source_buffer.begin(), + static_cast(source_offset + data_size)), + std::back_inserter(data)); + remain -= data_size; + source_offset = 0U; + } + + return true; +} +#endif // defined(PROJECT_ENABLE_CURL) } // namespace repertory::utils::encryption -#endif // defined(PROJECT_ENABLE_LIBSODIUM) +#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined (PROJECT_ENABLE_BOOST) diff --git a/support/src/utils/error.cpp b/support/src/utils/error.cpp index 6b42fa79..095e8945 100644 --- a/support/src/utils/error.cpp +++ b/support/src/utils/error.cpp @@ -21,51 +21,32 @@ */ #include "utils/error.hpp" -namespace { -struct default_exception_handler final - : repertory::utils::error::i_exception_handler { - void handle_exception(std::string_view function_name) const override { - std::cerr << function_name << "|exception|unknown" << std::endl; - } - - void handle_exception(std::string_view function_name, - const std::exception &ex) const override { - std::cerr << function_name << "|exception|" - << (ex.what() == nullptr ? "unknown" : ex.what()) << std::endl; - } -}; - -static default_exception_handler default_handler{}; - -static std::atomic - exception_handler{ - &default_handler, - }; -} // namespace - namespace repertory::utils::error { +std::atomic exception_handler{ + &default_exception_handler}; + void handle_exception(std::string_view function_name) { - i_exception_handler *handler{exception_handler}; + const i_exception_handler *handler{exception_handler}; if (handler != nullptr) { handler->handle_exception(function_name); return; } - default_handler.handle_exception(function_name); + default_exception_handler.handle_exception(function_name); } void handle_exception(std::string_view function_name, const std::exception &ex) { - i_exception_handler *handler{exception_handler}; + const i_exception_handler *handler{exception_handler}; if (handler != nullptr) { handler->handle_exception(function_name, ex); return; } - default_handler.handle_exception(function_name, ex); + default_exception_handler.handle_exception(function_name, ex); } -void set_exception_handler(i_exception_handler *handler) { +void set_exception_handler(const i_exception_handler *handler) { exception_handler = handler; } } // namespace repertory::utils::error diff --git a/support/src/utils/file.cpp b/support/src/utils/file.cpp index 1657e842..e39ccb35 100644 --- a/support/src/utils/file.cpp +++ b/support/src/utils/file.cpp @@ -21,227 +21,309 @@ */ #include "utils/file.hpp" +#include "utils/common.hpp" #include "utils/encryption.hpp" #include "utils/error.hpp" #include "utils/path.hpp" #include "utils/string.hpp" +#include "utils/time.hpp" +#include "utils/unix.hpp" +#include "utils/windows.hpp" namespace repertory::utils::file { -auto file::open_file(std::filesystem::path path) -> file { - path = utils::path::absolute(path.string()); - if (not is_file(path.string())) { - throw std::runtime_error("file not found: " + path.string()); - } - - auto stream = std::fstream{ - path, - std::ios_base::binary | std::ios_base::in | std::ios_base::out, - }; - return { - std::move(stream), - path, - }; -} - -auto file::open_or_create_file(std::filesystem::path path) -> file { - path = utils::path::absolute(path.string()); - auto stream = std::fstream{ - path.string().c_str(), - std::ios_base::binary | std::ios_base::trunc | std::ios_base::in | - std::ios_base::out, - }; - - return { - std::move(stream), - path, - }; -} - -void file::close() { - if (stream_.is_open()) { - stream_.close(); - } -} - -auto file::move_to(std::filesystem::path new_path) -> bool { - new_path = utils::path::absolute(new_path.string()); - - auto reopen{false}; - if (stream_.is_open()) { - reopen = true; - close(); - } - - std::filesystem::rename(path_, new_path, error_); - if (not error_) { - path_ = new_path; - if (reopen) { - *this = open_file(path_); - } - return true; - } - - if (reopen) { - *this = open_file(path_); - } - return false; -} - -auto file::read_(unsigned char *data, std::size_t to_read, std::uint64_t offset, - std::size_t *total_read) -> bool { +auto change_to_process_directory() -> bool { static constexpr const std::string_view function_name{ static_cast(__FUNCTION__), }; - if (total_read != nullptr) { - (*total_read) = 0U; + try { +#if defined(_WIN32) + std::string file_name; + file_name.resize(MAX_PATH + 1U); + + ::GetModuleFileNameA(nullptr, file_name.data(), + static_cast(file_name.size() - 1U)); + auto path = utils::path::strip_to_file_name(file_name.c_str()); + ::SetCurrentDirectoryA(path.c_str()); +#else // !defined(_WIN32) + std::string path; + path.resize(PATH_MAX + 1); +#if defined(__APPLE__) + proc_pidpath(getpid(), path.c_str(), path.size()); +#else // !defined(__APPLE__) + auto res = readlink("/proc/self/exe", path.data(), path.size()); + if (res == -1) { + throw std::runtime_error("failed to readlink|" + path + '|' + + std::to_string(errno)); + } +#endif // defined(__APPLE__) + path = utils::path::get_parent_path(path); + res = chdir(path.c_str()); + if (res != 0) { + throw std::runtime_error("failed to chdir|" + path + '|' + + std::to_string(errno)); + } +#endif // defined(_WIN32) + + return true; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); } - try { - stream_.seekg(static_cast(offset)); + return false; +} - auto before = stream_.tellg(); - if (before == -1) { - throw std::runtime_error("failed to tellg() before read"); +auto create_temp_name(std::string_view file_part) -> std::string { + std::array data{ + utils::generate_random_between(0U, 9U), + utils::generate_random_between(0U, 9U), + utils::generate_random_between(0U, 9U), + utils::generate_random_between(0U, 9U), + utils::generate_random_between(0U, 9U), + utils::generate_random_between(0U, 9U), + utils::generate_random_between(0U, 9U), + utils::generate_random_between(0U, 9U), + }; + + return std::accumulate(data.begin(), data.end(), std::string{file_part} + '_', + [](auto &&name, auto &&val) -> auto { + return name + std::to_string(val); + }); +} + +auto create_temp_name(std::wstring_view file_part) -> std::wstring { + return utils::string::from_utf8( + create_temp_name(utils::string::to_utf8(file_part))); +} + +auto get_free_drive_space(std::string_view path) + -> std::optional { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { +#if defined(_WIN32) + ULARGE_INTEGER li{}; + if (not ::GetDiskFreeSpaceEx(std::string{path}.c_str(), &li, nullptr, + nullptr)) { + throw std::runtime_error("failed to get free disk space|" + + std::string{path} + '|' + + std::to_string(utils::get_last_error_code())); } - stream_.read(reinterpret_cast(data), - static_cast(to_read)); + return li.QuadPart; +#endif // defined(_WIN32) + +#if defined(__linux__) + struct statfs64 st {}; + if (statfs64(std::string{path}.c_str(), &st) != 0) { + throw std::runtime_error("failed to get free disk space|" + + std::string{path} + '|' + + std::to_string(utils::get_last_error_code())); + } + + return st.f_bfree * static_cast(st.f_bsize); +#endif // defined(__linux__) + +#if defined(__APPLE__) + struct statvfs st {}; + if (statvfs(path.c_str(), &st) != 0) { + throw std::runtime_error("failed to get free disk space|" + + std::string{path} + '|' + + std::to_string(utils::get_last_error_code())); + } + + return st.f_bfree * static_cast(st.f_frsize); +#endif // defined(__APPLE__) + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return std::nullopt; +} + +auto get_free_drive_space(std::wstring_view path) + -> std::optional { + return get_free_drive_space(utils::string::to_utf8(path)); +} + +auto get_time(std::string_view path, + time_type type) -> std::optional { + auto times = get_times(path); + if (times.has_value()) { + return times->get(type); + } + + return std::nullopt; +} + +auto get_time(std::wstring_view path, + time_type type) -> std::optional { + return get_time(utils::string::to_utf8(path), type); +} + +auto get_times(std::string_view path) -> std::optional { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + file_times ret{}; + +#if defined(_WIN32) + auto file_handle = + ::CreateFileA(std::string{path}.c_str(), GENERIC_READ, + FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, + nullptr, OPEN_EXISTING, 0U, nullptr); + if (file_handle == INVALID_HANDLE_VALUE) { + throw std::runtime_error("failed to get file times|" + std::string{path} + + '|' + + std::to_string(utils::get_last_error_code())); + } + + std::array times{}; + auto res = + ::GetFileTime(file_handle, ×.at(0U), ×.at(1U), ×.at(2U)); + ::CloseHandle(file_handle); + + if (not res) { + throw std::runtime_error("failed to get file times|" + std::string{path} + + '|' + + std::to_string(utils::get_last_error_code())); + } + + ret.accessed = utils::time::windows_file_time_to_unix_time(times.at(1U)); + ret.created = utils::time::windows_file_time_to_unix_time(times.at(0U)); + ret.modified = utils::time::windows_file_time_to_unix_time(times.at(2U)); + ret.written = utils::time::windows_file_time_to_unix_time(times.at(2U)); +#else // !defined(_WIN32) + struct stat64 st {}; + if (stat64(std::string{path}.c_str(), &st) != 0) { + throw std::runtime_error("failed to get file times|" + std::string{path} + + '|' + std::to_string(errno)); + } + + ret.accessed = static_cast(st.st_atim.tv_nsec) + + static_cast(st.st_atim.tv_sec) * + utils::time::NANOS_PER_SECOND; + ret.created = static_cast(st.st_ctim.tv_nsec) + + static_cast(st.st_ctim.tv_sec) * + utils::time::NANOS_PER_SECOND; + ret.modified = static_cast(st.st_mtim.tv_nsec) + + static_cast(st.st_mtim.tv_sec) * + utils::time::NANOS_PER_SECOND; + ret.written = static_cast(st.st_mtim.tv_nsec) + + static_cast(st.st_mtim.tv_sec) * + utils::time::NANOS_PER_SECOND; +#endif // defined(_WIN32) + + return ret; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return std::nullopt; +} + +auto get_times(std::wstring_view path) -> std::optional { + return get_times(utils::string::to_utf8(path)); +} + +auto get_total_drive_space(std::string_view path) + -> std::optional { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { +#if defined(_WIN32) + ULARGE_INTEGER li{}; + if (not ::GetDiskFreeSpaceEx(std::string{path}.c_str(), nullptr, &li, + nullptr)) { + throw std::runtime_error("failed to get total disk space|" + + std::string{path} + '|' + + std::to_string(utils::get_last_error_code())); + } + + return li.QuadPart; +#endif // defined(_WIN32) + +#if defined(__linux__) + struct statfs64 st {}; + if (statfs64(std::string{path}.c_str(), &st) != 0) { + throw std::runtime_error("failed to get total disk space|" + + std::string{path} + '|' + + std::to_string(utils::get_last_error_code())); + } + + return st.f_blocks * static_cast(st.f_bsize); +#endif // defined(__linux__) + +#if defined(__APPLE__) + struct statvfs st {}; + if (statvfs(path.c_str(), &st) != 0) { + throw std::runtime_error("failed to get total disk space|" + + std::string{path} + '|' + + std::to_string(utils::get_last_error_code())); + } + + return st.f_blocks * static_cast(st.f_frsize); +#endif // defined(__APPLE__) + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return std::nullopt; +} + +auto get_total_drive_space(std::wstring_view path) + -> std::optional { + return get_total_drive_space(utils::string::to_utf8(path)); +} + +auto i_fs_item::get_time(time_type type) const -> std::optional { + return utils::file::get_time(get_path(), type); +} + +auto i_file::read_all(data_buffer &data, std::uint64_t offset, + std::size_t *total_read) -> bool { + data_buffer buffer; + buffer.resize(get_read_buffer_size()); + + std::size_t current_read{}; + while (read(reinterpret_cast(buffer.data()), + buffer.size() * sizeof(data_buffer::value_type), offset, + ¤t_read)) { if (total_read != nullptr) { - auto after = stream_.tellg(); - if (after >= 0) { - (*total_read) = static_cast(after - before); - } else if (after == -1 && not stream_.eof()) { - throw std::runtime_error("failed to tellg() after read"); - } + *total_read += current_read; + } + + if (current_read != 0U) { + offset += current_read; + + data.insert( + data.end(), buffer.begin(), + std::next(buffer.begin(), + static_cast( + current_read / sizeof(data_buffer::value_type)))); + continue; } return true; - } catch (const std::exception &e) { - utils::error::handle_exception(function_name, e); - } catch (...) { - utils::error::handle_exception(function_name); } return false; } -auto file::remove() -> bool { - close(); - return std::filesystem::remove(path_, error_); -} - -auto file::truncate(std::size_t size) -> bool { - auto reopen{false}; - if (stream_.is_open()) { - reopen = true; - close(); - } - - std::filesystem::resize_file(path_, size, error_); - if (reopen) { - *this = open_file(path_); - } - - return not error_; -} - -auto file::write_(const unsigned char *data, std::size_t to_write, - std::size_t offset, std::size_t *total_written) -> bool { - static constexpr const std::string_view function_name{ - static_cast(__FUNCTION__), - }; - - if (total_written != nullptr) { - (*total_written) = 0U; - } - - try { - stream_.seekp(static_cast(offset)); - auto before = stream_.tellp(); - if (before == -1) { - throw std::runtime_error("failed to tellp() before write"); - } - - stream_.write(reinterpret_cast(data), - static_cast(to_write)); - - auto after = stream_.tellp(); - if (after == -1) { - throw std::runtime_error("failed to tellp() after write"); - } - - if (total_written != nullptr) { - (*total_written) = static_cast(after - before); - } - - return true; - } catch (const std::exception &e) { - utils::error::handle_exception(function_name, e); - } catch (...) { - utils::error::handle_exception(function_name); - } - - return false; -} - -auto get_file_size(std::string_view path, std::uint64_t &file_size) -> bool { - auto abs_path = utils::path::absolute(path); - - file_size = 0U; - -#if defined(_WIN32) - struct _stat64 st {}; - if (_stat64(abs_path.c_str(), &st) != 0) { -#else // !defined(_WIN32) - struct stat st {}; - if (stat(abs_path.c_str(), &st) != 0) { -#endif // defined(_WIN32) - return false; - } - - if (st.st_size >= 0) { - file_size = static_cast(st.st_size); - } - - return (st.st_size >= 0); -} - -auto get_file_size(std::wstring_view path, std::uint64_t &file_size) -> bool { - return get_file_size(utils::string::to_utf8(path), file_size); -} - -auto is_directory(std::string_view path) -> bool { - auto abs_path = utils::path::absolute(path); - -#if defined(_WIN32) - return ::PathIsDirectoryA(abs_path.c_str()) != 0; -#else // !defined(_WIN32) - struct stat st {}; - return (stat(abs_path.c_str(), &st) == 0 && S_ISDIR(st.st_mode)); -#endif // defined(_WIN32) -} - -auto is_directory(std::wstring_view path) -> bool { - return is_directory(utils::string::to_utf8(path)); -} - -auto is_file(std::string_view path) -> bool { - auto abs_path = utils::path::absolute(path); - -#if defined(_WIN32) - return (::PathFileExistsA(abs_path.c_str()) && - not ::PathIsDirectoryA(abs_path.c_str())); -#else // !defined(_WIN32) - struct stat st {}; - return (stat(abs_path.c_str(), &st) == 0 && not S_ISDIR(st.st_mode)); -#endif // defined(_WIN32) -} - -auto is_file(std::wstring_view path) -> bool { - return is_file(utils::string::to_utf8(path)); -} - #if defined(PROJECT_ENABLE_JSON) #if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) auto read_json_file(std::string_view path, nlohmann::json &data, @@ -255,44 +337,42 @@ auto read_json_file(std::string_view path, nlohmann::json &data) -> bool { try { auto abs_path = utils::path::absolute(path); - if (not is_file(abs_path)) { - return true; + auto file = file::open_file(abs_path); + if (not *file) { + return false; } - std::ifstream file_stream{ - abs_path.c_str(), - std::ios_base::binary | std::ios::in, - }; - if (not file_stream.is_open()) { - throw std::runtime_error("failed to open file: " + abs_path); - } - - auto ret{true}; try { - std::stringstream stream; - stream << file_stream.rdbuf(); + data_buffer buffer{}; + if (not file->read_all(buffer, 0U)) { + return false; + } - auto json_text = stream.str(); #if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) if (password.has_value()) { - auto decrypted_data = - utils::encryption::decrypt_data(*password, json_text); - json_text = {decrypted_data.begin(), decrypted_data.end()}; + data_buffer decrypted_data{}; + if (not utils::encryption::decrypt_data(*password, buffer, + decrypted_data)) { + return false; + } + + buffer = decrypted_data; } #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) - if (not json_text.empty()) { - data = nlohmann::json::parse(json_text.c_str()); + + std::string json_str(buffer.begin(), buffer.end()); + if (not json_str.empty()) { + data = nlohmann::json::parse(json_str); } } catch (const std::exception &e) { utils::error::handle_exception(function_name, e); - ret = false; + return false; } catch (...) { utils::error::handle_exception(function_name); - ret = false; + return false; } - file_stream.close(); - return ret; + return true; } catch (const std::exception &e) { utils::error::handle_exception(function_name, e); } catch (...) { @@ -315,19 +395,26 @@ auto write_json_file(std::string_view path, try { auto file = file::open_or_create_file(path); - if (not file.truncate()) { - throw std::runtime_error("failed to truncate file: " + - file.get_error_code().message()); + if (not file->truncate()) { + throw std::runtime_error("failed to truncate file"); } #if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) if (password.has_value()) { - return file.write(utils::encryption::encrypt_data(*password, data.dump()), - 0U); + const auto str_data = data.dump(); + + data_buffer encrypted_data{}; + utils::encryption::encrypt_data( + *password, reinterpret_cast(str_data.c_str()), + str_data.size(), encrypted_data); + return file->write(encrypted_data, 0U); } #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) - return file.write(data, 0U); + auto json_str = data.dump(); + return file->write( + reinterpret_cast(json_str.c_str()), + json_str.size(), 0U); } catch (const std::exception &e) { utils::error::handle_exception(function_name, e); } catch (...) { @@ -368,4 +455,170 @@ auto write_json_file(std::wstring_view path, } #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) #endif // defined(PROJECT_ENABLE_JSON) + +#if defined(PROJECT_ENABLE_LIBDSM) +static constexpr const auto validate_smb_path = + [](std::string_view path) -> bool { + return (not utils::string::begins_with(path, "///") && + utils::string::begins_with(path, "//") && + not utils::string::contains(path, " ") && + std::count(path.begin(), path.end(), '/') >= 3U); +}; + +auto smb_create_smb_path(std::string_view smb_path, + std::string_view rel_path) -> std::string { + if (not validate_smb_path(smb_path)) { + throw std::runtime_error("invalid smb path|" + std::string{smb_path}); + } + + std::string path{rel_path}; + utils::path::format_path(path, "/", "\\"); + utils::string::left_trim(path, '/'); + + auto old_parts = + repertory::utils::string::split(smb_path.substr(2U), '/', false); + old_parts.erase(std::next(old_parts.begin(), 2U), old_parts.end()); + + auto new_parts = repertory::utils::string::split(path, '/', false); + old_parts.insert(old_parts.end(), new_parts.begin(), new_parts.end()); + + path = utils::string::join(old_parts, '/'); + path = "//" + utils::path::format_path(path, "/", "\\"); + + if (not validate_smb_path(path)) { + throw std::runtime_error("invalid smb path|" + std::string{path}); + } + + return path; +} + +auto smb_create_and_validate_relative_path( + std::string_view smb_path, std::string_view path) -> std::string { + if (not validate_smb_path(smb_path)) { + throw std::runtime_error("invalid smb path|" + std::string{smb_path}); + } + + std::string dir_path; + if (utils::string::begins_with(path, "//")) { + if (not utils::file::smb_parent_is_same(smb_path, path)) { + throw std::runtime_error("failed to validate path|" + + std::string{smb_path} + '|' + std::string{path} + + "|parent paths are not the same"); + } + + return utils::file::smb_create_relative_path(path); + } + + return utils::file::smb_create_relative_path(std::string{smb_path} + '/' + + std::string{path}); +} + +auto smb_create_relative_path(std::string_view smb_path) -> std::string { + if (not validate_smb_path(smb_path)) { + throw std::runtime_error("invalid smb path|" + std::string{smb_path}); + } + + std::string path{smb_path}; + utils::path::format_path(path, "\\", "/"); + utils::string::left_trim(path, '\\'); + + auto parts = repertory::utils::string::split(path, '\\', false); + parts.erase(parts.begin(), std::next(parts.begin(), 2U)); + return "\\" + utils::string::join(parts, '\\'); +} + +auto smb_create_search_path(std::string_view smb_path) -> std::string { + if (not validate_smb_path(smb_path)) { + throw std::runtime_error("invalid smb path|" + std::string{smb_path}); + } + + std::string path{smb_path}; + utils::string::left_trim(path, '/'); + + auto parts = repertory::utils::string::split(path, '/', false); + parts.erase(parts.begin(), std::next(parts.begin(), 2U)); + + auto search_path = repertory::utils::string::join(parts, '\\'); + return search_path.empty() ? "\\*" : "\\" + search_path + "\\*"; +} + +auto smb_get_parent_path(std::string_view smb_path) -> std::string { + if (not validate_smb_path(smb_path)) { + throw std::runtime_error("invalid smb path|" + std::string{smb_path}); + } + + auto parts = repertory::utils::string::split(smb_path.substr(2U), '/', false); + if (parts.size() > 2U) { + parts.erase(std::prev(parts.end()), parts.end()); + } + + auto parent_smb_path = "//" + utils::string::join(parts, '/'); + if (not validate_smb_path(parent_smb_path)) { + throw std::runtime_error("invalid smb path|" + parent_smb_path); + } + + return parent_smb_path; +} + +auto smb_get_root_path(std::string_view smb_path) -> std::string { + if (not validate_smb_path(smb_path)) { + throw std::runtime_error("invalid smb path|" + std::string{smb_path}); + } + + auto parts = repertory::utils::string::split(smb_path.substr(2U), '/', false); + if (parts.size() > 2U) { + parts.erase(std::next(parts.begin(), 2U), parts.end()); + } + + return "//" + utils::string::join(parts, '/'); +} + +auto smb_get_unc_path(std::string_view smb_path) -> std::string { + if (not validate_smb_path(smb_path)) { + throw std::runtime_error("invalid smb path|" + std::string{smb_path}); + } + + std::string unc_path{smb_path}; + utils::path::format_path(unc_path, "\\", "/"); + return '\\' + unc_path; +} + +auto smb_get_uri_path(std::string_view smb_path) -> std::string { + if (not validate_smb_path(smb_path)) { + throw std::runtime_error("invalid smb path|" + std::string{smb_path}); + } + + return "smb:" + std::string{smb_path}; +} + +auto smb_get_uri_path(std::string_view smb_path, std::string_view user, + std::string_view password) -> std::string { + if (not validate_smb_path(smb_path)) { + throw std::runtime_error("invalid smb path|" + std::string{smb_path}); + } + + return "smb://" + std::string{user} + ':' + std::string{password} + '@' + + std::string{smb_path.substr(2U)}; +} + +auto smb_parent_is_same(std::string_view smb_path1, + std::string_view smb_path2) -> bool { + if (not(validate_smb_path(smb_path1) && validate_smb_path(smb_path2))) { + return false; + } + + auto parts1 = utils::string::split(smb_path1.substr(2U), "/", false); + auto parts2 = utils::string::split(smb_path2.substr(2U), "/", false); + if (parts1.size() < 2U || parts2.size() < 2U) { + return false; + } + + if (parts2.at(1U).empty() || parts1.at(1U).empty()) { + return false; + } + + return std::equal(parts1.begin(), std::next(parts1.begin(), 2U), + parts2.begin()); +} +#endif // defined(PROJECT_ENABLE_LIBDSM) } // namespace repertory::utils::file diff --git a/support/src/utils/file_directory.cpp b/support/src/utils/file_directory.cpp new file mode 100644 index 00000000..a62a989b --- /dev/null +++ b/support/src/utils/file_directory.cpp @@ -0,0 +1,466 @@ +/* + Copyright <2018-2024> + + 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. +*/ +#include "utils/file.hpp" + +#include "utils/common.hpp" +#include "utils/error.hpp" +#include "utils/unix.hpp" +#include "utils/windows.hpp" + +namespace { +auto traverse_directory( + std::string_view path, + std::function directory_action, + std::function file_action) -> bool { + auto res{true}; + +#if defined(_WIN32) + WIN32_FIND_DATAA fd{}; + auto search = repertory::utils::path::combine(path, {"*.*"}); + auto find = ::FindFirstFileA(search.c_str(), &fd); + if (find == INVALID_HANDLE_VALUE) { + throw std::runtime_error( + "failed to open directory|" + std::string{path} + '|' + + std::to_string(repertory::utils::get_last_error_code())); + } + + do { + if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + if ((std::string_view(fd.cFileName) != ".") && + (std::string_view(fd.cFileName) != "..")) { + res = directory_action(repertory::utils::file::directory{ + repertory::utils::path::combine(path, {fd.cFileName})}); + } + } else { + res = file_action(repertory::utils::file::file( + repertory::utils::path::combine(path, {fd.cFileName}))); + } + } while (res && (::FindNextFileA(find, &fd) != 0)); + + ::FindClose(find); +#else // !defined(_WIN32) + auto *root = opendir(std::string{path}.c_str()); + if (root == nullptr) { + throw std::runtime_error( + "failed to open directory|" + std::string{path} + '|' + + std::to_string(repertory::utils::get_last_error_code())); + } + + struct dirent *de{nullptr}; + while (res && (de = readdir(root))) { + if (de->d_type == DT_DIR) { + if ((std::string_view(de->d_name) != ".") && + (std::string_view(de->d_name) != "..")) { + res = directory_action(repertory::utils::file::directory( + repertory::utils::path::combine(path, {de->d_name}))); + } + } else { + res = file_action(repertory::utils::file::file( + repertory::utils::path::combine(path, {de->d_name}))); + } + } + + closedir(root); +#endif // defined(_WIN32) + + return res; +} +} // namespace + +namespace repertory::utils::file { +auto directory::copy_to(std::string_view new_path, + bool overwrite) const -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + throw std::runtime_error("failed to copy directory|" + path_ + '|' + + std::string{new_path} + '+' + + std::to_string(overwrite) + "|not implemented"); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto directory::count(bool recursive) const -> std::uint64_t { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + std::uint64_t ret{0U}; + + traverse_directory( + path_, + [&ret, &recursive](auto dir_item) -> bool { + if (recursive) { + ret += dir_item.count(true); + } + + ++ret; + return true; + }, + [&ret](auto /* file_item */) -> bool { + ++ret; + return true; + }); + + return ret; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return 0U; +} + +auto directory::create_directory(std::string_view path) const + -> fs_directory_t { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + auto abs_path = utils::path::combine(path_, {path}); + if (directory{abs_path}.exists()) { + return std::make_unique(abs_path); + } + +#if defined(_WIN32) + auto res = ::SHCreateDirectory(nullptr, + utils::string::from_utf8(abs_path).c_str()); + if (res != ERROR_SUCCESS) { + throw std::runtime_error("failed to create directory|" + + std::string{abs_path} + '|' + + std::to_string(res)); + } +#else // !defined(_WIN32) + auto ret{true}; + auto paths = + utils::string::split(abs_path, utils::path::directory_seperator, false); + + std::string current_path; + for (std::size_t idx = 0U; ret && (idx < paths.size()); ++idx) { + if (paths.at(idx).empty()) { + current_path = utils::path::directory_seperator; + continue; + } + + current_path = utils::path::combine(current_path, {paths.at(idx)}); + auto status = mkdir(current_path.c_str(), S_IRWXU); + ret = ((status == 0) || (errno == EEXIST)); + } +#endif + + return std::make_unique(abs_path); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return nullptr; +} + +auto directory::exists() const -> bool { +#if defined(_WIN32) + return ::PathIsDirectoryA(path_.c_str()) != 0; +#else // !defined(_WIN32) + struct stat64 st {}; + return (stat64(path_.c_str(), &st) == 0 && S_ISDIR(st.st_mode)); +#endif // defined(_WIN32) + + return false; +} + +auto directory::get_directory(std::string_view path) const -> fs_directory_t { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + auto dir_path = utils::path::combine(path_, {path}); + return fs_directory_t{ + directory{dir_path}.exists() ? new directory{dir_path} : nullptr, + }; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return nullptr; +} + +auto directory::get_directories() const -> std::vector { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + std::vector ret{}; + + traverse_directory( + path_, + [&ret](auto dir_item) -> bool { + ret.emplace_back(fs_directory_t{ + new directory(dir_item.get_path()), + }); + + return true; + }, + [](auto /* file_item */) -> bool { return true; }); + + return ret; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return {}; +} + +auto directory::create_file(std::string_view file_name, + bool read_only) const -> fs_file_t { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + auto file_path = utils::path::combine(path_, {file_name}); + return file::open_or_create_file(file_path, read_only); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return nullptr; +} + +auto directory::get_file(std::string_view path) const -> fs_file_t { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + auto file_path = utils::path::combine(path_, {path}); + return fs_file_t{ + file{file_path}.exists() ? new file(file_path) : nullptr, + }; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return nullptr; +} + +auto directory::get_files() const -> std::vector { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + std::vector ret{}; + + traverse_directory( + path_, [](auto /* dir_item */) -> bool { return true; }, + [&ret](auto file_item) -> bool { + ret.emplace_back(fs_file_t{ + new file(file_item.get_path()), + }); + return true; + }); + + return ret; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return {}; +} + +auto directory::get_items() const -> std::vector { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + std::vector ret{}; + + traverse_directory( + path_, + [&ret](auto dir_item) -> bool { + ret.emplace_back(fs_item_t{ + new directory(dir_item.get_path()), + }); + return true; + }, + [&ret](auto file_item) -> bool { + ret.emplace_back(fs_item_t{ + new file(file_item.get_path()), + }); + return true; + }); + + return ret; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return {}; +} + +auto directory::is_symlink() const -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + return std::filesystem::is_symlink(path_); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto directory::move_to(std::string_view new_path) -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + throw std::runtime_error("failed to move directory|" + path_ + '|' + + std::string{new_path} + "|not implemented"); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto directory::remove() -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + if (not exists()) { + return true; + } + + return utils::retry_action([this]() -> bool { + try { +#if defined(_WIN32) + return ::RemoveDirectoryA(path_.c_str()); +#else // !defined(_WIN32) + return (rmdir(path_.c_str()) == 0); +#endif // defined(_WIN32) + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; + }); +} + +auto directory::remove_recursively() -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not exists()) { + return true; + } + + if (not traverse_directory( + path_, + [](auto dir_item) -> bool { return dir_item.remove_recursively(); }, + [](auto file_item) -> bool { return file_item.remove(); })) { + return false; + } + + return remove(); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto directory::size(bool recursive) const -> std::uint64_t { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + std::uint64_t ret{0U}; + + traverse_directory( + path_, + [&ret, &recursive](auto dir_item) -> bool { + if (recursive) { + ret += dir_item.size(true); + } + + return true; + }, + [&ret](auto file_item) -> bool { + auto cur_size = file_item.size(); + if (cur_size.has_value()) { + ret += cur_size.value(); + } + return true; + }); + + return ret; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return 0U; +} +} // namespace repertory::utils::file diff --git a/repertory/librepertory/include/utils/action_queue.hpp b/support/src/utils/file_enc_file.cpp similarity index 51% rename from repertory/librepertory/include/utils/action_queue.hpp rename to support/src/utils/file_enc_file.cpp index 6f8ffe0f..2ff40ac4 100644 --- a/repertory/librepertory/include/utils/action_queue.hpp +++ b/support/src/utils/file_enc_file.cpp @@ -1,49 +1,53 @@ -/* - Copyright <2018-2024> - - 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. -*/ -#ifndef INCLUDE_UTILS_ACTION_QUEUE_HPP_ -#define INCLUDE_UTILS_ACTION_QUEUE_HPP_ - -#include "utils/single_thread_service_base.hpp" - -namespace repertory::utils::action_queue { -class action_queue final : single_thread_service_base { - explicit action_queue(const std::string &id, - std::uint8_t max_concurrent_actions = 5u); - -private: - std::string id_; - std::uint8_t max_concurrent_actions_; - -private: - std::deque> queue_; - mutable std::mutex queue_mtx_; - std::condition_variable queue_notify_; - -protected: - void service_function() override; - -public: - void push(std::function action); -}; -} // namespace repertory::utils::action_queue - -#endif // INCLUDE_UTILS_ACTION_QUEUE_HPP_ +/* + Copyright <2018-2024> + + 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. +*/ +#include "utils/file.hpp" + +#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) + +namespace repertory::utils::file { +auto enc_file::attach_file(fs_file_t file) -> fs_file_t {} + +enc_file::enc_file(fs_file_t file) : file_(std::move(file)) {} + +void enc_file::close() {} + +auto enc_file::copy_to(std::string_view new_path, + bool overwrite) const -> bool {} + +void enc_file::flush() const {} + +auto enc_file::move_to(std::string_view path) -> bool {} + +auto enc_file::read(unsigned char *data, std::size_t to_read, + std::uint64_t offset, std::size_t *total_read) -> bool {} + +auto enc_file::remove() -> bool {} + +auto enc_file::truncate(std::size_t size) -> bool {} + +auto enc_file::write(const unsigned char *data, std::size_t to_write, + std::size_t offset, std::size_t *total_written) -> bool {} + +auto enc_file::size() const -> std::optional {} +} // namespace repertory::utils::file + +#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) diff --git a/support/src/utils/file_file.cpp b/support/src/utils/file_file.cpp new file mode 100644 index 00000000..983c9032 --- /dev/null +++ b/support/src/utils/file_file.cpp @@ -0,0 +1,539 @@ +/* + Copyright <2018-2024> + + 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. +*/ +#include "utils/file.hpp" + +#include "utils/collection.hpp" +#include "utils/common.hpp" +#include "utils/error.hpp" +#include "utils/path.hpp" + +namespace { +[[nodiscard]] auto get_file_size(std::string_view path, + std::uint64_t &file_size) -> bool { + auto abs_path = repertory::utils::path::absolute(path); + file_size = 0U; + +#if defined(_WIN32) + struct _stat64 st {}; + auto res = _stat64(std::string{path}.c_str(), &st); + if (res != 0) { + return false; + } + + file_size = static_cast(st.st_size); + return true; +#else // !defined(_WIN32) + std::error_code ec{}; + file_size = std::filesystem::file_size(abs_path, ec); + return (ec.value() == 0); +#endif // defined(_WIN32) +} + +[[nodiscard]] auto is_file(std::string_view path) -> bool { + auto abs_path = repertory::utils::path::absolute(path); + +#if defined(_WIN32) + return (::PathFileExistsA(abs_path.c_str()) && + not ::PathIsDirectoryA(abs_path.c_str())); +#else // !defined(_WIN32) + struct stat64 st {}; + return (stat64(abs_path.c_str(), &st) == 0 && not S_ISDIR(st.st_mode)); +#endif // defined(_WIN32) +} +} // namespace + +namespace repertory::utils::file { +// auto file::attach_file(native_handle handle, +// bool read_only) -> fs_file_t { +// static constexpr const std::string_view function_name{ +// static_cast(__FUNCTION__), +// }; +// +// try { +// std::string path; +// +// #if defined(_WIN32) +// path.resize(repertory::max_path_length + 1); +// ::GetFinalPathNameByHandleA(handle, path.data(), +// static_cast(path.size()), +// FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); +// #else // !defined(_WIN32) +// path.resize(repertory::max_path_length + 1); +// +// #if defined(__APPLE__) +// fcntl(handle, F_GETPATH, source_path.data()); +// #else // !defined(__APPLE__) +// readlink(("/proc/self/fd/" + std::to_string(handle)).c_str(), +// path.data(), +// path.size()); +// #endif // defined(__APPLE__) +// #endif // defined(_WIN32) +// +// path = path.c_str(); +// +// #if defined(_WIN32) +// auto *ptr = _fdopen( +// static_cast(_open_osfhandle(reinterpret_cast(handle), +// read_only ? _O_RDONLY : _O_RDWR)), +// read_only ? "rb" : "rb+"); +// #else // !defined(_WIN32) +// auto *ptr = fdopen(handle, read_only ? "rb" : "rb+"); +// #endif // defined(_WIN32) +// +// return fs_file_t(new file{ +// file_t{ptr}, +// utils::path::absolute(path), +// read_only, +// }); +// } catch (const std::exception &e) { +// utils::error::handle_exception(function_name, e); +// } catch (...) { +// utils::error::handle_exception(function_name); +// } +// +// return nullptr; +// } + +void file::open() { + if (not is_file(path_)) { + throw std::runtime_error("file not found: " + path_); + } + +#if defined(_WIN32) + file_.reset(_fsopen(path_.c_str(), read_only_ ? "rb" : "rb+", _SH_DENYNO)); +#else // !defined(_WIN32) + file_.reset(fopen(path_.c_str(), read_only_ ? "rb" : "rb+")); +#endif // defined(_WIN32) +} + +auto file::open_file(std::string_view path, bool read_only) -> fs_file_t { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + auto *ptr = new file{ + nullptr, + utils::path::absolute(path), + read_only, + }; + auto new_file = fs_file_t(ptr); + + try { + ptr->open(); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return new_file; +} + +auto file::open_or_create_file(std::string_view path, + bool read_only) -> fs_file_t { + auto abs_path = utils::path::absolute(path); + if (not is_file(abs_path)) { +#if defined(_WIN32) + int old_mode{}; + _umask_s(077, &old_mode); +#else // !defined(_WIN32) + auto old_mode = umask(077); +#endif // defined(_WIN32) + +#if defined(_WIN32) + auto *ptr = _fsopen(abs_path.c_str(), "ab+", _SH_DENYNO); +#else // !defined(_WIN32) + auto *ptr = fopen(abs_path.c_str(), "ab+"); +#endif // defined(_WIN32) + + if (ptr != nullptr) { + fclose(ptr); + } + +#if defined(_WIN32) + _umask_s(old_mode, nullptr); +#else // !defined(_WIN32) + umask(old_mode); +#endif // defined(_WIN32) + } + + return open_file(abs_path, read_only); +} + +void file::close() { file_.reset(); } + +auto file::copy_to(std::string_view new_path, bool overwrite) const -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + auto to_path = utils::path::absolute(new_path); + if (directory(to_path).exists()) { + return false; + } + +#if defined(_WIN32) + return ::CopyFileA(path_.c_str(), to_path.c_str(), + overwrite ? TRUE : FALSE); +#else // !defined(_WIN32) + return std::filesystem::copy_file( + path_, to_path, + overwrite ? std::filesystem::copy_options::overwrite_existing + : std::filesystem::copy_options::skip_existing); +#endif // defined(_WIN32) + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto file::exists() const -> bool { return is_file(path_); } + +void file::flush() const { + if (file_) { + fflush(file_.get()); + } +} + +auto file::get_handle() const -> native_handle { + if (file_) { +#if defined(_WIN32) + return reinterpret_cast( + _get_osfhandle(_fileno(file_.get()))); +#else // !defined(_WIN32) + return fileno(file_.get()); +#endif // defined(_WIN32) + } + + return INVALID_HANDLE_VALUE; +} + +auto file::is_symlink() const -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + return std::filesystem::is_symlink(path_); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto file::move_to(std::string_view path) -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + auto abs_path = utils::path::absolute(path); + + auto reopen{false}; + if (file_) { + reopen = true; + close(); + } + + auto success{false}; +#if defined(_WIN32) + success = !!::MoveFileExA(path_.c_str(), abs_path.c_str(), + MOVEFILE_REPLACE_EXISTING); +#else // !// defined(_WIN32) + std::error_code ec{}; + std::filesystem::rename(path_, abs_path, ec); + success = ec.value() == 0; +#endif // defined(_WIN32) + + if (success) { + path_ = abs_path; + } + + if (reopen) { + try { + open(); + return success; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + } + + return false; +} + +auto file::read(unsigned char *data, std::size_t to_read, std::uint64_t offset, + std::size_t *total_read) -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + if (total_read != nullptr) { + (*total_read) = 0U; + } + + try { + if (not file_) { + throw std::runtime_error("file is not open for reading"); + } + + if (fseeko(file_.get(), static_cast(offset), SEEK_SET) == + -1) { + throw std::runtime_error("failed to seek before read"); + } + + std::size_t bytes_read{0U}; + while (bytes_read != to_read) { + auto res = + fread(&data[bytes_read], 1U, to_read - bytes_read, file_.get()); + if (not feof(file_.get()) && ferror(file_.get())) { + throw std::runtime_error("failed to read file bytes"); + } + + if (res == 0) { + break; + } + + bytes_read += static_cast(res); + } + + if (total_read != nullptr) { + (*total_read) = bytes_read; + } + + return true; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +#if defined(PROJECT_ENABLE_LIBSODIUM) +auto file::sha256() -> std::optional { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + auto should_close{false}; + auto read_only{read_only_}; + std::optional ret; + + try { + if (file_ == nullptr) { + should_close = true; + read_only_ = true; + this->open(); + } + + crypto_hash_sha256_state state{}; + auto res = crypto_hash_sha256_init(&state); + if (res != 0) { + throw std::runtime_error("failed to initialize sha256|" + + std::to_string(res)); + } + + { + data_buffer buffer(get_read_buffer_size()); + std::uint64_t read_offset{0U}; + std::size_t bytes_read{0U}; + while (i_file::read(buffer, read_offset, &bytes_read)) { + if (not bytes_read) { + break; + } + + read_offset += bytes_read; + res = crypto_hash_sha256_update( + &state, reinterpret_cast(buffer.data()), + bytes_read); + if (res != 0) { + throw std::runtime_error("failed to update sha256|" + + std::to_string(res)); + } + } + } + + std::array out{}; + res = crypto_hash_sha256_final(&state, out.data()); + if (res != 0) { + throw std::runtime_error("failed to finalize sha256|" + + std::to_string(res)); + } + + ret = utils::collection::to_hex_string(out); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + if (should_close) { + read_only_ = read_only; + close(); + } + + return ret; +} +#endif // defined(PROJECT_ENABLE_LIBSODIUM) + +auto file::remove() -> bool { + if (not exists()) { + return true; + } + + close(); + + return utils::retry_action([this]() -> bool { +#if defined(_WIN32) + return ::DeleteFileA(path_.c_str()); +#else // !defined(_WIN32) + std::error_code ec{}; + return std::filesystem::remove(path_, ec); +#endif // defined(_WIN32) + }); +} + +auto file::truncate(std::size_t size) -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + auto reopen{false}; + if (file_) { + reopen = true; + close(); + } + + std::error_code ec{}; + std::filesystem::resize_file(path_, size, ec); + + auto success{ec.value() == 0}; + + if (reopen) { + try { + open(); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + success = false; + } catch (...) { + utils::error::handle_exception(function_name); + success = false; + } + } + + return success; +} + +auto file::write(const unsigned char *data, std::size_t to_write, + std::size_t offset, std::size_t *total_written) -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + if (total_written != nullptr) { + (*total_written) = 0U; + } + + try { + if (not file_) { + throw std::runtime_error("file is not open for writing"); + } + + auto res = fseeko(file_.get(), static_cast(offset), SEEK_SET); + if (res == -1) { + throw std::runtime_error("failed to seek before write"); + } + + std::size_t bytes_written{0U}; + while (bytes_written != to_write) { + res = fwrite(reinterpret_cast(&data[bytes_written]), 1U, + to_write - bytes_written, file_.get()); + if (not feof(file_.get()) && ferror(file_.get())) { + throw std::runtime_error("failed to write file bytes"); + } + + if (res == 0) { + break; + } + + bytes_written += static_cast(res); + } + + flush(); + + if (total_written != nullptr) { + (*total_written) = bytes_written; + } + + return true; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto file::size() const -> std::optional { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (file_) { + if (fseeko(file_.get(), 0, SEEK_END) == -1) { + throw std::runtime_error("failed to seek"); + } + + auto size = ftello(file_.get()); + if (size == -1) { + throw std::runtime_error("failed to get position"); + } + + return static_cast(size); + } + + std::uint64_t size{}; + if (not get_file_size(path_, size)) { + throw std::runtime_error("failed to get file size"); + } + + return size; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return std::nullopt; +} +} // namespace repertory::utils::file diff --git a/support/src/utils/file_smb_directory.cpp b/support/src/utils/file_smb_directory.cpp new file mode 100644 index 00000000..54e9162e --- /dev/null +++ b/support/src/utils/file_smb_directory.cpp @@ -0,0 +1,615 @@ +/* + Copyright <2018-2024> + + 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. +*/ +#include "utils/file.hpp" + +#include "utils/common.hpp" +#include "utils/error.hpp" + +#if defined(PROJECT_ENABLE_LIBDSM) +namespace repertory::utils::file { +auto smb_directory::open(std::string_view host, std::string_view user, + std::string_view password, + std::string_view share_name) -> smb_directory_t { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + smb_session_t session{smb_session_new(), smb_session_deleter}; + netbios_ns_t ns{netbios_ns_new()}; + + sockaddr_in addr{}; + + auto res = netbios_ns_resolve( + ns.get(), std::string{host}.c_str(), NETBIOS_FILESERVER, + reinterpret_cast(&addr.sin_addr.s_addr)); + if (res != DSM_SUCCESS) { + res = inet_pton(AF_INET, std::string{host}.c_str(), &addr.sin_addr); + if (res != 1) { + throw std::runtime_error("failed to resolve host|" + std::string{host} + + '|' + std::to_string(errno)); + } + } + + res = smb_session_connect(session.get(), std::string{host}.c_str(), + static_cast(addr.sin_addr.s_addr), + SMB_TRANSPORT_TCP); + if (res != DSM_SUCCESS) { + throw std::runtime_error("failed to connect to host|" + + std::string{host} + '|' + std::to_string(res)); + } + + smb_session_set_creds(session.get(), std::string{host}.c_str(), + std::string{user}.c_str(), + std::string{password}.c_str()); + res = smb_session_login(session.get()); + if (res != DSM_SUCCESS) { + throw std::runtime_error("failed to logon to host|" + std::string{host} + + '|' + std::string{user} + '|' + + std::to_string(res)); + } + + smb_tid tid{}; + res = + smb_tree_connect(session.get(), std::string{share_name}.c_str(), &tid); + if (res != DSM_SUCCESS) { + throw std::runtime_error("failed to connect to share|" + + std::string{share_name} + '|' + + std::to_string(res)); + } + + return smb_directory_t{ + new smb_directory{ + "//" + std::string{host} + "/" + std::string{share_name}, + session, + share_name, + tid, + }, + }; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return nullptr; +} + +auto smb_directory::open(std::wstring_view host, std::wstring_view user, + std::wstring_view password, + std::wstring_view share_name) -> smb_directory_t { + return open(utils::string::to_utf8(host), utils::string::to_utf8(user), + utils::string::to_utf8(password), + utils::string::to_utf8(share_name)); +} + +auto smb_directory::copy_to(std::string_view new_path, + bool overwrite) const -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + // auto to_path = utils::path::absolute(new_path); + + throw std::runtime_error("failed to copy directory|" + path_ + '|' + + std::string{new_path} + "|not implemented"); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto smb_directory::count(bool recursive) const -> std::uint64_t { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + smb_stat_list_t list{ + smb_find(session_.get(), tid_, smb_create_search_path(path_).c_str())}; + auto count = smb_stat_list_count(list.get()); + + if (not recursive) { + return count; + } + + throw std::runtime_error("failed to get directory count recursively|" + + path_ + "|not implemented"); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return 0U; +} + +auto smb_directory::create_directory(std::string_view path) const + -> fs_directory_t { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + auto dir = get_directory(path); + if (dir) { + return dir; + } + + auto res = smb_directory_create( + session_.get(), tid_, + smb_create_and_validate_relative_path(path_, path).c_str()); + if (res != DSM_SUCCESS) { + throw std::runtime_error("failed to create directory|" + path_ + '/' + + std::string{path} + '|' + std::to_string(res)); + } + + return get_directory(path); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return nullptr; +} + +auto smb_directory::create_file(std::string_view file_name, + bool read_only) const -> fs_file_t { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + auto fs_file = get_file(file_name); + if (fs_file) { + if (not dynamic_cast(fs_file.get())->open(read_only)) { + throw std::runtime_error("failed to open existing file|" + + std::string{file_name}); + } + + return fs_file; + } + + auto rel_path = smb_create_and_validate_relative_path(path_, file_name); + + smb_fd fd{}; + auto res = + smb_fopen(session_.get(), tid_, rel_path.c_str(), SMB_MOD_RW, &fd); + if (res != DSM_SUCCESS) { + return nullptr; + } + smb_fclose(session_.get(), fd); + + res = smb_fopen(session_.get(), tid_, rel_path.c_str(), + read_only ? SMB_MOD_RO : SMB_MOD_RW2, &fd); + if (res != DSM_SUCCESS) { + return nullptr; + } + + return std::make_unique( + fd, smb_create_smb_path(path_, std::string{rel_path}), session_, + share_name_, tid_); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return nullptr; +} +auto smb_directory::exists() const -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + smb_stat_t st{smb_fstat(session_.get(), tid_, + smb_create_relative_path(path_).c_str())}; + if (not st) { + return false; + } + + return smb_stat_get(st.get(), SMB_STAT_ISDIR) != 0U; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto smb_directory::get_directory(std::string_view path) const + -> fs_directory_t { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + auto rel_path = smb_create_and_validate_relative_path(path_, path); + smb_stat_t st{smb_fstat(session_.get(), tid_, rel_path.c_str())}; + if (not st) { + throw std::runtime_error("failed to stat directory|" + rel_path); + } + + bool is_dir{smb_stat_get(st.get(), SMB_STAT_ISDIR) != 0U}; + if (not is_dir) { + throw std::runtime_error("path is not a directory|" + rel_path); + } + + return smb_directory_t{ + new smb_directory{ + smb_create_smb_path(path_, rel_path), + session_, + share_name_, + tid_, + }, + }; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return nullptr; +} + +auto smb_directory::get_directories() const -> std::vector { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + smb_stat_list_t list{ + smb_find(session_.get(), tid_, smb_create_search_path(path_).c_str())}; + if (not list) { + throw std::runtime_error("failed to get directory list|" + path_); + } + + std::vector ret{}; + + auto count = smb_stat_list_count(list.get()); + for (std::size_t idx = 0U; idx < count; ++idx) { + auto st = smb_stat_list_at(list.get(), idx); + + bool is_dir{smb_stat_get(st, SMB_STAT_ISDIR) != 0U}; + if (not is_dir) { + continue; + } + + std::string name{smb_stat_name(st)}; + if (name == "." || name == "..") { + continue; + } + + ret.emplace_back(smb_directory_t{ + new smb_directory{ + smb_create_smb_path(path_, name), + session_, + share_name_, + tid_, + }, + }); + } + + return ret; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return {}; +} + +auto smb_directory::get_file(std::string_view path) const -> fs_file_t { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + auto rel_path = smb_create_and_validate_relative_path(path_, path); + smb_stat_t st{smb_fstat(session_.get(), tid_, rel_path.c_str())}; + if (not st) { + throw std::runtime_error("failed to stat file|" + rel_path); + } + + bool is_dir{smb_stat_get(st.get(), SMB_STAT_ISDIR) != 0U}; + if (is_dir) { + throw std::runtime_error("path is not a file|" + rel_path); + } + + return std::make_unique( + std::nullopt, smb_create_smb_path(path_, std::string{rel_path}), + session_, share_name_, tid_); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return nullptr; +} + +auto smb_directory::get_files() const -> std::vector { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + smb_stat_list_t list{ + smb_find(session_.get(), tid_, smb_create_search_path(path_).c_str())}; + if (not list) { + throw std::runtime_error("failed to get file list|" + path_); + } + + std::vector ret{}; + + auto count = smb_stat_list_count(list.get()); + for (std::size_t idx = 0U; idx < count; ++idx) { + auto st = smb_stat_list_at(list.get(), idx); + bool is_dir{smb_stat_get(st, SMB_STAT_ISDIR) != 0U}; + if (is_dir) { + continue; + } + + std::string name{smb_stat_name(st)}; + ret.emplace_back(std::make_unique( + std::nullopt, smb_create_smb_path(path_, name), session_, share_name_, + tid_)); + } + + return ret; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return {}; +} + +auto smb_directory::get_items() const -> std::vector { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + smb_stat_list_t list{ + smb_find(session_.get(), tid_, smb_create_search_path(path_).c_str())}; + if (not list) { + throw std::runtime_error("failed to get item list|" + path_); + } + std::vector ret{}; + + auto count = smb_stat_list_count(list.get()); + for (std::size_t idx = 0U; idx < count; ++idx) { + auto st = smb_stat_list_at(list.get(), idx); + + bool is_dir{smb_stat_get(st, SMB_STAT_ISDIR) != 0U}; + std::string name{smb_stat_name(st)}; + + if (is_dir) { + if (name == "." || name == "..") { + continue; + } + + ret.emplace_back(smb_directory_t{ + new smb_directory{ + path_ + '/' + name, + session_, + share_name_, + tid_, + }, + }); + continue; + } + + ret.emplace_back(std::make_unique( + std::nullopt, smb_create_smb_path(path_, name), session_, share_name_, + tid_)); + } + + return ret; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return {}; +} + +auto smb_directory::is_symlink() const -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto smb_directory::move_to(std::string_view new_path) -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + throw std::runtime_error("failed to move directory|" + path_ + '|' + + std::string{new_path} + "|not implemented"); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto smb_directory::remove() -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + if (not exists()) { + return true; + } + + return utils::retry_action([this]() -> bool { + try { + auto res = smb_directory_rm(session_.get(), tid_, + smb_create_relative_path(path_).c_str()); + if (res != DSM_SUCCESS) { + throw std::runtime_error("failed to remove directory|" + path_ + '|' + + std::to_string(res)); + } + + return true; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; + }); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto smb_directory::remove_recursively() -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + if (not exists()) { + return true; + } + + throw std::runtime_error("failed to remove directory recursively|" + path_ + + "|not implemented"); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto smb_directory::size(bool /* recursive */) const -> std::uint64_t { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + throw std::runtime_error("failed to get directory size|" + path_ + + "|not implemented"); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} +} // namespace repertory::utils::file + +#endif // defined(PROJECT_ENABLE_LIBDSM) diff --git a/support/src/utils/file_smb_file.cpp b/support/src/utils/file_smb_file.cpp new file mode 100644 index 00000000..dde4eb71 --- /dev/null +++ b/support/src/utils/file_smb_file.cpp @@ -0,0 +1,460 @@ +/* + Copyright <2018-2024> + + 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. +*/ +#include "utils/file.hpp" + +#include "utils/common.hpp" +#include "utils/error.hpp" +#include "utils/string.hpp" + +#if defined(PROJECT_ENABLE_LIBDSM) + +namespace repertory::utils::file { +void smb_file::close() { + if (fd_.has_value()) { + smb_fclose(session_.get(), *fd_); + fd_.reset(); + } +} + +auto smb_file::copy_to(std::string_view new_path, + bool overwrite) const -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + // auto to_path = utils::path::absolute(new_path); + + throw std::runtime_error("failed to copy file|" + path_ + '|' + + std::string{new_path} + '|' + + std::to_string(overwrite) + "|not implemented"); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto smb_file::exists() const -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + smb_stat_t st{smb_fstat(session_.get(), tid_, + smb_create_relative_path(path_).c_str())}; + if (not st) { + return false; + } + + return smb_stat_get(st.get(), SMB_STAT_ISDIR) == 0U; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +void smb_file::flush() const { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + throw std::runtime_error("failed to flush file|" + path_ + + "|not implemented"); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } +} + +auto smb_file::get_time(smb_session *session, smb_tid tid, std::string path, + time_type type) -> std::optional { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (session == nullptr) { + throw std::runtime_error("session not found|" + path); + } + + auto rel_path = smb_create_relative_path(path); + smb_stat_t st{smb_fstat(session, tid, rel_path.c_str())}; + if (not st) { + throw std::runtime_error("failed to stat directory|" + rel_path); + } + + switch (type) { + case time_type::accessed: + return smb_stat_get(st.get(), SMB_STAT_ATIME); + + case time_type::created: + return smb_stat_get(st.get(), SMB_STAT_CTIME); + + case time_type::modified: + return smb_stat_get(st.get(), SMB_STAT_MTIME); + + case time_type::written: + return smb_stat_get(st.get(), SMB_STAT_WTIME); + } + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return std::nullopt; +} + +auto smb_file::is_symlink() const -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto smb_file::move_to(std::string_view new_path) -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (utils::string::begins_with(new_path, "//")) { + throw std::runtime_error("failed to move file|" + path_ + '|' + + std::string{new_path} + + "|new path must be in same share"); + } + + auto from_path = smb_create_relative_path(path_); + auto to_path = smb_create_and_validate_relative_path( + utils::string::begins_with(new_path, "/") ? smb_get_root_path(path_) + : smb_get_parent_path(path_), + new_path); + + auto was_open{false}; + if (fd_.has_value()) { + close(); + was_open = true; + } + + auto res = smb_tree_connect(session_.get(), share_name_.c_str(), &tid_); + if (res != DSM_SUCCESS) { + throw std::runtime_error("failed to connect to share|" + share_name_ + + '|' + std::to_string(res)); + } + + res = smb_file_mv(session_.get(), tid_, from_path.c_str(), to_path.c_str()); + if (res != DSM_SUCCESS) { + throw std::runtime_error("failed to move file|" + path_ + '|' + + from_path + '|' + to_path + '|' + + std::to_string(res)); + } + + path_ = smb_create_smb_path(path_, to_path); + if (was_open) { + return open(read_only_); + } + + return true; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto smb_file::open(bool read_only) -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (fd_.has_value()) { + if (read_only == read_only_) { + return true; + } + + close(); + } + + auto rel_path = smb_create_relative_path(path_); + + auto res = smb_tree_connect(session_.get(), share_name_.c_str(), &tid_); + if (res != DSM_SUCCESS) { + throw std::runtime_error("failed to connect to share|" + share_name_ + + '|' + std::to_string(res)); + } + + smb_fd fd{}; + res = smb_fopen(session_.get(), tid_, rel_path.c_str(), + read_only ? SMB_MOD_RO : SMB_MOD_RW2, &fd); + if (res != DSM_SUCCESS) { + throw std::runtime_error("failed to open file|" + path_ + '|' + rel_path + + '|' + utils::string::from_bool(read_only) + '|' + + std::to_string(res)); + } + + fd_ = fd; + read_only_ = read_only; + + return true; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto smb_file::read(unsigned char *data, std::size_t to_read, + std::uint64_t offset, std::size_t *total_read) -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (total_read != nullptr) { + (*total_read) = 0U; + } + + if (not fd_.has_value()) { + throw std::runtime_error("failed to read file|" + path_ + + "|file not open"); + } + + auto res = smb_fseek(session_.get(), *fd_, static_cast(offset), + SMB_SEEK_SET); + if (res == -1) { + throw std::runtime_error("failed to seek file|" + path_ + '|' + + std::to_string(offset) + '|' + + std::to_string(res)); + } + + std::size_t bytes_read{0U}; + while (bytes_read != to_read) { + res = smb_fread(session_.get(), *fd_, &data[bytes_read], + to_read - bytes_read); + if (res == -1) { + throw std::runtime_error("failed to read file|" + path_ + '|' + + std::to_string(to_read) + '|' + + std::to_string(res)); + } + + if (res == 0) { + break; + } + + bytes_read += static_cast(res); + } + + if (total_read != nullptr) { + (*total_read) = bytes_read; + } + + return true; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto smb_file::remove() -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not exists()) { + return true; + } + + close(); + + return utils::retry_action([this]() -> bool { + try { + auto res = smb_tree_connect(session_.get(), share_name_.c_str(), &tid_); + if (res != DSM_SUCCESS) { + throw std::runtime_error("failed to connect to share|" + share_name_ + + '|' + std::to_string(res)); + } + + auto rel_path = smb_create_relative_path(path_); + res = smb_file_rm(session_.get(), tid_, rel_path.c_str()); + if (res != DSM_SUCCESS) { + throw std::runtime_error( + "failed to remove file|" + path_ + '|' + rel_path + '|' + + std::to_string(res) + '|' + + std::to_string(smb_session_get_nt_status(session_.get()))); + } + + return true; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; + }); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto smb_file::size() const -> std::optional { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + auto rel_path = smb_create_relative_path(path_); + smb_stat_t st{smb_fstat(session_.get(), tid_, rel_path.c_str())}; + if (not st) { + throw std::runtime_error("failed to stat directory|" + rel_path); + } + + return smb_stat_get(st.get(), SMB_STAT_SIZE); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return std::nullopt; +} + +auto smb_file::truncate(std::size_t size) -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + throw std::runtime_error("failed to truncate file|" + path_ + '|' + + std::to_string(size) + "|not implemented"); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + +auto smb_file::write(const unsigned char *data, std::size_t to_write, + std::size_t offset, std::size_t *total_written) -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (total_written != nullptr) { + (*total_written) = 0U; + } + + if (not fd_.has_value()) { + throw std::runtime_error("failed to write file|" + path_ + + "|file not open"); + } + + auto res = smb_fseek(session_.get(), *fd_, static_cast(offset), + SMB_SEEK_SET); + if (res == -1) { + throw std::runtime_error("failed to seek file|" + path_ + '|' + + std::to_string(offset) + '|' + + std::to_string(res)); + } + + std::size_t bytes_written{0U}; + while (bytes_written != to_write) { + res = smb_fwrite(session_.get(), *fd_, + const_cast(&data[bytes_written]), + to_write - bytes_written); + if (res == -1) { + throw std::runtime_error("failed to write file|" + path_ + '|' + + std::to_string(to_write) + '|' + + std::to_string(res)); + } + + if (res == 0) { + break; + } + + bytes_written += static_cast(res); + } + + if (total_written != nullptr) { + (*total_written) = bytes_written; + } + + return true; + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} +} // namespace repertory::utils::file + +#endif // defined(PROJECT_ENABLE_LIBDSM) diff --git a/support/src/utils/file_thread_file.cpp b/support/src/utils/file_thread_file.cpp new file mode 100644 index 00000000..7cc7eaa0 --- /dev/null +++ b/support/src/utils/file_thread_file.cpp @@ -0,0 +1,59 @@ +/* + Copyright <2018-2024> + + 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. +*/ +#include "utils/file.hpp" + +namespace repertory::utils::file { +// auto thread_file::attach_file(native_handle handle, +// bool read_only) -> fs_file_t {} + +auto thread_file::attach_file(fs_file_t file) -> fs_file_t {} + +auto thread_file::open_file(std::string_view path, + bool read_only) -> fs_file_t {} + +auto thread_file::open_or_create_file(std::string_view path, + bool read_only) -> fs_file_t {} + +thread_file::thread_file(fs_file_t file) : file_(std::move(file)) {} + +void thread_file::close() {} + +auto thread_file::copy_to(std::string_view new_path, + bool overwrite) const -> bool {} + +void thread_file::flush() const {} + +auto thread_file::move_to(std::string_view path) -> bool {} + +auto thread_file::read(unsigned char *data, std::size_t to_read, + std::uint64_t offset, std::size_t *total_read) -> bool {} + +auto thread_file::remove() -> bool {} + +auto thread_file::truncate(std::size_t size) -> bool {} + +auto thread_file::write(const unsigned char *data, std::size_t to_write, + std::size_t offset, + std::size_t *total_written) -> bool {} + +auto thread_file::size() const -> std::optional {} +} // namespace repertory::utils::file diff --git a/support/src/utils/hash.cpp b/support/src/utils/hash.cpp index 7cc2fe52..7b4895c8 100644 --- a/support/src/utils/hash.cpp +++ b/support/src/utils/hash.cpp @@ -23,47 +23,140 @@ #if defined(PROJECT_ENABLE_LIBSODIUM) -namespace {} // namespace - namespace repertory::utils::encryption { auto create_hash_blake2b_256(std::string_view data) -> hash_256_t { - return create_hash_blake2b_t(data); + return create_hash_blake2b_t( + reinterpret_cast(data.data()), data.size()); } auto create_hash_blake2b_256(std::wstring_view data) -> hash_256_t { - return create_hash_blake2b_t(data); + return create_hash_blake2b_t( + reinterpret_cast(data.data()), + data.size() * sizeof(wchar_t)); +} + +auto create_hash_blake2b_256(const data_buffer &data) -> hash_256_t { + return create_hash_blake2b_t( + reinterpret_cast(data.data()), + data.size() * sizeof(data_buffer::value_type)); } auto create_hash_blake2b_384(std::string_view data) -> hash_384_t { - return create_hash_blake2b_t(data); + return create_hash_blake2b_t( + reinterpret_cast(data.data()), data.size()); } auto create_hash_blake2b_384(std::wstring_view data) -> hash_384_t { - return create_hash_blake2b_t(data); + return create_hash_blake2b_t( + reinterpret_cast(data.data()), + data.size() * sizeof(wchar_t)); +} + +auto create_hash_blake2b_384(const data_buffer &data) -> hash_384_t { + return create_hash_blake2b_t( + reinterpret_cast(data.data()), + data.size() * sizeof(data_buffer::value_type)); } auto create_hash_blake2b_512(std::string_view data) -> hash_512_t { - return create_hash_blake2b_t(data); + return create_hash_blake2b_t( + reinterpret_cast(data.data()), data.size()); } auto create_hash_blake2b_512(std::wstring_view data) -> hash_512_t { - return create_hash_blake2b_t(data); + return create_hash_blake2b_t( + reinterpret_cast(data.data()), + data.size() * sizeof(wchar_t)); +} + +auto create_hash_blake2b_512(const data_buffer &data) -> hash_512_t { + return create_hash_blake2b_t( + reinterpret_cast(data.data()), + data.size() * sizeof(data_buffer::value_type)); } auto create_hash_sha256(std::string_view data) -> hash_256_t { - return create_hash_sha256_t(data); + return create_hash_sha256( + reinterpret_cast(data.data()), data.size()); } auto create_hash_sha256(std::wstring_view data) -> hash_256_t { - return create_hash_sha256_t(data); + return create_hash_sha256( + reinterpret_cast(data.data()), + data.size() * sizeof(wchar_t)); +} + +auto create_hash_sha256(const data_buffer &data) -> hash_256_t { + return create_hash_sha256( + reinterpret_cast(data.data()), + data.size() * sizeof(data_buffer::value_type)); } auto create_hash_sha512(std::string_view data) -> hash_512_t { - return create_hash_sha512_t(data); + return create_hash_sha512( + reinterpret_cast(data.data()), data.size()); } auto create_hash_sha512(std::wstring_view data) -> hash_512_t { - return create_hash_sha512_t(data); + return create_hash_sha512( + reinterpret_cast(data.data()), + data.size() * sizeof(wchar_t)); +} + +auto create_hash_sha512(const data_buffer &data) -> hash_512_t { + return create_hash_sha512( + reinterpret_cast(data.data()), + data.size() * sizeof(data_buffer::value_type)); +} + +auto create_hash_sha512(const unsigned char *data, + std::size_t data_size) -> hash_512_t { + hash_512_t hash{}; + + crypto_hash_sha512_state state{}; + auto res = crypto_hash_sha512_init(&state); + if (res != 0) { + throw std::runtime_error("failed to initialize sha-512|" + + std::to_string(res)); + } + + res = crypto_hash_sha512_update(&state, data, data_size); + if (res != 0) { + throw std::runtime_error("failed to update sha-512|" + std::to_string(res)); + } + + res = crypto_hash_sha512_final(&state, hash.data()); + if (res != 0) { + throw std::runtime_error("failed to finalize sha-512|" + + std::to_string(res)); + } + + return hash; +} + +auto create_hash_sha256(const unsigned char *data, + std::size_t data_size) -> hash_256_t { + hash_256_t hash{}; + + crypto_hash_sha256_state state{}; + auto res = crypto_hash_sha256_init(&state); + if (res != 0) { + throw std::runtime_error("failed to initialize sha-256|" + + std::to_string(res)); + } + + res = crypto_hash_sha256_update(&state, data, data_size); + if (res != 0) { + throw std::runtime_error("failed to update sha-256|" + std::to_string(res)); + } + + res = crypto_hash_sha256_final(&state, hash.data()); + if (res != 0) { + throw std::runtime_error("failed to finalize sha-256|" + + std::to_string(res)); + } + + return hash; } } // namespace repertory::utils::encryption diff --git a/support/src/utils/path.cpp b/support/src/utils/path.cpp index 92af6b0d..84037685 100644 --- a/support/src/utils/path.cpp +++ b/support/src/utils/path.cpp @@ -23,18 +23,11 @@ #include "utils/common.hpp" #include "utils/error.hpp" +#include "utils/file.hpp" #include "utils/string.hpp" #include "utils/unix.hpp" namespace { -static const std::string directory_seperator_str{ - repertory::utils::path::directory_seperator, -}; - -static const std::wstring directory_seperator_str_w{ - repertory::utils::path::directory_seperator_w, -}; - [[nodiscard]] auto resolve(std::string path) -> std::string { #if defined(_WIN32) if (repertory::utils::string::contains(path, "~") || @@ -52,12 +45,17 @@ static const std::wstring directory_seperator_str_w{ #else // !defined (_WIN32) if (repertory::utils::string::contains(path, "~")) { std::string home{}; - repertory::utils::use_getpwuid(getuid(), [&home](struct passwd *pw) { - home = (pw->pw_dir ? pw->pw_dir : ""); - if (home.empty() || ((home == "/") && (getuid() != 0))) { - home = repertory::utils::path::combine("/home", {pw->pw_name}); - } - }); + auto res = + repertory::utils::use_getpwuid(getuid(), [&home](struct passwd *pw) { + home = (pw->pw_dir ? pw->pw_dir : ""); + if (home.empty() || + ((home == repertory::utils::path::slash) && (getuid() != 0))) { + home = repertory::utils::path::combine("/home", {pw->pw_name}); + } + }); + if (res) { + throw std::runtime_error("failed to getpwuid: " + res.reason); + } return repertory::utils::string::replace(path, "~", home); } @@ -70,42 +68,59 @@ static const std::wstring directory_seperator_str_w{ namespace repertory::utils::path { auto absolute(std::string_view path) -> std::string { std::string abs_path{path}; - abs_path = resolve(abs_path); - format_path(abs_path, directory_seperator, not_directory_seperator); + if (abs_path.empty()) { + return abs_path; + } + abs_path = finalize(resolve(abs_path)); #if defined(_WIN32) - if (not abs_path.empty() && ::PathIsRelativeA(abs_path.c_str())) { - std::string temp; - temp.resize(MAX_PATH + 1); - abs_path = _fullpath(temp.data(), abs_path.c_str(), MAX_PATH); + if (not utils::string::contains(abs_path, dot)) { + return abs_path; } + + std::string temp; + temp.resize(repertory::max_path_length + 1); + ::GetFullPathNameA(abs_path.c_str(), static_cast(temp.size()), + temp.data(), nullptr); #else // !defined(_WIN32) - if (not abs_path.empty() && (abs_path.at(0U) != '/')) { - auto found{false}; - std::string tmp{abs_path}; - do { - auto *res = realpath(tmp.c_str(), nullptr); - if (res != nullptr) { - abs_path = res + std::string{directory_seperator} + - abs_path.substr(tmp.size()); - free(res); - found = true; - } else if (tmp == ".") { - found = true; - } else { - tmp = dirname(tmp.data()); - } - } while (not found); + if (not utils::string::contains(abs_path, dot) || + utils::string::begins_with(abs_path, slash)) { + return abs_path; } + + auto found{false}; + std::string tmp{abs_path}; + do { + auto *res = realpath(tmp.c_str(), nullptr); + if (res != nullptr) { + abs_path = + res + std::string{directory_seperator} + abs_path.substr(tmp.size()); + free(res); + found = true; + } else if (tmp == dot) { + found = true; + } else { + tmp = dirname(tmp.data()); + } + } while (not found); #endif // defined(_WIN32) - return format_path(abs_path, directory_seperator, not_directory_seperator); + return finalize(abs_path); } auto absolute(std::wstring_view path) -> std::wstring { return utils::string::from_utf8(absolute(utils::string::to_utf8(path))); } +auto exists(std::string_view path) -> bool { + return utils::file::file{path}.exists() || + utils::file::directory{path}.exists(); +} + +auto exists(std::wstring_view path) -> bool { + return exists(utils::string::to_utf8(path)); +} + auto find_program_in_path(const std::string &name_without_extension) -> std::string { static std::mutex mtx{}; @@ -137,12 +152,12 @@ auto find_program_in_path(const std::string &name_without_extension) static constexpr const auto split_char = ':'; #endif // defined(_WIN32) - const auto search_path_list = utils::string::split(path, split_char, false); + auto search_path_list = utils::string::split(path, split_char, false); for (auto &&search_path : search_path_list) { for (auto &&extension : extension_list) { auto exec_path = combine( search_path, {name_without_extension + std::string{extension}}); - if (std::filesystem::exists(exec_path)) { + if (utils::file::file(exec_path).exists()) { found_items[name_without_extension] = exec_path; return exec_path; } @@ -158,41 +173,66 @@ find_program_in_path(std::wstring_view name_without_extension) -> std::wstring { find_program_in_path(utils::string::to_utf8(name_without_extension))); } -auto get_parent_directory(std::string_view path) -> std::string { - auto ret = std::filesystem::path{path}.parent_path().string(); -#if !defined(_WIN32) - if (ret == ".") { - ret = "/"; - } -#endif // !defined(_WIN32) +auto get_parent_path(std::string_view path) -> std::string { + auto abs_path = absolute(path); - return absolute(ret); +#if defined(_WIN32) + ::PathRemoveFileSpecA(abs_path.data()); + abs_path = abs_path.c_str(); +#else // !defined(_WIN32) + abs_path = std::filesystem::path{abs_path}.parent_path().string(); +#endif // defined(_WIN32) + + return finalize(abs_path); } -auto get_parent_directory(std::wstring_view path) -> std::wstring { +auto get_parent_path(std::wstring_view path) -> std::wstring { return utils::string::from_utf8( - get_parent_directory(utils::string::to_utf8(path))); + get_parent_path(utils::string::to_utf8(path))); } -auto is_trash_directory(std::string_view path) -> bool { - auto trash_path = utils::string::to_lower(absolute(path)); - return utils::string::begins_with(trash_path, - directory_seperator_str + ".trash-") || - utils::string::begins_with(trash_path, - directory_seperator_str + ".trashes") || - utils::string::begins_with(trash_path, - directory_seperator_str + "$recycle.bin"); +auto get_relative_path(std::string_view path, + std::string_view root_path) -> std::string { + auto abs_path = absolute(path); + auto abs_root_path = + absolute(root_path) + std::string{get_directory_seperator()}; +#if defined(_WIN32) + if (utils::string::to_lower(abs_path).starts_with( + utils::string::to_lower(abs_root_path))) { +#else // !defined(_WIN32) + if (abs_path.starts_with(abs_root_path)) { +#endif // defined(_WIN32) + return abs_path.substr(abs_root_path.size()); + } + + return abs_path; } -auto is_trash_directory(std::wstring_view path) -> bool { - return is_trash_directory(utils::string::to_utf8(path)); +auto get_relative_path(std::wstring_view path, + std::wstring_view root_path) -> std::wstring { + return utils::string::from_utf8(get_relative_path( + utils::string::to_utf8(path), utils::string::to_utf8(root_path))); +} + +auto contains_trash_directory(std::string_view path) -> bool { + auto parts = utils::string::split(utils::string::to_lower(absolute(path)), + get_directory_seperator(), false); + + return std::find_if(parts.begin(), parts.end(), [](auto &&part) -> bool { + return utils::string::begins_with(part, ".trash-") || + part == ".trashes" || part == "$recycle.bin"; + }) != parts.end(); +} + +auto contains_trash_directory(std::wstring_view path) -> bool { + return contains_trash_directory(utils::string::to_utf8(path)); } auto make_file_uri(std::string_view path) -> std::string { auto abs_path = absolute(path); #if defined(_WIN32) - utils::string::replace(abs_path, '\\', '/'); - abs_path = '/' + abs_path; + utils::string::replace(abs_path, backslash, slash); + abs_path = std::string{slash} + abs_path; #endif // defined(_WIN32) return "file://" + abs_path; } @@ -201,31 +241,11 @@ auto make_file_uri(std::wstring_view path) -> std::wstring { return utils::string::from_utf8(make_file_uri(utils::string::to_utf8(path))); } -auto remove_file_name(std::string_view path) -> std::string { - auto abs_path = absolute(path); - -#if defined(_WIN32) - ::PathRemoveFileSpecA(abs_path.data()); - abs_path = abs_path.c_str(); -#else // !defined(_WIN32) - if (abs_path != "/") { - auto idx{abs_path.size() - 1U}; - while ((idx != 0U) && (abs_path.at(idx) != '/')) { - idx--; - } - - abs_path = (idx > 0U) ? absolute(abs_path.substr(0U, idx)) : "/"; - } -#endif // defined(_WIN32) - - return abs_path; -} - auto strip_to_file_name(std::string path) -> std::string { #if defined(_WIN32) return ::PathFindFileNameA(path.c_str()); #else // !defined(_WIN32) - return utils::string::contains(path, "/") ? basename(path.data()) : path; + return utils::string::contains(path, slash) ? basename(path.data()) : path; #endif // defined(_WIN32) } diff --git a/support/src/utils/time.cpp b/support/src/utils/time.cpp index 0c30977e..3460cfb1 100644 --- a/support/src/utils/time.cpp +++ b/support/src/utils/time.cpp @@ -22,32 +22,6 @@ #include "utils/time.hpp" namespace repertory::utils::time { -#if defined(_WIN32) -// https://www.frenk.com/2009/12/convert-filetime-to-unix-timestamp/ -auto filetime_to_unix_time(const FILETIME &file_time) -> std::uint64_t { - LARGE_INTEGER date{}; - date.HighPart = static_cast(file_time.dwHighDateTime); - date.LowPart = file_time.dwLowDateTime; - date.QuadPart -= 116444736000000000LL; - - return static_cast(date.QuadPart) * 100ULL; -} -#endif // defined(_WIN32) - -auto get_file_time_now() -> std::uint64_t { -#if defined(_WIN32) - SYSTEMTIME sys_time{}; - ::GetSystemTime(&sys_time); - - FILETIME file_time{}; - ::SystemTimeToFileTime(&sys_time, &file_time); - return static_cast( - (reinterpret_cast(&file_time))->QuadPart); -#else // !defined(_WIN32) - return get_time_now(); -#endif // defined(_WIN32) -} - void get_local_time_now(struct tm &local_time) { std::memset(&local_time, 0, sizeof(local_time)); @@ -61,21 +35,10 @@ void get_local_time_now(struct tm &local_time) { } auto get_time_now() -> std::uint64_t { -#if defined(_WIN32) return static_cast( - std::chrono::system_clock::to_time_t(std::chrono::system_clock::now())); -#endif // defined(_WIN32) - -#if defined(__APPLE__) - return std::chrono::nanoseconds( - std::chrono::system_clock::now().time_since_epoch()) - .count(); -#else // !defined(__APPLE__) - return static_cast( - std::chrono::nanoseconds( - std::chrono::high_resolution_clock::now().time_since_epoch()) + std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) .count()); -#endif // defined(__APPLE__) } #if defined(_WIN32) @@ -91,17 +54,35 @@ auto strptime(const char *s, const char *f, struct tm *tm) -> const char * { return reinterpret_cast(s + input.tellg()); } -auto time64_to_unix_time(const __time64_t &time) -> std::uint64_t { - return static_cast(time * NANOS_PER_SECOND); -} - // https://www.frenk.com/2009/12/convert-filetime-to-unix-timestamp/ auto unix_time_to_filetime(std::uint64_t unix_time) -> FILETIME { - const auto win_time = (unix_time / 100ULL) + 116444736000000000ULL; + auto win_time = unix_time_to_windows_time(unix_time); + FILETIME file_time{}; file_time.dwHighDateTime = static_cast(win_time >> 32U); file_time.dwLowDateTime = win_time & 0xFFFFFFFF; return file_time; } + +auto windows_file_time_to_unix_time(FILETIME win_time) -> std::uint64_t { + return windows_time_to_unix_time( + (static_cast(win_time.dwHighDateTime) << 32ULL) | + static_cast(win_time.dwLowDateTime)); +} + +auto windows_time_t_to_unix_time(__time64_t win_time) -> std::uint64_t { + return static_cast( + std::chrono::duration_cast( + std::chrono::system_clock::from_time_t(win_time).time_since_epoch()) + .count()); +} #endif // defined(_WIN32) + +auto unix_time_to_windows_time(std::uint64_t unix_time) -> std::uint64_t { + return (unix_time / WIN32_TIME_NANOS_PER_TICK) + WIN32_TIME_CONVERSION; +} + +auto windows_time_to_unix_time(std::uint64_t win_time) -> std::uint64_t { + return (win_time * WIN32_TIME_NANOS_PER_TICK) - WIN32_TIME_CONVERSION; +} } // namespace repertory::utils::time diff --git a/support/src/utils/windows.cpp b/support/src/utils/windows.cpp index 3b19af33..eb01fd02 100644 --- a/support/src/utils/windows.cpp +++ b/support/src/utils/windows.cpp @@ -76,7 +76,7 @@ auto run_process_elevated(std::vector args) -> int { } std::string full_path; - full_path.resize(MAX_PATH + 1); + full_path.resize(repertory::max_path_length + 1); if (::GetModuleFileNameA(nullptr, full_path.data(), MAX_PATH)) { SHELLEXECUTEINFOA sei{}; diff --git a/repertory/librepertory/include/utils/encrypt.hpp b/support/test/include/test.hpp similarity index 50% rename from repertory/librepertory/include/utils/encrypt.hpp rename to support/test/include/test.hpp index 13147422..1d432b0e 100644 --- a/repertory/librepertory/include/utils/encrypt.hpp +++ b/support/test/include/test.hpp @@ -1,46 +1,61 @@ -/* - Copyright <2018-2024> - - 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. -*/ -#ifndef INCLUDE_UTILS_ENCRYPT_HPP_ -#define INCLUDE_UTILS_ENCRYPT_HPP_ - -#include "types/repertory.hpp" -#include "utils/encryption.hpp" - -namespace repertory::utils::encryption { -using reader_func = std::function; -// Prototypes -[[nodiscard]] auto decrypt_file_path(std::string_view encryption_token, - std::string &file_path) -> api_error; - -[[nodiscard]] auto decrypt_file_name(std::string_view encryption_token, - std::string &file_name) -> api_error; - -[[nodiscard]] auto -read_encrypted_range(const http_range &range, - const utils::encryption::hash_256_t &key, - reader_func reader, std::uint64_t total_size, - data_buffer &data) -> api_error; -} // namespace repertory::utils::encryption - -#endif // INCLUDE_UTILS_ENCRYPT_HPP_ +/* + Copyright <2018-2024> + + 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. +*/ +#ifndef REPERTORY_TEST_INCLUDE_TEST_HPP_ +#define REPERTORY_TEST_INCLUDE_TEST_HPP_ + +#if defined(U) +#undef U +#endif // defined(U) + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +using ::testing::_; +using namespace ::testing; + +#define COMMA , + +#include "utils/all.hpp" + +namespace repertory::test { +[[nodiscard]] auto +create_random_file(std::size_t size) -> utils::file::i_file &; + +[[nodiscard]] auto +generate_test_file_name(std::string_view file_name_no_extension) -> std::string; + +#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) +template +static void decrypt_and_verify(const buffer_t &buffer, std::string_view token, + result_t &result) { + EXPECT_TRUE(utils::encryption::decrypt_data(token, buffer, result)); +} +#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) + +auto generate_test_directory() -> utils::file::i_directory &; + +[[nodiscard]] auto get_test_input_dir() -> std::string; + +[[nodiscard]] auto get_test_output_dir() -> std::string; +} // namespace repertory::test + +#endif // REPERTORY_TEST_INCLUDE_TEST_HPP_ diff --git a/support/test/src/test.cpp b/support/test/src/test.cpp new file mode 100644 index 00000000..d5a1e9e0 --- /dev/null +++ b/support/test/src/test.cpp @@ -0,0 +1,142 @@ +/* + Copyright <2018-2024> + + 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. +*/ +#include "test.hpp" + +namespace { +static std::recursive_mutex file_mtx{}; + +static std::vector> + generated_files{}; + +static void delete_generated_files() { + repertory::recur_mutex_lock lock{file_mtx}; + std::optional parent_path; + for (auto &&path : generated_files) { + if (parent_path->empty()) { + parent_path = repertory::utils::path::get_parent_path(path->get_path()); + } + + [[maybe_unused]] auto removed = path->remove(); + } + generated_files.clear(); + + if (parent_path.has_value()) { + EXPECT_TRUE( + repertory::utils::file::directory(*parent_path).remove_recursively()); + } +} + +struct file_deleter final { + ~file_deleter() { delete_generated_files(); } +}; + +static auto deleter{std::make_unique()}; +} // namespace + +namespace repertory::test { +auto create_random_file(std::size_t size) -> utils::file::i_file & { + auto path = generate_test_file_name("random"); + auto file = utils::file::file::open_or_create_file(path); + EXPECT_TRUE(*file); + if (*file) { + data_buffer buf(size); +#if defined(PROJECT_ENABLE_LIBSODIUM) + randombytes_buf(buf.data(), buf.size()); +#else // !defined(PROJECT_ENABLE_LIBSODIUM) + thread_local std::mt19937 gen(static_cast( + std::time(nullptr) ^ std::random_device{}())); + std::uniform_int_distribution dis(0U, 255U); + std::generate(buf.begin(), buf.end(), [&]() -> auto { return dis(gen); }); +#endif // defined(PROJECT_ENABLE_LIBSODIUM) + + std::size_t bytes_written{}; + EXPECT_TRUE(file->write(buf, 0U, &bytes_written)); + EXPECT_EQ(size, bytes_written); + + EXPECT_EQ(size, file->size()); + } + + recur_mutex_lock lock{file_mtx}; + generated_files.emplace_back(std::move(file)); + return *dynamic_cast(generated_files.back().get()); +} + +auto generate_test_directory() -> utils::file::i_directory & { + auto path = utils::path::combine( + get_test_output_dir(), + { + std::string{"test_dir"} + std::to_string(generated_files.size()), + }); + + recur_mutex_lock lock{file_mtx}; + generated_files.emplace_back(std::unique_ptr( + new utils::file::directory{path})); + + auto &ret = + *dynamic_cast(generated_files.back().get()); + EXPECT_TRUE(ret.create_directory()); + return ret; +} + +auto generate_test_file_name(std::string_view file_name_no_extension) + -> std::string { + auto path = utils::path::combine( + get_test_output_dir(), { + std::string{file_name_no_extension} + + std::to_string(generated_files.size()), + }); + + recur_mutex_lock lock{file_mtx}; + generated_files.emplace_back( + std::unique_ptr(new utils::file::file{path})); + return generated_files.back()->get_path(); +} + +auto get_test_input_dir() -> std::string { + static auto test_path = ([]() -> std::string { + auto dir = utils::get_environment_variable("PROJECT_TEST_DIR"); + return utils::path::combine(dir.empty() ? "." : dir, {"test_config"}); + })(); + + return test_path; +} + +auto get_test_output_dir() -> std::string { + static auto test_path = ([]() -> std::string { + auto temp = utils::file::create_temp_name("project_test"); + +#if defined(_WIN32) + auto path = utils::path::combine("%TEMP%", {temp}); +#else // !defined(_WIN32) + auto path = utils::path::combine("/tmp", {temp}); +#endif // defined(_WIN32) + + if (not utils::file::directory(path).exists()) { + EXPECT_TRUE(utils::file::directory{path}.create_directory()); + } + + return path; + })(); + + return test_path; +} +} // namespace repertory::test diff --git a/support/test/src/utils/collection_test.cpp b/support/test/src/utils/collection_test.cpp new file mode 100644 index 00000000..39d85bb5 --- /dev/null +++ b/support/test/src/utils/collection_test.cpp @@ -0,0 +1,250 @@ +/* + Copyright <2018-2024> + + 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. +*/ +#include "test.hpp" + +namespace repertory { +TEST(utils_collection, excludes) { + auto data = {"cow", "moose", "dog", "chicken"}; + EXPECT_FALSE(utils::collection::excludes(data, "chicken")); + EXPECT_FALSE(utils::collection::excludes(data, "cow")); + EXPECT_FALSE(utils::collection::excludes(data, "dog")); + EXPECT_FALSE(utils::collection::excludes(data, "moose")); + EXPECT_TRUE(utils::collection::excludes(data, "mouse")); +} + +TEST(utils_collection, includes) { + auto data = {"cow", "moose", "dog", "chicken"}; + EXPECT_FALSE(utils::collection::includes(data, "mice")); + EXPECT_TRUE(utils::collection::includes(data, "chicken")); + EXPECT_TRUE(utils::collection::includes(data, "cow")); + EXPECT_TRUE(utils::collection::includes(data, "dog")); + EXPECT_TRUE(utils::collection::includes(data, "moose")); +} + +TEST(utils_collection, from_hex_string) { + { + auto data = "0xABCDEF10"; + std::vector val{}; + EXPECT_TRUE(utils::collection::from_hex_string(data, val)); + EXPECT_EQ(4U, val.size()); + } + + { + auto data = " 0xABCDEF10 "; + std::vector val{}; + EXPECT_TRUE(utils::collection::from_hex_string(data, val)); + EXPECT_EQ(4U, val.size()); + } + + { + auto data = "ABCDEF10"; + std::vector val{}; + EXPECT_TRUE(utils::collection::from_hex_string(data, val)); + EXPECT_EQ(4U, val.size()); + } + + { + auto data = "ACDEF"; + std::vector val{}; + EXPECT_TRUE(utils::collection::from_hex_string(data, val)); + EXPECT_EQ(3U, val.size()); + } + + { + auto data = " ACDEF "; + std::vector val{}; + EXPECT_TRUE(utils::collection::from_hex_string(data, val)); + EXPECT_EQ(3U, val.size()); + } + + { + auto data = ""; + std::vector val{}; + EXPECT_TRUE(utils::collection::from_hex_string(data, val)); + EXPECT_TRUE(val.empty()); + } + + { + auto data = L"0xABCDEF10"; + std::vector val{}; + EXPECT_TRUE(utils::collection::from_hex_string(data, val)); + EXPECT_EQ(4U, val.size()); + } + + { + auto data = L" 0xABCDEF10 "; + std::vector val{}; + EXPECT_TRUE(utils::collection::from_hex_string(data, val)); + EXPECT_EQ(4U, val.size()); + } + + { + auto data = L"ABCDEF10"; + std::vector val{}; + EXPECT_TRUE(utils::collection::from_hex_string(data, val)); + EXPECT_EQ(4U, val.size()); + } + + { + auto data = L"ACDEF"; + std::vector val{}; + EXPECT_TRUE(utils::collection::from_hex_string(data, val)); + EXPECT_EQ(3U, val.size()); + } + + { + auto data = L" ACDEF "; + std::vector val{}; + EXPECT_TRUE(utils::collection::from_hex_string(data, val)); + EXPECT_EQ(3U, val.size()); + } + + { + auto data = L""; + std::vector val{}; + EXPECT_TRUE(utils::collection::from_hex_string(data, val)); + EXPECT_TRUE(val.empty()); + } +} + +TEST(utils_collection, from_hex_string_fails) { + { + auto data = "ABCDEF1Z"; + std::vector val{}; + EXPECT_FALSE(utils::collection::from_hex_string(data, val)); + EXPECT_TRUE(val.empty()); + } + + { + auto data = "ABC DEF1"; + std::vector val{}; + EXPECT_FALSE(utils::collection::from_hex_string(data, val)); + EXPECT_TRUE(val.empty()); + } + + { + auto data = "0x"; + std::vector val{}; + EXPECT_FALSE(utils::collection::from_hex_string(data, val)); + EXPECT_TRUE(val.empty()); + } + + { + auto data = " 0x "; + std::vector val{}; + EXPECT_FALSE(utils::collection::from_hex_string(data, val)); + EXPECT_TRUE(val.empty()); + } + + { + auto data = L"ABCDEF1Z"; + std::vector val{}; + EXPECT_FALSE(utils::collection::from_hex_string(data, val)); + EXPECT_TRUE(val.empty()); + } + + { + auto data = L"ABC DEF1"; + std::vector val{}; + EXPECT_FALSE(utils::collection::from_hex_string(data, val)); + EXPECT_TRUE(val.empty()); + } + + { + auto data = L"0x"; + std::vector val{}; + EXPECT_FALSE(utils::collection::from_hex_string(data, val)); + EXPECT_TRUE(val.empty()); + } + + { + auto data = L" 0x"; + std::vector val{}; + EXPECT_FALSE(utils::collection::from_hex_string(data, val)); + EXPECT_TRUE(val.empty()); + } +} + +TEST(utils_collection, to_hex_string) { + { + std::array col{ + static_cast(0xFF), + static_cast(0xEE), + }; + + auto str = utils::collection::to_hex_string(col); + EXPECT_STREQ("ffee", str.c_str()); + + auto w_str = utils::collection::to_hex_wstring(col); + EXPECT_STREQ(L"ffee", w_str.c_str()); + } + + { + std::array col{ + static_cast(0xFF), + static_cast(0xEE), + }; + + auto str = utils::collection::to_hex_string(col); + EXPECT_STREQ("ffee", str.c_str()); + + auto w_str = utils::collection::to_hex_wstring(col); + EXPECT_STREQ(L"ffee", w_str.c_str()); + } +} + +TEST(utils_collection, remove_element) { + { + std::vector col{ + static_cast(0xFF), + static_cast(0xEE), + }; + + utils::collection::remove_element(col, 0xFF); + EXPECT_EQ(1U, col.size()); + EXPECT_EQ(static_cast(0xEE), col.at(0U)); + } + + { + std::vector col{ + static_cast(0xFF), + static_cast(0xEE), + }; + + utils::collection::remove_element(col, 0xEE); + EXPECT_EQ(1U, col.size()); + EXPECT_EQ(static_cast(0xFF), col.at(0U)); + } + + { + std::vector col{ + static_cast(0xFF), + static_cast(0xEE), + }; + + utils::collection::remove_element(col, 0xEF); + EXPECT_EQ(2U, col.size()); + EXPECT_EQ(static_cast(0xFF), col.at(0U)); + EXPECT_EQ(static_cast(0xEE), col.at(1U)); + } +} +} // namespace repertory diff --git a/support/test/src/utils/common_test.cpp b/support/test/src/utils/common_test.cpp index 0d1d822d..5317ce8f 100644 --- a/support/test/src/utils/common_test.cpp +++ b/support/test/src/utils/common_test.cpp @@ -19,12 +19,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "gtest/gtest.h" - -#include "utils/common.hpp" - -#include "utils/collection.hpp" -#include "utils/string.hpp" +#include "test.hpp" namespace repertory { TEST(utils_common, calculate_read_size) { @@ -129,40 +124,40 @@ TEST(utils_common, create_uuid_string) { #endif // defined(PROJECT_ENABLE_STDUUID) #if defined(PROJECT_ENABLE_LIBSODIUM) -TEST(utils_common, generate_random) { +TEST(utils_common, generate_secure_random) { { - auto r1 = utils::generate_random(); - auto r2 = utils::generate_random(); + auto r1 = utils::generate_secure_random(); + auto r2 = utils::generate_secure_random(); EXPECT_NE(r1, r2); } { - auto r1 = utils::generate_random>(6U); - auto r2 = utils::generate_random>(6U); + auto r1 = utils::generate_secure_random>(6U); + auto r2 = utils::generate_secure_random>(6U); EXPECT_EQ(6U, r1.size()); EXPECT_EQ(r1.size(), r2.size()); EXPECT_NE(r1, r2); } { - auto r1 = utils::generate_random>(); - auto r2 = utils::generate_random>(); + auto r1 = utils::generate_secure_random>(); + auto r2 = utils::generate_secure_random>(); EXPECT_EQ(4U, r1.size()); EXPECT_EQ(r1.size(), r2.size()); EXPECT_NE(0, std::memcmp(r1.data(), r2.data(), r1.size())); } { - auto r1 = utils::generate_random(6U); - auto r2 = utils::generate_random(6U); + auto r1 = utils::generate_secure_random(6U); + auto r2 = utils::generate_secure_random(6U); EXPECT_EQ(6U, r1.size()); EXPECT_EQ(r1.size(), r2.size()); EXPECT_NE(0, std::memcmp(r1.data(), r2.data(), r1.size())); } { - auto r1 = utils::generate_random(6U); - auto r2 = utils::generate_random(6U); + auto r1 = utils::generate_secure_random(6U); + auto r2 = utils::generate_secure_random(6U); EXPECT_EQ(6U, r1.size()); EXPECT_EQ(r1.size(), r2.size()); EXPECT_NE(0, std::memcmp(r1.data(), r2.data(), r1.size())); @@ -243,7 +238,6 @@ TEST(utils_common, generate_random_between_throws_error_on_invalid_range) { std::range_error); } -#if defined(PROJECT_ENABLE_LIBSODIUM) TEST(utils_common, generate_random_string) { static constexpr const auto max_iterations{10000L}; @@ -271,14 +265,13 @@ TEST(utils_common, generate_random_string_for_zero_length) { EXPECT_TRUE(utils::generate_random_string(0U).empty()); EXPECT_TRUE(utils::generate_random_wstring(0U).empty()); } -#endif // defined(PROJECT_ENABLE_LIBSODIUM) TEST(utils_common, get_environment_variable) { static constexpr const std::string path_env{"PATH"}; std::string path; #if defined(_WIN32) - path.resize(MAX_PATH + 1U); + path.resize(repertory::max_path_length + 1U); auto size = ::GetEnvironmentVariableA(path_env.c_str(), path.data(), 0U); path.resize(size); @@ -298,7 +291,7 @@ TEST(utils_common, get_environment_variable) { #if defined(PROJECT_ENABLE_BOOST) TEST(utils_common, get_next_available_port) { std::uint16_t available_port{}; - for (std::uint16_t port = 1U; port < 65535; ++port) { + for (std::uint16_t port = 1025U; port < 1030U; ++port) { EXPECT_TRUE(utils::get_next_available_port(port, available_port)); EXPECT_GE(available_port, port); } diff --git a/repertory/repertory_test/src/encrypting_reader_test.cpp b/support/test/src/utils/encrypting_reader_test.cpp similarity index 57% rename from repertory/repertory_test/src/encrypting_reader_test.cpp rename to support/test/src/utils/encrypting_reader_test.cpp index 31f4c27e..078dba8a 100644 --- a/repertory/repertory_test/src/encrypting_reader_test.cpp +++ b/support/test/src/utils/encrypting_reader_test.cpp @@ -1,283 +1,225 @@ -/* - Copyright <2018-2024> - - 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. -*/ -#include "test_common.hpp" - -#include "types/repertory.hpp" -#include "utils/encrypting_reader.hpp" -#include "utils/file_utils.hpp" -#include "utils/path.hpp" - -namespace repertory { -static auto get_source_file_name() -> std::string { - return generate_test_file_name("./test_data", "encrypting_reader"); -} - -TEST(encrypting_reader, get_encrypted_file_name) { - const auto source_file_name = get_source_file_name(); - ASSERT_TRUE(utils::file::retry_delete_file(source_file_name)); - - const auto token = std::string("moose"); - auto source_file = create_random_file(source_file_name, 1024UL); - EXPECT_TRUE(source_file != nullptr); - if (source_file) { - stop_type stop_requested = false; - utils::encryption::encrypting_reader reader( - "test.dat", source_file_name, stop_requested, token, std::nullopt); - - auto file_name = reader.get_encrypted_file_name(); - - EXPECT_EQ(api_error::success, - utils::encryption::decrypt_file_name(token, file_name)); - EXPECT_STREQ("test.dat", file_name.c_str()); - - source_file->close(); - } - - EXPECT_TRUE(utils::file::retry_delete_file(source_file_name)); -} - -TEST(encrypting_reader, file_data) { - const auto source_file_name = get_source_file_name(); - EXPECT_TRUE(utils::file::retry_delete_file(source_file_name)); - - const auto token = std::string("moose"); - auto source_file = create_random_file( - source_file_name, - 8U * utils::encryption::encrypting_reader::get_data_chunk_size()); - EXPECT_TRUE(source_file != nullptr); - if (source_file) { - stop_type stop_requested = false; - utils::encryption::encrypting_reader reader("test.dat", source_file_name, - stop_requested, token); - - for (std::uint8_t i = 0U; i < 8U; i++) { - data_buffer buffer( - utils::encryption::encrypting_reader::get_encrypted_chunk_size()); - for (std::uint8_t j = 0U; j < 2U; j++) { - EXPECT_EQ( - buffer.size() / 2U, - utils::encryption::encrypting_reader::reader_function( - reinterpret_cast(&buffer[(buffer.size() / 2U) * j]), - buffer.size() / 2U, 1U, &reader)); - } - - data_buffer decrypted_data; - EXPECT_TRUE( - utils::encryption::decrypt_data(token, buffer, decrypted_data)); - - EXPECT_EQ(utils::encryption::encrypting_reader::get_data_chunk_size(), - decrypted_data.size()); - - std::size_t bytes_read{}; - data_buffer file_data(decrypted_data.size()); - EXPECT_TRUE(source_file->read_bytes( - file_data.data(), file_data.size(), - utils::encryption::encrypting_reader::get_data_chunk_size() * i, - bytes_read)); - EXPECT_EQ(0, std::memcmp(file_data.data(), decrypted_data.data(), - file_data.size())); - } - - source_file->close(); - } - EXPECT_TRUE(utils::file::retry_delete_file(source_file_name)); -} - -TEST(encrypting_reader, file_data_in_multiple_chunks) { - const auto source_file_name = get_source_file_name(); - ASSERT_TRUE(utils::file::retry_delete_file(source_file_name)); - - const auto token = std::string("moose"); - auto source_file = create_random_file( - source_file_name, - 8U * utils::encryption::encrypting_reader::get_data_chunk_size()); - EXPECT_TRUE(source_file != nullptr); - if (source_file) { - stop_type stop_requested = false; - utils::encryption::encrypting_reader reader("test.dat", source_file_name, - stop_requested, token); - - for (std::uint8_t i = 0U; i < 8U; i += 2U) { - data_buffer buffer( - utils::encryption::encrypting_reader::get_encrypted_chunk_size() * - 2U); - EXPECT_EQ(buffer.size(), - utils::encryption::encrypting_reader::reader_function( - reinterpret_cast(buffer.data()), buffer.size(), 1U, - &reader)); - - for (std::uint8_t j = 0U; j < 2U; j++) { - data_buffer decrypted_data; - const auto offset = (j * (buffer.size() / 2U)); - EXPECT_TRUE(utils::encryption::decrypt_data( - token, - data_buffer( - std::next(buffer.begin(), static_cast(offset)), - std::next(buffer.begin(), static_cast( - offset + (buffer.size() / 2U)))), - decrypted_data)); - - EXPECT_EQ(utils::encryption::encrypting_reader::get_data_chunk_size(), - decrypted_data.size()); - - std::size_t bytes_read{}; - data_buffer file_data(decrypted_data.size()); - EXPECT_TRUE(source_file->read_bytes( - file_data.data(), file_data.size(), - (utils::encryption::encrypting_reader::get_data_chunk_size() * i) + - (j * - utils::encryption::encrypting_reader::get_data_chunk_size()), - bytes_read)); - EXPECT_EQ(0, std::memcmp(file_data.data(), decrypted_data.data(), - file_data.size())); - } - } - - source_file->close(); - } - - EXPECT_TRUE(utils::file::retry_delete_file(source_file_name)); -} - -TEST(encrypting_reader, file_data_as_stream) { - const auto source_file_name = get_source_file_name(); - ASSERT_TRUE(utils::file::retry_delete_file(source_file_name)); - - const auto token = std::string("moose"); - auto source_file = create_random_file( - source_file_name, - 8U * utils::encryption::encrypting_reader::get_data_chunk_size()); - EXPECT_TRUE(source_file != nullptr); - if (source_file) { - stop_type stop_requested = false; - utils::encryption::encrypting_reader reader("test.dat", source_file_name, - stop_requested, token); - auto io_stream = reader.create_iostream(); - EXPECT_FALSE(io_stream->seekg(0, std::ios_base::end).fail()); - EXPECT_TRUE(io_stream->good()); - EXPECT_EQ(reader.get_total_size(), - static_cast(io_stream->tellg())); - EXPECT_FALSE(io_stream->seekg(0, std::ios_base::beg).fail()); - EXPECT_TRUE(io_stream->good()); - - for (std::uint8_t i = 0U; i < 8U; i++) { - data_buffer buffer( - utils::encryption::encrypting_reader::get_encrypted_chunk_size()); - EXPECT_FALSE( - io_stream->seekg(static_cast(i * buffer.size())) - .fail()); - EXPECT_TRUE(io_stream->good()); - for (std::uint8_t j = 0U; j < 2U; j++) { - EXPECT_FALSE( - io_stream - ->read( - reinterpret_cast(&buffer[(buffer.size() / 2U) * j]), - static_cast(buffer.size()) / 2U) - .fail()); - EXPECT_TRUE(io_stream->good()); - } - - data_buffer decrypted_data; - EXPECT_TRUE( - utils::encryption::decrypt_data(token, buffer, decrypted_data)); - - EXPECT_EQ(utils::encryption::encrypting_reader::get_data_chunk_size(), - decrypted_data.size()); - - std::size_t bytes_read{}; - data_buffer file_data(decrypted_data.size()); - EXPECT_TRUE(source_file->read_bytes( - file_data.data(), file_data.size(), - utils::encryption::encrypting_reader::get_data_chunk_size() * i, - bytes_read)); - EXPECT_EQ(0, std::memcmp(file_data.data(), decrypted_data.data(), - file_data.size())); - } - - source_file->close(); - } - EXPECT_TRUE(utils::file::retry_delete_file(source_file_name)); -} - -TEST(encrypting_reader, file_data_in_multiple_chunks_as_stream) { - const auto source_file_name = get_source_file_name(); - ASSERT_TRUE(utils::file::retry_delete_file(source_file_name)); - - const auto token = std::string("moose"); - auto source_file = create_random_file( - source_file_name, - 8u * utils::encryption::encrypting_reader::get_data_chunk_size()); - EXPECT_TRUE(source_file != nullptr); - if (source_file) { - stop_type stop_requested = false; - utils::encryption::encrypting_reader reader("test.dat", source_file_name, - stop_requested, token); - auto io_stream = reader.create_iostream(); - EXPECT_FALSE(io_stream->seekg(0, std::ios_base::end).fail()); - EXPECT_TRUE(io_stream->good()); - EXPECT_EQ(reader.get_total_size(), - static_cast(io_stream->tellg())); - EXPECT_FALSE(io_stream->seekg(0, std::ios_base::beg).fail()); - EXPECT_TRUE(io_stream->good()); - - for (std::uint8_t i = 0U; i < 8U; i += 2U) { - data_buffer buffer( - utils::encryption::encrypting_reader::get_encrypted_chunk_size() * - 2U); - EXPECT_FALSE(io_stream - ->read(reinterpret_cast(buffer.data()), - static_cast(buffer.size())) - .fail()); - EXPECT_TRUE(io_stream->good()); - - for (std::uint8_t j = 0U; j < 2U; j++) { - data_buffer decrypted_data; - const auto offset = (j * (buffer.size() / 2U)); - EXPECT_TRUE(utils::encryption::decrypt_data( - token, - data_buffer( - std::next(buffer.begin(), static_cast(offset)), - std::next(buffer.begin(), static_cast( - offset + (buffer.size() / 2U)))), - decrypted_data)); - - EXPECT_EQ(utils::encryption::encrypting_reader::get_data_chunk_size(), - decrypted_data.size()); - - std::size_t bytes_read{}; - data_buffer file_data(decrypted_data.size()); - EXPECT_TRUE(source_file->read_bytes( - file_data.data(), file_data.size(), - (utils::encryption::encrypting_reader::get_data_chunk_size() * i) + - (j * - utils::encryption::encrypting_reader::get_data_chunk_size()), - bytes_read)); - EXPECT_EQ(0, std::memcmp(file_data.data(), decrypted_data.data(), - file_data.size())); - } - } - - source_file->close(); - } - EXPECT_TRUE(utils::file::retry_delete_file(source_file_name)); -} -} // namespace repertory +/* + Copyright <2018-2024> + + 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. +*/ +#include "test.hpp" + +#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) + +namespace repertory { +TEST(utils_encrypting_reader, read_file_data) { + const auto token = std::string("moose"); + auto &source_file = test::create_random_file( + 8U * utils::encryption::encrypting_reader::get_data_chunk_size()); + EXPECT_TRUE(source_file); + if (source_file) { + stop_type stop_requested{false}; + utils::encryption::encrypting_reader reader( + "test.dat", source_file.get_path(), stop_requested, token); + + for (std::uint8_t i = 0U; i < 8U; i++) { + data_buffer buffer( + utils::encryption::encrypting_reader::get_encrypted_chunk_size()); + for (std::uint8_t j = 0U; j < 2U; j++) { + ASSERT_EQ( + buffer.size() / 2U, + utils::encryption::encrypting_reader::reader_function( + reinterpret_cast(&buffer[(buffer.size() / 2U) * j]), + buffer.size() / 2U, 1U, &reader)); + } + + data_buffer decrypted_data; + EXPECT_TRUE( + utils::encryption::decrypt_data(token, buffer, decrypted_data)); + + EXPECT_EQ(utils::encryption::encrypting_reader::get_data_chunk_size(), + decrypted_data.size()); + + std::size_t bytes_read{}; + data_buffer file_data(decrypted_data.size()); + EXPECT_TRUE(source_file.read( + file_data, + utils::encryption::encrypting_reader::get_data_chunk_size() * i, + &bytes_read)); + EXPECT_EQ(0, std::memcmp(file_data.data(), decrypted_data.data(), + file_data.size())); + } + } +} + +TEST(utils_encrypting_reader, read_file_data_in_multiple_chunks) { + const auto token = std::string("moose"); + auto &source_file = test::create_random_file( + 8U * utils::encryption::encrypting_reader::get_data_chunk_size()); + EXPECT_TRUE(source_file); + if (source_file) { + stop_type stop_requested{false}; + utils::encryption::encrypting_reader reader( + "test.dat", source_file.get_path(), stop_requested, token); + + for (std::uint8_t i = 0U; i < 8U; i += 2U) { + data_buffer buffer( + utils::encryption::encrypting_reader::get_encrypted_chunk_size() * + 2U); + EXPECT_EQ(buffer.size(), + utils::encryption::encrypting_reader::reader_function( + reinterpret_cast(buffer.data()), buffer.size(), 1U, + &reader)); + + for (std::uint8_t j = 0U; j < 2U; j++) { + data_buffer decrypted_data; + const auto offset = (j * (buffer.size() / 2U)); + EXPECT_TRUE(utils::encryption::decrypt_data( + token, + data_buffer( + std::next(buffer.begin(), static_cast(offset)), + std::next(buffer.begin(), static_cast( + offset + (buffer.size() / 2U)))), + decrypted_data)); + + EXPECT_EQ(utils::encryption::encrypting_reader::get_data_chunk_size(), + decrypted_data.size()); + + std::size_t bytes_read{}; + data_buffer file_data(decrypted_data.size()); + EXPECT_TRUE(source_file.read( + file_data, + (utils::encryption::encrypting_reader::get_data_chunk_size() * i) + + (j * + utils::encryption::encrypting_reader::get_data_chunk_size()), + &bytes_read)); + EXPECT_EQ(0, std::memcmp(file_data.data(), decrypted_data.data(), + file_data.size())); + } + } + } +} + +TEST(utils_encrypting_reader, read_file_data_as_stream) { + const auto token = std::string("moose"); + auto &source_file = test::create_random_file( + 8U * utils::encryption::encrypting_reader::get_data_chunk_size()); + EXPECT_TRUE(source_file); + if (source_file) { + stop_type stop_requested{false}; + utils::encryption::encrypting_reader reader( + "test.dat", source_file.get_path(), stop_requested, token); + auto io_stream = reader.create_iostream(); + EXPECT_FALSE(io_stream->seekg(0, std::ios_base::end).fail()); + EXPECT_TRUE(io_stream->good()); + EXPECT_EQ(reader.get_total_size(), + static_cast(io_stream->tellg())); + EXPECT_FALSE(io_stream->seekg(0, std::ios_base::beg).fail()); + EXPECT_TRUE(io_stream->good()); + + for (std::uint8_t i = 0U; i < 8U; i++) { + data_buffer buffer( + utils::encryption::encrypting_reader::get_encrypted_chunk_size()); + EXPECT_FALSE( + io_stream->seekg(static_cast(i * buffer.size())) + .fail()); + EXPECT_TRUE(io_stream->good()); + for (std::uint8_t j = 0U; j < 2U; j++) { + EXPECT_FALSE( + io_stream + ->read( + reinterpret_cast(&buffer[(buffer.size() / 2U) * j]), + static_cast(buffer.size()) / 2U) + .fail()); + EXPECT_TRUE(io_stream->good()); + } + + data_buffer decrypted_data; + EXPECT_TRUE( + utils::encryption::decrypt_data(token, buffer, decrypted_data)); + + EXPECT_EQ(utils::encryption::encrypting_reader::get_data_chunk_size(), + decrypted_data.size()); + + std::size_t bytes_read{}; + data_buffer file_data(decrypted_data.size()); + EXPECT_TRUE(source_file.read( + file_data, + utils::encryption::encrypting_reader::get_data_chunk_size() * i, + &bytes_read)); + EXPECT_EQ(0, std::memcmp(file_data.data(), decrypted_data.data(), + file_data.size())); + } + } +} + +TEST(utils_encrypting_reader, read_file_data_in_multiple_chunks_as_stream) { + const auto token = std::string("moose"); + auto &source_file = test::create_random_file( + 8u * utils::encryption::encrypting_reader::get_data_chunk_size()); + EXPECT_TRUE(source_file); + if (source_file) { + stop_type stop_requested{false}; + utils::encryption::encrypting_reader reader( + "test.dat", source_file.get_path(), stop_requested, token); + auto io_stream = reader.create_iostream(); + EXPECT_FALSE(io_stream->seekg(0, std::ios_base::end).fail()); + EXPECT_TRUE(io_stream->good()); + EXPECT_EQ(reader.get_total_size(), + static_cast(io_stream->tellg())); + EXPECT_FALSE(io_stream->seekg(0, std::ios_base::beg).fail()); + EXPECT_TRUE(io_stream->good()); + + for (std::uint8_t i = 0U; i < 8U; i += 2U) { + data_buffer buffer( + utils::encryption::encrypting_reader::get_encrypted_chunk_size() * + 2U); + EXPECT_FALSE(io_stream + ->read(reinterpret_cast(buffer.data()), + static_cast(buffer.size())) + .fail()); + EXPECT_TRUE(io_stream->good()); + + for (std::uint8_t j = 0U; j < 2U; j++) { + data_buffer decrypted_data; + const auto offset = (j * (buffer.size() / 2U)); + EXPECT_TRUE(utils::encryption::decrypt_data( + token, + data_buffer( + std::next(buffer.begin(), static_cast(offset)), + std::next(buffer.begin(), static_cast( + offset + (buffer.size() / 2U)))), + decrypted_data)); + + EXPECT_EQ(utils::encryption::encrypting_reader::get_data_chunk_size(), + decrypted_data.size()); + + std::size_t bytes_read{}; + data_buffer file_data(decrypted_data.size()); + EXPECT_TRUE(source_file.read( + file_data, + (utils::encryption::encrypting_reader::get_data_chunk_size() * i) + + (j * + utils::encryption::encrypting_reader::get_data_chunk_size()), + &bytes_read)); + EXPECT_EQ(0, std::memcmp(file_data.data(), decrypted_data.data(), + file_data.size())); + } + } + } +} +} // namespace repertory + +#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) diff --git a/support/test/src/utils/encryption_test.cpp b/support/test/src/utils/encryption_test.cpp index 73c1dec7..6a0e1913 100644 --- a/support/test/src/utils/encryption_test.cpp +++ b/support/test/src/utils/encryption_test.cpp @@ -19,22 +19,19 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include "test.hpp" + #if defined(PROJECT_ENABLE_LIBSODIUM) -#include "gtest/gtest.h" - -#include "utils/collection.hpp" -#include "utils/encryption.hpp" - namespace repertory { -static const std::string token{"moose"}; -static const std::wstring token_w{L"moose"}; +static constexpr const std::string_view token{"moose"}; +static constexpr const std::wstring_view token_w{L"moose"}; TEST(utils_encryption, generate_key) { auto key1 = utils::encryption::generate_key(token); EXPECT_STREQ( - "182072537ada59e4d6b18034a80302ebae935f66adbdf0f271d3d36309c2d481", + "ab4a0b004e824962913f7c0f79582b6ec7a3b8726426ca61d1a0a28ce5049e96", utils::collection::to_hex_string(key1).c_str()); auto key2 = @@ -50,9 +47,15 @@ TEST(utils_encryption, generate_key) { auto key1_w = utils::encryption::generate_key(token_w); EXPECT_NE(key1, key1_w); +#if defined(_WIN32) EXPECT_STREQ( - L"590ac70125bec4501172937f6a2cbdeb22a87b5e40d5595eccd06b2b20548d8f", + L"4f5eb2a2ab34e3777b230465283923080b9ba59311e74058ccd74185131d11fe", utils::collection::to_hex_wstring(key1_w).c_str()); +#else // !defined(_WIN32) + EXPECT_STREQ( + L"0392d95ed3eee9772fbb9af68fedf829a8eb0adbe8575d9691cc9a752196766a", + utils::collection::to_hex_wstring(key1_w).c_str()); +#endif auto key2_w = utils::encryption::generate_key(L"moose"); @@ -68,12 +71,12 @@ TEST(utils_encryption, generate_key) { EXPECT_NE(key4_w, key4); } -TEST(utils_encryption, generate_key_default_hasher_is_sha256) { +TEST(utils_encryption, generate_key_default_hasher_is_blake2b_256) { auto key1 = utils::encryption::generate_key(token); auto key2 = utils::encryption::generate_key( token, [](auto &&data, auto &&size) -> auto { - return utils::encryption::create_hash_sha256( + return utils::encryption::create_hash_blake2b_256( std::string_view(reinterpret_cast(data), size)); }); EXPECT_EQ(key1, key2); @@ -82,7 +85,7 @@ TEST(utils_encryption, generate_key_default_hasher_is_sha256) { utils::encryption::generate_key(token_w); auto key2_w = utils::encryption::generate_key( token_w, [](auto &&data, auto &&size) -> auto { - return utils::encryption::create_hash_sha256(std::wstring_view( + return utils::encryption::create_hash_blake2b_256(std::wstring_view( reinterpret_cast(data), size / sizeof(wchar_t))); }); EXPECT_EQ(key1_w, key2_w); @@ -93,44 +96,44 @@ TEST(utils_encryption, generate_key_default_hasher_is_sha256) { TEST(utils_encryption, generate_key_with_hasher) { auto key1 = utils::encryption::generate_key( - token, [](auto &&data, auto &&size) -> auto { - return utils::encryption::create_hash_sha256( - std::string_view(reinterpret_cast(data), size)); - }); + token, utils::encryption::blake2b_256_hasher); EXPECT_STREQ( - "182072537ada59e4d6b18034a80302ebae935f66adbdf0f271d3d36309c2d481", + "ab4a0b004e824962913f7c0f79582b6ec7a3b8726426ca61d1a0a28ce5049e96", utils::collection::to_hex_string(key1).c_str()); auto key2 = utils::encryption::generate_key( - token, [](auto &&data, auto &&size) -> auto { - return utils::encryption::create_hash_blake2b_256( - std::string_view(reinterpret_cast(data), size)); - }); + token, utils::encryption::sha256_hasher); EXPECT_NE(key1, key2); EXPECT_STREQ( - "ab4a0b004e824962913f7c0f79582b6ec7a3b8726426ca61d1a0a28ce5049e96", + "182072537ada59e4d6b18034a80302ebae935f66adbdf0f271d3d36309c2d481", utils::collection::to_hex_string(key2).c_str()); auto key1_w = utils::encryption::generate_key( - token_w, [](auto &&data, auto &&size) -> auto { - return utils::encryption::create_hash_sha256(std::wstring_view( - reinterpret_cast(data), size / sizeof(wchar_t))); - }); + token_w, utils::encryption::blake2b_256_hasher); +#if defined(_WIN32) EXPECT_STREQ( - L"590ac70125bec4501172937f6a2cbdeb22a87b5e40d5595eccd06b2b20548d8f", + L"4f5eb2a2ab34e3777b230465283923080b9ba59311e74058ccd74185131d11fe", utils::collection::to_hex_wstring(key1_w).c_str()); - - auto key2_w = utils::encryption::generate_key( - token_w, [](auto &&data, auto &&size) -> auto { - return utils::encryption::create_hash_blake2b_256(std::wstring_view( - reinterpret_cast(data), size / sizeof(wchar_t))); - }); - EXPECT_NE(key1_w, key2_w); - +#else // !defined(_WIN32) EXPECT_STREQ( L"0392d95ed3eee9772fbb9af68fedf829a8eb0adbe8575d9691cc9a752196766a", + utils::collection::to_hex_wstring(key1_w).c_str()); +#endif + + auto key2_w = utils::encryption::generate_key( + token_w, utils::encryption::sha256_hasher); + EXPECT_NE(key1_w, key2_w); + +#if defined(_WIN32) + EXPECT_STREQ( + L"918e4c6d39bb373f139b5fac8ec0548a9770da399b2835608974ffeac7fab6c4", utils::collection::to_hex_wstring(key2_w).c_str()); +#else // !defined(_WIN32) + EXPECT_STREQ( + L"590ac70125bec4501172937f6a2cbdeb22a87b5e40d5595eccd06b2b20548d8f", + utils::collection::to_hex_wstring(key2_w).c_str()); +#endif EXPECT_NE(key1_w, key1); EXPECT_NE(key2_w, key2); @@ -241,6 +244,39 @@ TEST(utils_encryption, decryption_failure) { std::string data; EXPECT_FALSE(utils::encryption::decrypt_data(key, result, data)); } + +TEST(utils_encryption, decrypt_file_name) { + auto &source_file = test::create_random_file( + 8U * utils::encryption::encrypting_reader::get_data_chunk_size()); + EXPECT_TRUE(source_file); + if (source_file) { + stop_type stop_requested{false}; + utils::encryption::encrypting_reader reader( + "test.dat", source_file.get_path(), stop_requested, token, + std::nullopt); + + auto file_name = reader.get_encrypted_file_name(); + + EXPECT_EQ(true, utils::encryption::decrypt_file_name(token, file_name)); + EXPECT_STREQ("test.dat", file_name.c_str()); + } +} + +TEST(utils_encryption, decrypt_file_path) { + auto &source_file = test::create_random_file( + 8U * utils::encryption::encrypting_reader::get_data_chunk_size()); + EXPECT_TRUE(source_file); + if (source_file) { + stop_type stop_requested{false}; + utils::encryption::encrypting_reader reader( + "test.dat", source_file.get_path(), stop_requested, token, "moose/cow"); + + auto file_path = reader.get_encrypted_file_path(); + + EXPECT_EQ(true, utils::encryption::decrypt_file_path(token, file_path)); + EXPECT_STREQ("/moose/cow/test.dat", file_path.c_str()); + } +} #endif // defined(PROJECT_ENABLE_BOOST) } // namespace repertory diff --git a/support/test/src/utils/error_test.cpp b/support/test/src/utils/error_test.cpp new file mode 100644 index 00000000..ccb710cb --- /dev/null +++ b/support/test/src/utils/error_test.cpp @@ -0,0 +1,66 @@ +/* + Copyright <2018-2024> + + 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. +*/ +#include "test.hpp" + +namespace repertory { +template +constexpr bool is_decay_equ = std::is_same_v, U>; + +TEST(utils_error, check_default_exception_handler) { + EXPECT_TRUE(utils::error::get_exception_handler() != nullptr); + if (&utils::error::default_exception_handler == + utils::error::get_exception_handler()) { + auto default_handler_is_iostream = + is_decay_equ; + EXPECT_TRUE(default_handler_is_iostream); + } +} + +TEST(utils_error, can_override_exception_handler) { + struct my_exc_handler : public utils::error::i_exception_handler { + MOCK_METHOD(void, handle_exception, (std::string_view function_name), + (const, override)); + + MOCK_METHOD(void, handle_exception, + (std::string_view function_name, const std::exception &ex), + (const, override)); + }; + + my_exc_handler handler{}; + utils::error::set_exception_handler(&handler); + + EXPECT_CALL(handler, handle_exception("test_func")).WillOnce(Return()); + utils::error::handle_exception("test_func"); + + auto ex = std::runtime_error("moose"); + EXPECT_CALL(handler, handle_exception(_, _)) + .WillOnce( + [&ex](std::string_view function_name, const std::exception &ex2) { + EXPECT_EQ("test_func_ex", function_name); + EXPECT_STREQ(ex.what(), ex2.what()); + }); + utils::error::handle_exception("test_func_ex", ex); + + utils::error::set_exception_handler(&utils::error::default_exception_handler); +} +} // namespace repertory diff --git a/support/test/src/utils/file_test.cpp b/support/test/src/utils/file_test.cpp new file mode 100644 index 00000000..d726c336 --- /dev/null +++ b/support/test/src/utils/file_test.cpp @@ -0,0 +1,517 @@ +/* + Copyright <2018-2024> + + 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. +*/ +#include "test.hpp" + +namespace { +static constexpr const auto file_type_count{1U}; +} + +namespace repertory { +TEST(utils_file, can_create_file) { + for (auto idx = 0U; idx < file_type_count; ++idx) { + auto path = test::generate_test_file_name("utils_file"); + EXPECT_FALSE(utils::file::file(path).exists() || + utils::file::directory(path).exists()); + + auto file = idx == 0U ? utils::file::file::open_or_create_file(path) + : utils::file::thread_file::open_or_create_file(path); + EXPECT_TRUE(*file); + + EXPECT_TRUE(utils::file::file(path).exists()); + } +} + +TEST(utils_file, can_open_file) { + for (auto idx = 0U; idx < file_type_count; ++idx) { + auto path = test::generate_test_file_name("utils_file"); + + { + auto file = idx == 0U + ? utils::file::file::open_or_create_file(path) + : utils::file::thread_file::open_or_create_file(path); + EXPECT_TRUE(*file); + } + + { + auto file = idx == 0U ? utils::file::file::open_file(path) + : utils::file::thread_file::open_file(path); + EXPECT_TRUE(*file); + } + } +} + +TEST(utils_file, open_file_fails_if_not_found) { + for (auto idx = 0U; idx < file_type_count; ++idx) { + auto path = test::generate_test_file_name("utils_file"); + + auto file = idx == 0U ? utils::file::file::open_file(path) + : utils::file::thread_file::open_file(path); + EXPECT_FALSE(*file); + } +} + +TEST(utils_file, write_fails_for_read_only_file) { + for (auto idx = 0U; idx < file_type_count; ++idx) { + auto path = test::generate_test_file_name("utils_file"); + + auto file = idx == 0U + ? utils::file::file::open_or_create_file(path, true) + : utils::file::thread_file::open_or_create_file(path, true); + EXPECT_TRUE(utils::file::file(path).exists()); + EXPECT_TRUE(*file); + std::size_t bytes_written{}; + EXPECT_FALSE(file->write(reinterpret_cast("0"), 1U, + 0U, &bytes_written)); + EXPECT_EQ(0U, bytes_written); + } +} + +// TEST(utils_file, can_attach_file) { +// for (auto idx = 0U; idx < file_type_count; ++idx) { +// auto path = test::generate_test_file_name("utils_file"); +// auto file = idx == 0U ? utils::file::file::open_or_create_file(path) +// : +// utils::file::thread_file::open_or_create_file(path); +// auto file2 = +// idx == 0U ? utils::file::file::attach_file(file->get_handle()) +// : +// utils::file::thread_file::attach_file(file->get_handle()); +// EXPECT_TRUE(*file); +// EXPECT_TRUE(*file2); +// EXPECT_EQ(file->get_path(), file2->get_path()); +// } +// } + +#if defined(PROJECT_ENABLE_JSON) +TEST(utils_file, read_and_write_json_file) { + auto path = test::generate_test_file_name("utils_file"); + + auto json_data = nlohmann::json({{"moose", "cow"}}); + EXPECT_TRUE(utils::file::write_json_file(path, json_data)); + + { + nlohmann::json result_data{}; + EXPECT_TRUE(utils::file::read_json_file(path, result_data)); + EXPECT_STREQ(json_data.dump().c_str(), result_data.dump().c_str()); + } + + { + nlohmann::json result_data{}; + EXPECT_TRUE(utils::file::read_json_file(utils::string::from_utf8(path), + result_data)); + EXPECT_STREQ(json_data.dump().c_str(), result_data.dump().c_str()); + } +} + +#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) +TEST(utils_file, read_and_write_json_file_encrypted) { + { + auto path = test::generate_test_file_name("utils_file"); + + auto json_data = nlohmann::json({{"moose", "cow"}}); + EXPECT_TRUE(utils::file::write_json_file(path, json_data, "moose")); + + nlohmann::json result_data{}; + EXPECT_TRUE(utils::file::read_json_file(path, result_data, "moose")); + EXPECT_STREQ(json_data.dump().c_str(), result_data.dump().c_str()); + + { + auto file = utils::file::file::open_file(path); + data_buffer encrypted_data{}; + EXPECT_TRUE(file->read_all(encrypted_data, 0U)); + + data_buffer decrypted_data{}; + EXPECT_TRUE(utils::encryption::decrypt_data("moose", encrypted_data, + decrypted_data)); + EXPECT_STREQ(json_data.dump().c_str(), + nlohmann::json::parse(std::string(decrypted_data.begin(), + decrypted_data.end())) + .dump() + .c_str()); + } + } + + { + auto path = + utils::string::from_utf8(test::generate_test_file_name("utils_file")); + + auto json_data = nlohmann::json({{"moose", "cow"}}); + EXPECT_TRUE(utils::file::write_json_file(path, json_data, L"moose")); + + nlohmann::json result_data{}; + EXPECT_TRUE(utils::file::read_json_file(path, result_data, L"moose")); + EXPECT_STREQ(json_data.dump().c_str(), result_data.dump().c_str()); + + { + auto file = utils::file::file::open_file(path); + data_buffer encrypted_data{}; + EXPECT_TRUE(file->read_all(encrypted_data, 0U)); + + data_buffer decrypted_data{}; + EXPECT_TRUE(utils::encryption::decrypt_data("moose", encrypted_data, + decrypted_data)); + EXPECT_STREQ(json_data.dump().c_str(), + nlohmann::json::parse(std::string(decrypted_data.begin(), + decrypted_data.end())) + .dump() + .c_str()); + } + } +} +#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) +#endif // defined(PROJECT_ENABLE_JSON) + +#if defined(PROJECT_ENABLE_LIBDSM) +TEST(utils_file, smb_create_smb_path) { + auto path = "//server/share"; + auto rel_path = "test/test.txt"; + auto smb_path = utils::file::smb_create_smb_path(path, rel_path); + EXPECT_STREQ("//server/share/test/test.txt", smb_path.c_str()); + + rel_path = "/test/test.txt"; + smb_path = utils::file::smb_create_smb_path(path, rel_path); + EXPECT_STREQ("//server/share/test/test.txt", smb_path.c_str()); + + rel_path = "test\\test.txt"; + smb_path = utils::file::smb_create_smb_path(path, rel_path); + EXPECT_STREQ("//server/share/test/test.txt", smb_path.c_str()); + + rel_path = "\\test\\test.txt"; + smb_path = utils::file::smb_create_smb_path(path, rel_path); + EXPECT_STREQ("//server/share/test/test.txt", smb_path.c_str()); +} + +TEST(utils_file, smb_create_relative_path) { + auto path = "//server/share/test.txt"; + auto rel_path = utils::file::smb_create_relative_path(path); + EXPECT_STREQ("\\test.txt", rel_path.c_str()); + + path = "//server/share/test"; + rel_path = utils::file::smb_create_relative_path(path); + EXPECT_STREQ("\\test", rel_path.c_str()); + + path = "//server/share/test/"; + rel_path = utils::file::smb_create_relative_path(path); + EXPECT_STREQ("\\test", rel_path.c_str()); + + path = "//server/share/test/"; + rel_path = utils::file::smb_create_relative_path(path); + EXPECT_STREQ("\\test", rel_path.c_str()); +} + +TEST(utils_file, smb_create_search_path) { + auto path = "//server/share"; + auto search_path = utils::file::smb_create_search_path(path); + EXPECT_STREQ("\\*", search_path.c_str()); + + path = "//server/share/"; + search_path = utils::file::smb_create_search_path(path); + EXPECT_STREQ("\\*", search_path.c_str()); + + path = "//server/share/folder"; + search_path = utils::file::smb_create_search_path(path); + EXPECT_STREQ("\\folder\\*", search_path.c_str()); + + path = "//server/share/folder/"; + search_path = utils::file::smb_create_search_path(path); + EXPECT_STREQ("\\folder\\*", search_path.c_str()); + + path = "//server/share/folder/next"; + search_path = utils::file::smb_create_search_path(path); + EXPECT_STREQ("\\folder\\next\\*", search_path.c_str()); + + path = "//server/share/folder/next/"; + search_path = utils::file::smb_create_search_path(path); + EXPECT_STREQ("\\folder\\next\\*", search_path.c_str()); +} + +TEST(utils_file, smb_parent_is_same) { + auto path1 = "//server/share"; + auto path2 = "//server/share"; + EXPECT_TRUE(utils::file::smb_parent_is_same(path1, path2)); + + path1 = "//server/share/"; + path2 = "//server/share/"; + EXPECT_TRUE(utils::file::smb_parent_is_same(path1, path2)); + + path1 = "//server/share/one"; + path2 = "//server/share/two"; + EXPECT_TRUE(utils::file::smb_parent_is_same(path1, path2)); +} + +TEST(utils_file, smb_parent_is_not_same) { + auto path1 = "server/share"; + auto path2 = "//server/share"; + EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2)); + + path1 = "server/share/"; + path2 = "server/share/"; + EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2)); + + path1 = "//server1/share/one"; + path2 = "//server/share/two"; + EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2)); + + path1 = "//server/share"; + path2 = "//server/share2"; + EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2)); + + path1 = "//server/share/"; + path2 = "//server/share2/"; + EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2)); + + path1 = "//server/share/one"; + path2 = "//server/share2/two"; + EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2)); + + path1 = "//server"; + path2 = "//server/share/two"; + EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2)); + + path1 = "//server/"; + path2 = "//server/"; + EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2)); + + path1 = "//server"; + path2 = "//server"; + EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2)); + + path1 = "// server/cow"; + path2 = "// server/cow"; + EXPECT_FALSE(utils::file::smb_parent_is_same(path1, path2)); +} +#endif // defined(PROJECT_ENABLE_LIBDSM) + +TEST(utils_file, directory_exists_in_path) { + auto &test_dir = test::generate_test_directory(); + EXPECT_FALSE( + utils::file::directory_exists_in_path(test_dir.get_path(), "moose")); + + EXPECT_FALSE(utils::file::directory_exists_in_path( + utils::string::from_utf8(test_dir.get_path()), L"moose")); + + EXPECT_FALSE(utils::file::file_exists_in_path(test_dir.get_path(), "moose")); + + EXPECT_FALSE(utils::file::file_exists_in_path( + utils::string::from_utf8(test_dir.get_path()), L"moose")); + + auto sub_dir = test_dir.create_directory("moose"); + EXPECT_TRUE(sub_dir != nullptr); + if (sub_dir) { + EXPECT_TRUE( + utils::file::directory_exists_in_path(test_dir.get_path(), "moose")); + + EXPECT_FALSE( + utils::file::file_exists_in_path(test_dir.get_path(), "moose")); + + EXPECT_TRUE(utils::file::directory_exists_in_path( + utils::string::from_utf8(test_dir.get_path()), L"moose")); + + EXPECT_FALSE(utils::file::file_exists_in_path( + utils::string::from_utf8(test_dir.get_path()), L"moose")); + } +} + +TEST(utils_file, file_exists_in_path) { + auto &test_dir = test::generate_test_directory(); + EXPECT_FALSE( + utils::file::file_exists_in_path(test_dir.get_path(), "moose.txt")); + + EXPECT_FALSE(utils::file::file_exists_in_path( + utils::string::from_utf8(test_dir.get_path()), L"moose.txt")); + + EXPECT_FALSE( + utils::file::directory_exists_in_path(test_dir.get_path(), "moose.txt")); + + EXPECT_FALSE(utils::file::directory_exists_in_path( + utils::string::from_utf8(test_dir.get_path()), L"moose.txt")); + + auto sub_file = test_dir.create_file("moose.txt", false); + EXPECT_TRUE(sub_file != nullptr); + if (sub_file) { + EXPECT_TRUE( + utils::file::file_exists_in_path(test_dir.get_path(), "moose.txt")); + + EXPECT_FALSE(utils::file::directory_exists_in_path(test_dir.get_path(), + "moose.txt")); + + EXPECT_TRUE(utils::file::file_exists_in_path( + utils::string::from_utf8(test_dir.get_path()), L"moose.txt")); + + EXPECT_FALSE(utils::file::directory_exists_in_path( + utils::string::from_utf8(test_dir.get_path()), L"moose.txt")); + } +} + +TEST(utils_file, get_free_drive_space) { +#if defined(_WIN32) + auto space = utils::file::get_free_drive_space("C:"); + auto space2 = utils::file::get_free_drive_space(L"C:"); +#else // defined(_WIN32) + auto space = utils::file::get_free_drive_space("/"); + auto space2 = utils::file::get_free_drive_space(L"/"); +#endif // !defined(_WIN32) + + EXPECT_TRUE(space.has_value()); + EXPECT_LT(0U, space.value()); + + EXPECT_TRUE(space2.has_value()); + EXPECT_EQ(space.value(), space2.value()); +} + +TEST(utils_file, get_free_drive_space_fails_for_bad_path) { + std::string name{"free_drive_space_test_XXXXXX"}; + auto temp = utils::file::create_temp_name("free_drive_space_test"); + + auto space = utils::file::get_free_drive_space(temp); + EXPECT_FALSE(space.has_value()); +} + +TEST(utils_file, get_total_drive_space) { +#if defined(_WIN32) + auto space = utils::file::get_total_drive_space("C:"); + auto space2 = utils::file::get_total_drive_space(L"C:"); +#else // defined(_WIN32) + auto space = utils::file::get_total_drive_space("/"); + auto space2 = utils::file::get_total_drive_space(L"/"); +#endif // !defined(_WIN32) + + EXPECT_TRUE(space.has_value()); + EXPECT_LT(0U, space.value()); + + EXPECT_TRUE(space2.has_value()); + EXPECT_EQ(space.value(), space2.value()); +} + +TEST(utils_file, create_temp_name) { + { + auto temp = utils::file::create_temp_name("test_temp"); + EXPECT_EQ(18U, temp.size()); + + auto temp2 = utils::file::create_temp_name("test_temp"); + EXPECT_STRNE(temp.c_str(), temp2.c_str()); + + EXPECT_TRUE(utils::string::begins_with(temp, "test_temp_")); + } + + { + auto temp = utils::file::create_temp_name(L"test_temp"); + EXPECT_EQ(18U, temp.size()); + + auto temp2 = utils::file::create_temp_name(L"test_temp"); + EXPECT_STRNE(temp.c_str(), temp2.c_str()); + + EXPECT_TRUE(utils::string::begins_with(temp, L"test_temp_")); + } +} + +TEST(utils_file, get_total_drive_space_fails_for_bad_path) { + auto temp = utils::file::create_temp_name("total_drive_space_test"); + auto space = utils::file::get_total_drive_space(temp); + EXPECT_FALSE(space.has_value()); +} + +TEST(utils_file, get_times) { + { + auto times = + utils::file::get_times(test::create_random_file(1U).get_path()); + EXPECT_TRUE(times.has_value()); + EXPECT_LT(0U, times->get(utils::file::time_type::accessed)); + EXPECT_LT(0U, times->get(utils::file::time_type::created)); + EXPECT_LT(0U, times->get(utils::file::time_type::modified)); + EXPECT_LT(0U, times->get(utils::file::time_type::written)); + } + + { + auto times = utils::file::get_times( + utils::string::from_utf8(test::create_random_file(1U).get_path())); + EXPECT_TRUE(times.has_value()); + EXPECT_LT(0U, times->get(utils::file::time_type::accessed)); + EXPECT_LT(0U, times->get(utils::file::time_type::created)); + EXPECT_LT(0U, times->get(utils::file::time_type::modified)); + EXPECT_LT(0U, times->get(utils::file::time_type::written)); + } +} + +TEST(utils_file, get_times_fails_if_not_found) { + auto temp = utils::path::combine(".", {"get_times_test"}); + auto times = utils::file::get_times(temp); + EXPECT_FALSE(times.has_value()); +} + +TEST(utils_file, get_time) { + { + auto file_path = test::create_random_file(1U).get_path(); + auto file_time = + utils::file::get_time(file_path, utils::file::time_type::accessed); + EXPECT_TRUE(file_time.has_value()); + EXPECT_LT(0U, file_time.value()); + + file_time = + utils::file::get_time(file_path, utils::file::time_type::created); + EXPECT_TRUE(file_time.has_value()); + EXPECT_LT(0U, file_time.value()); + + file_time = + utils::file::get_time(file_path, utils::file::time_type::modified); + EXPECT_TRUE(file_time.has_value()); + EXPECT_LT(0U, file_time.value()); + + file_time = + utils::file::get_time(file_path, utils::file::time_type::written); + EXPECT_TRUE(file_time.has_value()); + EXPECT_LT(0U, file_time.value()); + } + + { + auto file_path = + utils::string::from_utf8(test::create_random_file(1U).get_path()); + + auto file_time = + utils::file::get_time(file_path, utils::file::time_type::accessed); + EXPECT_TRUE(file_time.has_value()); + EXPECT_LT(0U, file_time.value()); + + file_time = + utils::file::get_time(file_path, utils::file::time_type::created); + EXPECT_TRUE(file_time.has_value()); + EXPECT_LT(0U, file_time.value()); + + file_time = + utils::file::get_time(file_path, utils::file::time_type::modified); + EXPECT_TRUE(file_time.has_value()); + EXPECT_LT(0U, file_time.value()); + + file_time = + utils::file::get_time(file_path, utils::file::time_type::written); + EXPECT_TRUE(file_time.has_value()); + EXPECT_LT(0U, file_time.value()); + } +} + +TEST(utils_file, get_time_fails_if_not_found) { + auto temp = utils::path::combine(".", {"get_times_test"}); + auto file_time = + utils::file::get_time(temp, utils::file::time_type::accessed); + EXPECT_FALSE(file_time.has_value()); +} +} // namespace repertory diff --git a/support/test/src/utils/hash_test.cpp b/support/test/src/utils/hash_test.cpp new file mode 100644 index 00000000..13287bbd --- /dev/null +++ b/support/test/src/utils/hash_test.cpp @@ -0,0 +1,186 @@ +/* + Copyright <2018-2024> + + 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. +*/ +#include "test.hpp" + +#if defined(PROJECT_ENABLE_LIBSODIUM) + +namespace repertory { +TEST(utils_hash, hash_type_sizes) { + EXPECT_EQ(32U, utils::encryption::hash_256_t{}.size()); + EXPECT_EQ(48U, utils::encryption::hash_384_t{}.size()); + EXPECT_EQ(64U, utils::encryption::hash_512_t{}.size()); +} + +TEST(utils_hash, default_hasher_is_blake2b) { + EXPECT_EQ( + &utils::encryption::blake2b_256_hasher, + &utils::encryption::default_create_hash()); + + EXPECT_EQ( + &utils::encryption::blake2b_384_hasher, + &utils::encryption::default_create_hash()); + + EXPECT_EQ( + &utils::encryption::blake2b_512_hasher, + &utils::encryption::default_create_hash()); +} + +TEST(utils_hash, blake2b_256) { + auto hash = utils::collection::to_hex_string( + utils::encryption::create_hash_blake2b_256("a")); + EXPECT_STREQ( + "8928aae63c84d87ea098564d1e03ad813f107add474e56aedd286349c0c03ea4", + hash.c_str()); + + hash = utils::collection::to_hex_string( + utils::encryption::create_hash_blake2b_256(L"a")); +#if defined(_WIN32) + EXPECT_STREQ( + "d2373b17cd8a8e19e39f52fa4905a274f93805fbb8bb4c7f3cb4b2cd6708ec8a", + hash.c_str()); +#else // !defined(_WIN32) + EXPECT_STREQ( + "9fdf5757d7eea386f0d34d2c0e202527986febf1ebb4315fcf7fff40776fa41d", + hash.c_str()); +#endif + + hash = utils::collection::to_hex_string( + utils::encryption::create_hash_blake2b_256({1U})); + EXPECT_STREQ( + "ee155ace9c40292074cb6aff8c9ccdd273c81648ff1149ef36bcea6ebb8a3e25", + hash.c_str()); +} + +TEST(utils_hash, blake2b_384) { + auto hash = utils::collection::to_hex_string( + utils::encryption::create_hash_blake2b_384("a")); + EXPECT_STREQ("7d40de16ff771d4595bf70cbda0c4ea0a066a6046fa73d34471cd4d93d827d7" + "c94c29399c50de86983af1ec61d5dcef0", + hash.c_str()); + + hash = utils::collection::to_hex_string( + utils::encryption::create_hash_blake2b_384(L"a")); +#if defined(_WIN32) + EXPECT_STREQ("637fe31d1e955760ef31043d525d9321826a778ddbe82fcde45a98394241380" + "96675e2f87e36b53ab223a7fd254198fd", + hash.c_str()); +#else // !defined(_WIN32) + EXPECT_STREQ("9d469bd9dab9d4b48b8688de7c22704a8de1b081294f9be294100dfa9f05c92" + "e8d3616476e46cd14f9e613fed80fd157", + hash.c_str()); +#endif + + hash = utils::collection::to_hex_string( + utils::encryption::create_hash_blake2b_384({1U})); + EXPECT_STREQ("42cfe875d08d816538103b906bb0b05202e0b09c4e981680c1110684fc7845b" + "c91c178fa167afcc445490644b2bf5f5b", + hash.c_str()); +} + +TEST(utils_hash, blake2b_512) { + auto hash = utils::collection::to_hex_string( + utils::encryption::create_hash_blake2b_512("a")); + EXPECT_STREQ( + "333fcb4ee1aa7c115355ec66ceac917c8bfd815bf7587d325aec1864edd24e34d5abe2c6" + "b1b5ee3face62fed78dbef802f2a85cb91d455a8f5249d330853cb3c", + hash.c_str()); + + hash = utils::collection::to_hex_string( + utils::encryption::create_hash_blake2b_512(L"a")); +#if defined(_WIN32) + EXPECT_STREQ( + "05970b95468b0b1941066ff189091493e73859ce41cde5ad08118e93ea1d81a57a144296" + "a26a9fe7781481bde97b886725e36e30b305d8bd5cce1ae36bf1564a", + hash.c_str()); +#else // !defined(_WIN32) + EXPECT_STREQ( + "bbc187c6e4d8525655d0ada62d16eed59f3db3ab07e04fb0483fd4ae21d88b984774add9" + "b3fbcff56f9638091013994f8e2d4646fdbbcb4879e2b5160bbb755d", + hash.c_str()); +#endif + + hash = utils::collection::to_hex_string( + utils::encryption::create_hash_blake2b_512({1U})); + EXPECT_STREQ( + "9545ba37b230d8a2e716c4707586542780815b7c4088edcb9af6a9452d50f32474d5ba9a" + "ab52a67aca864ef2696981c2eadf49020416136afd838fb048d21653", + hash.c_str()); +} + +TEST(utils_hash, sha256) { + auto hash = utils::collection::to_hex_string( + utils::encryption::create_hash_sha256("a")); + EXPECT_STREQ( + "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb", + hash.c_str()); + + hash = utils::collection::to_hex_string( + utils::encryption::create_hash_sha256(L"a")); +#if defined(_WIN32) + EXPECT_STREQ( + "ffe9aaeaa2a2d5048174df0b80599ef0197ec024c4b051bc9860cff58ef7f9f3", + hash.c_str()); +#else // !defined(_WIN32) + EXPECT_STREQ( + "a2d398922901344d08180dc41d3e9d73d8c148c7f6e092835bbb28e02dbcf184", + hash.c_str()); +#endif + + hash = utils::collection::to_hex_string( + utils::encryption::create_hash_sha256({1U})); + EXPECT_STREQ( + "4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a", + hash.c_str()); +} + +TEST(utils_hash, sha512) { + auto hash = utils::collection::to_hex_string( + utils::encryption::create_hash_sha512("a")); + EXPECT_STREQ( + "1f40fc92da241694750979ee6cf582f2d5d7d28e18335de05abc54d0560e0f5302860c65" + "2bf08d560252aa5e74210546f369fbbbce8c12cfc7957b2652fe9a75", + hash.c_str()); + + hash = utils::collection::to_hex_string( + utils::encryption::create_hash_sha512(L"a")); +#if defined(_WIN32) + EXPECT_STREQ( + "5c2ca3d50f46ece6066c53bd1a490cbe5f72d2738ae9417332e91e5c3f75205c639d71a9" + "a41d67d965fa137dddf439e0ab9443a6ea44915e90d8b5b566d1c076", + hash.c_str()); +#else // !defined(_WIN32) + EXPECT_STREQ( + "a93498d992e81915075144cb304d2bdf040b336283f888252244882d8366dd3a6e2d9749" + "077114dda1a9aa1a7b69d33f7a781f003ccd12e599a6341014f29aaf", + hash.c_str()); +#endif + + hash = utils::collection::to_hex_string( + utils::encryption::create_hash_sha512({1U})); + EXPECT_STREQ( + "7b54b66836c1fbdd13d2441d9e1434dc62ca677fb68f5fe66a464baadecdbd00576f8d6b" + "5ac3bcc80844b7d50b1cc6603444bbe7cfcf8fc0aa1ee3c636d9e339", + hash.c_str()); +} +} // namespace repertory + +#endif // defined(PROJECT_ENABLE_LIBSODIUM) diff --git a/support/test/src/utils/path_test.cpp b/support/test/src/utils/path_test.cpp index da767ab3..fff28838 100644 --- a/support/test/src/utils/path_test.cpp +++ b/support/test/src/utils/path_test.cpp @@ -19,11 +19,31 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "gtest/gtest.h" +#include "test.hpp" -#include "utils/common.hpp" -#include "utils/path.hpp" -#include "utils/string.hpp" +#if defined(_WIN32) +namespace { +static const auto test_path = [](std::string str) -> std::string { +#if defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES) + if (repertory::utils::string::begins_with(str, "\\")) { + str = repertory::utils::string::to_lower( + std::filesystem::current_path().string().substr(0U, 2U)) + + str; + } + + str = std::string{repertory::utils::path::long_notation} + str; +#else // !defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES) + if (repertory::utils::string::begins_with(str, "\\")) { + str = repertory::utils::string::to_lower( + std::filesystem::current_path().string().substr(0U, 2U)) + + str; + } +#endif // defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES) + + return repertory::utils::string::right_trim(str, '\\'); +}; +} // namespace +#endif // defined(_WIN32) namespace repertory { TEST(utils_path, constants) { @@ -31,10 +51,17 @@ TEST(utils_path, constants) { EXPECT_EQ(std::wstring_view{L"\\"}, utils::path::backslash_w); EXPECT_EQ(std::string_view{"."}, utils::path::dot); EXPECT_EQ(std::wstring_view{L"."}, utils::path::dot_w); + EXPECT_EQ(std::string_view{".\\"}, utils::path::dot_backslash); + EXPECT_EQ(std::wstring_view{L".\\"}, utils::path::dot_backslash_w); EXPECT_EQ(std::string_view{"./"}, utils::path::dot_slash); EXPECT_EQ(std::wstring_view{L"./"}, utils::path::dot_slash_w); EXPECT_EQ(std::string_view{"/"}, utils::path::slash); EXPECT_EQ(std::wstring_view{L"/"}, utils::path::slash_w); + +#if defined(_WIN32) + EXPECT_EQ(std::string_view{"\\\\"}, utils::path::unc_notation); + EXPECT_EQ(std::wstring_view{L"\\\\"}, utils::path::unc_notation_w); +#endif // defined(_WIN32) } TEST(utils_path, directory_seperator) { @@ -86,6 +113,12 @@ TEST(utils_path, get_dot) { EXPECT_EQ(utils::path::dot_w, utils::path::get_dot()); } +TEST(utils_path, get_dot_backslash) { + EXPECT_EQ(utils::path::dot_backslash, utils::path::get_dot_backslash()); + EXPECT_EQ(utils::path::dot_backslash_w, + utils::path::get_dot_backslash()); +} + TEST(utils_path, get_dot_slash) { EXPECT_EQ(utils::path::dot_slash, utils::path::get_dot_slash()); EXPECT_EQ(utils::path::dot_slash_w, utils::path::get_dot_slash()); @@ -96,40 +129,56 @@ TEST(utils_path, get_slash) { EXPECT_EQ(utils::path::slash_w, utils::path::get_slash()); } +TEST(utils_path, get_long_notation) { + EXPECT_EQ(utils::path::long_notation, utils::path::get_long_notation()); + EXPECT_EQ(utils::path::long_notation_w, + utils::path::get_long_notation()); +} + TEST(utils_path, combine) { auto s = utils::path::combine(R"(\test\path)", {}); #if defined(_WIN32) - EXPECT_STREQ(R"(\test\path)", s.c_str()); + EXPECT_STREQ(test_path(R"(\test\path)").c_str(), s.c_str()); #else EXPECT_STREQ("/test/path", s.c_str()); #endif s = utils::path::combine(R"(\test)", {R"(\path)"}); #if defined(_WIN32) - EXPECT_STREQ(R"(\test\path)", s.c_str()); + EXPECT_STREQ(test_path(R"(\test\path)").c_str(), s.c_str()); #else EXPECT_STREQ("/test/path", s.c_str()); #endif s = utils::path::combine(R"(\test)", {R"(\path)", R"(\again\)"}); #if defined(_WIN32) - EXPECT_STREQ(R"(\test\path\again)", s.c_str()); + EXPECT_STREQ(test_path(R"(\test\path\again)").c_str(), s.c_str()); #else EXPECT_STREQ("/test/path/again", s.c_str()); #endif + s = utils::path::combine("/home/test/.dest", {".state"}); +#if defined(_WIN32) + EXPECT_STREQ(test_path(R"(\home\test\.dest\.state)").c_str(), s.c_str()); +#else + EXPECT_STREQ("/home/test/.dest/.state", s.c_str()); +#endif + #if defined(_WIN32) s = utils::path::combine(R"(R:\test)", {R"(\path)", R"(\again\)"}); - EXPECT_STREQ(R"(r:\test\path\again)", s.c_str()); + EXPECT_STREQ(test_path(R"(r:\test\path\again)").c_str(), s.c_str()); s = utils::path::combine("R:", {R"(\path)", R"(\again\)"}); - EXPECT_STREQ(R"(r:\path\again)", s.c_str()); + EXPECT_STREQ(test_path(R"(r:\path\again)").c_str(), s.c_str()); s = utils::path::combine("R:", {}); - EXPECT_STREQ("r:", s.c_str()); + EXPECT_STREQ(test_path("r:").c_str(), s.c_str()); s = utils::path::combine("R:", {"\\"}); - EXPECT_STREQ("r:", s.c_str()); + EXPECT_STREQ(test_path("r:").c_str(), s.c_str()); + + s = utils::path::combine("\\\\moose", {"cow"}); + EXPECT_STREQ("\\\\moose\\cow", s.c_str()); #endif } @@ -204,6 +253,15 @@ TEST(utils_path, create_api_path) { s = utils::path::create_api_path("/cow\\\\/moose\\\\/\\dog\\chicken\\"); EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str()); + + s = utils::path::create_api_path(".state"); + EXPECT_STREQ("/.state", s.c_str()); + + s = utils::path::create_api_path("/.state/.local"); + EXPECT_STREQ("/.state/.local", s.c_str()); + + s = utils::path::create_api_path("./.state/.local"); + EXPECT_STREQ("/.state/.local", s.c_str()); } TEST(utils_path, finalize) { @@ -212,104 +270,137 @@ TEST(utils_path, finalize) { s = utils::path::finalize(R"(\)"); #if defined(_WIN32) - EXPECT_STREQ(R"(\)", s.c_str()); + EXPECT_STREQ(test_path(R"(\)").c_str(), s.c_str()); #else EXPECT_STREQ("/", s.c_str()); #endif s = utils::path::finalize("/"); #if defined(_WIN32) - EXPECT_STREQ(R"(\)", s.c_str()); + EXPECT_STREQ(test_path(R"(\)").c_str(), s.c_str()); #else EXPECT_STREQ("/", s.c_str()); #endif s = utils::path::finalize(R"(\\)"); #if defined(_WIN32) - EXPECT_STREQ(R"(\)", s.c_str()); + EXPECT_STREQ("\\\\", s.c_str()); #else EXPECT_STREQ("/", s.c_str()); #endif s = utils::path::finalize("//"); #if defined(_WIN32) - EXPECT_STREQ(R"(\)", s.c_str()); + EXPECT_STREQ("\\\\", s.c_str()); #else EXPECT_STREQ("/", s.c_str()); #endif s = utils::path::finalize("/cow///moose/////dog/chicken"); #if defined(_WIN32) - EXPECT_STREQ(R"(\cow\moose\dog\chicken)", s.c_str()); + EXPECT_STREQ(test_path(R"(\cow\moose\dog\chicken)").c_str(), s.c_str()); #else EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str()); #endif s = utils::path::finalize("\\cow\\\\\\moose\\\\\\\\dog\\chicken/"); #if defined(_WIN32) - EXPECT_STREQ(R"(\cow\moose\dog\chicken)", s.c_str()); + EXPECT_STREQ(test_path(R"(\cow\moose\dog\chicken)").c_str(), s.c_str()); #else EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str()); #endif s = utils::path::finalize("/cow\\\\/moose\\\\/\\dog\\chicken\\"); #if defined(_WIN32) - EXPECT_STREQ(R"(\cow\moose\dog\chicken)", s.c_str()); + EXPECT_STREQ(test_path(R"(\cow\moose\dog\chicken)").c_str(), s.c_str()); #else EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str()); #endif #if defined(_WIN32) s = utils::path::finalize("D:"); - EXPECT_STREQ("d:", s.c_str()); + EXPECT_STREQ(test_path("d:").c_str(), s.c_str()); s = utils::path::finalize("D:\\"); - EXPECT_STREQ("d:", s.c_str()); + EXPECT_STREQ(test_path("d:").c_str(), s.c_str()); s = utils::path::finalize("D:\\moose"); - EXPECT_STREQ("d:\\moose", s.c_str()); + EXPECT_STREQ(test_path("d:\\moose").c_str(), s.c_str()); s = utils::path::finalize("D:\\moose\\"); - EXPECT_STREQ("d:\\moose", s.c_str()); + EXPECT_STREQ(test_path("d:\\moose").c_str(), s.c_str()); s = utils::path::finalize("D:/"); - EXPECT_STREQ("d:", s.c_str()); + EXPECT_STREQ(test_path("d:").c_str(), s.c_str()); s = utils::path::finalize("D:/moose"); - EXPECT_STREQ("d:\\moose", s.c_str()); + EXPECT_STREQ(test_path("d:\\moose").c_str(), s.c_str()); s = utils::path::finalize("D:/moose/"); - EXPECT_STREQ("d:\\moose", s.c_str()); + EXPECT_STREQ(test_path("d:\\moose").c_str(), s.c_str()); + + s = utils::path::finalize("\\\\moose\\cow"); + EXPECT_STREQ("\\\\moose\\cow", s.c_str()); + + s = utils::path::finalize("//moose/cow"); + EXPECT_STREQ("\\\\moose\\cow", s.c_str()); +#else // !defined(_WIN32) + s = utils::path::finalize("\\\\moose\\cow"); + EXPECT_STREQ("/moose/cow", s.c_str()); + + s = utils::path::finalize("//moose/cow"); + EXPECT_STREQ("/moose/cow", s.c_str()); #endif // defined(_WIN32) } TEST(utils_path, absolute) { - auto dir = utils::path::absolute(std::filesystem::current_path().string()); + auto dir = utils::path::get_current_path(); auto path = utils::path::absolute("."); EXPECT_STREQ(dir.c_str(), path.c_str()); path = utils::path::absolute("./"); EXPECT_STREQ(dir.c_str(), path.c_str()); + path = utils::path::absolute(R"(.\)"); + EXPECT_STREQ(dir.c_str(), path.c_str()); + #if defined(_WIN32) + path = utils::path::absolute(R"(.\moose)"); + EXPECT_STREQ((dir + R"(\moose)").c_str(), path.c_str()); + + path = utils::path::absolute(R"(./moose)"); + EXPECT_STREQ((dir + R"(\moose)").c_str(), path.c_str()); + + path = utils::path::absolute(R"(\\server\share)"); + EXPECT_STREQ(R"(\\server\share)", path.c_str()); + + path = utils::path::absolute(R"(//server/share)"); + EXPECT_STREQ(R"(\\server\share)", path.c_str()); + auto home_env = utils::get_environment_variable("USERPROFILE"); #else // !defined(_WIN32) + path = utils::path::absolute(R"(.\moose)"); + EXPECT_STREQ((dir + R"(/moose)").c_str(), path.c_str()); + + path = utils::path::absolute(R"(./moose)"); + EXPECT_STREQ((dir + R"(/moose)").c_str(), path.c_str()); + + path = utils::path::absolute(R"(\\server\share)"); + EXPECT_STREQ(R"(/server/share)", path.c_str()); + auto home_env = utils::get_environment_variable("HOME"); #endif // defined(_WIN32) - auto home = utils::path::absolute(home_env); path = utils::path::absolute("~"); EXPECT_STREQ(home.c_str(), path.c_str()); - - // path = utils::path::absolute("~/.local"); } TEST(utils_path, absolute_can_resolve_path_variables) { std::string home{}; #if defined(_WIN32) - home.resize(MAX_PATH + 1U); + home.resize(repertory::max_path_length + 1U); auto size = ::GetEnvironmentVariableA("USERPROFILE", home.data(), 0U); home.resize(size); @@ -331,4 +422,128 @@ TEST(utils_path, absolute_can_resolve_path_variables) { EXPECT_STREQ(home.c_str(), expanded_str.c_str()); #endif // defined(_WIN32) } + +TEST(utils_path, get_parent_path) { +#if defined(_WIN32) + { + auto dir = R"(c:\test)"; + auto parent = utils::path::get_parent_path(dir); + EXPECT_STREQ("c:", parent.c_str()); + + dir = R"(c:\test\file.txt)"; + parent = utils::path::get_parent_path(dir); + EXPECT_STREQ(R"(c:\test)", parent.c_str()); + + dir = "c:"; + parent = utils::path::get_parent_path(dir); + EXPECT_STREQ("c:", parent.c_str()); + } + + { + auto dir = LR"(c:\test)"; + auto parent = utils::path::get_parent_path(dir); + EXPECT_STREQ(L"c:", parent.c_str()); + + dir = LR"(c:\test\file.txt)"; + parent = utils::path::get_parent_path(dir); + EXPECT_STREQ(LR"(c:\test)", parent.c_str()); + + dir = L"c:"; + parent = utils::path::get_parent_path(dir); + EXPECT_STREQ(L"c:", parent.c_str()); + } +#else // !defined(_WIN32) + { + auto dir = "/test"; + auto parent = utils::path::get_parent_path(dir); + EXPECT_STREQ("/", parent.c_str()); + + dir = "/test/test"; + parent = utils::path::get_parent_path(dir); + EXPECT_STREQ("/test", parent.c_str()); + } + + { + auto dir = L"/test"; + auto parent = utils::path::get_parent_path(dir); + EXPECT_STREQ(L"/", parent.c_str()); + + dir = L"/test/test"; + parent = utils::path::get_parent_path(dir); + EXPECT_STREQ(L"/test", parent.c_str()); + } +#endif // defined(_WIN32) +} + +TEST(utils_path, contains_trash_directory) { +#if defined(_WIN32) + { + auto dir = R"(c:\$recycle.bin)"; + EXPECT_TRUE(utils::path::contains_trash_directory(dir)); + + dir = R"(c:\$recycle.bin\moose.txt)"; + EXPECT_TRUE(utils::path::contains_trash_directory(dir)); + } + + { + auto dir = LR"(c:\$recycle.bin)"; + EXPECT_TRUE(utils::path::contains_trash_directory(dir)); + + dir = LR"(c:\$recycle.bin\moose.txt)"; + EXPECT_TRUE(utils::path::contains_trash_directory(dir)); + } +#else // !defined(_WIN32) + { + auto dir = "/$recycle.bin"; + EXPECT_TRUE(utils::path::contains_trash_directory(dir)); + + dir = "/$recycle.bin/moose.txt"; + EXPECT_TRUE(utils::path::contains_trash_directory(dir)); + } + + { + auto dir = L"/$recycle.bin"; + EXPECT_TRUE(utils::path::contains_trash_directory(dir)); + + dir = L"/$recycle.bin/moose.txt"; + EXPECT_TRUE(utils::path::contains_trash_directory(dir)); + } +#endif // defined(_WIN32) +} + +TEST(utils_path, does_not_contain_trash_directory) { +#if defined(_WIN32) + { + auto dir = R"(c:\recycle.bin)"; + EXPECT_FALSE(utils::path::contains_trash_directory(dir)); + + dir = R"(c:\recycle.bin\moose.txt)"; + EXPECT_FALSE(utils::path::contains_trash_directory(dir)); + } + + { + auto dir = LR"(c:\recycle.bin)"; + EXPECT_FALSE(utils::path::contains_trash_directory(dir)); + + dir = LR"(c:\recycle.bin\moose.txt)"; + EXPECT_FALSE(utils::path::contains_trash_directory(dir)); + } +#else // !defined(_WIN32) + { + auto dir = "/recycle.bin"; + EXPECT_FALSE(utils::path::contains_trash_directory(dir)); + + dir = "/recycle.bin/moose.txt)"; + EXPECT_FALSE(utils::path::contains_trash_directory(dir)); + } + + { + auto dir = L"/recycle.bin"; + EXPECT_FALSE(utils::path::contains_trash_directory(dir)); + + dir = L"/recycle.bin/moose.txt)"; + EXPECT_FALSE(utils::path::contains_trash_directory(dir)); + } +#endif // defined(_WIN32) +} } // namespace repertory diff --git a/support/test/src/utils/string_test.cpp b/support/test/src/utils/string_test.cpp index 41e770c8..f25bc9d6 100644 --- a/support/test/src/utils/string_test.cpp +++ b/support/test/src/utils/string_test.cpp @@ -19,9 +19,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "gtest/gtest.h" - -#include "utils/string.hpp" +#include "test.hpp" namespace repertory { TEST(utils_string, begins_with) {