2018-09-12 03:12:19 +00:00
|
|
|
package restorer
|
|
|
|
|
|
|
|
import (
|
2024-06-01 19:56:56 +00:00
|
|
|
"fmt"
|
2022-12-02 18:36:43 +00:00
|
|
|
"os"
|
2024-06-01 19:56:56 +00:00
|
|
|
"path/filepath"
|
2024-06-13 20:52:31 +00:00
|
|
|
"runtime"
|
2018-09-12 03:12:19 +00:00
|
|
|
"testing"
|
|
|
|
|
2024-06-01 19:56:56 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2018-09-12 03:12:19 +00:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFilesWriterBasic(t *testing.T) {
|
2022-12-09 12:42:33 +00:00
|
|
|
dir := rtest.TempDir(t)
|
2018-09-12 03:12:19 +00:00
|
|
|
w := newFilesWriter(1)
|
|
|
|
|
2018-09-15 00:18:37 +00:00
|
|
|
f1 := dir + "/f1"
|
|
|
|
f2 := dir + "/f2"
|
2018-09-12 03:12:19 +00:00
|
|
|
|
2022-08-07 15:26:46 +00:00
|
|
|
rtest.OK(t, w.writeToFile(f1, []byte{1}, 0, 2, false))
|
2019-11-27 12:22:38 +00:00
|
|
|
rtest.Equals(t, 0, len(w.buckets[0].files))
|
|
|
|
|
2022-08-07 15:26:46 +00:00
|
|
|
rtest.OK(t, w.writeToFile(f2, []byte{2}, 0, 2, false))
|
2019-11-27 12:22:38 +00:00
|
|
|
rtest.Equals(t, 0, len(w.buckets[0].files))
|
|
|
|
|
2022-08-07 15:26:46 +00:00
|
|
|
rtest.OK(t, w.writeToFile(f1, []byte{1}, 1, -1, false))
|
2019-11-27 12:22:38 +00:00
|
|
|
rtest.Equals(t, 0, len(w.buckets[0].files))
|
|
|
|
|
2022-08-07 15:26:46 +00:00
|
|
|
rtest.OK(t, w.writeToFile(f2, []byte{2}, 1, -1, false))
|
2019-11-27 12:22:38 +00:00
|
|
|
rtest.Equals(t, 0, len(w.buckets[0].files))
|
2018-09-12 03:12:19 +00:00
|
|
|
|
2022-12-02 18:36:43 +00:00
|
|
|
buf, err := os.ReadFile(f1)
|
2018-09-12 03:12:19 +00:00
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.Equals(t, []byte{1, 1}, buf)
|
|
|
|
|
2022-12-02 18:36:43 +00:00
|
|
|
buf, err = os.ReadFile(f2)
|
2018-09-12 03:12:19 +00:00
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.Equals(t, []byte{2, 2}, buf)
|
|
|
|
}
|
2024-06-01 19:56:56 +00:00
|
|
|
|
|
|
|
func TestCreateFile(t *testing.T) {
|
|
|
|
basepath := filepath.Join(t.TempDir(), "test")
|
|
|
|
|
|
|
|
scenarios := []struct {
|
|
|
|
name string
|
|
|
|
create func(t testing.TB, path string)
|
2024-06-01 20:21:16 +00:00
|
|
|
check func(t testing.TB, path string)
|
2024-06-01 19:56:56 +00:00
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
2024-06-01 20:21:16 +00:00
|
|
|
name: "file",
|
|
|
|
create: func(t testing.TB, path string) {
|
2024-06-01 19:56:56 +00:00
|
|
|
rtest.OK(t, os.WriteFile(path, []byte("test-test-test-data"), 0o400))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-06-01 20:21:16 +00:00
|
|
|
name: "empty dir",
|
|
|
|
create: func(t testing.TB, path string) {
|
2024-06-01 19:56:56 +00:00
|
|
|
rtest.OK(t, os.Mkdir(path, 0o400))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-06-01 20:21:16 +00:00
|
|
|
name: "symlink",
|
|
|
|
create: func(t testing.TB, path string) {
|
2024-06-01 19:56:56 +00:00
|
|
|
rtest.OK(t, os.Symlink("./something", path))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-06-01 20:21:16 +00:00
|
|
|
name: "filled dir",
|
|
|
|
create: func(t testing.TB, path string) {
|
2024-06-01 19:56:56 +00:00
|
|
|
rtest.OK(t, os.Mkdir(path, 0o700))
|
|
|
|
rtest.OK(t, os.WriteFile(filepath.Join(path, "file"), []byte("data"), 0o400))
|
|
|
|
},
|
2024-06-13 20:52:31 +00:00
|
|
|
err: notEmptyDirError(),
|
2024-06-01 20:21:16 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hardlinks",
|
|
|
|
create: func(t testing.TB, path string) {
|
|
|
|
rtest.OK(t, os.WriteFile(path, []byte("test-test-test-data"), 0o400))
|
|
|
|
rtest.OK(t, os.Link(path, path+"h"))
|
|
|
|
},
|
|
|
|
check: func(t testing.TB, path string) {
|
2024-06-13 20:52:31 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// hardlinks are not supported on windows
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-01 20:21:16 +00:00
|
|
|
data, err := os.ReadFile(path + "h")
|
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.Equals(t, "test-test-test-data", string(data), "unexpected content change")
|
|
|
|
},
|
2024-06-01 19:56:56 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
size int64
|
|
|
|
isSparse bool
|
|
|
|
}{
|
|
|
|
{5, false},
|
|
|
|
{21, false},
|
|
|
|
{100, false},
|
|
|
|
{5, true},
|
|
|
|
{21, true},
|
|
|
|
{100, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, sc := range scenarios {
|
|
|
|
t.Run(sc.name, func(t *testing.T) {
|
2024-06-01 20:21:16 +00:00
|
|
|
for j, test := range tests {
|
|
|
|
path := basepath + fmt.Sprintf("%v%v", i, j)
|
2024-06-01 19:56:56 +00:00
|
|
|
sc.create(t, path)
|
|
|
|
f, err := createFile(path, test.size, test.isSparse)
|
|
|
|
if sc.err == nil {
|
|
|
|
rtest.OK(t, err)
|
|
|
|
fi, err := f.Stat()
|
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.Assert(t, fi.Mode().IsRegular(), "wrong filetype %v", fi.Mode())
|
|
|
|
rtest.Assert(t, fi.Size() <= test.size, "unexpected file size expected %v, got %v", test.size, fi.Size())
|
|
|
|
rtest.OK(t, f.Close())
|
2024-06-01 20:21:16 +00:00
|
|
|
if sc.check != nil {
|
|
|
|
sc.check(t, path)
|
|
|
|
}
|
2024-06-01 19:56:56 +00:00
|
|
|
} else {
|
|
|
|
rtest.Assert(t, errors.Is(err, sc.err), "unexpected error got %v expected %v", err, sc.err)
|
|
|
|
}
|
|
|
|
rtest.OK(t, os.RemoveAll(path))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|