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

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()