Skip to content

Commit

Permalink
Modernised package
Browse files Browse the repository at this point in the history
  • Loading branch information
cavaliercoder committed Oct 28, 2021
1 parent 925f952 commit b4d3577
Show file tree
Hide file tree
Showing 17 changed files with 131 additions and 261 deletions.
3 changes: 0 additions & 3 deletions .gitignore

This file was deleted.

14 changes: 7 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
language: go

go:
- 1.4.3
- 1.5.4
- 1.6.4
- 1.7.6
- 1.8.3

script: make check
- 1.17
- 1.16
- 1.15
- 1.14
- 1.13
- 1.12
- 1.11
18 changes: 0 additions & 18 deletions Makefile

This file was deleted.

61 changes: 4 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,62 +1,9 @@
# go-cpio [![GoDoc](https://godoc.org/github.com/cavaliercoder/go-cpio?status.svg)](https://godoc.org/github.com/cavaliercoder/go-cpio) [![Build Status](https://travis-ci.org/cavaliercoder/go-cpio.svg?branch=master)](https://travis-ci.org/cavaliercoder/go-cpio) [![Go Report Card](https://goreportcard.com/badge/github.com/cavaliercoder/go-cpio)](https://goreportcard.com/report/github.com/cavaliercoder/go-cpio)
# cpio
[![Go Reference](https://pkg.go.dev/badge/github.com/cavaliergopher/cpio.svg)](https://pkg.go.dev/github.com/cavaliergopher/cpio) [![Build Status](https://app.travis-ci.com/cavaliergopher/cpio.svg?branch=main)](https://app.travis-ci.com/cavaliergopher/cpio) [![Go Report Card](https://goreportcard.com/badge/github.com/cavaliergopher/cpio)](https://goreportcard.com/report/github.com/cavaliergopher/cpio)

This package provides a Go native implementation of the CPIO archive file
format.
Package cpio provides readers and writers for the CPIO archive file format.

Currently, only the SVR4 (New ASCII) format is supported, both with and without
checksums.

```go
// 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()
}
```
Copyright 2021, Ryan Armstrong <[email protected]>
8 changes: 0 additions & 8 deletions cpio.go

This file was deleted.

9 changes: 9 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Package cpio providers readers and writers for CPIO archives. Currently, only
the SVR4 (New ASCII) format is supported, both with and without checksums.
This package aims to be feel like Go's archive/tar package.
See the [CPIO man page](https://www.freebsd.org/cgi/man.cgi?query=cpio&sektion=5).
*/
package cpio
6 changes: 3 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"log"
"os"

"github.com/cavaliercoder/go-cpio"
"github.com/cavaliergopher/cpio"
)

func Example() {
Expand Down Expand Up @@ -38,14 +38,14 @@ func Example() {
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)
r := cpio.NewReader(buf)

// Iterate through the files in the archive.
for {
Expand Down
37 changes: 10 additions & 27 deletions fileinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,53 @@ import (
"time"
)

// headerFileInfo implements os.FileInfo.
type headerFileInfo struct {
// fileInfo implements fs.FileInfo.
type fileInfo struct {
h *Header
}

// Name returns the base name of the file.
func (fi headerFileInfo) Name() string {
func (fi fileInfo) Name() string {
if fi.IsDir() {
return path.Base(path.Clean(fi.h.Name))
}
return path.Base(fi.h.Name)
}

func (fi headerFileInfo) Size() int64 { return fi.h.Size }
func (fi headerFileInfo) IsDir() bool { return fi.Mode().IsDir() }
func (fi headerFileInfo) ModTime() time.Time { return fi.h.ModTime }
func (fi headerFileInfo) Sys() interface{} { return fi.h }
func (fi fileInfo) Size() int64 { return fi.h.Size }
func (fi fileInfo) IsDir() bool { return fi.Mode().IsDir() }
func (fi fileInfo) ModTime() time.Time { return fi.h.ModTime }
func (fi fileInfo) Sys() interface{} { return fi.h }

func (fi headerFileInfo) Mode() (mode os.FileMode) {
// Set file permission bits.
func (fi fileInfo) Mode() (mode os.FileMode) {
mode = os.FileMode(fi.h.Mode).Perm()

// Set setuid, setgid and sticky bits.
if fi.h.Mode&ModeSetuid != 0 {
// setuid
mode |= os.ModeSetuid
}
if fi.h.Mode&ModeSetgid != 0 {
// setgid
mode |= os.ModeSetgid
}
if fi.h.Mode&ModeSticky != 0 {
// sticky
mode |= os.ModeSticky
}

// Set file mode bits.
// clear perm, setuid, setgid and sticky bits.
m := os.FileMode(fi.h.Mode) & 0170000
m := os.FileMode(fi.h.Mode) & ModeType
if m == ModeDir {
// directory
mode |= os.ModeDir
}
if m == ModeNamedPipe {
// named pipe (FIFO)
mode |= os.ModeNamedPipe
}
if m == ModeSymlink {
// symbolic link
mode |= os.ModeSymlink
}
if m == ModeDevice {
// device file
mode |= os.ModeDevice
}
if m == ModeCharDevice {
// Unix character device
mode |= os.ModeDevice
mode |= os.ModeCharDevice
mode |= os.ModeDevice | os.ModeCharDevice
}
if m == ModeSocket {
// Unix domain socket
mode |= os.ModeSocket
}

return mode
}
35 changes: 0 additions & 35 deletions fuzz.go

This file was deleted.

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/cavaliergopher/cpio

go 1.17
2 changes: 1 addition & 1 deletion hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type digest struct {
sum uint32
}

// NewHash returns a new hash.Hash32 computing the SVR4 checksum.
// NewHash returns a new hash.Hash32 for computing SVR4 checksums.
func NewHash() hash.Hash32 {
return &digest{}
}
Expand Down
Loading

0 comments on commit b4d3577

Please sign in to comment.