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 ¶
- Constants
- Variables
- func MarshalFileMode(m fs.FileMode) uint64
- func NewFS(r io.Reader) (fs.FS, error)
- func UnmarshalFileMode(m uint64) fs.FileMode
- type DirFile
- type FS
- type File
- func (f *File) Close() error
- func (f *File) Info() (fs.FileInfo, error)
- func (f *File) IsDir() bool
- func (f *File) ModTime() time.Time
- func (f *File) Mode() fs.FileMode
- func (f *File) Name() string
- func (f *File) Read(b []byte) (int, error)
- func (f *File) Size() int64
- func (f *File) Stat() (fs.FileInfo, error)
- func (f *File) Sys() interface{}
- func (f *File) Type() fs.FileMode
- type Reader
- type Writer
Examples ¶
Constants ¶
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 )
const DefaultBlockSize = 512
DefaultBlockSize is the default block size when writing. The default matches GNU cpio
Variables ¶
var HeaderMagic = []byte("070707")
var TrailerPath = []byte("TRAILER!!!")
Functions ¶
func MarshalFileMode ¶
func NewFS ¶
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 ¶
Types ¶
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:
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
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:
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 ¶
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 ¶
Close writes out the trailer file, pads the writer to a multiple of blockSize, and returns the total bytes written
func (*Writer) WriteFS ¶
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: