Browse Source

新增InitPathReal()函数,程序启动时的路径解析支持符号连接

niujiuru 4 days ago
parent
commit
b5962d961b
1 changed files with 16 additions and 0 deletions
  1. 16 0
      baseapp/path.go

+ 16 - 0
baseapp/path.go

@@ -47,6 +47,14 @@ func SetOptDirs(cfgEnabled, runEnabled, varEnabled, imgEnabled bool) {
 }
 
 func InitPath() {
+	initPath(false)
+}
+
+func InitPathReal() {
+	initPath(true)
+}
+
+func initPath(resolveSymlink bool) {
 	file, err := exec.LookPath(os.Args[0])
 	if err != nil {
 		fmt.Printf("An error occurred while calling the exec.LookPath() function: %v\n", err)
@@ -59,6 +67,14 @@ func InitPath() {
 		os.Exit(1)
 	}
 
+	if resolveSymlink { // 解析符号链接
+		path, err = filepath.EvalSymlinks(path)
+		if err != nil {
+			fmt.Printf("An error occurred while calling the filepath.EvalSymlinks() function: %v\n", err)
+			os.Exit(1)
+		}
+	}
+
 	EXEC_DIR = filepath.Dir(path)
 	EXEC_FILENAME = filepath.Base(path)