third_party/libprocess/include/stout/protobuf.hpp
// Read the next protobuf from the file by first reading the "size"
// followed by the contents (as written by 'write' above).
Read the next protobuf from the file by first reading the "size"
followed by the contents (as written by 'write' above).
Read the next protobuf from the file by first reading the " size " followed by the contents ( as written by ' write ' above ) .
inline Result<bool> read(int fd, google::protobuf::Message* message)
{
  CHECK(message != NULL);
  message->Clear();
  off_t offset = lseek(fd, 0, SEEK_CUR);
  if (offset < 0) {
    std::string error = strerror(errno);
    error = error + " (" + __FILE__ + ":" + stringify(__LINE__) + ")";
    return Result<bool>::error(error);
  }
  uint32_t size;
  ssize_t length = ::read(fd, (void*) &size, sizeof(size));
  if (length == 0) {
    return Result<bool>::none();
  } else if (length == -1) {
    std::string error = strerror(errno);
    error = error + " (" + __FILE__ + ":" + stringify(__LINE__) + ")";
    lseek(fd, offset, SEEK_SET);
    return Result<bool>::error(error);
  } else if (length != sizeof(size)) {
    return false;
  }
  if (size > 10 * 1024 * 1024) { 
    std::string error = "Size > 10 MB, possible corruption detected";
    error = error + " (" + __FILE__ + ":" + stringify(__LINE__) + ")";
    lseek(fd, offset, SEEK_SET);
    return Result<bool>::error(error);;
  }
  char* temp = new char[size];
  length = ::read(fd, temp, size);
  if (length == 0) {
    delete[] temp;
    return Result<bool>::none();
  } else if (length == -1) {
    std::string error = strerror(errno);
    error = error + " (" + __FILE__ + ":" + stringify(__LINE__) + ")";
    lseek(fd, offset, SEEK_SET);
    delete[] temp;
    return Result<bool>::error(error);
  } else if (length != size) {
    delete[] temp;
    return false;
  }
  google::protobuf::io::ArrayInputStream stream(temp, length);
  bool parsed = message->ParseFromZeroCopyStream(&stream);
  delete[] temp;
  if (!parsed) {
    std::string error = "Failed to parse protobuf";
    error = error + " (" + __FILE__ + ":" + stringify(__LINE__) + ")";
    lseek(fd, offset, SEEK_SET);
    return Result<bool>::error(error);;
  }
  return true;
}
inline Result < bool > read ( int fd , google :: protobuf :: Message * message ) { CHECK ( message != NULL ) ; message -> Clear ( ) ; off_t offset = lseek ( fd , 0 , SEEK_CUR ) ; if ( offset < 0 ) { std :: string error = strerror ( errno ) ; error = error + " " + __FILE__ + " " + stringify ( __LINE__ ) + " " ; return Result < bool > :: error ( error ) ; } uint32_t size ; ssize_t length = :: read ( fd , ( void * ) & size , sizeof ( size ) ) ; if ( length == 0 ) { return Result < bool > :: none ( ) ; } else if ( length == -1 ) { std :: string error = strerror ( errno ) ; error = error + " " + __FILE__ + " " + stringify ( __LINE__ ) + " " ; lseek ( fd , offset , SEEK_SET ) ; return Result < bool > :: error ( error ) ; } else if ( length != sizeof ( size ) ) { return false ; } if ( size > 10 * 1024 * 1024 ) { std :: string error = " " ; error = error + " " + __FILE__ + " " + stringify ( __LINE__ ) + " " ; lseek ( fd , offset , SEEK_SET ) ; return Result < bool > :: error ( error ) ; ; } char * temp = new char [ size ] ; length = :: read ( fd , temp , size ) ; if ( length == 0 ) { delete [ ] temp ; return Result < bool > :: none ( ) ; } else if ( length == -1 ) { std :: string error = strerror ( errno ) ; error = error + " " + __FILE__ + " " + stringify ( __LINE__ ) + " " ; lseek ( fd , offset , SEEK_SET ) ; delete [ ] temp ; return Result < bool > :: error ( error ) ; } else if ( length != size ) { delete [ ] temp ; return false ; } google :: protobuf :: io :: ArrayInputStream stream ( temp , length ) ; bool parsed = message -> ParseFromZeroCopyStream ( & stream ) ; delete [ ] temp ; if ( ! parsed ) { std :: string error = " " ; error = error + " " + __FILE__ + " " + stringify ( __LINE__ ) + " " ; lseek ( fd , offset , SEEK_SET ) ; return Result < bool > :: error ( error ) ; ; } return true ; }
Read the next protobuf from the file by first reading the "size"
followed by the contents (as written by 'write' above).
Read the next protobuf from the file by first reading the " size " followed by the contents ( as written by ' write ' above ) .
// Save the offset so we can re-adjust if something goes wrong. // Save the error, reset the file offset, and return the error. // TODO(benh): Use a different format for writing a protobuf to disk // so that we don't need to have broken heuristics like this! // 10 MB // Save the error, reset the file offset, and return the error. // Save the error, reset the file offset, and return the error. // Save the error, reset the file offset, and return the error.