make Tree private

This commit is contained in:
Motiejus Jakštys 2021-05-24 00:11:58 +03:00
parent 8dedaebcf7
commit 02cec0f76f
3 changed files with 21 additions and 21 deletions

View File

@ -193,10 +193,10 @@ func writeFile(tr *tar.Reader, tw *tar.Writer, hdr *tar.Header) error {
return nil
}
func whiteoutDirs(whreaddir map[string]int, nlayers int) []*Tree {
ret := make([]*Tree, nlayers)
func whiteoutDirs(whreaddir map[string]int, nlayers int) []*tree {
ret := make([]*tree, nlayers)
for i := range ret {
ret[i] = New()
ret[i] = newTree()
}
for fname, layer := range whreaddir {
if layer == 0 {

View File

@ -6,18 +6,18 @@ import (
"strings"
)
// Tree is a way to store directory paths for whiteouts.
// tree is a way to store directory paths for whiteouts.
// It is semi-optimized for reads and non-optimized for writes;
// See Merge() and HasPrefix for trade-offs.
type Tree struct {
type tree struct {
name string
children []*Tree
children []*tree
end bool
}
// New creates a new tree from a given path.
func New(paths ...string) *Tree {
t := &Tree{name: ".", children: []*Tree{}}
func newTree(paths ...string) *tree {
t := &tree{name: ".", children: []*tree{}}
for _, path := range paths {
t.Add(path)
}
@ -25,7 +25,7 @@ func New(paths ...string) *Tree {
}
// Add adds a sequence to a tree
func (t *Tree) Add(path string) {
func (t *tree) Add(path string) {
t.add(strings.Split(filepath.Clean(path), "/"))
}
@ -35,18 +35,18 @@ func (t *Tree) Add(path string) {
// really small (usually 1 or 2), it does not really matter. If you find a
// real-world container with 30+ whiteout paths on a single path, please ping
// the author/maintainer of this code.
func (t *Tree) HasPrefix(path string) bool {
func (t *tree) HasPrefix(path string) bool {
return t.hasprefix(strings.Split(filepath.Clean(path), "/"))
}
// Merge merges adds t2 to t. It is not optimized for speed, since it's walking
// full branch for every other branch.
func (t *Tree) Merge(t2 *Tree) {
func (t *tree) Merge(t2 *tree) {
t.merge(t2, []string{})
}
// String stringifies a tree
func (t *Tree) String() string {
func (t *tree) String() string {
if len(t.children) == 0 {
return "<empty>"
}
@ -57,7 +57,7 @@ func (t *Tree) String() string {
return strings.Join(res.res, ":")
}
func (t *Tree) add(nodes []string) {
func (t *tree) add(nodes []string) {
if len(nodes) == 0 {
t.end = true
return
@ -69,12 +69,12 @@ func (t *Tree) add(nodes []string) {
}
}
newNode := &Tree{name: nodes[0]}
newNode := &tree{name: nodes[0]}
t.children = append(t.children, newNode)
newNode.add(nodes[1:])
}
func (t *Tree) hasprefix(nodes []string) bool {
func (t *tree) hasprefix(nodes []string) bool {
if len(nodes) == 0 {
return t.end
}
@ -95,7 +95,7 @@ type stringer struct {
res []string
}
func (s *stringer) stringify(t *Tree, acc []string) {
func (s *stringer) stringify(t *tree, acc []string) {
if t.name == "" {
return
}
@ -109,7 +109,7 @@ func (s *stringer) stringify(t *Tree, acc []string) {
}
}
func (t *Tree) merge(t2 *Tree, acc []string) {
func (t *tree) merge(t2 *tree, acc []string) {
if t2.end {
t.add(append(acc[1:], t2.name))
}

View File

@ -33,7 +33,7 @@ func TestTree(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tree := New(tt.paths...)
tree := newTree(tt.paths...)
for _, path := range tt.matchTrue {
t.Run(path, func(t *testing.T) {
@ -53,8 +53,8 @@ func TestTree(t *testing.T) {
}
func TestTreeMerge(t *testing.T) {
tree1 := New("bin/ar", "var/cache/apt")
tree2 := New("bin/ar", "bin/busybox", "usr/share/doc")
tree1 := newTree("bin/ar", "var/cache/apt")
tree2 := newTree("bin/ar", "bin/busybox", "usr/share/doc")
tree1.Merge(tree2)
assert.Equal(t, "./bin/ar:./bin/busybox:./usr/share/doc:./var/cache/apt", tree1.String())
assert.Equal(t, "./bin/ar:./bin/busybox:./usr/share/doc", tree2.String())
@ -90,7 +90,7 @@ func TestString(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tree := New(tt.paths...)
tree := newTree(tt.paths...)
assert.Equal(t, tt.wantStr, tree.String())
})
}