parent
c4b1cd35bb
commit
d8ca4ac92a
@ -3,13 +3,13 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/anacrolix/tagflag"
|
||||||
"github.com/bradfitz/iter"
|
"github.com/bradfitz/iter"
|
||||||
"github.com/edsrzf/mmap-go"
|
"github.com/edsrzf/mmap-go"
|
||||||
|
|
||||||
@ -17,35 +17,56 @@ import (
|
|||||||
"github.com/anacrolix/torrent/mmap_span"
|
"github.com/anacrolix/torrent/mmap_span"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
func mmapFile(name string) (mm mmap.MMap, err error) {
|
||||||
torrentPath = flag.String("torrent", "/path/to/the.torrent", "path of the torrent file")
|
f, err := os.Open(name)
|
||||||
dataPath = flag.String("path", "/torrent/data", "path of the torrent data")
|
if err != nil {
|
||||||
)
|
return
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
fi, err := f.Stat()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if fi.Size() == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return mmap.MapRegion(f, -1, mmap.RDONLY, mmap.COPY, 0)
|
||||||
|
}
|
||||||
|
|
||||||
func fileToMmap(filename string, length int64) mmap.MMap {
|
func verifyTorrent(info *metainfo.Info, root string) error {
|
||||||
osFile, err := os.Open(filename)
|
span := new(mmap_span.MMapSpan)
|
||||||
if os.IsNotExist(err) {
|
for _, file := range info.UpvertedFiles() {
|
||||||
|
filename := filepath.Join(append([]string{root, info.Name}, file.Path...)...)
|
||||||
|
mm, err := mmapFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if int64(len(mm)) != file.Length {
|
||||||
|
return fmt.Errorf("file %q has wrong length", filename)
|
||||||
|
}
|
||||||
|
span.Append(mm)
|
||||||
|
}
|
||||||
|
for i := range iter.N(info.NumPieces()) {
|
||||||
|
p := info.Piece(i)
|
||||||
|
hash := sha1.New()
|
||||||
|
_, err := io.Copy(hash, io.NewSectionReader(span, p.Offset(), p.Length()))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
fmt.Printf("%d: %x: %v\n", i, p.Hash(), bytes.Equal(hash.Sum(nil), p.Hash().Bytes()))
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
goMMap, err := mmap.MapRegion(osFile, int(length), mmap.RDONLY, mmap.COPY, 0)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
if int64(len(goMMap)) != length {
|
|
||||||
log.Printf("file mmap has wrong size: %#v", filename)
|
|
||||||
}
|
|
||||||
osFile.Close()
|
|
||||||
|
|
||||||
return goMMap
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.SetFlags(log.Flags() | log.Lshortfile)
|
log.SetFlags(log.Flags() | log.Lshortfile)
|
||||||
flag.Parse()
|
var flags = struct {
|
||||||
metaInfo, err := metainfo.LoadFromFile(*torrentPath)
|
DataDir string
|
||||||
|
tagflag.StartPos
|
||||||
|
TorrentFile string
|
||||||
|
}{}
|
||||||
|
tagflag.Parse(&flags)
|
||||||
|
metaInfo, err := metainfo.LoadFromFile(flags.TorrentFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -53,27 +74,8 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error unmarshalling info: %s", err)
|
log.Fatalf("error unmarshalling info: %s", err)
|
||||||
}
|
}
|
||||||
mMapSpan := &mmap_span.MMapSpan{}
|
err = verifyTorrent(&info, flags.DataDir)
|
||||||
if len(info.Files) > 0 {
|
|
||||||
for _, file := range info.Files {
|
|
||||||
filename := filepath.Join(append([]string{*dataPath, info.Name}, file.Path...)...)
|
|
||||||
goMMap := fileToMmap(filename, file.Length)
|
|
||||||
mMapSpan.Append(goMMap)
|
|
||||||
}
|
|
||||||
log.Println(len(info.Files))
|
|
||||||
} else {
|
|
||||||
goMMap := fileToMmap(*dataPath, info.Length)
|
|
||||||
mMapSpan.Append(goMMap)
|
|
||||||
}
|
|
||||||
log.Println(mMapSpan.Size())
|
|
||||||
log.Println(len(info.Pieces))
|
|
||||||
for i := range iter.N(info.NumPieces()) {
|
|
||||||
p := info.Piece(i)
|
|
||||||
hash := sha1.New()
|
|
||||||
_, err := io.Copy(hash, io.NewSectionReader(mMapSpan, p.Offset(), p.Length()))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatalf("torrent failed verification: %s", err)
|
||||||
}
|
|
||||||
fmt.Printf("%d: %x: %v\n", i, p.Hash(), bytes.Equal(hash.Sum(nil), p.Hash().Bytes()))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user