mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
move fs and file ownership related funcs to fs package
This commit is contained in:
@@ -0,0 +1,47 @@
|
|||||||
|
package fs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/user"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func MkDataDir(dir, owner string) error {
|
||||||
|
parent, _ := filepath.Split(dir)
|
||||||
|
// Use 0711 for parent directories to allow `owner` to access its nested data directory.
|
||||||
|
if err := os.MkdirAll(parent, 0711); err != nil {
|
||||||
|
return fmt.Errorf("create directory %q: %w", parent, err)
|
||||||
|
}
|
||||||
|
if err := os.Mkdir(dir, 0700); err != nil {
|
||||||
|
if !os.IsExist(err) {
|
||||||
|
return fmt.Errorf("create directory %q: %w", dir, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := Chown(dir, owner); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Chown(path, owner string) error {
|
||||||
|
if owner != "" {
|
||||||
|
usr, err := user.Lookup(owner)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("lookup user %q: %w", owner, err)
|
||||||
|
}
|
||||||
|
uid, err := strconv.Atoi(usr.Uid)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("parse %q user ID (UID) %q: %w", owner, usr.Uid, err)
|
||||||
|
}
|
||||||
|
gid, err := strconv.Atoi(usr.Gid)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("parse %q user group ID (GID) %q: %w", owner, usr.Gid, err)
|
||||||
|
}
|
||||||
|
if err = os.Chown(path, uid, gid); err != nil {
|
||||||
|
return fmt.Errorf("chown %q: %w", path, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -6,9 +6,7 @@ import (
|
|||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"uncloud/internal/fs"
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -54,46 +52,8 @@ func (c *Config) Write(path, owner string) error {
|
|||||||
if err := os.WriteFile(path, data.Bytes(), 0600); err != nil {
|
if err := os.WriteFile(path, data.Bytes(), 0600); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := Chown(path, owner); err != nil {
|
if err := fs.Chown(path, owner); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func MkDataDir(dir, owner string) error {
|
|
||||||
parent, _ := filepath.Split(dir)
|
|
||||||
// Use 0711 for parent directories to allow `owner` to access its nested data directory.
|
|
||||||
if err := os.MkdirAll(parent, 0711); err != nil {
|
|
||||||
return fmt.Errorf("create directory %q: %w", parent, err)
|
|
||||||
}
|
|
||||||
if err := os.Mkdir(dir, 0700); err != nil {
|
|
||||||
if !os.IsExist(err) {
|
|
||||||
return fmt.Errorf("create directory %q: %w", dir, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := Chown(dir, owner); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func Chown(path, owner string) error {
|
|
||||||
if owner != "" {
|
|
||||||
usr, err := user.Lookup(owner)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("lookup user %q: %w", owner, err)
|
|
||||||
}
|
|
||||||
uid, err := strconv.Atoi(usr.Uid)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("parse %q user ID (UID) %q: %w", owner, usr.Uid, err)
|
|
||||||
}
|
|
||||||
gid, err := strconv.Atoi(usr.Gid)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("parse %q user group ID (GID) %q: %w", owner, usr.Gid, err)
|
|
||||||
}
|
|
||||||
if err = os.Chown(path, uid, gid); err != nil {
|
|
||||||
return fmt.Errorf("chown %q: %w", path, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"uncloud/internal/corrosion"
|
"uncloud/internal/corrosion"
|
||||||
"uncloud/internal/docker"
|
"uncloud/internal/docker"
|
||||||
|
"uncloud/internal/fs"
|
||||||
"uncloud/internal/machine/api/pb"
|
"uncloud/internal/machine/api/pb"
|
||||||
apiproxy "uncloud/internal/machine/api/proxy"
|
apiproxy "uncloud/internal/machine/api/proxy"
|
||||||
"uncloud/internal/machine/cluster"
|
"uncloud/internal/machine/cluster"
|
||||||
@@ -430,7 +431,7 @@ func listenUnixSocket(path string) (net.Listener, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Machine) configureCorrosion() error {
|
func (m *Machine) configureCorrosion() error {
|
||||||
if err := corroservice.MkDataDir(m.config.CorrosionDir, corroservice.DefaultUser); err != nil {
|
if err := fs.MkDataDir(m.config.CorrosionDir, corroservice.DefaultUser); err != nil {
|
||||||
return fmt.Errorf("create corrosion data directory: %w", err)
|
return fmt.Errorf("create corrosion data directory: %w", err)
|
||||||
}
|
}
|
||||||
configPath := filepath.Join(m.config.CorrosionDir, "config.toml")
|
configPath := filepath.Join(m.config.CorrosionDir, "config.toml")
|
||||||
@@ -475,7 +476,7 @@ func (m *Machine) configureCorrosion() error {
|
|||||||
if err := os.WriteFile(schemaPath, []byte(store.Schema), 0644); err != nil {
|
if err := os.WriteFile(schemaPath, []byte(store.Schema), 0644); err != nil {
|
||||||
return fmt.Errorf("write corrosion schema: %w", err)
|
return fmt.Errorf("write corrosion schema: %w", err)
|
||||||
}
|
}
|
||||||
if err := corroservice.Chown(schemaPath, corroservice.DefaultUser); err != nil {
|
if err := fs.Chown(schemaPath, corroservice.DefaultUser); err != nil {
|
||||||
return fmt.Errorf("chown corrosion schema: %w", err)
|
return fmt.Errorf("chown corrosion schema: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user