static int/*boolean*/ CommonRead(int fd, int err, int/*boolean*/ mayBlock, INTEGER* len) { if (CommonError(err)) return FALSE; switch (err) { case ECONNRESET: *len = 0; return TRUE; case EPIPE: case ENETRESET: IOError(ConnLost); return FALSE; case EWOULDBLOCK: #if EAGAIN != EWOULDBLOCK case EAGAIN: #endif if (!mayBlock) { *len = -1; return TRUE; } break; default: IOError(Unexpected); } SchedulerPosix__IOWait(fd, TRUE, 0); return FALSE; }
static int/*boolean*/ CommonError(int err) { switch (err) { #ifdef _WIN32 case WSAETIMEDOUT: #else case ETIMEDOUT: #endif IOError(Timeout); return TRUE; #ifdef _WIN32 case WSAENETUNREACH: case WSAEHOSTUNREACH: case WSAEHOSTDOWN: case WSAENETDOWN: #else case ENETUNREACH: case EHOSTUNREACH: case EHOSTDOWN: case ENETDOWN: #endif IOError(Unreachable); return TRUE; } return FALSE; }
PipeImpl::PipeImpl() : _out(PipeIODevice::Read) , _in(PipeIODevice::Write) { MSGQUEUEOPTIONS writeOpts, readOpts; memset(&writeOpts, 0, sizeof(writeOpts)); memset(&readOpts, 0, sizeof(readOpts)); writeOpts.dwSize = sizeof(MSGQUEUEOPTIONS); writeOpts.dwFlags = MSGQUEUE_ALLOW_BROKEN; writeOpts.dwMaxMessages = 100; writeOpts.cbMaxMessage = 1024; writeOpts.bReadAccess = FALSE; readOpts = writeOpts; readOpts.bReadAccess = TRUE; HANDLE outputHandle = CreateMsgQueue(NULL, &writeOpts); if (outputHandle == INVALID_HANDLE_VALUE) throw IOError( PT_ERROR_MSG("Could not create message queue handle") ); HANDLE inputHandle = OpenMsgQueue(::GetCurrentProcess(), outputHandle, &readOpts); if (inputHandle == INVALID_HANDLE_VALUE) throw IOError( PT_ERROR_MSG("Could not open message queue handle") ); _out.open(inputHandle); _in.open(outputHandle); }
void PaymentOperationStateHandler::deleteRecord( const TransactionUUID &transactionUUID) { string query = "DELETE FROM " + mTableName + " WHERE transaction_uuid = ?;"; sqlite3_stmt *stmt; int rc = sqlite3_prepare_v2(mDataBase, query.c_str(), -1, &stmt, 0); if (rc != SQLITE_OK) { throw IOError("PaymentOperationStateHandler::delete: " "Bad query; sqlite error: " + to_string(rc)); } rc = sqlite3_bind_blob(stmt, 1, transactionUUID.data, NodeUUID::kBytesSize, SQLITE_STATIC); if (rc != SQLITE_OK) { throw IOError("PaymentOperationStateHandler::delete: " "Bad binding of TransactionUUID; sqlite error: " + to_string(rc)); } rc = sqlite3_step(stmt); sqlite3_reset(stmt); sqlite3_finalize(stmt); if (rc == SQLITE_DONE) { #ifdef STORAGE_HANDLER_DEBUG_LOG info() << "prepare deleting is completed successfully"; #endif } else { throw IOError("PaymentOperationStateHandler::delete: " "Run query; sqlite error: " + to_string(rc)); } }
pair<BytesShared, size_t> PaymentOperationStateHandler::byTransaction( const TransactionUUID &transactionUUID) { string query = "SELECT state, state_bytes_count FROM " + mTableName + " WHERE transaction_uuid = ?;"; sqlite3_stmt *stmt; int rc = sqlite3_prepare_v2(mDataBase, query.c_str(), -1, &stmt, 0); if (rc != SQLITE_OK) { throw IOError("PaymentOperationStateHandler::byTransaction: " "Bad query; sqlite error: " + to_string(rc)); } rc = sqlite3_bind_blob(stmt, 1, transactionUUID.data, NodeUUID::kBytesSize, SQLITE_STATIC); if (rc != SQLITE_OK) { throw IOError("PaymentOperationStateHandler::byTransaction: " "Bad binding of TransactionUUID; sqlite error: " + to_string(rc)); } rc = sqlite3_step(stmt); if (rc == SQLITE_ROW) { size_t stateBytesCount = (size_t)sqlite3_column_int(stmt, 1); BytesShared state = tryMalloc(stateBytesCount); memcpy( state.get(), sqlite3_column_blob(stmt, 0), stateBytesCount); sqlite3_reset(stmt); sqlite3_finalize(stmt); return make_pair(state, stateBytesCount); } else { sqlite3_reset(stmt); sqlite3_finalize(stmt); throw NotFoundError("PaymentOperationStateHandler::byTransaction: " "There are now records with requested transactionUUID"); } }
std::size_t PipeIODevice::onRead(char* buffer, std::size_t count, bool& eof) { if( Read != _mode ) throw IOError( PT_ERROR_MSG("Could not read from write only pipe") ); DWORD readBytes = 0; DWORD flags = 0; eof = false; DWORD timeout = _timeout == EventLoop::WaitInfinite ? INFINITE : static_cast<std::size_t>(_timeout); if(_bufferSize) { readBytes = _bufferSize; } else if ( FALSE == ReadMsgQueue(handle(), &_buffer[0], _msgSize, &readBytes, timeout, &flags) ) { throw IOError("ReadMsgQueue failed"); } memcpy(buffer, &_buffer[0], count); _bufferSize = 0; if (count >= readBytes) return readBytes; std::vector<char>::iterator beginData = (_buffer.begin() + count); std::vector<char>::iterator endData = (_buffer.begin() + readBytes); std::copy(beginData, endData, _buffer.begin()); _bufferSize = (readBytes - count); return count; }
// =========================================================================== // static method definitions // =========================================================================== OutputDevice& OutputDevice::getDevice(const std::string& name) { // check whether the device has already been aqcuired if (myOutputDevices.find(name) != myOutputDevices.end()) { return *myOutputDevices[name]; } // build the device OutputDevice* dev = 0; // check whether the device shall print to stdout if (name == "stdout") { dev = OutputDevice_COUT::getDevice(); } else if (name == "stderr") { dev = OutputDevice_CERR::getDevice(); } else if (FileHelpers::isSocket(name)) { try { int port = TplConvert::_2int(name.substr(name.find(":") + 1).c_str()); dev = new OutputDevice_Network(name.substr(0, name.find(":")), port); } catch (NumberFormatException&) { throw IOError("Given port number '" + name.substr(name.find(":") + 1) + "' is not numeric."); } catch (EmptyData&) { throw IOError("No port number given."); } } else { const size_t len = name.length(); dev = new OutputDevice_File(name, len > 4 && name.substr(len - 4) == ".sbx"); } dev->setPrecision(); dev->getOStream() << std::setiosflags(std::ios::fixed); myOutputDevices[name] = dev; return *dev; }
void ResultsInterface::writeResult( const char *bytes, const size_t bytesCount) { if (mFIFODescriptor == 0){ #ifdef MAC_OS mFIFODescriptor = open( FIFOFilePath().c_str(), O_WRONLY | O_DSYNC); #endif #ifdef LINUX mFIFODescriptor = open( FIFOFilePath().c_str(), O_WRONLY | O_RSYNC | O_DSYNC); #endif if (mFIFODescriptor == -1) { throw IOError( "ResultsInterface::ResultsInterface: " "Can't open FIFO file."); } } if (write(mFIFODescriptor, bytes, bytesCount) != bytesCount) { close(mFIFODescriptor); #ifdef MAC_OS mFIFODescriptor = open( FIFOFilePath().c_str(), O_WRONLY | O_DSYNC); #endif #ifdef LINUX mFIFODescriptor = open( FIFOFilePath().c_str(), O_WRONLY | O_RSYNC | O_DSYNC); #endif if (mFIFODescriptor == -1) { throw IOError( "ResultsInterface::ResultsInterface: " "Can't open FIFO file."); } if (write(mFIFODescriptor, bytes, bytesCount) != bytesCount) { throw IOError( "ResultsInterface::writeResult: " "Can't write result to the disk."); } } #ifdef TESTS__TRUSTLINES Timestamp start_time_s = posix::microsec_clock::universal_time(); MicrosecondsTimestamp starttime_m = microsecondsTimestamp(start_time_s); auto debug = mLog.debug("ResultsInterface"); debug << starttime_m; #endif }
void ClientImpl::readBody(std::string& s) { s.clear(); _chunkedEncoding = _replyHeader.chunkedTransferEncoding(); _chunkedIStream.reset(); if (_chunkedEncoding) { log_debug("read body with chunked encoding"); char ch; while (_chunkedIStream.get(ch)) s += ch; log_debug("eod=" << _chunkedIStream.eod()); if (!_chunkedIStream.eod()) throw IOError("error reading HTTP reply body: incomplete chunked data stream"); } else { unsigned n = _replyHeader.contentLength(); log_debug("read body; content-size: " << n); s.reserve(n); char ch; while (n-- && _stream.get(ch)) s += ch; if (_stream.fail()) throw IOError("error reading HTTP reply body"); //log_debug("body read: \"" << s << '"'); } if (!_replyHeader.keepAlive()) { log_debug("close socket - no keep alive"); _socket.close(); } else { log_debug("do not close socket - keep alive"); } }
size_t TcpSocket::onBeginWrite(const char* buffer, size_t n) { if (!_impl->isConnected()) throw IOError("socket not connected when trying to read"); return _impl->beginWrite(buffer, n); }
std::size_t PipeIODevice::onEndRead(EventLoop& loop, char* buffer, std::size_t n, bool& eof) { loop.selector().disable(_ioh); DWORD readBytes = 0; DWORD flags = 0; eof = false; // TODO: can we receive EOF? if (_bufferSize) { readBytes = _bufferSize; } else if ( FALSE == ReadMsgQueue(handle(), &_buffer[0], _msgSize, &readBytes, INFINITE, &flags) ) { throw IOError( PT_ERROR_MSG("Could not read from message queue handle") ); } DWORD bytesToCopy = std::min<DWORD>(_rbuflen, readBytes); memcpy(_rbuf, &_buffer[0], bytesToCopy); _bufferSize = 0; if (_rbuflen >= readBytes) return readBytes; std::vector<char>::iterator beginData = (_buffer.begin() + bytesToCopy); std::vector<char>::iterator endData = (_buffer.begin() + readBytes); std::copy(beginData, endData, _buffer.begin()); _bufferSize = (readBytes - bytesToCopy); return bytesToCopy; }
ROOTNtuple::ROOTNtuple(const std::string& fileName_, const std::string& treeName_){ fROOTFile = new TFile(fileName_.c_str()); if (fROOTFile->IsZombie()){ delete fROOTFile; throw IOError("ROOTNtuple::File Does not Exist! or is Zombie " + fileName_); } fNtuple = dynamic_cast<TNtuple*>(fROOTFile -> Get(treeName_.c_str())); if(!fNtuple){ delete fROOTFile; throw IOError(Formatter()<<"ROOTNtuple::Tree does not exist, or isn't an ntuple! tree : " << treeName_ << ", filename: "<<fileName_); } }
std::vector<std::string> TextFile::read() { if(!isValid_() || !isReading()) { throw IOError("File is not open for reading"); } for(const auto& symbol : skipSymbols_) { if(boost::starts_with(next_line_, symbol)) { advanceLine_(); return read(); } } std::vector<std::string> sline; boost::split(sline, next_line_, boost::is_any_of(delimiter_)); for(auto& s : sline) { boost::trim(s); } // Prefetch the next line from the file. advanceLine_(); return sline; }
size_t TcpSocket::onBeginRead(char* buffer, size_t n, bool& eof) { if (!_impl->isConnected()) throw IOError("socket not connected when trying to read"); return _impl->beginRead(buffer, n, eof); }
// =========================================================================== // method definitions // =========================================================================== OutputDevice_File::OutputDevice_File(const std::string& fullName, const bool binary) : OutputDevice(binary), myFileStream(0) { #ifdef WIN32 if (fullName == "/dev/null") { myFileStream = new std::ofstream("NUL"); #else if (fullName == "nul" || fullName == "NUL") { myFileStream = new std::ofstream("/dev/null"); #endif } else { myFileStream = new std::ofstream(fullName.c_str(), binary ? std::ios::binary : std::ios_base::out); } if (!myFileStream->good()) { delete myFileStream; throw IOError("Could not build output file '" + fullName + "'."); } } OutputDevice_File::~OutputDevice_File() { myFileStream->close(); delete myFileStream; } std::ostream& OutputDevice_File::getOStream() { return *myFileStream; }
void write_supertensor_index(const string& name, const vector<Ref<const supertensor_reader_t>>& readers) { // Check consistency GEODE_ASSERT(readers.size()); const uint32_t slice = readers[0]->header.stones; const auto sections = descendent_sections(section_t(),slice).at(slice); GEODE_ASSERT(sections->slice==int(slice)); Hashtable<section_t,Ref<const supertensor_reader_t>> section_reader; for (const auto reader : readers) { GEODE_ASSERT(int(reader->header.filter)==filter); section_reader.set(reader->header.section,reader); } for (const auto section : sections->sections) GEODE_ASSERT(section_reader.contains(section)); // Write index FILE* file = fopen(name.c_str(),"wb"); if (!file) throw IOError(format("write_supertensor_index: can't open '%s' for writing",name)); fwrite("pentago index \n",1,20,file); fwrite(&slice,sizeof(uint32_t),1,file); GEODE_ASSERT(ftell(file)==24); for (const auto section : sections->sections) { const auto reader = section_reader.get(section); Array<compact_blob_t> blobs(reader->offset.flat.size(),uninit); for (const int i : range(blobs.size())) { const uint64_t offset = reader->offset.flat[i]; blobs[i].set_offset(offset); blobs[i].size = reader->compressed_size_.flat[i]; } fwrite(blobs.data(),sizeof(compact_blob_t),blobs.size(),file); } const auto index = new_<supertensor_index_t>(sections); GEODE_ASSERT(uint64_t(ftell(file))==index->section_offset.back()); fclose(file); }
int __cdecl SocketPosixC__Create(int/*boolean*/ reliable) { int one = 1; int fd = socket(AF_INET, reliable ? SOCK_STREAM : SOCK_DGRAM, 0); if (fd == -1) { Exception e = Unexpected; switch (GetSocketError()) { #ifdef _WIN32 case WSAEMFILE: #else case EMFILE: case ENFILE: #endif e = NoResources; break; } IOError(e); } #ifndef _WIN32 MakeNonBlocking(fd); #endif setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); return fd; }
unsigned char GetUChar() { int c = ::fgetc(f_); if(c == EOF) IOError(name_, "fgetc EOF or read error"); return static_cast<unsigned char>(c); }
void AbstractReptile::load() { QFile file("temp.data"); if(!file.exists()) return; if(file.open(QFile::ReadOnly|QFile::Text)) { QTextStream in(&file); QString line=in.readLine(); this->currentUrl.setUrl(line); line=in.readLine(); this->analysisData.setPattern(line); line=in.readLine(); this->analysisNextUrl.setPattern(line); line=in.readLine(); while(line!=QString("")) { allUrl.insert(line,1); line=in.readLine(); } line=in.readLine(); while(!in.atEnd()) { data.insert(line,1); line=in.readLine(); } } else { emit IOError(QString("Read file error: open fall.")); } file.close(); }
static void QRemove( char *filename ) /***********************************/ { if( remove( filename ) != 0 ) { IOError( "problem removing file" ); } }
static const char* RoughEncoding(InStm *stm) { // usually it's enough to test the first byte only... switch(stm->GetUChar()) { case 0x00: return "UTF-32BE"; case 0x0E: return "SCSU"; case 0x2B: return "UTF-7"; case '<': return "UTF-8"; // temporary assume UTF-8 XML case 0x84: return "GB-18030"; case 0xEF: return "UTF-8"; case 0xDD: return "UTF-EBCDIC"; case 0xF7: return "UTF-1"; case 0xFB: return "BOCU-1"; case 0xFE: return "UTF-16BE"; case 0xFF: if(stm->GetUChar() != 0xFE) break; return stm->GetUChar() ? "UTF-16LE" : "UTF-32LE"; default: break; } IOError(stm->UIFileName(), "bad XML or unknown encoding"); return NULL; }
void AbstractReptile::save() { QFile file("temp.data"); if(file.open(QFile::WriteOnly|QFile::Text)) { QTextStream out(&file); out<<this->currentUrl.toString()<<"\n"; out<<this->analysisData.pattern()<<"\n"; out<<this->analysisNextUrl.pattern()<<"\n"; for(auto i:this->allUrl.keys()) { out<<i<<"\n"; } out<<"\n"; for(auto i:this->data.keys()) { out<<i<<"\n"; } } else { emit IOError(QString("Write file error: open fall.")); } file.close(); }
void NeuronsImageWriter::write() { // make sure we have a recent id map updateInputs(); unsigned int numSections = _idMap->size(); if (_annotation) { _directory += std::string("_") + boost::lexical_cast<std::string>(*_annotation); } // prepare the output directory boost::filesystem::path directory(_directory); if (!boost::filesystem::exists(directory)) { boost::filesystem::create_directory(directory); } else if (!boost::filesystem::is_directory(directory)) { BOOST_THROW_EXCEPTION(IOError() << error_message(std::string("\"") + _directory + "\" is not a directory") << STACK_TRACE); } // save output images for (unsigned int i = 0; i < numSections; i++) { std::stringstream filename; filename << _directory << "/" << _basename << std::setw(4) << std::setfill('0') << i << ".tiff"; vigra::exportImage(srcImageRange(*(*_idMap)[i]), vigra::ImageExportInfo(filename.str().c_str())); } }
void FileDeviceImpl::resize(off_type size) { int ret = ::ftruncate(fd(), size); if(ret != 0) throw IOError( PT_ERROR_MSG("ftruncate failed") ); }
FileDeviceImpl::pos_type FileDeviceImpl::seek(off_type offset, std::ios::seekdir sd ) { int whence = std::ios::cur; switch(sd) { case std::ios::beg: whence = SEEK_SET; break; case std::ios::cur: whence = SEEK_CUR; break; case std::ios::end: whence = SEEK_END; break; default: break; } off_t ret = lseek(fd(), offset, whence); if( ret == (off_t)-1 ) throw IOError( PT_ERROR_MSG("lseek failed") ); return ret; }
void TcpSocketImpl::endConnect() { log_trace("ending connect"); if(_pfd && ! _socket.wbuf()) { _pfd->events &= ~POLLOUT; } checkPendingError(); if( _isConnected ) return; try { while (true) { pollfd pfd; pfd.fd = this->fd(); pfd.revents = 0; pfd.events = POLLOUT; log_debug("wait " << timeout() << " ms"); bool avail = this->wait(this->timeout(), pfd); if (avail) { // something has happened int sockerr = checkConnect(); if (_isConnected) return; if (++_addrInfoPtr == _addrInfo.impl()->end()) { // no more addrInfo - propagate error throw IOError(connectFailedMessage(_addrInfo, sockerr)); } } else if (++_addrInfoPtr == _addrInfo.impl()->end()) { log_debug("timeout"); throw IOTimeout(); } close(); _connectResult = tryConnect(); if (_isConnected) return; checkPendingError(); } } catch(...) { close(); throw; } }
std::size_t PipeIODevice::onWrite(const char* buffer, std::size_t count) { if( Write != _mode ) throw IOError( PT_ERROR_MSG("Could not write on a read only pipe") ); DWORD timeout = _timeout == EventLoop::WaitInfinite ? INFINITE : static_cast<std::size_t>(_timeout); DWORD bytesToWrite = std::min<DWORD>(count, _msgSize); if ( FALSE == WriteMsgQueue(handle(), (LPVOID) buffer, bytesToWrite, timeout, 0)) { throw IOError("WriteMsgQueue failed"); } return bytesToWrite; }
void file::write(types::str const &str) { if (not is_open) throw ValueError("I/O operation on closed file"); if (mode.find_first_of("wa+") == -1) throw IOError("file.write() : File not opened for writing."); fwrite(str.c_str(), sizeof(char), str.size(), **data); }
void file::seek(int offset, int whence) { if (not is_open) throw ValueError("I/O operation on closed file"); if (whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END) throw IOError("file.seek() : Invalid argument."); fseek(**data, offset, whence); }
void PipeIODevice::writeMessage(const char* buffer, std::size_t count) { DWORD timeout = _timeout == EventLoop::WaitInfinite ? INFINITE : static_cast<std::size_t>(_timeout); if( FALSE == WriteMsgQueue(handle(), (LPVOID) buffer, count, timeout, 0) ) throw IOError("WriteMsgQueue failed"); }