mirror of
https://github.com/restic/restic.git
synced 2026-12-04 15:44:46 +00:00
data / repository: quote fields in String() methods
This commit is contained in:
1 parent
584ec23160
commit
2518cd4a75
15 files changed
+106
-13
No files matched your search
@@ -280,7 +280,7 @@ func runForget(ctx context.Context, opts ForgetOptions, pruneOptions PruneOption
|
||||
keep, remove, reasons := data.ApplyPolicy(snapshotGroup, policy)
|
||||
|
||||
if !policy.Empty() && len(keep) == 0 {
|
||||
return fmt.Errorf("refusing to delete last snapshot of snapshot group \"%v\"", key.String())
|
||||
return fmt.Errorf("refusing to delete last snapshot of snapshot group [%s]", key.String())
|
||||
}
|
||||
if len(keep) != 0 && !gopts.Quiet && !gopts.JSON {
|
||||
printer.P("keep %d snapshots:\n", len(keep))
|
||||
|
||||
@@ -42,7 +42,7 @@ func TestRunForgetSafetyNet(t *testing.T) {
|
||||
KeepTags: data.TagLists{data.TagList{"invalid"}},
|
||||
GroupBy: data.SnapshotGroupByOptions{Host: true, Path: true},
|
||||
})
|
||||
rtest.Assert(t, strings.Contains(err.Error(), `refusing to delete last snapshot of snapshot group "host example, path`), "wrong error message got %v", err)
|
||||
rtest.Assert(t, strings.Contains(err.Error(), `refusing to delete last snapshot of snapshot group [host example, path`), "wrong error message got %v", err)
|
||||
|
||||
// disallow `forget --unsafe-allow-remove-all`
|
||||
err = testRunForgetMayFail(t, env.gopts, ForgetOptions{
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
func formatNodeOutput(n lsNodeOutput, long bool, human bool) string {
|
||||
if !long {
|
||||
return n.Path
|
||||
return ui.Quote(n.Path)
|
||||
}
|
||||
|
||||
var mode os.FileMode
|
||||
@@ -31,7 +31,7 @@ func formatNodeOutput(n lsNodeOutput, long bool, human bool) string {
|
||||
mode = os.ModeDir
|
||||
case data.NodeTypeSymlink:
|
||||
mode = os.ModeSymlink
|
||||
target = fmt.Sprintf(" -> %v", n.LinkTarget)
|
||||
target = fmt.Sprintf(" -> %s", ui.Quote(n.LinkTarget))
|
||||
case data.NodeTypeDev:
|
||||
mode = os.ModeDevice
|
||||
case data.NodeTypeCharDev:
|
||||
@@ -44,7 +44,7 @@ func formatNodeOutput(n lsNodeOutput, long bool, human bool) string {
|
||||
|
||||
return fmt.Sprintf("%s %5d %5d %s %s %s%s",
|
||||
mode|n.Mode, n.UID, n.GID, size,
|
||||
n.ModTime.Local().Format(global.TimeFormat), n.Path,
|
||||
n.ModTime.Local().Format(global.TimeFormat), ui.Quote(n.Path),
|
||||
target)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -54,6 +56,26 @@ func TestFormatNode(t *testing.T) {
|
||||
human: true,
|
||||
expect: "---------- 1000 2000 14.000 MiB 2020-01-02 03:04:05 " + testPath,
|
||||
},
|
||||
{
|
||||
path: "dir\nfile",
|
||||
Node: node,
|
||||
long: false,
|
||||
expect: strconv.Quote("dir\nfile"),
|
||||
},
|
||||
{
|
||||
path: "dir\nfile",
|
||||
Node: data.Node{
|
||||
Name: "link",
|
||||
Type: data.NodeTypeSymlink,
|
||||
Mode: os.ModeSymlink | 0777,
|
||||
LinkTarget: "target\x1b",
|
||||
UID: 1000,
|
||||
GID: 2000,
|
||||
ModTime: time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC),
|
||||
},
|
||||
long: true,
|
||||
expect: "Lrwxrwxrwx 1000 2000 0 2020-01-02 03:04:05 " + strconv.Quote("dir\nfile") + " -> " + strconv.Quote("target\x1b"),
|
||||
},
|
||||
} {
|
||||
r := formatNode(c.path, &c.Node, c.long, c.human)
|
||||
rtest.Equals(t, c.expect, r)
|
||||
|
||||
@@ -135,7 +135,7 @@ func (node Node) String() string {
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s %5d %5d %6d %s %s",
|
||||
mode|node.Mode, node.UID, node.GID, node.Size, node.ModTime, node.Name)
|
||||
mode|node.Mode, node.UID, node.GID, node.Size, node.ModTime, ui.Quote(node.Name))
|
||||
}
|
||||
|
||||
// GetExtendedAttribute gets the extended attribute.
|
||||
|
||||
@@ -2,6 +2,8 @@ package data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -93,3 +95,11 @@ func TestSymlinkSerializationFormat(t *testing.T) {
|
||||
test.Assert(t, n2.LinkTargetRaw == nil, "quoted link target is just a helper field and must be unset after decoding")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeStringQuotesName(t *testing.T) {
|
||||
ordinary := Node{Name: "file", UID: 1, GID: 2}.String()
|
||||
test.Assert(t, strings.HasSuffix(ordinary, " file"), "ordinary name was quoted: %s", ordinary)
|
||||
|
||||
quoted := Node{Name: "a\nb", UID: 1, GID: 2}.String()
|
||||
test.Assert(t, strings.HasSuffix(quoted, " "+strconv.Quote("a\nb")), "hostile name was not quoted: %s", quoted)
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/restic/restic/internal/debug"
|
||||
"github.com/restic/restic/internal/restic"
|
||||
"github.com/restic/restic/internal/ui"
|
||||
)
|
||||
|
||||
// Snapshot is the state of a resource at one point in time.
|
||||
@@ -119,7 +120,7 @@ func ForAllSnapshots(ctx context.Context, be restic.Lister, loader restic.Loader
|
||||
|
||||
func (sn Snapshot) String() string {
|
||||
return fmt.Sprintf("snapshot %s of %v at %s by %s@%s",
|
||||
sn.id.Str(), sn.Paths, sn.Time, sn.Username, sn.Hostname)
|
||||
sn.id.Str(), ui.QuoteSlice(sn.Paths), sn.Time, ui.Quote(sn.Username), ui.Quote(sn.Hostname))
|
||||
}
|
||||
|
||||
// ID returns the snapshot's ID.
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/restic/restic/internal/ui"
|
||||
)
|
||||
|
||||
type SnapshotGroupByOptions struct {
|
||||
@@ -69,13 +71,13 @@ type SnapshotGroupKey struct {
|
||||
func (s *SnapshotGroupKey) String() string {
|
||||
var parts []string
|
||||
if s.Hostname != "" {
|
||||
parts = append(parts, fmt.Sprintf("host %v", s.Hostname))
|
||||
parts = append(parts, fmt.Sprintf("host %v", ui.Quote(s.Hostname)))
|
||||
}
|
||||
if len(s.Paths) != 0 {
|
||||
parts = append(parts, fmt.Sprintf("path %v", s.Paths))
|
||||
parts = append(parts, fmt.Sprintf("path %v", ui.QuoteSlice(s.Paths)))
|
||||
}
|
||||
if len(s.Tags) != 0 {
|
||||
parts = append(parts, fmt.Sprintf("tags %v", s.Tags))
|
||||
parts = append(parts, fmt.Sprintf("tags %v", ui.QuoteSlice(s.Tags)))
|
||||
}
|
||||
return strings.Join(parts, ", ")
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package data_test
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
@@ -48,3 +49,15 @@ func TestGroupByOptions(t *testing.T) {
|
||||
test.Assert(t, err != nil, "missing error on invalid tags")
|
||||
test.Assert(t, !opts.Host && !opts.Path && !opts.Tag, "unexpected opts %s %s %s", opts.Host, opts.Path, opts.Tag)
|
||||
}
|
||||
|
||||
func TestSnapshotGroupKeyStringQuotes(t *testing.T) {
|
||||
ordinary := (&data.SnapshotGroupKey{Hostname: "host", Paths: []string{"/ok"}, Tags: []string{"tag"}}).String()
|
||||
test.Equals(t, "host host, path [/ok], tags [tag]", ordinary)
|
||||
|
||||
hostile := (&data.SnapshotGroupKey{
|
||||
Hostname: "a\nb",
|
||||
Paths: []string{"/ok", "p\nq"},
|
||||
Tags: []string{"t\tu"},
|
||||
}).String()
|
||||
test.Equals(t, "host "+strconv.Quote("a\nb")+", path [/ok "+strconv.Quote("p\nq")+"], tags ["+strconv.Quote("t\tu")+"]", hostile)
|
||||
}
|
||||
@@ -66,3 +66,15 @@ func testLoadJSONUnpacked(t *testing.T, version uint) {
|
||||
rtest.Equals(t, sn.Hostname, sn2.Hostname)
|
||||
rtest.Equals(t, sn.Username, sn2.Username)
|
||||
}
|
||||
|
||||
func TestSnapshotStringQuotesFields(t *testing.T) {
|
||||
ordinary := data.Snapshot{Paths: []string{"/home"}, Username: "alice", Hostname: "box"}.String()
|
||||
rtest.Equals(t, "snapshot [nil] of [/home] at 0001-01-01 00:00:00 +0000 UTC by alice@box", ordinary)
|
||||
|
||||
hostile := data.Snapshot{
|
||||
Paths: []string{"/ok", "a\nb"},
|
||||
Username: "u\nv",
|
||||
Hostname: "host",
|
||||
}.String()
|
||||
rtest.Equals(t, `snapshot [nil] of [/ok "a\nb"] at 0001-01-01 00:00:00 +0000 UTC by "u\nv"@host`, hostile)
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/restic/restic/internal/ui"
|
||||
)
|
||||
|
||||
// TagList is a list of tags.
|
||||
@@ -25,7 +27,7 @@ func splitTagList(s string) (TagList, error) {
|
||||
}
|
||||
|
||||
func (l TagList) String() string {
|
||||
return "[" + strings.Join(l, ", ") + "]"
|
||||
return "[" + strings.Join(ui.QuoteSlice(l), ", ") + "]"
|
||||
}
|
||||
|
||||
// Set updates the TagList's value.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
rtest "github.com/restic/restic/internal/test"
|
||||
@@ -78,3 +79,8 @@ func TestTagListSetInvalid(t *testing.T) {
|
||||
rtest.Equals(t, TagLists(nil), ls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTagListStringQuotes(t *testing.T) {
|
||||
rtest.Equals(t, "[ok, plain]", TagList{"ok", "plain"}.String())
|
||||
rtest.Equals(t, "[ok, "+strconv.Quote("a\nb")+"]", TagList{"ok", "a\nb"}.String())
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/restic/restic/internal/errors"
|
||||
"github.com/restic/restic/internal/restic"
|
||||
"github.com/restic/restic/internal/ui"
|
||||
|
||||
"github.com/restic/restic/internal/backend"
|
||||
"github.com/restic/restic/internal/debug"
|
||||
@@ -299,7 +300,7 @@ func (k *Key) String() string {
|
||||
if k == nil {
|
||||
return "<Key nil>"
|
||||
}
|
||||
return fmt.Sprintf("<Key of %s@%s, created on %s>", k.Username, k.Hostname, k.Created)
|
||||
return fmt.Sprintf("<Key of %s@%s, created on %s>", ui.Quote(k.Username), ui.Quote(k.Hostname), k.Created)
|
||||
}
|
||||
|
||||
// ID returns an identifier for the key.
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/restic/restic/internal/debug"
|
||||
"github.com/restic/restic/internal/errors"
|
||||
"github.com/restic/restic/internal/restic"
|
||||
"github.com/restic/restic/internal/ui"
|
||||
)
|
||||
|
||||
// unlockCancelDelay bounds the duration how long lock cleanup operations will wait
|
||||
@@ -376,7 +377,7 @@ func (l *lockHandle) checkExistence(ctx context.Context) (bool, error) {
|
||||
|
||||
func (l *lockHandle) String() string {
|
||||
text := fmt.Sprintf("PID %d on %s by %s (UID %d, GID %d)\nlock was created at %s (%s ago)\nstorage ID %v",
|
||||
l.PID, l.Hostname, l.Username, l.UID, l.GID,
|
||||
l.PID, ui.Quote(l.Hostname), ui.Quote(l.Username), l.UID, l.GID,
|
||||
l.Time.Format("2006-01-02 15:04:05"), time.Since(l.Time),
|
||||
l.lockID.Str())
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -246,3 +247,25 @@ func TestLockRefreshStaleMissing(t *testing.T) {
|
||||
err = lock.refreshStaleLock(context.TODO())
|
||||
rtest.Assert(t, err == errRemovedLock, "unexpected error, expected %v, got %v", errRemovedLock, err)
|
||||
}
|
||||
|
||||
func TestLockStringQuotesHost(t *testing.T) {
|
||||
created := time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC)
|
||||
age := regexp.MustCompile(`\([^)]* ago\)`)
|
||||
lockText := func(host string) string {
|
||||
return age.ReplaceAllString((&lockHandle{Lock: Lock{PID: 7, Hostname: host, Username: "user", UID: 1, GID: 2, Time: created}}).String(), "(AGE ago)")
|
||||
}
|
||||
|
||||
rtest.Equals(t, "PID 7 on host by user (UID 1, GID 2)\nlock was created at 2020-01-02 03:04:05 (AGE ago)\nstorage ID [nil]", lockText("host"))
|
||||
rtest.Equals(t, "PID 7 on \"h\\x1b\" by user (UID 1, GID 2)\nlock was created at 2020-01-02 03:04:05 (AGE ago)\nstorage ID [nil]", lockText("h\x1b"))
|
||||
}
|
||||
|
||||
func TestKeyStringQuotesIdentity(t *testing.T) {
|
||||
rtest.Equals(t, "<Key nil>", (*Key)(nil).String())
|
||||
|
||||
created := time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC)
|
||||
ordinary := (&Key{Username: "alice", Hostname: "box", Created: created}).String()
|
||||
rtest.Equals(t, "<Key of alice@box, created on 2020-01-02 03:04:05 +0000 UTC>", ordinary)
|
||||
|
||||
hostile := (&Key{Username: "a\nb", Hostname: "box", Created: created}).String()
|
||||
rtest.Equals(t, `<Key of "a\nb"@box, created on 2020-01-02 03:04:05 +0000 UTC>`, hostile)
|
||||
}
|
||||
Reference in new issue
Block a user