metainfo.Info.BuildFromFilePath: Ensure stable file ordering
Spotted by @axet, with precedent in Transmission.
This commit is contained in:
parent
838c35f3ea
commit
2490c96f2f
|
@ -11,6 +11,8 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/anacrolix/missinggo"
|
||||
|
||||
"github.com/anacrolix/torrent/bencode"
|
||||
)
|
||||
|
||||
|
@ -83,6 +85,9 @@ func (info *Info) BuildFromFilePath(root string) (err error) {
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
missinggo.SortSlice(info.Files, func(l, r FileInfo) bool {
|
||||
return strings.Join(l.Path, "/") < strings.Join(r.Path, "/")
|
||||
})
|
||||
err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
|
||||
return os.Open(filepath.Join(root, strings.Join(fi.Path, string(filepath.Separator))))
|
||||
})
|
||||
|
|
|
@ -3,7 +3,9 @@ package metainfo
|
|||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/anacrolix/missinggo"
|
||||
|
@ -68,3 +70,29 @@ func TestNumPieces(t *testing.T) {
|
|||
assert.EqualValues(t, _case.NumPieces, info.NumPieces())
|
||||
}
|
||||
}
|
||||
|
||||
func touchFile(path string) (err error) {
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = f.Close()
|
||||
return
|
||||
}
|
||||
|
||||
func TestBuildFromFilePathOrder(t *testing.T) {
|
||||
td, err := ioutil.TempDir("", "anacrolix")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(td)
|
||||
require.NoError(t, touchFile(filepath.Join(td, "b")))
|
||||
require.NoError(t, touchFile(filepath.Join(td, "a")))
|
||||
info := Info{
|
||||
PieceLength: 1,
|
||||
}
|
||||
require.NoError(t, info.BuildFromFilePath(td))
|
||||
assert.EqualValues(t, []FileInfo{{
|
||||
Path: []string{"a"},
|
||||
}, {
|
||||
Path: []string{"b"},
|
||||
}}, info.Files)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue