From 8749e2c650a9d52dbd20d11ca9bea0e1aa1ac0be Mon Sep 17 00:00:00 2001 From: Hiromu OCHIAI Date: Wed, 26 Oct 2022 15:36:08 +0900 Subject: [PATCH 1/2] [Breaking Change] Skip now accepts FileInfo Close #40 --- README.md | 4 ++-- all_test.go | 4 ++-- copy.go | 14 ++++++++------ example_test.go | 2 +- options.go | 8 +++----- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 9335dc2..32edfd8 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ type Options struct { OnDirExists func(src, dest string) DirExistsAction // Skip can specify which files should be skipped - Skip func(src string) (bool, error) + Skip func(srcinfo os.FileInfo, src, dest string) (bool, error) // PermissionControl can control permission of // every entry. @@ -80,7 +80,7 @@ type Options struct { ```go // For example... opt := Options{ - Skip: func(src string) (bool, error) { + Skip: func(info os.FileInfo, src, dest string) (bool, error) { return strings.HasSuffix(src, ".git"), nil }, } diff --git a/all_test.go b/all_test.go index 214647c..6154964 100644 --- a/all_test.go +++ b/all_test.go @@ -152,7 +152,7 @@ func TestOptions_OnSymlink(t *testing.T) { } func TestOptions_Skip(t *testing.T) { - opt := Options{Skip: func(src string) (bool, error) { + opt := Options{Skip: func(info os.FileInfo, src, dest string) (bool, error) { switch { case strings.HasSuffix(src, "_skip"): return true, nil @@ -186,7 +186,7 @@ func TestOptions_Skip(t *testing.T) { Because(t, "if Skip func returns error, Copy should be interrupted", func(t *testing.T) { errInsideSkipFunc := errors.New("Something wrong inside Skip") - opt := Options{Skip: func(src string) (bool, error) { + opt := Options{Skip: func(info os.FileInfo, src, dest string) (bool, error) { return false, errInsideSkipFunc }} err := Copy("test/data/case06", "test/data.copy/case06.01", opt) diff --git a/copy.go b/copy.go index 556c2b6..58a93ab 100644 --- a/copy.go +++ b/copy.go @@ -49,12 +49,14 @@ func switchboard(src, dest string, info os.FileInfo, opt Options) (err error) { // Because this "copy" could be called recursively, // "info" MUST be given here, NOT nil. func copyNextOrSkip(src, dest string, info os.FileInfo, opt Options) error { - skip, err := opt.Skip(src) - if err != nil { - return err - } - if skip { - return nil + if opt.Skip != nil { + skip, err := opt.Skip(info, src, dest) + if err != nil { + return err + } + if skip { + return nil + } } return switchboard(src, dest, info, opt) } diff --git a/example_test.go b/example_test.go index 88cc590..d097857 100644 --- a/example_test.go +++ b/example_test.go @@ -24,7 +24,7 @@ func ExampleOptions() { "test/data/example", "test/data.copy/example_with_options", Options{ - Skip: func(src string) (bool, error) { + Skip: func(info os.FileInfo, src, dest string) (bool, error) { return strings.HasSuffix(src, ".git-like"), nil }, OnSymlink: func(src string) SymlinkAction { diff --git a/options.go b/options.go index a020e7a..bd18d56 100644 --- a/options.go +++ b/options.go @@ -15,7 +15,7 @@ type Options struct { OnDirExists func(src, dest string) DirExistsAction // Skip can specify which files should be skipped - Skip func(src string) (bool, error) + Skip func(srcinfo os.FileInfo, src, dest string) (bool, error) // Specials includes special files to be copied. default false. Specials bool @@ -94,10 +94,8 @@ func getDefaultOptions(src, dest string) Options { OnSymlink: func(string) SymlinkAction { return Shallow // Do shallow copy }, - OnDirExists: nil, // Default behavior is "Merge". - Skip: func(string) (bool, error) { - return false, nil // Don't skip - }, + OnDirExists: nil, // Default behavior is "Merge". + Skip: nil, // Do not skip anything AddPermission: 0, // Add nothing PermissionControl: PerservePermission, // Just preserve permission Sync: false, // Do not sync From fa129b100b493a2d0c9a081c68936b91d6c6f4cf Mon Sep 17 00:00:00 2001 From: Hiromu OCHIAI Date: Wed, 9 Nov 2022 12:10:43 +0900 Subject: [PATCH 2/2] Fix for fmt and gocyclo --- all_test.go | 4 ++-- copy.go | 33 +++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/all_test.go b/all_test.go index 6154964..910570a 100644 --- a/all_test.go +++ b/all_test.go @@ -357,9 +357,9 @@ func TestOptions_CopyRateLimit(t *testing.T) { start := time.Now() err = Copy("test/data/case16", "test/data.copy/case16", opt) - elasped := time.Since(start) + elapsed := time.Since(start) Expect(t, err).ToBe(nil) - Expect(t, elasped > 5*time.Second).ToBe(true) + Expect(t, elapsed > 5*time.Second).ToBe(true) } type SleepyReader struct { diff --git a/copy.go b/copy.go index 58a93ab..60643dd 100644 --- a/copy.go +++ b/copy.go @@ -131,18 +131,10 @@ func fcopy(src, dest string, info os.FileInfo, opt Options) (err error) { // and pass everything to "copy" recursively. func dcopy(srcdir, destdir string, info os.FileInfo, opt Options) (err error) { - _, err = os.Stat(destdir) - if err == nil && opt.OnDirExists != nil && destdir != opt.intent.dest { - switch opt.OnDirExists(srcdir, destdir) { - case Replace: - if err := os.RemoveAll(destdir); err != nil { - return err - } - case Untouchable: - return nil - } // case "Merge" is default behaviour. Go through. - } else if err != nil && !os.IsNotExist(err) { - return err // Unwelcome error type...! + if skip, err := onDirExists(opt, srcdir, destdir); err != nil { + return err + } else if skip { + return nil } // Make dest dir with 0755 so that everything writable. @@ -181,6 +173,23 @@ func dcopy(srcdir, destdir string, info os.FileInfo, opt Options) (err error) { return } +func onDirExists(opt Options, srcdir, destdir string) (bool, error) { + _, err := os.Stat(destdir) + if err == nil && opt.OnDirExists != nil && destdir != opt.intent.dest { + switch opt.OnDirExists(srcdir, destdir) { + case Replace: + if err := os.RemoveAll(destdir); err != nil { + return false, err + } + case Untouchable: + return true, nil + } // case "Merge" is default behaviour. Go through. + } else if err != nil && !os.IsNotExist(err) { + return true, err // Unwelcome error type...! + } + return false, nil +} + func onsymlink(src, dest string, opt Options) error { switch opt.OnSymlink(src) { case Shallow: