Hello. I’m a Golang noobie and this is one of my first apps.
The goal is to walk through an entire file system and find all the files that are world-writable. The script should take less than 5 minutes. This code works but we have some really large filesystems which can take up to 10 minutes to walk.
Is there a better way to check for the file mode and is there a most efficient method of doing the checks? Would there be any benefit to using concurrency?
// FilePathWalkDir accepts a root dir and then returns a
// string array of files that are world writable by everyone.
func FilePathWalkDir() ([]string, error) {
var files []string
err := filepath.WalkDir("/", func(path string, d os.DirEntry, err error) error {
// If the path is a directory and one of the skip dirs then skip it.
if d.IsDir() {
switch skipPath := path; skipPath {
case "/proc":
return filepath.SkipDir
case "/run":
return filepath.SkipDir
case "/sys/fs":
return filepath.SkipDir
case "/selinux":
return filepath.SkipDir
}
}
// If the type of entry is a regular file then get the permissions and if it's world writable
// add it to the files array.
if d.Type().IsRegular() {
info, _ := d.Info()
s := string(info.Mode().String()[8])
matched, _ := regexp.MatchString(`w`, s)
if matched {
files = append(files, path)
}
}
return nil
})
// return the files array
return files, err
}