MCPcopy
hub / github.com/AlistGo/alist / MakeDir

Function MakeDir

internal/op/fs.go:307–360  ·  view source on GitHub ↗
(ctx context.Context, storage driver.Driver, path string, lazyCache ...bool)

Source from the content-addressed store, hash-verified

305var mkdirG singleflight.Group[interface{}]
306
307func 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
362func Move(ctx context.Context, storage driver.Driver, srcPath, dstDirPath string, lazyCache ...bool) error {
363 if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {

Callers 9

MakeDirMethod · 0.92
AddURLMethod · 0.92
AddURLMethod · 0.92
AddURLMethod · 0.92
AddURLMethod · 0.92
makeDirFunction · 0.92
PutFunction · 0.70
PutURLFunction · 0.70

Calls 13

FixAndCleanPathFunction · 0.92
IsObjectNotFoundFunction · 0.92
WrapObjNameFunction · 0.92
IsBoolFunction · 0.92
KeyFunction · 0.85
GetUnwrapFunction · 0.85
addCacheObjFunction · 0.85
ClearCacheFunction · 0.85
ConfigMethod · 0.65
GetStorageMethod · 0.65
DoMethod · 0.65
MakeDirMethod · 0.65

Tested by

no test coverage detected