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>release/v1.15
parent
f8fd8996c0
commit
acd5e5a868
|
@ -79,7 +79,7 @@ func loadRepoConfig() {
|
||||||
log.Fatal("Failed to get custom %s files: %v", t, err)
|
log.Fatal("Failed to get custom %s files: %v", t, err)
|
||||||
}
|
}
|
||||||
if isDir {
|
if isDir {
|
||||||
customFiles, err := com.StatDir(customPath)
|
customFiles, err := util.StatDir(customPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to get custom %s files: %v", t, err)
|
log.Fatal("Failed to get custom %s files: %v", t, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,8 +14,6 @@ import (
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
|
|
||||||
"github.com/unknwon/com"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -39,7 +37,7 @@ func Dir(name string) ([]string, error) {
|
||||||
return []string{}, fmt.Errorf("Unabe to check if custom directory %s is a directory. %v", customDir, err)
|
return []string{}, fmt.Errorf("Unabe to check if custom directory %s is a directory. %v", customDir, err)
|
||||||
}
|
}
|
||||||
if isDir {
|
if isDir {
|
||||||
files, err := com.StatDir(customDir, true)
|
files, err := util.StatDir(customDir, true)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []string{}, fmt.Errorf("Failed to read custom directory. %v", err)
|
return []string{}, fmt.Errorf("Failed to read custom directory. %v", err)
|
||||||
|
@ -55,7 +53,7 @@ func Dir(name string) ([]string, error) {
|
||||||
return []string{}, fmt.Errorf("Unabe to check if static directory %s is a directory. %v", staticDir, err)
|
return []string{}, fmt.Errorf("Unabe to check if static directory %s is a directory. %v", staticDir, err)
|
||||||
}
|
}
|
||||||
if isDir {
|
if isDir {
|
||||||
files, err := com.StatDir(staticDir, true)
|
files, err := util.StatDir(staticDir, true)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []string{}, fmt.Errorf("Failed to read static directory. %v", err)
|
return []string{}, fmt.Errorf("Failed to read static directory. %v", err)
|
||||||
|
|
|
@ -14,8 +14,6 @@ import (
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
|
|
||||||
"github.com/unknwon/com"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -38,7 +36,7 @@ func Dir(name string) ([]string, error) {
|
||||||
return []string{}, fmt.Errorf("Failed to check if custom directory %s is a directory. %v", err)
|
return []string{}, fmt.Errorf("Failed to check if custom directory %s is a directory. %v", err)
|
||||||
}
|
}
|
||||||
if isDir {
|
if isDir {
|
||||||
files, err := com.StatDir(customDir, true)
|
files, err := util.StatDir(customDir, true)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []string{}, fmt.Errorf("Failed to read custom directory. %v", err)
|
return []string{}, fmt.Errorf("Failed to read custom directory. %v", err)
|
||||||
|
|
|
@ -18,7 +18,6 @@ import (
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
|
|
||||||
"gitea.com/macaron/macaron"
|
"gitea.com/macaron/macaron"
|
||||||
"github.com/unknwon/com"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -65,7 +64,7 @@ func Mailer() (*texttmpl.Template, *template.Template) {
|
||||||
log.Warn("Unable to check if templates dir %s is a directory. Error: %v", staticDir, err)
|
log.Warn("Unable to check if templates dir %s is a directory. Error: %v", staticDir, err)
|
||||||
}
|
}
|
||||||
if isDir {
|
if isDir {
|
||||||
files, err := com.StatDir(staticDir)
|
files, err := util.StatDir(staticDir)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Failed to read %s templates dir. %v", staticDir, err)
|
log.Warn("Failed to read %s templates dir. %v", staticDir, err)
|
||||||
|
@ -94,7 +93,7 @@ func Mailer() (*texttmpl.Template, *template.Template) {
|
||||||
log.Warn("Unable to check if templates dir %s is a directory. Error: %v", customDir, err)
|
log.Warn("Unable to check if templates dir %s is a directory. Error: %v", customDir, err)
|
||||||
}
|
}
|
||||||
if isDir {
|
if isDir {
|
||||||
files, err := com.StatDir(customDir)
|
files, err := util.StatDir(customDir)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Failed to read %s templates dir. %v", customDir, err)
|
log.Warn("Failed to read %s templates dir. %v", customDir, err)
|
||||||
|
|
|
@ -21,7 +21,6 @@ import (
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
|
|
||||||
"gitea.com/macaron/macaron"
|
"gitea.com/macaron/macaron"
|
||||||
"github.com/unknwon/com"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -83,7 +82,7 @@ func NewTemplateFileSystem() templateFileSystem {
|
||||||
log.Warn("Unable to check if templates dir %s is a directory. Error: %v", customDir, err)
|
log.Warn("Unable to check if templates dir %s is a directory. Error: %v", customDir, err)
|
||||||
}
|
}
|
||||||
if isDir {
|
if isDir {
|
||||||
files, err := com.StatDir(customDir)
|
files, err := util.StatDir(customDir)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Failed to read %s templates dir. %v", customDir, err)
|
log.Warn("Failed to read %s templates dir. %v", customDir, err)
|
||||||
|
@ -179,7 +178,7 @@ func Mailer() (*texttmpl.Template, *template.Template) {
|
||||||
log.Warn("Failed to check if custom directory %s is a directory. %v", err)
|
log.Warn("Failed to check if custom directory %s is a directory. %v", err)
|
||||||
}
|
}
|
||||||
if isDir {
|
if isDir {
|
||||||
files, err := com.StatDir(customDir)
|
files, err := util.StatDir(customDir)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Failed to read %s templates dir. %v", customDir, err)
|
log.Warn("Failed to read %s templates dir. %v", customDir, err)
|
||||||
|
|
|
@ -5,8 +5,11 @@
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// EnsureAbsolutePath ensure that a path is absolute, making it
|
// EnsureAbsolutePath ensure that a path is absolute, making it
|
||||||
|
@ -70,3 +73,80 @@ func IsExist(path string) (bool, error) {
|
||||||
}
|
}
|
||||||
return false, err
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue