github.com/rootless-containers/rootlesskit/v2@v2.3.4/pkg/parent/cgrouputil/moby_sys_mountinfo_tmphack.go (about)

     1  package cgrouputil
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"strings"
     7  
     8  	"github.com/moby/sys/mountinfo"
     9  )
    10  
    11  // mountinfoFSType returns m.Fstype on mountinfo v0.1.3,
    12  // returns m.FSType on mountinfo v0.4.0.
    13  func mountinfoFSType(m *mountinfo.Info) (string, bool) {
    14  	elem := reflect.ValueOf(m).Elem()
    15  	for i := 0; i < elem.NumField(); i++ {
    16  		typeField := elem.Type().Field(i)
    17  		name := typeField.Name
    18  		typ := typeField.Type.String()
    19  		if strings.ToLower(name) == "fstype" && typ == "string" {
    20  			value := elem.Field(i).String()
    21  			return value, true
    22  		}
    23  	}
    24  
    25  	return "", false
    26  }
    27  
    28  // mountinfoFSTypeFilter is reimplementation of mountinfo.FSTypeFilter.
    29  // Temporary solution for supporting both moby/sys/mountinfo@v0.1.3 and @v0.4.0 .
    30  // Will be removed after downstream projects stop using @v0.1.3 .
    31  func mountinfoFSTypeFilter(fstype ...string) mountinfo.FilterFunc {
    32  	return func(m *mountinfo.Info) (bool, bool) {
    33  		mFSType, ok := mountinfoFSType(m)
    34  		if !ok {
    35  			panic(fmt.Errorf("failed to get Fstype (FSType) of %+v", m))
    36  		}
    37  		for _, t := range fstype {
    38  			if mFSType == t {
    39  				return false, false // don't skeep, keep going
    40  			}
    41  		}
    42  		return true, false // skip, keep going
    43  	}
    44  }