UTIL_THROW_IF(value>static_cast<uint64_t>(std::numeric_limits<std::size_t>::max()),OverflowException,"Integer overflow detected. This model is too big for 32-bit code.");
#warning "The file functions on MinGW have not been tested for file sizes above 2^31 - 1. Please read https://stackoverflow.com/questions/12539488/determine-64-bit-file-size-in-c-on-mingw-32-bit and fix"
#elif defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#include <io.h>
#else
#include <unistd.h>
#endif
namespaceutil{
scoped_fd::~scoped_fd(){
if(fd_!=-1&&close(fd_)){
std::cerr<<"Could not close file "<<fd_<<std::endl;
std::abort();
}
}
voidscoped_FILE_closer::Close(std::FILE*file){
if(file&&std::fclose(file)){
std::cerr<<"Could not close file "<<file<<std::endl;
std::abort();
}
}
// Note that ErrnoException records errno before NameFromFD is called.
UTIL_THROW_IF_ARG(-1==fallocate(fd,FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE,offset,size),FDException,(fd),"in punching a hole at "<<offset<<" for "<<size<<" bytes.");
#else
UTIL_THROW(UnsupportedOSException,"fallocate hole punching requires Linux and glibc >= 2.18");
#endif
}
namespace{
std::size_tGuardLarge(std::size_tsize){
// The following operating systems have broken read/write/pread/pwrite that
// only supports up to 2^31.
// OS X man pages claim to support 64-bit, but Kareem M. Darwish had problems
// building with larger files, so APPLE is also here.
DWORDsmaller_size=28672;// Received reports that 31346 worked but higher values did not. This rounds down to the nearest multiple of 4096, the page size.
/* BROKEN: changes file pointer. Even if you save it and change it back, it won't be safe to use concurrently with write() or read() which lmplz does. */