logo

go-deblob

remove binary blobs from a directory git clone https://hacktivis.me/git/go-deblob.git
commit: 60f04634e354d939c2d4c1f8cf60954aa440e50a
parent c89ed0e805813d5482863b53e36b6c896792a6fd
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Wed, 26 May 2021 03:25:52 +0200

Make -e only exclude from removing, not scanning

Diffstat:

Mgo-deblob.12+-
Mgo-deblob.go24+++++++++++++++---------
Mgo-deblob_test.go2+-
3 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/go-deblob.1 b/go-deblob.1 @@ -24,7 +24,7 @@ The options are as follows: .It Fl n Scan the files but do not actually delete them. .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. +Paths to be excluded from removal, 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 diff --git a/go-deblob.go b/go-deblob.go @@ -57,7 +57,7 @@ func is_excluded(filename string) bool { return false } -func checkDir(dirname string) { +func checkDir(dirname string, ignoring bool) { dirFd, err := os.Open(dirname) if err != nil { log.Printf("os.Open(%s): %s\n", dirname, err) @@ -71,18 +71,24 @@ func checkDir(dirname string) { } for _, file := range files { filename := path.Join(dirname, file.Name()) - if is_excluded(filename) { - fmt.Printf("ignoring: %s\n", filename) - continue + if !ignoring { + ignoring = is_excluded(filename) } if file.IsDir() { - checkDir(filename) + checkDir(filename, ignoring) } else if isBlob(filename) { - fmt.Printf("removing: %s\n", filename) + if ignoring { + fmt.Printf("ignoring: %s\n", filename) + continue + } + if noop { + fmt.Printf("detected: %s\n", filename) continue } + + fmt.Printf("removing: %s\n", filename) err = os.Remove(filename) if err != nil { log.Printf("os.Remove(%s): %s\n", filename, err) @@ -93,7 +99,7 @@ func checkDir(dirname string) { func usage() { println("Usage: go-deblob [-e exclude] [-d workdir]") - println(" -e Exclude filepath from scanning/removal (defaults to none)") + println(" -e Exclude filepath from removal (defaults to none)") println(" -d Set working directory (defaults to current dir)") println(" -n No actual removal, only scan and log") println("See `man 1 go-deblob` for more information.") @@ -127,9 +133,9 @@ func main() { os.Exit(1) } - fmt.Printf(":: Checking for blobs, removed:\n") + fmt.Printf(":: Checking for blobs\n") - checkDir(".") + checkDir(".", false) fmt.Printf(":: Done checking for blobs\n") } diff --git a/go-deblob_test.go b/go-deblob_test.go @@ -51,7 +51,7 @@ func TestCheckDir(t *testing.T) { return } - checkDir(dirname) + checkDir(dirname, false) err = filepath.Walk(dirname, func(path string, info os.FileInfo, err error) error { if err != nil {