doFirstSplit creates the first splits of f, at offset off. That is, from 'file' it creates both 'file.part-1' and 'file.part-2' and then removes 'file'. It returns the last split.
(f *os.File, dir, fname, next1 string, off int64)
| 223 | // then removes 'file'. |
| 224 | // It returns the last split. |
| 225 | func doFirstSplit(f *os.File, dir, fname, next1 string, off int64) (*os.File, error) { |
| 226 | next1path := filepath.Join(dir, next1) |
| 227 | next2, _, _ := nextSplit(next1) |
| 228 | next2path := filepath.Join(dir, next2) |
| 229 | |
| 230 | // Open the 2 first parts |
| 231 | f1, err := open(next1path) |
| 232 | if err != nil { |
| 233 | closeFiles(f) |
| 234 | return nil, err |
| 235 | } |
| 236 | f2, err := open(next2path) |
| 237 | if err != nil { |
| 238 | closeFiles(f, f1) |
| 239 | return nil, err |
| 240 | } |
| 241 | |
| 242 | // Split the original file into 2 parts |
| 243 | if _, err := f.Seek(0, io.SeekStart); err != nil { |
| 244 | closeFiles(f, f1, f2) |
| 245 | return nil, err |
| 246 | } |
| 247 | |
| 248 | if _, err := io.CopyN(f1, f, off); err != nil { |
| 249 | closeFiles(f, f1, f2) |
| 250 | return nil, err |
| 251 | } |
| 252 | |
| 253 | if _, err := io.Copy(f2, f); err != nil { |
| 254 | closeFiles(f, f1, f2) |
| 255 | return nil, err |
| 256 | } |
| 257 | |
| 258 | // Close the 2 parts and remove the original file |
| 259 | if err := f.Close(); err != nil { |
| 260 | closeFiles(f1, f2) |
| 261 | return nil, err |
| 262 | } |
| 263 | if err := f1.Close(); err != nil { |
| 264 | closeFiles(f2) |
| 265 | return nil, err |
| 266 | } |
| 267 | if err := os.Remove(filepath.Join(dir, fname)); err != nil { |
| 268 | closeFiles(f2) |
| 269 | return nil, err |
| 270 | } |
| 271 | return f2, nil |
| 272 | } |
| 273 | |
| 274 | var splitFnameRx = regexp.MustCompile(`(\S+)-part-(\d+)(.*)`) |
| 275 |