Commit f573b7b5 authored by Kejie Zhang's avatar Kejie Zhang Committed by Lucas Molas

correctly handle offsets bigger than file size

parent 4973eab0
...@@ -200,6 +200,16 @@ func (dm *DagModifier) Sync() error { ...@@ -200,6 +200,16 @@ func (dm *DagModifier) Sync() error {
// Number of bytes we're going to write // Number of bytes we're going to write
buflen := dm.wrBuf.Len() buflen := dm.wrBuf.Len()
fs, err := fileSize(dm.curNode)
if err != nil {
return err
}
if fs < dm.writeStart {
if err := dm.expandSparse(int64(dm.writeStart - fs)); err != nil {
return err
}
}
// overwrite existing dag nodes // overwrite existing dag nodes
thisc, err := dm.modifyDag(dm.curNode, dm.writeStart) thisc, err := dm.modifyDag(dm.curNode, dm.writeStart)
if err != nil { if err != nil {
...@@ -225,8 +235,8 @@ func (dm *DagModifier) Sync() error { ...@@ -225,8 +235,8 @@ func (dm *DagModifier) Sync() error {
} }
dm.writeStart += uint64(buflen) dm.writeStart += uint64(buflen)
dm.wrBuf = nil dm.wrBuf = nil
return nil return nil
} }
......
...@@ -7,12 +7,14 @@ import ( ...@@ -7,12 +7,14 @@ import (
"io/ioutil" "io/ioutil"
"testing" "testing"
dag "github.com/ipfs/go-merkledag"
h "github.com/ipfs/go-unixfs/importer/helpers" h "github.com/ipfs/go-unixfs/importer/helpers"
trickle "github.com/ipfs/go-unixfs/importer/trickle" trickle "github.com/ipfs/go-unixfs/importer/trickle"
uio "github.com/ipfs/go-unixfs/io" uio "github.com/ipfs/go-unixfs/io"
testu "github.com/ipfs/go-unixfs/test" testu "github.com/ipfs/go-unixfs/test"
u "github.com/ipfs/go-ipfs-util" u "github.com/ipfs/go-ipfs-util"
"github.com/ipfs/go-unixfs"
) )
func testModWrite(t *testing.T, beg, size uint64, orig []byte, dm *DagModifier, opts testu.NodeOpts) []byte { func testModWrite(t *testing.T, beg, size uint64, orig []byte, dm *DagModifier, opts testu.NodeOpts) []byte {
...@@ -410,6 +412,53 @@ func testDagTruncate(t *testing.T, opts testu.NodeOpts) { ...@@ -410,6 +412,53 @@ func testDagTruncate(t *testing.T, opts testu.NodeOpts) {
} }
} }
func TestDagSync(t *testing.T) {
dserv := testu.GetDAGServ()
nd := dag.NodeWithData(unixfs.FilePBData(nil, 0))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dagmod, err := NewDagModifier(ctx, nd, dserv, testu.SizeSplitterGen(512))
if err != nil {
t.Fatal(err)
}
_, err = dagmod.Write([]byte("test1"))
if err != nil {
t.Fatal(err)
}
err = dagmod.Sync()
if err != nil {
t.Fatal(err)
}
err = dagmod.Truncate(0)
if err != nil {
t.Fatal(err)
}
_, err = dagmod.Write([]byte("test2"))
if err != nil {
t.Fatal(err)
}
err = dagmod.Sync()
if err != nil {
t.Fatal(err)
}
out, err := ioutil.ReadAll(dagmod)
if err != nil {
t.Fatal(err)
}
if err = testu.ArrComp(out[5:], []byte("test2")); err != nil {
t.Fatal(err)
}
}
// TestDagTruncateSameSize tests that a DAG truncated // TestDagTruncateSameSize tests that a DAG truncated
// to the same size (i.e., doing nothing) doesn't modify // to the same size (i.e., doing nothing) doesn't modify
// the DAG (its hash). // the DAG (its hash).
......
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