[libcxx-commits] [libcxx] 86e20b0 - [libcxx] Use _ftelli64/_fseeki64 on Windows (#123128)
via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Jan 29 05:25:47 PST 2025
Author: Martin Storsjö
Date: 2025-01-29T15:25:43+02:00
New Revision: 86e20b00c313e96db3b69d440bfb2ca9063f08f0
URL: https://github.com/llvm/llvm-project/commit/86e20b00c313e96db3b69d440bfb2ca9063f08f0
DIFF: https://github.com/llvm/llvm-project/commit/86e20b00c313e96db3b69d440bfb2ca9063f08f0.diff
LOG: [libcxx] Use _ftelli64/_fseeki64 on Windows (#123128)
This allows using the full 64 bit range for file offsets.
This should fix the issue reported downstream at
https://github.com/mstorsjo/llvm-mingw/issues/462.
Added:
Modified:
libcxx/include/fstream
libcxx/test/std/input.output/file.streams/fstreams/ifstream.members/offset_range.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/include/fstream b/libcxx/include/fstream
index f0e9425e0a53d9..de5c07035dba9c 100644
--- a/libcxx/include/fstream
+++ b/libcxx/include/fstream
@@ -216,12 +216,6 @@ typedef basic_fstream<wchar_t> wfstream;
_LIBCPP_PUSH_MACROS
# include <__undef_macros>
-# if !defined(_LIBCPP_MSVCRT) && !defined(_NEWLIB_VERSION)
-# define _LIBCPP_HAS_OFF_T_FUNCTIONS 1
-# else
-# define _LIBCPP_HAS_OFF_T_FUNCTIONS 0
-# endif
-
# if _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
_LIBCPP_BEGIN_NAMESPACE_STD
@@ -362,6 +356,9 @@ private:
bool __read_mode();
void __write_mode();
+ _LIBCPP_HIDE_FROM_ABI static int __fseek(FILE* __file, pos_type __offset, int __whence);
+ _LIBCPP_HIDE_FROM_ABI static pos_type __ftell(FILE* __file);
+
_LIBCPP_EXPORTED_FROM_ABI friend FILE* __get_ostream_file(ostream&);
// There are multiple (__)open function, they use
diff erent C-API open
@@ -936,31 +933,42 @@ basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
default:
return pos_type(off_type(-1));
}
-# if !_LIBCPP_HAS_OFF_T_FUNCTIONS
- if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
+ if (__fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
return pos_type(off_type(-1));
- pos_type __r = ftell(__file_);
-# else
- if (::fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
- return pos_type(off_type(-1));
- pos_type __r = ftello(__file_);
-# endif
+ pos_type __r = __ftell(__file_);
__r.state(__st_);
return __r;
}
+template <class _CharT, class _Traits>
+int basic_filebuf<_CharT, _Traits>::__fseek(FILE* __file, pos_type __offset, int __whence) {
+# if defined(_LIBCPP_MSVCRT_LIKE)
+ return _fseeki64(__file, __offset, __whence);
+# elif defined(_NEWLIB_VERSION)
+ return fseek(__file, __offset, __whence);
+# else
+ return ::fseeko(__file, __offset, __whence);
+# endif
+}
+
+template <class _CharT, class _Traits>
+typename basic_filebuf<_CharT, _Traits>::pos_type basic_filebuf<_CharT, _Traits>::__ftell(FILE* __file) {
+# if defined(_LIBCPP_MSVCRT_LIKE)
+ return _ftelli64(__file);
+# elif defined(_NEWLIB_VERSION)
+ return ftell(__file);
+# else
+ return ftello(__file);
+# endif
+}
+
template <class _CharT, class _Traits>
typename basic_filebuf<_CharT, _Traits>::pos_type
basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode) {
if (__file_ == nullptr || sync())
return pos_type(off_type(-1));
-# if !_LIBCPP_HAS_OFF_T_FUNCTIONS
- if (fseek(__file_, __sp, SEEK_SET))
+ if (__fseek(__file_, __sp, SEEK_SET))
return pos_type(off_type(-1));
-# else
- if (::fseeko(__file_, __sp, SEEK_SET))
- return pos_type(off_type(-1));
-# endif
__st_ = __sp.state();
return __sp;
}
@@ -1007,13 +1015,8 @@ int basic_filebuf<_CharT, _Traits>::sync() {
}
}
}
-# if !_LIBCPP_HAS_OFF_T_FUNCTIONS
- if (fseek(__file_, -__c, SEEK_CUR))
+ if (__fseek(__file_, -__c, SEEK_CUR))
return -1;
-# else
- if (::fseeko(__file_, -__c, SEEK_CUR))
- return -1;
-# endif
if (__update_st)
__st_ = __state;
__extbufnext_ = __extbufend_ = __extbuf_;
diff --git a/libcxx/test/std/input.output/file.streams/fstreams/ifstream.members/offset_range.pass.cpp b/libcxx/test/std/input.output/file.streams/fstreams/ifstream.members/offset_range.pass.cpp
index 6ffe750564c2c9..c6e07d045e1458 100644
--- a/libcxx/test/std/input.output/file.streams/fstreams/ifstream.members/offset_range.pass.cpp
+++ b/libcxx/test/std/input.output/file.streams/fstreams/ifstream.members/offset_range.pass.cpp
@@ -11,12 +11,6 @@
// Test that we can seek using offsets larger than 32 bit, and that we can
// retrieve file offsets larger than 32 bit.
-// On MSVC targets, we only use the 32 bit fseek/ftell functions. For MinGW
-// targets, we use fseeko/ftello, but the user needs to define
-// _FILE_OFFSET_BITS=64 to make them 64 bit.
-//
-// XFAIL: target={{.*}}-windows{{.*}}
-
// On 32 bit Android platforms, off_t is 32 bit by default. By defining
// _FILE_OFFSET_BITS=64, one gets a 64 bit off_t, but the corresponding
// 64 bit ftello/fseeko functions are only available since Android API 24 (7.0).
More information about the libcxx-commits
mailing list