{"thread":{"id":"62969","subject":"[PATCH v6 01/18] reftable/stack: stop using `read_in_full()`","startedAt":"2025-02-18T09:21:57Z","lastAt":"2025-03-20T15:17:44Z","messageCount":23,"participants":["Patrick Steinhardt","Junio C Hamano"],"isPatch":true,"patchVersion":6,"patchTotal":18},"messages":[{"id":"512565","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-1-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"[PATCH v6 01/18] reftable/stack: stop using `read_in_full()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:37Z","receivedAt":"2025-02-18T09:21:57Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"There is a single callsite of `read_in_full()` in the reftable library.\nOpen-code the function to reduce our dependency on the Git library.\n\nNote that we only partially port over the logic from `read_in_full()`\nand its underlying `xread()` helper. Most importantly, the latter also\nknows to handle `EWOULDBLOCK` via `handle_nonblock()`. This logic is\nirrelevant for us though because the reftable library never sets the\n`O_NONBLOCK` option in the first place.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/stack.c | 18 ++++++++++++++----\n 1 file changed, 14 insertions(+), 4 deletions(-)\n\ndiff --git a/reftable/stack.c b/reftable/stack.c\nindex 6c4e8be19b1..1cc47b94d9c 100644\n--- a/reftable/stack.c\n+++ b/reftable/stack.c\n@@ -115,13 +115,16 @@ int reftable_new_stack(struct reftable_stack **dest, const char *dir,\n \n static int fd_read_lines(int fd, char ***namesp)\n {\n-\toff_t size = lseek(fd, 0, SEEK_END);\n \tchar *buf = NULL;\n \tint err = 0;\n+\toff_t size;\n+\n+\tsize = lseek(fd, 0, SEEK_END);\n \tif (size < 0) {\n \t\terr = REFTABLE_IO_ERROR;\n \t\tgoto done;\n \t}\n+\n \terr = lseek(fd, 0, SEEK_SET);\n \tif (err < 0) {\n \t\terr = REFTABLE_IO_ERROR;\n@@ -134,9 +137,16 @@ static int fd_read_lines(int fd, char ***namesp)\n \t\tgoto done;\n \t}\n \n-\tif (read_in_full(fd, buf, size) != size) {\n-\t\terr = REFTABLE_IO_ERROR;\n-\t\tgoto done;\n+\tfor (off_t total_read = 0; total_read < size; ) {\n+\t\tssize_t bytes_read = read(fd, buf + total_read, size - total_read);\n+\t\tif (bytes_read < 0 && (errno == EAGAIN || errno == EINTR))\n+\t\t\tcontinue;\n+\t\tif (bytes_read < 0 || !bytes_read) {\n+\t\t\terr = REFTABLE_IO_ERROR;\n+\t\t\tgoto done;\n+\t\t}\n+\n+\t\ttotal_read += bytes_read;\n \t}\n \tbuf[size] = 0;\n \n\n-- \n2.48.1.666.gff9fcf71b7.dirty\n\n"},{"id":"512566","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250127-pks-reftable-drop-git-compat-util-v1-0-6e280a564877@pks.im","subject":"[PATCH v6 00/18] reftable: stop using \"git-compat-util.h\"","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:36Z","receivedAt":"2025-02-18T09:21:58Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Hi,\n\nthis patch series is the final step to fully decouple the reftable\nlibrary from the rest of the Git codebase. The goal of this is to make\nthe library reusable by other projects like libgit2 by simply copying\nover the source files, making Git the canonical upstream for reftable\nfunctionality.\n\nThis patch series stops using all kinds of helpers exposed by our\n\"git-compat-util.h\" header and open-codes them instead. In order to keep\nus from using these helpers by accident the final step is to pull out\nPOSIX-related bits and pieces into a new \"compat/posix.h\" header, which\nthe reftable library then uses instead of \"git-compat-util.h\".\n\nChanges in v3:\n  - Fix type of `total_read` variable used to track how many bytes we\n    have read in `fd_read_lines()`.\n  - Drop the patch use root-relative includes again. Let's rather\n    discuss this outside of the scope of this series.\n  - Link to v2: https://lore.kernel.org/r/20250128-pks-reftable-drop-git-compat-util-v2-0-c85c20336317@pks.im\n\nChanges in v4:\n  - Fix a couple of now-invalid relative includes that I missed. This\n    fixes the build issue in \"seen\" with Meson.\n  - Link to v3: https://lore.kernel.org/r/20250203-pks-reftable-drop-git-compat-util-v3-0-446c9ed4ee9e@pks.im\n\nChanges in v5:\n  - Rework the implementation of `REFTABLE_UNUSED` to use\n    `__attribute__((__unused__))`.\n  - Link to v4: https://lore.kernel.org/r/20250206-pks-reftable-drop-git-compat-util-v4-0-603d276d5f95@pks.im\n\nChanges in v6:\n  - I have rebased the patch series on top of \"master\" at 03944513488\n    (The eleventh batch, 2025-02-14). On the one hand this gets rid of\n    the conflict with the zlib-ng changes, which have meanwhile been\n    merged to that branch. And on the other hand it pulls in the new CI\n    build for MSVC+Meson, which allows me to have better test coverage\n    on Windows to better detect breakage there.\n  - Stop moving around Win32 files so that we do the minimum viable\n    change in this context. Hopefully, this makes it easier for Dscho to\n    integrate into Git for Windows. If it doesn't I don't really have\n    any other good ideas for how to do it, so the only alternative would\n    be to just ignore Windows altogether.\n  - Link to v5: https://lore.kernel.org/r/20250207-pks-reftable-drop-git-compat-util-v5-0-ba2adc79110f@pks.im\n\nThanks!\n\nPatrick\n\n---\nPatrick Steinhardt (18):\n      reftable/stack: stop using `read_in_full()`\n      reftable/stack: stop using `write_in_full()`\n      reftable/blocksource: stop using `xmmap()`\n      reftable/record: stop using `COPY_ARRAY()`\n      reftable/record: stop using `BUG()` in `reftable_record_init()`\n      reftable/record: don't `BUG()` in `reftable_record_cmp()`\n      reftable: stop using `BUG()` in trivial cases\n      reftable/basics: stop using `st_mult()` in array allocators\n      reftable/basics: provide wrappers for big endian conversion\n      reftable/reader: stop using `ARRAY_SIZE()` macro\n      reftable/system: introduce `reftable_rand()`\n      reftable/stack: stop using `sleep_millisec()`\n      reftable/basics: stop using `SWAP()` macro\n      reftable/basics: introduce `REFTABLE_UNUSED` annotation\n      compat/mingw: split out POSIX-related bits\n      git-compat-util.h: split out POSIX-emulating bits\n      reftable: decouple from Git codebase by pulling in \"compat/posix.h\"\n      Makefile: skip reftable library for Coccinelle\n\n Makefile                         |   2 +-\n compat/mingw-posix.h             | 431 +++++++++++++++++++++++++++++++\n compat/mingw.h                   | 426 +-----------------------------\n compat/msvc-posix.h              |  33 +++\n compat/msvc.h                    |  30 +--\n compat/posix.h                   | 541 +++++++++++++++++++++++++++++++++++++++\n git-compat-util.h                | 531 +-------------------------------------\n reftable/basics.c                |  19 --\n reftable/basics.h                | 123 ++++++++-\n reftable/block.c                 |  16 +-\n reftable/blocksource.c           |  17 +-\n reftable/iter.c                  |  13 +-\n reftable/merged.c                |  27 +-\n reftable/pq.c                    |  40 ++-\n reftable/pq.h                    |   2 +-\n reftable/reader.c                |  33 ++-\n reftable/record.c                |  84 +++---\n reftable/record.h                |   6 +-\n reftable/stack.c                 |  52 +++-\n reftable/system.c                |   7 +\n reftable/system.h                |   8 +-\n reftable/writer.c                |  27 +-\n t/unit-tests/t-reftable-basics.c |  28 +-\n t/unit-tests/t-reftable-pq.c     |  22 +-\n t/unit-tests/t-reftable-record.c |  42 ++-\n 25 files changed, 1405 insertions(+), 1155 deletions(-)\n\nRange-diff versus v5:\n\n 1:  18b6360460f =  1:  3589d759e69 reftable/stack: stop using `read_in_full()`\n 2:  4426ec59a5e =  2:  e9fb7b0821f reftable/stack: stop using `write_in_full()`\n 3:  4db9255d16d =  3:  f509993ee13 reftable/blocksource: stop using `xmmap()`\n 4:  2752fb006c5 =  4:  48247ebf145 reftable/record: stop using `COPY_ARRAY()`\n 5:  3bdd13f015e =  5:  795e9650146 reftable/record: stop using `BUG()` in `reftable_record_init()`\n 6:  21529172e83 =  6:  8221aec91a9 reftable/record: don't `BUG()` in `reftable_record_cmp()`\n 7:  e62c056f8e8 =  7:  8563af898e5 reftable: stop using `BUG()` in trivial cases\n 8:  b506a862abc =  8:  447c7166c97 reftable/basics: stop using `st_mult()` in array allocators\n 9:  b1749f88610 =  9:  108d40a63f5 reftable/basics: provide wrappers for big endian conversion\n10:  f583f0cfb03 = 10:  19068751b6c reftable/reader: stop using `ARRAY_SIZE()` macro\n11:  c59328f9299 ! 11:  316ca16a18c reftable/system: introduce `reftable_rand()`\n    @@ reftable/system.c\n     \n      ## reftable/system.h ##\n     @@ reftable/system.h: license that can be found in the LICENSE file or at\n    - \n      #include \"git-compat-util.h\"\n    + #include \"compat/zlib-compat.h\"\n      \n     +/*\n     + * Return a random 32 bit integer. This function is expected to return\n12:  0657d144327 = 12:  3cf2df99203 reftable/stack: stop using `sleep_millisec()`\n13:  504ae579c30 = 13:  5184d4185a6 reftable/basics: stop using `SWAP()` macro\n14:  34b0ae5f697 = 14:  a108a876c9a reftable/basics: introduce `REFTABLE_UNUSED` annotation\n15:  1552c1f45bf <  -:  ----------- compat/mingw: split out POSIX-related bits\n -:  ----------- > 15:  48186eb7aeb compat/mingw: split out POSIX-related bits\n16:  7cebd3c3381 ! 16:  16f2371c868 git-compat-util.h: split out POSIX-emulating bits\n    @@ compat/posix.h (new)\n     +#endif\n     +\n     +#if defined(__MINGW32__)\n    -+#include \"mingw/posix.h\"\n    ++#include \"mingw-posix.h\"\n     +#elif defined(_MSC_VER)\n    -+#include \"msvc/posix.h\"\n    ++#include \"msvc-posix.h\"\n     +#else\n     +#include <sys/utsname.h>\n     +#include <sys/wait.h>\n    @@ git-compat-util.h: static inline int _have_unix_sockets(void)\n     @@ git-compat-util.h: static inline int is_xplatform_dir_sep(int c)\n      #elif defined(_MSC_VER)\n      #include \"compat/win32/path-utils.h\"\n    - #include \"compat/msvc/compat-util.h\"\n    + #include \"compat/msvc.h\"\n     -#else\n     -#include <sys/utsname.h>\n     -#include <sys/wait.h>\n17:  dbb9b816d2e ! 17:  4461c8cf4a3 reftable: decouple from Git codebase by pulling in \"compat/posix.h\"\n    @@ reftable/system.h: license that can be found in the LICENSE file or at\n      /* This header glues the reftable library to the rest of Git */\n      \n     -#include \"git-compat-util.h\"\n    -+#include \"../compat/posix.h\"\n    -+#include <zlib.h>\n    ++#include \"compat/posix.h\"\n    + #include \"compat/zlib-compat.h\"\n      \n      /*\n    -  * Return a random 32 bit integer. This function is expected to return\n18:  f7b470f3387 = 18:  3e70da1abc8 Makefile: skip reftable library for Coccinelle\n\n---\nbase-commit: 03944513488db4a81fdb4c21c3b515e4cb260b05\nchange-id: 20241119-pks-reftable-drop-git-compat-util-470f2bfde562\n\n"},{"id":"512567","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-2-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"[PATCH v6 02/18] reftable/stack: stop using `write_in_full()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:38Z","receivedAt":"2025-02-18T09:21:59Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Similar to the preceding commit, drop our use of `write_in_full()` and\nimplement a new wrapper `reftable_write_full()` that handles this logic\nfor us. This is done to reduce our dependency on the Git library.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/stack.c | 28 ++++++++++++++++++++++++----\n 1 file changed, 24 insertions(+), 4 deletions(-)\n\ndiff --git a/reftable/stack.c b/reftable/stack.c\nindex 1cc47b94d9c..c33f0c3333a 100644\n--- a/reftable/stack.c\n+++ b/reftable/stack.c\n@@ -48,6 +48,25 @@ static int stack_fsync(const struct reftable_write_options *opts, int fd)\n \treturn fsync(fd);\n }\n \n+static ssize_t reftable_write_data(int fd, const void *data, size_t size)\n+{\n+\tsize_t total_written = 0;\n+\tconst char *p = data;\n+\n+\twhile (total_written < size) {\n+\t\tssize_t bytes_written = write(fd, p, size - total_written);\n+\t\tif (bytes_written < 0 && (errno == EAGAIN || errno == EINTR))\n+\t\t\tcontinue;\n+\t\tif (bytes_written < 0)\n+\t\t\treturn REFTABLE_IO_ERROR;\n+\n+\t\ttotal_written += bytes_written;\n+\t\tp += bytes_written;\n+\t}\n+\n+\treturn total_written;\n+}\n+\n struct fd_writer {\n \tconst struct reftable_write_options *opts;\n \tint fd;\n@@ -56,7 +75,7 @@ struct fd_writer {\n static ssize_t fd_writer_write(void *arg, const void *data, size_t sz)\n {\n \tstruct fd_writer *writer = arg;\n-\treturn write_in_full(writer->fd, data, sz);\n+\treturn reftable_write_data(writer->fd, data, sz);\n }\n \n static int fd_writer_flush(void *arg)\n@@ -784,7 +803,8 @@ int reftable_addition_commit(struct reftable_addition *add)\n \t\t\tgoto done;\n \t}\n \n-\terr = write_in_full(add->tables_list_lock.fd, table_list.buf, table_list.len);\n+\terr = reftable_write_data(add->tables_list_lock.fd,\n+\t\t\t\t  table_list.buf, table_list.len);\n \treftable_buf_release(&table_list);\n \tif (err < 0) {\n \t\terr = REFTABLE_IO_ERROR;\n@@ -1470,8 +1490,8 @@ static int stack_compact_range(struct reftable_stack *st,\n \t\t\tgoto done;\n \t}\n \n-\terr = write_in_full(tables_list_lock.fd,\n-\t\t\t    tables_list_buf.buf, tables_list_buf.len);\n+\terr = reftable_write_data(tables_list_lock.fd,\n+\t\t\t\t  tables_list_buf.buf, tables_list_buf.len);\n \tif (err < 0) {\n \t\terr = REFTABLE_IO_ERROR;\n \t\tunlink(new_table_path.buf);\n\n-- \n2.48.1.666.gff9fcf71b7.dirty\n\n"},{"id":"512568","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-3-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"[PATCH v6 03/18] reftable/blocksource: stop using `xmmap()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:39Z","receivedAt":"2025-02-18T09:22:00Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"We use `xmmap()` to map reftables into memory. This function has two\nproblems:\n\n  - It causes us to die in case the mmap fails.\n\n  - It ties us to the Git codebase.\n\nRefactor the code to use mmap(3p) instead with manual error checking.\nNote that this function may not be the system-provided mmap(3p), but may\npoint to our `git_mmap()` wrapper that emulates the syscall on systems\nthat do not have mmap(3p) available.\n\nFix `reftable_block_source_from_file()` to properly bubble up the error\ncode in case the map(3p) call fails.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/blocksource.c | 11 ++++++++---\n 1 file changed, 8 insertions(+), 3 deletions(-)\n\ndiff --git a/reftable/blocksource.c b/reftable/blocksource.c\nindex bba4a45b98a..02972c46f4b 100644\n--- a/reftable/blocksource.c\n+++ b/reftable/blocksource.c\n@@ -98,7 +98,7 @@ static struct reftable_block_source_vtable file_vtable = {\n int reftable_block_source_from_file(struct reftable_block_source *bs,\n \t\t\t\t    const char *name)\n {\n-\tstruct file_block_source *p;\n+\tstruct file_block_source *p = NULL;\n \tstruct stat st;\n \tint fd, err;\n \n@@ -122,7 +122,12 @@ int reftable_block_source_from_file(struct reftable_block_source *bs,\n \t}\n \n \tp->size = st.st_size;\n-\tp->data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);\n+\tp->data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);\n+\tif (p->data == MAP_FAILED) {\n+\t\terr = REFTABLE_IO_ERROR;\n+\t\tp->data = NULL;\n+\t\tgoto out;\n+\t}\n \n \tassert(!bs->ops);\n \tbs->ops = &file_vtable;\n@@ -135,5 +140,5 @@ int reftable_block_source_from_file(struct reftable_block_source *bs,\n \t\tclose(fd);\n \tif (err < 0)\n \t\treftable_free(p);\n-\treturn 0;\n+\treturn err;\n }\n\n-- \n2.48.1.666.gff9fcf71b7.dirty\n\n"},{"id":"512569","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-4-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"[PATCH v6 04/18] reftable/record: stop using `COPY_ARRAY()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:40Z","receivedAt":"2025-02-18T09:22:00Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Drop our use of `COPY_ARRAY()`, replacing it with an open-coded variant\nthereof. This is done to reduce our dependency on the Git library.\n\nWhile at it, guard the whole array copy logic so that we only copy it in\ncase there actually is anything to be copied. Otherwise, we may end up\ntrying to allocate a zero-sized array, which will return a NULL pointer\nand thus cause us to return an `REFTABLE_OUT_OF_MEMORY_ERROR`.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/record.c | 16 +++++++++++-----\n 1 file changed, 11 insertions(+), 5 deletions(-)\n\ndiff --git a/reftable/record.c b/reftable/record.c\nindex 8919df8a4d3..2c0cc32cbd3 100644\n--- a/reftable/record.c\n+++ b/reftable/record.c\n@@ -504,11 +504,17 @@ static int reftable_obj_record_copy_from(void *rec, const void *src_rec,\n \tif (src->hash_prefix_len)\n \t\tmemcpy(obj->hash_prefix, src->hash_prefix, obj->hash_prefix_len);\n \n-\tREFTABLE_ALLOC_ARRAY(obj->offsets, src->offset_len);\n-\tif (!obj->offsets)\n-\t\treturn REFTABLE_OUT_OF_MEMORY_ERROR;\n-\tobj->offset_len = src->offset_len;\n-\tCOPY_ARRAY(obj->offsets, src->offsets, src->offset_len);\n+\tif (src->offset_len) {\n+\t\tif (sizeof(*src->offsets) > SIZE_MAX / src->offset_len)\n+\t\t\treturn REFTABLE_OUT_OF_MEMORY_ERROR;\n+\n+\t\tREFTABLE_ALLOC_ARRAY(obj->offsets, src->offset_len);\n+\t\tif (!obj->offsets)\n+\t\t\treturn REFTABLE_OUT_OF_MEMORY_ERROR;\n+\n+\t\tmemcpy(obj->offsets, src->offsets, sizeof(*src->offsets) * src->offset_len);\n+\t\tobj->offset_len = src->offset_len;\n+\t}\n \n \treturn 0;\n }\n\n-- \n2.48.1.666.gff9fcf71b7.dirty\n\n"},{"id":"512570","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-5-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"[PATCH v6 05/18] reftable/record: stop using `BUG()` in `reftable_record_init()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:41Z","receivedAt":"2025-02-18T09:22:01Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"We're aborting the program via `BUG()` in case `reftable_record_init()`\nwas invoked with an unknown record type. This is bad because we may now\ndie in library code, and because it makes us depend on the Git codebase.\n\nRefactor the code such that `reftable_record_init()` can return an error\ncode to the caller. Adapt any callers accordingly.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/block.c                 | 4 +++-\n reftable/merged.c                | 5 ++++-\n reftable/reader.c                | 5 ++++-\n reftable/record.c                | 8 ++++----\n reftable/record.h                | 4 ++--\n t/unit-tests/t-reftable-pq.c     | 6 +++---\n t/unit-tests/t-reftable-record.c | 2 +-\n 7 files changed, 21 insertions(+), 13 deletions(-)\n\ndiff --git a/reftable/block.c b/reftable/block.c\nindex b14a8f1259a..999876826d1 100644\n--- a/reftable/block.c\n+++ b/reftable/block.c\n@@ -508,7 +508,9 @@ int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,\n \tit->block_len = br->block_len;\n \tit->hash_size = br->hash_size;\n \n-\treftable_record_init(&rec, block_reader_type(br));\n+\terr = reftable_record_init(&rec, block_reader_type(br));\n+\tif (err < 0)\n+\t\tgoto done;\n \n \t/*\n \t * We're looking for the last entry less than the wanted key so that\ndiff --git a/reftable/merged.c b/reftable/merged.c\nindex e72b39e178d..4156eec07fc 100644\n--- a/reftable/merged.c\n+++ b/reftable/merged.c\n@@ -253,7 +253,10 @@ int merged_table_init_iter(struct reftable_merged_table *mt,\n \t}\n \n \tfor (size_t i = 0; i < mt->readers_len; i++) {\n-\t\treftable_record_init(&subiters[i].rec, typ);\n+\t\tret = reftable_record_init(&subiters[i].rec, typ);\n+\t\tif (ret < 0)\n+\t\t\tgoto out;\n+\n \t\tret = reader_init_iter(mt->readers[i], &subiters[i].iter, typ);\n \t\tif (ret < 0)\n \t\t\tgoto out;\ndiff --git a/reftable/reader.c b/reftable/reader.c\nindex 3f2e4b28005..de6e6dd9322 100644\n--- a/reftable/reader.c\n+++ b/reftable/reader.c\n@@ -360,7 +360,10 @@ static int table_iter_seek_linear(struct table_iter *ti,\n \tstruct reftable_record rec;\n \tint err;\n \n-\treftable_record_init(&rec, reftable_record_type(want));\n+\terr = reftable_record_init(&rec, reftable_record_type(want));\n+\tif (err < 0)\n+\t\tgoto done;\n+\n \terr = reftable_record_key(want, &want_key);\n \tif (err < 0)\n \t\tgoto done;\ndiff --git a/reftable/record.c b/reftable/record.c\nindex 2c0cc32cbd3..1e18f8dffb1 100644\n--- a/reftable/record.c\n+++ b/reftable/record.c\n@@ -1306,7 +1306,7 @@ reftable_record_vtable(struct reftable_record *rec)\n \tabort();\n }\n \n-void reftable_record_init(struct reftable_record *rec, uint8_t typ)\n+int reftable_record_init(struct reftable_record *rec, uint8_t typ)\n {\n \tmemset(rec, 0, sizeof(*rec));\n \trec->type = typ;\n@@ -1315,11 +1315,11 @@ void reftable_record_init(struct reftable_record *rec, uint8_t typ)\n \tcase BLOCK_TYPE_REF:\n \tcase BLOCK_TYPE_LOG:\n \tcase BLOCK_TYPE_OBJ:\n-\t\treturn;\n+\t\treturn 0;\n \tcase BLOCK_TYPE_INDEX:\n \t\treftable_buf_init(&rec->u.idx.last_key);\n-\t\treturn;\n+\t\treturn 0;\n \tdefault:\n-\t\tBUG(\"unhandled record type\");\n+\t\treturn REFTABLE_API_ERROR;\n \t}\n }\ndiff --git a/reftable/record.h b/reftable/record.h\nindex c7755a4d750..e1846c294ba 100644\n--- a/reftable/record.h\n+++ b/reftable/record.h\n@@ -130,8 +130,8 @@ struct reftable_record {\n \t} u;\n };\n \n-/* Initialize the reftable record for the given type */\n-void reftable_record_init(struct reftable_record *rec, uint8_t typ);\n+/* Initialize the reftable record for the given type. */\n+int reftable_record_init(struct reftable_record *rec, uint8_t typ);\n \n /* see struct record_vtable */\n int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b);\ndiff --git a/t/unit-tests/t-reftable-pq.c b/t/unit-tests/t-reftable-pq.c\nindex f3f8a0cdf38..d8a4c283a11 100644\n--- a/t/unit-tests/t-reftable-pq.c\n+++ b/t/unit-tests/t-reftable-pq.c\n@@ -32,7 +32,7 @@ static void t_pq_record(void)\n \tchar *last = NULL;\n \n \tfor (i = 0; i < N; i++) {\n-\t\treftable_record_init(&recs[i], BLOCK_TYPE_REF);\n+\t\tcheck(!reftable_record_init(&recs[i], BLOCK_TYPE_REF));\n \t\trecs[i].u.ref.refname = xstrfmt(\"%02\"PRIuMAX, (uintmax_t)i);\n \t}\n \n@@ -72,7 +72,7 @@ static void t_pq_index(void)\n \tsize_t N = ARRAY_SIZE(recs), i;\n \n \tfor (i = 0; i < N; i++) {\n-\t\treftable_record_init(&recs[i], BLOCK_TYPE_REF);\n+\t\tcheck(!reftable_record_init(&recs[i], BLOCK_TYPE_REF));\n \t\trecs[i].u.ref.refname = (char *) \"refs/heads/master\";\n \t}\n \n@@ -111,7 +111,7 @@ static void t_merged_iter_pqueue_top(void)\n \tsize_t N = ARRAY_SIZE(recs), i;\n \n \tfor (i = 0; i < N; i++) {\n-\t\treftable_record_init(&recs[i], BLOCK_TYPE_REF);\n+\t\tcheck(!reftable_record_init(&recs[i], BLOCK_TYPE_REF));\n \t\trecs[i].u.ref.refname = (char *) \"refs/heads/master\";\n \t}\n \ndiff --git a/t/unit-tests/t-reftable-record.c b/t/unit-tests/t-reftable-record.c\nindex d49d2a2729c..6540bd20e37 100644\n--- a/t/unit-tests/t-reftable-record.c\n+++ b/t/unit-tests/t-reftable-record.c\n@@ -17,7 +17,7 @@ static void t_copy(struct reftable_record *rec)\n \tuint8_t typ;\n \n \ttyp = reftable_record_type(rec);\n-\treftable_record_init(&copy, typ);\n+\tcheck(!reftable_record_init(&copy, typ));\n \treftable_record_copy_from(&copy, rec, REFTABLE_HASH_SIZE_SHA1);\n \t/* do it twice to catch memory leaks */\n \treftable_record_copy_from(&copy, rec, REFTABLE_HASH_SIZE_SHA1);\n\n-- \n2.48.1.666.gff9fcf71b7.dirty\n\n"},{"id":"512571","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-6-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"[PATCH v6 06/18] reftable/record: don't `BUG()` in `reftable_record_cmp()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:42Z","receivedAt":"2025-02-18T09:22:01Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"The reftable library aborts with a bug in case `reftable_record_cmp()`\nis invoked with two records of differing types. This would cause the\nprogram to die without the caller being able to handle the error, which\nis not something we want in the context of library code. And it ties us\nto the Git codebase.\n\nRefactor the code such that `reftable_record_cmp()` returns an error\ncode separate from the actual comparison result. This requires us to\nalso adapt some callers up the callchain in a similar fashion.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/merged.c                | 20 +++++++++++++++-----\n reftable/pq.c                    | 36 +++++++++++++++++++++++++++++-------\n reftable/pq.h                    |  2 +-\n reftable/record.c                | 10 ++++++----\n reftable/record.h                |  2 +-\n t/unit-tests/t-reftable-pq.c     | 16 ++++++++++++----\n t/unit-tests/t-reftable-record.c | 40 ++++++++++++++++++++++++++++------------\n 7 files changed, 92 insertions(+), 34 deletions(-)\n\ndiff --git a/reftable/merged.c b/reftable/merged.c\nindex 4156eec07fc..563864068c1 100644\n--- a/reftable/merged.c\n+++ b/reftable/merged.c\n@@ -66,8 +66,11 @@ static int merged_iter_seek(struct merged_iter *mi, struct reftable_record *want\n \tint err;\n \n \tmi->advance_index = -1;\n-\twhile (!merged_iter_pqueue_is_empty(mi->pq))\n-\t\tmerged_iter_pqueue_remove(&mi->pq);\n+\twhile (!merged_iter_pqueue_is_empty(mi->pq)) {\n+\t\terr = merged_iter_pqueue_remove(&mi->pq, NULL);\n+\t\tif (err < 0)\n+\t\t\treturn err;\n+\t}\n \n \tfor (size_t i = 0; i < mi->subiters_len; i++) {\n \t\terr = iterator_seek(&mi->subiters[i].iter, want);\n@@ -120,7 +123,9 @@ static int merged_iter_next_entry(struct merged_iter *mi,\n \tif (empty)\n \t\treturn 1;\n \n-\tentry = merged_iter_pqueue_remove(&mi->pq);\n+\terr = merged_iter_pqueue_remove(&mi->pq, &entry);\n+\tif (err < 0)\n+\t\treturn err;\n \n \t/*\n \t  One can also use reftable as datacenter-local storage, where the ref\n@@ -134,11 +139,16 @@ static int merged_iter_next_entry(struct merged_iter *mi,\n \t\tstruct pq_entry top = merged_iter_pqueue_top(mi->pq);\n \t\tint cmp;\n \n-\t\tcmp = reftable_record_cmp(top.rec, entry.rec);\n+\t\terr = reftable_record_cmp(top.rec, entry.rec, &cmp);\n+\t\tif (err < 0)\n+\t\t\treturn err;\n \t\tif (cmp > 0)\n \t\t\tbreak;\n \n-\t\tmerged_iter_pqueue_remove(&mi->pq);\n+\t\terr = merged_iter_pqueue_remove(&mi->pq, NULL);\n+\t\tif (err < 0)\n+\t\t\treturn err;\n+\n \t\terr = merged_iter_advance_subiter(mi, top.index);\n \t\tif (err < 0)\n \t\t\treturn err;\ndiff --git a/reftable/pq.c b/reftable/pq.c\nindex 5591e875e1e..ef8035cfd9b 100644\n--- a/reftable/pq.c\n+++ b/reftable/pq.c\n@@ -15,13 +15,18 @@ license that can be found in the LICENSE file or at\n \n int pq_less(struct pq_entry *a, struct pq_entry *b)\n {\n-\tint cmp = reftable_record_cmp(a->rec, b->rec);\n+\tint cmp, err;\n+\n+\terr = reftable_record_cmp(a->rec, b->rec, &cmp);\n+\tif (err < 0)\n+\t\treturn err;\n+\n \tif (cmp == 0)\n \t\treturn a->index > b->index;\n \treturn cmp < 0;\n }\n \n-struct pq_entry merged_iter_pqueue_remove(struct merged_iter_pqueue *pq)\n+int merged_iter_pqueue_remove(struct merged_iter_pqueue *pq, struct pq_entry *out)\n {\n \tsize_t i = 0;\n \tstruct pq_entry e = pq->heap[0];\n@@ -32,17 +37,34 @@ struct pq_entry merged_iter_pqueue_remove(struct merged_iter_pqueue *pq)\n \t\tsize_t min = i;\n \t\tsize_t j = 2 * i + 1;\n \t\tsize_t k = 2 * i + 2;\n-\t\tif (j < pq->len && pq_less(&pq->heap[j], &pq->heap[i]))\n-\t\t\tmin = j;\n-\t\tif (k < pq->len && pq_less(&pq->heap[k], &pq->heap[min]))\n-\t\t\tmin = k;\n+\t\tint cmp;\n+\n+\t\tif (j < pq->len) {\n+\t\t\tcmp = pq_less(&pq->heap[j], &pq->heap[i]);\n+\t\t\tif (cmp < 0)\n+\t\t\t\treturn -1;\n+\t\t\telse if (cmp)\n+\t\t\t\tmin = j;\n+\t\t}\n+\n+\t\tif (k < pq->len) {\n+\t\t\tcmp = pq_less(&pq->heap[k], &pq->heap[min]);\n+\t\t\tif (cmp < 0)\n+\t\t\t\treturn -1;\n+\t\t\telse if (cmp)\n+\t\t\t\tmin = k;\n+\t\t}\n+\n \t\tif (min == i)\n \t\t\tbreak;\n \t\tSWAP(pq->heap[i], pq->heap[min]);\n \t\ti = min;\n \t}\n \n-\treturn e;\n+\tif (out)\n+\t\t*out = e;\n+\n+\treturn 0;\n }\n \n int merged_iter_pqueue_add(struct merged_iter_pqueue *pq, const struct pq_entry *e)\ndiff --git a/reftable/pq.h b/reftable/pq.h\nindex 83c062eecac..ff39016445b 100644\n--- a/reftable/pq.h\n+++ b/reftable/pq.h\n@@ -22,7 +22,7 @@ struct merged_iter_pqueue {\n \tsize_t cap;\n };\n \n-struct pq_entry merged_iter_pqueue_remove(struct merged_iter_pqueue *pq);\n+int merged_iter_pqueue_remove(struct merged_iter_pqueue *pq, struct pq_entry *out);\n int merged_iter_pqueue_add(struct merged_iter_pqueue *pq, const struct pq_entry *e);\n void merged_iter_pqueue_release(struct merged_iter_pqueue *pq);\n int pq_less(struct pq_entry *a, struct pq_entry *b);\ndiff --git a/reftable/record.c b/reftable/record.c\nindex 1e18f8dffb1..b39d99fcc75 100644\n--- a/reftable/record.c\n+++ b/reftable/record.c\n@@ -1195,12 +1195,14 @@ int reftable_record_is_deletion(struct reftable_record *rec)\n \t\treftable_record_data(rec));\n }\n \n-int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b)\n+int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b,\n+\t\t\tint *cmp)\n {\n \tif (a->type != b->type)\n-\t\tBUG(\"cannot compare reftable records of different type\");\n-\treturn reftable_record_vtable(a)->cmp(\n-\t\treftable_record_data(a), reftable_record_data(b));\n+\t\treturn -1;\n+\t*cmp = reftable_record_vtable(a)->cmp(reftable_record_data(a),\n+\t\t\t\t\t      reftable_record_data(b));\n+\treturn 0;\n }\n \n int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, uint32_t hash_size)\ndiff --git a/reftable/record.h b/reftable/record.h\nindex e1846c294ba..867810a9328 100644\n--- a/reftable/record.h\n+++ b/reftable/record.h\n@@ -134,7 +134,7 @@ struct reftable_record {\n int reftable_record_init(struct reftable_record *rec, uint8_t typ);\n \n /* see struct record_vtable */\n-int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b);\n+int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b, int *cmp);\n int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, uint32_t hash_size);\n int reftable_record_key(struct reftable_record *rec, struct reftable_buf *dest);\n int reftable_record_copy_from(struct reftable_record *rec,\ndiff --git a/t/unit-tests/t-reftable-pq.c b/t/unit-tests/t-reftable-pq.c\nindex d8a4c283a11..c128fe8616a 100644\n--- a/t/unit-tests/t-reftable-pq.c\n+++ b/t/unit-tests/t-reftable-pq.c\n@@ -21,7 +21,9 @@ static void merged_iter_pqueue_check(const struct merged_iter_pqueue *pq)\n \n static int pq_entry_equal(struct pq_entry *a, struct pq_entry *b)\n {\n-\treturn !reftable_record_cmp(a->rec, b->rec) && (a->index == b->index);\n+\tint cmp;\n+\tcheck(!reftable_record_cmp(a->rec, b->rec, &cmp));\n+\treturn !cmp && (a->index == b->index);\n }\n \n static void t_pq_record(void)\n@@ -49,7 +51,9 @@ static void t_pq_record(void)\n \n \twhile (!merged_iter_pqueue_is_empty(pq)) {\n \t\tstruct pq_entry top = merged_iter_pqueue_top(pq);\n-\t\tstruct pq_entry e = merged_iter_pqueue_remove(&pq);\n+\t\tstruct pq_entry e;\n+\n+\t\tcheck(!merged_iter_pqueue_remove(&pq, &e));\n \t\tmerged_iter_pqueue_check(&pq);\n \n \t\tcheck(pq_entry_equal(&top, &e));\n@@ -90,7 +94,9 @@ static void t_pq_index(void)\n \n \tfor (i = N - 1; i > 0; i--) {\n \t\tstruct pq_entry top = merged_iter_pqueue_top(pq);\n-\t\tstruct pq_entry e = merged_iter_pqueue_remove(&pq);\n+\t\tstruct pq_entry e;\n+\n+\t\tcheck(!merged_iter_pqueue_remove(&pq, &e));\n \t\tmerged_iter_pqueue_check(&pq);\n \n \t\tcheck(pq_entry_equal(&top, &e));\n@@ -129,7 +135,9 @@ static void t_merged_iter_pqueue_top(void)\n \n \tfor (i = N - 1; i > 0; i--) {\n \t\tstruct pq_entry top = merged_iter_pqueue_top(pq);\n-\t\tstruct pq_entry e = merged_iter_pqueue_remove(&pq);\n+\t\tstruct pq_entry e;\n+\n+\t\tcheck(!merged_iter_pqueue_remove(&pq, &e));\n \n \t\tmerged_iter_pqueue_check(&pq);\n \t\tcheck(pq_entry_equal(&top, &e));\ndiff --git a/t/unit-tests/t-reftable-record.c b/t/unit-tests/t-reftable-record.c\nindex 6540bd20e37..59549663736 100644\n--- a/t/unit-tests/t-reftable-record.c\n+++ b/t/unit-tests/t-reftable-record.c\n@@ -100,16 +100,20 @@ static void t_reftable_ref_record_comparison(void)\n \t\t\t.u.ref.value.symref = (char *) \"refs/heads/master\",\n \t\t},\n \t};\n+\tint cmp;\n \n \tcheck(!reftable_record_equal(&in[0], &in[1], REFTABLE_HASH_SIZE_SHA1));\n-\tcheck(!reftable_record_cmp(&in[0], &in[1]));\n+\tcheck(!reftable_record_cmp(&in[0], &in[1], &cmp));\n+\tcheck(!cmp);\n \n \tcheck(!reftable_record_equal(&in[1], &in[2], REFTABLE_HASH_SIZE_SHA1));\n-\tcheck_int(reftable_record_cmp(&in[1], &in[2]), >, 0);\n+\tcheck(!reftable_record_cmp(&in[1], &in[2], &cmp));\n+\tcheck_int(cmp, >, 0);\n \n \tin[1].u.ref.value_type = in[0].u.ref.value_type;\n \tcheck(reftable_record_equal(&in[0], &in[1], REFTABLE_HASH_SIZE_SHA1));\n-\tcheck(!reftable_record_cmp(&in[0], &in[1]));\n+\tcheck(!reftable_record_cmp(&in[0], &in[1], &cmp));\n+\tcheck(!cmp);\n }\n \n static void t_reftable_ref_record_compare_name(void)\n@@ -209,17 +213,20 @@ static void t_reftable_log_record_comparison(void)\n \t\t\t.u.log.update_index = 22,\n \t\t},\n \t};\n+\tint cmp;\n \n \tcheck(!reftable_record_equal(&in[0], &in[1], REFTABLE_HASH_SIZE_SHA1));\n \tcheck(!reftable_record_equal(&in[1], &in[2], REFTABLE_HASH_SIZE_SHA1));\n-\tcheck_int(reftable_record_cmp(&in[1], &in[2]), >, 0);\n+\tcheck(!reftable_record_cmp(&in[1], &in[2], &cmp));\n+\tcheck_int(cmp, >, 0);\n \t/* comparison should be reversed for equal keys, because\n \t * comparison is now performed on the basis of update indices */\n-\tcheck_int(reftable_record_cmp(&in[0], &in[1]), <, 0);\n+\tcheck(!reftable_record_cmp(&in[0], &in[1], &cmp));\n+\tcheck_int(cmp, <, 0);\n \n \tin[1].u.log.update_index = in[0].u.log.update_index;\n \tcheck(reftable_record_equal(&in[0], &in[1], REFTABLE_HASH_SIZE_SHA1));\n-\tcheck(!reftable_record_cmp(&in[0], &in[1]));\n+\tcheck(!reftable_record_cmp(&in[0], &in[1], &cmp));\n }\n \n static void t_reftable_log_record_compare_key(void)\n@@ -396,16 +403,20 @@ static void t_reftable_obj_record_comparison(void)\n \t\t\t.u.obj.hash_prefix_len = 5,\n \t\t},\n \t};\n+\tint cmp;\n \n \tcheck(!reftable_record_equal(&in[0], &in[1], REFTABLE_HASH_SIZE_SHA1));\n-\tcheck(!reftable_record_cmp(&in[0], &in[1]));\n+\tcheck(!reftable_record_cmp(&in[0], &in[1], &cmp));\n+\tcheck(!cmp);\n \n \tcheck(!reftable_record_equal(&in[1], &in[2], REFTABLE_HASH_SIZE_SHA1));\n-\tcheck_int(reftable_record_cmp(&in[1], &in[2]), >, 0);\n+\tcheck(!reftable_record_cmp(&in[1], &in[2], &cmp));\n+\tcheck_int(cmp, >, 0);\n \n \tin[1].u.obj.offset_len = in[0].u.obj.offset_len;\n \tcheck(reftable_record_equal(&in[0], &in[1], REFTABLE_HASH_SIZE_SHA1));\n-\tcheck(!reftable_record_cmp(&in[0], &in[1]));\n+\tcheck(!reftable_record_cmp(&in[0], &in[1], &cmp));\n+\tcheck(!cmp);\n }\n \n static void t_reftable_obj_record_roundtrip(void)\n@@ -486,19 +497,24 @@ static void t_reftable_index_record_comparison(void)\n \t\t\t.u.idx.last_key = REFTABLE_BUF_INIT,\n \t\t},\n \t};\n+\tint cmp;\n+\n \tcheck(!reftable_buf_addstr(&in[0].u.idx.last_key, \"refs/heads/master\"));\n \tcheck(!reftable_buf_addstr(&in[1].u.idx.last_key, \"refs/heads/master\"));\n \tcheck(!reftable_buf_addstr(&in[2].u.idx.last_key, \"refs/heads/branch\"));\n \n \tcheck(!reftable_record_equal(&in[0], &in[1], REFTABLE_HASH_SIZE_SHA1));\n-\tcheck(!reftable_record_cmp(&in[0], &in[1]));\n+\tcheck(!reftable_record_cmp(&in[0], &in[1], &cmp));\n+\tcheck(!cmp);\n \n \tcheck(!reftable_record_equal(&in[1], &in[2], REFTABLE_HASH_SIZE_SHA1));\n-\tcheck_int(reftable_record_cmp(&in[1], &in[2]), >, 0);\n+\tcheck(!reftable_record_cmp(&in[1], &in[2], &cmp));\n+\tcheck_int(cmp, >, 0);\n \n \tin[1].u.idx.offset = in[0].u.idx.offset;\n \tcheck(reftable_record_equal(&in[0], &in[1], REFTABLE_HASH_SIZE_SHA1));\n-\tcheck(!reftable_record_cmp(&in[0], &in[1]));\n+\tcheck(!reftable_record_cmp(&in[0], &in[1], &cmp));\n+\tcheck(!cmp);\n \n \tfor (size_t i = 0; i < ARRAY_SIZE(in); i++)\n \t\treftable_record_release(&in[i]);\n\n-- \n2.48.1.666.gff9fcf71b7.dirty\n\n"},{"id":"512572","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-8-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"[PATCH v6 08/18] reftable/basics: stop using `st_mult()` in array allocators","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:44Z","receivedAt":"2025-02-18T09:22:02Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"We're using `st_mult()` as part of our macro helpers that allocate\narrays. This is bad due two two reasons:\n\n  - `st_mult()` causes us to die in case the multiplication overflows.\n\n  - `st_mult()` ties us to the Git codebase.\n\nRefactor the code to instead detect overflows manually and return an\nerror in such cases.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/basics.h | 36 ++++++++++++++++++++++++++++++++----\n 1 file changed, 32 insertions(+), 4 deletions(-)\n\ndiff --git a/reftable/basics.h b/reftable/basics.h\nindex a2a010a0e19..646f8d67f2d 100644\n--- a/reftable/basics.h\n+++ b/reftable/basics.h\n@@ -117,18 +117,46 @@ void reftable_free(void *p);\n void *reftable_calloc(size_t nelem, size_t elsize);\n char *reftable_strdup(const char *str);\n \n-#define REFTABLE_ALLOC_ARRAY(x, alloc) (x) = reftable_malloc(st_mult(sizeof(*(x)), (alloc)))\n+static inline int reftable_alloc_size(size_t nelem, size_t elsize, size_t *out)\n+{\n+\tif (nelem && elsize > SIZE_MAX / nelem)\n+\t\treturn -1;\n+\t*out = nelem * elsize;\n+\treturn 0;\n+}\n+\n+#define REFTABLE_ALLOC_ARRAY(x, alloc) do { \\\n+\t\tsize_t alloc_size; \\\n+\t\tif (reftable_alloc_size(sizeof(*(x)), (alloc), &alloc_size) < 0) { \\\n+\t\t\terrno = ENOMEM; \\\n+\t\t\t(x) = NULL; \\\n+\t\t} else { \\\n+\t\t\t(x) = reftable_malloc(alloc_size); \\\n+\t\t} \\\n+\t} while (0)\n #define REFTABLE_CALLOC_ARRAY(x, alloc) (x) = reftable_calloc((alloc), sizeof(*(x)))\n-#define REFTABLE_REALLOC_ARRAY(x, alloc) (x) = reftable_realloc((x), st_mult(sizeof(*(x)), (alloc)))\n+#define REFTABLE_REALLOC_ARRAY(x, alloc) do { \\\n+\t\tsize_t alloc_size; \\\n+\t\tif (reftable_alloc_size(sizeof(*(x)), (alloc), &alloc_size) < 0) { \\\n+\t\t\terrno = ENOMEM; \\\n+\t\t\t(x) = NULL; \\\n+\t\t} else { \\\n+\t\t\t(x) = reftable_realloc((x), alloc_size); \\\n+\t\t} \\\n+\t} while (0)\n \n static inline void *reftable_alloc_grow(void *p, size_t nelem, size_t elsize,\n \t\t\t\t\tsize_t *allocp)\n {\n \tvoid *new_p;\n-\tsize_t alloc = *allocp * 2 + 1;\n+\tsize_t alloc = *allocp * 2 + 1, alloc_bytes;\n \tif (alloc < nelem)\n \t\talloc = nelem;\n-\tnew_p = reftable_realloc(p, st_mult(elsize, alloc));\n+\tif (reftable_alloc_size(elsize, alloc, &alloc_bytes) < 0) {\n+\t\terrno = ENOMEM;\n+\t\treturn p;\n+\t}\n+\tnew_p = reftable_realloc(p, alloc_bytes);\n \tif (!new_p)\n \t\treturn p;\n \t*allocp = alloc;\n\n-- \n2.48.1.666.gff9fcf71b7.dirty\n\n"},{"id":"512573","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-7-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"[PATCH v6 07/18] reftable: stop using `BUG()` in trivial cases","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:43Z","receivedAt":"2025-02-18T09:22:02Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Stop using `BUG()` in the remaining trivial cases that we still have in\nthe reftable library. Instead of aborting the program, we'll now bubble\nup a `REFTABLE_API_ERROR` to indicate misuse of the calling conventions.\n\nNote that in both `reftable_reader_{inc,dec}ref()` we simply stop\ncalling `BUG()` altogether. The only situation where the counter should\nbe zero is when the structure has already been free'd anyway, so we\nwould run into undefined behaviour regardless of whether we try to abort\nthe program or not.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/iter.c   | 3 +--\n reftable/reader.c | 4 ----\n reftable/writer.c | 5 ++---\n 3 files changed, 3 insertions(+), 9 deletions(-)\n\ndiff --git a/reftable/iter.c b/reftable/iter.c\nindex 86e801ca9fb..b2ffb09c16b 100644\n--- a/reftable/iter.c\n+++ b/reftable/iter.c\n@@ -146,8 +146,7 @@ static int indexed_table_ref_iter_next_block(struct indexed_table_ref_iter *it)\n static int indexed_table_ref_iter_seek(void *p UNUSED,\n \t\t\t\t       struct reftable_record *want UNUSED)\n {\n-\tBUG(\"seeking indexed table is not supported\");\n-\treturn -1;\n+\treturn REFTABLE_API_ERROR;\n }\n \n static int indexed_table_ref_iter_next(void *p, struct reftable_record *rec)\ndiff --git a/reftable/reader.c b/reftable/reader.c\nindex de6e6dd9322..36a5633eded 100644\n--- a/reftable/reader.c\n+++ b/reftable/reader.c\n@@ -677,8 +677,6 @@ int reftable_reader_new(struct reftable_reader **out,\n \n void reftable_reader_incref(struct reftable_reader *r)\n {\n-\tif (!r->refcount)\n-\t\tBUG(\"cannot increment ref counter of dead reader\");\n \tr->refcount++;\n }\n \n@@ -686,8 +684,6 @@ void reftable_reader_decref(struct reftable_reader *r)\n {\n \tif (!r)\n \t\treturn;\n-\tif (!r->refcount)\n-\t\tBUG(\"cannot decrement ref counter of dead reader\");\n \tif (--r->refcount)\n \t\treturn;\n \tblock_source_close(&r->source);\ndiff --git a/reftable/writer.c b/reftable/writer.c\nindex f3ab1035d61..239573ade24 100644\n--- a/reftable/writer.c\n+++ b/reftable/writer.c\n@@ -158,7 +158,7 @@ int reftable_writer_new(struct reftable_writer **out,\n \t\topts = *_opts;\n \toptions_set_defaults(&opts);\n \tif (opts.block_size >= (1 << 24))\n-\t\tBUG(\"configured block size exceeds 16MB\");\n+\t\treturn REFTABLE_API_ERROR;\n \n \treftable_buf_init(&wp->block_writer_data.last_key);\n \treftable_buf_init(&wp->last_key);\n@@ -302,8 +302,7 @@ static int writer_add_record(struct reftable_writer *w,\n \t}\n \n \tif (block_writer_type(w->block_writer) != reftable_record_type(rec))\n-\t\tBUG(\"record of type %d added to writer of type %d\",\n-\t\t    reftable_record_type(rec), block_writer_type(w->block_writer));\n+\t\treturn REFTABLE_API_ERROR;\n \n \t/*\n \t * Try to add the record to the writer. If this succeeds then we're\n\n-- \n2.48.1.666.gff9fcf71b7.dirty\n\n"},{"id":"512574","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-10-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"[PATCH v6 10/18] reftable/reader: stop using `ARRAY_SIZE()` macro","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:46Z","receivedAt":"2025-02-18T09:22:03Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"We have a single user of the `ARRAY_SIZE()` macro in the reftable\nreader. Drop its use to reduce our dependence on the Git codebase.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/reader.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/reftable/reader.c b/reftable/reader.c\nindex bf07a0a586f..c3a36746652 100644\n--- a/reftable/reader.c\n+++ b/reftable/reader.c\n@@ -849,7 +849,7 @@ int reftable_reader_print_blocks(const char *tablename)\n \tprintf(\"header:\\n\");\n \tprintf(\"  block_size: %d\\n\", r->block_size);\n \n-\tfor (i = 0; i < ARRAY_SIZE(sections); i++) {\n+\tfor (i = 0; i < sizeof(sections) / sizeof(*sections); i++) {\n \t\terr = table_iter_seek_start(&ti, sections[i].type, 0);\n \t\tif (err < 0)\n \t\t\tgoto done;\n\n-- \n2.48.1.666.gff9fcf71b7.dirty\n\n"},{"id":"512575","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-9-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"[PATCH v6 09/18] reftable/basics: provide wrappers for big endian conversion","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:45Z","receivedAt":"2025-02-18T09:22:03Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"We're using a mixture of big endian conversion functions provided by\nboth the reftable library, but also by the Git codebase. Refactor the\ncode so that we exclusively use reftable-provided wrappers in order to\nuntangle us from the Git codebase.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/basics.c                | 19 ----------\n reftable/basics.h                | 76 ++++++++++++++++++++++++++++++++++++++--\n reftable/block.c                 | 12 +++----\n reftable/reader.c                | 22 ++++++------\n reftable/record.c                |  8 ++---\n reftable/writer.c                | 20 +++++------\n t/unit-tests/t-reftable-basics.c | 28 ++++++++++++---\n 7 files changed, 127 insertions(+), 58 deletions(-)\n\ndiff --git a/reftable/basics.c b/reftable/basics.c\nindex 3b5ea27bbdc..8c4a4433e41 100644\n--- a/reftable/basics.c\n+++ b/reftable/basics.c\n@@ -147,25 +147,6 @@ char *reftable_buf_detach(struct reftable_buf *buf)\n \treturn result;\n }\n \n-void put_be24(uint8_t *out, uint32_t i)\n-{\n-\tout[0] = (uint8_t)((i >> 16) & 0xff);\n-\tout[1] = (uint8_t)((i >> 8) & 0xff);\n-\tout[2] = (uint8_t)(i & 0xff);\n-}\n-\n-uint32_t get_be24(uint8_t *in)\n-{\n-\treturn (uint32_t)(in[0]) << 16 | (uint32_t)(in[1]) << 8 |\n-\t       (uint32_t)(in[2]);\n-}\n-\n-void put_be16(uint8_t *out, uint16_t i)\n-{\n-\tout[0] = (uint8_t)((i >> 8) & 0xff);\n-\tout[1] = (uint8_t)(i & 0xff);\n-}\n-\n size_t binsearch(size_t sz, int (*f)(size_t k, void *args), void *args)\n {\n \tsize_t lo = 0;\ndiff --git a/reftable/basics.h b/reftable/basics.h\nindex 646f8d67f2d..c1ddbaec3ff 100644\n--- a/reftable/basics.h\n+++ b/reftable/basics.h\n@@ -76,9 +76,79 @@ char *reftable_buf_detach(struct reftable_buf *buf);\n \n /* Bigendian en/decoding of integers */\n \n-void put_be24(uint8_t *out, uint32_t i);\n-uint32_t get_be24(uint8_t *in);\n-void put_be16(uint8_t *out, uint16_t i);\n+static inline void reftable_put_be16(void *out, uint16_t i)\n+{\n+\tunsigned char *p = out;\n+\tp[0] = (uint8_t)((i >> 8) & 0xff);\n+\tp[1] = (uint8_t)((i >> 0) & 0xff);\n+}\n+\n+static inline void reftable_put_be24(void *out, uint32_t i)\n+{\n+\tunsigned char *p = out;\n+\tp[0] = (uint8_t)((i >> 16) & 0xff);\n+\tp[1] = (uint8_t)((i >>  8) & 0xff);\n+\tp[2] = (uint8_t)((i >>  0) & 0xff);\n+}\n+\n+static inline void reftable_put_be32(void *out, uint32_t i)\n+{\n+\tunsigned char *p = out;\n+\tp[0] = (uint8_t)((i >> 24) & 0xff);\n+\tp[1] = (uint8_t)((i >> 16) & 0xff);\n+\tp[2] = (uint8_t)((i >>  8) & 0xff);\n+\tp[3] = (uint8_t)((i >>  0) & 0xff);\n+}\n+\n+static inline void reftable_put_be64(void *out, uint64_t i)\n+{\n+\tunsigned char *p = out;\n+\tp[0] = (uint8_t)((i >> 56) & 0xff);\n+\tp[1] = (uint8_t)((i >> 48) & 0xff);\n+\tp[2] = (uint8_t)((i >> 40) & 0xff);\n+\tp[3] = (uint8_t)((i >> 32) & 0xff);\n+\tp[4] = (uint8_t)((i >> 24) & 0xff);\n+\tp[5] = (uint8_t)((i >> 16) & 0xff);\n+\tp[6] = (uint8_t)((i >>  8) & 0xff);\n+\tp[7] = (uint8_t)((i >>  0) & 0xff);\n+}\n+\n+static inline uint16_t reftable_get_be16(const void *in)\n+{\n+\tconst unsigned char *p = in;\n+\treturn (uint16_t)(p[0]) << 8 |\n+\t       (uint16_t)(p[1]) << 0;\n+}\n+\n+static inline uint32_t reftable_get_be24(const void *in)\n+{\n+\tconst unsigned char *p = in;\n+\treturn (uint32_t)(p[0]) << 16 |\n+\t       (uint32_t)(p[1]) << 8 |\n+\t       (uint32_t)(p[2]) << 0;\n+}\n+\n+static inline uint32_t reftable_get_be32(const void *in)\n+{\n+\tconst unsigned char *p = in;\n+\treturn (uint32_t)(p[0]) << 24 |\n+\t       (uint32_t)(p[1]) << 16 |\n+\t       (uint32_t)(p[2]) <<  8|\n+\t       (uint32_t)(p[3]) <<  0;\n+}\n+\n+static inline uint64_t reftable_get_be64(const void *in)\n+{\n+\tconst unsigned char *p = in;\n+\treturn (uint64_t)(p[0]) << 56 |\n+\t       (uint64_t)(p[1]) << 48 |\n+\t       (uint64_t)(p[2]) << 40 |\n+\t       (uint64_t)(p[3]) << 32 |\n+\t       (uint64_t)(p[4]) << 24 |\n+\t       (uint64_t)(p[5]) << 16 |\n+\t       (uint64_t)(p[6]) <<  8 |\n+\t       (uint64_t)(p[7]) <<  0;\n+}\n \n /*\n  * find smallest index i in [0, sz) at which `f(i) > 0`, assuming that f is\ndiff --git a/reftable/block.c b/reftable/block.c\nindex 999876826d1..53b5e044690 100644\n--- a/reftable/block.c\n+++ b/reftable/block.c\n@@ -147,13 +147,13 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)\n int block_writer_finish(struct block_writer *w)\n {\n \tfor (uint32_t i = 0; i < w->restart_len; i++) {\n-\t\tput_be24(w->block + w->next, w->restarts[i]);\n+\t\treftable_put_be24(w->block + w->next, w->restarts[i]);\n \t\tw->next += 3;\n \t}\n \n-\tput_be16(w->block + w->next, w->restart_len);\n+\treftable_put_be16(w->block + w->next, w->restart_len);\n \tw->next += 2;\n-\tput_be24(w->block + 1 + w->header_off, w->next);\n+\treftable_put_be24(w->block + 1 + w->header_off, w->next);\n \n \t/*\n \t * Log records are stored zlib-compressed. Note that the compression\n@@ -215,7 +215,7 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block,\n {\n \tuint32_t full_block_size = table_block_size;\n \tuint8_t typ = block->data[header_off];\n-\tuint32_t sz = get_be24(block->data + header_off + 1);\n+\tuint32_t sz = reftable_get_be24(block->data + header_off + 1);\n \tint err = 0;\n \tuint16_t restart_count = 0;\n \tuint32_t restart_start = 0;\n@@ -299,7 +299,7 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block,\n \t\tfull_block_size = sz;\n \t}\n \n-\trestart_count = get_be16(block->data + sz - 2);\n+\trestart_count = reftable_get_be16(block->data + sz - 2);\n \trestart_start = sz - 2 - 3 * restart_count;\n \trestart_bytes = block->data + restart_start;\n \n@@ -354,7 +354,7 @@ int block_reader_first_key(const struct block_reader *br, struct reftable_buf *k\n \n static uint32_t block_reader_restart_offset(const struct block_reader *br, size_t idx)\n {\n-\treturn get_be24(br->restart_bytes + 3 * idx);\n+\treturn reftable_get_be24(br->restart_bytes + 3 * idx);\n }\n \n void block_iter_seek_start(struct block_iter *it, const struct block_reader *br)\ndiff --git a/reftable/reader.c b/reftable/reader.c\nindex 36a5633eded..bf07a0a586f 100644\n--- a/reftable/reader.c\n+++ b/reftable/reader.c\n@@ -101,18 +101,18 @@ static int parse_footer(struct reftable_reader *r, uint8_t *footer,\n \t}\n \n \tf++;\n-\tr->block_size = get_be24(f);\n+\tr->block_size = reftable_get_be24(f);\n \n \tf += 3;\n-\tr->min_update_index = get_be64(f);\n+\tr->min_update_index = reftable_get_be64(f);\n \tf += 8;\n-\tr->max_update_index = get_be64(f);\n+\tr->max_update_index = reftable_get_be64(f);\n \tf += 8;\n \n \tif (r->version == 1) {\n \t\tr->hash_id = REFTABLE_HASH_SHA1;\n \t} else {\n-\t\tswitch (get_be32(f)) {\n+\t\tswitch (reftable_get_be32(f)) {\n \t\tcase REFTABLE_FORMAT_ID_SHA1:\n \t\t\tr->hash_id = REFTABLE_HASH_SHA1;\n \t\t\tbreak;\n@@ -127,24 +127,24 @@ static int parse_footer(struct reftable_reader *r, uint8_t *footer,\n \t\tf += 4;\n \t}\n \n-\tr->ref_offsets.index_offset = get_be64(f);\n+\tr->ref_offsets.index_offset = reftable_get_be64(f);\n \tf += 8;\n \n-\tr->obj_offsets.offset = get_be64(f);\n+\tr->obj_offsets.offset = reftable_get_be64(f);\n \tf += 8;\n \n \tr->object_id_len = r->obj_offsets.offset & ((1 << 5) - 1);\n \tr->obj_offsets.offset >>= 5;\n \n-\tr->obj_offsets.index_offset = get_be64(f);\n+\tr->obj_offsets.index_offset = reftable_get_be64(f);\n \tf += 8;\n-\tr->log_offsets.offset = get_be64(f);\n+\tr->log_offsets.offset = reftable_get_be64(f);\n \tf += 8;\n-\tr->log_offsets.index_offset = get_be64(f);\n+\tr->log_offsets.index_offset = reftable_get_be64(f);\n \tf += 8;\n \n \tcomputed_crc = crc32(0, footer, f - footer);\n-\tfile_crc = get_be32(f);\n+\tfile_crc = reftable_get_be32(f);\n \tf += 4;\n \tif (computed_crc != file_crc) {\n \t\terr = REFTABLE_FORMAT_ERROR;\n@@ -214,7 +214,7 @@ static int32_t extract_block_size(uint8_t *data, uint8_t *typ, uint64_t off,\n \n \t*typ = data[0];\n \tif (reftable_is_block_type(*typ)) {\n-\t\tresult = get_be24(data + 1);\n+\t\tresult = reftable_get_be24(data + 1);\n \t}\n \treturn result;\n }\ndiff --git a/reftable/record.c b/reftable/record.c\nindex b39d99fcc75..3552bafa994 100644\n--- a/reftable/record.c\n+++ b/reftable/record.c\n@@ -689,7 +689,7 @@ static int reftable_log_record_key(const void *r, struct reftable_buf *dest)\n \t\treturn err;\n \n \tts = (~ts) - rec->update_index;\n-\tput_be64(&i64[0], ts);\n+\treftable_put_be64(&i64[0], ts);\n \n \terr = reftable_buf_add(dest, i64, sizeof(i64));\n \tif (err < 0)\n@@ -814,7 +814,7 @@ static int reftable_log_record_encode(const void *rec, struct string_view s,\n \tif (s.len < 2)\n \t\treturn -1;\n \n-\tput_be16(s.buf, r->value.update.tz_offset);\n+\treftable_put_be16(s.buf, r->value.update.tz_offset);\n \tstring_view_consume(&s, 2);\n \n \tn = encode_string(\n@@ -846,7 +846,7 @@ static int reftable_log_record_decode(void *rec, struct reftable_buf key,\n \t}\n \n \tmemcpy(r->refname, key.buf, key.len - 8);\n-\tts = get_be64(key.buf + key.len - 8);\n+\tts = reftable_get_be64((unsigned char *)key.buf + key.len - 8);\n \n \tr->update_index = (~max) - ts;\n \n@@ -937,7 +937,7 @@ static int reftable_log_record_decode(void *rec, struct reftable_buf key,\n \t\tgoto done;\n \t}\n \n-\tr->value.update.tz_offset = get_be16(in.buf);\n+\tr->value.update.tz_offset = reftable_get_be16(in.buf);\n \tstring_view_consume(&in, 2);\n \n \tn = decode_string(scratch, in);\ndiff --git a/reftable/writer.c b/reftable/writer.c\nindex 239573ade24..913b971b591 100644\n--- a/reftable/writer.c\n+++ b/reftable/writer.c\n@@ -99,9 +99,9 @@ static int writer_write_header(struct reftable_writer *w, uint8_t *dest)\n \n \tdest[4] = writer_version(w);\n \n-\tput_be24(dest + 5, w->opts.block_size);\n-\tput_be64(dest + 8, w->min_update_index);\n-\tput_be64(dest + 16, w->max_update_index);\n+\treftable_put_be24(dest + 5, w->opts.block_size);\n+\treftable_put_be64(dest + 8, w->min_update_index);\n+\treftable_put_be64(dest + 16, w->max_update_index);\n \tif (writer_version(w) == 2) {\n \t\tuint32_t hash_id;\n \n@@ -116,7 +116,7 @@ static int writer_write_header(struct reftable_writer *w, uint8_t *dest)\n \t\t\treturn -1;\n \t\t}\n \n-\t\tput_be32(dest + 24, hash_id);\n+\t\treftable_put_be32(dest + 24, hash_id);\n \t}\n \n \treturn header_size(writer_version(w));\n@@ -730,19 +730,19 @@ int reftable_writer_close(struct reftable_writer *w)\n \t}\n \n \tp += writer_write_header(w, footer);\n-\tput_be64(p, w->stats.ref_stats.index_offset);\n+\treftable_put_be64(p, w->stats.ref_stats.index_offset);\n \tp += 8;\n-\tput_be64(p, (w->stats.obj_stats.offset) << 5 | w->stats.object_id_len);\n+\treftable_put_be64(p, (w->stats.obj_stats.offset) << 5 | w->stats.object_id_len);\n \tp += 8;\n-\tput_be64(p, w->stats.obj_stats.index_offset);\n+\treftable_put_be64(p, w->stats.obj_stats.index_offset);\n \tp += 8;\n \n-\tput_be64(p, w->stats.log_stats.offset);\n+\treftable_put_be64(p, w->stats.log_stats.offset);\n \tp += 8;\n-\tput_be64(p, w->stats.log_stats.index_offset);\n+\treftable_put_be64(p, w->stats.log_stats.index_offset);\n \tp += 8;\n \n-\tput_be32(p, crc32(0, footer, p - footer));\n+\treftable_put_be32(p, crc32(0, footer, p - footer));\n \tp += 4;\n \n \terr = w->flush(w->write_arg);\ndiff --git a/t/unit-tests/t-reftable-basics.c b/t/unit-tests/t-reftable-basics.c\nindex 9ba7eb05ada..c9e751e49e9 100644\n--- a/t/unit-tests/t-reftable-basics.c\n+++ b/t/unit-tests/t-reftable-basics.c\n@@ -128,12 +128,30 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)\n \t\treftable_buf_release(&b);\n \t}\n \n-\tif_test (\"put_be24 and get_be24 work\") {\n+\tif_test (\"reftable_put_be64 and reftable_get_be64 work\") {\n+\t\tuint64_t in = 0x1122334455667788;\n+\t\tuint8_t dest[8];\n+\t\tuint64_t out;\n+\t\treftable_put_be64(dest, in);\n+\t\tout = reftable_get_be64(dest);\n+\t\tcheck_int(in, ==, out);\n+\t}\n+\n+\tif_test (\"reftable_put_be32 and reftable_get_be32 work\") {\n+\t\tuint32_t in = 0x11223344;\n+\t\tuint8_t dest[4];\n+\t\tuint32_t out;\n+\t\treftable_put_be32(dest, in);\n+\t\tout = reftable_get_be32(dest);\n+\t\tcheck_int(in, ==, out);\n+\t}\n+\n+\tif_test (\"reftable_put_be24 and reftable_get_be24 work\") {\n \t\tuint32_t in = 0x112233;\n \t\tuint8_t dest[3];\n \t\tuint32_t out;\n-\t\tput_be24(dest, in);\n-\t\tout = get_be24(dest);\n+\t\treftable_put_be24(dest, in);\n+\t\tout = reftable_get_be24(dest);\n \t\tcheck_int(in, ==, out);\n \t}\n \n@@ -141,8 +159,8 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)\n \t\tuint32_t in = 0xfef1;\n \t\tuint8_t dest[3];\n \t\tuint32_t out;\n-\t\tput_be16(dest, in);\n-\t\tout = get_be16(dest);\n+\t\treftable_put_be16(dest, in);\n+\t\tout = reftable_get_be16(dest);\n \t\tcheck_int(in, ==, out);\n \t}\n \n\n-- \n2.48.1.666.gff9fcf71b7.dirty\n\n"},{"id":"512577","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-11-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"[PATCH v6 11/18] reftable/system: introduce `reftable_rand()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:47Z","receivedAt":"2025-02-18T09:22:04Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Introduce a new system-level `reftable_rand()` function that generates a\nsingle unsigned integer for us. The implementation of this function is\nto be provided by the calling codebase, which allows us to more easily\nhook into pre-seeded random number generators.\n\nAdapt the two callsites where we generated random data.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/stack.c  | 4 ++--\n reftable/system.c | 5 +++++\n reftable/system.h | 6 ++++++\n 3 files changed, 13 insertions(+), 2 deletions(-)\n\ndiff --git a/reftable/stack.c b/reftable/stack.c\nindex c33f0c3333a..08893fd454f 100644\n--- a/reftable/stack.c\n+++ b/reftable/stack.c\n@@ -523,7 +523,7 @@ static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,\n \t\tclose(fd);\n \t\tfd = -1;\n \n-\t\tdelay = delay + (delay * git_rand(CSPRNG_BYTES_INSECURE)) / UINT32_MAX + 1;\n+\t\tdelay = delay + (delay * reftable_rand()) / UINT32_MAX + 1;\n \t\tsleep_millisec(delay);\n \t}\n \n@@ -688,7 +688,7 @@ int reftable_stack_add(struct reftable_stack *st,\n static int format_name(struct reftable_buf *dest, uint64_t min, uint64_t max)\n {\n \tchar buf[100];\n-\tuint32_t rnd = git_rand(CSPRNG_BYTES_INSECURE);\n+\tuint32_t rnd = reftable_rand();\n \tsnprintf(buf, sizeof(buf), \"0x%012\" PRIx64 \"-0x%012\" PRIx64 \"-%08x\",\n \t\t min, max, rnd);\n \treftable_buf_reset(dest);\ndiff --git a/reftable/system.c b/reftable/system.c\nindex adf8e4d30b8..e25ccc0da3c 100644\n--- a/reftable/system.c\n+++ b/reftable/system.c\n@@ -4,6 +4,11 @@\n #include \"../lockfile.h\"\n #include \"../tempfile.h\"\n \n+uint32_t reftable_rand(void)\n+{\n+\treturn git_rand(CSPRNG_BYTES_INSECURE);\n+}\n+\n int tmpfile_from_pattern(struct reftable_tmpfile *out, const char *pattern)\n {\n \tstruct tempfile *tempfile;\ndiff --git a/reftable/system.h b/reftable/system.h\nindex d02eacea8f0..bb6a7e6285b 100644\n--- a/reftable/system.h\n+++ b/reftable/system.h\n@@ -14,6 +14,12 @@ license that can be found in the LICENSE file or at\n #include \"git-compat-util.h\"\n #include \"compat/zlib-compat.h\"\n \n+/*\n+ * Return a random 32 bit integer. This function is expected to return\n+ * pre-seeded data.\n+ */\n+uint32_t reftable_rand(void);\n+\n /*\n  * An implementation-specific temporary file. By making this specific to the\n  * implementation it becomes possible to tie temporary files into any kind of\n\n-- \n2.48.1.666.gff9fcf71b7.dirty\n\n"},{"id":"512576","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-12-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"[PATCH v6 12/18] reftable/stack: stop using `sleep_millisec()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:48Z","receivedAt":"2025-02-18T09:22:05Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Refactor our use of `sleep_millisec()` by open-coding it with poll(3p),\nwhich is the current implementation of this function. Ideally, we'd use\na more direct way to sleep, but there is no equivalent to sleep(3p) that\nwould accept milliseconds as input.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/stack.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/reftable/stack.c b/reftable/stack.c\nindex 08893fd454f..6dac015b473 100644\n--- a/reftable/stack.c\n+++ b/reftable/stack.c\n@@ -524,7 +524,7 @@ static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,\n \t\tfd = -1;\n \n \t\tdelay = delay + (delay * reftable_rand()) / UINT32_MAX + 1;\n-\t\tsleep_millisec(delay);\n+\t\tpoll(NULL, 0, delay);\n \t}\n \n out:\n\n-- \n2.48.1.666.gff9fcf71b7.dirty\n\n"},{"id":"512578","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-13-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"[PATCH v6 13/18] reftable/basics: stop using `SWAP()` macro","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:49Z","receivedAt":"2025-02-18T09:22:07Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Stop using `SWAP()` macro in favor of an open-coded variant of it. Note\nthat this also requires us to open-code the build assert that `SWAP()`\nitself uses to verify that the size of both variables matches.\n\nThis is done to reduce our dependency on the Git codebase.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/basics.h |  9 +++++++++\n reftable/merged.c |  2 +-\n reftable/pq.c     |  4 ++--\n reftable/record.c | 16 ++++++++--------\n 4 files changed, 20 insertions(+), 11 deletions(-)\n\ndiff --git a/reftable/basics.h b/reftable/basics.h\nindex c1ddbaec3ff..59000798f0b 100644\n--- a/reftable/basics.h\n+++ b/reftable/basics.h\n@@ -266,6 +266,15 @@ static inline void *reftable_alloc_grow(void *p, size_t nelem, size_t elsize,\n # define strdup(str) REFTABLE_BANNED(strdup)\n #endif\n \n+#define REFTABLE_SWAP(a, b) do {\t\t\t\t\t\t\t\t\\\n+\tvoid *_swap_a_ptr = &(a);\t\t\t\t\t\t\t\t\\\n+\tvoid *_swap_b_ptr = &(b);\t\t\t\t\t\t\t\t\\\n+\tunsigned char _swap_buffer[sizeof(a) - 2 * sizeof(a) * (sizeof(a) != sizeof(b))];\t\\\n+\tmemcpy(_swap_buffer, _swap_a_ptr, sizeof(a));\t\t\t\t\t\t\\\n+\tmemcpy(_swap_a_ptr, _swap_b_ptr, sizeof(a));\t\t\t\t\t\t\\\n+\tmemcpy(_swap_b_ptr, _swap_buffer, sizeof(a));\t\t\t\t\t\t\\\n+} while (0)\n+\n /* Find the longest shared prefix size of `a` and `b` */\n size_t common_prefix_size(struct reftable_buf *a, struct reftable_buf *b);\n \ndiff --git a/reftable/merged.c b/reftable/merged.c\nindex 563864068c1..4ff1553772a 100644\n--- a/reftable/merged.c\n+++ b/reftable/merged.c\n@@ -155,7 +155,7 @@ static int merged_iter_next_entry(struct merged_iter *mi,\n \t}\n \n \tmi->advance_index = entry.index;\n-\tSWAP(*rec, *entry.rec);\n+\tREFTABLE_SWAP(*rec, *entry.rec);\n \treturn 0;\n }\n \ndiff --git a/reftable/pq.c b/reftable/pq.c\nindex ef8035cfd9b..82394a972db 100644\n--- a/reftable/pq.c\n+++ b/reftable/pq.c\n@@ -57,7 +57,7 @@ int merged_iter_pqueue_remove(struct merged_iter_pqueue *pq, struct pq_entry *ou\n \n \t\tif (min == i)\n \t\t\tbreak;\n-\t\tSWAP(pq->heap[i], pq->heap[min]);\n+\t\tREFTABLE_SWAP(pq->heap[i], pq->heap[min]);\n \t\ti = min;\n \t}\n \n@@ -81,7 +81,7 @@ int merged_iter_pqueue_add(struct merged_iter_pqueue *pq, const struct pq_entry\n \t\tsize_t j = (i - 1) / 2;\n \t\tif (pq_less(&pq->heap[j], &pq->heap[i]))\n \t\t\tbreak;\n-\t\tSWAP(pq->heap[j], pq->heap[i]);\n+\t\tREFTABLE_SWAP(pq->heap[j], pq->heap[i]);\n \t\ti = j;\n \t}\n \ndiff --git a/reftable/record.c b/reftable/record.c\nindex 3552bafa994..9a1edf39a02 100644\n--- a/reftable/record.c\n+++ b/reftable/record.c\n@@ -237,11 +237,11 @@ static int reftable_ref_record_copy_from(void *rec, const void *src_rec,\n \tsize_t refname_cap = 0;\n \tint err;\n \n-\tSWAP(refname, ref->refname);\n-\tSWAP(refname_cap, ref->refname_cap);\n+\tREFTABLE_SWAP(refname, ref->refname);\n+\tREFTABLE_SWAP(refname_cap, ref->refname_cap);\n \treftable_ref_record_release(ref);\n-\tSWAP(ref->refname, refname);\n-\tSWAP(ref->refname_cap, refname_cap);\n+\tREFTABLE_SWAP(ref->refname, refname);\n+\tREFTABLE_SWAP(ref->refname_cap, refname_cap);\n \n \tif (src->refname) {\n \t\tsize_t refname_len = strlen(src->refname);\n@@ -376,11 +376,11 @@ static int reftable_ref_record_decode(void *rec, struct reftable_buf key,\n \t\treturn n;\n \tstring_view_consume(&in, n);\n \n-\tSWAP(refname, r->refname);\n-\tSWAP(refname_cap, r->refname_cap);\n+\tREFTABLE_SWAP(refname, r->refname);\n+\tREFTABLE_SWAP(refname_cap, r->refname_cap);\n \treftable_ref_record_release(r);\n-\tSWAP(r->refname, refname);\n-\tSWAP(r->refname_cap, refname_cap);\n+\tREFTABLE_SWAP(r->refname, refname);\n+\tREFTABLE_SWAP(r->refname_cap, refname_cap);\n \n \tREFTABLE_ALLOC_GROW_OR_NULL(r->refname, key.len + 1, r->refname_cap);\n \tif (!r->refname) {\n\n-- \n2.48.1.666.gff9fcf71b7.dirty\n\n"},{"id":"512579","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-14-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"[PATCH v6 14/18] reftable/basics: introduce `REFTABLE_UNUSED` annotation","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:50Z","receivedAt":"2025-02-18T09:22:08Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Introduce the `REFTABLE_UNUSED` annotation and replace all existing\nusers of `UNUSED` in the reftable library to use the new macro instead.\n\nNote that we unconditionally define `MAYBE_UNUSED` in the exact same\nway, so doing so unconditionally for `REFTABLE_UNUSED` should be fine,\ntoo.\n\nSuggested-by: Toon Claes <toon@iotcl.com>\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/basics.h      |  2 ++\n reftable/blocksource.c |  6 +++---\n reftable/iter.c        | 10 +++++-----\n reftable/record.c      | 26 +++++++++++++-------------\n reftable/writer.c      |  2 +-\n 5 files changed, 24 insertions(+), 22 deletions(-)\n\ndiff --git a/reftable/basics.h b/reftable/basics.h\nindex 59000798f0b..fd59cbb7720 100644\n--- a/reftable/basics.h\n+++ b/reftable/basics.h\n@@ -16,6 +16,8 @@ license that can be found in the LICENSE file or at\n #include \"system.h\"\n #include \"reftable-basics.h\"\n \n+#define REFTABLE_UNUSED __attribute__((__unused__))\n+\n struct reftable_buf {\n \tsize_t alloc;\n \tsize_t len;\ndiff --git a/reftable/blocksource.c b/reftable/blocksource.c\nindex 02972c46f4b..78c1be23373 100644\n--- a/reftable/blocksource.c\n+++ b/reftable/blocksource.c\n@@ -13,14 +13,14 @@ license that can be found in the LICENSE file or at\n #include \"reftable-blocksource.h\"\n #include \"reftable-error.h\"\n \n-static void reftable_buf_return_block(void *b UNUSED, struct reftable_block *dest)\n+static void reftable_buf_return_block(void *b REFTABLE_UNUSED, struct reftable_block *dest)\n {\n \tif (dest->len)\n \t\tmemset(dest->data, 0xff, dest->len);\n \treftable_free(dest->data);\n }\n \n-static void reftable_buf_close(void *b UNUSED)\n+static void reftable_buf_close(void *b REFTABLE_UNUSED)\n {\n }\n \n@@ -67,7 +67,7 @@ static uint64_t file_size(void *b)\n \treturn ((struct file_block_source *)b)->size;\n }\n \n-static void file_return_block(void *b UNUSED, struct reftable_block *dest UNUSED)\n+static void file_return_block(void *b REFTABLE_UNUSED, struct reftable_block *dest REFTABLE_UNUSED)\n {\n }\n \ndiff --git a/reftable/iter.c b/reftable/iter.c\nindex b2ffb09c16b..f520382e700 100644\n--- a/reftable/iter.c\n+++ b/reftable/iter.c\n@@ -25,17 +25,17 @@ int iterator_next(struct reftable_iterator *it, struct reftable_record *rec)\n \treturn it->ops->next(it->iter_arg, rec);\n }\n \n-static int empty_iterator_seek(void *arg UNUSED, struct reftable_record *want UNUSED)\n+static int empty_iterator_seek(void *arg REFTABLE_UNUSED, struct reftable_record *want REFTABLE_UNUSED)\n {\n \treturn 0;\n }\n \n-static int empty_iterator_next(void *arg UNUSED, struct reftable_record *rec UNUSED)\n+static int empty_iterator_next(void *arg REFTABLE_UNUSED, struct reftable_record *rec REFTABLE_UNUSED)\n {\n \treturn 1;\n }\n \n-static void empty_iterator_close(void *arg UNUSED)\n+static void empty_iterator_close(void *arg REFTABLE_UNUSED)\n {\n }\n \n@@ -143,8 +143,8 @@ static int indexed_table_ref_iter_next_block(struct indexed_table_ref_iter *it)\n \treturn 0;\n }\n \n-static int indexed_table_ref_iter_seek(void *p UNUSED,\n-\t\t\t\t       struct reftable_record *want UNUSED)\n+static int indexed_table_ref_iter_seek(void *p REFTABLE_UNUSED,\n+\t\t\t\t       struct reftable_record *want REFTABLE_UNUSED)\n {\n \treturn REFTABLE_API_ERROR;\n }\ndiff --git a/reftable/record.c b/reftable/record.c\nindex 9a1edf39a02..142853d5070 100644\n--- a/reftable/record.c\n+++ b/reftable/record.c\n@@ -490,7 +490,7 @@ static void reftable_obj_record_release(void *rec)\n }\n \n static int reftable_obj_record_copy_from(void *rec, const void *src_rec,\n-\t\t\t\t\t uint32_t hash_size UNUSED)\n+\t\t\t\t\t uint32_t hash_size REFTABLE_UNUSED)\n {\n \tstruct reftable_obj_record *obj = rec;\n \tconst struct reftable_obj_record *src = src_rec;\n@@ -528,7 +528,7 @@ static uint8_t reftable_obj_record_val_type(const void *rec)\n }\n \n static int reftable_obj_record_encode(const void *rec, struct string_view s,\n-\t\t\t\t      uint32_t hash_size UNUSED)\n+\t\t\t\t      uint32_t hash_size REFTABLE_UNUSED)\n {\n \tconst struct reftable_obj_record *r = rec;\n \tstruct string_view start = s;\n@@ -563,8 +563,8 @@ static int reftable_obj_record_encode(const void *rec, struct string_view s,\n \n static int reftable_obj_record_decode(void *rec, struct reftable_buf key,\n \t\t\t\t      uint8_t val_type, struct string_view in,\n-\t\t\t\t      uint32_t hash_size UNUSED,\n-\t\t\t\t      struct reftable_buf *scratch UNUSED)\n+\t\t\t\t      uint32_t hash_size REFTABLE_UNUSED,\n+\t\t\t\t      struct reftable_buf *scratch REFTABLE_UNUSED)\n {\n \tstruct string_view start = in;\n \tstruct reftable_obj_record *r = rec;\n@@ -618,13 +618,13 @@ static int reftable_obj_record_decode(void *rec, struct reftable_buf key,\n \treturn start.len - in.len;\n }\n \n-static int not_a_deletion(const void *p UNUSED)\n+static int not_a_deletion(const void *p REFTABLE_UNUSED)\n {\n \treturn 0;\n }\n \n static int reftable_obj_record_equal_void(const void *a, const void *b,\n-\t\t\t\t\t  uint32_t hash_size UNUSED)\n+\t\t\t\t\t  uint32_t hash_size REFTABLE_UNUSED)\n {\n \tstruct reftable_obj_record *ra = (struct reftable_obj_record *) a;\n \tstruct reftable_obj_record *rb = (struct reftable_obj_record *) b;\n@@ -1054,7 +1054,7 @@ static int reftable_index_record_key(const void *r, struct reftable_buf *dest)\n }\n \n static int reftable_index_record_copy_from(void *rec, const void *src_rec,\n-\t\t\t\t\t   uint32_t hash_size UNUSED)\n+\t\t\t\t\t   uint32_t hash_size REFTABLE_UNUSED)\n {\n \tstruct reftable_index_record *dst = rec;\n \tconst struct reftable_index_record *src = src_rec;\n@@ -1075,13 +1075,13 @@ static void reftable_index_record_release(void *rec)\n \treftable_buf_release(&idx->last_key);\n }\n \n-static uint8_t reftable_index_record_val_type(const void *rec UNUSED)\n+static uint8_t reftable_index_record_val_type(const void *rec REFTABLE_UNUSED)\n {\n \treturn 0;\n }\n \n static int reftable_index_record_encode(const void *rec, struct string_view out,\n-\t\t\t\t\tuint32_t hash_size UNUSED)\n+\t\t\t\t\tuint32_t hash_size REFTABLE_UNUSED)\n {\n \tconst struct reftable_index_record *r =\n \t\t(const struct reftable_index_record *)rec;\n@@ -1097,10 +1097,10 @@ static int reftable_index_record_encode(const void *rec, struct string_view out,\n }\n \n static int reftable_index_record_decode(void *rec, struct reftable_buf key,\n-\t\t\t\t\tuint8_t val_type UNUSED,\n+\t\t\t\t\tuint8_t val_type REFTABLE_UNUSED,\n \t\t\t\t\tstruct string_view in,\n-\t\t\t\t\tuint32_t hash_size UNUSED,\n-\t\t\t\t\tstruct reftable_buf *scratch UNUSED)\n+\t\t\t\t\tuint32_t hash_size REFTABLE_UNUSED,\n+\t\t\t\t\tstruct reftable_buf *scratch REFTABLE_UNUSED)\n {\n \tstruct string_view start = in;\n \tstruct reftable_index_record *r = rec;\n@@ -1120,7 +1120,7 @@ static int reftable_index_record_decode(void *rec, struct reftable_buf key,\n }\n \n static int reftable_index_record_equal(const void *a, const void *b,\n-\t\t\t\t       uint32_t hash_size UNUSED)\n+\t\t\t\t       uint32_t hash_size REFTABLE_UNUSED)\n {\n \tstruct reftable_index_record *ia = (struct reftable_index_record *) a;\n \tstruct reftable_index_record *ib = (struct reftable_index_record *) b;\ndiff --git a/reftable/writer.c b/reftable/writer.c\nindex 913b971b591..ce55a1deb06 100644\n--- a/reftable/writer.c\n+++ b/reftable/writer.c\n@@ -649,7 +649,7 @@ static void write_object_record(void *void_arg, void *key)\n done:;\n }\n \n-static void object_record_free(void *void_arg UNUSED, void *key)\n+static void object_record_free(void *void_arg REFTABLE_UNUSED, void *key)\n {\n \tstruct obj_index_tree_node *entry = key;\n \n\n-- \n2.48.1.666.gff9fcf71b7.dirty\n\n"},{"id":"512580","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-15-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"[PATCH v6 15/18] compat/mingw: split out POSIX-related bits","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:51Z","receivedAt":"2025-02-18T09:22:10Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Split out POSIX-related bits from \"compat/mingw.h\" and \"compat/msvc.h\".\nThis is in preparation for splitting up \"git-compat-utils.h\" into a\nheader that provides POSIX-compatibility and a header that provides\ncommon wrappers used by the Git project.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n compat/mingw-posix.h | 431 +++++++++++++++++++++++++++++++++++++++++++++++++++\n compat/mingw.h       | 426 +-------------------------------------------------\n compat/msvc-posix.h  |  33 ++++\n compat/msvc.h        |  30 +---\n 4 files changed, 467 insertions(+), 453 deletions(-)\n\ndiff --git a/compat/mingw-posix.h b/compat/mingw-posix.h\nnew file mode 100644\nindex 00000000000..8dddfa818d6\n--- /dev/null\n+++ b/compat/mingw-posix.h\n@@ -0,0 +1,431 @@\n+#ifndef COMPAT_MINGW_POSIX_H\n+#define COMPAT_MINGW_POSIX_H\n+\n+#ifdef __MINGW64_VERSION_MAJOR\n+#include <stdint.h>\n+#include <wchar.h>\n+typedef _sigset_t sigset_t;\n+#endif\n+#include <winsock2.h>\n+#include <ws2tcpip.h>\n+\n+/* MinGW-w64 reports to have flockfile, but it does not actually have it. */\n+#ifdef __MINGW64_VERSION_MAJOR\n+#undef _POSIX_THREAD_SAFE_FUNCTIONS\n+#endif\n+\n+/*\n+ * things that are not available in header files\n+ */\n+\n+typedef int uid_t;\n+typedef int socklen_t;\n+#ifndef __MINGW64_VERSION_MAJOR\n+typedef int pid_t;\n+#define hstrerror strerror\n+#endif\n+\n+#define S_IFLNK    0120000 /* Symbolic link */\n+#define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK)\n+#define S_ISSOCK(x) 0\n+\n+#ifndef S_IRWXG\n+#define S_IRGRP 0\n+#define S_IWGRP 0\n+#define S_IXGRP 0\n+#define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)\n+#endif\n+#ifndef S_IRWXO\n+#define S_IROTH 0\n+#define S_IWOTH 0\n+#define S_IXOTH 0\n+#define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)\n+#endif\n+\n+#define S_ISUID 0004000\n+#define S_ISGID 0002000\n+#define S_ISVTX 0001000\n+\n+#define WIFEXITED(x) 1\n+#define WIFSIGNALED(x) 0\n+#define WEXITSTATUS(x) ((x) & 0xff)\n+#define WTERMSIG(x) SIGTERM\n+\n+#ifndef EWOULDBLOCK\n+#define EWOULDBLOCK EAGAIN\n+#endif\n+#ifndef ELOOP\n+#define ELOOP EMLINK\n+#endif\n+#define SHUT_WR SD_SEND\n+\n+#define SIGHUP 1\n+#define SIGQUIT 3\n+#define SIGKILL 9\n+#define SIGPIPE 13\n+#define SIGALRM 14\n+#define SIGCHLD 17\n+\n+#define F_GETFD 1\n+#define F_SETFD 2\n+#define FD_CLOEXEC 0x1\n+\n+#if !defined O_CLOEXEC && defined O_NOINHERIT\n+#define O_CLOEXEC\tO_NOINHERIT\n+#endif\n+\n+#ifndef EAFNOSUPPORT\n+#define EAFNOSUPPORT WSAEAFNOSUPPORT\n+#endif\n+#ifndef ECONNABORTED\n+#define ECONNABORTED WSAECONNABORTED\n+#endif\n+#ifndef ENOTSOCK\n+#define ENOTSOCK WSAENOTSOCK\n+#endif\n+\n+struct passwd {\n+\tchar *pw_name;\n+\tchar *pw_gecos;\n+\tchar *pw_dir;\n+};\n+\n+typedef void (__cdecl *sig_handler_t)(int);\n+struct sigaction {\n+\tsig_handler_t sa_handler;\n+\tunsigned sa_flags;\n+};\n+#define SA_RESTART 0\n+\n+struct itimerval {\n+\tstruct timeval it_value, it_interval;\n+};\n+#define ITIMER_REAL 0\n+\n+struct utsname {\n+\tchar sysname[16];\n+\tchar nodename[1];\n+\tchar release[16];\n+\tchar version[16];\n+\tchar machine[1];\n+};\n+\n+/*\n+ * sanitize preprocessor namespace polluted by Windows headers defining\n+ * macros which collide with git local versions\n+ */\n+#undef HELP_COMMAND /* from winuser.h */\n+\n+/*\n+ * trivial stubs\n+ */\n+\n+static inline int readlink(const char *path UNUSED, char *buf UNUSED, size_t bufsiz UNUSED)\n+{ errno = ENOSYS; return -1; }\n+static inline int symlink(const char *oldpath UNUSED, const char *newpath UNUSED)\n+{ errno = ENOSYS; return -1; }\n+static inline int fchmod(int fildes UNUSED, mode_t mode UNUSED)\n+{ errno = ENOSYS; return -1; }\n+#ifndef __MINGW64_VERSION_MAJOR\n+static inline pid_t fork(void)\n+{ errno = ENOSYS; return -1; }\n+#endif\n+static inline unsigned int alarm(unsigned int seconds UNUSED)\n+{ return 0; }\n+static inline int fsync(int fd)\n+{ return _commit(fd); }\n+static inline void sync(void)\n+{}\n+static inline uid_t getuid(void)\n+{ return 1; }\n+static inline struct passwd *getpwnam(const char *name UNUSED)\n+{ return NULL; }\n+static inline int fcntl(int fd UNUSED, int cmd, ...)\n+{\n+\tif (cmd == F_GETFD || cmd == F_SETFD)\n+\t\treturn 0;\n+\terrno = EINVAL;\n+\treturn -1;\n+}\n+\n+#define sigemptyset(x) (void)0\n+static inline int sigaddset(sigset_t *set UNUSED, int signum UNUSED)\n+{ return 0; }\n+#define SIG_BLOCK 0\n+#define SIG_UNBLOCK 0\n+static inline int sigprocmask(int how UNUSED, const sigset_t *set UNUSED, sigset_t *oldset UNUSED)\n+{ return 0; }\n+static inline pid_t getppid(void)\n+{ return 1; }\n+static inline pid_t getpgid(pid_t pid)\n+{ return pid == 0 ? getpid() : pid; }\n+static inline pid_t tcgetpgrp(int fd UNUSED)\n+{ return getpid(); }\n+\n+/*\n+ * simple adaptors\n+ */\n+\n+int mingw_mkdir(const char *path, int mode);\n+#define mkdir mingw_mkdir\n+\n+#define WNOHANG 1\n+pid_t waitpid(pid_t pid, int *status, int options);\n+\n+#define kill mingw_kill\n+int mingw_kill(pid_t pid, int sig);\n+\n+#define locate_in_PATH mingw_locate_in_PATH\n+char *mingw_locate_in_PATH(const char *cmd);\n+\n+/*\n+ * implementations of missing functions\n+ */\n+\n+int pipe(int filedes[2]);\n+unsigned int sleep (unsigned int seconds);\n+int mkstemp(char *template);\n+int gettimeofday(struct timeval *tv, void *tz);\n+#ifndef __MINGW64_VERSION_MAJOR\n+struct tm *gmtime_r(const time_t *timep, struct tm *result);\n+struct tm *localtime_r(const time_t *timep, struct tm *result);\n+#endif\n+int getpagesize(void);\t/* defined in MinGW's libgcc.a */\n+struct passwd *getpwuid(uid_t uid);\n+int setitimer(int type, struct itimerval *in, struct itimerval *out);\n+int sigaction(int sig, struct sigaction *in, struct sigaction *out);\n+int link(const char *oldpath, const char *newpath);\n+int uname(struct utsname *buf);\n+\n+/*\n+ * replacements of existing functions\n+ */\n+\n+int mingw_unlink(const char *pathname);\n+#define unlink mingw_unlink\n+\n+int mingw_rmdir(const char *path);\n+#define rmdir mingw_rmdir\n+\n+int mingw_open (const char *filename, int oflags, ...);\n+#define open mingw_open\n+#undef OPEN_RETURNS_EINTR\n+\n+int mingw_fgetc(FILE *stream);\n+#define fgetc mingw_fgetc\n+\n+FILE *mingw_fopen (const char *filename, const char *otype);\n+#define fopen mingw_fopen\n+\n+FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream);\n+#define freopen mingw_freopen\n+\n+int mingw_fflush(FILE *stream);\n+#define fflush mingw_fflush\n+\n+ssize_t mingw_write(int fd, const void *buf, size_t len);\n+#define write mingw_write\n+\n+int mingw_access(const char *filename, int mode);\n+#undef access\n+#define access mingw_access\n+\n+int mingw_chdir(const char *dirname);\n+#define chdir mingw_chdir\n+\n+int mingw_chmod(const char *filename, int mode);\n+#define chmod mingw_chmod\n+\n+char *mingw_mktemp(char *template);\n+#define mktemp mingw_mktemp\n+\n+char *mingw_getcwd(char *pointer, int len);\n+#define getcwd mingw_getcwd\n+\n+#ifdef NO_UNSETENV\n+#error \"NO_UNSETENV is incompatible with the Windows-specific startup code!\"\n+#endif\n+\n+/*\n+ * We bind *env() routines (even the mingw_ ones) to private mingw_ versions.\n+ * These talk to the CRT using UNICODE/wchar_t, but maintain the original\n+ * narrow-char API.\n+ *\n+ * Note that the MSCRT maintains both ANSI (getenv()) and UNICODE (_wgetenv())\n+ * routines and stores both versions of each environment variable in parallel\n+ * (and secretly updates both when you set one or the other), but it uses CP_ACP\n+ * to do the conversion rather than CP_UTF8.\n+ *\n+ * Since everything in the git code base is UTF8, we define the mingw_ routines\n+ * to access the CRT using the UNICODE routines and manually convert them to\n+ * UTF8.  This also avoids round-trip problems.\n+ *\n+ * This also helps with our linkage, since \"_wenviron\" is publicly exported\n+ * from the CRT.  But to access \"_environ\" we would have to statically link\n+ * to the CRT (/MT).\n+ *\n+ * We require NO_SETENV (and let gitsetenv() call our mingw_putenv).\n+ */\n+#define getenv       mingw_getenv\n+#define putenv       mingw_putenv\n+#define unsetenv     mingw_putenv\n+char *mingw_getenv(const char *name);\n+int   mingw_putenv(const char *name);\n+\n+int mingw_gethostname(char *host, int namelen);\n+#define gethostname mingw_gethostname\n+\n+struct hostent *mingw_gethostbyname(const char *host);\n+#define gethostbyname mingw_gethostbyname\n+\n+int mingw_getaddrinfo(const char *node, const char *service,\n+\t\t      const struct addrinfo *hints, struct addrinfo **res);\n+#define getaddrinfo mingw_getaddrinfo\n+\n+int mingw_socket(int domain, int type, int protocol);\n+#define socket mingw_socket\n+\n+int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz);\n+#define connect mingw_connect\n+\n+int mingw_bind(int sockfd, struct sockaddr *sa, size_t sz);\n+#define bind mingw_bind\n+\n+int mingw_setsockopt(int sockfd, int lvl, int optname, void *optval, int optlen);\n+#define setsockopt mingw_setsockopt\n+\n+int mingw_shutdown(int sockfd, int how);\n+#define shutdown mingw_shutdown\n+\n+int mingw_listen(int sockfd, int backlog);\n+#define listen mingw_listen\n+\n+int mingw_accept(int sockfd, struct sockaddr *sa, socklen_t *sz);\n+#define accept mingw_accept\n+\n+int mingw_rename(const char*, const char*);\n+#define rename mingw_rename\n+\n+#if defined(USE_WIN32_MMAP) || defined(_MSC_VER)\n+int mingw_getpagesize(void);\n+#define getpagesize mingw_getpagesize\n+#endif\n+\n+int win32_fsync_no_flush(int fd);\n+#define fsync_no_flush win32_fsync_no_flush\n+\n+#define FSYNC_COMPONENTS_PLATFORM_DEFAULT (FSYNC_COMPONENTS_DEFAULT | FSYNC_COMPONENT_LOOSE_OBJECT)\n+#define FSYNC_METHOD_DEFAULT (FSYNC_METHOD_BATCH)\n+\n+struct rlimit {\n+\tunsigned int rlim_cur;\n+};\n+#define RLIMIT_NOFILE 0\n+\n+static inline int getrlimit(int resource, struct rlimit *rlp)\n+{\n+\tif (resource != RLIMIT_NOFILE) {\n+\t\terrno = EINVAL;\n+\t\treturn -1;\n+\t}\n+\n+\trlp->rlim_cur = 2048;\n+\treturn 0;\n+}\n+\n+/*\n+ * Use mingw specific stat()/lstat()/fstat() implementations on Windows,\n+ * including our own struct stat with 64 bit st_size and nanosecond-precision\n+ * file times.\n+ */\n+#ifndef __MINGW64_VERSION_MAJOR\n+#define off_t off64_t\n+#define lseek _lseeki64\n+#ifndef _MSC_VER\n+struct timespec {\n+\ttime_t tv_sec;\n+\tlong tv_nsec;\n+};\n+#endif\n+#endif\n+\n+struct mingw_stat {\n+    _dev_t st_dev;\n+    _ino_t st_ino;\n+    _mode_t st_mode;\n+    short st_nlink;\n+    short st_uid;\n+    short st_gid;\n+    _dev_t st_rdev;\n+    off64_t st_size;\n+    struct timespec st_atim;\n+    struct timespec st_mtim;\n+    struct timespec st_ctim;\n+};\n+\n+#define st_atime st_atim.tv_sec\n+#define st_mtime st_mtim.tv_sec\n+#define st_ctime st_ctim.tv_sec\n+\n+#ifdef stat\n+#undef stat\n+#endif\n+#define stat mingw_stat\n+int mingw_lstat(const char *file_name, struct stat *buf);\n+int mingw_stat(const char *file_name, struct stat *buf);\n+int mingw_fstat(int fd, struct stat *buf);\n+#ifdef fstat\n+#undef fstat\n+#endif\n+#define fstat mingw_fstat\n+#ifdef lstat\n+#undef lstat\n+#endif\n+#define lstat mingw_lstat\n+\n+\n+int mingw_utime(const char *file_name, const struct utimbuf *times);\n+#define utime mingw_utime\n+size_t mingw_strftime(char *s, size_t max,\n+\t\t   const char *format, const struct tm *tm);\n+#define strftime mingw_strftime\n+\n+pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env,\n+\t\t     const char *dir,\n+\t\t     int fhin, int fhout, int fherr);\n+int mingw_execvp(const char *cmd, char *const *argv);\n+#define execvp mingw_execvp\n+int mingw_execv(const char *cmd, char *const *argv);\n+#define execv mingw_execv\n+\n+static inline unsigned int git_ntohl(unsigned int x)\n+{ return (unsigned int)ntohl(x); }\n+#define ntohl git_ntohl\n+\n+sig_handler_t mingw_signal(int sig, sig_handler_t handler);\n+#define signal mingw_signal\n+\n+int mingw_raise(int sig);\n+#define raise mingw_raise\n+\n+/*\n+ * ANSI emulation wrappers\n+ */\n+\n+int winansi_isatty(int fd);\n+#define isatty winansi_isatty\n+\n+int winansi_dup2(int oldfd, int newfd);\n+#define dup2 winansi_dup2\n+\n+void winansi_init(void);\n+HANDLE winansi_get_osfhandle(int fd);\n+\n+#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)\n+#define PRIuMAX \"I64u\"\n+#define PRId64 \"I64d\"\n+#else\n+#include <inttypes.h>\n+#endif\n+\n+#endif /* COMPAT_MINGW_POSIX_H */\ndiff --git a/compat/mingw.h b/compat/mingw.h\nindex ebfb8ba423b..444daedfa52 100644\n--- a/compat/mingw.h\n+++ b/compat/mingw.h\n@@ -1,185 +1,10 @@\n-#ifdef __MINGW64_VERSION_MAJOR\n-#include <stdint.h>\n-#include <wchar.h>\n-typedef _sigset_t sigset_t;\n-#endif\n-#include <winsock2.h>\n-#include <ws2tcpip.h>\n-\n-/* MinGW-w64 reports to have flockfile, but it does not actually have it. */\n-#ifdef __MINGW64_VERSION_MAJOR\n-#undef _POSIX_THREAD_SAFE_FUNCTIONS\n-#endif\n+#include \"mingw-posix.h\"\n \n struct config_context;\n int mingw_core_config(const char *var, const char *value,\n \t\t      const struct config_context *ctx, void *cb);\n #define platform_core_config mingw_core_config\n \n-/*\n- * things that are not available in header files\n- */\n-\n-typedef int uid_t;\n-typedef int socklen_t;\n-#ifndef __MINGW64_VERSION_MAJOR\n-typedef int pid_t;\n-#define hstrerror strerror\n-#endif\n-\n-#define S_IFLNK    0120000 /* Symbolic link */\n-#define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK)\n-#define S_ISSOCK(x) 0\n-\n-#ifndef S_IRWXG\n-#define S_IRGRP 0\n-#define S_IWGRP 0\n-#define S_IXGRP 0\n-#define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)\n-#endif\n-#ifndef S_IRWXO\n-#define S_IROTH 0\n-#define S_IWOTH 0\n-#define S_IXOTH 0\n-#define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)\n-#endif\n-\n-#define S_ISUID 0004000\n-#define S_ISGID 0002000\n-#define S_ISVTX 0001000\n-\n-#define WIFEXITED(x) 1\n-#define WIFSIGNALED(x) 0\n-#define WEXITSTATUS(x) ((x) & 0xff)\n-#define WTERMSIG(x) SIGTERM\n-\n-#ifndef EWOULDBLOCK\n-#define EWOULDBLOCK EAGAIN\n-#endif\n-#ifndef ELOOP\n-#define ELOOP EMLINK\n-#endif\n-#define SHUT_WR SD_SEND\n-\n-#define SIGHUP 1\n-#define SIGQUIT 3\n-#define SIGKILL 9\n-#define SIGPIPE 13\n-#define SIGALRM 14\n-#define SIGCHLD 17\n-\n-#define F_GETFD 1\n-#define F_SETFD 2\n-#define FD_CLOEXEC 0x1\n-\n-#if !defined O_CLOEXEC && defined O_NOINHERIT\n-#define O_CLOEXEC\tO_NOINHERIT\n-#endif\n-\n-#ifndef EAFNOSUPPORT\n-#define EAFNOSUPPORT WSAEAFNOSUPPORT\n-#endif\n-#ifndef ECONNABORTED\n-#define ECONNABORTED WSAECONNABORTED\n-#endif\n-#ifndef ENOTSOCK\n-#define ENOTSOCK WSAENOTSOCK\n-#endif\n-\n-struct passwd {\n-\tchar *pw_name;\n-\tchar *pw_gecos;\n-\tchar *pw_dir;\n-};\n-\n-typedef void (__cdecl *sig_handler_t)(int);\n-struct sigaction {\n-\tsig_handler_t sa_handler;\n-\tunsigned sa_flags;\n-};\n-#define SA_RESTART 0\n-\n-struct itimerval {\n-\tstruct timeval it_value, it_interval;\n-};\n-#define ITIMER_REAL 0\n-\n-struct utsname {\n-\tchar sysname[16];\n-\tchar nodename[1];\n-\tchar release[16];\n-\tchar version[16];\n-\tchar machine[1];\n-};\n-\n-/*\n- * sanitize preprocessor namespace polluted by Windows headers defining\n- * macros which collide with git local versions\n- */\n-#undef HELP_COMMAND /* from winuser.h */\n-\n-/*\n- * trivial stubs\n- */\n-\n-static inline int readlink(const char *path UNUSED, char *buf UNUSED, size_t bufsiz UNUSED)\n-{ errno = ENOSYS; return -1; }\n-static inline int symlink(const char *oldpath UNUSED, const char *newpath UNUSED)\n-{ errno = ENOSYS; return -1; }\n-static inline int fchmod(int fildes UNUSED, mode_t mode UNUSED)\n-{ errno = ENOSYS; return -1; }\n-#ifndef __MINGW64_VERSION_MAJOR\n-static inline pid_t fork(void)\n-{ errno = ENOSYS; return -1; }\n-#endif\n-static inline unsigned int alarm(unsigned int seconds UNUSED)\n-{ return 0; }\n-static inline int fsync(int fd)\n-{ return _commit(fd); }\n-static inline void sync(void)\n-{}\n-static inline uid_t getuid(void)\n-{ return 1; }\n-static inline struct passwd *getpwnam(const char *name UNUSED)\n-{ return NULL; }\n-static inline int fcntl(int fd UNUSED, int cmd, ...)\n-{\n-\tif (cmd == F_GETFD || cmd == F_SETFD)\n-\t\treturn 0;\n-\terrno = EINVAL;\n-\treturn -1;\n-}\n-\n-#define sigemptyset(x) (void)0\n-static inline int sigaddset(sigset_t *set UNUSED, int signum UNUSED)\n-{ return 0; }\n-#define SIG_BLOCK 0\n-#define SIG_UNBLOCK 0\n-static inline int sigprocmask(int how UNUSED, const sigset_t *set UNUSED, sigset_t *oldset UNUSED)\n-{ return 0; }\n-static inline pid_t getppid(void)\n-{ return 1; }\n-static inline pid_t getpgid(pid_t pid)\n-{ return pid == 0 ? getpid() : pid; }\n-static inline pid_t tcgetpgrp(int fd UNUSED)\n-{ return getpid(); }\n-\n-/*\n- * simple adaptors\n- */\n-\n-int mingw_mkdir(const char *path, int mode);\n-#define mkdir mingw_mkdir\n-\n-#define WNOHANG 1\n-pid_t waitpid(pid_t pid, int *status, int options);\n-\n-#define kill mingw_kill\n-int mingw_kill(pid_t pid, int sig);\n-\n-#define locate_in_PATH mingw_locate_in_PATH\n-char *mingw_locate_in_PATH(const char *cmd);\n-\n #ifndef NO_OPENSSL\n #include <openssl/ssl.h>\n static inline int mingw_SSL_set_fd(SSL *ssl, int fd)\n@@ -201,249 +26,6 @@ static inline int mingw_SSL_set_wfd(SSL *ssl, int fd)\n #define SSL_set_wfd mingw_SSL_set_wfd\n #endif\n \n-/*\n- * implementations of missing functions\n- */\n-\n-int pipe(int filedes[2]);\n-unsigned int sleep (unsigned int seconds);\n-int mkstemp(char *template);\n-int gettimeofday(struct timeval *tv, void *tz);\n-#ifndef __MINGW64_VERSION_MAJOR\n-struct tm *gmtime_r(const time_t *timep, struct tm *result);\n-struct tm *localtime_r(const time_t *timep, struct tm *result);\n-#endif\n-int getpagesize(void);\t/* defined in MinGW's libgcc.a */\n-struct passwd *getpwuid(uid_t uid);\n-int setitimer(int type, struct itimerval *in, struct itimerval *out);\n-int sigaction(int sig, struct sigaction *in, struct sigaction *out);\n-int link(const char *oldpath, const char *newpath);\n-int uname(struct utsname *buf);\n-\n-/*\n- * replacements of existing functions\n- */\n-\n-int mingw_unlink(const char *pathname);\n-#define unlink mingw_unlink\n-\n-int mingw_rmdir(const char *path);\n-#define rmdir mingw_rmdir\n-\n-int mingw_open (const char *filename, int oflags, ...);\n-#define open mingw_open\n-#undef OPEN_RETURNS_EINTR\n-\n-int mingw_fgetc(FILE *stream);\n-#define fgetc mingw_fgetc\n-\n-FILE *mingw_fopen (const char *filename, const char *otype);\n-#define fopen mingw_fopen\n-\n-FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream);\n-#define freopen mingw_freopen\n-\n-int mingw_fflush(FILE *stream);\n-#define fflush mingw_fflush\n-\n-ssize_t mingw_write(int fd, const void *buf, size_t len);\n-#define write mingw_write\n-\n-int mingw_access(const char *filename, int mode);\n-#undef access\n-#define access mingw_access\n-\n-int mingw_chdir(const char *dirname);\n-#define chdir mingw_chdir\n-\n-int mingw_chmod(const char *filename, int mode);\n-#define chmod mingw_chmod\n-\n-char *mingw_mktemp(char *template);\n-#define mktemp mingw_mktemp\n-\n-char *mingw_getcwd(char *pointer, int len);\n-#define getcwd mingw_getcwd\n-\n-#ifdef NO_UNSETENV\n-#error \"NO_UNSETENV is incompatible with the Windows-specific startup code!\"\n-#endif\n-\n-/*\n- * We bind *env() routines (even the mingw_ ones) to private mingw_ versions.\n- * These talk to the CRT using UNICODE/wchar_t, but maintain the original\n- * narrow-char API.\n- *\n- * Note that the MSCRT maintains both ANSI (getenv()) and UNICODE (_wgetenv())\n- * routines and stores both versions of each environment variable in parallel\n- * (and secretly updates both when you set one or the other), but it uses CP_ACP\n- * to do the conversion rather than CP_UTF8.\n- *\n- * Since everything in the git code base is UTF8, we define the mingw_ routines\n- * to access the CRT using the UNICODE routines and manually convert them to\n- * UTF8.  This also avoids round-trip problems.\n- *\n- * This also helps with our linkage, since \"_wenviron\" is publicly exported\n- * from the CRT.  But to access \"_environ\" we would have to statically link\n- * to the CRT (/MT).\n- *\n- * We require NO_SETENV (and let gitsetenv() call our mingw_putenv).\n- */\n-#define getenv       mingw_getenv\n-#define putenv       mingw_putenv\n-#define unsetenv     mingw_putenv\n-char *mingw_getenv(const char *name);\n-int   mingw_putenv(const char *name);\n-\n-int mingw_gethostname(char *host, int namelen);\n-#define gethostname mingw_gethostname\n-\n-struct hostent *mingw_gethostbyname(const char *host);\n-#define gethostbyname mingw_gethostbyname\n-\n-int mingw_getaddrinfo(const char *node, const char *service,\n-\t\t      const struct addrinfo *hints, struct addrinfo **res);\n-#define getaddrinfo mingw_getaddrinfo\n-\n-int mingw_socket(int domain, int type, int protocol);\n-#define socket mingw_socket\n-\n-int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz);\n-#define connect mingw_connect\n-\n-int mingw_bind(int sockfd, struct sockaddr *sa, size_t sz);\n-#define bind mingw_bind\n-\n-int mingw_setsockopt(int sockfd, int lvl, int optname, void *optval, int optlen);\n-#define setsockopt mingw_setsockopt\n-\n-int mingw_shutdown(int sockfd, int how);\n-#define shutdown mingw_shutdown\n-\n-int mingw_listen(int sockfd, int backlog);\n-#define listen mingw_listen\n-\n-int mingw_accept(int sockfd, struct sockaddr *sa, socklen_t *sz);\n-#define accept mingw_accept\n-\n-int mingw_rename(const char*, const char*);\n-#define rename mingw_rename\n-\n-#if defined(USE_WIN32_MMAP) || defined(_MSC_VER)\n-int mingw_getpagesize(void);\n-#define getpagesize mingw_getpagesize\n-#endif\n-\n-int win32_fsync_no_flush(int fd);\n-#define fsync_no_flush win32_fsync_no_flush\n-\n-#define FSYNC_COMPONENTS_PLATFORM_DEFAULT (FSYNC_COMPONENTS_DEFAULT | FSYNC_COMPONENT_LOOSE_OBJECT)\n-#define FSYNC_METHOD_DEFAULT (FSYNC_METHOD_BATCH)\n-\n-struct rlimit {\n-\tunsigned int rlim_cur;\n-};\n-#define RLIMIT_NOFILE 0\n-\n-static inline int getrlimit(int resource, struct rlimit *rlp)\n-{\n-\tif (resource != RLIMIT_NOFILE) {\n-\t\terrno = EINVAL;\n-\t\treturn -1;\n-\t}\n-\n-\trlp->rlim_cur = 2048;\n-\treturn 0;\n-}\n-\n-/*\n- * Use mingw specific stat()/lstat()/fstat() implementations on Windows,\n- * including our own struct stat with 64 bit st_size and nanosecond-precision\n- * file times.\n- */\n-#ifndef __MINGW64_VERSION_MAJOR\n-#define off_t off64_t\n-#define lseek _lseeki64\n-#ifndef _MSC_VER\n-struct timespec {\n-\ttime_t tv_sec;\n-\tlong tv_nsec;\n-};\n-#endif\n-#endif\n-\n-struct mingw_stat {\n-    _dev_t st_dev;\n-    _ino_t st_ino;\n-    _mode_t st_mode;\n-    short st_nlink;\n-    short st_uid;\n-    short st_gid;\n-    _dev_t st_rdev;\n-    off64_t st_size;\n-    struct timespec st_atim;\n-    struct timespec st_mtim;\n-    struct timespec st_ctim;\n-};\n-\n-#define st_atime st_atim.tv_sec\n-#define st_mtime st_mtim.tv_sec\n-#define st_ctime st_ctim.tv_sec\n-\n-#ifdef stat\n-#undef stat\n-#endif\n-#define stat mingw_stat\n-int mingw_lstat(const char *file_name, struct stat *buf);\n-int mingw_stat(const char *file_name, struct stat *buf);\n-int mingw_fstat(int fd, struct stat *buf);\n-#ifdef fstat\n-#undef fstat\n-#endif\n-#define fstat mingw_fstat\n-#ifdef lstat\n-#undef lstat\n-#endif\n-#define lstat mingw_lstat\n-\n-\n-int mingw_utime(const char *file_name, const struct utimbuf *times);\n-#define utime mingw_utime\n-size_t mingw_strftime(char *s, size_t max,\n-\t\t   const char *format, const struct tm *tm);\n-#define strftime mingw_strftime\n-\n-pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env,\n-\t\t     const char *dir,\n-\t\t     int fhin, int fhout, int fherr);\n-int mingw_execvp(const char *cmd, char *const *argv);\n-#define execvp mingw_execvp\n-int mingw_execv(const char *cmd, char *const *argv);\n-#define execv mingw_execv\n-\n-static inline unsigned int git_ntohl(unsigned int x)\n-{ return (unsigned int)ntohl(x); }\n-#define ntohl git_ntohl\n-\n-sig_handler_t mingw_signal(int sig, sig_handler_t handler);\n-#define signal mingw_signal\n-\n-int mingw_raise(int sig);\n-#define raise mingw_raise\n-\n-/*\n- * ANSI emulation wrappers\n- */\n-\n-int winansi_isatty(int fd);\n-#define isatty winansi_isatty\n-\n-int winansi_dup2(int oldfd, int newfd);\n-#define dup2 winansi_dup2\n-\n-void winansi_init(void);\n-HANDLE winansi_get_osfhandle(int fd);\n-\n /*\n  * git specific compatibility\n  */\n@@ -457,12 +39,6 @@ static inline void convert_slashes(char *path)\n #define PATH_SEP ';'\n char *mingw_query_user_email(void);\n #define query_user_email mingw_query_user_email\n-#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)\n-#define PRIuMAX \"I64u\"\n-#define PRId64 \"I64d\"\n-#else\n-#include <inttypes.h>\n-#endif\n \n /**\n  * Verifies that the specified path is owned by the user running the\ndiff --git a/compat/msvc-posix.h b/compat/msvc-posix.h\nnew file mode 100644\nindex 00000000000..c500b8b4aaf\n--- /dev/null\n+++ b/compat/msvc-posix.h\n@@ -0,0 +1,33 @@\n+#ifndef COMPAT_MSVC_POSIX_H\n+#define COMPAT_MSVC_POSIX_H\n+\n+#include <direct.h>\n+#include <process.h>\n+#include <malloc.h>\n+#include <io.h>\n+\n+#pragma warning(disable: 4018) /* signed/unsigned comparison */\n+#pragma warning(disable: 4244) /* type conversion, possible loss of data */\n+#pragma warning(disable: 4090) /* 'function' : different 'const' qualifiers (ALLOC_GROW etc.)*/\n+\n+/* porting function */\n+#define inline __inline\n+#define __inline__ __inline\n+#define __attribute__(x)\n+#define strcasecmp   _stricmp\n+#define strncasecmp  _strnicmp\n+#define ftruncate    _chsize\n+#define strtoull     _strtoui64\n+#define strtoll      _strtoi64\n+\n+#undef ERROR\n+\n+#define ftello _ftelli64\n+\n+typedef int sigset_t;\n+/* open for reading, writing, or both (not in fcntl.h) */\n+#define O_ACCMODE     (_O_RDONLY | _O_WRONLY | _O_RDWR)\n+\n+#include \"mingw-posix.h\"\n+\n+#endif /* COMPAT_MSVC_POSIX_H */\ndiff --git a/compat/msvc.h b/compat/msvc.h\nindex 1d7a8c61456..2b87c0a7c70 100644\n--- a/compat/msvc.h\n+++ b/compat/msvc.h\n@@ -1,33 +1,7 @@\n #ifndef __MSVC__HEAD\n #define __MSVC__HEAD\n \n-#include <direct.h>\n-#include <process.h>\n-#include <malloc.h>\n-#include <io.h>\n-\n-#pragma warning(disable: 4018) /* signed/unsigned comparison */\n-#pragma warning(disable: 4244) /* type conversion, possible loss of data */\n-#pragma warning(disable: 4090) /* 'function' : different 'const' qualifiers (ALLOC_GROW etc.)*/\n-\n-/* porting function */\n-#define inline __inline\n-#define __inline__ __inline\n-#define __attribute__(x)\n-#define strcasecmp   _stricmp\n-#define strncasecmp  _strnicmp\n-#define ftruncate    _chsize\n-#define strtoull     _strtoui64\n-#define strtoll      _strtoi64\n-\n-#undef ERROR\n-\n-#define ftello _ftelli64\n-\n-typedef int sigset_t;\n-/* open for reading, writing, or both (not in fcntl.h) */\n-#define O_ACCMODE     (_O_RDONLY | _O_WRONLY | _O_RDWR)\n-\n-#include \"compat/mingw.h\"\n+#include \"msvc-posix.h\"\n+#include \"mingw.h\"\n \n #endif\n\n-- \n2.48.1.666.gff9fcf71b7.dirty\n\n"},{"id":"512581","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-16-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"[PATCH v6 16/18] git-compat-util.h: split out POSIX-emulating bits","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:52Z","receivedAt":"2025-02-18T09:22:12Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"The \"git-compat-util.h\" header is a treasure trove of various bits and\npieces used throughout the project. It basically mixes two different\nthings into one:\n\n  - Providing a POSIX-like interface even on platforms that aren't\n    POSIX-compliant.\n\n  - Providing low-level functionality that is specific to Git.\n\nThis intermixing is a bit of a problem for the reftable library as we\ndon't want to recreate the POSIX-like interface there. But neither do we\nwant to pull in the Git-specific functionality, as it is otherwise quite\neasy to start depending on the Git codebase again.\n\nSplit out a new header \"compat/posix.h\" that only contains the bits and\npieces relevant for the emulation of POSIX, which we will start using in\nthe next commit.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n compat/posix.h    | 541 ++++++++++++++++++++++++++++++++++++++++++++++++++++++\n git-compat-util.h | 531 +----------------------------------------------------\n 2 files changed, 543 insertions(+), 529 deletions(-)\n\ndiff --git a/compat/posix.h b/compat/posix.h\nnew file mode 100644\nindex 00000000000..f4c71f9427d\n--- /dev/null\n+++ b/compat/posix.h\n@@ -0,0 +1,541 @@\n+#ifndef COMPAT_POSIX_H\n+#define COMPAT_POSIX_H\n+\n+#define _FILE_OFFSET_BITS 64\n+\n+/*\n+ * Derived from Linux \"Features Test Macro\" header\n+ * Convenience macros to test the versions of gcc (or\n+ * a compatible compiler).\n+ * Use them like this:\n+ *  #if GIT_GNUC_PREREQ (2,8)\n+ *   ... code requiring gcc 2.8 or later ...\n+ *  #endif\n+ *\n+ * This macro of course is not part of POSIX, but we need it for the UNUSED\n+ * macro which is used by some of our POSIX compatibility wrappers.\n+*/\n+#if defined(__GNUC__) && defined(__GNUC_MINOR__)\n+# define GIT_GNUC_PREREQ(maj, min) \\\n+\t((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))\n+#else\n+ #define GIT_GNUC_PREREQ(maj, min) 0\n+#endif\n+\n+/*\n+ * UNUSED marks a function parameter that is always unused.  It also\n+ * can be used to annotate a function, a variable, or a type that is\n+ * always unused.\n+ *\n+ * A callback interface may dictate that a function accepts a\n+ * parameter at that position, but the implementation of the function\n+ * may not need to use the parameter.  In such a case, mark the parameter\n+ * with UNUSED.\n+ *\n+ * When a parameter may be used or unused, depending on conditional\n+ * compilation, consider using MAYBE_UNUSED instead.\n+ */\n+#if GIT_GNUC_PREREQ(4, 5)\n+#define UNUSED __attribute__((unused)) \\\n+\t__attribute__((deprecated (\"parameter declared as UNUSED\")))\n+#elif defined(__GNUC__)\n+#define UNUSED __attribute__((unused)) \\\n+\t__attribute__((deprecated))\n+#else\n+#define UNUSED\n+#endif\n+\n+#ifdef __MINGW64__\n+#define _POSIX_C_SOURCE 1\n+#elif defined(__sun__)\n+ /*\n+  * On Solaris, when _XOPEN_EXTENDED is set, its header file\n+  * forces the programs to be XPG4v2, defeating any _XOPEN_SOURCE\n+  * setting to say we are XPG5 or XPG6.  Also on Solaris,\n+  * XPG6 programs must be compiled with a c99 compiler, while\n+  * non XPG6 programs must be compiled with a pre-c99 compiler.\n+  */\n+# if __STDC_VERSION__ - 0 >= 199901L\n+# define _XOPEN_SOURCE 600\n+# else\n+# define _XOPEN_SOURCE 500\n+# endif\n+#elif !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__USLC__) && \\\n+      !defined(_M_UNIX) && !defined(__sgi) && !defined(__DragonFly__) && \\\n+      !defined(__TANDEM) && !defined(__QNX__) && !defined(__MirBSD__) && \\\n+      !defined(__CYGWIN__)\n+#define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */\n+#define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */\n+#endif\n+#define _ALL_SOURCE 1\n+#define _GNU_SOURCE 1\n+#define _BSD_SOURCE 1\n+#define _DEFAULT_SOURCE 1\n+#define _NETBSD_SOURCE 1\n+#define _SGI_SOURCE 1\n+\n+#if defined(WIN32) && !defined(__CYGWIN__) /* Both MinGW and MSVC */\n+# if !defined(_WIN32_WINNT)\n+#  define _WIN32_WINNT 0x0600\n+# endif\n+#define WIN32_LEAN_AND_MEAN  /* stops windows.h including winsock.h */\n+#include <winsock2.h>\n+#ifndef NO_UNIX_SOCKETS\n+#include <afunix.h>\n+#endif\n+#include <windows.h>\n+#define GIT_WINDOWS_NATIVE\n+#endif\n+\n+#include <unistd.h>\n+#include <stdio.h>\n+#include <sys/stat.h>\n+#include <fcntl.h>\n+#include <stddef.h>\n+#include <stdlib.h>\n+#include <stdarg.h>\n+#include <stdbool.h>\n+#include <string.h>\n+#ifdef HAVE_STRINGS_H\n+#include <strings.h> /* for strcasecmp() */\n+#endif\n+#include <errno.h>\n+#include <limits.h>\n+#include <locale.h>\n+#ifdef NEEDS_SYS_PARAM_H\n+#include <sys/param.h>\n+#endif\n+#include <sys/types.h>\n+#include <dirent.h>\n+#include <sys/time.h>\n+#include <time.h>\n+#include <signal.h>\n+#include <assert.h>\n+#include <regex.h>\n+#include <utime.h>\n+#include <syslog.h>\n+#if !defined(NO_POLL_H)\n+#include <poll.h>\n+#elif !defined(NO_SYS_POLL_H)\n+#include <sys/poll.h>\n+#else\n+/* Pull the compat stuff */\n+#include <poll.h>\n+#endif\n+#ifdef HAVE_BSD_SYSCTL\n+#include <sys/sysctl.h>\n+#endif\n+\n+#if defined(__MINGW32__)\n+#include \"mingw-posix.h\"\n+#elif defined(_MSC_VER)\n+#include \"msvc-posix.h\"\n+#else\n+#include <sys/utsname.h>\n+#include <sys/wait.h>\n+#include <sys/resource.h>\n+#include <sys/socket.h>\n+#include <sys/ioctl.h>\n+#include <sys/statvfs.h>\n+#include <termios.h>\n+#ifndef NO_SYS_SELECT_H\n+#include <sys/select.h>\n+#endif\n+#include <netinet/in.h>\n+#include <netinet/tcp.h>\n+#include <arpa/inet.h>\n+#include <netdb.h>\n+#include <pwd.h>\n+#include <sys/un.h>\n+#ifndef NO_INTTYPES_H\n+#include <inttypes.h>\n+#else\n+#include <stdint.h>\n+#endif\n+#ifdef HAVE_ARC4RANDOM_LIBBSD\n+#include <bsd/stdlib.h>\n+#endif\n+#ifdef HAVE_GETRANDOM\n+#include <sys/random.h>\n+#endif\n+#ifdef NO_INTPTR_T\n+/*\n+ * On I16LP32, ILP32 and LP64 \"long\" is the safe bet, however\n+ * on LLP86, IL33LLP64 and P64 it needs to be \"long long\",\n+ * while on IP16 and IP16L32 it is \"int\" (resp. \"short\")\n+ * Size needs to match (or exceed) 'sizeof(void *)'.\n+ * We can't take \"long long\" here as not everybody has it.\n+ */\n+typedef long intptr_t;\n+typedef unsigned long uintptr_t;\n+#endif\n+#undef _ALL_SOURCE /* AIX 5.3L defines a struct list with _ALL_SOURCE. */\n+#include <grp.h>\n+#define _ALL_SOURCE 1\n+#endif\n+\n+#ifdef MKDIR_WO_TRAILING_SLASH\n+#define mkdir(a,b) compat_mkdir_wo_trailing_slash((a),(b))\n+int compat_mkdir_wo_trailing_slash(const char*, mode_t);\n+#endif\n+\n+#ifdef time\n+#undef time\n+#endif\n+static inline time_t git_time(time_t *tloc)\n+{\n+\tstruct timeval tv;\n+\n+\t/*\n+\t * Avoid time(NULL), which can disagree with gettimeofday(2)\n+\t * and filesystem timestamps.\n+\t */\n+\tgettimeofday(&tv, NULL);\n+\n+\tif (tloc)\n+\t\t*tloc = tv.tv_sec;\n+\treturn tv.tv_sec;\n+}\n+#define time git_time\n+\n+#ifdef NO_STRUCT_ITIMERVAL\n+struct itimerval {\n+\tstruct timeval it_interval;\n+\tstruct timeval it_value;\n+};\n+#endif\n+\n+#ifdef NO_SETITIMER\n+static inline int git_setitimer(int which UNUSED,\n+\t\t\t\tconst struct itimerval *value UNUSED,\n+\t\t\t\tstruct itimerval *newvalue UNUSED) {\n+\treturn 0; /* pretend success */\n+}\n+#undef setitimer\n+#define setitimer(which,value,ovalue) git_setitimer(which,value,ovalue)\n+#endif\n+\n+#ifndef NO_LIBGEN_H\n+#include <libgen.h>\n+#else\n+#define basename gitbasename\n+char *gitbasename(char *);\n+#define dirname gitdirname\n+char *gitdirname(char *);\n+#endif\n+\n+#ifndef NO_ICONV\n+#include <iconv.h>\n+#endif\n+\n+/* On most systems <netdb.h> would have given us this, but\n+ * not on some systems (e.g. z/OS).\n+ */\n+#ifndef NI_MAXHOST\n+#define NI_MAXHOST 1025\n+#endif\n+\n+#ifndef NI_MAXSERV\n+#define NI_MAXSERV 32\n+#endif\n+\n+/* On most systems <limits.h> would have given us this, but\n+ * not on some systems (e.g. GNU/Hurd).\n+ */\n+#ifndef PATH_MAX\n+#define PATH_MAX 4096\n+#endif\n+\n+#ifndef NAME_MAX\n+#define NAME_MAX 255\n+#endif\n+\n+typedef uintmax_t timestamp_t;\n+#define PRItime PRIuMAX\n+#define parse_timestamp strtoumax\n+#define TIME_MAX UINTMAX_MAX\n+#define TIME_MIN 0\n+\n+int lstat_cache_aware_rmdir(const char *path);\n+#if !defined(__MINGW32__) && !defined(_MSC_VER)\n+#define rmdir lstat_cache_aware_rmdir\n+#endif\n+\n+#if defined(NO_MMAP) || defined(USE_WIN32_MMAP)\n+\n+#ifndef PROT_READ\n+#define PROT_READ 1\n+#define PROT_WRITE 2\n+#define MAP_PRIVATE 1\n+#endif\n+\n+#define mmap git_mmap\n+#define munmap git_munmap\n+void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);\n+int git_munmap(void *start, size_t length);\n+\n+#else /* NO_MMAP || USE_WIN32_MMAP */\n+\n+#include <sys/mman.h>\n+\n+#endif /* NO_MMAP || USE_WIN32_MMAP */\n+\n+#ifndef MAP_FAILED\n+#define MAP_FAILED ((void *)-1)\n+#endif\n+\n+#ifdef NEEDS_MODE_TRANSLATION\n+#undef S_IFMT\n+#undef S_IFREG\n+#undef S_IFDIR\n+#undef S_IFLNK\n+#undef S_IFBLK\n+#undef S_IFCHR\n+#undef S_IFIFO\n+#undef S_IFSOCK\n+#define S_IFMT   0170000\n+#define S_IFREG  0100000\n+#define S_IFDIR  0040000\n+#define S_IFLNK  0120000\n+#define S_IFBLK  0060000\n+#define S_IFCHR  0020000\n+#define S_IFIFO  0010000\n+#define S_IFSOCK 0140000\n+#ifdef stat\n+#undef stat\n+#endif\n+#define stat(path, buf) git_stat(path, buf)\n+int git_stat(const char *, struct stat *);\n+#ifdef fstat\n+#undef fstat\n+#endif\n+#define fstat(fd, buf) git_fstat(fd, buf)\n+int git_fstat(int, struct stat *);\n+#ifdef lstat\n+#undef lstat\n+#endif\n+#define lstat(path, buf) git_lstat(path, buf)\n+int git_lstat(const char *, struct stat *);\n+#endif\n+\n+#ifdef NO_PREAD\n+#define pread git_pread\n+ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);\n+#endif\n+\n+#ifdef NO_SETENV\n+#define setenv gitsetenv\n+int gitsetenv(const char *, const char *, int);\n+#endif\n+\n+#ifdef NO_MKDTEMP\n+#define mkdtemp gitmkdtemp\n+char *gitmkdtemp(char *);\n+#endif\n+\n+#ifdef NO_UNSETENV\n+#define unsetenv gitunsetenv\n+int gitunsetenv(const char *);\n+#endif\n+\n+#ifdef NO_STRCASESTR\n+#define strcasestr gitstrcasestr\n+char *gitstrcasestr(const char *haystack, const char *needle);\n+#endif\n+\n+#ifdef NO_STRLCPY\n+#define strlcpy gitstrlcpy\n+size_t gitstrlcpy(char *, const char *, size_t);\n+#endif\n+\n+#ifdef NO_STRTOUMAX\n+#define strtoumax gitstrtoumax\n+uintmax_t gitstrtoumax(const char *, char **, int);\n+#define strtoimax gitstrtoimax\n+intmax_t gitstrtoimax(const char *, char **, int);\n+#endif\n+\n+#ifdef NO_HSTRERROR\n+#define hstrerror githstrerror\n+const char *githstrerror(int herror);\n+#endif\n+\n+#ifdef NO_MEMMEM\n+#define memmem gitmemmem\n+void *gitmemmem(const void *haystack, size_t haystacklen,\n+\t\tconst void *needle, size_t needlelen);\n+#endif\n+\n+#ifdef OVERRIDE_STRDUP\n+#ifdef strdup\n+#undef strdup\n+#endif\n+#define strdup gitstrdup\n+char *gitstrdup(const char *s);\n+#endif\n+\n+#ifdef NO_GETPAGESIZE\n+#define getpagesize() sysconf(_SC_PAGESIZE)\n+#endif\n+\n+#ifndef O_CLOEXEC\n+#define O_CLOEXEC 0\n+#endif\n+\n+#ifdef FREAD_READS_DIRECTORIES\n+# if !defined(SUPPRESS_FOPEN_REDEFINITION)\n+#  ifdef fopen\n+#   undef fopen\n+#  endif\n+#  define fopen(a,b) git_fopen(a,b)\n+# endif\n+FILE *git_fopen(const char*, const char*);\n+#endif\n+\n+#ifdef SNPRINTF_RETURNS_BOGUS\n+#ifdef snprintf\n+#undef snprintf\n+#endif\n+#define snprintf git_snprintf\n+int git_snprintf(char *str, size_t maxsize,\n+\t\t const char *format, ...);\n+#ifdef vsnprintf\n+#undef vsnprintf\n+#endif\n+#define vsnprintf git_vsnprintf\n+int git_vsnprintf(char *str, size_t maxsize,\n+\t\t  const char *format, va_list ap);\n+#endif\n+\n+#ifdef OPEN_RETURNS_EINTR\n+#undef open\n+#define open git_open_with_retry\n+int git_open_with_retry(const char *path, int flag, ...);\n+#endif\n+\n+#ifdef __GLIBC_PREREQ\n+#if __GLIBC_PREREQ(2, 1)\n+#define HAVE_STRCHRNUL\n+#endif\n+#endif\n+\n+#ifndef HAVE_STRCHRNUL\n+#define strchrnul gitstrchrnul\n+static inline char *gitstrchrnul(const char *s, int c)\n+{\n+\twhile (*s && *s != c)\n+\t\ts++;\n+\treturn (char *)s;\n+}\n+#endif\n+\n+#ifdef NO_INET_PTON\n+int inet_pton(int af, const char *src, void *dst);\n+#endif\n+\n+#ifdef NO_INET_NTOP\n+const char *inet_ntop(int af, const void *src, char *dst, size_t size);\n+#endif\n+\n+#ifdef NO_PTHREADS\n+#define atexit git_atexit\n+int git_atexit(void (*handler)(void));\n+#endif\n+\n+#ifndef HOST_NAME_MAX\n+#define HOST_NAME_MAX 256\n+#endif\n+\n+#include \"../sane-ctype.h\"\n+\n+void git_stable_qsort(void *base, size_t nmemb, size_t size,\n+\t\t      int(*compar)(const void *, const void *));\n+#ifdef INTERNAL_QSORT\n+#define qsort git_stable_qsort\n+#endif\n+\n+#define QSORT(base, n, compar) sane_qsort((base), (n), sizeof(*(base)), compar)\n+static inline void sane_qsort(void *base, size_t nmemb, size_t size,\n+\t\t\t      int(*compar)(const void *, const void *))\n+{\n+\tif (nmemb > 1)\n+\t\tqsort(base, nmemb, size, compar);\n+}\n+\n+#define STABLE_QSORT(base, n, compar) \\\n+\tgit_stable_qsort((base), (n), sizeof(*(base)), compar)\n+\n+#ifndef HAVE_ISO_QSORT_S\n+int git_qsort_s(void *base, size_t nmemb, size_t size,\n+\t\tint (*compar)(const void *, const void *, void *), void *ctx);\n+#define qsort_s git_qsort_s\n+#endif\n+\n+#define QSORT_S(base, n, compar, ctx) do {\t\t\t\\\n+\tif (qsort_s((base), (n), sizeof(*(base)), compar, ctx))\t\\\n+\t\tBUG(\"qsort_s() failed\");\t\t\t\\\n+} while (0)\n+\n+#ifdef NO_NSEC\n+#undef USE_NSEC\n+#define ST_CTIME_NSEC(st) 0\n+#define ST_MTIME_NSEC(st) 0\n+#else\n+#ifdef USE_ST_TIMESPEC\n+#define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctimespec.tv_nsec))\n+#define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtimespec.tv_nsec))\n+#else\n+#define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctim.tv_nsec))\n+#define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtim.tv_nsec))\n+#endif\n+#endif\n+\n+#ifndef va_copy\n+/*\n+ * Since an obvious implementation of va_list would be to make it a\n+ * pointer into the stack frame, a simple assignment will work on\n+ * many systems.  But let's try to be more portable.\n+ */\n+#ifdef __va_copy\n+#define va_copy(dst, src) __va_copy(dst, src)\n+#else\n+#define va_copy(dst, src) ((dst) = (src))\n+#endif\n+#endif\n+\n+#ifndef _POSIX_THREAD_SAFE_FUNCTIONS\n+static inline void git_flockfile(FILE *fh UNUSED)\n+{\n+\t; /* nothing */\n+}\n+static inline void git_funlockfile(FILE *fh UNUSED)\n+{\n+\t; /* nothing */\n+}\n+#undef flockfile\n+#undef funlockfile\n+#undef getc_unlocked\n+#define flockfile(fh) git_flockfile(fh)\n+#define funlockfile(fh) git_funlockfile(fh)\n+#define getc_unlocked(fh) getc(fh)\n+#endif\n+\n+#ifdef FILENO_IS_A_MACRO\n+int git_fileno(FILE *stream);\n+# ifndef COMPAT_CODE_FILENO\n+#  undef fileno\n+#  define fileno(p) git_fileno(p)\n+# endif\n+#endif\n+\n+#ifdef NEED_ACCESS_ROOT_HANDLER\n+int git_access(const char *path, int mode);\n+# ifndef COMPAT_CODE_ACCESS\n+#  ifdef access\n+#  undef access\n+#  endif\n+#  define access(path, mode) git_access(path, mode)\n+# endif\n+#endif\n+\n+#endif /* COMPAT_POSIX_H */\ndiff --git a/git-compat-util.h b/git-compat-util.h\nindex e123288e8f1..03e2ba59d3e 100644\n--- a/git-compat-util.h\n+++ b/git-compat-util.h\n@@ -23,26 +23,9 @@\n #include <crtdbg.h>\n #endif\n \n-struct strbuf;\n-\n-\n-#define _FILE_OFFSET_BITS 64\n+#include \"compat/posix.h\"\n \n-\n-/* Derived from Linux \"Features Test Macro\" header\n- * Convenience macros to test the versions of gcc (or\n- * a compatible compiler).\n- * Use them like this:\n- *  #if GIT_GNUC_PREREQ (2,8)\n- *   ... code requiring gcc 2.8 or later ...\n- *  #endif\n-*/\n-#if defined(__GNUC__) && defined(__GNUC_MINOR__)\n-# define GIT_GNUC_PREREQ(maj, min) \\\n-\t((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))\n-#else\n- #define GIT_GNUC_PREREQ(maj, min) 0\n-#endif\n+struct strbuf;\n \n #if defined(__GNUC__) || defined(__clang__)\n #  define PRAGMA(pragma)           _Pragma(#pragma)\n@@ -176,71 +159,6 @@ DISABLE_WARNING(-Wsign-compare)\n /* Approximation of the length of the decimal representation of this type. */\n #define decimal_length(x)\t((int)(sizeof(x) * 2.56 + 0.5) + 1)\n \n-#ifdef __MINGW64__\n-#define _POSIX_C_SOURCE 1\n-#elif defined(__sun__)\n- /*\n-  * On Solaris, when _XOPEN_EXTENDED is set, its header file\n-  * forces the programs to be XPG4v2, defeating any _XOPEN_SOURCE\n-  * setting to say we are XPG5 or XPG6.  Also on Solaris,\n-  * XPG6 programs must be compiled with a c99 compiler, while\n-  * non XPG6 programs must be compiled with a pre-c99 compiler.\n-  */\n-# if __STDC_VERSION__ - 0 >= 199901L\n-# define _XOPEN_SOURCE 600\n-# else\n-# define _XOPEN_SOURCE 500\n-# endif\n-#elif !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__USLC__) && \\\n-      !defined(_M_UNIX) && !defined(__sgi) && !defined(__DragonFly__) && \\\n-      !defined(__TANDEM) && !defined(__QNX__) && !defined(__MirBSD__) && \\\n-      !defined(__CYGWIN__)\n-#define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */\n-#define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */\n-#endif\n-#define _ALL_SOURCE 1\n-#define _GNU_SOURCE 1\n-#define _BSD_SOURCE 1\n-#define _DEFAULT_SOURCE 1\n-#define _NETBSD_SOURCE 1\n-#define _SGI_SOURCE 1\n-\n-/*\n- * UNUSED marks a function parameter that is always unused.  It also\n- * can be used to annotate a function, a variable, or a type that is\n- * always unused.\n- *\n- * A callback interface may dictate that a function accepts a\n- * parameter at that position, but the implementation of the function\n- * may not need to use the parameter.  In such a case, mark the parameter\n- * with UNUSED.\n- *\n- * When a parameter may be used or unused, depending on conditional\n- * compilation, consider using MAYBE_UNUSED instead.\n- */\n-#if GIT_GNUC_PREREQ(4, 5)\n-#define UNUSED __attribute__((unused)) \\\n-\t__attribute__((deprecated (\"parameter declared as UNUSED\")))\n-#elif defined(__GNUC__)\n-#define UNUSED __attribute__((unused)) \\\n-\t__attribute__((deprecated))\n-#else\n-#define UNUSED\n-#endif\n-\n-#if defined(WIN32) && !defined(__CYGWIN__) /* Both MinGW and MSVC */\n-# if !defined(_WIN32_WINNT)\n-#  define _WIN32_WINNT 0x0600\n-# endif\n-#define WIN32_LEAN_AND_MEAN  /* stops windows.h including winsock.h */\n-#include <winsock2.h>\n-#ifndef NO_UNIX_SOCKETS\n-#include <afunix.h>\n-#endif\n-#include <windows.h>\n-#define GIT_WINDOWS_NATIVE\n-#endif\n-\n #if defined(NO_UNIX_SOCKETS) || !defined(GIT_WINDOWS_NATIVE)\n static inline int _have_unix_sockets(void)\n {\n@@ -253,45 +171,6 @@ static inline int _have_unix_sockets(void)\n #define have_unix_sockets _have_unix_sockets\n #endif\n \n-#include <unistd.h>\n-#include <stdio.h>\n-#include <sys/stat.h>\n-#include <fcntl.h>\n-#include <stddef.h>\n-#include <stdlib.h>\n-#include <stdarg.h>\n-#include <stdbool.h>\n-#include <string.h>\n-#ifdef HAVE_STRINGS_H\n-#include <strings.h> /* for strcasecmp() */\n-#endif\n-#include <errno.h>\n-#include <limits.h>\n-#include <locale.h>\n-#ifdef NEEDS_SYS_PARAM_H\n-#include <sys/param.h>\n-#endif\n-#include <sys/types.h>\n-#include <dirent.h>\n-#include <sys/time.h>\n-#include <time.h>\n-#include <signal.h>\n-#include <assert.h>\n-#include <regex.h>\n-#include <utime.h>\n-#include <syslog.h>\n-#if !defined(NO_POLL_H)\n-#include <poll.h>\n-#elif !defined(NO_SYS_POLL_H)\n-#include <sys/poll.h>\n-#else\n-/* Pull the compat stuff */\n-#include <poll.h>\n-#endif\n-#ifdef HAVE_BSD_SYSCTL\n-#include <sys/sysctl.h>\n-#endif\n-\n /* Used by compat/win32/path-utils.h, and more */\n static inline int is_xplatform_dir_sep(int c)\n {\n@@ -308,48 +187,6 @@ static inline int is_xplatform_dir_sep(int c)\n #elif defined(_MSC_VER)\n #include \"compat/win32/path-utils.h\"\n #include \"compat/msvc.h\"\n-#else\n-#include <sys/utsname.h>\n-#include <sys/wait.h>\n-#include <sys/resource.h>\n-#include <sys/socket.h>\n-#include <sys/ioctl.h>\n-#include <sys/statvfs.h>\n-#include <termios.h>\n-#ifndef NO_SYS_SELECT_H\n-#include <sys/select.h>\n-#endif\n-#include <netinet/in.h>\n-#include <netinet/tcp.h>\n-#include <arpa/inet.h>\n-#include <netdb.h>\n-#include <pwd.h>\n-#include <sys/un.h>\n-#ifndef NO_INTTYPES_H\n-#include <inttypes.h>\n-#else\n-#include <stdint.h>\n-#endif\n-#ifdef HAVE_ARC4RANDOM_LIBBSD\n-#include <bsd/stdlib.h>\n-#endif\n-#ifdef HAVE_GETRANDOM\n-#include <sys/random.h>\n-#endif\n-#ifdef NO_INTPTR_T\n-/*\n- * On I16LP32, ILP32 and LP64 \"long\" is the safe bet, however\n- * on LLP86, IL33LLP64 and P64 it needs to be \"long long\",\n- * while on IP16 and IP16L32 it is \"int\" (resp. \"short\")\n- * Size needs to match (or exceed) 'sizeof(void *)'.\n- * We can't take \"long long\" here as not everybody has it.\n- */\n-typedef long intptr_t;\n-typedef unsigned long uintptr_t;\n-#endif\n-#undef _ALL_SOURCE /* AIX 5.3L defines a struct list with _ALL_SOURCE. */\n-#include <grp.h>\n-#define _ALL_SOURCE 1\n #endif\n \n /* used on Mac OS X */\n@@ -370,60 +207,6 @@ static inline const char *precompose_string_if_needed(const char *in)\n #define probe_utf8_pathname_composition()\n #endif\n \n-#ifdef MKDIR_WO_TRAILING_SLASH\n-#define mkdir(a,b) compat_mkdir_wo_trailing_slash((a),(b))\n-int compat_mkdir_wo_trailing_slash(const char*, mode_t);\n-#endif\n-\n-#ifdef time\n-#undef time\n-#endif\n-static inline time_t git_time(time_t *tloc)\n-{\n-\tstruct timeval tv;\n-\n-\t/*\n-\t * Avoid time(NULL), which can disagree with gettimeofday(2)\n-\t * and filesystem timestamps.\n-\t */\n-\tgettimeofday(&tv, NULL);\n-\n-\tif (tloc)\n-\t\t*tloc = tv.tv_sec;\n-\treturn tv.tv_sec;\n-}\n-#define time git_time\n-\n-#ifdef NO_STRUCT_ITIMERVAL\n-struct itimerval {\n-\tstruct timeval it_interval;\n-\tstruct timeval it_value;\n-};\n-#endif\n-\n-#ifdef NO_SETITIMER\n-static inline int git_setitimer(int which UNUSED,\n-\t\t\t\tconst struct itimerval *value UNUSED,\n-\t\t\t\tstruct itimerval *newvalue UNUSED) {\n-\treturn 0; /* pretend success */\n-}\n-#undef setitimer\n-#define setitimer(which,value,ovalue) git_setitimer(which,value,ovalue)\n-#endif\n-\n-#ifndef NO_LIBGEN_H\n-#include <libgen.h>\n-#else\n-#define basename gitbasename\n-char *gitbasename(char *);\n-#define dirname gitdirname\n-char *gitdirname(char *);\n-#endif\n-\n-#ifndef NO_ICONV\n-#include <iconv.h>\n-#endif\n-\n #ifndef NO_OPENSSL\n #ifdef __APPLE__\n #undef __AVAILABILITY_MACROS_USES_AVAILABILITY\n@@ -441,34 +224,6 @@ char *gitdirname(char *);\n # include <sys/sysinfo.h>\n #endif\n \n-/* On most systems <netdb.h> would have given us this, but\n- * not on some systems (e.g. z/OS).\n- */\n-#ifndef NI_MAXHOST\n-#define NI_MAXHOST 1025\n-#endif\n-\n-#ifndef NI_MAXSERV\n-#define NI_MAXSERV 32\n-#endif\n-\n-/* On most systems <limits.h> would have given us this, but\n- * not on some systems (e.g. GNU/Hurd).\n- */\n-#ifndef PATH_MAX\n-#define PATH_MAX 4096\n-#endif\n-\n-#ifndef NAME_MAX\n-#define NAME_MAX 255\n-#endif\n-\n-typedef uintmax_t timestamp_t;\n-#define PRItime PRIuMAX\n-#define parse_timestamp strtoumax\n-#define TIME_MAX UINTMAX_MAX\n-#define TIME_MIN 0\n-\n #ifndef PATH_SEP\n #define PATH_SEP ':'\n #endif\n@@ -492,11 +247,6 @@ static inline int noop_core_config(const char *var UNUSED,\n #define platform_core_config noop_core_config\n #endif\n \n-int lstat_cache_aware_rmdir(const char *path);\n-#if !defined(__MINGW32__) && !defined(_MSC_VER)\n-#define rmdir lstat_cache_aware_rmdir\n-#endif\n-\n #ifndef has_dos_drive_prefix\n static inline int git_has_dos_drive_prefix(const char *path UNUSED)\n {\n@@ -824,25 +574,6 @@ static inline bool strip_suffix(const char *str, const char *suffix,\n \tmemcpy(_swap_b_ptr, _swap_buffer, sizeof(a));\t\t\\\n } while (0)\n \n-#if defined(NO_MMAP) || defined(USE_WIN32_MMAP)\n-\n-#ifndef PROT_READ\n-#define PROT_READ 1\n-#define PROT_WRITE 2\n-#define MAP_PRIVATE 1\n-#endif\n-\n-#define mmap git_mmap\n-#define munmap git_munmap\n-void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);\n-int git_munmap(void *start, size_t length);\n-\n-#else /* NO_MMAP || USE_WIN32_MMAP */\n-\n-#include <sys/mman.h>\n-\n-#endif /* NO_MMAP || USE_WIN32_MMAP */\n-\n #ifdef NO_MMAP\n \n /* This value must be multiple of (pagesize * 2) */\n@@ -858,177 +589,15 @@ int git_munmap(void *start, size_t length);\n \n #endif /* NO_MMAP */\n \n-#ifndef MAP_FAILED\n-#define MAP_FAILED ((void *)-1)\n-#endif\n-\n #ifdef NO_ST_BLOCKS_IN_STRUCT_STAT\n #define on_disk_bytes(st) ((st).st_size)\n #else\n #define on_disk_bytes(st) ((st).st_blocks * 512)\n #endif\n \n-#ifdef NEEDS_MODE_TRANSLATION\n-#undef S_IFMT\n-#undef S_IFREG\n-#undef S_IFDIR\n-#undef S_IFLNK\n-#undef S_IFBLK\n-#undef S_IFCHR\n-#undef S_IFIFO\n-#undef S_IFSOCK\n-#define S_IFMT   0170000\n-#define S_IFREG  0100000\n-#define S_IFDIR  0040000\n-#define S_IFLNK  0120000\n-#define S_IFBLK  0060000\n-#define S_IFCHR  0020000\n-#define S_IFIFO  0010000\n-#define S_IFSOCK 0140000\n-#ifdef stat\n-#undef stat\n-#endif\n-#define stat(path, buf) git_stat(path, buf)\n-int git_stat(const char *, struct stat *);\n-#ifdef fstat\n-#undef fstat\n-#endif\n-#define fstat(fd, buf) git_fstat(fd, buf)\n-int git_fstat(int, struct stat *);\n-#ifdef lstat\n-#undef lstat\n-#endif\n-#define lstat(path, buf) git_lstat(path, buf)\n-int git_lstat(const char *, struct stat *);\n-#endif\n-\n #define DEFAULT_PACKED_GIT_LIMIT \\\n \t((1024L * 1024L) * (size_t)(sizeof(void*) >= 8 ? (32 * 1024L * 1024L) : 256))\n \n-#ifdef NO_PREAD\n-#define pread git_pread\n-ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);\n-#endif\n-\n-#ifdef NO_SETENV\n-#define setenv gitsetenv\n-int gitsetenv(const char *, const char *, int);\n-#endif\n-\n-#ifdef NO_MKDTEMP\n-#define mkdtemp gitmkdtemp\n-char *gitmkdtemp(char *);\n-#endif\n-\n-#ifdef NO_UNSETENV\n-#define unsetenv gitunsetenv\n-int gitunsetenv(const char *);\n-#endif\n-\n-#ifdef NO_STRCASESTR\n-#define strcasestr gitstrcasestr\n-char *gitstrcasestr(const char *haystack, const char *needle);\n-#endif\n-\n-#ifdef NO_STRLCPY\n-#define strlcpy gitstrlcpy\n-size_t gitstrlcpy(char *, const char *, size_t);\n-#endif\n-\n-#ifdef NO_STRTOUMAX\n-#define strtoumax gitstrtoumax\n-uintmax_t gitstrtoumax(const char *, char **, int);\n-#define strtoimax gitstrtoimax\n-intmax_t gitstrtoimax(const char *, char **, int);\n-#endif\n-\n-#ifdef NO_HSTRERROR\n-#define hstrerror githstrerror\n-const char *githstrerror(int herror);\n-#endif\n-\n-#ifdef NO_MEMMEM\n-#define memmem gitmemmem\n-void *gitmemmem(const void *haystack, size_t haystacklen,\n-\t\tconst void *needle, size_t needlelen);\n-#endif\n-\n-#ifdef OVERRIDE_STRDUP\n-#ifdef strdup\n-#undef strdup\n-#endif\n-#define strdup gitstrdup\n-char *gitstrdup(const char *s);\n-#endif\n-\n-#ifdef NO_GETPAGESIZE\n-#define getpagesize() sysconf(_SC_PAGESIZE)\n-#endif\n-\n-#ifndef O_CLOEXEC\n-#define O_CLOEXEC 0\n-#endif\n-\n-#ifdef FREAD_READS_DIRECTORIES\n-# if !defined(SUPPRESS_FOPEN_REDEFINITION)\n-#  ifdef fopen\n-#   undef fopen\n-#  endif\n-#  define fopen(a,b) git_fopen(a,b)\n-# endif\n-FILE *git_fopen(const char*, const char*);\n-#endif\n-\n-#ifdef SNPRINTF_RETURNS_BOGUS\n-#ifdef snprintf\n-#undef snprintf\n-#endif\n-#define snprintf git_snprintf\n-int git_snprintf(char *str, size_t maxsize,\n-\t\t const char *format, ...);\n-#ifdef vsnprintf\n-#undef vsnprintf\n-#endif\n-#define vsnprintf git_vsnprintf\n-int git_vsnprintf(char *str, size_t maxsize,\n-\t\t  const char *format, va_list ap);\n-#endif\n-\n-#ifdef OPEN_RETURNS_EINTR\n-#undef open\n-#define open git_open_with_retry\n-int git_open_with_retry(const char *path, int flag, ...);\n-#endif\n-\n-#ifdef __GLIBC_PREREQ\n-#if __GLIBC_PREREQ(2, 1)\n-#define HAVE_STRCHRNUL\n-#endif\n-#endif\n-\n-#ifndef HAVE_STRCHRNUL\n-#define strchrnul gitstrchrnul\n-static inline char *gitstrchrnul(const char *s, int c)\n-{\n-\twhile (*s && *s != c)\n-\t\ts++;\n-\treturn (char *)s;\n-}\n-#endif\n-\n-#ifdef NO_INET_PTON\n-int inet_pton(int af, const char *src, void *dst);\n-#endif\n-\n-#ifdef NO_INET_NTOP\n-const char *inet_ntop(int af, const void *src, char *dst, size_t size);\n-#endif\n-\n-#ifdef NO_PTHREADS\n-#define atexit git_atexit\n-int git_atexit(void (*handler)(void));\n-#endif\n-\n static inline size_t st_add(size_t a, size_t b)\n {\n \tif (unsigned_add_overflows(a, b))\n@@ -1295,12 +864,6 @@ static inline size_t xsize_t(off_t len)\n \treturn (size_t) len;\n }\n \n-#ifndef HOST_NAME_MAX\n-#define HOST_NAME_MAX 256\n-#endif\n-\n-#include \"sane-ctype.h\"\n-\n /*\n  * Like skip_prefix, but compare case-insensitively. Note that the comparison\n  * is done via tolower(), so it is strictly ASCII (no multi-byte characters or\n@@ -1366,34 +929,6 @@ static inline int strtol_i(char const *s, int base, int *result)\n \treturn 0;\n }\n \n-void git_stable_qsort(void *base, size_t nmemb, size_t size,\n-\t\t      int(*compar)(const void *, const void *));\n-#ifdef INTERNAL_QSORT\n-#define qsort git_stable_qsort\n-#endif\n-\n-#define QSORT(base, n, compar) sane_qsort((base), (n), sizeof(*(base)), compar)\n-static inline void sane_qsort(void *base, size_t nmemb, size_t size,\n-\t\t\t      int(*compar)(const void *, const void *))\n-{\n-\tif (nmemb > 1)\n-\t\tqsort(base, nmemb, size, compar);\n-}\n-\n-#define STABLE_QSORT(base, n, compar) \\\n-\tgit_stable_qsort((base), (n), sizeof(*(base)), compar)\n-\n-#ifndef HAVE_ISO_QSORT_S\n-int git_qsort_s(void *base, size_t nmemb, size_t size,\n-\t\tint (*compar)(const void *, const void *, void *), void *ctx);\n-#define qsort_s git_qsort_s\n-#endif\n-\n-#define QSORT_S(base, n, compar, ctx) do {\t\t\t\\\n-\tif (qsort_s((base), (n), sizeof(*(base)), compar, ctx))\t\\\n-\t\tBUG(\"qsort_s() failed\");\t\t\t\\\n-} while (0)\n-\n #ifndef REG_STARTEND\n #error \"Git requires REG_STARTEND support. Compile with NO_REGEX=NeedsStartEnd\"\n #endif\n@@ -1418,39 +953,12 @@ int git_regcomp(regex_t *preg, const char *pattern, int cflags);\n # define FORCE_DIR_SET_GID 0\n #endif\n \n-#ifdef NO_NSEC\n-#undef USE_NSEC\n-#define ST_CTIME_NSEC(st) 0\n-#define ST_MTIME_NSEC(st) 0\n-#else\n-#ifdef USE_ST_TIMESPEC\n-#define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctimespec.tv_nsec))\n-#define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtimespec.tv_nsec))\n-#else\n-#define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctim.tv_nsec))\n-#define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtim.tv_nsec))\n-#endif\n-#endif\n-\n #ifdef UNRELIABLE_FSTAT\n #define fstat_is_reliable() 0\n #else\n #define fstat_is_reliable() 1\n #endif\n \n-#ifndef va_copy\n-/*\n- * Since an obvious implementation of va_list would be to make it a\n- * pointer into the stack frame, a simple assignment will work on\n- * many systems.  But let's try to be more portable.\n- */\n-#ifdef __va_copy\n-#define va_copy(dst, src) __va_copy(dst, src)\n-#else\n-#define va_copy(dst, src) ((dst) = (src))\n-#endif\n-#endif\n-\n /* usage.c: only to be used for testing BUG() implementation (see test-tool) */\n extern int BUG_exit_code;\n \n@@ -1480,41 +988,6 @@ void bug_fl(const char *file, int line, const char *fmt, ...);\n # define SHELL_PATH \"/bin/sh\"\n #endif\n \n-#ifndef _POSIX_THREAD_SAFE_FUNCTIONS\n-static inline void git_flockfile(FILE *fh UNUSED)\n-{\n-\t; /* nothing */\n-}\n-static inline void git_funlockfile(FILE *fh UNUSED)\n-{\n-\t; /* nothing */\n-}\n-#undef flockfile\n-#undef funlockfile\n-#undef getc_unlocked\n-#define flockfile(fh) git_flockfile(fh)\n-#define funlockfile(fh) git_funlockfile(fh)\n-#define getc_unlocked(fh) getc(fh)\n-#endif\n-\n-#ifdef FILENO_IS_A_MACRO\n-int git_fileno(FILE *stream);\n-# ifndef COMPAT_CODE_FILENO\n-#  undef fileno\n-#  define fileno(p) git_fileno(p)\n-# endif\n-#endif\n-\n-#ifdef NEED_ACCESS_ROOT_HANDLER\n-int git_access(const char *path, int mode);\n-# ifndef COMPAT_CODE_ACCESS\n-#  ifdef access\n-#  undef access\n-#  endif\n-#  define access(path, mode) git_access(path, mode)\n-# endif\n-#endif\n-\n /*\n  * Our code often opens a path to an optional file, to work on its\n  * contents when we can successfully open it.  We can ignore a failure\n\n-- \n2.48.1.666.gff9fcf71b7.dirty\n\n"},{"id":"512582","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-17-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"[PATCH v6 17/18] reftable: decouple from Git codebase by pulling in \"compat/posix.h\"","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:53Z","receivedAt":"2025-02-18T09:22:13Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"The reftable library includes \"git-compat-util.h\" in order to get a\nPOSIX-like programming environment that papers over various differences\nbetween platforms. The header also brings with it a couple of helpers\nspecific to the Git codebase though, and over time we have started to\nuse these helpers in the reftable library, as well.\n\nThis makes it very hard to use the reftable library as a standalone\nlibrary without the rest of the Git codebase, so other libraries like\ne.g. libgit2 cannot easily use it. But now that we have removed all\ncalls to Git-specific functionality and have split out \"compat/posix.h\"\nas a separate header we can address this.\n\nStop including \"git-compat-util.h\" and instead include \"compat/posix.h\"\nto finalize the decoupling of the reftable library from the rest of the\nGit codebase. The only bits which remain specific to Git are \"system.h\"\nand \"system.c\", which projects will have to provide.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reftable/system.c | 2 ++\n reftable/system.h | 2 +-\n 2 files changed, 3 insertions(+), 1 deletion(-)\n\ndiff --git a/reftable/system.c b/reftable/system.c\nindex e25ccc0da3c..1ee268b125d 100644\n--- a/reftable/system.c\n+++ b/reftable/system.c\n@@ -1,3 +1,5 @@\n+#include \"../git-compat-util.h\"\n+\n #include \"system.h\"\n #include \"basics.h\"\n #include \"reftable-error.h\"\ndiff --git a/reftable/system.h b/reftable/system.h\nindex bb6a7e6285b..10055fbff2d 100644\n--- a/reftable/system.h\n+++ b/reftable/system.h\n@@ -11,7 +11,7 @@ license that can be found in the LICENSE file or at\n \n /* This header glues the reftable library to the rest of Git */\n \n-#include \"git-compat-util.h\"\n+#include \"compat/posix.h\"\n #include \"compat/zlib-compat.h\"\n \n /*\n\n-- \n2.48.1.666.gff9fcf71b7.dirty\n\n"},{"id":"512583","messageId":"20250218-pks-reftable-drop-git-compat-util-v6-18-8c1f39fb4c02@pks.im","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"[PATCH v6 18/18] Makefile: skip reftable library for Coccinelle","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-02-18T09:20:54Z","receivedAt":"2025-02-18T09:22:15Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"The reftable library does not use any of the common helpers that the Git\nproject has. Consequently, most of the rules that we have in Coccinelle\ndo not apply to the library at all and may even generate false positives\nwhen a pattern can be converted to use a Git helper function.\n\nExclude reftable library sources from being checked by Coccinelle to\navoid such false positives.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n Makefile | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/Makefile b/Makefile\nindex cd0806e1e69..83f1df5502b 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -955,7 +955,7 @@ FOUND_SOURCE_FILES := $(filter-out $(GENERATED_H),$(shell $(SOURCES_CMD)))\n FOUND_C_SOURCES = $(filter %.c,$(FOUND_SOURCE_FILES))\n FOUND_H_SOURCES = $(filter %.h,$(FOUND_SOURCE_FILES))\n \n-COCCI_SOURCES = $(filter-out $(THIRD_PARTY_SOURCES),$(FOUND_C_SOURCES))\n+COCCI_SOURCES = $(filter-out $(THIRD_PARTY_SOURCES) reftable/%,$(FOUND_C_SOURCES))\n \n LIB_H = $(FOUND_H_SOURCES)\n \n\n-- \n2.48.1.666.gff9fcf71b7.dirty\n\n"},{"id":"512632","messageId":"xmqqjz9nkrqt.fsf@gitster.g","threadId":"62969","inReplyTo":"20250218-pks-reftable-drop-git-compat-util-v6-0-8c1f39fb4c02@pks.im","subject":"Re: [PATCH v6 00/18] reftable: stop using \"git-compat-util.h\"","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-02-18T18:55:06Z","receivedAt":"2025-02-18T18:55:09Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> Changes in v6:\n>   - I have rebased the patch series on top of \"master\" at 03944513488\n>     (The eleventh batch, 2025-02-14). On the one hand this gets rid of\n>     the conflict with the zlib-ng changes, which have meanwhile been\n>     merged to that branch. And on the other hand it pulls in the new CI\n>     build for MSVC+Meson, which allows me to have better test coverage\n>     on Windows to better detect breakage there.\n>   - Stop moving around Win32 files so that we do the minimum viable\n>     change in this context. Hopefully, this makes it easier for Dscho to\n>     integrate into Git for Windows. If it doesn't I don't really have\n>     any other good ideas for how to do it, so the only alternative would\n>     be to just ignore Windows altogether.\n>   - Link to v5: https://lore.kernel.org/r/20250207-pks-reftable-drop-git-compat-util-v5-0-ba2adc79110f@pks.im\n\nThanks.  I think the \"unlink\" thing on Windows I have was queued\ndepended on the previous iteration of this series so let me make\nsure that I'd unentangle it while replacing the topic with this\nround.\n\nWill queue.  Thanks.\n"},{"id":"514015","messageId":"xmqqa59r2lk8.fsf@gitster.g","threadId":"62969","inReplyTo":"xmqqjz9nkrqt.fsf@gitster.g","subject":"Re: [PATCH v6 00/18] reftable: stop using \"git-compat-util.h\"","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-03-11T23:29:27Z","receivedAt":"2025-03-11T23:29:30Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Patrick Steinhardt <ps@pks.im> writes:\n>\n>> Changes in v6:\n>>   - I have rebased the patch series on top of \"master\" at 03944513488\n>>     (The eleventh batch, 2025-02-14). On the one hand this gets rid of\n>>     the conflict with the zlib-ng changes, which have meanwhile been\n>>     merged to that branch. And on the other hand it pulls in the new CI\n>>     build for MSVC+Meson, which allows me to have better test coverage\n>>     on Windows to better detect breakage there.\n>>   - Stop moving around Win32 files so that we do the minimum viable\n>>     change in this context. Hopefully, this makes it easier for Dscho to\n>>     integrate into Git for Windows. If it doesn't I don't really have\n>>     any other good ideas for how to do it, so the only alternative would\n>>     be to just ignore Windows altogether.\n>>   - Link to v5: https://lore.kernel.org/r/20250207-pks-reftable-drop-git-compat-util-v5-0-ba2adc79110f@pks.im\n>\n> Thanks.  I think the \"unlink\" thing on Windows I have was queued\n> depended on the previous iteration of this series so let me make\n> sure that I'd unentangle it while replacing the topic with this\n> round.\n>\n> Will queue.  Thanks.\n\nSo, sorry I lost track.  I think I saw you ask Dscho about the\nWindows specific bits once in a thread on What's cooking report,\nbut I do not recall the outcome.  Is this topic (and the unlink fix\nthat depends on this series) now OK, or would we see further updates\nonce 2.49 is tagged?\n\nThanks.\n"},{"id":"514022","messageId":"Z9Eu_fZbI8ndkzHw@pks.im","threadId":"62969","inReplyTo":"xmqqa59r2lk8.fsf@gitster.g","subject":"Re: [PATCH v6 00/18] reftable: stop using \"git-compat-util.h\"","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-03-12T06:51:41Z","receivedAt":"2025-03-12T06:51:51Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Tue, Mar 11, 2025 at 04:29:27PM -0700, Junio C Hamano wrote:\n> Junio C Hamano <gitster@pobox.com> writes:\n> \n> > Patrick Steinhardt <ps@pks.im> writes:\n> >\n> >> Changes in v6:\n> >>   - I have rebased the patch series on top of \"master\" at 03944513488\n> >>     (The eleventh batch, 2025-02-14). On the one hand this gets rid of\n> >>     the conflict with the zlib-ng changes, which have meanwhile been\n> >>     merged to that branch. And on the other hand it pulls in the new CI\n> >>     build for MSVC+Meson, which allows me to have better test coverage\n> >>     on Windows to better detect breakage there.\n> >>   - Stop moving around Win32 files so that we do the minimum viable\n> >>     change in this context. Hopefully, this makes it easier for Dscho to\n> >>     integrate into Git for Windows. If it doesn't I don't really have\n> >>     any other good ideas for how to do it, so the only alternative would\n> >>     be to just ignore Windows altogether.\n> >>   - Link to v5: https://lore.kernel.org/r/20250207-pks-reftable-drop-git-compat-util-v5-0-ba2adc79110f@pks.im\n> >\n> > Thanks.  I think the \"unlink\" thing on Windows I have was queued\n> > depended on the previous iteration of this series so let me make\n> > sure that I'd unentangle it while replacing the topic with this\n> > round.\n> >\n> > Will queue.  Thanks.\n> \n> So, sorry I lost track.  I think I saw you ask Dscho about the\n> Windows specific bits once in a thread on What's cooking report,\n> but I do not recall the outcome.  Is this topic (and the unlink fix\n> that depends on this series) now OK, or would we see further updates\n> once 2.49 is tagged?\n\nThere was a class of test failures caused by mismatching allocators in\nGit for Windows for which I've sent a proposed fix for downstream [1].\nI've been talking to Johannes on Monday and he mentioned that he'll\nlikely end up taking that patch as the pragmatic fix. Which to the best\nof my knowledge would mean that this series here is unblocked and can\nstart to move forward again. Johannes, please correct me in case I'm\nmisstating anything here.\n\nSo based on that I don't plan to reroll and hope that we can move\nforward with the series once Git v2.49.0 is out.\n\nThanks!\n\nPatrick\n\n[1]: <Z8bQsflIqzZd3AMs@pks.im>\n"},{"id":"514770","messageId":"Z9wxki6ES7ZRN7Z9@pks.im","threadId":"62969","inReplyTo":"Z9Eu_fZbI8ndkzHw@pks.im","subject":"Re: [PATCH v6 00/18] reftable: stop using \"git-compat-util.h\"","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-03-20T15:17:38Z","receivedAt":"2025-03-20T15:17:44Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Wed, Mar 12, 2025 at 07:51:41AM +0100, Patrick Steinhardt wrote:\n> On Tue, Mar 11, 2025 at 04:29:27PM -0700, Junio C Hamano wrote:\n> > Junio C Hamano <gitster@pobox.com> writes:\n> > \n> > > Patrick Steinhardt <ps@pks.im> writes:\n> > >\n> > >> Changes in v6:\n> > >>   - I have rebased the patch series on top of \"master\" at 03944513488\n> > >>     (The eleventh batch, 2025-02-14). On the one hand this gets rid of\n> > >>     the conflict with the zlib-ng changes, which have meanwhile been\n> > >>     merged to that branch. And on the other hand it pulls in the new CI\n> > >>     build for MSVC+Meson, which allows me to have better test coverage\n> > >>     on Windows to better detect breakage there.\n> > >>   - Stop moving around Win32 files so that we do the minimum viable\n> > >>     change in this context. Hopefully, this makes it easier for Dscho to\n> > >>     integrate into Git for Windows. If it doesn't I don't really have\n> > >>     any other good ideas for how to do it, so the only alternative would\n> > >>     be to just ignore Windows altogether.\n> > >>   - Link to v5: https://lore.kernel.org/r/20250207-pks-reftable-drop-git-compat-util-v5-0-ba2adc79110f@pks.im\n> > >\n> > > Thanks.  I think the \"unlink\" thing on Windows I have was queued\n> > > depended on the previous iteration of this series so let me make\n> > > sure that I'd unentangle it while replacing the topic with this\n> > > round.\n> > >\n> > > Will queue.  Thanks.\n> > \n> > So, sorry I lost track.  I think I saw you ask Dscho about the\n> > Windows specific bits once in a thread on What's cooking report,\n> > but I do not recall the outcome.  Is this topic (and the unlink fix\n> > that depends on this series) now OK, or would we see further updates\n> > once 2.49 is tagged?\n> \n> There was a class of test failures caused by mismatching allocators in\n> Git for Windows for which I've sent a proposed fix for downstream [1].\n> I've been talking to Johannes on Monday and he mentioned that he'll\n> likely end up taking that patch as the pragmatic fix. Which to the best\n> of my knowledge would mean that this series here is unblocked and can\n> start to move forward again. Johannes, please correct me in case I'm\n> misstating anything here.\n> \n> So based on that I don't plan to reroll and hope that we can move\n> forward with the series once Git v2.49.0 is out.\n\nQuick status update: Johannes mentioned in [1] that he is fine with\nmoving forward with this topic now. Thanks!\n\nPatrick\n\n[1]: https://github.com/gitgitgadget/git/pull/1874#issuecomment-2740342672\n"}]}