mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-10-27 13:37:09 +00:00
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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user