From 3e753bb8adbae94e1d5c361065a6d71e6011a29e Mon Sep 17 00:00:00 2001
From: Matt Joiner <anacrolix@gmail.com>
Date: Sat, 7 Mar 2015 17:11:45 +1100
Subject: [PATCH] Change up the data storage interfaces a bit

---
 client.go            | 12 +++++-------
 client_test.go       |  8 ++++----
 data/blob/blob.go    | 14 ++++++++++++--
 data/data.go         | 19 +++++++++++++++++++
 fs/torrentfs_test.go |  7 +++++--
 torrent.go           | 11 ++---------
 6 files changed, 47 insertions(+), 24 deletions(-)
 create mode 100644 data/data.go

diff --git a/client.go b/client.go
index 8eeb5ad4..ce8d4e3c 100644
--- a/client.go
+++ b/client.go
@@ -36,6 +36,7 @@ import (
 	"syscall"
 	"time"
 
+	"bitbucket.org/anacrolix/go.torrent/data"
 	filePkg "bitbucket.org/anacrolix/go.torrent/data/file"
 	"bitbucket.org/anacrolix/go.torrent/dht"
 	"bitbucket.org/anacrolix/go.torrent/internal/pieceordering"
@@ -455,8 +456,8 @@ func NewClient(cfg *Config) (cl *Client, err error) {
 		disableTCP:       cfg.DisableTCP,
 		_configDir:       cfg.ConfigDir,
 		config:           *cfg,
-		torrentDataOpener: func(md *metainfo.Info) (StatelessData, error) {
-			return filePkg.TorrentData(md, cfg.DataDir), nil
+		torrentDataOpener: func(md *metainfo.Info) data.Data {
+			return filePkg.TorrentData(md, cfg.DataDir)
 		},
 
 		quit:     make(chan struct{}),
@@ -1601,7 +1602,7 @@ func (cl *Client) setStorage(t *torrent, td Data) (err error) {
 	return
 }
 
-type TorrentDataOpener func(*metainfo.Info) (StatelessData, error)
+type TorrentDataOpener func(*metainfo.Info) data.Data
 
 func (cl *Client) setMetaData(t *torrent, md metainfo.Info, bytes []byte) (err error) {
 	err = t.setMetadata(md, bytes, &cl.mu)
@@ -1619,10 +1620,7 @@ func (cl *Client) setMetaData(t *torrent, md metainfo.Info, bytes []byte) (err e
 		return
 	}
 	close(t.gotMetainfo)
-	td, err := cl.torrentDataOpener(&md)
-	if err != nil {
-		return
-	}
+	td := cl.torrentDataOpener(&md)
 	err = cl.setStorage(t, td)
 	return
 }
diff --git a/client_test.go b/client_test.go
index 48eddfec..01d923b8 100644
--- a/client_test.go
+++ b/client_test.go
@@ -12,7 +12,6 @@ import (
 	"time"
 
 	"bitbucket.org/anacrolix/go.torrent/data/blob"
-	"github.com/anacrolix/libtorgo/metainfo"
 
 	"github.com/bradfitz/iter"
 
@@ -254,9 +253,10 @@ func TestClientTransfer(t *testing.T) {
 		t.Fatal(err)
 	}
 	defer os.RemoveAll(leecherDataDir)
-	cfg.TorrentDataOpener = func(info *metainfo.Info) (StatelessData, error) {
-		return blob.TorrentData(info, leecherDataDir), nil
-	}
+	// cfg.TorrentDataOpener = func(info *metainfo.Info) (data.Data, error) {
+	// 	return blob.TorrentData(info, leecherDataDir), nil
+	// }
+	cfg.TorrentDataOpener = blob.NewStore(leecherDataDir).OpenTorrent
 	leecher, _ := NewClient(&cfg)
 	defer leecher.Stop()
 	leecherGreeting, _ := leecher.AddTorrent(mi)
diff --git a/data/blob/blob.go b/data/blob/blob.go
index 2f871d6f..5802c18a 100644
--- a/data/blob/blob.go
+++ b/data/blob/blob.go
@@ -9,6 +9,8 @@ import (
 	"os"
 	"path/filepath"
 
+	dataPkg "bitbucket.org/anacrolix/go.torrent/data"
+
 	"github.com/anacrolix/libtorgo/metainfo"
 )
 
@@ -22,8 +24,16 @@ type data struct {
 	baseDir string
 }
 
-func TorrentData(info *metainfo.Info, baseDir string) *data {
-	return &data{info, baseDir}
+type store struct {
+	baseDir string
+}
+
+func (me store) OpenTorrent(info *metainfo.Info) dataPkg.Data {
+	return &data{info, me.baseDir}
+}
+
+func NewStore(baseDir string) dataPkg.Store {
+	return store{baseDir}
 }
 
 func (me *data) pieceHashHex(i int) string {
diff --git a/data/data.go b/data/data.go
new file mode 100644
index 00000000..2364eee3
--- /dev/null
+++ b/data/data.go
@@ -0,0 +1,19 @@
+package data
+
+import (
+	"io"
+
+	"github.com/anacrolix/libtorgo/metainfo"
+)
+
+type Store interface {
+	OpenTorrent(*metainfo.Info) Data
+}
+
+type Data interface {
+	// OpenSection(off, n int64) (io.ReadCloser, error)
+	// ReadAt(p []byte, off int64) (n int, err error)
+	// Close()
+	WriteAt(p []byte, off int64) (n int, err error)
+	WriteSectionTo(w io.Writer, off, n int64) (written int64, err error)
+}
diff --git a/fs/torrentfs_test.go b/fs/torrentfs_test.go
index 4c865ba4..ab8bc0a0 100644
--- a/fs/torrentfs_test.go
+++ b/fs/torrentfs_test.go
@@ -14,6 +14,8 @@ import (
 	"testing"
 	"time"
 
+	"bitbucket.org/anacrolix/go.torrent/data"
+
 	"golang.org/x/net/context"
 
 	"bitbucket.org/anacrolix/go.torrent"
@@ -191,8 +193,9 @@ func TestDownloadOnDemand(t *testing.T) {
 
 		NoDefaultBlocklist: true,
 
-		TorrentDataOpener: func(info *metainfo.Info) (torrent.StatelessData, error) {
-			return mmap.TorrentData(info, filepath.Join(layout.BaseDir, "download"))
+		TorrentDataOpener: func(info *metainfo.Info) data.Data {
+			ret, _ := mmap.TorrentData(info, filepath.Join(layout.BaseDir, "download"))
+			return ret
 		},
 
 		// This can be used to check if clients can connect to other clients
diff --git a/torrent.go b/torrent.go
index cd9f2633..13ca8aa2 100644
--- a/torrent.go
+++ b/torrent.go
@@ -12,6 +12,7 @@ import (
 	"sync"
 	"time"
 
+	"bitbucket.org/anacrolix/go.torrent/data"
 	pp "bitbucket.org/anacrolix/go.torrent/peer_protocol"
 	"bitbucket.org/anacrolix/go.torrent/tracker"
 	"bitbucket.org/anacrolix/go.torrent/util"
@@ -43,18 +44,10 @@ type peersKey struct {
 	Port    int
 }
 
-type StatelessData interface {
-	// OpenSection(off, n int64) (io.ReadCloser, error)
-	// ReadAt(p []byte, off int64) (n int, err error)
-	// Close()
-	WriteAt(p []byte, off int64) (n int, err error)
-	WriteSectionTo(w io.Writer, off, n int64) (written int64, err error)
-}
-
 // Represents data storage for a Torrent. Additional optional interfaces to
 // implement are io.Closer, io.ReaderAt, StatefulData, and SectionOpener.
 type Data interface {
-	StatelessData
+	data.Data
 }
 
 // Data maintains per-piece persistent state.
-- 
2.51.0