2016-03-28 18:57:04 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/anacrolix/missinggo"
|
|
|
|
"github.com/edsrzf/mmap-go"
|
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
|
|
"github.com/anacrolix/torrent/mmap_span"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mmapStorage struct {
|
2016-07-08 22:36:32 +08:00
|
|
|
baseDir string
|
2016-03-28 18:57:04 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 19:50:43 +08:00
|
|
|
func NewMMap(baseDir string) Client {
|
2016-03-28 18:57:04 +08:00
|
|
|
return &mmapStorage{
|
2016-07-08 22:36:32 +08:00
|
|
|
baseDir: baseDir,
|
2016-03-28 18:57:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (s *mmapStorage) OpenTorrent(info *metainfo.InfoEx) (t Torrent, err error) {
|
2016-06-20 14:11:43 +08:00
|
|
|
span, err := mMapTorrent(&info.Info, s.baseDir)
|
2016-03-28 19:40:29 +08:00
|
|
|
t = &mmapTorrentStorage{
|
|
|
|
span: span,
|
2016-07-08 22:36:32 +08:00
|
|
|
pc: pieceCompletionForDir(s.baseDir),
|
2016-03-28 18:57:04 +08:00
|
|
|
}
|
2016-03-28 19:40:29 +08:00
|
|
|
return
|
2016-03-28 18:57:04 +08:00
|
|
|
}
|
|
|
|
|
2016-03-28 19:40:29 +08:00
|
|
|
type mmapTorrentStorage struct {
|
2016-06-20 16:20:59 +08:00
|
|
|
span mmap_span.MMapSpan
|
|
|
|
pc pieceCompletion
|
2016-03-28 19:40:29 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (ts *mmapTorrentStorage) Piece(p metainfo.Piece) Piece {
|
2016-03-28 18:57:04 +08:00
|
|
|
return mmapStoragePiece{
|
2016-06-20 16:20:59 +08:00
|
|
|
pc: ts.pc,
|
2016-03-28 18:57:04 +08:00
|
|
|
p: p,
|
2016-04-19 12:11:11 +08:00
|
|
|
ReaderAt: io.NewSectionReader(ts.span, p.Offset(), p.Length()),
|
|
|
|
WriterAt: missinggo.NewSectionWriter(ts.span, p.Offset(), p.Length()),
|
2016-03-28 18:57:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (ts *mmapTorrentStorage) Close() error {
|
2016-07-06 12:02:15 +08:00
|
|
|
ts.pc.Close()
|
|
|
|
return ts.span.Close()
|
2016-03-28 19:40:29 +08:00
|
|
|
}
|
|
|
|
|
2016-03-28 18:57:04 +08:00
|
|
|
type mmapStoragePiece struct {
|
2016-06-20 16:20:59 +08:00
|
|
|
pc pieceCompletion
|
|
|
|
p metainfo.Piece
|
2016-03-28 18:57:04 +08:00
|
|
|
io.ReaderAt
|
|
|
|
io.WriterAt
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (sp mmapStoragePiece) GetIsComplete() bool {
|
2016-06-20 16:20:59 +08:00
|
|
|
return sp.pc.Get(sp.p)
|
2016-03-28 18:57:04 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (sp mmapStoragePiece) MarkComplete() error {
|
2016-06-20 16:20:59 +08:00
|
|
|
sp.pc.Set(sp.p, true)
|
2016-03-28 18:57:04 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-20 14:11:43 +08:00
|
|
|
func mMapTorrent(md *metainfo.Info, location string) (mms mmap_span.MMapSpan, err error) {
|
2016-03-28 18:57:04 +08:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
mms.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
for _, miFile := range md.UpvertedFiles() {
|
|
|
|
fileName := filepath.Join(append([]string{location, md.Name}, miFile.Path...)...)
|
|
|
|
err = os.MkdirAll(filepath.Dir(fileName), 0777)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("error creating data directory %q: %s", filepath.Dir(fileName), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var file *os.File
|
|
|
|
file, err = os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
func() {
|
|
|
|
defer file.Close()
|
|
|
|
var fi os.FileInfo
|
|
|
|
fi, err = file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if fi.Size() < miFile.Length {
|
|
|
|
err = file.Truncate(miFile.Length)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if miFile.Length == 0 {
|
|
|
|
// Can't mmap() regions with length 0.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var mMap mmap.MMap
|
|
|
|
mMap, err = mmap.MapRegion(file,
|
|
|
|
int(miFile.Length), // Probably not great on <64 bit systems.
|
|
|
|
mmap.RDWR, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("error mapping file %q, length %d: %s", file.Name(), miFile.Length, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if int64(len(mMap)) != miFile.Length {
|
|
|
|
panic("mmap has wrong length")
|
|
|
|
}
|
|
|
|
mms.Append(mMap)
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|