mirror of
https://github.com/git-for-windows/git.git
synced 2026-07-06 01:43:36 -05:00
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>
56 lines
1.5 KiB
C
56 lines
1.5 KiB
C
#ifndef COMPAT_MSVC_POSIX_H
|
|
#define COMPAT_MSVC_POSIX_H
|
|
|
|
#include <direct.h>
|
|
#include <process.h>
|
|
#include <malloc.h>
|
|
#include <io.h>
|
|
|
|
#pragma warning(disable: 4018) /* signed/unsigned comparison */
|
|
#pragma warning(disable: 4244) /* type conversion, possible loss of data */
|
|
#pragma warning(disable: 4090) /* 'function' : different 'const' qualifiers (ALLOC_GROW etc.)*/
|
|
|
|
/* porting function */
|
|
#define inline __inline
|
|
#define __inline__ __inline
|
|
#define __attribute__(x)
|
|
#define strcasecmp _stricmp
|
|
#define strncasecmp _strnicmp
|
|
#define strtoull _strtoui64
|
|
#define strtoll _strtoi64
|
|
|
|
#undef ERROR
|
|
|
|
#define ftello _ftelli64
|
|
|
|
typedef int sigset_t;
|
|
/* open for reading, writing, or both (not in fcntl.h) */
|
|
#define O_ACCMODE (_O_RDONLY | _O_WRONLY | _O_RDWR)
|
|
|
|
#include "mingw-posix.h"
|
|
|
|
/*
|
|
* MSVC's `_chsize()` takes a 32-bit `long` and silently truncates files
|
|
* to 2 GiB. `_chsize_s()` accepts a 64-bit length but returns 0 on
|
|
* success or an errno value on failure, rather than the -1/errno
|
|
* convention POSIX `ftruncate()` callers expect. Wrap it so callers
|
|
* that test the return value as `< 0` or against `-1` keep working.
|
|
*
|
|
* Note: this declaration must follow `#include "mingw-posix.h"` so
|
|
* `off_t` resolves to `off64_t` and the parameter type matches the
|
|
* underlying `_chsize_s()` width.
|
|
*/
|
|
static inline int msvc_ftruncate(int fd, off_t length)
|
|
{
|
|
int err = _chsize_s(fd, length);
|
|
|
|
if (err) {
|
|
errno = err;
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
#define ftruncate msvc_ftruncate
|
|
|
|
#endif /* COMPAT_MSVC_POSIX_H */
|