feat: use uid/gid as UserID and UserGroup if current user not found in passwd
If user.Current() failed to parse current user info, e.g. no entry in
passwd file, use uid/gid as UserID and UserGroup, and use "/" as
default HomeDir. So we don't need to panic.
package basic
import (
+ "fmt"
"os"
"os/user"
"strconv"
"strings"
+ "syscall"
+ logger "d7y.io/dragonfly/v2/internal/dflog"
"d7y.io/dragonfly/v2/pkg/util/stringutils"
)
@@ -36,9 +39,14 @@ var (
func init() {
u, err := user.Current()
if err != nil {
- panic(err)
- }
+ logger.Warnf("Failed to get current user: %s", err.Error())
+ logger.Info("Use uid as Username")
+ UserID = syscall.Getuid()
+ Username = fmt.Sprintf("%d", UserID)
+ UserGroup = syscall.Getgid()
+ HomeDir = "/"
+ } else {
Username = u.Username
UserID, _ = strconv.Atoi(u.Uid)
UserGroup, _ = strconv.Atoi(u.Gid)
@@ -48,6 +56,7 @@ func init() {
if stringutils.IsBlank(HomeDir) {
panic("home dir is empty")
}
+ }
TmpDir = os.TempDir()
TmpDir = strings.TrimSpace(TmpDir)
