Sort bind mounts (#134)

* Sort bind mounts

Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com>

* Add comment

Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com>

---------

Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com>
This commit is contained in:
Mauro Morales
2023-06-12 10:47:49 +02:00
committed by GitHub
parent 0811f0f054
commit 62831b8ecf
3 changed files with 40 additions and 1 deletions

View File

@@ -6,6 +6,8 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/containerd/containerd/mount"
@@ -31,6 +33,22 @@ type State struct {
fstabs []*fstab.Mount
}
// SortedBindMounts returns the nodes with less depth first and in alphabetical order.
func (s *State) SortedBindMounts() []string {
bindMountsCopy := s.BindMounts
sort.Slice(bindMountsCopy, func(i, j int) bool {
iAry := strings.Split(bindMountsCopy[i], "/")
jAry := strings.Split(bindMountsCopy[j], "/")
iSize := len(iAry)
jSize := len(jAry)
if iSize == jSize {
return strings.Compare(iAry[len(iAry)-1], jAry[len(jAry)-1]) == -1
}
return iSize < jSize
})
return bindMountsCopy
}
func (s *State) path(p ...string) string {
return filepath.Join(append([]string{s.Rootdir}, p...)...)
}