cargo

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package cargo validates crates and supplies Cargo sparse-index records. A crate version is one gzip-compressed TAR archive, stored at crates/<name>/<name>-<version>.crate, whose Cargo.toml must name the crate and version its path does. Its index record, the line Cargo reads from the crate's sparse index file, is kept as a property of the archive's item, so the item tree remains the source of truth.

Role

artifacts module, ecosystem layer: the crate validation and sparse index rules of the Cargo slice. Its Handler plugs into the item service through the format hooks (decision 0032), so a crate published with cargo publish and one written through the item routes meet the same checks; the slice's http package (artifacts/ecosystem/cargo/http) serves the sparse registry and web API over what it keeps.

Inputs

  • Driving: the format registry (artifacts/ecosystem), which hands the item service's hooks for cargo repositories to Handler, and the work queue, which runs every format's reindex through it (JFrog's /api/cargo/{repoKey}/reindex schedules one); and the protocol, which checks names and versions (ValidName, ValidVersion), builds and parses paths (IndexPath, CratePath, ParseCratePath), checks published and fetched crates (Inspect) and encodes the records it serves (Record, Encode).
  • Driven: the format hooks' environment (format.Env): its blobs hold the archives, and its store holds the items and their properties. The generic blackout and include and exclude checks (artifacts/ecosystem/generic) run before the format's own.
  • Settings: none.

Outputs

  • Handler: the hooks that read an archive once before its transaction (Inspect), admit only a valid crate at a canonical path (Admit), keep each stored archive's index record (Deployed), and recheck every stored crate and rewrite its record (Reindex).
  • The cargo.index item property (RecordProperty): a version's index record as one JSON line, with the dependencies and features a native publish declared; an archive written through the item routes without one gets a record with none. The cargo.yanked property (YankedProperty) holds the protocol's yank flag, which Deployed folds into the record.
  • The crate and index rules: Inspect, ValidName, ValidVersion, IndexPath, CratePath, ParseCratePath, Record, Dependency, Encode, ErrInvalid and the size bounds (MaxCrate, MaxIndex).

Invariants

  • A crate is read to its end within fixed bounds (64 MiB compressed, 512 MiB expanded, 10,000 entries and a 1 MiB Cargo.toml): every entry is a unique regular file or folder under <name>-<version>/, the TAR ends with its two zero blocks, only zeros follow them, and the gzip checksum verifies.
  • A record is kept and served only when it is valid (Record.IsValid) and names the crate, version and SHA-256 of the archive it belongs to.
  • The property names are stored, so they stay byte-identical.

References

Status

Implemented P9c, which delivered the plan's P30 group (issues #762 to #768). Moved here in P9d.

Testing

  • cargo_test.go: the sparse index and crate paths, record bounds, manifest validation, and unsafe or corrupt archives and tails.
  • handler_test.go: a crate's lifecycle and reindex through the item service, and the writes the hooks refuse.

Index

Constants

View Source
const (
	// MaxCrate bounds a crate archive at 64 MiB compressed, however it
	// arrives: a native publish, an item write or a remote's fetch.
	MaxCrate = 64 << 20

	// MaxIndex bounds an upstream sparse index document, config.json or an
	// index file, that a remote repository reads, at 16 MiB.
	MaxIndex = 16 << 20
	// RecordProperty is the item property that holds a crate version's
	// index record, the one JSON line Encode makes. Items keep the name, so
	// it never changes.
	RecordProperty = "cargo.index"
	// YankedProperty is the item property that holds a crate version's yank
	// flag, "true" or "false", which Deployed copies into the record. Items
	// keep the name, so it never changes.
	YankedProperty = "cargo.yanked"
)

Variables

View Source
var (

	// ErrInvalid is the error Inspect and Encode return for a crate archive
	// or index record they refuse; Inspect wraps it with the gzip or TAR
	// reader's fault when one caused the refusal.
	ErrInvalid = errors.New("cargo: invalid cargo crate")
)

Functions

func CratePath

func CratePath(name, version string) string

CratePath is the item path of the archive of crate name at version: crates/<name>/<name>-<version>.crate. It keeps the case of name and checks neither argument.

func Encode

func Encode(r Record) (string, error)

Encode returns r as its index line, compact JSON without a trailing newline, or ErrInvalid when r is not valid (IsValid).

func IndexPath

func IndexPath(name string) string

IndexPath is the path of crate name's sparse index file below the index root, from the lowercased name, as Cargo lays the index out: 1/<name> and 2/<name> for names of one and two characters, 3/<first>/<name> for three, and <first two>/<next two>/<name> for longer names. name must be valid (ValidName).

func Inspect

func Inspect(ctx context.Context, body io.Reader, size int64, p string) (string, string, error)

Inspect validates the complete bounded .crate gzip tarball. Every entry must be a unique path under the archive root, and the manifest identity must match the canonical item path.

func ParseCratePath

func ParseCratePath(p string) (name, version string, ok bool)

ParseCratePath splits an item path into the crate name and version whose archive it holds, and reports ok only when p is exactly CratePath(name, version) with a valid name and version.

func Serves

func Serves(t repo.PackageType) bool

Serves reports whether t is the Cargo package type.

func ValidName

func ValidName(s string) bool

ValidName reports whether s is a crate name the registry accepts: an ASCII letter followed by up to 63 ASCII letters, digits, underscores and hyphens.

func ValidVersion

func ValidVersion(s string) bool

ValidVersion reports whether s is a semantic version: major.minor.patch with an optional pre-release and build metadata.

Types

type Dependency

type Dependency struct {
	Name            string   `json:"name"`
	Req             string   `json:"req"`
	Features        []string `json:"features"`
	Optional        bool     `json:"optional"`
	DefaultFeatures bool     `json:"default_features"`
	Target          *string  `json:"target"`
	Kind            string   `json:"kind"`
	Registry        *string  `json:"registry"`
	Package         *string  `json:"package"`
}

Dependency is one entry of a record's deps, with the JSON fields of Cargo's index format: the name the dependent crate uses for it, its version requirement, its features, whether it is optional and whether its default features are on, its target and its kind (normal, dev or build), the index of the registry it comes from (nil for this one), and Package, the crate's own name when the dependency renames it.

type Handler

type Handler struct{ generic.Handler }

Handler is the Cargo format's hooks: it admits a crate archive only at its canonical path and only when its Cargo.toml names the crate and version that path does, and keeps each stored archive's index record. It embeds the generic hooks, so every path is classed as generic content is.

func (Handler) Admit

func (Handler) Admit(
	ctx context.Context,
	env format.Env,
	c repo.Config,
	w format.Write,
) (format.Write, error)

Admit applies the generic deploy policy, wrapping its refusal, then passes a checksum deploy. Any other write must be at a crate path, crates/<name>/<name>-<version>.crate (400), and its archive must be a valid crate whose Cargo.toml names that crate and version (400). A remote's copy at any other path is kept as a plain file (format.RemoteCopy). It reuses Inspect's reading when it covers the same content and path, and reads the stored archive itself otherwise.

func (Handler) Deployed

func (Handler) Deployed(ctx context.Context, env format.Env, _ repo.Config, it model.Item) error

Deployed keeps a stored crate's index record: it checks the archive at a crate path again and writes RecordProperty as the record Encode makes, taken from the record already there, as a native publish sets it, or else from the path and checksum with no dependencies or features, with the yank flag from YankedProperty. A record that is invalid or names another crate, version or checksum is refused with 400, as is an archive that no longer validates. Folders and files at other paths change nothing.

func (Handler) Inspect

func (Handler) Inspect(
	ctx context.Context,
	env format.Env,
	c repo.Config,
	w format.Write,
) (any, error)

Inspect reads and validates the stored archive of a write to a .crate path before the write's transaction, and returns what it found for Admit, so an archive is read once per write. A checksum deploy, a remote's copy outside the crate layout and any other file inspect nothing. An invalid crate is refused with 400; an archive that cannot be opened fails the write.

func (Handler) Reindex

func (Handler) Reindex(ctx context.Context, env format.Env, c repo.Config) error

Reindex runs Deployed over every stored .crate file of a local or federated repository and stops at the first that fails; any other class is refused with 400.

type Record

type Record struct {
	Name        string              `json:"name"`
	Version     string              `json:"vers"`
	Deps        []Dependency        `json:"deps"`
	Checksum    string              `json:"cksum"`
	Features    map[string][]string `json:"features"`
	Yanked      bool                `json:"yanked"`
	Links       *string             `json:"links,omitempty"`
	RustVersion *string             `json:"rust_version,omitempty"`
}

Record is one version's line in a crate's sparse index file, with the JSON fields of Cargo's index format: the name and version, the dependencies, the SHA-256 of the archive (cksum), the features, the yank flag and the optional links and rust_version. RecordProperty holds it on the archive's item.

func (Record) IsValid

func (r Record) IsValid() bool

IsValid reports whether r may be kept and served: a valid name and version, a checksum of 64 lowercase hexadecimal characters, deps and features that are not nil, so they encode as [] and {} rather than null, and dependencies with valid names and version requirements of 1 to 256 bytes.

Directories

Path Synopsis
Package cargohttp serves the HTTP sparse registry and Cargo web API on Cargo local, federated and remote repositories, at /artifactory/api/cargo/{repoKey}: the sparse index's config.json and index files, the native publish, crate downloads, yank and unyank, and a crate's version list.
Package cargohttp serves the HTTP sparse registry and Cargo web API on Cargo local, federated and remote repositories, at /artifactory/api/cargo/{repoKey}: the sparse index's config.json and index files, the native publish, crate downloads, yank and unyank, and a crate's version list.

Jump to

Keyboard shortcuts

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