this function tries to make a particular range of a file allocated (corresponding to disk space) it is advisory, and the range specified in the arguments will never contain live data
| 779 | // this function tries to make a particular range of a file allocated (corresponding to disk space) |
| 780 | // it is advisory, and the range specified in the arguments will never contain live data |
| 781 | void AllocateFileRange(FILE* file, unsigned int offset, unsigned int length) { |
| 782 | #if defined(WIN32) |
| 783 | // Windows-specific version |
| 784 | HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file)); |
| 785 | LARGE_INTEGER nFileSize; |
| 786 | int64_t nEndPos = (int64_t)offset + length; |
| 787 | nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF; |
| 788 | nFileSize.u.HighPart = nEndPos >> 32; |
| 789 | SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN); |
| 790 | SetEndOfFile(hFile); |
| 791 | #elif defined(MAC_OSX) |
| 792 | // OSX specific version |
| 793 | fstore_t fst; |
| 794 | fst.fst_flags = F_ALLOCATECONTIG; |
| 795 | fst.fst_posmode = F_PEOFPOSMODE; |
| 796 | fst.fst_offset = 0; |
| 797 | fst.fst_length = (off_t)offset + length; |
| 798 | fst.fst_bytesalloc = 0; |
| 799 | if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) { |
| 800 | fst.fst_flags = F_ALLOCATEALL; |
| 801 | fcntl(fileno(file), F_PREALLOCATE, &fst); |
| 802 | } |
| 803 | ftruncate(fileno(file), fst.fst_length); |
| 804 | #elif defined(__linux__) |
| 805 | // Version using posix_fallocate |
| 806 | off_t nEndPos = (off_t)offset + length; |
| 807 | posix_fallocate(fileno(file), 0, nEndPos); |
| 808 | #else |
| 809 | // Fallback version |
| 810 | // TODO: just write one byte per block |
| 811 | static const char buf[65536] = {}; |
| 812 | fseek(file, offset, SEEK_SET); |
| 813 | while (length > 0) { |
| 814 | unsigned int now = 65536; |
| 815 | if (length < now) now = length; |
| 816 | fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway |
| 817 | length -= now; |
| 818 | } |
| 819 | #endif |
| 820 | } |
| 821 | |
| 822 | void ShrinkDebugFile() { |
| 823 | // Scroll debug.log if it's getting too big |
no outgoing calls
no test coverage detected