1 /* ftruncate emulations for native Windows.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, see <http://www.gnu.org/licenses/>. */
23 /* A native Windows platform. */
27 # if _GL_WINDOWS_64_BIT_OFF_T
29 /* Large File Support: off_t is 64-bit, but chsize() takes only a 32-bit
30 argument. So, define a 64-bit safe SetFileSize function ourselves. */
32 /* Ensure that <windows.h> declares GetFileSizeEx. */
34 # define _WIN32_WINNT 0x500
36 /* Get declarations of the native Windows API functions. */
37 # define WIN32_LEAN_AND_MEAN
40 /* Get _get_osfhandle. */
41 # include "msvc-nothrow.h"
44 SetFileSize (HANDLE h, LONGLONG size)
46 LARGE_INTEGER old_size;
48 if (!GetFileSizeEx (h, &old_size))
51 if (size != old_size.QuadPart)
53 /* Duplicate the handle, so we are free to modify its file position. */
54 HANDLE curr_process = GetCurrentProcess ();
57 if (!DuplicateHandle (curr_process, /* SourceProcessHandle */
59 curr_process, /* TargetProcessHandle */
60 (PHANDLE) &tmph, /* TargetHandle */
61 (DWORD) 0, /* DesiredAccess */
62 FALSE, /* InheritHandle */
63 DUPLICATE_SAME_ACCESS)) /* Options */
66 if (size < old_size.QuadPart)
68 /* Reduce the size. */
69 LONG size_hi = (LONG) (size >> 32);
70 if (SetFilePointer (tmph, (LONG) size, &size_hi, FILE_BEGIN)
71 == INVALID_SET_FILE_POINTER
72 && GetLastError() != NO_ERROR)
77 if (!SetEndOfFile (tmph))
85 /* Increase the size by adding zero bytes at the end. */
86 static char zero_bytes[1024];
88 LONG pos_lo = SetFilePointer (tmph, (LONG) 0, &pos_hi, FILE_END);
90 if (pos_lo == INVALID_SET_FILE_POINTER
91 && GetLastError() != NO_ERROR)
96 pos = ((LONGLONG) pos_hi << 32) | (ULONGLONG) (ULONG) pos_lo;
100 LONGLONG count = size - pos;
101 if (count > sizeof (zero_bytes))
102 count = sizeof (zero_bytes);
103 if (!WriteFile (tmph, zero_bytes, (DWORD) count, &written, NULL)
109 pos += (ULONGLONG) (ULONG) written;
112 /* Close the handle. */
119 ftruncate (int fd, off_t length)
121 HANDLE handle = (HANDLE) _get_osfhandle (fd);
123 if (handle == INVALID_HANDLE_VALUE)
133 if (!SetFileSize (handle, length))
135 switch (GetLastError ())
137 case ERROR_ACCESS_DENIED:
140 case ERROR_HANDLE_DISK_FULL:
141 case ERROR_DISK_FULL:
142 case ERROR_DISK_TOO_FRAGMENTED:
158 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
159 # include "msvc-inval.h"
161 chsize_nothrow (int fd, long length)
167 result = chsize (fd, length);
178 # define chsize chsize_nothrow
182 ftruncate (int fd, off_t length)
184 return chsize (fd, length);