cran

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: 25 Imported by: 0

Documentation

Overview

Package cran implements the CRAN package, index and archive rules. It checks every R package archive a repository stores or caches against its path and DESCRIPTION, keeps the package index and the cran.name, cran.version and cran.feed item properties in step with the packages, and derives a local or federated repository's PACKAGES and PACKAGES.gz for each feed directory R reads. It also reads the PACKAGES indexes that remote and virtual reads rely on, and finds the source archives still in the legacy archive layout.

Role

artifacts module, ecosystem layer: the CRAN package, index and archive rules of the CRAN slice. Its Handler plugs into the item service through the format hooks (decision 0032), so every write and delete in a CRAN repository reaches it whichever API carried it, and it rebuilds the indexes as queued work (decision 0033). The CRAN protocol is the http subpackage (cranhttp), which serves every class's index, the native POST uploads and the legacy archive migration with this package.

Inputs

  • Driving: the format registry (artifacts/ecosystem), which hands the item service's hooks for cran repositories to Handler; the work queue, which runs the index rebuild (KindIndex) through Handler.Work and every format's reindex through Handler.Reindex; the protocol (cranhttp), which answers an index read by decoding the member's index (DecodeIndex: a local or federated repository's derived one once its rebuild is done, a remote's from upstream) and encoding again only the packages the caller has Read permission on, a package the download policy's patterns reject among them, a virtual repository's members merged in order (EncodeIndex), whose POST /api/cran/{repoKey}/sources and binaries check the feed (Feed) and take the package's identity from the archive (ReadUploaded, ErrPackage), and whose archive migration (GET /api/cran/{repoKey}/move-archives) finds what to move (LegacyArchiveTarget); and the item kit (artifacts/adapters/http/itemkit), whose remote source request checks an upstream index or package before a remote caches it (IndexPath, DecodeIndex, IsPackage, ReadPackage).
  • Driven: the format hooks' environment (format.Env): its blobs hold the packages; its store holds the items, their properties and the package index (artifacts/core/pkgindex), and lists a repository's files by AQL (artifacts/core/aql); Enqueue asks for the index rebuild when the write commits; and Derive and Underive write and remove the indexes. The shared xz reader (platform/xzfile) reads a .tar.xz source archive whole, as R reads it through liblzma. The generic deploy policy (artifacts/ecosystem/generic) is the first check Admit applies.
  • Settings: none. The archive migration's switch (cran.archive_mover_enabled, off by default) belongs to the protocol that serves it (cranhttp.CranAPI.ArchiveMoverEnabled).

Outputs

  • Handler: the hooks that read a package before its transaction (Inspect), admit only a valid package at a valid path and refuse a write of an index to a repository that derives its own (Admit), keep the package index and properties in step with deploys and deletes (Deployed, Deleting, Deleted), class the indexes as derived (Classify), make an index read wait for its rebuild (Owed), derive the indexes (WorkKinds, Work) and rebuild the package index (Reindex).
  • The path rules: Serves, Feed, IndexPath, PackagePath, IsPackage, ValidName and ValidVersion, and the index names PackagesName and PackagesGzipName.
  • The package rules: Package, ReadPackage and ReadUploaded, which fail with ErrPackage.
  • The index rules: EncodeIndex, DecodeIndex and CompareVersion.
  • The archive rule: LegacyArchiveTarget.
  • The stored names: KindIndex and the Prop* property names.

Invariants

  • A package's name and version come from its file name, <name>_<version> with its feed's suffix (.tar.gz, .tar.bz2 or .tar.xz under src/contrib; .zip, .tgz and .tar.gz under the Windows, macOS and Linux binary feeds), and its DESCRIPTION must give the same Package and Version.
  • An archive is read to its end, compressor checksums and ZIP CRCs included, before its DESCRIPTION is trusted, within fixed bounds (1 GiB unpacked, 50,000 entries, a 256 KiB DESCRIPTION), and every entry must lie in the package's own folder.
  • A feed is a terminal directory: src/contrib, or bin/<windows|linux|macosx>/contrib/<R major.minor>, macOS with an optional build folder before contrib. Each feed has its own PACKAGES and PACKAGES.gz, which list only the newest version of each package (CompareVersion, R's version order) with the MD5 sum of its stored bytes. A package in a folder below its feed, such as src/contrib/Archive/<name>/, stays downloadable but is never listed.
  • An index is at most 32 MiB and 100,000 records. A decoded index lists each package once, every record naming a file of the index's own feed, so an upstream index cannot point a client elsewhere.
  • A local or federated repository's PACKAGES and PACKAGES.gz are derived, never uploaded (409); a remote caches the upstream's after the item kit's remote source request has checked it, and cached content queues no rebuild.
  • KindIndex and the property names are stored, so they stay byte-identical.

References

Status

Implemented P9c, which delivered the plan's P32 group (issues #777 to #780 and #789). Moved here in P9d.

Testing

  • package_test.go: the path grammar, ReadPackage on the unchanged CRAN source archive and Windows binary, archive identity and integrity, unsafe ZIP entries, and ReadUploaded's identity and refusals.
  • index_test.go: EncodeIndex's newest version and round trip, empty feeds and size limits, excess records, untrusted paths and duplicates.
  • archive_test.go: LegacyArchiveTarget.
  • edge_test.go: index ownership and the upload policy, untrusted archive content, .tar.xz and binary indexes, the DESCRIPTION and index trust boundary, cached and remote content queuing nothing, untrusted index records, damaged compressed indexes and a canceled ZIP read.
  • work_test.go: the hooks and Work: inspection of committed bytes, storage and work queue failures, regenerating only advertised packages, removing a feed's index after its last package, archives and unrelated files kept out of the index, and the refusals.
  • xz_test.go: a .tar.xz source package whose xz stream lost its index and footer, refused at both package paths (#1331).
  • xz_budget_test.go: that package, whole and cut, answers the same under the xz dictionary budget as without it (#1354).

testdata holds the unchanged CRAN assertthat 0.2.1 source archive and Windows binary and its published PACKAGES entry, which the suites in test/server/artifacts/cran, the protocol's tests (http/cran_test.go, cran_routes_test.go and cran_archive_test.go) and the acceptance steps also read. The protocol's native R evidence is the @cran acceptance partition (test/acceptance/features/cran.feature).

Index

Constants

View Source
const (
	KindIndex   = "cran.index"
	PropName    = "cran.name"
	PropVersion = "cran.version"
	PropFeed    = "cran.feed"
)

KindIndex is the work kind that rebuilds a local or federated repository's PACKAGES and PACKAGES.gz (Work), keyed by the repository. PropName, PropVersion and PropFeed are the item properties Deployed and Reindex set on every package from its path, which its DESCRIPTION must match. The work queue and the items keep these values, so they never change.

View Source
const (
	PackagesName     = "PACKAGES"
	PackagesGzipName = "PACKAGES.gz"
)

PackagesName and PackagesGzipName are the file names of a feed's package index, plain and gzip-compressed, as R reads them from a feed directory.

Variables

View Source
var ErrPackage = errors.New("cran: invalid cran package")

ErrPackage is the error the package, upload and index rules return for content that fails their checks: an invalid archive, DESCRIPTION, path, index or index record. Callers match it with errors.Is, as the root upload handler does to answer 400.

Functions

func CompareVersion

func CompareVersion(a, b string) int

CompareVersion follows R's numeric component ordering for version labels; separators divide components and numeric chunks compare by value.

func EncodeIndex

func EncodeIndex(in []Package, feed string) (map[string][]byte, error)

EncodeIndex records only the newest version per package, as a CRAN client expects from a terminal PACKAGES directory. Older files remain addressable.

func Feed

func Feed(p string) (string, bool)

Feed is a terminal R package directory, without a trailing slash.

func IndexPath

func IndexPath(p string) (feed, name string, ok bool)

IndexPath reports whether p is the PACKAGES or PACKAGES.gz of a feed directory, and returns that feed and the file name.

func IsPackage

func IsPackage(p string) bool

IsPackage reports whether p is a package path PackagePath admits.

func LegacyArchiveTarget

func LegacyArchiveTarget(p string) (string, bool)

LegacyArchiveTarget recognizes the source archive layout used by older Artifactory releases. The package and version directories must agree with the filename; unrelated paths and canonical archives are left alone.

func PackagePath

func PackagePath(p string) (feed, name, version string, ok bool)

PackagePath admits only paths from which a native R client can install.

func Serves

func Serves(t repo.PackageType) bool

Serves reports whether t is the CRAN package type.

func ValidName

func ValidName(name string) bool

ValidName reports whether name is a valid R package name: a letter, then letters, digits and dots.

func ValidVersion

func ValidVersion(version string) bool

ValidVersion reports whether version is a valid package version: digits, then any number of letter and digit components, each after a dot or a hyphen.

Types

type Handler

type Handler struct{ generic.Handler }

Handler is the CRAN format's hooks: it checks every R package archive written to a CRAN repository against its path and DESCRIPTION, keeps the package index and the cran.* properties in step with the packages, and derives a local or federated repository's PACKAGES and PACKAGES.gz for each feed as queued work (KindIndex).

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, and passes a checksum deploy. A local or federated repository derives its own PACKAGES and PACKAGES.gz, so a write of either to one of its feeds is refused (409). A file with an R archive suffix (.tar.gz, .tar.bz2, .tar.xz, .tgz or .zip) at a path that is not a package path is refused (400), and a package must be a valid archive that matches its path (400): Admit repeats Inspect's check unless Inspect already read the same content at the same path. Any other file passes.

func (Handler) Classify

func (Handler) Classify(_ repo.Config, p string) format.Class

Classify classes each feed's PACKAGES and PACKAGES.gz as derived, so replication and federation skip them and each side generates its own, and every other path as unique.

func (Handler) Deleted

func (Handler) Deleted(
	ctx context.Context,
	env format.Env,
	c repo.Config,
	removed []model.Item,
) error

Deleted re-syncs each removed package's version in the package index, once per name and version, from that version's remaining packages. A version with none left is not its concern: the item service has already pruned it.

func (Handler) Deleting

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

Deleting queues the index rebuild when a local or federated repository's own package is about to be deleted, so the rebuild is asked for exactly when the delete commits. Folders and other files change nothing.

func (Handler) Deployed

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

Deployed sets a stored package's cran.name, cran.version and cran.feed properties, brings its version in the package index up to date and, for a local or federated repository's own write, queues the index rebuild. A version's files are every package in the repository with that name and version, in any feed or folder, so Deployed reads them all rather than trusting their paths. Folders and other files change nothing.

func (Handler) Inspect

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

Inspect reads and checks the stored content a write puts at a package path, before the write's transaction, and returns the package with its DESCRIPTION fields for Admit and Deployed, so an archive is read once per write. A checksum deploy and any other path inspect nothing. A write with no content digest, or whose archive is invalid or does not match its path, is refused with 400.

func (Handler) Owed

func (Handler) Owed(c repo.Config, cached bool, p string) []format.WorkRef

Owed makes a read of a local or federated repository's PACKAGES or PACKAGES.gz wait for its pending rebuild, so a client never reads an index that lags a completed write. A remote's cache owes nothing.

func (Handler) Reindex

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

Reindex rebuilds repository c's package index from its packages (a remote's from its cache): a package per name and a version per version, whose files are its archives in every feed and whose creation time is the oldest file's, versions in R's order (CompareVersion). It reads every package, failing on the first that is missing or invalid, and restores the cran.* properties unless env.SkipProperties is set. A local or federated repository's index rebuild is then queued.

func (Handler) Work

func (Handler) Work(ctx context.Context, env format.Env, work work.WorkItem) error

Work runs KindIndex for the repository work.Key names: it derives each feed's PACKAGES and PACKAGES.gz from the packages stored directly in that feed, listing the newest version of each with its DESCRIPTION's dependency and build fields and the MD5 sum of its stored bytes (EncodeIndex), and removes the indexes of a feed left with no package. It ignores other kinds, a repository that no longer exists and one that is not a local or federated CRAN repository, and fails on the first package it cannot read.

func (Handler) WorkKinds

func (Handler) WorkKinds() []string

WorkKinds returns KindIndex, the one kind Work runs.

type Package

type Package struct {
	Name, Version, Path, Feed string
	Fields                    map[string]string
	Size                      int64
	MD5, SHA256               string
}

Package is one R package archive: its Name and Version (from the file name, which the DESCRIPTION must match), its repository Path and the Feed directory that path belongs to, and its DESCRIPTION Fields, or a decoded index record's fields. Size, MD5 and SHA256 describe the stored bytes where they are known: ReadPackage sets Size, Work sets all three from the stored item, and DecodeIndex sets MD5 from the record's MD5sum.

func DecodeIndex

func DecodeIndex(body []byte, indexPath string) ([]Package, error)

DecodeIndex reads a feed's PACKAGES or PACKAGES.gz body, named by indexPath, into its package records, an empty body giving none. Every record must give a valid Package and Version whose file (File, or <Package>_<Version> with the feed's suffix, below an optional relative Path) is a package path of the index's own feed, with at most one record per package and an optional 32-digit hex MD5sum. The body is at most 32 MiB, compressed and expanded, and 100,000 records. Anything else fails with ErrPackage.

func ReadPackage

func ReadPackage(ctx context.Context, r io.ReadSeeker, size int64, p string) (Package, error)

ReadPackage checks the entire archive, including trailing CRCs, before its DESCRIPTION is allowed to enter an index.

func ReadUploaded

func ReadUploaded(
	ctx context.Context,
	r io.ReadSeeker,
	size int64,
	feed, suffix string,
) (Package, error)

ReadUploaded infers the package identity from an archive whose POST URL supplies the feed but has no filename. The content still goes through the same complete archive validator as a direct path upload.

Directories

Path Synopsis
Package cranhttp serves the CRAN protocol over the item kit: every class's PACKAGES and PACKAGES.gz, listing only the packages the caller has Read permission on, with a virtual repository's members merged in order; a virtual or remote repository's packages; the native source and binary uploads (POST /api/cran/{repoKey}/sources and /binaries); and the artifactory spec's legacy archive migration (GET /api/cran/{repoKey}/move-archives).
Package cranhttp serves the CRAN protocol over the item kit: every class's PACKAGES and PACKAGES.gz, listing only the packages the caller has Read permission on, with a virtual repository's members merged in order; a virtual or remote repository's packages; the native source and binary uploads (POST /api/cran/{repoKey}/sources and /binaries); and the artifactory spec's legacy archive migration (GET /api/cran/{repoKey}/move-archives).

Jump to

Keyboard shortcuts

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