From ae2e4bf7e7af32a432a1c500f95ed32377276890 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 27 Jun 2022 19:25:38 +1000 Subject: [PATCH] Use metainfo.ChoosePieceLength from more locations --- cmd/torrent/serve.go | 2 +- metainfo/info.go | 3 +++ metainfo/metainfo.go | 2 -- {cmd/torrent => metainfo}/piece-length.go | 18 +++++++++--------- 4 files changed, 13 insertions(+), 12 deletions(-) rename {cmd/torrent => metainfo}/piece-length.go (83%) diff --git a/cmd/torrent/serve.go b/cmd/torrent/serve.go index 7915ddd9..b32f245b 100644 --- a/cmd/torrent/serve.go +++ b/cmd/torrent/serve.go @@ -31,7 +31,7 @@ func serve(ctx args.SubCmdCtx) error { if err != nil { return fmt.Errorf("calculating total length of %q: %v", filePath, err) } - pieceLength := choosePieceLength(totalLength) + pieceLength := metainfo.ChoosePieceLength(totalLength) info := metainfo.Info{ PieceLength: pieceLength, } diff --git a/metainfo/info.go b/metainfo/info.go index 1b794473..bab08af9 100644 --- a/metainfo/info.go +++ b/metainfo/info.go @@ -74,6 +74,9 @@ func (info *Info) BuildFromFilePath(root string) (err error) { slices.Sort(info.Files, func(l, r FileInfo) bool { return strings.Join(l.Path, "/") < strings.Join(r.Path, "/") }) + if info.PieceLength == 0 { + info.PieceLength = ChoosePieceLength(info.TotalLength()) + } err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) { return os.Open(filepath.Join(root, strings.Join(fi.Path, string(filepath.Separator)))) }) diff --git a/metainfo/metainfo.go b/metainfo/metainfo.go index 7ffa6d5b..cd787260 100644 --- a/metainfo/metainfo.go +++ b/metainfo/metainfo.go @@ -62,10 +62,8 @@ func (mi MetaInfo) Write(w io.Writer) error { // Set good default values in preparation for creating a new MetaInfo file. func (mi *MetaInfo) SetDefaults() { - mi.Comment = "" mi.CreatedBy = "github.com/anacrolix/torrent" mi.CreationDate = time.Now().Unix() - // mi.Info.PieceLength = 256 * 1024 } // Creates a Magnet from a MetaInfo. Optional infohash and parsed info can be provided. diff --git a/cmd/torrent/piece-length.go b/metainfo/piece-length.go similarity index 83% rename from cmd/torrent/piece-length.go rename to metainfo/piece-length.go index b6574fd3..9835dc5b 100644 --- a/cmd/torrent/piece-length.go +++ b/metainfo/piece-length.go @@ -28,24 +28,24 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -package main +package metainfo // For more context on why these numbers, see http://wiki.vuze.com/w/Torrent_Piece_Size -const MinimumPieceLength = 16 * 1024 -const TargetPieceCountLog2 = 10 -const TargetPieceCountMin = 1 << TargetPieceCountLog2 +const minimumPieceLength = 16 * 1024 +const targetPieceCountLog2 = 10 +const targetPieceCountMin = 1 << targetPieceCountLog2 -// Target piece count should be < TargetPieceCountMax -const TargetPieceCountMax = TargetPieceCountMin << 1 +// Target piece count should be < targetPieceCountMax +const targetPieceCountMax = targetPieceCountMin << 1 // Choose a good piecelength. -func choosePieceLength(totalLength int64) (pieceLength int64) { +func ChoosePieceLength(totalLength int64) (pieceLength int64) { // Must be a power of 2. // Must be a multiple of 16KB // Prefer to provide around 1024..2048 pieces. - pieceLength = MinimumPieceLength + pieceLength = minimumPieceLength pieces := totalLength / pieceLength - for pieces >= TargetPieceCountMax { + for pieces >= targetPieceCountMax { pieceLength <<= 1 pieces >>= 1 } -- 2.48.1