aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2026-01-09 00:37:21 +0700
committerShulhan <ms@kilabit.info>2026-01-09 00:38:08 +0700
commit34693c1b7ab126e1fbda3810eb46b735c350505c (patch)
treef2437def68eb031c9482d409804941edf70c8c62
parent0fde383e8f30a0d7504e7987f6a3ebe6a082d69a (diff)
downloadpakakeh.go-34693c1b7ab126e1fbda3810eb46b735c350505c.tar.xz
lib/os: add function IsBinaryStream
The IsBinaryStream return true if the content has more than 75% non-printable characters, excluding spaces. While at it, replace the body of IsBinary with it and update the test cases to use the internal files.
-rw-r--r--lib/os/example_test.go9
-rw-r--r--lib/os/os.go73
-rw-r--r--lib/os/os_test.go26
3 files changed, 62 insertions, 46 deletions
diff --git a/lib/os/example_test.go b/lib/os/example_test.go
index 574490a6..5fc15488 100644
--- a/lib/os/example_test.go
+++ b/lib/os/example_test.go
@@ -1,6 +1,5 @@
-// Copyright 2023, Shulhan <ms@kilabit.info>. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
+// SPDX-License-Identifier: BSD-3-Clause
+// SPDX-FileCopyrightText: 2023 M. Shulhan <ms@kilabit.info>
package os_test
@@ -22,8 +21,8 @@ func ExampleEnvironments() {
}
func ExampleIsBinary() {
- fmt.Println(libos.IsBinary("/bin/bash"))
- fmt.Println(libos.IsBinary("io.go"))
+ fmt.Println(libos.IsBinary(`testdata/exp.bz2`))
+ fmt.Println(libos.IsBinary(`os.go`))
// Output:
// true
// false
diff --git a/lib/os/os.go b/lib/os/os.go
index 3d6cbce9..24946792 100644
--- a/lib/os/os.go
+++ b/lib/os/os.go
@@ -1,6 +1,5 @@
-// SPDX-FileCopyrightText: 2022 M. Shulhan <ms@kilabit.info>
-//
// SPDX-License-Identifier: BSD-3-Clause
+// SPDX-FileCopyrightText: 2022 M. Shulhan <ms@kilabit.info>
// Package os extend the standard os package to provide additional
// functionalities.
@@ -145,56 +144,54 @@ func Environments() (envs map[string]string) {
return envs
}
-// IsBinary will return true if content of file is binary.
+// IsBinary return true if the content has more than 75% non-printable
+// characters, excluding spaces.
// If file is not exist or there is an error when reading or closing the file,
// it will return false.
-func IsBinary(file string) bool {
+func IsBinary(file string) (is bool) {
var (
- f *os.File
- err error
- total int
- printable int
+ f *os.File
+ err error
)
-
f, err = os.Open(file)
if err != nil {
return false
}
- var (
- content = make([]byte, 768)
-
- n int
- x int
- )
-
- for total < 512 {
- n, err = f.Read(content)
- if err != nil {
- break
- }
-
- content = content[:n]
-
- for x = range len(content) {
- if ascii.IsSpace(content[x]) {
- continue
- }
- if content[x] >= 33 && content[x] <= 126 {
- printable++
- }
- total++
- }
+ is = true
+ content := make([]byte, 1024)
+ _, err = f.Read(content)
+ if err != nil {
+ is = false
}
-
err = f.Close()
if err != nil {
- return false
+ is = false
}
+ if !is {
+ return is
+ }
+ return IsBinaryStream(content)
+}
- var ratio = float64(printable) / float64(total)
-
- return ratio <= float64(0.75)
+// IsBinaryStream return true if the content has more than 75% non-printable
+// characters, excluding spaces.
+func IsBinaryStream(content []byte) bool {
+ var (
+ total int
+ printable int
+ )
+ for x := 0; total < 512 && x < len(content); x++ {
+ if ascii.IsSpace(content[x]) {
+ continue
+ }
+ if content[x] >= 33 && content[x] <= 126 {
+ printable++
+ }
+ total++
+ }
+ ratio := float64(printable) / float64(total)
+ return ratio <= 0.75
}
// IsDirEmpty will return true if directory is not exist or empty; otherwise
diff --git a/lib/os/os_test.go b/lib/os/os_test.go
index 0bacb67a..88c420c5 100644
--- a/lib/os/os_test.go
+++ b/lib/os/os_test.go
@@ -1,6 +1,5 @@
-// Copyright 2022, Shulhan <ms@kilabit.info>. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
+// SPDX-License-Identifier: BSD-3-Clause
+// SPDX-FileCopyrightText: 2022 M. Shulhan <ms@kilabit.info>
package os
@@ -116,6 +115,27 @@ func TestCopy(t *testing.T) {
}
}
+func TestIsBinaryStream(t *testing.T) {
+ listCase := []struct {
+ path string
+ exp bool
+ }{{
+ path: `testdata/exp.bz2`,
+ exp: true,
+ }, {
+ path: `os.go`,
+ exp: false,
+ }}
+ for _, tc := range listCase {
+ content, err := os.ReadFile(tc.path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ got := IsBinaryStream(content)
+ test.Assert(t, tc.path, tc.exp, got)
+ }
+}
+
func TestIsDirEmpty(t *testing.T) {
emptyDir := "testdata/dirempty"
err := os.MkdirAll(emptyDir, 0700)