From 66eadf35dc89f3b5e6144a145db329a48ec1e7a2 Mon Sep 17 00:00:00 2001 From: Antonio Navarro Perez Date: Thu, 18 Nov 2021 11:36:07 -0800 Subject: [PATCH] Fix problems on windows (#87) --- README.md | 8 +++----- binary_fs.go | 4 ++-- cmd/distribyted/main.go | 35 +++++++++++++++++++++++++---------- config/handler.go | 4 ++-- fuse/handler.go | 4 ++-- go.mod | 5 +++-- go.sum | 12 ++++++++++++ log/log.go | 16 ++++++++++++++++ webdav/fs.go | 7 ++----- 9 files changed, 67 insertions(+), 28 deletions(-) create mode 100644 log/log.go diff --git a/README.md b/README.md index a988959..02cda78 100644 --- a/README.md +++ b/README.md @@ -50,11 +50,9 @@ Check [here][main-url] or [here][doc-folder-url] for further documentation. Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**. -1. Fork the Project -2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`) -3. Commit your changes (`git commit -m 'Add some AmazingFeature'`) -4. Push to the Branch (`git push origin feature/AmazingFeature`) -5. Open a Pull Request +Any contribution is welcome, but some areas need more care than others: +- Windows and macOS tests and compatibility. I don't have any easy way to test distribyted on these operating systems. +- Web interface. Web development is not my _forte_. ## License diff --git a/binary_fs.go b/binary_fs.go index 2e788cb..2c7c8b7 100644 --- a/binary_fs.go +++ b/binary_fs.go @@ -2,7 +2,7 @@ package distribyted import ( "net/http" - "path" + "path/filepath" "strings" ) @@ -29,5 +29,5 @@ func (fs *binaryFileSystem) Exists(prefix string, filepath string) bool { } func (fs *binaryFileSystem) Open(name string) (http.File, error) { - return fs.fs.Open(path.Join(fs.base, name)) + return fs.fs.Open(filepath.Join(fs.base, name)) } diff --git a/cmd/distribyted/main.go b/cmd/distribyted/main.go index 9fc924a..44700bc 100644 --- a/cmd/distribyted/main.go +++ b/cmd/distribyted/main.go @@ -1,10 +1,12 @@ package main import ( + "bufio" "fmt" "os" "os/signal" - "path" + "path/filepath" + "runtime" "syscall" "time" @@ -12,20 +14,19 @@ import ( "github.com/anacrolix/torrent/storage" "github.com/distribyted/distribyted/config" "github.com/distribyted/distribyted/fs" + "github.com/rs/zerolog/log" + "github.com/urfave/cli/v2" + "github.com/distribyted/distribyted/fuse" "github.com/distribyted/distribyted/http" + dlog "github.com/distribyted/distribyted/log" "github.com/distribyted/distribyted/torrent" "github.com/distribyted/distribyted/torrent/loader" "github.com/distribyted/distribyted/webdav" - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" - "github.com/urfave/cli/v2" ) func init() { - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) - zerolog.TimeFieldFormat = zerolog.TimeFormatUnix - zerolog.SetGlobalLevel(zerolog.InfoLevel) + dlog.Load() } const ( @@ -68,6 +69,14 @@ func main() { Action: func(c *cli.Context) error { err := load(c.String(configFlag), c.Int(portFlag), c.Int(webDAVPortFlag), c.Bool(fuseAllowOther)) + + // stop program execution on errors to avoid flashing consoles + if err != nil && runtime.GOOS == "windows" { + log.Error().Err(err).Msg("problem starting application") + fmt.Print("Press 'Enter' to continue...") + bufio.NewReader(os.Stdin).ReadBytes('\n') + } + return err }, @@ -91,14 +100,20 @@ func load(configPath string, port, webDAVPort int, fuseAllowOther bool) error { return fmt.Errorf("error creating metadata folder: %w", err) } - fc, err := filecache.NewCache(path.Join(conf.Torrent.MetadataFolder, "cache")) + cf := filepath.Join(conf.Torrent.MetadataFolder, "cache") + fc, err := filecache.NewCache(cf) if err != nil { return fmt.Errorf("error creating cache: %w", err) } st := storage.NewResourcePieces(fc.AsResourceProvider()) - fis, err := torrent.NewFileItemStore(path.Join(conf.Torrent.MetadataFolder, "items"), 2*time.Hour) + // cache is not working with windows + if runtime.GOOS == "windows" { + st = storage.NewFile(cf) + } + + fis, err := torrent.NewFileItemStore(filepath.Join(conf.Torrent.MetadataFolder, "items"), 2*time.Hour) if err != nil { return fmt.Errorf("error starting item store: %w", err) } @@ -111,7 +126,7 @@ func load(configPath string, port, webDAVPort int, fuseAllowOther bool) error { cl := loader.NewConfig(conf.Routes) ss := torrent.NewStats() - dbl, err := loader.NewDB(path.Join(conf.Torrent.MetadataFolder, "magnetdb")) + dbl, err := loader.NewDB(filepath.Join(conf.Torrent.MetadataFolder, "magnetdb")) if err != nil { return fmt.Errorf("error starting magnet database: %w", err) } diff --git a/config/handler.go b/config/handler.go index 9fccf94..0c6f0f6 100644 --- a/config/handler.go +++ b/config/handler.go @@ -4,7 +4,7 @@ import ( "fmt" "io/ioutil" "os" - "path" + "path/filepath" "github.com/distribyted/distribyted" "github.com/rs/zerolog/log" @@ -34,7 +34,7 @@ func (c *Handler) createFromTemplateFile() ([]byte, error) { return nil, err } - if err := os.MkdirAll(path.Dir(c.p), 0744); err != nil { + if err := os.MkdirAll(filepath.Dir(c.p), 0744); err != nil { return nil, fmt.Errorf("error creating path for configuration file: %s, %w", c.p, err) } return tb, ioutil.WriteFile(c.p, tb, 0644) diff --git a/fuse/handler.go b/fuse/handler.go index 30e2549..33d4f62 100644 --- a/fuse/handler.go +++ b/fuse/handler.go @@ -2,7 +2,7 @@ package fuse import ( "os" - "path" + "path/filepath" "runtime" "github.com/billziss-gh/cgofuse/fuse" @@ -28,7 +28,7 @@ func (s *Handler) Mount(fss map[string]fs.Filesystem) error { folder := s.path // On windows, the folder must don't exist if runtime.GOOS == "windows" { - folder = path.Dir(s.path) + folder = filepath.Dir(s.path) } if err := os.MkdirAll(folder, 0744); err != nil && !os.IsExist(err) { return err diff --git a/go.mod b/go.mod index 7e464d3..265c9ab 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( github.com/pion/mdns v0.0.5 // indirect github.com/pion/rtp v1.7.4 // indirect github.com/pion/sctp v1.8.0 // indirect - github.com/rs/zerolog v1.25.0 + github.com/rs/zerolog v1.26.0 github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 github.com/stretchr/testify v1.7.0 @@ -72,7 +72,8 @@ require ( github.com/huandu/xstrings v1.3.2 // indirect github.com/json-iterator/go v1.1.10 // indirect github.com/klauspost/compress v1.12.3 // indirect - github.com/mattn/go-isatty v0.0.12 // indirect + github.com/mattn/go-colorable v0.1.11 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.1 // indirect github.com/mschoch/smat v0.2.0 // indirect diff --git a/go.sum b/go.sum index 130b579..d2d5aca 100644 --- a/go.sum +++ b/go.sum @@ -259,8 +259,12 @@ github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgx github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/mattn/go-colorable v0.1.11 h1:nQ+aFkoE2TMGc0b68U2OKSexC+eq46+XwZzWXHRmPYs= +github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= @@ -378,6 +382,8 @@ github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417/go.mod h1:qe5TWALJ8/a1 github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.25.0 h1:Rj7XygbUHKUlDPcVdoLyR91fJBsduXj5fRxyqIQj/II= github.com/rs/zerolog v1.25.0/go.mod h1:7KHcEGe0QZPOm2IE4Kpb5rTh6n1h2hIgS5OOnu1rUaI= +github.com/rs/zerolog v1.26.0 h1:ORM4ibhEZeTeQlCojCK2kPz1ogAY4bGs4tD+SaAdGaE= +github.com/rs/zerolog v1.26.0/go.mod h1:yBiM87lvSqX8h0Ww4sdzNSkVYZ8dL2xjZJG1lAuGZEo= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= @@ -434,6 +440,7 @@ github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1: github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU= go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4= go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk= @@ -528,7 +535,10 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211102192858-4dd72447c267 h1:7zYaz3tjChtpayGDzu6H0hDAUM5zIGA2XW7kRNgQ0jc= golang.org/x/sys v0.0.0-20211102192858-4dd72447c267/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -554,6 +564,8 @@ golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4f golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/tools v0.1.7 h1:6j8CgantCy3yc8JGBqkDLMKWqZ0RDU2g1HVgacojGWQ= +golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/log/log.go b/log/log.go new file mode 100644 index 0000000..261cd22 --- /dev/null +++ b/log/log.go @@ -0,0 +1,16 @@ +package log + +import ( + "github.com/mattn/go-colorable" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" +) + +func Load() { + // fix console colors on windows + cso := colorable.NewColorableStdout() + + log.Logger = log.Output(zerolog.ConsoleWriter{Out: cso}) + zerolog.TimeFieldFormat = zerolog.TimeFormatUnix + zerolog.SetGlobalLevel(zerolog.InfoLevel) +} diff --git a/webdav/fs.go b/webdav/fs.go index 02ae392..5327c51 100644 --- a/webdav/fs.go +++ b/webdav/fs.go @@ -4,7 +4,7 @@ import ( "context" "io" "os" - "path" + "path/filepath" "sync" "time" @@ -41,7 +41,7 @@ func (wd *WebDAV) OpenFile(ctx context.Context, name string, flag int, perm os.F dirContent = dir } - wdf := newFile(path.Base(p), f, dirContent) + wdf := newFile(filepath.Base(p), f, dirContent) return wdf, nil } @@ -158,13 +158,10 @@ func (wdf *webDAVFile) Seek(offset int64, whence int) (int64, error) { switch whence { case io.SeekStart: wdf.pos = offset - break case io.SeekCurrent: wdf.pos = wdf.pos + offset - break case io.SeekEnd: wdf.pos = wdf.fi.Size() + offset - break } return wdf.pos, nil