@cryptotaxi247 / kubo / commits / a8bfa947d

fuse/ipns: use Read() not ReadAll()

Juan Batiz-Benet committed Jan 29, 2015 at 16:10 UTC a8bfa947d8c0f9f9ac8b445ea1e182c46fdb99b4
2 files changed +43 -15
fuse/ipns/ipns_unix.go
+40 -13
@@ -4,7 +4,7 @@ package ipns
4
5 import (
6 "errors"
7 - "io/ioutil"
7 + "io"
8 "os"
9 "path/filepath"
10 "time"
@@ -13,6 +13,7 @@ import (
13 fs "github.com/jbenet/go-ipfs/Godeps/_workspace/src/bazil.org/fuse/fs"
14 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
15 proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
16 + eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
17
18 core "github.com/jbenet/go-ipfs/core"
19 chunk "github.com/jbenet/go-ipfs/importer/chunk"
@@ -24,11 +25,12 @@ import (
25 uio "github.com/jbenet/go-ipfs/unixfs/io"
26 ftpb "github.com/jbenet/go-ipfs/unixfs/pb"
27 u "github.com/jbenet/go-ipfs/util"
28 + lgbl "github.com/jbenet/go-ipfs/util/eventlog/loggables"
29 )
30
31 const IpnsReadonly = true
32
31 -var log = u.Logger("ipns")
33 +var log = eventlog.Logger("fuse/ipns")
34
35 var (
36 shortRepublishTimeout = time.Millisecond * 5
@@ -336,21 +338,46 @@ func (s *Node) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
338 return nil, fuse.ENOENT
339 }
340
339 -// ReadAll reads the object data as file data
340 -func (s *Node) ReadAll(intr fs.Intr) ([]byte, fuse.Error) {
341 - log.Debugf("ipns: ReadAll [%s]", s.name)
342 - r, err := uio.NewDagReader(context.TODO(), s.Nd, s.Ipfs.DAG)
341 +func (s *Node) Read(req *fuse.ReadRequest, resp *fuse.ReadResponse, intr fs.Intr) fuse.Error {
342 + // intr will be closed by fuse if the request is cancelled. turn this into a context.
343 + ctx, cancel := context.WithCancel(context.TODO())
344 + defer cancel() // make sure all operations we started close.
345 +
346 + // we wait on intr and cancel our context if it closes.
347 + go func() {
348 + select {
349 + case <-intr: // closed by fuse
350 + cancel() // cancel our context
351 + case <-ctx.Done():
352 + }
353 + }()
354 +
355 + k, err := s.Nd.Key()
356 if err != nil {
344 - return nil, err
357 + return err
358 }
346 - // this is a terrible function... 'ReadAll'?
347 - // what if i have a 6TB file? GG RAM.
348 - b, err := ioutil.ReadAll(r)
359 +
360 + // setup our logging event
361 + lm := make(lgbl.DeferredMap)
362 + lm["fs"] = "ipns"
363 + lm["key"] = func() interface{} { return k.Pretty() }
364 + lm["req_offset"] = req.Offset
365 + lm["req_size"] = req.Size
366 + defer log.EventBegin(ctx, "fuseRead", lm).Done()
367 +
368 + r, err := uio.NewDagReader(ctx, s.Nd, s.Ipfs.DAG)
369 if err != nil {
350 - log.Errorf("[%s] Readall error: %s", s.name, err)
351 - return nil, err
370 + return err
371 + }
372 + o, err := r.Seek(req.Offset, os.SEEK_SET)
373 + lm["res_offset"] = o
374 + if err != nil {
375 + return err
376 }
353 - return b, nil
377 + n, err := io.ReadFull(r, resp.Data[:req.Size])
378 + resp.Data = resp.Data[:n]
379 + lm["res_size"] = n
380 + return err // may be non-nil / not succeeded
381 }
382
383 func (n *Node) Write(req *fuse.WriteRequest, resp *fuse.WriteResponse, intr fs.Intr) fuse.Error {
fuse/readonly/readonly_unix.go
+3 -2
@@ -166,6 +166,7 @@ func (s *Node) Read(req *fuse.ReadRequest, resp *fuse.ReadResponse, intr fs.Intr
166
167 // setup our logging event
168 lm := make(lgbl.DeferredMap)
169 + lm["fs"] = "ipfs"
170 lm["key"] = func() interface{} { return k.Pretty() }
171 lm["req_offset"] = req.Offset
172 lm["req_size"] = req.Size
@@ -176,12 +177,12 @@ func (s *Node) Read(req *fuse.ReadRequest, resp *fuse.ReadResponse, intr fs.Intr
177 return err
178 }
179 o, err := r.Seek(req.Offset, os.SEEK_SET)
179 - lm["req_offset"] = o
180 + lm["res_offset"] = o
181 if err != nil {
182 return err
183 }
184 n, err := io.ReadFull(r, resp.Data[:req.Size])
185 resp.Data = resp.Data[:n]
185 - lm["req_size"] = n
186 + lm["res_size"] = n
187 return err // may be non-nil / not succeeded
188 }