diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2020-12-22 07:40:57 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-22 07:40:57 +0800 |
commit | acd5e5a868c2c158132b4017f318943d1e6c573f (patch) | |
tree | d7e440a5add88a6779f605c82fa6e9085ebd424d /modules/util | |
parent | f8fd8996c090a02fe482587ccf03a319459fc704 (diff) | |
download | gitea-acd5e5a868c2c158132b4017f318943d1e6c573f.tar.gz gitea-acd5e5a868c2c158132b4017f318943d1e6c573f.zip |
Add StatDir and replace com.StatDir (#14099)
* Add StatDir and replace com.StatDir
* a nit
* Remove wrong file
Co-authored-by: 6543 <6543@obermui.de>
Diffstat (limited to 'modules/util')
-rw-r--r-- | modules/util/path.go | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/modules/util/path.go b/modules/util/path.go index fbcefb83b6..aa3d009899 100644 --- a/modules/util/path.go +++ b/modules/util/path.go @@ -5,8 +5,11 @@ package util import ( + "errors" "os" + "path" "path/filepath" + "strings" ) // EnsureAbsolutePath ensure that a path is absolute, making it @@ -70,3 +73,80 @@ func IsExist(path string) (bool, error) { } return false, err } + +func statDir(dirPath, recPath string, includeDir, isDirOnly, followSymlinks bool) ([]string, error) { + dir, err := os.Open(dirPath) + if err != nil { + return nil, err + } + defer dir.Close() + + fis, err := dir.Readdir(0) + if err != nil { + return nil, err + } + + statList := make([]string, 0) + for _, fi := range fis { + if strings.Contains(fi.Name(), ".DS_Store") { + continue + } + + relPath := path.Join(recPath, fi.Name()) + curPath := path.Join(dirPath, fi.Name()) + if fi.IsDir() { + if includeDir { + statList = append(statList, relPath+"/") + } + s, err := statDir(curPath, relPath, includeDir, isDirOnly, followSymlinks) + if err != nil { + return nil, err + } + statList = append(statList, s...) + } else if !isDirOnly { + statList = append(statList, relPath) + } else if followSymlinks && fi.Mode()&os.ModeSymlink != 0 { + link, err := os.Readlink(curPath) + if err != nil { + return nil, err + } + + isDir, err := IsDir(link) + if err != nil { + return nil, err + } + if isDir { + if includeDir { + statList = append(statList, relPath+"/") + } + s, err := statDir(curPath, relPath, includeDir, isDirOnly, followSymlinks) + if err != nil { + return nil, err + } + statList = append(statList, s...) + } + } + } + return statList, nil +} + +// StatDir gathers information of given directory by depth-first. +// It returns slice of file list and includes subdirectories if enabled; +// it returns error and nil slice when error occurs in underlying functions, +// or given path is not a directory or does not exist. +// +// Slice does not include given path itself. +// If subdirectories is enabled, they will have suffix '/'. +func StatDir(rootPath string, includeDir ...bool) ([]string, error) { + if isDir, err := IsDir(rootPath); err != nil { + return nil, err + } else if !isDir { + return nil, errors.New("not a directory or does not exist: " + rootPath) + } + + isIncludeDir := false + if len(includeDir) != 0 { + isIncludeDir = includeDir[0] + } + return statDir(rootPath, "", isIncludeDir, false, false) +} |