Skip to content

Commit

Permalink
feat: RemoveFile logic as a method
Browse files Browse the repository at this point in the history
  • Loading branch information
dundee committed Feb 9, 2024
1 parent c869904 commit ed14328
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/gdu/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func (a *App) runAction(ui UI, path string) error {
} else if a.Flags.ReadFromStorage {
ui.SetAnalyzer(analyze.CreateStoredAnalyzer(a.Flags.StoragePath))
if err := ui.ReadFromStorage(a.Flags.StoragePath, path); err != nil {
return fmt.Errorf("reading from storage: %w", err)
return fmt.Errorf("reading from storage (%s): %w", a.Flags.StoragePath, err)
}
} else {
if build.RootPathPrefix != "" {
Expand Down
28 changes: 19 additions & 9 deletions pkg/analyze/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ func (f *File) AddFile(item fs.Item) {
panic("AddFile should not be called on file")
}

// RemoveFile panics on file
func (f *File) RemoveFile(item fs.Item) {
panic("RemoveFile should not be called on file")
}

// Dir struct
type Dir struct {
*File
Expand Down Expand Up @@ -204,16 +209,11 @@ func (f *Dir) UpdateStats(linkedItems fs.HardLinkedItems) {
f.Usage = totalUsage
}

// RemoveItemFromDir removes item from dir
func RemoveItemFromDir(dir fs.Item, item fs.Item) error {
err := os.RemoveAll(item.GetPath())
if err != nil {
return err
}

dir.SetFiles(dir.GetFiles().Remove(item))
// RemoveFile panics on file
func (f *Dir) RemoveFile(item fs.Item) {
f.SetFiles(f.GetFiles().Remove(item))

cur := dir.(*Dir)
cur := f
for {
cur.ItemCount -= item.GetItemCount()
cur.Size -= item.GetSize()
Expand All @@ -224,6 +224,16 @@ func RemoveItemFromDir(dir fs.Item, item fs.Item) error {
}
cur = cur.Parent.(*Dir)
}
}

// RemoveItemFromDir removes item from dir
func RemoveItemFromDir(dir fs.Item, item fs.Item) error {
err := os.RemoveAll(item.GetPath())
if err != nil {
return err
}

dir.RemoveFile(item)
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/analyze/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func init() {
gob.RegisterName("analyze.StoredDir", &StoredDir{})
gob.RegisterName("analyze.Dir", &Dir{})
gob.RegisterName("analyze.File", &File{})
gob.RegisterName("analyze.ParentDir", &ParentDir{})
}

var DefaultStorage *Storage
Expand Down
35 changes: 35 additions & 0 deletions pkg/analyze/stored.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,40 @@ func (f *StoredDir) GetFiles() fs.Files {
return files
}

// SetFiles sets files in directory
func (f *StoredDir) SetFiles(files fs.Files) {
f.Files = files
}

// RemoveFile panics on file
func (f *StoredDir) RemoveFile(item fs.Item) {
if !DefaultStorage.IsOpen() {
closeFn := DefaultStorage.Open()
defer closeFn()
}

f.SetFiles(f.GetFiles().Remove(item))
f.cachedFiles = nil

cur := f
for {
cur.ItemCount -= item.GetItemCount()
cur.Size -= item.GetSize()
cur.Usage -= item.GetUsage()

err := DefaultStorage.StoreDir(cur)
if err != nil {
log.Print(err.Error())
}

parent := cur.GetParent()
if parent == nil {
break
}
cur = parent.(*StoredDir)
}
}

// GetItemStats returns item count, apparent usage and real usage of this dir
func (f *StoredDir) GetItemStats(linkedItems fs.HardLinkedItems) (int, int64, int64) {
f.UpdateStats(linkedItems)
Expand Down Expand Up @@ -332,3 +366,4 @@ func (p *ParentDir) UpdateStats(linkedItems fs.HardLinkedItems) { panic("not imp
func (p *ParentDir) AddFile(fs.Item) { panic("not implemented") }
func (p *ParentDir) GetFiles() fs.Files { panic("not implemented") }
func (p *ParentDir) SetFiles(fs.Files) { panic("not implemented") }
func (f *ParentDir) RemoveFile(item fs.Item) { panic("not implemented") }
32 changes: 32 additions & 0 deletions pkg/analyze/stored_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,35 @@ func TestStoredAnalyzer(t *testing.T) {
t, int64(5), dir.GetFiles()[0].(*StoredDir).GetFiles()[1].(*StoredDir).GetFiles()[0].GetSize(),
)
}

func TestRemoveStoredFile(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()

a := CreateStoredAnalyzer("/tmp/badger")
dir := a.AnalyzeDir(
"test_dir", func(_, _ string) bool { return false }, false,
).(*StoredDir)

a.GetDone().Wait()
a.ResetProgress()

dir.UpdateStats(make(fs.HardLinkedItems))

// test dir info
assert.Equal(t, "test_dir", dir.Name)
assert.Equal(t, int64(7+4096*3), dir.Size)
assert.Equal(t, 5, dir.ItemCount)
assert.True(t, dir.IsDir())

subdir := dir.GetFiles()[0].(*StoredDir)
subdir.RemoveFile(subdir.GetFiles()[0])

closeFn := DefaultStorage.Open()
defer closeFn()
stored, err := DefaultStorage.GetDirForPath("test_dir")
assert.NoError(t, err)

assert.Equal(t, 4, stored.GetItemCount())
assert.Equal(t, int64(5+4096*3), stored.GetSize())
}
1 change: 1 addition & 0 deletions pkg/fs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Item interface {
AddFile(Item)
GetFiles() Files
SetFiles(Files)
RemoveFile(Item)
}

// Files - slice of pointers to File
Expand Down

0 comments on commit ed14328

Please sign in to comment.