Platform: Consolidate regular file resolution

Reuse ResolveRegularFile for Lua loading/signing, local config, and assets.
Only evaluate file symlinks when needed, tolerating junction-related
resolution failures on a best-effort basis.
This commit is contained in:
Meo597
2026-10-25 23:52:22 +08:00
parent 1b1647cd9d
commit 20d107a28a
4 changed files with 41 additions and 47 deletions
+8 -13
View File
@@ -5,6 +5,7 @@ import (
"path/filepath"
xlua "github.com/xtls/xray-core/common/lua"
"github.com/xtls/xray-core/common/platform"
"github.com/xtls/xray-core/main/commands/base"
)
@@ -32,15 +33,15 @@ func executeSign(cmd *base.Command, args []string) {
cmd.Usage()
}
info, err := os.Stat(args[0])
input, info, err := platform.ResolveRegularFile(args[0])
if err != nil {
base.Fatalf("failed to inspect Lua file: %s", err)
}
if !info.Mode().IsRegular() {
base.Fatalf("Lua input is not a regular file: %s", args[0])
if filepath.Ext(input) != ".lua" {
base.Fatalf("Lua file extension must be .lua")
}
code, err := os.ReadFile(args[0])
code, err := os.ReadFile(input)
if err != nil {
base.Fatalf("failed to read Lua file: %s", err)
}
@@ -58,7 +59,7 @@ func executeSign(cmd *base.Command, args []string) {
}
output := *signOutput
if output == "" {
output = args[0]
output = input
}
if err := writeSignedFile(output, signed, info.Mode().Perm()); err != nil {
base.Fatalf("failed to write signed Lua file: %s", err)
@@ -66,15 +67,9 @@ func executeSign(cmd *base.Command, args []string) {
}
func writeSignedFile(path string, code []byte, mode os.FileMode) error {
if info, err := os.Stat(path); err == nil {
if !info.Mode().IsRegular() {
return &os.PathError{Op: "write", Path: path, Err: os.ErrInvalid}
}
if resolved, info, err := platform.ResolveRegularFile(path); err == nil {
path = resolved
mode = info.Mode().Perm()
path, err = filepath.EvalSymlinks(path)
if err != nil {
return err
}
} else if !os.IsNotExist(err) {
return err
}
+2 -5
View File
@@ -14,6 +14,7 @@ import (
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/platform"
"github.com/xtls/xray-core/common/utils"
"github.com/xtls/xray-core/main/commands/base"
"github.com/xtls/xray-core/main/confloader"
@@ -58,11 +59,7 @@ func readConfigFile(file *os.File) ([]byte, error) {
return nil, err
}
if info.Mode().IsRegular() {
path, err := filepath.Abs(file.Name())
if err != nil {
return nil, err
}
path, err = filepath.EvalSymlinks(path)
path, _, err := platform.ResolveRegularFile(file.Name())
if err != nil {
return nil, err
}