Use metainfo.ChoosePieceLength from more locations
This commit is contained in:
parent
12279621e4
commit
ae2e4bf7e7
@ -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,
|
||||
}
|
||||
|
@ -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))))
|
||||
})
|
||||
|
@ -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.
|
||||
|
@ -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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user