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 {
|
if err != nil {
|
||||||
return fmt.Errorf("calculating total length of %q: %v", filePath, err)
|
return fmt.Errorf("calculating total length of %q: %v", filePath, err)
|
||||||
}
|
}
|
||||||
pieceLength := choosePieceLength(totalLength)
|
pieceLength := metainfo.ChoosePieceLength(totalLength)
|
||||||
info := metainfo.Info{
|
info := metainfo.Info{
|
||||||
PieceLength: pieceLength,
|
PieceLength: pieceLength,
|
||||||
}
|
}
|
||||||
|
@ -74,6 +74,9 @@ func (info *Info) BuildFromFilePath(root string) (err error) {
|
|||||||
slices.Sort(info.Files, func(l, r FileInfo) bool {
|
slices.Sort(info.Files, func(l, r FileInfo) bool {
|
||||||
return strings.Join(l.Path, "/") < strings.Join(r.Path, "/")
|
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) {
|
err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
|
||||||
return os.Open(filepath.Join(root, strings.Join(fi.Path, string(filepath.Separator))))
|
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.
|
// Set good default values in preparation for creating a new MetaInfo file.
|
||||||
func (mi *MetaInfo) SetDefaults() {
|
func (mi *MetaInfo) SetDefaults() {
|
||||||
mi.Comment = ""
|
|
||||||
mi.CreatedBy = "github.com/anacrolix/torrent"
|
mi.CreatedBy = "github.com/anacrolix/torrent"
|
||||||
mi.CreationDate = time.Now().Unix()
|
mi.CreationDate = time.Now().Unix()
|
||||||
// mi.Info.PieceLength = 256 * 1024
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a Magnet from a MetaInfo. Optional infohash and parsed info can be provided.
|
// 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
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
// 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
|
// For more context on why these numbers, see http://wiki.vuze.com/w/Torrent_Piece_Size
|
||||||
const MinimumPieceLength = 16 * 1024
|
const minimumPieceLength = 16 * 1024
|
||||||
const TargetPieceCountLog2 = 10
|
const targetPieceCountLog2 = 10
|
||||||
const TargetPieceCountMin = 1 << TargetPieceCountLog2
|
const targetPieceCountMin = 1 << targetPieceCountLog2
|
||||||
|
|
||||||
// Target piece count should be < TargetPieceCountMax
|
// Target piece count should be < targetPieceCountMax
|
||||||
const TargetPieceCountMax = TargetPieceCountMin << 1
|
const targetPieceCountMax = targetPieceCountMin << 1
|
||||||
|
|
||||||
// Choose a good piecelength.
|
// 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 power of 2.
|
||||||
// Must be a multiple of 16KB
|
// Must be a multiple of 16KB
|
||||||
// Prefer to provide around 1024..2048 pieces.
|
// Prefer to provide around 1024..2048 pieces.
|
||||||
pieceLength = MinimumPieceLength
|
pieceLength = minimumPieceLength
|
||||||
pieces := totalLength / pieceLength
|
pieces := totalLength / pieceLength
|
||||||
for pieces >= TargetPieceCountMax {
|
for pieces >= targetPieceCountMax {
|
||||||
pieceLength <<= 1
|
pieceLength <<= 1
|
||||||
pieces >>= 1
|
pieces >>= 1
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user