1
Fork 0

add WithFilePrefix

This adds an option to prefix every file path with a given string.
main
Motiejus Jakštys 2021-08-29 16:55:32 +03:00 committed by Motiejus Jakštys
parent 71aab65b75
commit 436a866f5d
6 changed files with 95 additions and 26 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/undocker
/undocker-* /undocker-*
coverage.html coverage.html
sha256sum.txt* sha256sum.txt*

54
main.go
View File

@ -2,6 +2,7 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -15,33 +16,56 @@ var Version = "unknown"
var VersionHash = "unknown" var VersionHash = "unknown"
const _usage = `Usage: const _usage = `Usage:
%s <infile> <outfile> %s [OPTION]... <infile> <outfile>
Flatten a Docker container image to a root file system. Flatten a Docker container image to a root file system.
Arguments: Arguments:
<infile>: Input Docker container. Tarball. <infile> Input Docker container. Tarball.
<outfile>: Output tarball, the root file system. '-' is stdout. <outfile> Output tarball, the root file system. '-' is stdout.
Options:
--prefix=<prefix> prefix all destination files with a given string.
undocker %s (%s) undocker %s (%s)
Built with %s Built with %s
` `
func usage(pre string, out io.Writer) {
fmt.Fprintf(out, pre+_usage,
filepath.Base(os.Args[0]),
Version,
VersionHash,
runtime.Version(),
)
}
func usageErr(pre string) {
usage(pre, os.Stderr)
os.Exit(2)
}
func main() { func main() {
runtime.GOMAXPROCS(1) // no need to create that many threads runtime.GOMAXPROCS(1) // no need to create that many threads
if len(os.Args) != 3 { var filePrefix string
fmt.Fprintf(os.Stderr, _usage, fs := flag.NewFlagSet("undocker", flag.ExitOnError)
filepath.Base(os.Args[0]), fs.Usage = func() { usageErr("") }
Version, fs.StringVar(&filePrefix, "prefix", "", "prefix files in the tarball")
VersionHash,
runtime.Version(), if len(os.Args) == 1 {
) usageErr("")
os.Exit(1) }
_ = fs.Parse(os.Args[1:]) // ExitOnError captures it
args := fs.Args()
if len(args) != 2 {
usageErr("invalid number of arguments\n")
} }
c := &command{flattener: rootfs.Flatten, Stdout: os.Stdout} c := &command{flattener: rootfs.Flatten, Stdout: os.Stdout}
if err := c.execute(os.Args[1], os.Args[2]); err != nil { if err := c.execute(args[0], args[1], filePrefix); err != nil {
fmt.Printf("Error: %v\n", err) fmt.Printf("Error: %v\n", err)
os.Exit(1) os.Exit(1)
} }
@ -49,11 +73,11 @@ func main() {
} }
type command struct { type command struct {
flattener func(io.ReadSeeker, io.Writer) error flattener func(io.ReadSeeker, io.Writer, ...rootfs.Option) error
Stdout io.Writer Stdout io.Writer
} }
func (c *command) execute(infile string, outfile string) (_err error) { func (c *command) execute(infile, outfile, filePrefix string) (_err error) {
rd, err := os.Open(infile) rd, err := os.Open(infile)
if err != nil { if err != nil {
return err return err
@ -84,5 +108,5 @@ func (c *command) execute(infile string, outfile string) (_err error) {
out = outf out = outf
} }
return c.flattener(rd, out) return c.flattener(rd, out, rootfs.WithFilePrefix(filePrefix))
} }

View File

@ -9,6 +9,8 @@ import (
"path/filepath" "path/filepath"
"regexp" "regexp"
"testing" "testing"
"git.sr.ht/~motiejus/undocker/rootfs"
) )
func TestExecute(t *testing.T) { func TestExecute(t *testing.T) {
@ -17,7 +19,7 @@ func TestExecute(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
fixture func(*testing.T, string) fixture func(*testing.T, string)
flattener func(io.ReadSeeker, io.Writer) error flattener func(io.ReadSeeker, io.Writer, ...rootfs.Option) error
infile string infile string
outfile string outfile string
wantErr string wantErr string
@ -99,7 +101,7 @@ func TestExecute(t *testing.T) {
inf := filepath.Join(dir, tt.infile) inf := filepath.Join(dir, tt.infile)
c := &command{Stdout: &stdout, flattener: tt.flattener} c := &command{Stdout: &stdout, flattener: tt.flattener}
err := c.execute(inf, tt.outfile) err := c.execute(inf, tt.outfile, "")
if tt.assertion != nil { if tt.assertion != nil {
tt.assertion(t, dir) tt.assertion(t, dir)
@ -135,11 +137,11 @@ func TestExecute(t *testing.T) {
} }
} }
func flattenPassthrough(r io.ReadSeeker, w io.Writer) error { func flattenPassthrough(r io.ReadSeeker, w io.Writer, _ ...rootfs.Option) error {
_, err := io.Copy(w, r) _, err := io.Copy(w, r)
return err return err
} }
func flattenBad(_ io.ReadSeeker, _ io.Writer) error { func flattenBad(_ io.ReadSeeker, _ io.Writer, _ ...rootfs.Option) error {
return errors.New("some error") return errors.New("some error")
} }

20
rootfs/options.go Normal file
View File

@ -0,0 +1,20 @@
package rootfs
type options struct {
filePrefix string
}
type Option interface {
apply(*options)
}
type filePrefixOption string
func (p filePrefixOption) apply(opts *options) {
opts.filePrefix = string(p)
}
// WithFilePrefixOption adds a prefix to all files in the output archive.
func WithFilePrefix(p string) Option {
return filePrefixOption(p)
}

View File

@ -35,7 +35,14 @@ type (
// Flatten flattens a docker image to a tarball. The underlying io.Writer // Flatten flattens a docker image to a tarball. The underlying io.Writer
// should be an open file handle, which the caller is responsible for closing // should be an open file handle, which the caller is responsible for closing
// themselves // themselves
func Flatten(rd io.ReadSeeker, w io.Writer) (_err error) { func Flatten(rd io.ReadSeeker, w io.Writer, opts ...Option) (_err error) {
options := options{
filePrefix: "",
}
for _, o := range opts {
o.apply(&options)
}
tr := tar.NewReader(rd) tr := tar.NewReader(rd)
var closer func() error var closer func() error
var err error var err error
@ -177,7 +184,8 @@ func Flatten(rd io.ReadSeeker, w io.Writer) (_err error) {
if hdr.Typeflag != tar.TypeDir && file2layer[hdr.Name] != i { if hdr.Typeflag != tar.TypeDir && file2layer[hdr.Name] != i {
continue continue
} }
if err := writeFile(tr, tw, hdr); err != nil { prefix := options.filePrefix
if err := writeFile(tr, tw, hdr, prefix); err != nil {
return err return err
} }
} }
@ -188,10 +196,10 @@ func Flatten(rd io.ReadSeeker, w io.Writer) (_err error) {
return nil return nil
} }
func writeFile(tr *tar.Reader, tw *tar.Writer, hdr *tar.Header) error { func writeFile(tr *tar.Reader, tw *tar.Writer, hdr *tar.Header, prefix string) error {
hdrOut := &tar.Header{ hdrOut := &tar.Header{
Typeflag: hdr.Typeflag, Typeflag: hdr.Typeflag,
Name: hdr.Name, Name: prefix + hdr.Name,
Linkname: hdr.Linkname, Linkname: hdr.Linkname,
Size: hdr.Size, Size: hdr.Size,
Mode: int64(hdr.Mode & 0777), Mode: int64(hdr.Mode & 0777),

View File

@ -34,6 +34,7 @@ func TestRootFS(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
opts []Option
image tarball image tarball
want []extractable want []extractable
wantErr string wantErr string
@ -54,7 +55,7 @@ func TestRootFS(t *testing.T) {
wantErr: "layer0/layer.tar defined in manifest, missing in tarball", wantErr: "layer0/layer.tar defined in manifest, missing in tarball",
}, },
{ {
name: "basic file overwrite, layer order mixed", name: "ok: basic file overwrite, layer order mixed",
image: tarball{ image: tarball{
file{Name: "layer1/layer.tar", Contents: layer1.Buffer()}, file{Name: "layer1/layer.tar", Contents: layer1.Buffer()},
file{Name: "layer0/layer.tar", Contents: layer0.Buffer()}, file{Name: "layer0/layer.tar", Contents: layer0.Buffer()},
@ -172,7 +173,7 @@ func TestRootFS(t *testing.T) {
}, },
}, },
{ {
name: "archived layer", name: "compressed layer",
image: tarball{ image: tarball{
file{Name: "layer1/layer.tar", Contents: layer1.Gzip()}, file{Name: "layer1/layer.tar", Contents: layer1.Gzip()},
file{Name: "layer0/layer.tar", Contents: layer0.Gzip()}, file{Name: "layer0/layer.tar", Contents: layer0.Gzip()},
@ -183,6 +184,19 @@ func TestRootFS(t *testing.T) {
file{Name: "/file", UID: 1, Contents: bytes.NewBufferString("from 1")}, file{Name: "/file", UID: 1, Contents: bytes.NewBufferString("from 1")},
}, },
}, },
{
name: "ok: add a file prefix",
opts: []Option{WithFilePrefix("/opt")},
image: tarball{
file{Name: "layer1/layer.tar", Contents: layer1.Buffer()},
file{Name: "layer0/layer.tar", Contents: layer0.Buffer()},
manifest{"layer0/layer.tar", "layer1/layer.tar"},
},
want: []extractable{
dir{Name: "/opt/", UID: 0},
file{Name: "/opt/file", UID: 1, Contents: bytes.NewBufferString("from 1")},
},
},
} }
for _, tt := range tests { for _, tt := range tests {
@ -190,7 +204,7 @@ func TestRootFS(t *testing.T) {
in := bytes.NewReader(tt.image.Buffer().Bytes()) in := bytes.NewReader(tt.image.Buffer().Bytes())
out := bytes.Buffer{} out := bytes.Buffer{}
err := Flatten(in, &out) err := Flatten(in, &out, tt.opts...)
if tt.wantErr != "" { if tt.wantErr != "" {
if err == nil { if err == nil {
t.Fatal("expected error, got nil") t.Fatal("expected error, got nil")