diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2021-11-08 22:04:51 -0500 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2021-11-10 21:37:55 -0500 |
commit | 4127f79d4b880f1e16e6fca930666a729417c4c3 (patch) | |
tree | c6dfc37619aa9747d0d0dbe2cb7c66e973d255b7 | |
parent | 40ec0e7df8e6299d7e27b2088ce79295e5db74f4 (diff) | |
download | libgit2-4127f79d4b880f1e16e6fca930666a729417c4c3.tar.gz |
refactor: make util an object library
Instead of simply including the utility files directly, make them a
cmake object library for easy reusability between other projects within
libgit2.
54 files changed, 345 insertions, 256 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index ae0a5d685..b725dfc8a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -97,6 +97,7 @@ include(FindPkgLibraries) include(FindThreads) include(FindStatNsec) include(Findfutimens) +include(SelectHashes) include(GNUInstallDirs) include(IdeSplitSources) include(FeatureSummary) diff --git a/cmake/SelectHashes.cmake b/cmake/SelectHashes.cmake index a5e3e22aa..c3659c4b9 100644 --- a/cmake/SelectHashes.cmake +++ b/cmake/SelectHashes.cmake @@ -22,9 +22,8 @@ endif() if(USE_SHA1 STREQUAL "CollisionDetection") set(GIT_SHA1_COLLISIONDETECT 1) add_definitions(-DSHA1DC_NO_STANDARD_INCLUDES=1) - add_definitions(-DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\"common.h\") - add_definitions(-DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\"common.h\") - file(GLOB SRC_SHA1 util/hash/sha1/collisiondetect.* util/hash/sha1/sha1dc/*) + add_definitions(-DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\"git2_util.h\") + add_definitions(-DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\"git2_util.h\") elseif(USE_SHA1 STREQUAL "OpenSSL") # OPENSSL_FOUND should already be set, we're checking USE_HTTPS @@ -34,13 +33,11 @@ elseif(USE_SHA1 STREQUAL "OpenSSL") else() list(APPEND LIBGIT2_PC_REQUIRES "openssl") endif() - file(GLOB SRC_SHA1 util/hash/sha1/openssl.*) elseif(USE_SHA1 STREQUAL "CommonCrypto") set(GIT_SHA1_COMMON_CRYPTO 1) - file(GLOB SRC_SHA1 util/hash/sha1/common_crypto.*) elseif(USE_SHA1 STREQUAL "mbedTLS") set(GIT_SHA1_MBEDTLS 1) - file(GLOB SRC_SHA1 util/hash/sha1/mbedtls.*) + file(GLOB SRC_SHA1 hash/sha1/mbedtls.*) list(APPEND LIBGIT2_SYSTEM_INCLUDES ${MBEDTLS_INCLUDE_DIR}) list(APPEND LIBGIT2_LIBS ${MBEDTLS_LIBRARIES}) # mbedTLS has no pkgconfig file, hence we can't require it @@ -49,14 +46,8 @@ elseif(USE_SHA1 STREQUAL "mbedTLS") list(APPEND LIBGIT2_PC_LIBS ${MBEDTLS_LIBRARIES}) elseif(USE_SHA1 STREQUAL "Win32") set(GIT_SHA1_WIN32 1) - file(GLOB SRC_SHA1 util/hash/sha1/win32.*) -elseif(USE_SHA1 STREQUAL "Generic") - file(GLOB SRC_SHA1 util/hash/sha1/generic.*) -else() +elseif(NOT (USE_SHA1 STREQUAL "Generic")) message(FATAL_ERROR "Asked for unknown SHA1 backend: ${USE_SHA1}") endif() -list(APPEND SRC_SHA1 "util/hash/sha1.h") -list(SORT SRC_SHA1) - add_feature_info(SHA ON "using ${USE_SHA1}") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e254329f4..fb4410ec7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,8 @@ add_library(git2internal OBJECT) set_target_properties(git2internal PROPERTIES C_STANDARD 90) +add_subdirectory(util) +list(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:util>) if(DEPRECATE_HARD) add_definitions(-DGIT_DEPRECATE_HARD) @@ -111,8 +113,6 @@ if(WIN32 AND USE_WINHTTP) endif() include(SelectHTTPSBackend) -include(SelectHashes) -target_sources(git2internal PRIVATE ${SRC_SHA1}) # Specify regular expression implementation find_package(PCRE) @@ -301,21 +301,6 @@ file(GLOB SRC_H list(SORT SRC_H) target_sources(git2internal PRIVATE ${SRC_H}) -# On Windows use specific platform sources -if(WIN32 AND NOT CYGWIN) - set(WIN_RC "util/win32/git2.rc") - - file(GLOB SRC_OS util/win32/*.c util/win32/*.h) - list(SORT SRC_OS) - target_sources(git2internal PRIVATE ${SRC_OS}) -elseif(AMIGA) - target_compile_definitions(git2internal PRIVATE NO_ADDRINFO NO_READDIR_R NO_MMAP) -else() - file(GLOB SRC_OS util/unix/*.c util/unix/*.h) - list(SORT SRC_OS) - target_sources(git2internal PRIVATE ${SRC_OS}) -endif() - if(USE_LEAK_CHECKER STREQUAL "valgrind") target_compile_definitions(git2internal PRIVATE VALGRIND) endif() @@ -327,10 +312,6 @@ file(GLOB SRC_GIT2 libgit2/*.c libgit2/*.h list(SORT SRC_GIT2) target_sources(git2internal PRIVATE ${SRC_GIT2}) -file(GLOB SRC_UTIL util/*.c util/*.h util/allocators/*.c util/allocators/*.h) -list(SORT SRC_UTIL) -target_sources(git2internal PRIVATE ${SRC_UTIL}) - if(APPLE) # The old Secure Transport API has been deprecated in macOS 10.15. set_source_files_properties(libgit2/streams/stransport.c PROPERTIES COMPILE_FLAGS -Wno-deprecated) diff --git a/src/libgit2/common.h b/src/libgit2/common.h index 640f94806..fb55f20c1 100644 --- a/src/libgit2/common.h +++ b/src/libgit2/common.h @@ -7,133 +7,17 @@ #ifndef INCLUDE_common_h__ #define INCLUDE_common_h__ -#ifndef LIBGIT2_NO_FEATURES_H -# include "git2/sys/features.h" -#endif - -#include "git2/common.h" -#include "cc-compat.h" - -/** Declare a function as always inlined. */ -#if defined(_MSC_VER) -# define GIT_INLINE(type) static __inline type -#elif defined(__GNUC__) -# define GIT_INLINE(type) static __inline__ type -#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) -# define GIT_INLINE(type) static inline type -#else -# define GIT_INLINE(type) static type -#endif - -/** Support for gcc/clang __has_builtin intrinsic */ -#ifndef __has_builtin -# define __has_builtin(x) 0 -#endif - -/** - * Declare that a function's return value must be used. - * - * Used mostly to guard against potential silent bugs at runtime. This is - * recommended to be added to functions that: - * - * - Allocate / reallocate memory. This prevents memory leaks or errors where - * buffers are expected to have grown to a certain size, but could not be - * resized. - * - Acquire locks. When a lock cannot be acquired, that will almost certainly - * cause a data race / undefined behavior. - */ -#if defined(__GNUC__) -# define GIT_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) -#else -# define GIT_WARN_UNUSED_RESULT -#endif - -#include <assert.h> -#include <errno.h> -#include <limits.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include <sys/types.h> -#include <sys/stat.h> - -#ifdef GIT_WIN32 - -# include <io.h> -# include <direct.h> -# include <winsock2.h> -# include <windows.h> -# include <ws2tcpip.h> -# include "win32/msvc-compat.h" -# include "win32/mingw-compat.h" -# include "win32/w32_common.h" -# include "win32/win32-compat.h" -# include "win32/error.h" -# include "win32/version.h" -# ifdef GIT_THREADS -# include "win32/thread.h" -# endif - -#else - -# include <unistd.h> -# include <strings.h> -# ifdef GIT_THREADS -# include <pthread.h> -# include <sched.h> -# endif - -#define GIT_LIBGIT2_CALL -#define GIT_SYSTEM_CALL - -#ifdef GIT_USE_STAT_ATIMESPEC -# define st_atim st_atimespec -# define st_ctim st_ctimespec -# define st_mtim st_mtimespec -#endif - -# include <arpa/inet.h> - -#endif - -#include "git2/types.h" -#include "git2/errors.h" +#include "git2_util.h" #include "errors.h" -#include "thread.h" -#include "integer.h" -#include "assert_safe.h" -#include "utf8.h" /* - * Include the declarations for deprecated functions; this ensures - * that they're decorated with the proper extern/visibility attributes. - */ +* Include the declarations for deprecated functions; this ensures +* that they're decorated with the proper extern/visibility attributes. +*/ #include "git2/deprecated.h" #include "posix.h" -#define DEFAULT_BUFSIZE 65536 -#define FILEIO_BUFSIZE DEFAULT_BUFSIZE -#define FILTERIO_BUFSIZE DEFAULT_BUFSIZE -#define NETIO_BUFSIZE DEFAULT_BUFSIZE - -/** - * Check a pointer allocation result, returning -1 if it failed. - */ -#define GIT_ERROR_CHECK_ALLOC(ptr) if (ptr == NULL) { return -1; } - -/** - * Check a string buffer allocation result, returning -1 if it failed. - */ -#define GIT_ERROR_CHECK_ALLOC_STR(buf) if ((void *)(buf) == NULL || git_str_oom(buf)) { return -1; } - -/** - * Check a return value and propagate result if non-zero. - */ -#define GIT_ERROR_CHECK_ERROR(code) \ - do { int _err = (code); if (_err) return _err; } while (0) - /** * Check a versioned structure for validity */ @@ -153,58 +37,4 @@ GIT_INLINE(int) git_error__check_version(const void *structure, unsigned int exp } #define GIT_ERROR_CHECK_VERSION(S,V,N) if (git_error__check_version(S,V,N) < 0) return -1 -/** - * Initialize a structure with a version. - */ -GIT_INLINE(void) git__init_structure(void *structure, size_t len, unsigned int version) -{ - memset(structure, 0, len); - *((int*)structure) = version; -} -#define GIT_INIT_STRUCTURE(S,V) git__init_structure(S, sizeof(*S), V) - -#define GIT_INIT_STRUCTURE_FROM_TEMPLATE(PTR,VERSION,TYPE,TPL) do { \ - TYPE _tmpl = TPL; \ - GIT_ERROR_CHECK_VERSION(&(VERSION), _tmpl.version, #TYPE); \ - memcpy((PTR), &_tmpl, sizeof(_tmpl)); } while (0) - - -/** Check for additive overflow, setting an error if would occur. */ -#define GIT_ADD_SIZET_OVERFLOW(out, one, two) \ - (git__add_sizet_overflow(out, one, two) ? (git_error_set_oom(), 1) : 0) - -/** Check for additive overflow, setting an error if would occur. */ -#define GIT_MULTIPLY_SIZET_OVERFLOW(out, nelem, elsize) \ - (git__multiply_sizet_overflow(out, nelem, elsize) ? (git_error_set_oom(), 1) : 0) - -/** Check for additive overflow, failing if it would occur. */ -#define GIT_ERROR_CHECK_ALLOC_ADD(out, one, two) \ - if (GIT_ADD_SIZET_OVERFLOW(out, one, two)) { return -1; } - -#define GIT_ERROR_CHECK_ALLOC_ADD3(out, one, two, three) \ - if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \ - GIT_ADD_SIZET_OVERFLOW(out, *(out), three)) { return -1; } - -#define GIT_ERROR_CHECK_ALLOC_ADD4(out, one, two, three, four) \ - if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \ - GIT_ADD_SIZET_OVERFLOW(out, *(out), three) || \ - GIT_ADD_SIZET_OVERFLOW(out, *(out), four)) { return -1; } - -#define GIT_ERROR_CHECK_ALLOC_ADD5(out, one, two, three, four, five) \ - if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \ - GIT_ADD_SIZET_OVERFLOW(out, *(out), three) || \ - GIT_ADD_SIZET_OVERFLOW(out, *(out), four) || \ - GIT_ADD_SIZET_OVERFLOW(out, *(out), five)) { return -1; } - -/** Check for multiplicative overflow, failing if it would occur. */ -#define GIT_ERROR_CHECK_ALLOC_MULTIPLY(out, nelem, elsize) \ - if (GIT_MULTIPLY_SIZET_OVERFLOW(out, nelem, elsize)) { return -1; } - -/* NOTE: other git_error functions are in the public errors.h header file */ - -/* Forward declare git_str */ -typedef struct git_str git_str; - -#include "util.h" - #endif diff --git a/src/libgit2/diff_xdiff.c b/src/libgit2/diff_xdiff.c index 278e2be36..77c181e32 100644 --- a/src/libgit2/diff_xdiff.c +++ b/src/libgit2/diff_xdiff.c @@ -11,6 +11,7 @@ #include "diff.h" #include "diff_driver.h" #include "patch_generate.h" +#include "utf8.h" static int git_xdiff_scan_int(const char **str, int *value) { diff --git a/src/libgit2/netops.c b/src/libgit2/netops.c index 0a27365b8..00640c600 100644 --- a/src/libgit2/netops.c +++ b/src/libgit2/netops.c @@ -12,7 +12,6 @@ #include "posix.h" #include "str.h" -#include "http_parser.h" #include "runtime.h" int gitno_recv(gitno_buffer *buf) diff --git a/src/libgit2/path.c b/src/libgit2/path.c index 05a3dc2cf..a19340efe 100644 --- a/src/libgit2/path.c +++ b/src/libgit2/path.c @@ -9,6 +9,7 @@ #include "repository.h" #include "fs_path.h" +#include "utf8.h" typedef struct { git_repository *repo; diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt new file mode 100644 index 000000000..a3e1fd5e5 --- /dev/null +++ b/src/util/CMakeLists.txt @@ -0,0 +1,95 @@ +# util: a shared library for common utility functions for libgit2 projects + +add_library(util OBJECT) +set_target_properties(util PROPERTIES C_STANDARD 90) + +set(UTIL_INCLUDES + "${CMAKE_BINARY_DIR}/src" + "${libgit2_SOURCE_DIR}/src/util" + "${libgit2_SOURCE_DIR}/include") + +file(GLOB SRC_UTIL *.c *.h allocators/*.c allocators/*.h) +list(SORT SRC_UTIL) +target_sources(util PRIVATE ${SRC_UTIL}) + +# +# Platform specific sources +# + +if(WIN32 AND NOT CYGWIN) + set(WIN_RC "win32/git2.rc") + + file(GLOB SRC_OS win32/*.c win32/*.h) + list(SORT SRC_OS) + target_sources(util PRIVATE ${SRC_OS}) +elseif(NOT AMIGA) + file(GLOB SRC_OS unix/*.c unix/*.h) + list(SORT SRC_OS) + target_sources(util PRIVATE ${SRC_OS}) +endif() + +if(AMIGA) + target_compile_definitions(util PRIVATE NO_ADDRINFO NO_READDIR_R NO_MMAP) +endif() + +# +# Hash backend selection +# + +if(USE_SHA1 STREQUAL "CollisionDetection") + file(GLOB SRC_HASH hash/sha1/collisiondetect.* hash/sha1/sha1dc/*) +elseif(USE_SHA1 STREQUAL "OpenSSL") + file(GLOB SRC_HASH hash/sha1/openssl.*) +elseif(USE_SHA1 STREQUAL "CommonCrypto") + file(GLOB SRC_HASH hash/sha1/common_crypto.*) +elseif(USE_SHA1 STREQUAL "mbedTLS") + file(GLOB SRC_HASH hash/sha1/mbedtls.*) +elseif(USE_SHA1 STREQUAL "Win32") + file(GLOB SRC_HASH hash/sha1/win32.*) +elseif(USE_SHA1 STREQUAL "Generic") + file(GLOB SRC_HASH hash/sha1/generic.*) +else() + message(FATAL_ERROR "Asked for unknown SHA1 backend: ${USE_SHA1}") +endif() + +list(APPEND SRC_HASH "hash/sha1.h") +list(SORT SRC_HASH) + +# +# Include builtin dependencies +# + +if(NOT (USE_HTTP_PARSER STREQUAL "system")) + list(APPEND UTIL_INCLUDES "${libgit2_SOURCE_DIR}/deps/http-parser") + list(APPEND UTIL_OBJECTS "$<TARGET_OBJECTS:http-parser>") +endif() + +if(REGEX_BACKEND STREQUAL "builtin") + list(APPEND UTIL_INCLUDES "${libgit2_SOURCE_DIR}/deps/pcre") + list(APPEND UTIL_OBJECTS $<TARGET_OBJECTS:pcre>) +endif() + +if(USE_BUNDLED_ZLIB STREQUAL "Chromium") + list(APPEND UTIL_INCLUDES "${libgit2_SOURCE_DIR}/deps/chromium-zlib") + list(APPEND UTIL_OBJECTS $<TARGET_OBJECTS:chromium_zlib>) +elseif(USE_BUNDLED_ZLIB) + list(APPEND UTIL_INCLUDES "${libgit2_SOURCE_DIR}/deps/zlib") + list(APPEND UTIL_OBJECTS $<TARGET_OBJECTS:zlib>) +endif() + +# +# Preprocessor definitions +# + +if(DEPRECATE_HARD) + add_definitions(-DGIT_DEPRECATE_HARD) +endif() + +if(USE_LEAK_CHECKER STREQUAL "valgrind") + target_compile_definitions(util PRIVATE VALGRIND) +endif() + +target_sources(util PRIVATE ${SRC_UTIL} ${SRC_OS} ${SRC_HASH}) + +target_include_directories(util PRIVATE ${UTIL_INCLUDES} PUBLIC ${libgit2_SOURCE_DIR}/include) +target_include_directories(util SYSTEM PRIVATE ${LIBGIT2_SYSTEM_INCLUDES}) diff --git a/src/util/allocators/failalloc.h b/src/util/allocators/failalloc.h index 6115e51e7..91264a0bb 100644 --- a/src/util/allocators/failalloc.h +++ b/src/util/allocators/failalloc.h @@ -8,7 +8,7 @@ #ifndef INCLUDE_allocators_failalloc_h__ #define INCLUDE_allocators_failalloc_h__ -#include "common.h" +#include "git2_util.h" extern void *git_failalloc_malloc(size_t len, const char *file, int line); extern void *git_failalloc_calloc(size_t nelem, size_t elsize, const char *file, int line); diff --git a/src/util/allocators/stdalloc.h b/src/util/allocators/stdalloc.h index fa23fe6e3..955038cb0 100644 --- a/src/util/allocators/stdalloc.h +++ b/src/util/allocators/stdalloc.h @@ -8,7 +8,7 @@ #ifndef INCLUDE_allocators_stdalloc_h__ #define INCLUDE_allocators_stdalloc_h__ -#include "common.h" +#include "git2_util.h" #include "alloc.h" diff --git a/src/util/allocators/win32_leakcheck.h b/src/util/allocators/win32_leakcheck.h index 089690f90..edcd9307f 100644 --- a/src/util/allocators/win32_leakcheck.h +++ b/src/util/allocators/win32_leakcheck.h @@ -8,7 +8,7 @@ #ifndef INCLUDE_allocators_win32_leakcheck_h #define INCLUDE_allocators_win32_leakcheck_h -#include "common.h" +#include "git2_util.h" #include "alloc.h" diff --git a/src/util/array.h b/src/util/array.h index e97688b36..cbab52ad1 100644 --- a/src/util/array.h +++ b/src/util/array.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_array_h__ #define INCLUDE_array_h__ -#include "common.h" +#include "git2_util.h" /* * Use this to declare a typesafe resizable array of items, a la: diff --git a/src/util/fs_path.c b/src/util/fs_path.c index 957f389cc..004745398 100644 --- a/src/util/fs_path.c +++ b/src/util/fs_path.c @@ -7,8 +7,9 @@ #include "fs_path.h" +#include "git2_util.h" +#include "futils.h" #include "posix.h" -#include "repository.h" #ifdef GIT_WIN32 #include "win32/posix.h" #include "win32/w32_buffer.h" @@ -21,6 +22,13 @@ #include <stdio.h> #include <ctype.h> +#define ensure_error_set(code) do { \ + const git_error *e = git_error_last(); \ + if (!e || !e->message) \ + git_error_set(e ? e->klass : GIT_ERROR_CALLBACK, \ + "filesystem callback returned %d", code); \ + } while(0) + static int dos_drive_prefix_length(const char *path) { int i; @@ -530,7 +538,7 @@ int git_fs_path_walk_up( if (!scan) { error = cb(data, ""); if (error) - git_error_set_after_callback(error); + ensure_error_set(error); return error; } @@ -543,7 +551,7 @@ int git_fs_path_walk_up( iter.ptr[scan] = oldc; if (error) { - git_error_set_after_callback(error); + ensure_error_set(error); break; } @@ -563,7 +571,7 @@ int git_fs_path_walk_up( if (!error && stop == 0 && iter.ptr[0] != '/') { error = cb(data, ""); if (error) - git_error_set_after_callback(error); + ensure_error_set(error); } return error; @@ -1164,7 +1172,7 @@ int git_fs_path_direach( /* Only set our own error if the callback did not set one already */ if (error != 0) { if (!git_error_last()) - git_error_set_after_callback(error); + ensure_error_set(error); break; } diff --git a/src/util/fs_path.h b/src/util/fs_path.h index 188dcf303..db869f34c 100644 --- a/src/util/fs_path.h +++ b/src/util/fs_path.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_fs_path_h__ #define INCLUDE_fs_path_h__ -#include "common.h" +#include "git2_util.h" #include "posix.h" #include "str.h" diff --git a/src/util/futils.h b/src/util/futils.h index 22098addf..ff5ea27a2 100644 --- a/src/util/futils.h +++ b/src/util/futils.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_futils_h__ #define INCLUDE_futils_h__ -#include "common.h" +#include "git2_util.h" #include "map.h" #include "posix.h" diff --git a/src/util/git2_util.h b/src/util/git2_util.h new file mode 100644 index 000000000..74da18544 --- /dev/null +++ b/src/util/git2_util.h @@ -0,0 +1,182 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git2_util_h__ +#define INCLUDE_git2_util_h__ + +#ifndef LIBGIT2_NO_FEATURES_H +# include "git2/sys/features.h" +#endif + +#include "git2/common.h" +#include "cc-compat.h" + +typedef struct git_str git_str; + +/** Declare a function as always inlined. */ +#if defined(_MSC_VER) +# define GIT_INLINE(type) static __inline type +#elif defined(__GNUC__) +# define GIT_INLINE(type) static __inline__ type +#else +# define GIT_INLINE(type) static type +#endif + +/** Support for gcc/clang __has_builtin intrinsic */ +#ifndef __has_builtin +# define __has_builtin(x) 0 +#endif + +/** + * Declare that a function's return value must be used. + * + * Used mostly to guard against potential silent bugs at runtime. This is + * recommended to be added to functions that: + * + * - Allocate / reallocate memory. This prevents memory leaks or errors where + * buffers are expected to have grown to a certain size, but could not be + * resized. + * - Acquire locks. When a lock cannot be acquired, that will almost certainly + * cause a data race / undefined behavior. + */ +#if defined(__GNUC__) +# define GIT_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) +#else +# define GIT_WARN_UNUSED_RESULT +#endif + +#include <assert.h> +#include <errno.h> +#include <limits.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include <sys/types.h> +#include <sys/stat.h> + +#ifdef GIT_WIN32 + +# include <io.h> +# include <direct.h> +# include <winsock2.h> +# include <windows.h> +# include <ws2tcpip.h> +# include "win32/msvc-compat.h" +# include "win32/mingw-compat.h" +# include "win32/win32-compat.h" +# include "win32/w32_common.h" +# include "win32/version.h" +# include "win32/error.h" +# ifdef GIT_THREADS +# include "win32/thread.h" +# endif + +#else + +# include <unistd.h> +# include <strings.h> +# ifdef GIT_THREADS +# include <pthread.h> +# include <sched.h> +# endif + +#define GIT_LIBGIT2_CALL +#define GIT_SYSTEM_CALL + +#ifdef GIT_USE_STAT_ATIMESPEC +# define st_atim st_atimespec +# define st_ctim st_ctimespec +# define st_mtim st_mtimespec +#endif + +# include <arpa/inet.h> + +#endif + +#include "git2/types.h" +#include "git2/errors.h" +#include "thread.h" +#include "integer.h" +#include "assert_safe.h" + +#include "posix.h" + +#define DEFAULT_BUFSIZE 65536 +#define FILEIO_BUFSIZE DEFAULT_BUFSIZE +#define FILTERIO_BUFSIZE DEFAULT_BUFSIZE +#define NETIO_BUFSIZE DEFAULT_BUFSIZE + +/** + * Check a pointer allocation result, returning -1 if it failed. + */ +#define GIT_ERROR_CHECK_ALLOC(ptr) if (ptr == NULL) { return -1; } + +/** + * Check a buffer allocation result, returning -1 if it failed. + */ +#define GIT_ERROR_CHECK_ALLOC_STR(buf) if ((void *)(buf) == NULL || git_str_oom(buf)) { return -1; } + +/** + * Check a return value and propagate result if non-zero. + */ +#define GIT_ERROR_CHECK_ERROR(code) \ + do { int _err = (code); if (_err) return _err; } while (0) + +/** + * Initialize a structure with a version. + */ +GIT_INLINE(void) git__init_structure(void *structure, size_t len, unsigned int version) +{ + memset(structure, 0, len); + *((int*)structure) = version; +} +#define GIT_INIT_STRUCTURE(S,V) git__init_structure(S, sizeof(*S), V) + +#define GIT_INIT_STRUCTURE_FROM_TEMPLATE(PTR,VERSION,TYPE,TPL) do { \ + TYPE _tmpl = TPL; \ + GIT_ERROR_CHECK_VERSION(&(VERSION), _tmpl.version, #TYPE); \ + memcpy((PTR), &_tmpl, sizeof(_tmpl)); } while (0) + + +/** Check for additive overflow, setting an error if would occur. */ +#define GIT_ADD_SIZET_OVERFLOW(out, one, two) \ + (git__add_sizet_overflow(out, one, two) ? (git_error_set_oom(), 1) : 0) + +/** Check for additive overflow, setting an error if would occur. */ +#define GIT_MULTIPLY_SIZET_OVERFLOW(out, nelem, elsize) \ + (git__multiply_sizet_overflow(out, nelem, elsize) ? (git_error_set_oom(), 1) : 0) + +/** Check for additive overflow, failing if it would occur. */ +#define GIT_ERROR_CHECK_ALLOC_ADD(out, one, two) \ + if (GIT_ADD_SIZET_OVERFLOW(out, one, two)) { return -1; } + +#define GIT_ERROR_CHECK_ALLOC_ADD3(out, one, two, three) \ + if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), three)) { return -1; } + +#define GIT_ERROR_CHECK_ALLOC_ADD4(out, one, two, three, four) \ + if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), three) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), four)) { return -1; } + +#define GIT_ERROR_CHECK_ALLOC_ADD5(out, one, two, three, four, five) \ + if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), three) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), four) || \ + GIT_ADD_SIZET_OVERFLOW(out, *(out), five)) { return -1; } + +/** Check for multiplicative overflow, failing if it would occur. */ +#define GIT_ERROR_CHECK_ALLOC_MULTIPLY(out, nelem, elsize) \ + if (GIT_MULTIPLY_SIZET_OVERFLOW(out, nelem, elsize)) { return -1; } + +#include "util.h" + +// TODO TODO TODO +extern void git_error_set(int, const char *, ...); +extern const git_error * git_error_last(void); + +#endif diff --git a/src/util/hash.h b/src/util/hash.h index 10b14a904..a53a107c8 100644 --- a/src/util/hash.h +++ b/src/util/hash.h @@ -8,7 +8,7 @@ #ifndef INCLUDE_hash_h__ #define INCLUDE_hash_h__ -#include "common.h" +#include "git2_util.h" #include "git2/oid.h" #include "hash/sha1.h" diff --git a/src/util/hash/sha1.h b/src/util/hash/sha1.h index 4b4dae3f8..9d32bce42 100644 --- a/src/util/hash/sha1.h +++ b/src/util/hash/sha1.h @@ -8,7 +8,7 @@ #ifndef INCLUDE_hash_sha1_h__ #define INCLUDE_hash_sha1_h__ -#include "common.h" +#include "git2_util.h" typedef struct git_hash_sha1_ctx git_hash_sha1_ctx; diff --git a/src/util/map.h b/src/util/map.h index 01931d199..c101e46f6 100644 --- a/src/util/map.h +++ b/src/util/map.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_map_h__ #define INCLUDE_map_h__ -#include "common.h" +#include "git2_util.h" /* p_mmap() prot values */ diff --git a/src/util/net.c b/src/util/net.c index 58792ee7d..752e07e5a 100644 --- a/src/util/net.c +++ b/src/util/net.c @@ -6,7 +6,6 @@ */ #include "net.h" -#include "netops.h" #include <ctype.h> #include "git2/errors.h" diff --git a/src/util/net.h b/src/util/net.h index 728add3d0..314780ec7 100644 --- a/src/util/net.h +++ b/src/util/net.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_net_h__ #define INCLUDE_net_h__ -#include "common.h" +#include "git2_util.h" typedef struct git_net_url { char *scheme; diff --git a/src/util/pool.h b/src/util/pool.h index cecb84665..0238431b0 100644 --- a/src/util/pool.h +++ b/src/util/pool.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_pool_h__ #define INCLUDE_pool_h__ -#include "common.h" +#include "git2_util.h" #include "vector.h" diff --git a/src/util/posix.h b/src/util/posix.h index d98bc82ca..0223b4146 100644 --- a/src/util/posix.h +++ b/src/util/posix.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_posix_h__ #define INCLUDE_posix_h__ -#include "common.h" +#include "git2_util.h" #include <fcntl.h> #include <time.h> diff --git a/src/util/regexp.h b/src/util/regexp.h index 2592ef383..d0862b107 100644 --- a/src/util/regexp.h +++ b/src/util/regexp.h @@ -8,7 +8,7 @@ #ifndef INCLUDE_regexp_h__ #define INCLUDE_regexp_h__ -#include "common.h" +#include "git2_util.h" #if defined(GIT_REGEX_BUILTIN) || defined(GIT_REGEX_PCRE) # include "pcre.h" diff --git a/src/util/runtime.c b/src/util/runtime.c index c05dee8b9..a7711ffc4 100644 --- a/src/util/runtime.c +++ b/src/util/runtime.c @@ -5,7 +5,7 @@ * a Linking Exception. For full terms see the included COPYING file. */ -#include "common.h" +#include "git2_util.h" #include "runtime.h" static git_runtime_shutdown_fn shutdown_callback[32]; diff --git a/src/util/runtime.h b/src/util/runtime.h index 24ac58ee9..6cbfd6043 100644 --- a/src/util/runtime.h +++ b/src/util/runtime.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_runtime_h__ #define INCLUDE_runtime_h__ -#include "common.h" +#include "git2_util.h" typedef int (*git_runtime_init_fn)(void); typedef void (*git_runtime_shutdown_fn)(void); diff --git a/src/util/str.h b/src/util/str.h index af7acc21f..15e9b3304 100644 --- a/src/util/str.h +++ b/src/util/str.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_str_h__ #define INCLUDE_str_h__ -#include "common.h" +#include "git2_util.h" struct git_str { char *ptr; diff --git a/src/util/strmap.h b/src/util/strmap.h index 9f5e4cc8b..b64d3dcb5 100644 --- a/src/util/strmap.h +++ b/src/util/strmap.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_strmap_h__ #define INCLUDE_strmap_h__ -#include "common.h" +#include "git2_util.h" /** A map with C strings as key. */ typedef struct kh_str_s git_strmap; diff --git a/src/libgit2/strnlen.h b/src/util/strnlen.h index eecfe3c02..eecfe3c02 100644 --- a/src/libgit2/strnlen.h +++ b/src/util/strnlen.h diff --git a/src/util/thread.c b/src/util/thread.c index 3171771d7..bc7364f8c 100644 --- a/src/util/thread.c +++ b/src/util/thread.c @@ -5,7 +5,7 @@ * a Linking Exception. For full terms see the included COPYING file. */ -#include "common.h" +#include "git2_util.h" #if !defined(GIT_THREADS) diff --git a/src/util/tsort.c b/src/util/tsort.c index 045efad23..2ef03d03a 100644 --- a/src/util/tsort.c +++ b/src/util/tsort.c @@ -5,7 +5,7 @@ * a Linking Exception. For full terms see the included COPYING file. */ -#include "common.h" +#include "git2_util.h" /** * An array-of-pointers implementation of Python's Timsort diff --git a/src/util/unix/map.c b/src/util/unix/map.c index 88f283ce8..b45859b1b 100644 --- a/src/util/unix/map.c +++ b/src/util/unix/map.c @@ -5,7 +5,7 @@ * a Linking Exception. For full terms see the included COPYING file. */ -#include "common.h" +#include "git2_util.h" #include "git2/common.h" diff --git a/src/util/unix/realpath.c b/src/util/unix/realpath.c index 893bac87b..41321f604 100644 --- a/src/util/unix/realpath.c +++ b/src/util/unix/realpath.c @@ -5,7 +5,7 @@ * a Linking Exception. For full terms see the included COPYING file. */ -#include "common.h" +#include "git2_util.h" #include "git2/common.h" diff --git a/src/util/utf8.c b/src/util/utf8.c index 77065cb71..c566fdf20 100644 --- a/src/util/utf8.c +++ b/src/util/utf8.c @@ -7,7 +7,7 @@ #include "utf8.h" -#include "common.h" +#include "git2_util.h" /* * git_utf8_iterate is taken from the utf8proc project, diff --git a/src/util/utf8.h b/src/util/utf8.h index dff91b294..753ab07e2 100644 --- a/src/util/utf8.h +++ b/src/util/utf8.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_utf8_h__ #define INCLUDE_utf8_h__ -#include "common.h" +#include "git2_util.h" /* * Iterate through an UTF-8 string, yielding one codepoint at a time. diff --git a/src/util/util.c b/src/util/util.c index e06d4ca09..aee95fddf 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -7,7 +7,7 @@ #include "util.h" -#include "common.h" +#include "git2_util.h" #ifdef GIT_WIN32 # include "win32/utf-conv.h" diff --git a/src/util/util.h b/src/util/util.h index 30cdd0ddf..933b92794 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -12,7 +12,7 @@ #endif #include "str.h" -#include "common.h" +#include "git2_util.h" #include "strnlen.h" #include "thread.h" diff --git a/src/util/varint.h b/src/util/varint.h index 652e22486..79b8f5548 100644 --- a/src/util/varint.h +++ b/src/util/varint.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_varint_h__ #define INCLUDE_varint_h__ -#include "common.h" +#include "git2_util.h" #include <stdint.h> diff --git a/src/util/vector.h b/src/util/vector.h index 3dcec3d13..0a62f9005 100644 --- a/src/util/vector.h +++ b/src/util/vector.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_vector_h__ #define INCLUDE_vector_h__ -#include "common.h" +#include "git2_util.h" typedef int (*git_vector_cmp)(const void *, const void *); diff --git a/src/util/win32/dir.h b/src/util/win32/dir.h index acd64729e..810111534 100644 --- a/src/util/win32/dir.h +++ b/src/util/win32/dir.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_win32_dir_h__ #define INCLUDE_win32_dir_h__ -#include "common.h" +#include "git2_util.h" #include "w32_util.h" diff --git a/src/util/win32/error.h b/src/util/win32/error.h index 9e81141ce..fd53b7f99 100644 --- a/src/util/win32/error.h +++ b/src/util/win32/error.h @@ -8,7 +8,7 @@ #ifndef INCLUDE_win32_error_h__ #define INCLUDE_win32_error_h__ -#include "common.h" +#include "git2_util.h" extern char *git_win32_get_error_message(DWORD error_code); diff --git a/src/util/win32/findfile.h b/src/util/win32/findfile.h index e11ccebc5..41590f898 100644 --- a/src/util/win32/findfile.h +++ b/src/util/win32/findfile.h @@ -8,7 +8,7 @@ #ifndef INCLUDE_win32_findfile_h__ #define INCLUDE_win32_findfile_h__ -#include "common.h" +#include "git2_util.h" extern int git_win32__find_system_dirs(git_str *out, const wchar_t *subpath); extern int git_win32__find_global_dirs(git_str *out); diff --git a/src/util/win32/map.c b/src/util/win32/map.c index 2aabc9b15..52e1363ea 100644 --- a/src/util/win32/map.c +++ b/src/util/win32/map.c @@ -5,7 +5,7 @@ * a Linking Exception. For full terms see the included COPYING file. */ -#include "common.h" +#include "git2_util.h" #include "map.h" #include <errno.h> diff --git a/src/util/win32/path_w32.h b/src/util/win32/path_w32.h index 4fadf8d08..3ec603851 100644 --- a/src/util/win32/path_w32.h +++ b/src/util/win32/path_w32.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_win32_path_w32_h__ #define INCLUDE_win32_path_w32_h__ -#include "common.h" +#include "git2_util.h" /** * Create a Win32 path (in UCS-2 format) from a UTF-8 string. If the given diff --git a/src/util/win32/posix.h b/src/util/win32/posix.h index 87c6b436a..6c1a0a32e 100644 --- a/src/util/win32/posix.h +++ b/src/util/win32/posix.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_win32_posix_h__ #define INCLUDE_win32_posix_h__ -#include "common.h" +#include "git2_util.h" #include "../posix.h" #include "win32-compat.h" #include "path_w32.h" diff --git a/src/util/win32/posix_w32.c b/src/util/win32/posix_w32.c index ba46b5ea9..b387ac7fb 100644 --- a/src/util/win32/posix_w32.c +++ b/src/util/win32/posix_w32.c @@ -5,7 +5,7 @@ * a Linking Exception. For full terms see the included COPYING file. */ -#include "common.h" +#include "git2_util.h" #include "../posix.h" #include "../futils.h" diff --git a/src/util/win32/precompiled.h b/src/util/win32/precompiled.h index 806b1698a..1163c3d63 100644 --- a/src/util/win32/precompiled.h +++ b/src/util/win32/precompiled.h @@ -1,4 +1,4 @@ -#include "common.h" +#include "git2_util.h" #include <errno.h> #include <limits.h> diff --git a/src/util/win32/thread.h b/src/util/win32/thread.h index 8305036b4..184762e2a 100644 --- a/src/util/win32/thread.h +++ b/src/util/win32/thread.h @@ -8,7 +8,7 @@ #ifndef INCLUDE_win32_thread_h__ #define INCLUDE_win32_thread_h__ -#include "common.h" +#include "git2_util.h" #if defined (_MSC_VER) # define GIT_RESTRICT __restrict diff --git a/src/util/win32/utf-conv.h b/src/util/win32/utf-conv.h index 6090a4b35..120d647ef 100644 --- a/src/util/win32/utf-conv.h +++ b/src/util/win32/utf-conv.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_win32_utf_conv_h__ #define INCLUDE_win32_utf_conv_h__ -#include "common.h" +#include "git2_util.h" #include <wchar.h> diff --git a/src/util/win32/w32_buffer.h b/src/util/win32/w32_buffer.h index 4227296d8..68ea96035 100644 --- a/src/util/win32/w32_buffer.h +++ b/src/util/win32/w32_buffer.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_win32_w32_buffer_h__ #define INCLUDE_win32_w32_buffer_h__ -#include "common.h" +#include "git2_util.h" #include "str.h" /** diff --git a/src/util/win32/w32_leakcheck.h b/src/util/win32/w32_leakcheck.h index cb45e3675..82d863851 100644 --- a/src/util/win32/w32_leakcheck.h +++ b/src/util/win32/w32_leakcheck.h @@ -8,7 +8,7 @@ #ifndef INCLUDE_win32_leakcheck_h__ #define INCLUDE_win32_leakcheck_h__ -#include "common.h" +#include "git2_util.h" /* Initialize the win32 leak checking system. */ int git_win32_leakcheck_global_init(void); diff --git a/src/util/win32/w32_util.h b/src/util/win32/w32_util.h index 1321d30e6..519663720 100644 --- a/src/util/win32/w32_util.h +++ b/src/util/win32/w32_util.h @@ -8,7 +8,7 @@ #ifndef INCLUDE_win32_w32_util_h__ #define INCLUDE_win32_w32_util_h__ -#include "common.h" +#include "git2_util.h" #include "utf-conv.h" #include "posix.h" diff --git a/src/util/zstream.h b/src/util/zstream.h index 3a59d9a36..0afe6ee17 100644 --- a/src/util/zstream.h +++ b/src/util/zstream.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_zstream_h__ #define INCLUDE_zstream_h__ -#include "common.h" +#include "git2_util.h" #include <zlib.h> diff --git a/tests/core/utf8.c b/tests/core/utf8.c index 021828e9e..e1987b8d6 100644 --- a/tests/core/utf8.c +++ b/tests/core/utf8.c @@ -1,4 +1,5 @@ #include "clar_libgit2.h" +#include "utf8.h" void test_core_utf8__char_length(void) { |