Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
correctly handle offsets bigger than file size
Browse files Browse the repository at this point in the history
  • Loading branch information
kjzz authored and schomatis committed Oct 29, 2018
1 parent 4973eab commit f573b7b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
12 changes: 11 additions & 1 deletion mod/dagmodifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -225,8 +235,8 @@ func (dm *DagModifier) Sync() error {
}

dm.writeStart += uint64(buflen)

dm.wrBuf = nil

return nil
}

Expand Down
49 changes: 49 additions & 0 deletions mod/dagmodifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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).
Expand Down

0 comments on commit f573b7b

Please sign in to comment.