3 #include <lib/base/rawfile.h>
4 #include <lib/base/eerror.h>
23 int eRawFile::open(const char *filename)
26 m_basename = filename;
30 m_fd = ::open(filename, O_RDONLY | O_LARGEFILE);
34 void eRawFile::setfd(int fd)
41 off_t eRawFile::lseek(off_t offset, int whence)
43 // eDebug("lseek: %lld, %d", offset, whence);
44 /* if there is only one file, use the native lseek - the file could be growing! */
46 return ::lseek(m_fd, offset, whence);
50 m_current_offset = offset;
53 m_current_offset += offset;
56 m_current_offset = m_totallength + offset;
60 if (m_current_offset < 0)
62 return m_current_offset;
67 int ret = ::close(m_fd);
72 ssize_t eRawFile::read(void *buf, size_t count)
74 // eDebug("read: %p, %d", buf, count);
75 switchOffset(m_current_offset);
79 if (m_current_offset + count > m_totallength)
80 count = m_totallength - m_current_offset;
85 int ret = ::read(m_fd, buf, count);
87 m_current_offset = m_last_offset += ret;
100 while (m_nrfiles < 1000) /* .999 is the last possible */
102 int f = openFile(m_nrfiles);
106 m_splitsize = ::lseek(f, 0, SEEK_END);
107 m_totallength += ::lseek(f, 0, SEEK_END);
112 // eDebug("found %d files, splitsize: %llx, totallength: %llx", m_nrfiles, m_splitsize, m_totallength);
115 int eRawFile::switchOffset(off_t off)
119 int filenr = off / m_splitsize;
120 if (filenr >= m_nrfiles)
121 filenr = m_nrfiles - 1;
122 if (filenr != m_current_file)
124 // eDebug("-> %d", filenr);
126 m_fd = openFile(filenr);
127 m_last_offset = m_base_offset = m_splitsize * filenr;
128 m_current_file = filenr;
133 if (off != m_last_offset)
135 // eDebug("%llx != %llx", off, m_last_offset);
136 m_last_offset = ::lseek(m_fd, off - m_base_offset, SEEK_SET) + m_base_offset;
137 // eDebug("offset now %llx", m_last_offset);
138 return m_last_offset;
141 // eDebug("offset already ok");
142 return m_last_offset;
146 int eRawFile::openFile(int nr)
148 std::string filename = m_basename;
152 snprintf(suffix, 5, ".%03d", nr);
155 return ::open(filename.c_str(), O_RDONLY);