Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPIO improvements #5

Merged
merged 5 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ concurrency:
cancel-in-progress: true

jobs:
cross_build:
name: Cross-platform builds
strategy:
matrix:
go-version: ['1.21.x']
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}

- name: Cross-compile
run: ./cross-compile.sh

build_and_test:
name: Build and test
strategy:
Expand Down
2 changes: 1 addition & 1 deletion cpio/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func ArchiveFromRecords(rs []Record) (*Archive, error) {
// ArchiveFromReader reads records from r into a new Archive in memory.
func ArchiveFromReader(r RecordReader) (*Archive, error) {
a := InMemArchive()
if err := Concat(a, r, nil); err != nil {
if err := Copy(a, r, nil); err != nil {
return nil, err
}
return a, nil
Expand Down
2 changes: 1 addition & 1 deletion cpio/cpio.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type RecordWriter interface {
// today.
type RecordFormat interface {
Reader(r io.ReaderAt) RecordReader
NewFileReader(*os.File) (RecordReader, error)
FileReader(f *os.File) RecordReader
Writer(w io.Writer) RecordWriter
}

Expand Down
2 changes: 2 additions & 0 deletions cpio/internal/upath/safejoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package upath allows safely joining filepaths together without breaking the
// boundary of a "chroot".
package upath

import (
Expand Down
9 changes: 5 additions & 4 deletions cpio/newc.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,11 @@
return EOFReader{&reader{n: n, r: r}}
}

// NewFileReader implements RecordFormat.Reader. If the file
// FileReader implements RecordFormat.Reader. If the file
// implements ReadAt, then it is used for greater efficiency.
// If it only implements Read, then a discarder will be used
// instead.
//
// Note a complication:
//
// r, _, _ := os.Pipe()
Expand All @@ -243,12 +244,12 @@
// current offset? If not, then the file is wrapped with a
// discardreader. The discard reader is far less efficient
// but allows cpio to read from a pipe.
func (n newc) NewFileReader(f *os.File) (RecordReader, error) {
func (n newc) FileReader(f *os.File) RecordReader {
_, err := f.Seek(0, 0)
if err == nil {
return EOFReader{&reader{n: n, r: f}}, nil
return EOFReader{&reader{n: n, r: f}}

Check warning on line 250 in cpio/newc.go

View check run for this annotation

Codecov / codecov/patch

cpio/newc.go#L250

Added line #L250 was not covered by tests
}
return EOFReader{&reader{n: n, r: &discarder{r: f}}}, nil
return EOFReader{&reader{n: n, r: &discarder{r: f}}}
}

func (r *reader) read(p []byte) error {
Expand Down
5 changes: 1 addition & 4 deletions cpio/newc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,10 +463,7 @@ func TestPipeWriteRead(t *testing.T) {

Debug = t.Logf

rdr, err := Newc.NewFileReader(rp)
if err != nil {
t.Fatal(err)
}
rdr := Newc.FileReader(rp)
for _, r := range records {
rec, err := rdr.ReadRecord()
if err != nil {
Expand Down
13 changes: 7 additions & 6 deletions cpio/sysinfo_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import (
)

func sysInfo(n string, sys *syscall.Stat_t) Info {
//nolint:unconvert
return Info{
Ino: sys.Ino,
Mode: uint64(sys.Mode),
UID: uint64(sys.Uid),
GID: uint64(sys.Gid),
NLink: sys.Nlink,
NLink: uint64(sys.Nlink),
MTime: uint64(sys.Mtim.Sec),
FileSize: uint64(sys.Size),
Dev: sys.Dev,
Major: sys.Dev >> 8,
Minor: sys.Dev & 0xff,
Rmajor: sys.Rdev >> 8,
Rminor: sys.Rdev & 0xff,
Dev: uint64(sys.Dev),
Major: uint64(sys.Dev >> 8),
Minor: uint64(sys.Dev & 0xff),
Rmajor: uint64(sys.Rdev >> 8),
Rminor: uint64(sys.Rdev & 0xff),
Name: n,
}
}
14 changes: 7 additions & 7 deletions cpio/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@
return nil
}

// Passthrough copies from a RecordReader to a RecordWriter.
// CopyAndFinish copies from a RecordReader to a RecordWriter.
//
// Passthrough writes a trailer record.
// CopyAndFinish writes a trailer record.
//
// It processes one record at a time to minimize the memory footprint.
func Passthrough(r RecordReader, w RecordWriter) error {
if err := Concat(w, r, nil); err != nil {
func CopyAndFinish(w RecordWriter, r RecordReader) error {
if err := Copy(w, r, nil); err != nil {

Check warning on line 205 in cpio/utils.go

View check run for this annotation

Codecov / codecov/patch

cpio/utils.go#L204-L205

Added lines #L204 - L205 were not covered by tests
return err
}
return WriteTrailer(w)
Expand All @@ -213,11 +213,11 @@
return w.WriteRecord(TrailerRecord)
}

// Concat reads files from r one at a time, and writes them to w.
// Copy reads files from r one at a time, and writes them to w.
//
// Concat does not write a trailer record and applies transform to every record
// Copy does not write a trailer record and applies transform to every record
// before writing it. transform may be nil.
func Concat(w RecordWriter, r RecordReader, transform func(Record) Record) error {
func Copy(w RecordWriter, r RecordReader, transform func(Record) Record) error {
return ForEachRecord(r, func(f Record) error {
if transform != nil {
f = transform(f)
Expand Down
30 changes: 30 additions & 0 deletions cross-compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

set -eu

GO="go"
if [ -v GOROOT ];
then
GO="$GOROOT/bin/go"
fi

function buildem() {
for GOOS in $1
do
for GOARCH in $2
do
echo "Building $GOOS/$GOARCH..."
GOOS=$GOOS GOARCH=$GOARCH $GO build ./...
done
done
}

GOARCHES="386 amd64 arm arm64 ppc64 ppc64le s390x mips mipsle mips64 mips64le"
buildem "linux" "$GOARCHES"

GOARCHES="386 amd64 arm arm64"
GOOSES="freebsd" # TBD netbsd openbsd windows
buildem "$GOOSES" "$GOARCHES"

GOARCHES_DARWIN="arm64 amd64"
buildem "darwin" "$GOARCHES_DARWIN"
Loading