From 3e5dc499a8b5629154c791f1497b146d102902aa Mon Sep 17 00:00:00 2001 From: usa Date: Mon, 2 May 2016 14:05:19 +0000 Subject: * win32/win32.c, include/ruby/win32.h (rb_w32_utruncate): implements new truncate alternative which accepts UTF-8 path. * file.c (truncate): use above function. [Bug #12340] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54889 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- win32/win32.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'win32/win32.c') diff --git a/win32/win32.c b/win32/win32.c index eb32b35d43..8ca3dbf979 100644 --- a/win32/win32.c +++ b/win32/win32.c @@ -5702,21 +5702,41 @@ rb_chsize(HANDLE h, off_t size) } /* License: Ruby's */ -int -rb_w32_truncate(const char *path, off_t length) +static int +w32_truncate(const char *path, off_t length, UINT cp) { HANDLE h; int ret; - h = CreateFile(path, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); + WCHAR *wpath; + + if (!(wpath = mbstr_to_wstr(cp, path, -1, NULL))) + return -1; + h = CreateFileW(wpath, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); if (h == INVALID_HANDLE_VALUE) { errno = map_errno(GetLastError()); + free(wpath); return -1; } + free(wpath); ret = rb_chsize(h, length); CloseHandle(h); return ret; } +/* License: Ruby's */ +int +rb_w32_utruncate(const char *path, off_t length) +{ + return w32_truncate(path, length, CP_UTF8); +} + +/* License: Ruby's */ +int +rb_w32_truncate(const char *path, off_t length) +{ + return w32_truncate(path, length, filecp()); +} + /* License: Ruby's */ int rb_w32_ftruncate(int fd, off_t length) -- cgit v1.2.3