diff --git a/client/pkg/fileutil/fileutil.go b/client/pkg/fileutil/fileutil.go index e442c3c92e8..d31ece3e24e 100644 --- a/client/pkg/fileutil/fileutil.go +++ b/client/pkg/fileutil/fileutil.go @@ -44,16 +44,12 @@ func IsDirWriteable(dir string) error { // TouchDirAll is similar to os.MkdirAll. It creates directories with 0700 permission if any directory // does not exists. TouchDirAll also ensures the given directory is writable. -func TouchDirAll(dir string) error { +func TouchDirAll(lg *zap.Logger, dir string) error { // If path is already a directory, MkdirAll does nothing and returns nil, so, // first check if dir exist with an expected permission mode. if Exist(dir) { err := CheckDirPermission(dir, PrivateDirMode) if err != nil { - lg, _ := zap.NewProduction() - if lg == nil { - lg = zap.NewExample() - } lg.Warn("check file permission", zap.Error(err)) } } else { @@ -70,8 +66,8 @@ func TouchDirAll(dir string) error { // CreateDirAll is similar to TouchDirAll but returns error // if the deepest directory was not empty. -func CreateDirAll(dir string) error { - err := TouchDirAll(dir) +func CreateDirAll(lg *zap.Logger, dir string) error { + err := TouchDirAll(lg, dir) if err == nil { var ns []string ns, err = ReadDir(dir) diff --git a/client/pkg/fileutil/fileutil_test.go b/client/pkg/fileutil/fileutil_test.go index 3a761ff9a7f..1804163823f 100644 --- a/client/pkg/fileutil/fileutil_test.go +++ b/client/pkg/fileutil/fileutil_test.go @@ -67,7 +67,7 @@ func TestCreateDirAll(t *testing.T) { defer os.RemoveAll(tmpdir) tmpdir2 := filepath.Join(tmpdir, "testdir") - if err = CreateDirAll(tmpdir2); err != nil { + if err = CreateDirAll(zaptest.NewLogger(t), tmpdir2); err != nil { t.Fatal(err) } @@ -75,7 +75,7 @@ func TestCreateDirAll(t *testing.T) { t.Fatal(err) } - if err = CreateDirAll(tmpdir2); err == nil || !strings.Contains(err.Error(), "to be empty, got") { + if err = CreateDirAll(zaptest.NewLogger(t), tmpdir2); err == nil || !strings.Contains(err.Error(), "to be empty, got") { t.Fatalf("unexpected error %v", err) } } @@ -186,7 +186,7 @@ func TestDirPermission(t *testing.T) { tmpdir2 := filepath.Join(tmpdir, "testpermission") // create a new dir with 0700 - if err = CreateDirAll(tmpdir2); err != nil { + if err = CreateDirAll(zaptest.NewLogger(t), tmpdir2); err != nil { t.Fatal(err) } // check dir permission with mode different than created dir diff --git a/client/pkg/transport/listener.go b/client/pkg/transport/listener.go index e8f475eb824..c3bc56a65b5 100644 --- a/client/pkg/transport/listener.go +++ b/client/pkg/transport/listener.go @@ -205,7 +205,7 @@ func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertVali ) return } - err = fileutil.TouchDirAll(dirpath) + err = fileutil.TouchDirAll(lg, dirpath) if err != nil { if info.Logger != nil { info.Logger.Warn( diff --git a/etcdutl/etcdutl/backup_command.go b/etcdutl/etcdutl/backup_command.go index 6cd243f3811..0fa1879ed4f 100644 --- a/etcdutl/etcdutl/backup_command.go +++ b/etcdutl/etcdutl/backup_command.go @@ -114,7 +114,7 @@ func HandleBackup(withV3 bool, srcDir string, destDir string, srcWAL string, des destWAL = datadir.ToWalDir(destDir) } - if err := fileutil.CreateDirAll(destSnap); err != nil { + if err := fileutil.CreateDirAll(lg, destSnap); err != nil { lg.Fatal("failed creating backup snapshot dir", zap.String("dest-snap", destSnap), zap.Error(err)) } diff --git a/etcdutl/snapshot/v3_snapshot.go b/etcdutl/snapshot/v3_snapshot.go index b17cd90aac1..7961977f18a 100644 --- a/etcdutl/snapshot/v3_snapshot.go +++ b/etcdutl/snapshot/v3_snapshot.go @@ -322,7 +322,7 @@ func (s *v3Manager) copyAndVerifyDB() error { return err } - if err := fileutil.CreateDirAll(s.snapDir); err != nil { + if err := fileutil.CreateDirAll(s.lg, s.snapDir); err != nil { return err } @@ -383,7 +383,7 @@ func (s *v3Manager) copyAndVerifyDB() error { // // TODO: This code ignores learners !!! func (s *v3Manager) saveWALAndSnap() (*raftpb.HardState, error) { - if err := fileutil.CreateDirAll(s.walDir); err != nil { + if err := fileutil.CreateDirAll(s.lg, s.walDir); err != nil { return nil, err } diff --git a/server/etcdmain/etcd.go b/server/etcdmain/etcd.go index 470eb83be98..2ff8d9173a5 100644 --- a/server/etcdmain/etcd.go +++ b/server/etcdmain/etcd.go @@ -276,7 +276,7 @@ func startProxy(cfg *config) error { } cfg.ec.Dir = filepath.Join(cfg.ec.Dir, "proxy") - err = fileutil.TouchDirAll(cfg.ec.Dir) + err = fileutil.TouchDirAll(lg, cfg.ec.Dir) if err != nil { return err } diff --git a/server/etcdserver/server.go b/server/etcdserver/server.go index cbf11738a7f..7f624fc0d42 100644 --- a/server/etcdserver/server.go +++ b/server/etcdserver/server.go @@ -349,13 +349,13 @@ func NewServer(cfg config.ServerConfig) (srv *EtcdServer, err error) { ) } - if terr := fileutil.TouchDirAll(cfg.DataDir); terr != nil { + if terr := fileutil.TouchDirAll(cfg.Logger, cfg.DataDir); terr != nil { return nil, fmt.Errorf("cannot access data directory: %v", terr) } haveWAL := wal.Exist(cfg.WALDir()) - if err = fileutil.TouchDirAll(cfg.SnapDir()); err != nil { + if err = fileutil.TouchDirAll(cfg.Logger, cfg.SnapDir()); err != nil { cfg.Logger.Fatal( "failed to create snapshot directory", zap.String("path", cfg.SnapDir()), @@ -548,7 +548,7 @@ func NewServer(cfg config.ServerConfig) (srv *EtcdServer, err error) { return nil, fmt.Errorf("unsupported bootstrap config") } - if terr := fileutil.TouchDirAll(cfg.MemberDir()); terr != nil { + if terr := fileutil.TouchDirAll(cfg.Logger, cfg.MemberDir()); terr != nil { return nil, fmt.Errorf("cannot access member directory: %v", terr) } diff --git a/server/wal/wal.go b/server/wal/wal.go index 0d652220ecf..01d0c28d6ba 100644 --- a/server/wal/wal.go +++ b/server/wal/wal.go @@ -115,7 +115,7 @@ func Create(lg *zap.Logger, dirpath string, metadata []byte) (*WAL, error) { } defer os.RemoveAll(tmpdirpath) - if err := fileutil.CreateDirAll(tmpdirpath); err != nil { + if err := fileutil.CreateDirAll(lg, tmpdirpath); err != nil { lg.Warn( "failed to create a temporary WAL directory", zap.String("tmp-dir-path", tmpdirpath), diff --git a/tests/functional/agent/handler.go b/tests/functional/agent/handler.go index 767a48ad137..3f5c62cd31c 100644 --- a/tests/functional/agent/handler.go +++ b/tests/functional/agent/handler.go @@ -473,7 +473,7 @@ func (srv *Server) handle_INITIAL_START_ETCD(req *rpcpb.Request) (*rpcpb.Respons }, nil } - err := fileutil.TouchDirAll(srv.Member.BaseDir) + err := fileutil.TouchDirAll(srv.lg, srv.Member.BaseDir) if err != nil { return nil, err } @@ -508,7 +508,7 @@ func (srv *Server) handle_INITIAL_START_ETCD(req *rpcpb.Request) (*rpcpb.Respons func (srv *Server) handle_RESTART_ETCD(req *rpcpb.Request) (*rpcpb.Response, error) { var err error if !fileutil.Exist(srv.Member.BaseDir) { - err = fileutil.TouchDirAll(srv.Member.BaseDir) + err = fileutil.TouchDirAll(srv.lg, srv.Member.BaseDir) if err != nil { return nil, err } @@ -579,7 +579,7 @@ func (srv *Server) handle_SIGQUIT_ETCD_AND_REMOVE_DATA() (*rpcpb.Response, error // create a new log file for next new member restart if !fileutil.Exist(srv.Member.BaseDir) { - err = fileutil.TouchDirAll(srv.Member.BaseDir) + err = fileutil.TouchDirAll(srv.lg, srv.Member.BaseDir) if err != nil { return nil, err } @@ -651,6 +651,7 @@ func (srv *Server) handle_SIGQUIT_ETCD_AND_ARCHIVE_DATA() (*rpcpb.Response, erro // TODO: support separate WAL directory if err = archive( + srv.lg, srv.Member.BaseDir, srv.Member.Etcd.LogOutputs[0], srv.Member.Etcd.DataDir, diff --git a/tests/functional/agent/utils.go b/tests/functional/agent/utils.go index 37a2c7adfae..2cd888ed0e0 100644 --- a/tests/functional/agent/utils.go +++ b/tests/functional/agent/utils.go @@ -25,15 +25,17 @@ import ( "time" "go.etcd.io/etcd/client/pkg/v3/fileutil" + + "go.uber.org/zap" ) // TODO: support separate WAL directory -func archive(baseDir, etcdLogPath, dataDir string) error { +func archive(lg *zap.Logger, baseDir, etcdLogPath, dataDir string) error { dir := filepath.Join(baseDir, "etcd-failure-archive", time.Now().Format(time.RFC3339)) if existDir(dir) { dir = filepath.Join(baseDir, "etcd-failure-archive", time.Now().Add(time.Second).Format(time.RFC3339)) } - if err := fileutil.TouchDirAll(dir); err != nil { + if err := fileutil.TouchDirAll(lg, dir); err != nil { return err } diff --git a/tests/functional/tester/cluster.go b/tests/functional/tester/cluster.go index 08c70afe994..cebcfd77d49 100644 --- a/tests/functional/tester/cluster.go +++ b/tests/functional/tester/cluster.go @@ -520,7 +520,7 @@ func (clus *Cluster) sendOpWithResp(idx int, op rpcpb.Operation) (*rpcpb.Respons "fixtures", "client", ) - if err = fileutil.TouchDirAll(dirClient); err != nil { + if err = fileutil.TouchDirAll(clus.lg, dirClient); err != nil { return nil, err } diff --git a/tests/functional/tester/cluster_run.go b/tests/functional/tester/cluster_run.go index ac65ebca3e9..4434155810f 100644 --- a/tests/functional/tester/cluster_run.go +++ b/tests/functional/tester/cluster_run.go @@ -37,7 +37,7 @@ func (clus *Cluster) Run() { // needs to obtain all the failpoints from the etcd member. clus.updateCases() - if err := fileutil.TouchDirAll(clus.Tester.DataDir); err != nil { + if err := fileutil.TouchDirAll(clus.lg, clus.Tester.DataDir); err != nil { clus.lg.Panic( "failed to create test data directory", zap.String("dir", clus.Tester.DataDir),