Skip to content

Commit 0d94749

Browse files
S.KulishS.Kulish
authored andcommitted
HW7 is completed
1 parent 27351a3 commit 0d94749

7 files changed

Lines changed: 153 additions & 5 deletions

File tree

hw07_file_copying/.sync

Whitespace-only changes.

hw07_file_copying/copy.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,68 @@ package main
22

33
import (
44
"errors"
5+
"github.com/cheggaaa/pb"
6+
"io"
7+
"os"
58
)
69

710
var (
811
ErrUnsupportedFile = errors.New("unsupported file")
912
ErrOffsetExceedsFileSize = errors.New("offset exceeds file size")
13+
ErrTheSameFile = errors.New("you are trying to rewrite the source file")
1014
)
1115

1216
func Copy(fromPath, toPath string, offset, limit int64) error {
13-
// Place your code here.
17+
if fromPath == toPath {
18+
return ErrTheSameFile
19+
}
20+
21+
inFile, err := os.Open(fromPath)
22+
23+
if err != nil {
24+
return err
25+
}
26+
27+
fileStat, err := os.Stat(fromPath)
28+
if err != nil {
29+
return err
30+
}
31+
32+
fileSize := fileStat.Size()
33+
if fileSize < offset {
34+
return ErrOffsetExceedsFileSize
35+
}
36+
37+
defer inFile.Close()
38+
39+
if limit == 0 {
40+
limit = fileSize
41+
}
42+
43+
if limit > fileSize-offset {
44+
limit = fileSize - offset
45+
}
46+
47+
outFile, err := os.Create(toPath)
48+
49+
if err != nil {
50+
return err
51+
}
52+
53+
defer outFile.Close()
54+
55+
if offset > 0 {
56+
inFile.Seek(offset, io.SeekStart)
57+
}
58+
59+
bar := pb.StartNew(int(limit))
60+
bar.Start()
61+
62+
reader := bar.NewProxyReader(inFile)
63+
64+
io.CopyN(outFile, reader, limit)
65+
66+
bar.Finish()
67+
1468
return nil
1569
}

hw07_file_copying/copy_test.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,53 @@
11
package main
22

3-
import "testing"
3+
import (
4+
"crypto/md5"
5+
"fmt"
6+
"github.com/stretchr/testify/require"
7+
"io"
8+
"os"
9+
"testing"
10+
)
11+
12+
func FileMD5(path string) string {
13+
h := md5.New()
14+
f, err := os.Open(path)
15+
if err != nil {
16+
panic(err)
17+
}
18+
defer f.Close()
19+
_, err = io.Copy(h, f)
20+
if err != nil {
21+
panic(err)
22+
}
23+
return fmt.Sprintf("%x", h.Sum(nil))
24+
}
425

526
func TestCopy(t *testing.T) {
6-
// Place your code here.
27+
err := Copy("testdata/file_does_not_exist.txt", "/tmp/copy_offset0_limit0.txt", 0, 0)
28+
require.Error(t, err)
29+
30+
err = Copy("testdata/input.txt", "testdata/input.txt", 0, 0)
31+
require.Error(t, err)
32+
33+
err = Copy("testdata/input.txt", "/tmp/offset_is_bigger_then_file_size.txt", 100000, 0)
34+
require.Error(t, err)
35+
36+
Copy("testdata/input.txt", "/tmp/copy_offset0_limit0.txt", 0, 0)
37+
require.Equal(t, FileMD5("testdata/out_offset0_limit0.txt"), FileMD5("/tmp/copy_offset0_limit0.txt"))
38+
39+
Copy("testdata/input.txt", "/tmp/copy_offset0_limit10.txt", 0, 10)
40+
require.Equal(t, FileMD5("testdata/out_offset0_limit10.txt"), FileMD5("/tmp/copy_offset0_limit10.txt"))
41+
42+
Copy("testdata/input.txt", "/tmp/copy_offset0_limit1000.txt", 0, 1000)
43+
require.Equal(t, FileMD5("testdata/out_offset0_limit1000.txt"), FileMD5("/tmp/copy_offset0_limit1000.txt"))
44+
45+
Copy("testdata/input.txt", "/tmp/copy_offset0_limit10000.txt", 0, 10000)
46+
require.Equal(t, FileMD5("testdata/out_offset0_limit10000.txt"), FileMD5("/tmp/copy_offset0_limit10000.txt"))
47+
48+
Copy("testdata/input.txt", "/tmp/copy_offset100_limit1000.txt", 100, 1000)
49+
require.Equal(t, FileMD5("testdata/out_offset100_limit1000.txt"), FileMD5("/tmp/copy_offset100_limit1000.txt"))
50+
51+
Copy("testdata/input.txt", "/tmp/copy_offset6000_limit1000.txt", 6000, 1000)
52+
require.Equal(t, FileMD5("testdata/out_offset6000_limit1000.txt"), FileMD5("/tmp/copy_offset6000_limit1000.txt"))
753
}

hw07_file_copying/go.mod

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
module github.com/fixme_my_friend/hw07_file_copying
1+
module github.com/sofiiakulish/hw07_file_copying
22

33
go 1.16
4+
5+
require (
6+
github.com/cheggaaa/pb v1.0.29
7+
github.com/fatih/color v1.10.0 // indirect
8+
github.com/mattn/go-runewidth v0.0.12 // indirect
9+
github.com/rivo/uniseg v0.2.0 // indirect
10+
github.com/stretchr/testify v1.8.0
11+
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect
12+
)

hw07_file_copying/go.sum

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
github.com/cheggaaa/pb v1.0.29 h1:FckUN5ngEk2LpvuG0fw1GEFx6LtyY2pWI/Z2QgCnEYo=
2+
github.com/cheggaaa/pb v1.0.29/go.mod h1:W40334L7FMC5JKWldsTWbdGjLo0RxUKK73K+TuPxX30=
3+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6+
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
7+
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
8+
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
9+
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
10+
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
11+
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
12+
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
13+
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
14+
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
15+
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
16+
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
17+
github.com/mattn/go-runewidth v0.0.12 h1:Y41i/hVW3Pgwr8gV+J23B9YEY0zxjptBuCWEaxmAOow=
18+
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
19+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
20+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
21+
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
22+
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
23+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
24+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
25+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
26+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
27+
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
28+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
29+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
30+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
31+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
32+
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
33+
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 h1:nonptSpoQ4vQjyraW20DXPAglgQfVnM9ZC6MmNLMR60=
34+
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
35+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
36+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
37+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
38+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
39+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3.59 MB
Binary file not shown.

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-
// Place your code here.
21+
Copy(from, to, offset, limit)
2222
}

0 commit comments

Comments
 (0)