Skip to content

Commit f94cf87

Browse files
committed
libct: use strings.CutPrefix where possible
Using strings.CutPrefix (available since Go 1.20) makes the code a tad more straightforward. No functional change. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 2e9bc48 commit f94cf87

File tree

7 files changed

+12
-16
lines changed

7 files changed

+12
-16
lines changed

libcontainer/cgroups/devices/systemd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,7 @@ func findDeviceGroup(ruleType devices.Type, ruleMajor int64) (string, error) {
216216
continue
217217
}
218218

219-
group := strings.TrimPrefix(line, ruleMajorStr)
220-
if len(group) < len(line) { // got it
219+
if group, ok := strings.CutPrefix(line, ruleMajorStr); ok {
221220
return prefix + group, nil
222221
}
223222
}

libcontainer/cgroups/file.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ func openFile(dir, file string, flags int) (*os.File, error) {
151151
if prepareOpenat2() != nil {
152152
return openFallback(path, flags, mode)
153153
}
154-
relPath := strings.TrimPrefix(path, cgroupfsPrefix)
155-
if len(relPath) == len(path) { // non-standard path, old system?
154+
relPath, ok := strings.CutPrefix(path, cgroupfsPrefix)
155+
if !ok { // Non-standard path, old system?
156156
return openFallback(path, flags, mode)
157157
}
158158

libcontainer/cgroups/fs2/freezer.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ func waitFrozen(dirPath string) (cgroups.FreezerState, error) {
104104
if i == maxIter {
105105
return cgroups.Undefined, fmt.Errorf("timeout of %s reached waiting for the cgroup to freeze", waitTime*maxIter)
106106
}
107-
line := scanner.Text()
108-
val := strings.TrimPrefix(line, "frozen ")
109-
if val != line { // got prefix
107+
if val, ok := strings.CutPrefix(scanner.Text(), "frozen "); ok {
110108
if val[0] == '1' {
111109
return cgroups.Frozen, nil
112110
}

libcontainer/cgroups/systemd/user.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ func DetectUID() (int, error) {
6161
scanner := bufio.NewScanner(bytes.NewReader(b))
6262
for scanner.Scan() {
6363
s := strings.TrimSpace(scanner.Text())
64-
if strings.HasPrefix(s, "OwnerUID=") {
65-
uidStr := strings.TrimPrefix(s, "OwnerUID=")
64+
if uidStr, ok := strings.CutPrefix(s, "OwnerUID="); ok {
6665
i, err := strconv.Atoi(uidStr)
6766
if err != nil {
6867
return -1, fmt.Errorf("could not detect the OwnerUID: %w", err)

libcontainer/cgroups/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,8 @@ func getHugePageSizeFromFilenames(fileNames []string) ([]string, error) {
332332

333333
for _, file := range fileNames {
334334
// example: hugepages-1048576kB
335-
val := strings.TrimPrefix(file, "hugepages-")
336-
if len(val) == len(file) {
335+
val, ok := strings.CutPrefix(file, "hugepages-")
336+
if !ok {
337337
// Unexpected file name: no prefix found, ignore it.
338338
continue
339339
}

libcontainer/configs/validate/rootless.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func rootlessEUIDMount(config *configs.Config) error {
5656
// Check that the options list doesn't contain any uid= or gid= entries
5757
// that don't resolve to root.
5858
for _, opt := range strings.Split(mount.Data, ",") {
59-
if str := strings.TrimPrefix(opt, "uid="); len(str) < len(opt) {
59+
if str, ok := strings.CutPrefix(opt, "uid="); ok {
6060
uid, err := strconv.Atoi(str)
6161
if err != nil {
6262
// Ignore unknown mount options.
@@ -65,9 +65,9 @@ func rootlessEUIDMount(config *configs.Config) error {
6565
if _, err := config.HostUID(uid); err != nil {
6666
return fmt.Errorf("cannot specify uid=%d mount option for rootless container: %w", uid, err)
6767
}
68+
continue
6869
}
69-
70-
if str := strings.TrimPrefix(opt, "gid="); len(str) < len(opt) {
70+
if str, ok := strings.CutPrefix(opt, "gid="); ok {
7171
gid, err := strconv.Atoi(str)
7272
if err != nil {
7373
// Ignore unknown mount options.

libcontainer/specconv/spec_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,8 +685,8 @@ func initSystemdProps(spec *specs.Spec) ([]systemdDbus.Property, error) {
685685
var sp []systemdDbus.Property
686686

687687
for k, v := range spec.Annotations {
688-
name := strings.TrimPrefix(k, keyPrefix)
689-
if len(name) == len(k) { // prefix not there
688+
name, ok := strings.CutPrefix(k, keyPrefix)
689+
if !ok { // prefix not there
690690
continue
691691
}
692692
if err := checkPropertyName(name); err != nil {

0 commit comments

Comments
 (0)