| giolekva | 1f6577a | 2020-03-25 12:53:06 +0400 | [diff] [blame] | 1 | package chunk |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "os" |
| 6 | |
| giolekva | d2a029d | 2020-03-25 23:06:08 +0400 | [diff] [blame] | 7 | "github.com/giolekva/pcloud/api" |
| giolekva | 1f6577a | 2020-03-25 12:53:06 +0400 | [diff] [blame] | 8 | ) |
| 9 | |
| 10 | type ReadOnlyFileChunk struct { |
| 11 | f *os.File |
| 12 | offset int |
| 13 | size int |
| 14 | } |
| 15 | |
| 16 | func NewReadOnlyFileChunk(f *os.File, offset, size int) Chunk { |
| 17 | return &ReadOnlyFileChunk{f, offset, size} |
| 18 | } |
| 19 | |
| 20 | func (c *ReadOnlyFileChunk) Stats() (ChunkInfo, error) { |
| 21 | return ChunkInfo{ |
| 22 | Status: api.ChunkStatus_READY, |
| 23 | Size: c.size, |
| 24 | Committed: c.size}, nil |
| 25 | } |
| 26 | |
| 27 | func (c *ReadOnlyFileChunk) ReaderAt() io.ReaderAt { |
| 28 | return &fileReader{c.f} |
| 29 | } |
| 30 | |
| 31 | func (c *ReadOnlyFileChunk) WriterAt() io.WriterAt { |
| 32 | return &fileWriter{c.f} |
| 33 | } |
| 34 | |
| 35 | type fileReader struct { |
| 36 | f *os.File |
| 37 | } |
| 38 | |
| 39 | func (f *fileReader) ReadAt(b []byte, offset int64) (int, error) { |
| 40 | return f.f.ReadAt(b, offset) |
| 41 | } |
| 42 | |
| 43 | type fileWriter struct { |
| 44 | f *os.File |
| 45 | } |
| 46 | |
| 47 | func (f *fileWriter) WriteAt(b []byte, offset int64) (int, error) { |
| 48 | return f.f.WriteAt(b, offset) |
| 49 | } |