2019-05-14 18:07:29 +00:00
|
|
|
package dump
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
2024-09-13 16:21:07 +00:00
|
|
|
"compress/gzip"
|
2019-05-14 18:07:29 +00:00
|
|
|
"context"
|
2023-04-27 09:45:41 +00:00
|
|
|
"fmt"
|
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
|
|
|
|
2023-05-28 09:35:55 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
2019-05-14 18:07:29 +00:00
|
|
|
"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) {
|
2024-09-13 16:21:07 +00:00
|
|
|
outer := d.w
|
|
|
|
|
|
|
|
if d.compress {
|
|
|
|
outer = gzip.NewWriter(outer)
|
|
|
|
}
|
|
|
|
w := tar.NewWriter(outer)
|
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")
|
2024-09-13 16:21:07 +00:00
|
|
|
|
|
|
|
if gz, ok := outer.(*gzip.Writer); ok {
|
|
|
|
err = gz.Close()
|
|
|
|
err = errors.Wrap(err, "Close")
|
|
|
|
}
|
2021-09-24 21:51:51 +00:00
|
|
|
}
|
|
|
|
}()
|
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
|
|
|
}
|
|
|
|
|
2024-07-09 17:51:44 +00:00
|
|
|
if node.Type == restic.NodeTypeFile {
|
2020-10-24 16:38:30 +00:00
|
|
|
header.Typeflag = tar.TypeReg
|
|
|
|
}
|
|
|
|
|
2024-07-09 17:51:44 +00:00
|
|
|
if node.Type == restic.NodeTypeSymlink {
|
2019-05-14 18:07:29 +00:00
|
|
|
header.Typeflag = tar.TypeSymlink
|
|
|
|
header.Linkname = node.LinkTarget
|
|
|
|
}
|
|
|
|
|
2024-07-09 17:51:44 +00:00
|
|
|
if node.Type == restic.NodeTypeDir {
|
2019-05-14 18:07:29 +00:00
|
|
|
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 {
|
2023-04-27 09:45:41 +00:00
|
|
|
return fmt.Errorf("writing header for %q: %w", node.Path, err)
|
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 {
|
2023-05-28 09:35:55 +00:00
|
|
|
// Check for Linux POSIX.1e ACLs.
|
|
|
|
//
|
|
|
|
// TODO support ACLs from other operating systems.
|
|
|
|
// FreeBSD ACLs have names "posix1e.acl_(access|default)",
|
|
|
|
// but their binary format may not match the Linux format.
|
|
|
|
aclKey := ""
|
|
|
|
switch attr.Name {
|
|
|
|
case "system.posix_acl_access":
|
|
|
|
aclKey = "SCHILY.acl.access"
|
|
|
|
case "system.posix_acl_default":
|
|
|
|
aclKey = "SCHILY.acl.default"
|
|
|
|
}
|
|
|
|
|
|
|
|
if aclKey != "" {
|
|
|
|
text, err := formatLinuxACL(attr.Value)
|
|
|
|
if err != nil {
|
|
|
|
debug.Log("parsing Linux ACL: %v, skipping", err)
|
|
|
|
continue
|
2019-05-14 18:07:29 +00:00
|
|
|
}
|
2023-05-28 09:35:55 +00:00
|
|
|
tmpMap[aclKey] = text
|
2019-05-14 18:07:29 +00:00
|
|
|
} else {
|
2023-05-28 09:35:55 +00:00
|
|
|
tmpMap["SCHILY.xattr."+attr.Name] = string(attr.Value)
|
2019-05-14 18:07:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmpMap
|
|
|
|
}
|