diff options
author | Santiago M. Mola <santi@mola.io> | 2016-11-11 16:31:04 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-11-11 16:31:04 +0100 |
commit | 3e7f535beae601d334186f7316af957bc24bd865 (patch) | |
tree | 4f1fb5da0c743070b939bd1edfe31157009c915f /utils | |
parent | ac095bb12c4d29722b60ba9f20590fa7cfa6bc7d (diff) | |
download | go-git-3e7f535beae601d334186f7316af957bc24bd865.tar.gz |
utils/fs: add ReadAt to memory file and tests. (#122)
* memory files now implement io.ReaderAt.
* tests now check ReadAt behaviour.
Diffstat (limited to 'utils')
-rw-r--r-- | utils/fs/memory/memory.go | 12 | ||||
-rw-r--r-- | utils/fs/os/os.go | 2 | ||||
-rw-r--r-- | utils/fs/test/fs_suite.go | 34 |
3 files changed, 46 insertions, 2 deletions
diff --git a/utils/fs/memory/memory.go b/utils/fs/memory/memory.go index 08a9599..ef54c37 100644 --- a/utils/fs/memory/memory.go +++ b/utils/fs/memory/memory.go @@ -189,6 +189,16 @@ func newFile(base, fullpath string, flag int) *file { } func (f *file) Read(b []byte) (int, error) { + n, err := f.ReadAt(b, f.position) + if err != nil { + return 0, err + } + + f.position += int64(n) + return n, err +} + +func (f *file) ReadAt(b []byte, off int64) (int, error) { if f.IsClosed() { return 0, fs.ErrClosed } @@ -197,7 +207,7 @@ func (f *file) Read(b []byte) (int, error) { return 0, errors.New("read not supported") } - n, err := f.content.ReadAt(b, f.position) + n, err := f.content.ReadAt(b, off) f.position += int64(n) return n, err diff --git a/utils/fs/os/os.go b/utils/fs/os/os.go index 4f4e651..5f9df67 100644 --- a/utils/fs/os/os.go +++ b/utils/fs/os/os.go @@ -178,6 +178,6 @@ func (f *osFile) Close() error { return f.file.Close() } -func (f *osFile) ReadAt(p []byte, off int64) (n int, err error) { +func (f *osFile) ReadAt(p []byte, off int64) (int, error) { return f.file.ReadAt(p, off) } diff --git a/utils/fs/test/fs_suite.go b/utils/fs/test/fs_suite.go index 8c413bf..5aa99e6 100644 --- a/utils/fs/test/fs_suite.go +++ b/utils/fs/test/fs_suite.go @@ -379,3 +379,37 @@ func (s *FilesystemSuite) TestJoin(c *C) { func (s *FilesystemSuite) TestBase(c *C) { c.Assert(s.Fs.Base(), Not(Equals), "") } + +func (s *FilesystemSuite) TestReadAtOnReadWrite(c *C) { + f, err := s.Fs.Create("foo") + c.Assert(err, IsNil) + _, err = f.Write([]byte("abcdefg")) + c.Assert(err, IsNil) + rf, ok := f.(io.ReaderAt) + c.Assert(ok, Equals, true) + b := make([]byte, 3) + n, err := rf.ReadAt(b, 2) + c.Assert(err, IsNil) + c.Assert(n, Equals, 3) + c.Assert(string(b), Equals, "cde") + c.Assert(f.Close(), IsNil) +} + +func (s *FilesystemSuite) TestReadAtOnReadOnly(c *C) { + f, err := s.Fs.Create("foo") + c.Assert(err, IsNil) + _, err = f.Write([]byte("abcdefg")) + c.Assert(err, IsNil) + c.Assert(f.Close(), IsNil) + + f, err = s.Fs.Open("foo") + c.Assert(err, IsNil) + rf, ok := f.(io.ReaderAt) + c.Assert(ok, Equals, true) + b := make([]byte, 3) + n, err := rf.ReadAt(b, 2) + c.Assert(err, IsNil) + c.Assert(n, Equals, 3) + c.Assert(string(b), Equals, "cde") + c.Assert(f.Close(), IsNil) +} |