github.com/shivakar/gdupes@v0.0.0-20180726052558-d5c070c306d0/gdupes/populate.go (about)

     1  package gdupes
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"sync"
     7  )
     8  
     9  // includeFileForProcessing checks fileinfo against the requested configuration.
    10  // Returns true if the file is to be included for processing, false otherwise
    11  func includeFileForProcessing(c *Config, f os.FileInfo) bool {
    12  	if c.NoHidden && f.Name()[:1] == "." {
    13  		return false
    14  	}
    15  	if c.NoEmpty && f.Size() == 0 {
    16  		return false
    17  	}
    18  	// If not a regular file (this includes directories and symlinks)
    19  	// don't add to the channel
    20  	if !f.Mode().IsRegular() {
    21  		return false
    22  	}
    23  	return true
    24  }
    25  
    26  // PopulateFiles recurses through the list of directories adding files to be
    27  // processed
    28  func PopulateFiles(c *Config, filesToProcess chan<- string, directories []string,
    29  	wg *sync.WaitGroup) {
    30  	defer wg.Done()
    31  	for _, d := range directories {
    32  		if c.Recurse {
    33  			err := filepath.Walk(d, func(path string, info os.FileInfo,
    34  				err error) error {
    35  				if err != nil {
    36  					return err
    37  				}
    38  				if includeFileForProcessing(c, info) {
    39  					filesToProcess <- path
    40  				}
    41  				return nil
    42  			})
    43  			if err != nil {
    44  				panic(err)
    45  			}
    46  		} else {
    47  			fd, err := os.Open(d)
    48  			if err != nil {
    49  				panic(err)
    50  			}
    51  			files, err := fd.Readdir(-1)
    52  			if err != nil {
    53  				panic(err)
    54  			}
    55  			for _, f := range files {
    56  				if includeFileForProcessing(c, f) {
    57  					filesToProcess <- filepath.Join(d, f.Name())
    58  				}
    59  			}
    60  		}
    61  	}
    62  	close(filesToProcess)
    63  }