/* */ #include "GlowFileAllocator.h" #include "DlAbortEx.h" #include "TimeA2.h" #include #include #include #include void GlowFileAllocator::allocate(int fd, int64_t totalLength) { struct stat fs; if(fstat(fd, &fs) < 0) { throw new DlAbortEx("fstat filed: %s", strerror(errno)); } if(fs.st_size != lseek(fd, 0, SEEK_END)) { throw new DlAbortEx("Seek failed: %s", strerror(errno)); } int32_t bufSize = 4096; char buf[4096]; memset(buf, 0, bufSize); int64_t x = (totalLength-fs.st_size+bufSize-1)/bufSize; fileAllocationMonitor->setMinValue(0); fileAllocationMonitor->setMaxValue(totalLength); fileAllocationMonitor->setCurrentValue(fs.st_size); fileAllocationMonitor->showProgress(); Time cp; for(int64_t i = 0; i < x; ++i) { if(write(fd, buf, bufSize) < 0) { throw new DlAbortEx("Allocation failed: %s", strerror(errno)); } if(cp.elapsedInMillis(500)) { fileAllocationMonitor->setCurrentValue(i*bufSize); fileAllocationMonitor->showProgress(); cp.reset(); } } fileAllocationMonitor->setCurrentValue(totalLength); fileAllocationMonitor->showProgress(); ftruncate(fd, totalLength); }