3 #include <lib/base/rawfile.h>
4 #include <lib/base/eerror.h>
24 int eRawFile::open(const char *filename, int cached)
28 m_basename = filename;
34 m_fd = ::open(filename, O_RDONLY | O_LARGEFILE);
38 m_file = ::fopen64(filename, "rb");
45 void eRawFile::setfd(int fd)
53 off_t eRawFile::lseek(off_t offset, int whence)
55 // eDebug("lseek: %lld, %d", offset, whence);
56 /* if there is only one file, use the native lseek - the file could be growing! */
60 return ::lseek(m_fd, offset, whence);
63 ::fseeko(m_file, offset, whence);
64 return ::ftello(m_file);
70 m_current_offset = offset;
73 m_current_offset += offset;
76 m_current_offset = m_totallength + offset;
80 if (m_current_offset < 0)
82 return m_current_offset;
96 int ret = ::close(m_fd);
102 ssize_t eRawFile::read(void *buf, size_t count)
104 // eDebug("read: %p, %d", buf, count);
105 switchOffset(m_current_offset);
109 if (m_current_offset + count > m_totallength)
110 count = m_totallength - m_current_offset;
118 ret = ::read(m_fd, buf, count);
120 ret = ::fread(buf, 1, count, m_file);
123 m_current_offset = m_last_offset += ret;
127 int eRawFile::valid()
135 void eRawFile::scan()
139 while (m_nrfiles < 1000) /* .999 is the last possible */
143 int f = openFile(m_nrfiles);
147 m_splitsize = ::lseek(f, 0, SEEK_END);
148 m_totallength += ::lseek(f, 0, SEEK_END);
152 FILE *f = (FILE*)openFile(m_nrfiles);
155 ::fseeko(f, 0, SEEK_END);
157 m_splitsize = ::ftello(f);
158 m_totallength += ::ftello(f);
164 // eDebug("found %d files, splitsize: %llx, totallength: %llx", m_nrfiles, m_splitsize, m_totallength);
167 int eRawFile::switchOffset(off_t off)
171 int filenr = off / m_splitsize;
172 if (filenr >= m_nrfiles)
173 filenr = m_nrfiles - 1;
174 if (filenr != m_current_file)
176 // eDebug("-> %d", filenr);
179 m_fd = openFile(filenr);
181 m_file = (FILE*)openFile(filenr);
182 m_last_offset = m_base_offset = m_splitsize * filenr;
183 m_current_file = filenr;
188 if (off != m_last_offset)
191 m_last_offset = ::lseek(m_fd, off - m_base_offset, SEEK_SET) + m_base_offset;
194 ::fseeko(m_file, off - m_base_offset, SEEK_SET);
195 m_last_offset = ::ftello(m_file) + m_base_offset;
197 return m_last_offset;
200 return m_last_offset;
204 int eRawFile::openFile(int nr)
206 std::string filename = m_basename;
210 snprintf(suffix, 5, ".%03d", nr);
214 return ::open(filename.c_str(), O_RDONLY | O_LARGEFILE);
216 return (int)::fopen64(filename.c_str(), "rb");