Close commits a complete output file atomically. It is a no-op when no Write occurred, and it discards the temporary file when a Write already failed.
()
| 696 | // Close commits a complete output file atomically. It is a no-op when no Write |
| 697 | // occurred, and it discards the temporary file when a Write already failed. |
| 698 | func (w *lazyFileWriter) Close() error { |
| 699 | if w.closed { |
| 700 | return w.closeErr |
| 701 | } |
| 702 | w.closed = true |
| 703 | if w.file == nil { |
| 704 | return nil |
| 705 | } |
| 706 | if w.writeErr != nil { |
| 707 | w.closeErr = cleanupOutputTemp(w.file, w.tempPath) |
| 708 | w.file = nil |
| 709 | w.tempPath = "" |
| 710 | return w.closeErr |
| 711 | } |
| 712 | if err := w.file.Sync(); err != nil { |
| 713 | cleanupErr := cleanupOutputTemp(w.file, w.tempPath) |
| 714 | w.file = nil |
| 715 | w.tempPath = "" |
| 716 | w.closeErr = errors.Join(fmt.Errorf("sync output file %s: %w", w.path, err), cleanupErr) |
| 717 | return w.closeErr |
| 718 | } |
| 719 | if err := w.file.Close(); err != nil { |
| 720 | cleanupErr := cleanupOutputTemp(w.file, w.tempPath) |
| 721 | w.file = nil |
| 722 | w.tempPath = "" |
| 723 | w.closeErr = errors.Join(fmt.Errorf("close output file %s: %w", w.path, err), cleanupErr) |
| 724 | return w.closeErr |
| 725 | } |
| 726 | w.file = nil |
| 727 | if err := os.Rename(w.tempPath, w.commitPath); err != nil { |
| 728 | cleanupErr := cleanupOutputTemp(nil, w.tempPath) |
| 729 | w.tempPath = "" |
| 730 | w.closeErr = errors.Join(fmt.Errorf("replace output file %s: %w", w.path, err), cleanupErr) |
| 731 | return w.closeErr |
| 732 | } |
| 733 | w.tempPath = "" |
| 734 | fmt.Fprintf(os.Stderr, "[ocr] Results written to %s\n", w.path) |
| 735 | return nil |
| 736 | } |
| 737 | |
| 738 | // resolveOutputWriter resolves the --output target into a writer plus a |
| 739 | // cleanup function. |
nothing calls this directly
no test coverage detected