2019-05-14 20:07:29 +02:00
|
|
|
package dump
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"context"
|
|
|
|
"io"
|
2020-10-24 18:38:30 +02:00
|
|
|
"os"
|
2019-05-21 20:48:45 +02:00
|
|
|
"path/filepath"
|
2019-05-14 20:07:29 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
)
|
|
|
|
|
2020-11-09 23:22:27 +01:00
|
|
|
type tarDumper struct {
|
|
|
|
w *tar.Writer
|
2019-05-14 20:07:29 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 23:22:27 +01:00
|
|
|
// Statically ensure that tarDumper implements dumper.
|
|
|
|
var _ dumper = tarDumper{}
|
2019-05-14 20:07:29 +02:00
|
|
|
|
2020-11-09 23:22:27 +01:00
|
|
|
// WriteTar will write the contents of the given tree, encoded as a tar to the given destination.
|
|
|
|
func WriteTar(ctx context.Context, repo restic.Repository, tree *restic.Tree, rootPath string, dst io.Writer) error {
|
|
|
|
dmp := tarDumper{w: tar.NewWriter(dst)}
|
2019-05-14 20:07:29 +02:00
|
|
|
|
2020-11-09 23:22:27 +01:00
|
|
|
err := writeDump(ctx, repo, tree, rootPath, dmp, dst)
|
|
|
|
if err != nil {
|
|
|
|
dmp.w.Close()
|
2020-12-19 00:04:17 +01:00
|
|
|
|
2020-11-09 23:22:27 +01:00
|
|
|
return err
|
2019-05-14 20:07:29 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 23:22:27 +01:00
|
|
|
return dmp.w.Close()
|
2019-05-14 20:07:29 +02:00
|
|
|
}
|
|
|
|
|
2020-10-24 18:38:30 +02:00
|
|
|
// copied from archive/tar.FileInfoHeader
|
|
|
|
const (
|
|
|
|
// Mode constants from the USTAR spec:
|
|
|
|
// See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html#tag_20_92_13_06
|
2020-12-19 00:04:17 +01:00
|
|
|
cISUID = 0o4000 // Set uid
|
|
|
|
cISGID = 0o2000 // Set gid
|
|
|
|
cISVTX = 0o1000 // Save text (sticky bit)
|
2020-10-24 18:38:30 +02:00
|
|
|
)
|
|
|
|
|
2020-11-09 23:22:27 +01:00
|
|
|
func (dmp tarDumper) dumpNode(ctx context.Context, node *restic.Node, repo restic.Repository) error {
|
2019-05-21 20:48:45 +02:00
|
|
|
relPath, err := filepath.Rel("/", node.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-14 20:07:29 +02:00
|
|
|
header := &tar.Header{
|
2020-08-30 18:15:51 +02:00
|
|
|
Name: filepath.ToSlash(relPath),
|
2019-05-14 20:07:29 +02:00
|
|
|
Size: int64(node.Size),
|
2020-12-19 00:04:17 +01:00
|
|
|
Mode: int64(node.Mode.Perm()), // cIS* constants are added later
|
2019-05-14 20:07:29 +02:00
|
|
|
Uid: int(node.UID),
|
|
|
|
Gid: int(node.GID),
|
2020-10-24 18:39:03 +02:00
|
|
|
Uname: node.User,
|
|
|
|
Gname: node.Group,
|
2019-05-14 20:07:29 +02:00
|
|
|
ModTime: node.ModTime,
|
|
|
|
AccessTime: node.AccessTime,
|
|
|
|
ChangeTime: node.ChangeTime,
|
|
|
|
PAXRecords: parseXattrs(node.ExtendedAttributes),
|
|
|
|
}
|
|
|
|
|
2020-10-24 18:38:30 +02:00
|
|
|
// adapted from archive/tar.FileInfoHeader
|
|
|
|
if node.Mode&os.ModeSetuid != 0 {
|
2020-12-19 00:04:17 +01:00
|
|
|
header.Mode |= cISUID
|
2020-10-24 18:38:30 +02:00
|
|
|
}
|
|
|
|
if node.Mode&os.ModeSetgid != 0 {
|
2020-12-19 00:04:17 +01:00
|
|
|
header.Mode |= cISGID
|
2020-10-24 18:38:30 +02:00
|
|
|
}
|
|
|
|
if node.Mode&os.ModeSticky != 0 {
|
2020-12-19 00:04:17 +01:00
|
|
|
header.Mode |= cISVTX
|
2020-10-24 18:38:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if IsFile(node) {
|
|
|
|
header.Typeflag = tar.TypeReg
|
|
|
|
}
|
|
|
|
|
2019-05-14 20:07:29 +02:00
|
|
|
if IsLink(node) {
|
|
|
|
header.Typeflag = tar.TypeSymlink
|
|
|
|
header.Linkname = node.LinkTarget
|
|
|
|
}
|
|
|
|
|
|
|
|
if IsDir(node) {
|
|
|
|
header.Typeflag = tar.TypeDir
|
2020-10-24 18:38:30 +02:00
|
|
|
header.Name += "/"
|
2019-05-14 20:07:29 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 23:22:27 +01:00
|
|
|
err = dmp.w.WriteHeader(header)
|
2019-05-14 20:07:29 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-12-19 00:04:17 +01:00
|
|
|
return errors.Wrap(err, "TarHeader")
|
2019-05-14 20:07:29 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 23:22:27 +01:00
|
|
|
return GetNodeData(ctx, dmp.w, repo, node)
|
2019-05-14 20:07:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseXattrs(xattrs []restic.ExtendedAttribute) map[string]string {
|
|
|
|
tmpMap := make(map[string]string)
|
|
|
|
|
|
|
|
for _, attr := range xattrs {
|
|
|
|
attrString := string(attr.Value)
|
|
|
|
|
|
|
|
if strings.HasPrefix(attr.Name, "system.posix_acl_") {
|
|
|
|
na := acl{}
|
|
|
|
na.decode(attr.Value)
|
|
|
|
|
|
|
|
if na.String() != "" {
|
|
|
|
if strings.Contains(attr.Name, "system.posix_acl_access") {
|
|
|
|
tmpMap["SCHILY.acl.access"] = na.String()
|
|
|
|
} else if strings.Contains(attr.Name, "system.posix_acl_default") {
|
|
|
|
tmpMap["SCHILY.acl.default"] = na.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tmpMap["SCHILY.xattr."+attr.Name] = attrString
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmpMap
|
|
|
|
}
|