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 eSingleLocker l(m_lock);
60 m_current_offset = lseek_internal(offset, whence);
61 return m_current_offset;
64 off_t eRawFile::lseek_internal(off_t offset, int whence)
66 // eDebug("lseek: %lld, %d", offset, whence);
67 /* if there is only one file, use the native lseek - the file could be growing! */
71 return ::lseek(m_fd, offset, whence);
74 if (::fseeko(m_file, offset, whence) < 0)
76 return ::ftello(m_file);
82 m_current_offset = offset;
85 m_current_offset += offset;
88 m_current_offset = m_totallength + offset;
92 if (m_current_offset < 0)
94 return m_current_offset;
108 int ret = ::close(m_fd);
114 ssize_t eRawFile::read(off_t offset, void *buf, size_t count)
116 eSingleLocker l(m_lock);
118 if (offset != m_current_offset)
120 m_current_offset = lseek_internal(offset, SEEK_SET);
121 if (m_current_offset < 0)
122 return m_current_offset;
125 switchOffset(m_current_offset);
129 if (m_current_offset + count > m_totallength)
130 count = m_totallength - m_current_offset;
138 ret = ::read(m_fd, buf, count);
140 ret = ::fread(buf, 1, count, m_file);
143 m_current_offset = m_last_offset += ret;
147 int eRawFile::valid()
155 void eRawFile::scan()
159 while (m_nrfiles < 1000) /* .999 is the last possible */
163 int f = openFileUncached(m_nrfiles);
167 m_splitsize = ::lseek(f, 0, SEEK_END);
168 m_totallength += ::lseek(f, 0, SEEK_END);
172 FILE *f = openFileCached(m_nrfiles);
175 ::fseeko(f, 0, SEEK_END);
177 m_splitsize = ::ftello(f);
178 m_totallength += ::ftello(f);
184 // eDebug("found %d files, splitsize: %llx, totallength: %llx", m_nrfiles, m_splitsize, m_totallength);
187 int eRawFile::switchOffset(off_t off)
191 int filenr = off / m_splitsize;
192 if (filenr >= m_nrfiles)
193 filenr = m_nrfiles - 1;
194 if (filenr != m_current_file)
196 // eDebug("-> %d", filenr);
199 m_fd = openFileUncached(filenr);
201 m_file = openFileCached(filenr);
202 m_last_offset = m_base_offset = m_splitsize * filenr;
203 m_current_file = filenr;
208 if (off != m_last_offset)
211 m_last_offset = ::lseek(m_fd, off - m_base_offset, SEEK_SET) + m_base_offset;
214 ::fseeko(m_file, off - m_base_offset, SEEK_SET);
215 m_last_offset = ::ftello(m_file) + m_base_offset;
217 return m_last_offset;
220 return m_last_offset;
225 FILE *eRawFile::openFileCached(int nr)
227 std::string filename = m_basename;
231 snprintf(suffix, 5, ".%03d", nr);
234 return ::fopen64(filename.c_str(), "rb");
238 int eRawFile::openFileUncached(int nr)
240 std::string filename = m_basename;
244 snprintf(suffix, 5, ".%03d", nr);
247 return ::open(filename.c_str(), O_RDONLY | O_LARGEFILE);
250 off_t eRawFile::length()
252 return m_totallength;