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 {
// Number of bytes we're going to write
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
thisc, err := dm.modifyDag(dm.curNode, dm.writeStart)
if err != nil {
......@@ -225,8 +235,8 @@ func (dm *DagModifier) Sync() error {
}
dm.writeStart += uint64(buflen)
dm.wrBuf = nil
return nil
}
......
......@@ -7,12 +7,14 @@ import (
"io/ioutil"
"testing"
dag "github.com/ipfs/go-merkledag"
h "github.com/ipfs/go-unixfs/importer/helpers"
trickle "github.com/ipfs/go-unixfs/importer/trickle"
uio "github.com/ipfs/go-unixfs/io"
testu "github.com/ipfs/go-unixfs/test"
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 {
......@@ -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
// to the same size (i.e., doing nothing) doesn't modify
// 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