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

Fix write of Symlink files #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func FileInfoHeader(fi os.FileInfo, link string) (*Header, error) {
case fm&os.ModeSymlink != 0:
h.Mode |= ModeSymlink
h.Linkname = link
h.Size = 0 // adjusted to len(Linkname) while writing header
case fm&os.ModeDevice != 0:
if fm&os.ModeCharDevice != 0 {
h.Mode |= ModeCharDevice
Expand Down
8 changes: 8 additions & 0 deletions svr4.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ func writeSVR4Header(w io.Writer, hdr *Header) (pad int64, err error) {
if !hdr.ModTime.IsZero() {
writeHex(hdrBuf[46:54], hdr.ModTime.Unix())
}
if hdr.Mode&^ModePerm == ModeSymlink {
hdr.Size = int64(len(hdr.Linkname))
}
writeHex(hdrBuf[54:62], hdr.Size)
writeHex(hdrBuf[94:102], int64(len(hdr.Name)+1))
if hdr.Checksum != 0 {
Expand All @@ -146,6 +149,11 @@ func writeSVR4Header(w io.Writer, hdr *Header) (pad int64, err error) {
return
}

_, err = io.WriteString(w, hdr.Linkname)
if err != nil {
return
}

// compute padding to end of file
pad = (4 - (hdr.Size % 4)) % 4
return
Expand Down
46 changes: 39 additions & 7 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,29 @@ import (
)

func store(w *cpio.Writer, fn string) error {
f, err := os.Open(fn)
fi, err := os.Lstat(fn)
if err != nil {
return err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return err
var link string
if fi.Mode()&os.ModeSymlink != 0 {
if link, err = os.Readlink(fn); err != nil {
return err
}
}
hdr, err := cpio.FileInfoHeader(fi, "")
hdr, err := cpio.FileInfoHeader(fi, link)
if err != nil {
return err
}
if err := w.WriteHeader(hdr); err != nil {
return err
}
if !fi.IsDir() {
if fi.Mode().IsRegular() {
f, err := os.Open(fn)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(w, f); err != nil {
return err
}
Expand All @@ -47,3 +53,29 @@ func TestWriter(t *testing.T) {
t.Fatalf("Close: %v", err)
}
}

func TestWriter_Symlink(t *testing.T) {
var buf bytes.Buffer
w := cpio.NewWriter(&buf)
func() {
defer func() {
if err := w.Close(); err != nil {
t.Fatalf("Close: %v", err)
}
}()
if err := store(w, "testdata/checklist.txt"); err != nil {
t.Fatalf("store: %v", err)
}
}()
r := cpio.NewReader(&buf)
hdr, err := r.Next()
if err != nil {
t.Fatalf("Next: %v", err)
}
if hdr.Mode&^cpio.ModePerm != cpio.ModeSymlink {
t.Fatalf("file has mode %s, expected %s (ModeSymlink)", hdr.Mode, cpio.FileMode(cpio.ModeSymlink))
}
if hdr.Linkname == "" {
t.Fatal("Empty Linkname on ModeSymlink file")
}
}