Commit 16e42f82 authored by Jeromy's avatar Jeromy

fixed ipns file persistence bug

parent a0d77dbf
......@@ -7,6 +7,7 @@ import (
ci "github.com/jbenet/go-ipfs/crypto"
mdag "github.com/jbenet/go-ipfs/merkledag"
nsys "github.com/jbenet/go-ipfs/namesys"
path "github.com/jbenet/go-ipfs/path"
"github.com/jbenet/go-ipfs/peer"
mdht "github.com/jbenet/go-ipfs/routing/mock"
)
......@@ -29,6 +30,7 @@ func NewMockNode() (*IpfsNode, error) {
// Routing
dht := mdht.NewMockRouter(nd.Identity, nd.Datastore)
nd.Routing = dht
// Bitswap
//??
......@@ -38,9 +40,16 @@ func NewMockNode() (*IpfsNode, error) {
return nil, err
}
dserv := &mdag.DAGService{bserv}
nd.DAG = &mdag.DAGService{bserv}
// Namespace resolver
nd.Namesys = nsys.NewMasterResolver(dht, dserv)
nd.Namesys = nsys.NewMasterResolver(dht, nd.DAG)
// Publisher
nd.Publisher = nsys.NewPublisher(nd.DAG, dht)
// Path resolver
nd.Resolver = &path.Resolver{nd.DAG}
return nd, nil
}
......@@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"testing"
"time"
fstest "github.com/jbenet/go-ipfs/Godeps/_workspace/src/bazil.org/fuse/fs/fstestutil"
"github.com/jbenet/go-ipfs/core"
......@@ -17,13 +18,40 @@ func randBytes(size int) []byte {
return b
}
func TestIpnsBasicIO(t *testing.T) {
localnode, err := core.NewMockNode()
func writeFile(t *testing.T, size int, path string) ([]byte, error) {
data := randBytes(size)
fi, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
n, err := fi.Write(data)
if err != nil {
t.Fatal(err)
}
if n != len(data) {
t.Fatal("Didnt write proper amount!")
}
err = fi.Close()
if err != nil {
t.Fatal(err)
}
fs, err := NewIpns(localnode, "")
return data, nil
}
func setupIpnsTest(t *testing.T, node *core.IpfsNode) (*core.IpfsNode, *fstest.Mount) {
var err error
if node == nil {
node, err = core.NewMockNode()
if err != nil {
t.Fatal(err)
}
}
fs, err := NewIpns(node, "")
if err != nil {
t.Fatal(err)
}
......@@ -31,40 +59,53 @@ func TestIpnsBasicIO(t *testing.T) {
if err != nil {
t.Fatal(err)
}
return node, mnt
}
func TestIpnsBasicIO(t *testing.T) {
_, mnt := setupIpnsTest(t, nil)
defer mnt.Close()
data := randBytes(12345)
fi, err := os.Create(mnt.Dir + "/local/testfile")
fname := mnt.Dir + "/local/testfile"
data, err := writeFile(t, 12345, fname)
if err != nil {
t.Fatal(err)
}
n, err := fi.Write(data)
rbuf, err := ioutil.ReadFile(fname)
if err != nil {
t.Fatal(err)
}
if n != len(data) {
t.Fatal("Didnt write proper amount!")
if !bytes.Equal(rbuf, data) {
t.Fatal("Incorrect Read!")
}
}
fi.Close()
//TODO: maybe wait for the publish to happen? or not, should test both cases
func TestFilePersistence(t *testing.T) {
node, mnt := setupIpnsTest(t, nil)
fi, err = os.Open(mnt.Dir + "/local/testfile")
fname := "/local/atestfile"
data, err := writeFile(t, 127, mnt.Dir+fname)
if err != nil {
t.Fatal(err)
}
rbuf, err := ioutil.ReadAll(fi)
// Wait for publish: TODO: make publish happen faster in tests
time.Sleep(time.Millisecond * 40)
mnt.Close()
node, mnt = setupIpnsTest(t, node)
defer mnt.Close()
rbuf, err := ioutil.ReadFile(mnt.Dir + fname)
if err != nil {
t.Fatal(err)
}
fi.Close()
if !bytes.Equal(rbuf, data) {
t.Fatal("Incorrect Read!")
t.Fatalf("File data changed between mounts! sizes differ: %d != %d", len(data), len(rbuf))
}
}
......@@ -70,7 +70,7 @@ func CreateRoot(n *core.IpfsNode, keys []ci.PrivKey, ipfsroot string) (*Root, er
nd := new(Node)
nd.Ipfs = n
nd.key = k
nd.repub = NewRepublisher(nd, time.Millisecond*10, time.Second)
nd.repub = NewRepublisher(nd, time.Millisecond*5, time.Millisecond*500)
go nd.repub.Run()
......@@ -249,6 +249,7 @@ func (n *Node) makeChild(name string, node *mdag.Node) *Node {
Nd: node,
name: name,
nsRoot: n.nsRoot,
parent: n,
}
if n.nsRoot == nil {
......@@ -322,11 +323,13 @@ func (n *Node) Flush(req *fuse.FlushRequest, intr fs.Intr) fuse.Error {
// folder, bad things would happen.
buf := bytes.NewReader(n.writerBuf.Bytes())
newNode, err := imp.NewDagFromReader(buf)
log.Debug("flush: new data = %v", newNode.Data)
if err != nil {
log.Critical("error creating dag from writerBuf: %s", err)
return err
}
if n.parent != nil {
log.Debug("updating self in parent!")
err := n.parent.update(n.name, newNode)
if err != nil {
log.Critical("error in updating ipns dag tree: %s", err)
......@@ -529,6 +532,7 @@ func (n *Node) Rename(req *fuse.RenameRequest, newDir fs.Node, intr fs.Intr) fus
}
func (n *Node) update(name string, newnode *mdag.Node) error {
log.Debug("update '%s' in '%s'", name, n.name)
nnode := n.Nd.Copy()
err := nnode.RemoveNodeLink(name)
if err != nil {
......@@ -537,7 +541,7 @@ func (n *Node) update(name string, newnode *mdag.Node) error {
nnode.AddNodeLink(name, newnode)
if n.parent != nil {
err := n.parent.update(n.name, newnode)
err := n.parent.update(n.name, nnode)
if err != nil {
return err
}
......
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