This commit is contained in:
Matt Joiner 2021-11-08 14:47:01 +11:00
parent f86af21cd2
commit c6ee03f449
47 changed files with 104 additions and 124 deletions

View File

@ -468,7 +468,6 @@ func (d *Decoder) readOneValue() bool {
}
return true
}
func (d *Decoder) parseUnmarshaler(v reflect.Value) bool {

View File

@ -23,8 +23,10 @@ var random_decode_tests = []random_decode_test{
{"5:hello", "hello"},
{"29:unicode test проверка", "unicode test проверка"},
{"d1:ai5e1:b5:helloe", map[string]interface{}{"a": int64(5), "b": "hello"}},
{"li5ei10ei15ei20e7:bencodee",
[]interface{}{int64(5), int64(10), int64(15), int64(20), "bencode"}},
{
"li5ei10ei15ei20e7:bencodee",
[]interface{}{int64(5), int64(10), int64(15), int64(20), "bencode"},
},
{"ldedee", []interface{}{map[string]interface{}{}, map[string]interface{}{}}},
{"le", []interface{}{}},
{"i604919719469385652980544193299329427705624352086e", func() *big.Int {
@ -135,7 +137,6 @@ func TestUnmarshalerBencode(t *testing.T) {
assert_equal(t, ss[0].x, "5:hello")
assert_equal(t, ss[1].x, "5:fruit")
assert_equal(t, ss[2].x, "3:way")
}
func TestIgnoreUnmarshalTypeError(t *testing.T) {

View File

@ -101,7 +101,6 @@ func (e *Encoder) reflectMarshaler(v reflect.Value) bool {
var bigIntType = reflect.TypeOf((*big.Int)(nil)).Elem()
func (e *Encoder) reflectValue(v reflect.Value) {
if e.reflectMarshaler(v) {
return
}

View File

@ -477,7 +477,6 @@ func (cl *Client) rejectAccepted(conn net.Conn) error {
}
if cl.config.DisableIPv4 && len(rip) == net.IPv4len {
return errors.New("ipv4 disabled")
}
if cl.config.DisableIPv6 && len(rip) == net.IPv6len && rip.To4() == nil {
return errors.New("ipv6 disabled")

View File

@ -648,7 +648,7 @@ func TestSetMaxEstablishedConn(t *testing.T) {
// Creates a file containing its own name as data. Make a metainfo from that, adds it to the given
// client, and returns a magnet link.
func makeMagnet(t *testing.T, cl *Client, dir string, name string) string {
os.MkdirAll(dir, 0770)
os.MkdirAll(dir, 0o770)
file, err := os.Create(filepath.Join(dir, name))
require.NoError(t, err)
file.Write([]byte(name))
@ -688,7 +688,7 @@ func testSeederLeecherPair(t *testing.T, seeder func(*ClientConfig), leecher fun
cfg := TestingConfig(t)
cfg.Seed = true
cfg.DataDir = filepath.Join(cfg.DataDir, "server")
os.Mkdir(cfg.DataDir, 0755)
os.Mkdir(cfg.DataDir, 0o755)
seeder(cfg)
server, err := NewClient(cfg)
require.NoError(t, err)

View File

@ -10,13 +10,11 @@ import (
"github.com/anacrolix/torrent/metainfo"
)
var (
builtinAnnounceList = [][]string{
var builtinAnnounceList = [][]string{
{"http://p4p.arenabg.com:1337/announce"},
{"udp://tracker.opentrackr.org:1337/announce"},
{"udp://tracker.openbittorrent.com:6969/announce"},
}
)
func main() {
log.SetFlags(log.Flags() | log.Lshortfile)

View File

@ -78,7 +78,7 @@ func dstFileName(picked string) string {
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
var rootGroup = struct {
rootGroup := struct {
Client *torrent.ClientConfig `group:"Client Options"`
TestPeers []string `long:"test-peer" description:"address of peer to inject to every torrent"`
Pick string `long:"pick" description:"filename to pick"`

View File

@ -64,7 +64,7 @@ func verifyTorrent(info *metainfo.Info, root string) error {
func main() {
log.SetFlags(log.Flags() | log.Lshortfile)
var flags = struct {
flags := struct {
DataDir string
tagflag.StartPos
TorrentFile string

View File

@ -24,8 +24,7 @@ import (
"github.com/anacrolix/torrent/util/dirwatch"
)
var (
args = struct {
var args = struct {
MetainfoDir string `help:"torrent files in this location describe the contents of the mounted filesystem"`
DownloadDir string `help:"location to save torrent data"`
MountDir string `help:"location the torrent contents are made available"`
@ -45,7 +44,6 @@ var (
ReadaheadBytes: 10 << 20,
ListenAddr: &net.TCPAddr{},
}
)
func exitSignalHandlers(fs *torrentfs.TorrentFS) {
c := make(chan os.Signal, 1)

View File

@ -40,7 +40,6 @@ func (me testFileBytesLeft) Run(t *testing.T) {
}
func TestFileBytesLeft(t *testing.T) {
testFileBytesLeft{
usualPieceSize: 3,
firstPieceIndex: 1,

View File

@ -14,9 +14,7 @@ type fileNode struct {
f *torrent.File
}
var (
_ fusefs.NodeOpener = fileNode{}
)
var _ fusefs.NodeOpener = fileNode{}
func (fn fileNode) Attr(ctx context.Context, attr *fuse.Attr) error {
attr.Size = uint64(fn.f.Length())

View File

@ -15,12 +15,10 @@ import (
)
const (
defaultMode = 0555
defaultMode = 0o555
)
var (
torrentfsReadRequests = expvar.NewInt("torrentfsReadRequests")
)
var torrentfsReadRequests = expvar.NewInt("torrentfsReadRequests")
type TorrentFS struct {
Client *torrent.Client
@ -55,9 +53,7 @@ type dirNode struct {
node
}
var (
_ fusefs.HandleReadDirAller = dirNode{}
)
var _ fusefs.HandleReadDirAller = dirNode{}
func isSubPath(parent, child string) bool {
if parent == "" {

View File

@ -68,9 +68,9 @@ func newGreetingLayout() (tl testLayout, err error) {
return
}
tl.Completed = filepath.Join(tl.BaseDir, "completed")
os.Mkdir(tl.Completed, 0777)
os.Mkdir(tl.Completed, 0o777)
tl.MountDir = filepath.Join(tl.BaseDir, "mnt")
os.Mkdir(tl.MountDir, 0777)
os.Mkdir(tl.MountDir, 0o777)
testutil.CreateDummyTorrentData(tl.Completed)
tl.Metainfo = testutil.GreetingMetaInfo()
return

View File

@ -12,7 +12,7 @@ import (
)
func main() {
var flags = struct {
flags := struct {
tagflag.StartPos
Ips []net.IP
}{}

View File

@ -59,6 +59,7 @@ var (
func (h *Hash) UnmarshalText(b []byte) error {
return h.FromHexString(string(b))
}
func (h Hash) MarshalText() (text []byte, err error) {
return []byte(h.HexString()), nil
}

View File

@ -67,7 +67,6 @@ func TestParseMagnetURI(t *testing.T) {
if err == nil {
t.Errorf("Failed to detect broken Magnet URI: %v", uri)
}
}
func TestMagnetize(t *testing.T) {

View File

@ -10,9 +10,7 @@ import (
type Node string
var (
_ bencode.Unmarshaler = (*Node)(nil)
)
var _ bencode.Unmarshaler = (*Node)(nil)
func (n *Node) UnmarshalBencode(b []byte) (err error) {
var iface interface{}

View File

@ -6,9 +6,7 @@ import (
type UrlList []string
var (
_ bencode.Unmarshaler = (*UrlList)(nil)
)
var _ bencode.Unmarshaler = (*UrlList)(nil)
func (me *UrlList) UnmarshalBencode(b []byte) error {
if len(b) == 0 {

View File

@ -20,7 +20,7 @@ func main() {
}
func mainErr() error {
var args = struct {
args := struct {
CryptoMethod mse.CryptoMethod
Dial *struct {
Network string `arg:"positional"`

View File

@ -481,7 +481,6 @@ func (cn *Peer) totalExpectingTime() (ret time.Duration) {
ret += time.Since(cn.lastStartedExpectingToReceiveChunks)
}
return
}
func (cn *PeerConn) onPeerSentCancel(r Request) {

View File

@ -155,7 +155,7 @@ func TestConnPexPeerFlags(t *testing.T) {
tcpAddr = &net.TCPAddr{IP: net.IPv6loopback, Port: 4848}
udpAddr = &net.UDPAddr{IP: net.IPv6loopback, Port: 4848}
)
var testcases = []struct {
testcases := []struct {
conn *PeerConn
f pp.PexPeerFlags
}{
@ -181,7 +181,7 @@ func TestConnPexEvent(t *testing.T) {
dialTcpAddr = &net.TCPAddr{IP: net.IPv6loopback, Port: 4747}
dialUdpAddr = &net.UDPAddr{IP: net.IPv6loopback, Port: 4747}
)
var testcases = []struct {
testcases := []struct {
t pexEventType
c *PeerConn
e pexEvent

View File

@ -247,7 +247,8 @@ func TestDontStealUnnecessarily(t *testing.T) {
Request: true,
NumPendingChunks: 9,
IterPendingChunks: chunkIterRange(9),
}},
},
},
Peers: []Peer{
firstStealer,
stealee,

View File

@ -116,8 +116,10 @@ func (p *peerId) GobDecode(b []byte) error {
return nil
}
type RequestIndex = request_strategy.RequestIndex
type chunkIndexType = request_strategy.ChunkIndex
type (
RequestIndex = request_strategy.RequestIndex
chunkIndexType = request_strategy.ChunkIndex
)
type peerRequests struct {
requestIndexes []RequestIndex

View File

@ -28,7 +28,6 @@ func TestLogExampleRequestMapOrdering(t *testing.T) {
for k := range makeTypicalRequests() {
t.Log(k)
}
}
func TestRequestMapOrderingPersistent(t *testing.T) {

View File

@ -67,7 +67,8 @@ func testLocater(t *testing.T, newLocater newLocater) {
{0, 1554},
{0, 1618},
{0, 1546},
{0, 8500}})
{0, 8500},
})
assertLocate(t, newLocater,
[]Length{1652, 1514, 1554, 1618, 1546, 129241752, 1537, 1536, 1551}, // 128737588
Extent{129236992, 16384},
@ -76,7 +77,8 @@ func testLocater(t *testing.T, newLocater newLocater) {
{129229108, 12644},
{0, 1537},
{0, 1536},
{0, 667}})
{0, 667},
})
}
func TestScan(t *testing.T) {

View File

@ -19,9 +19,7 @@ const (
boltDbIncompleteValue = "i"
)
var (
completionBucketKey = []byte("completion")
)
var completionBucketKey = []byte("completion")
type boltPieceCompletion struct {
db *bbolt.DB
@ -30,9 +28,9 @@ type boltPieceCompletion struct {
var _ PieceCompletion = (*boltPieceCompletion)(nil)
func NewBoltPieceCompletion(dir string) (ret PieceCompletion, err error) {
os.MkdirAll(dir, 0750)
os.MkdirAll(dir, 0o750)
p := filepath.Join(dir, ".torrent.bolt.db")
db, err := bbolt.Open(p, 0660, &bbolt.Options{
db, err := bbolt.Open(p, 0o660, &bbolt.Options{
Timeout: time.Second,
})
if err != nil {

View File

@ -51,6 +51,7 @@ func (me *boltPiece) MarkComplete() error {
func (me *boltPiece) MarkNotComplete() error {
return me.pc().Set(me.pk(), false)
}
func (me *boltPiece) ReadAt(b []byte, off int64) (n int, err error) {
err = me.db.View(func(tx *bbolt.Tx) error {
db := tx.Bucket(dataBucketKey)

View File

@ -30,7 +30,7 @@ type boltTorrent struct {
}
func NewBoltDB(filePath string) ClientImplCloser {
db, err := bbolt.Open(filepath.Join(filePath, "bolt.db"), 0600, &bbolt.Options{
db, err := bbolt.Open(filepath.Join(filePath, "bolt.db"), 0o600, &bbolt.Options{
Timeout: time.Second,
})
expect.Nil(err)

View File

@ -126,7 +126,7 @@ func (fs *fileTorrentImpl) Close() error {
// writes will ever occur to them (no torrent data is associated with a zero-length file). The
// caller should make sure the file name provided is safe/sanitized.
func CreateNativeZeroLengthFile(name string) error {
os.MkdirAll(filepath.Dir(name), 0777)
os.MkdirAll(filepath.Dir(name), 0o777)
var f io.Closer
f, err := os.Create(name)
if err != nil {
@ -188,9 +188,9 @@ func (fst fileTorrentImplIO) WriteAt(p []byte, off int64) (n int, err error) {
// log.Printf("write at %v: %v bytes", off, len(p))
fst.fts.segmentLocater.Locate(segments.Extent{off, int64(len(p))}, func(i int, e segments.Extent) bool {
name := fst.fts.files[i].path
os.MkdirAll(filepath.Dir(name), 0777)
os.MkdirAll(filepath.Dir(name), 0o777)
var f *os.File
f, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE, 0666)
f, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE, 0o666)
if err != nil {
return false
}

View File

@ -132,13 +132,13 @@ func mMapTorrent(md *metainfo.Info, location string) (mms *mmap_span.MMapSpan, e
func mmapFile(name string, size int64) (ret mmap.MMap, err error) {
dir := filepath.Dir(name)
err = os.MkdirAll(dir, 0750)
err = os.MkdirAll(dir, 0o750)
if err != nil {
err = fmt.Errorf("making directory %q: %s", dir, err)
return
}
var file *os.File
file, err = os.OpenFile(name, os.O_CREATE|os.O_RDWR, 0666)
file, err = os.OpenFile(name, os.O_CREATE|os.O_RDWR, 0o666)
if err != nil {
return
}

View File

@ -24,8 +24,10 @@ var safeFilePathTests = []struct {
{input: []string{"a", filepath.FromSlash(`b/..`)}, expectErr: false},
{input: []string{"a", filepath.FromSlash(`b/../../..`)}, expectErr: true},
{input: []string{"a", filepath.FromSlash(`b/../.././..`)}, expectErr: true},
{input: []string{
filepath.FromSlash(`NewSuperHeroMovie-2019-English-720p.avi /../../../../../Roaming/Microsoft/Windows/Start Menu/Programs/Startup/test3.exe`)},
{
input: []string{
filepath.FromSlash(`NewSuperHeroMovie-2019-English-720p.avi /../../../../../Roaming/Microsoft/Windows/Start Menu/Programs/Startup/test3.exe`),
},
expectErr: true,
},
}

1
t.go
View File

@ -232,7 +232,6 @@ func (t *Torrent) initFiles() {
})
offset += fi.Length
}
}
// Returns handles to the files in the torrent. This requires that the Info is

View File

@ -209,7 +209,6 @@ func (t *Torrent) KnownSwarm() (ks []PeerInfo) {
// Add active peers to the list
for conn := range t.conns {
ks = append(ks, PeerInfo{
Id: conn.PeerID,
Addr: conn.RemoteAddr,
@ -1506,7 +1505,6 @@ func (t *Torrent) startWebsocketAnnouncer(u url.URL) torrentTrackerAnnouncer {
}
}()
return wst
}
func (t *Torrent) startScrapingTracker(_url string) {

View File

@ -58,7 +58,8 @@ func TestSetAnnounceInfohashParamWithSpaces(t *testing.T) {
someUrl := &url.URL{}
ihBytes := [20]uint8{
0x2b, 0x76, 0xa, 0xa1, 0x78, 0x93, 0x20, 0x30, 0xc8, 0x47,
0xdc, 0xdf, 0x8e, 0xae, 0xbf, 0x56, 0xa, 0x1b, 0xd1, 0x6c}
0xdc, 0xdf, 0x8e, 0xae, 0xbf, 0x56, 0xa, 0x1b, 0xd1, 0x6c,
}
setAnnounceParams(
someUrl,
&udp.AnnounceRequest{

View File

@ -28,9 +28,7 @@ type Peer = trHttp.Peer
type AnnounceEvent = udp.AnnounceEvent
var (
ErrBadScheme = errors.New("unknown scheme")
)
var ErrBadScheme = errors.New("unknown scheme")
type Announce struct {
TrackerUrl string

View File

@ -112,7 +112,6 @@ func (me *trackerScraper) trackerUrl(ip net.IP) string {
// Return how long to wait before trying again. For most errors, we return 5
// minutes, a relatively quick turn around for DNS changes.
func (me *trackerScraper) announce(ctx context.Context, event tracker.AnnounceEvent) (ret trackerAnnounceResult) {
defer func() {
ret.Completed = time.Now()
}()
@ -190,7 +189,6 @@ func (me *trackerScraper) canIgnoreInterval(notify *<-chan struct{}) bool {
}
func (me *trackerScraper) Run() {
defer me.announceStopped()
ctx, cancel := context.WithCancel(context.Background())

View File

@ -150,7 +150,6 @@ func (tc *TrackerClient) Close() error {
}
func (tc *TrackerClient) announceOffers() {
// tc.Announce grabs a lock on tc.outboundOffers. It also handles the case where outboundOffers
// is nil. Take ownership of outboundOffers here.
tc.mu.Lock()