cpio

package module
v0.9.4 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2021 License: MIT Imports: 10 Imported by: 3

README

pkg.go.dev

About

go-cpio-odc is a pure Go reader and writer for POSIX.1 portable format cpio archives (also known as odc).

Installing

go get github.com/korylprince/go-cpio-odc/v3

If you have any issues or questions create an issue.

Usage

The library provides a Reader and Writer, as well as an fs.FS to browse the archive and a Writer.WriteFS method to write and entire fs.FS to the archive.

See examples on pkg.go.dev.

Testing

go test -v

Some tests require GNU's cpio to be in $PATH. If it's not available, those test will be skipped.

Documentation

Overview

package cpio is a library to read and write POSIX.1 (also known as odc or old portable ASCII) cpio archive files

Index

Examples

Constants

View Source
const (
	MaskFileType   uint64 = 0170000
	ModeSocket     uint64 = 0140000
	ModeSymlink    uint64 = 0120000
	ModeRegular    uint64 = 0100000
	ModeDevice     uint64 = 0060000
	ModeDir        uint64 = 0040000
	ModeCharDevice uint64 = 0020000
	ModeNamedPipe  uint64 = 0010000
	ModeSetuid     uint64 = 0004000
	ModeSetgid     uint64 = 0002000
	ModeSticky     uint64 = 0001000
)
View Source
const DefaultBlockSize = 512

DefaultBlockSize is the default block size when writing. The default matches GNU cpio

Variables

View Source
var HeaderMagic = []byte("070707")
View Source
var TrailerPath = []byte("TRAILER!!!")

Functions

func MarshalFileMode

func MarshalFileMode(m fs.FileMode) uint64

func NewFS

func NewFS(r io.Reader) (fs.FS, error)

NewFS returns an fs.FS by parsing a cpio archive from the given reader. Note: the entire reader is buffered in memory, since cpio archives don't contain a table of contents

func UnmarshalFileMode

func UnmarshalFileMode(m uint64) fs.FileMode

Types

type DirFile

type DirFile struct {
	*File
	// contains filtered or unexported fields
}

func (*DirFile) ReadDir

func (f *DirFile) ReadDir(n int) ([]fs.DirEntry, error)

ReadDir implements the ReadDirFile interface

type FS

type FS struct {
	// contains filtered or unexported fields
}
Example
f, err := os.Open("testdata/test.cpio")
if err != nil {
	log.Fatalln("could not open archive:", err)
}

fs, err := cpio.NewFS(f)
if err != nil {
	log.Fatalln("could not create fs:", err)
}

file, err := fs.Open("test.txt")
if err != nil {
	log.Fatalln("could not open file:", err)
}

info, err := file.Stat()
if err != nil {
	log.Fatalln("could not stat file:", err)
}

log.Println("test.txt mode:", info.Mode())
Output:

func (*FS) Open

func (f *FS) Open(name string) (fs.File, error)

Open implements the fs.FS interface

func (*FS) ReadFile

func (f *FS) ReadFile(name string) ([]byte, error)

ReadFile implements the fs.ReadFileFS interface

type File

type File struct {
	Device       uint64
	Inode        uint64
	FileMode     fs.FileMode
	UID          uint64
	GID          uint64
	NLink        uint64
	RDev         uint64
	ModifiedTime time.Time
	Path         string
	Body         []byte
	// contains filtered or unexported fields
}

File is a file stored in a cpio archive. All uint64 fields are limited to 48 bits, and any higher bits are truncated by Writer

func ReadFile

func ReadFile(path string) (*File, error)

ReadFile returns a *File for the given path. Where possible (on unix), it will use syscalls to get fields not available to os.Stat

func (*File) Close

func (f *File) Close() error

func (*File) Info

func (f *File) Info() (fs.FileInfo, error)

func (*File) IsDir

func (f *File) IsDir() bool

func (*File) ModTime

func (f *File) ModTime() time.Time

func (*File) Mode

func (f *File) Mode() fs.FileMode

func (*File) Name

func (f *File) Name() string

func (*File) Read

func (f *File) Read(b []byte) (int, error)

func (*File) Size

func (f *File) Size() int64

func (*File) Stat

func (f *File) Stat() (fs.FileInfo, error)

func (*File) Sys

func (f *File) Sys() interface{}

func (*File) Type

func (f *File) Type() fs.FileMode

type Reader

type Reader struct {
	// contains filtered or unexported fields
}
Example
f, err := os.Open("testdata/test.cpio")
if err != nil {
	log.Fatalln("could not open archive:", err)
}

r := cpio.NewReader(f)
var (
	files []*cpio.File
	file  *cpio.File
)
for file, err = r.Next(); err == nil; file, err = r.Next() {
	files = append(files, file)
}
if errors.Is(err, io.EOF) {
	log.Println("found", len(files), "files")
	return
}
if err != nil {
	log.Fatalln("could not read archive:", err)
}
Output:

func NewReader

func NewReader(r io.Reader) *Reader

func (*Reader) Next

func (r *Reader) Next() (*File, error)

Next parses and returns the next file in the archive. If no files are left in the archive, err will be io.EOF

type Writer

type Writer struct {
	// contains filtered or unexported fields
}
Example
buf := new(bytes.Buffer)
w := cpio.NewWriter(buf, 0)
f := &cpio.File{
	FileMode: 0644,
	Path:     "hello.txt",
	Body:     []byte("Hello, world!\n"),
}

if err := w.WriteFile(f); err != nil {
	log.Fatalln("could not write file:", err)
}

n, err := w.Close()
if err != nil {
	log.Fatalln("could not close file:", err)
}

log.Println("wrote", n, "bytes")
Output:

func NewWriter

func NewWriter(w io.Writer, blockSize int) *Writer

NewWriter returns a new Writer using the given block size. If blockSize is 0, DefaultBlockSize will be used. Note: the writer doesn't actually write in blockSize blocks. Instead the output is padded with NULL bytes to make the total size a multiple of blockSize.

func (*Writer) Close

func (w *Writer) Close() (int64, error)

Close writes out the trailer file, pads the writer to a multiple of blockSize, and returns the total bytes written

func (*Writer) WriteFS

func (w *Writer) WriteFS(f fs.FS, skipErrs bool) error

WriteFS runs fs.WalkDir on f and writes every file and directory encountered. If skipErrs is false, any error encountered while walking the FS will halt the process and the error will be returned

Example
buf := new(bytes.Buffer)
w := cpio.NewWriter(buf, 0)
dir := os.DirFS("testdata")
if err := w.WriteFS(dir, false); err != nil {
	log.Fatalln("could not write fs:", err)
}

n, err := w.Close()
if err != nil {
	log.Fatalln("could not close file:", err)
}

log.Println("wrote", n, "bytes")
Output:

func (*Writer) WriteFile

func (w *Writer) WriteFile(f *File) error

WriteFile writes f to the Writer

Jump to

Keyboard shortcuts

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