2019-06-01 16:09:38 +00:00
|
|
|
package dump
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
2023-04-27 09:45:41 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2019-06-01 16:09:38 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-10-24 16:27:19 +00:00
|
|
|
"strings"
|
2019-06-01 16:09:38 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/fs"
|
2023-04-27 09:45:41 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
rtest "github.com/restic/restic/internal/test"
|
2019-06-01 16:09:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWriteTar(t *testing.T) {
|
2021-09-24 21:51:51 +00:00
|
|
|
WriteTest(t, "tar", checkTar)
|
2019-06-01 16:09:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkTar(t *testing.T, testDir string, srcTar *bytes.Buffer) error {
|
|
|
|
tr := tar.NewReader(srcTar)
|
|
|
|
|
|
|
|
fileNumber := 0
|
|
|
|
tarFiles := 0
|
|
|
|
|
|
|
|
err := filepath.Walk(testDir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.Name() != filepath.Base(testDir) {
|
|
|
|
fileNumber++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
matchPath := filepath.Join(testDir, hdr.Name)
|
2020-10-24 16:27:19 +00:00
|
|
|
match, err := os.Lstat(matchPath)
|
2019-06-01 16:09:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check metadata, tar header contains time rounded to seconds
|
|
|
|
fileTime := match.ModTime().Round(time.Second)
|
|
|
|
tarTime := hdr.ModTime
|
|
|
|
if !fileTime.Equal(tarTime) {
|
|
|
|
return fmt.Errorf("modTime does not match, got: %s, want: %s", fileTime, tarTime)
|
|
|
|
}
|
|
|
|
|
2020-10-24 16:27:19 +00:00
|
|
|
if os.FileMode(hdr.Mode).Perm() != match.Mode().Perm() || os.FileMode(hdr.Mode)&^os.ModePerm != 0 {
|
|
|
|
return fmt.Errorf("mode does not match, got: %v, want: %v", os.FileMode(hdr.Mode), match.Mode())
|
|
|
|
}
|
|
|
|
|
2020-11-02 10:24:26 +00:00
|
|
|
switch hdr.Typeflag {
|
|
|
|
case tar.TypeDir:
|
2019-06-01 16:09:38 +00:00
|
|
|
// this is a folder
|
|
|
|
if hdr.Name == "." {
|
|
|
|
// we don't need to check the root folder
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-08-30 16:15:51 +00:00
|
|
|
filebase := filepath.ToSlash(match.Name())
|
|
|
|
if filepath.Base(hdr.Name) != filebase {
|
|
|
|
return fmt.Errorf("foldernames don't match got %v want %v", filepath.Base(hdr.Name), filebase)
|
2019-06-01 16:09:38 +00:00
|
|
|
}
|
2020-10-24 16:27:19 +00:00
|
|
|
if !strings.HasSuffix(hdr.Name, "/") {
|
|
|
|
return fmt.Errorf("foldernames must end with separator got %v", hdr.Name)
|
|
|
|
}
|
2020-11-02 10:24:26 +00:00
|
|
|
case tar.TypeSymlink:
|
2020-10-24 16:27:19 +00:00
|
|
|
target, err := fs.Readlink(matchPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if target != hdr.Linkname {
|
|
|
|
return fmt.Errorf("symlink target does not match, got %s want %s", target, hdr.Linkname)
|
|
|
|
}
|
2020-11-02 10:24:26 +00:00
|
|
|
default:
|
2019-06-01 16:09:38 +00:00
|
|
|
if match.Size() != hdr.Size {
|
|
|
|
return fmt.Errorf("size does not match got %v want %v", hdr.Size, match.Size())
|
|
|
|
}
|
2022-12-02 18:36:43 +00:00
|
|
|
contentsFile, err := os.ReadFile(matchPath)
|
2019-06-01 16:09:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
contentsTar := &bytes.Buffer{}
|
|
|
|
_, err = io.Copy(contentsTar, tr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if contentsTar.String() != string(contentsFile) {
|
|
|
|
return fmt.Errorf("contents does not match, got %s want %s", contentsTar, contentsFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tarFiles++
|
|
|
|
}
|
|
|
|
|
|
|
|
if tarFiles != fileNumber {
|
|
|
|
return fmt.Errorf("not the same amount of files got %v want %v", tarFiles, fileNumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-27 09:45:41 +00:00
|
|
|
|
|
|
|
// #4307.
|
|
|
|
func TestFieldTooLong(t *testing.T) {
|
|
|
|
const maxSpecialFileSize = 1 << 20 // Unexported limit in archive/tar.
|
|
|
|
|
|
|
|
node := restic.Node{
|
|
|
|
Name: "file_with_xattr",
|
|
|
|
Path: "/file_with_xattr",
|
|
|
|
Type: "file",
|
|
|
|
Mode: 0644,
|
|
|
|
ExtendedAttributes: []restic.ExtendedAttribute{
|
|
|
|
{
|
|
|
|
Name: "user.way_too_large",
|
|
|
|
Value: make([]byte, 2*maxSpecialFileSize),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
d := Dumper{format: "tar"}
|
|
|
|
err := d.dumpNodeTar(context.Background(), &node, tar.NewWriter(io.Discard))
|
|
|
|
|
|
|
|
// We want a tar.ErrFieldTooLong that has the filename.
|
|
|
|
rtest.Assert(t, errors.Is(err, tar.ErrFieldTooLong), "wrong type %T", err)
|
|
|
|
rtest.Assert(t, strings.Contains(err.Error(), node.Path),
|
|
|
|
"no filename in %q", err)
|
|
|
|
}
|