2016-03-28 18:57:04 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2017-12-02 07:26:09 +08:00
|
|
|
"errors"
|
2016-03-28 18:57:04 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/anacrolix/missinggo"
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
|
|
"github.com/anacrolix/torrent/mmap_span"
|
2018-11-02 20:12:01 +08:00
|
|
|
"github.com/edsrzf/mmap-go"
|
2016-03-28 18:57:04 +08:00
|
|
|
)
|
|
|
|
|
2018-01-06 13:38:41 +08:00
|
|
|
type mmapClientImpl struct {
|
2016-07-08 22:36:32 +08:00
|
|
|
baseDir string
|
2017-05-22 10:15:48 +08:00
|
|
|
pc PieceCompletion
|
2016-03-28 18:57:04 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 13:10:57 +08:00
|
|
|
func NewMMap(baseDir string) ClientImpl {
|
2017-05-22 10:15:48 +08:00
|
|
|
return NewMMapWithCompletion(baseDir, pieceCompletionForDir(baseDir))
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMMapWithCompletion(baseDir string, completion PieceCompletion) ClientImpl {
|
2018-01-06 13:38:41 +08:00
|
|
|
return &mmapClientImpl{
|
2016-07-08 22:36:32 +08:00
|
|
|
baseDir: baseDir,
|
2017-05-22 10:15:48 +08:00
|
|
|
pc: completion,
|
2016-03-28 18:57:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-06 13:38:41 +08:00
|
|
|
func (s *mmapClientImpl) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (t TorrentImpl, err error) {
|
2016-08-26 18:29:05 +08:00
|
|
|
span, err := mMapTorrent(info, s.baseDir)
|
2016-03-28 19:40:29 +08:00
|
|
|
t = &mmapTorrentStorage{
|
2017-07-10 19:33:39 +08:00
|
|
|
infoHash: infoHash,
|
|
|
|
span: span,
|
|
|
|
pc: s.pc,
|
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
|
|
|
}
|
|
|
|
|
2018-01-06 13:38:41 +08:00
|
|
|
func (s *mmapClientImpl) Close() error {
|
2016-10-25 16:07:26 +08:00
|
|
|
return s.pc.Close()
|
|
|
|
}
|
|
|
|
|
2016-03-28 19:40:29 +08:00
|
|
|
type mmapTorrentStorage struct {
|
2017-07-10 19:33:39 +08:00
|
|
|
infoHash metainfo.Hash
|
2018-02-02 16:28:09 +08:00
|
|
|
span *mmap_span.MMapSpan
|
2017-07-10 19:33:39 +08:00
|
|
|
pc PieceCompletion
|
2016-03-28 19:40:29 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 13:10:57 +08:00
|
|
|
func (ts *mmapTorrentStorage) Piece(p metainfo.Piece) PieceImpl {
|
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,
|
2017-07-10 19:33:39 +08:00
|
|
|
ih: ts.infoHash,
|
2018-02-02 16:28:09 +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 {
|
2017-05-22 10:15:48 +08:00
|
|
|
pc PieceCompletion
|
2016-06-20 16:20:59 +08:00
|
|
|
p metainfo.Piece
|
2016-08-26 18:29:05 +08:00
|
|
|
ih metainfo.Hash
|
2016-03-28 18:57:04 +08:00
|
|
|
io.ReaderAt
|
|
|
|
io.WriterAt
|
|
|
|
}
|
|
|
|
|
2016-08-26 18:29:05 +08:00
|
|
|
func (me mmapStoragePiece) pieceKey() metainfo.PieceKey {
|
|
|
|
return metainfo.PieceKey{me.ih, me.p.Index()}
|
|
|
|
}
|
|
|
|
|
2017-10-12 13:09:32 +08:00
|
|
|
func (sp mmapStoragePiece) Completion() Completion {
|
|
|
|
c, _ := sp.pc.Get(sp.pieceKey())
|
|
|
|
return c
|
2016-03-28 18:57:04 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (sp mmapStoragePiece) MarkComplete() error {
|
2016-08-26 18:29:05 +08:00
|
|
|
sp.pc.Set(sp.pieceKey(), true)
|
2016-03-28 18:57:04 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-02 13:10:57 +08:00
|
|
|
func (sp mmapStoragePiece) MarkNotComplete() error {
|
|
|
|
sp.pc.Set(sp.pieceKey(), false)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-02 16:28:09 +08:00
|
|
|
func mMapTorrent(md *metainfo.Info, location string) (mms *mmap_span.MMapSpan, err error) {
|
|
|
|
mms = &mmap_span.MMapSpan{}
|
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...)...)
|
2017-09-18 10:19:34 +08:00
|
|
|
var mm mmap.MMap
|
|
|
|
mm, err = mmapFile(fileName, miFile.Length)
|
2016-03-28 18:57:04 +08:00
|
|
|
if err != nil {
|
2017-09-18 10:19:34 +08:00
|
|
|
err = fmt.Errorf("file %q: %s", miFile.DisplayPath(md), err)
|
2016-03-28 18:57:04 +08:00
|
|
|
return
|
|
|
|
}
|
2017-09-18 11:41:19 +08:00
|
|
|
if mm != nil {
|
|
|
|
mms.Append(mm)
|
|
|
|
}
|
2017-09-18 10:19:34 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func mmapFile(name string, size int64) (ret mmap.MMap, err error) {
|
|
|
|
dir := filepath.Dir(name)
|
|
|
|
err = os.MkdirAll(dir, 0777)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("making directory %q: %s", dir, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var file *os.File
|
|
|
|
file, err = os.OpenFile(name, os.O_CREATE|os.O_RDWR, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
var fi os.FileInfo
|
|
|
|
fi, err = file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if fi.Size() < size {
|
|
|
|
// I think this is necessary on HFS+. Maybe Linux will SIGBUS too if
|
|
|
|
// you overmap a file but I'm not sure.
|
|
|
|
err = file.Truncate(size)
|
2016-03-28 18:57:04 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-09-18 10:19:34 +08:00
|
|
|
if size == 0 {
|
|
|
|
// Can't mmap() regions with length 0.
|
|
|
|
return
|
|
|
|
}
|
2017-12-02 07:26:09 +08:00
|
|
|
intLen := int(size)
|
|
|
|
if int64(intLen) != size {
|
|
|
|
err = errors.New("size too large for system")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ret, err = mmap.MapRegion(file, intLen, mmap.RDWR, 0, 0)
|
2017-09-18 10:19:34 +08:00
|
|
|
if err != nil {
|
2017-12-02 07:26:09 +08:00
|
|
|
err = fmt.Errorf("error mapping region: %s", err)
|
2017-09-18 10:19:34 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if int64(len(ret)) != size {
|
2017-12-02 07:26:09 +08:00
|
|
|
panic(len(ret))
|
2017-09-18 10:19:34 +08:00
|
|
|
}
|
2016-03-28 18:57:04 +08:00
|
|
|
return
|
|
|
|
}
|