Hash :
3f3d9b41
Author :
Date :
2012-04-14T23:27:45
Large File Support for native Windows platforms. * m4/largefile.m4 (gl_LARGEFILE): New macro. * modules/largefile (configure.ac): Require gl_LARGEFILE. * lib/sys_types.in.h (off_t) [WINDOWS_64_BIT_OFF_T]: Define to a 64-bit type. * m4/sys_types_h.m4 (gl_SYS_TYPES_H): Set WINDOWS_64_BIT_OFF_T. * modules/sys_types (Makefile.am): Substitute WINDOWS_64_BIT_OFF_T. * doc/posix-headers/sys_types.texi: Mention the effect of the 'largefile' module. * lib/fcntl.in.h: Add comments about off_t. * modules/fcntl-h (Depends-on): Add sys_types. * lib/unistd.in.h [WINDOWS_64_BIT_OFF_T]: Include <sys/types.h>. (ftruncate): Replace it if REPLACE_FTRUNCATE is 1. * m4/unistd_h.m4 (gl_UNISTD_H): Require gl_SYS_TYPES_H. (gl_UNISTD_H_DEFAULTS): Initialize REPLACE_FTRUNCATE. * modules/unistd (Depends-on): Add sys_types. (Makefile.am): Substitute WINDOWS_64_BIT_OFF_T, REPLACE_FTRUNCATE. * lib/lseek.c (rpl_lseek) [_GL_WINDOWS_64_BIT_OFF_T]: Use _lseeki64 instead of lseek. * m4/lseek.m4 (gl_FUNC_LSEEK): Require gl_SYS_TYPES_H. Set REPLACE_LSEEK if WINDOWS_64_BIT_OFF_T is 1. * modules/lseek (Depends-on): Add sys_types. * lib/ftruncate.c: Put under GPLv3+. Include <windows.h>, msvc-nothrow.h. (SetFileSize): New function. (ftruncate) [_GL_WINDOWS_64_BIT_OFF_T]: New implementation. * m4/ftruncate.m4 (gl_FUNC_FTRUNCATE): Set REPLACE_FTRUNCATE on Windows if Large File Support is requested. * modules/ftruncate (configure.ac): Consider REPLACE_FTRUNCATE. (Depends-on): Add sys_types, msvc-nothrow. Update conditions. * lib/stdio.in.h: Add comments about off_t. * modules/stdio (Depends-on): Add sys_types. * lib/ftello.c [_GL_WINDOWS_64_BIT_OFF_T]: Use _ftelli64 or ftello64 instead of ftello. * m4/ftello.m4 (gl_FUNC_FTELLO): Require gl_SYS_TYPES_H. Set REPLACE_FTELLO if WINDOWS_64_BIT_OFF_T is 1. (gl_PREREQ_FTELLO): New macro. * modules/ftello (Depends-on): Add sys_types. (configure.ac): Incoke gl_PREREQ_FTELLO. * lib/fseeko.c [_GL_WINDOWS_64_BIT_OFF_T]: Use _fseeki64 or fseeko64 instead of fseeko. * m4/fseeko.m4 (gl_FUNC_FSEEKO): Require gl_SYS_TYPES_H. Set REPLACE_FSEEKO if WINDOWS_64_BIT_OFF_T is 1. (gl_PREREQ_FSEEKO): New macro. * modules/fseeko (Depends-on): Add sys_types. (configure.ac): Invoke gl_PREREQ_FSEEKO. * lib/sys_stat.in.h: Add comments about off_t. (stat, fstat) [WINDOWS_64_BIT_ST_SIZE]: Define to variants that use a 64-bit integer for st_size in 'struct stat'. * m4/sys_stat_h.m4 (gl_HEADER_SYS_STAT_H): Set WINDOWS_64_BIT_ST_SIZE. Define _GL_WINDOWS_64_BIT_ST_SIZE. * modules/sys_stat (Depends-on): Add sys_types. (Makefile.am): Substitute WINDOWS_64_BIT_ST_SIZE. * lib/stat.c (stat) [_GL_WINDOWS_64_BIT_ST_SIZE]: Define to _stati64 instead of stat or _stat. * lib/fstat.c [_GL_WINDOWS_64_BIT_ST_SIZE]: Use _fstati64 and 'struct _stati64' instead of fstat and 'struct stat'. * m4/fstat.m4 (gl_FUNC_FSTAT): Require gl_HEADER_SYS_STAT_H. Set REPLACE_FSTAT if WINDOWS_64_BIT_ST_SIZE is 1. Reported by Ray Satiro <raysatiro@yahoo.com>.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
/* ftruncate emulations for native Windows.
Copyright (C) 1992-2012 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
/* Specification. */
#include <unistd.h>
#if HAVE_CHSIZE
/* A native Windows platform. */
# include <errno.h>
# if _GL_WINDOWS_64_BIT_OFF_T
/* Large File Support: off_t is 64-bit, but chsize() takes only a 32-bit
argument. So, define a 64-bit safe SetFileSize function ourselves. */
/* Ensure that <windows.h> declares GetFileSizeEx. */
# undef _WIN32_WINNT
# define _WIN32_WINNT 0x500
/* Get declarations of the native Windows API functions. */
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
/* Get _get_osfhandle. */
# include "msvc-nothrow.h"
static BOOL
SetFileSize (HANDLE h, LONGLONG size)
{
LARGE_INTEGER old_size;
if (!GetFileSizeEx (h, &old_size))
return FALSE;
if (size != old_size.QuadPart)
{
/* Duplicate the handle, so we are free to modify its file position. */
HANDLE curr_process = GetCurrentProcess ();
HANDLE tmph;
if (!DuplicateHandle (curr_process, /* SourceProcessHandle */
h, /* SourceHandle */
curr_process, /* TargetProcessHandle */
(PHANDLE) &tmph, /* TargetHandle */
(DWORD) 0, /* DesiredAccess */
FALSE, /* InheritHandle */
DUPLICATE_SAME_ACCESS)) /* Options */
return FALSE;
if (size < old_size.QuadPart)
{
/* Reduce the size. */
LONG size_hi = (LONG) (size >> 32);
if (SetFilePointer (tmph, (LONG) size, &size_hi, FILE_BEGIN)
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR)
{
CloseHandle (tmph);
return FALSE;
}
if (!SetEndOfFile (tmph))
{
CloseHandle (tmph);
return FALSE;
}
}
else
{
/* Increase the size by adding zero bytes at the end. */
static char zero_bytes[1024];
LONG pos_hi = 0;
LONG pos_lo = SetFilePointer (tmph, (LONG) 0, &pos_hi, FILE_END);
LONGLONG pos;
if (pos_lo == INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR)
{
CloseHandle (tmph);
return FALSE;
}
pos = ((LONGLONG) pos_hi << 32) | (ULONGLONG) (ULONG) pos_lo;
while (pos < size)
{
DWORD written;
LONGLONG count = size - pos;
if (count > sizeof (zero_bytes))
count = sizeof (zero_bytes);
if (!WriteFile (tmph, zero_bytes, (DWORD) count, &written, NULL)
|| written == 0)
{
CloseHandle (tmph);
return FALSE;
}
pos += (ULONGLONG) (ULONG) written;
}
}
/* Close the handle. */
CloseHandle (tmph);
}
return TRUE;
}
int
ftruncate (int fd, off_t length)
{
HANDLE handle = (HANDLE) _get_osfhandle (fd);
if (handle == INVALID_HANDLE_VALUE)
{
errno = EBADF;
return -1;
}
if (length < 0)
{
errno = EINVAL;
return -1;
}
if (!SetFileSize (handle, length))
{
switch (GetLastError ())
{
case ERROR_ACCESS_DENIED:
errno = EACCES;
break;
case ERROR_HANDLE_DISK_FULL:
case ERROR_DISK_FULL:
case ERROR_DISK_TOO_FRAGMENTED:
errno = ENOSPC;
break;
default:
errno = EIO;
break;
}
return -1;
}
return 0;
}
# else
# include <io.h>
# if HAVE_MSVC_INVALID_PARAMETER_HANDLER
# include "msvc-inval.h"
static inline int
chsize_nothrow (int fd, long length)
{
int result;
TRY_MSVC_INVAL
{
result = chsize (fd, length);
}
CATCH_MSVC_INVAL
{
result = -1;
errno = EBADF;
}
DONE_MSVC_INVAL;
return result;
}
# define chsize chsize_nothrow
# endif
int
ftruncate (int fd, off_t length)
{
return chsize (fd, length);
}
# endif
#endif