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
- Variables
- func CompareVersions(a, b string) int
- func Fold(v string) string
- func Gunzip(r io.Reader, limit int64) ([]byte, error)
- func Gzip(data []byte) ([]byte, error)
- func Less(a, b string) bool
- func Licenses(field string) []string
- func Listing(page []byte) []string
- func ParseFile(file, ext string) (string, string, bool)
- func ReadRecords(r io.Reader, limit int64, fn func(Record) error) error
- func RenderRDS(recs []Record, binary bool) ([]byte, error)
- func RenderText(recs []Record) []byte
- func StandardFields(binary bool) []string
- func StoredFields(desc Record, binary bool) map[string]string
- func ValidPackageName(name string) bool
- func ValidVersion(v string) bool
- type Field
- type Kind
- type Path
- type Record
- type Tree
- type Variant
Constants ¶
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.
const ArchiveDir = "Archive"
ArchiveDir is the directory below SourceTree that holds older source versions, one subdirectory per package.
const MaxNameLen = 128
MaxNameLen bounds a package name.
const MaxVersionLen = 64
MaxVersionLen bounds a package version.
const SourceTree = "src/contrib"
SourceTree is the directory of source packages.
Variables ¶
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") )
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.
var ErrUnsupportedExt = errors.New("cran: unsupported package extension")
ErrUnsupportedExt is returned for a package file extension other than .tar.gz, .tgz or .zip.
var Source = Tree{Dir: SourceTree, Ext: ".tar.gz"} //nolint:gochecknoglobals // constant value
Source is the source tree.
Functions ¶
func CompareVersions ¶
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 ¶
Fold collapses every run of white space (continuation line breaks included) of a field value to one space.
func Less ¶
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 ¶
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 ¶
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 ReadRecords ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
type Record ¶
type Record []Field
Record is one DCF paragraph: its fields in document order. A field name occurs at most once.
func IndexRecord ¶
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 ¶
ParseDescription reads the single record of a DESCRIPTION file.
func ParsePackages ¶
ParsePackages reads a PACKAGES document. Every record must name a package and its version.
func ReadDescription ¶
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.
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.
type Variant ¶
type Variant int
Variant is the encoding of a PACKAGES index.
func (Variant) ContentType ¶
ContentType is the media type the variant is served with.