(ctx context.Context, storage driver.Driver, path string, lazyCache ...bool)
| 305 | var mkdirG singleflight.Group[interface{}] |
| 306 | |
| 307 | func MakeDir(ctx context.Context, storage driver.Driver, path string, lazyCache ...bool) error { |
| 308 | if storage.Config().CheckStatus && storage.GetStorage().Status != WORK { |
| 309 | return errors.Errorf("storage not init: %s", storage.GetStorage().Status) |
| 310 | } |
| 311 | path = utils.FixAndCleanPath(path) |
| 312 | key := Key(storage, path) |
| 313 | _, err, _ := mkdirG.Do(key, func() (interface{}, error) { |
| 314 | // check if dir exists |
| 315 | f, err := GetUnwrap(ctx, storage, path) |
| 316 | if err != nil { |
| 317 | if errs.IsObjectNotFound(err) { |
| 318 | parentPath, dirName := stdpath.Split(path) |
| 319 | err = MakeDir(ctx, storage, parentPath) |
| 320 | if err != nil { |
| 321 | return nil, errors.WithMessagef(err, "failed to make parent dir [%s]", parentPath) |
| 322 | } |
| 323 | parentDir, err := GetUnwrap(ctx, storage, parentPath) |
| 324 | // this should not happen |
| 325 | if err != nil { |
| 326 | return nil, errors.WithMessagef(err, "failed to get parent dir [%s]", parentPath) |
| 327 | } |
| 328 | |
| 329 | switch s := storage.(type) { |
| 330 | case driver.MkdirResult: |
| 331 | var newObj model.Obj |
| 332 | newObj, err = s.MakeDir(ctx, parentDir, dirName) |
| 333 | if err == nil { |
| 334 | if newObj != nil { |
| 335 | addCacheObj(storage, parentPath, model.WrapObjName(newObj)) |
| 336 | } else if !utils.IsBool(lazyCache...) { |
| 337 | ClearCache(storage, parentPath) |
| 338 | } |
| 339 | } |
| 340 | case driver.Mkdir: |
| 341 | err = s.MakeDir(ctx, parentDir, dirName) |
| 342 | if err == nil && !utils.IsBool(lazyCache...) { |
| 343 | ClearCache(storage, parentPath) |
| 344 | } |
| 345 | default: |
| 346 | return nil, errs.NotImplement |
| 347 | } |
| 348 | return nil, errors.WithStack(err) |
| 349 | } |
| 350 | return nil, errors.WithMessage(err, "failed to check if dir exists") |
| 351 | } |
| 352 | // dir exists |
| 353 | if f.IsDir() { |
| 354 | return nil, nil |
| 355 | } |
| 356 | // dir to make is a file |
| 357 | return nil, errors.New("file exists") |
| 358 | }) |
| 359 | return err |
| 360 | } |
| 361 | |
| 362 | func Move(ctx context.Context, storage driver.Driver, srcPath, dstDirPath string, lazyCache ...bool) error { |
| 363 | if storage.Config().CheckStatus && storage.GetStorage().Status != WORK { |
no test coverage detected