9 "github.com/anacrolix/missinggo"
10 "github.com/edsrzf/mmap-go"
12 "github.com/anacrolix/torrent/metainfo"
13 "github.com/anacrolix/torrent/mmap_span"
16 type mmapStorage struct {
21 func NewMMap(baseDir string) ClientImpl {
22 return NewMMapWithCompletion(baseDir, pieceCompletionForDir(baseDir))
25 func NewMMapWithCompletion(baseDir string, completion PieceCompletion) ClientImpl {
32 func (s *mmapStorage) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (t TorrentImpl, err error) {
33 span, err := mMapTorrent(info, s.baseDir)
34 t = &mmapTorrentStorage{
42 func (s *mmapStorage) Close() error {
46 type mmapTorrentStorage struct {
47 infoHash metainfo.Hash
48 span mmap_span.MMapSpan
52 func (ts *mmapTorrentStorage) Piece(p metainfo.Piece) PieceImpl {
53 return mmapStoragePiece{
57 ReaderAt: io.NewSectionReader(ts.span, p.Offset(), p.Length()),
58 WriterAt: missinggo.NewSectionWriter(ts.span, p.Offset(), p.Length()),
62 func (ts *mmapTorrentStorage) Close() error {
64 return ts.span.Close()
67 type mmapStoragePiece struct {
75 func (me mmapStoragePiece) pieceKey() metainfo.PieceKey {
76 return metainfo.PieceKey{me.ih, me.p.Index()}
79 func (sp mmapStoragePiece) GetIsComplete() (ret bool) {
80 ret, _ = sp.pc.Get(sp.pieceKey())
84 func (sp mmapStoragePiece) MarkComplete() error {
85 sp.pc.Set(sp.pieceKey(), true)
89 func (sp mmapStoragePiece) MarkNotComplete() error {
90 sp.pc.Set(sp.pieceKey(), false)
94 func mMapTorrent(md *metainfo.Info, location string) (mms mmap_span.MMapSpan, err error) {
100 for _, miFile := range md.UpvertedFiles() {
101 fileName := filepath.Join(append([]string{location, md.Name}, miFile.Path...)...)
103 mm, err = mmapFile(fileName, miFile.Length)
105 err = fmt.Errorf("file %q: %s", miFile.DisplayPath(md), err)
115 func mmapFile(name string, size int64) (ret mmap.MMap, err error) {
116 dir := filepath.Dir(name)
117 err = os.MkdirAll(dir, 0777)
119 err = fmt.Errorf("making directory %q: %s", dir, err)
123 file, err = os.OpenFile(name, os.O_CREATE|os.O_RDWR, 0666)
129 fi, err = file.Stat()
133 if fi.Size() < size {
134 // I think this is necessary on HFS+. Maybe Linux will SIGBUS too if
135 // you overmap a file but I'm not sure.
136 err = file.Truncate(size)
142 // Can't mmap() regions with length 0.
145 ret, err = mmap.MapRegion(file,
146 int(size), // Probably not great on <64 bit systems.
149 err = fmt.Errorf("mapping file %q, length %d: %s", file.Name(), size, err)
152 if int64(len(ret)) != size {
153 panic("mmap has wrong length")