13 "github.com/anacrolix/missinggo/v2"
14 "github.com/edsrzf/mmap-go"
16 "github.com/anacrolix/torrent/metainfo"
17 "github.com/anacrolix/torrent/mmap_span"
20 type mmapClientImpl struct {
25 // TODO: Support all the same native filepath configuration that NewFileOpts provides.
26 func NewMMap(baseDir string) ClientImplCloser {
27 return NewMMapWithCompletion(baseDir, pieceCompletionForDir(baseDir))
30 func NewMMapWithCompletion(baseDir string, completion PieceCompletion) *mmapClientImpl {
31 return &mmapClientImpl{
37 func (s *mmapClientImpl) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (_ TorrentImpl, err error) {
38 span, err := mMapTorrent(info, s.baseDir)
39 t := &mmapTorrentStorage{
44 return TorrentImpl{Piece: t.Piece, Close: t.Close, Flush: t.Flush}, err
47 func (s *mmapClientImpl) Close() error {
51 type mmapTorrentStorage struct {
52 infoHash metainfo.Hash
53 span *mmap_span.MMapSpan
54 pc PieceCompletionGetSetter
57 func (ts *mmapTorrentStorage) Piece(p metainfo.Piece) PieceImpl {
58 return mmapStoragePiece{
62 ReaderAt: io.NewSectionReader(ts.span, p.Offset(), p.Length()),
63 WriterAt: missinggo.NewSectionWriter(ts.span, p.Offset(), p.Length()),
67 func (ts *mmapTorrentStorage) Close() error {
68 errs := ts.span.Close()
75 func (ts *mmapTorrentStorage) Flush() error {
76 errs := ts.span.Flush()
83 type mmapStoragePiece struct {
84 pc PieceCompletionGetSetter
91 func (me mmapStoragePiece) pieceKey() metainfo.PieceKey {
92 return metainfo.PieceKey{me.ih, me.p.Index()}
95 func (sp mmapStoragePiece) Completion() Completion {
96 c, err := sp.pc.Get(sp.pieceKey())
103 func (sp mmapStoragePiece) MarkComplete() error {
104 sp.pc.Set(sp.pieceKey(), true)
108 func (sp mmapStoragePiece) MarkNotComplete() error {
109 sp.pc.Set(sp.pieceKey(), false)
113 func mMapTorrent(md *metainfo.Info, location string) (mms *mmap_span.MMapSpan, err error) {
114 mms = &mmap_span.MMapSpan{}
120 for _, miFile := range md.UpvertedFiles() {
122 safeName, err = ToSafeFilePath(append([]string{md.Name}, miFile.Path...)...)
126 fileName := filepath.Join(location, safeName)
128 mm, err = mmapFile(fileName, miFile.Length)
130 err = fmt.Errorf("file %q: %s", miFile.DisplayPath(md), err)
139 func mmapFile(name string, size int64) (_ FileMapping, err error) {
140 dir := filepath.Dir(name)
141 err = os.MkdirAll(dir, 0o750)
143 err = fmt.Errorf("making directory %q: %s", dir, err)
147 file, err = os.OpenFile(name, os.O_CREATE|os.O_RDWR, 0o666)
157 fi, err = file.Stat()
161 if fi.Size() < size {
162 // I think this is necessary on HFS+. Maybe Linux will SIGBUS too if
163 // you overmap a file but I'm not sure.
164 err = file.Truncate(size)
169 return func() (ret mmapWithFile, err error) {
172 // Can't mmap() regions with length 0.
176 if int64(intLen) != size {
177 err = errors.New("size too large for system")
180 ret.mmap, err = mmap.MapRegion(file, intLen, mmap.RDWR, 0, 0)
182 err = fmt.Errorf("error mapping region: %s", err)
185 if int64(len(ret.mmap)) != size {
192 // Combines a mmapped region and file into a storage Mmap abstraction, which handles closing the
194 func WrapFileMapping(region mmap.MMap, file *os.File) FileMapping {
201 type FileMapping = mmap_span.Mmap
203 // Handles closing the mmap's file handle (needed for Windows). Could be implemented differently by
205 type mmapWithFile struct {
210 func (m mmapWithFile) Flush() error {
211 return m.mmap.Flush()
214 func (m mmapWithFile) Unmap() (err error) {
218 fileErr := m.f.Close()
225 func (m mmapWithFile) Bytes() []byte {