Switch from launchpad.net/gommap to github.com/edsrzf/mmap-go for Windows support

Fixes #45.
This commit is contained in:
Matt Joiner 2015-12-23 03:50:34 +11:00
parent f9a04b4f29
commit 812f5af66b
3 changed files with 17 additions and 16 deletions

View File

@ -9,7 +9,7 @@ import (
"os"
"path/filepath"
"launchpad.net/gommap"
"github.com/edsrzf/mmap-go"
"github.com/anacrolix/torrent/metainfo"
"github.com/anacrolix/torrent/mmap_span"
@ -20,13 +20,12 @@ var (
dataPath = flag.String("path", "/torrent/data", "path of the torrent data")
)
func fileToMmap(filename string, length int64, devZero *os.File) gommap.MMap {
func fileToMmap(filename string, length int64, devZero *os.File) mmap.MMap {
osFile, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
mmapFd := osFile.Fd()
goMMap, err := gommap.MapRegion(mmapFd, 0, length, gommap.PROT_READ, gommap.MAP_PRIVATE)
goMMap, err := mmap.MapRegion(osFile, int(length), mmap.RDONLY, mmap.COPY, 0)
if err != nil {
log.Fatal(err)
}

View File

@ -5,7 +5,7 @@ import (
"os"
"path/filepath"
"launchpad.net/gommap"
"github.com/edsrzf/mmap-go"
"github.com/anacrolix/torrent/metainfo"
"github.com/anacrolix/torrent/mmap_span"
@ -64,8 +64,10 @@ func TorrentData(md *metainfo.Info, location string) (ret *torrentData, err erro
// Can't mmap() regions with length 0.
return
}
var mMap gommap.MMap
mMap, err = gommap.MapRegion(file.Fd(), 0, miFile.Length, gommap.PROT_READ|gommap.PROT_WRITE, gommap.MAP_SHARED)
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

View File

@ -3,28 +3,28 @@ package mmap_span
import (
"io"
"launchpad.net/gommap"
"github.com/edsrzf/mmap-go"
)
type segment struct {
gommap.MMap
*mmap.MMap
}
func (me segment) Size() int64 {
return int64(len(me.MMap))
return int64(len(*me.MMap))
}
type MMapSpan struct {
span
}
func (me *MMapSpan) Append(mmap gommap.MMap) {
me.span = append(me.span, segment{mmap})
func (me *MMapSpan) Append(mmap mmap.MMap) {
me.span = append(me.span, segment{&mmap})
}
func (me MMapSpan) Close() {
for _, mMap := range me.span {
mMap.(segment).UnsafeUnmap()
mMap.(segment).Unmap()
}
}
@ -37,7 +37,7 @@ func (me MMapSpan) Size() (ret int64) {
func (me MMapSpan) ReadAt(p []byte, off int64) (n int, err error) {
me.ApplyTo(off, func(intervalOffset int64, interval sizer) (stop bool) {
_n := copy(p, interval.(segment).MMap[intervalOffset:])
_n := copy(p, (*interval.(segment).MMap)[intervalOffset:])
p = p[_n:]
n += _n
return len(p) == 0
@ -51,7 +51,7 @@ func (me MMapSpan) ReadAt(p []byte, off int64) (n int, err error) {
func (me MMapSpan) WriteSectionTo(w io.Writer, off, n int64) (written int64, err error) {
me.ApplyTo(off, func(intervalOffset int64, interval sizer) (stop bool) {
var _n int
p := interval.(segment).MMap[intervalOffset:]
p := (*interval.(segment).MMap)[intervalOffset:]
if n < int64(len(p)) {
p = p[:n]
}
@ -69,7 +69,7 @@ func (me MMapSpan) WriteSectionTo(w io.Writer, off, n int64) (written int64, err
func (me MMapSpan) WriteAt(p []byte, off int64) (n int, err error) {
me.ApplyTo(off, func(iOff int64, i sizer) (stop bool) {
mMap := i.(segment)
_n := copy(mMap.MMap[iOff:], p)
_n := copy((*mMap.MMap)[iOff:], p)
// err = mMap.Sync(gommap.MS_ASYNC)
// if err != nil {
// return true