From 9f098362fbe3059f12641fe537d640afb80f2922 Mon Sep 17 00:00:00 2001 From: Gaius Date: Wed, 28 Sep 2022 19:37:32 +0800 Subject: [PATCH] refactor: pkg basic (#1712) Signed-off-by: Gaius --- client/config/dfcache.go | 6 +-- client/config/dfget.go | 6 +-- client/daemon/rpcserver/rpcserver.go | 6 +-- client/dfcache/dfcache.go | 5 +-- client/dfget/dfget.go | 9 ++-- cmd/dfget/cmd/root.go | 4 +- pkg/basic/user.go | 66 ---------------------------- pkg/dfpath/dfpath_darwin.go | 4 +- pkg/digest/digest_test.go | 4 +- pkg/os/user/user.go | 47 ++++++++++++++++++++ 10 files changed, 67 insertions(+), 90 deletions(-) delete mode 100644 pkg/basic/user.go create mode 100644 pkg/os/user/user.go diff --git a/client/config/dfcache.go b/client/config/dfcache.go index fc5d9689f..f33778756 100644 --- a/client/config/dfcache.go +++ b/client/config/dfcache.go @@ -30,7 +30,7 @@ import ( "d7y.io/dragonfly/v2/cmd/dependency/base" "d7y.io/dragonfly/v2/internal/dferrors" - "d7y.io/dragonfly/v2/pkg/basic" + "d7y.io/dragonfly/v2/pkg/os/user" "d7y.io/dragonfly/v2/pkg/strings" ) @@ -215,7 +215,7 @@ func (cfg *CacheOption) checkOutput() error { } outputDir, _ := path.Split(cfg.Output) - if err := MkdirAll(outputDir, 0777, basic.UserID, basic.UserGroup); err != nil { + if err := MkdirAll(outputDir, 0777, os.Getuid(), os.Getgid()); err != nil { return err } @@ -229,7 +229,7 @@ func (cfg *CacheOption) checkOutput() error { if err := syscall.Access(dir, syscall.O_RDWR); err == nil { break } else if os.IsPermission(err) || dir == "/" { - return fmt.Errorf("user[%s] path[%s] %v", basic.Username, cfg.Output, err) + return fmt.Errorf("user[%s] path[%s] %v", user.Username(), cfg.Output, err) } } return nil diff --git a/client/config/dfget.go b/client/config/dfget.go index 3c40ccc73..d0d158083 100644 --- a/client/config/dfget.go +++ b/client/config/dfget.go @@ -32,8 +32,8 @@ import ( "d7y.io/dragonfly/v2/cmd/dependency/base" "d7y.io/dragonfly/v2/internal/dferrors" logger "d7y.io/dragonfly/v2/internal/dflog" - "d7y.io/dragonfly/v2/pkg/basic" "d7y.io/dragonfly/v2/pkg/net/url" + "d7y.io/dragonfly/v2/pkg/os/user" pkgstrings "d7y.io/dragonfly/v2/pkg/strings" "d7y.io/dragonfly/v2/pkg/unit" ) @@ -232,7 +232,7 @@ func (cfg *ClientOption) checkOutput() error { return fmt.Errorf("path[%s] is not absolute path", cfg.Output) } outputDir, _ := path.Split(cfg.Output) - if err := MkdirAll(outputDir, 0777, basic.UserID, basic.UserGroup); err != nil { + if err := MkdirAll(outputDir, 0777, os.Getuid(), os.Getgid()); err != nil { return err } @@ -247,7 +247,7 @@ func (cfg *ClientOption) checkOutput() error { if err := syscall.Access(dir, syscall.O_RDWR); err == nil { break } else if os.IsPermission(err) || dir == "/" { - return fmt.Errorf("user[%s] path[%s] %v", basic.Username, cfg.Output, err) + return fmt.Errorf("user[%s] path[%s] %v", user.Username(), cfg.Output, err) } } return nil diff --git a/client/daemon/rpcserver/rpcserver.go b/client/daemon/rpcserver/rpcserver.go index 89d3fa99e..5542e7960 100644 --- a/client/daemon/rpcserver/rpcserver.go +++ b/client/daemon/rpcserver/rpcserver.go @@ -54,9 +54,9 @@ import ( "d7y.io/dragonfly/v2/client/util" "d7y.io/dragonfly/v2/internal/dferrors" logger "d7y.io/dragonfly/v2/internal/dflog" - "d7y.io/dragonfly/v2/pkg/basic" "d7y.io/dragonfly/v2/pkg/idgen" "d7y.io/dragonfly/v2/pkg/net/http" + "d7y.io/dragonfly/v2/pkg/os/user" dfdaemonserver "d7y.io/dragonfly/v2/pkg/rpc/dfdaemon/server" "d7y.io/dragonfly/v2/pkg/safe" "d7y.io/dragonfly/v2/pkg/source" @@ -796,7 +796,7 @@ func checkOutput(output string) error { return fmt.Errorf("path[%s] is not absolute path", output) } outputDir, _ := path.Split(output) - if err := config.MkdirAll(outputDir, 0777, basic.UserID, basic.UserGroup); err != nil { + if err := config.MkdirAll(outputDir, 0777, os.Getuid(), os.Getgid()); err != nil { return err } @@ -805,7 +805,7 @@ func checkOutput(output string) error { if err := syscall.Access(dir, syscall.O_RDWR); err == nil { break } else if os.IsPermission(err) || dir == "/" { - return fmt.Errorf("user[%s] path[%s] %v", basic.Username, output, err) + return fmt.Errorf("user[%s] path[%s] %v", user.Username(), output, err) } } return nil diff --git a/client/dfcache/dfcache.go b/client/dfcache/dfcache.go index 5b9983587..4debf9212 100644 --- a/client/dfcache/dfcache.go +++ b/client/dfcache/dfcache.go @@ -30,7 +30,6 @@ import ( "d7y.io/dragonfly/v2/client/config" "d7y.io/dragonfly/v2/internal/dferrors" logger "d7y.io/dragonfly/v2/internal/dflog" - "d7y.io/dragonfly/v2/pkg/basic" dfdaemonclient "d7y.io/dragonfly/v2/pkg/rpc/dfdaemon/client" ) @@ -236,8 +235,8 @@ func newExportRequest(cfg *config.DfcacheConfig) *dfdaemonv1.ExportTaskRequest { UrlMeta: &commonv1.UrlMeta{ Tag: cfg.Tag, }, - Uid: int64(basic.UserID), - Gid: int64(basic.UserGroup), + Uid: int64(os.Getuid()), + Gid: int64(os.Getgid()), LocalOnly: cfg.LocalOnly, } } diff --git a/client/dfget/dfget.go b/client/dfget/dfget.go index 84dd456dd..c7e423736 100644 --- a/client/dfget/dfget.go +++ b/client/dfget/dfget.go @@ -38,7 +38,6 @@ import ( "d7y.io/dragonfly/v2/client/config" logger "d7y.io/dragonfly/v2/internal/dflog" - "d7y.io/dragonfly/v2/pkg/basic" "d7y.io/dragonfly/v2/pkg/digest" dfdaemonclient "d7y.io/dragonfly/v2/pkg/rpc/dfdaemon/client" "d7y.io/dragonfly/v2/pkg/source" @@ -192,8 +191,8 @@ func downloadFromSource(ctx context.Context, cfg *config.DfgetConfig, hdr map[st } // change file owner - if err = os.Chown(target.Name(), basic.UserID, basic.UserGroup); err != nil { - return fmt.Errorf("change file owner to uid[%d] gid[%d]: %w", basic.UserID, basic.UserGroup, err) + if err = os.Chown(target.Name(), os.Getuid(), os.Getgid()); err != nil { + return fmt.Errorf("change file owner to uid[%d] gid[%d]: %w", os.Getuid(), os.Getgid(), err) } if err = os.Rename(target.Name(), cfg.Output); err != nil { @@ -243,8 +242,8 @@ func newDownRequest(cfg *config.DfgetConfig, hdr map[string]string) *dfdaemonv1. Application: cfg.Application, }, Pattern: cfg.Pattern, - Uid: int64(basic.UserID), - Gid: int64(basic.UserGroup), + Uid: int64(os.Getuid()), + Gid: int64(os.Getgid()), KeepOriginalOffset: cfg.KeepOriginalOffset, } } diff --git a/cmd/dfget/cmd/root.go b/cmd/dfget/cmd/root.go index e566d2441..5a3a749f5 100644 --- a/cmd/dfget/cmd/root.go +++ b/cmd/dfget/cmd/root.go @@ -35,10 +35,10 @@ import ( "d7y.io/dragonfly/v2/client/dfget" "d7y.io/dragonfly/v2/cmd/dependency" logger "d7y.io/dragonfly/v2/internal/dflog" - "d7y.io/dragonfly/v2/pkg/basic" "d7y.io/dragonfly/v2/pkg/dfnet" "d7y.io/dragonfly/v2/pkg/dfpath" "d7y.io/dragonfly/v2/pkg/net/ip" + "d7y.io/dragonfly/v2/pkg/os/user" "d7y.io/dragonfly/v2/pkg/rpc/dfdaemon/client" "d7y.io/dragonfly/v2/pkg/source" "d7y.io/dragonfly/v2/pkg/types" @@ -95,7 +95,7 @@ var rootCmd = &cobra.Command{ fmt.Printf("--%s-- %s\n", start.Format("2006-01-02 15:04:05"), dfgetConfig.URL) fmt.Printf("dfget version: %s\n", version.GitVersion) - fmt.Printf("current user: %s, default peer ip: %s\n", basic.Username, ip.IPv4) + fmt.Printf("current user: %s, default peer ip: %s\n", user.Username(), ip.IPv4) fmt.Printf("output path: %s\n", dfgetConfig.Output) // do get file diff --git a/pkg/basic/user.go b/pkg/basic/user.go deleted file mode 100644 index 0bf1f6b19..000000000 --- a/pkg/basic/user.go +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2020 The Dragonfly Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package basic - -import ( - "fmt" - "os" - "os/user" - "strconv" - "strings" - "syscall" - - logger "d7y.io/dragonfly/v2/internal/dflog" - pkgstrings "d7y.io/dragonfly/v2/pkg/strings" -) - -var ( - HomeDir string - TmpDir string - Username string - UserID int - UserGroup int -) - -func init() { - u, err := user.Current() - if err != nil { - 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) - - HomeDir = u.HomeDir - HomeDir = strings.TrimSpace(HomeDir) - if pkgstrings.IsBlank(HomeDir) { - panic("home dir is empty") - } - } - - TmpDir = os.TempDir() - TmpDir = strings.TrimSpace(TmpDir) - if pkgstrings.IsBlank(TmpDir) { - TmpDir = "/tmp" - } -} diff --git a/pkg/dfpath/dfpath_darwin.go b/pkg/dfpath/dfpath_darwin.go index 5087ca16c..e4c578f93 100644 --- a/pkg/dfpath/dfpath_darwin.go +++ b/pkg/dfpath/dfpath_darwin.go @@ -21,10 +21,10 @@ package dfpath import ( "path/filepath" - "d7y.io/dragonfly/v2/pkg/basic" + "d7y.io/dragonfly/v2/pkg/os/user" ) -var DefaultWorkHome = filepath.Join(basic.HomeDir, ".dragonfly") +var DefaultWorkHome = filepath.Join(user.HomeDir(), ".dragonfly") var DefaultCacheDir = filepath.Join(DefaultWorkHome, "cache") var DefaultConfigDir = filepath.Join(DefaultWorkHome, "config") var DefaultLogDir = filepath.Join(DefaultWorkHome, "logs") diff --git a/pkg/digest/digest_test.go b/pkg/digest/digest_test.go index 80dec4ded..8a30842e1 100644 --- a/pkg/digest/digest_test.go +++ b/pkg/digest/digest_test.go @@ -28,8 +28,6 @@ import ( "github.com/google/uuid" "github.com/stretchr/testify/assert" - - "d7y.io/dragonfly/v2/pkg/basic" ) func TestSha256(t *testing.T) { @@ -56,7 +54,7 @@ func TestMd5Reader(t *testing.T) { func TestHashFile(t *testing.T) { var expected = "5d41402abc4b2a76b9719d911017c592" - path := filepath.Join(basic.TmpDir, uuid.NewString()) + path := filepath.Join(os.TempDir(), uuid.NewString()) f, err := os.OpenFile(path, syscall.O_CREAT|syscall.O_TRUNC|syscall.O_RDWR, fs.FileMode(0644)) assert.Nil(t, err) defer f.Close() diff --git a/pkg/os/user/user.go b/pkg/os/user/user.go new file mode 100644 index 000000000..13d20ec06 --- /dev/null +++ b/pkg/os/user/user.go @@ -0,0 +1,47 @@ +/* + * Copyright 2022 The Dragonfly Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package user + +import ( + "fmt" + "os" + "os/user" + + logger "d7y.io/dragonfly/v2/internal/dflog" +) + +// HomeDir is the path to the user's home directory. +func HomeDir() string { + u, err := user.Current() + if err != nil { + logger.Warnf("Failed to get current user: %s. Use / as HomeDir", err.Error()) + return "/" + } + + return u.HomeDir +} + +// Username is the username. +func Username() string { + u, err := user.Current() + if err != nil { + logger.Warnf("Failed to get current user: %s. Use os.Getuid() as username", err.Error()) + return fmt.Sprint(os.Getuid()) + } + + return u.Username +}