2019-05-14 18:07:29 +00:00
|
|
|
package dump
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"context"
|
2020-10-24 16:38:30 +00:00
|
|
|
"os"
|
2019-05-21 18:48:45 +00:00
|
|
|
"path/filepath"
|
2019-05-14 18:07:29 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
)
|
|
|
|
|
2021-09-24 21:51:51 +00:00
|
|
|
func (d *Dumper) dumpTar(ctx context.Context, ch <-chan *restic.Node) (err error) {
|
|
|
|
w := tar.NewWriter(d.w)
|
2019-05-14 18:07:29 +00:00
|
|
|
|
2021-09-24 21:51:51 +00:00
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = w.Close()
|
|
|
|
err = errors.Wrap(err, "Close")
|
|
|
|
}
|
|
|
|
}()
|
2019-05-14 18:07:29 +00:00
|
|
|
|
2021-09-24 21:51:51 +00:00
|
|
|
for node := range ch {
|
|
|
|
if err := d.dumpNodeTar(ctx, node, w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-24 13:38:23 +00:00
|
|
|
}
|
2021-09-24 21:51:51 +00:00
|
|
|
return nil
|
2019-05-14 18:07:29 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 16:38:30 +00: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-18 23:04:17 +00:00
|
|
|
cISUID = 0o4000 // Set uid
|
|
|
|
cISGID = 0o2000 // Set gid
|
|
|
|
cISVTX = 0o1000 // Save text (sticky bit)
|
2020-10-24 16:38:30 +00:00
|
|
|
)
|
|
|
|
|
2022-12-30 17:12:12 +00:00
|
|
|
// in a 32-bit build of restic:
|
|
|
|
// substitute a uid or gid of -1 (which was converted to 2^32 - 1) with 0
|
2022-12-28 17:46:58 +00:00
|
|
|
func tarIdentifier(id uint32) int {
|
2022-12-30 17:12:12 +00:00
|
|
|
if int(id) == -1 {
|
2022-12-27 15:36:04 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return int(id)
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:51:51 +00:00
|
|
|
func (d *Dumper) dumpNodeTar(ctx context.Context, node *restic.Node, w *tar.Writer) error {
|
2019-05-21 18:48:45 +00:00
|
|
|
relPath, err := filepath.Rel("/", node.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:07:29 +00:00
|
|
|
header := &tar.Header{
|
2020-08-30 16:15:51 +00:00
|
|
|
Name: filepath.ToSlash(relPath),
|
2019-05-14 18:07:29 +00:00
|
|
|
Size: int64(node.Size),
|
2020-12-18 23:04:17 +00:00
|
|
|
Mode: int64(node.Mode.Perm()), // cIS* constants are added later
|
2022-12-28 17:46:58 +00:00
|
|
|
Uid: tarIdentifier(node.UID),
|
|
|
|
Gid: tarIdentifier(node.GID),
|
2020-10-24 16:39:03 +00:00
|
|
|
Uname: node.User,
|
|
|
|
Gname: node.Group,
|
2019-05-14 18:07:29 +00:00
|
|
|
ModTime: node.ModTime,
|
|
|
|
AccessTime: node.AccessTime,
|
|
|
|
ChangeTime: node.ChangeTime,
|
|
|
|
PAXRecords: parseXattrs(node.ExtendedAttributes),
|
|
|
|
}
|
|
|
|
|
2020-10-24 16:38:30 +00:00
|
|
|
// adapted from archive/tar.FileInfoHeader
|
|
|
|
if node.Mode&os.ModeSetuid != 0 {
|
2020-12-18 23:04:17 +00:00
|
|
|
header.Mode |= cISUID
|
2020-10-24 16:38:30 +00:00
|
|
|
}
|
|
|
|
if node.Mode&os.ModeSetgid != 0 {
|
2020-12-18 23:04:17 +00:00
|
|
|
header.Mode |= cISGID
|
2020-10-24 16:38:30 +00:00
|
|
|
}
|
|
|
|
if node.Mode&os.ModeSticky != 0 {
|
2020-12-18 23:04:17 +00:00
|
|
|
header.Mode |= cISVTX
|
2020-10-24 16:38:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if IsFile(node) {
|
|
|
|
header.Typeflag = tar.TypeReg
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:07:29 +00:00
|
|
|
if IsLink(node) {
|
|
|
|
header.Typeflag = tar.TypeSymlink
|
|
|
|
header.Linkname = node.LinkTarget
|
|
|
|
}
|
|
|
|
|
|
|
|
if IsDir(node) {
|
|
|
|
header.Typeflag = tar.TypeDir
|
2020-10-24 16:38:30 +00:00
|
|
|
header.Name += "/"
|
2019-05-14 18:07:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 21:51:51 +00:00
|
|
|
err = w.WriteHeader(header)
|
2019-05-14 18:07:29 +00:00
|
|
|
if err != nil {
|
2020-12-18 23:04:17 +00:00
|
|
|
return errors.Wrap(err, "TarHeader")
|
2019-05-14 18:07:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 21:51:51 +00:00
|
|
|
return d.writeNode(ctx, w, node)
|
2019-05-14 18:07:29 +00: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
|
|
|
|
}
|