mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
* Check autom if we can connect via Unix socket When there is no config, but a unix socket does exist, connect via the unix socket. Prohibit saving the config if this is the case. Fixes: #148 Use the new CutPrefix to shorten some code. Signed-off-by: Miek Gieben <miek@miek.nl> * remove error checking; it can not be hit Signed-off-by: Miek Gieben <miek@miek.nl> * Extra error text when logged in locally Signed-off-by: Miek Gieben <miek@miek.nl> --------- Signed-off-by: Miek Gieben <miek@miek.nl>
79 lines
1.6 KiB
Go
79 lines
1.6 KiB
Go
package fs
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/user"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func ExpandHomeDir(path string) string {
|
|
if len(path) == 0 {
|
|
return path
|
|
}
|
|
if path[0] == '~' {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return path
|
|
}
|
|
return strings.Replace(path, "~", home, 1)
|
|
}
|
|
return path
|
|
}
|
|
|
|
// LookupUIDGID returns the user and group IDs for the given username.
|
|
func LookupUIDGID(username string) (uid, gid int, err error) {
|
|
usr, err := user.Lookup(username)
|
|
if err != nil {
|
|
err = fmt.Errorf("lookup user %q: %w", username, err)
|
|
return
|
|
}
|
|
uid, err = strconv.Atoi(usr.Uid)
|
|
if err != nil {
|
|
err = fmt.Errorf("parse %q user ID (UID) %q: %w", username, usr.Uid, err)
|
|
return
|
|
}
|
|
gid, err = strconv.Atoi(usr.Gid)
|
|
if err != nil {
|
|
err = fmt.Errorf("parse %q user group ID (GID) %q: %w", username, usr.Gid, err)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func Chown(path, username, group string) error {
|
|
uid, gid := -1, -1
|
|
if username != "" {
|
|
usr, err := user.Lookup(username)
|
|
if err != nil {
|
|
return fmt.Errorf("lookup user %q: %w", username, err)
|
|
}
|
|
uid, err = strconv.Atoi(usr.Uid)
|
|
if err != nil {
|
|
return fmt.Errorf("parse %q user ID (UID) %q: %w", username, usr.Uid, err)
|
|
}
|
|
}
|
|
|
|
if group != "" {
|
|
grp, err := user.LookupGroup(group)
|
|
if err != nil {
|
|
return fmt.Errorf("lookup group %q: %w", group, err)
|
|
}
|
|
gid, err = strconv.Atoi(grp.Gid)
|
|
if err != nil {
|
|
return fmt.Errorf("parse %q group ID (GID) %q: %w", group, grp.Gid, err)
|
|
}
|
|
}
|
|
|
|
if err := os.Chown(path, uid, gid); err != nil {
|
|
return fmt.Errorf("chown %q: %w", path, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Exists(path string) bool {
|
|
_, err := os.Stat(path)
|
|
return err == nil
|
|
}
|