util

package
v0.0.0-...-b5aa0b6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2017 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMaxMessageSize = 20 * 1024 * 1024

DefaultMaxMessageSize gives the default max for messages sent on a MessageStream.

View Source
const OOBMaxLength = 100 // usually under 64 in practice

Maximum amount of out-of-band data supported. This is enough to send at least a set of credentials and 3 file descriptors.

Variables

View Source
var (
	ErrOOBSendFailed  = errors.New("error sending out-of-band unix socket data")
	ErrOOBParseFailed = errors.New("error parsing out-of-band unix socket data")
)

Error types for the protorpc package.

View Source
var ErrMessageTooLarge = errors.New("messagestream: message is too large")

ErrMessageTooLarge is the error message returned when a message larger than DefaultMaxMessageSize is sent on a MessageStream.

Functions

func CreatePath

func CreatePath(path string, dirPerm, filePerm os.FileMode) (*os.File, error)

CreatePath creates a file after creating any necessary directories.

func FindExecutable

func FindExecutable(name string, dirs []string) string

FindExecutable searches for an executable in a path. If the name is already an absolute slash, the search path is ignored. If the search fails, emptystring is returned.

func GoBinPath

func GoBinPath() []string

GoBinPath returns dir/bin for each dir in $GOPATH

func IsDir

func IsDir(path string) bool

IsDir checks whether the path is a directory.

func IsExecutable

func IsExecutable(file string) bool

IsExecutable checks whether the file has an executable bits set.

func LiberalSearchPath

func LiberalSearchPath() []string

LiberalSearchPath returns LocalPath, GoBinPath, and SystemPath together, in that order.

func LocalPath

func LocalPath() []string

LocalPath returns the directory of the current executable

func Logged

func Logged(err error) error

Logged prints an error and some context to glog.Error, then returns the same error. The intended usage is like so:

foo, err := somefunc()
if err != nil {
  return 0, Logged(err)
}
...

The context consists of a stack trace, but omitting parts of the trace that were already shown by the most recently printed error.

func NewFile

func NewFile(fd int) *os.File

NewFile wraps a file descriptor inside an os.File, also giving it a reasonable name.

func NewUnixSingleReadWriteCloser

func NewUnixSingleReadWriteCloser(path string) io.ReadWriteCloser

NewUnixSingleReadWriteCloser listens on a given Unix socket path and returns a UnixSingleReadWriteCloser that will accept a single connection on this socket and communicate only with it.

func SystemPath

func SystemPath() []string

SystemPath returns the elements of $PATH

func UseEnvFlags

func UseEnvFlags(prefix ...string)

UseEnvFlags pulls variables from the environment to use as values for flags. For each prefix X, an environment variable X_f will be used as the value for flag f. If flag.Parse() has not been called, then flags on the command line will override those from the environment. Otherwise environment flags will override those on the command line.

func WritePath

func WritePath(path string, data []byte, dirPerm, filePerm os.FileMode) error

WritePath writes data to a file after creating any necessary directories.

Types

type ChanReadWriteCloser

type ChanReadWriteCloser struct {
	R <-chan []byte
	W chan []byte
}

A ChanReadWriteCloser implements io.ReadWriteCloser over chan []byte.

func (ChanReadWriteCloser) Close

func (crw ChanReadWriteCloser) Close() error

Close implements io.Closer for ChanReadWriteCloser.

func (ChanReadWriteCloser) Read

func (crw ChanReadWriteCloser) Read(p []byte) (int, error)

Read implements io.Reader for ChanReadWriteCloser.

func (ChanReadWriteCloser) Write

func (crw ChanReadWriteCloser) Write(p []byte) (int, error)

Write implements io.Writer for ChanReadWriteCloser.

type MessageReader

type MessageReader interface {
	ReadMessage(m proto.Message) error
}

A MessageReader is a stream from which protobuf messages can be read.

type MessageStream

type MessageStream struct {
	MaxMessageSize int // Negative means unlimited
	io.ReadWriteCloser
}

A MessageStream is an io.ReadWriteCloser that can also read and write strings and protobuf messages. Boundaries are preserved for strings and protobuf messages using a 32-bit (network byte order) length prefix before the contents of the string or marshalled protobuf message. MessageStream can also enforce an upper-limit on the size of received messages.

func DeserializeFDMessageStream

func DeserializeFDMessageStream(s string) (*MessageStream, error)

DeserializeFDMessageStream takes a string description of the form "tao::FDMessageStream(X, Y)" and returns a MessageStream that uses file descriptor X as the reader and file descriptor Y as the writer.

func DeserializeFileMessageStream

func DeserializeFileMessageStream(s string) (*MessageStream, error)

DeserializeFileMessageStream takes a string description of the form "tao::FileMessageChannel(X)" and returns a MessageStream that uses file X to communicate.

func DeserializeUnixSocketMessageStream

func DeserializeUnixSocketMessageStream(f string) (*MessageStream, error)

DeserializeUnixSocketMessageStream takes a string filename and returns a MessageStream that is based on the Unix socket for this file.

func NewMessageStream

func NewMessageStream(pipe io.ReadWriteCloser) *MessageStream

NewMessageStream creates a MessageStream for the given pipe with a reception limit of DefaultMaxMessageSize.

func (*MessageStream) ReadMessage

func (ms *MessageStream) ReadMessage(m proto.Message) error

ReadMessage reads a 32-bit length followed by a protobuf message. If m is nil, the incoming message is discarded.

func (*MessageStream) ReadString

func (ms *MessageStream) ReadString() (string, error)

ReadString reads a 32-bit length followed by a string.

func (*MessageStream) WriteMessage

func (ms *MessageStream) WriteMessage(m proto.Message) (int, error)

WriteMessage writes 32-bit length followed by a protobuf message. If m is nil, a blank message is written instead.

func (*MessageStream) WriteString

func (ms *MessageStream) WriteString(s string) (int, error)

WriteString writes a 32-bit length followed by the string.

type MessageWriter

type MessageWriter interface {
	WriteMessage(m proto.Message) error
}

A MessageWriter is a stream to which protobuf messages can be written.

type OOBUnixConn

type OOBUnixConn struct {
	*net.UnixConn
	// contains filtered or unexported fields
}

OOBUnixConn provides the same operations as net.UnixConn, plus the ability to asynchronously make use of the out-of-band mechanism to share file descriptors and credentials.

func NewOOBUnixConn

func NewOOBUnixConn(conn *net.UnixConn) *OOBUnixConn

NewOOBUnixConn returns a new util.OOBUnixConn, which provides the same operations as net.UnixConn but also allows sharing of file descriptors and credentials.

func (*OOBUnixConn) PeerCred

func (s *OOBUnixConn) PeerCred() *Ucred

PeerCred retreives the most recently passed peer credential, or nil if no credentials have been received yet.

func (*OOBUnixConn) Read

func (s *OOBUnixConn) Read(p []byte) (n int, err error)

func (*OOBUnixConn) ShareFDs

func (s *OOBUnixConn) ShareFDs(fd ...int)

ShareFDs adds some file descriptors to the list of filescriptors to be shared during the next Write.

func (*OOBUnixConn) SharedFiles

func (s *OOBUnixConn) SharedFiles() []*os.File

SharedFiles retreives the open files shared during recent Read calls.

func (*OOBUnixConn) Write

func (s *OOBUnixConn) Write(buf []byte) (int, error)

type PairReadWriteCloser

type PairReadWriteCloser struct {
	io.ReadCloser
	io.WriteCloser
}

A PairReadWriteCloser groups an io.ReadCloser and an io.WriteCloser into a single structure that implements the io.ReadWriteCloser interface. This can be used to turn a pair of uni-directional streams into a single bi-directional stream.

func NewPairReadWriteCloser

func NewPairReadWriteCloser(r io.ReadCloser, w io.WriteCloser) *PairReadWriteCloser

NewPairReadWriteCloser creates a new io.ReadWriteCloser given separate streams for reading and writing. If both streams refer to the same object, then the read stream will be wrapped in an ioutil.NopCloser() so that Close() on the resulting io.ReadWriteCloser() will only close that underlying stream object once.

func (PairReadWriteCloser) Close

func (pair PairReadWriteCloser) Close() error

Close closes the underying streams, both the io.ReadCloser and the io.WriteCloser.

type StringReader

type StringReader interface {
	ReadString() (string, error)
}

A StringReader is a stream from which strings can be read.

type StringWriter

type StringWriter interface {
	WriteString(s string) (n int, err error)
}

A StringWriter is a stream to which strings can be written.

type Ucred

type Ucred struct {
	Uid uint32
	Gid uint32
}

Ucred holds credentials of a peer process.

type UnixSingleReadWriteCloser

type UnixSingleReadWriteCloser struct {
	// contains filtered or unexported fields
}

A UnixSingleReadWriteCloser accepts a single connection and reads and writes to this connection

func (*UnixSingleReadWriteCloser) Close

func (usrwc *UnixSingleReadWriteCloser) Close() error

Close closes the connection if there is one and closes the listener.

func (*UnixSingleReadWriteCloser) Read

func (usrwc *UnixSingleReadWriteCloser) Read(p []byte) (int, error)

Read accepts a connection if there isn't one already and reads from the connection.

func (*UnixSingleReadWriteCloser) Write

func (usrwc *UnixSingleReadWriteCloser) Write(p []byte) (int, error)

Write accepts a connection if there isn't one already and writes to the connection.

Directories

Path Synopsis
Package options works in concert with flag, adding prettier printing of options.
Package options works in concert with flag, adding prettier printing of options.
Package protorpc implements a protobuf-based ClientCodec and ServerCodec for the rpc package.
Package protorpc implements a protobuf-based ClientCodec and ServerCodec for the rpc package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL