mirror of
https://github.com/fiatjaf/nak.git
synced 2025-12-08 16:48:51 +00:00
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).
This commit is contained in:
2
fs.go
2
fs.go
@@ -42,7 +42,7 @@ var fsCmd = &cli.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
root := nostrfs.NewNostrRoot(
|
root := nostrfs.NewNostrRoot(
|
||||||
ctx,
|
context.WithValue(ctx, "log", log),
|
||||||
sys,
|
sys,
|
||||||
keyer.NewReadOnlyUser(c.String("pubkey")),
|
keyer.NewReadOnlyUser(c.String("pubkey")),
|
||||||
mountpoint,
|
mountpoint,
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ type EntityDir struct {
|
|||||||
|
|
||||||
var _ = (fs.NodeGetattrer)((*EntityDir)(nil))
|
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)
|
publishedAt := uint64(e.evt.CreatedAt)
|
||||||
out.Ctime = publishedAt
|
out.Ctime = publishedAt
|
||||||
|
|
||||||
@@ -67,6 +67,8 @@ func CreateEntityDir(
|
|||||||
extension string,
|
extension string,
|
||||||
event *nostr.Event,
|
event *nostr.Event,
|
||||||
) *fs.Inode {
|
) *fs.Inode {
|
||||||
|
log := ctx.Value("log").(func(msg string, args ...any))
|
||||||
|
|
||||||
h := parent.EmbeddedInode().NewPersistentInode(
|
h := parent.EmbeddedInode().NewPersistentInode(
|
||||||
ctx,
|
ctx,
|
||||||
&EntityDir{ctx: ctx, wd: wd, evt: event},
|
&EntityDir{ctx: ctx, wd: wd, evt: event},
|
||||||
@@ -179,14 +181,17 @@ func CreateEntityDir(
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
r, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
r, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log("failed to load image %s: %s\n", url, err)
|
||||||
return nil, 0
|
return nil, 0
|
||||||
}
|
}
|
||||||
resp, err := http.DefaultClient.Do(r)
|
resp, err := http.DefaultClient.Do(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log("failed to load image %s: %s\n", url, err)
|
||||||
return nil, 0
|
return nil, 0
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
if resp.StatusCode >= 300 {
|
if resp.StatusCode >= 300 {
|
||||||
|
log("failed to load image %s: %s\n", url, err)
|
||||||
return nil, 0
|
return nil, 0
|
||||||
}
|
}
|
||||||
w := &bytes.Buffer{}
|
w := &bytes.Buffer{}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ type EventDir struct {
|
|||||||
|
|
||||||
var _ = (fs.NodeGetattrer)((*EventDir)(nil))
|
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)
|
out.Mtime = uint64(e.evt.CreatedAt)
|
||||||
return fs.OK
|
return fs.OK
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 == "" {
|
if r.rootPubKey == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -76,7 +76,7 @@ func (r *NostrRoot) OnAdd(context.Context) {
|
|||||||
), true)
|
), 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)
|
out.SetEntryTimeout(time.Minute * 5)
|
||||||
|
|
||||||
child := r.GetChild(name)
|
child := r.GetChild(name)
|
||||||
@@ -84,8 +84,8 @@ func (r *NostrRoot) Lookup(ctx context.Context, name string, out *fuse.EntryOut)
|
|||||||
return child, fs.OK
|
return child, fs.OK
|
||||||
}
|
}
|
||||||
|
|
||||||
if pp, err := nip05.QueryIdentifier(ctx, name); err == nil {
|
if pp, err := nip05.QueryIdentifier(r.ctx, name); err == nil {
|
||||||
npubdir := CreateNpubDir(ctx, r.sys, r, r.wd, *pp)
|
npubdir := CreateNpubDir(r.ctx, r.sys, r, r.wd, *pp)
|
||||||
return npubdir, fs.OK
|
return npubdir, fs.OK
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,10 +96,10 @@ func (r *NostrRoot) Lookup(ctx context.Context, name string, out *fuse.EntryOut)
|
|||||||
|
|
||||||
switch p := pointer.(type) {
|
switch p := pointer.(type) {
|
||||||
case nostr.ProfilePointer:
|
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
|
return npubdir, fs.OK
|
||||||
case nostr.EventPointer:
|
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 {
|
if err != nil {
|
||||||
return nil, syscall.ENOENT
|
return nil, syscall.ENOENT
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ var (
|
|||||||
_ = (fs.NodeGetattrer)((*ViewDir)(nil))
|
_ = (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()
|
now := nostr.Now()
|
||||||
if n.filter.Until != nil {
|
if n.filter.Until != nil {
|
||||||
now = *n.filter.Until
|
now = *n.filter.Until
|
||||||
@@ -39,7 +39,7 @@ func (n *ViewDir) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOu
|
|||||||
return fs.OK
|
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) {
|
if n.fetched.CompareAndSwap(true, true) {
|
||||||
return fs.OK
|
return fs.OK
|
||||||
}
|
}
|
||||||
@@ -52,8 +52,8 @@ func (n *ViewDir) Opendir(ctx context.Context) syscall.Errno {
|
|||||||
aMonthAgo := now - 30*24*60*60
|
aMonthAgo := now - 30*24*60*60
|
||||||
n.filter.Since = &aMonthAgo
|
n.filter.Since = &aMonthAgo
|
||||||
|
|
||||||
for ie := range n.sys.Pool.FetchMany(ctx, n.relays, n.filter, nostr.WithLabel("nakfs")) {
|
for ie := range n.sys.Pool.FetchMany(n.ctx, n.relays, n.filter, nostr.WithLabel("nakfs")) {
|
||||||
basename, inode := n.create(ctx, n, ie.Event)
|
basename, inode := n.create(n.ctx, n, ie.Event)
|
||||||
n.AddChild(basename, inode, true)
|
n.AddChild(basename, inode, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,7 +61,7 @@ func (n *ViewDir) Opendir(ctx context.Context) syscall.Errno {
|
|||||||
filter.Until = &aMonthAgo
|
filter.Until = &aMonthAgo
|
||||||
|
|
||||||
n.AddChild("@previous", n.NewPersistentInode(
|
n.AddChild("@previous", n.NewPersistentInode(
|
||||||
ctx,
|
n.ctx,
|
||||||
&ViewDir{
|
&ViewDir{
|
||||||
ctx: n.ctx,
|
ctx: n.ctx,
|
||||||
sys: n.sys,
|
sys: n.sys,
|
||||||
@@ -72,8 +72,8 @@ func (n *ViewDir) Opendir(ctx context.Context) syscall.Errno {
|
|||||||
fs.StableAttr{Mode: syscall.S_IFDIR},
|
fs.StableAttr{Mode: syscall.S_IFDIR},
|
||||||
), true)
|
), true)
|
||||||
} else {
|
} else {
|
||||||
for ie := range n.sys.Pool.FetchMany(ctx, n.relays, n.filter, nostr.WithLabel("nakfs")) {
|
for ie := range n.sys.Pool.FetchMany(n.ctx, n.relays, n.filter, nostr.WithLabel("nakfs")) {
|
||||||
basename, inode := n.create(ctx, n, ie.Event)
|
basename, inode := n.create(n.ctx, n, ie.Event)
|
||||||
n.AddChild(basename, inode, true)
|
n.AddChild(basename, inode, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user