mirror of
https://codeberg.org/davrot/forgejo.git
synced 2025-05-17 14:00:01 +02:00
Introduce path Clean/Join helper functions (#23495)
Since #23493 has conflicts with latest commits, this PR is my proposal for fixing #23371 Details are in the comments And refactor the `modules/options` module, to make it always use "filepath" to access local files. Benefits: * No need to do `util.CleanPath(strings.ReplaceAll(p, "\\", "/"))), "/")` any more (not only one before) * The function behaviors are clearly defined
This commit is contained in:
parent
253a00aaac
commit
ce9dee5a1e
16 changed files with 261 additions and 152 deletions
|
@ -7,36 +7,38 @@ import (
|
|||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
)
|
||||
|
||||
var directories = make(directorySet)
|
||||
|
||||
// Locale reads the content of a specific locale from static/bindata or custom path.
|
||||
func Locale(name string) ([]byte, error) {
|
||||
return fileFromDir(path.Join("locale", util.CleanPath(name)))
|
||||
return fileFromOptionsDir("locale", name)
|
||||
}
|
||||
|
||||
// Readme reads the content of a specific readme from static/bindata or custom path.
|
||||
func Readme(name string) ([]byte, error) {
|
||||
return fileFromDir(path.Join("readme", util.CleanPath(name)))
|
||||
return fileFromOptionsDir("readme", name)
|
||||
}
|
||||
|
||||
// Gitignore reads the content of a gitignore locale from static/bindata or custom path.
|
||||
func Gitignore(name string) ([]byte, error) {
|
||||
return fileFromDir(path.Join("gitignore", util.CleanPath(name)))
|
||||
return fileFromOptionsDir("gitignore", name)
|
||||
}
|
||||
|
||||
// License reads the content of a specific license from static/bindata or custom path.
|
||||
func License(name string) ([]byte, error) {
|
||||
return fileFromDir(path.Join("license", util.CleanPath(name)))
|
||||
return fileFromOptionsDir("license", name)
|
||||
}
|
||||
|
||||
// Labels reads the content of a specific labels from static/bindata or custom path.
|
||||
func Labels(name string) ([]byte, error) {
|
||||
return fileFromDir(path.Join("label", util.CleanPath(name)))
|
||||
return fileFromOptionsDir("label", name)
|
||||
}
|
||||
|
||||
// WalkLocales reads the content of a specific locale
|
||||
|
@ -79,17 +81,54 @@ func walkAssetDir(root string, callback func(path, name string, d fs.DirEntry, e
|
|||
return nil
|
||||
}
|
||||
|
||||
func statDirIfExist(dir string) ([]string, error) {
|
||||
isDir, err := util.IsDir(dir)
|
||||
// mustLocalPathAbs coverts a path to absolute path
|
||||
// FIXME: the old behavior (StaticRootPath might not be absolute), not ideal, just keep the same as before
|
||||
func mustLocalPathAbs(s string) string {
|
||||
abs, err := filepath.Abs(s)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to check if static directory %s is a directory. %w", dir, err)
|
||||
// This should never happen in a real system. If it happens, the user must have already been in trouble: the system is not able to resolve its own paths.
|
||||
log.Fatal("Unable to get absolute path for %q: %v", s, err)
|
||||
}
|
||||
if !isDir {
|
||||
return nil, nil
|
||||
return abs
|
||||
}
|
||||
|
||||
func joinLocalPaths(baseDirs []string, subDir string, elems ...string) (paths []string) {
|
||||
abs := make([]string, len(elems)+2)
|
||||
abs[1] = subDir
|
||||
copy(abs[2:], elems)
|
||||
for _, baseDir := range baseDirs {
|
||||
abs[0] = mustLocalPathAbs(baseDir)
|
||||
paths = append(paths, util.FilePathJoinAbs(abs...))
|
||||
}
|
||||
files, err := util.StatDir(dir, true)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to read directory %q. %w", dir, err)
|
||||
return paths
|
||||
}
|
||||
|
||||
func listLocalDirIfExist(baseDirs []string, subDir string, elems ...string) (files []string, err error) {
|
||||
for _, localPath := range joinLocalPaths(baseDirs, subDir, elems...) {
|
||||
isDir, err := util.IsDir(localPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to check if path %q is a directory. %w", localPath, err)
|
||||
} else if !isDir {
|
||||
continue
|
||||
}
|
||||
|
||||
dirFiles, err := util.StatDir(localPath, true)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to read directory %q. %w", localPath, err)
|
||||
}
|
||||
files = append(files, dirFiles...)
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func readLocalFile(baseDirs []string, subDir string, elems ...string) ([]byte, error) {
|
||||
for _, localPath := range joinLocalPaths(baseDirs, subDir, elems...) {
|
||||
data, err := os.ReadFile(localPath)
|
||||
if err == nil {
|
||||
return data, nil
|
||||
} else if !os.IsNotExist(err) {
|
||||
log.Error("Unable to read file %q. Error: %v", localPath, err)
|
||||
}
|
||||
}
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue