Skip to content

Commit

Permalink
fix: can not download invalid utf8 name with BestName and BestPath (#915
Browse files Browse the repository at this point in the history
)
  • Loading branch information
evrins authored Mar 18, 2024
1 parent 2a543b0 commit e6be81d
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/magnet-metainfo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {
<-t.GotInfo()
mi := t.Metainfo()
t.Drop()
f, err := os.Create(t.Info().Name + ".torrent")
f, err := os.Create(t.Info().BestName() + ".torrent")
if err != nil {
log.Fatalf("error creating torrent metainfo file: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/torrent/metainfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func pprintMetainfo(metainfo *metainfo.MetaInfo, flags pprintMetainfoFlags) erro
return fmt.Errorf("error unmarshalling info: %s", err)
}
if flags.JustName {
fmt.Printf("%s\n", info.Name)
fmt.Printf("%s\n", info.BestName())
return nil
}
d := map[string]interface{}{
Expand Down
4 changes: 2 additions & 2 deletions cmd/torrent/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func serve() (cmd bargle.Command) {
return fmt.Errorf("building info from path %q: %w", filePath, err)
}
for _, fi := range info.UpvertedFiles() {
log.Printf("added %q", fi.Path)
log.Printf("added %q", fi.BestPath())
}
mi := metainfo.MetaInfo{
InfoBytes: bencode.MustMarshal(info),
Expand All @@ -61,7 +61,7 @@ func serve() (cmd bargle.Command) {
Storage: storage.NewFileOpts(storage.NewFileClientOpts{
ClientBaseDir: filePath,
FilePathMaker: func(opts storage.FilePathMakerOpts) string {
return filepath.Join(opts.File.Path...)
return filepath.Join(opts.File.BestPath()...)
},
TorrentDirMaker: nil,
PieceCompletion: pc,
Expand Down
10 changes: 5 additions & 5 deletions fs/torrentfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ func isSubPath(parent, child string) bool {
func (dn dirNode) ReadDirAll(ctx context.Context) (des []fuse.Dirent, err error) {
names := map[string]bool{}
for _, fi := range dn.metadata.UpvertedFiles() {
filePathname := strings.Join(fi.Path, "/")
filePathname := strings.Join(fi.BestPath(), "/")
if !isSubPath(dn.path, filePathname) {
continue
}
var name string
if dn.path == "" {
name = fi.Path[0]
name = fi.BestPath()[0]
} else {
dirPathname := strings.Split(dn.path, "/")
name = fi.Path[len(dirPathname)]
name = fi.BestPath()[len(dirPathname)]
}
if names[name] {
continue
Expand All @@ -91,7 +91,7 @@ func (dn dirNode) ReadDirAll(ctx context.Context) (des []fuse.Dirent, err error)
de := fuse.Dirent{
Name: name,
}
if len(fi.Path) == len(dn.path)+1 {
if len(fi.BestPath()) == len(dn.path)+1 {
de.Type = fuse.DT_File
} else {
de.Type = fuse.DT_Dir
Expand Down Expand Up @@ -168,7 +168,7 @@ func (rn rootNode) ReadDirAll(ctx context.Context) (dirents []fuse.Dirent, err e
continue
}
dirents = append(dirents, fuse.Dirent{
Name: info.Name,
Name: info.BestName(),
Type: func() fuse.DirentType {
if !info.IsDir() {
return fuse.DT_File
Expand Down
2 changes: 1 addition & 1 deletion fs/torrentfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestUnmountWedged(t *testing.T) {
}()
go func() {
defer cancel()
_, err := ioutil.ReadFile(filepath.Join(layout.MountDir, tt.Info().Name))
_, err := ioutil.ReadFile(filepath.Join(layout.MountDir, tt.Info().BestName()))
require.Error(t, err)
}()

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/issue-908/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func main() {
log.Fatal(err)
}
for _, fi := range info.Files {
log.Printf("added %q", fi.Path)
log.Printf("added %q", fi.BestPath())
}
mi := &metainfo.MetaInfo{
InfoBytes: bencode.MustMarshal(info),
Expand Down
2 changes: 1 addition & 1 deletion internal/testutil/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (t *Torrent) Info(pieceLength int64) metainfo.Info {
}
}
err := info.GeneratePieces(func(fi metainfo.FileInfo) (io.ReadCloser, error) {
return io.NopCloser(strings.NewReader(t.GetFile(strings.Join(fi.Path, "/")).Data)), nil
return io.NopCloser(strings.NewReader(t.GetFile(strings.Join(fi.BestPath(), "/")).Data)), nil
})
expect.Nil(err)
return info
Expand Down
4 changes: 2 additions & 2 deletions metainfo/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ func (info *Info) BuildFromFilePath(root string) (err error) {
return
}
slices.Sort(info.Files, func(l, r FileInfo) bool {
return strings.Join(l.Path, "/") < strings.Join(r.Path, "/")
return strings.Join(l.BestPath(), "/") < strings.Join(r.BestPath(), "/")
})
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))))
return os.Open(filepath.Join(root, strings.Join(fi.BestPath(), string(filepath.Separator))))
})
if err != nil {
err = fmt.Errorf("error generating pieces: %s", err)
Expand Down
4 changes: 2 additions & 2 deletions metainfo/metainfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func testFile(t *testing.T, filename string) {
require.NoError(t, err)

if len(info.Files) == 1 {
t.Logf("Single file: %s (length: %d)\n", info.Name, info.Files[0].Length)
t.Logf("Single file: %s (length: %d)\n", info.BestName(), info.Files[0].Length)
} else {
t.Logf("Multiple files: %s\n", info.Name)
t.Logf("Multiple files: %s\n", info.BestName())
for _, f := range info.Files {
t.Logf(" - %s (length: %d)\n", path.Join(f.Path...), f.Length)
}
Expand Down
2 changes: 1 addition & 1 deletion spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TorrentSpecFromMetaInfoErr(mi *metainfo.MetaInfo) (*TorrentSpec, error) {
InfoHashV2: v2Infohash,
PieceLayers: mi.PieceLayers,
InfoBytes: mi.InfoBytes,
DisplayName: info.Name,
DisplayName: info.BestName(),
Webseeds: mi.UrlList,
DhtNodes: func() (ret []string) {
ret = make([]string, 0, len(mi.Nodes))
Expand Down
6 changes: 3 additions & 3 deletions storage/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func NewFileOpts(opts NewFileClientOpts) ClientImplCloser {
if opts.FilePathMaker == nil {
opts.FilePathMaker = func(opts FilePathMakerOpts) string {
var parts []string
if opts.Info.Name != metainfo.NoName {
parts = append(parts, opts.Info.Name)
if opts.Info.BestName() != metainfo.NoName {
parts = append(parts, opts.Info.BestName())
}
return filepath.Join(append(parts, opts.File.Path...)...)
return filepath.Join(append(parts, opts.File.BestPath()...)...)
}
}
if opts.PieceCompletion == nil {
Expand Down
2 changes: 1 addition & 1 deletion storage/mmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func mMapTorrent(md *metainfo.Info, location string) (mms *mmap_span.MMapSpan, e
}()
for _, miFile := range md.UpvertedFiles() {
var safeName string
safeName, err = ToSafeFilePath(append([]string{md.Name}, miFile.Path...)...)
safeName, err = ToSafeFilePath(append([]string{md.BestName()}, miFile.BestPath()...)...)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion webseed/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func newRequest(
if strings.HasSuffix(url_, "/") {
// BEP specifies that we append the file path. We need to escape each component of the path
// for things like spaces and '#'.
url_ += trailingPath(info.Name, fileInfo.Path, pathEscaper)
url_ += trailingPath(info.BestName(), fileInfo.BestPath(), pathEscaper)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url_, nil)
if err != nil {
Expand Down

0 comments on commit e6be81d

Please sign in to comment.