MCPcopy Create free account
hub / github.com/LUX-Core/lux / AllocateFileRange

Function AllocateFileRange

src/util.cpp:745–786  ·  view source on GitHub ↗

* 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 */

Source from the content-addressed store, hash-verified

743 * it is advisory, and the range specified in the arguments will never contain live data
744 */
745void AllocateFileRange(FILE* file, unsigned int offset, unsigned int length)
746{
747#if defined(WIN32)
748 // Windows-specific version
749 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
750 LARGE_INTEGER nFileSize;
751 int64_t nEndPos = (int64_t)offset + length;
752 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
753 nFileSize.u.HighPart = nEndPos >> 32;
754 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
755 SetEndOfFile(hFile);
756#elif defined(MAC_OSX)
757 // OSX specific version
758 fstore_t fst;
759 fst.fst_flags = F_ALLOCATECONTIG;
760 fst.fst_posmode = F_PEOFPOSMODE;
761 fst.fst_offset = 0;
762 fst.fst_length = (off_t)offset + length;
763 fst.fst_bytesalloc = 0;
764 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
765 fst.fst_flags = F_ALLOCATEALL;
766 fcntl(fileno(file), F_PREALLOCATE, &fst);
767 }
768 ftruncate(fileno(file), fst.fst_length);
769#elif defined(__linux__)
770 // Version using posix_fallocate
771 off_t nEndPos = (off_t)offset + length;
772 posix_fallocate(fileno(file), 0, nEndPos);
773#else
774 // Fallback version
775 // TODO: just write one byte per block
776 static const char buf[65536] = {};
777 fseek(file, offset, SEEK_SET);
778 while (length > 0) {
779 unsigned int now = 65536;
780 if (length < now)
781 now = length;
782 fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
783 length -= now;
784 }
785#endif
786}
787
788void ShrinkDebugFile()
789{

Callers 2

FindBlockPosFunction · 0.85
FindUndoPosFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected