Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .tipi/env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CMAKE_TIPI_PROVIDER_ENABLE=ON
50 changes: 21 additions & 29 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,36 @@ if ("${COMPILER_IN_USE}" STREQUAL "GNU" OR "${COMPILER_IN_USE}" MATCHES "CLANG")
)
endif()

find_package(Threads)

include (FetchContent)
FetchContent_Declare(
Boost
GIT_REPOSITORY https://github.com/boostorg/boost.git
# boost 1.80
GIT_TAG 32da69a36f84c5255af8a994951918c258bac601
GIT_REPOSITORY https://github.com/nxxm/boost.git
# boost-1.89.0 with updates
GIT_TAG 23090bb21676cb22f1d4ceed21d59fda8b420cc9
EXCLUDE_FROM_ALL
)
FetchContent_MakeAvailable(Boost)
find_package(boost_filesystem CONFIG REQUIRED)
find_package(boost_system CONFIG REQUIRED)
find_package(boost_uuid CONFIG REQUIRED)
find_package(boost_filesystem CONFIG)
find_package(boost_system CONFIG)
find_package(boost_uuid CONFIG)

# Define library
add_library(file INTERFACE )
add_library(file INTERFACE)
add_library(cpp-pre_file::file ALIAS file)

set(include_install_dir "include")

target_include_directories(file BEFORE INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
$<INSTALL_INTERFACE:${include_install_dir}/>)

target_link_libraries(file INTERFACE
Boost::system Boost::filesystem Boost::uuid
${CMAKE_THREAD_LIBS_INIT}
target_sources(file
PUBLIC
FILE_SET HEADERS
FILES
pre/file/hash.hpp
pre/file/string.hpp
pre/file/home.hpp
)
target_compile_features(file INTERFACE cxx_std_17)


# Targets:
install(
TARGETS
EXPORT "${targets_export_name}"
LIBRARY DESTINATION "lib"
ARCHIVE DESTINATION "lib"
RUNTIME DESTINATION "bin"
INCLUDES DESTINATION "${include_install_dir}"
target_link_libraries(file INTERFACE
Boost::filesystem
Boost::system
Boost::uuid
)

add_subdirectory(tests)
Expand Down Expand Up @@ -100,7 +92,7 @@ install(
LIBRARY DESTINATION "lib"
ARCHIVE DESTINATION "lib"
RUNTIME DESTINATION "bin"
INCLUDES DESTINATION "${include_install_dir}"
FILE_SET HEADERS DESTINATION "include"
)

# Headers:
Expand Down
32 changes: 29 additions & 3 deletions pre/file/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <stdexcept>
#include <system_error>
#include <iterator>
#include <fstream>
#include <string>
#include <vector>
Expand All @@ -10,6 +11,13 @@

namespace pre::file {

namespace {
constexpr char int_to_hex[16] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'a', 'b', 'c', 'd', 'e', 'f'
};
}

using namespace std::string_literals;

//! Calculate the SHA1 sum for a file on disk
Expand Down Expand Up @@ -41,13 +49,31 @@ namespace pre::file {
}

// compute the hash
uint32_t hash[5] = {0};
boost::uuids::detail::sha1::digest_type hash = {0};
char hash_buf[41] = {0};

sha1.get_digest(hash);

for (int i = 0; i < 5; i++) {
std::sprintf(hash_buf + (i << 3), "%08x", hash[i]);
if constexpr (std::size(hash) == 20) {
// Newer Boost has digest_type of type "unsigned char[20]"
for (size_t i = 0; i < std::size(hash); i++) {
hash_buf[i * 2 + 0] = int_to_hex[(hash[i] >> 4) & 0xF];
hash_buf[i * 2 + 1] = int_to_hex[(hash[i] ) & 0xF];
}
} else if constexpr (std::size(hash) == 5) {
// Older Boost has digest_type of type "unsigned int[5]"
for (size_t i = 0; i < std::size(hash); i++) {
hash_buf[i * 2 + 0] = int_to_hex[(hash[i] >> 28) & 0xF];
hash_buf[i * 2 + 1] = int_to_hex[(hash[i] >> 24) & 0xF];
hash_buf[i * 2 + 2] = int_to_hex[(hash[i] >> 20) & 0xF];
hash_buf[i * 2 + 3] = int_to_hex[(hash[i] >> 16) & 0xF];
hash_buf[i * 2 + 4] = int_to_hex[(hash[i] >> 12) & 0xF];
hash_buf[i * 2 + 5] = int_to_hex[(hash[i] >> 8) & 0xF];
hash_buf[i * 2 + 6] = int_to_hex[(hash[i] >> 4) & 0xF];
hash_buf[i * 2 + 7] = int_to_hex[(hash[i] ) & 0xF];
}
} else {
throw std::runtime_error("Unsupported boost::uuids::detail::sha1::digest_type size");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should set ec here not throw, throw is only from the overload without ec, this is following the same pricinple as how boost.asio design APIs

If we could do static_assert it would be better, but it seems not possible in our c++ version.

}

return std::string(hash_buf);
Expand Down