2019-04-03 02:23:23 -04:00
|
|
|
package fsutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-11-17 05:31:07 -05:00
|
|
|
"syscall"
|
2019-04-03 02:23:23 -04:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/tonistiigi/fsutil/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func WriteTar(ctx context.Context, fs FS, w io.Writer) error {
|
|
|
|
tw := tar.NewWriter(w)
|
|
|
|
err := fs.Walk(ctx, func(path string, fi os.FileInfo, err error) error {
|
2020-11-17 05:31:07 -05:00
|
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-03 02:23:23 -04:00
|
|
|
stat, ok := fi.Sys().(*types.Stat)
|
|
|
|
if !ok {
|
2020-11-17 05:31:07 -05:00
|
|
|
return errors.WithStack(&os.PathError{Path: path, Err: syscall.EBADMSG, Op: "fileinfo without stat info"})
|
2019-04-03 02:23:23 -04:00
|
|
|
}
|
|
|
|
hdr, err := tar.FileInfoHeader(fi, stat.Linkname)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
name := filepath.ToSlash(path)
|
|
|
|
if fi.IsDir() && !strings.HasSuffix(name, "/") {
|
|
|
|
name += "/"
|
|
|
|
}
|
|
|
|
hdr.Name = name
|
|
|
|
|
|
|
|
hdr.Uid = int(stat.Uid)
|
|
|
|
hdr.Gid = int(stat.Gid)
|
|
|
|
hdr.Devmajor = stat.Devmajor
|
|
|
|
hdr.Devminor = stat.Devminor
|
|
|
|
hdr.Linkname = stat.Linkname
|
|
|
|
if hdr.Linkname != "" {
|
|
|
|
hdr.Size = 0
|
2020-11-17 05:31:07 -05:00
|
|
|
if fi.Mode()&os.ModeSymlink != 0 {
|
2020-04-16 11:15:03 -04:00
|
|
|
hdr.Typeflag = tar.TypeSymlink
|
|
|
|
} else {
|
|
|
|
hdr.Typeflag = tar.TypeLink
|
|
|
|
}
|
2019-04-03 02:23:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(stat.Xattrs) > 0 {
|
|
|
|
hdr.PAXRecords = map[string]string{}
|
|
|
|
}
|
|
|
|
for k, v := range stat.Xattrs {
|
|
|
|
hdr.PAXRecords["SCHILY.xattr."+k] = string(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tw.WriteHeader(hdr); err != nil {
|
2020-11-17 05:31:07 -05:00
|
|
|
return errors.Wrapf(err, "failed to write file header %s", name)
|
2019-04-03 02:23:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if hdr.Typeflag == tar.TypeReg && hdr.Size > 0 && hdr.Linkname == "" {
|
|
|
|
rc, err := fs.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(tw, rc); err != nil {
|
2020-11-17 05:31:07 -05:00
|
|
|
return errors.WithStack(err)
|
2019-04-03 02:23:23 -04:00
|
|
|
}
|
|
|
|
if err := rc.Close(); err != nil {
|
2020-11-17 05:31:07 -05:00
|
|
|
return errors.WithStack(err)
|
2019-04-03 02:23:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tw.Close()
|
|
|
|
}
|