Skip to content

Commit

Permalink
optimize this function to run faster
Browse files Browse the repository at this point in the history
Signed-off-by: Rumen Vasilev <[email protected]>
  • Loading branch information
rumenvasilev committed Nov 11, 2023
1 parent 6133a3d commit 34e8170
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 47 deletions.
49 changes: 8 additions & 41 deletions internal/util/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,56 +212,23 @@ func GetSignatureFiles(dir string) ([]string, error) {
}

func IsBinaryFile(path string) bool {
buf, err := readToBuffer(path)
if err != nil {
// couldn't read file, exit with false
return false
}

// https://groups.google.com/g/golang-nuts/c/YeLL7L7SwWs/m/LGlsc9GIJlUJ
// if the encoding is invalid, it returns (RuneError, 1)
runerr, res := utf8.DecodeRune(buf)
return (res == 1 && runerr == utf8.RuneError)
}

func readToBuffer(path string) ([]byte, error) {
buf := make([]byte, 512)

f, err := os.Open(path)
if err != nil {
return nil, err
// couldn't open file, exit with false
return false
}
defer f.Close()

_, err = f.Read(buf)
if err != nil {
return nil, err
// couldn't read file, exit with false
return false
}

// for {
// fmt.Println("run")
// // if len(buf) >= cap(buf) {
// // fmt.Println("Length:", len(buf), "Capacity:", cap(buf))
// // break
// // }
// n, err := f.Read(buf)
// if err == io.EOF {
// break
// }
// if err != nil {
// continue
// }
// fmt.Println(n)
// if n == 512 {
// break
// }
// }

return buf, nil
// https://groups.google.com/g/golang-nuts/c/YeLL7L7SwWs/m/LGlsc9GIJlUJ
// if the encoding is invalid, it returns (RuneError, 1)
runerr, res := utf8.DecodeRune(buf)
return (res == 1 && runerr == utf8.RuneError)
}

// f, err := Open(name)
// if err != nil {
// return nil, err
// }
// defer f.Close()
39 changes: 33 additions & 6 deletions internal/util/io_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package util

import (
"fmt"
"os"
"strings"
"testing"

. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFileExists(t *testing.T) {
Expand Down Expand Up @@ -40,13 +44,36 @@ func TestFileExists(t *testing.T) {

func Test_IsBinaryFile(t *testing.T) {
assert.False(t, IsBinaryFile("./strings.go"))
assert.True(t, IsBinaryFile("../../bin/rvsecret-darwin"))
bin := findBinary(t)
require.NotEmpty(t, bin, "Make sure you've run `make build` before running this test. It relies on the binary being present in bin directory.")
assert.True(t, IsBinaryFile(fmt.Sprintf("../../bin/%s", bin)))
assert.False(t, IsBinaryFile("some/unexisting/file"))
}

func Test_readToBuffer(t *testing.T) {
t.Skip()
res, err := readToBuffer("rvsecret")
assert.NoError(t, err)
assert.Equal(t, "", string(res))
// finds the pre-built binary under the name <root>/bin/rvsecret*
func findBinary(t *testing.T) string {
cwd, err := os.Getwd()
require.NoError(t, err)
sp := strings.Split(cwd, "/")
root := strings.Join(sp[:len(sp)-2], "/")
dir, err := os.ReadDir(fmt.Sprintf("%s/bin", root))
require.NoError(t, err, "Please run `make build` before running this test.")
binaryName := ""
for _, entry := range dir {
if entry.IsDir() {
continue
} else if entry.Type().IsRegular() {
if strings.Contains(entry.Name(), "rvsecret") {
binaryName = entry.Name()
break
}
}
}
return binaryName
}

func BenchmarkIsBinaryFile(b *testing.B) {
for n := 0; n < b.N; n++ {
IsBinaryFile("../../bin/rvsecret-darwin")
}
}

0 comments on commit 34e8170

Please sign in to comment.