findSplitPoint searches for a suitable split point in f, which is the offset of the last line feed which is prior to the split size. A split offset of zero indicates that f can't be split.
(f *os.File)
| 131 | // of the last line feed which is prior to the split size. |
| 132 | // A split offset of zero indicates that f can't be split. |
| 133 | func (w *splitWriter) findSplitPoint(f *os.File) (off int64, err error) { |
| 134 | buf := make([]byte, w.bufsize) |
| 135 | cur := w.maxsize - w.bufsize |
| 136 | |
| 137 | // Read f in reverse, starting from the split size, one buffer at a time. |
| 138 | for { |
| 139 | if _, err = f.ReadAt(buf, cur); err != nil { |
| 140 | return 0, err |
| 141 | } |
| 142 | |
| 143 | lf := 0 |
| 144 | lastlf := -1 |
| 145 | for lf != -1 { |
| 146 | lf = bytes.IndexByte(buf[lastlf+1:], '\n') |
| 147 | if lf != -1 { |
| 148 | lastlf += lf + 1 |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if lastlf != -1 { |
| 153 | // We found a suitable non-zero split offset. |
| 154 | off = cur + int64(lastlf) + 1 |
| 155 | break |
| 156 | } |
| 157 | |
| 158 | if cur == 0 { |
| 159 | return 0, nil |
| 160 | } |
| 161 | |
| 162 | // Prepare to read a new buffer |
| 163 | cur -= w.bufsize |
| 164 | if cur <= 0 { |
| 165 | cur = 0 |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return off, nil |
| 170 | } |
| 171 | |
| 172 | func doSplit(f *os.File, off int64) (*os.File, error) { |
| 173 | // Find the filename of the next part number |