From a6ccd6055aaeaaef550e947ad8580ee89dcbc78e Mon Sep 17 00:00:00 2001 From: Pavel Sviderski Date: Wed, 27 Nov 2024 14:30:08 +1000 Subject: [PATCH] move fs and file ownership related funcs to fs package --- internal/fs/fs.go | 47 +++++++++++++++++++++++++ internal/machine/corroservice/config.go | 44 ++--------------------- internal/machine/machine.go | 5 +-- 3 files changed, 52 insertions(+), 44 deletions(-) create mode 100644 internal/fs/fs.go diff --git a/internal/fs/fs.go b/internal/fs/fs.go new file mode 100644 index 00000000..95835966 --- /dev/null +++ b/internal/fs/fs.go @@ -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 +} diff --git a/internal/machine/corroservice/config.go b/internal/machine/corroservice/config.go index 3869cd9b..d5a571f4 100644 --- a/internal/machine/corroservice/config.go +++ b/internal/machine/corroservice/config.go @@ -6,9 +6,7 @@ import ( "github.com/BurntSushi/toml" "net/netip" "os" - "os/user" - "path/filepath" - "strconv" + "uncloud/internal/fs" ) const ( @@ -54,46 +52,8 @@ func (c *Config) Write(path, owner string) error { if err := os.WriteFile(path, data.Bytes(), 0600); err != nil { return err } - if err := Chown(path, owner); err != nil { + if err := fs.Chown(path, owner); err != nil { return err } 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 -} diff --git a/internal/machine/machine.go b/internal/machine/machine.go index 8e642298..f1cc4277 100644 --- a/internal/machine/machine.go +++ b/internal/machine/machine.go @@ -21,6 +21,7 @@ import ( "strconv" "uncloud/internal/corrosion" "uncloud/internal/docker" + "uncloud/internal/fs" "uncloud/internal/machine/api/pb" apiproxy "uncloud/internal/machine/api/proxy" "uncloud/internal/machine/cluster" @@ -430,7 +431,7 @@ func listenUnixSocket(path string) (net.Listener, 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) } 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 { 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) }