torrent/storage/mmap.go

155 lines
3.2 KiB
Go
Raw Normal View History

2016-03-28 10:57:04 +00: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 {
baseDir string
2017-05-22 02:15:48 +00:00
pc PieceCompletion
2016-03-28 10:57:04 +00:00
}
func NewMMap(baseDir string) ClientImpl {
2017-05-22 02:15:48 +00:00
return NewMMapWithCompletion(baseDir, pieceCompletionForDir(baseDir))
}
func NewMMapWithCompletion(baseDir string, completion PieceCompletion) ClientImpl {
2016-03-28 10:57:04 +00:00
return &mmapStorage{
baseDir: baseDir,
2017-05-22 02:15:48 +00:00
pc: completion,
2016-03-28 10:57:04 +00:00
}
}
func (s *mmapStorage) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (t TorrentImpl, err error) {
span, err := mMapTorrent(info, s.baseDir)
t = &mmapTorrentStorage{
infoHash: infoHash,
span: span,
pc: s.pc,
2016-03-28 10:57:04 +00:00
}
return
2016-03-28 10:57:04 +00:00
}
func (s *mmapStorage) Close() error {
return s.pc.Close()
}
type mmapTorrentStorage struct {
infoHash metainfo.Hash
span mmap_span.MMapSpan
pc PieceCompletion
}
func (ts *mmapTorrentStorage) Piece(p metainfo.Piece) PieceImpl {
2016-03-28 10:57:04 +00:00
return mmapStoragePiece{
pc: ts.pc,
2016-03-28 10:57:04 +00:00
p: p,
ih: ts.infoHash,
ReaderAt: io.NewSectionReader(ts.span, p.Offset(), p.Length()),
WriterAt: missinggo.NewSectionWriter(ts.span, p.Offset(), p.Length()),
2016-03-28 10:57:04 +00:00
}
}
func (ts *mmapTorrentStorage) Close() error {
ts.pc.Close()
return ts.span.Close()
}
2016-03-28 10:57:04 +00:00
type mmapStoragePiece struct {
2017-05-22 02:15:48 +00:00
pc PieceCompletion
p metainfo.Piece
ih metainfo.Hash
2016-03-28 10:57:04 +00:00
io.ReaderAt
io.WriterAt
}
func (me mmapStoragePiece) pieceKey() metainfo.PieceKey {
return metainfo.PieceKey{me.ih, me.p.Index()}
}
2016-07-12 06:45:22 +00:00
func (sp mmapStoragePiece) GetIsComplete() (ret bool) {
ret, _ = sp.pc.Get(sp.pieceKey())
2016-07-12 06:45:22 +00:00
return
2016-03-28 10:57:04 +00:00
}
func (sp mmapStoragePiece) MarkComplete() error {
sp.pc.Set(sp.pieceKey(), true)
2016-03-28 10:57:04 +00:00
return nil
}
func (sp mmapStoragePiece) MarkNotComplete() error {
sp.pc.Set(sp.pieceKey(), false)
return nil
}
func mMapTorrent(md *metainfo.Info, location string) (mms mmap_span.MMapSpan, err error) {
2016-03-28 10:57:04 +00:00
defer func() {
if err != nil {
mms.Close()
}
}()
for _, miFile := range md.UpvertedFiles() {
fileName := filepath.Join(append([]string{location, md.Name}, miFile.Path...)...)
var mm mmap.MMap
mm, err = mmapFile(fileName, miFile.Length)
2016-03-28 10:57:04 +00:00
if err != nil {
err = fmt.Errorf("file %q: %s", miFile.DisplayPath(md), err)
2016-03-28 10:57:04 +00:00
return
}
mms.Append(mm)
}
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 10:57:04 +00:00
if err != nil {
return
}
}
if size == 0 {
// Can't mmap() regions with length 0.
return
}
ret, err = mmap.MapRegion(file,
int(size), // Probably not great on <64 bit systems.
mmap.RDWR, 0, 0)
if err != nil {
err = fmt.Errorf("mapping file %q, length %d: %s", file.Name(), size, err)
return
}
if int64(len(ret)) != size {
panic("mmap has wrong length")
}
2016-03-28 10:57:04 +00:00
return
}