]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Some utils moved to missinggo
authorMatt Joiner <anacrolix@gmail.com>
Mon, 3 Aug 2015 14:29:01 +0000 (00:29 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 3 Aug 2015 14:29:01 +0000 (00:29 +1000)
13 files changed:
client_test.go
dht/addr.go
dht/announce.go
dht/dht.go
dht/dht_test.go
fs/torrentfs_test.go
iplist/iplist_test.go
mse/mse.go
torrent.go
util/addr.go [deleted file]
util/copy.go [deleted file]
util/copy_test.go [deleted file]
util/dirwatch/dirwatch.go

index 5e416fb42bb92625ed9d123fa95b0005dc6df69d..7bc9756c063a81d653980ff4f93585bd8d2d373c 100644 (file)
@@ -13,6 +13,7 @@ import (
        "time"
 
        _ "github.com/anacrolix/envpprof"
+       "github.com/anacrolix/missinggo"
        "github.com/anacrolix/utp"
        "github.com/bradfitz/iter"
        "github.com/stretchr/testify/assert"
@@ -23,7 +24,6 @@ import (
        "github.com/anacrolix/torrent/data/blob"
        "github.com/anacrolix/torrent/internal/testutil"
        "github.com/anacrolix/torrent/metainfo"
-       "github.com/anacrolix/torrent/util"
 )
 
 func init() {
@@ -87,7 +87,7 @@ func TestTorrentInitialState(t *testing.T) {
        dir, mi := testutil.GreetingTestTorrent()
        defer os.RemoveAll(dir)
        tor, err := newTorrent(func() (ih InfoHash) {
-               util.CopyExact(ih[:], mi.Info.Hash)
+               missinggo.CopyExact(ih[:], mi.Info.Hash)
                return
        }())
        if err != nil {
@@ -196,7 +196,7 @@ func TestUTPRawConn(t *testing.T) {
                        }
                }
        }()
-       udpAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("localhost:%d", util.AddrPort(l.Addr())))
+       udpAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("localhost:%d", missinggo.AddrPort(l.Addr())))
        if err != nil {
                t.Fatal(err)
        }
@@ -274,8 +274,8 @@ func TestClientTransfer(t *testing.T) {
        }())
        leecherGreeting.AddPeers([]Peer{
                Peer{
-                       IP:   util.AddrIP(seeder.ListenAddr()),
-                       Port: util.AddrPort(seeder.ListenAddr()),
+                       IP:   missinggo.AddrIP(seeder.ListenAddr()),
+                       Port: missinggo.AddrPort(seeder.ListenAddr()),
                },
        })
        r := leecherGreeting.NewReader()
@@ -430,8 +430,8 @@ func TestResponsive(t *testing.T) {
        }())
        leecherTorrent.AddPeers([]Peer{
                Peer{
-                       IP:   util.AddrIP(seeder.ListenAddr()),
-                       Port: util.AddrPort(seeder.ListenAddr()),
+                       IP:   missinggo.AddrIP(seeder.ListenAddr()),
+                       Port: missinggo.AddrPort(seeder.ListenAddr()),
                },
        })
        reader := leecherTorrent.NewReader()
index ea6589d41b19a774e85c2dce9345b0cd58c4fbaa..a0abf96a8a2a386472b3f949263bc1cee0ef4ff1 100644 (file)
@@ -3,7 +3,7 @@ package dht
 import (
        "net"
 
-       "github.com/anacrolix/torrent/util"
+       "github.com/anacrolix/missinggo"
 )
 
 // Used internally to refer to node network addresses.
@@ -37,5 +37,5 @@ func (ca cachedAddr) IP() net.IP {
 }
 
 func newDHTAddr(addr net.Addr) dHTAddr {
-       return cachedAddr{addr, addr.String(), util.AddrIP(addr)}
+       return cachedAddr{addr, addr.String(), missinggo.AddrIP(addr)}
 }
index a179263b11e54d4f8271d815c22295e0d172df9e..75a9558e643779cafc4986faa547de335ba02c7e 100644 (file)
@@ -6,6 +6,7 @@ import (
        "log"
        "time"
 
+       "github.com/anacrolix/missinggo"
        "github.com/anacrolix/sync"
        "github.com/willf/bloom"
 
@@ -97,7 +98,7 @@ func (s *Server) Announce(infoHash string, port int, impliedPort bool) (*Announc
 }
 
 func (me *Announce) gotNodeAddr(addr dHTAddr) {
-       if util.AddrPort(addr) == 0 {
+       if missinggo.AddrPort(addr) == 0 {
                // Not a contactable address.
                return
        }
index ff27fcff04afeaa9163bfff471bd425cfa5c31fa..ed544f6066163216b9bca3497cf78682f76c96e7 100644 (file)
@@ -20,6 +20,7 @@ import (
        "os"
        "time"
 
+       "github.com/anacrolix/missinggo"
        "github.com/anacrolix/sync"
 
        "github.com/anacrolix/torrent/bencode"
@@ -637,7 +638,7 @@ func (s *Server) serve() error {
                        continue
                }
                s.mu.Lock()
-               blocked := s.ipBlocked(util.AddrIP(addr))
+               blocked := s.ipBlocked(missinggo.AddrIP(addr))
                s.mu.Unlock()
                if blocked {
                        readBlocked.Add(1)
@@ -809,7 +810,7 @@ func (s *Server) nodeTimedOut(addr dHTAddr) {
 
 func (s *Server) writeToNode(b []byte, node dHTAddr) (err error) {
        if list := s.ipBlockList; list != nil {
-               if r := list.Lookup(util.AddrIP(node.UDPAddr())); r != nil {
+               if r := list.Lookup(missinggo.AddrIP(node.UDPAddr())); r != nil {
                        err = fmt.Errorf("write to %s blocked: %s", node, r.Description)
                        return
                }
@@ -908,14 +909,14 @@ func (ni *NodeInfo) PutCompact(b []byte) error {
        if n := copy(b[:], ni.ID[:]); n != 20 {
                panic(n)
        }
-       ip := util.AddrIP(ni.Addr).To4()
+       ip := missinggo.AddrIP(ni.Addr).To4()
        if len(ip) != 4 {
                return errors.New("expected ipv4 address")
        }
        if n := copy(b[20:], ip); n != 4 {
                panic(n)
        }
-       binary.BigEndian.PutUint16(b[24:], uint16(util.AddrPort(ni.Addr)))
+       binary.BigEndian.PutUint16(b[24:], uint16(missinggo.AddrPort(ni.Addr)))
        return nil
 }
 
@@ -923,7 +924,7 @@ func (cni *NodeInfo) UnmarshalCompact(b []byte) error {
        if len(b) != 26 {
                return errors.New("expected 26 bytes")
        }
-       util.CopyExact(cni.ID[:], b[:20])
+       missinggo.CopyExact(cni.ID[:], b[:20])
        cni.Addr = newDHTAddr(&net.UDPAddr{
                IP:   net.IPv4(b[20], b[21], b[22], b[23]),
                Port: int(binary.BigEndian.Uint16(b[24:26])),
@@ -969,11 +970,11 @@ func (s *Server) liftNodes(d Msg) {
                return
        }
        for _, cni := range d.Nodes() {
-               if util.AddrPort(cni.Addr) == 0 {
+               if missinggo.AddrPort(cni.Addr) == 0 {
                        // TODO: Why would people even do this?
                        continue
                }
-               if s.ipBlocked(util.AddrIP(cni.Addr)) {
+               if s.ipBlocked(missinggo.AddrIP(cni.Addr)) {
                        continue
                }
                n := s.getNode(cni.Addr, string(cni.ID[:]))
index 8091242a446ceb0f90fcfb67089113a4220362e1..a797a60610edde5b503f7df57e90a7601dd9f465 100644 (file)
@@ -7,7 +7,7 @@ import (
        "net"
        "testing"
 
-       "github.com/anacrolix/torrent/util"
+       "github.com/anacrolix/missinggo"
 )
 
 func TestSetNilBigInt(t *testing.T) {
@@ -207,7 +207,7 @@ func TestServerDefaultNodeIdSecure(t *testing.T) {
                t.Fatal(err)
        }
        defer s.Close()
-       if !nodeIdSecure(s.ID(), util.AddrIP(s.Addr())) {
+       if !nodeIdSecure(s.ID(), missinggo.AddrIP(s.Addr())) {
                t.Fatal("not secure")
        }
 }
index c14cb21c8fa9e1d38487ca7fa92ea1cfa3677dbc..a83a0cd536f09a761e303ad9446d4869b3980956 100644 (file)
@@ -17,6 +17,7 @@ import (
 
        "bazil.org/fuse"
        fusefs "bazil.org/fuse/fs"
+       "github.com/anacrolix/missinggo"
        netContext "golang.org/x/net/context"
 
        "github.com/anacrolix/torrent"
@@ -24,7 +25,6 @@ import (
        "github.com/anacrolix/torrent/data/mmap"
        "github.com/anacrolix/torrent/internal/testutil"
        "github.com/anacrolix/torrent/metainfo"
-       "github.com/anacrolix/torrent/util"
 )
 
 func init() {
@@ -45,7 +45,7 @@ func TestTCPAddrString(t *testing.T) {
        ras := c.RemoteAddr().String()
        ta := &net.TCPAddr{
                IP:   net.IPv4(127, 0, 0, 1),
-               Port: util.AddrPort(l.Addr()),
+               Port: missinggo.AddrPort(l.Addr()),
        }
        s := ta.String()
        if ras != s {
index f21ecc3dbad632730b79adc69e5d4c05e27b1bd7..b65f13bb99b513b37e1f925eaa0193e7dc8c0137 100644 (file)
@@ -7,9 +7,8 @@ import (
        "strings"
        "testing"
 
+       "github.com/anacrolix/missinggo"
        "github.com/bradfitz/iter"
-
-       "github.com/anacrolix/torrent/util"
 )
 
 var sample = `
@@ -58,7 +57,7 @@ func connRemoteAddrIP(network, laddr string, dialHost string) net.IP {
                panic(err)
        }
        go func() {
-               c, err := net.Dial(network, net.JoinHostPort(dialHost, fmt.Sprintf("%d", util.AddrPort(l.Addr()))))
+               c, err := net.Dial(network, net.JoinHostPort(dialHost, fmt.Sprintf("%d", missinggo.AddrPort(l.Addr()))))
                if err != nil {
                        panic(err)
                }
@@ -69,7 +68,7 @@ func connRemoteAddrIP(network, laddr string, dialHost string) net.IP {
                panic(err)
        }
        defer c.Close()
-       ret := util.AddrIP(c.RemoteAddr())
+       ret := missinggo.AddrIP(c.RemoteAddr())
        return ret
 }
 
index 9995301f929a7755c1040cd5f6e4921ce3bc351b..7c21c7e10507f81eb30fded0973b1ba575dba261 100644 (file)
@@ -17,9 +17,8 @@ import (
        "strconv"
        "sync"
 
+       "github.com/anacrolix/missinggo"
        "github.com/bradfitz/iter"
-
-       "github.com/anacrolix/torrent/util"
 )
 
 const (
@@ -168,7 +167,7 @@ func (h *handshake) establishS() (err error) {
        var Y, S big.Int
        Y.SetBytes(b[:])
        S.Exp(&Y, &x, &p)
-       util.CopyExact(&h.s, paddedLeft(S.Bytes(), 96))
+       missinggo.CopyExact(&h.s, paddedLeft(S.Bytes(), 96))
        return
 }
 
index c263dcafce477e402c9738618bc68defc0c77847..12eca58459ebcab79317df581b5af6f68f5cff58 100644 (file)
@@ -18,7 +18,6 @@ import (
        "github.com/anacrolix/torrent/metainfo"
        pp "github.com/anacrolix/torrent/peer_protocol"
        "github.com/anacrolix/torrent/tracker"
-       "github.com/anacrolix/torrent/util"
 )
 
 func (t *torrent) pieceNumPendingBytes(index int) (count pp.Integer) {
@@ -226,7 +225,7 @@ func (t *torrent) setMetadata(md *metainfo.Info, infoBytes []byte, eventLocker s
        for _, hash := range infoPieceHashes(md) {
                piece := &piece{}
                piece.Event.L = eventLocker
-               util.CopyExact(piece.Hash[:], hash)
+               missinggo.CopyExact(piece.Hash[:], hash)
                t.Pieces = append(t.Pieces, piece)
        }
        for _, conn := range t.Conns {
@@ -641,7 +640,7 @@ func (t *torrent) hashPiece(piece pp.Integer) (ps pieceSum) {
        p := t.Pieces[piece]
        p.pendingWrites.Wait()
        t.data.WriteSectionTo(hash, int64(piece)*t.Info.PieceLength, t.Info.PieceLength)
-       util.CopyExact(ps[:], hash.Sum(nil))
+       missinggo.CopyExact(ps[:], hash.Sum(nil))
        return
 }
 
diff --git a/util/addr.go b/util/addr.go
deleted file mode 100644 (file)
index 44354da..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-package util
-
-import (
-       "net"
-       "strconv"
-)
-
-// Extracts the port as an integer from an address string.
-func AddrPort(addr net.Addr) int {
-       switch raw := addr.(type) {
-       case *net.UDPAddr:
-               return raw.Port
-       default:
-               _, port, err := net.SplitHostPort(addr.String())
-               if err != nil {
-                       panic(err)
-               }
-               i64, err := strconv.ParseInt(port, 0, 0)
-               if err != nil {
-                       panic(err)
-               }
-               return int(i64)
-       }
-}
-
-func AddrIP(addr net.Addr) net.IP {
-       switch raw := addr.(type) {
-       case *net.UDPAddr:
-               return raw.IP
-       case *net.TCPAddr:
-               return raw.IP
-       default:
-               host, _, err := net.SplitHostPort(addr.String())
-               if err != nil {
-                       panic(err)
-               }
-               return net.ParseIP(host)
-       }
-}
diff --git a/util/copy.go b/util/copy.go
deleted file mode 100644 (file)
index 94d9825..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-package util
-
-import (
-       "fmt"
-       "reflect"
-)
-
-func CopyExact(dest interface{}, src interface{}) {
-       dV := reflect.ValueOf(dest)
-       sV := reflect.ValueOf(src)
-       if dV.Kind() == reflect.Ptr {
-               dV = dV.Elem()
-       }
-       if dV.Kind() == reflect.Array && !dV.CanAddr() {
-               panic(fmt.Sprintf("dest not addressable: %T", dest))
-       }
-       if sV.Kind() == reflect.Ptr {
-               sV = sV.Elem()
-       }
-       if sV.Kind() == reflect.String {
-               sV = sV.Convert(reflect.SliceOf(dV.Type().Elem()))
-       }
-       if !sV.IsValid() {
-               panic("invalid source, probably nil")
-       }
-       if dV.Len() != sV.Len() {
-               panic(fmt.Sprintf("dest len (%d) != src len (%d)", dV.Len(), sV.Len()))
-       }
-       if dV.Len() != reflect.Copy(dV, sV) {
-               panic("dammit")
-       }
-}
diff --git a/util/copy_test.go b/util/copy_test.go
deleted file mode 100644 (file)
index 4529871..0000000
+++ /dev/null
@@ -1,89 +0,0 @@
-package util
-
-import (
-       "bytes"
-       "strings"
-       "testing"
-)
-
-func TestCopyToArray(t *testing.T) {
-       var arr [3]byte
-       bb := []byte{1, 2, 3}
-       CopyExact(&arr, bb)
-       if !bytes.Equal(arr[:], bb) {
-               t.FailNow()
-       }
-}
-
-func TestCopyToSlicedArray(t *testing.T) {
-       var arr [5]byte
-       CopyExact(arr[:], "hello")
-       if !bytes.Equal(arr[:], []byte("hello")) {
-               t.FailNow()
-       }
-}
-
-func TestCopyDestNotAddr(t *testing.T) {
-       defer func() {
-               r := recover()
-               if r == nil {
-                       t.FailNow()
-               }
-               t.Log(r)
-       }()
-       var arr [3]byte
-       CopyExact(arr, "nope")
-}
-
-func TestCopyLenMismatch(t *testing.T) {
-       defer func() {
-               r := recover()
-               if r == nil {
-                       t.FailNow()
-               }
-               t.Log(r)
-       }()
-       CopyExact(make([]byte, 2), "abc")
-}
-
-func TestCopySrcString(t *testing.T) {
-       dest := make([]byte, 3)
-       CopyExact(dest, "lol")
-       if string(dest) != "lol" {
-               t.FailNow()
-       }
-       func() {
-               defer func() {
-                       r := recover()
-                       if r == nil {
-                               t.FailNow()
-                       }
-               }()
-               CopyExact(dest, "rofl")
-       }()
-       var arr [5]byte
-       CopyExact(&arr, interface{}("hello"))
-       if string(arr[:]) != "hello" {
-               t.FailNow()
-       }
-}
-
-func TestCopySrcNilInterface(t *testing.T) {
-       var arr [3]byte
-       defer func() {
-               r := recover().(string)
-               if !strings.Contains(r, "invalid source") {
-                       t.FailNow()
-               }
-       }()
-       CopyExact(&arr, nil)
-}
-
-func TestCopySrcPtr(t *testing.T) {
-       var bigDst [1024]byte
-       var bigSrc [1024]byte = [1024]byte{'h', 'i'}
-       CopyExact(&bigDst, &bigSrc)
-       if !bytes.Equal(bigDst[:], bigSrc[:]) {
-               t.FailNow()
-       }
-}
index ed65c80fefda60fff04cf91613db3e237db08a84..7d87f844e7558d7cd505c50ee4193b40b7d9e575 100644 (file)
@@ -9,11 +9,11 @@ import (
        "path/filepath"
        "strings"
 
+       "github.com/anacrolix/missinggo"
        "github.com/go-fsnotify/fsnotify"
 
        "github.com/anacrolix/torrent"
        "github.com/anacrolix/torrent/metainfo"
-       "github.com/anacrolix/torrent/util"
 )
 
 type Change uint
@@ -70,7 +70,7 @@ func torrentFileInfoHash(fileName string) (ih torrent.InfoHash, ok bool) {
        if mi == nil {
                return
        }
-       util.CopyExact(ih[:], mi.Info.Hash)
+       missinggo.CopyExact(ih[:], mi.Info.Hash)
        ok = true
        return
 }
@@ -108,7 +108,7 @@ func scanDir(dirName string) (ee map[torrent.InfoHash]entity) {
                        e := entity{
                                TorrentFilePath: fullName,
                        }
-                       util.CopyExact(&e.InfoHash, ih)
+                       missinggo.CopyExact(&e.InfoHash, ih)
                        addEntity(e)
                case ".magnet":
                        uris, err := magnetFileURIs(fullName)