From 80eee8d7eb9f75142db2d16b5ba9c40818d2f6d9 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Mon, 17 Apr 2023 08:48:54 +0200 Subject: [PATCH] updated according to code review --- all_test.go | 13 +++++-------- copy.go | 19 ++++++------------- options.go | 4 ++-- test/data/case17/README.md | 2 +- 4 files changed, 14 insertions(+), 24 deletions(-) diff --git a/all_test.go b/all_test.go index 8a36ceb..a370e44 100644 --- a/all_test.go +++ b/all_test.go @@ -364,7 +364,7 @@ func TestOptions_CopyRateLimit(t *testing.T) { func TestOptions_OnFileError(t *testing.T) { opt := Options{ - OnErr: nil, + OnError: nil, } // existing, process nromally @@ -378,18 +378,15 @@ func TestOptions_OnFileError(t *testing.T) { _, err = os.Stat("test/data.copy/case17/non-existing") Expect(t, os.IsNotExist(err)).ToBe(true) - // existing, err not passed - var called bool - opt.OnErr = func(err error) error { - called = true + // existing, nil err not passed + opt.OnError = func(_, _ string, err error) error { return err } err = Copy("test/data/case17", "test/data.copy/case17", opt) Expect(t, err).ToBe(nil) - Expect(t, called).ToBe(false) // not existing, process err - opt.OnErr = func(err error) error { return err } + opt.OnError = func(_, _ string, err error) error { return err } err = Copy("test/data/case17/non-existing", "test/data.copy/case17/non-existing", opt) Expect(t, os.IsNotExist(err)).ToBe(true) @@ -397,7 +394,7 @@ func TestOptions_OnFileError(t *testing.T) { Expect(t, os.IsNotExist(err)).ToBe(true) // not existing, ignore err - opt.OnErr = func(err error) error { return nil } + opt.OnError = func(_, _ string, err error) error { return nil } err = Copy("test/data/case17/non-existing", "test/data.copy/case17/non-existing", opt) Expect(t, err).ToBe(nil) diff --git a/copy.go b/copy.go index 84bdfba..71ff844 100644 --- a/copy.go +++ b/copy.go @@ -19,7 +19,7 @@ func Copy(src, dest string, opts ...Options) error { opt := assureOptions(src, dest, opts...) info, err := os.Lstat(src) if err != nil { - return onError(err, opt) + return onError(src, dest, err, opt) } return switchboard(src, dest, info, opt) } @@ -27,12 +27,8 @@ func Copy(src, dest string, opts ...Options) error { // switchboard switches proper copy functions regarding file type, etc... // If there would be anything else here, add a case to this switchboard. func switchboard(src, dest string, info os.FileInfo, opt Options) (err error) { - defer func() { - err = onError(err, opt) - }() - if info.Mode()&os.ModeDevice != 0 && !opt.Specials { - return err + return onError(src, dest, err, opt) } switch { @@ -46,7 +42,7 @@ func switchboard(src, dest string, info os.FileInfo, opt Options) (err error) { err = fcopy(src, dest, info, opt) } - return err + return onError(src, dest, err, opt) } // copyNextOrSkip decide if this src should be copied or not. @@ -241,13 +237,10 @@ func fclose(f *os.File, reported *error) { // onError lets caller to handle errors // occured when copying a file. -func onError(err error, opt Options) error { - if err == nil { - return nil - } - if opt.OnErr == nil { +func onError(src, dest string, err error, opt Options) error { + if opt.OnError == nil { return err } - return opt.OnErr(err) + return opt.OnError(src, dest, err) } diff --git a/options.go b/options.go index bc898fa..52d636c 100644 --- a/options.go +++ b/options.go @@ -15,7 +15,7 @@ type Options struct { OnDirExists func(src, dest string) DirExistsAction // OnErr lets called decide whether or not to continue on particular copy error. - OnErr func(err error) error + OnError func(src, dest string, err error) error // Skip can specify which files should be skipped Skip func(srcinfo os.FileInfo, src, dest string) (bool, error) @@ -98,7 +98,7 @@ func getDefaultOptions(src, dest string) Options { return Shallow // Do shallow copy }, OnDirExists: nil, // Default behavior is "Merge". - OnErr: nil, // Default is "accept error" + OnError: nil, // Default is "accept error" Skip: nil, // Do not skip anything AddPermission: 0, // Add nothing PermissionControl: PerservePermission, // Just preserve permission diff --git a/test/data/case17/README.md b/test/data/case17/README.md index 880f3b9..bfce975 100644 --- a/test/data/case17/README.md +++ b/test/data/case17/README.md @@ -1,5 +1,5 @@ So if you wanted to ignore error you should add something like this: ```go -opt.OnFileErr = func(_ error) error { return nil } +opt.OnError = func(src, dst string, _ error) error { return nil } ``` The default value is nil and accepts raised error. \ No newline at end of file