v0.0.0-...-bd9ca74 Latest Latest

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

Go to latest
Published: Oct 27, 2021 License: Apache-2.0 Imports: 9 Imported by: 1



Package bagit implements the enough of the BagIt specification to save and read the BagIt files used by Bendo. It creates zip files which do not use compression. It only supports MD5 and SHA256 checksums for the manifest file.

Specific items not implemented from the BagIt specification are fetch files and holely bags. It also doesn't preserve the order of the tags nor multiple occurrences of tags in the bag-info.txt file.

This package allows for reading a bag, verifying a bag, and creating new bags. It does not provide any services for updating a bag. Checksums are generated for each file when a bag is created. After that, checksums are only calculated when a bag is explicitly verified. In particular, checksums are not calculated when reading content from a bag.

The interface is designed to somewhat mirror the archive/zip interface.

The BagIt spec can be found at



View Source
const (
	// Version is the version of the BagIt specification this package implements.
	Version = "0.97"


View Source
var (
	ErrExtraFile   = errors.New("bagit: extra file")
	ErrMissingFile = errors.New("bagit: missing file")
	ErrChecksum    = errors.New("bagit: checksum mismatch")

Possible verification errors.

View Source
var (
	// ErrNotFound means a stream inside a zip file with the given name
	// could not be found.
	ErrNotFound = errors.New("stream not found")


This section is empty.


type Bag

type Bag struct {
	// contains filtered or unexported fields

Bag represents a single BagIt file.

func New

func New() Bag

New creates a new BagIt structure. Is this needed at all?

type BagError

type BagError struct {
	Err  error
	File string

BagError is used to hold a validation error and the file in the bag which has the error.

func (BagError) Error

func (err BagError) Error() string

type Checksum

type Checksum struct {
	MD5    []byte
	SHA1   []byte
	SHA256 []byte
	SHA512 []byte

Checksum contains all the checksums we know about for a given file. Some entries may be empty. At least one entry should be present.

type Reader

type Reader struct {
	// contains filtered or unexported fields

Reader allows for reading an existing Bag file (in ZIP format).

A Reader does not validate checksums or load tags until asked to do so. Use Verify() to hash and verify all the manifests. Call Tags() to load the tag files "bagit.txt" and "bag-info.txt". Other tag files are ignored. Since tags are stored in a map, the order of the tags is not preserved.

func NewReader

func NewReader(r io.ReaderAt, size int64) (*Reader, error)

NewReader creates a bag reader which wraps r. It expects a ZIP datastream, and uses size to locate the zip manifest block, which is at the end.

Closing a reader does not close the wrapped ReaderAt.

func (*Reader) Checksum

func (r *Reader) Checksum(name string) *Checksum

Checksum returns a pointer to the Checksum struct of the given filername culled from the manifests map. It assumes that the request file resides in the data directory of the bag, and so prepends "data/" to the filename provided. If no entry exists for the given file, it returns nil

func (*Reader) Files

func (r *Reader) Files() []string

Files returns a list of the payload files inside this bag (as opposed to the tag and manifest files). The initial "data/" prefix is removed from the file names.

The result is recalculated each time the function is called. For large bags generating the result could take a while.

func (*Reader) Open

func (r *Reader) Open(name string) (io.ReadCloser, error)

Open returns a reader for the file having the given name. Note, that inside the bag, the file is searched for from the path "<bag name>/data/<name>".

func (*Reader) Tags

func (r *Reader) Tags() map[string]string

Tags returns a map giving all of the tags stored in this bag. The keys are the tag names, and the values are the value of the tag. Since tags are returned as a map, the order of the tags in the tag files is not preserved. Also, if a tag should occur more than one time, only the last occurrence is returned.

The tag files "bagit.txt" and "bag-info.txt" are read. Other tag files are ignored.

func (*Reader) Verify

func (r *Reader) Verify() error

Verify computes the checksum of each file in this bag, and checks it against the manifest files. Both payload ("data/") and tag files are checked. The file list is read from manifest files for MD5, SHA1, SHA256, and SHA512 hashes, although only MD5 and SHA256 hashes are actually computed and verified. Files missing an entry in a manifest file, or manifest entires missing a corresponding file will cause a verification error. Tag files which are missing a manifest entry are the only exception to the verification error.

type Writer

type Writer struct {
	// contains filtered or unexported fields

Writer allows for writing a new bag file. When it is closed, all the relevant tag files and manifests will be written out.

func NewWriter

func NewWriter(w io.Writer, name string) *Writer

NewWriter creates a new bag writer which will serialize itself to the provided io.Writer. Use name to set the directory name the bag will unserialize into, as required by the spec.

func (*Writer) Checksum

func (w *Writer) Checksum() *Checksum

Checksum returns the checksums for what has been written so far to the last io.Writer returned by Create().

func (*Writer) Close

func (w *Writer) Close() error

Close this Writer and serialize all necessary bookkeeping files. It does not close the original io.Writer provided to NewWriter().

func (*Writer) Create

func (w *Writer) Create(name string) (io.Writer, error)

Create a new file inside this bag. The file will be put inside the "data/" directory.

func (*Writer) SetTag

func (w *Writer) SetTag(tag, content string)

SetTag adds the given tag to this bag, and sets it to be equal to content. The bag writer will add the tags "Payload-Oxum", "Bagging-Date", and "Bag-Size" itself. Other useful tags are listed in the BagIt specification.

Jump to

Keyboard shortcuts

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