• R/O
  • HTTP
  • SSH
  • HTTPS

mingw-org-wsl: 提交

The MinGW.org Windows System Libraries


Commit MetaInfo

修訂9b6ff66dfd09e0a002fbacc1edacf4ec1c99258a (tree)
時間2020-01-18 21:28:21
作者Keith Marshall <keith@user...>
CommiterKeith Marshall

Log Message

Support GCC-9.x gratuitous dependency on ftruncate64() function.

Change Summary

差異

--- a/mingwrt/ChangeLog
+++ b/mingwrt/ChangeLog
@@ -1,5 +1,18 @@
11 2020-01-17 Keith Marshall <keith@users.osdn.me>
22
3+ Support GCC-9.x gratuitous dependency on ftruncate64() function.
4+
5+ * include/unistd.h (ftruncate64): Declare prototype; implement it...
6+ * mingwex/ftruncate.c: ...in this new file; it will delegate to...
7+ (_chsize_s): ...this MSVCRT.DLL function, if available.
8+
9+ * Makefile.in (libmingwex.a): Add dependency on...
10+ (ftruncate.$OBJEXT): ...this.
11+
12+ * msvcrt-xref/msvcrt.def.in (_chsize_s): Require dlsym look-up.
13+
14+2020-01-17 Keith Marshall <keith@users.osdn.me>
15+
316 Preserve order of tests for integrity of header files.
417
518 * tests/Makefile.in: Explicitly sort $wildcard output, within...
--- a/mingwrt/Makefile.in
+++ b/mingwrt/Makefile.in
@@ -7,7 +7,7 @@ PACKAGE_TARNAME := @PACKAGE_TARNAME@
77 PACKAGE_VERSION := @PACKAGE_VERSION@
88
99 # Written by Keith Marshall <keithmarshall@users.sourceforge.net>
10-# Copyright (C) 2014-2018, MinGW.org Project
10+# Copyright (C) 2014-2020, MinGW.org Project
1111 #
1212 #
1313 # Permission is hereby granted, free of charge, to any person obtaining a
@@ -459,12 +459,12 @@ libmingwex.a: $(LIBMINGWEX_MEMALIGN_OBJECTS)
459459
460460 # Some additional miscellaneous functions, in libmingwex.a
461461 #
462+libmingwex.a: $(addsuffix .$(OBJEXT), ftruncate getdelim gettimeofday)
462463 libmingwex.a: $(addsuffix .$(OBJEXT), glob getopt basename dirname nsleep)
463464 libmingwex.a: $(addsuffix .$(OBJEXT), clockapi clockres clockset clocktime)
464465 libmingwex.a: $(addsuffix .$(OBJEXT), insque remque tdelete tfind tsearch twalk)
465466 libmingwex.a: $(addsuffix .$(OBJEXT), dirent wdirent dlfcn strerror_r strtok_r)
466467 libmingwex.a: $(addsuffix .$(OBJEXT), mkstemp mkdtemp cryptnam setenv)
467-libmingwex.a: $(addsuffix .$(OBJEXT), getdelim gettimeofday)
468468
469469 vpath %.s ${mingwrt_srcdir}/mingwex
470470 vpath %.sx ${mingwrt_srcdir}/mingwex
--- a/mingwrt/include/unistd.h
+++ b/mingwrt/include/unistd.h
@@ -11,7 +11,7 @@
1111 * Ramiro Polla <ramiro@lisha.ufsc.br>
1212 * Gregory McGarry <gregorymcgarry@users.sourceforge.net>
1313 * Keith Marshall <keithmarshall@users.sourceforge.net>
14- * Copyright (C) 1997, 1999, 2002-2004, 2007-2009, 2014-2017,
14+ * Copyright (C) 1997, 1999, 2002-2004, 2007-2009, 2014-2017, 2020,
1515 * MinGW.org Project.
1616 *
1717 *
@@ -132,6 +132,15 @@ __CRT_INLINE __JMPSTUB__(( FUNCTION = ftruncate, DLLENTRY = _chsize ))
132132 int ftruncate( int __fd, off_t __length ){ return _chsize( __fd, __length ); }
133133 #endif
134134
135+/* Although non-standard, GCC's C++ library from GCC-9.x gratuitously
136+ * assumes, and requires, this 64-bit off_t variant to be available; it
137+ * could be emulated by Microsoft's _chsize_s() function, which is only
138+ * supported from Vista onward; therefore, to ensure support on legacy
139+ * platforms, we prefer our own libmingwex.a implementation, and so,
140+ * we do not provide an inline implementation.
141+ */
142+int __cdecl ftruncate64( int, __off64_t );
143+
135144 _END_C_DECLS
136145
137146 #endif /* _POSIX_C_SOURCE */
--- /dev/null
+++ b/mingwrt/mingwex/ftruncate.c
@@ -0,0 +1,167 @@
1+/*
2+ * ftruncate.c
3+ *
4+ * Implement a 64-bit file size capable ftruncate() function; GCC-9.x
5+ * gratuitously assumes that this is available, via the ftruncate64()
6+ * entry point.
7+ *
8+ * $Id$
9+ *
10+ * Written by Keith Marshall <keith@users.osdn.me>
11+ * Copyright (C) 2020, MinGW.org Project
12+ *
13+ *
14+ * Permission is hereby granted, free of charge, to any person obtaining a
15+ * copy of this software and associated documentation files (the "Software"),
16+ * to deal in the Software without restriction, including without limitation
17+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18+ * and/or sell copies of the Software, and to permit persons to whom the
19+ * Software is furnished to do so, subject to the following conditions:
20+ *
21+ * The above copyright notice and this permission notice (including the next
22+ * paragraph) shall be included in all copies or substantial portions of the
23+ * Software.
24+ *
25+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31+ * DEALINGS IN THE SOFTWARE.
32+ *
33+ */
34+#include <dlfcn.h>
35+#include <unistd.h>
36+#include <winbase.h>
37+#include <stdint.h>
38+#include <string.h>
39+#include <stdio.h>
40+#include <errno.h>
41+
42+/* The following in-line function is provided to facilitate abnormal return
43+ * from ftruncate64(), (in which case the return value is always -1), while
44+ * setting the global errno indicator to an appropriate error code.
45+ */
46+static __inline__ __attribute__((__always_inline__))
47+int errout( int error_code ){ errno = error_code; return -1L; }
48+
49+/* When running on Vista, or later, or for applications which have been
50+ * linked against non-free MSVCR80.DLL, or later, we may be able to simply
51+ * substitute a call to Microsoft's _chsize_s() function, (which behaves
52+ * as a 64-bit variant of the universally available _chsize() function).
53+ * On legacy Windows versions, which are unlikely to provide _chsize_s(),
54+ * we need to provide our own fallback 64-bit chsize() implementation;
55+ * this may be static, but cannot be inlined, because we need a physical
56+ * entry point address to which execution may be redirected.
57+ */
58+static int mingw_chsize64_fallback( int fd, __off64_t offset )
59+{
60+ /* POSIX.1 requires the file pointer to be unchanged, as a consequence
61+ * of calling ftruncate(), (and Microsoft's _chsize() functions do seem
62+ * to satisfy this requirement); however, to mark a new end of file, we
63+ * need move the file pointer to the new end of file offset, so we need
64+ * to save the original pointer now, to restore later.
65+ */
66+ __off64_t cur_offset = _lseeki64( fd, 0LL, SEEK_CUR );
67+
68+ /* In the event that the new end of file offset requires the file to be
69+ * extended beyond its current end of file offset, POSIX.1 also requires
70+ * NUL byte padding to be written to the extended file space, (and again,
71+ * Microsoft's _chsize() functions seem to do this); we may reposition
72+ * the file pointer to its current end of file offset, in preparation
73+ * for the possibility that we need to fulfil this requirement.
74+ */
75+ __off64_t end_offset = _lseeki64( fd, 0LL, SEEK_END );
76+
77+ /* We will eventually need to restore the original file pointer, AFTER
78+ * we have evaluated the return status code, so we will need to save
79+ * this.
80+ */
81+ int retval;
82+
83+ /* There are two possible options for repositioning the end of file
84+ * pointer:
85+ */
86+ if( offset > end_offset )
87+ {
88+ /* In this case, the file is to be extended beyond its current
89+ * end of file offset; initialize a NUL filled buffer, which we
90+ * may then copy to the extended region of the file, to satisfy
91+ * the POSIX.1 requirement that this region shall be NUL filled.
92+ */
93+ char padding[BUFSIZ];
94+ memset( padding, 0, sizeof( padding ) );
95+
96+ /* Recompute the desired offset, relative to the current end of
97+ * file, then repeatedly write copies of the NUL filled buffer,
98+ * until the file space represented by this relative offset has
99+ * been completely filled; (this results in advancement of the
100+ * file pointer to the desired new end of file offset).
101+ */
102+ offset -= end_offset;
103+ while( offset > (__off64_t)(sizeof( padding )) )
104+ offset -= write( fd, padding, sizeof( padding ) );
105+ write( fd, padding, offset );
106+ }
107+ else
108+ /* In the alternative case, the new end of file pointer will lie
109+ * within the space already occupied by the file; we may simply
110+ * seek directly to the desired offset.
111+ */
112+ _lseeki64( fd, offset, SEEK_SET );
113+
114+ /* We have now adjusted the file pointer to be coincident with the
115+ * desired new end of file offset; this is exactly what is required
116+ * by the Windows API function, to mark the new end of file.
117+ */
118+ retval = SetEndOfFile( (void *)(_get_osfhandle( fd )) )
119+ ? 0 : errout( EBADF );
120+
121+ /* Finally, we must restore the originally saved file pointer, before
122+ * we return the status code from the ftruncate() operation.
123+ */
124+ _lseeki64( fd, cur_offset, SEEK_SET );
125+ return retval;
126+}
127+
128+/* Regardless of the platform version, Microsoft do not provide an
129+ * implementation of ftruncate64(); all link-time references to this
130+ * function will be resolved by this libmingwex.a implementation.
131+ */
132+int ftruncate64( int fd, __off64_t offset )
133+{
134+ /* The offset parameter MUST be positive valued; bail out if not.
135+ */
136+ if( 0LL > offset ) return errout( EINVAL );
137+
138+ /* For offsets which may be represented by a 32-bit integer, we
139+ * may ALWAYS delegate this call to Microsoft's _chsize().
140+ */
141+ if( INT32_MAX >= offset ) return _chsize( fd, (off_t)(offset) );
142+
143+ { /* For offsets which cannot be represented within 32-bits, we
144+ * MAY be able to delegate this call, (and also any subsequent
145+ * calls), to Microsoft's _chsize_s(); set up a redirector to
146+ * handle such delegation...
147+ */
148+ static int (*redirector_hook)( int, __off64_t ) = NULL;
149+
150+ /* ...initially checking for _chsize_s() availability...
151+ */
152+ if( (redirector_hook == NULL)
153+ && ((redirector_hook = dlsym( RTLD_DEFAULT, "_chsize_s" )) == NULL) )
154+
155+ /* ...and setting up a suitable fallback if not...
156+ */
157+ redirector_hook = mingw_chsize64_fallback;
158+
159+ /* ...and ultimately, on initial selection, (and directly on
160+ * all subsequent calls), hand off execution to the selected
161+ * delegate function.
162+ */
163+ return redirector_hook( fd, offset );
164+ }
165+}
166+
167+/* $RCSfile$: end of file */
--- a/mingwrt/msvcrt-xref/msvcrt.def.in
+++ b/mingwrt/msvcrt-xref/msvcrt.def.in
@@ -5,7 +5,7 @@
55 * definition files, supporting multiple OS platform versions.
66 *
77 * Compiled by Keith Marshall <keithmarshall@users.sourceforge.net>
8- * Copyright (C) 2014, 2016, 2018, MinGW.org Project.
8+ * Copyright (C) 2014, 2016, 2018, 2020, MinGW.org Project.
99 *
1010 *
1111 * Permission is hereby granted, free of charge, to any person obtaining a
@@ -656,7 +656,7 @@ _chmod
656656 _chsize
657657 #if __MSVCRT_VERSION__ >= 0x0600UL
658658 # if __MSVCRT_VERSION__ < 0x07000000UL || __MSVCRT_VERSION__ >= 0x08000000UL
659-_chsize_s
659+__MINGW_DLSYM(_chsize_s)
660660 # endif
661661 #endif
662662 #if __MSVCRT_VERSION__ >= 0x0600UL
Show on old repository browser