| 531 | |
| 532 | |
| 533 | bool CFile::SetLength(uint64 new_len) |
| 534 | { |
| 535 | MULE_VALIDATE_STATE(IsOpened(), "CFile: Cannot set length when no file is open."); |
| 536 | |
| 537 | std::lock_guard<std::recursive_mutex> lock(m_mutex); |
| 538 | |
| 539 | // ftruncate / chsize operate on the kernel-side size; drain the |
| 540 | // userspace buffer first so any pending bytes are part of the |
| 541 | // length-resolution decision (e.g. extend-then-write patterns). |
| 542 | DrainWriteBuffer(); |
| 543 | |
| 544 | #ifdef __WINDOWS__ |
| 545 | #ifdef _MSC_VER |
| 546 | // MSVC has a 64bit version |
| 547 | bool result = _chsize_s(m_fd, new_len) == 0; |
| 548 | #else |
| 549 | // MingW has an old runtime without it |
| 550 | bool result = chsize(m_fd, new_len) == 0; |
| 551 | #endif |
| 552 | #else |
| 553 | bool result = ftruncate(m_fd, new_len) != -1; |
| 554 | #endif |
| 555 | |
| 556 | syscall_check(result, m_filePath, "truncating file"); |
| 557 | |
| 558 | return result; |
| 559 | } |
| 560 | // File_checked_for_headers |