Skip to content

Commit 7e4359b

Browse files
committed
Handle seek consistently for compressed and remote input streams
1 parent ad59524 commit 7e4359b

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

core/src/dataio.cxx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ connect_remote(const std::string &path, int timeout)
131131
class RemoteInputStreamBuffer : public std::streambuf {
132132
public:
133133
RemoteInputStreamBuffer(const std::string &path, int timeout, size_t size)
134-
: buffer_(size) {
134+
: buffer_(size), bytes_(0) {
135135
fd_ = connect_remote(path, timeout);
136136
setg(buffer_.data(), buffer_.data(), buffer_.data());
137137
}
@@ -170,12 +170,26 @@ class RemoteInputStreamBuffer : public std::streambuf {
170170
gbump(to_read);
171171
n_read += to_read;
172172
}
173+
bytes_ += n_read;
173174
return n_read;
174175
}
175176

177+
std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way,
178+
std::ios_base::openmode mode) {
179+
// short-circuit for tellg
180+
if (off == 0 && way == std::ios_base::cur)
181+
return bytes_;
182+
log_fatal("Seek not implemented for remote stream");
183+
}
184+
185+
std::streampos seekpos(std::streampos pos, std::ios_base::openmode mode) {
186+
log_fatal("Seek not implemented for remote stream");
187+
}
188+
176189
private:
177190
int fd_;
178191
std::vector<char> buffer_;
192+
size_t bytes_;
179193
};
180194

181195
class UnsupportedCodec : public std::streambuf {
@@ -248,6 +262,15 @@ class Decoder : public std::streambuf {
248262
return n_read;
249263
}
250264

265+
std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way,
266+
std::ios_base::openmode mode) {
267+
log_fatal("Seek not implemented for compressed stream");
268+
}
269+
270+
std::streampos seekpos(std::streampos pos, std::ios_base::openmode mode) {
271+
log_fatal("Seek not implemented for compressed stream");
272+
}
273+
251274
virtual int decode() = 0;
252275

253276
std::istream& file_;
@@ -340,8 +363,6 @@ class InputStreamCounter : public std::streambuf {
340363
log_fatal("Input file stream buffer required");
341364
}
342365

343-
std::streampos bytes() const { return bytes_; }
344-
345366
protected:
346367
int_type underflow() {
347368
return buffer_->sgetc();
@@ -355,7 +376,7 @@ class InputStreamCounter : public std::streambuf {
355376
}
356377

357378
std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way,
358-
std::ios_base::openmode mode) {
379+
std::ios_base::openmode mode) {
359380
// short-circuit for tellg
360381
if (off == 0 && way == std::ios_base::cur)
361382
return bytes_;

0 commit comments

Comments
 (0)