4 #include <lib/base/rawfile.h>
5 #include <lib/base/eerror.h>
25 int eRawFile::open(const char *filename, int cached)
29 m_basename = filename;
35 m_fd = ::open(filename, O_RDONLY | O_LARGEFILE);
39 m_file = ::fopen64(filename, "rb");
46 void eRawFile::setfd(int fd)
54 off_t eRawFile::lseek(off_t offset, int whence)
56 // eDebug("lseek: %lld, %d", offset, whence);
57 /* if there is only one file, use the native lseek - the file could be growing! */
61 return ::lseek(m_fd, offset, whence);
64 ::fseeko(m_file, offset, whence);
65 return ::ftello(m_file);
71 m_current_offset = offset;
74 m_current_offset += offset;
77 m_current_offset = m_totallength + offset;
81 if (m_current_offset < 0)
83 return m_current_offset;
97 int ret = ::close(m_fd);
103 ssize_t eRawFile::read(void *buf, size_t count)
105 // eDebug("read: %p, %d", buf, count);
106 switchOffset(m_current_offset);
110 if (m_current_offset + count > m_totallength)
111 count = m_totallength - m_current_offset;
119 ret = ::read(m_fd, buf, count);
121 ret = ::fread(buf, 1, count, m_file);
124 m_current_offset = m_last_offset += ret;
128 int eRawFile::valid()
136 void eRawFile::scan()
140 while (m_nrfiles < 1000) /* .999 is the last possible */
144 int f = openFileUncached(m_nrfiles);
148 m_splitsize = ::lseek(f, 0, SEEK_END);
149 m_totallength += ::lseek(f, 0, SEEK_END);
153 FILE *f = openFileCached(m_nrfiles);
156 ::fseeko(f, 0, SEEK_END);
158 m_splitsize = ::ftello(f);
159 m_totallength += ::ftello(f);
165 // eDebug("found %d files, splitsize: %llx, totallength: %llx", m_nrfiles, m_splitsize, m_totallength);
168 int eRawFile::switchOffset(off_t off)
172 int filenr = off / m_splitsize;
173 if (filenr >= m_nrfiles)
174 filenr = m_nrfiles - 1;
175 if (filenr != m_current_file)
177 // eDebug("-> %d", filenr);
180 m_fd = openFileUncached(filenr);
182 m_file = openFileCached(filenr);
183 m_last_offset = m_base_offset = m_splitsize * filenr;
184 m_current_file = filenr;
189 if (off != m_last_offset)
192 m_last_offset = ::lseek(m_fd, off - m_base_offset, SEEK_SET) + m_base_offset;
195 ::fseeko(m_file, off - m_base_offset, SEEK_SET);
196 m_last_offset = ::ftello(m_file) + m_base_offset;
198 return m_last_offset;
201 return m_last_offset;
206 FILE *eRawFile::openFileCached(int nr)
208 std::string filename = m_basename;
212 snprintf(suffix, 5, ".%03d", nr);
215 return ::fopen64(filename.c_str(), "rb");
219 int eRawFile::openFileUncached(int nr)
221 std::string filename = m_basename;
225 snprintf(suffix, 5, ".%03d", nr);
228 return ::open(filename.c_str(), O_RDONLY | O_LARGEFILE);
231 off_t eRawFile::length()
233 return m_totallength;