| 273 | } |
| 274 | |
| 275 | SampledRequest* SampleIterator::Next() { |
| 276 | if (!_cur_buf.empty()) { |
| 277 | bool error = false; |
| 278 | SampledRequest* r = Pop(_cur_buf, &error); |
| 279 | if (r) { |
| 280 | return r; |
| 281 | } |
| 282 | if (error) { |
| 283 | _cur_buf.clear(); |
| 284 | if (_cur_fd >= 0) { |
| 285 | ::close(_cur_fd); |
| 286 | _cur_fd = -1; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | while (1) { |
| 291 | while (_cur_fd >= 0) { |
| 292 | ssize_t nr = _cur_buf.append_from_file_descriptor(_cur_fd, 524288); |
| 293 | if (nr < 0) { |
| 294 | if (errno != EAGAIN && errno != EINTR) { |
| 295 | PLOG(ERROR) << "Fail to read fd=" << _cur_fd; |
| 296 | break; |
| 297 | } |
| 298 | } else if (nr == 0) { // EOF |
| 299 | break; |
| 300 | } else { |
| 301 | return Next(); // tailr |
| 302 | } |
| 303 | } |
| 304 | _cur_buf.clear(); |
| 305 | if (_cur_fd >= 0) { |
| 306 | ::close(_cur_fd); |
| 307 | _cur_fd = -1; |
| 308 | } |
| 309 | |
| 310 | if (_enum == NULL) { |
| 311 | _enum = new butil::FileEnumerator( |
| 312 | _dir, false, butil::FileEnumerator::FILES); |
| 313 | } |
| 314 | butil::FilePath filename = _enum->Next(); |
| 315 | if (filename.empty()) { |
| 316 | return NULL; |
| 317 | } |
| 318 | _cur_fd = open(filename.value().c_str(), O_RDONLY); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | SampledRequest* SampleIterator::Pop(butil::IOBuf& buf, bool* format_error) { |
| 323 | char backing_buf[12]; |
no test coverage detected