cmd/torrent/piece-length.go => metainfo/piece-length.go | 18 +++++++++--------- cmd/torrent/serve.go | 2 +- metainfo/info.go | 3 +++ metainfo/metainfo.go | 2 -- diff --git a/cmd/torrent/piece-length.go b/metainfo/piece-length.go rename from cmd/torrent/piece-length.go rename to metainfo/piece-length.go index b6574fd36f469327f7243d0675368dba8bd68f8e..9835dc5b2fbeae4c287abe5803fccd5b932ba8f3 100644 --- a/cmd/torrent/piece-length.go +++ b/metainfo/piece-length.go @@ -28,24 +28,24 @@ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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 } diff --git a/cmd/torrent/serve.go b/cmd/torrent/serve.go index 7915ddd91a33779f963ea355a3d17336a867bf7a..b32f245b089dc83813e7d5de8c56ba7e235eb0c9 100644 --- a/cmd/torrent/serve.go +++ b/cmd/torrent/serve.go @@ -31,7 +31,7 @@ totalLength, err := totalLength(filePath) 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 1b7944738081d823f1e4836715b898b9f2fa486d..bab08af945a6045582732339b9ebc27459a07c6b 100644 --- a/metainfo/info.go +++ b/metainfo/info.go @@ -74,6 +74,9 @@ } 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 7ffa6d5b8f9b6e7637dcd7a4115d562f2c0a318e..cd787260c150e972b8987713b65904a7f30c766b 100644 --- a/metainfo/metainfo.go +++ b/metainfo/metainfo.go @@ -62,10 +62,8 @@ } // 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.