logo

go-deblob

remove binary blobs from a directory
commit: d5db16740fa88a1696fe99ae02f3b63abb676298
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Wed, 11 Dec 2019 02:46:31 +0100

Initial commit

Diffstat:

A.gitignore2++
Ago-deblob.go90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ago.mod3+++
3 files changed, 95 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +/go-deblob+ \ No newline at end of file diff --git a/go-deblob.go b/go-deblob.go @@ -0,0 +1,90 @@ +package main + +import ( + "bytes" + "fmt" + "io" + "log" + "os" + "path" +) + +func isBlob(filename string) (result bool) { + result = false + // 512 is larger than needed but it's one physical block size + header := make([]byte, 512) + + file, err := os.Open(filename) + if err != nil { + log.Printf("os.Open(%s): %s\n", filename, err) + return + } + + defer file.Close() + + _, err = file.Read(header) + if err != nil && err != io.EOF { + log.Printf("(%s) os.Read: %s\n", filename, err) + return + } + + if bytes.HasPrefix(header, []byte("\x7fELF")) || bytes.HasPrefix(header, []byte("!<arch>\x0a")) { + result = true + } + + return +} + +func checkDir(dirname string) { + dirFd, err := os.Open(dirname) + if err != nil { + log.Printf("os.Open(%s): %s\n", dirname, err) + return + } + + files, err := dirFd.Readdir(0) + if err != nil { + log.Printf("(%s) os.Readdir: %v\n", dirname, err) + return + } + for _, file := range files { + filename := path.Join(dirname, file.Name()) + + if file.IsDir() { + checkDir(filename) + } else if isBlob(filename) { + fmt.Println(filename) + err = os.Remove(filename) + if err != nil { + log.Printf("os.Remove(%s): %s\n", filename, err) + } + } + } +} + +func main() { + if len(os.Args) != 2 { + fmt.Printf("Usage: go-deblog [workdir containing blobs]\n") + os.Exit(1) + } + + workdir := os.Args[1] + + log.SetFlags(log.Lshortfile) + + dirinfo, err := os.Stat(workdir) + if err != nil { + log.Printf("os.Stat(%s): %s\n", workdir, err) + os.Exit(1) + } + if !dirinfo.IsDir() { + fmt.Printf("Error: %s is not a directory\n", workdir) + os.Exit(1) + } + + fmt.Printf(":: Checking for blobs, removed:\n") + + checkDir(workdir) + + fmt.Printf(":: Done checking for blobs\n") +} diff --git a/go.mod b/go.mod @@ -0,0 +1,3 @@ +module hacktivis.me/git/go-deblob + +go 1.12