// checkDirectorySingle is check directory using single goroutine func checkDirectorySingle(basePath string, extensions []string, ch chan<- string, closeFlg bool) { // read directory fis, err := ioutil.ReadDir(basePath) if err != nil { //fmt.Printf("error : %s\n", err) return } for _, fi := range fis { //fmt.Printf("file name is %s\n", fi.Name()) if reg.IsInvisiblefile(fi.Name()) { continue } fullPath := filepath.Join(basePath, fi.Name()) //fmt.Printf("full path is %s\n", fullPath) if fi.IsDir() { //fmt.Println("this is directory. skip.") //check more deep directory checkDirectorySingle(fullPath, extensions, ch, false) } else { for _, ex := range extensions { //fmt.Printf("search %s from %s\n", ex, fi.Name()) if reg.IsExtFile(fi.Name(), ex) { ch <- fullPath } } } } if closeFlg { close(ch) } }
// checkDirectoryJIC is check directory without goroutine func checkDirectoryJIC(basePath string, extensions []string) { // read directory fis, err := ioutil.ReadDir(basePath) if err != nil { //fmt.Printf("error : %s\n", err) return } for _, fi := range fis { //fmt.Printf("file name is %s\n", fi.Name()) if reg.IsInvisiblefile(fi.Name()) { continue } fullPath := filepath.Join(basePath, fi.Name()) //fmt.Printf("full path is %s\n", fullPath) if fi.IsDir() { //fmt.Println("this is directory. skip.") //check more deep directory checkDirectoryJIC(fullPath, extensions) } else { for _, ex := range extensions { //fmt.Printf("search %s from %s\n", ex, fi.Name()) if reg.IsExtFile(fi.Name(), ex) { fileNames = append(fileNames, fullPath) } } } } }
// checkDirectory is to check directory using goroutine as semaphore func checkDirectory(basePath string, extensions []string, ch chan<- string, chSmp chan bool, wg *sync.WaitGroup, closeFlg bool) { // read directory fis, err := ioutil.ReadDir(basePath) if err != nil { //fmt.Printf("error : %s\n", err) return } for _, fi := range fis { //fmt.Printf("file name is %s\n", fi.Name()) if reg.IsInvisiblefile(fi.Name()) { continue } fullPath := filepath.Join(basePath, fi.Name()) //fmt.Printf("full path is %s\n", fullPath) if fi.IsDir() { wg.Add(1) chSmp <- true //fmt.Println("this is directory. skip.") //check more deep directory go func() { defer func() { <-chSmp wg.Done() }() checkDirectory(fullPath, extensions, ch, chSmp, wg, false) }() } else { for _, ex := range extensions { //fmt.Printf("search %s from %s\n", ex, fi.Name()) if reg.IsExtFile(fi.Name(), ex) { ch <- fullPath } } } } if closeFlg { wg.Wait() close(ch) } }