Skip to content

Commit

Permalink
fix: allow using storage together with background deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
dundee committed Apr 10, 2024
1 parent 62935f2 commit 1774a37
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
3 changes: 0 additions & 3 deletions cmd/gdu/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pkg/analyze/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions pkg/analyze/stored.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"runtime/debug"
"sync"
"time"

"github.com/dundee/gdu/v5/internal/common"
Expand Down Expand Up @@ -146,6 +147,7 @@ func (a *StoredAnalyzer) processDir(path string) *StoredDir {
BasePath: path,
},
nil,
sync.Mutex{},
}
dir.AddFile(subdir)

Expand Down Expand Up @@ -211,6 +213,7 @@ func (a *StoredAnalyzer) updateProgress() {
type StoredDir struct {
*Dir
cachedFiles fs.Files
dbLock sync.Mutex
}

// GetParent returns parent dir
Expand Down Expand Up @@ -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()
}
Expand All @@ -255,6 +260,7 @@ func (f *StoredDir) GetFiles() fs.Files {
BasePath: f.GetPath(),
},
nil,
sync.Mutex{},
}

err := DefaultStorage.LoadDir(dir)
Expand All @@ -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()
}
Expand Down
4 changes: 2 additions & 2 deletions tui/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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()
Expand Down
29 changes: 29 additions & 0 deletions tui/tui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 1774a37

Please sign in to comment.