<commit_before>// +build linux

package libcontainer

import (
	"fmt"
	"strings"

	"github.com/opencontainers/runc/libcontainer/configs"
	"github.com/syndtr/gocapability/capability"
)

const allCapabilityTypes = capability.CAPS | capability.BOUNDS | capability.AMBS

var capabilityMap map[string]capability.Cap

func init() {
	capabilityMap = make(map[string]capability.Cap, capability.CAP_LAST_CAP+1)
	for _, c := range capability.List() {
		if c > capability.CAP_LAST_CAP {
			continue
		}
		capabilityMap["CAP_"+strings.ToUpper(c.String())] = c
	}
}

func newContainerCapList(capConfig *configs.Capabilities) (*containerCapabilities, error) {
	bounding := make([]capability.Cap, len(capConfig.Bounding))
	for i, c := range capConfig.Bounding {
		v, ok := capabilityMap[c]
		if !ok {
			return nil, fmt.Errorf("unknown capability %q", c)
		}
		bounding[i] = v
	}
	effective := make([]capability.Cap, len(capConfig.Effective))
	for i, c := range capConfig.Effective {
		v, ok := capabilityMap[c]
		if !ok {
			return nil, fmt.Errorf("unknown capability %q", c)
		}
		effective[i] = v
	}
	inheritable := make([]capability.Cap, len(capConfig.Inheritable))
	for i, c := range capConfig.Inheritable {
		v, ok := capabilityMap[c]
		if !ok {
			return nil, fmt.Errorf("unknown capability %q", c)
		}
		inheritable[i] = v
	}
	permitted := make([]capability.Cap, len(capConfig.Permitted))
	for i, c := range capConfig.Permitted {
		v, ok := capabilityMap[c]
		if !ok {
			return nil, fmt.Errorf("unknown capability %q", c)
		}
		permitted[i] = v
	}
	ambient := make([]capability.Cap, len(capConfig.Ambient))
	for i, c := range capConfig.Ambient {
		v, ok := capabilityMap[c]
		if !ok {
			return nil, fmt.Errorf("unknown capability %q", c)
		}
		ambient[i] = v
	}
	pid, err := capability.NewPid2(0)
	if err != nil {
		return nil, err
	}
	err = pid.Load()
	if err != nil {
		return nil, err
	}
	return &containerCapabilities{
		bounding:    bounding,
		effective:   effective,
		inheritable: inheritable,
		permitted:   permitted,
		ambient:     ambient,
		pid:         pid,
	}, nil
}

type containerCapabilities struct {
	pid         capability.Capabilities
	bounding    []capability.Cap
	effective   []capability.Cap
	inheritable []capability.Cap
	permitted   []capability.Cap<commit_msg>libcontainer: newContainerCapList() refactor to reduce duplicated code<commit_after>// +build linux

package libcontainer

import (
	"fmt"
	"strings"

	"github.com/opencontainers/runc/libcontainer/configs"
	"github.com/syndtr/gocapability/capability"
)

const allCapabilityTypes = capability.CAPS | capability.BOUNDS | capability.AMBS

var capabilityMap map[string]capability.Cap

func init() {
	capabilityMap = make(map[string]capability.Cap, capability.CAP_LAST_CAP+1)
	for _, c := range capability.List() {
		if c > capability.CAP_LAST_CAP {
			continue
		}
		capabilityMap["CAP_"+strings.ToUpper(c.String())] = c
	}
}

func newContainerCapList(capConfig *configs.Capabilities) (*containerCapabilities, error) {
	var (
		err  error
		caps containerCapabilities
	)

	if caps.bounding, err = capSlice(capConfig.Bounding); err != nil {
		return nil, err
	}
	if caps.effective, err = capSlice(capConfig.Effective); err != nil {
		return nil, err
	}
	if caps.inheritable, err = capSlice(capConfig.Inheritable); err != nil {
		return nil, err
	}
	if caps.permitted, err = capSlice(capConfig.Permitted); err != nil {
		return nil, err
	}
	if caps.ambient, err = capSlice(capConfig.Ambient); err != nil {
		return nil, err
	}
	if caps.pid, err = capability.NewPid2(0); err != nil {
		return nil, err
	}
	if err = caps.pid.Load(); err != nil {
		return nil, err
	}
	return &caps, nil
}

func capSlice(caps []string) ([]capability.Cap, error) {
	out := make([]capability.Cap, len(caps))
	for i, c := range caps {
		v, ok := capabilityMap[c]
		if !ok {
			return nil, fmt.Errorf("unknown capability %q", c)
		}
		out[i] = v
	}
	return out, nil
}

type containerCapabilities struct {
	pid         capability.Capabilities
	bounding    []capability.Cap
	effective   []capability.Cap
	inheritable []capability.Cap
	permitted   []capability.Cap
{'fraction_non_alphanumeric': 0.13202042304886943, 'fraction_numerical': 0.0014587892049598833, 'mean_word_length': 4.035495716034272, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6419174', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1439, 'n_words': 343}