| 1 | package wrapio |
| 2 | |
| 3 | import "io" |
| 4 | |
| 5 | // DoerAt is a common interface for wrappers WriteAt or ReadAt functions |
| 6 | type DoerAt interface { |
| 7 | DoAt([]byte, int64) (int, error) |
| 8 | } |
| 9 | |
| 10 | // DoAtFunc is implemented by ReadAt/WriteAt |
| 11 | type DoAtFunc func([]byte, int64) (int, error) |
| 12 | |
| 13 | type wrapper struct { |
| 14 | off int64 |
| 15 | wrapAt int64 |
| 16 | doat DoAtFunc |
| 17 | } |
| 18 | |
| 19 | func (w *wrapper) Offset() int64 { |
| 20 | return w.off |
| 21 | } |
| 22 | |
| 23 | func (w *wrapper) Seek(offset int64, whence int) (int64, error) { |
| 24 | switch whence { |
| 25 | case 0: |
| 26 | w.off = offset |
| 27 | case 1: |
| 28 | w.off += offset |
| 29 | case 2: |
| 30 | w.off = (w.wrapAt + offset) |
| 31 | } |
| 32 | w.off %= w.wrapAt |
| 33 | return w.off, nil |
| 34 | } |
| 35 | |
| 36 | func (w *wrapper) DoAt(p []byte, off int64) (n int, err error) { |
| 37 | return w.doat(p, off) |
| 38 | } |
| 39 | |
| 40 | // WrapWriter wraps writes around a section of data. |
| 41 | type WrapWriter struct { |
| 42 | *wrapper |
| 43 | } |
| 44 | |
| 45 | // NewWrapWriter creates a WrapWriter starting at offset off, and wrapping at offset wrapAt. |
| 46 | func NewWrapWriter(w io.WriterAt, off int64, wrapAt int64) *WrapWriter { |
| 47 | return &WrapWriter{ |
| 48 | &wrapper{ |
| 49 | doat: w.WriteAt, |
| 50 | off: (off % wrapAt), |
| 51 | wrapAt: wrapAt, |
| 52 | }, |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Write writes p starting at the current offset, wrapping when it reaches the end. |
| 57 | // The current offset is shifted forward by the amount written. |
| 58 | func (w *WrapWriter) Write(p []byte) (n int, err error) { |
| 59 | n, err = Wrap(w, p, w.off, w.wrapAt) |
| 60 | w.off = (w.off + int64(n)) % w.wrapAt |
| 61 | return n, err |
| 62 | } |
| 63 | |
| 64 | // WriteAt writes p starting at offset off, wrapping when it reaches the end. |
| 65 | func (w *WrapWriter) WriteAt(p []byte, off int64) (n int, err error) { |
| 66 | return Wrap(w, p, off, w.wrapAt) |
| 67 | } |
| 68 | |
| 69 | // WrapReader wraps reads around a section of data. |
| 70 | type WrapReader struct { |
| 71 | *wrapper |
| 72 | } |
| 73 | |
| 74 | // NewWrapReader creates a WrapReader starting at offset off, and wrapping at offset wrapAt. |
| 75 | func NewWrapReader(r io.ReaderAt, off int64, wrapAt int64) *WrapReader { |
| 76 | return &WrapReader{ |
| 77 | &wrapper{ |
| 78 | doat: r.ReadAt, |
| 79 | off: (off % wrapAt), |
| 80 | wrapAt: wrapAt, |
| 81 | }, |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Read reads into p starting at the current offset, wrapping if it reaches the end. |
| 86 | // The current offset is shifted forward by the amount read. |
| 87 | func (r *WrapReader) Read(p []byte) (n int, err error) { |
| 88 | n, err = Wrap(r, p, r.off, r.wrapAt) |
| 89 | r.off = (r.off + int64(n)) % r.wrapAt |
| 90 | return n, err |
| 91 | } |
| 92 | |
| 93 | // ReadAt reads into p starting at the current offset, wrapping when it reaches the end. |
| 94 | func (r *WrapReader) ReadAt(p []byte, off int64) (n int, err error) { |
| 95 | return Wrap(r, p, off, r.wrapAt) |
| 96 | } |
| 97 | |
| 98 | // maxConsecutiveEmptyActions determines how many consecutive empty reads/writes can occur before giving up |
| 99 | const maxConsecutiveEmptyActions = 100 |
| 100 | |
| 101 | // Wrap causes an action on an array of bytes (like read/write) to be done from an offset off, |
| 102 | // wrapping at offset wrapAt. |
| 103 | func Wrap(w DoerAt, p []byte, off int64, wrapAt int64) (n int, err error) { |
| 104 | var m, fails int |
| 105 | |
| 106 | off %= wrapAt |
| 107 | |
| 108 | for len(p) > 0 { |
| 109 | |
| 110 | if off+int64(len(p)) < wrapAt { |
| 111 | m, err = w.DoAt(p, off) |
| 112 | } else { |
| 113 | space := wrapAt - off |
| 114 | m, err = w.DoAt(p[:space], off) |
| 115 | } |
| 116 | |
| 117 | if err != nil && err != io.EOF { |
| 118 | return n + m, err |
| 119 | } |
| 120 | |
| 121 | switch m { |
| 122 | case 0: |
| 123 | fails++ |
| 124 | default: |
| 125 | fails = 0 |
| 126 | } |
| 127 | |
| 128 | if fails > maxConsecutiveEmptyActions { |
| 129 | return n + m, io.ErrNoProgress |
| 130 | } |
| 131 | |
| 132 | n += m |
| 133 | p = p[m:] |
| 134 | off += int64(m) |
| 135 | off %= wrapAt |
| 136 | } |
| 137 | |
| 138 | return n, err |
| 139 | } |