fix(brain_edit): snapshot, rollback and lock file go through the brain root (#192)
brain_edit did its own I/O by path around notes.Edit: - the pre-edit snapshot used os.ReadFile, which hung on a FIFO note held open by a writer; - the rollback (when the provenance stamp fails) used os.WriteFile; - the edit lock "<note>.lock" was opened with O_CREATE by path, so a planted dangling .lock symlink made a successful edit create a file at the link's target, in the brain or outside it. Snapshot is brainfs ReadRegular, rollback WriteRegular, and the lock is opened and removed through new brainfs OpenLock/RemoveLock: clean path, real parent directories, an existing lock must be regular, and the opened handle must be the file at the path (SameFile). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -22,6 +22,10 @@ import (
|
||||
// ErrNotRegular is returned for an entry that is not a regular file.
|
||||
var ErrNotRegular = errors.New("not a regular file")
|
||||
|
||||
// ErrLockChanged is returned by OpenLock when the lock file was replaced
|
||||
// while it was being opened.
|
||||
var ErrLockChanged = errors.New("lock file replaced while opening")
|
||||
|
||||
// ErrTooLarge is returned by ReadRegularMax for a file over the limit.
|
||||
var ErrTooLarge = errors.New("file too large")
|
||||
|
||||
@@ -270,6 +274,54 @@ func (b *Brain) mkdirReal(rel string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// OpenLock opens (creating if absent) a regular lock file at a clean
|
||||
// brain-relative path for flock, through the root (hyperguild#192): every
|
||||
// directory above it must be real, an existing lock must be a regular file,
|
||||
// and the opened handle must be the file at the path (os.Root follows an
|
||||
// in-root final symlink even with O_NOFOLLOW, so the Lstat pre-check is
|
||||
// what refuses a dangling in-brain link). A link swapped in between the
|
||||
// pre-check and the open can still have an empty file created at an in-brain
|
||||
// target before the refusal: the #167 residual. ErrLockChanged reports a
|
||||
// lock replaced while opening (a peer removed and recreated it); callers
|
||||
// retry.
|
||||
func (b *Brain) OpenLock(rel string) (*os.File, error) {
|
||||
if err := b.RealParent(rel); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if fi, err := b.root.Lstat(rel); err == nil && !fi.Mode().IsRegular() {
|
||||
return nil, fmt.Errorf("%s: %w", rel, ErrNotRegular)
|
||||
} else if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||
return nil, err
|
||||
}
|
||||
f, err := b.root.OpenFile(rel, os.O_RDWR|os.O_CREATE|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0o644)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hfi, herr := f.Stat()
|
||||
lfi, lerr := b.root.Lstat(rel)
|
||||
if herr != nil || !hfi.Mode().IsRegular() {
|
||||
_ = f.Close()
|
||||
return nil, fmt.Errorf("%s: %w", rel, ErrNotRegular)
|
||||
}
|
||||
if lerr != nil || !os.SameFile(lfi, hfi) {
|
||||
_ = f.Close()
|
||||
if lerr == nil && !lfi.Mode().IsRegular() {
|
||||
return nil, fmt.Errorf("%s: %w", rel, ErrNotRegular)
|
||||
}
|
||||
return nil, fmt.Errorf("%s: %w", rel, ErrLockChanged)
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// LockIsCurrent reports whether f is still the file at rel: after taking a
|
||||
// flock, a lock file removed and recreated by a peer means the flock is on
|
||||
// an orphaned inode and does not exclude anyone (hyperguild#192).
|
||||
func (b *Brain) LockIsCurrent(rel string, f *os.File) bool {
|
||||
hfi, herr := f.Stat()
|
||||
lfi, lerr := b.root.Lstat(rel)
|
||||
return herr == nil && lerr == nil && os.SameFile(lfi, hfi)
|
||||
}
|
||||
|
||||
// ReadRegular reads a brain-relative path, refusing anything that is not a
|
||||
// regular file, both before the open and on the opened handle.
|
||||
func (b *Brain) ReadRegular(rel string) ([]byte, error) {
|
||||
|
||||
Reference in New Issue
Block a user