Skip to content

Commit

Permalink
Add WrapReader, Fix #89
Browse files Browse the repository at this point in the history
  • Loading branch information
otiai10 committed Oct 25, 2022
1 parent 155df18 commit afcdb2a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 53 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ coverage.txt
vendor
.vagrant
.idea/

# Test Specific
test/data/case16/large.file
31 changes: 24 additions & 7 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package copy

import (
"errors"
"io"
"io/ioutil"
"math"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -330,26 +330,43 @@ func TestOptions_PreserveOwner(t *testing.T) {
}

func TestOptions_CopyRateLimit(t *testing.T) {
opt := Options{CopyRateLimit: 50} // 50 KB/s
size := int64(100 * 1024) // 100 KB
costSec := int64(2) // 2 seconds

file, err := os.Create("test/data/case16/large.file")
if err != nil {
t.Errorf("failed to create test file: %v", err)
return
}

size := int64(100 * 1024) // 100 KB
if err := file.Truncate(size); err != nil {
t.Errorf("failed to truncate test file: %v", err)
t.SkipNow()
return
}

opt := Options{WrapReader: func(src *os.File) io.Reader {
return &SleepyReader{src, 1}
}}

start := time.Now()
err = Copy("test/data/case16", "test/data.copy/case16", opt)
copyCost := time.Since(start)
elasped := time.Since(start)
Expect(t, err).ToBe(nil)
t.Log("copy cost", copyCost)
Expect(t, int64(math.Floor(copyCost.Seconds()+0.5)) == costSec).ToBe(true)
Expect(t, elasped > 5*time.Second).ToBe(true)
}

type SleepyReader struct {
src *os.File
sec time.Duration
}

func (r *SleepyReader) Read(p []byte) (int, error) {
n, e := r.src.Read(p)
if e != nil && e != io.EOF {
return n, e
}
if n > 0 {
time.Sleep(time.Second * r.sec)
}
return n, e
}
6 changes: 3 additions & 3 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ func fcopy(src, dest string, info os.FileInfo, opt Options) (err error) {

var buf []byte = nil
var w io.Writer = f

var r io.Reader = s
if opt.CopyRateLimit > 0 {
r = NewRateLimitedReader(r, opt.CopyRateLimit)

if opt.WrapReader != nil {
r = opt.WrapReader(s)
}

if opt.CopyBufferSize != 0 {
Expand Down
14 changes: 9 additions & 5 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package copy

import "os"
import (
"io"
"os"
)

// Options specifies optional actions on copying.
type Options struct {
Expand Down Expand Up @@ -46,9 +49,10 @@ type Options struct {
// See https://golang.org/pkg/io/#CopyBuffer for more information.
CopyBufferSize uint

// Limit the rate of copying files with n KB per second.
// If zero, will not limit the copy operation.
CopyRateLimit int64
// If you want to add some limitation on reading src file,
// you can wrap the src and provide new reader,
// such as `RateLimitReader` in the test case.
WrapReader func(src *os.File) io.Reader

intent struct {
src string
Expand Down Expand Up @@ -96,7 +100,7 @@ func getDefaultOptions(src, dest string) Options {
Sync: false, // Do not sync
PreserveTimes: false, // Do not preserve the modification time
CopyBufferSize: 0, // Do not specify, use default bufsize (32*1024)
CopyRateLimit: 0, // Do not specify, use default rate (unlimited)
WrapReader: nil, // Do not wrap src files, use them as they are.
intent: struct {
src string
dest string
Expand Down
38 changes: 0 additions & 38 deletions ratelimited_reader.go

This file was deleted.

1 change: 1 addition & 0 deletions test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

func setup(m *testing.M) {
os.RemoveAll("test/data.copy")
os.MkdirAll("test/data.copy", os.ModePerm)
os.Symlink("test/data/case01", "test/data/case03/case01")
os.Chmod("test/data/case07/dir_0555", 0o555)
Expand Down

0 comments on commit afcdb2a

Please sign in to comment.