]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Fix error handling for bad torrent adds with safe file handling v1.18.0
authorMatt Joiner <anacrolix@gmail.com>
Thu, 15 Oct 2020 05:03:43 +0000 (16:03 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 15 Oct 2020 05:03:43 +0000 (16:03 +1100)
client.go
cmd/torrent/main.go
storage/file.go
storage/safe-path.go

index 25f7a860dd8aaf957f6f658edcf153a61ce7ffe3..111812c3a07bbefd1141f1bd7126a6335a839383 100644 (file)
--- a/client.go
+++ b/client.go
@@ -1150,6 +1150,9 @@ func (cl *Client) AddTorrentInfoHashWithStorage(infoHash metainfo.Hash, specStor
 func (cl *Client) AddTorrentSpec(spec *TorrentSpec) (t *Torrent, new bool, err error) {
        t, new = cl.AddTorrentInfoHashWithStorage(spec.InfoHash, spec.Storage)
        err = t.MergeSpec(spec)
+       if err != nil && new {
+               t.Drop()
+       }
        return
 }
 
index 8378376617b98b02297e0f2d7918beda41195c1a..c684b092e2b7aefb8f5a978312c9f04c6c644f24 100644 (file)
@@ -4,6 +4,7 @@ package main
 import (
        "expvar"
        "fmt"
+       stdLog "log"
        "net"
        "net/http"
        "os"
@@ -210,6 +211,7 @@ func main() {
 }
 
 func mainErr() error {
+       stdLog.SetFlags(stdLog.Flags() | stdLog.Lshortfile)
        var flags struct {
                tagflag.StartPos
                Command string
@@ -288,7 +290,10 @@ func downloadErr(args []string, parent *tagflag.Parser) error {
        http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
                client.WriteStatus(w)
        })
-       addTorrents(client)
+       err = addTorrents(client)
+       if err != nil {
+               return fmt.Errorf("adding torrents: %w", err)
+       }
        defer outputStats(client)
        if client.WaitAll() {
                log.Print("downloaded ALL the torrents")
index 35f1cc40000b8a67f247e0ffe4ddbbf622dd48a4..66b0debfa47c4a2e0a5da0898fa509e1ac36d9aa 100644 (file)
@@ -73,7 +73,7 @@ func (fs *fileClientImpl) OpenTorrent(info *metainfo.Info, infoHash metainfo.Has
        for i, fileInfo := range upvertedFiles {
                s, err := ToSafeFilePath(append([]string{info.Name}, fileInfo.Path...)...)
                if err != nil {
-                       return nil, fmt.Errorf("file %v has unsafe path %q", i, fileInfo.Path)
+                       return nil, fmt.Errorf("file %v has unsafe path %q: %w", i, fileInfo.Path, err)
                }
                f := file{
                        path:   filepath.Join(dir, s),
index 89796ad68f2f93d37a11ce365ba36c74614ea5ce..9e50b7e3a13129610a7648188e4a746549d381d7 100644 (file)
@@ -2,7 +2,6 @@ package storage
 
 import (
        "errors"
-       "log"
        "path/filepath"
        "strings"
 )
@@ -21,7 +20,6 @@ func ToSafeFilePath(fileInfoComponents ...string) (string, error) {
        }
        safeFilePath := filepath.Join(safeComps...)
        fc := firstComponent(safeFilePath)
-       log.Printf("%q", fc)
        switch fc {
        case "..":
                return "", errors.New("escapes root dir")