From f499d665986d8d7398e10e3e64a55395c7328f21 Mon Sep 17 00:00:00 2001 From: Gaius Date: Mon, 11 Jul 2022 10:39:00 +0800 Subject: [PATCH] fix: dfpath creates redundant directories (#1446) Signed-off-by: Gaius --- cmd/dfget/cmd/daemon.go | 16 +++++--- cmd/scheduler/cmd/root.go | 4 +- go.mod | 2 +- pkg/dfpath/dfpath.go | 76 +++++++++++++++++++++++++------------ pkg/dfpath/dfpath_darwin.go | 1 + pkg/dfpath/dfpath_linux.go | 1 + pkg/dfpath/dfpath_test.go | 43 +++++++++++++++------ 7 files changed, 100 insertions(+), 43 deletions(-) diff --git a/cmd/dfget/cmd/daemon.go b/cmd/dfget/cmd/daemon.go index d7adab587..13a0b5594 100644 --- a/cmd/dfget/cmd/daemon.go +++ b/cmd/dfget/cmd/daemon.go @@ -104,17 +104,21 @@ func initDaemonDfpath(cfg *config.DaemonOption) (dfpath.Dfpath, error) { options = append(options, dfpath.WithWorkHome(cfg.WorkHome)) } - if cfg.CacheDir != "" { - options = append(options, dfpath.WithCacheDir(cfg.CacheDir)) - } - if cfg.LogDir != "" { options = append(options, dfpath.WithLogDir(cfg.LogDir)) } - if cfg.DataDir != "" { - options = append(options, dfpath.WithDataDir(cfg.DataDir)) + cacheDir := dfpath.DefaultCacheDir + if cfg.CacheDir != "" { + cacheDir = cfg.CacheDir } + options = append(options, dfpath.WithCacheDir(cacheDir)) + + dataDir := dfpath.DefaultDataDir + if cfg.DataDir != "" { + dataDir = cfg.DataDir + } + options = append(options, dfpath.WithDataDir(dataDir)) return dfpath.New(options...) } diff --git a/cmd/scheduler/cmd/root.go b/cmd/scheduler/cmd/root.go index b6d90b825..b6fa910d7 100644 --- a/cmd/scheduler/cmd/root.go +++ b/cmd/scheduler/cmd/root.go @@ -93,9 +93,11 @@ func initDfpath(cfg *config.ServerConfig) (dfpath.Dfpath, error) { options = append(options, dfpath.WithLogDir(cfg.LogDir)) } + cacheDir := dfpath.DefaultCacheDir if cfg.CacheDir != "" { - options = append(options, dfpath.WithCacheDir(cfg.CacheDir)) + cacheDir = cfg.CacheDir } + options = append(options, dfpath.WithCacheDir(cacheDir)) return dfpath.New(options...) } diff --git a/go.mod b/go.mod index 6e4c3de33..87e998c63 100644 --- a/go.mod +++ b/go.mod @@ -35,6 +35,7 @@ require ( github.com/google/uuid v1.3.0 github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 + github.com/hashicorp/go-multierror v1.1.1 github.com/jarcoal/httpmock v1.0.8 github.com/looplab/fsm v0.3.0 github.com/mcuadros/go-gin-prometheus v0.1.0 @@ -122,7 +123,6 @@ require ( github.com/googleapis/enterprise-certificate-proxy v0.1.0 // indirect github.com/googleapis/gax-go/v2 v2.4.0 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect - github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/go-uuid v1.0.3 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect diff --git a/pkg/dfpath/dfpath.go b/pkg/dfpath/dfpath.go index a55485299..f3052ea1b 100644 --- a/pkg/dfpath/dfpath.go +++ b/pkg/dfpath/dfpath.go @@ -19,14 +19,15 @@ package dfpath import ( - "fmt" "io/fs" "os" "path/filepath" "sync" + + "github.com/hashicorp/go-multierror" ) -// Dfpath is the interface used for init project path +// Dfpath is the interface used for init project path. type Dfpath interface { WorkHome() string CacheDir() string @@ -38,7 +39,7 @@ type Dfpath interface { DfgetLockPath() string } -// Dfpath provides init project path function +// Dfpath provides init project path function. type dfpath struct { workHome string cacheDir string @@ -50,76 +51,103 @@ type dfpath struct { dfgetLockPath string } -// Cache of the dfpath +// Cache of the dfpath. var cache struct { sync.Once - d *dfpath - errs []error + d *dfpath + err *multierror.Error } -// Option is a functional option for configuring the dfpath +// Option is a functional option for configuring the dfpath. type Option func(d *dfpath) -// WithWorkHome set the workhome directory +// WithWorkHome set the workhome directory. func WithWorkHome(dir string) Option { return func(d *dfpath) { d.workHome = dir } } -// WithCacheDir set the cache directory +// WithCacheDir set the cache directory. func WithCacheDir(dir string) Option { return func(d *dfpath) { d.cacheDir = dir } } -// WithLogDir set the log directory +// WithLogDir set the log directory. func WithLogDir(dir string) Option { return func(d *dfpath) { d.logDir = dir } } -// WithDataDir set download data directory +// WithDataDir set download data directory. func WithDataDir(dir string) Option { return func(d *dfpath) { d.dataDir = dir } } -// New returns a new dfpath interface +// WithPluginDir set plugin directory. +func WithPluginDir(dir string) Option { + return func(d *dfpath) { + d.pluginDir = dir + } +} + +// New returns a new dfpath interface. func New(options ...Option) (Dfpath, error) { cache.Do(func() { d := &dfpath{ - workHome: DefaultWorkHome, - cacheDir: DefaultCacheDir, - logDir: DefaultLogDir, - dataDir: DefaultDataDir, + workHome: DefaultWorkHome, + logDir: DefaultLogDir, + pluginDir: DefaultPluginDir, } for _, opt := range options { opt(d) } - d.pluginDir = filepath.Join(d.workHome, "plugins") + // Initialize dfdaemon path. d.daemonSockPath = filepath.Join(d.workHome, "daemon.sock") d.daemonLockPath = filepath.Join(d.workHome, "daemon.lock") d.dfgetLockPath = filepath.Join(d.workHome, "dfget.lock") - // Create directories - for name, dir := range map[string]string{"workHome": d.workHome, "cacheDir": d.cacheDir, "logDir": d.logDir, "dataDir": d.dataDir, - "pluginDir": d.pluginDir} { - if err := os.MkdirAll(dir, fs.FileMode(0755)); err != nil { - cache.errs = append(cache.errs, fmt.Errorf("create %s dir %s failed: %v", name, dir, err)) + // Create workhome directory. + if err := os.MkdirAll(d.workHome, fs.FileMode(0755)); err != nil { + cache.err = multierror.Append(cache.err, err) + } + + // Create log directory. + if err := os.MkdirAll(d.logDir, fs.FileMode(0755)); err != nil { + cache.err = multierror.Append(cache.err, err) + } + + // Create plugin directory. + if err := os.MkdirAll(d.pluginDir, fs.FileMode(0755)); err != nil { + cache.err = multierror.Append(cache.err, err) + } + + // Create cache directory. + if d.cacheDir != "" { + if err := os.MkdirAll(d.cacheDir, fs.FileMode(0755)); err != nil { + cache.err = multierror.Append(cache.err, err) + } + } + + // Create data directory. + if d.dataDir != "" { + if err := os.MkdirAll(d.dataDir, fs.FileMode(0755)); err != nil { + cache.err = multierror.Append(cache.err, err) } } cache.d = d }) - if len(cache.errs) > 0 { - return nil, fmt.Errorf("create dfpath failed: %s", cache.errs) + if cache.err.ErrorOrNil() != nil { + return nil, cache.err } d := *cache.d diff --git a/pkg/dfpath/dfpath_darwin.go b/pkg/dfpath/dfpath_darwin.go index 41590a72f..1e52f44f2 100644 --- a/pkg/dfpath/dfpath_darwin.go +++ b/pkg/dfpath/dfpath_darwin.go @@ -30,3 +30,4 @@ var DefaultCacheDir = filepath.Join(DefaultWorkHome, "cache") var DefaultConfigDir = filepath.Join(DefaultWorkHome, "config") var DefaultLogDir = filepath.Join(DefaultWorkHome, "logs") var DefaultDataDir = filepath.Join(DefaultWorkHome, "data") +var DefaultPluginDir = filepath.Join(DefaultWorkHome, "plugins") diff --git a/pkg/dfpath/dfpath_linux.go b/pkg/dfpath/dfpath_linux.go index 589f72fc1..623504898 100644 --- a/pkg/dfpath/dfpath_linux.go +++ b/pkg/dfpath/dfpath_linux.go @@ -24,3 +24,4 @@ var DefaultCacheDir = "/var/cache/dragonfly" var DefaultConfigDir = "/etc/dragonfly" var DefaultLogDir = "/var/log/dragonfly" var DefaultDataDir = "/var/lib/dragonfly" +var DefaultPluginDir = "/usr/local/dragonfly/plugins" diff --git a/pkg/dfpath/dfpath_test.go b/pkg/dfpath/dfpath_test.go index 169750283..c5ac694f3 100644 --- a/pkg/dfpath/dfpath_test.go +++ b/pkg/dfpath/dfpath_test.go @@ -20,6 +20,7 @@ import ( "sync" "testing" + "github.com/hashicorp/go-multierror" "github.com/stretchr/testify/assert" ) @@ -43,13 +44,14 @@ func TestNew(t *testing.T) { expect: func(t *testing.T, options []Option) { assert := assert.New(t) cache.Once = sync.Once{} - cache.errs = []error{} + cache.err = &multierror.Error{} d, err := New(options...) assert.NoError(err) assert.Equal(d.WorkHome(), DefaultWorkHome) - assert.Equal(d.CacheDir(), DefaultCacheDir) + assert.Equal(d.CacheDir(), "") assert.Equal(d.LogDir(), DefaultLogDir) - assert.Equal(d.DataDir(), DefaultDataDir) + assert.Equal(d.DataDir(), "") + assert.Equal(d.PluginDir(), DefaultPluginDir) }, }, { @@ -58,13 +60,14 @@ func TestNew(t *testing.T) { expect: func(t *testing.T, options []Option) { assert := assert.New(t) cache.Once = sync.Once{} - cache.errs = []error{} + cache.err = &multierror.Error{} d, err := New(options...) assert.NoError(err) assert.Equal(d.WorkHome(), "foo") - assert.Equal(d.CacheDir(), DefaultCacheDir) + assert.Equal(d.CacheDir(), "") assert.Equal(d.LogDir(), DefaultLogDir) - assert.Equal(d.DataDir(), DefaultDataDir) + assert.Equal(d.DataDir(), "") + assert.Equal(d.PluginDir(), DefaultPluginDir) }, }, { @@ -73,13 +76,14 @@ func TestNew(t *testing.T) { expect: func(t *testing.T, options []Option) { assert := assert.New(t) cache.Once = sync.Once{} - cache.errs = []error{} + cache.err = &multierror.Error{} d, err := New(options...) assert.NoError(err) assert.Equal(d.WorkHome(), DefaultWorkHome) assert.Equal(d.CacheDir(), "foo") assert.Equal(d.LogDir(), DefaultLogDir) - assert.Equal(d.DataDir(), DefaultDataDir) + assert.Equal(d.DataDir(), "") + assert.Equal(d.PluginDir(), DefaultPluginDir) }, }, { @@ -88,13 +92,30 @@ func TestNew(t *testing.T) { expect: func(t *testing.T, options []Option) { assert := assert.New(t) cache.Once = sync.Once{} - cache.errs = []error{} + cache.err = &multierror.Error{} d, err := New(options...) assert.NoError(err) assert.Equal(d.WorkHome(), DefaultWorkHome) - assert.Equal(d.CacheDir(), DefaultCacheDir) + assert.Equal(d.CacheDir(), "") assert.Equal(d.LogDir(), "foo") - assert.Equal(d.DataDir(), DefaultDataDir) + assert.Equal(d.DataDir(), "") + assert.Equal(d.PluginDir(), DefaultPluginDir) + }, + }, + { + name: "new dfpath by pluginDir", + options: []Option{WithPluginDir("foo")}, + expect: func(t *testing.T, options []Option) { + assert := assert.New(t) + cache.Once = sync.Once{} + cache.err = &multierror.Error{} + d, err := New(options...) + assert.NoError(err) + assert.Equal(d.WorkHome(), DefaultWorkHome) + assert.Equal(d.CacheDir(), "") + assert.Equal(d.LogDir(), DefaultLogDir) + assert.Equal(d.DataDir(), "") + assert.Equal(d.PluginDir(), "foo") }, }, }