Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

meta: keep valid utf8 in dumped JSON #1973

Merged
merged 5 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions pkg/meta/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io"
"strings"
"unicode/utf8"
)

const (
Expand Down Expand Up @@ -100,20 +101,24 @@ var CHARS = []byte("0123456789ABCDEF")
func escape(original string) string {
// similar to url.Escape but backward compatible if no '%' in it
var escValue = make([]byte, 0, len(original))
for i := 0; i < len(original); i++ {
c := original[i]
if c < 32 || c >= 127 || c == '%' || c == '"' || c == '\\' {
for i, r := range original {
if r == utf8.RuneError || r < 32 || r == '%' || r == '"' || r == '\\' {
if escValue == nil {
escValue = make([]byte, i, len(original))
escValue = make([]byte, i, len(original)*2)
for j := 0; j < i; j++ {
escValue[j] = original[j]
}
}
c := byte(r)
if r == utf8.RuneError {
c = original[i]
}
escValue = append(escValue, '%')
escValue = append(escValue, CHARS[(c>>4)&0xF])
escValue = append(escValue, CHARS[c&0xF])
} else if escValue != nil {
escValue = append(escValue, c)
n := utf8.RuneLen(r)
escValue = append(escValue, original[i:i+n]...)
}
}
if escValue == nil {
Expand Down
48 changes: 41 additions & 7 deletions pkg/meta/load_dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,49 @@ const sampleFile = "metadata.sample"
const subSampleFile = "metadata-sub.sample"

func TestEscape(t *testing.T) {
var cs = []byte("hello 世界")
for i := 0; i < 256; i++ {
cs = append(cs, byte(i))
cases := []struct {
value []rune
gbkStart, gbkEnd int
}{
{value: []rune("%1F果汁数据科技有限公司%2B"), gbkStart: 0, gbkEnd: 0},
{value: []rune("果汁数据科技有限公司%1F"), gbkStart: 0, gbkEnd: 1},
{value: []rune("果汁数据科技有限公司"), gbkStart: 1, gbkEnd: 2},
{value: []rune("果汁数据科技有限公司"), gbkStart: 1, gbkEnd: 4},
{value: []rune("果汁数据科技有限公司"), gbkStart: 5, gbkEnd: 10},
{value: []rune("果汁数据科技有限公司"), gbkStart: 0, gbkEnd: 10},
{value: []rune("GBK果汁数据科技有限公司文件"), gbkStart: 0, gbkEnd: 15},
{value: []rune("%果汁数据科%技有限公司%"), gbkStart: 1, gbkEnd: 4},
{value: []rune("\"果汁数据科\"技有限公司%"), gbkStart: 1, gbkEnd: 4},
{value: []rune("\\果汁数\\据科技有限公司"), gbkStart: 1, gbkEnd: 4},
}
for _, c := range cases {
var v []byte
prefix := c.value[:c.gbkStart]
middle := c.value[c.gbkStart:c.gbkEnd]
suffix := c.value[c.gbkEnd:]
gbk, err := Utf8ToGbk([]byte(string(middle)))
if err != nil {
t.Fatalf("Utf8ToGbk error: %v", err)
}
v = append(v, []byte(string(prefix))...)
v = append(v, gbk...)
v = append(v, []byte(string(suffix))...)
s := escape(string(v))
t.Log("escape value: ", s)
r := unescape(s)
if bytes.Compare(r, v) != 0 {
t.Fatalf("expected %v, but got %v", v, r)
}
}
s := string(cs)
r := unescape(escape(s))
if bytes.Compare(r, cs) != 0 {
t.Fatalf("expected %v, but got %v", s, r)
}

func Utf8ToGbk(s []byte) ([]byte, error) {
reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewEncoder())
d, e := ioutil.ReadAll(reader)
if e != nil {
return nil, e
}
return d, nil
}

func GbkToUtf8(s []byte) ([]byte, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/meta/metadata-sub.sample
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
],
"FSTree": {
"attr": {"inode":3,"type":"directory","mode":493,"uid":501,"gid":20,"atime":1623746591,"mtime":1623746610,"ctime":1623746610,"atimensec":959224000,"mtimensec":959224000,"ctimensec":959224000,"nlink":2,"length":0},
"xattrs": [{"name":"dk","value":"%E6%9E%9C%E6%B1%81"}],
"xattrs": [{"name":"dk","value":"果汁"}],
"entries": {
"big": {
"attr": {"inode":6,"type":"regular","mode":420,"uid":501,"gid":0,"atime":1637150857,"mtime":1637150858,"ctime":1637150878,"atimensec":961503000,"mtimensec":961503000,"ctimensec":961503000,"nlink":1,"length":104857600},
Expand Down
10 changes: 5 additions & 5 deletions pkg/meta/metadata.sample
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
"attr": {"inode":1,"type":"directory","mode":511,"uid":0,"gid":0,"atime":1623745101,"mtime":1638437879,"ctime":1638437879,"nlink":5,"length":0},
"xattrs": [{"name":"lastBackup","value":"2021-11-23T18:29:54+08:00"}],
"entries": {
"GBK%B9%FB%D6%AD%CA%FD%BE%DD%BF%C6%BC%BC%D3%D0%CF%DE%B9%AB%CB%BE%CE%C4%BC%FE": {
"GBK%B9%FB֭%CA%FD%BEݿƼ%BC%D3%D0%CF޹%AB˾%CEļ%FE": {
"attr": {"inode":34,"type":"regular","mode":420,"uid":501,"gid":0,"atime":1648717321,"mtime":1648717321,"ctime":1648717321,"atimensec":401146000,"mtimensec":401146000,"ctimensec":401146000,"nlink":1,"length":0}
},
"UTF8%E6%9E%9C%E6%B1%81%E6%95%B0%E6%8D%AE%E7%A7%91%E6%8A%80%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8%E6%96%87%E4%BB%B6": {
"UTF8果汁数据科技有限公司文件": {
"attr": {"inode":33,"type":"regular","mode":420,"uid":501,"gid":0,"atime":1648717211,"mtime":1648717211,"ctime":1648717211,"atimensec":36325000,"mtimensec":36325000,"ctimensec":36325000,"nlink":1,"length":0}
},
"UTF8%E6%9E%9C%E6%B1%81%E6%95%B0%E6%8D%AE%E7%A7%91%E6%8A%80%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8%E7%9B%AE%E5%BD%95": {
"UTF8果汁数据科技有限公司目录": {
"attr": {"inode":32,"type":"directory","mode":493,"uid":501,"gid":0,"atime":1648717173,"mtime":1648717173,"ctime":1648717173,"atimensec":605897000,"mtimensec":605897000,"ctimensec":605897000,"nlink":2,"length":0},
"entries": {
}
Expand All @@ -57,7 +57,7 @@
},
"d1": {
"attr": {"inode":3,"type":"directory","mode":493,"uid":501,"gid":20,"atime":1623746591,"mtime":1623746610,"ctime":1623746610,"atimensec":959224000,"mtimensec":959224000,"ctimensec":959224000,"nlink":2,"length":0},
"xattrs": [{"name":"dk","value":"%E6%9E%9C%E6%B1%81"}],
"xattrs": [{"name":"dk","value":"果汁"}],
"entries": {
"big": {
"attr": {"inode":6,"type":"regular","mode":420,"uid":501,"gid":0,"atime":1637150857,"mtime":1637150858,"ctime":1637150878,"atimensec":961503000,"mtimensec":961503000,"ctimensec":961503000,"nlink":1,"length":104857600},
Expand All @@ -83,7 +83,7 @@
},
"s1": {
"attr": {"inode":5,"type":"symlink","mode":420,"uid":501,"gid":20,"atime":1623746645,"mtime":1623746645,"ctime":1623746645,"atimensec":984144000,"mtimensec":984144000,"ctimensec":984144000,"nlink":1,"length":0},
"symlink": "GBK%B9%FB%D6%AD%CA%FD%BE%DD%BF%C6%BC%BC%D3%D0%CF%DE%B9%AB%CB%BE%CE%C4%BC%FE"
"symlink": "GBK%B9%FB֭%CA%FD%BEݿƼ%BC%D3%D0%CF޹%AB˾%CEļ%FE"
},
"sd": {
"attr": {"inode":26,"type":"symlink","mode":420,"uid":1,"gid":0,"atime":1637664458,"mtime":1637664458,"ctime":1637664458,"atimensec":873647000,"mtimensec":873647000,"ctimensec":873647000,"nlink":1,"length":0},
Expand Down