4 #include <lib/base/rawfile.h>
5 #include <lib/base/eerror.h>
28 int eRawFile::open(const char *filename, int cached)
32 m_basename = filename;
38 m_fd = ::open(filename, O_RDONLY | O_LARGEFILE);
42 m_file = ::fopen64(filename, "rb");
49 void eRawFile::setfd(int fd)
57 off_t eRawFile::lseek(off_t offset, int whence)
59 // eDebug("lseek: %lld, %d", offset, whence);
60 /* if there is only one file, use the native lseek - the file could be growing! */
64 return ::lseek(m_fd, offset, whence);
67 ::fseeko(m_file, offset, whence);
68 return ::ftello(m_file);
74 m_current_offset = offset;
77 m_current_offset += offset;
80 m_current_offset = m_totallength + offset;
84 if (m_current_offset < 0)
86 return m_current_offset;
100 int ret = ::close(m_fd);
106 ssize_t eRawFile::read(void *buf, size_t count)
108 // eDebug("read: %p, %d", buf, count);
109 switchOffset(m_current_offset);
113 if (m_current_offset + count > m_totallength)
114 count = m_totallength - m_current_offset;
122 ret = ::read(m_fd, buf, count);
124 ret = ::fread(buf, 1, count, m_file);
127 m_current_offset = m_last_offset += ret;
131 int eRawFile::valid()
139 void eRawFile::scan()
143 while (m_nrfiles < 1000) /* .999 is the last possible */
147 int f = openFileUncached(m_nrfiles);
151 m_splitsize = ::lseek(f, 0, SEEK_END);
152 m_totallength += ::lseek(f, 0, SEEK_END);
156 FILE *f = openFileCached(m_nrfiles);
159 ::fseeko(f, 0, SEEK_END);
161 m_splitsize = ::ftello(f);
162 m_totallength += ::ftello(f);
168 // eDebug("found %d files, splitsize: %llx, totallength: %llx", m_nrfiles, m_splitsize, m_totallength);
171 int eRawFile::switchOffset(off_t off)
175 int filenr = off / m_splitsize;
176 if (filenr >= m_nrfiles)
177 filenr = m_nrfiles - 1;
178 if (filenr != m_current_file)
180 // eDebug("-> %d", filenr);
183 m_fd = openFileUncached(filenr);
185 m_file = openFileCached(filenr);
186 m_last_offset = m_base_offset = m_splitsize * filenr;
187 m_current_file = filenr;
192 if (off != m_last_offset)
195 m_last_offset = ::lseek(m_fd, off - m_base_offset, SEEK_SET) + m_base_offset;
198 ::fseeko(m_file, off - m_base_offset, SEEK_SET);
199 m_last_offset = ::ftello(m_file) + m_base_offset;
201 return m_last_offset;
204 return m_last_offset;
209 FILE *eRawFile::openFileCached(int nr)
211 std::string filename = m_basename;
215 snprintf(suffix, 5, ".%03d", nr);
218 return ::fopen64(filename.c_str(), "rb");
222 int eRawFile::openFileUncached(int nr)
224 std::string filename = m_basename;
228 snprintf(suffix, 5, ".%03d", nr);
231 return ::open(filename.c_str(), O_RDONLY | O_LARGEFILE);
234 off_t eRawFile::length()
236 return m_totallength;
239 off_t eRawFile::position()
244 return ::lseek(m_fd, 0, SEEK_CUR);
246 return ::fseeko(m_file, 0, SEEK_CUR);
248 return m_current_offset;
251 eSingleLock &eRawFile::getLock()