mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-10 21:53:57 +08:00
libct: use strings.CutPrefix where possible
Using strings.CutPrefix (available since Go 1.20) instead of strings.HasPrefix and/or strings.TrimPrefix makes the code a tad more straightforward. No functional change. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
@@ -224,8 +224,7 @@ func findDeviceGroup(ruleType devices.Type, ruleMajor int64) (string, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
group := strings.TrimPrefix(line, ruleMajorStr)
|
||||
if len(group) < len(line) { // got it
|
||||
if group, ok := strings.CutPrefix(line, ruleMajorStr); ok {
|
||||
return prefix + group, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,8 +151,8 @@ func openFile(dir, file string, flags int) (*os.File, error) {
|
||||
if prepareOpenat2() != nil {
|
||||
return openFallback(path, flags, mode)
|
||||
}
|
||||
relPath := strings.TrimPrefix(path, cgroupfsPrefix)
|
||||
if len(relPath) == len(path) { // non-standard path, old system?
|
||||
relPath, ok := strings.CutPrefix(path, cgroupfsPrefix)
|
||||
if !ok { // Non-standard path, old system?
|
||||
return openFallback(path, flags, mode)
|
||||
}
|
||||
|
||||
|
||||
@@ -104,9 +104,7 @@ func waitFrozen(dirPath string) (cgroups.FreezerState, error) {
|
||||
if i == maxIter {
|
||||
return cgroups.Undefined, fmt.Errorf("timeout of %s reached waiting for the cgroup to freeze", waitTime*maxIter)
|
||||
}
|
||||
line := scanner.Text()
|
||||
val := strings.TrimPrefix(line, "frozen ")
|
||||
if val != line { // got prefix
|
||||
if val, ok := strings.CutPrefix(scanner.Text(), "frozen "); ok {
|
||||
if val[0] == '1' {
|
||||
return cgroups.Frozen, nil
|
||||
}
|
||||
|
||||
@@ -61,8 +61,7 @@ func DetectUID() (int, error) {
|
||||
scanner := bufio.NewScanner(bytes.NewReader(b))
|
||||
for scanner.Scan() {
|
||||
s := strings.TrimSpace(scanner.Text())
|
||||
if strings.HasPrefix(s, "OwnerUID=") {
|
||||
uidStr := strings.TrimPrefix(s, "OwnerUID=")
|
||||
if uidStr, ok := strings.CutPrefix(s, "OwnerUID="); ok {
|
||||
i, err := strconv.Atoi(uidStr)
|
||||
if err != nil {
|
||||
return -1, fmt.Errorf("could not detect the OwnerUID: %w", err)
|
||||
|
||||
@@ -332,8 +332,8 @@ func getHugePageSizeFromFilenames(fileNames []string) ([]string, error) {
|
||||
|
||||
for _, file := range fileNames {
|
||||
// example: hugepages-1048576kB
|
||||
val := strings.TrimPrefix(file, "hugepages-")
|
||||
if len(val) == len(file) {
|
||||
val, ok := strings.CutPrefix(file, "hugepages-")
|
||||
if !ok {
|
||||
// Unexpected file name: no prefix found, ignore it.
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ func rootlessEUIDMount(config *configs.Config) error {
|
||||
// Check that the options list doesn't contain any uid= or gid= entries
|
||||
// that don't resolve to root.
|
||||
for _, opt := range strings.Split(mount.Data, ",") {
|
||||
if str := strings.TrimPrefix(opt, "uid="); len(str) < len(opt) {
|
||||
if str, ok := strings.CutPrefix(opt, "uid="); ok {
|
||||
uid, err := strconv.Atoi(str)
|
||||
if err != nil {
|
||||
// Ignore unknown mount options.
|
||||
@@ -65,9 +65,9 @@ func rootlessEUIDMount(config *configs.Config) error {
|
||||
if _, err := config.HostUID(uid); err != nil {
|
||||
return fmt.Errorf("cannot specify uid=%d mount option for rootless container: %w", uid, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if str := strings.TrimPrefix(opt, "gid="); len(str) < len(opt) {
|
||||
if str, ok := strings.CutPrefix(opt, "gid="); ok {
|
||||
gid, err := strconv.Atoi(str)
|
||||
if err != nil {
|
||||
// Ignore unknown mount options.
|
||||
|
||||
@@ -685,8 +685,8 @@ func initSystemdProps(spec *specs.Spec) ([]systemdDbus.Property, error) {
|
||||
var sp []systemdDbus.Property
|
||||
|
||||
for k, v := range spec.Annotations {
|
||||
name := strings.TrimPrefix(k, keyPrefix)
|
||||
if len(name) == len(k) { // prefix not there
|
||||
name, ok := strings.CutPrefix(k, keyPrefix)
|
||||
if !ok { // prefix not there
|
||||
continue
|
||||
}
|
||||
if err := checkPropertyName(name); err != nil {
|
||||
|
||||
@@ -88,8 +88,8 @@ func stripRoot(root, path string) string {
|
||||
func SearchLabels(labels []string, key string) (string, bool) {
|
||||
key += "="
|
||||
for _, s := range labels {
|
||||
if strings.HasPrefix(s, key) {
|
||||
return s[len(key):], true
|
||||
if val, ok := strings.CutPrefix(s, key); ok {
|
||||
return val, true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
|
||||
Reference in New Issue
Block a user