Documentation ¶
Overview ¶
Package cpio implements access to CPIO archives. Currently, only the SVR4 (New ASCII) format is supported, both with and without checksums.
References:
https://www.freebsd.org/cgi/man.cgi?query=cpio&sektion=5
Example ¶
// Create a buffer to write our archive to. buf := new(bytes.Buffer) // Create a new cpio archive. w := cpio.NewWriter(buf) // Add some files to the archive. var files = []struct { Name, Body string }{ {"readme.txt", "This archive contains some text files."}, {"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"}, {"todo.txt", "Get animal handling license."}, } for _, file := range files { hdr := &cpio.Header{ Name: file.Name, Mode: 0600, Size: int64(len(file.Body)), } if err := w.WriteHeader(hdr); err != nil { log.Fatalln(err) } if _, err := w.Write([]byte(file.Body)); err != nil { log.Fatalln(err) } } // Make sure to check the error on Close. if err := w.Close(); err != nil { log.Fatalln(err) } // Open the cpio archive for reading. b := bytes.NewReader(buf.Bytes()) r := cpio.NewReader(b) // Iterate through the files in the archive. for { hdr, err := r.Next() if err == io.EOF { // end of cpio archive break } if err != nil { log.Fatalln(err) } fmt.Printf("Contents of %s:\n", hdr.Name) if _, err := io.Copy(os.Stdout, r); err != nil { log.Fatalln(err) } fmt.Println() }
Output: Contents of readme.txt: This archive contains some text files. Contents of gopher.txt: Gopher names: George Geoffrey Gonzo Contents of todo.txt: Get animal handling license.
Index ¶
Examples ¶
Constants ¶
const ( ModeSetuid = 04000 // Set uid ModeSetgid = 02000 // Set gid ModeSticky = 01000 // Save text (sticky bit) ModeDir = 040000 // Directory ModeNamedPipe = 010000 // FIFO ModeRegular = 0100000 // Regular file ModeSymlink = 0120000 // Symbolic link ModeDevice = 060000 // Block special file ModeCharDevice = 020000 // Character special file ModeSocket = 0140000 // Socket ModeType = 0170000 // Mask for the type bits ModePerm = 0777 // Unix permission bits )
Mode constants from the cpio spec.
Variables ¶
var ( ErrWriteTooLong = errors.New("cpio: write too long") ErrWriteAfterClose = errors.New("cpio: write after close") )
var (
ErrHeader = errors.New("cpio: invalid cpio header")
)
Functions ¶
func NewHash ¶
NewHash returns a new hash.Hash32 computing the SVR4 checksum.
Example ¶
// Open the cpio archive for reading. f, err := os.Open("testdata/test_svr4_crc.cpio") if err != nil { log.Fatal(err) } defer f.Close() r := NewReader(f) // create a Hash h := NewHash() // Iterate through the files in the archive. for { hdr, err := r.Next() if err == io.EOF { // end of cpio archive return } if err != nil { log.Fatal(err) } // skip symlinks, directories, etc. if !hdr.Mode.IsRegular() { continue } // read file into hash h.Reset() _, err = io.CopyN(h, r, hdr.Size) if err != nil { log.Fatal(err) } // check hash matches header checksum sum := Checksum(h.Sum32()) if sum == hdr.Checksum { fmt.Printf("Checksum OK: %v (%v)\n", hdr.Name, hdr.Checksum) } else { fmt.Printf("Checksum FAIL: %v - expected %v, got %v\n", hdr.Name, hdr.Checksum, sum) } }
Output: Checksum OK: gophers.txt (00000C98) Checksum OK: readme.txt (00000E3D) Checksum OK: todo.txt (00000A52)
Types ¶
type Checksum ¶
type Checksum uint32
Checksum is the sum of all bytes in the file data. This sum is computed treating all bytes as unsigned values and using unsigned arithmetic. Only the least-significant 32 bits of the sum are stored. Use NewHash to compute the actual checksum of an archived file.
type FileMode ¶
type FileMode int64
A FileMode represents a file's mode and permission bits.
func (FileMode) IsDir ¶
IsDir reports whether m describes a directory. That is, it tests for the ModeDir bit being set in m.
func (FileMode) IsRegular ¶
IsRegular reports whether m describes a regular file. That is, it tests for the ModeRegular bit being set in m.
type Header ¶
type Header struct { DeviceID int Inode int64 // inode number Mode FileMode // permission and mode bits UID int // user id of the owner GID int // group id of the owner Links int // number of inbound links ModTime time.Time // modified time Size int64 // size in bytes Name string // filename Linkname string // target name of link Checksum Checksum // computed checksum // contains filtered or unexported fields }
A Header represents a single header in a CPIO archive.
func FileInfoHeader ¶
FileInfoHeader creates a partially-populated Header from fi. If fi describes a symlink, FileInfoHeader records link as the link target. If fi describes a directory, a slash is appended to the name. Because os.FileInfo's Name method returns only the base name of the file it describes, it may be necessary to modify the Name field of the returned header to provide the full path name of the file.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
A Reader provides sequential access to the contents of a CPIO archive. A CPIO archive consists of a sequence of files. The Next method advances to the next file in the archive (including the first), and then it can be treated as an io.Reader to access the file's data.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
A Writer provides sequential writing of a CPIO archive. A CPIO archive consists of a sequence of files. Call WriteHeader to begin a new file, and then call Write to supply that file's data, writing at most hdr.Size bytes in total.
func (*Writer) Close ¶
Close closes the CPIO archive, flushing any unwritten data to the underlying writer.
func (*Writer) Write ¶
Write writes to the current entry in the CPIO archive. Write returns the error ErrWriteTooLong if more than hdr.Size bytes are written after WriteHeader.
func (*Writer) WriteHeader ¶
WriteHeader writes hdr and prepares to accept the file's contents. WriteHeader calls Flush if it is not the first header. Calling after a Close will return ErrWriteAfterClose.