Enable fill | write in dir. (#126)

* add default value of PayloadProcessNum&FillByFAllocate

Signed-off-by: andrewmatilde <davis6813585853062@outlook.com>

* recover unit-test

Signed-off-by: andrewmatilde <davis6813585853062@outlook.com>

* recover unit-test

Signed-off-by: andrewmatilde <davis6813585853062@outlook.com>

* Enable fill | write in dir.

Signed-off-by: andrewmatilde <davis6813585853062@outlook.com>

* Enable fill | write in dir.

Signed-off-by: andrewmatilde <davis6813585853062@outlook.com>

* Enable fill | write in dir.

Signed-off-by: andrewmatilde <davis6813585853062@outlook.com>

* fix lint

Signed-off-by: andrewmatilde <davis6813585853062@outlook.com>

* fix log

Signed-off-by: andrewmatilde <davis6813585853062@outlook.com>
This commit is contained in:
Andrewmatilde 2022-01-17 13:05:44 +08:00 committed by GitHub
parent fafc3eb912
commit ed3ab388be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 29 deletions

View File

@ -179,35 +179,40 @@ func initPath(opt *DiskOption) (string, error) {
case DiskFillAction, DiskWritePayloadAction:
if opt.Path == "" {
var err error
// Check if the path is valid.
opt.Path, err = utils.CreateTempFile()
opt.Path, err = os.Getwd()
if err != nil {
log.Error(fmt.Sprintf("unexpected err when CreateTempFile in action: %s", opt.Action))
log.Error("unexpected err when execute os.Getwd()", zap.Error(err))
return "", err
}
}
fi, err := os.Stat(opt.Path)
if err != nil {
// check if Path of file is valid when Path is not empty
if os.IsNotExist(err) {
var b []byte
if err := ioutil.WriteFile(opt.Path, b, 0600); err != nil {
return "", err
}
if err := os.Remove(opt.Path); err != nil {
return "", err
}
return opt.Path, nil
}
return "", err
}
if fi.IsDir() {
opt.Path, err = utils.CreateTempFile(opt.Path)
if err != nil {
log.Error(fmt.Sprintf("unexpected err : %v , when CreateTempFile in action %s with path %s.", err, opt.Action, opt.Path))
return "", err
}
if err := os.Remove(opt.Path); err != nil {
return "", err
}
} else {
_, err := os.Stat(opt.Path)
if err != nil {
// check if Path of file is valid when Path is not empty
if os.IsNotExist(err) {
var b []byte
if err := ioutil.WriteFile(opt.Path, b, 0600); err != nil {
return "", err
}
if err := os.Remove(opt.Path); err != nil {
return "", err
}
} else {
return "", err
}
} else {
return "", fmt.Errorf("fill into an existing file")
}
return opt.Path, err
}
return opt.Path, nil
return "", fmt.Errorf("fill into an existing file")
case DiskReadPayloadAction:
if opt.Path == "" {
path, err := utils.GetRootDevice()

View File

@ -15,7 +15,6 @@ package utils
import (
"io/ioutil"
"os"
"github.com/pingcap/errors"
"github.com/pingcap/log"
@ -23,12 +22,7 @@ import (
)
// CreateTempFile will create a temp file in current directory.
func CreateTempFile() (string, error) {
path, err := os.Getwd()
if err != nil {
log.Error("unexpected err when execute os.Getwd()", zap.Error(err))
return "", err
}
func CreateTempFile(path string) (string, error) {
tempFile, err := ioutil.TempFile(path, "example")
if err != nil {
log.Error("unexpected err when open temp file", zap.Error(err))