Correctly encode non utf8 node names
This commit is contained in:
parent
fa94d408f3
commit
45e40eb27a
3 changed files with 50 additions and 1 deletions
|
@ -14,7 +14,7 @@ all: restic
|
||||||
restic: $(wildcard *.go) $(wildcard ../../*.go) $(wildcard ../../*/*.go)
|
restic: $(wildcard *.go) $(wildcard ../../*.go) $(wildcard ../../*/*.go)
|
||||||
go build $(TAGS) -ldflags "$(LDFLAGS)"
|
go build $(TAGS) -ldflags "$(LDFLAGS)"
|
||||||
|
|
||||||
debug: TAGS=-tags debug_cmd
|
debug: TAGS=-tags "debug debug_cmd"
|
||||||
debug: restic
|
debug: restic
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
|
|
23
tree.go
23
tree.go
|
@ -1,6 +1,7 @@
|
||||||
package restic
|
package restic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
@ -367,6 +368,28 @@ func (node Node) SameContent(olderNode *Node) bool {
|
||||||
return true
|
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() {
|
func (b Blob) Free() {
|
||||||
if b.ID != nil {
|
if b.ID != nil {
|
||||||
b.ID.Free()
|
b.ID.Free()
|
||||||
|
|
26
tree_test.go
26
tree_test.go
|
@ -1,10 +1,13 @@
|
||||||
package restic_test
|
package restic_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/restic/restic"
|
||||||
)
|
)
|
||||||
|
|
||||||
var testFiles = []struct {
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue