Skip to content

Commit

Permalink
libct: use strings.CutPrefix where possible
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
kolyshkin committed Jan 10, 2025
1 parent 480792b commit 05f53d6
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 18 deletions.
3 changes: 1 addition & 2 deletions libcontainer/cgroups/devices/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,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
}
}
Expand Down
4 changes: 2 additions & 2 deletions libcontainer/cgroups/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 1 addition & 3 deletions libcontainer/cgroups/fs2/freezer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions libcontainer/cgroups/systemd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions libcontainer/cgroups/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions libcontainer/configs/validate/rootless.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions libcontainer/specconv/spec_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions libcontainer/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 05f53d6

Please sign in to comment.