]> Sergey Matveev's repositories - btrtrc.git/blobdiff - misc.go
Encapsulate torrent data, and provide os.File and mmap-based implementations
[btrtrc.git] / misc.go
diff --git a/misc.go b/misc.go
index 85b8f588411dfbf95f02196578884865cf00b7c1..f14c8f74d9d752b1c605bcb91954b7ac5801d17c 100644 (file)
--- a/misc.go
+++ b/misc.go
@@ -5,15 +5,10 @@ import (
        "errors"
        "fmt"
        "math/rand"
-       "os"
-       "path/filepath"
        "sync"
        "time"
 
-       "bitbucket.org/anacrolix/go.torrent/mmap_span"
        "bitbucket.org/anacrolix/go.torrent/peer_protocol"
-       "github.com/anacrolix/libtorgo/metainfo"
-       "launchpad.net/gommap"
 )
 
 const (
@@ -104,68 +99,6 @@ var (
        ErrDataNotReady = errors.New("data not ready")
 )
 
-func upvertedSingleFileInfoFiles(info *metainfo.Info) []metainfo.FileInfo {
-       if len(info.Files) != 0 {
-               return info.Files
-       }
-       return []metainfo.FileInfo{{Length: info.Length, Path: nil}}
-}
-
-func mmapTorrentData(md *metainfo.Info, location string) (mms *mmap_span.MMapSpan, err error) {
-       mms = &mmap_span.MMapSpan{}
-       defer func() {
-               if err != nil {
-                       mms.Close()
-                       mms = nil
-               }
-       }()
-       for _, miFile := range upvertedSingleFileInfoFiles(md) {
-               fileName := filepath.Join(append([]string{location, md.Name}, miFile.Path...)...)
-               err = os.MkdirAll(filepath.Dir(fileName), 0777)
-               if err != nil {
-                       err = fmt.Errorf("error creating data directory %q: %s", filepath.Dir(fileName), err)
-                       return
-               }
-               var file *os.File
-               file, err = os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0666)
-               if err != nil {
-                       return
-               }
-               func() {
-                       defer file.Close()
-                       var fi os.FileInfo
-                       fi, err = file.Stat()
-                       if err != nil {
-                               return
-                       }
-                       if fi.Size() < miFile.Length {
-                               err = file.Truncate(miFile.Length)
-                               if err != nil {
-                                       return
-                               }
-                       }
-                       if miFile.Length == 0 {
-                               // 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)
-                       if err != nil {
-                               err = fmt.Errorf("error mapping file %q, length %d: %s", file.Name(), miFile.Length, err)
-                               return
-                       }
-                       if int64(len(mMap)) != miFile.Length {
-                               panic("mmap has wrong length")
-                       }
-                       mms.Append(mMap)
-               }()
-               if err != nil {
-                       return
-               }
-       }
-       return
-}
-
 // The size in bytes of a metadata extension piece.
 func metadataPieceSize(totalSize int, piece int) int {
        ret := totalSize - piece*(1<<14)