summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Milde <daniel@milde.cz>2024-04-10 17:39:41 +0200
committerDaniel Milde <daniel@milde.cz>2024-04-10 17:39:58 +0200
commit1774a37e595913dc4513f5529722027509177b48 (patch)
treea20d1778953237eb34ee2821fd6f3f36c19102ed
parent62935f2a253cb4080c54bfa7d2fcaf44a565a15e (diff)
fix: allow using storage together with background deletion
-rw-r--r--cmd/gdu/app/app.go3
-rw-r--r--pkg/analyze/storage.go1
-rw-r--r--pkg/analyze/stored.go8
-rw-r--r--tui/background.go4
-rw-r--r--tui/tui_test.go29
5 files changed, 40 insertions, 5 deletions
diff --git a/cmd/gdu/app/app.go b/cmd/gdu/app/app.go
index 81e658e..4ac6696 100644
--- a/cmd/gdu/app/app.go
+++ b/cmd/gdu/app/app.go
@@ -136,9 +136,6 @@ func (a *App) Run() (err error) {
if a.Flags.NoPrefix && a.Flags.UseSIPrefix {
return fmt.Errorf("--no-prefix and --si cannot be used at once")
}
- if a.Flags.UseStorage && a.Flags.DeleteInBackground {
- return fmt.Errorf("--use-storage and --delete-in-background cannot be used together")
- }
path := a.getPath()
path, _ = filepath.Abs(path)
diff --git a/pkg/analyze/storage.go b/pkg/analyze/storage.go
index 99c2958..351e1b3 100644
--- a/pkg/analyze/storage.go
+++ b/pkg/analyze/storage.go
@@ -119,6 +119,7 @@ func (s *Storage) GetDirForPath(path string) (fs.Item, error) {
BasePath: dirPath,
},
nil,
+ sync.Mutex{},
}
err := s.LoadDir(dir)
if err != nil {
diff --git a/pkg/analyze/stored.go b/pkg/analyze/stored.go
index 70dba76..79bdd03 100644
--- a/pkg/analyze/stored.go
+++ b/pkg/analyze/stored.go
@@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"runtime/debug"
+ "sync"
"time"
"github.com/dundee/gdu/v5/internal/common"
@@ -146,6 +147,7 @@ func (a *StoredAnalyzer) processDir(path string) *StoredDir {
BasePath: path,
},
nil,
+ sync.Mutex{},
}
dir.AddFile(subdir)
@@ -211,6 +213,7 @@ func (a *StoredAnalyzer) updateProgress() {
type StoredDir struct {
*Dir
cachedFiles fs.Files
+ dbLock sync.Mutex
}
// GetParent returns parent dir
@@ -240,6 +243,8 @@ func (f *StoredDir) GetFiles() fs.Files {
}
if !DefaultStorage.IsOpen() {
+ f.dbLock.Lock()
+ defer f.dbLock.Unlock()
closeFn := DefaultStorage.Open()
defer closeFn()
}
@@ -255,6 +260,7 @@ func (f *StoredDir) GetFiles() fs.Files {
BasePath: f.GetPath(),
},
nil,
+ sync.Mutex{},
}
err := DefaultStorage.LoadDir(dir)
@@ -279,6 +285,8 @@ func (f *StoredDir) SetFiles(files fs.Files) {
// RemoveFile panics on file
func (f *StoredDir) RemoveFile(item fs.Item) {
if !DefaultStorage.IsOpen() {
+ f.dbLock.Lock()
+ defer f.dbLock.Unlock()
closeFn := DefaultStorage.Open()
defer closeFn()
}
diff --git a/tui/background.go b/tui/background.go
index 8f32c1a..f373f23 100644
--- a/tui/background.go
+++ b/tui/background.go
@@ -55,7 +55,7 @@ func (ui *UI) deleteItem(item fs.Item, shouldEmpty bool) {
deleteItems = append(deleteItems, file)
}
} else {
- parentDir = item.GetParent()
+ parentDir = ui.currentDir
deleteItems = append(deleteItems, item)
}
@@ -73,7 +73,7 @@ func (ui *UI) deleteItem(item fs.Item, shouldEmpty bool) {
}
}
- if item.GetParent() == ui.currentDir {
+ if item.GetParent().GetPath() == ui.currentDir.GetPath() {
ui.app.QueueUpdateDraw(func() {
row, _ := ui.table.GetSelection()
x, y := ui.table.GetOffset()
diff --git a/tui/tui_test.go b/tui/tui_test.go
index d02c949..6974de1 100644
--- a/tui/tui_test.go
+++ b/tui/tui_test.go
@@ -539,6 +539,35 @@ func TestDeleteMarkedInBackground(t *testing.T) {
assert.NoFileExists(t, "test_dir/nested/file2")
}
+func TestDeleteMarkedInBackgroundWithStorage(t *testing.T) {
+ fin := testdir.CreateTestDir()
+ defer fin()
+
+ ui := getAnalyzedPathMockedApp(t, false, true, false)
+ ui.SetAnalyzer(analyze.CreateStoredAnalyzer("/tmp/badger"))
+ ui.SetDeleteInBackground()
+
+ assert.Equal(t, 1, ui.table.GetRowCount())
+
+ ui.fileItemSelected(0, 0) // nested
+
+ ui.markedRows[1] = struct{}{} // subnested
+ ui.markedRows[2] = struct{}{} // file2
+
+ ui.deleteMarked(false)
+
+ <-ui.done // wait for deletion of subnested
+ <-ui.done // wait for deletion of file2
+
+ for _, f := range ui.app.(*testapp.MockedApp).GetUpdateDraws() {
+ f()
+ }
+
+ assert.DirExists(t, "test_dir/nested")
+ assert.NoDirExists(t, "test_dir/nested/subnested")
+ assert.NoFileExists(t, "test_dir/nested/file2")
+}
+
func TestDeleteMarkedInBackgroundWithErr(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()