From 083395ee53a7c1e0d236e58cb72c4e4c7afc02c5 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Wed, 15 Jun 2022 19:10:11 +0800 Subject: [PATCH] feat: recursive create folder --- internal/driver/error.go | 11 +++++++++-- internal/operations/fs.go | 13 +++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/internal/driver/error.go b/internal/driver/error.go index 15ffa8cb..377eb21c 100644 --- a/internal/driver/error.go +++ b/internal/driver/error.go @@ -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 +} diff --git a/internal/operations/fs.go b/internal/operations/fs.go index df013609..8c7ca7e2 100644 --- a/internal/operations/fs.go +++ b/internal/operations/fs.go @@ -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) }