Skip to content

Commit

Permalink
feat: recursive create folder
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 15, 2022
1 parent 2d60dab commit 083395e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
11 changes: 9 additions & 2 deletions internal/driver/error.go
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions internal/operations/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 083395e

Please sign in to comment.