Skip to content

Commit 48092a9

Browse files
committed
flatfile: use 64-bit file positioning syscalls in Windows
1 parent 17556cb commit 48092a9

4 files changed

Lines changed: 36 additions & 6 deletions

File tree

‎src/flatfile.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ FILE* FlatFileSeq::Open(const FlatFilePos& pos, bool read_only) const
4646
LogError("Unable to open file %s", fs::PathToString(path));
4747
return nullptr;
4848
}
49-
if (pos.nPos && fseek(file, pos.nPos, SEEK_SET)) {
49+
if (pos.nPos && FileSeek64(file, pos.nPos, SEEK_SET)) {
5050
LogError("Unable to seek to position %u of %s", pos.nPos, fs::PathToString(path));
5151
if (fclose(file) != 0) {
5252
LogError("Unable to close file %s", fs::PathToString(path));

‎src/streams.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
AutoFile::AutoFile(std::FILE* file, const Obfuscation& obfuscation) : m_file{file}, m_obfuscation{obfuscation}
1414
{
1515
if (!IsNull()) {
16-
auto pos{std::ftell(m_file)};
16+
auto pos{FileTell64(m_file)};
1717
if (pos >= 0) m_position = pos;
1818
}
1919
}
@@ -35,15 +35,15 @@ void AutoFile::seek(int64_t offset, int origin)
3535
if (IsNull()) {
3636
throw std::ios_base::failure("AutoFile::seek: file handle is nullptr");
3737
}
38-
if (std::fseek(m_file, offset, origin) != 0) {
38+
if (FileSeek64(m_file, offset, origin) != 0) {
3939
throw std::ios_base::failure(feof() ? "AutoFile::seek: end of file" : "AutoFile::seek: fseek failed");
4040
}
4141
if (origin == SEEK_SET) {
4242
m_position = offset;
4343
} else if (origin == SEEK_CUR && m_position.has_value()) {
4444
*m_position += offset;
4545
} else {
46-
int64_t r{std::ftell(m_file)};
46+
int64_t r{FileTell64(m_file)};
4747
if (r < 0) {
4848
throw std::ios_base::failure("AutoFile::seek: ftell failed");
4949
}

‎src/util/fs_helpers.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ void DirectoryCommit(const fs::path& dirname)
150150
bool TruncateFile(FILE* file, uint64_t length)
151151
{
152152
#if defined(WIN32)
153-
return _chsize(_fileno(file), length) == 0;
153+
return _chsize_s(_fileno(file), length) == 0;
154154
#else
155155
return ftruncate(fileno(file), length) == 0;
156156
#endif
@@ -232,7 +232,7 @@ void AllocateFileRange(FILE* file, uint64_t offset, unsigned int length)
232232
// Fallback version
233233
// TODO: just write one byte per block
234234
static const char buf[65536] = {};
235-
if (fseek(file, offset, SEEK_SET)) {
235+
if (FileSeek64(file, offset, SEEK_SET)) {
236236
return;
237237
}
238238
while (length > 0) {

‎src/util/fs_helpers.h‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,36 @@ void DirectoryCommit(const fs::path& dirname);
4646

4747
bool TruncateFile(FILE* file, uint64_t length);
4848

49+
/**
50+
* 64-bit-clean file positioning.
51+
*
52+
* The wrapper is necessary for Windows, because `long` is only 32 bits there,
53+
* even in 64-bit builds (LLP64).
54+
*/
55+
inline int FileSeek64(FILE* file, int64_t offset, int origin)
56+
{
57+
#if defined(WIN32)
58+
return _fseeki64(file, offset, origin);
59+
#else
60+
return std::fseek(file, offset, origin);
61+
#endif
62+
}
63+
64+
/**
65+
* 64-bit-clean file positioning.
66+
*
67+
* The wrapper is necessary for Windows, because `long` is only 32 bits there,
68+
* even in 64-bit builds (LLP64).
69+
*/
70+
inline int64_t FileTell64(FILE* file)
71+
{
72+
#if defined(WIN32)
73+
return _ftelli64(file);
74+
#else
75+
return std::ftell(file);
76+
#endif
77+
}
78+
4979
/**
5080
* Try to raise the file descriptor limit to the requested number.
5181
*

0 commit comments

Comments
 (0)