From fe1f50f79886dae845a0474e84853549c1455bbe Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Tue, 11 Mar 2025 12:37:27 -0300 Subject: [PATCH] fs: logging and proper (?) handling of context passing (basically now we ignore the context given to us by the fuse library because they're weird). --- fs.go | 2 +- nostrfs/entitydir.go | 7 ++++++- nostrfs/eventdir.go | 2 +- nostrfs/root.go | 12 ++++++------ nostrfs/viewdir.go | 14 +++++++------- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/fs.go b/fs.go index ba63b27..6b09e04 100644 --- a/fs.go +++ b/fs.go @@ -42,7 +42,7 @@ var fsCmd = &cli.Command{ } root := nostrfs.NewNostrRoot( - ctx, + context.WithValue(ctx, "log", log), sys, keyer.NewReadOnlyUser(c.String("pubkey")), mountpoint, diff --git a/nostrfs/entitydir.go b/nostrfs/entitydir.go index c45664c..040b12b 100644 --- a/nostrfs/entitydir.go +++ b/nostrfs/entitydir.go @@ -30,7 +30,7 @@ type EntityDir struct { var _ = (fs.NodeGetattrer)((*EntityDir)(nil)) -func (e *EntityDir) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno { +func (e *EntityDir) Getattr(_ context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno { publishedAt := uint64(e.evt.CreatedAt) out.Ctime = publishedAt @@ -67,6 +67,8 @@ func CreateEntityDir( extension string, event *nostr.Event, ) *fs.Inode { + log := ctx.Value("log").(func(msg string, args ...any)) + h := parent.EmbeddedInode().NewPersistentInode( ctx, &EntityDir{ctx: ctx, wd: wd, evt: event}, @@ -179,14 +181,17 @@ func CreateEntityDir( defer cancel() r, err := http.NewRequestWithContext(ctx, "GET", url, nil) if err != nil { + log("failed to load image %s: %s\n", url, err) return nil, 0 } resp, err := http.DefaultClient.Do(r) if err != nil { + log("failed to load image %s: %s\n", url, err) return nil, 0 } defer resp.Body.Close() if resp.StatusCode >= 300 { + log("failed to load image %s: %s\n", url, err) return nil, 0 } w := &bytes.Buffer{} diff --git a/nostrfs/eventdir.go b/nostrfs/eventdir.go index 253e99c..e2edcce 100644 --- a/nostrfs/eventdir.go +++ b/nostrfs/eventdir.go @@ -31,7 +31,7 @@ type EventDir struct { var _ = (fs.NodeGetattrer)((*EventDir)(nil)) -func (e *EventDir) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno { +func (e *EventDir) Getattr(_ context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno { out.Mtime = uint64(e.evt.CreatedAt) return fs.OK } diff --git a/nostrfs/root.go b/nostrfs/root.go index 32c6cc1..6bdf969 100644 --- a/nostrfs/root.go +++ b/nostrfs/root.go @@ -40,7 +40,7 @@ func NewNostrRoot(ctx context.Context, sys *sdk.System, user nostr.User, mountpo } } -func (r *NostrRoot) OnAdd(context.Context) { +func (r *NostrRoot) OnAdd(_ context.Context) { if r.rootPubKey == "" { return } @@ -76,7 +76,7 @@ func (r *NostrRoot) OnAdd(context.Context) { ), true) } -func (r *NostrRoot) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) { +func (r *NostrRoot) Lookup(_ context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) { out.SetEntryTimeout(time.Minute * 5) child := r.GetChild(name) @@ -84,8 +84,8 @@ func (r *NostrRoot) Lookup(ctx context.Context, name string, out *fuse.EntryOut) return child, fs.OK } - if pp, err := nip05.QueryIdentifier(ctx, name); err == nil { - npubdir := CreateNpubDir(ctx, r.sys, r, r.wd, *pp) + if pp, err := nip05.QueryIdentifier(r.ctx, name); err == nil { + npubdir := CreateNpubDir(r.ctx, r.sys, r, r.wd, *pp) return npubdir, fs.OK } @@ -96,10 +96,10 @@ func (r *NostrRoot) Lookup(ctx context.Context, name string, out *fuse.EntryOut) switch p := pointer.(type) { case nostr.ProfilePointer: - npubdir := CreateNpubDir(ctx, r.sys, r, r.wd, p) + npubdir := CreateNpubDir(r.ctx, r.sys, r, r.wd, p) return npubdir, fs.OK case nostr.EventPointer: - eventdir, err := FetchAndCreateEventDir(ctx, r, r.wd, r.sys, p) + eventdir, err := FetchAndCreateEventDir(r.ctx, r, r.wd, r.sys, p) if err != nil { return nil, syscall.ENOENT } diff --git a/nostrfs/viewdir.go b/nostrfs/viewdir.go index ddc6979..8e9cf6e 100644 --- a/nostrfs/viewdir.go +++ b/nostrfs/viewdir.go @@ -28,7 +28,7 @@ var ( _ = (fs.NodeGetattrer)((*ViewDir)(nil)) ) -func (n *ViewDir) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno { +func (n *ViewDir) Getattr(_ context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno { now := nostr.Now() if n.filter.Until != nil { now = *n.filter.Until @@ -39,7 +39,7 @@ func (n *ViewDir) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOu return fs.OK } -func (n *ViewDir) Opendir(ctx context.Context) syscall.Errno { +func (n *ViewDir) Opendir(_ context.Context) syscall.Errno { if n.fetched.CompareAndSwap(true, true) { return fs.OK } @@ -52,8 +52,8 @@ func (n *ViewDir) Opendir(ctx context.Context) syscall.Errno { aMonthAgo := now - 30*24*60*60 n.filter.Since = &aMonthAgo - for ie := range n.sys.Pool.FetchMany(ctx, n.relays, n.filter, nostr.WithLabel("nakfs")) { - basename, inode := n.create(ctx, n, ie.Event) + for ie := range n.sys.Pool.FetchMany(n.ctx, n.relays, n.filter, nostr.WithLabel("nakfs")) { + basename, inode := n.create(n.ctx, n, ie.Event) n.AddChild(basename, inode, true) } @@ -61,7 +61,7 @@ func (n *ViewDir) Opendir(ctx context.Context) syscall.Errno { filter.Until = &aMonthAgo n.AddChild("@previous", n.NewPersistentInode( - ctx, + n.ctx, &ViewDir{ ctx: n.ctx, sys: n.sys, @@ -72,8 +72,8 @@ func (n *ViewDir) Opendir(ctx context.Context) syscall.Errno { fs.StableAttr{Mode: syscall.S_IFDIR}, ), true) } else { - for ie := range n.sys.Pool.FetchMany(ctx, n.relays, n.filter, nostr.WithLabel("nakfs")) { - basename, inode := n.create(ctx, n, ie.Event) + for ie := range n.sys.Pool.FetchMany(n.ctx, n.relays, n.filter, nostr.WithLabel("nakfs")) { + basename, inode := n.create(n.ctx, n, ie.Event) n.AddChild(basename, inode, true) } }