cmd/ncdu: fix out of range panic in delete

This commit is contained in:
buengese 2021-04-14 18:20:17 +02:00
parent dcd4edc9f5
commit 4a5cbf2a19

View File

@ -485,11 +485,15 @@ func (u *UI) removeEntry(pos int) {
// delete the entry at the current position // delete the entry at the current position
func (u *UI) delete() { func (u *UI) delete() {
if u.d == nil || len(u.entries) == 0 {
return
}
ctx := context.Background() ctx := context.Background()
dirPos := u.sortPerm[u.dirPosMap[u.path].entry] cursorPos := u.dirPosMap[u.path]
entry := u.entries[dirPos] dirPos := u.sortPerm[cursorPos.entry]
dirEntry := u.entries[dirPos]
u.boxMenu = []string{"cancel", "confirm"} u.boxMenu = []string{"cancel", "confirm"}
if obj, isFile := entry.(fs.Object); isFile { if obj, isFile := dirEntry.(fs.Object); isFile {
u.boxMenuHandler = func(f fs.Fs, p string, o int) (string, error) { u.boxMenuHandler = func(f fs.Fs, p string, o int) (string, error) {
if o != 1 { if o != 1 {
return "Aborted!", nil return "Aborted!", nil
@ -499,27 +503,33 @@ func (u *UI) delete() {
return "", err return "", err
} }
u.removeEntry(dirPos) u.removeEntry(dirPos)
if cursorPos.entry >= len(u.entries) {
u.move(-1) // move back onto a valid entry
}
return "Successfully deleted file!", nil return "Successfully deleted file!", nil
} }
u.popupBox([]string{ u.popupBox([]string{
"Delete this file?", "Delete this file?",
u.fsName + entry.String()}) u.fsName + dirEntry.String()})
} else { } else {
u.boxMenuHandler = func(f fs.Fs, p string, o int) (string, error) { u.boxMenuHandler = func(f fs.Fs, p string, o int) (string, error) {
if o != 1 { if o != 1 {
return "Aborted!", nil return "Aborted!", nil
} }
err := operations.Purge(ctx, f, entry.String()) err := operations.Purge(ctx, f, dirEntry.String())
if err != nil { if err != nil {
return "", err return "", err
} }
u.removeEntry(dirPos) u.removeEntry(dirPos)
if cursorPos.entry >= len(u.entries) {
u.move(-1) // move back onto a valid entry
}
return "Successfully purged folder!", nil return "Successfully purged folder!", nil
} }
u.popupBox([]string{ u.popupBox([]string{
"Purge this directory?", "Purge this directory?",
"ALL files in it will be deleted", "ALL files in it will be deleted",
u.fsName + entry.String()}) u.fsName + dirEntry.String()})
} }
} }