2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-25 14:17:42 +00:00

Correctly encode non utf8 node names

This commit is contained in:
Alexander Neumann 2014-12-06 22:01:54 +01:00
parent fa94d408f3
commit 45e40eb27a
3 changed files with 50 additions and 1 deletions

View File

@ -14,7 +14,7 @@ all: restic
restic: $(wildcard *.go) $(wildcard ../../*.go) $(wildcard ../../*/*.go)
go build $(TAGS) -ldflags "$(LDFLAGS)"
debug: TAGS=-tags debug_cmd
debug: TAGS=-tags "debug debug_cmd"
debug: restic
clean:

23
tree.go
View File

@ -1,6 +1,7 @@
package restic
import (
"encoding/json"
"errors"
"fmt"
"os"
@ -367,6 +368,28 @@ func (node Node) SameContent(olderNode *Node) bool {
return true
}
func (node Node) MarshalJSON() ([]byte, error) {
type nodeJSON Node
nj := nodeJSON(node)
name := strconv.Quote(node.Name)
nj.Name = name[1 : len(name)-1]
return json.Marshal(nj)
}
func (node *Node) UnmarshalJSON(data []byte) error {
type nodeJSON Node
var nj *nodeJSON = (*nodeJSON)(node)
err := json.Unmarshal(data, nj)
if err != nil {
return err
}
nj.Name, err = strconv.Unquote(`"` + nj.Name + `"`)
return err
}
func (b Blob) Free() {
if b.ID != nil {
b.ID.Free()

View File

@ -1,10 +1,13 @@
package restic_test
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/restic/restic"
)
var testFiles = []struct {
@ -50,3 +53,26 @@ func TestTree(t *testing.T) {
}
}()
}
var testNodes = []restic.Node{
restic.Node{Name: "normal"},
restic.Node{Name: "with backslashes \\zzz"},
restic.Node{Name: "test utf-8 föbärß"},
restic.Node{Name: "test invalid \x00\x01\x02\x03\x04"},
restic.Node{Name: "test latin1 \x75\x6d\x6c\xe4\xfc\x74\xf6\x6e\xdf\x6e\x6c\x6c"},
}
func TestNodeMarshal(t *testing.T) {
for i, n := range testNodes {
data, err := json.Marshal(&n)
ok(t, err)
var node restic.Node
err = json.Unmarshal(data, &node)
ok(t, err)
if n.Name != node.Name {
t.Fatalf("Node %d: Names are not equal, want: %q got: %q", i, n.Name, node.Name)
}
}
}