X-Git-Url: https://git.cweiske.de/enigma2.git/blobdiff_plain/08ee4eef0af570f64937fd7a20c5d71dfb89cabc..1a2a0e2f94f8f9ba64b1e2d11b42bec88ed612d0:/lib/base/rawfile.cpp diff --git a/lib/base/rawfile.cpp b/lib/base/rawfile.cpp index 0d8a93b5..3a09e07e 100644 --- a/lib/base/rawfile.cpp +++ b/lib/base/rawfile.cpp @@ -1,9 +1,13 @@ +#include #include #include #include #include +DEFINE_REF(eRawFile); + eRawFile::eRawFile() + :m_lock(false) { m_fd = -1; m_file = 0; @@ -51,6 +55,13 @@ void eRawFile::setfd(int fd) } off_t eRawFile::lseek(off_t offset, int whence) +{ + eSingleLocker l(m_lock); + m_current_offset = lseek_internal(offset, whence); + return m_current_offset; +} + +off_t eRawFile::lseek_internal(off_t offset, int whence) { // eDebug("lseek: %lld, %d", offset, whence); /* if there is only one file, use the native lseek - the file could be growing! */ @@ -60,7 +71,8 @@ off_t eRawFile::lseek(off_t offset, int whence) return ::lseek(m_fd, offset, whence); else { - ::fseeko(m_file, offset, whence); + if (::fseeko(m_file, offset, whence) < 0) + perror("fseeko"); return ::ftello(m_file); } } @@ -99,11 +111,19 @@ int eRawFile::close() } } -ssize_t eRawFile::read(void *buf, size_t count) +ssize_t eRawFile::read(off_t offset, void *buf, size_t count) { -// eDebug("read: %p, %d", buf, count); + eSingleLocker l(m_lock); + + if (offset != m_current_offset) + { + m_current_offset = lseek_internal(offset, SEEK_SET); + if (m_current_offset < 0) + return m_current_offset; + } + switchOffset(m_current_offset); - + if (m_nrfiles >= 2) { if (m_current_offset + count > m_totallength) @@ -140,7 +160,7 @@ void eRawFile::scan() { if (!m_cached) { - int f = openFile(m_nrfiles); + int f = openFileUncached(m_nrfiles); if (f < 0) break; if (!m_nrfiles) @@ -149,7 +169,7 @@ void eRawFile::scan() ::close(f); } else { - FILE *f = (FILE*)openFile(m_nrfiles); + FILE *f = openFileCached(m_nrfiles); if (!f) break; ::fseeko(f, 0, SEEK_END); @@ -176,9 +196,9 @@ int eRawFile::switchOffset(off_t off) // eDebug("-> %d", filenr); close(); if (!m_cached) - m_fd = openFile(filenr); + m_fd = openFileUncached(filenr); else - m_file = (FILE*)openFile(filenr); + m_file = openFileCached(filenr); m_last_offset = m_base_offset = m_splitsize * filenr; m_current_file = filenr; } @@ -201,7 +221,8 @@ int eRawFile::switchOffset(off_t off) } } -int eRawFile::openFile(int nr) +/* m_cached */ +FILE *eRawFile::openFileCached(int nr) { std::string filename = m_basename; if (nr) @@ -210,8 +231,23 @@ int eRawFile::openFile(int nr) snprintf(suffix, 5, ".%03d", nr); filename += suffix; } - if (!m_cached) - return ::open(filename.c_str(), O_RDONLY | O_LARGEFILE); - else - return (int)::fopen64(filename.c_str(), "rb"); + return ::fopen64(filename.c_str(), "rb"); +} + +/* !m_cached */ +int eRawFile::openFileUncached(int nr) +{ + std::string filename = m_basename; + if (nr) + { + char suffix[5]; + snprintf(suffix, 5, ".%03d", nr); + filename += suffix; + } + return ::open(filename.c_str(), O_RDONLY | O_LARGEFILE); +} + +off_t eRawFile::length() +{ + return m_totallength; }