Commit d588636c authored by Jeromy's avatar Jeromy

remove 'Open' from ipnsfs

parent f679127d
......@@ -38,38 +38,6 @@ func NewDirectory(name string, node *dag.Node, parent childCloser, fs *Filesyste
}
}
// Open opens a file at the given path 'tpath'
func (d *Directory) Open(tpath []string, mode int) (*File, error) {
if len(tpath) == 0 {
return nil, ErrIsDirectory
}
if len(tpath) == 1 {
fi, err := d.childFile(tpath[0])
if err == nil {
return fi, nil
}
if mode|os.O_CREATE != 0 {
fnode := new(dag.Node)
fnode.Data = ft.FilePBData(nil, 0)
nfi, err := NewFile(tpath[0], fnode, d, d.fs)
if err != nil {
return nil, err
}
d.files[tpath[0]] = nfi
return nfi, nil
}
return nil, os.ErrNotExist
}
dir, err := d.childDir(tpath[0])
if err != nil {
return nil, err
}
return dir.Open(tpath[1:], mode)
}
// closeChild updates the child by the given name to the dag node 'nd'
// and changes its own dag node, then propogates the changes upward
func (d *Directory) closeChild(name string, nd *dag.Node) error {
......
......@@ -14,7 +14,6 @@ import (
"errors"
"fmt"
"os"
"strings"
"sync"
"time"
......@@ -69,17 +68,6 @@ func NewFilesystem(ctx context.Context, ds dag.DAGService, nsys namesys.NameSyst
return fs, nil
}
// Open opens a file at the given path
func (fs *Filesystem) Open(tpath string, mode int) (*File, error) {
pathelem := strings.Split(tpath, "/")
r, ok := fs.roots[pathelem[0]]
if !ok {
return nil, os.ErrNotExist
}
return r.Open(pathelem[1:], mode)
}
func (fs *Filesystem) Close() error {
wg := sync.WaitGroup{}
for _, r := range fs.roots {
......@@ -206,31 +194,6 @@ func (kr *KeyRoot) GetValue() FSNode {
return kr.val
}
func (kr *KeyRoot) Open(tpath []string, mode int) (*File, error) {
if kr.val == nil {
// No entry here. KeyRoot was created incorrectly
panic("nil keyroot.val, improperly constructed keyroot")
}
if len(tpath) > 0 {
// Make sure our root is a directory
dir, ok := kr.val.(*Directory)
if !ok {
return nil, os.ErrNotExist
}
return dir.Open(tpath, mode)
}
switch t := kr.val.(type) {
case *Directory:
return nil, ErrIsDirectory
case *File:
return t, nil
default:
panic("unrecognized type, should not happen")
}
}
// closeChild implements the childCloser interface, and signals to the publisher that
// there are changes ready to be published
func (kr *KeyRoot) closeChild(name string, nd *dag.Node) error {
......
package ipnsfs_test
import (
"bytes"
"io/ioutil"
"os"
"path"
"testing"
core "github.com/jbenet/go-ipfs/core"
. "github.com/jbenet/go-ipfs/ipnsfs"
u "github.com/jbenet/go-ipfs/util"
)
func testFS(t *testing.T, nd *core.IpfsNode) *Filesystem {
fs, err := NewFilesystem(nd.Context(), nd.DAG, nd.Namesys, nd.Pinning, nd.PrivateKey)
if err != nil {
t.Fatal(err)
}
return fs
}
// Test some basic operations
// testing in fuse/ipns is sufficient to prove this code works properly
func TestBasic(t *testing.T) {
mock, err := core.NewMockNode()
if err != nil {
t.Fatal(err)
}
fs := testFS(t, mock)
k := u.Key(mock.Identity)
p := path.Join(k.B58String(), "file")
fi, err := fs.Open(p, os.O_CREATE)
if err != nil {
t.Fatal(err)
}
data := []byte("Hello World")
n, err := fi.Write(data)
if err != nil {
t.Fatal(err)
}
if n != len(data) {
t.Fatal("wrote incorrect amount")
}
err = fi.Close()
if err != nil {
t.Fatal(err)
}
nfi, err := fs.Open(p, os.O_RDONLY)
if err != nil {
t.Fatal(err)
}
out, err := ioutil.ReadAll(nfi)
if err != nil {
t.Fatal(err)
}
err = nfi.Close()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(out, data) {
t.Fatal("Write failed.")
}
err = fs.Close()
if err != nil {
t.Fatal(err)
}
// Open the filesystem again, and try to read our file
nfs := testFS(t, mock)
fi, err = nfs.Open(p, os.O_RDONLY)
nb, err := ioutil.ReadAll(fi)
if err != nil {
t.Fatal(err)
}
t.Log(nb)
if !bytes.Equal(nb, data) {
t.Fatal("data not the same after closing down fs")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment