third_party/libprocess/include/stout/os.hpp
// Read the contents of the file from its current offset
// and return it as a string.
Read the contents of the file from its current offset
and return it as a string.
Read the contents of the file from its current offset and return it as a string .
inline Result<std::string> read(int fd)
{
  off_t offset = lseek(fd, 0, SEEK_CUR);
  if (offset == -1) {
    return Result<std::string>::error("Error seeking to SEEK_CUR");
  }
  off_t size = lseek(fd, 0, SEEK_END);
  if (size == -1) {
      return Result<std::string>::error("Error seeking to SEEK_END");
  }
  if (lseek(fd, offset, SEEK_SET) == -1) {
    return Result<std::string>::error("Error seeking to SEEK_SET");
  }
  char* buffer = new char[size];
  ssize_t length = ::read(fd, buffer, size);
  if (length == 0) {
    return Result<std::string>::none();
  } else if (length == -1) {
    return Result<std::string>::error(strerror(errno));
  } else if (length != size) {
    return Result<std::string>::error("Couldn't read the entire file");
  }
  std::string result(buffer, size);
  return result;
}
inline Result < std :: string > read ( int fd ) { off_t offset = lseek ( fd , 0 , SEEK_CUR ) ; if ( offset == -1 ) { return Result < std :: string > :: error ( " " ) ; } off_t size = lseek ( fd , 0 , SEEK_END ) ; if ( size == -1 ) { return Result < std :: string > :: error ( " " ) ; } if ( lseek ( fd , offset , SEEK_SET ) == -1 ) { return Result < std :: string > :: error ( " " ) ; } char * buffer = new char [ size ] ; ssize_t length = :: read ( fd , buffer , size ) ; if ( length == 0 ) { return Result < std :: string > :: none ( ) ; } else if ( length == -1 ) { return Result < std :: string > :: error ( strerror ( errno ) ) ; } else if ( length != size ) { return Result < std :: string > :: error ( " " ) ; } std :: string result ( buffer , size ) ; return result ; }
Read the contents of the file from its current offset
and return it as a string.
Read the contents of the file from its current offset and return it as a string .
// Get the size of the file. // Allocate memory. // Save the error, reset the file offset, and return the error.