Skip to content

Commit 255cfff

Browse files
S.KulishS.Kulish
authored andcommitted
code review fixes
1 parent 0d94749 commit 255cfff

3 files changed

Lines changed: 24 additions & 16 deletions

File tree

hw07_file_copying/copy.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package main
22

33
import (
44
"errors"
5+
"fmt"
56
"github.com/cheggaaa/pb"
67
"io"
78
"os"
89
)
910

1011
var (
11-
ErrUnsupportedFile = errors.New("unsupported file")
12+
// ErrUnsupportedFile = errors.New("unsupported file")
1213
ErrOffsetExceedsFileSize = errors.New("offset exceeds file size")
1314
ErrTheSameFile = errors.New("you are trying to rewrite the source file")
1415
)
@@ -19,23 +20,22 @@ func Copy(fromPath, toPath string, offset, limit int64) error {
1920
}
2021

2122
inFile, err := os.Open(fromPath)
22-
2323
if err != nil {
24-
return err
24+
return fmt.Errorf("open input file: %w", err)
2525
}
2626

27+
defer inFile.Close()
28+
2729
fileStat, err := os.Stat(fromPath)
2830
if err != nil {
29-
return err
31+
return fmt.Errorf("read input file info: %w", err)
3032
}
3133

3234
fileSize := fileStat.Size()
3335
if fileSize < offset {
3436
return ErrOffsetExceedsFileSize
3537
}
3638

37-
defer inFile.Close()
38-
3939
if limit == 0 {
4040
limit = fileSize
4141
}
@@ -45,9 +45,8 @@ func Copy(fromPath, toPath string, offset, limit int64) error {
4545
}
4646

4747
outFile, err := os.Create(toPath)
48-
4948
if err != nil {
50-
return err
49+
return fmt.Errorf("open output file: %w", err)
5150
}
5251

5352
defer outFile.Close()
@@ -61,7 +60,10 @@ func Copy(fromPath, toPath string, offset, limit int64) error {
6160

6261
reader := bar.NewProxyReader(inFile)
6362

64-
io.CopyN(outFile, reader, limit)
63+
_, err = io.CopyN(outFile, reader, limit)
64+
if err != nil {
65+
return fmt.Errorf("copy data to file: %w", err)
66+
}
6567

6668
bar.Finish()
6769

hw07_file_copying/copy_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,27 @@ func TestCopy(t *testing.T) {
3333
err = Copy("testdata/input.txt", "/tmp/offset_is_bigger_then_file_size.txt", 100000, 0)
3434
require.Error(t, err)
3535

36-
Copy("testdata/input.txt", "/tmp/copy_offset0_limit0.txt", 0, 0)
36+
err = Copy("testdata/input.txt", "/tmp/copy_offset0_limit0.txt", 0, 0)
37+
require.NoError(t, err)
3738
require.Equal(t, FileMD5("testdata/out_offset0_limit0.txt"), FileMD5("/tmp/copy_offset0_limit0.txt"))
3839

39-
Copy("testdata/input.txt", "/tmp/copy_offset0_limit10.txt", 0, 10)
40+
err = Copy("testdata/input.txt", "/tmp/copy_offset0_limit10.txt", 0, 10)
41+
require.NoError(t, err)
4042
require.Equal(t, FileMD5("testdata/out_offset0_limit10.txt"), FileMD5("/tmp/copy_offset0_limit10.txt"))
4143

42-
Copy("testdata/input.txt", "/tmp/copy_offset0_limit1000.txt", 0, 1000)
44+
err = Copy("testdata/input.txt", "/tmp/copy_offset0_limit1000.txt", 0, 1000)
45+
require.NoError(t, err)
4346
require.Equal(t, FileMD5("testdata/out_offset0_limit1000.txt"), FileMD5("/tmp/copy_offset0_limit1000.txt"))
4447

45-
Copy("testdata/input.txt", "/tmp/copy_offset0_limit10000.txt", 0, 10000)
48+
err = Copy("testdata/input.txt", "/tmp/copy_offset0_limit10000.txt", 0, 10000)
49+
require.NoError(t, err)
4650
require.Equal(t, FileMD5("testdata/out_offset0_limit10000.txt"), FileMD5("/tmp/copy_offset0_limit10000.txt"))
4751

48-
Copy("testdata/input.txt", "/tmp/copy_offset100_limit1000.txt", 100, 1000)
52+
err = Copy("testdata/input.txt", "/tmp/copy_offset100_limit1000.txt", 100, 1000)
53+
require.NoError(t, err)
4954
require.Equal(t, FileMD5("testdata/out_offset100_limit1000.txt"), FileMD5("/tmp/copy_offset100_limit1000.txt"))
5055

51-
Copy("testdata/input.txt", "/tmp/copy_offset6000_limit1000.txt", 6000, 1000)
56+
err = Copy("testdata/input.txt", "/tmp/copy_offset6000_limit1000.txt", 6000, 1000)
57+
require.NoError(t, err)
5258
require.Equal(t, FileMD5("testdata/out_offset6000_limit1000.txt"), FileMD5("/tmp/copy_offset6000_limit1000.txt"))
5359
}

hw07_file_copying/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ func init() {
1818

1919
func main() {
2020
flag.Parse()
21-
Copy(from, to, offset, limit)
21+
Copy(from, to, offset, limit)
2222
}

0 commit comments

Comments
 (0)