diff --git a/go.mod b/go.mod index 795e4eb39..66f4a3b59 100644 --- a/go.mod +++ b/go.mod @@ -80,6 +80,7 @@ require ( github.com/onsi/ginkgo v1.9.0 // indirect github.com/onsi/gomega v1.6.0 // indirect github.com/opentracing/opentracing-go v1.1.0 + github.com/otiai10/copy v1.0.2 github.com/polydawn/refmt v0.0.0-20190809202753-05966cbd336a github.com/smartystreets/assertions v1.0.1 // indirect github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect diff --git a/go.sum b/go.sum index e06831208..937485ffa 100644 --- a/go.sum +++ b/go.sum @@ -510,6 +510,11 @@ github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFSt github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw= +github.com/otiai10/copy v1.0.2 h1:DDNipYy6RkIkjMwy+AWzgKiNTyj2RUI9yEMeETEpVyc= +github.com/otiai10/copy v1.0.2/go.mod h1:c7RpqBkwMom4bYTSkLSym4VSJz/XtncWRAj/J4PEIMY= +github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE= +github.com/otiai10/mint v1.3.0 h1:Ady6MKVezQwHBkGzLFbrsywyp09Ah7rkmfjV3Bcr5uc= +github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/lib/sectorbuilder/sectorbuilder.go b/lib/sectorbuilder/sectorbuilder.go index 84d06cccf..7a5d744ab 100644 --- a/lib/sectorbuilder/sectorbuilder.go +++ b/lib/sectorbuilder/sectorbuilder.go @@ -4,7 +4,6 @@ import ( "fmt" "io" "os" - "path/filepath" "strconv" "sync" "sync/atomic" @@ -12,6 +11,7 @@ import ( sectorbuilder "github.com/filecoin-project/filecoin-ffi" "github.com/ipfs/go-datastore" logging "github.com/ipfs/go-log" + dcopy "github.com/otiai10/copy" "golang.org/x/xerrors" "github.com/filecoin-project/lotus/build" @@ -688,15 +688,15 @@ func fallbackPostChallengeCount(sectors uint64) uint64 { } func (sb *SectorBuilder) ImportFrom(osb *SectorBuilder) error { - if err := copyAllFiles(osb.cacheDir, sb.cacheDir); err != nil { + if err := dcopy.Copy(osb.cacheDir, sb.cacheDir); err != nil { return err } - if err := copyAllFiles(osb.sealedDir, sb.sealedDir); err != nil { + if err := dcopy.Copy(osb.sealedDir, sb.sealedDir); err != nil { return err } - if err := copyAllFiles(osb.stagedDir, sb.stagedDir); err != nil { + if err := dcopy.Copy(osb.stagedDir, sb.stagedDir); err != nil { return err } @@ -722,66 +722,3 @@ func (sb *SectorBuilder) SetLastSectorID(id uint64) error { sb.lastID = id return nil } - -func copyAllFiles(from, to string) error { - dir, err := os.Open(from) - if err != nil { - return err - } - - names, err := dir.Readdirnames(0) - if err != nil { - return xerrors.Errorf("failed to list items in dir: %w", err) - } - for _, n := range names { - if err := copyFile(filepath.Join(from, n), filepath.Join(to, n)); err != nil { - return xerrors.Errorf("copying file failed: %w", err) - } - } - - return nil -} - -func copyFile(from, to string) error { - st, err := os.Stat(to) - if err != nil { - if !os.IsNotExist(err) { - return xerrors.Errorf("stat of target file returned unexpected error: %w", err) - } - } - - if st != nil { - log.Warn("destination file %q already exists! skipping copy...", to) - return nil - } - - dst, err := os.Create(to) - if err != nil { - return xerrors.Errorf("failed to create target file: %w", err) - } - defer dst.Close() - - fromst, err := os.Stat(from) - if err != nil { - return err - } - - if fromst.IsDir() { - if err := os.Mkdir(to, 0755); err != nil { - return xerrors.Errorf("making target directory failed: %w", err) - } - return copyAllFiles(from, to) - } - - src, err := os.Open(from) - if err != nil { - return xerrors.Errorf("failed to open source file: %w", err) - } - defer src.Close() - - if _, err := io.Copy(dst, src); err != nil { - return xerrors.Errorf("copy failed: %w", err) - } - - return nil -}