Skip to content

Commit

Permalink
ui/backup: Quote funny filenames
Browse files Browse the repository at this point in the history
Fixes #4191.
  • Loading branch information
greatroar committed Feb 5, 2023
1 parent 49fa8fe commit 78f659a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/ui/backup/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package backup
import (
"fmt"
"sort"
"strconv"
"time"
"unicode"

"github.com/restic/restic/internal/archiver"
"github.com/restic/restic/internal/restic"
Expand Down Expand Up @@ -86,6 +88,8 @@ func (b *TextProgress) Error(item string, err error) error {
// CompleteItem is the status callback function for the archiver when a
// file/dir has been saved successfully.
func (b *TextProgress) CompleteItem(messageType, item string, previous, current *restic.Node, s archiver.ItemStats, d time.Duration) {
item = quote(item)

switch messageType {
case "dir new":
b.VV("new %v, saved in %.3fs (%v added, %v stored, %v metadata)",
Expand All @@ -108,6 +112,15 @@ func (b *TextProgress) CompleteItem(messageType, item string, previous, current
}
}

func quote(filename string) string {
for _, r := range filename {
if r == ' ' || r == unicode.ReplacementChar || !unicode.IsPrint(r) {
return strconv.Quote(filename)
}
}
return filename
}

// ReportTotal sets the total stats up to now
func (b *TextProgress) ReportTotal(item string, start time.Time, s archiver.ScanStats) {
b.V("scan finished in %.3fs: %v files, %s",
Expand Down
30 changes: 30 additions & 0 deletions internal/ui/backup/text_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package backup

import (
"strconv"
"testing"

rtest "github.com/restic/restic/internal/test"
)

func TestQuote(t *testing.T) {
for _, c := range []struct {
in string
needQuote bool
}{
{"foo.bar/baz", false},
{"föó_bàŕ-bãẑ", false},
{" foo ", true},
{"foo bar", true},
{"foo\nbar", true},
{"foo\rbar", true},
{"foo\abar", true},
{"\xff", true},
} {
if c.needQuote {
rtest.Equals(t, strconv.Quote(c.in), quote(c.in))
} else {
rtest.Equals(t, c.in, quote(c.in))
}
}
}

0 comments on commit 78f659a

Please sign in to comment.