MCPcopy Index your code
hub / github.com/devspace-sh/devspace / deleteSafeRecursive

Method deleteSafeRecursive

pkg/devspace/sync/downstream.go:432–493  ·  view source on GitHub ↗
(relativePath string, deleteChanges []*remote.Change, force bool)

Source from the content-addressed store, hash-verified

430}
431
432func (d *downstream) deleteSafeRecursive(relativePath string, deleteChanges []*remote.Change, force bool) {
433 absolutePath := filepath.Join(d.sync.LocalPath, relativePath)
434 relativePath = getRelativeFromFullPath(absolutePath, d.sync.LocalPath)
435
436 // Check if path is in delete changes
437 found := false
438 for _, remove := range deleteChanges {
439 if remove.Path == relativePath {
440 found = true
441 }
442 }
443
444 // We don't delete the folder or the contents if we haven't tracked it
445 if !force {
446 if d.sync.fileIndex.fileMap[relativePath] == nil || !found {
447 d.sync.log.Infof("Downstream - Skip delete directory '.%s'", relativePath)
448 return
449 }
450 }
451
452 // Delete directory from fileMap
453 defer delete(d.sync.fileIndex.fileMap, relativePath)
454 files, err := os.ReadDir(absolutePath)
455 if err != nil {
456 return
457 }
458
459 // Loop over directory contents and check if we should delete the contents
460 for _, dirEntry := range files {
461 f, err := dirEntry.Info()
462 if err != nil {
463 continue
464 }
465
466 if fsutil.IsRecursiveSymlink(f, path.Join(relativePath, f.Name())) {
467 continue
468 }
469
470 childRelativePath := filepath.ToSlash(filepath.Join(relativePath, f.Name()))
471 childAbsFilepath := filepath.Join(d.sync.LocalPath, childRelativePath)
472 if shouldRemoveLocal(childAbsFilepath, d.sync.fileIndex.fileMap[childRelativePath], d.sync, force) {
473 if f.IsDir() {
474 d.deleteSafeRecursive(childRelativePath, deleteChanges, force)
475 } else {
476 err = os.Remove(childAbsFilepath)
477 if err != nil {
478 d.sync.log.Infof("Downstream - Skip file delete '.%s': %v", relativePath, err)
479 }
480 }
481 } else {
482 d.sync.log.Infof("Downstream - Skip delete '.%s'", relativePath)
483 }
484
485 delete(d.sync.fileIndex.fileMap, childRelativePath)
486 }
487
488 // This will not remove the directory if there is still a file or directory in it
489 err = os.Remove(absolutePath)

Callers 1

removeMethod · 0.95

Calls 9

IsRecursiveSymlinkFunction · 0.92
shouldRemoveLocalFunction · 0.85
getRelativeFromFullPathFunction · 0.70
NameMethod · 0.65
RemoveMethod · 0.65
InfofMethod · 0.45
InfoMethod · 0.45
IsDirMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected