fork/gdu
git clone https://git.julia.blue/fork/gdu
da638da
master
1package remove 2 3import ( 4"os" 5"runtime" 6"sync" 7 8"github.com/dundee/gdu/v5/pkg/fs" 9) 10 11var concurrencyLimit = make (chan struct {},3 * runtime .GOMAXPROCS (0 )) 12 13// ItemFromDirParallel removes item from dir 14func ItemFromDirParallel (dir ,item fs.Item )error { 15if ! item .IsDir () { 16return ItemFromDir (dir ,item ) 17 } 18errChan := make (chan error ,1 )// we show only first error 19var wait sync.WaitGroup 20 21// remove all files in the directory in parallel 22for file := range item .GetFilesLocked (fs .SortBySize ,fs .SortDesc ) { 23if ! file .IsDir () { 24continue 25 } 26 27wait .Add (1 ) 28go func (itemPath string ) { 29concurrencyLimit <- struct {}{} 30defer func () {<- concurrencyLimit }() 31 32err := os .RemoveAll (itemPath ) 33if err != nil { 34select { 35// write error to channel if it's empty 36case errChan <- err : 37default : 38 } 39 } 40wait .Done () 41 }(file .GetPath ()) 42 } 43 44wait .Wait () 45 46// check if there was an error 47select { 48case err := <- errChan : 49return err 50default : 51 } 52 53// remove the directory itself 54err := os .RemoveAll (item .GetPath ()) 55if err != nil { 56return err 57 } 58 59// update parent directory 60dir .RemoveFile (item ) 61return nil 62}