mirror of
https://github.com/fiatjaf/nak.git
synced 2025-07-29 16:28:29 -04:00
.github
nostrfs
asyncfile.go
deterministicfile.go
entitydir.go
eventdir.go
helpers.go
npubdir.go
root.go
viewdir.go
writeablefile.go
.gitignore
LICENSE
README.md
blossom.go
bunker.go
count.go
curl.go
decode.go
dvm.go
encode.go
encrypt_decrypt.go
event.go
example_test.go
fetch.go
flags.go
fs.go
fs_windows.go
go.mod
go.sum
helpers.go
helpers_key.go
key.go
main.go
mcp.go
musig2.go
outbox.go
relay.go
req.go
serve.go
verify.go
wallet.go
zapstore.yaml
57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package nostrfs
|
|
|
|
import (
|
|
"context"
|
|
"sync/atomic"
|
|
"syscall"
|
|
|
|
"github.com/hanwen/go-fuse/v2/fs"
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
|
"github.com/nbd-wtf/go-nostr"
|
|
)
|
|
|
|
type AsyncFile struct {
|
|
fs.Inode
|
|
ctx context.Context
|
|
fetched atomic.Bool
|
|
data []byte
|
|
ts nostr.Timestamp
|
|
load func() ([]byte, nostr.Timestamp)
|
|
}
|
|
|
|
var (
|
|
_ = (fs.NodeOpener)((*AsyncFile)(nil))
|
|
_ = (fs.NodeGetattrer)((*AsyncFile)(nil))
|
|
)
|
|
|
|
func (af *AsyncFile) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
|
|
if af.fetched.CompareAndSwap(false, true) {
|
|
af.data, af.ts = af.load()
|
|
}
|
|
|
|
out.Size = uint64(len(af.data))
|
|
out.Mtime = uint64(af.ts)
|
|
return fs.OK
|
|
}
|
|
|
|
func (af *AsyncFile) Open(ctx context.Context, flags uint32) (fs.FileHandle, uint32, syscall.Errno) {
|
|
if af.fetched.CompareAndSwap(false, true) {
|
|
af.data, af.ts = af.load()
|
|
}
|
|
|
|
return nil, fuse.FOPEN_KEEP_CACHE, 0
|
|
}
|
|
|
|
func (af *AsyncFile) Read(
|
|
ctx context.Context,
|
|
f fs.FileHandle,
|
|
dest []byte,
|
|
off int64,
|
|
) (fuse.ReadResult, syscall.Errno) {
|
|
end := int(off) + len(dest)
|
|
if end > len(af.data) {
|
|
end = len(af.data)
|
|
}
|
|
return fuse.ReadResultData(af.data[off:end]), 0
|
|
}
|