Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use os.ReadDir for lightweight directory reading #2346

Merged
merged 3 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions internal/metrics/collectors/processes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package collectors
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -45,7 +47,7 @@ func getWorkerProcesses() (int, int, error) {
var workerProcesses int
var prevWorkerProcesses int

procFolders, err := ioutil.ReadDir("/proc")
procFolders, err := os.ReadDir("/proc")
if err != nil {
return 0, 0, fmt.Errorf("unable to read directory /proc : %w", err)
}
Expand All @@ -56,8 +58,11 @@ func getWorkerProcesses() (int, int, error) {
continue
}

cmdlineFile := fmt.Sprintf("/proc/%v/cmdline", folder.Name())
content, err := ioutil.ReadFile(cmdlineFile)
cmdlineFile := filepath.Clean(fmt.Sprintf("/proc/%v/cmdline", folder.Name()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Juneezee

could you possibly clarify why it is necessary to use filepath.Clean ?

Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pleshakov Hi, thanks for reviewing this PR.

filepath.Clean is necessary here to address the linting error, see this comment #2346 (comment)

if !strings.HasPrefix(cmdlineFile, "/proc/") {
panic(fmt.Errorf("unsafe input"))
}
content, err := os.ReadFile(cmdlineFile)
Juneezee marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return 0, 0, fmt.Errorf("unable to read file %v: %w", cmdlineFile, err)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/metrics/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package metrics

import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -75,7 +74,7 @@ func runServer(port string, registry prometheus.Gatherer, prometheusSecret *api_
}

func writeTempFile(data []byte, name string) (*os.File, error) {
f, err := ioutil.TempFile("", name)
f, err := os.CreateTemp("", name)
if err != nil {
return nil, fmt.Errorf("failed to create temp file: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/nginx/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package nginx

import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -265,7 +264,7 @@ func (lm *LocalManager) DeleteAppProtectResourceFile(name string) {

// ClearAppProtectFolder clears contents of a config folder
func (lm *LocalManager) ClearAppProtectFolder(name string) {
files, err := ioutil.ReadDir(name)
files, err := os.ReadDir(name)
if err != nil {
glog.Fatalf("Failed to read the App Protect folder %s: %v", name, err)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/nginx/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package nginx
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -51,7 +50,7 @@ func createFileAndWrite(name string, b []byte) error {
}

func createFileAndWriteAtomically(filename string, tempPath string, mode os.FileMode, content []byte) {
file, err := ioutil.TempFile(tempPath, path.Base(filename))
file, err := os.CreateTemp(tempPath, path.Base(filename))
if err != nil {
glog.Fatalf("Couldn't create a temp file for the file %v: %v", filename, err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/nginx/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"fmt"
"html/template"
"io/ioutil"
"io"
"net"
"net/http"
"strconv"
Expand Down Expand Up @@ -47,7 +47,7 @@ func (c *verifyClient) GetConfigVersion() (int, error) {
return 0, fmt.Errorf("non-200 response: %v", resp.StatusCode)
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return 0, fmt.Errorf("failed to read the response body: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/nginx/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package nginx

import (
"bytes"
"io/ioutil"
"io"
"net/http"
"strings"
"testing"
Expand All @@ -14,7 +14,7 @@ type Transport struct{}
func (c Transport) RoundTrip(_ *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString("42")),
Body: io.NopCloser(bytes.NewBufferString("42")),
Header: make(http.Header),
}, nil
}
Expand Down