| 1 |
package fsnoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
|
| 8 |
"github.com/go-git/go-git/v5/utils/merkletrie/noder" |
| 9 |
) |
| 10 |
|
| 11 |
// New function creates a full merkle trie from the string description of |
| 12 |
// a filesystem tree. See examples of the string format in the package |
| 13 |
// description. |
| 14 |
func New(s string) (noder.Noder, error) { |
| 15 |
return decodeDir([]byte(s), root) |
| 16 |
} |
| 17 |
|
| 18 |
const ( |
| 19 |
root = true |
| 20 |
nonRoot = false |
| 21 |
) |
| 22 |
|
| 23 |
// Expected data: a fsnoder description, for example: A(foo bar qux ...). |
| 24 |
// When isRoot is true, unnamed dirs are supported, for example: (foo |
| 25 |
// bar qux ...) |
| 26 |
func decodeDir(data []byte, isRoot bool) (*dir, error) { |
| 27 |
data = bytes.TrimSpace(data) |
| 28 |
if len(data) == 0 { |
| 29 |
return nil, io.EOF |
| 30 |
} |
| 31 |
|
| 32 |
// get the name of the dir and remove it from the data. In case the |
| 33 |
// there is no name and isRoot is true, just use "" as the name. |
| 34 |
var name string |
| 35 |
switch end := bytes.IndexRune(data, dirStartMark); end { |
| 36 |
case -1: |
| 37 |
return nil, fmt.Errorf("%c not found", dirStartMark) |
| 38 |
case 0: |
| 39 |
if isRoot { |
| 40 |
name = "" |
| 41 |
} else { |
| 42 |
return nil, fmt.Errorf("inner unnamed dirs not allowed: %s", data) |
| 43 |
} |
| 44 |
default: |
| 45 |
name = string(data[0:end]) |
| 46 |
data = data[end:] |
| 47 |
} |
| 48 |
|
| 49 |
// check data ends with the dirEndMark |
| 50 |
if data[len(data)-1] != dirEndMark { |
| 51 |
return nil, fmt.Errorf("malformed data: last %q not found", |
| 52 |
dirEndMark) |
| 53 |
} |
| 54 |
data = data[1 : len(data)-1] // remove initial '(' and last ')' |
| 55 |
|
| 56 |
children, err := decodeChildren(data) |
| 57 |
if err != nil { |
| 58 |
return nil, err |
| 59 |
} |
| 60 |
|
| 61 |
return newDir(name, children) |
| 62 |
} |
| 63 |
|
| 64 |
func isNumber(b rune) bool { |
| 65 |
return '0' <= b && b <= '9' |
| 66 |
} |
| 67 |
|
| 68 |
func isLetter(b rune) bool { |
| 69 |
return ('a' <= b && b <= 'z') || ('A' <= b && b <= 'Z') |
| 70 |
} |
| 71 |
|
| 72 |
func decodeChildren(data []byte) ([]noder.Noder, error) { |
| 73 |
data = bytes.TrimSpace(data) |
| 74 |
if len(data) == 0 { |
| 75 |
return nil, nil |
| 76 |
} |
| 77 |
|
| 78 |
chunks := split(data) |
| 79 |
ret := make([]noder.Noder, len(chunks)) |
| 80 |
var err error |
| 81 |
for i, c := range chunks { |
| 82 |
ret[i], err = decodeChild(c) |
| 83 |
if err != nil { |
| 84 |
return nil, fmt.Errorf("malformed element %d (%s): %s", i, c, err) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return ret, nil |
| 89 |
} |
| 90 |
|
| 91 |
// returns the description of the elements of a dir. It is just looking |
| 92 |
// for spaces if they are not part of inner dirs. |
| 93 |
func split(data []byte) [][]byte { |
| 94 |
chunks := [][]byte{} |
| 95 |
|
| 96 |
start := 0 |
| 97 |
dirDepth := 0 |
| 98 |
for i, b := range data { |
| 99 |
switch b { |
| 100 |
case dirStartMark: |
| 101 |
dirDepth++ |
| 102 |
case dirEndMark: |
| 103 |
dirDepth-- |
| 104 |
case dirElementSep: |
| 105 |
if dirDepth == 0 { |
| 106 |
chunks = append(chunks, data[start:i+1]) |
| 107 |
start = i + 1 |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
chunks = append(chunks, data[start:]) |
| 112 |
|
| 113 |
return chunks |
| 114 |
} |
| 115 |
|
| 116 |
// A child can be a file or a dir. |
| 117 |
func decodeChild(data []byte) (noder.Noder, error) { |
| 118 |
clean := bytes.TrimSpace(data) |
| 119 |
if len(data) < 3 { |
| 120 |
return nil, fmt.Errorf("element too short: %s", clean) |
| 121 |
} |
| 122 |
|
| 123 |
fileNameEnd := bytes.IndexRune(data, fileStartMark) |
| 124 |
dirNameEnd := bytes.IndexRune(data, dirStartMark) |
| 125 |
switch { |
| 126 |
case fileNameEnd == -1 && dirNameEnd == -1: |
| 127 |
return nil, fmt.Errorf( |
| 128 |
"malformed child, no file or dir start mark found") |
| 129 |
case fileNameEnd == -1: |
| 130 |
return decodeDir(clean, nonRoot) |
| 131 |
case dirNameEnd == -1: |
| 132 |
return decodeFile(clean) |
| 133 |
case dirNameEnd < fileNameEnd: |
| 134 |
return decodeDir(clean, nonRoot) |
| 135 |
case dirNameEnd > fileNameEnd: |
| 136 |
return decodeFile(clean) |
| 137 |
} |
| 138 |
|
| 139 |
return nil, fmt.Errorf("unreachable") |
| 140 |
} |
| 141 |
|
| 142 |
func decodeFile(data []byte) (noder.Noder, error) { |
| 143 |
nameEnd := bytes.IndexRune(data, fileStartMark) |
| 144 |
if nameEnd == -1 { |
| 145 |
return nil, fmt.Errorf("malformed file, no %c found", fileStartMark) |
| 146 |
} |
| 147 |
contentStart := nameEnd + 1 |
| 148 |
contentEnd := bytes.IndexRune(data, fileEndMark) |
| 149 |
if contentEnd == -1 { |
| 150 |
return nil, fmt.Errorf("malformed file, no %c found", fileEndMark) |
| 151 |
} |
| 152 |
|
| 153 |
switch { |
| 154 |
case nameEnd > contentEnd: |
| 155 |
return nil, fmt.Errorf("malformed file, found %c before %c", |
| 156 |
fileEndMark, fileStartMark) |
| 157 |
case contentStart == contentEnd: |
| 158 |
name := string(data[:nameEnd]) |
| 159 |
if !validFileName(name) { |
| 160 |
return nil, fmt.Errorf("invalid file name") |
| 161 |
} |
| 162 |
return newFile(name, "") |
| 163 |
default: |
| 164 |
name := string(data[:nameEnd]) |
| 165 |
if !validFileName(name) { |
| 166 |
return nil, fmt.Errorf("invalid file name") |
| 167 |
} |
| 168 |
contents := string(data[contentStart:contentEnd]) |
| 169 |
if !validFileContents(contents) { |
| 170 |
return nil, fmt.Errorf("invalid file contents") |
| 171 |
} |
| 172 |
return newFile(name, contents) |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
func validFileName(s string) bool { |
| 177 |
for _, c := range s { |
| 178 |
if !isLetter(c) && c != '.' { |
| 179 |
return false |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
return true |
| 184 |
} |
| 185 |
|
| 186 |
func validFileContents(s string) bool { |
| 187 |
for _, c := range s { |
| 188 |
if !isNumber(c) { |
| 189 |
return false |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
return true |
| 194 |
} |
| 195 |
|
| 196 |
// HashEqual returns if a and b have the same hash. |
| 197 |
func HashEqual(a, b noder.Hasher) bool { |
| 198 |
return bytes.Equal(a.Hash(), b.Hash()) |
| 199 |
} |