func followSymLink(name string) (os.FileInfo, error) { link, err := filepath.EvalSymlinks(name) if err != nil { myDebug.Error(err) return nil, err } fi, err1 := os.Lstat(link) if err1 != nil { myDebug.Error(err1) return nil, err1 } if (fi.Mode() & os.ModeSymlink) != 0 { return followSymLink(link) } else { return fi, nil } }
func SearchDir(dirName string, pattern string) []string { var files []string var directories []string directories = append(directories, dirName) counter := 0 for { if counter == len(directories) { break } dirent := strings.TrimSuffix(directories[counter], "/") + "/" counter++ subfiles, err := ioutil.ReadDir(dirent) if err != nil { myDebug.Error(err) continue } for _, entries := range subfiles { name := dirent + entries.Name() if (entries.Mode() & os.ModeSymlink) != 0 { entries, err = followSymLink(name) if err != nil { continue } } switch { case entries.IsDir(): directories = append(directories, name) case entries.Mode().IsRegular(): if strings.Contains(name, pattern) { files = append(files, name) } } } } return files }