Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
using path/filepath instead of path for file operations
Browse files Browse the repository at this point in the history
Signed-off-by: SataQiu <[email protected]>
  • Loading branch information
SataQiu committed Nov 6, 2019
1 parent 7300a6b commit 0f654c1
Show file tree
Hide file tree
Showing 18 changed files with 79 additions and 84 deletions.
10 changes: 5 additions & 5 deletions cmd/dfget/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"fmt"
"os"
"os/user"
"path"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -169,11 +169,11 @@ func initProperties() ([]*propertiesResult, error) {
if cfg.WorkHome == "" {
cfg.WorkHome = properties.WorkHome
if cfg.WorkHome == "" {
cfg.WorkHome = path.Join(currentUser.HomeDir, ".small-dragonfly")
cfg.WorkHome = filepath.Join(currentUser.HomeDir, ".small-dragonfly")
}
}
cfg.RV.MetaPath = path.Join(cfg.WorkHome, "meta", "host.meta")
cfg.RV.SystemDataDir = path.Join(cfg.WorkHome, "data")
cfg.RV.MetaPath = filepath.Join(cfg.WorkHome, "meta", "host.meta")
cfg.RV.SystemDataDir = filepath.Join(cfg.WorkHome, "data")
cfg.RV.FileLength = -1

return results, nil
Expand All @@ -185,7 +185,7 @@ func initProperties() ([]*propertiesResult, error) {
// while console log will output the dfget client's log in console/terminal for
// debugging usage.
func initClientLog() error {
logFilePath := path.Join(cfg.WorkHome, "logs", "dfclient.log")
logFilePath := filepath.Join(cfg.WorkHome, "logs", "dfclient.log")

opts := []dflog.Option{
dflog.WithLogFile(logFilePath),
Expand Down
4 changes: 2 additions & 2 deletions cmd/dfget/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package app

import (
"path"
"path/filepath"

"github.com/dragonflyoss/Dragonfly/dfget/config"
"github.com/dragonflyoss/Dragonfly/dfget/core/uploader"
Expand Down Expand Up @@ -82,7 +82,7 @@ func runServer() error {
}

func initServerLog() error {
logFilePath := path.Join(cfg.WorkHome, "logs", "dfserver.log")
logFilePath := filepath.Join(cfg.WorkHome, "logs", "dfserver.log")

opts := []dflog.Option{
dflog.WithLogFile(logFilePath),
Expand Down
3 changes: 1 addition & 2 deletions cmd/supernode/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package app
import (
"fmt"
"os"
"path"
"path/filepath"
"reflect"
"time"
Expand Down Expand Up @@ -318,7 +317,7 @@ func decodeWithYAML(types ...reflect.Type) mapstructure.DecodeHookFunc {

// initLog initializes log Level and log format.
func initLog(logger *logrus.Logger, logPath string) error {
logFilePath := path.Join(supernodeViper.GetString("base.homeDir"), "logs", logPath)
logFilePath := filepath.Join(supernodeViper.GetString("base.homeDir"), "logs", logPath)

opts := []dflog.Option{
dflog.WithLogFile(logFilePath),
Expand Down
3 changes: 1 addition & 2 deletions dfget/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"io/ioutil"
"os"
"os/user"
"path"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -84,7 +83,7 @@ func (suite *ConfigSuite) TestNewConfig(c *check.C) {

if curUser, err := user.Current(); err != nil {
c.Assert(cfg.User, check.Equals, curUser.Username)
c.Assert(cfg.WorkHome, check.Equals, path.Join(curUser.HomeDir, ".small-dragonfly"))
c.Assert(cfg.WorkHome, check.Equals, filepath.Join(curUser.HomeDir, ".small-dragonfly"))
}
}

Expand Down
7 changes: 3 additions & 4 deletions dfget/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"io/ioutil"
"math/rand"
"os"
"path"
"path/filepath"
"strconv"
"time"
Expand Down Expand Up @@ -89,7 +88,7 @@ func prepare(cfg *config.Config) (err error) {
rv := &cfg.RV

rv.RealTarget = cfg.Output
rv.TargetDir = path.Dir(rv.RealTarget)
rv.TargetDir = filepath.Dir(rv.RealTarget)
if err = fileutils.CreateDirectory(rv.TargetDir); err != nil {
return err
}
Expand All @@ -98,7 +97,7 @@ func prepare(cfg *config.Config) (err error) {
return err
}

if err = fileutils.CreateDirectory(path.Dir(rv.MetaPath)); err != nil {
if err = fileutils.CreateDirectory(filepath.Dir(rv.MetaPath)); err != nil {
return err
}
if err = fileutils.CreateDirectory(cfg.WorkHome); err != nil {
Expand Down Expand Up @@ -238,7 +237,7 @@ func createTempTargetFile(targetDir string, sign string) (name string, e error)
return f.Name(), e
}

f, e = os.OpenFile(path.Join(targetDir, fmt.Sprintf("%s%d", prefix, rand.Uint64())),
f, e = os.OpenFile(filepath.Join(targetDir, fmt.Sprintf("%s%d", prefix, rand.Uint64())),
os.O_CREATE|os.O_EXCL, 0755)
if e == nil {
return f.Name(), e
Expand Down
4 changes: 2 additions & 2 deletions dfget/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"math/rand"
"net"
"os"
"path"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -65,7 +65,7 @@ func (s *CoreTestSuite) TearDownSuite(c *check.C) {
func (s *CoreTestSuite) TestPrepare(c *check.C) {
buf := &bytes.Buffer{}
cfg := s.createConfig(buf)
cfg.Output = path.Join(s.workHome, "test.output")
cfg.Output = filepath.Join(s.workHome, "test.output")

err := prepare(cfg)
fmt.Printf("%s\nerror:%v", buf.String(), err)
Expand Down
8 changes: 4 additions & 4 deletions dfget/core/downloader/back_downloader/back_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"

"github.com/dragonflyoss/Dragonfly/dfget/config"
"github.com/dragonflyoss/Dragonfly/dfget/core/downloader"
Expand Down Expand Up @@ -90,13 +90,13 @@ func (bd *BackDownloader) Run() error {
return err
}

printer.Printf("start download %s from the source station", path.Base(bd.Target))
logrus.Infof("start download %s from the source station", path.Base(bd.Target))
printer.Printf("start download %s from the source station", filepath.Base(bd.Target))
logrus.Infof("start download %s from the source station", filepath.Base(bd.Target))

defer bd.Cleanup()

prefix := "backsource." + bd.cfg.Sign + "."
if f, err = ioutil.TempFile(path.Dir(bd.Target), prefix); err != nil {
if f, err = ioutil.TempFile(filepath.Dir(bd.Target), prefix); err != nil {
return err
}
bd.tempFileName = f.Name()
Expand Down
8 changes: 4 additions & 4 deletions dfget/core/downloader/back_downloader/back_downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"net"
"net/http"
"os"
"path"
"path/filepath"
"testing"

"github.com/dragonflyoss/Dragonfly/dfget/config"
Expand Down Expand Up @@ -64,8 +64,8 @@ func (s *BackDownloaderTestSuite) TearDownSuite(c *check.C) {
}

func (s *BackDownloaderTestSuite) TestBackDownloader_Run(c *check.C) {
testFileMd5 := helper.CreateTestFileWithMD5(path.Join(s.workHome, "download.test"), "test downloader")
dst := path.Join(s.workHome, "back.test")
testFileMd5 := helper.CreateTestFileWithMD5(filepath.Join(s.workHome, "download.test"), "test downloader")
dst := filepath.Join(s.workHome, "back.test")

cfg := helper.CreateConfig(nil, s.workHome)
bd := &BackDownloader{
Expand Down Expand Up @@ -102,7 +102,7 @@ func (s *BackDownloaderTestSuite) TestBackDownloader_Run(c *check.C) {
}

func (s *BackDownloaderTestSuite) TestBackDownloader_Run_NotExist(c *check.C) {
dst := path.Join(s.workHome, "back.test")
dst := filepath.Join(s.workHome, "back.test")

cfg := helper.CreateConfig(nil, s.workHome)
bd := &BackDownloader{
Expand Down
6 changes: 3 additions & 3 deletions dfget/core/downloader/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package downloader
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -56,8 +56,8 @@ func (s *DownloaderTestSuite) TestMoveFile(c *check.C) {
tmp, _ := ioutil.TempDir("/tmp", "dfget-TestMoveFile-")
defer os.RemoveAll(tmp)

src := path.Join(tmp, "a")
dst := path.Join(tmp, "b")
src := filepath.Join(tmp, "a")
dst := filepath.Join(tmp, "b")
md5str := helper.CreateTestFileWithMD5(src, "hello")

err := MoveFile(src, dst, "x")
Expand Down
4 changes: 2 additions & 2 deletions dfget/core/downloader/p2p_downloader/client_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"

"github.com/dragonflyoss/Dragonfly/pkg/fileutils"

Expand All @@ -39,7 +39,7 @@ func init() {

func (s *ClientWriterTestSuite) SetUpSuite(c *check.C) {
s.workHome, _ = ioutil.TempDir("/tmp", "dfget-ClientWriterTestSuite-")
serviceFilePath := path.Join(s.workHome, "cwtest.service")
serviceFilePath := filepath.Join(s.workHome, "cwtest.service")
s.serviceFile, _ = fileutils.OpenFile(serviceFilePath, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0755)
}

Expand Down
8 changes: 4 additions & 4 deletions dfget/core/helper/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"io/ioutil"
"math/rand"
"os"
"path"
"path/filepath"

api_types "github.com/dragonflyoss/Dragonfly/apis/types"
"github.com/dragonflyoss/Dragonfly/dfget/config"
Expand All @@ -41,9 +41,9 @@ func CreateConfig(writer io.Writer, workHome string) *config.Config {
}
cfg := config.NewConfig()
cfg.WorkHome = workHome
cfg.RV.MetaPath = path.Join(cfg.WorkHome, "meta", "host.meta")
cfg.RV.SystemDataDir = path.Join(cfg.WorkHome, "data")
fileutils.CreateDirectory(path.Dir(cfg.RV.MetaPath))
cfg.RV.MetaPath = filepath.Join(cfg.WorkHome, "meta", "host.meta")
cfg.RV.SystemDataDir = filepath.Join(cfg.WorkHome, "data")
fileutils.CreateDirectory(filepath.Dir(cfg.RV.MetaPath))
fileutils.CreateDirectory(cfg.RV.SystemDataDir)

logrus.StandardLogger().Out = writer
Expand Down
4 changes: 2 additions & 2 deletions dfget/core/uploader/peer_server_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"strings"

Expand All @@ -47,7 +47,7 @@ type PeerServerExecutorTestSuite struct {

func (s *PeerServerExecutorTestSuite) SetUpSuite(c *check.C) {
s.workHome, _ = ioutil.TempDir("/tmp", "dfget-PeerServerTestSuite-")
s.script = path.Join(s.workHome, "script.sh")
s.script = filepath.Join(s.workHome, "script.sh")
s.writeScript("")
s.start()
}
Expand Down
6 changes: 3 additions & 3 deletions dfget/core/uploader/uploader_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"

"github.com/dragonflyoss/Dragonfly/dfget/config"
"github.com/dragonflyoss/Dragonfly/version"
Expand Down Expand Up @@ -86,7 +86,7 @@ func (s *UploaderUtilTestSuite) TestGeneratePort(c *check.C) {
}

func (s *UploaderUtilTestSuite) TestGetPort(c *check.C) {
metaPath := path.Join(s.workHome, "meta")
metaPath := filepath.Join(s.workHome, "meta")
port := getPortFromMeta(metaPath)
c.Assert(port, check.Equals, 0)

Expand All @@ -102,7 +102,7 @@ func (s *UploaderUtilTestSuite) TestGetPort(c *check.C) {

func (s *UploaderUtilTestSuite) TestUpdateServicePortInMeta(c *check.C) {
expectedPort := 80
metaPath := path.Join(s.workHome, "meta")
metaPath := filepath.Join(s.workHome, "meta")
updateServicePortInMeta(metaPath, expectedPort)
port := getPortFromMeta(metaPath)
c.Assert(port, check.Equals, expectedPort)
Expand Down
Loading

0 comments on commit 0f654c1

Please sign in to comment.