]> Sergey Matveev's repositories - btrtrc.git/commitdiff
fix: can not download invalid utf8 name with BestName and BestPath (#915)
authorEvrins Hu <evrins.hu@gmail.com>
Mon, 18 Mar 2024 05:15:40 +0000 (13:15 +0800)
committerGitHub <noreply@github.com>
Mon, 18 Mar 2024 05:15:40 +0000 (16:15 +1100)
13 files changed:
cmd/magnet-metainfo/main.go
cmd/torrent/metainfo.go
cmd/torrent/serve.go
fs/torrentfs.go
fs/torrentfs_test.go
internal/cmd/issue-908/main.go
internal/testutil/spec.go
metainfo/info.go
metainfo/metainfo_test.go
spec.go
storage/file.go
storage/mmap.go
webseed/request.go

index 536f7abff308df7cab39fcbe70418bb11333c798..0e21d94d9521e166677c406b18566aa878e5f7bc 100644 (file)
@@ -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)
                        }
index 929cb45dc9cd00df89aff20514d07bcc36f49fe8..dd9d399c49ec83214d6d88783094ab8d14d6dd05 100644 (file)
@@ -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{}{
index d37dafafaac83fbd121206558e018eda1f983597..b257f73af7b20d66e677661b39aa362ba8705f51 100644 (file)
@@ -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),
@@ -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,
index 1b7b144e78fb961b5b374c9a1461ca9e6559cda4..f011d19ba8f5139715c89dadcaaabb4a1986f858 100644 (file)
@@ -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
@@ -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
@@ -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
index 097f1bb2377449d1c4300e4d8496a1f90806ad11..1bf9004e61ccfdb1cc8132d5d70657d0a2827c96 100644 (file)
@@ -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)
        }()
 
index 96da2e8ebf2a0c7a3a5b9cdd17416a4e7a0ea3f4..1bd59724c7072cb40ccea80b148c8c1f123293d5 100644 (file)
@@ -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),
index 63e4a74c633f646bdb1efa7c1607684cf0b807d9..ad0e4074def5b72c06f825f26969a13b5fb7762f 100644 (file)
@@ -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
index 5d6300ec51ec45eb9ed8d9724527fb87f4078ca2..3f14b08b5b97ee697a0056daa9caaa6333595b30 100644 (file)
@@ -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)
index 9f0f9f5f73f3aafdf9c7941eb9eb2aacadad0797..4d7c2b343b6812a5bda49a907788a23074a16b1c 100644 (file)
@@ -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)
                }
diff --git a/spec.go b/spec.go
index f54b325b5cee9708da43656aade5a21ffb5094dc..d0535a99e191801bae31d008dd269d8bda93f587 100644 (file)
--- a/spec.go
+++ b/spec.go
@@ -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))
index 231825fdfeed38f9dfeb1229542c8fde31942bde..d15e579d7e7d92aceb901165180313b46cddeb95 100644 (file)
@@ -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 {
index 1851c3238d2618cdc1c43789dff6189013ae2908..f7536591fabb9db61df691b09556ff6f59f6b5b4 100644 (file)
@@ -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
                }
index a8aefab3b3018a0d2ee60709bdc7d564801524ba..63b21665252b30fbfc31ae7402121e91526318b6 100644 (file)
@@ -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 {