]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Use metainfo.ChoosePieceLength from more locations
authorMatt Joiner <anacrolix@gmail.com>
Mon, 27 Jun 2022 09:25:38 +0000 (19:25 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Sun, 3 Jul 2022 11:47:16 +0000 (21:47 +1000)
cmd/torrent/serve.go
metainfo/info.go
metainfo/metainfo.go
metainfo/piece-length.go [moved from cmd/torrent/piece-length.go with 83% similarity]

index 7915ddd91a33779f963ea355a3d17336a867bf7a..b32f245b089dc83813e7d5de8c56ba7e235eb0c9 100644 (file)
@@ -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,
                }
index 1b7944738081d823f1e4836715b898b9f2fa486d..bab08af945a6045582732339b9ebc27459a07c6b 100644 (file)
@@ -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))))
        })
index 7ffa6d5b8f9b6e7637dcd7a4115d562f2c0a318e..cd787260c150e972b8987713b65904a7f30c766b 100644 (file)
@@ -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.
similarity index 83%
rename from cmd/torrent/piece-length.go
rename to metainfo/piece-length.go
index b6574fd36f469327f7243d0675368dba8bd68f8e..9835dc5b2fbeae4c287abe5803fccd5b932ba8f3 100644 (file)
 // (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
        }