fix: return an error instead of panicking on orphaned MemMapFs Remove

unRegisterWithParent called log.Panic when a file's parent directory was
no longer registered (for example when the parent was removed first),
crashing the whole program from an ordinary Remove call. Return a normal
error instead so Remove, RemoveAll and Rename can surface it to the
caller. The now-unused log import is dropped.

Fixes #388
This commit is contained in:
dualfroz committed 2026-09-02 11:40:32 +02:00
1 parent d43dc733dc
commit c37cd819a9
2 files changed
+28 -2

No files matched your search

+5 -2
View File
@@ -16,7 +16,6 @@ package afero
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"sort"
@@ -71,7 +70,11 @@ func (m *MemMapFs) unRegisterWithParent(fileName string) error {
}
parent := m.findParent(f)
if parent == nil {
log.Panic("parent of ", f.Name(), " is nil")
// The parent directory is no longer registered (for example it was
// removed before this file). There is nothing to unregister from, and
// crashing the whole process is never appropriate, so surface an error
// that callers can handle.
return fmt.Errorf("parent of %s is nil", f.Name())
}
parent.Lock()