Skip to content

Commit 86974ff

Browse files
committed
tolerate the legacy relative path behavior
1 parent 5bc51f9 commit 86974ff

File tree

3 files changed

+44
-54
lines changed

3 files changed

+44
-54
lines changed

modules/options/base.go

+36-24
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"code.gitea.io/gitea/modules/util"
1515
)
1616

17+
var directories = make(directorySet)
18+
1719
// Locale reads the content of a specific locale from static/bindata or custom path.
1820
func Locale(name string) ([]byte, error) {
1921
return fileFromOptionsDir("locale", name)
@@ -79,37 +81,47 @@ func walkAssetDir(root string, callback func(path, name string, d fs.DirEntry, e
7981
return nil
8082
}
8183

82-
func statDirIfExist(dir string) ([]string, error) {
83-
isDir, err := util.IsDir(dir)
84+
// mustLocalPathAbs coverts a path to absolute path
85+
// FIXME: the old behavior (StaticRootPath might not be absolute), not ideal, just keep the same as before
86+
func mustLocalPathAbs(s string) string {
87+
abs, err := filepath.Abs(s)
8488
if err != nil {
85-
return nil, fmt.Errorf("unable to check if static directory %s is a directory. %w", dir, err)
89+
log.Fatal("Unable to get absolute path for %q: %v", s, err)
8690
}
87-
if !isDir {
88-
return nil, nil
91+
return abs
92+
}
93+
94+
func joinLocalPaths(baseDirs []string, subDir string, elems ...string) (paths []string) {
95+
abs := make([]string, len(elems)+2)
96+
abs[1] = subDir
97+
copy(abs[2:], elems)
98+
for _, baseDir := range baseDirs {
99+
abs[0] = mustLocalPathAbs(baseDir)
100+
paths = append(paths, util.SafeFilePathAbs(abs...))
89101
}
90-
files, err := util.StatDir(dir, true)
91-
if err != nil {
92-
return nil, fmt.Errorf("unable to read directory %q. %w", dir, err)
102+
return paths
103+
}
104+
105+
func listLocalDirIfExist(baseDirs []string, subDir string, elems ...string) (files []string, err error) {
106+
for _, localPath := range joinLocalPaths(baseDirs, subDir, elems...) {
107+
isDir, err := util.IsDir(localPath)
108+
if err != nil {
109+
return nil, fmt.Errorf("unable to check if static directory %s is a directory. %w", localPath, err)
110+
} else if !isDir {
111+
continue
112+
}
113+
114+
dirFiles, err := util.StatDir(localPath, true)
115+
if err != nil {
116+
return nil, fmt.Errorf("unable to read directory %q. %w", localPath, err)
117+
}
118+
files = append(files, dirFiles...)
93119
}
94120
return files, nil
95121
}
96122

97-
func readFileFromLocal(base []string, sub string, elems ...string) ([]byte, error) {
98-
localPathElems := make([]string, len(elems)+2) // path[0] will be used for the custom path prefix
99-
localPathElems[1] = sub
100-
copy(localPathElems[2:], elems)
101-
102-
for _, dir := range base {
103-
if !filepath.IsAbs(dir) {
104-
// FIXME: the old behavior (CustomPath or StaticRootPath might not be absolute), not ideal, just keep the same as before
105-
var err error
106-
dir, err = filepath.Abs(dir)
107-
if err != nil {
108-
return nil, fmt.Errorf("unable to get absolute path for %q. %w", dir, err)
109-
}
110-
}
111-
localPathElems[0] = dir
112-
localPath := util.SafeFilePathAbs(localPathElems...)
123+
func readLocalFile(baseDirs []string, subDir string, elems ...string) ([]byte, error) {
124+
for _, localPath := range joinLocalPaths(baseDirs, subDir, elems...) {
113125
data, err := os.ReadFile(localPath)
114126
if err == nil {
115127
return data, nil

modules/options/dynamic.go

+4-15
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,25 @@ package options
77

88
import (
99
"code.gitea.io/gitea/modules/setting"
10-
"code.gitea.io/gitea/modules/util"
1110
)
1211

13-
var directories = make(directorySet)
14-
1512
// Dir returns all files from static or custom directory.
1613
func Dir(name string) ([]string, error) {
1714
if directories.Filled(name) {
1815
return directories.Get(name), nil
1916
}
2017

21-
var result []string
22-
23-
for _, dir := range []string{
24-
util.SafeFilePathAbs(setting.CustomPath, "options", name), // custom dir
25-
util.SafeFilePathAbs(setting.StaticRootPath, "options", name), // static dir
26-
} {
27-
files, err := statDirIfExist(dir)
28-
if err != nil {
29-
return nil, err
30-
}
31-
result = append(result, files...)
18+
result, err := listLocalDirIfExist([]string{setting.CustomPath, setting.StaticRootPath}, "options", name)
19+
if err != nil {
20+
return nil, err
3221
}
3322

3423
return directories.AddAndGet(name, result), nil
3524
}
3625

3726
// fileFromOptionsDir is a helper to read files from custom or static path.
3827
func fileFromOptionsDir(elems ...string) ([]byte, error) {
39-
return readFileFromLocal([]string{setting.CustomPath, setting.StaticRootPath}, "options", elems...)
28+
return readLocalFile([]string{setting.CustomPath, setting.StaticRootPath}, "options", elems...)
4029
}
4130

4231
// IsDynamic will return false when using embedded data (-tags bindata)

modules/options/static.go

+4-15
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,20 @@ package options
88
import (
99
"fmt"
1010
"io"
11-
"path/filepath"
1211

1312
"code.gitea.io/gitea/modules/setting"
1413
"code.gitea.io/gitea/modules/util"
1514
)
1615

17-
var directories = make(directorySet)
18-
1916
// Dir returns all files from custom directory or bindata.
2017
func Dir(name string) ([]string, error) {
2118
if directories.Filled(name) {
2219
return directories.Get(name), nil
2320
}
2421

25-
var result []string
26-
27-
for _, dir := range []string{
28-
filepath.Join(setting.CustomPath, "options", name), // custom dir
29-
// no static dir
30-
} {
31-
files, err := statDirIfExist(dir)
32-
if err != nil {
33-
return nil, err
34-
}
35-
result = append(result, files...)
22+
result, err := listLocalDirIfExist([]string{setting.CustomPath}, "options", name)
23+
if err != nil {
24+
return nil, err
3625
}
3726

3827
files, err := AssetDir(name)
@@ -65,7 +54,7 @@ func AssetDir(dirName string) ([]string, error) {
6554
// fileFromOptionsDir is a helper to read files from custom path or bindata.
6655
func fileFromOptionsDir(elems ...string) ([]byte, error) {
6756
// only try custom dir, no static dir
68-
if data, err := readFileFromLocal([]string{setting.CustomPath}, "options", elems...); err == nil {
57+
if data, err := readLocalFile([]string{setting.CustomPath}, "options", elems...); err == nil {
6958
return data, nil
7059
}
7160

0 commit comments

Comments
 (0)