Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions subcommands/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ type Restore struct {
Snapshots []string
}

const (
snapshotErrorsRootPath = "/"
backupErrorsInspectWarnFmt = "could not inspect snapshot backup errors: %s"
backupErrorsRestoreWarnFmt = "snapshot contains %d backup %s; restored files are limited to data that was backed up"
backupErrorSingular = "error"
backupErrorPlural = "errors"
backupErrorSingularThreshold = 1
)

func init() {
subcommands.Register(func() subcommands.Subcommand { return &Restore{} }, 0, "restore")
}
Expand Down Expand Up @@ -198,7 +207,34 @@ func (cmd *Restore) Execute(ctx *appcontext.AppContext, repo *repository.Reposit
return 1, err
}

backupErrorCount, err := snapshotBackupErrorCount(snap)
if err != nil {
ctx.GetLogger().Warn(backupErrorsInspectWarnFmt, err)
} else if backupErrorCount != 0 {
errorWord := backupErrorPlural
if backupErrorCount == backupErrorSingularThreshold {
errorWord = backupErrorSingular
}
ctx.GetLogger().Warn(backupErrorsRestoreWarnFmt, backupErrorCount, errorWord)
}

snap.Close()
}
return 0, nil
}

func snapshotBackupErrorCount(snap *snapshot.Snapshot) (int, error) {
fsc, err := snap.Filesystem()
if err != nil {
return 0, err
}

count := 0
for _, err := range fsc.Errors(snapshotErrorsRootPath) {
if err != nil {
return 0, err
}
count++
}
return count, nil
}
43 changes: 43 additions & 0 deletions subcommands/restore/restore_extra_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
package restore

import (
"bytes"
"encoding/hex"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"

"github.com/PlakarKorp/kloset/connectors"
"github.com/PlakarKorp/kloset/objects"
ptesting "github.com/PlakarKorp/plakar/testing"
"github.com/stretchr/testify/require"
)

const (
restoreWarningRootPath = "/"
restoreWarningDataPath = "/data.txt"
restoreWarningDeniedPath = "/denied.txt"
restoreWarningDataName = "data.txt"
restoreWarningPayload = "payload"
restoreWarningExpectedOutput = "snapshot contains 1 backup error"
)

func mkRestoreDir(t *testing.T) string {
t.Helper()
dir, err := os.MkdirTemp("", "tmp_to_restore")
Expand Down Expand Up @@ -116,6 +129,36 @@ func TestRestoreWithSnapshotPath(t *testing.T) {
require.False(t, sawBar, "another_subdir should not have been restored")
}

func TestRestoreWarnsWhenSnapshotContainsBackupErrors(t *testing.T) {
bufOut := bytes.NewBuffer(nil)
bufErr := bytes.NewBuffer(nil)
repo, ctx := ptesting.GenerateRepository(t, bufOut, bufErr, nil)
snap := ptesting.GenerateSnapshot(t, repo, nil,
ptesting.WithGenerator(func(ch chan<- *connectors.Record) {
ch <- connectors.NewRecord(restoreWarningRootPath, "", objects.FileInfo{
Lname: restoreWarningRootPath, Lmode: os.ModeDir | 0755,
}, nil, nil)
ch <- connectors.NewRecord(restoreWarningDataPath, "", objects.FileInfo{
Lname: restoreWarningDataName, Lmode: 0644, Lsize: int64(len(restoreWarningPayload)),
}, nil, func() (io.ReadCloser, error) {
return io.NopCloser(strings.NewReader(restoreWarningPayload)), nil
})
ch <- connectors.NewError(restoreWarningDeniedPath, os.ErrPermission)
}),
)
defer snap.Close()

dir := mkRestoreDir(t)
id := snap.Header.GetIndexID()
cmd := &Restore{}
require.NoError(t, cmd.Parse(ctx, []string{"-to", dir, hex.EncodeToString(id[:])}))

status, err := cmd.Execute(ctx, repo)
require.NoError(t, err)
require.Equal(t, 0, status)
require.Contains(t, bufErr.String(), restoreWarningExpectedOutput)
}

func TestRestoreSkipPermissionsFlag(t *testing.T) {
_, _, ctx := generateSnapshot(t)
cmd := &Restore{}
Expand Down