| 1 |
package fsnoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"hash/fnv" |
| 7 |
"sort" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/go-git/go-git/v5/utils/merkletrie/noder" |
| 11 |
) |
| 12 |
|
| 13 |
// Dir values implement directory-like noders. |
| 14 |
type dir struct { |
| 15 |
name string // relative |
| 16 |
children []noder.Noder // sorted by name |
| 17 |
hash []byte // memoized |
| 18 |
} |
| 19 |
|
| 20 |
type byName []noder.Noder |
| 21 |
|
| 22 |
func (a byName) Len() int { return len(a) } |
| 23 |
func (a byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 24 |
func (a byName) Less(i, j int) bool { |
| 25 |
return strings.Compare(a[i].Name(), a[j].Name()) < 0 |
| 26 |
} |
| 27 |
|
| 28 |
// copies the children slice, so nobody can modify the order of its |
| 29 |
// elements from the outside. |
| 30 |
func newDir(name string, children []noder.Noder) (*dir, error) { |
| 31 |
cloned := make([]noder.Noder, len(children)) |
| 32 |
_ = copy(cloned, children) |
| 33 |
sort.Sort(byName(cloned)) |
| 34 |
|
| 35 |
if hasChildrenWithNoName(cloned) { |
| 36 |
return nil, fmt.Errorf("non-root inner nodes cannot have empty names") |
| 37 |
} |
| 38 |
|
| 39 |
if hasDuplicatedNames(cloned) { |
| 40 |
return nil, fmt.Errorf("children cannot have duplicated names") |
| 41 |
} |
| 42 |
|
| 43 |
return &dir{ |
| 44 |
name: name, |
| 45 |
children: cloned, |
| 46 |
}, nil |
| 47 |
} |
| 48 |
|
| 49 |
func hasChildrenWithNoName(children []noder.Noder) bool { |
| 50 |
for _, c := range children { |
| 51 |
if c.Name() == "" { |
| 52 |
return true |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
return false |
| 57 |
} |
| 58 |
|
| 59 |
func hasDuplicatedNames(children []noder.Noder) bool { |
| 60 |
if len(children) < 2 { |
| 61 |
return false |
| 62 |
} |
| 63 |
|
| 64 |
for i := 1; i < len(children); i++ { |
| 65 |
if children[i].Name() == children[i-1].Name() { |
| 66 |
return true |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return false |
| 71 |
} |
| 72 |
|
| 73 |
func (d *dir) Hash() []byte { |
| 74 |
if d.hash == nil { |
| 75 |
d.calculateHash() |
| 76 |
} |
| 77 |
|
| 78 |
return d.hash |
| 79 |
} |
| 80 |
|
| 81 |
// hash is calculated as the hash of "dir " plus the concatenation, for |
| 82 |
// each child, of its name, a space and its hash. Children are sorted |
| 83 |
// alphabetically before calculating the hash, so the result is unique. |
| 84 |
func (d *dir) calculateHash() { |
| 85 |
h := fnv.New64a() |
| 86 |
h.Write([]byte("dir ")) |
| 87 |
for _, c := range d.children { |
| 88 |
h.Write([]byte(c.Name())) |
| 89 |
h.Write([]byte(" ")) |
| 90 |
h.Write(c.Hash()) |
| 91 |
} |
| 92 |
d.hash = h.Sum([]byte{}) |
| 93 |
} |
| 94 |
|
| 95 |
func (d *dir) Name() string { |
| 96 |
return d.name |
| 97 |
} |
| 98 |
|
| 99 |
func (d *dir) IsDir() bool { |
| 100 |
return true |
| 101 |
} |
| 102 |
|
| 103 |
// returns a copy so nobody can alter the order of its elements from the |
| 104 |
// outside. |
| 105 |
func (d *dir) Children() ([]noder.Noder, error) { |
| 106 |
clon := make([]noder.Noder, len(d.children)) |
| 107 |
_ = copy(clon, d.children) |
| 108 |
return clon, nil |
| 109 |
} |
| 110 |
|
| 111 |
func (d *dir) NumChildren() (int, error) { |
| 112 |
return len(d.children), nil |
| 113 |
} |
| 114 |
|
| 115 |
func (d *dir) Skip() bool { |
| 116 |
return false |
| 117 |
} |
| 118 |
|
| 119 |
const ( |
| 120 |
dirStartMark = '(' |
| 121 |
dirEndMark = ')' |
| 122 |
dirElementSep = ' ' |
| 123 |
) |
| 124 |
|
| 125 |
// The string generated by this method is unique for each tree, as the |
| 126 |
// children of each node are sorted alphabetically by name when |
| 127 |
// generating the string. |
| 128 |
func (d *dir) String() string { |
| 129 |
var buf bytes.Buffer |
| 130 |
|
| 131 |
buf.WriteString(d.name) |
| 132 |
buf.WriteRune(dirStartMark) |
| 133 |
|
| 134 |
for i, c := range d.children { |
| 135 |
if i != 0 { |
| 136 |
buf.WriteRune(dirElementSep) |
| 137 |
} |
| 138 |
buf.WriteString(c.String()) |
| 139 |
} |
| 140 |
|
| 141 |
buf.WriteRune(dirEndMark) |
| 142 |
|
| 143 |
return buf.String() |
| 144 |
} |