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);
62 return ::fseeko(m_file, offset, whence);
67 m_current_offset = offset;
70 m_current_offset += offset;
73 m_current_offset = m_totallength + offset;
77 if (m_current_offset < 0)
79 return m_current_offset;
93 int ret = ::close(m_fd);
99 ssize_t eRawFile::read(void *buf, size_t count)
101 // eDebug("read: %p, %d", buf, count);
102 switchOffset(m_current_offset);
106 if (m_current_offset + count > m_totallength)
107 count = m_totallength - m_current_offset;
115 ret = ::read(m_fd, buf, count);
117 ret = ::fread(buf, 1, count, m_file);
120 m_current_offset = m_last_offset += ret;
124 int eRawFile::valid()
129 void eRawFile::scan()
133 while (m_nrfiles < 1000) /* .999 is the last possible */
137 int f = openFile(m_nrfiles);
141 m_splitsize = ::lseek(f, 0, SEEK_END);
142 m_totallength += ::lseek(f, 0, SEEK_END);
146 FILE *f = (FILE*)openFile(m_nrfiles);
150 m_splitsize = ::fseeko(f, 0, SEEK_END);
151 m_totallength += ::fseeko(f, 0, SEEK_END);
157 // eDebug("found %d files, splitsize: %llx, totallength: %llx", m_nrfiles, m_splitsize, m_totallength);
160 int eRawFile::switchOffset(off_t off)
164 int filenr = off / m_splitsize;
165 if (filenr >= m_nrfiles)
166 filenr = m_nrfiles - 1;
167 if (filenr != m_current_file)
169 // eDebug("-> %d", filenr);
172 m_fd = openFile(filenr);
174 m_file = (FILE*)openFile(filenr);
175 m_last_offset = m_base_offset = m_splitsize * filenr;
176 m_current_file = filenr;
181 if (off != m_last_offset)
184 m_last_offset = ::lseek(m_fd, off - m_base_offset, SEEK_SET) + m_base_offset;
186 m_last_offset = ::fseeko(m_file, off - m_base_offset, SEEK_SET) + m_base_offset;
187 return m_last_offset;
190 return m_last_offset;
194 int eRawFile::openFile(int nr)
196 std::string filename = m_basename;
200 snprintf(suffix, 5, ".%03d", nr);
204 return ::open(filename.c_str(), O_RDONLY | O_LARGEFILE);
206 return (int)::fopen64(filename.c_str(), "rb");