Created
May 14, 2020 16:40
-
-
Save selfup/5a8395e5eaa1194206b54cc5b60b0545 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"runtime" | |
"strings" | |
"sync" | |
) | |
func main() { | |
var paths string | |
flag.StringVar(&paths, "p", "", "any absolute path bu comma delimited. Example: '/tmp,/etc/,.") | |
var keywords string | |
flag.StringVar(&keywords, "k", "", "keywords to start scanning recursively from") | |
flag.Parse() | |
scanKeywords := strings.Split(keywords, ",") | |
scanPaths := strings.Split(paths, ",") | |
nfsf := NewFileSizeFinder(scanKeywords) | |
for _, path := range scanPaths { | |
nfsf.Scan(path) | |
} | |
for _, file := range nfsf.Files { | |
fmt.Println(file) | |
} | |
} | |
// FileSizeFinder struct contains needed data to perform concurrent operations | |
type FileSizeFinder struct { | |
mutex sync.Mutex | |
Direction string | |
Files []string | |
Keywords []string | |
} | |
// NewFileSizeFinder creates a pointer to FileSizeFinder with default values | |
func NewFileSizeFinder(keywords []string) *FileSizeFinder { | |
fsf := new(FileSizeFinder) | |
if runtime.GOOS == "windows" { | |
fsf.Direction = "\\" | |
} else { | |
fsf.Direction = "/" | |
} | |
fsf.Keywords = keywords | |
return fsf | |
} | |
// Scan is a concurrent/parallel directory walker | |
func (f *FileSizeFinder) Scan(directory string) { | |
_, err := ioutil.ReadDir(directory) | |
if err != nil { | |
panic(err) | |
} | |
f.findFiles(directory, "") | |
} | |
func (f *FileSizeFinder) findFiles(directory string, prefix string) { | |
paths, _ := ioutil.ReadDir(directory) | |
var dirs []os.FileInfo | |
var files []os.FileInfo | |
for _, path := range paths { | |
if path.IsDir() { | |
dirs = append(dirs, path) | |
} else { | |
files = append(files, path) | |
} | |
} | |
for _, file := range files { | |
for _, keyword := range f.Keywords { | |
if strings.Contains(file.Name(), keyword) { | |
f.mutex.Lock() | |
f.Files = append(f.Files, directory+f.Direction+file.Name()) | |
f.mutex.Unlock() | |
} | |
} | |
} | |
dirLen := len(dirs) | |
if dirLen > 0 { | |
var dirGroup sync.WaitGroup | |
dirGroup.Add(dirLen) | |
for _, dir := range dirs { | |
go func(diR os.FileInfo, direcTory string, direcTion string) { | |
f.findFiles(direcTory+direcTion+diR.Name(), direcTory) | |
dirGroup.Done() | |
}(dir, directory, f.Direction) | |
} | |
dirGroup.Wait() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment