xfercommon

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package xfercommon holds helpers for a file transfer that do not know which protocol is moving the bytes. FTP and SFTP use them. This package has no FTP code and no SSH code.

Index

Constants

View Source
const (
	// MaxSize is the most a generated payload may be: 4 GiB.
	MaxSize = 4 << 30
	// MaxPath is the most bytes a remote path may be.
	MaxPath = 4096
	// MaxBody is the most a caller-supplied body may be.
	MaxBody = 64 << 20
)

Variables

This section is empty.

Functions

func CheckPath

func CheckPath(path string) error

CheckPath is CheckText for a remote path, and it also limits the length.

func CheckText

func CheckText(field, v string) error

CheckText refuses a CR, LF or NUL in v. The error names the field and does not include the value.

func Cut added in v0.9.0

func Cut(s string, n int) string

Cut keeps at most n bytes of s.

func FillReader

func FillReader(mode string, n int64) (io.Reader, error)

FillReader returns a reader of exactly n bytes. mode is random, zero or text. The same mode and n always produce the same bytes. The reader never holds those bytes all at once.

func HashPayload added in v0.9.0

func HashPayload(fill string, size int64, sizeSet bool, body []byte) string

HashPayload is the SHA-256 hex of the bytes an upload would send.

func HexDigest

func HexDigest(sum []byte) string

HexDigest is the hex encoding of sum, lowercase.

func NewHash

func NewHash() hash.Hash

NewHash returns a SHA-256 hash.

func ParseSize

func ParseSize(s string) (int64, error)

ParseSize reads a size such as 1024, 10KiB, 10MiB or 1GiB. The unit is 1024-based. A bare number is a count of bytes.

func ReadStream added in v0.9.0

func ReadStream(r io.Reader, expect string, doHash bool) (int64, string, bool, error)

ReadStream reads r to the end. It never keeps the whole stream. When expect is set, it looks for that text in the first 1 MiB. When hash is set, it returns the SHA-256 hex of the whole stream.

func Refuse added in v0.9.0

func Refuse(prefix, mode, path string, allowWrites, allowAdmin, script bool) error

Refuse applies the write and delete safety rules. A nil error means the call may use the network.

func ReplaceID

func ReplaceID(s, id string) string

ReplaceID replaces each {id} in s with id. Nothing else is substituted.

func TextLeft added in v0.9.0

func TextLeft(path string) string

TextLeft is the reply text when a roundtrip file could not be deleted.

func ValidSHA256

func ValidSHA256(s string) bool

ValidSHA256 reports whether s is 64 hex characters.

Types

type CountingReader

type CountingReader struct {
	R io.Reader
	N int64
}

CountingReader counts the bytes read from r.

func (*CountingReader) Read

func (c *CountingReader) Read(p []byte) (int, error)

type CountingWriter

type CountingWriter struct {
	W io.Writer
	N int64
}

CountingWriter counts the bytes written to w.

func (*CountingWriter) Write

func (c *CountingWriter) Write(p []byte) (int, error)

type Entry added in v0.9.0

type Entry struct {
	Name string
	Type string
	Size int64
	Time time.Time
}

Entry is one name from a directory listing. Time is UTC.

type IDs added in v0.9.0

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

IDs makes a new {id} value for each call of one driver.

func NewIDs added in v0.9.0

func NewIDs() (*IDs, error)

NewIDs returns an id source. The salt is random.

func (*IDs) Next added in v0.9.0

func (ids *IDs) Next() string

Next returns the next id. It is unique for this source.

func (*IDs) Pending added in v0.9.0

func (ids *IDs) Pending() string

Pending is the id the next Next call will return.

type Job added in v0.9.0

type Job struct {
	Mode          string
	Path          string
	Size          int64
	SizeSet       bool
	Fill          string
	ExpectSize    int64
	ExpectSizeSet bool
	ExpectSHA     string
	Expect        string
	Limit         int
	Hidden        bool
}

Job is the work of one call. Connection options are not part of it.

func ParseJob added in v0.9.0

func ParseJob(prefix string, opts map[string]string, body []byte, isConn func(string) bool, listDefault string, rejectDashPath bool) (Job, error)

ParseJob reads the job options. prefix is the protocol name in errors. isConn reports a connection option, which this function ignores. listDefault is the path used when mode is list and path is empty. rejectDashPath refuses a list path that starts with "-".

type Reply added in v0.9.0

type Reply struct {
	Entries []Entry
	Size    int64
	Total   int
	Timing  Timing
	Text    string
}

Reply is what a file-transfer call returns besides the byte counts.

type Timing added in v0.9.0

type Timing struct {
	ConnectMs  int64
	LoginMs    int64
	TransferMs int64
	UploadMs   int64
	DownloadMs int64
	DeleteMs   int64
}

Timing is how long the parts of a call took, in milliseconds.

Jump to

Keyboard shortcuts

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