Skip to content

Commit 8525289

Browse files
committed
simplify os.ReadFile, fine tune messages and comments, fix lint
1 parent 6211415 commit 8525289

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

modules/options/base.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ func readFileFromLocal(base []string, sub string, elems ...string) ([]byte, erro
102102
for _, dir := range base {
103103
localPathElems[0] = dir
104104
localPath := util.SafeFilePathAbs(localPathElems...)
105-
isFile, err := util.IsFile(localPath)
106-
if err != nil {
107-
log.Error("Unable to check if %s is a file. Error: %v", localPath, err)
108-
} else if isFile {
109-
return os.ReadFile(localPath)
105+
data, err := os.ReadFile(localPath)
106+
if err == nil {
107+
return data, nil
108+
} else if !os.IsNotExist(err) {
109+
log.Error("Unable to read file %q. Error: %v", localPath, err)
110110
}
111111
}
112-
return nil, fmt.Errorf("asset file does not exist: %v", elems)
112+
return nil, fmt.Errorf("local file does not exist: %v", elems)
113113
}

modules/options/dynamic.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func Dir(name string) ([]string, error) {
3434
return directories.AddAndGet(name, result), nil
3535
}
3636

37-
// fileFromOptionsDir is a helper to read files from static or custom path.
37+
// fileFromOptionsDir is a helper to read files from custom or static path.
3838
func fileFromOptionsDir(elems ...string) ([]byte, error) {
3939
return readFileFromLocal([]string{setting.CustomPath, setting.StaticRootPath}, "options", elems...)
4040
}

modules/options/static.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ package options
88
import (
99
"fmt"
1010
"io"
11+
"path/filepath"
1112

1213
"code.gitea.io/gitea/modules/setting"
14+
"code.gitea.io/gitea/modules/util"
1315
)
1416

1517
var directories = make(directorySet)
@@ -62,11 +64,12 @@ func AssetDir(dirName string) ([]string, error) {
6264

6365
// fileFromOptionsDir is a helper to read files from custom path or bindata.
6466
func fileFromOptionsDir(elems ...string) ([]byte, error) {
67+
// only try custom dir, no static dir
6568
if data, err := readFileFromLocal([]string{setting.CustomPath}, "options", elems...); err == nil {
6669
return data, nil
6770
}
6871

69-
f, err := Assets.Open(name)
72+
f, err := Assets.Open(util.SafePathRelX(elems...))
7073
if err != nil {
7174
return nil, err
7275
}

0 commit comments

Comments
 (0)