/*************************************************************** -*- c++ -*- * Copyright (c) 2003,2004 by Marcel Wiesweg * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include "packedstorage.h" PackedStorage::PackedStorage(int maxMB) : LegacyStorage(maxMB) { } #define TOC_SIZE 8 //The file structure is simple: // TOC_SIZE*PageAddress contains the numbers of the following pages // TOC_SIZE*TELETEXT_PAGESIZE contains the page data //and the same again. bool PackedStorage::seekTo(PageID page, int desc, bool create) { lseek(desc, 0, SEEK_SET); PageAddress addr[TOC_SIZE]; while (::read(desc, addr, sizeof(addr)) == sizeof(addr)) { lseek(desc, 0, SEEK_CUR); for (int index=0; indexmaxBytes ) freeSpace(); return (StorageHandle)desc; } //no space on disk? make some space available if (errno == ENOSPC) freeSpace(); //second try desc=open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (desc==-1 && !wroteError) { //report error to syslog - once! wroteError=true; esyslog("OSD-Teletext: Error opening teletext file %s: %s", filename, strerror(errno)); } if (desc==-1) return StorageHandle(); else if (!seekTo(page, desc, true)) { ::close(desc); return StorageHandle(); } if ( maxBytes && byteCount>maxBytes ) freeSpace(); return (StorageHandle)desc; } StorageHandle PackedStorage::openForReading(PageID page, bool countAsAccess) { int desc; if ( (desc=(int)LegacyStorage::openForReading(page, false))!= -1 ) { if (!seekTo(page, desc, false)) { //this is not an error condition here, may and shall happen! ::close(desc); } else { return (StorageHandle)desc; } } return StorageHandle(); }