cran

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: 14 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 cran implements the parts of the R package repository protocol that do not depend on the server: the repository path grammar, the DESCRIPTION (Debian control file, "DCF") parser, R's version ordering, the PACKAGES index in its three encodings (plain, gzip, serialized R object) and the reading of DESCRIPTION out of a source or binary package archive.

Every input is untrusted: parsers are bounded in size, never recurse and are fuzzed.

Index

Constants

View Source
const (
	// MaxDescriptionBytes bounds a DESCRIPTION file.
	MaxDescriptionBytes = 1 << 20
	// MaxIndexBytes bounds an uncompressed PACKAGES file of an upstream
	// repository (CRAN's is about 30 MiB).
	MaxIndexBytes = 256 << 20
)

Limits of the DCF reader.

View Source
const ArchiveDir = "Archive"

ArchiveDir is the directory below SourceTree that holds older source versions, one subdirectory per package.

View Source
const MaxNameLen = 128

MaxNameLen bounds a package name.

View Source
const MaxVersionLen = 64

MaxVersionLen bounds a package version.

View Source
const SourceTree = "src/contrib"

SourceTree is the directory of source packages.

Variables

View Source
var (
	// ErrSyntax marks a document that is not valid DCF.
	ErrSyntax = errors.New("cran: malformed DCF")
	// ErrTooLarge marks a DCF document or field beyond the limits.
	ErrTooLarge = errors.New("cran: DCF too large")
)
View Source
var ErrNoDescription = errors.New("cran: package archive has no DESCRIPTION")

ErrNoDescription marks a package archive without a DESCRIPTION file in its top-level package directory.

View Source
var ErrUnsupportedExt = errors.New("cran: unsupported package extension")

ErrUnsupportedExt is returned for a package file extension other than .tar.gz, .tgz or .zip.

View Source
var Source = Tree{Dir: SourceTree, Ext: ".tar.gz"} //nolint:gochecknoglobals // constant value

Source is the source tree.

Functions

func CompareVersions

func CompareVersions(a, b string) int

CompareVersions orders two valid versions the way R's package_version does: component by component as integers (so "1.10" > "1.9" and "1.0" == "1-0" in the components), a version that is a prefix of the other being the older. It returns -1, 0 or 1. Two spellings of one version that differ only in the separators or leading zeros compare equal, so callers that need a total order break the tie on the literal string.

func Fold

func Fold(v string) string

Fold collapses every run of white space (continuation line breaks included) of a field value to one space.

func Gunzip

func Gunzip(r io.Reader, limit int64) ([]byte, error)

Gunzip decompresses a gzip stream of at most limit decompressed bytes.

func Gzip

func Gzip(data []byte) ([]byte, error)

Gzip compresses data as a gzip stream.

func Less

func Less(a, b string) bool

Less is the total order used to pick the newest of several versions: R's ordering with the literal text as the tie-break.

func Licenses

func Licenses(field string) []string

Licenses splits the License field of a DESCRIPTION into the licenses it offers: alternatives are separated by "|", and a "+ file LICENSE" (or LICENCE) suffix, which points at a file of the package rather than naming a license, is dropped. "GPL-2 | file LICENSE" yields ["GPL-2"].

func Listing

func Listing(page []byte) []string

Listing returns the link targets of an Apache-style directory index, in document order: what remotes::install_version reads out of src/contrib/Archive/<pkg>/ and what the hosted repository publishes at src/contrib/Archive/ as its package names. Absolute, query and parent links are not entries and are skipped.

func ParseFile

func ParseFile(file, ext string) (string, string, bool)

ParseFile splits "<package>_<version><ext>".

func ReadRecords

func ReadRecords(r io.Reader, limit int64, fn func(Record) error) error

ReadRecords parses a DCF document of at most limit bytes (limit <= 0 means MaxIndexBytes) and calls fn with every record, in order; fn returning an error stops the read with that error. Records are separated by blank lines; a line starting with white space continues the previous field. A repeated field in one record, a line that is neither a field nor a continuation, invalid UTF-8 and NUL bytes are errors for strict readers; this one repairs invalid UTF-8 (a latin1 DESCRIPTION is common in old packages) and rejects the rest.

func RenderRDS

func RenderRDS(recs []Record, binary bool) ([]byte, error)

RenderRDS is PACKAGES.rds for recs: a gzip-compressed version 2 XDR serialization of a character matrix (one row per record, one column per field, NA for a missing field) with dimnames list(NULL, fields), which is what R's write_PACKAGES saves and available.packages reads. The columns are the standard fields of the tree followed by every other field the records carry, in order of first appearance.

func RenderText

func RenderText(recs []Record) []byte

RenderText is the PACKAGES document of recs: records in the given order, fields in the record's order, separated by blank lines. A value that spans lines is written with continuation lines.

func StandardFields

func StandardFields(binary bool) []string

StandardFields returns the index fields of a source (binary false) or binary tree, in the order a PACKAGES record lists them. The slice is shared.

func StoredFields

func StoredFields(desc Record, binary bool) map[string]string

StoredFields picks the index fields of a package out of its DESCRIPTION, folded to one line each and without the empty ones. Package, Version and MD5sum are not among them: the first two come from the package's identity and the checksum from the stored file.

func ValidPackageName

func ValidPackageName(name string) bool

ValidPackageName reports whether name is an R package name: ASCII letters, digits and dots, starting with a letter, ending with a letter or digit, at least two characters.

func ValidVersion

func ValidVersion(v string) bool

ValidVersion reports whether v is an R package version: non-negative integers separated by "." or "-" (R accepts both as one separator), such as "1.0", "0.9-3" or "2.1.0.9000". R itself requires at least two components for a package, but old repositories hold single-component versions and R's package_version() reads them, so one component is accepted.

Types

type Field

type Field struct {
	Name, Value string
}

Field is one "Name: value" pair of a record. Continuation lines of a value are joined with "\n", without their leading white space.

type Kind

type Kind int

Kind is the type of a repository request path.

const (
	// KindOther is a path a CRAN repository does not serve.
	KindOther Kind = iota
	// KindIndex is a tree's PACKAGES, PACKAGES.gz or PACKAGES.rds.
	KindIndex
	// KindPackage is a package file of a tree: <pkg>_<version>.tar.gz, .zip
	// or .tgz below the tree's directory.
	KindPackage
	// KindArchiveRoot is the listing of the source tree's Archive directory.
	KindArchiveRoot
	// KindArchiveDir is the listing of Archive/<pkg>/.
	KindArchiveDir
	// KindArchivePackage is Archive/<pkg>/<pkg>_<version>.tar.gz: an older
	// source package. Its canonical path is the source tree's.
	KindArchivePackage
)

type Path

type Path struct {
	Kind Kind
	Tree Tree
	// Variant is the encoding of a KindIndex path.
	Variant Variant
	// File is the package file name of KindPackage and KindArchivePackage.
	File string
	// Package is the package name of every kind but the index and the archive
	// root; Version is the version in File.
	Package, Version string
}

Path is a parsed repository request path.

func ParsePath

func ParsePath(rel string) (Path, bool)

ParsePath classifies a path relative to the repository root. A leading slash is allowed. ok is false for a path that is not one a CRAN repository serves.

func (Path) Canonical

func (p Path) Canonical() string

Canonical is the path the package file is stored and listed under: the tree's directory plus the file name (never the Archive spelling).

type Record

type Record []Field

Record is one DCF paragraph: its fields in document order. A field name occurs at most once.

func IndexRecord

func IndexRecord(pkg, version, md5 string, fields map[string]string, binary bool) Record

IndexRecord is the PACKAGES record of one package file: identity, the stored fields and the MD5 of the file, in the standard field order.

func ParseDescription

func ParseDescription(r io.Reader) (Record, error)

ParseDescription reads the single record of a DESCRIPTION file.

func ParsePackages

func ParsePackages(r io.Reader, limit int64) ([]Record, error)

ParsePackages reads a PACKAGES document. Every record must name a package and its version.

func ReadDescription

func ReadDescription(ra io.ReaderAt, size int64, ext string) (string, Record, error)

ReadDescription reads <dir>/DESCRIPTION out of a package archive of size bytes: a ".tar.gz" or ".tgz" tarball, or a ".zip" (ext selects). dir is the archive's top-level directory, which for a well-formed package is its name.

func (Record) Get

func (r Record) Get(name string) string

Get returns the value of the field name (matched exactly, as R does), "" when absent.

func (Record) Lookup

func (r Record) Lookup(name string) (string, bool)

Lookup returns the value of the field name and whether the record has it.

type Tree

type Tree struct {
	// Dir is the tree's path relative to the repository root:
	// "src/contrib", "bin/windows/contrib/4.4", "bin/macosx/contrib/4.4" or
	// "bin/macosx/big-sur-arm64/contrib/4.4".
	Dir string
	// Binary is false for the source tree.
	Binary bool
	// Ext is the file extension of the packages of the tree: ".tar.gz",
	// ".zip" or ".tgz".
	Ext string
}

Tree is one package tree of a repository: the directory its PACKAGES index and its package files live in.

func ParseTree

func ParseTree(dir string) (Tree, bool)

ParseTree recognises a tree directory.

type Variant

type Variant int

Variant is the encoding of a PACKAGES index.

const (
	// VariantText is the plain PACKAGES file.
	VariantText Variant = iota
	// VariantGzip is PACKAGES.gz.
	VariantGzip
	// VariantRDS is PACKAGES.rds, a gzip-compressed serialized R matrix.
	VariantRDS
)

func (Variant) ContentType

func (v Variant) ContentType() string

ContentType is the media type the variant is served with.

func (Variant) File

func (v Variant) File() string

File is the file name of the variant.

Jump to

Keyboard shortcuts

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