git/compat
Johannes Schindelin 8ea69373a4 compat/msvc: use _chsize_s for ftruncate
On Windows, `unsigned long` and `long` are 32 bits even on 64-bit
builds. The MSVC compatibility header has shimmed `ftruncate()` with

	#define ftruncate _chsize

ever since `compat/msvc-posix.h` was introduced. `_chsize()` takes a
32-bit `long` for the new length, which silently truncates files (and
the requested size) to 2 GiB. That is enough to make t7508 test 126
"git add fails gracefully with 4 GiB and 8 GiB files" fail under
MSVC: `test-tool truncate` creates a sparse 4 GiB or 8 GiB file via
the shimmed `ftruncate()`, and the test never gets off the ground.

`_chsize_s()` is the modern replacement, accepts a 64-bit `__int64`
length, and is the only sensible target on Windows. The catch is that
it does not follow the POSIX `-1` + `errno` convention: it returns
`0` on success and an errno value (a small positive integer) on
failure. A plain `#define ftruncate _chsize_s` would therefore
silently break callers that test the return value as `< 0` or against
`-1`, of which there are several: `http.c`, `parallel-checkout.c`,
and `t/helper/test-truncate.c` among them.

Introduce a `static inline` wrapper that calls `_chsize_s()`, copies
its errno return into `errno`, and translates the result to the
familiar `-1` / `0` convention, then point `ftruncate` at the
wrapper. Place the wrapper after `#include "mingw-posix.h"` so the
`off_t` parameter resolves to the already-widened `off64_t` rather
than the 32-bit `_off_t` from `compat/vcbuild/include/unistd.h`.

MinGW is unaffected: its `ftruncate()` already takes `off_t` and
routes through `ftruncate64()` when `_FILE_OFFSET_BITS=64`, which is
the default in our build.

Assisted-by: Opus 4.7
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2026-06-15 07:45:40 -07:00
..
darwin
fsmonitor fsmonitor: implement filesystem change listener for Linux 2026-04-15 08:44:33 -07:00
linux
poll unify and bump _WIN32_WINNT definition to Windows 8.1 2026-04-06 10:06:20 -07:00
regex
simple-ipc
stub
vcbuild
win32 Merge branch 'pt/fsmonitor-linux' 2026-05-31 10:00:38 +09:00
.gitattributes
access.c
apple-common-crypto.h
basename.c
bswap.h
compiler.h
disk.h
fileno.c
fopen.c
hstrerror.c
inet_ntop.c
inet_pton.c
memmem.c
mingw-posix.h
mingw.c mingw: optionally use legacy (non-POSIX) delete semantics 2026-05-08 09:53:12 +09:00
mingw.h
mkdir.c
mmap.c
msvc-posix.h compat/msvc: use _chsize_s for ftruncate 2026-06-15 07:45:40 -07:00
msvc.c
msvc.h
nonblock.c
nonblock.h
obstack.c
obstack.h
open.c
posix.h Revert "compat/posix: introduce writev(3p) wrapper" 2026-04-09 14:48:24 -07:00
pread.c
precompose_utf8.c
precompose_utf8.h
qsort_s.c
regcomp_enhanced.c regexp: leave a pointer to resurrect workaround for Homebrew 2026-03-25 15:04:30 -07:00
setenv.c
sha1-chunked.c
sha1-chunked.h
snprintf.c
stat.c
strcasestr.c
strdup.c
strlcpy.c
strtoimax.c
strtoumax.c
terminal.c
terminal.h
unsetenv.c
win32.h
win32mmap.c
winansi.c compat/winansi: drop pre-Vista workaround 2026-04-06 10:06:22 -07:00
zlib-compat.h test-tool: add a helper to synthesize large packfiles 2026-05-09 11:25:32 +09:00