Browse Source

context++

κρμγ 7 năm trước cách đây
mục cha
commit
fd32031525
4 tập tin đã thay đổi với 27 bổ sung21 xóa
  1. 8 6
      dav_ipfs/filesys.go
  2. 12 11
      dav_ipfs/projection/fs_sys.go
  3. 4 3
      ipfs_api/add.go
  4. 3 1
      ipfs_api/local_refs.go

+ 8 - 6
dav_ipfs/filesys.go

@@ -1,6 +1,7 @@
 package dav_ipfs
 
 import (
+	"context"
 	"fmt"
 	"github.com/kpmy/mipfs/ipfs_api"
 	"github.com/kpmy/ypk/dom"
@@ -29,7 +30,7 @@ func (f *filesystem) root() *chain {
 	return f.rootLevel
 }
 
-func (f *filesystem) Mkdir(name string, perm os.FileMode) (err error) {
+func (f *filesystem) Mkdir(ctx context.Context, name string, perm os.FileMode) (err error) {
 	root := f.root()
 	chain := newChain(root.mirror(), root.Hash+"/"+strings.Trim(name, "/"))
 	if tail := chain.tail(); !tail.exists() {
@@ -63,7 +64,7 @@ func (f *filesystem) Mkdir(name string, perm os.FileMode) (err error) {
 	return
 }
 
-func (f *filesystem) OpenFile(name string, flag int, perm os.FileMode) (ret webdav.File, err error) {
+func (f *filesystem) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (ret webdav.File, err error) {
 	//log.Println("open", name, flag, perm)
 	root := f.root()
 	path := newChain(root.mirror(), root.Hash+"/"+strings.Trim(name, "/"))
@@ -92,7 +93,7 @@ func (f *filesystem) OpenFile(name string, flag int, perm os.FileMode) (ret webd
 	return
 }
 
-func (f *filesystem) RemoveAll(name string) (err error) {
+func (f *filesystem) RemoveAll(ctx context.Context, name string) (err error) {
 	root := f.root()
 	chain := newChain(root.mirror(), root.Hash+"/"+strings.Trim(name, "/"))
 	if tail := chain.tail(); tail.exists() {
@@ -114,7 +115,7 @@ func (f *filesystem) RemoveAll(name string) (err error) {
 	return
 }
 
-func (f *filesystem) Rename(oldName, newName string) (err error) {
+func (f *filesystem) Rename(ctx context.Context, oldName, newName string) (err error) {
 	//log.Println("rename", oldName, newName)
 	root := f.root()
 	on := newChain(root.mirror(), root.Hash+"/"+strings.Trim(oldName, "/"))
@@ -158,7 +159,7 @@ func (f *filesystem) Rename(oldName, newName string) (err error) {
 	return
 }
 
-func (f *filesystem) Stat(name string) (fi os.FileInfo, err error) {
+func (f *filesystem) Stat(ctx context.Context, name string) (fi os.FileInfo, err error) {
 	//log.Println("stat", name)
 	root := f.root()
 	chain := newChain(root.mirror(), root.Hash+"/"+strings.Trim(name, "/"))
@@ -183,7 +184,8 @@ func (f *filesystem) String() string {
 
 func (f *filesystem) ETag(name string) (ret string, err error) {
 	var fi os.FileInfo
-	if fi, err = f.Stat(name); err == nil {
+	ctx := context.Background()
+	if fi, err = f.Stat(ctx, name); err == nil {
 		ret = fmt.Sprintf(`"%x%x"`, fi.ModTime().UnixNano(), fi.Size())
 	}
 	return

+ 12 - 11
dav_ipfs/projection/fs_sys.go

@@ -1,6 +1,7 @@
 package projection //import "github.com/kpmy/mipfs/dav_ipfs/projection"
 
 import (
+	"context"
 	"fmt"
 	"github.com/kpmy/mipfs/dav_ipfs"
 	"github.com/kpmy/mipfs/ipfs_api"
@@ -177,20 +178,20 @@ func isProjection(split []string) bool {
 	return strings.ToLower(split[0]) == ProjectionRoot
 }
 
-func (p *projection) Mkdir(name string, perm os.FileMode) (err error) {
+func (p *projection) Mkdir(ctx context.Context, name string, perm os.FileMode) (err error) {
 	ls := splitPath(name)
 	switch {
 	case len(ls) > 1 && isProjection(ls):
 		err = os.ErrPermission
 	case len(ls) == 1 && isProjection(ls):
-		err = p.inner.Mkdir(strings.ToLower(name), perm)
+		err = p.inner.Mkdir(ctx, strings.ToLower(name), perm)
 	default:
-		err = p.inner.Mkdir(name, perm)
+		err = p.inner.Mkdir(ctx, name, perm)
 	}
 	return
 }
 
-func (p *projection) OpenFile(name string, flag int, perm os.FileMode) (ret webdav.File, err error) {
+func (p *projection) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (ret webdav.File, err error) {
 	log.Println("open", name)
 	ls := splitPath(name)
 	switch {
@@ -208,41 +209,41 @@ func (p *projection) OpenFile(name string, flag int, perm os.FileMode) (ret webd
 			err = os.ErrNotExist
 		}
 	default:
-		ret, err = p.inner.OpenFile(name, flag, perm)
+		ret, err = p.inner.OpenFile(ctx, name, flag, perm)
 	}
 	return
 }
 
-func (p *projection) RemoveAll(name string) (err error) {
+func (p *projection) RemoveAll(ctx context.Context, name string) (err error) {
 	ls := splitPath(name)
 	switch {
 	case isProjection(ls):
 		err = os.ErrPermission
 	default:
-		err = p.inner.RemoveAll(name)
+		err = p.inner.RemoveAll(ctx, name)
 	}
 	return
 }
 
-func (p *projection) Rename(oldName, newName string) (err error) {
+func (p *projection) Rename(ctx context.Context, oldName, newName string) (err error) {
 	ls := splitPath(oldName)
 	switch {
 	case isProjection(ls):
 		err = os.ErrPermission
 	default:
-		err = p.inner.Rename(oldName, newName)
+		err = p.inner.Rename(ctx, oldName, newName)
 	}
 	return
 }
 
-func (p *projection) Stat(name string) (fi os.FileInfo, err error) {
+func (p *projection) Stat(ctx context.Context, name string) (fi os.FileInfo, err error) {
 	log.Println("stat", name)
 	ls := splitPath(name)
 	switch {
 	case isProjection(ls):
 		fi = &cat{}
 	default:
-		fi, err = p.inner.Stat(name)
+		fi, err = p.inner.Stat(ctx, name)
 	}
 	return
 }

+ 4 - 3
ipfs_api/add.go

@@ -5,8 +5,9 @@ import (
 	"io"
 	"io/ioutil"
 
+	"context"
 	"github.com/ipfs/go-ipfs-api"
-	files "github.com/whyrusleeping/go-multipart-files"
+	"github.com/whyrusleeping/go-multipart-files"
 )
 
 type object struct {
@@ -26,8 +27,8 @@ func (s *MyShell) Add(r io.Reader) (string, error) {
 	fr := files.NewReaderFile("", "", rc, nil)
 	slf := files.NewSliceFile("", "", []files.File{fr})
 	fileReader := files.NewMultiFileReader(slf, true)
-
-	req := shell.NewRequest(s.Url, "add")
+	ctx := context.Background()
+	req := shell.NewRequest(ctx, s.Url, "add")
 	req.Body = fileReader
 	req.Opts["progress"] = "false"
 	req.Opts["chunker"] = "size-1048576"

+ 3 - 1
ipfs_api/local_refs.go

@@ -3,11 +3,13 @@ package ipfs_api
 import (
 	"encoding/json"
 
+	"context"
 	"github.com/ipfs/go-ipfs-api"
 )
 
 func (s *MyShell) LocalRefs() (<-chan string, error) {
-	req := shell.NewRequest(s.Url, "refs/local")
+	ctx := context.Background()
+	req := shell.NewRequest(ctx, s.Url, "refs/local")
 
 	resp, err := req.Send(s.Client)
 	if err != nil {