-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: delete directory items in parallel
- Loading branch information
Showing
9 changed files
with
277 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package remove | ||
|
||
import ( | ||
"os" | ||
"runtime" | ||
"sync" | ||
|
||
"github.com/dundee/gdu/v5/pkg/fs" | ||
) | ||
|
||
var concurrencyLimit = make(chan struct{}, 3*runtime.GOMAXPROCS(0)) | ||
|
||
// RemoveItemFromDirParallel removes item from dir | ||
func RemoveItemFromDirParallel(dir fs.Item, item fs.Item) error { | ||
if !item.IsDir() { | ||
return RemoveItemFromDir(dir, item) | ||
} | ||
errChan := make(chan error, 1) // we show only first error | ||
var wait sync.WaitGroup | ||
|
||
// remove all files in the directory in parallel | ||
for _, file := range item.GetFilesLocked() { | ||
wait.Add(1) | ||
go func(itemPath string) { | ||
concurrencyLimit <- struct{}{} | ||
defer func() { <-concurrencyLimit }() | ||
|
||
err := os.RemoveAll(itemPath) | ||
if err != nil { | ||
select { | ||
// write error to channel if it's empty | ||
case errChan <- err: | ||
default: | ||
} | ||
} | ||
wait.Done() | ||
}(file.GetPath()) | ||
} | ||
|
||
wait.Wait() | ||
|
||
// check if there was an error | ||
select { | ||
case err := <-errChan: | ||
return err | ||
default: | ||
} | ||
|
||
// remove the directory itself | ||
err := os.RemoveAll(item.GetPath()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// update parent directory | ||
dir.RemoveFile(item) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package remove | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/dundee/gdu/v5/internal/testdir" | ||
"github.com/dundee/gdu/v5/pkg/analyze" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRemoveItemFromDirParallelWithErr(t *testing.T) { | ||
fin := testdir.CreateTestDir() | ||
defer fin() | ||
|
||
err := os.Chmod("test_dir/nested", 0) | ||
assert.Nil(t, err) | ||
defer func() { | ||
err = os.Chmod("test_dir/nested", 0755) | ||
assert.Nil(t, err) | ||
}() | ||
|
||
dir := &analyze.Dir{ | ||
File: &analyze.File{ | ||
Name: "test_dir", | ||
}, | ||
BasePath: ".", | ||
} | ||
|
||
subdir := &analyze.Dir{ | ||
File: &analyze.File{ | ||
Name: "nested", | ||
Parent: dir, | ||
}, | ||
} | ||
|
||
err = RemoveItemFromDirParallel(dir, subdir) | ||
assert.Contains(t, err.Error(), "permission denied") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package remove | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/dundee/gdu/v5/internal/testdir" | ||
"github.com/dundee/gdu/v5/pkg/analyze" | ||
"github.com/dundee/gdu/v5/pkg/fs" | ||
) | ||
|
||
func TestRemoveFileParallel(t *testing.T) { | ||
dir := &analyze.Dir{ | ||
File: &analyze.File{ | ||
Name: "xxx", | ||
Size: 5, | ||
Usage: 12, | ||
}, | ||
ItemCount: 3, | ||
BasePath: ".", | ||
} | ||
|
||
subdir := &analyze.Dir{ | ||
File: &analyze.File{ | ||
Name: "yyy", | ||
Size: 4, | ||
Usage: 8, | ||
Parent: dir, | ||
}, | ||
ItemCount: 2, | ||
} | ||
file := &analyze.File{ | ||
Name: "zzz", | ||
Size: 3, | ||
Usage: 4, | ||
Parent: subdir, | ||
} | ||
dir.Files = fs.Files{subdir} | ||
subdir.Files = fs.Files{file} | ||
|
||
err := RemoveItemFromDirParallel(subdir, file) | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, 0, len(subdir.Files)) | ||
assert.Equal(t, 1, subdir.ItemCount) | ||
assert.Equal(t, int64(1), subdir.Size) | ||
assert.Equal(t, int64(4), subdir.Usage) | ||
assert.Equal(t, 1, len(dir.Files)) | ||
assert.Equal(t, 2, dir.ItemCount) | ||
assert.Equal(t, int64(2), dir.Size) | ||
} | ||
|
||
func TestRemoveDirParallel(t *testing.T) { | ||
fin := testdir.CreateTestDir() | ||
defer fin() | ||
|
||
analyzer := analyze.CreateAnalyzer() | ||
dir := analyzer.AnalyzeDir( | ||
"test_dir", func(_, _ string) bool { return false }, false, | ||
).(*analyze.Dir) | ||
analyzer.GetDone().Wait() | ||
dir.UpdateStats(make(fs.HardLinkedItems)) | ||
|
||
subdir := dir.Files[0].(*analyze.Dir) | ||
|
||
err := RemoveItemFromDirParallel(dir, subdir) | ||
assert.Nil(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters