void Database::_WriteBlock(Item towrite, uint64_t blockpos) { backing.seekg(blockpos); backing.seekp(blockpos); uint64_t blkhdrs[4]; if (backing.eof()) { blkhdrs[0] = 0; blkhdrs[1] = towrite.itemsize; blkhdrs[2] = towrite.itemsize; blkhdrs[3] = towrite.itemsize + (sizeof(uint64_t)*4) + 1; backing.write(0, 1); backing.write((char*)&blkhdrs, sizeof(uint64_t)*4); backing.write(towrite.item, towrite.itemsize); backing.flush(); } else { _GetLock(blockpos, true); backing.read((char*)blkhdrs, sizeof(uint64_t)*4); if (towrite.itemsize + 1 + (sizeof(uint64_t)*4) <= blkhdrs[3]) { //Block large enough or non-existent block backing.write(0, 1); blkhdrs[0] = 0; blkhdrs[1] = towrite.itemsize; blkhdrs[2] = towrite.itemsize; //Keep blkhdrs[3] backing.write((char*)&blkhdrs, sizeof(uint64_t)*4); backing.write(towrite.item, towrite.itemsize); backing.flush(); _ReleaseLock(blockpos, true); } else { //Have to continue to a new block, allocate or reuse std::streampos currentpos = backing.tellp(); backing.seekp(0, std::ios::end); std::streampos endpos = backing.tellp(); backing.seekp(currentpos, std::ios::beg); blkhdrs[0] = (uint64_t) endpos; //Keep blkhdrs[1] blkhdrs[2] = towrite.itemsize; //Keep blkhdrs[3] uint64_t blockcancontain = blkhdrs[3] - ((sizeof(uint64_t)*4) + 1); backing.write(0, 1); backing.write((char*)&blkhdrs, sizeof(uint64_t)*4); backing.write(towrite.item, blockcancontain); backing.flush(); _ReleaseLock(blockpos, true); //Continue to next block! Item process; process.itemsize = towrite.itemsize-blockcancontain; process.item = towrite.item+blockcancontain; _WriteBlock(process, blockpos); } } }
void IndexDataStructure::writeAt(std::fstream & _output_file, std::streampos _position) { // remember the write file position std::streampos w_pos = _output_file.tellp(); // go to asked write position _output_file.seekp(_position); writeToNext(_output_file); // restore original write position _output_file.seekp(w_pos); }
int User::writeUNPW(std::fstream &fout){ if (!fout.is_open()){ fout.open("UNPW.bin", std::ios::out, std::ios::binary); } auto fpos = fout.cur; fout.seekp(0, fout.beg); encode(username); encode(password); fout.write((char*)&username, sizeof(str)); fout.write((char*)&password, sizeof(str)); decode(username); decode(password); fout.seekp(0, fpos); return 0; }
uint64_t erase (const size_t offset, std::fstream &lib) { // ar header format: // offset, length, description // 0 16 File name, ASCII // 16 12 File modification timestamp, Decimal // 28 6 Owner ID, Decimal // 34 6 Group ID, Decimal // 40 8 File mode, Octal // 48 10 File size in bytes, Decimal // 58 2 File magic, 0x60 0x0A // we will use this a lot. this are right-padded. const char zero = 0x30; const char pad = 0x20; // first of all, archive creation timestamp goes to zero lib.seekp (static_cast<int64_t> (offset + 16)); lib.put (zero); for (size_t i = 0; i < 11; ++i) lib.put (pad); // now the uid: lib.seekp (static_cast<int64_t> (offset + 28)); lib.put (zero); for (size_t i = 0; i < 5; ++i) lib.put (pad); // and gid lib.seekp (static_cast<int64_t> (offset + 34)); lib.put (zero); for (size_t i = 0; i < 5; ++i) lib.put (pad); // return the size: lib.seekg (static_cast<int64_t> (offset + 48)); char size[10]; lib.read (size, 10); for (ssize_t i = 9; i >= 0; --i) { if (size[i] == pad) size[i] = '\0'; } uint64_t ret = static_cast<uint64_t> (std::atol (size)); if (ret % 2 != 0) ++ret; // everything is aligned to uint16_t, but file size is the // actual file size. ret += 60; // add header size return ret; }
void Database::_ReleaseLock(uint64_t blockpos, bool readonly) { if (readonly) { char blocklock = 0; backing.seekp(blockpos); backing.write(&blocklock, 1); } }
long long TCPTransport::recvfile(std::fstream& ofs, long long offset, long long size) { if (!m_bConnected) return -1; if (ofs.bad() || ofs.fail()) return -1; ofs.seekp(offset); int block = 1000000; char* buf = new char[block]; long long recd = 0; while (recd < size) { int unit = int((size - recd) > block ? block : size - recd); recv(buf, unit); ofs.write(buf, unit); recd += unit; } delete [] buf; return recd; }
bool XorEncryptor::encryptData(std::fstream &original, std::fstream &result) { if (!original.is_open() || !result.is_open()) { return false; } original.seekg(0, std::ios::beg); result.seekp(0, std::ios::beg); char c = 0; unsigned i = 0; while (original.good()) { original.read(&c, 1); c ^= password[i]; if(original.gcount() > 0) { result.write(&c, 1); } if (++i == passSize) { i = 0; } } original.seekg(0, std::ios::beg); result.seekg(0, std::ios::beg); result.flush(); return true; }
bool StandardFileProvider::seek( Size pos ) { if( _seekg ) _fstream.seekg( pos, ios::beg ); if( _seekp ) _fstream.seekp( pos, ios::beg ); return _fstream.fail(); }
BlockOffset writeBlockToFile(Block b, std::fstream& f) { BlockOffset off = f.tellp(); f.write(reinterpret_cast<char *>(&b.data), sizeof(b.data)); for(auto& o : b.offsets) { f.write(reinterpret_cast<char *>(&o), sizeof(o)); } f.seekp(off, f.beg); return off; }
void resultsfile::checkformodifications(std::fstream& file) { assert(file.good()); trace << "DEBUG (resultsfile): checking file for modifications." << std::endl; // check for user modifications sha curdigest; file.seekg(0); curdigest.process(file); // reset file file.clear(); if (curdigest == filedigest) file.seekp(fileptr); else { cerr << "NOTICE: file modifications found - appending." << std::endl; // set current write position to end-of-file file.seekp(0, std::ios_base::end); fileptr = file.tellp(); } }
void filesystem::supercluster::save(std::fstream& s, uint32_t index) { uint64_t offset = SUPERCLUSTER_SIZE * index; char buffer[CLUSTER_SIZE]; for(unsigned i = 0; i < CLUSTERS_PER_SUPER; i++) serialization::u32b(buffer + 4 * i, clusters[i]); s.clear(); s.seekp(offset, std::ios_base::beg); s.write(buffer, CLUSTER_SIZE); if(!s) throw std::runtime_error("Can't write cluster table"); }
int serialize(linked_list<T>& list, std::fstream& out) { if(!out.is_open()) throw serialize_exception(); serialize(LINKEDLIST_SERIAL_ID, out); int byte_count_pos = out.tellp(); out.seekp(sizeof(int), std::ios::cur); int byte_count = 0; byte_count += serialize(list.list_size, out); for(typename linked_list<T>::link* current = (list.head)->next; current != NULL; current = current->next) byte_count += serialize(current->element, out); int end_pos = out.tellp(); out.seekp(byte_count_pos); serialize(byte_count, out); out.seekp(end_pos); return byte_count + sizeof(int) + sizeof(long); // content + header }
void WriteSector(char* buffer, size_t sec) { sec += partitionOffset; if (sec > partitionLength) return; if ((sec * 512) > fsize) { std::cout << "WOAH BRO THIS SECTOR IS WRONG: " << sec << std::endl; return; } f.seekp(sec * 512, std::ios::beg); f.write(buffer, 512); }
void IndexDataStructure::writeToNext(std::fstream & _output_file) { m_is_indexed = true; m_index_position = _output_file.tellp(); _output_file.write((char *)(&m_count), sizeof(m_count)); _output_file.write((char *)(m_keys), sizeof(m_keys[0]) * m_max_count); _output_file.write((char *)(m_addresses), sizeof(m_addresses[0]) * m_max_count); _output_file.write((char *)(&m_next_sibling_address), sizeof(m_next_sibling_address)); // skip free block space _output_file.seekp(m_block_size - m_real_data_size, _output_file.cur); }
void Database::_GetLock(uint64_t blockpos, bool readonly) { char blocklock = 1; backing.seekg(blockpos); backing.read(&blocklock, 1); while (blocklock == 1) { std::this_thread::sleep_for(std::chrono::milliseconds(50)); //Thanks StackOverflow! backing.seekg(blockpos); backing.read(&blocklock, 1); } if (readonly) { blocklock = 1; backing.seekp(blockpos); backing.write(&blocklock, 1); } }
exp bool RawWrite(const char* fname, size_t offsetIn, size_t offset, size_t len) { std::ifstream in(fname, std::ios::in | std::ios::binary); if (!in.is_open()) return false; // Seek to proper offsets in.seekg(offsetIn, std::ios::beg); f.seekp(offset + partitionOffset * 512, std::ios::beg); // Read data char* buffer = new char[len]; in.read(buffer, len); f.write(buffer, len); delete[] buffer; in.close(); return true; }
bool History::open(bool bWrite, std::fstream &f) { char buffer[64]; snprintf(buffer, sizeof(buffer), "history%c%lu.history", #ifdef WIN32 '\\', #else '/', #endif m_nUin); string fname; pMain->buildFileName(fname, buffer); f.open(fname.c_str(), bWrite ? ios::out | ios::app : ios::in); if (!f.is_open()){ log(L_WARN, "File %s not open", fname.c_str()); return false; } if (bWrite) f.seekp(0, ios::end); return true; }
void push_back(uint8_t x) { m_write_buf[m_widx] = x; if (m_sync) { m_read_buf[m_widx] = x; } ++m_widx; if (m_widx == m_buffer_size) { if (!m_sync) { // if not sync, write block to disk if (!m_stream.is_open()) { m_stream.open(m_file_name, std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc); } m_stream.seekp(m_buffer_size * (m_wb++), std::ios::beg); m_stream.write((char*)m_write_buf, m_buffer_size); ++m_disk_buffered_blocks; } m_sync = 0; m_widx = 0; } }
BlockOffset seekRW(std::fstream& f, BlockOffset off, std::ios_base::seekdir dir = std::ios_base::beg) { f.seekp(off, dir); f.seekg(off, dir); return off; }
template<class V> void save(const V* var) const { m_stream->seekp(m_pos + (reinterpret_cast<const char*>(var) - reinterpret_cast<const char*>(&m_data)), std::ios::beg); m_stream->write(reinterpret_cast<const char*>(var), sizeof(V)); const_cast<U*>(dynamic_cast<const U*>(this))->saved(); }