Skip to content

Commit

Permalink
Merge pull request #11 from stapelberg/dir
Browse files Browse the repository at this point in the history
FileInfoHeader: set Size = 0 for directories
  • Loading branch information
cavaliercoder authored Jun 26, 2018
2 parents 9caab6f + 9f461de commit 7b5e8d7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func FileInfoHeader(fi os.FileInfo, link string) (*Header, error) {
case fi.IsDir():
h.Mode |= ModeDir
h.Name += "/"
h.Size = 0
case fm&os.ModeSymlink != 0:
h.Mode |= ModeSymlink
h.Linkname = link
Expand Down
2 changes: 2 additions & 0 deletions testdata/etc/hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
127.0.0.1 localhost
::1 localhost
53 changes: 53 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cpio_test

import (
"bytes"
"io"
"os"
"testing"

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

func store(w *cpio.Writer, fn string) error {
f, err := os.Open(fn)
if err != nil {
return err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return err
}
hdr, err := cpio.FileInfoHeader(fi, "")
if err != nil {
return err
}
if err := w.WriteHeader(hdr); err != nil {
return err
}
if !fi.IsDir() {
if _, err := io.Copy(w, f); err != nil {
return err
}
}

return err
}

func TestWriter(t *testing.T) {
var buf bytes.Buffer
w := cpio.NewWriter(&buf)

if err := store(w, "testdata/etc"); err != nil {
t.Fatalf("store: %v", err)
}

if err := store(w, "testdata/etc/hosts"); err != nil {
t.Fatalf("store: %v", err)
}

if err := w.Close(); err != nil {
t.Fatalf("Close: %v", err)
}
}

0 comments on commit 7b5e8d7

Please sign in to comment.