osdep/io: add ftruncate for win32

Trivial wrapper around _chsize_s() to return -1 on errors always.
_chsize_s() returns errno value. At the same time errno is set to the
same value.

FILE_WRITE_DATA has to be added in append mode, because _chsize_s()
requires write permissions, not only append. We lose atomic append, but
we follow POSIX here, so that's fine, I guess.
This commit is contained in:
Kacper Michajłow committed 2025-01-26 22:10:00 +01:00
1 parent 98627b027d
commit b01e1ab61a
2 files changed
+14 -1

No files matched your search

+9 -1
View File
@@ -369,8 +369,9 @@ int mp_open(const char *filename, int oflag, ...)
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
// Setting FILE_APPEND_DATA and avoiding GENERIC_WRITE/FILE_WRITE_DATA
// will make the file handle use atomic append behavior
// However to implement ftruncate we need FILE_WRITE_DATA
static const DWORD append =
FILE_APPEND_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA;
FILE_APPEND_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_WRITE_DATA;
DWORD access = 0;
DWORD disposition = 0;
@@ -709,6 +710,13 @@ off_t mp_lseek64(int fd, off_t offset, int whence)
return _lseeki64(fd, offset, whence);
}
int mp_ftruncate64(int fd, off_t length)
{
if (_chsize_s(fd, length) == 0)
return 0;
return -1;
}
_Thread_local
static struct {
DWORD errcode;
+5
View File
@@ -100,6 +100,7 @@ char *mp_to_utf8(void *talloc_ctx, const wchar_t *s);
#include "dirent-win.h"
#else
#include <dirent.h>
#include <unistd.h>
#endif
#ifdef _WIN32
@@ -133,6 +134,7 @@ char ***mp_penviron(void);
#undef off_t
#define off_t int64_t
off_t mp_lseek64(int fd, off_t offset, int whence);
int mp_ftruncate64(int fd, off_t length);
void *mp_dlopen(const char *filename, int flag);
void *mp_dlsym(void *handle, const char *symbol);
char *mp_dlerror(void);
@@ -198,6 +200,9 @@ void mp_globfree(mp_glob_t *pglob);
#undef lseek
#define lseek(...) mp_lseek64(__VA_ARGS__)
#undef ftruncate
#define ftruncate(...) mp_ftruncate64(__VA_ARGS__)
#define RTLD_NOW 0
#define RTLD_LOCAL 0
#define dlopen(fn,fg) mp_dlopen((fn), (fg))