github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/utils/file.go (about)

     1  package utils
     2  
     3  import (
     4  	"io/fs"
     5  	"path/filepath"
     6  )
     7  
     8  const executablePerm = 0111
     9  
    10  // ListAllExecutableFiles returns all the executable files in the given path
    11  func ListAllExecutableFiles(basedir string) ([]string, error) {
    12  	files := []string{}
    13  	err := filepath.Walk(basedir, func(path string, info fs.FileInfo, err error) error {
    14  		if err != nil {
    15  			return err
    16  		}
    17  		if info.IsDir() && path != basedir {
    18  			return filepath.SkipDir
    19  		}
    20  		if !info.IsDir() && isExecutable(info.Mode().Perm()) {
    21  			files = append(files, path)
    22  		}
    23  		return nil
    24  	})
    25  
    26  	return files, err
    27  }
    28  
    29  func ListAllSharedLibFiles(basedir string) ([]string, error) {
    30  	files := []string{}
    31  	err := filepath.Walk(basedir, func(path string, info fs.FileInfo, err error) error {
    32  		if err != nil {
    33  			return err
    34  		}
    35  		if info.IsDir() && path != basedir {
    36  			return filepath.SkipDir
    37  		}
    38  		if !info.IsDir() && filepath.Ext(path) == ".so" {
    39  			files = append(files, path)
    40  		}
    41  		return nil
    42  	})
    43  
    44  	return files, err
    45  }
    46  
    47  func isExecutable(perm fs.FileMode) bool {
    48  	return perm&executablePerm == executablePerm
    49  }