commit: 4d3f4ced2f4b1df31290267232d543239d2b6779
parent: da83e65d57778428d3174272e67189a4f2fcbfc2
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Thu, 12 Dec 2019 00:42:21 +0100
Add exclusion feature
Diffstat:
2 files changed, 58 insertions(+), 14 deletions(-)
diff --git a/go-deblob.1 b/go-deblob.1
@@ -9,15 +9,24 @@
.Nd remove binary blobs from a directory
.Sh SYNOPSIS
.Nm
-.Ar directory
+.Op Fl e Ar excluded paths
+.Op Fl d Ar working directory
.Sh DESCRIPTION
-Done for cleaning a tarball extracted at
-.Ar directory
-from binary blobs, to be sure that a package actually builds from source. Scanning for blobs inside source files is considered to be out-of-scope for now as
+The
+.Nm
+utility cleans a directory from binary blobs, for instance an extracted source tarball to be sure that it fully builds from source. Scanning for blobs inside source files is considered to be out-of-scope for now as
.Lk https://www.fsfla.org/svn/fsfla/software/linux-libre/scripts/ linux-libre deblob script
already exists.
.Pp
-For now it only scans for:
+The options are as follows:
+.Bl -tag -width Ds
+.It Fl e Ar excluded paths
+Paths to be excluded from the scan, accepts shell globbing with Golang path.Match. Pass the option multiple times to do multiple exclusions.
+.It Fl d Ar working directory
+Directory to be scanned rather than the current working directory on execution.
+.El
+.Pp
+Blobs scanned against are the following:
.Bl -dash -compact
.It
ELF files by the magic-header
diff --git a/go-deblob.go b/go-deblob.go
@@ -6,12 +6,15 @@ package main
import (
"bytes"
"fmt"
+ "git.sr.ht/~sircmpwn/getopt"
"io"
"log"
"os"
"path"
)
+var excludes []string
+
func isBlob(filename string) (result bool) {
result = false
// 512 is larger than needed but it's one physical block size
@@ -31,13 +34,24 @@ func isBlob(filename string) (result bool) {
return
}
- if bytes.HasPrefix(header, []byte("\x7fELF")) || bytes.HasPrefix(header, []byte("!<arch>\x0a")) {
+ if bytes.HasPrefix(header, []byte("\x7fELF")) || bytes.HasPrefix(header, []byte("!<arch>\n")) {
result = true
}
return
}
+func is_excluded(filename string) bool {
+ for _, exclude := range excludes {
+ matched, err := path.Match(exclude, filename)
+ if matched {
+ return true
+ }
+ }
+
+ return false
+}
+
func checkDir(dirname string) {
dirFd, err := os.Open(dirname)
if err != nil {
@@ -52,11 +66,15 @@ func checkDir(dirname string) {
}
for _, file := range files {
filename := path.Join(dirname, file.Name())
+ if is_excluded(filename) {
+ fmt.Printf("! %s", filename)
+ continue
+ }
if file.IsDir() {
checkDir(filename)
} else if isBlob(filename) {
- fmt.Println(filename)
+ fmt.Printf("- %s", filename)
err = os.Remove(filename)
if err != nil {
log.Printf("os.Remove(%s): %s\n", filename, err)
@@ -65,15 +83,32 @@ func checkDir(dirname string) {
}
}
-func main() {
- if len(os.Args) != 2 {
- fmt.Printf("Usage: go-deblog [directory]\n")
- os.Exit(1)
- }
-
- workdir := os.Args[1]
+func usage() {
+ println("Usage: go-deblob [-e exclude] [-d workdir]")
+ println(" -e Exclude filepath from scanning/removal (defaults to none)")
+ println(" -d Set working directory (defaults to current dir)")
+ println("See `man 1 go-deblob` for more information.")
+}
+func main() {
log.SetFlags(log.Lshortfile)
+ workdir := "."
+
+ opts, optind, err := getopt.Getopts(os.Args, "e:d:")
+ if err != nil {
+ log.Fatal(err)
+ }
+ for _, opt := range opts {
+ switch opt.Option {
+ case 'e':
+ append(excludes, opt.Value)
+ case 'd':
+ workdir = opt.Value
+ default:
+ usage()
+ os.Exit(1)
+ }
+ }
dirinfo, err := os.Stat(workdir)
if err != nil {