diff --git a/cmd/ncdu/scan/scan.go b/cmd/ncdu/scan/scan.go index b835ef4fd..ab753256d 100644 --- a/cmd/ncdu/scan/scan.go +++ b/cmd/ncdu/scan/scan.go @@ -70,6 +70,45 @@ func (d *Dir) Entries() fs.DirEntries { return append(fs.DirEntries(nil), d.entries...) } +// Remove removes the i-th entry from the +// in-memory representation of the remote directory +func (d *Dir) Remove(i int) { + d.mu.Lock() + defer d.mu.Unlock() + d.remove(i) +} + +// removes the i-th entry from the +// in-memory representation of the remote directory +// +// Call with d.mu held +func (d *Dir) remove(i int) { + size := d.entries[i].Size() + count := int64(1) + + subDir, ok := d.getDir(i) + if ok { + size = subDir.size + count = subDir.count + delete(d.dirs, path.Base(subDir.path)) + } + + d.size -= size + d.count -= count + d.entries = append(d.entries[:i], d.entries[i+1:]...) + + dir := d + // populate changed size and count to parent(s) + for parent := d.parent; parent != nil; parent = parent.parent { + parent.mu.Lock() + parent.dirs[path.Base(dir.path)] = dir + parent.size -= size + parent.count -= count + dir = parent + parent.mu.Unlock() + } +} + // gets the directory of the i-th entry // // returns nil if it is a file