提交 33eb1470 作者: Juan Batiz-Benet

Merge pull request #694 from jbenet/fuse-seek

fuse/ipfs: seeking
...@@ -4,7 +4,7 @@ package ipns ...@@ -4,7 +4,7 @@ package ipns
import ( import (
"errors" "errors"
"io/ioutil" "io"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
fs "github.com/jbenet/go-ipfs/Godeps/_workspace/src/bazil.org/fuse/fs" fs "github.com/jbenet/go-ipfs/Godeps/_workspace/src/bazil.org/fuse/fs"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto" proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
core "github.com/jbenet/go-ipfs/core" core "github.com/jbenet/go-ipfs/core"
chunk "github.com/jbenet/go-ipfs/importer/chunk" chunk "github.com/jbenet/go-ipfs/importer/chunk"
...@@ -24,11 +25,12 @@ import ( ...@@ -24,11 +25,12 @@ import (
uio "github.com/jbenet/go-ipfs/unixfs/io" uio "github.com/jbenet/go-ipfs/unixfs/io"
ftpb "github.com/jbenet/go-ipfs/unixfs/pb" ftpb "github.com/jbenet/go-ipfs/unixfs/pb"
u "github.com/jbenet/go-ipfs/util" u "github.com/jbenet/go-ipfs/util"
lgbl "github.com/jbenet/go-ipfs/util/eventlog/loggables"
) )
const IpnsReadonly = true const IpnsReadonly = true
var log = u.Logger("ipns") var log = eventlog.Logger("fuse/ipns")
var ( var (
shortRepublishTimeout = time.Millisecond * 5 shortRepublishTimeout = time.Millisecond * 5
...@@ -336,21 +338,46 @@ func (s *Node) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) { ...@@ -336,21 +338,46 @@ func (s *Node) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
return nil, fuse.ENOENT return nil, fuse.ENOENT
} }
// ReadAll reads the object data as file data func (s *Node) Read(req *fuse.ReadRequest, resp *fuse.ReadResponse, intr fs.Intr) fuse.Error {
func (s *Node) ReadAll(intr fs.Intr) ([]byte, fuse.Error) { // intr will be closed by fuse if the request is cancelled. turn this into a context.
log.Debugf("ipns: ReadAll [%s]", s.name) ctx, cancel := context.WithCancel(context.TODO())
r, err := uio.NewDagReader(context.TODO(), s.Nd, s.Ipfs.DAG) defer cancel() // make sure all operations we started close.
// we wait on intr and cancel our context if it closes.
go func() {
select {
case <-intr: // closed by fuse
cancel() // cancel our context
case <-ctx.Done():
}
}()
k, err := s.Nd.Key()
if err != nil { if err != nil {
return nil, err return err
}
// setup our logging event
lm := make(lgbl.DeferredMap)
lm["fs"] = "ipns"
lm["key"] = func() interface{} { return k.Pretty() }
lm["req_offset"] = req.Offset
lm["req_size"] = req.Size
defer log.EventBegin(ctx, "fuseRead", lm).Done()
r, err := uio.NewDagReader(ctx, s.Nd, s.Ipfs.DAG)
if err != nil {
return err
} }
// this is a terrible function... 'ReadAll'? o, err := r.Seek(req.Offset, os.SEEK_SET)
// what if i have a 6TB file? GG RAM. lm["res_offset"] = o
b, err := ioutil.ReadAll(r)
if err != nil { if err != nil {
log.Errorf("[%s] Readall error: %s", s.name, err) return err
return nil, err
} }
return b, nil n, err := io.ReadFull(r, resp.Data[:req.Size])
resp.Data = resp.Data[:n]
lm["res_size"] = n
return err // may be non-nil / not succeeded
} }
func (n *Node) Write(req *fuse.WriteRequest, resp *fuse.WriteResponse, intr fs.Intr) fuse.Error { func (n *Node) Write(req *fuse.WriteRequest, resp *fuse.WriteResponse, intr fs.Intr) fuse.Error {
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
package readonly package readonly
import ( import (
"io/ioutil" "io"
"os" "os"
fuse "github.com/jbenet/go-ipfs/Godeps/_workspace/src/bazil.org/fuse" fuse "github.com/jbenet/go-ipfs/Godeps/_workspace/src/bazil.org/fuse"
...@@ -16,12 +16,13 @@ import ( ...@@ -16,12 +16,13 @@ import (
core "github.com/jbenet/go-ipfs/core" core "github.com/jbenet/go-ipfs/core"
mdag "github.com/jbenet/go-ipfs/merkledag" mdag "github.com/jbenet/go-ipfs/merkledag"
path "github.com/jbenet/go-ipfs/path" path "github.com/jbenet/go-ipfs/path"
eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
uio "github.com/jbenet/go-ipfs/unixfs/io" uio "github.com/jbenet/go-ipfs/unixfs/io"
ftpb "github.com/jbenet/go-ipfs/unixfs/pb" ftpb "github.com/jbenet/go-ipfs/unixfs/pb"
u "github.com/jbenet/go-ipfs/util" lgbl "github.com/jbenet/go-ipfs/util/eventlog/loggables"
) )
var log = u.Logger("ipfs") var log = eventlog.Logger("fuse/ipfs")
// FileSystem is the readonly Ipfs Fuse Filesystem. // FileSystem is the readonly Ipfs Fuse Filesystem.
type FileSystem struct { type FileSystem struct {
...@@ -144,14 +145,44 @@ func (s *Node) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) { ...@@ -144,14 +145,44 @@ func (s *Node) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
return nil, fuse.ENOENT return nil, fuse.ENOENT
} }
// ReadAll reads the object data as file data func (s *Node) Read(req *fuse.ReadRequest, resp *fuse.ReadResponse, intr fs.Intr) fuse.Error {
func (s *Node) ReadAll(intr fs.Intr) ([]byte, fuse.Error) { // intr will be closed by fuse if the request is cancelled. turn this into a context.
log.Debug("Read node.") ctx, cancel := context.WithCancel(context.TODO())
r, err := uio.NewDagReader(context.TODO(), s.Nd, s.Ipfs.DAG) defer cancel() // make sure all operations we started close.
// we wait on intr and cancel our context if it closes.
go func() {
select {
case <-intr: // closed by fuse
cancel() // cancel our context
case <-ctx.Done():
}
}()
k, err := s.Nd.Key()
if err != nil {
return err
}
// setup our logging event
lm := make(lgbl.DeferredMap)
lm["fs"] = "ipfs"
lm["key"] = func() interface{} { return k.Pretty() }
lm["req_offset"] = req.Offset
lm["req_size"] = req.Size
defer log.EventBegin(ctx, "fuseRead", lm).Done()
r, err := uio.NewDagReader(ctx, s.Nd, s.Ipfs.DAG)
if err != nil {
return err
}
o, err := r.Seek(req.Offset, os.SEEK_SET)
lm["res_offset"] = o
if err != nil { if err != nil {
return nil, err return err
} }
// this is a terrible function... 'ReadAll'? n, err := io.ReadFull(r, resp.Data[:req.Size])
// what if i have a 6TB file? GG RAM. resp.Data = resp.Data[:n]
return ioutil.ReadAll(r) lm["res_size"] = n
return err // may be non-nil / not succeeded
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论