Skip to content

Commit a96a112

Browse files
authored
gguf : fix ftell/fseek for Windows (ggml-org#19870)
1 parent 2446419 commit a96a112

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

ggml/src/gguf.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
#define GGUF_MAX_STRING_LENGTH (1024*1024*1024)
1919
#define GGUF_MAX_ARRAY_ELEMENTS (1024*1024*1024)
2020

21+
#ifdef _WIN32
22+
# define gguf_ftell _ftelli64
23+
# define gguf_fseek _fseeki64
24+
#else
25+
# define gguf_ftell ftello
26+
# define gguf_fseek fseeko
27+
#endif
28+
2129
template <typename T>
2230
struct type_to_gguf_type;
2331

@@ -319,22 +327,22 @@ struct gguf_reader {
319327

320328
// remaining bytes in the file
321329
uint64_t nbytes_remain() const {
322-
const long cur = ftell(file);
330+
const int64_t cur = gguf_ftell(file);
323331
if (cur < 0) {
324332
return 0;
325333
}
326-
if (fseek(file, 0, SEEK_END) != 0) {
327-
fseek(file, cur, SEEK_SET);
334+
if (gguf_fseek(file, 0, SEEK_END) != 0) {
335+
gguf_fseek(file, cur, SEEK_SET);
328336

329337
return 0;
330338
}
331-
const long end = ftell(file);
339+
const int64_t end = gguf_ftell(file);
332340
if (end < 0) {
333-
fseek(file, cur, SEEK_SET);
341+
gguf_fseek(file, cur, SEEK_SET);
334342

335343
return 0;
336344
}
337-
fseek(file, cur, SEEK_SET);
345+
gguf_fseek(file, cur, SEEK_SET);
338346
return static_cast<uint64_t>(end - cur);
339347
}
340348
};
@@ -671,14 +679,14 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
671679
GGML_ASSERT(int64_t(ctx->info.size()) == n_tensors);
672680

673681
// we require the data section to be aligned, so take into account any padding
674-
if (fseek(file, GGML_PAD(ftell(file), ctx->alignment), SEEK_SET) != 0) {
682+
if (gguf_fseek(file, GGML_PAD(gguf_ftell(file), ctx->alignment), SEEK_SET) != 0) {
675683
GGML_LOG_ERROR("%s: failed to seek to beginning of data section\n", __func__);
676684
gguf_free(ctx);
677685
return nullptr;
678686
}
679687

680688
// store the current file offset - this is where the data section starts
681-
ctx->offset = ftell(file);
689+
ctx->offset = gguf_ftell(file);
682690

683691
// compute the total size of the data section, taking into account the alignment
684692
{

0 commit comments

Comments
 (0)