xfer

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package xfer moves selection and drag-and-drop payloads through the pipes Wayland hands the two ends of a transfer. Both ends are foreign clients — the offer we read from, the consumer we write to — and either may be hostile or broken: an app on the machine can offer a text payload and stream gigabytes into a paste, or request our selection and never read it. Every transfer here is therefore bounded twice: in size, so a peer cannot grow our memory without limit, and in time, so a stalled peer cannot hang a paste or the event loop.

Index

Constants

View Source
const DefaultTimeout = 5 * time.Second

DefaultTimeout bounds one transfer against a peer that stalls: long enough for an honest but slow peer, short enough that a hung paste surfaces as an error instead of a frozen window. Callers keep it in a var so tests can shorten it.

View Source
const MaxPayload = 16 << 20

MaxPayload caps one transfer at 16 MiB: orders of magnitude above any honest text selection or drop payload, yet small enough that a peer streaming at the cap cannot crowd the rest of the process out of memory. Reading past the cap truncates the transfer — the bytes that fit come back alongside an error, and the fd is drained (under the same deadline) so the peer's pipe never blocks forever.

Variables

View Source
var (
	// ErrTooLarge reports that the peer streamed past MaxPayload; the
	// bytes that fit were returned with it. Callers decide what a
	// truncated payload is worth — lys-ui's app layer treats the error
	// as a rejected paste.
	ErrTooLarge = errors.New("xfer: peer streamed past the payload budget")

	// ErrTimeout reports that the peer stalled — never wrote, stopped
	// mid-stream, or stopped reading — and the deadline cut the
	// transfer off.
	ErrTimeout = errors.New("xfer: peer stalled and the deadline cut the transfer off")
)

Functions

func Copy

func Copy(w io.Writer, r *os.File, limit int64, timeout time.Duration) (int64, error)

Copy drains r into w up to limit bytes, under a deadline, and reports the bytes written: the streaming form of Read, for a caller that owns the memory a transfer lands in (reused, or off the Go heap). The deadline is a timer that closes r: a read blocked on a silent peer wakes with our own close error, which reports as ErrTimeout. time.AfterFunc creates no goroutine until it fires and the defer stops it when the transfer finishes early, so nothing is created for, or leaks after, a normal transfer.

A payload past limit is truncated: the bytes that fit are written, and the error wraps ErrTooLarge, after the rest of the stream is drained into the void (still under the deadline) so the peer's writes to the shared pipe never block on a full buffer. A write w refuses ends the transfer with its error.

func DeadlineWriter

func DeadlineWriter(fd uintptr, timeout time.Duration) (*os.File, error)

DeadlineWriter prepares the pipe write end a consumer handed over — the compositor dup'ed it out of the requesting client's pipe — for a bounded source write: a consumer that stops reading cannot stall the writer forever, the deadline breaks the write, and EPIPE from a consumer that closed early ends it at once. Close the writer when the transfer ends, whatever the outcome, so the consumer sees EOF rather than a hang; a write cut short by the deadline or EPIPE leaves the consumer a short payload plus EOF — evidence the transfer broke, not a puzzle about where it stopped.

The descriptor arrives in blocking mode, and a blocking os.File is invisible to the runtime poller — no deadline could touch it and a stuck write would be unstoppable. Flipping the descriptor to non-blocking first hands NewFile a pollable file whose write takes the deadline. The flag lives on the open file description, but the requesting client closed its hold on that description when it handed the end over, so nothing else observes the switch.

func Read

func Read(r *os.File, limit int64, timeout time.Duration) ([]byte, error)

Read drains r up to limit bytes, under a deadline, into a fresh slice: Copy into memory Read owns.

A payload past limit is truncated: the bytes that fit are returned together with an error wrapping ErrTooLarge. A failed read returns no bytes.

Types

This section is empty.

Jump to

Keyboard shortcuts

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