p2

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: AGPL-3.0 Imports: 17 Imported by: 1 module, 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 p2 is the grammar of Eclipse p2 repositories as an update site proxy needs it: the layout of a repository (metadata documents, bundles, features, binaries), the composite repositories that point at other repositories, and the rewriting of the documents that would otherwise send a client around the proxy (the mirror and statistics URLs of artifacts.xml, absolute child locations of a composite repository).

A p2 repository is a directory of static files:

p2.index                                     which metadata files exist
content.(jar|xml|xml.xz)                     installable units
artifacts.(jar|xml|xml.xz)                   artifact descriptors and download rules
compositeContent.(jar|xml|xml.xz)            composite: child repositories
compositeArtifacts.(jar|xml|xml.xz)
plugins/<id>_<version>.jar                   OSGi bundles (also .pack.gz, legacy)
features/<id>_<version>.jar                  features
binary/<id>_<version>                        other artifacts

Nothing here talks to the network.

Index

Constants

View Source
const HostPrefix = "_host"

HostPrefix is the first segment of the keys that address the upstream host outside the proxied base URL. A composite repository may name children in a sibling directory ("../../technology/epp/packages/2024-12/"); the proxy serves them under "_host/<path on the upstream host>" so that the client stays inside the proxy.

View Source
const (
	// MaxXMLBytes bounds the XML of one document after decompression. A real
	// artifacts.xml of a large update site is some tens of MiB.
	MaxXMLBytes int64 = 512 << 20
)

Limits of the documents the proxy rewrites. The documents come from an upstream the operator trusts to publish software, not to be benign: every bound below holds for hostile input.

Variables

View Source
var (
	// ErrBadArchive is returned for a jar that is not the plain archive of one
	// XML document.
	ErrBadArchive = errors.New("p2: unusable metadata archive")
	// ErrNoEntry is returned when a jar lacks the XML entry its name promises.
	ErrNoEntry = errors.New("p2: archive has no document entry")
)
View Source
var (
	// ErrNotRepository is returned for a document that is not a p2 repository
	// (a captive portal page that parses as XML, a second root element).
	ErrNotRepository = errors.New("p2: not a repository document")
	// ErrDirective is returned for a DTD or any other <!...> directive. p2
	// documents have none, and refusing them keeps entity tricks out.
	ErrDirective = errors.New("p2: directive in a repository document")
	// ErrTooDeep is returned for elements nested deeper than p2 documents are.
	ErrTooDeep = errors.New("p2: document nests too deeply")
	// ErrTokenTooLarge is returned for a single XML token over its bound.
	ErrTokenTooLarge = errors.New("p2: XML token too large")
	// ErrTruncated is returned for a document that ends inside an element.
	ErrTruncated = errors.New("p2: document ends inside an element")
	// ErrTooLarge is returned for a document whose XML exceeds MaxXMLBytes.
	ErrTooLarge = errors.New("p2: document too large")
)
View Source
var ErrBadURL = errors.New("p2: not an absolute http(s) URL")

ErrBadURL is returned for an upstream or document URL that is not an absolute http(s) URL.

Functions

func Coordinates

func Coordinates(key string) (string, string, bool)

Coordinates recovers the id and version of a bundle or feature from the key of its file ("plugins/org.eclipse.core.runtime_3.31.0.v20240101.jar"); ok is false for anything else, binaries included (their names carry no extension, and an id there is a classifier path, not a bundle).

func IsArtifact

func IsArtifact(key string) bool

IsArtifact reports whether key is an artifact: a file of a plugins, features or binary directory. Artifacts are named by id and version, so they never change once published.

func IsMetadata

func IsMetadata(key string) bool

IsMetadata reports whether key names a repository metadata file (p2.index, content, artifacts and composite documents, with their checksum files).

func RewriteXML

func RewriteXML(dst io.Writer, src io.Reader, opts Options) error

RewriteXML copies the p2 repository document in src to dst with the repository properties p2.mirrorsURL and p2.statsURI removed (the size of the properties element follows) and the child locations of a composite repository relocated through opts. Everything else is copied byte for byte: the rewriter tracks the offsets of the tokens it reads and passes the bytes between them through, so attribute quoting, whitespace and comments survive.

It reads src once, holding one token at a time, and refuses what a repository document never contains: a root other than <repository>, a directive (DTD), nesting beyond maxDepth, a token over maxTokenBytes, a document that ends inside an element. The decoder does not expand entities, so a document cannot grow in the rewriter; the caller bounds the length of src.

func UpstreamURL

func UpstreamURL(upstream, key string) (string, bool)

UpstreamURL maps a key of the proxy to the URL it is fetched from: the key below the upstream URL, or for a HostPrefix key the path on the upstream host. ok is false when key addresses the host namespace with nothing below it.

Types

type Codec

type Codec int

Codec is how a metadata document is stored.

const (
	// CodecXML is the plain "<name>.xml" file.
	CodecXML Codec = iota
	// CodecJar is "<name>.jar": a zip archive that holds "<name>.xml".
	CodecJar
	// CodecXZ is "<name>.xml.xz".
	CodecXZ
)

type Doc

type Doc struct {
	// Name is the base name of the document without extension ("artifacts",
	// "compositeContent", ...); Name + ".xml" is the entry of a jar.
	Name  string
	Codec Codec
	// Composite is set for the composite repositories, whose children point at
	// other repositories.
	Composite bool
}

Doc is a metadata document the proxy rewrites before it caches it.

func ParseDoc

func ParseDoc(key string) (Doc, bool)

ParseDoc recognises the rewritten metadata documents by the last segment of a key: artifacts, compositeArtifacts and compositeContent in the three encodings.

func (Doc) Entry

func (d Doc) Entry() string

Entry is the name of the XML file inside a jar.

func (Doc) Rewrite

func (d Doc) Rewrite(dst io.Writer, src io.ReaderAt, size int64, opts Options) error

Rewrite decodes the document d stored in src (size bytes), rewrites it with RewriteXML and writes it to dst in the same encoding. A jar is rewritten into a jar of one deflated entry; an xz document into one xz stream.

Hardening: the decoded XML is bounded by MaxXMLBytes whatever the encoding declares (a bomb is refused, not half-read), a jar is refused when its directory announces more than maxJarEntries entries (before anything is allocated for them) or has no entry of the document's name, and an xz stream that asks for a dictionary over the pkgformat limit, or holds more than one stream, is refused.

type Options

type Options struct {
	// Relocate maps the location of a composite repository's child (see
	// Relocator.Relocate); nil leaves the children alone.
	Relocate func(location string) (string, bool)
}

Options tune RewriteXML.

type Relocator

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

Relocator maps the locations of a composite repository's children onto keys of the proxy. The proxy serves the files below its upstream URL under the same relative path, and the rest of the upstream host under HostPrefix, so a child that the upstream names by absolute URL, or by a relative path that leaves the repository, is reached through the proxy too.

func NewRelocator

func NewRelocator(upstream, docURL, key string) (*Relocator, error)

NewRelocator returns the relocator of the document stored under key, which is fetched from docURL, of a proxy whose upstream is upstream.

func (*Relocator) Relocate

func (r *Relocator) Relocate(location string) (string, bool)

Relocate returns the location a client must use for the child at location, and reports whether it differs from location. A child on another host, and any location that carries a query or fragment (a download redirector, not a repository directory), are left as they are: the client reaches them directly.

Directories

Path Synopsis
Package p2test builds p2 repository documents for tests.
Package p2test builds p2 repository documents for tests.

Jump to

Keyboard shortcuts

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