mapleFU commented on code in PR #1839:
URL: https://github.com/apache/kvrocks/pull/1839#discussion_r1365808687
##########
src/common/rdb_stream.cc:
##########
@@ -50,20 +50,18 @@ Status RdbFileStream::Open() {
return Status::OK();
}
-StatusOr<size_t> RdbFileStream::Read(char *buf, size_t len) {
- size_t n = 0;
+Status RdbFileStream::Read(char *buf, size_t len) {
while (len) {
size_t read_bytes = std::min(max_read_chunk_size_, len);
ifs_.read(buf, static_cast<std::streamsize>(read_bytes));
if (!ifs_.good()) {
- return Status(Status::NotOK, fmt::format("read failed: {}:",
strerror(errno)));
+ return {Status::NotOK, fmt::format("read failed: {}:", strerror(errno))};
}
check_sum_ = crc64(check_sum_, reinterpret_cast<const unsigned char
*>(buf), read_bytes);
- buf = (char *)buf + read_bytes;
+ buf = buf + read_bytes;
+ DCHECK(len >= read_bytes);
len -= read_bytes;
total_read_bytes_ += read_bytes;
- n += read_bytes;
}
-
- return n;
+ return Status::OK();
Review Comment:
```c++
ifs_.read(buf, static_cast<std::streamsize>(read_bytes));
if (!ifs_.good()) {
if (!ifs_.eof()) {
return {Status::NotOK, fmt::format("read failed: {}:",
strerror(errno))};
}
auto eof_read_bytes = static_cast<size_t>(ifs_.gcount());
if (read_bytes != eof_read_bytes) {
return {Status::NotOK, fmt::format("read failed: {}:",
strerror(errno))};
}
}
```
I've update it to code like this. In `read(2)`, if file reaches EOF, the
`read` would return `0`. And return `-1` when meets error. So I think previous
code is ok. But I don't get what the `ifstream` works even after I gothrough
the cppreference. So I change it to the code above.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]