Skip to content
This repository was archived by the owner on May 27, 2023. It is now read-only.

Commit 13c7676

Browse files
patch GetCurrDir function so that when deploying lpmx on symlinked partition, the path can be correctly resolved
1 parent 060eb62 commit 13c7676

File tree

1 file changed

+45
-35
lines changed

1 file changed

+45
-35
lines changed

utils/utils.go

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import (
1111
"math/rand"
1212
"net/http"
1313
"os"
14+
"os/user"
1415
"path/filepath"
1516
"strconv"
1617
"strings"
1718
"time"
18-
"os/user"
1919

2020
. "github.com/JasonYangShadow/lpmx/error"
2121
. "github.com/JasonYangShadow/lpmx/log"
@@ -57,11 +57,11 @@ func FileExist(file string) bool {
5757
}
5858

5959
func RegularFileExist(file string) bool {
60-
ftype, err := FileType(file)
61-
if err == nil && (ftype == TYPE_REGULAR) {
62-
return true
63-
}
64-
return false
60+
ftype, err := FileType(file)
61+
if err == nil && (ftype == TYPE_REGULAR) {
62+
return true
63+
}
64+
return false
6565
}
6666

6767
func FolderExist(folder string) bool {
@@ -1004,9 +1004,19 @@ func ReverseStrArray(input []string) []string {
10041004
}
10051005

10061006
func GetCurrDir() (string, *Error) {
1007-
arg_path, _ := filepath.Abs(filepath.Dir(os.Args[0]))
10081007
filename := filepath.Base(os.Args[0])
1009-
searchPaths := []string{arg_path, "."}
1008+
exepath, eerr := os.Executable()
1009+
if eerr != nil {
1010+
cerr := ErrNew(eerr, "could not determine current executable path")
1011+
return "", cerr
1012+
}
1013+
cwd, werr := filepath.Abs(filepath.Dir(exepath))
1014+
if werr != nil {
1015+
cerr := ErrNew(werr, fmt.Sprintf("could not resolve current path: %s for cwd", exepath))
1016+
return "", cerr
1017+
}
1018+
searchPaths := []string{cwd}
1019+
//lpmx will search current cwd as well as system PATH
10101020
searchPaths = append(searchPaths, strings.Split(os.Getenv("PATH"), ":")...)
10111021
for _, path := range searchPaths {
10121022
p := fmt.Sprintf("%s/%s", path, filename)
@@ -1018,28 +1028,28 @@ func GetCurrDir() (string, *Error) {
10181028
return "", cerr
10191029
}
10201030

1021-
func GetConfigDir() (string, *Error){
1022-
curr, cerr := GetCurrDir()
1023-
if cerr != nil{
1024-
return "", cerr
1025-
}
1026-
var config string
1027-
//if lpmx is installed in system path, we do not have permission to create .lpmxsys/.lpmxdata
1028-
if(strings.HasPrefix(curr, "/usr/bin") || strings.HasPrefix(curr, "/usr/local/bin") || strings.HasPrefix(curr, "/bin") || strings.HasPrefix(curr, "/sbin")){
1029-
user, err := user.Current()
1030-
if err != nil{
1031-
cerr := ErrNew(err, "could not get current user")
1032-
return "", cerr
1033-
}
1034-
if user.Uid == "0"{
1035-
cerr := ErrNew(ErrPermissionRoot, "should not use root to start LPMX")
1036-
return "", cerr
1037-
}
1038-
config = user.HomeDir
1039-
}else{
1040-
config = curr
1041-
}
1042-
return config, nil
1031+
func GetConfigDir() (string, *Error) {
1032+
curr, cerr := GetCurrDir()
1033+
if cerr != nil {
1034+
return "", cerr
1035+
}
1036+
var config string
1037+
//if lpmx is installed in system path, we do not have permission to create .lpmxsys/.lpmxdata
1038+
if strings.HasPrefix(curr, "/usr/bin") || strings.HasPrefix(curr, "/usr/local/bin") || strings.HasPrefix(curr, "/bin") || strings.HasPrefix(curr, "/sbin") {
1039+
user, err := user.Current()
1040+
if err != nil {
1041+
cerr := ErrNew(err, "could not get current user")
1042+
return "", cerr
1043+
}
1044+
if user.Uid == "0" {
1045+
cerr := ErrNew(ErrPermissionRoot, "should not use root to start LPMX")
1046+
return "", cerr
1047+
}
1048+
config = user.HomeDir
1049+
} else {
1050+
config = curr
1051+
}
1052+
return config, nil
10431053
}
10441054

10451055
func AddVartoFile(env string, file string) *Error {
@@ -1146,11 +1156,11 @@ func GetHostOSInfo() (string, string, *Error) {
11461156
//}
11471157

11481158
func GetProcessIdByName(name string) (bool, string, *Error) {
1149-
//get current pid
1150-
out_uid, oerr := CommandBash(fmt.Sprintf("cat /proc/self/loginuid"))
1151-
if oerr!= nil{
1152-
return false, "", oerr
1153-
}
1159+
//get current pid
1160+
out_uid, oerr := CommandBash(fmt.Sprintf("cat /proc/self/loginuid"))
1161+
if oerr != nil {
1162+
return false, "", oerr
1163+
}
11541164
cmd_context := fmt.Sprintf("pgrep -U %s %s", out_uid, name)
11551165
out, err := CommandBash(cmd_context)
11561166
if err != nil {

0 commit comments

Comments
 (0)