From d9c5b2d0310e284d0efc2c3289bc28e37c16d0be Mon Sep 17 00:00:00 2001 From: Karl233 Date: Sun, 8 Oct 2023 17:39:18 +0800 Subject: [PATCH] bug fix in node hard delete --- mega.go | 6 ++++-- mega_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/mega.go b/mega.go index 1bd5f1c..4562191 100644 --- a/mega.go +++ b/mega.go @@ -1836,8 +1836,10 @@ func (m *Mega) Delete(node *Node, destroy bool) error { return err } - parent := m.FS.lookup[node.hash] - parent.removeChild(node) + nodeCache := m.FS.lookup[node.hash] + if nodeCache != nil && nodeCache.parent != nil { + nodeCache.parent.removeChild(node) + } delete(m.FS.lookup, node.hash) return nil diff --git a/mega_test.go b/mega_test.go index cea481e..e12547f 100644 --- a/mega_test.go +++ b/mega_test.go @@ -240,6 +240,40 @@ func TestDelete(t *testing.T) { session.FS.mutex.Unlock() } +func TestHardDelete(t *testing.T) { + session := initSession(t) + node, _, _ := uploadFile(t, session, 31, session.FS.root) + parentTmp := node.parent + if parentTmp != session.FS.root { + t.Error("Expects parent node to be root") + } + + retry(t, "Hard delete", func() error { + return session.Delete(node, true) + }) + + time.Sleep(1 * time.Second) // wait for the event + + session.FS.mutex.Lock() + if _, ok := session.FS.lookup[node.hash]; ok { + t.Error("Expects file to be disappeared") + } + + isInParent := false + for _, child := range parentTmp.children { + if child.hash == node.hash { + isInParent = true + break + } + } + + if isInParent { + t.Error("Expects file to be deleted in parent's children slice") + } + session.FS.mutex.Unlock() + +} + func TestCreateDir(t *testing.T) { session := initSession(t) node := createDir(t, session, "testdir1", session.FS.root)