metainfo/metainfo.go | 5 +++++ metainfo/metainfo_test.go | 28 ++++++++++++++++++++++++++++ diff --git a/metainfo/metainfo.go b/metainfo/metainfo.go index 8ecd7c5d7123ab93396b84e725aabe941424b21a..8d9f6e3093c0dc94db9feaf15354207a7e197016 100644 --- a/metainfo/metainfo.go +++ b/metainfo/metainfo.go @@ -11,6 +11,8 @@ "path/filepath" "strings" "time" + "github.com/anacrolix/missinggo" + "github.com/anacrolix/torrent/bencode" ) @@ -83,6 +85,9 @@ }) 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)))) }) diff --git a/metainfo/metainfo_test.go b/metainfo/metainfo_test.go index b2845fa9d87f6ad11ef8a7d3a7ce66cf1806ef71..a119f31381eab3e0733401b88f15ddb8d700345f 100644 --- a/metainfo/metainfo_test.go +++ b/metainfo/metainfo_test.go @@ -3,7 +3,9 @@ import ( "io" "io/ioutil" + "os" "path" + "path/filepath" "testing" "github.com/anacrolix/missinggo" @@ -68,3 +70,29 @@ assert.NoError(t, err) 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) +}