feat: recursive create folder

refactor/fs
Noah Hsu 2022-06-15 19:10:11 +08:00
parent 2d60dab13c
commit 083395ee53
2 changed files with 22 additions and 2 deletions

View File

@ -1,11 +1,18 @@
package driver
import "errors"
import (
"errors"
pkgerr "github.com/pkg/errors"
)
var (
ErrorDirNotFound = errors.New("directory not found")
ErrorObjectNotFound = errors.New("object not found")
ErrNotImplement = errors.New("not implement")
ErrNotSupport = errors.New("not support")
ErrRelativePath = errors.New("access using relative path is not allowed")
)
func IsErrObjectNotFound(err error) bool {
return pkgerr.Cause(err) == ErrorObjectNotFound
}

View File

@ -96,6 +96,19 @@ func Link(ctx context.Context, account driver.Driver, path string, args model.Li
}
func MakeDir(ctx context.Context, account driver.Driver, path string) error {
// check if dir exists
f, err := Get(ctx, account, path)
if f != nil && f.IsDir() {
return nil
}
if err != nil && !driver.IsErrObjectNotFound(err) {
return errors.WithMessage(err, "failed to check if dir exists")
}
parentPath := stdpath.Dir(path)
err = MakeDir(ctx, account, parentPath)
if err != nil {
return errors.WithMessagef(err, "failed to make parent dir [%s]", parentPath)
}
return account.MakeDir(ctx, path)
}