Skip to content

Commit

Permalink
Make readuint return maxInt when file has max value.
Browse files Browse the repository at this point in the history
This brings the behavior closer to getStatFileContentUint64
in cgroups1.

Signed-off-by: Manu Gupta <manugupt1@gmail.com>
  • Loading branch information
manugupt1 committed May 2, 2023
1 parent 62f3fd5 commit 83e12f7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cgroup1/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cgroup1

import (
"bufio"
"bytes"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -137,13 +138,19 @@ func readUint(path string) (uint64, error) {
}
defer f.Close()

b := make([]byte, 32) // Chose 32 as some files have leading/trailing whitespaces and alignment
// We should only need 20 bytes for the max uint64, but for a nice power of 2
// lets use 32.
b := make([]byte, 32)
n, err := f.Read(b)
if err != nil {
return 0, err
}

return parseUint(strings.TrimSpace(string(b[:n])), 10, 64)
s := string(bytes.TrimSpace(b[:n]))
if s == "max" {
// Return 0 for the max value to maintain backward compatibility.
return 0, nil
}
return parseUint(s, 10, 64)
}

func parseUint(s string, base, bitSize int) (uint64, error) {
Expand Down
19 changes: 19 additions & 0 deletions cgroup1/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package cgroup1

import (
"os"
"path/filepath"
"testing"
)

Expand All @@ -30,3 +32,20 @@ func BenchmarkReaduint64(b *testing.B) {
}
}
}

func TestReadUint(t *testing.T) {
tDir := t.TempDir()
pidsmax := filepath.Join(tDir, "pids.max")
err := os.WriteFile(pidsmax, []byte("max"), 0644)
if err != nil {
t.Fatal(err)
}
max, err := readUint(pidsmax)
if err != nil {
t.Fatal(err)
}
// test for backwards compatibility
if max != 0 {
t.Fail()
}
}

0 comments on commit 83e12f7

Please sign in to comment.